Jump to content

BMP file format

From Wikipedia, the free encyclopedia

Template:Short description Template:AboutTemplate:Infobox file format The BMP file format (also known as bitmap image file) the Windows Bitmap file format, or simply bitmap, is a raster graphics image file format used to store bitmap digital images, independently of the display device (such as a graphics adapter).[1] The underlying device-independent bitmap representation is commonly called a DIB: it consists of a DIB header, optional color information, and pixel data, describing the image independently of the display device; a BMP file wraps such a DIB in an additional 14-byte file header. It originated in the closely linked early development of Microsoft Windows and OS/2, whose bitmap formats were at first identical;[2]; used by the GDI subsystem of both platforms, it is sometimes called the Windows and OS/2 bitmap file format and is usually named with the file extension Template:Mono or Template:Mono.[3]

The BMP file format is capable of storing two-dimensional digital images in various color depths, and optionally with data compression, alpha channels, and color profiles.[1]

The format's simplicity, its familiarity on Windows, and its status as a relatively well-documented, open format have made it very widely supported, so that image-processing programs on many operating systems can read and write BMP files.[4][3] The same bitmap structure is embedded in related Windows formats: ICO and CUR files, for example, contain images that begin with a Template:Mono. Because uncompressed BMP files hold a great deal of redundant data, they can often be reduced substantially by general-purpose lossless compression such as ZIP or RAR.

The bitmap header structures were documented from the outset in Microsoft's general Windows and OS/2 programmer's references rather than in a specification dedicated to BMP alone;[1] a formal specification of the same structures, using normative language, was only later folded into the Windows Metafile (WMF) specification,[5] which Microsoft placed under its Open Specification Promise.

History

The bitmap formats associated with Microsoft Windows are among the oldest image formats on the IBM PC, having originated with the first release of Windows and evolved alongside it — and, for a time, alongside OS/2 — over the following decade. Because the format carries no field recording its header version, the successive variants are distinguished by the size of their header, and an application identifies a variant by reading the header-size field at the start of the bitmap header.[1][6]

Windows 1.0 (1985) introduced only a simple device-dependent bitmap (DDB) — not yet the device-independent, DIB-based file format described in this article — highly dependent on the output device for which it was created, so a bitmap suited to one display was not necessarily usable on another.[7] A program created such a bitmap through handle-based GDI calls and loaded it from a module's resources; the Template:Mono structure recorded only its width, height, plane count and bits-per-pixel, and the raw bits followed in the device's own format, with no color table of its own.[8]

While these early versions of Windows were being developed, Microsoft shared responsibility with IBM for the first releases of OS/2, whose Presentation Manager graphical interface used a closely related bitmap format. According to the Encyclopedia of Graphics File Formats, support for a programmable color palette was added to both Windows and BMP as Windows 2.x (1987) was being developed, making its DIB format identical to that of OS/2 1.1 (1988).[1][2] This device-independent bitmap (DIB) emerged from that joint work, and sources differ over which side originated it — and even over when it reached Windows. The Encyclopedia credits Windows itself, with Presentation Manager adopting the format afterward.[2] Other accounts instead trace the DIB to OS/2: Charles Petzold writes that the DIB "is new in Windows 3" and "an extension of the bitmap format supported in the OS/2 1.1 Presentation Manager",[7][6] and a 1990 BYTE column reported that "DIBs first surfaced in OS/2 1.1 as the Graphics Programming Interface bit map".[9] Raymond Chen, a longtime Microsoft engineer, dates this earlier, to Windows 2.0, stating that "Windows 2.0 adopted OS/2's bitmap format in order to foster interoperability" between the two systems.[10]

That early format used a 12-byte information header — later named Template:Mono — whose leading size field was required to be 12.[11] IBM's own documentation,[12] and that same Microsoft reference,[11] instead name this OS/2 header Template:Mono — the same name Microsoft later gave the unrelated 40-byte structure below. To avoid that collision, some secondary sources instead call the OS/2 1.x and 2.x headers Template:Mono and Template:Mono;[2] this article uses IBM's own names for them.

Windows 3.0 (1990) carried the device-independent bitmap forward in the form still dominant today, built around a new 40-byte Template:Mono, which superseded the earlier 12-byte header. When written to disk with a Template:Mono, this became the basis of the modern BMP file format.[6][7] The release also added run-length encoding for 4- and 8-bit-per-pixel (bpp) images (Template:Mono and Template:Mono),[6] and still accepted the older 12-byte header, which it documented as a Template:Mono "compatible with … OS/2 Presentation Manager versions 1.1 and 1.2".[6]

OS/2 2.0 (1992) then extended its version independently. The 64-byte Template:Mono widened the dimensions to 32 bits and added 24-bit RLE compression and Huffman 1D compression for bi-level images.[12][2] OS/2 also defined a bitmap-array container — identified by the Template:Mono signature and holding images tagged Template:Mono, Template:Mono, Template:Mono, Template:Mono or Template:Mono — that could bundle several device-dependent and device-independent renderings of a bitmap, icon or pointer in a single file, the device-independent version first.[12][2] As the two companies' operating-system efforts diverged, the Windows and OS/2 formats grew steadily further apart.[1]

