1

im new to programming so this may have a simple solution. Heres what im trying to do.

My program loads a .bmp image and then takes the width and height to find out how many pixels the image has. Then it uses calloc() to make three arrays for the RGB values of each pixel, theres the array for RED, BLUE, GREEN. I have used a while() loop to take the first pixel and put the RGB values in the first element of their respective arrays. Then it moves on and does the same for the second pixel, third, fourth... This is where my issue is, it doesnt seem to want to put the values in the arrays. I did it like this,

while(temp=fgetc(fp) != NULL)
{
BLUE[current_pixel]=temp;

temp=fgetc(fp);
GREEN[current_pixel]=temp;

temp=fgetc(fp);
RED[current_pixel]=temp;

current_pixel++;
}

current_pixel is a variable that keeps track of what pixel Im looking at right now.

So I guess what I really want to know is why can't i do BLUE[current_pixel]=temp; I don't recieve any errors when compiling and I used a printf() statement to check where the problem is.

I have tried BLUE[1]=temp and it works fine, but thats no good for my program because I can't move on to the next pixel to save values.

Thank you in advance for any help!

EDIT: I still cant get it working so im just going to post the entire program here.

#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE*fp;
int current_pixel=0;
int temp=0;
int paddingremoved=0;
int cycle=0;
int *RED;
int *BLUE;
int *GREEN;
int imgstrt=0;
int width=0;
int height=0;
int padding=0;

fp=fopen("C:\\Users\\Jason\\Documents\\test.bmp","rb");

if(fp==NULL)
{
printf("Error: File could not be opened");
getchar();
return(0);
}


fseek(fp,10,SEEK_SET);
fread(&imgstrt,1,1,fp);
printf("Image Starts At:%d\n",imgstrt);

fseek(fp,18,SEEK_SET);
fread(&width,4,1,fp);
printf("Image Width:%d\n",width);

fseek(fp,22,SEEK_SET);
fread(&height,4,1,fp);  
printf("Image Height:%d\n",height);

padding=(4 -(width*3)%4)%4;
printf("Padding:%d\n",padding);
getchar();

RED = (int*)calloc(height*width+1,sizeof(int));
GREEN = (int*)calloc(height*width+1,sizeof(int));
BLUE = (int*)calloc(height*width+1,sizeof(int));

if(RED == NULL)
{printf("Red Allocation Faliure\n");}
else{printf("Red Allocation Successful\n");}

if(GREEN == NULL)
{printf(" Green Allocation Faliure\n");}
else{printf("Green Allocation Successful\n");}

if(BLUE == NULL)
{printf("Blue Allocation Faliure\n");}
else{printf("Blue Allocation Successful\n");}

fseek(fp,54,SEEK_SET);
/*---------------------Main Loop--------------------------------*/
while((temp = fgetc(fp)) != EOF)
{
    BLUE[current_pixel]=temp;
    temp=fgetc(fp);
    GREEN[current_pixel]=temp;
    temp=fgetc(fp);
    RED[current_pixel]=temp;
    cycle++;
    current_pixel++;

    printf("[%d,%d,%d::%d] ",RED[current_pixel],GREEN[current_pixel],BLUE[current_pixel],current_pixel);
/*------------------------------------------------------------------*/

    /*---------------------Padding Remover-----------------------*/
    if(cycle==width)
    {printf("\n");
    while (paddingremoved!=padding)
    {fgetc(fp); 
                paddingremoved++;}
    cycle=0;
    paddingremoved=0;}
    /*-----------------------------------------------------------*/
}

getchar();
free(RED);
free(BLUE);
free(GREEN);
return(0);
}
4
  • 1
    what is the initial value of current_pixel? (presumably zero, but not shown) Commented Dec 9, 2012 at 0:44
  • 2
    Wherever the problem is, it's in the code you're not showing us. Commented Dec 9, 2012 at 0:46
  • 1
    @BrianRoach: look closer ;) Commented Dec 9, 2012 at 0:56
  • Oh wow ... wasn't even looking at the LOOP. I presumed that was working. Was completely hung up on current_pixel being garbage; good catch. Commented Dec 9, 2012 at 0:59

2 Answers 2

4

Due to C operator precendence, your while condition reads as:

temp=(fgetc(fp) != NULL)

and not

(temp=fgetc(fp)) != NULL

Use parens. Also, fgetc return EOF on error/finish, not NULL.

Sign up to request clarification or add additional context in comments.

Comments

1

Your code always sets every BLUE pixel to 1, because of operator precedence. The while() loop condition compares the result of fgetc(fp) with NULL, and then sets temp to the result of that comparison (which is 0 or 1). So every BLUE pixel will be set to 1.

Try this instead:

while((temp = fgetc(fp)) != EOF)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.