========================================================================== Basic Description This file format is the MS-Windows standard format. It holds black-and- white, 16-colour, 256-colour and Truecolour images. The palletized 16-colour and 256-colour images may be compressed via run length encoding. Notice there is also a OS/2-BMP format. ========================================================================== Basic File Format Offset Name Size Description Header 14 bytes Windows Structure: BITMAPFILEHEADER 0 Signature 2 bytes 'BM' 2 FileSize 4 bytes File size in bytes 6 reserved 4 bytes unused (=0) 10 DataOffset 4 bytes File offset to Raster Data InfoHeader 40 bytes Windows Structure: BITMAPINFOHEADER 14 Size 4 bytes Size of InfoHeader =40 18 Width 4 bytes Bitmap Width 22 Height 4 bytes Bitmap Height 26 Planes 2 bytes Number of Planes (=1) 28 BitCount 2 bytes Bits per Pixel 1 = monochrome palette. NumColours = 1 4 = 4bit palletized. NumColours = 16 8 = 8bit palletized. NumColours = 256 16 = 16bit RGB. NumColours = 65536 (?) 24 = 24bit RGB. NumColours = 16M 30 Compression 4 bytes Type of Compression 0 = BI_RGB   no compression 1 = BI_RLE8 8bit RLE encoding   2 = BI_RLE4 4bit RLE encoding 34 ImageSize 4 bytes (compressed) Size of Image  It is valid to set this =0 if Compression = 0 38 XpixelsPerM 4 bytes horizontal resolution: Pixels/metre 42 YpixelsPerM 4 bytes vertical resolution: Pixels/metre 46 ColoursUsed 4 bytes Number of actually used colours 50 ColoursImportant 4 bytes Number of important colours, 0 = all 54 ColourTable 4 * NumColours bytes present only if BitCount <= 8   colours should be ordered by importance {Red 1 byte Red intensity Green 1 byte Green intensity Blue 1 byte Blue intensity reserved 1 byte unused (=0) } repeated NumColours times 54+4*NumColours Raster Data ImageSize bytes The pixel data ========================================================================== Raster Data encoding Depending on the image's BitCount and on the Compression flag there are six different encoding schemes. All of them share the following:  Pixels are stored bottom-up, left-to-right. Pixel lines are padded with zeros to end on a 32bit (4byte) boundary. For uncompressed formats every line will have the same number of bytes. Colour indices are zero based, meaning a pixel colour of 0 represents the first colour table entry, a pixel colour of 255 (if there are that many) represents the 256th entry. For images with more than 256 colours there is no colour table.  Raster Data encoding for 1-bit / black & white images ----------------------------------------------------- BitCount = 1 Compression = 0  Every byte holds 8 pixels, its highest order bit representing the leftmost pixel of those. There are 2 colour table entries. Some readers will ignore them though, and assume that 0 is black and 1 is white. If you are storing black and white pictures you should stick to this, with any other two colours this is not an issue. Remember padding with zeros up to a 32bit boundary (this can be up to 31 zeros/pixels!)  Raster Data encoding for 4-bit / 16-colour images ------------------------------------------------- BitCount = 4 Compression = 0  Every byte holds 2 pixels, its high order 4 bits representing the left pixel, the low order 4 bits representing the right pixel. There are 16 colour table entries. These colours do not have to be the 16 MS-Windows standard colours. Padding each line with zeros up to a 32-bit boundary will result in up to 28 zeros = 7 'wasted pixels'. Raster Data encoding for 8-bit / 256-colour images -------------------------------------------------- BitCount = 8 Compression = 0  Every byte holds 1 pixel. There are 256 colour table entries. Padding each line with zeros up to a 32-bit boundary will result in up to 3 bytes of zeros = 3 'wasted pixels'. Raster Data encoding for 16-bit / hicolour images ------------------------------------------------- BitCount = 16 Compression = 0  Every 2 bytes / 16 bits holds 1 pixel.   The pixels are not colour table pointers. There are no colour table entries. Padding each line with zeros up to a 16-bit boundary will result in up to 2 zero bytes. Raster Data encoding for 24-bit / truecolour images --------------------------------------------------- BitCount = 24 Compression = 0  Every 4 bytes / 32 bits holds 1 pixel. The first holds its red, the second its green, and the third its blue intensity. The fourth byte is reserved and should be zero. There are no colour table entries. The pixels are not colour table pointers. No zero padding necessary. Raster Data compression for 1-bit / black-and-white images ---------------------------------------------------------- There doesn't appear to be a compressed 1-bit image compression format. Compressed 1-bit images must be stored as compressed 4-bit images. Raster Data compression for 4-bit / 16-colour images ---------------------------------------------------- BitCount = 4 Compression = 2  The pixel data is stored in 2-byte / 16-bit chunks. The first of these specifies the number of consecutive pixels with the same pair of colour. The second byte defines two colour indices. The resulting pixel pattern will be interleaved high-order 4 bits and low order 4 bits (ABABA...). If the first byte is zero, the second defines an escape code. The End-of-Bitmap is zero padded to end on a 32-bit boundary. Due to the 16-bit-ness of this structure this will always be either two additional zero bytes or none. n (byte 1) c (Byte 2) Description ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ >0 any n pixels are to be drawn. The 1st, 3rd, 5th, etc. pixel colours is the high-order 4 bits of c, the 2nd, 4th, 6th, etc. pixel colours is in the low-order 4 bits of c. If both colours are the same, it results in just n pixels of colour c. n is also twice (or twice + 1) the number of byte 'c's output. Be careful not to output an n of zero, as that is an end-of-line. 0 0 End-of-line. This is required at the end of each pixel line. 0 1 End-of-Bitmap. This is required at the end of the bitmap. If is followed by zero or two zero bytes to end on a 32-bit boundary. 0 2 Delta. The following 2 bytes define an unsigned offset in x and y direction (y being up) The skipped pixels should get a colour zero. 0 >=3 The following c bytes will be read as single uncompressed pixel colours. Up to 12 bits of zeros follow, to put the file/memory pointer on a 16-bit boundary again. Example for 4bit RLE ~~~~~~~~~~~~~~~~~~~~ Compressed Data Expanded data 03 04 0 4 0 05 06 0 6 0 6 0 00 06 45 56 67 00 4 5 5 6 6 7 04 78 7 8 7 8 00 02 05 01 Move 5 right and 1 up. (Windows docs say down, which is wrong) 00 00 End-of-line 09 1E 1 E 1 E 1 E 1 E 1 00 01 EndofBitmap 00 00 Zero padding for 32bit boundary Raster Data compression for 8-bit / 256-colour images ----------------------------------------------------- BitCount = 8 Compression = 1  The pixel data is stored in 2-bytes / 16-bit chunks. The first of these specifies the number of consecutive pixels with the same colour. The second byte defines their colour index. If the first byte is zero, the second defines an escape code. The End-of-Bitmap is zero padded to end on a 32-bit boundary. Due to the 16-bit-ness of this structure this will always be either two zero bytes or none.   n (byte 1) c (Byte 2) Description ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ >0 any n pixels of colour number c. n is also the number of byte 'c's output. Be careful not to output an n of zero, as that is an end-of-line. 0 0 End-of-line. This is required at the end of each pixel line. 0 1 End-of-Bitmap. This is required at the end of the bitmap. If is followed by zero or two zero bytes to end on a 32-bit boundary. 0 2 Delta. The following 2 bytes define an unsigned offset in x and y direction (y being up) The skipped pixels should get a colour zero. 0 >=3 The following c bytes will be read as single uncompressed pixel colours. If c is odd then a zero follows, putting the file/memory pointer on a 16-bit boundary again. Example for 8bit RLE ~~~~~~~~~~~~~~~~~~~~ Compressed Data Expanded data 03 04 04 04 04 05 06 06 06 06 06 06 00 03 45 56 67 00 45 56 67 02 78 78 78 00 02 05 01 Move 5 right and 1 up. (Windows docs say down, which is wrong) 00 00 End-of-line 09 1E 1E 1E 1E 1E 1E 1E 1E 1E 1E 00 01 End-of-bitmap 00 00 Zero padding for 32bit boundary ========================================================================== Portability Although BMPs were invented by Microsoft for its Windows platform, a lot of programs on other platforms are capable of reading and writing them. Notice the Intel order in 2-byte and 4-byte integer values (Least significant byte first). The 16-bit BMPs have been introduced to Windows after the others, still puzzling many applications.