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. |
| 0x04 (4) | BI_JPEG | 0 | The pixels are a JPEG image. |
| 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 | 1, 4, 8, 32 | Uncompressed CMYK. Windows metafiles only. |
| 0x0C (12) | BI_CMYKRLE8 | 8 | CMYK with RLE8. Windows metafiles only. |
| 0x0D (13) | BI_CMYKRLE4 | 4 | CMYK with RLE4. Windows metafiles only. |
| a FOURCC | A four-character code naming a YUV layout or a video codec. |
A compressed bitmap is always bottom-up: its height must be positive. Its
biSizeImage must give the size of the compressed data, and its bit count
must match the method: 8 for the RLE8 methods, 4 for the RLE4 ones.
CMYK
The three BI_CMYK values come from the Windows metafile format
(MS-WMF)
and store CMYK colours instead of RGB: the colour table holds CMYK colours, and 32-bit
pixels are CMYK colours themselves.
A CMYK colour is the little-endian 32-bit value
cyan << 24 | magenta << 16 | yellow << 8 | black, so its
four bytes are black, yellow, magenta, cyan in file order, each from 0 to 255. Each
colour table entry is one such value. Palette images under
BI_CMYK, BI_CMYKRLE8 and BI_CMYKRLE4 use the
same row, RLE8 and RLE4 layouts as their RGB counterparts, looking the index up in the
CMYK table.
To convert a colour to RGB, a viewer takes red = (255 − cyan) × (255 − black) ÷ 255,
and the same for green from magenta and blue from yellow. This is the simple
conversion; an exact one needs a colour profile.
FOURCC values
Microsoft’s BITMAPINFOHEADER reference allows
biCompression to be a FOURCC: four ASCII characters, read as a little-endian
number. It names a YUV sample layout or a video codec, as used for video frames held in
a bitmap. A YUV image is always top row first, whatever the sign of the height.
YUV layouts
These are the layouts on Microsoft’s Recommended 8-Bit YUV Formats page (with its “Other YUV Formats”) and its 10-bit and 16-bit YUV Video Formats page. U is Cb and V is Cr. The stride is the width of a line in bytes.
| FOURCC | Sampling | Layout |
|---|---|---|
AYUV | 4:4:4 | Packed, 4 bytes a pixel: V, U, Y, alpha (Microsoft shows the order in a figure). |
I444 | 4:4:4 | Planar: the Y plane, then U, then V, each of the same stride and number of lines, with no padding. |
YUY2, YUYV | 4:2:2 | Packed, 4 bytes for 2 pixels: Y0, U, Y1, V. |
YVYU | 4:2:2 | Packed: Y0, V, Y1, U. |
UYVY | 4:2:2 | Packed: U, Y0, V, Y1. |
Y42T | 4:2:2 | As UYVY, but the lowest bit of each Y is a chroma key: 0 transparent, 1 opaque. |
I422 | 4:2:2 | Planar: the Y plane, then U, then V, the chroma planes at half the stride and with every line. |
Y41P | 4:1:1 | Packed, 12 bytes for 8 pixels: U0, Y0, V0, Y1, U4, Y2, V4, Y3, Y4, Y5, Y6, Y7. |
Y41T | 4:1:1 | As Y41P, with the chroma key in the lowest bit of each Y. |
NV11 | 4:1:1 | The Y plane, then U and V interleaved, one pair for every 4 pixels of every line. Microsoft does not give the chroma stride; the explorer takes it as half the luma stride, which holds a line. |
I420, IYUV | 4:2:0 | Planar: the Y plane, then U, then V, the chroma planes at half the stride and half the lines. |
YV12 | 4:2:0 | As I420 with V before U. |
NV12 | 4:2:0 | The Y plane, then U and V interleaved, at the luma stride and half the lines. |
NV21 | 4:2:0 | As NV12 with V before U. Not on Microsoft’s pages; the common Android layout. |
IMC1 | 4:2:0 | The Y plane, then V, then U, each chroma line at the full luma stride with its second half unused. The planes start on multiples of 16 lines: V at ((Height + 15) & ~15) × Stride and U at (((Height × 3) / 2 + 15) & ~15) × Stride from the start. |
IMC3 | 4:2:0 | As IMC1 with U and V swapped. |
IMC2 | 4:2:0 | The Y plane, then from ((Height + 15) & ~15) × Stride the chroma lines, each holding a line of V and, from the half-stride boundary, a line of U. |
IMC4 | 4:2:0 | As IMC2 with U and V swapped. |
AI44 | palette | One byte a pixel: a palette index in the top 4 bits and alpha in the low 4. The palette is part of the video media type, which a bitmap file does not carry, so the explorer draws the indexes as greys. |
P010, P016 | 4:2:0 | A plane of 16-bit little-endian Y, then U and V interleaved as 16-bit pairs at the same stride and half the lines. P010 keeps its 10 bits at the top of each 16. |
P210, P216 | 4:2:2 | As P010 and P016 with chroma on every line. |
Y210, Y216 | 4:2:2 | Packed 16-bit words: Y0, U, Y1, V; Y210’s 10 bits at the top of each word. |
Y410 | 4:4:4 | One 32-bit value a pixel: U in bits 0 to 9, Y in 10 to 19, V in 20 to 29, alpha in 30 and 31 (3 is opaque). |
Y416 | 4:4:4 | Four 16-bit words a pixel: U, Y, V, alpha. |
A bitmap file gives no stride. The explorer takes the smallest stride the layout allows,
or a larger one when that makes the layout exactly biSizeImage bytes long.
For some heights, such as 20, Microsoft’s IMC1 and IMC3 formulas start the second chroma
plane before the first has ended; the explorer follows the formulas as written and says
so. It converts every layout to RGB with the BT.601 formulas on Microsoft’s page,
using the top 8 bits of 10- and 16-bit samples.
Codecs
Any other FOURCC names a video codec, and the pixel data is one compressed frame. The explorer decodes the two whose frames are still images it can read, and names the rest:
| FOURCC | Codec | In the explorer |
|---|---|---|
MJPG, JPEG | Motion JPEG: each frame is a JPEG image. Frames often leave out the Huffman tables, and then the standard tables of ITU-T T.81 Annex K apply. | Decoded, and opens as a JPEG file |
MPNG, png | Motion PNG: each frame is a PNG image. | Decoded, and opens as a PNG file |
MRLE | Microsoft RLE: the RLE8 or RLE4 coding below, chosen by the bit count. | Decoded |
H264, X264, AVC1 | H.264 (ITU-T H.264) | Named only |
HEVC, H265, HVC1 | H.265 (ITU-T H.265) | Named only |
MP4V, XVID, DIVX, DX50, FMP4 | MPEG-4 Part 2 (ISO/IEC 14496-2) | Named only |
MP42, MP43, DIV3 | Microsoft’s pre-standard MPEG-4; no public specification | Named only |
WMV1, WMV2 | Windows Media Video 7 and 8; no public specification | Named only |
WMV3, WVC1 | VC-1 (SMPTE 421M) | Named only |
MPG1, MPG2 | MPEG-1 and MPEG-2 video | Named only |
dvsd | DV (IEC 61834) | Named only |
CVID | Cinepak; no public specification from its maker | Named only |
IV32, IV41, IV50 | Intel Indeo 3, 4 and 5; no public specification | Named only |
MSVC, CRAM | Microsoft Video 1; no public specification from Microsoft | Named only |
HFYU | Huffyuv, lossless; documented only by its source code | Named only |
FFV1 | FFV1, lossless (IETF RFC 9043) | Named only |
VP80, VP90, AV01 | VP8 (RFC 6386), VP9 and AV1 | Named only |
Video codecs are formats of their own, far larger than BMP; a FOURCC the explorer does not know is shown as bytes with its four characters.
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.