Windows NT (1993) introduced support for using Template:Mono with 16- and 32-bpp DIBs, with the color masks following the 40-byte header instead of a color table.[1] Two further, historically undocumented header variants of 52 and 56 bytes, which fold those RGB masks (and an alpha mask) into the header itself, are also encountered (described below).

Windows 95 (1995) introduced the 108-byte Template:Mono, later also supported by Windows NT 4.0 (1996), which added an explicit alpha-channel mask together with a color-space identifier, CIE color endpoints and per-channel gamma values, allowing Image Color Matching information to travel with the bitmap.[1] Windows 98 (1998) followed with the 124-byte Template:Mono, later also supported by Windows 2000 (1999), which can embed or link an ICC color profile.[3]

Independently of this header-borne alpha mask (see below), the alpha-blending functions available in Windows 2000 and later could take a per-pixel alpha value directly from a 32-bit Template:Mono image, with no color mask involved.[13][14]

Rather than being defined in a single specification dedicated to BMP, the format's structures were documented release by release inside the general Windows and OS/2 programmer's references, header files and online help.[1] The later Windows Metafile specification was the first to define these structures formally, in normative language.[5]

Windows bitmap format

Template:Plain image with caption The bitmap image file consists of fixed-size structures (headers) as well as variable-sized structures appearing in a predetermined sequence. Many different versions of some of these structures can appear in the file, due to the long evolution of this file format. Some applications create bitmap image files which are not compliant with the Microsoft documentation; not all fields are used, and a value of 0 will be found in these unused fields.[15] All multi-byte integer fields defined by the BMP and Windows/OS/2 bitmap structures — every header field, as well as the color masks — are stored in little-endian byte order, a heritage of the format's origin on Intel-based machines.[1]

A device-independent bitmap (DIB) is a format for defining bitmaps at various color resolutions so that an image can be moved from one device to another independently of the display hardware — hence the "device-independent" in the name. Unlike a device-dependent bitmap, which exists inside the system as a bitmap object, a DIB is an external format, normally transported in metafiles, in BMP files, and on the clipboard (the Template:Mono data format).[15] A BMP file and a bare DIB are not identical, however: the file begins with the 14-byte bitmap file header (Template:Mono), whereas a DIB transported in a metafile or on the clipboard omits that header and begins directly with the DIB header.

Diagram 1 shows the layout of a BMP file using the Template:Mono, the most extensive Windows DIB header — it appears in the diagram because it contains every field added by the successive header extensions. Earlier DIB header variants contain fewer fields, but the structures they define form the initial part of the same layout; the variant actually present in a given file is determined by the DIB header's size field.

In its simplest form, a BMP file contains three structures in sequence: the 14-byte bitmap file header (Template:Mono), which identifies the file and records the offset of the pixel array; a DIB header — most commonly Template:Mono — whose size determines the header variant; and the pixel array, which holds the image data, with each row (scan line) padded to a multiple of four bytes.

Beyond this minimal layout, a file may contain further structures, depending on the pixel format and the header variant:

  • Color masks — RGB masks may follow a Template:Mono when Template:Mono is Template:Mono; in Template:Mono and later, the masks are stored as header fields.
  • Color table — an array of Template:Mono entries; required for indexed-color images (8 bpp or less) and normally absent at 16 bpp and above.
  • ICC color profile — available only with Template:Mono; it may be embedded in the file or referenced by a path to an external profile.[16]

Because the pixel array and the optional profile are located by offset fields rather than fixed positions, the format also permits small alignment gaps before them. In practice such gaps are almost always absent.

File header

This block of bytes (Template:Mono) stands at the start of the file and is used to identify it: an application typically reads it first to confirm that the file is a BMP file and is not damaged.

Windows Template:Mono[1]
Offset (bytes) Size (bytes) Member[17] Description
0 2 Template:Mono The header field used to identify the file; it must be BM (the ASCII characters "B" and "M", 0x424D).
2 4 Template:Mono The size of the BMP file in bytes
6 2 Template:Mono Reserved; must be zero
8 2 Template:Mono Reserved; must be zero
10 4 Template:Mono The offset, i.e. starting address, of the byte where the bitmap image data (pixel array) can be found.

DIB header

The DIB header records the image's dimensions, color depth, compression method and related properties. It comes in several variants that all begin with a 32-bit size field, which tells an application which variant is present, since Microsoft extended the format several times. The 12-byte Template:Mono is the oldest and simplest variant; the 40-byte Template:Mono introduced with Windows 3.0 is the common format still written today, and Template:Mono and later extend it further.

Windows DIB headers
Size (bytes) Header name OS support Features
12 Template:Mono OS/2 1.1[2]; Windows 3.0[6][7] The original and smallest DIB header; stores width and height as 16-bit values and supports no compression.
40 Template:Mono Windows 3.0[6][1] Extends bitmap width and height to 4 bytes. Adds RLE compression.
108 Template:Mono Windows NT 4.0, 95[1] Adds color space type and gamma correction
124 Template:Mono Windows 2000, 98[3] Adds a rendering intent and ICC color profiles

The 12-byte Template:Mono — identical to the OS/2 1.1 header (detailed below) — contains:

Windows Template:Mono[18]
Offset (bytes) Size (bytes) Member Description
14 4 Template:Mono The size of this header, in bytes (12)
18 2 Template:Mono The bitmap width in pixels (unsigned 16-bit)
20 2 Template:Mono The bitmap height in pixels (unsigned 16-bit)
22 2 Template:Mono The number of color planes (must be 1)
24 2 Template:Mono The number of bits per pixel

