BMP structure Detailed

Source: Internet
Author: User
Tags bmp image transparent color repetition

Bitmap Bitmapinfoheader and Bitmapfileheader:

First look at bitmapinfoheader, write only a few major
Bisize contains the size of the struct (including the color table)
Biwidth and Biheight, respectively, are the length and width of the picture
Biplanes is the number of layers that the target drawing device contains and must be set to 1
biBitCount is the number of bits of the image, such as 24-bit, 8-bit, etc.
Bixpelspermeter, Biypelspermeter is the real world with a number of pixels per metre set to 3780
biSizeImage size of image data = Biwidth x biheight x biBitCount
---------------------------------------------------------------------------------
Look at Bitmapfileheader again.
Bftype the type of picture must be BM fill 0x4d42 that is the decimal 19778
Bfoffbits from file header to color data (??) Offset 54+sizeof (rgbquad) *256
Bfsize the size of the picture, Bfoffbits + long x Width x digits For example for 128x128x24 bit image bfsize=128x128x24 + 54+sizeof (rgbquad) *256
BfReserved1 and bfReserved1 must be 0

BMP file structure and its access
Digital Image storage in the external memory device is an image file, the image must be in accordance with a known, recognized data storage sequence and structure to store, in order to make different programs on the image file open or disk operation, to achieve data sharing. the order and structure in which image data is stored in a file is called an image file format. There are many kinds of image file formats that are widely circulated, including BMP, GIF, JPEG, TIFF, PSD, DICOM, MPEG and so on. In a variety of image file formats, some are presented by a hardware and software manufacturer and widely accepted and adopted formats, such as BMP, GIF and PSD format, and the other part is the format presented by various international standards organizations, such as JPEG, TIFF and DICOM, JPEG is the international still image compression standard organization proposed format, TIFF is a format proposed by some manufacturers, dicom is the medical image of the international Standard organization of medical image Special format.
BMP files are the recommended and supported image file formats of the Windows operating system , which is a file format for storing the image data of memory or display files without compression, so called bitmap (bitmap) files, because their file name extension is BMP, So called BMP file format, referred to as BMP file. This book is the image of the algorithm programming for BMP image files, so in this chapter we detail the BMP file structure and read-write operations to deepen the understanding of image data.

BMP files are generally composed of 4 parts, namely, bitmap file header, bitmap information header, palette, and image data, as shown in table 5-1.

Table 5-1 BMP File Composition structure

Bitmap file Header (Bitmap-file header)

Bitmap Information Header (bitmap-information header)

Color tables/palettes (color table)

Bitmap data (Bitmap-data)

Let's look at the details of each component in detail below.

1 . Bitmap file Header (bitmap-file header)

The bitmap file header (Bitmap-file header) contains the image type, image size, image data storage address, and two reserved unused fields.

Open the WINGDI.h file and search for "Bitmapfileheader" to locate the data structure definition of the bitmap file header of the BMP file.

typedef struct TAGBITMAPFILEHEADER {
WORD Bftype;
DWORD bfsize;
WORD bfReserved1;
WORD BfReserved2;
DWORD bfoffbits;
} Bitmapfileheader, far *lpbitmapfileheader, *pbitmapfileheader;

2 . Bitmap Information Header (bitmap-information header)

The bitmap information header (bitmap-information header) contains the size of the bitmap information header, the width of the image, the color depth of the image, the compression that describes the image data, and some other parameters.

Open the WINGDI.h file and search for "Tagbitmapinfoheader" to locate the data structure definition of the bitmap information header of the BMP file.

typedef struct tagbitmapinfoheader{
DWORD bisize;
LONG Biwidth;
LONG Biheight;
WORD biplanes;
WORD biBitCount;
DWORD bicompression;
DWORD biSizeImage;
LONG Bixpelspermeter;
LONG Biypelspermeter;
DWORD biclrused;
DWORD biclrimportant;
} Bitmapinfoheader, far *lpbitmapinfoheader, *pbitmapinfoheader;

