The DIB File

You can create a device-independent bitmap and save it to a disk file (with the extension .BMP) in either the SDKPAINT program included in the Windows Software Development Kit or the PAINTBRUSH program included in the retail Windows product. The file begins with a file header section defined by the BITMAPFILEHEADER structure. This structure has five fields:

Field Size Description

bfType WORD The bytes ”BM“ (for bitmap)
bfSize DWORD Total size of the file
bfReserved1 WORD Set to 0
bfReserved2 WORD Set to 0
bfOffBits DWORD Offset to the bitmap bits from the beginning of the file

This is followed by another header defined by the BITMAPINFOHEADER structure. This structure has 11 fields:

Field Size Description

biSize DWORD Size of the BITMAPINFOHEADER structure in bytes
biWidth DWORD Width of the bitmap in pixels
biHeight DWORD Height of the bitmap in pixels
biPlanes WORD Set to 1
biBitCount WORD Color bits per pixel (1, 4, 8, or 24)
biCompression DWORD Compression scheme (0 for none)
biSizeImage DWORD Size of bitmap bits in bytes (only required if compression is used)
biXPelsPerMeter DWORD Horizontal resolution in pixels per meter
biYPelsPerMeter DWORD Vertical resolution in pixels per meter
biClrUsed DWORD Number of colors used in image
biClrImportant DWORD Number of important colors in image

All fields following the biBitCount field may be set to 0 for default values.

If biClrUsed is set to 0 and the number of color bits per pixel is 1, 4, or 8, the BITMAPINFOHEADER structure is followed by a color table, which consists of two or more RGBQUAD structures. The RGBQUAD structure defines an RGB color value:

Field Size Description

rgbBlue BYTE Blue intensity
rgbGreen BYTE Green intensity
rgbRed BYTE Red intensity
rgbReserved BYTE Set to 0

The number of RGBQUAD structures is usually determined by the biBitCount field: 2 RGBQUAD structures are required for 1 color bit, 16 for 4 color bits, and 256 for 8 color bits. However, if the biClrUsed field is nonzero, then the biClrUsed field contains the number of RGBQUAD structures in the color table.

The color table is followed by the array of bits that define the bitmap image. This array begins with the bottom row of pixels. Each row begins with the leftmost pixels. Each pixel corresponds to 1, 4, 8, or 24 bits.

For a monochrome bitmap with 1 color bit per pixel, the first pixel in each row is represented by the most significant bit of the first byte in each row. If this bit is 0, the color of the pixel can be obtained from the first RGBQUAD structure in the color table. If the bit is 1, the color is given by the second RGBQUAD structure in the color table.

For a 16-color bitmap with 4 color bits per pixel, the first pixel in each row is represented by the most significant four bits of the first byte in each row. The color of each pixel is obtained by using the 4-bit value as an index into the 16 entries in the color table.

For a 256-color bitmap, each byte corresponds to one pixel. The color of the pixel is obtained by indexing the 256 entries in the color table by the byte.

If the bitmap image contains 24 color bits per pixel, each set of three bytes is an RGB value of the pixel. There is no color table (unless the biClrUsed field in the BITMAPINFOHEADER structure is nonzero).

In each case, each row of the bitmap data contains a multiple of four bytes. The row is padded on the right to ensure this.

The bitmap format supported in OS/2 1.1 and above is very similar. It begins with a BITMAPFILEHEADER structure but is followed by a BITMAPCOREHEADER structure. (You can determine if a bitmap file uses this format or the Windows 3 format by examining the first field of this structure.) The color table consists of RGBTRIPLE structures rather than RGBQUAD structures.