For compatibility reasons, most applications write the older Template:Mono, which remains the common Windows format; it is described in the next table. Unless explicitly noted, the members in this table are unsigned integers.

Windows Template:Mono[1]
Offset (bytes) Size (bytes) Member[19] Description
14 4 Template:Mono The size of this header, in bytes (40)
18 4 Template:Mono The bitmap width in pixels (signed integer, but required to be positive)[20]
22 4 Template:Mono The bitmap height in pixels (signed integer, must not be zero); a negative value selects top-down row order (see below)[20]
26 2 Template:Mono The number of color planes (must be 1)
28 2 Template:Mono The number of bits per pixel, which is the color depth of the image. Typical values are 1, 4, 8, 16, 24 and 32.
30 4 Template:Mono The compression method being used. Listed below
34 4 Template:Mono The image size. This is the size of the raw bitmap data; a dummy 0 can be given for BI_RGB bitmaps.
38 4 Template:Mono The bitmap's intended horizontal resolution on the target device, in pixels per meter (signed integer); an application can use it to pick the best-matching resource or print scale, but GDI itself never reads the field. 0 means unspecified.[15]
42 4 Template:Mono The intended vertical resolution, in pixels per meter (signed integer); see Template:Mono above[15]
46 4 Template:Mono The number of color table entries used by the bitmap, or, for 16 bpp and higher, the size of a color table used for palette optimization only — not as an index table for pixel values; 0 means the default size (see below)
50 4 Template:Mono The number of color table entries considered important for display, or 0 if all are (see below)

Versions after Template:Mono only add fields to its end. Two further extensions, rarely encountered and never officially documented by Microsoft, are known: the 52-byte Template:Mono adds fields to Template:Mono for red, green and blue color masks, and the 56-byte Template:Mono in turn adds a field to Template:Mono for an alpha channel mask, together forming an RGBA mask set later standardized as header fields in Template:Mono (see below). Their existence was confirmed by an Adobe employee, who stated that this documentation had at one point been included in official Microsoft documentation.[21] The same engineer stated that Photoshop writes only two DIB header sizes — the 40-byte Template:Mono and the 56-byte Template:Mono — reserving the latter for its advanced formats, among them 16-bpp images that carry an alpha channel, such as A1R5G5B5 and A4R4G4B4.[21] GIMP likewise offers 16-bpp A1R5G5B5 among its documented BMP export formats.[22]

The compression method (offset 30) is most often Template:Mono (0), meaning no compression. Template:Mono (1) and Template:Mono (2) apply run-length encoding and are limited to 8- and 4-bpp bitmaps respectively. Template:Mono (3), restricted to 16- or 32-bpp images,[19] means that three color masks specify the RGB bit layout for each pixel; their placement relative to the header is detailed below.

A handful of further values exist but are rarely encountered in practice: Template:Mono (4) and Template:Mono (5) embed a JPEG or PNG image in place of raw pixel data (from Template:Mono on); these values are documented as valid only for the Template:Mono and Template:Mono functions, and only when their destination is a printer device, letting an application forward already-compressed image data straight to printers with hardware JPEG/PNG support rather than for general bitmap storage or display.[23] Template:Mono (6) is a Windows CE extension, valid in Windows CE .NET 4.0 and later,[24] and is described below; Windows CE also departs from the desktop format with a non-standard 2 bpp format (below). Template:Mono (11), Template:Mono (12) and Template:Mono (13) appear only inside Windows Metafiles, holding uncompressed, RLE-8 and RLE-4 CMYK images respectively.[5]

Template:Mono and later reuse Template:Mono's first 40 bytes unchanged, each member merely renamed with a Template:Mono/Template:Mono prefix, and then append further fields.

Windows Template:Mono extension fields
Offset (bytes) Size (bytes) Member[25] Description
54 4 Template:Mono Mask selecting the red bits of each pixel; used only when Template:Mono is Template:Mono (see Color masks below)
58 4 Template:Mono Mask selecting the green bits of each pixel
62 4 Template:Mono Mask selecting the blue bits of each pixel
66 4 Template:Mono Mask selecting the alpha bits of each pixel
70 4 Template:Mono The color space of the DIB. The possible values are listed below
74 36 Template:Mono The CIE XYZ coordinates of the color space's red, green and blue endpoints, used only when Template:Mono is Template:Mono
110 4 Template:Mono Gamma response curve for red, in unsigned 16.16 fixed-point format; used only when Template:Mono is Template:Mono
114 4 Template:Mono Gamma response curve for green
118 4 Template:Mono Gamma response curve for blue

Template:Mono in turn keeps every Template:Mono field unchanged, renamed with a Template:Mono prefix, and appends four more fields to support linked or embedded ICC color profiles:

Windows Template:Mono extension fields
Offset (bytes) Size (bytes) Member[26] Description
122 4 Template:Mono The rendering intent to use for the bitmap. The possible values are listed below
126 4 Template:Mono Offset, in bytes from the start of this header, to an embedded or linked ICC profile
130 4 Template:Mono Size, in bytes, of the embedded profile data
134 4 Template:Mono Reserved; should be zero

Color management