3 . Color tables/palettes ( color table)

Color tables/palettes (color table) are unique to monochrome, 16-color, and 256-color image files, the corresponding palette sizes are 2, 16, and 256, the palette is in 4 bytes, each 4 bytes holds a color value, and the image's data is the index to the palette.

You can think of a palette as an array of 4 bytes per array element, assuming that the palette data for a 1256-color BMP image is:

palette [0]= Black, palette [1]= White, palette [2]= Red, palette [3]= blue ... palette [255]= Yellow

Image data The data in the palette [1], palette [0], palette [2], and palette [255] is displayed to display the image color.

In earlier computers, the graphics card was relatively backward, not necessarily guaranteed to display all colors, so the color data in the color palette should be as far as possible in the image of the main color in order in the front, the Biclrimportant field of the bitmap information header indicates how many colors are important.

Each palette has a size of 4 bytes, and a color value is stored by blue, green, and red.

Open the WINGDI.h file and search for "tagrgbtriple" to locate the data structure definition of the BMP file's palette.

typedef struct TAGRGBQUAD {
BYTE Rgbblue;
BYTE Rgbgreen;
BYTE rgbred;
BYTE rgbreserved;
} Rgbquad;

4 . Bitmap data (Bitmap-data)

If the image is monochrome, 16-color, and 256-color, the bitmap data is closely followed by the palette, and the bitmap data is the index number that points to the palette.

If the bitmap is 16-bit, 24-bit, and 32-bit color, the color palette is not preserved in the image file, i.e. there is no palette, and the image is given directly in the bitmap data.

16-bit images use 2 bytes to save color values, common in two formats: 5-bit red 5-bit green 5-bit blue and 5-bit red 6-bit green 5-bit blue, which is 555 format and 565 format . The 555 format uses only 15 bits, the last one is reserved, and is set to 0.

24-bit Images use 3 bytes to save color values, each of which represents a color, arranged in red, green, and blue.

The 32-bit image uses 4 bytes to hold the color value, each of which represents a color, except for the original red, green, and blue, as well as the alpha channel, the Transparent color.

If the image has a color palette, the bitmap data can be compressed and uncompressed as needed, and if compression is selected, the BMP image is compressed using the RLE4 or RLE8 compression algorithm, depending on whether it is a 16-color or 256-color.

The RLE4 is compressed with 16-color image data, and RLE4 compresses the data in the manner shown in table 5-5.

Suppose you have the following 16-color bitmap data, a total of 20 bytes, and the data uses RLE4 compression:

05 00 04 05 00 08 09 05 04 00 04 05 08 09 04 08 07 01 00 00

Data decompression first read 05, because 05 is not equal to 0, so choose a scheme, according to a scheme, 05 indicates the number of times after the data repeat, and then read 00,00 represents two color index, each index 4 bits, the first pixel in the high 4 bits, the second pixel in the low 4 bits, that is, in one byte low pixels in the high, High pixels at low levels. 05 00 after decompression equals 00 00 0.

Read 04, select a scheme, according to the above operation parsing, 04 is the number of times after the data repeat, 05 is two color index, the 3rd color index is 5, and the 4th color index is 0. 04 05 after decompression equals 05 05.

Read 00, select the B scheme, and read 08,08 to indicate the number of color indexes that are valid later. 00 08 after decompression equals 09 05 04 00.

Read 04, select a scheme, according to the above operation parsing, 04 is the number of times after the repetition of data, 05 is two color index. 04 05 after decompression equals 05 05.

Read 08, select a scheme, according to the above operation Parsing, 08 is the number of times after the repetition of data, 09 is two color index. 08 09 After decompression equals 09 09 09 09.
Read 04, select a scheme, according to the above operation parsing, 04 is the number of times after the repetition of data, 08 is two color index. 04 08 After decompression equals 08 08.

Read 07, select a scheme, according to the above operation parsing, 07 is the number of times after the repetition of data, 01 is two color index. 07 01 after decompression equals 01 01 01 0.

