The BMP File Format

中文English

Abstract

An introduction to Microsoft's BMP format for uncompressed images.

Contents
  1. What Is BMP?
  2. Designing an Image File Format
  3. The BMP File Format
    1. File Header
    2. Bitmap Information Header
    3. Palette
    4. Pixel Data
  4. Writing a BMP File

What Is BMP?

BMP is an image file format created by Microsoft. Its name is short for bitmap, and its extension is .bmp. BMP images are usually stored with little or no compression, so they can be too large to transfer efficiently over the internet.

Designing an Image File Format

Files are stored as binary data. For example, if you write helloworld into a file called hello.txt, the computer stores it as:

$ od -tax  hello.txt
0000000    h   e   l   l   o   w   o   r   l   d
                 6c6c6568        726f776f        0000646c
0000012

h maps to 0x68, e to 0x65, l to 0x6c, o to 0x6f, w to 0x77, r to 0x72, r to 0x6c, and d to 0x64 The text is encoded as UTF-8, which is compatible with ASCII. The first 128 characters are each represented by a single byte, so these English letters use their ASCII values..

An image file stores colors where a text file stores characters. An image is a collection of colors. How can we represent them in binary?

The simplest approach is to use three bytes for each color, one each for red, green, and blue. For example, 0xff0000 is red, 0x00ff00 is green, and 0x000000 is black.

A 4-by-4 image going from black to white can be represented like this:

000000 111111 222222 333333
444444 555555 666666 777777
888888 999999 aaaaaa bbbbbb
cccccc dddddd eeeeee ffffff

Add two 0x00 bytes as an end marker. That brings the file size to 4 × 4 × 3 + 2 = 50 bytes. A 1080-by-1920 image would take 1080 × 1920 × 3 + 2 = 6,220,802 bytes, or about 6 MB. That is a lot of data to transfer for a single image.

The BMP File Format

BMP works much like this example. It can also use a palette, which lets pixels store indices into a list of colors. The indices save space, though the files can still be large.

A BMP file can be divided into four parts:

  1. File header
  2. Bitmap information header
  3. Palette
  4. Pixel data

Here is what each part contains.

File Header

The first 14 bytes are the file header, which contains general information about the file:

FieldBytesDescription
File signature (magic number)2Identifies the file type. Usually 0x42 0x4d, the ASCII characters “BM”.
File size4The maximum BMP file size is $2^{8*4} = 2^{32} = 4\text{(GB)}$.
Reserved4Reserved for future use.
Pixel data offset4The byte offset of the pixel data from the beginning of the file.

Bitmap Information Header

The bitmap information header follows. A common version is 40 bytes long:

FieldBytesDescription
Bitmap information header size4For a 40-byte header, this is 0x00000028.
Image width4Measured in pixels.
Image height4Measured in pixels.
Color planes2Must be 1.
Bit depth2Bits per pixel. Common values are 1, 4, 8, 16, 24, and 32.
Compression method4Usually 0, meaning uncompressed.
Pixel data size4Size of the pixel data in bytes.
Horizontal resolution4Measured in pixels per meter. A common value is 0x00000b12, or 2,834 pixels per meter.
Vertical resolution4Uses the same units as horizontal resolution.
Number of palette colors4The number of palette entries. A value of 0 indicates $2^{\text{bit depth}}$ colors.
Number of important colors4Usually 0, meaning all colors are important.

Palette

The palette contains one four-byte entry per color. For example, 0xff000000 represents blue and 0xffff0000 represents cyan. The palette size is $\text{number of colors} * 4 \text{(B)}$. A single color takes just four bytes.

The palette can have a variable number of entries. In this layout, subtract 52 from the pixel data offset stored in the file header to get the palette size in bytes, then divide by four to get the entry count.

Pixel Data

Suppose the palette entries are 0xff000000, 0x00ff0000, and 0x0000ff00, and the pixel data is 0x00, 0x02, 0x00, 0x01. The resulting colors are blue, red, blue, and green. The bit depth determines the size of each index.

The complete file looks like this:

Layout of a BMP file
.bmp file layout

The final two bytes, 0x0000, can be omitted.

Writing a BMP File

Now that we know the layout, we can create an empty file and write the data byte by byte:

with open('a.bmp', mode='wb') as f:
    # 定义文件头,一共 14 字节
    FILE_HEADER = bytes([
        0x42, 0x4d,              # ASCII 的 "BM"
        0x38, 0x05, 0x00, 0x00,  # 文件的大小:4 字节
        0x00, 0x00,              # 保留的字节
        0x00, 0x00,              # 保留的字节
        0x36, 0x04, 0x00, 0x00   # 像素数据区的偏移量
    ])
    f.write(FILE_HEADER)

    # 位图信息头,一共 40 字节
    FILE_INFO = bytes([
        0x28, 0x00, 0x00, 0x00,  # 位图信息头大小:40 个字节
        0x10, 0x00, 0x00, 0x00,  # 图像的宽度,单位为像素:16 像素
        0x10, 0x00, 0x00, 0x00,  # 图像的高度:16 像素
        0x01, 0x00,              # 色彩平面数:只能是 1
        0x08, 0x00,              # 每个像素的位数:8位
        0x00, 0x00, 0x00, 0x00,  # 是否压缩:不压缩
        0x00, 0x01, 0x00, 0x00,  # 像素数据区占的字节数
        0x12, 0x0b, 0x00, 0x00,  # 图像的横向分辨率
        0x12, 0x0b, 0x00, 0x00,  # 图像的纵向分辨率
        0x00, 0x00, 0x00, 0x00,  # 调试板的颜色数:为 0 时表示颜色数为 2^{色深} 个
        0x00, 0x00, 0x00, 0x00   # 重要颜色的数量:为 0 时说明所有颜色都重要
    ])
    f.write(FILE_INFO)

    # 调色板,从全黑到全白,共 256 个颜色
    PALETTE_SIZE = 256
    for i in range(PALETTE_SIZE):
        f.write(bytes([i, i, i, 0x00]))

    # 像素数据区
    # 注意:数据是从图像左下角往右上角写的。
    # 也就是说左下角为黑色,右上角为白色。
    IMAGEDATA_SIZE = 256
    for i in range(IMAGEDATA_SIZE):
        f.write(bytes([i]))

    f.write(bytes([0x00, 0x00]))  # 结束标志

    f.flush()
    f.close()

The result is a 16-by-16 bitmap:

Generated bitmap
Generated .bmp file

The pixel data starts at the lower-left corner and runs to the right. The first byte is 0x00, so the lower-left corner is black.

The bitmap format is easy to understand. It is less suitable for sending images over the internet because the color data is generally uncompressed, and larger images quickly produce larger files.

For example, the original estimate for a 256-by-256 grayscale image is 54 + (4 × 256 + 512 × 512 × 1) = 263,222 bytes, or about 257 KB. By comparison, a 400-by-400 color JPEG can take just 25,730 bytes, or about 25 KB. The exact file size depends on the JPEG compression settings.

Formats such as JPEG and PNG are common on the web because they save bandwidth.

JPEG filters high-frequency image components before encoding, which usually produces a much smaller file. BMP is easier to inspect and experiment with because the pixel data is typically uncompressed.

Loading Disqus comments…