我正在编写一个程序来反转位图图像的颜色。我在 unsigned char RGB 值上使用 ~ 运算符来反转颜色,并且 print 语句显示数字已正确反转。不过我认为我的 fwrite 可能出了问题,因为图像没有改变。
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);
最佳答案
“rb”选项以读取模式而不是写入模式打开文件。如果要读写,就必须在每个输入和输出之间使用文件定位功能。请参阅man page .
关于c - 使用 C 反转位图颜色不会改变图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39819811/