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

北城以北 提交于 2019-12-06 03:43:27

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.)

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