← BMP
BMP·Format guide
All tools

How a BMP file is built

BMP (Windows bitmap, also called DIB: device-independent bitmap) is one of the simplest image formats: a small file header, a header that describes the pixels, sometimes a colour table, and the pixels themselves, usually uncompressed. Microsoft and IBM grew it together for Windows and OS/2, so there are several header variants, told apart by their size. This page describes the layout as the BMP tool reads it and the BmpFile library.

Contents

  1. The parts of the file
  2. The file header
  3. The DIB header variants
  4. BITMAPINFOHEADER, field by field
  5. Masks, colour spaces and profiles (V2 to V5)
  6. Compression
  7. The colour table
  8. The pixel array: row order and padding
  9. Run-length encoding
  10. OS/2: core headers, arrays and icons
  11. What to look for
  12. What the tool shows, and its limits
  13. The BmpFile library

1The parts of the file

All numbers are little-endian. The parts follow each other, except the pixels, which start wherever the file header says, and a V5 profile, which can be anywhere (usually after the pixels).

Part Size Contents
file header 14 “BM”, the file size, two reserved words, the offset of the pixels.
DIB header 12 to 124 Starts with its own size, which says which variant it is; then the width, height, bit depth, compression…
channel masks 12 or 16 Only after a 40-byte header with BI_BITFIELDS or BI_ALPHABITFIELDS; the later headers hold the masks inside.
colour table entries × 3 or 4 For 1, 2, 4 and 8 bits per pixel: the palette the pixels index. Optional (a suggestion) for deeper images.
gap any Nothing requires the pixels to follow the table: the bytes between are unused.
pixel array stride × rows The rows, each padded to a multiple of 4 bytes; or the compressed data.
ICC profile any V5 only: an embedded profile, or the file name of a linked one.

2The file header (BITMAPFILEHEADER)

Offset Bytes Field Meaning
0 2 signature BM for Windows bitmaps. OS/2 also has BA (bitmap array), CI (colour icon), CP (colour pointer), IC (icon), PT (pointer).
2 4 file size The whole file, in bytes. Readers ignore it, so it is often wrong; in OS/2 icons and arrays it is the size of the headers instead.
6 2 + 2 reserved 0. In OS/2 icons and pointers: the hotspot (x, y).
10 4 pixel data offset Where the pixel array starts, from the start of the file.

A packed DIB (on the clipboard, in a .ico, in a resource) is the same thing without this header: the pixels then follow the colour table directly.

3The DIB header variants

Each new version only added fields at the end, so a reader that knows the first 40 bytes reads every Windows variant. The size is the only version number.

Size Name What it adds
12 BITMAPCOREHEADER (OS/2 1.x, Windows 2) 16-bit width and height, planes, bit count; no compression. The colour table has 3-byte entries.
16, 64 OS/2 2.x BITMAPINFOHEADER2 The 40-byte fields with OS/2's own compression numbers (3 = Huffman 1D, 4 = RLE24), then units, recording order, halftoning and colour encoding. A writer may cut it short anywhere after 16 bytes: the missing fields are 0.
40 BITMAPINFOHEADER (Windows 3) 32-bit signed width and height, compression, image size, resolution, colours used and important. The most common one.
52 BITMAPV2INFOHEADER The red, green and blue masks inside the header (Adobe; never documented by Microsoft).
56 BITMAPV3INFOHEADER And an alpha mask.
108 BITMAPV4HEADER (Windows 95) The four masks, a colour space type, the CIE XYZ endpoints of red, green and blue, and a gamma per channel.
124 BITMAPV5HEADER (Windows 98) A rendering intent, and the offset and size of an ICC profile.

4BITMAPINFOHEADER, field by field

Offset Bytes Field Meaning
0 4 size 40 (or more for the later variants).
4 4 width In pixels, signed; must be positive.
8 4 height In pixels, signed. Positive: the rows are stored bottom-up. Negative: top-down (see the pixel array).
12 2 planes Always 1.
14 2 bits per pixel 1, 4, 8 (indexes into the colour table), 16, 24, 32 (direct colour); 2 on Windows CE; 0 when the data is a JPEG or PNG; 64 in some Windows imaging code.
16 4 compression See Compression.
20 4 image size The size of the pixel data. May be 0 for uncompressed images; required for compressed ones.
24 + 28 4 + 4 resolution Pixels per metre, horizontally and vertically: 2835 ≈ 72 dpi, 3780 ≈ 96 dpi (dpi = pixels per metre × 0.0254).
32 4 colours used Entries in the colour table; 0 means all that the bit depth allows (2bits).
36 4 important colours How many entries are needed to show the image well; 0 = all. Ignored today.

