BMP file format

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Dicklyon (talk | contribs) at 16:28, 30 September 2007 (→‎Pixel storage). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Windows Bitmap
Filename extension
.bmp or .dib
Internet media type
image/x-ms-bmp (unofficial)
Type code'BMP '
Uniform Type Identifier (UTI)com.microsoft.bmp
Type of formatRaster graphics

The BMP file format, sometimes called bitmap or DIB file format (for device-independent bitmap), is an image file format used to store bitmap digital images, especially on Microsoft Windows and OS/2 operating systems.

Many graphical user interfaces use bitmaps in their built-in graphics subsystems;[1] 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 (or .DIB for device-independent bitmap).

Pixel storage

In uncompressed BMP files, and many other bitmap file formats, image pixels are stored with a color depth of 1, 4, 8, 16, 24, or 32 bits per pixel. Images of 8 bits and fewer can be either grayscale or indexed color. An alpha channel (for transparency) may be stored in a separate file, where it is similar to a greyscale image, or in a fourth channel that converts 24-bit images to 32 bits per pixel.

Uncompressed bitmap files (such as BMP) are typically much larger than compressed (with any of various methods) image file formats for the same image. For example, the 1058×1058 Wikipedia logo, which occupies about 287.65 KB in the PNG format, takes about 3281.5 KB as a 24-bit BMP file. Uncompressed formats are generally unsuitable for transferring images on the Internet or other slow or capacity-limited media.

The bits representing the bitmap pixels are packed within rows. Depending on the color depth, a pixel in the picture will occupy at least n/8 bytes (n is the bit depth, since 1 byte equals 8 bits). The approximate size for a n-bit (2n colors) BMP file in bytes can be calculated, including the effect of starting each word on a 32-bit dword boundary, as:

, where the ceiling function gives the least integer that's at least as big as the argument; that is, the number of 32-bit dwords needed to hold a row of n-bit pixels;
, where height and width are given in pixels.

In the formula above, 54 is the size of the headers in the popular Windows V3 BMP version (14-byte BMP file header plus 40-byte DIB V3 header); some other header versions will be larger or smaller than that, as described in tables below. And is the size of the color palette; this size is an approximation, as the color palette size will be bytes in the OS/2 V1 version, and some other versions may optionally define only the number of colors needed by the image, potentially fewer than .[2] Only files with 8 or fewer bits per pixel use a palette; for 16-bit (or higher) bitmaps, omit the palette part from the size calculation:

For detailed information, see the sections on file format below.

Device-independent bitmaps and BMP file format

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 file format. According to Microsoft support:[2]

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 (CF_DIB data format).

A typical BMP file usually contains the following blocks of data:

BMP Header Stores general information about the BMP file.
Bitmap Information (DIB header) Stores detailed information about the bitmap image.
Color Palette Stores the definition of the colors being used for indexed color bitmaps.
Bitmap Data Stores the actual image, pixel by pixel.

The following sections discuss the data stored in the BMP file or DIB in details. This is the standard BMP file format.[2] Some bitmap images may be stored using a slightly different format, depending on the application that creates it. Also, not all fields are used; a value of 0 will be found in these unused fields.

DIBs in memory

A BMP file is designed to have its contents loaded into memory as a DIB, an important component of the Windows GDI API. The BMP file format and the DIB data structure are identical except for the 14-byte BMP header.

BMP file header

This block of bytes is added before the DIB format used internally by GDI and serves for identification. A typical application will read this block first to ensure that the file is actually a BMP file and that it is not damaged.

Offset# Size Purpose
0 2 the magic number used to identify the BMP file: 0x424D (ASCII code points for B and M)
2 4 the size of the BMP file in bytes
6 2 reserved; actual value depends on the application that creates the image
8 2 reserved; actual value depends on the application that creates the image
10 4 the offset, i.e. starting address, of the byte where the bitmap data can be found.

