BMP picture structure analysis

BMP picture structure

A bmp picture consists of a file header, a bitmap information header, a palette below the 24-bit picture, and image data.
Let's analyze a 4-bit BMP picture (25*25) first, and open the picture with UE.
Insert picture description here

File header

The file header includes the following items
typedef struct tagBITMAPFILEHEADER { WORD bfType; DWORD bfSize; WORD bfReserved1; WORD bfReserved2; DWORD bfOffBits; } BITMAPFILEHEADER;





  • bfType (2byte)
    file type, the specific value is 0x4D42 = "BM"
  • bfSize (4byte)
    file size, the size of this picture is 0x00000206 = 518 (decimal) byte.
  • bfReserved1 and bfReserved2 (each 2byte)
    reserved items, must be set to 0.
  • bfOffBits (4byte)
    The byte offset from the beginning of the file to the specific image data, 0x00000076 = 118 (decimal), the specific file header (14byte) + bitmap information header (40byte) + palette (64byte) = 118byte.

Bitmap header

包含下列项
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;

  • biSize (4byte)
    the size of the bitmap information header, all pictures are 0x00000028 = 40 (decimal) byte.
  • biWidth (4byte)
    bitmap width, the unit is pixel, 0x00000019 = 25 (decimal).
  • biHeight; (4byte)
    bitmap height, in pixels, 0x00000019 = 25 (decimal).
  • The number of biPlanes (2byte)
    bitmap planes is set to 1, 0x0001.
  • biBitCount (2byte)
    color depth (bits), also known as bit depth, bit resolution, the unit is bpp (bit/pixel), which means to store the number of bits required for each pixel, the value is 1, 4, 8, 16,24,32 etc. This picture is 0x0004 = 4 bits.

  • Whether biCompression (4byte) is compressed, 0 (no compression), 1 (RLE 8, 8-bit RLE compression), 2 (RLE 4, 4-bit RLE compression, 3 (Bitfields, bitfield storage). 0x00000000 means no compression.
  • biSizeImage (4byte)
    image data part size, unit byte, the value is equal to the bitmap width size (unit byte, must be the nearest to the width byte number and a multiple of 4) × bitmap height, upper image bitmap width It is 16byte*25 height=400byte, which will be explained in detail below. 0x00000190 = 400, the picture analysis is consistent with the calculation.
  • biXPelsPerMeter (4byte)
    is the horizontal resolution expressed in pixels/meter, 0x00001EC2 = 7874 (decimal).
  • biYPelsPerMeter (4byte)
    is the horizontal resolution expressed in pixels/meter, 0x00001EC2 = 7874 (decimal).
  • biClrUsed (4byte)
    color index number in the palette, 0 means the picture has a palette.
  • biClrImportant (4byte) The
    number of the color index that is important to affect, 0 means all are important.

Color palette

The structure is arranged in the order of blue, green and red, and the last digit is reserved.
typedef struct tagRGBQUAD { BYTE rgbBlue; BYTE rgbGreen; BYTE rgbRed; BYTE rgbReserved; } RGBQUAD; The data in the palette is a group of 4 bytes each, representing blue, green, red and reserved value (hexadecimal) The number of colors in the picture is 2^4=16, and the number of bytes occupied is 16 * 4=64byte.





index blue green red Reserved value
0 00 00 00 00
1 11 11 11 00
2 22 22 22 00
00
14 EE EE EE 00
15 FF FF FF 00

In an 8-bit BMP picture, there are 2^8=256 colors of the picture, and the number of bytes occupied is 256*4=1024byte.

index blue green red Reserved value
0 00 00 00 00
1 01 01 01 00
2 02 02 02 00
3 03 03 03 00
00
254 FE FE FE 00
255 FF FF FF 00

Image data

With the index table of the palette, the index value will be used to represent the pixel in the image data. The actual width of the picture = the number of bytes required for the bitmap width to store each pixel%4? The bitmap width stores the number of bytes required for each pixel: the closest value to (bitmap width * the number of bytes required to store each pixel) is greater than its value.

Image data of 4-bit BMP pictures:

4-bit BMP image data
Each pixel is represented by 4 bits, FF means that the colors of the first two pixels are the colors with index number F in the palette (white); in turn, in FB, the color with index number B is gray; 00 means that both pixels are the color with index number 0 in the palette (black). A 4-bit picture requires 4/8 bytes per pixel, and the width of the picture is 25 pixels, it needs 12.5 bytes to represent, and the number of bytes per line must be an integer multiple of 4, the nearest number to 12.5 is 16 words Section, so each line needs 16 bytes of storage, and other values ​​are used to fill in the shortcomings. There are 25 lines of such data, and the size of all image data is 16*25=400byte. The 4-bit 24×24 is 12 bytes each, 33×33 is 16.5 bytes, and the completion is 20 bytes. The data size of the image is based on the value of biSizeImage and the insufficient bits at the end will be filled with 00.
In addition to analyzing the number of bytes per line, you can also directly use the following formula to calculate:
DataSizePerLine1=biBitCount * biWidth/32 * 4, the
actual number of bytes occupied by each scan line is calculated as:
DataSizePerLine2= (biWidth * biBitCount +31)/ 32 * 4;

Image data of 8-bit BMP pictures:

8-bit BMP image data
Each pixel is represented by 8bit, and FF indicates that the color of this pixel is the color of the 255th index number in the palette (white). CE indicates that the pixel is the color of the 206th index number (grey to white), and 03 indicates that the pixel is the color of the third index number (basically all black). This picture is 35×35 pixels, the actual number of bytes in each scan line is (35 * 8+31)/32*4 = 36byte, and the actual width of 37×37 is 40byte.

Image data of 24-bit BMP pictures:

24-bit true color BMP pictures, no palette, image data directly behind the file header and bitmap information header.
The picture below is a 14×14 24-bit BMP picture.
Insert picture description here
There is no palette in the picture, the width is 14 pixels, each pixel is represented by 3byte, the data size of each row is 14×3=42byte, which cannot be divisible by 4, and it is filled with 00 to 44byte, calculated by the formula ((14 * 24+31)/32 * 4 == 44). The three bytes respectively represent the values ​​of B, G, and R. The end of each line is filled with other values. When processing the image, pay attention to skip these invalid padding data. which is:

BGR BGR BGR BGR BGR BGR BGR BGR BGR BGR BGR BGR BGR BGR 00 00
BGR BGR BGR BGR BGR BGR BGR BGR BGR BGR BGR BGR BGR BGR BGR 00 00
……
BGR BGR BGR BGR BGR BGR BGR BGR BGR BGR BGR BGR BGR BGR BGR 00 00

Guess you like

Origin blog.csdn.net/sxtdzj/article/details/86232702