单色 .bmp 中图像前的三个 255 是什么?

What are the three 255's before the image in a monochrome .bmp?

我正在尝试找出我正在处理的项目的位图格式,但有一件事我不太明白。在此 .bmp 中:

00000000  42 4d aa 00 00 00 00 00  00 00 82 00 00 00 6c 00  |BM............l.|
00000010  00 00 0a 00 00 00 0a 00  00 00 01 00 01 00 00 00  |................|
00000020  00 00 28 00 00 00 13 0b  00 00 13 0b 00 00 02 00  |..(.............|
00000030  00 00 02 00 00 00 42 47  52 73 00 00 00 00 00 00  |......BGRs......|
00000040  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000060  00 00 00 00 00 00 00 00  00 00 02 00 00 00 00 00  |................|
00000070  00 00 00 00 00 00 00 00  00 00 ff ff ff 00 00 00  |................|
00000080  00 00 ff c0 00 00 ff c0  00 00 ff c0 00 00 ff c0  |................|
*
000000a0  00 00 ff c0 00 00 aa 80  00 00                    |..........|
000000aa

偏移量 122(第 7 行)处的三个 256 是什么。我假设它们是某种颜色指示器,但我不确定。

This 是我正在使用的图像。

十六进制转储的顶部是 "BITMAPINFO structure" (https://msdn.microsoft.com/en-us/library/dd183375(v=vs.85).aspx)。紧接着是颜色索引数组 bmiColors(尽管它的长度可能是 0,您应该在 BITMAPINFO 数据中检查它)。

虽然有人这么说

[t]hough seemingly a simple format, it is complicated by its many different versions, lack of an official specification, lack of any version control process, and ambiguities and contradictions in the documentation.
(http://fileformats.archiveteam.org/wiki/BMP)

您实际上不需要理解每个字节。各种结构要么具有固定大小(例如初始 BITMAPFILEHEADER),要么将其长度作为其第一个值。

大多数记录完备的部分的逐行注释:

--------  BITMAPFILEHEADER
00000000  42 4d          file type identifier
00000002  aa 00 00 00    size in bytes of the entire file
00000006  00 00          (reserved and must be 0)
00000008  00 00          (reserved and must be 0)
0000000A  82 00 00 00    offset from the beginning of the BITMAPFILEHEADER structure to the bitmap bits

--------  BITMAPINFOHEADER
0000000E  6c 00 00 00    BITMAPINFOHEADER structure size
00000012  0a 00 00 00    image width in pixels
00000016  0a 00 00 00    image height in pixels
0000001A  01 00          number of planes
0000001C  01 00          number of bits per pixel
0000001E  00 00 00 00    compression
00000022  28 00 00 00    size in bytes of image data
00000026  13 0b 00 00    horizontal resolution in pixels-per-meter
0000002A  13 0b 00 00    vertical resolution in pixels-per-meter
0000002E  02 00 00 00    number of colors in the color table that are actually used by the bitmap
00000032  02 00 00 00    number of color indexes that are required ("important")

........ badly documented stuff ........
00000036  42 47 52 73 00 00 00 00 00 00
00000040  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00000060  00 00 00 00 00 00 00 00 00 00 02 00 00 00 00 00
00000070  00 00 00 00 00 00 00 00 00 00 
--------  end of BITMAPINFOHEADER

--------  bmiColors array
0000007A  ff ff ff 00    color table entry #0
0000007E  00 00 00 00    color table entry #1

--------  Image data
00000082  ff c0 00 00 ff c0 00 00 ff c0 00 00 ff c0
000000a0  00 00 ff c0 00 00 aa 80 00 00 

0000001C处的"number of bits per pixel"是1:

"1 = The bitmap is monochrome, and the bmiColors member of BITMAPINFO contains two entries. Each bit in the bitmap array represents a pixel. If the bit is clear, the pixel is displayed with the color of the first entry in the bmiColors table; if the bit is set, the pixel has the color of the second entry in the table." (https://msdn.microsoft.com/en-us/library/dd183376(v=vs.85).aspx)

并且数组中的颜色数报告为 2。因此 bmiColors 数组包含 2 个微软 RGBQUAD 格式的元素,值的顺序为蓝色、绿色、红色的奇数, 和保留。

简而言之:在你的图像中,0的像素值(单色图像中的0位)是FFFFFF:白色,1的像素值是000000:黑色。