The BMP File Format
An extract from Chapter 2 of Miano, J. 1999, "Compressed Image File Formats", Addison-Wesley, New York.
The BMP file structure is very simple and is shown in Figure 2.1.
File Header
Image Header
Colour table
Pixel data
Figure 2.1
Bitmap File
Structure
File Header
Every Windows BMP begins with a BITMAPFILEHEADER structure whose layout is shown in Table 2.1.
The main function of this structure is to serve as the signature that identifies that file format.
Three checks can be made to ensure that the file you are reading is in fact a BMP file:-
  • The first two bytes of the file must contain the ASCII characters "B" followed by "M."
  • If you are using a file system where you can determine the exact file size in bytes, you can compare the file size with the value in the bfSize field
  • The bfReservedl and bfReserved2 fields must be zero.
The file header also specifies the location of the pixel data in the file.
When decoding a BMP file you must use the bfOffbits field to determine the offset from the beginning of the file
to where the pixel data starts.
Most applications place the pixel data immediately following the BITMAPINFOHEADER structure or palette, if it is present.
However, some applications place filler bytes between these structures and the pixel data
so you must use the bfOffbits to determine the number of bytes from the BITMAPFILEHEAOER structure to the pixel data.

Field Name Size in Bytes Description
bfType 2 Contains the characters "BM" that identify the file type
bfsize 4 File size
bfReservedl 2 Unused
bfReserved2 2 Unused
bfOffBits 4 Offset to start of pixel data
Table 2.1 BITMAPFILEHEADER Structure


Image Header
The image header immediately follows the BITMAPFILEHEADER structure. BITMAPINFOHEADER is the Windows format.
Unfortunately, there is no version field in the BMP definitions.
The only way to determine the type of image structure used in a particular file is to examine the structure's size field, which is the first 4 bytes of both structure types.
The size of BITMAPINFOHEADER, at least 40 bytes. The layout of BITMAPINFOHEADER is shown in Table 2.2.
This structure gives the dimensions and bit depth of the image and tells if the image is compressed.
Windows 95 supports a BMP format that uses an enlarged version of this header.
Few applications create BMP files using this format; however; a decoder should be implemented
so that it knows that header sizes can be larger than 40 bytes.
The image height is an unsigned value.
A negative value for the biHeight field specifies that the pixel data is ordered from the top down
rather than the normal bottom up. Images with a negative biHeight value may not be compressed.

Field Name Size Description
biSize 4 Header size-Must be at least 40
biWidth 4 Imagewidth
biHeight 4 Image height
biPlanes 2 Must be 1
biBitCount 2 Bits per pixel- 1, 4, 8, 16, 24, or 32
biCompression 4 Compression type- BI_RGB=O, BI_RLE8=1,
BI_RLE4=2, or BI_BITFIELDS=3
biSizelmage 4 Image Size-May be zero if not compressed
bixPelsPerMeter 4 Preferred resolution in pixels per meter
biyPelsPerMeter 4 Preferred resolution in pixels per meter
biClrUsed 4 Number of entries in the color map that are actually used
biClrlmportant 4 Number of significant colors
Table 2.2 BITMAPINFOHEADER Structure


Color Palette
The color palette immediately follows the file header and can be in one of three formats
The first two are used to map pixel data to ROB color values when the bit count is 1, 4, or 8 (biBitCount or bcBitCount fields).
For BMP files in the Windows format, the palette consists of an array of 2bitcount RGBQUAD structures (Table 2.4).
BMP files in OS/2 format use an array of RCBTRIPLE structures (Table 2.5).

Field Size Description
rgbBlue 1 Blue color value
rgbGreen 1 Redcolorvalue
rgbRed 1 Green color value
rgbReserved 1 Must be zero
Table 2.4 RGBQUAD Structure

Pixel Data
The pixel data follows the palette or bit masks if they are present.
Otherwise, it follows the BITMAPINFOHEADER structure.
Normally the data follows immediately, but there may be intervening fill bytes.
You must use the bfOffBits field in the BITMAPFILEHEADER
to determine the offset from the BITMAPFILEHEADER structure to the pixel data.

The format of the pixel data depends upon the number of bits per pixel.
8 Bits per Pixel. Each pixel in the row is represented by 1 byte that is an index into the color palette.

Page created on February 20th 2003