Template:See also Template:Mono and Template:Mono (offset 70) identify the color space of the DIB.[27][28] Template:Mono (0) means the endpoints and gamma values are given in the endpoint and gamma fields of the Template:Mono extension table above; Template:Mono (0x73524742) selects sRGB, and Template:Mono (0x57696E20) the Windows default color space, which on modern Windows is sRGB.[29] These three logical color-space values are available in Template:Mono and are also valid in Template:Mono.

Template:Mono accepts two further values for Template:Mono: Template:Mono (0x4C494E4B) and Template:Mono (0x4D424544), specific to it since Template:Mono lacks the profile-data fields they require. For all values other than Template:Mono, the endpoint and gamma fields above are ignored.[26]

When Template:Mono is Template:Mono or Template:Mono, Template:Mono gives the offset, in bytes from the beginning of the Template:Mono structure, to the profile data; for a packed DIB, and similarly in the BMP file format, this data follows the pixel array.[26] For Template:Mono, the data found there is the ICC profile itself; for Template:Mono, it is instead the null-terminated path of an external profile file — which may be a fully qualified local or network path — composed of characters from the Windows-1252 code page, not Unicode. For an embedded profile, Template:Mono gives the size of the profile data in bytes.[26]

Template:Mono (offset 122, Template:Mono only) sets the rendering intent.[26][30][31] It can be Template:Mono (1), the ICC Saturation intent, which maintains saturation and is used for business charts and other situations requiring undithered colors; Template:Mono (2), Relative Colorimetric, which maintains a colorimetric match and is used for graphic designs and named colors; Template:Mono (4), Perceptual, which maintains contrast and is used for photographs and natural images; or Template:Mono (8), Absolute Colorimetric, which maintains the white point and matches colors to their nearest color in the destination gamut.

Several classic GDI bitmap functions use the color-space information in the header when performing color matching: Template:Mono, Template:Mono, Template:Mono, Template:Mono and Template:Mono do so only when it is Template:Mono or Template:Mono; for any other header, the current device context's own color profile (or, failing that, sRGB) is used instead.[32]

Color masks

Template:See also Color masks specify exactly which bits, within a 16- or 32-bit pixel, belong to each color channel. These masks can be supplied in two ways: when the compression method is Template:Mono, three Template:Mono color masks — for red, green and blue — follow the header;[1] alternatively, versions after Template:Mono include the masks as part of the header itself (see above for how this developed across versions), with all four — red, green, blue and alpha — officially documented as header fields from Template:Mono onward.[25] Of these, only the red, green and blue masks are tied to Template:Mono — valid only under that compression method, whichever way they are supplied — while the alpha mask, available only as a header field, carries no such restriction and is described as always valid, regardless of compression method.[25][33] The following example illustrates the mechanism:

A sample 16-bit color-mask layout, R5G6B5, from the most significant bit to the least
Channel Bit positions Width (bits) Mask
Red 15–11 5 0x0000F800
Green 10–5 6 0x000007E0
Blue 4–0 5 0x0000001F

The bit fields defined by the color masks must be contiguous and non-overlapping, but their order is arbitrary; a common bit-significance ordering is A-R-G-B, from the most significant bit to the least significant.[1]

A sample 32-bit color-mask layout, A8R8G8B8, in the most common order, with the alpha mask supplied as a header field
Channel Bit positions Width (bits) Mask
Alpha 31–24 8 0xFF000000
Red 23–16 8 0x00FF0000
Green 15–8 8 0x0000FF00
Blue 7–0 8 0x000000FF

When no masks are supplied at all, a fixed default layout is assumed for the bit depth, shown below for every depth.

The standard pixel formats in a DIB
Bits per pixel Template:Mono
(no compression)[18]
Template:Mono and later[19]d
Template:Mono Template:Mono
1 2 colorsa up to 2 colorsb Not defined
4 16 colorsa up to 16 colorsb Not defined
8 256 colorsa up to 256 colorsb Not defined
16 Not defined X1R5G5B5 user-defined masksc
24 R8G8B8 R8G8B8 Not defined
32 Not defined X8R8G8B8 user-defined masksc

Template:Block indent

A less common extension, found on Windows CE, adds a fourth compression value, Template:Mono, which appends a fourth mask, for alpha, directly after the three color masks — the trailing-mask counterpart, for the plain Template:Mono, of the alpha mask that versions after it instead carry as a header field, officially documented as such from Template:Mono onward.[24]

Color table

Template:See also The color table, or palette, lists the colors used by the image. In an indexed-color bitmap each pixel is stored as a number of bits (1, 4 or 8) that form an index into this table; the entry it selects gives the color the pixel is displayed in.[1] The table thus tells the application which actual color each index value represents. When present, it follows the header and any trailing color masks (see above).

For indexed-color images, the default number of entries is 2n, where n is the bit depth (the biBitCount member); a smaller count may be given instead in the biClrUsed member, and a value of zero means the table uses the full, default size for that bit depth.[19] The biClrImportant member can likewise record how many of those color indexes are actually required to display the bitmap; a value of zero means all of them are.[19] Unlike biClrUsed, biClrImportant is purely advisory for applications — GDI itself never reads it.[15]

In Windows bitmaps each entry occupies 4 bytes in the order blue, green, red, 0x00 — the X8R8G8B8 layout (a Template:Mono), whose fourth byte is reserved rather than a true alpha channel.

