What is the BMP file format used by many image file viewers? How can this bitmap image file format invented by Microsoft be viewed?

Portrait photo of Andreas Pehnack

Andreas Pehnack

What is the File Format BMP?

BMP stands for "Bitmap Image File," and is a type of image file format used to store digital images. BMP files are widely supported and can be viewed on most computers, as well as on many mobile devices and web browsers.

The common format was originally developed by Microsoft and is still in widespread use today. It is a relatively simple file format that stores a bitmap image as a grid of pixels, where each pixel is represented by a color value. BMP files can be uncompressed or compressed using a lossless compression algorithm called RLE (Run Length Encoding).

One of the main advantages of the BMP format is that it is widely supported and can be opened by most image editing software and web browsers. However, BMP files are generally larger in size than other image file formats, such as JPEG and PNG, which can make them less suitable for use in situations where file size is a concern, such as when storing large numbers of images or when uploading images to the internet.

In addition to standard BMP files, there are also several variations of the BMP file format, including:

  • DIB (Device Independent Bitmap): This is a version of the BMP file format that includes additional metadata, such as the color depth and resolution of the image.
  • RLE: This is a version of the BMP file format that uses RLE compression to reduce the image size.
  • OS/2 BMP: This is a version of the BMP file format that was developed for use on the OS/2 operating system.

Overall, the BMP file format is a simple and widely supported image file format that is suitable for a wide range of applications. It is a good choice for storing images that need to be viewed on a variety of devices and platforms, but may not be the most efficient choice when file size is a concern.

BMP files at a glance#

  • BMP files can store images with a range of color depths, including 1-bit (black and white), 4-bit (16 colors), 8-bit (256 colors), 24-bit (true color), and 32-bit (true color with alpha channel).
  • The format includes a header that contains metadata about the image, such as the image width and height, the color depth, and the number of bytes in the bitmap image data.
  • The pixel data stored in the bmp file is organized in rows from bottom to top, with each row being padded to a multiple of 4 bytes. This means that the actual size of a data block is often larger than the size of the raw data containing pixels itself.
  • The format does not support transparent pixels, but a variation called the Alpha Bitmap (ABM) file format can be used to store images with an alpha channel for transparency.
  • BMP files can be edited using image editing software, such as Photoshop or GIMP, or using a hex editor if more advanced changes to the raw bitmap data are needed.
  • BMP files are often used in Microsoft Windows operating systems and are the default image format for saving screen captures in Windows.
  • While the format is not as widely used as other image formats, such as JPEG or PNG, it is still a common choice for storing simple images and is supported by most image editing software and web browsers.

Bitmap File Header#

The start of the file contains a header structure. This structure contains metadata about the image, such as the size, color depth, and resolution of the image. All integer values are stored in little-endian format Here is an example of the structure of a BMP file header:

typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER;

The BITMAPFILEHEADER structure contains the following fields:

  • bfType: This field specifies the type of the BMP file in the first 2 bytes of the BMP. It should be set to the value "BM" to indicate that the file is a bitmap file.
  • bfSize: This field specifies the size of the BMP in bytes.
  • bfReserved1 and bfReserved2: These fields are reserved for future use and should be set to 0.
  • bfOffBits: This field specifies the offset from the beginning of the file to the start of the pixel data.

Bitmap Info Header#

In addition to the BITMAPFILEHEADER structure, a BMP file also includes a BITMAPINFOHEADER structure, which contains additional metadata about the image, such as the image height and width, the number of planes, and the color depth.

Here is an example of the structure of a BITMAPINFOHEADER:

