BMP格式分析

我們用這張3×2的影像來看BMP檔跟PNG檔的格式是怎麼樣。

先用sublime或ultraedit什麼的HEX編輯軟體把BMP圖檔打開,會發現內容是

BMP表頭有兩種, BMP跟DIB,BMP的是

Offset hex Offset dec Size Purpose
00 0 2 bytes 42 4d 這兩個bytes是BM兩個字
02 2 4 bytes 檔案大小
06 6 2 bytes 保留
08 8 2 bytes 保留
0A 10 4 bytes 偏移量,資料的位置

DIB的是

Offset (hex) Offset (dec) Size (bytes) Windows BITMAPINFOHEADER
0E 14 4 the size of this header (40 bytes)
12 18 4 the bitmap width in pixels (signed integer)
16 22 4 the bitmap height in pixels (signed integer)
1A 26 2 the number of color planes (must be 1)
1C 28 2 the number of bits per pixel, which is the color depth of the image. Typical values are 1, 4, 8, 16, 24 and 32.
1E 30 4 the compression method being used. See the next table for a list of possible values
22 34 4 the image size. This is the size of the raw bitmap data; a dummy 0 can be given for BI_RGB bitmaps.
26 38 4 the horizontal resolution of the image. (pixel per metre, signed integer)
2A 42 4 the vertical resolution of the image. (pixel per metre, signed integer)
2E 46 4 the number of colors in the color palette, or 0 to default to 2n
32 50 4 the number of important colors used, or 0 when every color is important; generally ignored

所以

 

檔案大小78個位元組是怎麼來的, BMP表頭固定14, DIB表頭是40, 至少就54了, 78-54=24, 24/6=4, 難道是4個位元組(32位元)來表示一個畫素嗎?

不是,色彩深度已經說明是24位元,所以要這樣看

把補的部份的值亂改一通,會發現影像內容沒有變化,例如:

 

 

 

參考資料

  1. https://en.wikipedia.org/wiki/BMP_file_format

 

Leave a Reply

你的電子郵件位址並不會被公開。 必要欄位標記為 *