Help with spatial resolution conversion programming (bmp image)

Click For Summary
SUMMARY

The forum discussion focuses on a C programming issue related to spatial resolution manipulation of BMP images. The user encountered a problem where their program hangs during execution, primarily due to the uninitialized variable 's', leading to infinite loops. Key recommendations include initializing 's' before use, adding print statements for debugging, and simplifying the averaging calculations to avoid overflow issues. The discussion also emphasizes the importance of providing clear error descriptions to facilitate troubleshooting.

PREREQUISITES
  • Understanding of C programming language syntax and structure
  • Familiarity with BMP image file format and its header structure
  • Knowledge of debugging techniques in C, including the use of print statements
  • Basic concepts of image processing, particularly spatial resolution manipulation
NEXT STEPS
  • Learn about BMP file format specifications and how to manipulate image data in C
  • Research C debugging techniques, including using gdb or integrated development environments (IDEs)
  • Explore image processing algorithms, focusing on separable filters and averaging techniques
  • Investigate memory management in C to prevent overflow and undefined behavior
USEFUL FOR

Beginner C programmers, image processing enthusiasts, and developers looking to manipulate BMP images and troubleshoot common programming issues.

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...
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 75 ·
3
Replies
75
Views
6K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 22 ·
Replies
22
Views
6K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 10 ·
Replies
10
Views
10K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K