typedef struct tagBITMAPINFOHEADER {
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER;

The BITMAPINFOHEADER structure contains the following fields:

  • biSize: This field specifies the size of the BITMAPINFOHEADER structure in bytes.
  • biWidth and biHeight: These fields specify the width and height of the image in pixels.
  • biPlanes: This field specifies the number of color planes in the image. It should be set to 1.
  • biBitCount: This field specifies the color depth of the image, in bits per pixel.
  • biCompression: This field specifies the type of compression used in the image, if any.
  • biSizeImage: This field specifies the size of the image data in bytes.
  • biXPelsPerMeter and biYPelsPerMeter: These fields specify the horizontal and vertical resolution of the image, in pixels per meter.
  • biClrUsed: This field specifies the number of colors in the image's color table.
  • biClrImportant: This field specifies the number of important colors in the image's color table.

After the BITMAPINFOHEADER structure may follow a color table and the pixel data itself. The color table is a list of colors that are used in the image, and is present if the image has a color depth of less than 24 bits per pixel.

Pixel format of the image data in BMP files#

In BMP files, the pixel data is stored as a grid of pixels, with each pixel being represented by a color value. The pixel data is stored in rows from bottom to top, with each row being padded to a multiple of 4 bytes. This means that the actual size of a BMP file may be larger than the size of the image data itself.

The format supports a range of color depths, including 1-bit (black and white), 4-bit (16 colors), 8-bit (256 colors), 24-bit (true color), and 32-bit (true color with alpha channel). The color depth of a BMP file determines the number of bits used to represent the color of each pixel, and therefore the number of colors that can be displayed in the image. For example, a BMP file with a color depth of 8 bits per pixel can display up to 256 different colors.

In addition to the pixel data, BMP files may also include a color table, which is a list of colors used in the image. The color table is present if the image has a color depth of less than 24 bits per pixel. The color table is used to map the pixel values in the image data to actual colors.

Run Length Encoding#

In the BMP file format, run length encoding (RLE) is a lossless compression algorithm that is used to reduce the size of the file. RLE works by replacing consecutive runs of the same data value with a single value and a count of the number of times that value appears. For example, if the bitmap data contained the following sequence of pixel values:

0 0 0 0 1 1 1 1 1 2 2 2 0 0 0 0

Using RLE compression, this sequence could be represented as:

4 0 9 1 6 2 4 0

The first number (4) indicates the number of times the value 0 appears, and the second number (9) indicates the number of times the value 1 appears, and so on. By using RLE compression, the size of the image file can be reduced, which can be beneficial in situations where file size is a concern.

RLE compression is a simple and effective compression algorithm, but it is not as efficient as other algorithms, such as JPEG or PNG. As a result, BMP files that use RLE compression may still be larger in size than other image file formats.

How to Identify a BMP File#

There are several ways to identify a bitmap file:

  1. File extension: BMP files are typically identified by their file extension, which is ".bmp". If a file has this extension, it is likely it contains a bitmap.
  2. File header: As explained above a specific file header can be used to identify them. The first two bytes of the BMP file contain the characters "B" and "M".
  3. File content: BMP image files are composed of a series of blocks of data, including the file header, image metadata, and image data. By examining the content of a file, it is possible to determine whether it is a BMP file or not. For example, the file length can be found in the `bfSize` field of the Bitmap File Header.
  4. File utilities: There are various file utilities and tools that can be used to identify the file type. For example, the "file" command on Linux systems can be used to identify the type of a file based on its content.

It is also worth noting that BMP images can be disguised by changing their file extension, so simply checking the file extension may not be sufficient to determine whether a file is a BMP file or not. In these cases, examining the file header or the file content may be necessary to identify the file correctly.

Versions of the BMP File Format#

There are several versions of BMP. The original version of the bitmap file format, also known as the "Windows BMP" or "Device-Independent Bitmap (DIB)" format, was developed by Microsoft and is still in widespread use today. This version is supported by most image editing software and web browsers.

In addition to the original format, there are also several variations of the it, including:

  • OS/2 BMP: This is a version of the file format that was developed for use on the OS/2 operating system. It is similar to the original format, but has some differences in the file header and color table format.
  • Alpha Bitmap (ABM): This is a version of the format that includes support for an alpha channel, which allows for transparency in the image.
  • Windows V3 BMP: This is a newer version of the format that includes support for additional metadata and color spaces, such as the sRGB color space.
  • Windows V4 BMP: This is an even newer version of the format that includes support for additional metadata and color spaces, such as the scRGB color space.

Overall, there are several variations of the this format, but the original version is the most widely supported and is the one that is most commonly used.

How can a BMP file be opened?#

BMP files can be opened using most image editing software, as well as many web browsers and operating system image viewers. Some common applications that can be used to open BMP files include:

  • Microsoft Paint: This is a basic image editing application that is included with most versions of the Windows operating system. It can be used to view and edit BMP files.
  • Adobe Photoshop: This is a professional image editing application that can be used to view and edit BMP files, as well as other image file formats.
  • GIMP: This is a free, open-source image editing application that can be used to edit and display the image, as well as other image file formats.
  • Web browsers: Most web browsers can be used to open a BMP file by simply dragging the file into the browser window or by opening the file using the browser's "File" menu.
  • Operating system image viewers: Most operating systems include a default image viewer that can be used to view BMP files. For example, on Windows systems, the Photos app can be used to view BMP files, and on macOS systems, the Preview app can be used.

In addition to these applications, there are also many other programs that can be used to open BMP files, including specialized image viewing and editing software, as well as hex editors, which can be used to view and edit the raw data of the file.

Synalyze It! and Hexinator allow to display the data fields of the `BITMAPFILEHEADER` and `BITMAPINFOHEADER` structures directly.

Screen Shot with BMP image file decoded by Synalyze It! Pro

References#

  1. Wikipedia has detailed information about the image format in this article. It explains many details about the different structures and data fields.
  2. Microsoft's specifications and explanations of bitmaps used by Windows: https://learn.microsoft.com/en-gb/windows/win32/gdi/about-bitmaps