Windows bitmap
Encyclopedia : W : WI : WIN : Windows bitmap
.BMP or .DIB (device-independent bitmap) is a bitmapped graphics format used internally by the Microsoft Windows graphics subsystem (GDI), and used commonly as a simple graphics file format on that platform.
Images are generally stored with a color depth of 2 (1-bit), 16 (4-bit), 256 (8-bit), 65,536 (16-bit), or 16.7 million (24-bit) colors (the bits represent the bits per pixel). 8-bit images can also be greyscale instead of indexed color. An alpha channel (for transparency) may be stored in a separate file, where it is similar to a greyscale image. A 32-bit version with integrated alpha channel has been introduced with Windows XP and is used within its logon and theme system; it has yet to gain wide support in image editing software.
Storage Algorithm
BMP files are usually not compressed, so they are typically much larger than compressed image file formats for the same image. For example, an 800×600 24-bit image will occupy almost 1.4 megabytes. As such they are generally unsuitable for transferring images on the Internet or other slow or capacity-limited media.Depending on the color depth, a pixel in the picture will be stored using one or more bytes, which is determined by n/8 (n is the bit depth, since 1 byte equals 8 bits). The color of the pixel will be calculated (by the picture viewer, for example) based on the ASCII code of the bytes and the corresponding values read from the color palette. For more detailed information, see the section of bitmap file format below.
The approximate size for a n-bit (2n colors) bitmap in bytes can be calculated as:
size of BMP file [\approx 54+4 \cdot 2^n+\frac], where height and width are given in pixels.
It should be noted that 54 in the above formula is the size of the header of the bitmap file. And [4 \cdot 2^n] is the size of the color palette. Notice that this is an approximation, as for a n-bit bitmap image, although there can be maximum [2^n] colors, a specific image may not use all of these colors. Since the color palette only defines the colors that are used by the image, the actual size of the color palette will be smaller than [4 \cdot 2^n].
For detailed information on how these values are derived, see the sections on file format below.
Due to storage algorithm, the calculated size will be slightly different from the actual file size, depending on several image parameters.
Typical File Format
A typical bitmap file usually contains the following blocks of data:
- Bitmap Header: stores general information about the bitmap file.
- Bitmap Information: stores detailed information about the bitmap image.
- Color Palette: stores the definition of the colors being used.
- Bitmap Data: stores the actual image, pixel by pixel.
Bitmap Header
This block of bytes is used for identification. A typical application will read this block first to ensure that the file is actually a bitmap file and that it is not damaged.
- Bytes #0-1 store the data that will be used to identify the bitmap file. Typical values for these 2 bytes are BM.
- Bytes #2-5 store the size of the bitmap file using a dword.
- Bytes #6-9 are reserved. Actual values depend on the application that creates the image.
- Bytes #10-13 store the offset, i.e. starting address, of the byte where the bitmap data can be found.
Bitmap Information
This block of bytes tells the application detailed information about the image, which will be used to display the image on the screen. It starts at byte #14 of the file.
- Bytes #14-17 specify the header size. Values are: 40 - Windows V3, 12 - OS/2 V1, 64 - OS/2 V2, 108 - Windows V4, 124 - Windows V5
- Bytes #18-21 store the bitmap width in pixels.
- Bytes #22-25 store the bitmap height in pixels.
- Bytes #26-27 store the number of color planes being used. Not often used.
- Bytes #28-29 store the number of bits per pixel, which is the color depth of the image. Typical values are 1, 4, 8 and 24.
- Bytes #30-33 define the compression method being used. Possible values are 0, 1, 2, 3, 4 and 5:
0 - none (also identified by BI_RGB) 1 - RLE 8-bit/pixel (also identified by BI_RLE8) 2 - RLE 4-bit/pixel (also identified by BI_RLE4) 3 - Bit field (also identified by BI_BITFIELDS) 4 - a JPEG image (also identified by BI_JPEG) 5 - a PNG image (also identified by BI_PNG)However, since most BMP images are uncompressed, the most common value is 0.
- Bytes #34-37 store the image size. This is the size of the raw bitmap data (see below), and should not be confused with the file size.
- Bytes #38-41 store the horizontal resolution of the image.
- Bytes #42-45 store the vertical resolution of the image.
- Bytes #46-49 store the number of colors used.
- Bytes #50-53 store the number of important colors used. This number will be equal to the number of colors when every color is important.
Color Palette
This block of bytes define the colors being used inside the image. As stated above, the bitmap picture will be stored pixel by pixel. Each pixel is described by a value which will be stored using one or more bytes. Therefore, the purpose of the color palette is to tell the application the actual color that each of these values corresponds to.A typical bitmap file uses the RGB color model. In this model, a color is created by mixing different intensities (which can vary from 0 to 255) of red (R), green (G) and blue (B). Or in other words, a color will be defined using its 3 values for R, G and B.
In the bitmap file implementation, the color palette contains many entries; the number of entries is the number of colors being used in the picture. Each entry contains 4 bytes: 3 for red, green and blue and the last one is unused (which will be filled with 0 by most applications). For each byte, a value of 0 indicates that the corresponding color (either red, green, or blue) is not used to create the current image color. On the contrary, a value of 255 indicates that maximum intensity is used.
Bitmap Data
This block of bytes describes the image, pixel by pixel. Pixels are stored from the bottom to the top of the images, and then from the left to the right. Each pixel is described using one or more bytes. If the number of bytes matching a horizontal line in the image is not a quadruple, i.e. an integer divisible by 4, the line is padded with null-bytes, which usually have ASCII codes of 0.Miscellaneous
Despite the huge file size, the simplicity of BMP and its widespread familiarity in MS Windows and elsewhere, as well as the fact that this format is well-documented and free of patents, makes it a very common format that image processing programs from many operating systems can read and write.The X Window System uses a similar .XBM format for true single-bit black and white images, and .XPM (pixelmap) for color images. There is also a .RAW format, which saves raw data with no other information. The Portable Pixmap file format (.PPM) and Truevision TGA (.TGA) formats also exist, but are rarely used - or only for special purposes. Yet other formats store as "bitmaps" (as opposed to vector graphics), but use compression or color indexes, and thus are not strictly considered true bitmaps.
Most BMP files compress very well with lossless data compression algorithms such as ZIP because they contain redundant data.
See also
External links
- [Microsoft's MSDN page on Bitmaps]
- [Bitmap Storage Explanation]
- [Bitmap File Structure]
- [Wotsit.org's entry on the BMP format]
- [An introduction to DIBs (Device Independent Bitmaps)]
- [Example BMP images with various internal formats]
- [Turbo C++ 3.0 Code to open 16-color bitmaps in DOS]
From Wikipedia, the Free Encyclopedia. Original article here. Support Wikipedia by contributing or donating.
All text is available under the terms of the GNU Free Documentation License See Wikipedia Copyrights for details.
