Reputation: 57
I'm writing a program to invert the colors of a bitmap image. I used the ~ operator on unsigned char RGB values to invert the colors, and print statements are showing that the numbers are correctly inverted. However I think maybe something is going wrong with my fwrite, because the image is not changing.
void invert_colors(struct head h, FILE* filep, struct dib_h dibh){
fseek(file_p, (int)*h.offset_to_pixels, SEEK_SET);
int wid;
int len;
struct pixel pix;
for (len = 0; len < (int)*dibh.imgheight; len++){
for (wid = 0; wid < (int)*dibh.imgwidth; wid++){
fread(&pix, 3, 1, filep);
pix.red = ~(pix.red);
pix.green = ~(pix.green);
pix.blue = ~(pix.blue);
fseek(filep, -3, SEEK_CUR);
fwrite(pix, 3, 1, filep);
}
fseek(filep, (((int)*dibh.imgwidth)*3)%4, SEEK_CUR);
}
fclose(filep);
Upvotes: 0
Views: 414
Reputation: 131
The "rb" option open the file in read mode and not writing. If you want to read and write, you have to use a file positioning function between each input and output. See the man page.
Upvotes: 1