Compression
The biCompression field of the
DIB header says how the
pixel data is stored. Most BMP files are
uncompressed; the others use a simple run-length encoding for palette images, or hold a
whole JPEG or PNG.
03 04 05 06 00 03 45 56 67 00 02 78 00 02 05 01
02 78 00 00 09 1E 00 01
Values
| Value | Name | Bit count | Meaning |
|---|---|---|---|
| 0x00 (0) | BI_RGB | any | Uncompressed. |
| 0x01 (1) | BI_RLE8 | 8 | Run-length encoded 8-bit indexes. |
| 0x02 (2) | BI_RLE4 | 4 | Run-length encoded 4-bit indexes. |
| 0x03 (3) | BI_BITFIELDS | 16, 32 | Uncompressed, with bit masks. In OS/2 2.x files, 3 means Huffman 1D for 1-bit images. |
| 0x04 (4) | BI_JPEG | 0 | The pixels are a JPEG image. In OS/2 2.x files, 4 means RLE24 for 24-bit images. |
| 0x05 (5) | BI_PNG | 0 | The pixels are a PNG image. |
| 0x06 (6) | BI_ALPHABITFIELDS | 16, 32 | Uncompressed, with bit masks including alpha. From Windows CE. |
| 0x0B (11) | BI_CMYK | CMYK, used only in Windows metafiles. | |
| 0x0C (12) | BI_CMYKRLE8 | CMYK with RLE8, used only in Windows metafiles. | |
| 0x0D (13) | BI_CMYKRLE4 | CMYK with RLE4, used only in Windows metafiles. |
A compressed bitmap is always bottom-up: its height must be positive. Its
biSizeImage must give the size of the compressed data.
RLE8
RLE8 data is read two bytes at a time. In encoded mode the first byte is a count from 1 to 255 and the second is a palette index to repeat that many times. A first byte of zero is an escape, and the second byte says what it means:
| Second byte | Meaning |
|---|---|
| 0x00 (0) | End of line: go to the start of the next row. |
| 0x01 (1) | End of bitmap: the image is complete. |
| 0x02 (2) | Delta: the next two bytes move the position right and on by that many pixels and rows. Skipped pixels are left undefined. |
| 0x03 (3) to 0xFF (255) | Absolute mode: that many literal palette indexes follow, padded with a zero byte to an even length. |
Decoding the example at the top of the page:
| Bytes | Result |
|---|---|
03 04 | 04 04 04 |
05 06 | 06 06 06 06 06 |
00 03 45 56 67 00 | 45 56 67, then one pad byte |
02 78 | 78 78 |
00 02 05 01 | Move 5 pixels right and 1 row on |
02 78 | 78 78 |
00 00 | End of line |
09 1E | 1E nine times |
00 01 | End of bitmap |
RLE4
RLE4 works the same way with 4-bit indexes. In encoded mode the count is a number of pixels and the second byte holds two indexes, which are used in turn: high nibble, low nibble, high nibble and so on. In absolute mode the count is a number of pixels, packed two to a byte, and the bytes are padded to an even length. The escapes are the same as RLE8.
| Bytes | Result |
|---|---|
03 04 | 0 4 0 |
05 06 | 0 6 0 6 0 |
00 06 45 56 67 00 | 4 5 5 6 6 7, then one pad byte |
04 78 | 7 8 7 8 |
00 02 05 01 | Move 5 pixels right and 1 row on |
00 00 | End of line |
09 1E | 1 E 1 E 1 E 1 E 1 |
00 01 | End of bitmap |
Embedded JPEG and PNG
With BI_JPEG or BI_PNG the bit count is 0 and the pixel data
is a complete JPEG or
PNG file, whose length is given by
biSizeImage. These values were added so that Windows could pass compressed
images straight to printers. Few programs write them in BMP files, and many readers do not
support them.