5Masks, colour spaces and profiles (V2 to V5)

Masks. With BI_BITFIELDS each pixel (16 or 32 bits, read as a little-endian number) is split into channels by masks: F800 07E0 001F is 5-6-5, 00FF0000 0000FF00 000000FF FF000000 is the usual BGRA byte order with alpha. The bits of a mask must be contiguous and the masks must not overlap. After a 40-byte header the three masks (four with BI_ALPHABITFIELDS) follow the header; from V2 on they are inside it, at offsets 40–55. Without masks, 16 bits is 5-5-5 (the top bit unused) and 32 bits is 8-8-8 with an unused top byte — which many writers fill with alpha anyway.

Offset Bytes Field (V4 and V5) Meaning
40 16 masks Red, green, blue, alpha.
56 4 colour space type 0 calibrated RGB (the endpoints and gamma below), 'sRGB', 'Win ' (the system's), 'LINK' (a linked profile, V5), 'MBED' (an embedded profile, V5). The four letters are stored as a little-endian number, so the bytes read backwards.
60 36 endpoints The CIE XYZ of the red, green and blue primaries, in 2.30 fixed point.
96 12 gamma Red, green, blue, in 16.16 fixed point. Used only with calibrated RGB.
108 4 intent (V5) 1 saturation (business), 2 relative colorimetric (graphics), 4 perceptual (images), 8 absolute colorimetric.
112 4 profile offset (V5) From the start of the DIB header, not of the file.
116 4 profile size (V5) An ICC profile for 'MBED'; for 'LINK', a file name ending with a 0 byte.
120 4 reserved 0.

6Compression

Value Name Bits per pixel Data
0 BI_RGB any Uncompressed.
1 BI_RLE8 8 Run-length encoded bytes (see below).
2 BI_RLE4 4 Run-length encoded nibbles.
3 BI_BITFIELDS 16, 32 Uncompressed, with channel masks. (OS/2 2.x: Huffman 1D, 1 bit, as in fax.)
4 BI_JPEG 0 A whole JPEG file. (OS/2 2.x: RLE24.)
5 BI_PNG 0 A whole PNG file. BI_JPEG and BI_PNG were meant for printers; display code rejects them.
6 BI_ALPHABITFIELDS 16, 32 Masks with alpha after a 40-byte header (Windows CE).
11–13 BI_CMYK, BI_CMYKRLE8, BI_CMYKRLE4 32, 8, 4 CMYK pixels (printing, Windows Metafile only).

Compressed images must be bottom-up (a negative height is only valid for BI_RGB and BI_BITFIELDS), and must give the image size.

7The colour table

It follows the header (and the masks, if any). Each entry is blue, green, red, then a reserved byte (RGBQUAD); after a 12-byte core header the entries are 3 bytes (RGBTRIPLE). There are colours used entries, or 2bits when that is 0. A pixel's value is an index into the table, so an index past its end has no colour. The table of a 1-bit image needs no black and white: the two entries can be any colours.

8The pixel array: row order and padding

Bottom-up. With a positive height, the first row in the file is the bottom row of the image (a legacy of OS/2's coordinate system, where y grows upwards). A negative height means top-down, the order of almost every other format.

Row padding. Every row is padded to a multiple of 4 bytes: the stride is floor((bits × width + 31) / 32) × 4. A 13-pixel row of 24 bits is 39 bytes of pixels and 1 of padding; the pixel array is stride × |height| bytes. Pixels of less than 8 bits share bytes, the leftmost pixel in the high bits. 24-bit pixels are blue, green, red; 16-bit and 32-bit pixels are little-endian numbers split by the masks.

9Run-length encoding

RLE8 and RLE4 data is a sequence of byte pairs:

Bytes Meaning
n c (n > 0) An encoded run: n pixels of colour c. In RLE4 c holds two colours that alternate.
0 0 End of the row.
0 1 End of the bitmap.
0 2 dx dy A delta: move dx pixels right and dy rows up; the skipped pixels are left undefined (often shown as transparent or the background).
0 n … (n ≥ 3) An absolute run: n pixels as they are (n bytes in RLE8, n/2 in RLE4), padded to an even number of bytes.

OS/2's RLE24 is the same with 3-byte colours.

10OS/2: core headers, arrays and icons

OS/2 1.x wrote the 12-byte core header with 3-byte colour entries; Windows 2 used the same. OS/2 2.x has a 64-byte header that starts like BITMAPINFOHEADER but numbers compression differently. Its other file types use the same headers:

In these files the file-size field is the size of the headers. All offsets are from the start of the file.

11What to look for

12What the tool shows, and its limits

The overview (dimensions, header variant, colour, compression, problems, and a bar of how the file's bytes are used); every part of the file with each field's offset, size and decoded value, its bytes, and a link to the Hex tool; the colour tables as swatches and a table; the channel masks bit by bit; and the pixel array: stride and padding, what the padding holds, the highest colour index, the fourth byte of 32-bit pixels, and for RLE the count of each kind of code. OS/2 bitmap arrays and colour icons are listed bitmap by bitmap.

It doesn't decode or draw the pixels, decompress Huffman 1D data, or read the embedded JPEG or PNG (open those bytes in their own viewer from the Hex tool). A packed DIB without a file header (as in .ico files) isn't recognised. The pixel checks (padding, indexes) are skipped beyond 256 MB of pixel data.

13The BmpFile library

lib/image/bmp.js is a JavaScript module with no user interface and no dependencies.

import { BmpFile } from "../../lib/image/bmp.js";
Member What it is
BmpFile.parse(bytes) Reads a BMP from a Uint8Array (or ArrayBuffer); throws if it isn't one. Damage doesn't throw: it is reported in problems.
BmpFile.isBmp(bytes) Whether the bytes start with a BMP signature and a sensible DIB header size.
bmp.fileHeader { offset, signature, signatureName, declaredSize, reserved1, reserved2, hotspotX, hotspotY, pixelOffset }.
bmp.header The DIB header: { offset, size, variant, os2, width, height, topDown, planes, bitCount, compression, compressionName, imageSize, xPixelsPerMetre, yPixelsPerMetre, dpiX, dpiY, colorsUsed, colorsImportant, redMask, greenMask, blueMask, alphaMask, colorSpaceType, colorSpaceName, endpoints, gamma, intent, intentName, profileOffset, profileSize } and the OS/2 2.x fields; what the variant lacks is null. os2 is true for an OS/2 2.x header (its compression numbers differ).
bmp.masks { red, green, blue, alpha, source, length, channels: [{ name, mask, shift, bits, contiguous }] }; source is "header", "after the header" or "default" (16 and 32 bits without masks). Null for indexed and 24-bit images.
bmp.colorTable { offset, length, entrySize, expected, entries: [{ red, green, blue, reserved }] }, or null.
bmp.pixels { offset, length, available, stride, rowBytes, rows, expectedSize, uncompressedSize, compressed, embedded, rle, padding, indexes, fourthBytes }. rle: { encodedRuns, absoluteRuns, endOfLines, deltas, pixels, endOfBitmap, length, rowsCovered, overflows }; padding: { bytes, nonZero }; indexes: { highest, outside }; fourthBytes: { zero, full, other }.
bmp.profile { offset, length, kind: "embedded" | "linked", path, icc } for a V5 profile; icc is its header ({ size, cmm, version, deviceClass, colorSpace, connectionSpace, signature, creator }).
bmp.images · bmp.array Every bitmap ({ label, fileHeader, header, masks, colorTable, pixels, profile, headersEnd }): one, or several in an OS/2 array or colour icon; the members above are those of the first. The entries of an OS/2 array: [{ offset, headerSize, next, displayWidth, displayHeight }].
bmp.parts Every part in file order: [{ kind, label, offset, length, image, fields: [{ name, offset, size, value, text }], problems }]. Kinds: array header, file header, DIB header, masks, colour table, pixels, profile, gap, trailing; image is the index in images.
bmp.gaps · bmp.trailing [{ offset, length, zero }] between parts; { offset, length } after the last part, or null.
bmp.problems · bmp.fileSize What is wrong with the file, as sentences; the number of bytes.
bmp.data(part) The bytes of a part (a view into the file).
BmpFile.SIGNATURES · HEADER_VARIANTS · COMPRESSIONS · COLOR_SPACES The names of the signatures, the header variants by size, the Windows compression values and the colour space types.
const bmp = BmpFile.parse(new Uint8Array(await file.arrayBuffer()));
console.log(bmp.header.variant, bmp.header.width, Math.abs(bmp.header.height), bmp.header.bitCount + "-bit");
console.log(bmp.header.topDown ? "top-down" : "bottom-up", bmp.pixels.stride + "-byte rows");
for (const part of bmp.parts) {
	console.log(part.kind, part.offset, part.length);
	for (const field of part.fields) {
		console.log("   ", field.name, field.text);
	}
}
if (bmp.trailing) {
	console.log(bmp.trailing.length + " bytes after the image");
}