Reputation: 1030
I am trying to learn (by myself and help of stackoverflow ) to read binary data as they are packed and distributed. I am doing this for shear curiosity, but i must say in advance that i don't have proper schooling from this field, everything i know is what i find on net and trial/error sort of work.
To prepare for this work i made several simple 1 pixel 32 bit files in photoshop with different colors to test and compare, and to help me differentiate between different bytes and bits. And started combing trough wikipedia and msdn bitmap webpages.
"literature " if someone is curious BMP/DIB header MSDN web page
And i hit the "road block" with compression "slot" of 4 bytes in DIB portion of header.
the makeshift function i made returns integer in little endian of value 6.
here is function:
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
This functions are currently makeshift , as said before i am still figuring things out.
Now i understand that pixels must be converted ( and maybe compressed ) from raw file pixel definition, but i have no clue what 6 should represent ?
I even thought that maybe this is false, that "compression" per se is a format of compression that is passed from another type of image when it is converted to bmp (For example : 1 - PNG, 2-JPEG etc ).
I even thought that i red it wrong, instead of making it integer i should get the raw binary and that should tell me something.
In binary : \x06\x00\x00\x00
as binary string : ◦◦◦
Which got me even more confused.
in wikipedia article it says:
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
in msdn web page it says:
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.
So, it is usually not compressed... so what does 6 stands for ? "nothing happening here, move on... " ?
So my questions are:
Are there some sorts of standardised codes for image compression ? Can help me with navigation trough these byte slots ? I can research it in detail later. Does anyone have any link or book that can guide me further in this quest ?
Upvotes: 0
Views: 589
Reputation: 1030
So first, i needed to seek byte 30th not 34th. Second , Kevin was too kind to point me to the specific section on wikipedia that says the next thing.
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
So since i've got value 6 from compression slot, i know that i need to read pixel grid as RGBA without compression.
Upvotes: 0