The Template:Mono format handles the color table differently: it supports only the full-size table, with no biClrUsed-style override, and each entry is 3 bytes, in blue, green, red order (a Template:Mono), rather than the 4-byte Template:Mono used elsewhere.[2][15]

Because the palette is always stored explicitly in the file, the choice of colors is left to the encoder.[1] A monochrome (1 bpp) image carries a two-entry table that is most often black and white, although the two entries are ordinary palette colors and may be any pair. By convention, 16-color (4 bpp) images tend to use a fixed, standard palette — typically the 16-color VGA/EGA palette, whose entries also correspond closely to the reserved static colors that Windows keeps in its system palette and does not allow to be changed.[34] 256-color (8 bpp) images, by contrast, are more often given a palette tailored to the individual image through color quantization.

One edge case falls outside this description: for DIBs held in memory rather than in a file, the color table may instead consist of 2-byte entries that are indexes into the currently realized palette,[35] rather than explicit color values.

At 16 bpp and higher the pixels carry their color directly, so a color table is normally absent, but it may be present as a palette for optimizing colors on palette-based devices — in which case Template:Mono specifies its size.[19] Even then, it is not an indexed palette, and none of its entries are read back as pixel indices.

Pixel array

The pixel array (also called the bitmap data) holds the actual image, one row (also called a scan line) at a time; its offset from the start of the file is given by the file header, after the DIB header and any color table or color masks.[15] The size of a stored row, including padding, is commonly called its stride. For uncompressed bitmaps, the pixel array consists of such DWORD-aligned scan lines; RLE-compressed bitmaps instead hold a variable-length encoded stream (see below).

Row order

Rows are usually stored "bottom-up": the first row of the pixel data is the bottom of the image, and each row runs left to right.[15] When the Image Height value is negative, an uncompressed bitmap is instead stored "top-down", with the first row at the top of the image;[15] top-down DIBs cannot be compressed, so their compression value must be Template:Mono or Template:Mono.[19] Template:Mono can never select top-down order this way, since its height field is unsigned (see above).

Row padding

The size of each row is rounded up to a multiple of 4 bytes (a 32-bit DWORD) by padding; the padding bytes are not necessarily zero.[36][37] The total number of bytes necessary to store one row of pixels can be calculated as: <math display="block">\text{RowSize} = \left\lceil\frac { \text{BitsPerPixel} \cdot \text{ImageWidth}}{32}\right\rceil \cdot 4 = \left\lfloor\frac { \text{BitsPerPixel} \cdot \text{ImageWidth}+31}{32}\right\rfloor \cdot 4,</math> Template:Block indent

For images with height above 1, multiple padded rows are stored consecutively, forming the pixel array, whose total size can be calculated by accounting for the effect of rounding up the size of each row to a multiple of 4 bytes, as follows:

<math display="block">\text{PixelArraySize} = \text{RowSize} \cdot |\text{ImageHeight}|</math> Template:Block indent

When the pixel array is loaded into memory, each row must begin at a memory address that is a multiple of 4; this restriction applies only in memory — for file storage, only the size of each row must be a multiple of 4 bytes, while the offset of the pixel array itself can be arbitrary.[15] A 24 bpp bitmap with Width=1 would have 3 bytes of data per row (blue, green, red) and 1 byte of padding, while Width=2 would have 6 bytes of data and 2 bytes of padding, Width=3 would have 9 bytes of data and 3 bytes of padding, and Width=4 would have 12 bytes of data and no padding.

Pixel formats

The Template:Mono (and later) supports 1, 4, 8, 16, 24 and 32 bpp;[19] the older Template:Mono is limited to 1, 4, 8 or 24 bpp, its Template:Mono field excluding 16 and 32.[18] Within a row, pixels are packed as follows:

  • At 1 bpp, each bit is one pixel, the left-most pixel in the most significant bit of the first byte; each bit indexes a two-entry color table.[15]
  • At 4 bpp, each byte holds two pixels, the left-most in the more significant nibble; each nibble indexes a color table of up to 16 colors.[15]
  • At 8 bpp, each byte is one pixel, indexing a color table of up to 256 colors.[15]
  • At 16 bpp, each pixel is a 2-byte WORD; by default its bits form the X1R5G5B5 layout (the top bit unused), or one given by color masks (commonly R5G6B5).[19]
  • At 24 bpp, each pixel is 3 bytes in blue, green, red order (R8G8B8).[15][37]
  • At 32 bpp, each pixel is a 4-byte DWORD; by default its bits form the X8R8G8B8 layout (the high byte unused), or one given by color masks (commonly A8R8G8B8).[19][38]

The standard Template:Mono layout does not assign a color channel to the fourth byte of a 32-bit pixel. Windows' GDI can nonetheless treat it as a per-pixel alpha value: its Template:Mono function supports per-pixel alpha only for 32-bit Template:Mono data, always taking the alpha from a fixed high byte (the bytes being blue, green, red, alpha in memory) — no color mask is involved, and this holds regardless of which DIB header describes the bitmap, so even a Template:Mono's or Template:Mono's own Template:Mono field is not consulted.[13] Template:Mono expects the alpha to be premultiplied and has been available since Windows 2000.[14] Applications that parse the header themselves, by contrast, such as GIMP and Google Chrome, do honor a declared Template:Mono when reading a file.[39][40]

