如何解释1 bpp BMP文件中的像素数组 [英] How to interpret the pixel array in a 1 bpp BMP file

查看:180
本文介绍了如何解释1 bpp BMP文件中的像素数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到在以下情况下解释像素数组的方法:
我在Windows 7上用MS Paint创建了一个BMP图像2x2像素。将它保存为单色BMP(我认为这意味着BMP与颜色深度每像素1位)

这是图像在放大时的样子(黑色像素,白色像素,白色像素,黑色像素)
< a href =https://i.stack.imgur.com/OsmiL.png =nofollow noreferrer> 然后我用一个十六进制编辑器打开bmp,我可以看到以下信息:

$ b

b

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

据我阅读维基百科关于BMP格式的文章( https://en.wikipedia.org/wiki/BMP_file_format )这部分是像素数组:

3f00 0000 4000 0000

是什么意思像素数组中的值? 3F值有特殊含义吗?

解决方案

您发布的十六进制转储与您发布的图片并不完全对应。这是我得到的:

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

颜色表从0x36开始,这里有两个RGBQUAD,第一个0x00000000对应黑色,下一个0x00FFFFFF对应白色。 / p>

像素数据在此之后开始,每个像素用一个位表示,即使图像每行只需要两位,每行也会对齐一个四字节的边界,所以第一行是0x80000000,第二行是0x40000000,很可能有些应用程序可能不会清除填充位。

行数据按字节解释,从最高位到最低位在每个字节中。

0x8的高位为1和0,我们应该看到颜色1(白色),然后是颜色0(黑色),顺序排列在最下面一排。其余的位被忽略,因为它们将对应于超出2的宽度的像素,因为接下来的三个字节仅用于确保随后的行与4字节边界对齐。



0x4的高位是0和1,所以我们应该看到颜色0(黑色),然后是颜色1(白色),按照下一排顺序排列。和以前一样,其余的位被忽略。

在你的十六进制转储中,颜色表是黑色(0x00000000)和灰色(0x003F3F3F)。没什么大不了。像素数据在第一行(底部)具有0和0的高位,在第二行(顶部)具有0和1。其他位是用于填充的随机垃圾。



(0x3F类似于灰度值的事实表明编码器可能不会打扰清除变量或寄存器在编写颜色表后重新使用。)


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?

解决方案

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

这篇关于如何解释1 bpp BMP文件中的像素数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