Tim 發問時間: 電腦與網際網路程式設計 · 5 年前

[急!20點]關於c++的bmp檔讀檔困難

在用c++來寫bmp檔讀檔的時候

因為我看不太懂網路上那些什麼fread,struct之類的東西(小弟不才)

就想說用自己的方法寫寫看

但是在header的地方就卡住了

我是打算先把每個byte都先顯示出來看看,再用ultra edit 打開圖檔做對照

寫法如下(有問題的部分):(圖檔沒有調色盤)

Ifstream pic("xxxxx.bmp",ios::in);

unsigned char test[54];//header共54位元

for(int i=0;i<=53;i++)

{

pic>>test[i];

}

for(int i=0;i<=53;i++)

{

unsigned int x;

x=test[i];

cout<<x<<" ";

}

雖然這樣出來的結果和ultra edit都差不多

像前兩個位元在ultra edit中是42 4D

而我寫出來是66 77

只是把結果換成十進位

但問題是

ultra edit中顯示0A或0B這種的(0+英文)

我那樣寫就會讀不出來,而且是直接忽略,繼續下一個位元

這是為什麼呢?懇請大大解惑(文長抱歉)

已更新項目:

感謝回答和意見的各位,疑惑已解

後來發現非文字檔要用binary來打開

就如同A TK UE 2AEO OR UFO大大所說

然後0A或0B這種會被忽略聽說是因為不用ios::binary的話

讀取時可能會被當成換行符號什麼的...

2 個解答

評分
  • Chen
    Lv 6
    5 年前
    最佳解答

    Color processing and enhancement( stretch, rotate, shear image) are basic function for image processing.

    JPG file is converted into bmp file or Bmp file is converted into jpg file.

    0.JPG file is converted into binary white & black file

    1.JPG file is converted into 4 bit grey level file

    2 JPG file is converted into 8 bit grey level file

    3.JPG file is converted into 8 bit colors file

    4.JPG file is converted into 16 bit grey file

    5 JPG file is converted into 16 bit colors file

    6 JPG file is converted into 24 bit colors file

    7 JPG file is converted into 36 bit colors file

    8 JPG file is converted into 48 bit colors fileBMP format :

    file header ( 14) + information header( 40 ) + color palettes(1024) + image data/indeximage data is that data for 24 bit colors, but it is only index for 8 bit colors or less 8 colors.attention 2thing:

    1.Your memory must be 4 multiple boundary, or skew picture or can't read or write in virtual memory.

    2.data of BMP format is from down up to top approach, but JPEG(other) format is from top to down approach. so processing data must do inverse of data of BMP format.

    for example:

    圖片參考:https://s.yimg.com/rk/AC08187637/o/1232200814.jpg

    • 登入以對解答發表意見
  • 卸貨
    Lv 5
    5 年前

    1. Open file with attribute ios::binary.

    2. Use pic.read, pic.write instead.

    • 登入以對解答發表意見
還有問題?馬上發問,尋求解答。