A few pixel formats fall outside this standard set. At 2 bpp, found only on Windows CE, each byte holds four pixels, the left-most in the two most significant bits, each indexing a color table of up to 4 colors.[41] At 64 bpp, a GDI+-specific extension to the header's bit-depth field (undocumented in the core Template:Mono spec),[42] each of the alpha, red, green and blue channels occupies 16 bits.[38]

Compression

Template:See also Indexed images at 4 or 8 bpp may be compressed with a run-length encoding (RLE) scheme, Template:Mono or Template:Mono respectively (see the compression values above); ordinary pixel arrays at 1, 16, 24 or 32 bpp are otherwise always stored uncompressed, and any depth may be stored uncompressed if desired.[1] Template:Mono and Template:Mono are a separate case, reserved for specific GDI printer operations rather than ordinary pixel storage (see the compression values above).[23] A run is normally encoded, in encoded mode, as a count byte followed by a single byte that either gives the color index to repeat (Template:Mono) or packs two alternating color indices into its high and low nibbles (Template:Mono); a count of zero instead introduces one of three escapes: end of line, end of bitmap, or a delta that skips to a new position without drawing. In absolute mode (also flagged by a zero count byte), the following byte instead gives the number, 3–255, of literal pixels that follow — one color-index byte per pixel for Template:Mono, or two nibble-packed pixels per byte for Template:Mono — with the run zero-padded to end on a 16-bit word boundary.[43][37]

File:Bmp format.svg
Example of a 2×2 pixel bitmap, with 24 bits/pixel encoding

Example

The following is an example of a 2×2-pixel, 24 bpp (R8G8B8) bitmap using the Windows Template:Mono DIB header.

Offset (bytes) Size (bytes) Hex value Value Description
File header
0 2 42 4D "BM" ID field (42 4D)
2 4 46 00 00 00 70 bytes (54+16) Size of the BMP file (54 bytes header + 16 bytes data)
6 2 00 00 0 Template:Mono — reserved; must be zero
8 2 00 00 0 Template:Mono — reserved; must be zero
10 4 36 00 00 00 54 bytes (14+40) Offset where the pixel array (bitmap data) can be found
DIB header
14 4 28 00 00 00 40 bytes Number of bytes in the DIB header (from this point)
18 4 02 00 00 00 2 pixels (left to right order) Width of the bitmap in pixels
22 4 02 00 00 00 2 pixels (bottom to top order) Height of the bitmap in pixels. Positive for bottom to top pixel order.
26 2 01 00 1 plane Number of color planes being used
28 2 18 00 24 bits Number of bits per pixel
30 4 00 00 00 00 0 BI_RGB, no pixel array compression used
34 4 10 00 00 00 16 bytes Size of the raw bitmap data (including padding)
38 4 13 0B 00 00 2835 pixels/metre horizontal Print resolution of the image,
72 DPI × 39.3701 inches per metre yields 2834.6472
42 4 13 0B 00 00 2835 pixels/metre vertical
46 4 00 00 00 00 0 colors Number of colors in the palette
50 4 00 00 00 00 0 important colors 0 means all colors are important
Pixel array
54 3 00 00 FF 0 0 255 Red, Pixel (x=0, y=1)
57 3 FF FF FF 255 255 255 White, Pixel (x=1, y=1)
60 2 00 00 0 0 Padding for 4 byte alignment (could be a value other than zero)
62 3 FF 00 00 255 0 0 Blue, Pixel (x=0, y=0)
65 3 00 FF 00 0 255 0 Green, Pixel (x=1, y=0)
68 2 00 00 0 0 Padding for 4 byte alignment (could be a value other than zero)

This example illustrates several properties of the BMP format at once. The pixel array begins at offset 54, immediately after the 14-byte file header and 40-byte DIB header. Because the image is 24 bpp and two pixels wide, each row contains 6 bytes of pixel data and 2 bytes of padding. The positive height means that the first stored row is the bottom row of the image.

OS/2 bitmap format

The headers and behavior described in this section are OS/2-specific — rarely produced by current software, but still occasionally encountered when reading older bitmap files.

IBM's documentation, and the Microsoft-published OS/2 Programmer's Reference, name the OS/2 1.x and 2.x DIB headers Template:Mono and Template:Mono respectively — the names this section uses below.[12][11]

Structurally, an OS/2 bitmap file is a chain of structures, each nested as a literal member of the structure enclosing it, down to the pixel array at the bottom. When the file uses the bitmap-array container format (see below), a bitmap-array header sits at the very top of this chain — itself never nested inside anything else — wrapping an ordinary file header, which in turn wraps a DIB header, itself immediately followed by a color table when one is present.[12] Because of this nesting, each structure's own recorded size is cumulative: a Template:Mono is 26 bytes (14 plus its nested 12-byte Template:Mono), a Template:Mono up to 78 (14 plus a Template:Mono of up to 64 bytes, less if truncated; see below), a Template:Mono 40 (14 plus the 26-byte Template:Mono), and a Template:Mono up to 92 (14 plus a Template:Mono of up to 78 bytes).[12][44] In practice, though, this recorded size (Template:Mono) is often simply written as 0;[2] parsers should instead rely on the nested header's own size field to determine its variant — just as Template:Mono (below) determines the resource type — and on Template:Mono rather than Template:Mono to walk a bitmap array.[44]

File headers

