BMP file format

The BMP file format, or bitmap, is a raster graphics image file format used to store bitmap digital images, independently of the display device (such as a graphics adapter), especially on Microsoft Windows[2] and OS/2[3] operating systems.

The Windows Metafile (WMF) specification covers the BMP file format.

The main purpose of DIBs is to allow bitmaps to be moved from one device to another (hence, the device-independent part of the name).

A DIB is normally transported in metafiles (usually using the StretchDIBits() function), BMP files, and the Clipboard (CF_DIB data format).The following sections discuss the data stored in the BMP file or DIB in detail.

Referring to the diagram 1, the bitmap file is composed of structures in the following order: A bitmap image file loaded into memory becomes a DIB data structure – an important component of the Windows GDI API.

In all cases, the pixel array must begin at a memory address that is a multiple of 4 bytes.

In all cases, the pixel array must begin at a memory address that is a multiple of 4 bytes.

In some cases it may be necessary to adjust the number of entries in the color table in order to force the memory address of the pixel array to a multiple of 4 bytes.

[8] For "packed DIBs" loaded in memory, the optional color profile data should immediately follow the pixel array, as depicted in diag.

All of them contain a dword (32-bit) field, specifying their size, so that an application can easily determine which header is used in the image.

An integrated alpha channel has been introduced with the undocumented BITMAPV3INFOHEADER and with the documented BITMAPV4HEADER (since Windows 95) and is used within Windows XP logon and theme system as well as Microsoft Office (since v2000); it is supported by some image editing software, such as Adobe Photoshop since version 7 and Adobe Flash since version MX 2004 (then known as Macromedia Flash).

All values are stored as unsigned integers, unless explicitly noted.The compression method (offset 30) can be: An OS/2 2.x OS22XBITMAPHEADER (BITMAPINFOHEADER2 in IBM's documentation) contains 24 additional bytes:[3] 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 The halftoning algorithm (offset 60) can be: The color table (palette) occurs in the BMP image file directly after the BMP file header, the DIB header, and after the optional three or four bitmasks if the BITMAPINFOHEADER header with BI_BITFIELDS (12 bytes) or BI_ALPHABITFIELDS (16 bytes) option is used.

Note: On Windows CE the BITMAPINFOHEADER header can be used with the BI_ALPHABITFIELDS[6] option in the biCompression member.

[3][5] In most cases, each entry in the color table occupies 4 bytes, in the order blue, green, red, 0x00 (see below for exceptions).

This may seem like a contradiction if no distinction is made between the mandatory palette entries and the optional color list.

The bits representing the bitmap pixels are packed in rows (also known as strides or scan lines).

[18] For images with height above 1, multiple padded rows are stored consecutively, forming a Pixel Array.

The total number of bytes necessary to store an array of pixels in an n bits per pixel (bpp) image, with 2n colors, can be calculated by accounting for the effect of rounding up the size of each row to a multiple of 4 bytes, as follows:

[5] Unless BITMAPCOREHEADER is used, uncompressed Windows bitmaps also can be stored from the top to bottom, when the Image Height value is negative.

In the original OS/2 DIB, the only four legal values of color depth were 1, 4, 8, and 24 bits per pixel (bpp).

The most ubiquitous field order is: Alpha, Blue, Green, Red (MSB to LSB).

The red, green and blue bit masks are valid only when the Compression member of the DIB header is set to BI_BITFIELDS.

Note that the bitmap data starts with the lower left hand corner of the image.

Many older graphical user interfaces used bitmaps in their built-in graphics subsystems;[25] for example, the Microsoft Windows and OS/2 platforms' GDI subsystem, where the specific format used is the Windows and OS/2 bitmap file format, usually named with the file extension of .BMP.

Some formats, such as RAR, even include routines specifically targeted at efficient compression of such data.

Example 1 of a 2×2 pixel bitmap, with 24 bits/pixel encoding