BMP file format

Source: Internet
Author: User

 

1. Composition of BMP files

A bmp file consists of four parts: File Header, bitmap information header, color information, and graphic data. The file header contains information such as the file size, file type, and image data deviation from the file header length; the bitmap information header contains the size information of the image. The image uses several bits to represent a pixel, whether the image is compressed, and the number of colors used by the image. Color information includes the color table used by the image. You need to use this color table to generate a color palette when displaying the image. However, if the image is true color, each pixel of the image is represented in 24 bits, this information does not exist in the file, so you do not need to operate the palette. The data block in the file indicates the corresponding pixel value of the image. Note that the order of storing the pixel value in the file is from left to right, from bottom to top, that is, in the BMP file, the last line of pixels of the image is first stored, and the first line of pixels of the image is stored, it is stored in the order on the left and right. The other detail that requires attention from readers is: when each line of the image is stored in a file, if the number of bytes occupied by the row's pixel value is a multiple of 4, the row is stored normally. Otherwise, you must add 0 at the backend to make up a multiple of 4.

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 {word bftype; // The type of the bitmap file, which must be "BMP" DWORD bfsize; // the size of the bitmap file, in bytes: Word bfreserved1; // reserved bitmap files, which must be 0 word bfreserved2; // reserved bitmap files, must be 0 DWORD bfoffbits; // the starting position of the bitmap data, expressed as the offset relative to the bitmap file header, in bytes} bitmapfileheader; this structure occupies 14 bytes.

3. Bitmap header

The BMP Bitmap header is used to describe the size and other information of the bitmap. Its structure is as follows:

Typedef struct tagbitmapinfoheader {DWORD bisize; // The number of bytes occupied by this structure long biwidth; // The width of the bitmap, in pixels long biheight; // The height of the bitmap, in pixels, word lanlanes; // the plane of the target device is countless. It 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) One DWORD bicompression; // bitmap compression type, must be 0 (not compressed), 1 (bi_rle8 compression type) or 2 (bi_rle4 compression type) DWORD bisizeimage; // bitmap size, in bytes long bixpelspermeter; // bitmap horizontal resolution, number of bytes long biypelspermeter; // bitmap vertical resolution, dwo per meter Rd biclrused; // number of colors in the color table used by the bitmap DWORD biclrimportant; // number of important colors in the bitmap display process} bitmapinfoheader; this structure occupies 40 bytes.

Note: For BMP file formats, when processing monochrome and true color images, no matter how large the image data is, the image data is not compressed. Generally, if the bitmap adopts the compression format, the 16-color image adopts the rle4 compression algorithm and the 256-color image adopts the rle8 compression algorithm.

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; // The Blue brightness (value range: 0-255) bytergbgreen; // The Green brightness (value range: 0-255) bytergbred; // 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 the bibitcount item in bitmapinfoheader. When bibitcount is 1, 4, and 8, there are 2, 16, and 256 color table items respectively. When bibitcount is 24, the image is true color. The color of each pixel is represented in three bytes, corresponding to the R, G, and B values respectively. The image file does not have a color table. The bitmap information header and the color table form bitmap information. The bitmapinfo structure is defined as follows:

Typedef struct tagbitmapinfo {bitmapinfoheader bmiheader; // bitmap information header rgbquad bmicolors [1]; // color table} bitmapinfo;

Note: In the rgbquad data structure, a reserved field rgbreserved is added, which does not represent any color and must be set to "0, in the color values defined in the rgbquad structure, the red, green, and blue colors are arranged in the same order as the color data in general true color image files: if the color of a pixel in a bitmap is described as ", FF, 00", it indicates that the pixel is red rather than blue.

5. bitmap data

The bitmap data records each pixel value of the bitmap or the index value of the corresponding pixel color table. The image record sequence is from left to right in the scan row, and the scanning rows are from bottom to top. This format is also called a bottom_up bitmap. Of course, there is a up_down bitmap, which records the order from top to bottom, and there is no compression form for this form of Bitmap. 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, 1 pixel occupies 3 bytes, and the image is a true color image. When the image is not true color, the image file contains a color table. The bitmap data indicates the index value of the corresponding pixel in the color table. When the image is true color, each pixel uses three bytes to represent the color values of the corresponding pixels of the image. Each byte corresponds to the values of R, G, and B, respectively. At this time, there is no color table in the image file. As I have mentioned above, Windows requires that the number of bytes occupied by a scanned row in an image file must be a multiple of 4 (in words), with less than 0 filling.

Calculation of the number of bytes occupied by a scanned row:

Datasizeperline = (biwidth * bibitcount + 31)/8; // number of bytes occupied by a scanned row

The bitmap data size is calculated as follows (without compression ):

Datasize = datasizeperline * biheight.

The above is a description of the BMP file format. After understanding the above structure, you can operate the image file correctly and read or write it.

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.