BMP Format

Title: | BMP Format
Subject: | Windows Bitmap File Format Specifications
Author: | Wim Wouters
Version: | V1.1

Table of Contents

Table of Contents

Introduction

Bitmap File Format

General

BMP Contents

Field Details

Height Field

Bits Per Pixel Field

Compression Field

Colors Field

Important Colors Field

Introduction

This document describes the Microsoft Windows and IBM OS/2 picture bitmap files, called Bitmaps or BMP files. Most of the descriptions of the BMP file concentrate on the Microsoft Windows BMP structures like

BMPINFOHEADER and BMPCOREINFO , but only a few describe the file contents on byte level. This information is therefor only intended to be used in applications where direct reading and writing of a BMP file is required.

Bitmap File Format

The following chapters contain the detailed information on the contents of the BMP file. First more general information will be given regarding the byte order and file alignment. The second chapter will concentrate on the byte-level contents of a BMP file. The third chapter will elaborate on this chapter and explain some of the concepts - like compression - and/or values in detail.

General

The BMP file has been created by Microsoft and IBM and is therefor very strictly bound to the architecture of the main hardware platform that both companies support: the IBM compatible PC. This means that all values stored in the BMP file are in the Intel format, sometimes also called the Little Endian format because of the byte order that an Intel processor uses internally to store values.

The BMP files are the way, Windows stores bit mapped images. The BMP image data is bit packed but every line must end on a dword boundary - if that’s not the case, it must be padded with zeroes. BMP files are stored bottom-up, that means that the first scan line is the bottom line.

The BMP format has four incarnations, two under Windows (new and old) and two under OS/2, all are described here.

BMP Contents

The following table contains a description of the contents of the BMP file. For every field, the file offset, the length and the contents will be given. For a more detailed discussion, see the following chapters.

Offset | Field | Size | Contents
0000h | Identifier | 2 bytes | The characters identifying the bitmap. The following entries are possible: ‘BM’ - Windows 3.1x, 95, NT, … ‘BA’ - OS/2 Bitmap Array ‘CI’ - OS/2 Color Icon ‘CP’ - OS/2 Color Pointer ‘IC’ - OS/2 Icon ‘PT’ - OS/2 Pointer
0002h | File Size | 1 dword | Complete file size in bytes.
0006h | Reserved | 1 dword | Reserved for later use.
000Ah | Bitmap Data Offset | 1 dword | Offset from beginning of file to the beginning of the bitmap data.
000Eh | Bitmap Header Size | 1 dword | Length of the Bitmap Info Header used to describe the bitmap colors, compression, … The following sizes are possible: 28h - Windows 3.1x, 95, NT, … 0Ch - OS/2 1.x F0h - OS/2 2.x
0012h | Width | 1 dword | Horizontal width of bitmap in pixels.
0016h | Height | 1 dword | Vertical height of bitmap in pixels.
001Ah | Planes | 1 word | Number of planes in this bitmap.
001Ch | Bits Per Pixel | 1 word | Bits per pixel used to store palette entry information. This also identifies in an indirect way the number of possible colors. Possible values are: 1 - Monochrome bitmap 4 - 16 color bitmap 8 - 256 color bitmap 16 - 16bit (high color) bitmap 24 - 24bit (true color) bitmap 32 - 32bit (true color) bitmap
001Eh | Compression | 1 dword | Compression specifications. The following values are possible: 0 - none (Also identified by BI_RGB) 1 - RLE 8-bit / pixel (Also identified by BI_RLE4) 2 - RLE 4-bit / pixel (Also identified by BI_RLE8) 3 - Bitfields (Also identified by BI_BITFIELDS)
0022h | Bitmap Data Size | 1 dword | Size of the bitmap data in bytes. This number must be rounded to the next 4 byte boundary.
0026h | HResolution | 1 dword | Horizontal resolution expressed in pixel per meter.
002Ah | VResolution | 1 dword | Vertical resolution expressed in pixels per meter.
002Eh | Colors | 1 dword | Number of colors used by this bitmap. For a 8-bit / pixel bitmap this will be 100h or 256.
0032h | Important Colors | 1 dword | Number of important colors. This number will be equal to the number of colors when every color is important.
0036h | Palette | N * 4 byte | The palette specification. For every entry in the palette four bytes are used to describe the RGB values of the color in the following way: 1 byte for blue component 1 byte for green component 1 byte for red component 1 byte filler which is set to 0 (zero)
0436h | Bitmap Data | x bytes | Depending on the compression specifications, this field contains all the bitmap data bytes which represent indices in the color palette.

