BMP is an image file format unrelated to hardware devices and is widely used. It uses a bit ing storage format. Apart from image depth options, it does not use any compression. Therefore, BMP files occupy a large amount of space. The image depth of BMP files can be lbit, 4bit, 8bit, and 24bit. When a BMP file stores data, the image is scanned in the order of left to right and bottom to top.
Because the BMP file format is a standard for exchanging graph-related data in windows, the graphic image software running in Windows supports the BMP image format.
A typical BMP image file consists of three parts: the data structure of the bitmap file header, which contains information such as the type and display content of the BMP image file; the bitmap information data structure, it contains BMP image width, height, compression method, and definition color information.
Data examples:
For example, the start of a BMP file:
Mongod 4690 0000 0000 0000 4600 0000 2800 0000 8000 0000 9000*0000 0100 1000 0300 0000 0090 a00f 0000 a00f 0000 0000 0000 0000 0000*00f8 0000 e007 0000 1f00 0000 0000 0000*02f1 84f1 04f1 84f1 84f1 06f2 84f1 06f2 04f2 86f2 06f2 86f2 86f2 ........
BMP files can be divided into four parts: bitmap file header, bitmap information header, color palette, and image data array, which are separated.
I. Image File Header
1) 1: (the number here represents the "Word", that is, two bytes, the same below) image file header. Required DH = 'bm ', which indicates the BMP format supported by windows.
2) 2-3: The entire file size. 4690 0000, 00009046 H = 36934.
3) 4-5: reserved, must be set to 0.
4) 6-7: The offset between the graph data from the file. 4600 0000, 00000046 H = 70, the above file header is 35 characters = 70 bytes.
5) 8-9: the length of the bitmap information header.
6) 10-11: The bitmap width, in pixels. 8000 0000, 00000080 H = 128.
7) 12-13: the height of the bitmap, in pixels. 9000 0000, 00000090 H = 144.
8) 14: bitmap number. The value is always 1. 0100, for 0001 H = 1.
Ii. Bitmap information Header
9) 15: The number of digits in each pixel. Available in 1 (monochrome), 4 (16 color), 8 (256 color), 16 (64 K color, high color), 24 (16 M color, true color), 32 (40 96 m color, enhanced real color ). 1000 is 0010 H = 16.
10) 16-17: compression Description: 0 (not compressed), 1 (RLE 8, 8-bit RLE compression), 2 (RLE 4, 4-bit RLE compression, 3 (bitfields, bit domain storage ). Rle is simply compressed using the pixel number + pixel value. T408 uses a bit field storage mode, which uses two bytes to represent a pixel and the bit field is allocated as r5b6g5. In the figure, 0300 0000 is 00000003 H = 3.
11) 18-19: the size of the bitmap data expressed by the number of cells. The value must be a multiple of 4, which is equal to the bitmap width × bitmap height × number of digits per pixel. 0090 0000 is 00009000 H = 80x90x2 h = 36864.
12) 20-21: horizontal resolution in pixels/meters. A00f 0000 is 0000 0fa0h = 4000.
13) 22-23: vertical resolution in pixels/meters. A00f 0000 is 0000 0fa0h = 4000.
14) 24-25: Number of color indexes used by bitmap. If it is set to 0, all color palette items are used.
15) 26-27: Number of color indexes that have an important impact on image display. If it is 0, it means all important.
3. color palette
16) 28-35: color palette specification. For each table item in the color palette, use the following method to describe the RGB value:
1 byte for the blue component
1 byte for green component
1 byte for red weight
1 byte for padding (set to 0)
Color palette is not used for 24-bit true color images, because the RGB value in the bitmap represents the color of each pixel.
For example, the color palette is 00f8 0000 e007 0000 1f00 0000 0000 0000, of which:
00fb 0000 is fb00h = 1111100000000000 (Binary) and is the mask of the red component.
E007 0000 is 07e0h = 0000011111100000 (Binary) and is the mask of the green component.
1f00 0000 is 0000000000011111 (Binary), which is the mask of the red component.
0000 0000 is always set to 0.
Perform the "and" Operation on the mask and pixel value and then shift the value to obtain the component values of different colors. Looking at the mask, we can see that, in fact, in the two bytes of each pixel value, 16 bits are 5, 6, and 5 BITs respectively R, G, and B. After the component value is taken out, multiply the R, G, and B values by 8, 4, and 8, respectively, the first component can be filled into a byte, and then the three bytes are combined by RGB, put it into the memory (also in reverse order), you can convert it to the 24-bit standard BMP format.
Iv. Image Data Array
17) 17-...: each two bytes represents a pixel. The first byte in the array represents the pixel in the lower left corner of the bitmap, And the last byte represents the pixel in the upper right corner of the bitmap.