Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Help with spatial resolution conversion programming (bmp image)

  1. Sep 27, 2015 #1
    Hello guys. I want to ask something about simple spatial resolution manipulating just by using c language. I have done my programming below, it managed to be compiled but for some reasons the program stucked in the middle when I try to run it. Really hope you guys can help. I am extremely beginner on this.

    Code (C):

    #define        width    640
    #define        height    581

    int main(void)
        FILE *fp;    
        unsignedchar header[54];                    
        unsignedchar img_work[width][height][3];  
        char input_file[128],output_file[128];  
        int v, h, w, i, c, s, ave_w[width], ave_h[height], average_h, average_w;

        /*------------Reading image------------*/
        printf("Enter name of the file¥n---");

        printf("The file that would be processed is %s.¥n", input_file);


        /*------------Spatial Resolution Program------------*/
        printf ("enter level of spatialization-- ");
        scanf ("%d", &v);

        for (i=0; i<v; i++) {
            s = s + s;

        for(c=0; c<3; c++){

            for(h=0; h<height; h++){

                for(w=0; w<width; w=w+s){
                    average_w = 0;

                    for (i=0; i<s; i++) {
                        ave_w[i] = img_work[w+i][h][c] / s;
                        average_w = average_w + ave_w;

                    for (i=0; i<width; i=i+s) {
                        img_work[w+i][h][c] = average_w;

        for(c=0; c<3; c++) {

            for(w=0; w<width; w++) {

                for(h=0; h<height; h=h+s) {
                    average_h = 0;

                    for (i=0; i<s; i++) {
                        ave_h[i] = img_work[w][h+i][c] / s;
                        average_h = average_h + ave_h[i];

                    for (i=0; i<height; i=i+s) {
                        img_work[w][h+i][c] = average_h;

        /*------------Writing File------------*/
        printf("Enter the name of the file that would be saved.¥n---");

        printf("Name of the file that would be saved is %s.¥n",output_file);


        return 0;
    I am really a beginner, so, sorry if it is lacking too much.

    [ EDIT ] Listing edited by mentor for better readability using code tags
    Last edited by a moderator: Sep 27, 2015
  2. jcsd
  3. Sep 27, 2015 #2


    Staff: Mentor

    1. Your program is horribly mangled due to the way that the browser renders some array notation that uses i as an index. The browser interprets [i] is the starting tag for italics. One workaround is to use a different index variable, say j, instead of i. Also, some other characters cannot be used -- [b] is the start tag for rendering boldface type, and [s] is the start tag for text that is lined out.

    2. When you post your code, please post it with a [ code=c ] tag at the beginning and a [ /code ] tag at the end. (Don't include the extra spaces -- I omitted them so that the browser will show the tags.)
    Your code should look something like this:
    Code (C):

    #include <stdio.h>
    #define width 640
    #define height 581
    int main(void)
       // Body of main
    3. "stucked in the middle" doesn't give us much help, besides which "stucked" is not a word in English. Are there any run-time errors? What is the last line of code that produces output? If you don't know how to use a debugger, a simple alternative is to add printf() statements that show what part of the code is running. Although we can probably figure out why your program isn't working given enough time, most of us won't want to spend the time trying to do so. Help us out by providing as much information as possible about the problem you're having.
  4. Sep 27, 2015 #3


    Staff: Mentor

    By stuck in the middle you mean its stuck in the for loops, right?

    My suggestion is to add print statements inside each loop so you know what index they are on...

    as an example in the inner most loop you could print out the value of each index and the value of the loop terminating variables.

    In particular the 's' one is suspect and may be very large.

    If you have access to a debugger then the print statements wont be needed as the debugger can help you to step through your code and see where things are going wrong. You have to start thinking like a computer to find your bugs.

    Lastly, please follow Mark44's advice as it was very painful for me to adjust your listing.
  5. Sep 27, 2015 #4

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    You are not setting the variable s before you first use it. In C, failing to initialize an automatic variable such as your variable s results in undefined behavior. I suspect that what's happening is that your variable s is initialized to zero. That is not something you should ever depend upon. In this case, that zero initialization is making the loop for(i=0; i<height; i=i+s) {...} loop forever. The increment instruction i=i+s has zero effect because s is zero.
  6. Oct 2, 2015 #5
    DH mentioned the killer issue (uninitialized "s", which probably is a negative number and results in an infinite loop). Once that's fixed and the program runs correctly, you'll suffer from poor output quality due to the use of 8-bit numbers in the calculations which leads to various overflows and truncations. There are also initialized but unused arrays (ave_w and ave_h). Remove them and also the " / s" in the s loops so that the code reads "average_w += img_work[w+i][h][c];" then after the loop add "average_w /= s;".

    One final comment. Is this an attempt at optimizing a separable image filter? If so, I'd stick with a straightforward implementation so that you can get a known good result and worry about optimization later. You might not even have to worry about optimizing if it's rarely used code (the image is fairly small and the user interacts via the keyboard, so they can't expect zippy performance).
  7. Oct 21, 2015 #6
    Sorry for the late reply. I have managed to solve the problem. Sorry for the bad code up here. I've post it when I was really really busy back then...
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Help with spatial resolution conversion programming (bmp image)
  1. BMP in c (Replies: 9)

  2. Help with programming (Replies: 5)