Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

North Asia engineer explains BMP file format in detail

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

BMP is a hardware-independent image file format that is widely used. It uses a bitmapped storage format and does not use any compression other than image depth, so BblP files take up a lot of space. BMP file image depth can be lbit, 4bit, 8bit and 24bit. BMP files store data in such a way that images are scanned from left to right and bottom to top.

Because BMP file format is a standard for exchanging graph-related data in Windows environment, all graphic image software running in Windows environment supports BMP image format.

A typical BMP image file consists of three parts: bitmap file header data structure, which contains information such as BMP image file type, display content, etc. Bitmap information data structure, which contains information such as BMP image width, height, compression method, and definition color.

Examples of specific data:

For example, a BMP file starts with:

424D 4690 0000 0000 0000 4600 0000 2800 0000 8000 0000 9000 0000 0100*1000 0300 0000 0090 0000 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 file can be divided into four parts: bitmap file header, bitmap information header, color plate, image data array, in the above figure has been separated by *.

1. Image file header

1) Image file header. 424Dh="BM," which means BMP format supported by Windows.

2) 2-3: Full file size. 4690 0000 is 00009046h=36934.

3) 4-5: Reserved, must be set to 0.

4) 6-7: Offset from the start of the file to the bitmap data. 4600 0000, 0000046 h=70, the file header above is 35 words =70 bytes.

5) 8-9: Length of bit map header.

6) 10-11: Bitmap width, in pixels. 8000 0000 is 0000080 h=128.

7) 12-13: Bitmap height, in pixels. 9000 0000 is 0000090 h=144.

8) 14: The number of bitmaps, which is always 1. 0100 is 0001h=1.

II. Bitmap header

9) 15: Number of bits per pixel. There are 1 (monochrome), 4 (16 colors), 8 (256 colors), 16 (64K colors, high color), 24 (16M colors, true color), 32 (4096M colors, enhanced true color). 1000 is 0010h=16.

10) 16-17: Compression Description: 0 (no compression), 1 (RLE 8, 8-bit RLE compression), 2 (RLE 4, 4-bit RLE compression, 3 (Bitfields, bitfield storage). RLE is simply compressed by pixel count + pixel value. The T408 uses bit-field storage, with two bytes representing a pixel and bit-field allocation of r5b6g5. 0300 0000 is 0000003 h=3.

11) 18-19: Size of bitmap data in bytes, which must be a multiple of 4 and numerically equal to bitmap width × bitmap height × number of bits per pixel. 0090 0000 is 00009000h=80×90×2h=36864.

12) 20-21: Horizontal resolution in pixels/meter. A00F 0000 is 0000 0FA0h=4000.

13) 22-23: Vertical resolution in pixels/meter. A00F 0000 is 0000 0FA0h=4000.

14) 24-25: Number of color indices used by bitmaps. If set to 0, all palette items are used.

15) 26-27: Number of color indices that have a significant impact on image display. If it's 0, it means it's important.

III. Color plate (not required)

16) 28-35: Color plate specifications. For each entry in the palette, the RGB values are described as follows:

1 byte for blue component

1 byte for green component

1 byte for red component

1 byte for padding (set to 0)

For 24-bit true color ×× images, no color plate is used because the RGB values in the bitmap represent the color of each pixel.

For example, the color plate is 00F8 0000 E007 0000 1F00 0000 0000, where:

00FB 0000 is FB00h= 11110000000 (binary), which is the mask for the red component.

E007 0000 is 07E0h= 00000111110000 (binary), which is a mask for the green component.

1F00 0000 is 001Fh= 0000000011111 (binary), which is the mask for the red component.

0000 0000 is always set to 0.

Each color component value can be obtained by performing an AND operation on the mask and the pixel value and then performing a shift operation. Looking at the mask, it can be seen that in fact, out of the 16 bits of the two bytes of each pixel value, 5, 6, and 5 bits are the r, g, and b component values respectively. After taking out the component values, multiply the r, g, and b values by 8, 4, and 8, respectively, to complete the first component into a byte, and then combine these three bytes according to rgb and put them into memory (also in reverse order), which can be converted into 24-bit standard BMP format.

