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. It appeared with Windows and OS/2 in the late 1980s and gained new header versions with Windows 3.0, Windows 95 and Windows 98, each adding to the one before.
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.
This page is the ordinary bitmap, whose file starts with BM. 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 version12 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 BITMAPCOREHEADER file allows only the uncompressed 1, 4, 8 and 24-bit
rows, and its colour table uses 3-byte entries instead of 4.
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
Variants
- Windows bitmap (BM) – Windows bitmaps: a header, an optional palette and rows of pixels
- 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