BMP

From MultimediaWiki
Jump to navigation Jump to search

BMP is the format used for storing lossless image data on Microsoft windows. In recent years Microsoft has started to favor other formats such as JPEG and PNG, but for many years BMP was the predominant format for lossless image storage. You can also find bitmap data embedded as resources in PE files and ICO files amongst other Microsoft formats. The article RLE talks about the compression scheme used in Microsoft bitmap files.

BMP files contain the following structures:

  • BMP Header (BITMAPFILEHEADER)
  • Bitmap information (BITMAPCOREHEADER, BITMAPINFOHEADER, BITMAPV4HEADER or BITMAPV5HEADER)
  • Colormap (array of RGBTRIPLE or RGBQUAD)
  • Bitmap data
struct BITMAPFILEHEADER {
 char magic[2];              // 'B' 'M'
 int32_t file_size;
 int16_t reserved[2];
 int32_t offset;            // offset to bitmap data, relative to start of file
} BITMAPFILEHEADER;
struct BITMAPCOREHEADER {
 int32_t header_size;       // 12
 int16_t width;
 int16_t height;
 int16_t planes;            // 1
 int16_t bits_per_pixel;    // must be 1, 4, 8 or 24
} BITMAPCOREHEADER;
struct BITMAPINFOHEADER {
 int32_t header_size;                // 40
 int32_t width;
 int32_t height;                     // if height > 0 then bitmap data is upside down (i.e. last scanline first)
                                     // else height = abs(height) and bitmap data is ordered normally
 int16_t planes;                     // 1
 int16_t bits_per_pixel;             // must be 0 (bpp is implied by embedded JPEG or PNG image), 1, 4, 8, 16, 24 or 32
 int32_t compression;                // 0 = uncompressed, 1 = RLE8, 2 = RLE4, 3 = bitfields, 4 = JPEG, 5 = PNG
 int32_t bitmap_data_size;
 int32_t hor_pixels_per_meter;
 int32_t vert_pixels_per_meter;
 int32_t number_of_colors;           // only relevant for bits_per_pixel < 16; if set to zero, it's assumed to be 2^bits_per_pixel
 int32_t number_of_important_colors; // 0 means all colors are important
} BITMAPINFOHEADER;
struct RGBTRIPLE {
 uint8_t blue;
 uint8_t green;
 uint8_t red;
} RGBTRIPLE;
struct RGBQUAD {
 uint8_t blue;
 uint8_t green;
 uint8_t red;
 uint8_t reserved;          // or alpha
} RGBQUAD;
  • Structure elements are written in little-endian format.
  • Each scanline of bitmap data is padded with zeroes to reach a multiple of four bytes.
  • Colormap data is only written for bits_per_pixel < 16.
  • For files containing the 12-byte BITMAPCOREHEADER, colormaps are arrays of RGBTRIPLE. In all other cases, they are arrays of RGBQUAD.
  • The most common BMP files consist of BITMAPFILEHEADER, BITMAPINFOHEADER, possible colormap and the bitmap data.