Read 00, select the B scheme, read 00,00 to indicate the number of valid color indexes, 0 means none, that is, to extract a row of data.

To synthesize the above operation, the extracted data is:

---------ten--

Looks like the original data size, does not reflect the compression effect, this is because the above example only selects 20 bytes of data, and this 20 bytes of data duplication of data is not many, using RLE compression data is not much, sometimes the size of compression may be larger than the original data. In fact, the general situation when the data is more and repeat the time, the use of RLE compression effect is still more ideal.

RLE8 compression mode can refer to the above RLE4 decompression method, the only difference is that RLE8 use 1 bytes to store the color index, and RLE4 use 4 bits to store the color index.

Combined with the analysis of the BMP file, the following two 256-color and 24-bit color BMP image hexadecimal analysis, through the hex editor to analyze the file structure, can increase the experience of the analysis file.

5-1 and Figure 5-2, respectively, 256-color BMP image Cat2.bmp and 24-bit color BMP image cat1.bmp. Where the resolution of the cat2.bmp image is 200x153 and the file size is 31 680 bytes. The cat1.bmp image has a resolution of 200x150 and a file size of 90 056 bytes.

Figure 5-1 Cat2.bmp Image

Figure 5-2 Cat1.bmp Image

Now parse the cat2.bmp image file, as shown in Winhex open cat2.bmp,5-3.

Figure 5-3 Opening the cat2.bmp image file in Winhex

The structure of the bitmap file header is analyzed first, as shown in 5-4. The meaning of each field in the bitmap file header of the cat2.bmp image is analyzed according to the bitmap file header structure definition of BMP file, as shown in table 5-6.

Figure 5-4 Bitmap file header for cat2.bmp image file

Table 5-6 meaning of each field in the bitmap file header in the cat2.bmp image file

Hexadecimal value

Description

4 D:

The ASCII value of BM, the BMP file identifier in Windows

C0 7B 00 00

7B c0h=31680, is the size of the Cat2 file

00 00 00 00

Reserved value, Total 0

36 04 00

436h=1078, is the address of the image data, that is, the length of the file header + Information header + Palette

Continue to analyze the next data, according to the BMP file structure definition, the next data is the bitmap information header, cat2.bmp image file of the bitmap information header of the content 5-5 is shown.

Figure 5-5 Bitmap Information header for cat2.bmp image

Table 5-7 shows the meaning of the bitmap information header fields in the Cat2.bmp image file.

Table 5-7 meaning of each field in the bitmap information header in the cat2.bmp image file

Hexadecimal value

Description

28 00 00 00:

Bitmap information header size for cat2.bmp images

C8 00 00 00

XX C8 = 200, is the width of the CAT2 image, unit pixels

99 00 00 00

00 00 00 99 = 153, is the height of the cat2 image, per pixel

01 00

Always 1

08 00

8,CAT2 = The color depth of the image, i.e. 2 of the 8 powers equals 256 colors

00 00 00 00

Compression mode, 0 means no compression

8A 77 00 00

8A = 30602, is the image data size of the CAT2 image, Unit bytes

0B 00 00

0B = 2834,CAT2 Image horizontal resolution, per pixel/m

0B 00 00

0B = Vertical of 2834,CAT2 image

resolution, per pixel/m

00 00 00 00

CAT2 the number of colors used in the image, 0 means using all colors

00 00 00 00

CAT2 the number of important colors in an image, 0 means all colors are important

Continue to analyze the next data, according to the BMP file structure definition, because the cat2.bmp image is a 256-color bitmap, so there should be 256 color palettes, each palette 4 bytes, the entire palette is 1024 bytes in size. The palette data for the cat2.bmp image file is 5-6 and shown in Figure 5-7.

Figure 5-6 The palette address of the cat2.bmp image is stored starting at 00000036h

Figure 5-7 cat2.bmp Image color palette data End address is 00000435h

