[From network] The format of the BMP file storage structure can be defined in wingdi. h file in windows. A bmp file consists of four parts: the bitmap file header, bitmap information header, palette, and image data, as shown in table 5-1. Table 5-1 Composition of the BMP file
Bitmap-File Header) |
Bitmap-Information Header) |
Color Table/color palette) |
Bitmap data) |
The following describes the details of each component. 1. Bitmap header) The bitmap-file header contains the image type, image size, image data storage address, and two reserved unused fields. Open the wingdi. h file and search for "bitmapfileheader" to locate the data structure definition of the BMP file's bitmap file header.
Typedef struct tagbitmapfileheader { Word bftype; DWORD bfsize; Word bfreserved1; Word bfreserved2; DWORD bfoffbits; } Bitmapfileheader, far * lpbitmapfileheader, * pbitmapfileheader; |
Table 5-2 lists the meanings of fields in tagbitmapfileheader. Table 5-2 tagbitmapfileheader Structure
Segment name |
Size (unit: bytes) |
Description |
Bftype |
2 |
Bitmap category, based on different operations Different systems, in Windows , The value of this field is always 'bm' |
Bfsize |
4 |
BMP Image File Size |
Bfreserved1 |
2 |
Total 0 |
Bfreserved2 |
2 |
Total 0 |
Bfoffbits |
4 |
Address of BMP Image Data |
2. Bitmap header) The bitmap-Information header contains the size of the bitmap information header, the image width and height, the image color depth, and compression, which indicate the size of the image data and other parameters. Open the wingdi. h file and search for "tagbitmapinfoheader" to locate the data structure definition of the BMP file's bitmap information header.
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, far * lpbitmapinfoheader, * pbitmapinfoheader; |
Table 5-3 lists the meanings of fields in tagbitmapfileheader. Table 5-3 tagbitmapfileheader Structure
Segment name |
Size (Unit: Bytes) |
Description |
Bisize |
4 |
The size of this structure varies with operating systems. In Windows, the total value of this field is 28 h bytes = 40 bytes. |
Biwidth |
4 |
BMP image width, in pixels |
Biheight |
4 |
Total 0 |
Biplanes |
2 |
Total 0 |
Bibitcount |
2 |
The color depth of a BMP image, that is, how many bits are used to represent a pixel. common values include 1, 4, 8, 16, 24, and 32, corresponding to monochrome, 16 colors, 256 colors, 16-bit high color, 24-bit real color, and 32-bit enhanced real color |
Bicompression |
4 |
Compression mode. 0 indicates no compression, 1 indicates rle8 compression, 2 indicates rle4 compression, and 3 indicates that each pixel value is determined by the specified mask. |
Bisizeimage |
4 |
BMP image data size, which must be a multiple of 4. When the image data size is not a multiple of 4, fill it with 0. |
Bixpelspermeter |
4 |
Horizontal resolution, in pixels/m |
Biypelspermeter |
4 |
Vertical Resolution, in pixels/m |
Biclrused |
4 |
The color used by the BMP image. 0 indicates that all colors are used. For a 256-color bitmap, the value is 100 h = 256. |
Biclrimportant |
4 |
Number of important colors. If this value is 0, all colors are important. For BMP images using the color palette, this value is used to assist the driver when the video card cannot display all colors.ProgramDisplay color |
|
3. Color Table/color table) Color Table/color table is unique to monochrome, 16-color, and 256-color image files. The corresponding color palette sizes are 2, 16, and 256. The color palette is in 4 bytes, each 4 bytes stores a color value. The image data points to the palette index. You can think of the color palette as an array. The size of each array element is 4 bytes. Assume that the palette data of a 256-color BMP image is:
Color Palette [0] = Black, color palette [1] = white, color palette [2] = red, color palette [3] = blue... Color Palette [255] = yellow |
Image Data 01 00 02 FF indicates that the image color is displayed by calling the data in the palette [1], palette [0], palette [2], and palette [255. In early computers, the video card is relatively backward and may not necessarily display all colors. Therefore, the color data in the color palette should be arranged in order as far as possible, the biclrimportant field in the Bitmap header specifies the number of colors that are important. Each color palette is 4 bytes in size and stores a color value in blue, green, and red. Open the wingdi. h file and search for "tagrgbtriple" to locate the data structure definition of the BMP file palette.
Typedef struct tagrgbquad { Byte rgbblue; Byte rgbgreen; Byte rgbred; Byte rgbreserved; } Rgbquad; |
Table 5-4 lists the meanings of each field in tagrgbtriple. Table 5-4 tagrgbtriple Structure
Segment name |
Size (unit: bytes) |
Description |
Rgbblue |
1 |
Blue Value |
Rgbgreen |
1 |
Green Value |
Rgbred |
1 |
Red Value |
Rgbreserved |
1 |
Reserved, total 0 |
4. Bitmap data) If the image is monochrome, 16, and 256 colors, the bitmap data follows the color palette, And the bitmap data points to the index number of the color palette. If the bitmap is a 16-bit, 24-bit, and 32-bit color, the color palette is not retained in the image file, that is, the color of the image is directly given in the bitmap data. A 16-bit image uses two bytes to store color values. There are two common formats: 5-bit red, 5-Bit green, 5-bit blue, and 5-bit red, 6-Bit green, and 5-bit blue, that is, the 555 format and the 565 format. The 555 format only uses 15 bits, and the last bits are retained, set to 0. The 24-bit image uses three bytes to save the color value. Each byte represents a color, which is arranged in red, green, and blue. The 32-bit image uses 4 bytes to save the color value. Each byte represents a color, in addition to the original red, green, blue, and Alpha channels, that is, the transparent color. If the image has a color palette, you can select compression or not compression as needed. If you select compression, the BMP image is 16 or 256 colors, rle4 or rle8 compression algorithm is used for compression. Rle4 compresses 16-color image data, while rle4 compresses data in the form shown in Table 5-5. Table 5-5 rle4 Compression Method
Solutions |
1 byte |
2 bytes |
3 bytes |
4 bytes |
N Bytes |
A |
Repeated times |
Color index |
|
|
|
B |
Set to 0 |
Valid Color index count |
Color index |
Color index |
Color index... |
Assume that the following 16-color bitmap data is 20 bytes in total, and the data is compressed using rle4:
05 00 04 05 00 08 09 05 04 00 04 05 08 09 04 08 07 01 00 00 |
When extracting the data, read 05 first. Because 05 is not equal to 0, select Solution A. According to solution a, 05 indicates the number of Data repeat times, and then read, 00 indicates there are two color indexes, each index occupies four places. The first pixel is in the upper four places, and the second pixel is in the lower four places. That is, the middle and low pixels in a single byte are in the upper position and the high pixels are in the lower position. After 05 00 decompression, It is equal to 00 00 0. Read 04, select solution a, parse according to the above operation, 04 is the number of Data repeat after, 05 is two color indexes, 3rd color indexes are 5, the index of the 4th colors is 0. 04 05. After decompression, It is equal to 05 05. Read 00, select solution B, and read, which indicates the number of valid color indexes. 00 08 decompress the package and the package is equal to 09 05 04 00. Read 04, select solution a, parse according to the above operation, 04 is the number of Data repeat after, 05 is two color indexes. 04 05. After decompression, It is equal to 05 05. Read 08, select solution a, parse according to the above operation, 08 is the number of Data repeat after, 09 is two color indexes. After decompression, It is equal to 09 09 09. Read 04, select Solution A, and parse it according to the above operation. 04 indicates the number of Data repeat times and 08 indicates two color indexes. 04 08. After decompression, It is equal to 08 08. Read 07, select solution a, parse according to the above operation, 07 is the number of Data repeat after, 01 is two color indexes. 07 01 decompress the package to 01 01 01 0. Read 00, select B solution, read, 00 indicates the number of valid color indexes, 0 indicates no, that is, extract a row of data. Based on the above operations, the extracted data is:
00 00 00 50 90 50 40 00 50 90 90 90 80 80 10 10 |
It seems that the data size is the same as the original data size and does not reflect the compression effect. This is because the above example only selects 20 bytes of data, and there are not many duplicate data in the 20 bytes of data, when RLE is used to compress data with a small amount of duplicate data, the size may be larger than the original data. In fact, when the data is large and repetitive, the RLE compression effect is still ideal. For the rle8 compression method, refer to the preceding rle4 decompression method. The only difference is that rle8 uses one byte to store color indexes, while rle4 uses four-bit to store color indexes. Combined with the analysis of BMP files, the following analyzes the hexadecimal Analysis of BMP images of 256 and 24 colors, and analyzes the file structure in the hexadecimal editor, able to increase the experience of analyzing files. 5-1 and 5-2 are shown in the cat2.bmp and 24-bit BMP images cat1.bmp respectively. The resolution of the cat2.bmp image is 200 × 153, and the file size is 31 680 bytes. The resolution of the cat1.bmp image is 200 × 150, and the file size is 90 056 bytes.
|
Figure 5-1 cat2.bmp Image |
|
Figure 5-2 cat1.bmp Image |
Go to the graph file to analyze cat2.bmp, and open cat2.bmp in winhex, as shown in Figure 5-3.
|
(Click to view the larger image) Figure 5-3 open the cat2.bmp image file in winhex. |
First, analyze the structure of the bitmap file header, as shown in Figure 5-4. The meanings of fields in the Bitmap header of the cat2.bmp image are analyzed based on the header structure of the BMP file, as shown in table 5-6.
|
(Click to view the larger image) Figure 5-4 cat2.bmp: the bitmap file header of the image file |
Table 5-6 Definitions of fields in the bitmap file header in cat2.bmp
Hexadecimal value |
Description |
42 4d: |
ASCII value of BM, the BMP File Identifier in Windows |
C0 7b 00 00 |
7b c0h = 31680, which is the size of the cat2 File |
00 00 00 00 |
Reserved value, total 0 |
36 04 00 |
436 H = 1078, is the address of the image data, that is, the file header + Information header + the length of the palette |
|