BMP file format

{{Short description|Windows Bitmap image file format family}}

{{About|the file format|the data structure/type of image|Bitmap graphics}}{{Infobox file format

| name = Windows Bitmap

| extension = .bmp, .dib

| mime = image/bmp{{cite IETF|rfc=7903|title=Windows Image Media Types|section=5|sectionname=IANA Considerations}}
image/x-bmp

| _nomimecode = y

| type code = 'BMP '
'BMPf'
'BMPp'

| uniform type = com.microsoft.bmp

| genre = Raster graphics

| open = OSP for WMF

| developer = Microsoft Corporation

}}

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{{cite book|title=Encyclopedia of Graphics File Formats|edition=Second|date=April 1996|author=James D. Murray|author2=William vanRyper|isbn=1-56592-161-5|publisher=O'Reilly|at=[https://archive.org/details/mac_Graphics_File_Formats_Second_Edition_1996/page/ bmp]|url=https://archive.org/details/mac_Graphics_File_Formats_Second_Edition_1996/page/|access-date=2014-03-07}} and OS/2 operating systems.

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. The Windows Metafile (WMF) specification covers the BMP file format.{{cite web|url=http://msdn.microsoft.com/en-us/library/cc250370.aspx|title=[MS-WMF]: Windows Metafile Format|publisher=MSDN|date=2014-02-13|access-date=2014-03-12}}

Device-independent bitmaps and the BMP file format

{{Plain image with caption|BMPfileFormat.svg|align=right|width=320px|caption=Diagram 1 – The structure of the bitmap image file}}

Microsoft has defined a particular representation of color bitmaps of different color depths, as an aid to exchanging bitmaps between devices and applications with a variety of internal representations. They called these device-independent bitmaps or DIBs, and the file format for them is called DIB file format or BMP image file format.

According to Microsoft support:{{cite web | url = https://msdn.microsoft.com/en-us/library/ms969901.aspx | title = DIBs and Their Uses | work = Microsoft Help and Support | access-date = 2015-05-14}}

A device-independent bitmap (DIB) is a format used to define device-independent bitmaps in various color resolutions. 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 an external format, in contrast to a device-dependent bitmap, which appears in the system as a bitmap object (created by an application...). A DIB is normally transported in metafiles (usually using the StretchDIBits() function), BMP files, and the Clipboard ({{mono|CF_DIB}} data format).

The following sections discuss the data stored in the BMP file or DIB in detail. This is the standard BMP file format. Some applications create bitmap image files which are not compliant with the Microsoft documentation. Also, not all fields are used; a value of 0 will be found in these unused fields.

File structure

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.

Referring to the diagram 1, the bitmap file is composed of structures in the following order:

class="wikitable"
Structure name

! Optional

! Size

! Purpose

! Comments

Bitmap file header

| {{No}}

| 14 bytes

| To store general information about the bitmap image file

| Not needed after the file is loaded in memory

DIB header

| {{No}}

| Fixed-size
(7 different versions exist)

| To store detailed information about the bitmap image and define the pixel format

| Immediately follows the Bitmap file header

Extra bit masks

| {{Yes}}

| 3 or 4 DWORDs
(12 or 16 bytes)

| To define the pixel format

| Present only in case the DIB header is the {{mono|BITMAPINFOHEADER}} and the Compression Method member is set to either BI_BITFIELDS or BI_ALPHABITFIELDS

Color table

| {{optional|Semi-optional}}

| {{varies|Variable size}}

| To define colors used by the bitmap image data (Pixel array)

| Mandatory for color depths ≤ 8 bits

Gap1

| {{Yes}}

| {{varies|Variable size}}

| Structure alignment

| An artifact of the File offset to Pixel array in the Bitmap file header

Pixel array

| {{No}}

| {{varies|Variable size}}

| To define the actual values of the pixels

| The pixel format is defined by the DIB header or Extra bit masks. Each row in the Pixel array is padded to a multiple of 4 bytes in size

Gap2

| {{Yes}}

| {{varies|Variable size}}

| Structure alignment

| An artifact of the ICC profile data offset field in the DIB header

ICC color profile

| {{Yes}}

| {{varies|Variable size}}

| To define the color profile for color management

| Can also contain a path to an external file containing the color profile. When loaded in memory as "non-packed DIB", it is located between the color table and Gap1.MSDN [http://msdn.microsoft.com/en-us/library/dd183386%28VS.85%29.aspx Bitmap Header Types]

{{clear|left}}

= DIBs in memory =

A bitmap image file loaded into memory becomes a DIB data structure – an important component of the Windows GDI API. The in-memory DIB data structure is almost the same as the BMP file format, but it does not contain the 14-byte bitmap file header and begins with the DIB header. For DIBs loaded in memory, the color table can also consist of 16-bit entries that constitute indexes to the currently realized paletteMSDN [http://msdn.microsoft.com/en-us/library/dd183375%28VS.85%29.aspx BITMAPINFO Structure] (an additional level of indirection), instead of explicit RGB color definitions. In all cases, the pixel array must begin at a memory address that is a multiple of 4 bytes. In non-packed DIBs loaded in memory, the optional color profile data should be located immediately after the color table and before the gap1 and pixel array (unlike in diag. 1).

When the size of gap1 and gap2 is zero, the in-memory DIB data structure is customarily referred to as "packed DIB" and can be referred to by a single pointer pointing to the beginning of the DIB header. 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. For "packed DIBs" loaded in memory, the optional color profile data should immediately follow the pixel array, as depicted in diag. 1 (with gap1=0 and gap2=0).
"Packed DIBs" are required by Windows clipboard API functions as well as by some Windows patterned brush and resource functions.Feng Yuan - Windows graphics programming: Win32 GDI and DirectDraw: [https://books.google.com/books?id=-O92IIF1Bj4C&pg=PA595 Packed Device-Independent Bitmap (CreateDIBPatternBrush, CreateDIBPatternBrushPt, FindResource, LoadResource, LockResource)]

{{Clear}}

= Bitmap file header =

This block of bytes is at the start of the file and is used to identify the file. A typical application reads this block first to ensure that the file is actually a BMP file and that it is not damaged. The first 2 bytes of the BMP file format are the character "B" then the character "M" in ASCII encoding. All of the integer values are stored in little-endian format (i.e. least-significant byte first).

class="wikitable"
Offset hex

! Offset dec

! Size

! Purpose

000

| 2 bytes

| The header field used to identify the BMP and DIB file is 0x42 0x4D in hexadecimal, same as BM in ASCII. The following entries are possible:

;BM: Windows 3.1x, 95, NT, ... etc.

;BA: OS/2 struct bitmap array

;CI: OS/2 struct color icon

;CP: OS/2 const color pointer

;IC: OS/2 struct icon

;PT: OS/2 pointer

022

| 4 bytes

| The size of the BMP file in bytes

066

| 2 bytes

| Reserved; actual value depends on the application that creates the image, if created manually can be 0

088

| 2 bytes

| Reserved; actual value depends on the application that creates the image, if created manually can be 0

0A10

| 4 bytes

| The offset, i.e. starting address, of the byte where the bitmap image data (pixel array) can be found.

== DIB header (bitmap information header) ==

This block of bytes tells the application detailed information about the image, which will be used to display the image on the screen. The block also matches the header used internally by Windows and OS/2 and has several different variants. 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. The reason that there are different headers is that Microsoft extended the DIB format several times. The new extended headers can be used with some GDI functions instead of the older ones, providing more functionality. Since the GDI supports a function for loading bitmap files, typical Windows applications use that functionality. One consequence of this is that for such applications, the BMP formats that they support match the formats supported by the Windows version being run. See the table below for more information.

class="wikitable"

|+ Windows and OS/2 bitmap headers

Size

! Header name

! OS support

! Features

! Written by

align="right"| 12

| {{mono|BITMAPCOREHEADER}}
{{mono|OS21XBITMAPHEADER}}

| Windows 2.0 or later
OS/2 1.x

|

|

align="right"| 64

| {{mono|OS22XBITMAPHEADER}}

| OS/2 {{mono|BITMAPCOREHEADER}}2

| Adds halftoning. Adds RLE and Huffman 1D compression.

|

align="right"| 16

| {{mono|OS22XBITMAPHEADER}}

| This variant of the previous header contains only the first 16 bytes and the remaining bytes are assumed to be zero values.

An example of such a case is the graphic [http://entropymine.com/jason/bmpsuite/bmpsuite/q/pal8os2v2-16.bmp pal8os2v2-16.bmp]{{cite web|

title=pal8os2v2-16.bmp|date=2015-10-30|access-date=2016-07-06|first=Jason|last=Summers|

url=http://entropymine.com/jason/bmpsuite/bmpsuite/q/pal8os2v2-16.bmp}}

of the BMP Suite.{{cite web|title=BMP Suite|

date=2015-10-30|access-date=2016-07-06|first=Jason|last=Summers|

url=http://entropymine.com/jason/bmpsuite/}}

|

|

align="right"| 40

| {{mono|BITMAPINFOHEADER}}

| Windows NT, 3.1x or later

| Extends bitmap width and height to 4 bytes. Adds 16 bpp and 32 bpp formats. Adds RLE compression.

|

align="right"| 52

| {{mono|BITMAPV2INFOHEADER}}

| Undocumented

| Adds RGB bit masks.

| Adobe Photoshop

align="right"| 56

| {{mono|BITMAPV3INFOHEADER}}

| Not officially documented, but this documentation was posted on Adobe's forums, by an employee of Adobe with a statement that the standard was at one point in the past included in official MS documentation{{cite web|url=https://forums.adobe.com/message/3272950#3272950|title=Invalid BMP Format with Alpha channel|work=Photoshop Windows forum|date=2010-11-15|publisher=Adobe|first=Chris|last=Cox|archive-url=https://web.archive.org/web/20150127132443/https://forums.adobe.com/message/3272950|url-status = live|archive-date=2015-01-27|access-date=2016-05-22}}

| Adds alpha channel bit mask.

| Adobe Photoshop

align="right"| 108

| {{mono|BITMAPV4HEADER}}

| Windows NT 4.0, 95 or later

| Adds color space type and gamma correction

|

align="right"| 124

| {{mono|BITMAPV5HEADER}}

| Windows NT 5.0, 98 or later

| Adds ICC color profiles

| The GIMP

class="wikitable"

! Offset (hex)

Offset (dec)

! Size (bytes)

! OS/2 1.x {{mono|BITMAPCOREHEADER}}{{cite book|title=Encyclopedia of Graphics File Formats|edition=Second|date=April 1996|author=James D. Murray|author2=William vanRyper|isbn=1-56592-161-5|publisher=O'Reilly|at=[https://archive.org/details/mac_Graphics_File_Formats_Second_Edition_1996/page/ os2bmp]|url=https://archive.org/details/mac_Graphics_File_Formats_Second_Edition_1996/page/|access-date=2014-03-07}}

0E14

| 4

| The size of this header (12 bytes)

1218

| 2

| The bitmap width in pixels (unsigned 16-bit)

1420

| 2

| The bitmap height in pixels (unsigned 16-bit)

1622

| 2

| The number of color planes, must be 1

1824

| 2

| The number of bits per pixel

|+ align="bottom" style="caption-side: bottom" | {{nobold|OS/2 1.x bitmaps are uncompressed and cannot be 16 or 32 bpp.}}

The Windows 2.x BITMAPCOREHEADER differs from the OS/2 1.x BITMAPCOREHEADER (shown in the table above) in the one detail that the image width and height fields are signed integers, not unsigned.{{Cite web|url=https://www.fileformat.info/format/bmp/egff.htm|title = Microsoft Windows Bitmap: Summary from the Encyclopedia of Graphics File Formats}}

Versions after {{mono|BITMAPINFOHEADER}} only add fields to the end of the header of the previous version.

For example: {{mono|BITMAPV2INFOHEADER}} adds fields to {{mono|BITMAPINFOHEADER}}, and {{mono|BITMAPV3INFOHEADER}} adds fields to {{mono|BITMAPV2INFOHEADER}}.

An integrated alpha channel has been introduced with the undocumented {{mono|BITMAPV3INFOHEADER}} and with the documented {{mono|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). It is also supported by GIMP, Google Chrome, Microsoft PowerPoint and Microsoft Word.

For compatibility reasons, most applications use the older DIB headers for saving files. With OS/2 no longer supported after Windows 2000, for now the common Windows format is the {{mono|BITMAPINFOHEADER}} header. See next table for its description. All values are stored as unsigned integers, unless explicitly noted.{{Clear}}

class="wikitable"

! Offset (hex)

Offset (dec)

! Size (bytes)

! Windows {{mono|BITMAPINFOHEADER}}

0E14

| 4

| the size of this header, in bytes (40)

1218

| 4

| the bitmap width in pixels (signed integer)

1622

| 4

| the bitmap height in pixels (signed integer)

1A26

| 2

| the number of color planes (must be 1)

1C28

| 2

| the number of bits per pixel, which is the color depth of the image. Typical values are 1, 4, 8, 16, 24 and 32.

1E30

| 4

| the compression method being used. See the next table for a list of possible values

2234

| 4

| the image size. This is the size of the raw bitmap data; a dummy 0 can be given for BI_RGB bitmaps.

2638

| 4

| the horizontal resolution of the image. (pixel per metre, signed integer)

2A42

| 4

| the vertical resolution of the image. (pixel per metre, signed integer)

2E46

| 4

| the number of colors in the color palette, or 0 to default to 2n

3250

| 4

| the number of important colors used, or 0 when every color is important; generally ignored

The compression method (offset 30) can be:

class="wikitable"
Value

! Identified by

! Compression method

! Comments

0

| {{mono|BI_RGB}}

| {{CNone|none}}

| Most common

1

| {{mono|BI_RLE8}}

| RLE 8-bit/pixel

| Can be used only with 8-bit/pixel bitmaps

2

| {{mono|BI_RLE4}}

| RLE 4-bit/pixel

| Can be used only with 4-bit/pixel bitmaps

3

| valign="top" | {{mono|BI_BITFIELDS}}

| valign="bottom" | {{mono|OS22XBITMAPHEADER}}: Huffman 1D

| {{mono|BITMAPV2INFOHEADER}}: RGB bit field masks,
{{mono|BITMAPV3INFOHEADER}}+: RGBA

4

| {{mono|BI_JPEG}}

| {{mono|OS22XBITMAPHEADER}}: RLE-24

| {{mono|BITMAPV4INFOHEADER}}+: JPEG image for printing

{{cite web | url = http://msdn.microsoft.com/en-us/library/dd145023(VS.85).aspx | title = JPEG and PNG Extensions for Specific Bitmap Functions and Structures}}

5

| {{mono|BI_PNG}}

|

| {{mono|BITMAPV4INFOHEADER}}+: PNG image for printing

6

| {{mono|BI_ALPHABITFIELDS}}

| RGBA bit field masks

| only Windows CE 5.0 with .NET 4.0 or later

11

| {{mono|BI_CMYK}}

| {{CNone|none}}

| only Windows Metafile CMYK

12

| {{mono|BI_CMYKRLE8}}

| RLE-8

| only Windows Metafile CMYK

13

| {{mono|BI_CMYKRLE4}}

| RLE-4

| only Windows Metafile CMYK

An OS/2 2.x {{mono|OS22XBITMAPHEADER}} ({{mono|BITMAPINFOHEADER2}} in IBM's documentation) contains 24 additional bytes:

class="wikitable"

! Offset (hex)

Offset (dec)

! Size (bytes)

! OS/2 {{mono|OS22XBITMAPHEADER}} ({{mono|BITMAPINFOHEADER2}})

3654

| 2

| An enumerated value specifying the units for the horizontal and vertical resolutions (offsets 38 and 42). The only defined value is 0, meaning pixels per metre

3856

| 2

| Padding. Ignored and should be zero

3A58

| 2

| An enumerated value indicating the direction in which the bits fill the bitmap. The only defined value is 0, meaning 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

3C60

| 2

| An enumerated value indicating a halftoning algorithm that should be used when rendering the image.

3E62

| 4

| Halftoning parameter 1 (see below)

4266

| 4

| Halftoning parameter 2 (see below)

4670

| 4

| An enumerated value indicating the color encoding for each entry in the color table. The only defined value is 0, indicating RGB.

4A74

| 4

| An application-defined identifier. Not used for image rendering

The halftoning algorithm (offset 60) can be:

class="wikitable"
Value

! Halftoning algorithm

! Comments

0

| {{CNone|none}}

| Most common

1

| Error diffusion

| Halftoning parameter 1 (offset 64) is the percentage of error damping. 100 indicates no damping. 0 indicates that errors are not diffused

2

| PANDA: Processing Algorithm for Noncoded Document Acquisition

| Halftoning parameters 1 and 2 (offsets 64 and 68, respectively) represent the X and Y dimensions, in pixels, respectively, of the halftoning pattern used

3

| Super-circle

| Halftoning parameters 1 and 2 (offsets 64 and 68, respectively) represent the X and Y dimensions, in pixels, respectively, of the halftoning pattern used

= Color table =

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 {{mono|BITMAPINFOHEADER}} header with BI_BITFIELDS (12 bytes) or BI_ALPHABITFIELDS (16 bytes) option is used. Therefore, its offset is the size of the {{mono|BITMAPFILEHEADER}} plus the size of the DIB header (plus optional 12-16 bytes for the three or four bit masks).

Note: On Windows CE the {{mono|BITMAPINFOHEADER}} header can be used with the BI_ALPHABITFIELDSMSDN - {{mono|BITMAPINFOHEADER}} (Windows CE 5.0): [http://msdn.microsoft.com/en-us/library/aa452885.aspx BI_ALPHABITFIELDS in biCompression member] option in the biCompression member.

The number of entries in the palette is either 2n (where n is the number of bits per pixel) or a smaller number specified in the header (in the OS/2 {{mono|BITMAPCOREHEADER}} header format, only the full-size palette is supported). In most cases, each entry in the color table occupies 4 bytes, in the order blue, green, red, 0x00 (see below for exceptions). This is indexed in the {{mono|BITMAPINFOHEADER}} in the structure member biBitCount.

The color table is a block of bytes (a table) listing the colors used by the image. Each pixel in an indexed color image is described by a number of bits (1, 4, or 8) which is an index of a single color described by this table. The purpose of the color palette in indexed color bitmaps is to inform the application about the actual color that each of these index values corresponds to. The purpose of the color table in non-indexed (non-palettized) bitmaps is to list the colors used by the bitmap for the purposes of optimization on devices with limited color display capability and to facilitate future conversion to different pixel formats and palettization.

The colors in the color table are usually specified in the 4-byte per entry ARGB32 format. The color table used with the OS/2 {{mono|BITMAPCOREHEADER}} uses the 3-byte per entry RGB24 format. For DIBs loaded in memory, the color table can optionally consist of 2-byte entries – these entries constitute indexes to the currently realized palette instead of explicit RGB color definitions.

Microsoft does not disallow the presence of a valid alpha channel bit maskMSDN – {{mono|BITMAPV4HEADER}}: [http://msdn.microsoft.com/en-us/library/dd183380%28VS.85%29.aspx The member bV4AlphaMask] in {{mono|BITMAPV4HEADER}} and {{mono|BITMAPV5HEADER}} for 1bpp, 4bpp and 8bpp indexed color images, which indicates that the color table entries can also specify an alpha component using the 8.8.8.[0-8].[0-8] format via the RGBQUAD.rgbReservedMSDN – RGBQUAD: [http://msdn.microsoft.com/en-us/library/dd162938%28VS.85%29.aspx rgbReserved member] member. However, some versions of Microsoft's documentation disallow this feature by stating that the RGBQUAD.rgbReserved member "must be zero".

As mentioned above, the color table is normally not used when the pixels are in the 16-bit per pixel (16bpp) format (and higher); there are normally no color table entries in those bitmap image files. However, the Microsoft documentation (on the MSDN web site as of Nov. 16, 2010see note under biClrUsed [http://msdn.microsoft.com/en-us/library/windows/desktop/dd183376(v=vs.85).aspx MSDN {{mono|BITMAPINFOHEADER}}]) specifies that for 16bpp (and higher), the color table can be present to store a list of colors intended for optimization on devices with limited color display capability, while it also specifies, that in such cases, no indexed palette entries are present in this Color Table. This may seem like a contradiction if no distinction is made between the mandatory palette entries and the optional color list.

= Pixel storage =

The bits representing the bitmap pixels are packed in rows (also known as strides or scan lines). The size of each row is rounded up to a multiple of 4 bytes (a 32-bit DWORD) by padding.{{Cite web|url=https://learn.microsoft.com/en-us/windows/win32/medfound/image-stride|title=Image Stride - Win32 apps|website=learn.microsoft.com}}

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

The total number of bytes necessary to store one row of pixels can be calculated as:

\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,

{{block indent|left=1.5|1=ImageWidth is expressed in pixels. The equation above uses the floor and ceiling functions.}}

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:

\text{PixelArraySize} = \text{RowSize} \cdot |\text{ImageHeight}|

{{block indent|left=1.5|1=ImageHeight is expressed in pixels. The absolute value is necessary because ImageHeight is expressed as a negative number for top-down images.}}

== Pixel array (bitmap data) ==

The pixel array is a block of 32-bit DWORDs, that describes the image pixel by pixel. Usually pixels are stored "bottom-up", starting in the lower left corner, going from left to right, and then row by row from the bottom to the top of the image. Unless {{mono|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).

Contemporary DIB Headers allow pixel formats with 1, 2, 4, 8, 16, 24 and 32 bits per pixel (bpp).MSDN - {{mono|BITMAPINFOHEADER}}: [http://msdn.microsoft.com/en-us/library/dd183376%28VS.85%29.aspx The member biBitCount] GDI+ also permits 64 bits per pixel.{{cite web|url=http://msdn.microsoft.com/en-us/library/ms536393(v=vs.85).aspx|publisher=MSDN|title=Types of Bitmaps|date=2012-06-03|access-date=2014-03-16}}

Padding bytes (not necessarily 0) must be appended to the end of the rows in order to bring up the length of the rows to a multiple of four bytes. When the pixel array is loaded into memory, each row must begin at a memory address that is a multiple of 4. This address/offset restriction is mandatory only for Pixel Arrays loaded in memory. For file storage purposes, only the size of each row must be a multiple of 4 bytes while the file offset can be arbitrary. A 24-bit 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.

== Compression ==

  • Indexed color images may be compressed with 4-bit or 8-bit RLE or Huffman 1D algorithm.
  • OS/2 {{mono|BITMAPCOREHEADER}}2 24bpp images may be compressed with the 24-bit RLE algorithm.
  • The 16bpp and 32bpp images are always stored uncompressed.
  • Note that images in all color depths can be stored without compression if so desired.

== Pixel format ==

  • The 1-bit per pixel (1bpp) format supports 2 distinct colors, (for example: black and white). The pixel values are stored in each bit, with the first (left-most) pixel in the most-significant bit of the first byte. Each bit is an index into a table of 2 colors. An unset bit will refer to the first color table entry, and a set bit will refer to the last (second) color table entry.
  • The 2-bit per pixel (2bpp) format supports 4 distinct colors and stores 4 pixels per 1 byte, the left-most pixel being in the two most significant bits (Windows CE only:MSDN: [http://msdn.microsoft.com/en-us/library/ms959648.aspx Windows CE - {{mono|BITMAPINFOHEADER}} Structure]). Each pixel value is a 2-bit index into a table of up to 4 colors.
  • The 4-bit per pixel (4bpp) format supports 16 distinct colors and stores 2 pixels per 1 byte, the left-most pixel being in the more significant nibble. Each pixel value is a 4-bit index into a table of up to 16 colors.
  • The 8-bit per pixel (8bpp) format supports 256 distinct colors and stores 1 pixel per 1 byte. Each byte is an index into a table of up to 256 colors.
  • The 16-bit per pixel (16bpp) format supports 65536 distinct colors and stores 1 pixel per 2-byte WORD. Each WORD can define the alpha, red, green and blue samples of the pixel.
  • The 24-bit per pixel (24bpp) format supports 16,777,216 distinct colors and stores 1 pixel value per 3 bytes. Each pixel value defines the red, green and blue samples of the pixel (8.8.8.0.0 in RGBAX notation). Specifically, in the order: blue, green and red (8 bits per each sample).
  • The 32-bit per pixel (32bpp) format supports 4,294,967,296 distinct colors and stores 1 pixel per 4-byte DWORD. Each DWORD can define the alpha, red, green and blue samples of the pixel.

In order to resolve the ambiguity of which bits define which samples, the DIB headers provide certain defaults as well as specific BITFIELDS, which are bit masks that define the membership of particular group of bits in a pixel to a particular channel. The following diagram defines this mechanism:

{{Plain image with caption|BitfieldsSLN.svg|Diag. 2 – The BITFIELDS mechanism for a 32-bit pixel depicted in RGBAX sample length notation|753px|center}}

The sample fields defined by the BITFIELDS bit masks have to be contiguous and non-overlapping, but the order of the sample fields is arbitrary. 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. The alpha bit mask is valid whenever it is present in the DIB header or when the Compression member of the DIB header is set to BI_ALPHABITFIELDS (Windows CE only).

{{Plain image with caption|SLNotation44440.svg|Diag. 3 – The pixel format with an alpha channel for a 16-bit pixel (in RGBAX sample Length notation) actually generated by Adobe PhotoshopAdobe Photoshop: [http://livedocs.adobe.com/en_US/Photoshop/10.0/WSfd1234e1c4b69f30ea53e41001031ab64-7751.html BMP Format] {{Webarchive|url=https://web.archive.org/web/20110922225022/http://livedocs.adobe.com/en_US/Photoshop/10.0/WSfd1234e1c4b69f30ea53e41001031ab64-7751.html |date=2011-09-22 }}|448px|center}}

{{Plain image with caption|AllBMPformats.png|All of the possible pixel formats in a DIB|700px|center}}

= RGB video subtypes =

The BITFIELD mechanism described above allows for the definition of tens of thousands of different pixel formats, however only several of them are used in practice, all palettized formats RGB8, RGB4, and RGB1 (marked in yellow in the table above, defined in dshow.h.MEDIASUBTYPE names):

class="wikitable" style="float:left"

|+ Uncompressed RGB Video Subtypes{{cite web|url=http://msdn.microsoft.com/en-us/library/windows/desktop/dd407253(v=vs.85).aspx|title=Uncompressed RGB Video Subtypes|publisher=MSDN|work=dshow.h|access-date=2014-03-11}}

!R.G.B.A.X

!RGB subtype

!R.G.B.A.X

!ARGB subtype

8.8.8.0.8

| RGB32

| 8.8.8.8.0

| ARGB32

|

| 10.10.10.2.0

| A2R10G10B10

8.8.8.0.0

| RGB24

| 10.10.10.2.0

| A2B10G10R10

5.6.5.0.0

| RGB565

| 4.4.4.4.0

| ARGB4444

5.5.5.0.1

| RGB555

| 5.5.5.1.0

| ARGB1555

class="wikitable" style="float:right"

|+ Bit fields for ten RGB bits

!Bit field

!Offset

(hex)

!colspan="3"|Bits A2R10G10B10

!colspan="3"|Bits A2B10G10R10

Red

|36

|style="color:blue"|00 00 F0 3F

|LE: 3FF00000

|20...29

|style="color:red" |FF 03 00 00

|LE: 000003FF

| 0... 9

Green

|3A

|00 FC 0F 00

|LE: 000FFC00

|10...19

|00 FC 0F 00

|LE: 000FFC00

|10...19

Blue

|3E

|style="color:red" |FF 03 00 00

|LE: 000003FF

| 0... 9

|style="color:blue"|00 00 F0 3F

|LE: 3FF00000

|20...29

Alpha

|42

|00 00 00 C0

|LE: C0000000

|30...31

|00 00 00 C0

|LE: C0000000

|30...31

{{Clear}}

In version 2.1.4 FFmpeg supported (in its own terminology) the BMP pixel formats bgra, bgr24, rgb565le, rgb555le, rgb444le, rgb8, bgr8, rgb4_byte, bgr4_byte, gray, pal8, and monob; i.e., bgra was the only supported pixel format with transparency.{{cite web|url=http://www.ffmpeg.org/general.html#Image-Formats|title=Image Formats|work=FFmpeg General Documentation|year=2014|access-date=2014-02-23}}

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

= Example 1 =

Following is an example of a 2×2 pixel, 24-bit bitmap (Windows DIB header {{mono|BITMAPINFOHEADER}}) with pixel format RGB24.

class="wikitable"

! Offset

(hex)

! Size

(bytes)

! Hex value

! Value

! Description

colspan="5" style="text-align: center;" | BMP Header
align="center" |00

| align="center" |2

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|42 4D

| style="padding: 0px 10px"|"BM"

| style="padding: 0px 10px"|ID field (42h, 4Dh)

align="center" |02

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|46 00 00 00

| style="padding: 0px 10px"|70 bytes (54+16)

| style="padding: 0px 10px"|Size of the BMP file (54 bytes header + 16 bytes data)

align="center" |06

| align="center" |2

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|00 00

| style="padding: 0px 10px"|Unused

| style="padding: 0px 10px"|Application specific

align="center" |08

| align="center" |2

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|00 00

| style="padding: 0px 10px"|Unused

| style="padding: 0px 10px"|Application specific

align="center" |0A

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|36 00 00 00

| style="padding: 0px 10px"|54 bytes (14+40)

| style="padding: 0px 10px"|Offset where the pixel array (bitmap data) can be found

colspan="5" style="text-align: center;" | DIB Header
align="center" |0E

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|28 00 00 00

| style="padding: 0px 10px"|40 bytes

| style="padding: 0px 10px"|Number of bytes in the DIB header (from this point)

align="center" |12

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|02 00 00 00

| style="padding: 0px 10px"|2 pixels (left to right order)

| style="padding: 0px 10px"|Width of the bitmap in pixels

align="center" |16

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|02 00 00 00

| style="padding: 0px 10px"|2 pixels (bottom to top order)

| style="padding: 0px 10px"|Height of the bitmap in pixels. Positive for bottom to top pixel order.

align="center" |1A

| align="center" |2

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|01 00

| style="padding: 0px 10px"|1 plane

| style="padding: 0px 10px"|Number of color planes being used

align="center" |1C

| align="center" |2

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|18 00

| style="padding: 0px 10px"|24 bits

| style="padding: 0px 10px"|Number of bits per pixel

align="center" |1E

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|00 00 00 00

| style="padding: 0px 10px"|0

| style="padding: 0px 10px"|BI_RGB, no pixel array compression used

align="center" |22

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|10 00 00 00

| style="padding: 0px 10px"|16 bytes

| style="padding: 0px 10px"|Size of the raw bitmap data (including padding)

align="center" |26

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|13 0B 00 00

| style="padding: 0px 10px"|2835 pixels/metre horizontal

| style="padding: 0px 10px" rowspan="2"|Print resolution of the image,
72 DPI × 39.3701 inches per metre yields 2834.6472

align="center" |2A

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|13 0B 00 00

| style="padding: 0px 10px"|2835 pixels/metre vertical

align="center" |2E

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|00 00 00 00

| style="padding: 0px 10px"|0 colors

| style="padding: 0px 10px"|Number of colors in the palette

align="center" |32

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|00 00 00 00

| style="padding: 0px 10px"|0 important colors

| style="padding: 0px 10px"|0 means all colors are important

colspan="5" align="center"|Start of pixel array (bitmap data)
align="center" |36

| align="center" |3

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|00 00 FF

| style="padding: 0px 10px"|0 0 255

| style="padding: 0px 10px"|Red, Pixel (x=0, y=1)

align="center" |39

| align="center" |3

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|FF FF FF

| style="padding: 0px 10px"|255 255 255

| style="padding: 0px 10px"|White, Pixel (x=1, y=1)

align="center" |3C

| align="center" |2

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|00 00

| style="padding: 0px 10px"|0 0

| style="padding: 0px 10px"|Padding for 4 byte alignment (could be a value other than zero)

align="center" |3E

| align="center" |3

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|FF 00 00

| style="padding: 0px 10px"|255 0 0

| style="padding: 0px 10px"|Blue, Pixel (x=0, y=0)

align="center" |41

| align="center" |3

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|00 FF 00

| style="padding: 0px 10px"|0 255 0

| style="padding: 0px 10px"|Green, Pixel (x=1, y=0)

align="center" |44

| align="center" |2

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|00 00

| style="padding: 0px 10px"|0 0

| style="padding: 0px 10px"|Padding for 4 byte alignment (could be a value other than zero)

{{clear|left}}

File:Bmp_format2.svg|frame|right|Example 2 of a 4×2 pixel bitmap, with 32 bits/pixel encoding

rect 50 50 100 100 0,0: blue 0000FF FF

rect 100 50 150 100 0,1: green 00FF00 FF

rect 150 50 200 100 0,2: red FF00000 FF

rect 200 50 250 100 0,3: white FFFFFF FF

rect 50 100 100 150 1,0: blue 0000FF 7F, half transparent

rect 100 100 150 150 1,1: green 00FF00 7F, half transparent

rect 150 100 200 150 1,2: red FF0000 7F, half transparent

rect 200 100 250 150 1,3: white FFFFFF 7F, half transparent

default Netpbm#Transparency

desc top-left

= Example 2 =

Following is an example of a 4×2 pixel, 32-bit bitmap with opacity values in the alpha channel (Windows DIB Header {{mono|BITMAPV4HEADER}}) with pixel format ARGB32.

class="wikitable"

! Offset

(hex)

! Size

(bytes)

! Hex value

! Value

! Description

colspan="5" style="text-align: center;" | BMP Header
align="center" |00

| align="center" |2

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|42 4D

| style="padding: 0px 10px"|"BM"

| style="padding: 0px 10px"|ID field (42h, 4Dh)

align="center" |02

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|9A 00 00 00

| style="padding: 0px 10px"|154 bytes (122+32)

| style="padding: 0px 10px"|Size of the BMP file

align="center" |06

| align="center" |2

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|00 00

| style="padding: 0px 10px"|Unused

| style="padding: 0px 10px"|Application specific

align="center" |08

| align="center" |2

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|00 00

| style="padding: 0px 10px"|Unused

| style="padding: 0px 10px"|Application specific

align="center" |0A

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|7A 00 00 00

| style="padding: 0px 10px"|122 bytes (14+108)

| style="padding: 0px 10px"|Offset where the pixel array (bitmap data) can be found

colspan="5" style="text-align: center;" | DIB Header
align="center" |0E

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|6C 00 00 00

| style="padding: 0px 10px"|108 bytes

| style="padding: 0px 10px"|Number of bytes in the DIB header (from this point)

align="center" |12

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|04 00 00 00

| style="padding: 0px 10px"|4 pixels (left to right order)

| style="padding: 0px 10px"|Width of the bitmap in pixels

align="center" |16

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|02 00 00 00

| style="padding: 0px 10px"|2 pixels (bottom to top order)

| style="padding: 0px 10px"|Height of the bitmap in pixels

align="center" |1A

| align="center" |2

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|01 00

| style="padding: 0px 10px"|1 plane

| style="padding: 0px 10px"|Number of color planes being used

align="center" |1C

| align="center" |2

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|20 00

| style="padding: 0px 10px"|32 bits

| style="padding: 0px 10px"|Number of bits per pixel

align="center" |1E

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|03 00 00 00

| style="padding: 0px 10px"|3

| style="padding: 0px 10px"|BI_BITFIELDS, no pixel array compression used

align="center" |22

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|20 00 00 00

| style="padding: 0px 10px"|32 bytes

| style="padding: 0px 10px"|Size of the raw bitmap data (including padding)

align="center" |26

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|13 0B 00 00

| style="padding: 0px 10px"|2835 pixels/metre horizontal

| style="padding: 0px 10px" rowspan="2"|Print resolution of the image,
72 DPI × 39.3701 inches per metre yields 2834.6472

align="center" |2A

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|13 0B 00 00

| style="padding: 0px 10px"|2835 pixels/metre vertical

align="center" |2E

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|00 00 00 00

| style="padding: 0px 10px"|0 colors

| style="padding: 0px 10px"|Number of colors in the palette

align="center" |32

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|00 00 00 00

| style="padding: 0px 10px"|0 important colors

| style="padding: 0px 10px"|0 means all colors are important

align="center" |36

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|00 00 FF 00

| style="padding: 0px 10px"|00FF0000 in big-endian

| style="padding: 0px 10px"|Red channel bit mask (valid because BI_BITFIELDS is specified)

align="center" |3A

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|00 FF 00 00

| style="padding: 0px 10px"|0000FF00 in big-endian

| style="padding: 0px 10px"|Green channel bit mask (valid because BI_BITFIELDS is specified)

align="center" |3E

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|FF 00 00 00

| style="padding: 0px 10px"|000000FF in big-endian

| style="padding: 0px 10px"|Blue channel bit mask (valid because BI_BITFIELDS is specified)

align="center" |42

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|00 00 00 FF

| style="padding: 0px 10px"|FF000000 in big-endian

| style="padding: 0px 10px"|Alpha channel bit mask

align="center" |46

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|20 6E 69 57

| style="padding: 0px 10px"|little-endian "Win "

| style="padding: 0px 10px"|LCS_WINDOWS_COLOR_SPACE

align="center" |4A

| align="center" |36

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|00...00

| style="padding: 0px 10px"|CIEXYZTRIPLE Color Space endpoints

| style="padding: 0px 10px"|Unused for LCS "Win " or "sRGB"

align="center" |6E

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|00 00 00 00

| style="padding: 0px 10px"|0 Red Gamma

| style="padding: 0px 10px"|Unused for LCS "Win " or "sRGB"

align="center" |72

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|00 00 00 00

| style="padding: 0px 10px"|0 Green Gamma

| style="padding: 0px 10px"|Unused for LCS "Win " or "sRGB"

align="center" |76

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|00 00 00 00

| style="padding: 0px 10px"|0 Blue Gamma

| style="padding: 0px 10px"|Unused for LCS "Win " or "sRGB"

colspan="5" align="center"|Start of the Pixel Array (the bitmap Data)
align="center" |7A

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|FF 00 00 7F

| style="padding: 0px 10px"|255 0 0 127

| style="padding: 0px 10px"|Blue (Alpha: 127), Pixel (x=0, y=1)

align="center" |7E

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|00 FF 00 7F

| style="padding: 0px 10px"|0 255 0 127

| style="padding: 0px 10px"|Green (Alpha: 127), Pixel (x=1, y=1)

align="center" |82

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|00 00 FF 7F

| style="padding: 0px 10px"|0 0 255 127

| style="padding: 0px 10px"|Red (Alpha: 127), Pixel (x=2, y=1)

align="center" |86

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|FF FF FF 7F

| style="padding: 0px 10px"|255 255 255 127

| style="padding: 0px 10px"|White (Alpha: 127), Pixel (x=3, y=1)

align="center" |8A

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|FF 00 00 FF

| style="padding: 0px 10px"|255 0 0 255

| style="padding: 0px 10px"|Blue (Alpha: 255), Pixel (x=0, y=0)

align="center" |8E

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|00 FF 00 FF

| style="padding: 0px 10px"|0 255 0 255

| style="padding: 0px 10px"|Green (Alpha: 255), Pixel (x=1, y=0)

align="center" |92

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|00 00 FF FF

| style="padding: 0px 10px"|0 0 255 255

| style="padding: 0px 10px"|Red (Alpha: 255), Pixel (x=2, y=0)

align="center" |96

| align="center" |4

| style="padding: 0px 20px; white-space: nowrap; font-family:monospace;"|FF FF FF FF

| style="padding: 0px 10px"|255 255 255 255

| style="padding: 0px 10px"|White (Alpha: 255), Pixel (x=3, y=0)

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

Usage of BMP format

The simplicity of the BMP file format, and its widespread familiarity in Windows and elsewhere, as well as the fact that this format is relatively well documented and has an open format, makes BMP a very common format that image processing programs from many operating systems can read and write. ICO and CUR files contain bitmaps starting with a BITMAPINFOHEADER.

Many older graphical user interfaces used bitmaps in their built-in graphics subsystems;{{cite book | title = Cross-Platform GUI Programming with Wxwidgets |author1=Julian Smart |author2=Stefan Csomor |author3=Kevin Hock |name-list-style=amp| url = https://books.google.com/books?id=CyMsvtgnq0QC&q=bitmap+pixmap+gui&pg=PA265| publisher = Prentice Hall | year = 2006 | isbn = 0-13-147381-6 }} 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.{{cite web|url=http://www.digitalpreservation.gov/formats/fdd/fdd000189.shtml|title=Bitmap Image File (BMP), Version 5|publisher=Library of Congress|work=Digital Preservation|date=2014-01-08|access-date=2014-03-11}}

While most BMP files have a relatively large file size due to lack of any compression (or generally low-ratio run-length encoding on palletized images), many BMP files can be considerably compressed with lossless data compression algorithms such as ZIP because they contain redundant data. Some formats, such as RAR, even include routines specifically targeted at efficient compression of such data.

Related formats

{{Main|Image file formats}}

The X Window System uses a similar XBM format for black-and-white images, and XPM (pixelmap) for color images. There are also a variety of "raw" formats, which save raw data with no other information. The Portable Pixmap (PPM) and Truevision TGA formats also exist, but are less often used – or only for special purposes; for example, TGA can contain transparency information.

References

{{Reflist|30em}}