python BMP/DIB 字节槽,压缩槽

python BMP/DIB byte slots , compression slot

我正在尝试学习(通过我自己和 Whosebug 的帮助)在打包和分发二进制数据时读取它们。我这样做是出于纯粹的好奇心,但我必须提前声明我没有接受过该领域的适当教育,我所知道的一切都是我在网上找到的和 trial/error 之类的工作。

为了准备这项工作,我在 photoshop 中用不同的颜色制作了几个简单的 1 像素 32 位文件来测试和比较,并帮助我区分不同的字节和位。并开始梳理维基百科和 msdn 位图网页。

"literature "如果有人好奇 BMP/DIB header MSDN web page

然后我在 header 的 DIB 部分压缩了 "slot" 4 个字节 "road block"。

我在值 6 的小端中制作了 returns 整数的临时函数。

这里是函数:

def BMP_compresion(fileobj): # bitmap compresion ??? i have no idea what the hell is this
    fileobj = manageFile(fileobj)
    fileobj.seek(34)
    comp = fileobj.read(4)
    toStart(fileobj) # returns the file to position 0 or closes the file if file needs to be closed
    return readNUMB(comp,False) # reads integer with struct.unpack

这个功能目前是权宜之计,如前所述,我还在想办法。

现在我知道像素必须从原始文件像素定义转换(并且可能压缩),但我不知道 6 应该代表什么?

我什至认为这可能是错误的,"compression" 本身是一种压缩格式,它是从另一种类型的图像转换为 bmp 时传递过来的(例如:1 - PNG,2 -JPEG 等)。

我什至认为我把它改错了,而不是把它变成整数我应该得到原始二进制文件,它应该告诉我一些事情。

二进制:\x06\x00\x00\x00
作为二进制字符串:◦◦◦

这让我更加困惑了。

在维基百科文章中说:

Indexed color images may be compressed with 4-bit or 8-bit RLE or Huffman 1D algorithm. OS/2 BITMAPCOREHEADER2 24bpp images may be compressed with the 24-bit RLE algorithm. The 16bpp and 32bpp images are always stored uncompressed. Note that images in all color depths can be stored without compression if so desired

在 msdn 网页上说:

BMP BMP is a standard format used by Windows to store device-independent and application-independent images. The number of bits per pixel (1, 4, 8, 15, 24, 32, or 64) for a given BMP file is specified in a file header. BMP files with 24 bits per pixel are common. BMP files are usually not compressed and, therefore, are not well suited for transfer across the Internet.

所以,它通常不被压缩...那么 6 代表什么? "nothing happening here, move on... " ?

所以我的问题是:

是否有一些用于图像压缩的标准化代码? 可以帮助我通过这些字节槽进行导航吗?我可以稍后详细研究它。 有没有人有任何 link 或书籍可以指导我进一步探索这个问题?

所以首先,我需要寻找第 30 个字节而不是第 34 个字节。 其次,凯文太友善了,没有给我指点维基百科上说明下一件事的特定部分。

The compression method (offset 30) can be:

0 :> BI_RGB : the file has no compression  
1 :> BI_RLE8 : Run length encoding ( 1 byte per pixel )  -> RLE
2 :> BI_RLE4 : Run length encoding ( nibble per pixel )  -> RLE
3 :> BI_BITFIELDS : Huffman 1D  
4 :> BI_JPEG : RLE-24  
5 :> BI_PNG : BITMAPV4INFOHEADER+: PNG image for printing  
6 :> BI_ALPHABITFIELDS : RGBA fields ( byte per field, sooo 1 byte per color )  
7 :> BI_CMYK : cmyk  
11 :> BI_CMYKRLE8: RLE-8
12 :> BI_CMYKRLE4 RLE-4

因此,由于我从压缩槽中获得了值 6,我知道我需要将像素网格读取为 RGBA 而无需压缩。