Reputation: 43
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);
}
Upvotes: 1
Views: 303
Reputation: 10613
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)
Upvotes: 1
Reputation: 15278
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
.
Upvotes: 4