简体   繁体   中英

inverting bitmap colors using C doesn't change the image

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);

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM