BMP graphics files explained

The BMP file was originated in 1991 when Microsoft had Windows 3.0 as the latest Windows platform. BMP is a Microsoft Device Independent Bitmap file and can contain images that are 1, 4, 8, or 24 bits/pixel. The 1-, 4- and 8-bit images have color maps while the 24-bit images are direct color (RGB).

BMP files store the Lo byte first, i.e. they use Intel conventions. Each file contains a file header, a bitmap header, a color map (not in 24-bit) and the image data. Some BMP files can be compressed using the RLE scheme. (See my description on PCX files for more info on RLE)

THE HEADER(s):
Offset  Size   Contents   Meaning
---------------------------------------------------------------------------------
  00	02	"BM"        Microsofts' BMP ID word
  02	04	varies      Size in bytes of the file
  06	04	00,00       Reserved
  10	04	varies      Offset in file where image starts
  14	04	40          Size of bitmap header
  18	04	varies      Width in pixels
  22	04	varies      Height in pixels
  26	02	1           Number of image planes (only one)
  28	02	varies      Bits per pixel (1,4,8, or 24)
  30	04	varies      Compression type
  34	04	varies      Size of compressed image (or zero)
  38	04	varies      Horizontal Res. in pixels/meter
  42	04	varies      Vertical Res. in pixels/meter
  46	04	varies      Number of colors used
  50	04	varies      Number of 'important' colors
  54	04*	varies      Color Map

* The length of the Color Map is 4 bytes plus the difference to 
the end of the bitmap header.

The Color Map sizes are normally 2 ,16, or 256 entries, but can be smaller if the image does not need a full set of colors. If the value at offset 46 in the header above is nonzero, it contains the number of colors used, which is also the number of entries in the color map. If the entry is zero then the color map is full size.

Please Note: Because the VGA provides only 64 levels of R,G, and B, the Color Map values must be divided by four (shr 2 times) when reading the BMP file for VGA display.

If the display device can not display all the colors available in the image, then BMP files put the 'important' color values at the front of the color map for better display.

Color map entries are four bytes each:
offset  Description
---------------------------------
  00	Blue value
  01	Green value
  02	Red value
  03	zero (reserved)


The bitmap image follows the color map. If the data is a 4- or an 8-bit image then it can be compressed using the RLE compression scheme.

Bits are stored a row at a time. Each row is padded to a 4-byte boundary with zero bytes. Rows are stored in order from the bottom of the image to the top.

Monochrome images (1-bit images): Each pixel is a single bit. The hi-order bit is the leftmost pixel.

Uncompressed 16 color images (4-bit images): These images are packed two pixels per byte, with the hi-nibble being the leftmost pixel.

Compressed 16 color images (4-bit images): These images are compressed using the RLE encoding scheme and consist of a sequence of groups. There are 3 kinds of groups: repeating, literal, and special.
A repeating group is 2 bytes, with the first byte as a pixel count and the second byte is a pair of pixels.
For example: 05h 16h translates to five pixels long of 01h 06h 01h 06h 01h. Notice you don't use the lo-order of the pixels byte if the count is odd.
A literal group is a zero byte, a byte with the pixel count, and the literal pixels. The pixel count must be at least 3. The literal pixels are padded out with zeros to an even number of bytes.
For example: 00h 05h 12h 34h 50h 00h translates to 01h 02h 03h 04h 05.
A special sequence of 00h 00h represents the end of a row. The special sequence of 00h 01h represents the end of the bitmap. The special sequence 00h 02h xx yy represents a position delta, saying to continue the image xx pixels to the right and yy pixels down.

Uncompressed 256 color images (8-bit images): These images are packed one pixel to a byte, with each row padded to a 4 byte boundary.

Compressed 256 color images (8-bit images): These images are packed using the RLE encoding scheme, consisting of a sequence of groups. There are three kinds of groups. (See compressed 4-bit color scheme above)

24-bits/pixel scheme consist of three bytes per pixel. The first byte being BLUE, the second GREEN, and the third being RED. Each row is zero padded to a 4-byte boundary.


If you have any other questions or see a mistake that I made, please e-mail me and I will do my best to help out.