BMP file format analysis
Introduction
BMP (Bitmap-file) graphic file is a graphic file format in Windows, and all image processing software running in a Windows environment supports BMP image file format. The internal image drawing operations in the Windows system are based on BMP. WINDOWS 3.0 The BMP diagram file format is related to the display device, so this BMP image file is called the device-related bitmap DDB (Device-Dependent Bitmap) file format. The BMP image file after Windows 3.0 has nothing to do with the display device, so this BMP image file format is called the device-free bitmap Dib (Device-Independent Bitmap) format (Note: After Windows 3.0, there is still a DDB bit in the system. Mage, like BitBLT () This function is based on the DDB bitmap. Just if you want to save images in the BMP format to disk files, Microsoft strongly recommends that you save in DIB format), the purpose is to make Windows Any type of display device displays the stored image. BMP bitmap file default file extension is BMP or BMP (sometimes it will also be extended in .dib or .rle).
File structure
Bitmap files can be viewed from 4 parts: Bitmap-File HEADER, Bitmap-Information Header, Color Table, and Defining Bits byte Arrays, it It has the form as follows.
The composition of the bitmap file structure Name symbol bitmap-file header BitmapFileHeaderBMFH BitmapinfoHeaderBMIH color table (Color Table) RGBQUADACOLORS [] Image Data Array Byteabitmapbits []
The bitmap file structure can be integrated in Table 6-01.
Table 01 Bitmap file structure content summary
The name size of the offset domain Size content image file header 0000h file identifier 2 Bytes two bytes of content to identify the type of bitmap: 'BM': Windows 3.1x, 95, NT, ... 'BA': OS / 2 Bitmap Array 'CI': OS / 2 Color icon 'CP': OS / 2 Color Pointer 'IC': OS / 2 icon 'PT': OS / 2 Pointer Note: Because the OS / 2 system is not popular, When programming, you only need to judge the first identifier "BM". 0002HFile Size1 DWORD 0006HRESERVED1 DWORD 0006HRESERVED1 DWORD 0006HRESERVED1 DWORD 0006HRESERVED1 DWORD 0006HRESERMAP DATA OFFSET1 DWORD Start from File Beginning Data (Bitmap Data) between Bitmap Data 000Ehbitmap Header Size1 DWord Bit The length of the Bitmap Info HEADER is used to describe the color, compression method, and the like of the bitmap. The following length is represented: 28H - Windows 3.1x, 95, NT, ... 0CH - OS / 2 1.x F0H - OS / 2 2.X Note: In the Windows 95, 98, 2000 and other operating systems, bitmap information head The length is not necessarily 28h because Microsoft has developed a new BMP file format, where the information head structure changes are relatively large, and the length is long. Therefore, it is best not to use the constant 28h directly, but should read this value from a specific file. This ensures compatibility of the program. The width of the 0012HWIDTH1 DWORD bitmap is based on the height of the pixel unit 0016hHHHEight1 DWORD bitmap, with the number of vertices of pixels 001ahplanes1 Word bitmap (Note: This value will always 1) Image Information head 001chbits per pixel1 word Bits 1 - Monochrome bitmap (actually there can be black and white by default) 4 - 16 colors in the default, you can define these two colors) 4 - 16 colors Figure 8 - 256 colors Bitchart 16 - 16bit High Color Bit Figure 24 - 24bit Real Color Bit Figure 32 - 32Bit Enhanced True Color Bitchart 001EHCompression1 DWORD Description: 0 - Do not compress (represented using bi_rgb) 1 - RLE 8 - Use 8-bit RLE compression (Expressed with Bi_RLE8) 2 - RLE 4 - Use a 4-bit RLE compression method (represented by bi_rle4) 3 - bitfields-bit field storage mode (represented by bi_bitfields) 0022HbitMap Data Size1 DWORD The size of bitmap data represented by bytes. This number must be 4 multiples 0026HHRESOLUTION1 DWORD The horizontal resolution of pixels / mi is indicated 002aHVResolution1 DWORD Number of colors used by Pixels / mhem represented 002EHCOLORS1 DWORD bitmaps. If 8-bit / pixels are indicated as 100 h or 256. 0032Himportant Colors1 DWORD specifies the important number of colors. When the value of this domain is equal to the number of colors (or equal to 0), all colors are as important as important palette data depending on the BMP version, different Paletten * 4 BYTE palette specification. For each entry in the palette, these four bytes describe the value of RGB with the following method: 1 byte for blue components
1 byte for green components
1 byte for red components
1 byte is used for fillers (set to 0)
Image data varies depending on the BMP version and the palette size. Different Bitmap Dataxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx depends on the compression method and the size of the image and the bit depth of the image, which contains all bitmap data bytes, which may be The index number of the color palette may also be the actual RGB value, which will determine the bit depth of the image information header. Release of components
Bitmap file header
The bitmap file header contains information about file type, file size, storage location, with BitmapFileHeader structure in a bitmap file in Windows 3.0 or later:
Typedef struct tagbitmapfilehead {/ * bmfh * /
Uint bftype;
DWORD BFSIZE;
Uint bfreserved1;
Uint bfreserved2;
DWORD BFOFFBITS;
} BitmapfileHeader;
among them: ?
BFTYPE
Description file type. (This value must be 0x4d42, which is the character 'bm'. We don't need to judge the bitmap ID of the OS / 2, which seems to have no meaningful meaning, and if you want to support OS / 2 The bitmap, the program will become very cumbersome. So, only this is recommended to check the 'bm' logo)?
BFSIZE
Illustrated the size of the file, using bytes
BFRESERVED1
Reserved, must be set to 0
BFRESERVED2
Reserved, must be set to 0
Bfoffbits
Note Offset from the file head to the byte between the actual image data. This parameter is very useful because the length of the bitmap information head and the palette changes depending on the situation, so you can use this offset value to read the bit data from the file from the file.
2. Bitmap information head
Bitmap information is defined by BitmapInfo structure, which consists of bitmap-information header and color table, with the former defines the BitMapInfoHeader structure, and the latter defines the RGBQUAD structure. The BitmapInfo structure has the following form:
Typedef struct tagbitmapinfo {/ * bmi * /
Bitmapinfoheader bmiheader;
RGBQUAD BMICOLORS [1];
} BitmapInfo;
among them: ?
Bmiheader
Description BitmapInfoHeader structure, which contains information about size and bit format of bitmaps.
Bmicolors
Describe the array of color table RGBQUAD structures, which contain the true RGB value of the indexed image.
The BitMapInfoHeader structure contains the size, compression type, and color format of bitmap files, which are defined as:
Typedef struct tagbitmapinfoheader {/ * bmih * /
DWORD Bisize;
Long biwidth;
Long biheight;
Word biplanes;
Word bibitcount;
DWORD BICOMPRESSION;
DWORD BisizeImage;
Long Bixpelspermeter;
Long biypelspermeter;
DWORD BICLRUSED;
DWORD BICLRIMPORTANT;
} BitmapInfoHeader;
among them: ?
Bisize
Description The number of words required for the BitmapInfoheader structure. Note: This value is not necessarily the size of the BitmapInfoHeader structure, which may also be the value of SIZEOF (Bitmapv4Header) or the value of Sizeof (Bitmapv5Header). This should be determined according to the format version of this bitmap file, but now, in the case of the situation, the vast majority of BMP images are the BitmapInfoHeader structure (may be too new, there is too new :-). BiWidth
Describe the width of the image, in pixels
Biheight
Describe the height of the image, in pixels. Note: This value has another place in addition to the height of the image, that is, indicating that the image is a bitmap, or a forward bitmap. If the value is a positive, the image is reverse, and if the value is a negative number, the image is forwarded. Most BMP files are inverted bitmaps, that is, the height value is a positive number. (Note: When the height value is a negative number (forward image), the image will not be compressed (that is, the BiCompression member will not be BI_RLE8 or BI_RLE4).
BIPLANES
For the target device, the number of positions, which will always be set to 1
Bibitcount
Description Bit number / pixel, its value is 1, 4, 8, 16, 24, or 32
Bicompression
Describe the type of image data compression. Its value can be one of the following:
Bi_RGB: No compression;
BI_RLE8: Each pixel 8 bit RLE compression encoding, compressed format consists of 2 bytes (repeated pixel count and color index);
BI_RLE4: Each pixel 4 bit of RLE compression encoding, compressed format consists of 2 bytes
Bi_bitfields: The bit of each pixel is determined by the specified mask.
BisizeImage
Describe the size of the image, in bytes. When using the BI_RGB format, it can be set to 0
Bixpelspermeter
Description Level resolution, indicate pixels / meter
Biypelspermeter
Describe the vertical resolution, expressed with pixels / meter
Biclrused
Note that the color index number in the color table actually used by the bitmap (if set to 0, the use of all palette items)
BICLRIMPORTANT
Number of color indexes with important impact on image display, if 0, are important.
The BitmapInfoHeader structure is now as follows:
(1) Positioning of color tables
The application can use the information stored in the Bisize member to find the color table in the BitmapInfo structure, as shown below:
Pcolor = ((LPSTR) PbitmapInfo (Word) (pbitmapinfo-> bmiheader.bisize))
(2) BIBITCOUNT
BIBITCOUNT = 1 indicates that the bitmap has up to two colors. By default, it is black and white, you can also define these two colors. There will be two palette items in the image information headset palette, called index 0 and index 1. Each bit in the image data array represents a pixel. If a bit is 0, the RGB value of the index 0 is displayed, and if the bit is 1, the RGB value of the index 1 is used.
BIBITCOUNT = 4 indicates that the bitmap has up to 16 colors. Each pixel is represented by 4 digits, and uses these 4 bits as a set of color tables to find the color of the pixel. For example, if the first byte in the bitmap is 0x1f, it indicates that there are two pixels, the color of the first pixel is found in the second entry of the color table, and the color of the second pixel is Find in the 16th entry of the color table. At this point, there will be 16 RGB items in the drawbacks. Corresponds to index 0 to index 15. BIBITCOUNT = 8 indicates that the bitmap has a maximum of 256 colors. Each pixel is represented by 8 bits, and uses this 8-bit as an entry for the color table to find the color of the pixel. For example, if the first byte in the bitmap is 0x1f, the color of this pixel is found in the colorful table. At this time, by default, 256 RGB items will be included in the palette, corresponding to index 0 to index 255.
BIBITCOUNT = 16 indicates that the bitmap has up to 216 colors. Each of the pigments is represented by 16 bits (2 bytes). This format is called high color, or is called enhanced 16-bit color, or 64k colors. It is more complicated. When the value of the BiCompression member is bi_rgb, it does not have a palette. In the 16-bit, the lowest 5 indicates the blue component, the middle of the middle indicates the green component, and the high 5 represents the red component, which takes up 15 bits, the highest reservation, set to 0. This format is also referred to as a 555 16-bit bitmap. If the value of BiCompression is bi_bitfields, then the situation is complicated, the first is that the original palette is occupied by three DWORD variables, called red, green, and blue mask. It is used to describe the position occupied by red, green and blue components in 16 bits. In Windows 95 (or 98), the system accepts two types of format: 555 and 565, under the 555 format, red, green, and blue mask are: 0x7c00, 0x03e0, 0x001F, and under 565 format They are: 0xF800, 0x07E0, 0x001F, respectively. After reading a pixel, you can use the mask "and" on the pixel value, thereby extracting the desired color components (of course, the appropriate left right movement). In the NT system, there is no format limit, but it is only possible to overlap between the requirements mask. (Note: This format is used in trouble, but because its display effect is close to the true color, the image data is much smaller than the real color image, so it is more used for game software) .
BIBITCOUNT = 24 indicates that the bitmap has up to 224 colors. This bitmap does not have a palette (Bmicolors member size is 0), in the bit array, each byte represents a pixel, respectively corresponds to color R, G, B.
BIBITCOUNT = 32 indicates that the bitmap has up to 232 colors. The structure of this bitmap is very similar to the 16-bit bitmap structure. When the value of BiCompression is Bi_RGB, it does not have a palette. There are 24 bits in 32 bits to store RGB values, the order is: the highest bit - reserved Red 8, green 8, blue 8 bits. This format is also a 888 32 bitmap. If the value of BiCompression is BI_BITFIELDS, the original palette will occupy three DWORD variables, become red, green, blue mask, which is used to describe red, green, and blue components in 32-bit positions. . In Windows 95 (OR 98), the system only accepts 888 format, that is, the value of three masks will only be: 0xff0000, 0xff00, 0xff. In the NT system, you only need to pay attention to no overlapping between the mask. (Note: This image format is compared to the rule because it is DWORD alignment, so the code optimization (simple) can be made when the image processing is performed in the memory. (3) Clrused
Members in the BitmapInfoheader structure specify the number of colors actually used. If the clrused is set to 0, the number of colors used by the bitmap is equal to the number of BIBITCOUNT members. Please note that if the value of Clrused is not a maximum value of the color or 0, it should pay attention to the calculation of the palette size when programming. For example, in the 4-bit bitmap, the default size of the palette should be 16 * Sizeof. (RGBQUAD), however, if the value of the clrused is not 16 or not 0, then the size of the palette should be clrused * sizeof (rgbquad).
(4) Image data compression
1 BI_RLE8: Each pixel is 8-bit RLE compression encoding, which can be compressed using any of an encoding method and an absolute manner, which can be used anywhere in the same figure.
Encoding method: consists of 2 bytes, the first byte specifies the number of pixels used to use the same color, the second byte specifies the color index used. In addition, the first byte in this byte pair can be set to 0, and the value of the second byte is represented:
The value of the second byte is 0: the end of the line.
The value of the second byte is 1: the image ends.
The value of the second byte is 2: The following two bytes indicate the offset of the next pixel from the current horizontal and vertical position.
Absolute mode: The first byte is set to 0, and the second byte is set to a value between 0x03 to 0xFF. In this way, the second byte represents the number of bytes behind this byte, each byte, contains a color index of a single pixel. The compressed data format requires the word boundary alignment. The following example is the 8-bit compressed image data represented by a 16-based:
03 04 05 06 00 03 45 56 67 00 02 78 00 02 05 01 02 78 00 00 09 1e 00 01 These compressed data can be construed as:
Compressed data? Expansion data 03 0404 04 04? 05 0606 06 06 06 06? 00 03 45 56 67 0045 56 67 02 7878 78? 00 02 05 01 From the current position, move downward, 52 7878 78 00 00 00 0000-00-00 0000-00-00 0000-00-00 0000-00-00 0000-00-00 0000-00-00 0000-00-00 0000-00-00 0000-00-00 0000-00-00 0000-00-00 0000-00-00 0000-00-00 0000-00-00 0000-00-00
2 BI_RLE4: Each pixel is 4-bit RLE compression encoding, and it can also be compressed using any of the encoding mode and absolute mode, which can also be used anywhere in the same figure. These two ways are:
Encoding method: consists of 2 bytes, the first byte specifies the number of pixels, the second byte contains two color indexes, one at a high 4 bits, the other at a low 4 digits. The first pixel uses a high 4-bit color index, the second uses a low 4-bit color index, and the third use high 4-bit color index, according to this class. Absolute way: The first byte of this byte pair is set to 0, the second byte contains a color index number, and its subsequent byte contains a color index, the color index is stored in this byte, low 4 Bit, a color index corresponds to a pixel. In addition, Bi_RLE4 is also combined with the value in the second byte:
The value of the second byte is 0: the end of the line.
The value of the second byte is 1: the image ends.
The value of the second byte is 2: The following two bytes indicate the offset of the next pixel from the current horizontal and vertical position.
The following example is the 4-bit compressed image data represented by a 16-based number:
03 04 05 06 00 06 45 56 67 00 04 78 00 02 05 01 04 78 00 00 09 1e 00 01
These compressed data can be construed as:
Compressed Data Expansion Data 03 040 4 005 060 6 0 6 0? 00 06 45 56 67 004 5 5 6 6 7? 04 787 8 7 8? 00 02 05 01 From the current position 5 positions, move downward 04 787 8 7 8? 00 00 00 Line End 09 1E1 E 1 E 1 E 1 E 1? 00 01RLE image end?
3. Colorful table
The elements contained in the color table are the same as the number of colors having the bitmap, and the color of the pixel is defined by the RGBQUAD structure. The color table is not used for 24-bit true color images (also including 16 bits, and 32-bit bitmaps), because the RGB value in the bitmap represents the color of each pixel. The color in the color table is sorted by the importance of color, which can assist the display driver to display color images to display devices that cannot display sufficient multi-color number. The RGBQUAD structure describes the color of the relative intensity of R, G, and is defined as follows:
TypedEf struct tagrgbquad {/ * rgbq * /
BYTE RGBBLUE;
BYTE RGBGREEN;
BYTE RGBRED;
BYTE RGBRESERVED;
} RGBQUAD;
among them: ?
RGBBLUE
Specify blue intensity
RGBGreen
Green intensity
Rgbred
Specify red intensity
RGBRESERVED
Keep, set to 0
4. Bitmap data
It is followed by the image data byte array after the color table. Each scan of the image consists of a continuous byte indicating the image pixel, and the number of bytes of each row depends on the number of images of the image and the image width represented by the pixel. The scan line is stored by the bottom, that is, the first byte in the array represents the pixels in the lower left corner of the bitmap, and the last byte represents pixels in the upper right corner of the bit. (Only for DIB, if it is forward DIB, the scanning is stored in the top, and the origin of the reverse DIB is in the lower left corner of the image, and the origin of the DIB is in the upper left corner of the image. At the same time, each scanned byte number must be 4 integrity, that is, DWORD alignment. If you want to make sure the image's scan line DWORD is aligned, you can use the following code: ((width * bibitcount) 31) >> 5) << 2