A bmp file starts with a bitmapfileheader struct. The 1st attribute of bitmapfileheader is bftype (2 bytes), which is always equal to & h4d42. Because the data in the memory is arranged on the left and right, the data in the memory is displayed as (42 4d) from the left to the right ), therefore, in ultraedit, the first two bytes are displayed as (42 4d), which is the feature of future data and will not be repeated. The 2nd attribute of bitmapfileheader is bfsize (4 bytes), indicating the size of the entire BMP file, which is equal to & h000004f8 = 1272 bytes. The 3rd and 4th attributes of bitmapfileheader are bfreserved1 and bfreserved2 (two bytes each). Here, the two reserved attributes are all 0, which is equal to & h0000 and & h0000. The 5th attribute of bitmapfileheader is bfoffbits (4 bytes), which indicates the position offset of the dib data zone in the BMP file. Here it is equal to & h00000076 = 118, indicates that the data zone starts from 118 bytes after the start of the file. The bitmapfileheader struct is over here. We will find that bitmapfileheader only occupies the 14-byte length starting from the BMP file. However, we need to note that a bitmapfileheader struct variable is defined in VB, the length of the attribute occupies 16 bytes, because the 1st attributes should have been allocated only 2 bytes, but actually 4 bytes are allocated and 2 more bytes are allowed, therefore, if you want to save a BMP image, pay attention to this when writing the bitmapfileheader struct. Next is the bitmapinfo struct. The bitmapinfo segment consists of the bitmapinfoheader structure and rgbquad structure. The rgbquad struct indicates the color information of the image, which can be omitted in some cases. Generally, the 24-bit and 32-bit images do not contain the rgbquad struct, because the RGB values shown in the dib data area are directly displayed, generally, four and eight images have the rgbquad structure. (The number of digits indicates the color information. For example, a four-digit image uses four bits to indicate the color information .) Whether the rgbquad struct exists in a BMP file can be determined based on bfoffbits, the first 5th attribute of the bitmapfileheader struct, because the length of the bitmapinfoheader struct is 40 bit, if the bitmapinfoheader struct is not offset to the dib data zone after it ends, the following data is the rgbquad struct. Here C:/Windows/blue sans 16.bmp is a 4bit image, so it carries the rgbquad struct. Next, go to the bitmapinfoheader section. The 1st attributes of bitmapinfoheader are bisize (4 bytes), which indicates the length of the bitmapinfoheader struct. The most common length is 40 bytes, in ultraedit, we can see that the next four bytes are equal to & hsf-0028 = 40 bytes. The 2nd attribute of bitmapinfoheader is biwidth (4 bytes), indicating the width of the BMP image, which is equal to & h00000030 = 48 pixels. The 3rd attribute of bitmapinfoheader is biheight (4 bytes), which indicates the height of the BMP image, which is equal to & h00000030 = 48 pixels. The 4th attributes of bitmapinfoheader are biplanes (2 bytes), indicating that the plane of the BMP image belongs. Obviously, the display has only one plane, so it is always equal to 1, and here it is equal to & h0001. The 5th attribute of bitmapinfoheader is bibitcount (2 bytes), which indicates the number of bits in the color of the BMP image, that is, 24 bitmap, 32 bitmap, and so on. This parameter is equal to & h0004, indicating that the image is a 4-Bit Bitmap. The first attribute of bitmapinfoheader is bicompression (4 bytes), which indicates the compression attribute of the image. The BMP image is not compressed and is equal to 0. Therefore, the value is & h00000000. The 7th attribute of bitmapinfoheader is bisizeimage (4 bytes), which indicates the size of the BMP image data zone. When a user familiar with bicompression is equal to 0, the value here can be omitted, so here is equal to & h00000000. The 8th attribute of bitmapinfoheader is bixpelspermeter (4 bytes), which indicates the number of pixels per meter on the X axis of the image. This parameter is equal to & h00000ec3 = 3779 pixels/meter. The 9th attribute of bitmapinfoheader is biypelspermeter (4 bytes), which indicates the number of pixels per meter on the Y axis of the image. This parameter is equal to & h00000ec3 = 3779 pixels/meter. The 10th attribute of bitmapinfoheader is biclrused (4 bytes), which indicates how many color index tables are used. Generally, the bibitcount attribute is used only when it is smaller than 16, if it is equal to 0, there are 2 ^ itcount color index tables, so here it is still equal to & h00000000. The 11th attribute of bitmapinfoheader is biclrimportant (4 bytes), which indicates the number of important colors. If it is equal to 0, it indicates that all colors are important, so here it is equal to & hsf-0000. The bitmapinfoheader struct ends. Since this image has not yet reached the offset of the dib data zone, the following part is the rgbquad struct. The rgbquad struct is composed of 4 bytes of data. Therefore, an rgbquad struct occupies only 4 bytes of space, represented in sequence from left to right (blue, green, red, not used ). For example, I have counted a total of 16 rgbquad components. Since the image is a 4-bitmap, 2 ^ 4 is exactly equal to 16, therefore, all 16 colors are enumerated. These colors are a color index table. Color index table number starts from 0, a total of 16 colors, so the number is 0-15. From ultraedit, we can see that the 16 rgbquad struct types are: Numbers :( blue, green, red, null) 0: (,) 1) 2: (,) 3: (,) 4: (,) 5: (,) 6: (80, 80,) 7: (80, 80, 80, 80, 00) 8: (C0, C0, C0, 00) 9: (, 00, FF, 00) 10: (00, FF,) 11: (00, FF, FF, 00) 12 :( ff, 00,00, 00) 13 :( ff, 00, FF, 00) 14 :( ff, FF, 00,00) 15 :( ff, FF, 00) for more intuitive representation of these colors, see the following picture. Here, the offset of the dib data zone is met, so the following bytes are the image content. Note that all the dib data scan rows are upside down and upside down. That is to say, an image first draws the bottom pixel and then the top pixel, therefore, the pixels represented by Dib data are represented from the lower left corner of the image to the upper right corner of the image. Because the image here is a 4-bit image, that is, 4-bit indicates a pixel, and one byte has 8 bits, a word can be saved to 2 pixels. From ultraedit, we can see that the first byte in the Dib data zone is & h44, And the hexadecimal number is exactly written in every 4 groups, the image is exactly the same as the 4-bit image. Therefore, & h44 indicates two pixels. 4 indicates the first pixel in the high position, and 4 indicates the second pixel in the low position. Here, 4 does not represent the RGB color, but the color index number is 4. Because the quotation marks start from 0, 4 indicates the 5th colors in the index table, from the figure, we can see that the index number 4 is blue. This is the first byte, indicating 2 pixels in the lower left corner of the image. If Photoshop opens the image, the color RGB values obtained from two pixels in the lower left corner are exactly the same as the RGB values of the 5th colors in the index table. The Dib data is similar. At this point, a BMP image is completely parsed. Based on this information, you can draw a BMP image. |