2016-10-02 79 views
0

我正在編寫一個程序來反轉位圖圖像的顏色。我在無符號字符RGB值上使用〜運算符來反轉顏色,並且打印語句顯示數字正確反轉。不過我認爲也許我的fwrite出了問題,因爲圖像沒有改變。使用C反轉位圖顏色不會更改圖像

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

完成後文件包含什麼內容? – nicomp

+1

這不是[mcve] - 你不顯示你如何打開文件。這可能是問題所在。 – usr2564301

+0

@nicomp文件不變 – 3802400

回答

1

「rb」選項以讀取模式打開文件,而不是寫入。如果您想讀寫,則必須在每個輸入和輸出之間使用文件定位功能。請參閱man page