BMP image file structure

Source: Internet
Author: User
Tags bmp image

A typical BMP image file consists of four parts:

1: The data structure of the bitmap file header, which contains information such as the type and content of the BMP image file;

2: The bitmap information data structure, which contains the width, height, compression method, and color of the BMP image;

3: color palette. This part is optional. Some bitmaps need a color palette, and some bitmaps, such as true color graphs (24-bit BMP) do not need a color palette;

4: bitmap data. The content varies depending on the number of digits used by the BMP bitmap. In the 24-Bit Bitmap, RGB is used directly, the other less than 24 digits use the color index value in the color palette.

 

 

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 (14 bytes)

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

{

Wordbf type; // The type of the bitmap file, which must be BMP (0-1 bytes)

DWORD bfsize; // the size of the bitmap file, in bytes (2-5 bytes)

Word bfreserved1; // reserved word of the bitmap file, which must be 0 (6-7 bytes)

Word bfreserved2; // reserved word of the bitmap file, which must be 0 (8-9 bytes)

DWORD bfoffbits; // the starting position of the bitmap data, relative to the bitmap (10-13 bytes)

// The offset of the file header, in bytes

} Bitmapfileheader;

3: Bitmap header (40 bytes)

The BMP Bitmap header is used to describe the size and other information of the bitmap.

Typedef struct tagbitmapinfoheader {

DWORD bisize; // The number of bytes occupied by the structure (14-17 bytes)

Long biwidth; // The bitmap width, in pixels (18-21 bytes)

Long biheight; // The height of the bitmap, in pixels (2-25 bytes)

Word biplanes; // The level of the target device, which must be 1 (26-27 bytes)

Word bibitcount; // The number of digits required for each pixel, which must be 1 (two-color), (28-29 bytes)

// 4 (16 colors), 8 (256 colors), or 24 (true color)

DWORD bicompression; // bitmap compression type, which must be 0 (not compressed), (30-33 bytes)

// 1 (bi_rle8 compression type) or 2 (bi_rle4 compression type)

DWORD bisizeimage; // bitmap size, in bytes (34-37 bytes)

Long bixpelspermeter; // horizontal bitmap resolution, number of bytes per meter (38-41 bytes)

Long biypelspermeter; // bitmap vertical resolution, number of bytes per meter (42-45 bytes)

DWORD biclrused; // number of colors in the color table used by the bitmap (6-49 bytes)

DWORD biclrimportant; // number of important colors in the bitmap display process (50-53 bytes)

} 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 {

Byte rgbblue; // blue brightness (value range: 0-255)

Byte rgbgreen; // The Green brightness (value range: 0-255)

Byte rgbred; // red brightness (value range: 0-255)

Byte rgbreserved; // 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,

 

Bisizeimage = (Bi. biwidth * bi. bibitcount) + 31 )&~ 31)/8) * bi. biheight;

If you use the Direct Read File method to display the BMP file, pay attention to it here! A bug found when I wrote a tool for converting images in the man-machine interface: When the read BMP image pixel is of some values, it can be correctly displayed, the other values are not displayed normally. After careful searching, we can find that only images with the X coordinate of 4 are displayed normally, this is the reason above, which leads to incorrect display.

Data examples:

For example, the start of a BMP file:

4d42 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: (the number here represents the "Word", that is, two bytes, the same below) image file header. 0x4d42 = '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.

Ii. Bitmap information Header

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.

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. This number must be a multiple of 4 and the value is equal to or greater than the minimum 4 of the bitmap width) × bitmap height × the number of digits in each 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

16) 28-... (uncertain): 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 blue 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) 27 (no palette)-...: 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.

V. Storage Algorithms

BMP files are usually not compressed, so they are generally much larger than the compressed image file format of the same image. For example, a 24-digit 800x600 occupies almost MB space. Therefore, they are generally not suitable for transmission over the Internet or other low-speed or capacity-limited media. Depending on the color depth, a pixel on an image can be expressed in one or more bytes, which is determined by N/8 (N is a bit depth, 1 byte contains 8 data bits ). The image browser calculates the color of the pixel Based on the byte ASCII value, and then reads the corresponding value from the color palette. For more information, see the following section on Bitmap files. The approximate number of N-bit 2n bitmap bytes can be calculated using the following formula: the size of the BMP file is approximately 54 + 4*2 n + (w * H * N)/8

The height and width are the number of bins. Note that 54 in the above formula is the file header of the bitmap file, which is the size of the color palette. In addition, it should be noted that this is an approximate value. For N-bit bitmap images, although there may be a maximum of 2n colors, a specific image may not use all these colors. Since the color palette only defines the color used by the image, the actual color palette is smaller. If you want to know how these values are obtained, refer to the file format section below. Due to factors determined by the storage algorithm itself, the size calculated based on different image parameters may be slightly different from the actual file size.

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.