BMP file format

 

BMP (Bitmap-File) image file, also known as bitmap file, is a graphics file format adopted by Windows. All image processing software running in the Windows environment supports BMP image file format.

BMP images prior to Windows 3.0 are related to display devices, so this BMP image is called device-dependent bitmap (device-dependent bitmap) file format. BMP image files after Windows 3.0 have nothing to do with the display device, so this BMP image file format is called device-independent bitmap DIB (device-independent bitmap) format.

It uses a bit-mapped storage format and does not use any other compression except for the optional image depth. Therefore, the BMP file takes up a lot of space. The image depth of BMP file can be 1bit, 4bit, 8bit, 16bit, 24bit and 32bit.

BMP files are mainly composed of four parts, bitmap header , bitmap information , palette , and bitmap data .

Bitmap header

variable byte effect
bfType 2 Indicate the type of the file, the value must be 0x424D, which is the character'BM', otherwise it means it is not BMP at all
bfSize 4 Specify the size of the bitmap file in bytes
bfReserved1 2 Reserved, must be set to 0
bfReserved2 2 Reserved, must be set to 0
bfOffBits 4 Describes the byte offset from the beginning of the file to the actual image data. Because of the bitmap information header. Will vary with the length of the palette, so you need to use this offset value to quickly read data from the file quickly

Bitmap information

variable byte effect
bitSize 4 Explain the number of bytes required for the BITMAPINFOHEADER structure
bitWidth 4 Specifies the width of pixels, in pixels
biHeight 4 Said the height of the pixel, in pixel units. This value also indicates whether the image is upside down or forward. If the value is positive, it means that the image is inverted, that is: the first row of data is actually the last row of the image. If the value is a negative value, the image is positive. Most BMP files are inverted, that is, the height value is a positive number
biPlanes 2 Indicates the number of planes of the bmp picture. Obviously the display has only one plane, so it is always 1
bitBitCount 2 Specify the number of bits/pixel, the value is 1, 4, 8, 16, 24, 32
bitCompression 4 Explain the type of image compression, where: BI_RGB: no compression. BI_RLE8: RLE compression coding with 8 bits per pixel. BI_RLE4: RLE compression coding with 4 bits per pixel. BI_BITFIELDS: The bits of each pixel are determined by the specified mask. BI_JPEG: JPEG format (only for printers). BI_PNG: PNG format (only for printers).
bitSizeImage 4 Describe the size of the image, in bytes. When BI_RGB format is used, it can be set to 0
bitXPelsPerMeter 4 Specify the horizontal resolution, expressed in pixels/meter
bitYPelsPerMeter 4 Explain the vertical resolution, expressed in pixels/meter
biClrUsed 4 Explain the number of color indexes in the color table actually used by the bitmap (0 means use all the palette items)
biClrImporant 4 Explain the number of color indexes that have an important impact on the image display, if it is 0, it means very important

Color palette

The palette does not have to exist.

The palette is actually a mapping table that identifies the corresponding relationship between the color index number and the color it represents. If there are only a few commonly used colors in an image, you can use the palette mode. Similar to SCC palette mode

Assuming an image is 1280x720, each pixel has three RGB channels, and each channel has 1 byte, then the entire image needs to use 1280x720x3=2764800bytes.

If only 32 colors are used in the image, you can use a table to store these 32 colors, and each pixel only needs to record its index in the table, and the 32 color indexes require 5bit. The entire image needs to use 1280x720x5bit=576000bytes.

Bitmap data

The bitmap data encoding method is determined by the bitCompression field in the bitmap information. Typically, the BI_RGB mode is not compressed, and each pixel is represented by 3 bytes.

Data alignment : The minimum unit of scanning by default in Windows is 4 bytes. If the data alignment meets this value, there will be a great gain in the speed of data acquisition. Therefore, BMP images comply with this requirement, requiring that the length of each line of data must be a multiple of 4. If it is not enough, bit filling (filling with 0) is required, so that fast access by line can be achieved. At this time, the size of the bitmap data area may not be represented by the width of the picture × the number of bytes per pixel × the high energy of the picture, because each row may need to be filled with bits.

For example, for a 30x30 image, 30 bytes per row is not an integral multiple of 4, and 0 needs to be filled to supplement each row to 32 bytes.

Case Analysis

Take a 1280x720 bmp image as an example to analyze its structure:

First, note that bmp is stored in little-endian mode , that is, low addresses store low-bit data, and high addresses store high-bit data.

File header analysis:

The red box is the header part of the bmp file, a total of 14 bytes:

1-2: Fixed 0x424D, representing the ASSIC code of "BM".

3-6: 0x36302A00, because it is little-endian, its true value is 0x002A3036=2764854bytes, which represents the size of the bitmap file. This is indeed the case through the file properties. (The little-endian mode below is not explained separately)

7-8: Reserved field, fixed to 0.

9-10: reserved field, fixed to 0.

11-14: 0x00000036=54, which means that 54 bytes offset from the beginning of the file is the data part. (The following can be verified, the file header is 14 bytes, the file information header is 40 bytes, there is no palette, so the data part starts from the 54th byte)

Bitmap information header:

The green box is the bitmap information header, a total of 40 bytes:

1-4: 0x00000028=40, indicating that the bitmap information header occupies 40 bytes.

5-8: 0x00000500=1280, which means that the image width is 1280 pixels.

9-12:0x0002D0=720, which means that the image height is 720 pixels, and the image is inverted, that is, the first line of data is actually the last line of the image.

13-14:0x0001=1, it is always 1.

15-16:0x0018=24, which means that each pixel is represented by 24bit.

17-20:0x0000000=0, BI_RGB mode means that the data is not compressed.

21-24:0x0000000=0, the value is 0 in BI_RGB mode.

25-28:0x0000000=0, horizontal resolution, default.

29-32:0x0000000=0, vertical resolution, default.

33-36:0x0000000=0, which means all palette items are used.

37-40:0x0000000=0, which means that all colors are important.

If you are interested, please follow the WeChat public account Video Coding

Guess you like

Origin blog.csdn.net/Dillon2015/article/details/106292536