IV. Image data array

17)17-...:Every two bytes represent a pixel. The first byte in the array represents the pixel in the lower left corner of the bitmap, while the last byte represents the pixel in the upper right corner of the bitmap.

Here's how to open a bmp file with winhex and see its hexadecimal data.

A bmp file starts with the BITMAPFILEHEADER structure,(frombyte.com) The first attribute of BITMAPFILEHEADER is bfType(2 bytes), which is constant here equal to 0x4D42. Since the data in the memory is arranged in the high position on the left and the low position on the right, it is displayed as (42 4D) from left to right in the memory, so the first two bytes in winhex are displayed as (42 4D).

The second attribute of BITMAPFILEHEADER is bfSize(4 bytes), which indicates the size of the entire bmp file, which is equal to 0x00004 F8=1272 bytes.

The third and fourth attributes of BITMAPFILEHEADER are bfReserved1 and bfReserved2 (2 bytes each) respectively, here are 2 reserved attributes, both of which are 0, here equal to &H0000 and 0×0000.

The fifth attribute of BITMAPFILEHEADER is bfOffBits(4 bytes), which indicates the position offset of the DIB data area in the bmp file, which is equal to 0× 0000076 =118, indicating that the data area starts 118 bytes from the beginning of the file.

BITMAPFILEHEADER struct is finished here. You will find that BITMAPFILEHEADER only occupies the first 14 bytes of bmp file length, but it needs to be specially explained that a BITMAPFILEHEADER struct variable is defined in vb, and its length occupies 16 bytes. The reason is that the first attribute should only be allocated 2 bytes, but it is actually allocated 4 bytes, and 2 bytes are extra. Therefore, if you want to save a bmp image, you must pay attention to this when writing the BITMAPFILEHEADER struct.

Next is the BITMAPINFO structure section. The BITMAPINFO (frombyte.com) segment consists of two parts: the BITMAPINFOHEADER structure and the RGBQUAD structure. The RGBQUAD structure represents the color information of the picture, and sometimes it can be omitted. Generally, 24-bit pictures and 32-bit pictures do not have the RGBQUAD structure, because the RGB values directly represented in the DIB data area, and generally 4-bit pictures and 8-bit pictures have the RGBQUAD structure. (The number of bits in a picture is the number of bits used to represent one color information. For example, a 4-bit picture represents 4 bits used to represent one color information.) Whether there is an RGBQUAD structure in a bmp file can be determined according to the fifth attribute bfOffBits of the BITMAPFILEHEADER structure, because the length of the BITMAPINFOHEADER structure is 40 bits. If the offset of the DIB data area is not reached after the end of the BITMAPINFOHEADER structure, it indicates that the next data is the RGBQUAD structure part. C:\WINDOWS\Blue Lace 16.bmp is a 4-bit image, so it has an RGBQUAD structure.

Let's move on to the BITMAPINFOHEADER section.

The first attribute of BITMAPINFOHEADER is biSize(4 bytes), which indicates the length of BITMAPINFOHEADER structure, the most common length is 40 bytes, and the next 4 bytes can be seen in UltraEdit equal to 0× 0000028 =40 bytes.

The second attribute of BITMAPINFOHEADER is biWidth(4 bytes), which indicates the width of the bmp picture, which is equal to 0× 0000030 =48 pixels.

The third attribute of BITMAPINFOHEADER is biHeight(4 bytes), which indicates the height of the bmp picture, which is equal to 0× 0000030 =48 pixels.

The fourth attribute of BITMAPINFOHEADER is biPlanes(2 bytes), which indicates the plane of bmp pictures. Obviously, the display has only one plane, so it is always equal to 1, which is equal to 0×0001 here.

The fifth attribute of BITMAPINFOHEADER is biBitCount(2 bytes), which indicates the number of color bits of bmp pictures, i.e. 24 bitmaps, 32 bitmaps, etc. This is equal to 0×0004, which means that the picture is a 4-bit map.

