Introduction
BMP is the bitmap image format of Microsoft Windows. It stores a device independent bitmap (DIB): a header describing the image, an optional palette and the pixels, usually uncompressed. This form of it began with the 40-byte header of Windows 3.0 in 1990, and gained new header versions with Windows 95 and Windows 98, each keeping every field of the one before and adding more at the end.
Because the pixels are usually stored as they are, BMP files are large but very simple
to read and write, which keeps the format common in programming, games, embedded systems
and test images. The same bitmap structure, without the file header, is used on the
Windows clipboard, in program resources and inside
ICO icons. Files may use the extension .bmp
or .dib, and run-length compressed ones (BI_RLE8 or
BI_RLE4) sometimes .rle.
This page is the ordinary bitmap, whose file starts with BM. Bitmaps with
the older 12-byte header of Windows 2 and OS/2 1.x, and OS/2 2.x bitmaps, also start
with BM but are separate formats. OS/2 used the same file header for bitmap
arrays, icons and pointers, with other type codes; see
all the BMP variations.
Structure
A BMP file has up to five parts, in this order. Only the first two are always present, and the values in the DIB header decide which of the others follow and how the pixels are stored. All numbers are little-endian.
0File headerBM, the file size and the offset of the pixels14 bytes14DIB header size, bit count, compression and, in later versions, colour space details; its first 4 bytes give its length and so its version40 to 124 bytes…Bit masks three or four 4-byte masks, only after a 40-byte header with bit field compression12 or 16 bytes…Colour table the palette, required at 8 bits per pixel and belowup to 1024 bytes…Pixel data rows padded to 4 bytes, bottom row first, or compressed; found at the offset in the file headervaries
How the header shapes the rest
Two fields of the DIB header, the bit count and the compression, decide what comes between the header and the pixels and how each pixel is stored:
| Bit count | Compression | Colour table | Bit masks | Pixel data |
|---|---|---|---|---|
1 | BI_RGB | 2 colours | none | 8 pixels per byte, each a palette index |
4 | BI_RGB | up to 16 colours | none | 2 pixels per byte |
4 | BI_RLE4 | up to 16 colours | none | run-length encoded, 4-bit indexes |
8 | BI_RGB | up to 256 colours | none | 1 byte per pixel |
8 | BI_RLE8 | up to 256 colours | none | run-length encoded, 8-bit indexes |
16 | BI_RGB | only if biClrUsed is set | fixed 5-5-5 | 2 bytes per pixel |
16 | BI_BITFIELDS | only if biClrUsed is set | red, green, blue | 2 bytes per pixel, split by the masks |
24 | BI_RGB | only if biClrUsed is set | none | 3 bytes per pixel: blue, green, red |
32 | BI_RGB | only if biClrUsed is set | fixed 8-8-8 | 4 bytes per pixel: blue, green, red, unused |
32 | BI_BITFIELDS | only if biClrUsed is set | red, green, blue (and alpha) | 4 bytes per pixel, split by the masks |
0 | BI_JPEG or BI_PNG | none | none | a complete JPEG or PNG image |
A complete file
This 2 by 2 pixel, 24-bit image is 70 bytes long. The file header is marked, then the
40-byte BITMAPINFOHEADER, then two rows of pixels, each 6 bytes padded to 8,
bottom row first.
00000000 42 4D 46 00 00 00 00 00 00 00 36 00 00 00 28 00
00000010 00 00 02 00 00 00 02 00 00 00 01 00 18 00 00 00
00000020 00 00 10 00 00 00 13 0B 00 00 13 0B 00 00 00 00
00000030 00 00 00 00 00 00 FF 00 00 00 FF 00 00 00 00 00
00000040 FF FF FF FF 00 00
Telling it apart
The explorer’s parser for this format checks two things: the file starts with
BM (42 4D), and the DIB header size, the four bytes at offset
14, is 40, 52, 56, 108 or 124. A file that fails either check is turned down, and the
explorer tries the parsers for the other BMP formats.
A BM file with a 12-byte header is a Windows 2 and OS/2 1.x bitmap, and
one with another size from 16 to 64 bytes is an OS/2 2.x bitmap; both are separate
formats.
Not described
The gaps are those on the BMP overview, and most of them belong to this variant: the video codecs a header can name by FOURCC (shown as labelled bytes), embedded ICC profile tags the viewer shows as bytes, and the OS/2 2.x, V4 and V5 headers, which were tested only on files the site built.
Also used for
- Golly RLE pattern – Run-length encoded Game of Life pattern: x = , y = , rule = header, then b, o and $ runs.
Variants
- Windows bitmap (BM) – Windows 3.0 and later: a 40- to 124-byte header
- Windows 2 and OS/2 1.x bitmap (BM) – the first bitmaps, with a 12-byte header
- OS/2 2.x bitmap (BM) – a 16- to 64-byte header, with Huffman 1D and RLE24
- OS/2 bitmap array (BA) – several OS/2 bitmaps, icons or pointers for different screens, chained in one file
- OS/2 icon (IC) – a black and white OS/2 icon: AND and XOR masks in one 1-bit bitmap
- OS/2 colour icon (CI) – a colour OS/2 icon: a 1-bit mask bitmap followed by a colour image
- OS/2 pointer (PT) – a black and white OS/2 mouse pointer, with its hotspot in the header
- OS/2 colour pointer (CP) – a colour OS/2 mouse pointer: masks, a colour image and a hotspot
Links
Software
- GIMP – free image editor that opens and saves BMP (Windows, macOS, Linux)
- Adobe Photoshop – image editor that opens and saves BMP (Windows, macOS)
- Paint.NET – free image editor that opens and saves BMP (Windows)
- Krita – free painting program that opens and saves BMP (Windows, macOS, Linux)
- IrfanView – image viewer and converter (Windows)
- XnView MP – image viewer and batch converter (Windows, macOS, Linux)
- ImageMagick – command-line tools that convert to and from BMP (Windows, macOS, Linux)
- Photopea – image editor in the browser that opens and saves BMP (web browser)
- Preview – Apple’s built-in viewer, which opens BMP (macOS)
Developers
Reference
- Bitmap Storage – Microsoft’s description of the BMP file layout
- BITMAPINFOHEADER structure – Microsoft’s reference for the 40-byte DIB header