This file header is analogous to the Windows Template:Mono (above), but with two differences: Windows' two reserved 16-bit fields instead hold the X and Y coordinates of a hotspot, used only when the file is an icon or pointer; and the size field records the size of the header structure itself, rather than the size of the whole file.[12][45]

Its type field (Template:Mono in IBM's Template:Mono/Template:Mono, Template:Mono in Windows) also records which kind of resource the file holds:

OS/2 Template:Mono signatures[12][2]
Signature Value (hex) Constant Resource type
BM 0x4D42 Template:Mono Bitmap
BA 0x4142 Template:Mono Bitmap array
IC 0x4349 Template:Mono Icon
CI 0x4943 Template:Mono Color icon
PT 0x5450 Template:Mono Pointer (mouse cursor)
CP 0x5043 Template:Mono Color pointer
OS/2 Template:Mono/Template:Mono[12][45][11]
Offset (bytes) Size (bytes) Member Description
0 2 Template:Mono The resource-type signature (see the table above)
2 4 Template:Mono The size of this file header structure in bytes — nominally cumulative with its nested DIB header, but in practice often simply written as 0 (see above); Windows' Template:Mono instead gives the size of the entire file
6 2 Template:Mono For an icon or pointer, the X coordinate of its hotspot; ignored for plain bitmaps (occupies the position of Windows' unused Template:Mono)
8 2 Template:Mono The corresponding Y coordinate of the hotspot (Windows' Template:Mono)
10 4 Template:Mono The offset, in bytes, from the start of this structure to the pixel data (Windows' Template:Mono)
14 variable Template:Mono / Template:Mono The nested information header: Template:Mono inside a Template:Mono, Template:Mono inside a Template:Mono (see below)

Bitmap arrays

A separate container format, with no Windows counterpart, bundles several device-dependent or device-independent renderings of the same bitmap, icon or pointer — for different display resolutions or color depths — together in one file as a bitmap array. Each rendering gets its own complete chain, headed by a Template:Mono (OS/2 1.x) or Template:Mono (OS/2 2.x), signature BA, wrapping an ordinary file header for that rendering. These per-rendering chains sit side by side in the file rather than nested inside one another: their array headers form a singly linked list, each one pointing, via an absolute file offset rather than one relative to itself, to the next (zero ending the chain); each also carries the pixel dimensions of the display device it targets (for example, 640×480 for VGA), and the device-independent rendering is required to come first.[12][2]

OS/2 Template:Mono/Template:Mono[12][2]
Offset (bytes) Size (bytes) Member Description
0 2 Template:Mono Template:Mono (BA)
2 4 Template:Mono The size of this array-header structure in bytes — nominally cumulative with its nested file header (see above)
6 4 Template:Mono The absolute file offset of the next array header — not an offset relative to this one — or zero if this is the last one
10 2 Template:Mono The width, in pixels, of the display device this rendering targets (for example, 640 for VGA)
12 2 Template:Mono The corresponding height of the target display device (for example, 480 for VGA)
14 variable Template:Mono / Template:Mono The file header for this rendering (Template:Mono or Template:Mono)

IBM shipped its own system icons this way: a single array could bundle a 32×32 4 bpp rendering for VGA together with a 40×40 4 bpp rendering for 8514/A and XGA displays, alongside smaller 1 bpp 32×32, 20×20 and 16×16 mini-icon renderings.[12]

Icons and pointers

Icons and pointers (mouse cursors) are stored as bitmaps using the same family of file headers, with the hotspot recorded in the file header. The AND and XOR masks always occupy a single 1-bit-per-pixel bitmap twice the declared height (Template:Mono/Template:Mono), XOR first and AND second — the same arrangement used by monochrome Windows icons and cursors. A color icon or pointer (Template:Mono/Template:Mono) adds a separate color layer after this one, giving two consecutive file headers: the AND/XOR bitmap first, the color layer second. For the mechanics of combining these masks, see ICO/CUR.[12][46]

DIB headers

The OS/2 1.1 header (Template:Mono) is byte-for-byte identical to the Windows Template:Mono (detailed above), merely using IBM's own field names — Template:Mono, Template:Mono, Template:Mono, Template:Mono and Template:Mono, corresponding to Windows' Template:Mono, Template:Mono, Template:Mono, Template:Mono and Template:Mono respectively.[18]

The OS/2 2.x Template:Mono corresponds field-for-field to the Windows 3.0 Template:Mono (above), then adds 24 further bytes:[12][2][47]

OS/2 Template:Mono, additional fields[2]
Offset (bytes) Size (bytes) Member[12] Description[2]
54 2 Template:Mono An enumerated value specifying the units for the horizontal and vertical resolutions (offsets 38 and 42). The default value, Template:Mono (0), means pixels per metre
56 2 Template:Mono Padding. Ignored and should be zero
58 2 Template:Mono An enumerated value indicating the direction in which the bits fill the bitmap. The default value, Template:Mono (0), means the origin is the lower-left corner. Bits fill from left-to-right, then bottom-to-top.

Note that Windows bitmaps (which don't include this field) can also specify an upper-left origin (bits fill from left-to-right, then top-to-bottom) by using a negative value for the image height

60 2 Template:Mono An enumerated value indicating a halftoning algorithm that should be used when rendering the image.
62 4 Template:Mono Halftoning parameter 1 (see below)
66 4 Template:Mono Halftoning parameter 2 (see below)
70 4 Template:Mono An enumerated value indicating the color encoding for each entry in the color table. The default value, Template:Mono (0), specifies RGB triples
74 4 Template:Mono An application-defined identifier. Not used for image rendering

The structure can be truncated after Template:Mono or any subsequent field, with the omitted fields taking their default (generally zero) values[12][48] — a truncated 16-byte form of the Template:Mono is encountered[2] in, for example, the file pal8os2v2-16.bmp[49] of the BMP Suite.[50]

OS/2 bitmaps support only 1, 4, 8, or 24 bpp.[12][2][15]

Compression

OS/2 2.x stores the compression scheme in the same field as Windows (Template:Mono), but assigns some of the same values to different, OS/2-specific schemes: besides RLE for 4- and 8-bpp images (shared with Windows), it defines Template:Mono (3), a modified Huffman encoding for bi-level (1 bpp) images, and Template:Mono (4), a run-length encoding for 24 bpp images.[12][2]

Halftoning

Rather than affecting how the pixel data itself is stored, Template:Mono (offset 60) records which halftoning algorithm should be used when rendering the image.[12] Its default and most common value, Template:Mono (0), disables halftoning. Template:Mono (1) selects error diffusion, whose damping is set by halftoning parameter 1 (Template:Mono, offset 62) as a percentage — 100 indicating no damping, 0 indicating that errors are not diffused. Template:Mono (2) selects PANDA, the Processing Algorithm for Non-coded Document Acquisition,[51] and Template:Mono (3) selects the Super Circle algorithm;[52] for both, halftoning parameters 1 and 2 (Template:Mono/Template:Mono, offsets 62 and 66, respectively) give the X and Y dimensions, in pixels, of the halftoning pattern used.

References

Template:Reflist

  • BMP Suite – a collection of test BMP files covering edge cases and non-standard variants, useful for exercising decoders

Template:Compression formats Template:Graphics file formats

  1. ↑ 1.00 1.01 1.02 1.03 1.04 1.05 1.06 1.07 1.08 1.09 1.10 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.18 Template:Cite book
  2. ↑ 2.00 2.01 2.02 2.03 2.04 2.05 2.06 2.07 2.08 2.09 2.10 2.11 2.12 2.13 2.14 2.15 2.16 2.17 Template:Cite book
  3. ↑ 3.0 3.1 3.2 3.3 {{#invoke:citation/CS1|citation |CitationClass=web }}
  4. ↑ Template:Cite book
  5. ↑ 5.0 5.1 5.2 {{#invoke:citation/CS1|citation |CitationClass=web }}
  6. ↑ 6.0 6.1 6.2 6.3 6.4 6.5 6.6 Template:Cite book
  7. ↑ 7.0 7.1 7.2 7.3 Template:Cite book
  8. ↑ Template:Cite book
  9. ↑ Template:Cite magazine
  10. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  11. ↑ 11.0 11.1 11.2 11.3 Template:Cite book
  12. ↑ 12.00 12.01 12.02 12.03 12.04 12.05 12.06 12.07 12.08 12.09 12.10 12.11 12.12 12.13 12.14 12.15 12.16 12.17 12.18 Template:Cite book
  13. ↑ 13.0 13.1 {{#invoke:citation/CS1|citation |CitationClass=web }}
  14. ↑ 14.0 14.1 {{#invoke:citation/CS1|citation |CitationClass=web }}
  15. ↑ 15.00 15.01 15.02 15.03 15.04 15.05 15.06 15.07 15.08 15.09 15.10 15.11 15.12 15.13 15.14 {{#invoke:citation/CS1|citation |CitationClass=web }}
  16. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  17. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  18. ↑ 18.0 18.1 18.2 18.3 {{#invoke:citation/CS1|citation |CitationClass=web }}
  19. ↑ 19.00 19.01 19.02 19.03 19.04 19.05 19.06 19.07 19.08 19.09 {{#invoke:citation/CS1|citation |CitationClass=web }}
  20. ↑ 20.0 20.1 {{#invoke:citation/CS1|citation |CitationClass=web }}
  21. ↑ 21.0 21.1 {{#invoke:citation/CS1|citation |CitationClass=web }}
  22. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  23. ↑ 23.0 23.1 {{#invoke:citation/CS1|citation |CitationClass=web }}
  24. ↑ 24.0 24.1 {{#invoke:citation/CS1|citation |CitationClass=web }}
  25. ↑ 25.0 25.1 25.2 {{#invoke:citation/CS1|citation |CitationClass=web }}
  26. ↑ 26.0 26.1 26.2 26.3 26.4 {{#invoke:citation/CS1|citation |CitationClass=web }}
  27. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  28. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  29. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  30. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  31. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  32. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  33. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  34. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  35. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  36. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  37. ↑ 37.0 37.1 37.2 Template:Cite magazine
  38. ↑ 38.0 38.1 {{#invoke:citation/CS1|citation |CitationClass=web }}
  39. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  40. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  41. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  42. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  43. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  44. ↑ 44.0 44.1 Template:Cite magazine
  45. ↑ 45.0 45.1 Template:Cite book
  46. ↑ Template:Cite book
  47. ↑ Template:Cite book
  48. ↑ Template:Cite magazine
  49. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  50. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  51. ↑ Template:Cite journal
  52. ↑ Template:Cite journal