Bitmap information (DIB 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 field, specifying their size, so that an application can easily determine which header is used in the image. The reason, why 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. Also the GDI supports a function for loading bitmap files, and its likely that a Windows application uses it. One consequence of this is that the bitmap formats a program supports, match the formats supported by the Windows version it is running on. See the table below for more information.

Size Header Identified by Supported by the GDI of
40 Windows V3 BITMAPINFOHEADER all Windows versions since Windows 3.0
12 OS/2 V1 BITMAPCOREHEADER OS/2 and also all Windows versions since Windows 3.0
64 OS/2 V2
108 Windows V4 BITMAPV4HEADER all Windows versions since Windows 95/NT4
124 Windows V5 BITMAPV5HEADER Windows 98/2000 and newer

For compatibility reasons, most applications use the older DIB headers for saving files. With OS/2 being obsolete, for now the only common format is the V3 header. See next table for its description. All values are stored as unsigned integers, unless explicitly noted.

Offset # Size Purpose
14 4 the size of this header (40 bytes)
18 4 the bitmap width in pixels (signed integer).
22 4 the bitmap height in pixels (signed integer).
26 2 the number of color planes being used. Must be set to 1.
28 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.
30 4 the compression method being used. See the next table for a list of possible values.
34 4 the image size. This is the size of the raw bitmap data (see below), and should not be confused with the file size.
38 4 the horizontal resolution of the image. (pixel per meter, signed integer)
42 4 the vertical resolution of the image. (pixel per meter, signed integer)
46 4 the number of colors in the color palette, or 0 to default to 2n.
50 4 the number of important colors used, or 0 when every color is important; generally ignored.

The compression method field (bytes #30-33) can have the following values:

Value Identified by Compression method Comments
0 BI_RGB none Most common
1 BI_RLE8 RLE 8-bit/pixel Can be used only with 8-bit/pixel bitmaps
2 BI_RLE4 RLE 4-bit/pixel Can be used only with 4-bit/pixel bitmaps
3 BI_BITFIELDS Bit field Can be used only with 16 and 32-bit/pixel bitmaps.
4 BI_JPEG JPEG The bitmap contains a JPEG image
5 BI_PNG PNG The bitmap contains a PNG image

Note: The image size field can be 0 for BI_RGB bitmaps.

The OS/2 V1 header is also popular:

Offset Size Purpose
14 4 the size of this header (12 bytes)
18 2 the bitmap width in pixels.
20 2 the bitmap height in pixels.
22 2 the number of color planes; 1 is the only legal value
24 2 the number of bits per pixel. Typical values are 1, 4, 8 and 24.

Note: OS/2 V1 bitmaps cannot be compressed and cannot be 16 or 32 bits/pixel. All values in the OS/2 V1 header are unsigned integers.

A 32-bit version of DIB with integrated alpha channel has been introduced with Windows XP and is used within its logon and theme system; it has yet to gain wide support in image editing software, but has been supported in Adobe Photoshop since version 7 and Adobe Flash since version MX 2004 (then known as Macromedia Flash).

Color palette

The palette, or color table, is a block of bytes defining the colors available for use in an indexed-color image. Each pixel in the bitmap is described by a few bits (1, 4, or 8) representing an index into this color table. That is, the purpose of the color palette in indexed-color bitmaps is to tell the application the actual color that each of these index values corresponds to.

A DIB always uses the RGB color model. In this model, a color is terms of different intensities (from 0 to 255) of the additive primary colors red (R), green (G), and blue (B). A color is thus defined using the 3 values for R, G and B (though stored in backwards order in each palette entry).

The number of entries in the palette is either 2n or a smaller number specified in the header (in the OS/2 V1 format, only the full-size palette is supported).[2][3] Each entry contains four bytes, except in the case of the OS/2 V1 versions, in which case there are only three bytes per entry.[3] The first (and only for OS/2 V1) three bytes store the values for blue, green, and red, respectively,[2] while the last one is unused and is filled with 0 by most applications.

As mentioned above, the color palette is not used when the bitmap is 16-bit or higher; there are no palette bytes in those cases.

Bitmap data

This block of bytes describes the image, pixel by pixel. Pixels are stored "upside-down" with respect to normal image raster scan order, 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.[2] Uncompressed Windows bitmaps are also be stored from the top row to the bottom, if the image height field is negative. If the number of bytes matching a row (scanline) in the image is not divisible by 4, the line is padded with one to three additional bytes of unspecified value so that the next row will start on a multiple of 4 byte location in memory or in the file. Following these rules there are several ways to store the pixel bits depending on the color depth and the compression type of the bitmap.

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 well-documented and free of patents, makes it a very common format that image processing programs from many operating systems can read and write.

While most BMP files have a relatively large file size due to lack of any compression, many BMP files can be considerably compressed with lossless data compression algorithms such as ZIP because they contain redundant data.

Related 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 saves 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.

Numerous other bitmap file formats are in use, though most not widely.[4]

See also

References

  1. ^ Julian Smart, Stefan Csomor, and Kevin Hock (2006). Cross-Platform GUI Programming with Wxwidgets. Prentice Hall. ISBN 0131473816.{{cite book}}: CS1 maint: multiple names: authors list (link)
  2. ^ a b c d e f "DIBs and Their Uses". Microsoft Help and Support. 2005-02-11.
  3. ^ a b "GFF Format Summary: OS/2 Bitmap".
  4. ^ "List of bitmap file types". Search File-Extensions.org.

External links