Storage Structure of the .bmp Image

Source: Internet
Author: User
Tags bmp image ultraedit

Take C:/Windows/blue # 16.bmp as an example to briefly describe the structure of the BMP image. This is a typical 4-digit, 16-color BMP image. Each computer should have this image. Before instructions, install the ultraedit software to view the hexadecimal value of the BMP image.

A bmp image consists of three parts: bitmapfileheader structure, bitmapinfo structure, and DiB data area. DiB indicates device-independent Bitmap (device-independent Bitmap ). The definitions of the two struct types are as follows: (Note that the integer type occupies 2 bytes and the long type occupies 4 bytes)
Private type bitmapfileheader // 14 bytes
Bftype as integer
Bfsize as long
Bfreserved1 as integer
Bfreserved2 as integer
Bfoffbits as long
End type
Private type bitmapinfo
Bmiheader as bitmapinfoheader
Bmicolors as rgbquad
End type
The bitmapinfo struct contains two child structs, which are defined as follows:
Private type bitmapinfoheader '40 bytes
Bisize as long
Biwidth as long
Biheight as long
Biplanes as integer
Bibitcount as integer
Bicompression as long
Bisizeimage as long
Bixpelspermeter as long
Biypelspermeter as long
Biclrused as long
Biclrimportant as long
End type
Private type rgbquad
Rgbblue as byte
Rgbgreen as byte
Rgbred as byte
Rgbreserved as byte
End type

Use ultraedit to open C:/Windows/Blue Route 16.bmp and view its hexadecimal data.
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 struct and rgbquad struct. The rgbquad struct indicates the color information of the image, which can be omitted in some cases. Generally, the 24-bit image and 32-bit image do not contain the rgbquad struct, because the RGB value directly expressed in the dib data area, 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 Partition 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 struct, because the image is 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 values are as follows:
No.: (blue, green, red, empty)
0)
1)
2)
3)
4th)
No. 5)
6: (80, 80)
7: (80, 80, 80, 00)
(C0, C0, C0, 00)
9: (, FF, 00)
10: (00, FF)
11: (00, FF, FF, 00)
12: (FF, 00)
13: (FF, 00, FF, 00)
14: (FF, FF)
15th: (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 of binary numbers, which is exactly the same as the 4bit image, so & h44 indicates two pixels, 4 indicates the first pixel, and 4 indicates the second pixel. Here, 4 does not represent the RGB color, but the color index number is 4. Because the index number starts 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.

 

 

Therefore, it is very convenient to display a BMP bitmap, especially for 24-bit and 32-bit images. The following Dib data area is based on (B, G, R) or (B, G, R, a) ordered, as long as the dib data zone is located according to the dib offset and stored in a buffer, the bitmapinfo struct also exists and is copied directly using copymemory, then you can call the createdibsection function to create an hbitmap and select this hbitmap into the DC. Then you can use bitblt or stretchblt to copy data to the Target DC, the BMP image is displayed in the drawing area.

 

 

 

If you still don't understand, there are:

1. BMP file Composition
A bmp file consists of four parts: File Header, bitmap information header, color information, and graphic data.
2. BMP File Header
The data structure of the BMP file header contains information such as the type, size, and start position of the BMP file.

Its structure is defined as follows:

Typedef struct tagbitmapfileheader
{
Wordbftype; // The type of the bitmap file, which must be BM.
DWORD bfsize; // the size of the bitmap file, in bytes.
Wordbfreserved1; // reserved word for bitmap files, which must be 0
Wordbfreserved2; // reserved word for bitmap files, which must be 0
DWORD bfoffbits; // the starting position of the bitmap data, relative to the bitmap
// The offset of the file header, in bytes
} Bitmapfileheader;

3. Bitmap header

The BMP Bitmap header is used to describe the size and other information of the bitmap.
Typedef struct tagbitmapinfoheader {
DWORD bisize; // number of bytes occupied by the structure
Longbiwidth; // The width of the bitmap, in pixels.
Longbiheight; // The height of the bitmap, in pixels
Word biplanes; // The level of the target device, which must be 1
Word bibitcount // The number of digits required for each pixel, which must be 1 (two-color ),
// 4 (16 colors), 8 (256 colors), or 24 (true color)
DWORD bicompression; // bitmap compression type, which must be 0 (not compressed ),
// 1 (bi_rle8 compression type) or 2 (bi_rle4 compression type)
DWORD bisizeimage; // bitmap size, in bytes
Longbixpelspermeter; // horizontal bitmap resolution, number of workers per meter
Longbiypelspermeter; // bitmap vertical resolution, number of workers per meter
DWORD biclrused; // number of colors in the color table used by the bitmap
DWORD biclrimportant; // number of important colors in the bitmap display process
} Bitmapinfoheader;

4. Color Table
A color table is used to describe the color in a bitmap. It has several table items. Each table item is an rgbquad-type structure and defines a color. The rgbquad structure is defined as follows:

Typedef struct tagrgbquad {
Bytergbblue; // blue brightness (value range: 0-255)
Bytergbgreen; // The brightness of the green color (value range: 0-255)
Bytergbred; // The Red brightness (value range: 0-255)
Bytergbreserved; // reserved, must be 0
} Rgbquad;
The number of rgbquad structure data in the color table is determined by bibitcount:
When bibitcount is 1, 4, and 8, there are 2, 16, and 256 table items respectively;
When bibitcount = 24, there is no color table item.
The bitmap information header and the color table form bitmap information. The bitmapinfo structure is defined as follows:
Typedef struct tagbitmapinfo {
Bitmapinfoheader bmiheader; // Bitmap header
Rgbquad bmicolors [1]; // color table
} Bitmapinfo;

5. bitmap data
The bitmap data records each pixel value of the bitmap. The record sequence is from left to right in the scan row, and the rows are from bottom to top. The number of bytes occupied by a pixel value of a bitmap:

When bibitcount = 1, 8 pixels constitute 1 byte;
When bibitcount = 4, 2 pixels constitute 1 byte;
When bibitcount = 8, 1 pixel occupies 1 byte;
When bibitcount = 24, one pixel occupies three bytes;
Windows requires that the number of bytes occupied by a scan row must be
A multiple of 4 (in the unit of long). The insufficient values are filled with 0,
Calculation of the number of bytes occupied by a scanned row:
Datasizeperline = (biwidth * bibitcount + 31)/8;
// The number of bytes that a scan row occupies
Datasizeperline = datasizeperline/4*4; // The number of bytes must be a multiple of 4
Bitmap data size (without compression ):
Datasize = datasizeperline * biheight;

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.