Overview of BMP image format doodle was published on 22:05:00
Introduction
BMP (Bitmap-file) is a Windows Image File Format. All image processing software running in Windows supports the BMP image file format. In Windows, Image Rendering is based on BMP. The BMP image file format before Windows 3.0 is related to the display device. Therefore, the BMP image file format is called the device-Dependent Bitmap File Format. BMP image files after Windows 3.0 have nothing to do with the display device. Therefore, this BMP image file format is called device-independent Bitmap (DIB) format (Note: After Windows 3.0, the DDB bitmap still exists in the system. functions such as bitblt () are based on the DDB bitmap, but if you want to save the image to a disk file in BMP format, microsoft strongly recommends that you store images in Dib format) to enable Windows to display stored images on any type of display device. The default file extension of a BMP bitmap file is BMP or BMP (sometimes it uses. DiB or. RLE as the extension ).
6.1.2 file structure
A bitmap file is composed of four parts: Bitmap-file header, bitmap-Information header, and color table) and the byte array that defines the bitmap. It has the following format.
Composition of Bitmap files |
Structure name |
Symbol |
| Bitmap-File Header) |
Bitmapfileheader |
Bmfh |
| Bitmap-Information Header) |
Bitmapinfoheader |
Bmih |
| Color Table) |
Rgbquad |
Acolors [] |
| Image Data array bytes |
Byte |
Abitmapbits [] |
The structure of the bitmap file can be integrated in Table 6-01.
Table 01 structure Summary of Bitmap files
| |
Offset |
Domain Name |
Size |
Content |
| Image files Header |
0000 h |
File ID |
2 bytes |
The two-byte content is used to identify the bitmap type: 'Bm ': Windows 3.1x, 95, NT ,... 'Ba': OS/2 bitmap Array 'Cies': OS/2 color icon 'Cp': OS/2 color pointer 'Ic ': OS/2 icon 'Pt': OS/2 pointer Note: Because OS/2 systems are not widely used, you only need to determine the first "BM" in programming. |
| |
0002 H |
File Size |
1 DWORD |
Size of the entire file in bytes |
| |
0006 H |
Reserved |
1 DWORD |
Reserved, must be set to 0 |
| |
000ah |
Bitmap data offset |
1 DWORD |
Offset between the data (bitmap data) starting from the file to the graph data |
| |
000eh |
Bitmap header size |
1 DWORD |
The length of the bitmap info header, used to describe the color and compression method of the bitmap. The following length: 28 h-Windows 3.1x, 95, NT ,... 0ch-OS/2 1.x F0h-OS/2 2.x Note: in Windows 95, 98, 2000, and other operating systems, the length of the Bitmap header is not necessarily 28 h, because Microsoft has developed a new BMP file format, the structure of the information header changes greatly and the length is extended. Therefore, it is best not to directly use the constant 28 h, but to read the value from a specific file. This ensures program compatibility. |
| |
0012 H |
Width |
1 DWORD |
The width of the bitmap, in pixels. |
| |
0016 H |
Height |
1 DWORD |
The height of the bitmap, in pixels. |
| |
001ah |
Planes |
1 word |
Bitmap bit number (Note: This value will always be 1) |
| Image Information Header |
001ch |
Bits per pixel |
1 word |
Number of digits in each pixel 1-monochrome Bitmap (in fact, there are two colors, black and white by default. You can define these two colors by yourself) 4-16 color bitmap 8-256-color bitmap 16-16 bit high-color bitmap 24-24bit true color bitmap 32-32bit enhanced true color bitmap |
| |
001eh |
Compression |
1 DWORD |
Compression description: 0-no compression (in bi_rgb format) 1-RLE 8-use the 8-bit RLE compression method (represented by bi_rle8) 2-RLE 4-use the 4-bit RLE compression method (represented by bi_rle4) 3-bitfields-bit domain storage method (represented by bi_bitfields) |
| |
0022 H |
Bitmap data size |
1 DWORD |
The size of the bitmap data expressed by the number of cells. The value must be a multiple of 4. |
| |
0026 H |
Hresolution |
1 DWORD |
Horizontal resolution in pixels/meters |
| |
002ah |
Vresolution |
1 DWORD |
Vertical Resolution in pixels/meters |
| |
002eh |
Colors |
1 DWORD |
The number of colors used by the bitmap. For example, 8-bit/pixel represents 256 h or. |
| |
0032 H |
Important colors |
1 DWORD |
Specify the number of important colors. When the value of this field is equal to the number of colors (or equal to 0), it indicates that all colors are equally important. |
| Color Palette data |
Varies with the BMP version. |
Palette |
N * 4 byte |
Color Palette specification. For each table item in the color palette, use the following methods to describe the values of RGB:
|
1 byte for the blue component |
|
1 byte for green component |
|
1 byte for red weight |
|
1 byte for padding (set to 0) |
|
| Image Data |
The size varies depending on the BMP version and palette size. |
Bitmap data |
Xxx bytes |
The size of this field depends on the compression method, image size, and image bit depth. It contains all bitmap data bytes, which may be the index number of the color palette, it may also be the actual RGB value, which is determined based on the bit depth value in the Image Information header. |
Component details
1. Bitmap File Header
The bitmap file header contains information about the file type, file size, and storage location. The bitmapfileheader structure is used in bitmap files of Windows 3.0 and later versions:
Typedef struct tagbitmapfileheader {/* bmfh */
Uint bftype;
DWORD bfsize;
Uint bfreserved1;
Uint bfreserved2;
DWORD bfoffbits;
} Bitmapfileheader;
Where:
Bftype
|
Description of the file type. (The value must be 0x4d42, that is, the character 'bm '. We don't need to judge the bitmap ID of OS/2. It seems that it makes no sense to do so now, and if we want to support the bitmap of OS/2, the program will become very cumbersome. Therefore, we recommend that you check the 'bm 'Id)
|
Bfsize
|
Indicates the file size, in bytes.
|
Bfreserved1
|
Reserved, must be set to 0
|
Bfreserved2
|
Reserved, must be set to 0
|
Bfoffbits
|
The offset of bytes from the file header to the actual image data. This parameter is very useful because the length of the Bitmap header and palette varies according to different situations, so you can use this offset value to quickly read in-place data from the file.
|
2. Bitmap header
Bitmap information is defined by the bitmapinfo structure. It consists of Bitmap-Information header and color table. The former is defined by the bitmapinfoheader structure, the latter is defined using the rgbquad structure. The bitmapinfo structure has the following forms:
Typedef struct tagbitmapinfo {/* BMI */
Bitmapinfoheader bmiheader;
Rgbquad bmicolors [1];
} Bitmapinfo;
Where:
Bmiheader
|
Description: bitmapinfoheader structure, which contains information about the size and bit format of Bitmap.
|
Bmicolors
|
An array of the rgbquad structure of the color table, which contains the true RGB values of the indexed image.
|
The bitmapinfoheader structure contains the size, compression type, and color format of the bitmap file. Its structure is defined:
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;
Where:
Bisize
|
Description of the number of words required for the bitmapinfoheader structure. Note: This value is not necessarily the size of the bitmapinfoheader structure. It may also be the value of sizeof (bitmapv4header) or the value of sizeof (bitmapv5header. This depends on the format version of the bitmap file. However, in the current situation, the vast majority of BMP images are in the bitmapinfoheader structure (probably because the latter two are too new :-).
|
Biwidth
|
Specifies the image width, in pixels.
|
Biheight
|
The height of the image, in pixels. Note: In addition to describing the height of an image, this value also has another use, that is, to indicate whether the image is a backward bitmap or a forward bitmap. If the value is a positive number, the image is backward. If the value is a negative number, the image is positive. Most BMP files are backward bitmaps. That is, the height value is a positive number. (Note: when the height value is a negative value (positive image), the image cannot be compressed (that is, the bicompression member cannot be bi_rle8 or bi_rle4 ).
|
Biplanes
|
Indicates the number of bit faces of the target device. The value is always set to 1.
|
Bibitcount
|
Indicates the number of bits/pixels. The value is 1, 4, 8, 16, 24, or 32.
|
Bicompression
|
Describes the types of image data compression. The value can be one of the following values:
|
Bi_rgb: no compression; |
|
Bi_rle8: 8-bit RLE compression for each pixel. The compression format consists of 2 bytes (repeated pixel count and color index ); |
|
Bi_rle4: 4-bit RLE compression for each pixel. The compression format consists of 2 bytes. |
|
Bi_bitfields: the bit of each pixel is determined by the specified mask. |
|
Bisizeimage
|
The size of the image, in bytes. When bi_rgb format is used, it can be set to 0. |
Bixpelspermeter
|
Horizontal resolution, expressed in pixels/meters |
Biypelspermeter
|
Indicates the vertical resolution, expressed in pixels/meters. |
Biclrused
|
Indicates the number of color indexes in the color table used by the bitmap (if it is set to 0, all color palette items are used) |
Biclrimportant
|
This indicates the number of color indexes that have an important impact on image display. If it is 0, it indicates that it is important.
|
The bitmapinfoheader structure is described 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 a bitmap can have up to two colors. By default, it is black and white. You can also define these two colors by yourself. There will be two palette items in the Image Information header, called index 0 and Index 1. Each bit in the image data array represents a pixel. If one digit is 0, the RGB value of index 0 is used for display. If the digit is 1, the RGB value of Index 1 is used.
Bibitcount = 4 indicates a maximum of 16 bitmap colors. Each pixel is represented by four digits, and the four digits are used as the table items of the color table 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, and the color of the first pixel is located in the 2nd table of the color table, the color of the second pixel is found in the 16th items in the color table. In this case, the color palette contains 16 RGB items by default. Corresponding to index 0 to index 15.
Bibitcount = 8 indicates a maximum of 256 bitmap colors. Each pixel is represented by eight bits and used as the table item of 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 will be searched in the 32nd table of the color table. By default, the color palette contains 256 RGB entries, which correspond to index 0 to index 255.
Bibitcount = 16 indicates that the bitmap can have a maximum of 216 colors. Each pigment is represented by 16 bits (2 bytes. This format is called high color, enhanced 16-bit color, or 64 K color. It is complex. When the value of the bicompression member is bi_rgb, it does not have a palette. In 16 bits, the lowest five bits represent the blue component, the middle five bits represent the green component, and the highest five bits represent the red component. A total of 15 bits are occupied, and the highest bits are retained, set to 0. This format is also called a 555 16-Bit Bitmap. If the value of the bicompression member is bi_bitfields, the situation is complicated. First, the position of the original color palette is occupied by three DWORD variables, which are called the red, green, and blue masks. It is used to describe the position of the red, green, and blue components in 16 bits. In Windows 95 (or 98), the system can accept two formats of bit domains: 555 and 565. In the 555 format, the red, green, and blue masks are: 0x7c00, 0x03e0, and 0x001f. In the 565 format, they are 0xf800, 0x07e0, and 0x001f respectively. After reading a pixel, you can use the mask "and" on the pixel value to extract the desired color weight (of course, after proper left and right shifting ). In the NT system, there is no format restriction, but the mask must not overlap. (Note: It is troublesome to use images in this format. However, because the display effect is similar to true color, and the image data is much smaller than the real color image, it is more used for game software ).
Bibitcount = 24 indicates that the bitmap can have a maximum of 224 colors. This bitmap has no color palette (the size of the bmicolors member is 0). In the in-place array, each 3 bytes represents a pixel, which corresponds to the color R, G, and B respectively.
Bibitcount = 32 indicates that the bitmap can have a maximum of 232 colors. The structure of this bitmap is very similar to that of a 16-Bit Bitmap. When the value of bicompression is bi_rgb, there is no color palette. The 32-Bit Bitmap contains 24 bits for storing RGB values. The order is: highest bit-retained, red 8 bits, green 8 bits, and Blue 8 bits. This format is also used as a 888 32 bitmap. If the value of the bicompression member is bi_bitfields, the original position of the color palette will be occupied by three DWORD variables and become a red, green, and blue mask, it is used to describe the position of the red, green, and blue components in 32 bits. In Windows 95 (or 98), the system only accepts the 888 format. That is to say, the values of the three masks can only be 0xff0000, 0xff00, or 0xff. In the NT System, you only need to make sure that there is no overlap between masks. (Note: This image format is relatively regular because it is aligned with DWORD, so assembly-level code optimization (simple) can be performed during image processing in the memory )).
(3) clrused
Clrused, a member in the bitmapinfoheader structure, specifies the actual number of colors used. If clrused is set to 0, the number of colors used by the bitmap is equal to the number of bibitcount members. Note that if the value of clrused is not the maximum value of available colors or is not 0, you should pay attention to the calculation of the color palette size during programming. For example, in a 4-bit diagram, the default size of the color palette should be 16 * sizeof (rgbquad). However, if the value of clrused is not 16 or 0, the size of the color palette should be clrused * sizeof (rgbquad ).
(4) Image Data Compression
① Bi_rle8: each pixel is an 8-bit RLE compression code, which can be compressed using either the encoding method or the absolute method, these two methods can be used anywhere in the same image.
Encoding method: consists of two bytes. The first byte specifies the number of pixels in the same color, and the second byte specifies the color index. In addition, the first byte in this Byte pair can be set to 0, and the value of the second byte is used together:
|
The value of the second byte is 0: the end of the row. |
|
The value of the second byte is 1: The end of the image. |
|
The second byte value is 2: The second 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 0x03 ~ A value between 0xff. In this way, the second byte represents the number of bytes following this byte, and each byte contains a color index of a single pixel. The format of the compressed data requires word boundary alignment. The following example shows the 8-bit compressed image data in hexadecimal notation:
03 04 05 06 00 03 45 56 67 00 02 78 00 02 05 01 02 78 00 00 09 1E 00 01
The compressed data can be interpreted:
Compressed Data |
Extended Data |
| 03 04 |
04 04 |
| 05 06 |
06 06 06 06 06 |
| 00 03 45 56 67 00 |
45 56 67 |
| 02 78 |
78 |
| 00 02 05 01 |
Move from the current position to the right, move back to the next row |
| 02 78 |
78 |
| 00 00 |
Row end |
| 09 1e |
1e 1E 1e 1E 1e 1E 1e |
| 00 01 |
Rle-encoded image ends |
② Bi_rle4: each pixel is a 4-bit RLE compression code. You can also use either the encoding method or the absolute method for compression, the two methods can also be used anywhere in the same image. The two methods are as follows:
Encoding method: consists of two bytes. The first byte specifies the number of pixels, and the second byte contains two color indexes. One is in the upper four bits, and the other is in the lower four bits. The first pixel uses a four-digit high color index, the second uses a four-digit low color index, and the second uses a four-digit high color index.
Absolute mode: the first byte in this Byte pair is set to 0, the second byte contains the number of color indexes, and the subsequent byte contains the color index, the color index is stored in the high and low bits of this byte. A color index corresponds to a pixel. In addition, bi_rle4 also uses the value in the second byte to represent:
|
The value of the second byte is 0: the end of the row. |
|
The value of the second byte is 1: The end of the image. |
|
The second byte value is 2: The second two bytes indicate the offset of the next pixel from the current horizontal and vertical position. |
The following example shows 4-bit compressed image data in hexadecimal notation:
03 04 05 06 00 06 45 56 67 00 04 78 00 02 05 01 04 78 00 00 09 1E 00 01
The compressed data can be interpreted:
Compressed Data |
Extended Data |
| 03 04 |
0 4 0 |
| 05 06 |
0 6 0 6 0 |
| 00 06 45 56 67 00 |
4 5 5 6 6 7 |
| 04 78 |
7 8 7 8 |
| 00 02 05 01 |
Move from the current position to the right, move back to the next row |
| 04 78 |
7 8 7 8 |
| 00 00 |
Row end |
| 09 1e |
1 E 1 E 1 E 1 E 1 |
| 00 01 |
RLE image end |
3. Color Table
The color table contains the same number of elements as the bitmap color. The pixel color is defined in the rgbquad structure. For 24-bit true color images, no color table is used (also including 16-bit, and 32-bit bitmaps ), because the RGB value in the bitmap represents the difficulty of every pixel. 2. The weakness of the anti-mite equipment. zhangnan allows the old Pu to catch the static hacker gbquad structure description by R, G, B relative strength of the color, definition:
Typedef struct tagrgbquad {/* rgbq */
Byte rgbblue;
Byte rgbgreen;
Byte rgbred;
Byte rgbreserved;
} Rgbquad;
Where:
Rgbblue
|
Specify the blue intensity
|
Rgbgreen
|
Specify the green intensity
|
Rgbred
|
Specify the red intensity
|
Rgbreserved
|
Reserved, set to 0
|
4. bitmap data
Next to the color table is the byte array of image data. Each scanned line of an image is composed of consecutive bytes that indicate the pixels of the image. The number of bytes of each row depends on the number of colors of the image and the width of the image represented by pixels. Scan rows are stored from the bottom to the top, that is, the first byte in the array represents the pixels in the lower left corner of the bitmap, And the last byte represents the pixels in the upper right corner of the bitmap. (Only for DiB and backward dib. If DiB is positive, the scanned rows are stored from top to bottom.) The source of backward DiB is in the lower left corner of the image, the source of the forward DiB is in the upper left corner of the image. At the same time, the number of bytes for each scan row must be an integral multiple of 4, that is, the DWORD is aligned. If you want to ensure the DWORD alignment of the scanned image, use the following code:
(Width * bibitcount) + 31)> 5) <2
5. Bibliography
Image File Format (top and bottom)-Windows Programming
Image File Format Daquan
Programming windows by Charles Petzold
6. Related Sites
Various formats: http://www.wotsit.org/
Various formats: http://www.csdn.net/
Bitmap format: http://www.cica.indiana.edu/graphics/image_specs/bmp.format.txt
<End> 〉