The sixth attribute of BITMAPINFOHEADER is biCompression(4 bytes), which indicates the compression attribute of the picture. bmp pictures are uncompressed and equal to 0, so here it is 0× 000000.

The seventh attribute of BITMAPINFOHEADER is biSizeImage(4 bytes), which indicates the size of the bmp image data area. When the previous familiar biCompression is equal to 0, the value here can be omitted, so it is equal to 0× 000000.

The eighth attribute of BITMAPINFOHEADER is biXPelsPerMeter(4 bytes), which indicates how many pixels per meter of the image X axis, which can be omitted, and here is equal to 0x0000EC3 =3779 pixels/meter.

The 9th attribute of BITMAPINFOHEADER is biYPelsPerMeter(4 bytes), which indicates how many pixels per meter in the Y-axis of the picture, which can be omitted. Here, it is equal to 0x0000EC3 =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 less than 16, and when it is equal to 0, it indicates that there are 2^biBitCount (frombyte.com) color index tables, so it is still equal to 0× 000000.

The 11th attribute of BITMAPINFOHEADER is biClrImportant(4 bytes), which indicates how many colors are important. When it is equal to 0, it means that all colors are important, so it is equal to 0× 000000.

At this point the BITMAPINFOHEADER structure ends.

Since this picture has not reached the offset of DIB data area up to this point, the next part is RGBQUAD structure. The RGBQUAD structure consists of 4 bytes of data, so an RGBQUAD structure takes up only 4 bytes of space, and each byte is represented sequentially from left to right (blue, green, red, unused). For example, this picture I counted a total of 16 RGBQUAD structures, because the picture is 4 bitmap, 2^4 is exactly equal to 16, so it enumerates all 16 colors, these colors are a color index table. The color index table numbers from 0 to 16 colors, so the numbers are 0-15. As you can see from winhex, in order, the 16 RGBQUAD structures are:

Number: (Blue, Green, Red, Empty)

Number 0:(00, 00, 00, 00)

Number 1:(00, 00, 80, 00)

Number 2:(00, 80, 00, 00)

Number 3:(00, 80, 80, 00)

Number 4:(80, 00, 00, 00)

Number 5:(80, 00, 80, 00)

Number 6:(80, 80, 00, 00)

Number 7:(80, 80, 80, 00)

No. 8:(C0, 00)

9:(00, 00, FF, 00)

Number 10:(00, FF, 00, 00)

No. 11:(00, FF, FF, 00)

Number 12:(FF, 00, 00, 00)

Number 13:(FF, 00, FF, 00)

Number 14:(FF, FF, 00, 00)

Number 15:(FF, 00)

For a more intuitive representation of these colors, see the picture below.

Here, the offset of the DIB data area is exactly satisfied, so the following bytes are the image content. It should be reminded that all DIB data scan lines are upside down, that is, a picture first draws the bottom pixels, then draws the top pixels, so the pixels represented by these DIB data start from the bottom left corner of the picture and continue to the top right corner of the picture.

Since the picture here is a 4-bit picture, that is to say, 4 bits represent a pixel, and a byte has 8 bits, so a byte can represent 2 pixels.

As can be seen from winhex, the first byte of DIB data area is 0×44, hexadecimal is exactly the binary number written every 4 groups, and the 4bit picture is exactly kissing, so 0×44 represents two pixels, the high order 4 represents the first pixel, and the low order 4 represents the second pixel. 4 here does not represent RGB color, but represents color index number 4. Since the quotation marks are numbered from 0, 4 represents the fifth color in the index table. It can be seen from the attached drawing that the index number 4 is blue. This is the first byte, representing the first two pixels in the lower left corner of the picture. If PhotoShop opens this picture, you can see that the RGB value of the color taken from the two pixels in the lower left corner is exactly equal to the RGB value of the fifth color in the index table. The DIB data follows and so on.

So far, a bmp picture is all parsed, and according to this information, a bmp picture can be drawn completely. Of course, you can also simply manually repair a damaged BMP file is not serious!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report