Click the blue text to follow usThe structure of a BMP file is actually very simple, consisting of two structures + an optional color palette + bitmap data.The first structure is BITMAPFILEHEADER, and the second structure is BITMAPINFOHEADER. Then there is the optional color palette (an array of RGBQUAD). Finally, there is the bitmap data.
1
The First StructureHere we will first talk about the detailed structure of the bitmap file header: BITMAPFILEHEADER.First, let’s look at the code prototype:
typedef struct tagBITMAPFILEHEADER{ WORD bfType; DWORD bfSize; WORD bfReserved1; WORD bfReserved2; DWORD bfOffBits;} BITMAPFILEHEADER, *PBITMAPFILEHEADER;
Among them:bfType:The identifier of the file, the value must be 0x4D42 (which is the two characters ‘BM’, note that it is Little-Endian)bfSize:is the size of the entire filebfReserved1 and bfReserved2must be 0.bfOffBits: is the offset of the bitmap data in the file.
2
The Second StructureThe bitmap information header: BITMAPINFOHEADER.
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, *PBITMAPINFOHEADER;
biSize:The size of this BITMAPINFOHEADER structure. Must be 40 (bytes).biWidth:The width of the BMP bitmap.biHeight:The height of the BMP bitmap.It should be noted that BMP bitmap data can be stored in two ways, usually as “bottom-up”, meaning that the first row of BMP bitmap data is actually the last row of the image. The other way is “top-down”, where the first row of bitmap data is the first row of the image.If the bitmap is “top-down”, then the value of biHeight is negative, and the actual height of the bitmap is actually (-biHeight), which is its opposite.Only “bottom-up” bitmaps support compression. “Bottom-up” is currently the most common type of bitmap.When saving bitmap files in PhotoShop, you can set it to output “bottom-up” or “top-down”. “Top-down” is “reverse row order”.biPlanes:The “number of planes” of the bitmap. This value must be 1.biBitCount:The “color depth” of the bitmap. Indicates how many bits represent a pixel in the bitmap data.A color depth of 1 indicates monochrome (actually “two colors”, usually black and white, but can be any two colors).A value of 2 indicates four colors, 4 indicates 16 colors, and 8 indicates 256 colors.When indicating the number of colors within 256, this BMP file is actually an image that uses palette colors, so there is a color palette after the BITMAPINFOHEADER structure.The so-called color palette is actually an array of RGBQUAD structures. The number of elements is the number of colors.biCompression:The compression format of the bitmap, the value can be BI_RGB (uncompressed), BI_RLE8 (8-bit RLE compressed), BI_RLE4 (4-bit RLE compressed), BI_BITFIELDS (indicates that each pixel in the color table has 3 DWORD mask data to indicate the red, green, and blue components. 16-bit and 32-bit bitmaps can use this.), BI_JPEG (the bitmap data is actually in JPG format), BI_PNG (the bitmap data is actually in PNG format).
When the biCompression value is BI_RLE8, biBitCount must be 8, indicating that this is an 8-bit 256-color indexed bitmap (with 256 palettes), and the bitmap data is compressed according to the following algorithm:
· The bitmap data consists of elements made up of two bytes each, forming an array.
· Each element has the first byte representing the number of repeated pixels, and the second byte is the 8-bit color index of the palette.
· If the first byte of the element is 0, then the second byte has a special meaning:
0: This row has ended.
1: This bitmap has ended.
2: Set the position of the next pixel, with the next two unsigned bytes specifying the X and Y offsets of the next pixel. Note that these are unsigned bytes, meaning the offset can only go forward.
3 to 0xFF: Indicates the length of a segment of uncompressed data following it, which is the length of the consecutive 8-bit pixel color index data. However, the data must be 2-byte aligned. When the biCompression value is BI_RLE4, biBitCount must be 4, indicating that this is a 4-bit 16-color indexed bitmap (with 16 palettes).
The difference from BI_RLE8 is that for each element, the second byte represents the color index of two pixels. The first byte indicates the number of repetitions, and the second byte represents the color of the two pixels.
For example, if the color at palette index 0 is black, and index 1 is white, and the element value is 0x05,0x01, then it represents “black white black white black white black white black white” for a total of 10 pixels appearing in one row.
biSizeImage:The size of the bitmap data block. In bytes. If your bitmap is not compressed, this value can be 0.biXPelsPerMeter:Indicates the number of pixels per meter horizontally. Can be 0.biYPelsPerMeter:Indicates the number of pixels per meter vertically. Can be 0.biClrUsed:The number of colors actually used in the palette for the bitmap. If this value is 0, it indicates that the bitmap uses the entire palette. Only 8-bit and 8-bit indexed color bitmaps need to consider this value. For 16-bit and above bitmaps, this value is ignored. biClrImportant indicates the number of important colors. If it is 0, it means all colors are important. Usually, its value equals biClrUsed or is 0.If the bitmap is 8-bit or below, the palette data follows. Otherwise, there is no palette data.The palette is simply an array of RGBQUAD.Then comes the bitmap data. If the bitmap is uncompressed, then the storage of bitmap data is row by row, with each row being a pixel every XX bytes, depending on the bit depth of the bitmap (biBitCount), and each row is aligned to 4 bytes! The extra part is padded with 0. This must be noted.Therefore, the number of bytes occupied by each row should be calculated using the following formula: (where biBitCount refers to the bit depth of the bitmap, which is a member of BITMAPINFOHEADER)Number of bytes per row = ((image width – 1) * biBitCount / 32 + 1) * 4;If you are using VB to read the bitmap, it is written like this:Number of bytes per row = ((image width – 1) * biBitCount \ 32 + 1) * 4It should also be noted that for “bottom-up” bitmaps and “top-down” bitmaps, if the biHeight member of BITMAPINFOHEADER is greater than zero, then this bitmap is “bottom-up”; otherwise, it is “top-down”. In a “bottom-up” bitmap, the rows are arranged from bottom to top, i.e., reverse row order. In a “top-down” bitmap, the row order is from top to bottom.
3
Bitmap Color TableThe bitmap color table is relatively simple:
typedef struct tagRGBQUAD{ BYTE rgbBlue; BYTE rgbGreen; BYTE rgbRed; BYTE rgbReserved;} RGBQUAD;
Red, green, and blue colors, and then there is one reserved byte.Finally, there is the bitmap data, which will not be described here.
If you are over 18 years old and find learning 【C language】 too difficult? Want to try other programming languages? Then I recommend you learn Python, a free Python zero-based course worth 499 yuan is available for a limited time, limited to 10 places!
▲ Scan the QR code - Get it for free
Click to read the original text to learn more.