Windows bitmap

from Wikipedia, the free encyclopedia
Windows bitmap
File extension : .bmp, .dib
MIME type : image / x-ms-bmp, image / x-bmp, image / bmp
Magic number : 42, 4D hex
BM
Developed by: Microsoft
Current version: 5
Type: Raster graphics


Windows Bitmap (BMP) or device-independent bitmap (DIB) is a two-dimensional raster graphics format that was developed for the Microsoft Windows and OS / 2 operating systems and introduced with Microsoft Windows 3.0 , which appeared in 1990. The file extension is .bmp , less often .dib .

features

BMPs come in three different versions. Most of the BMP files are version 3; there are no previous versions. The later versions 4 and 5 are extremely rare.

Windows bitmaps (version 3) allow color depths of 1, 4, 8, 16, 24 or 32 bpp (bpp = bits per pixel, bits per pixel), although not all bits actually have to be used with 16 and 32 bpp . Alpha channels , color correction, and metadata are not supported. Windows bitmaps are stored either uncompressed or lossless using RLE ( run length encoding ) compression . This is a rather weak procedure, so that BMP files are much larger than other formats such as PNG and are rarely used for the Internet . The BMP format is relatively simple for this. BMPs are particularly widespread in the Windows environment; Common graphics software supports the format without any problems (with the exception of the more exotic color depths of 16 and 32  bpp ).

Theoretically, the maximum width / height of an image is 2,147,483,647 pixels (2 31  - 1). In practice, many decoders only accept significantly lower values.

File format (version 3)

File header
( BITMAPFILEHEADER )
Information block
( BITMAPINFO ):
Bitmap properties
( BITMAPINFOHEADER )

Possibly: color masks

Possibly: color table
Possibly: Unused space
Image data
Possibly: Unused space

BMP files consist of three parts: the file header, the information block and the image data (see diagram on the right).

In the following, WORD denotes a 16-bit unsigned integer , DWORD a 32-bit unsigned integer and LONG a 32-bit integer encoded in two's complement . BMP uses the little endian convention.

File header

BITMAPFILEHEADER (size: 14 bytes)
Offset (byte) Data type size Surname content
Dec Hex Windows style C-style
0 0 WORD uint16_t 2 bytes bfType ASCII character string "BM" (Hex: 0x42 0x4D, decimal: 66 77).
2 2 DWORD uint32_t 4 bytes bfSize Size of the BMP file in bytes. (unreliable)
6th 6th DWORD uint32_t 4 bytes bfReserved Reserved, depending on the software, 0 by default
10 A. DWORD uint32_t 4 bytes bfOffBits Offset of the image data in bytes from the beginning of the file.

This does not always have the value 54 (it results from 14 byte header + 40 byte info block) and must therefore be read out dynamically, otherwise the image may be incorrectly displayed.

Information block

Bitmap properties

The information block begins with the following structure, which contains the bitmap properties

BITMAPINFOHEADER (size: 40 bytes)
Offset (byte) Data type size Surname content
Dec Hex Windows style C-style
14th E. DWORD uint32_t 4 bytes biSize Size of the BITMAPINFOHEADER structure in bytes
18th 12 LONG int32_t 4 bytes biWidth Width of the bitmap in pixels. The first byte is the least significant and the last byte is the most significant.
22nd 16 LONG int32_t 4 bytes biHeight The amount indicates the height of the bitmap in pixels. The first byte is the least significant and the last byte is the most significant.
  • If the value is positive, the bitmap is a so-called "bottom-up" bitmap (the image data start with the lowest and end with the uppermost image line). This is the most common variant.
  • If the value is negative, the bitmap is a “top-down” bitmap (the image data start with the topmost and end with the bottommost image line).
26th 1A WORD uint16_t 2 bytes biPlanes 1 (Status in some older formats such as PCX for the number of color levels, but is not used for BMP)
28 1C WORD uint16_t 2 bytes biBitCount Specifies the color depth of the bitmap in bpp ; must be one of the following values: 1, 4, 8, 16, 24 or 32. At 1, 4 and 8 bpp the colors are indexed .
30th 1E DWORD uint32_t 4 bytes biCompression One of the following values:
  • 0 ( BI_RGB ): Image data are uncompressed.
  • 1 ( BI_RLE8 ): Image data are run length coded for 8 bpp. Only allowed if biBitCount = 8 and biHeight positive.
  • 2 ( BI_RLE4 ): Image data are run length coded for 4 bpp. Only allowed if biBitCount = 4 and biHeight positive.
  • 3 ( BI_BITFIELDS ): Image data are uncompressed and coded in a user-defined manner (using color masks). Only allowed if biBitCount = 16 or 32.
34 22nd DWORD uint32_t 4 bytes biSizeImage
  • If biCompression = BI_RGB : Either 0 or the size of the image data in bytes.
  • Otherwise: size of the image data in bytes.
38 26th LONG int32_t 4 bytes biXPelsPerMeter Horizontal resolution of the target output device in pixels per meter; but is mostly set to 0 for BMP files.
42 2A LONG int32_t 4 bytes biYPelsPerMeter Vertical resolution of the target output device in pixels per meter; but is mostly set to 0 for BMP files.
46 2E DWORD uint32_t 4 bytes biClrUsed
  • If biBitCount = 1: 0.
  • If biBitCount = 4 or 8: the number of entries in the color table; 0 means the maximum number (2, 16 or 256).
  • Otherwise: The number of entries in the color table (0 = no color table). Even if it is not necessary in this case, a color table recommended for color quantization can still be specified.
