Help with spatial resolution conversion programming (bmp image)

AI Thread Summary
The discussion revolves around a C program aimed at manipulating spatial resolution in images. The user, a beginner, encounters issues with the program freezing during execution. Key points include the need for proper initialization of variables, particularly the variable 's', which is critical for loop control. The uninitialized 's' likely leads to infinite loops, causing the program to hang. Suggestions are made to add print statements for debugging and to use a debugger for step-by-step analysis. Additionally, the code contains inefficiencies, such as unnecessary calculations and unused variables, which should be addressed for better performance. The importance of clear and readable code is emphasized, along with the recommendation to focus on a straightforward implementation before considering optimizations. The user later reports successfully resolving the issue.
hilman
Messages
17
Reaction score
0
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.

C:
#include<stdio.h>
#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---");
    scanf("%s",input_file);

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

    fp=fopen(input_file,"rb");     
    fread(header,1,54,fp); 
    fread(img_work,1,width*height*3,fp); 
    fclose(fp);

    /*------------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---");
    scanf("%s",output_file);

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

    fp=fopen(output_file,"wb");
    fwrite(header,1,54,fp);
    fwrite(img_work,1,width*height*3,fp); 
    fclose(fp);

    printf("End.¥n");
    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:
Technology news on Phys.org
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 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 -- is the start tag for rendering boldface type, and 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:
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.
 
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 won't 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.
 
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.
 
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).
 
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...
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
4
Views
3K
Replies
75
Views
6K
Replies
22
Views
5K
Replies
10
Views
10K
Replies
4
Views
2K
Replies
1
Views
2K
Replies
2
Views
4K
Back
Top