Filetype Structure - Bitmaps

Bitmap File Header
Byte Number Description
0-1 File descriptor. Always equal to the bytes, "BM" (0x42 0x4d)
2-5 File size in bytes
filesize = byte[2] + (byte[3] * 28) + (byte[4] * 216) + (byte[5] * 224)
6-9 Reserved bytes, all bytes are zero
10-13 Bitmap Header Offset Length. Bitmap headers are 54 bytes in length. Byte 10 contains the value 54, bytes 11-13 are zero.
Bitmap Information Header
Byte Number Description
14-17 Bitmap Information Header Offset. The Information Header consists of 40 bytes. Byte 14 has a value of 40, bytes 15-17 are zero.
18-21 Bitmap image width in pixels.
width = byte[18] + (byte[19] * 28) + (byte[20] * 216) + (byte[21] * 224)
22-25 Bitmap image height in pixels.
height = byte[22] + (byte[23] * 28) + (byte[24] * 216) + (byte[25] * 224)
26-27 Image planes. Byte 26 has a value of one, byte 27 has a value of zero.
28-29 Pixel bit-count (color depth). For 24-bit color, byte 28 has a value of 24, and byte 29 has a value of zero.
30-33 Bitmap compression. For most cases, the values of these bytes are zero.
34-37 Image data block size in bytes.
imageDataSize = filesize - 54. Includes fill (padding) bytes.
38-41 Horizontal pixel resolution. (example: 150 dpi)
xResolution = byte[38] + (byte[39] * 28) + (byte[40] * 216) + (byte[41] * 224)
42-45 Vertical pixel resolution. (example: 150 dpi)
yResolution = byte[38] + (byte[39] * 28) + (byte[40] * 216) + (byte[41] * 224)
46-49 Colors Used. Obsolete. For most purposes, these bytes are zero.
50-53 Important Colors Used. Obsolete. For most purposes, these bytes are zero.
Image Data Block
Byte Number Description
54-n Pixel color data and ending padding bytes.
Image Data Block

The length of the entire bitmap header is 54 bytes. (i.e. The actual image data starts after 54 bytes of data.) Bitmaps are written upside-down. This means that the first pixel read is the lower left corner pixel. Bitmap readers build the image from bottom to top, left to right.

The bitmap specification calls for the image data to be arranged in four byte-blocks. In order to make the four byte-blocks equal length, padding bytes must be added to the end of the rows to create a row length that is evenly divisible by four. Therefore the number of added padding bytes can be calculated by:

numPadBytes = width % 4;

where width is the value from bytes 18-21.