50 32 DWORD uint32_t 4 bytes biClrImportant
  • If biBitCount = 1, 4 or 8: The number of all colors used in the picture; 0 means all colors in the color table.
  • Otherwise:
    • If there is a color table and it contains all the colors used in the image: their number.
    • Otherwise: 0.

Color masks

If biCompression = BI_BITFIELDS, then 3 DWORDs follow , containing the bit masks for the red, green and blue values. Set bits mean that in the data of a pixel that bit is used for the respective color channel. The following conditions must be met:

  • set bits must follow one another directly;
  • for 16 bpp, the bits set must be in the two least significant bytes;
  • the bit masks of the individual color channels must not overlap.

Color table

  • If biClrUsed = 0:
    • If biBitCount = 1, 4 or 8: A color table with 2 biBitCount entries follows .
    • Otherwise: There is no color table.
  • Otherwise: A color table with biClrUsed entries follows.

Each entry in the color table is 4 bytes in size and contains one byte each for the blue, green and red components, as well as a byte set to 0 (in this order!).

Image data

The image data start at the offset bfOffBits. The size of the image data is approximately (only applies to image widths that can be divided by 4) biWidth × biHeight × biBitCount / 8 if biCompression = BI_RGB , otherwise biSizeImage.

The image data are stored line by line. If biHeight is positive, the image data starts with the last and ends with the first image line, otherwise it is the other way around. With BI_BITFIELDS and BI_RGB , the length of each line is a multiple of 4 bytes and, if necessary, is padded with zero bytes.

The further format of the image data depends on the value of the biCompression field:

  • BI_BITFIELDS
Each line of the image is aligned to an integer multiple of 4 bytes by filling it with zeros on the right. The format of the pixels is defined in the color masks. With 16 bpp only the two low-order bytes of the color masks are taken into account.
  • BI_RGB
Each line of the image is aligned to an integer multiple of 4 bytes by filling it with zeros on the right.
1, 4 or 8 bpp:
The data of each pixel consists of a 0-based index on the entry in the color table.
16 bpp:
The format is the same as for BI_BITFIELDS if the following color masks were used:
0x00007C00 for the red channel
0x000003E0 for the green channel
0x0000001F for the blue channel
Each color channel is 5 bits per pixel; a total of 32,768 possible colors result (one bit is unused).
24 bpp:
The data of each pixel consists of one byte each for the blue, green and red channels (in that order!).
32 bpp:
The format is the same as for BI_BITFIELDS if the following color masks were used:
0x00FF0000 for the red channel
0x0000FF00 for the green channel
0x000000FF for the blue channel
Each color channel is 8 bits per pixel; a total of 16,777,216 possible colors result (8 bits are unused). Some programs such as Adobe Photoshop interpret the remaining 8 bits ( 0xFF000000 ) as an alpha channel with 256 possible levels of transparency. However, this is not intended by the specification.
  • BI_RLE8 and BI_RLE4
Two consecutive bytes each form a data record. If the first byte has a value other than 0, the second byte is repeated as often (with BI_RLE4 : the next 2 nibbles in total, example: 05 67 → 6 7 6 7 6) as indicated by the first byte. However, if the first byte has the value 0, the meaning depends on the second byte:
0: End of the picture line.
1: End of the bitmap.
2: Shift of the current pixel position. The next two bytes indicate the shift to the right and down.
n = 3-255: The following n bytes (with BI_RLE4 : the following n nibbles) are accepted directly; the next data record is found at the following even offset (counted from the start of the image data).
The result is interpreted as in the uncompressed case.

Advantages and disadvantages

Benefits of bitmaps include:

  • The easy creation of already in memory existing computer pixel data.
  • The efficient and simple access to the image data due to their grid-like arrangement.
  • The color information can be changed by changing an existing palette without changing the image data itself.
  • The simple output on raster-based output devices such as monitors or printers.

One disadvantage is the large file size compared to compressed formats.

Versions 4 and 5

With Windows 95 and Windows 98, Microsoft introduced newer versions 4 and 5 of the BMP format, which allow alpha channels and color correction and can be used as a container format for PNG and JFIF files. However, these new formats are rarely found as separate files and are rarely supported by applications; they are more likely to be used as an internal format in Windows programs.

See also

literature

  • Thomas W. Lipp: Graphic formats. Microsoft Press, Unterschleißheim 1997, ISBN 3-86063-391-0 .
  • John Miano: Compressed Image File Formats. Addison-Wesley, Reading 2000, ISBN 0-201-60443-4 .

Web links

Individual evidence

  1. Source code for BMPImageReader.cpp in Chromium. (No longer available online.) In: chromium.googlesource.com. Archived from the original on December 8, 2015 ; accessed on December 2, 2015 . Info: The archive link was inserted automatically and has not yet been checked. Please check the original and archive link according to the instructions and then remove this notice. @1@ 2Template: Webachiv / IABot / chromium.googlesource.com
  2. Source code for nsBMPDecoder.cpp in Mozilla Firefox. In: dxr.mozilla.org. Retrieved December 2, 2015 .
  3. BITMAPFILEHEADER Structure
  4. BITMAPINFOHEADER Structure
  5. Bitmap Storage
  6. ^ Pros and Cons of Bitmap File Formats In: James D. Murray, William VanRyper: Encyclopedia of Graphics File Formats. 2nd Edition. O'Reilly & Associates, Bonn 1996, ISBN 1-56592-161-5 .