How to interpret the pixel array in a 1 bpp BMP file

旧街凉风 提交于 2020-01-02 09:35:05

问题


I can't find a way to explain the pixel array in the following situation: I created a BMP image 2x2 pixels with MS Paint on Windows 7. Saved it as monochrome BMP (which I think means BMP with color depth 1 bit per pixel)

This is how the image looks like when zoomed it (black pixel, white pixel, white pixel, black pixel)

Then I open the bmp with a hex editor and I can see the following info:

00: 424d 4600 0000 0000 0000 3e00 0000 2800  BMF.......>...(.
10: 0000 0200 0000 0200 0000 0100 0100 0000  ................
20: 0000 0800 0000 0000 0000 0000 0000 0000  ................
30: 0000 0000 0000 0000 0000 3f3f 3f00 3f00  ..........???.?.
40: 0000 4000 0000 0a                        ..@....

As far as I read in the Wikipedia article about the BMP format (https://en.wikipedia.org/wiki/BMP_file_format) this part is the pixel array:

3f00 0000 4000 0000

What is the meaning of the values in the pixel array? Does the 3F value has a special meaning?


回答1:


That hex dump you posted doesn't quite correspond to the image you posted. Here's what I get:

00000000 42 4D 46 00 00 00 00 00 00 00 3E 00 00 00 28 00 BMF.......>...(.
00000010 00 00 02 00 00 00 02 00 00 00 01 00 01 00 00 00 ................
00000020 00 00 08 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000030 00 00 00 00 00 00 00 00 00 00 FF FF FF 00 80 00 ................
00000040 00 00 40 00 00 00                               ..@...

The color table starts at 0x36. There are two RGBQUADs there. The first, 0x00000000, corresponds to black. The next, 0x00FFFFFF, corresponse to white.

The pixel data starts after that. Each pixel is represented by a single bit. Even though it would take only two bits for each row of your image, each row is aligned to a four-byte boundary. Thus the first row is 0x80000000 and the second is 0x40000000. It's likely that some applications might not bother to clear out the padding bits.

The row data is interpreted byte by byte, from the most-significant bit to the least in each byte.

The high bits of 0x8 are 1 and 0, and we should see color 1 (white) followed by color 0 (black), in that order on the bottom row. The rest of the bits are ignored because they would correspond to pixels beyond the width of 2, as are the next three bytes exist only to ensure the subsequent row is aligned to the 4-byte boundary.

The high bits of 0x4 are 0 and 1, so we should see color 0 (black) followed by color 1 (white), in that order on the next row up. As before, the rest of the bits are ignored.

In your hex dump, the color table was black (0x00000000) and gray (0x003F3F3F). No big deal. The pixel data had high bits of 0 and 0 on the first (bottom) row and 0 and 1 on the second (top) row. The other bits are random garbage used for padding.

(The fact that the 0x3F is similar to the gray value suggests the encoder perhaps didn't bother clearing a variable or register that was re-used after writing the color table.)



来源:https://stackoverflow.com/questions/35862660/how-to-interpret-the-pixel-array-in-a-1-bpp-bmp-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!