Detailed description of BMP files

Source: Internet
Author: User
Tags bmp image ultraedit

BMP is an image file format unrelated to hardware devices and is widely used. It uses a bit ing storage format. In addition to image depth options, it does not use any compression. Therefore, bblp 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: 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 (optional)
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.

 

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.