As can be seen from figure 5-6 and figure 5-7, the color palette address of the cat2.bmp image starts from 00000036h to 00000435h, i.e. 00000435h-00000036h + 1 =400h = 1024.

If you want to see the actual display color of the palette for the CAT2 image, you can use Adobe Photoshop CS to open cat2.bmp, select "image" → "mode" → "color table" in the Adobe Photoshop CS menu bar to view the Cat2 palette. As shown in 5-8.

Figure 5-8 Viewing the Cat2 palette in Adobe Photoshop CS

The color palette of the cat2.bmp shown in Figure 5-8 and the hexadecimal data in Figure 5-6 correspond to one by one. Clicking on the color of any one pixel on the Adobe Photoshop cs palette pops up a color Picker dialog that displays detailed composition information for that pixel color. The Cat2.bmp color palette and the hexadecimal data for the cat2.bmp are shown in the corresponding relationship 5-9.

Continue to analyze the next data, according to the BMP file structure definition, if an image has a color palette, then immediately after the color palette is the image of the data, the data is not the actual color values, but to the index of the palette array, according to the index to get the color palette, 5-10 is shown.

Figure 5-9 The corresponding relationship between the Cat2.bmp palette and the hexadecimal data of the cat2.bmp

Figure 5-10 cat2.bmp image data

Because Cat2.bmp is a 256-color bitmap, which uses 8-bit color depth as an index to the palette array, you can tell by the data shown in Figure 5-10: B1 49 49 49 49 49 99 indicates that the color of the 1th pixel in the lower-left corner of the cat2.bmp bitmap equals the palette [ 49], the color of the 2nd pixel equals the palette [49], the 3rd pixel is the color of the palette [49], the 4th pixel color equals the palette [B1] ... And so on After analyzing the cat2.bmp image, the next analysis is cat1.bmp.

Cat1.bmp image is a 24-bit color image, according to the definition of BMP file structure, cat1.bmp image does not have a color palette, the image data is stored in the actual color data, each pixel is represented by 3 bytes, respectively, is red and green blue. Because Cat1.bmp and cat2.bmp bitmap file header and bitmap information header structure, so cat1.bmp bitmap file header and bitmap information header can refer to the analysis of the face cat2.bmp, the following from the cat1.bmp of the bitmap information header end of the position to start analysis, 5-11.

Figure 5-11 Image data of cat1.bmp image

From figure 5-11 can be seen to represent each pixel red, green and blue three-color values, the actual storage is reversed storage, in the analysis of BMP image format need to pay attention to this.

Through the analysis of the storage structure of BMP file above, it is found that there is a lot of duplicate data in the bitmap file header and bitmap information header of BMP file. If the storage of a large number of BMP bitmap with deep, will inevitably waste a lot of storage space, so many times the game programmer will remove the BMP file header and information header, only a few of the necessary information and image data, then the BMP file header and information header which fields are to be retained?

Compare two 24-bit color-depth BMP image files using the Winhex file comparison feature, and observe the file header and information header of two files in a different place, as shown in 5-12.

Figure 5-12 A BMP image file comparing two 24-bit color depths using Winhex

As can be seen from figure 5-12, two of the same color depth of the BMP image of the file header and information header a total of 4 different places, respectively, the file header of the file size, the image width of the information head, image height and image data size.

So many times, the game programmer only retains the image file size, image width, image height and image data size information, and sometimes do not need to preserve the size of the file value, using the image data size value can be.

In the analysis of unknown file storage format, if you encounter the file header removed files, such as the above-mentioned BMP file, the analysis of unknown file format brings some difficulties. At this time need to use the hexadecimal editor file comparison function, observe two of the same kind of unknown file format to find some potential laws, if it is not observed regularly, it can only use the White box analysis method, swapped with this unknown file format of the program for disassembly tracking debugging. Of course, inspiration and luck are also important at times.

Transferred from: http://www.cnblogs.com/lzlsky/archive/2012/08/16/2641698.html

BMP structure Detailed

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.