BMP file Format

Source: Internet
Author: User

Where is BMP in the end is sacred?

The BMP file format, also known as bitmap (bitmap) or DIB (Device-independent devices, device-independent bitmaps), is a widely used image file format in Windows systems. Since it can save the data of the image pixel domain without any transformation, it becomes an important source for us to obtain raw data. The graphical user interface of Windows (graphical user interfaces) also provides support for BMP formats in its built-in image subsystem GDI.

So what is the format of BMP?

The data for BMP files is divided into four parts according to the order of the files starting from the file header:

BMP header (BMP file header): Provides information about the format, size, etc. of the file

Bitmap Information Header (bitmap information): Provides information on image data size, number of bits, compression, color index, etc.

Color palette (color palette): optional, such as using an index to represent an image, a palette is a mapping table of its corresponding color

Bitmap data (bitmap): Image data ^_^

The following is a table to analyze these four sections, combined with the definition of the Windows fabric body.

We generally see the image is based on 24-bit images, that is, R, G, b three colors each with 8 bit to represent, such an image we call true color, this case is not required palette, that is, the bitmap information is immediately behind the bitmap data. Therefore, we often see there is a saying: Bitmap files from the beginning of the file header offset 54 bytes is the bitmap data, which is actually said to be 24 or 32-bit diagram of the case. This explains why the program we wrote in this program is useless for some bitmap files.

The following is an analysis of a specific image to see the layout and composition of the four data segments in the bitmap file.

The images we use are shown below:

  

int bisize//Information header required number of bytes (14-17 bytes)

int Biwidth//width of bitmap (18-21 bytes)

int Biheight//height of bitmap (22-25 bytes)

int biplanes//target device level, must be 1 (26-27 bytes)

The number of bits (28-29 bytes) required for an int bibitcount//pixel must be either 1-bit (two-color), 4-bit (16-color), 8-bit (256-color), or 24-bit (true color).

int bicompression//bitmap compression type, must be either 0 (not compressed) (30-33 bytes), 1 (bi_rleb compression type), or 2 (bi_rle4 compression type).

int biSizeImage//actual bitmap image size, i.e. the entire actual drawn image size (34-37 bytes)

int Bixpelspermeter//bitmap horizontal resolution, pixels per metre (38-41 bytes) This number is the system default value

int Biypelspermeter//bitmap vertical resolution, pixels per metre (42-45 bytes) This number is the system default value

int biclrused//bitmap The number of colors in the color table actually used (46-49 bytes), if 0, all used

int Biclrimportant//The number of important colors in the bitmap display (50-53 bytes), if 0, indicating all important

We will emphasize the next bisize, Biheight and biBitCount according to the analytic chart.

<!--endfragment-->

<!--endfragment-->

According to the information I have listed above,

The first message in the information header is bisize, which means that the total byte space for this information header is 40.

Then we combine the above mentioned file header total byte space is 14, then we add is not the 54 it! So the offset bfoffbits is 54! But you might ask, why is it 54? How did you get here? is not all BMP file offset bfoffbits is 54? Oh, no, not all BMP file offset bfoffbits are 54, for 24-bit bitmap, yes, for the other BMP file is not necessarily! This offset is important, and we must pay attention to its accuracy when reading and writing.

We return to the above bisize, according to the previous file header is accounted for 14 bytes, then the bisize address is 第14-17 byte, then corresponding, then that is 28 00 00 00. And according to what we said above, the storage of the machine is small-end storage, so the correct reading is 00 00 00 28.

While reading and writing behind the bitmap data, it is important to note that this height is a positive or negative. If it is positive, then the bitmap data is read and written in order from left to right, from bottom to top. Otherwise it's from left to right, top to bottom. Because the height in this figure is positive, the following discussion of bitmap data is described in order from left to right, bottom to top.

We read the BMP file of the second piece of information header, the next third color table will be based on the second piece of information, what meaning? Because if the BMP file is a 24-bit or 32-bit image, then there is no color table this piece, only when the number of bits less than 24, it is necessary to consider this piece.

Three. Color table
A color table is actually a mapping table that identifies the corresponding relationship between the color index number and the color it represents. Its layout in the file is like a two-dimensional array palette[n][4], where N represents the total number of color indexes, and four elements per row represent the values of B, G, R, and Alpha for that index, each of which takes one byte. If no transparent channel is set, Alpha is 0. Specific information, we can Google or Niang.

Four. Bitmap data

Okay, so here's our most important piece, writing or reading the actual data of the BMP file, which is the pixel of the image we're drawing. We know that a pixel consists of three int values, namely RGB, red component, green component, blue component. And a pixel occupies three bytes in memory, that is, each component occupies one byte. Then we need to get the component of each pixel and then store it in BGR order in memory in the small end. Is that all you can do? No,no,no. No, because we have to pay attention to it. The minimum unit of scanning for Windows is 4 bytes, and if the data alignment satisfies this value, there is a significant gain in data acquisition speed. As a result, BMP images conform to this requirement, requiring that the length of each line of data must be a multiple of 4, if it is not enough to be filled with bits (with 0 padding), so that fast access to rows can be achieved. At this point, the size of the bitmap data area is not necessarily the picture width × the number of bytes per pixel x picture high energy representation, because each row may also need to be bit populated.

BMP file Format

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.