Note:

The following sizes were used in the specification above:

Size | # bytes | Sign
char | 1 | signed
word | 2 | unsigned
dword | 4 | unsigned

Field Details

Some of the fields require some more information. The following chapters will try to provide this information:

Height Field

The Height field identifies the height of the bitmap in pixels. In other words, it describes the number of scan lines of the bitmap. If this field is negative, indicating a top-down DIB, the Compression field must be either BI_RGB or BI_BITFIELDS. Top-down DIBs cannot be compressed.

Bits Per Pixel Field

The Bits Per Pixel (BBP) field of the bitmap file determines the number of bits that define each pixel and the maximum number of colors in the bitmap.

- When this field is equal to 1.

The bitmap is monochrome, and the palette contains two entries. Each bit in the bitmap array represents a pixel. If the bit is clear, the pixel is displayed with the color of the first entry in the palette; if the bit is set, the pixel has the color of the second entry in the table.

- When this field is equal to 4.

The bitmap has a maximum of 16 colors, and the palette contains up to 16 entries. Each pixel in the bitmap is represented by a 4-bit index into the palette. For example, if the first byte in the bitmap is 1Fh, the byte represents two pixels. The first pixel contains the color in the second palette entry, and the second pixel contains the color in the sixteenth palette entry.

- When this field is equal to 8.

The bitmap has a maximum of 256 colors, and the palette contains up to 256 entries. In this case, each byte in the array represents a single pixel.

- When this field is equal to 16.

The bitmap has a maximum of 2^16 colors. If the Compression field of the bitmap file is set to BI_RGB, the Palette field does not contain any entries. Each word in the bitmap array represents a single pixel. The relative intensities of red, green, and blue are represented with 5 bits for each color component. The value for blue is in the least significant 5 bits, followed by 5 bits each for green and red, respectively. The most significant bit is not used.

If the Compression field of the bitmap file is set to BI_BITFIELDS, the Palette field contains three dword color masks that specify the red, green, and blue components, respectively, of each pixel. Each word in the bitmap array represents a single pixel.

Windows NT specific:

When the Compression field is set to BI_BITFIELDS, bits set in each dword mask must be contiguous and should not overlap the bits of another mask. All the bits in the pixel do not have to be used.

Windows 95 specific:

When the Compression field is set to BI_BITFIELDS, Windows 95 supports only the following 16bpp color masks: A 5-5-5 16-bit image, where the blue mask is 0x001F, the green mask is 0x03E0, and the red mask is 0x7C00; and a 5-6-5 16-bit image, where the blue mask is 0x001F, the green mask is 0x07E0, and the red mask is 0xF800.

- When this field is equal to 24.

The bitmap has a maximum of 2^24 colors, and the Palette field does not contain any entries. Each 3-byte triplet in the bitmap array represents the relative intensities of blue, green, and red, respectively, for a pixel.

- When this field is equal to 32.

The bitmap has a maximum of 2^32 colors. If the Compression field of the bitmap is set to BI_RGB, the Palette field does not contain any entries. Each dword in the bitmap array represents the relative intensities of blue, green, and red, respectively, for a pixel. The high byte in each dword is not used.

If the Compression field of the bitmap is set to BI_BITFIELDS, the Palette field contains three dword color masks that specify the red, green, and blue components, respectively, of each pixel. Each dword in the bitmap array represents a single pixel.

Windows NT specific:

When the Compression field is set to BI_BITFIELDS, bits set in each dword mask must be contiguous and should not overlap the bits of another mask. All the bits in the pixel do not have to be used.

Windows 95 specific:

When the Compression field is set to BI_BITFIELDS, Windows 95 supports only the following 32bpp color mask: The blue mask is 0x000000FF, the green mask is 0x0000FF00, and the red mask is 0x00FF0000.

Compression Field

The Compression field specifies the way the bitmap data is stored in the file. This information together with the Bits Per Pixel (BPP) field identifies the compression algorithm to follow.

The following values are possible in this field:

Value | Meaning
BI_RGB | An uncompressed format.
BI_RLE4 | An RLE format for bitmaps with 4 bits per pixel. The compression format is a two-byte format consisting of a count byte followed by two word-length color indices. For more information, see the following Remarks section.
BI_RLE8 | A run-length encoded (RLE) format for bitmaps with 8 bits per pixel. The compression format is a two-byte format consisting of a count byte followed by a byte containing a color index. For more information, see the following Remarks section.
BI_BITFIELDS | Specifies that the bitmap is not compressed and that the color table consists of three double word color masks that specify the red, green, and blue components, respectively, of each pixel. This is valid when used with 16- and 32- bits-per-pixel bitmaps.

When the Compression field is BI_RLE8, the bitmap is compressed by using a run-length encoding (RLE) format for an 8-bit bitmap. This format can be compressed in encoded or absolute modes. Both modes can occur anywhere in the same bitmap.

- Encoded mode consists of two bytes:

The first byte specifies the number of consecutive pixels to be drawn using the color index contained in the second byte. In addition, the first byte of the pair can be set to zero to indicate an escape that denotes an end of line, end of bitmap, or delta. The interpretation of the escape depends on the value of the second byte of the pair, which can be one of the following:

0 | End of line.
1 | End of bitmap.
2 | Delta. The two bytes following the escape contain unsigned values indicating the horizontal and vertical offsets of the next pixel from the current position.

- Absolute mode.

The first byte is zero and the second byte is a value in the range 03H through FFH. The second byte represents the number of bytes that follow, each of which contains the color index of a single pixel. When the second byte is 2 or less, the escape has the same meaning as in encoded mode. In absolute mode, each run must be aligned on a word boundary.

The following example shows the hexadecimal values of an 8-bit compressed bitmap.

03 04 05 06 00 03 45 56 67 00 02 78 00 02 05 01 02 78 00 00 09 1E 00 01

This bitmap would expand as follows (two-digit values represent a color index for a single pixel):

04 04 04

06 06 06 06 06

45 56 67

78 78

move current position 5 right and 1 down

78 78

end of line

1E 1E 1E 1E 1E 1E 1E 1E 1E

end of RLE bitmap

When the Compression field is BI_RLE4, the bitmap is compressed by using a run-length encoding format for a 4-bit bitmap, which also uses encoded and absolute modes:

- In encoded mode.

The first byte of the pair contains the number of pixels to be drawn using the color indices in the second byte. The second byte contains two color indices, one in its high-order four bits and one in its low-order four bits. The first of the pixels is drawn using the color specified by the high-order four bits, the second is drawn using the color in the low-order four bits, the third is drawn using the color in the high-order four bits, and so on, until all the pixels specified by the first byte have been drawn.

- In absolute mode.

The first byte is zero, the second byte contains the number of color indices that follow, and subsequent bytes contain color indices in their high- and low-order four bits, one color index for each pixel. In absolute mode, each run must be aligned on a word boundary.

The end-of-line, end-of-bitmap, and delta escapes described for BI_RLE8 also apply to BI_RLE4 compression.

The following example shows the hexadecimal values of a 4-bit compressed bitmap.

03 04 05 06 00 06 45 56 67 00 04 78 00 02 05 01 04 78 00 00 09 1E 00 01

This bitmap would expand as follows (single-digit values represent a color index for a single pixel):

0 4 0

0 6 0 6 0

4 5 5 6 6 7

7 8 7 8

move current position 5 right and 1 down

7 8 7 8

end of line

1 E 1 E 1 E 1 E 1

end of RLE bitmap

Colors Field

The Colors field specifies the number of color indices in the color table that are actually used by the bitmap. If this value is zero, the bitmap uses the maximum number of colors corresponding to the value of the BBP field for the compression mode specified by the Compression field.

If the Colors field is nonzero and the BBP field less than 16, the Colors field specifies the actual number of colors the graphics engine or device driver accesses.

If the BBP field is 16 or greater, then Colors field specifies the size of the color table used to optimize performance of Windows color palettes.

If BBP equals 16 or 32, the optimal color palette starts immediately following the three double word masks.

If the bitmap is a packed bitmap (a bitmap in which the bitmap array immediately follows the bitmap header and which is referenced by a single pointer), the Colors field must be either 0 or the actual size of the color table.

Important Colors Field

The Important Colors field s

pecifies the number of color indices that are considered important for displaying the bitmap. If this value is zero, all colors are important.