This article starts from the format of the bitmap file, gives a detailed format description, and has source code analysis. First, bitmap file structure
Bitmap file consists of three parts: file header bitmap information -bit image line data
1, bitmap file header. The bitmap file is primarily used to identify bitmap files. The following is the definition of a bitmap file header structure:
Typedef struct tagbitmapfileheader {// bmfh
Word bftype;
DWORD BFSIZE;
Word bfReserved1;
Word bfReserved2;
DWORD BFOFFBITS;
} BitmapfileHeader;
The bftype value should be "BM" (0x4D42), and the flag is a bitmap file. The value of BFSIZE is the size of the bitmap file. 2. The value recorded in the bitmap information is used to assign memory, set the palette information, read pixel values, and the like. The following is the definition of the bitmap information structure:
Typedef struct tagbitmapinfo {
Bitmapinfoheader bmiheader;
RGBQUAD BMICOLORS [1];
} BitmapInfo;
The visible bitmap information is also composed of two parts: bitmap information head color table
2.1 bitmap information head. The bitmap information header includes the number of bytes of single pixels and the formats describing the color, and further includes the width, height, the bit plane of the target device, the compression format of the image. The following is the definition of the bitmap information head structure:
Typedef struct tagbitmapinfoheader {// bmih
DWORD Bisize;
Long biwidth;
Long biheight;
Word biplanes;
Word Bibitcount
DWORD BICOMPRESSION;
DWORD BisizeImage;
Long Bixpelspermeter;
Long biypelspermeter;
DWORD BICLRUSED;
DWORD BICLRIMPORTANT;
} BitmapInfoHeader;
The following table is an explanation of each member of the structure:
Structural members Description Bisize Structure BitmapInfoHeader's byte, XIzeof (BitmapInfoHeader) * Biwidth with pixel-unit image width * Biheight with image length of pixel * BiPlanes target device bit number BIbitCount Each pixel bit * (1) BiCompression image compression format (this value is almost 0) BisizeImage (in terms of Bi_RGB compression method) BiXPELSPERMETER, in the size of the BiXPELSPERMELSPERMETER vertical direction The number of colors that actually used in the pixels of each meter BICLRUSED palette (2) BICLRIMPORTANT actual bitmap (3)
Description: * It is necessary to pay attention, because they are the variables that we often refer to when performing bitmap operations (1) For each pixel, there is a meaning: 0, used in JPEG format 1, Mono-picture, the palette contains two colors, that is, the black and white picture of us usually said 4,16-color Figure 8, 256 color map, usually a grayscale map 16,64k, generally no palette, image Each byte in the data represents a pixel, 5 or 6 bits represents a RGB component 24, 16m true color map, generally no palette, and each byte each byte in the image data indicates a pixel, each byte Indicates a RGB component 32, 4G true color, generally no palette, each 4 bytes represent a pixel, and a transparency is added to the RGBA mode (2) this value is usually 0. Indicates that all colors determined using BIBITCount, exceptions are the maximum number of colors that are smaller than the color depth of the color depth.
(3) This value is usually 0, indicating that all colors are required 2.2 color tables. The color table is typically provided for the image of the 16-bit, for the 16-bit and 16-bit image, since the RGB (a) color of the corresponding pixel is described directly in the position of the position of the position, so the palette is proposed. . For the 16-bit image, since only the palette index value recorded in its bit image, it is necessary to obtain the corresponding RGB (a) color according to this index to the palette. The role of color table is to create a palette.
The figure below is a simple schematic of a bitmap with a palette and a palette. Figure 1 Difference between palette and no palette bit map
The color table is composed of color menu, and the color entries are defined as follows:
TypedEf struct tagrgbquad {// rgbq
BYTE RGBBLUE;
BYTE RGBGREEN;
BYTE RGBRED;
BYTE RGBRESERVED;
} RGBQUAD;
The problem there is to note is that the color order in the RGBQUAD structure is BGR, not the usual RGB.
3, bitmap data. Finally, after bitmap file header, bitmap information head, bitmap color table, is the main part of bitmap: bitmap data. According to different bitmaps, the number of bytes occupied by bitmap data is also different. For example, for 8-bit bitmaps, each byte represents a pixel, for 16-bit bitmap, each two bytes represent one Pixel, for a 24-bit bitmap, each byte represents a pixel, for a 32-bit bitmap, and each four bytes represent one pixel. Second, the source code analysis // code gives only the final displayed part, here is the key // m_nState indicating that different open display BMP files void cpictureview :: Ondraw (CDC * PDC) {// Todo: add draw code For native data here cpicturedoc * pdoc = getDocument (); assert_valid (pdoc); if (m_nState == 1) // first mode, complete {cfile file; // denotes to open through the most basic read mode; // BMP file char * pcolor; int I, j; file.open (m_pname, cfile :: modeRead, null); ulonglong ulsize = file.getLongth (); char * pbuf = new char [ulsize]; // Define a biphoto map Buffer UINT UITEMP = file.read (PBUF, ULSIZE); // read bitmap data to buffer file.close (); long cx, cy;
DWORD BFOFFBITS; WORD BIBITCOUNT; BFOFFBITS = * (DWORD *) (PBUF 10); // Mobile 10 bits, remove DWORD BFOFFBITS; CX = * (long *) (PBUF 18); // Take out X, Y Direction pixel value, movement 18 and 22 (actual CY = * (long *) (PBUF 22); // Distance BFOFFBITS 40) BIBITCOTINT = * (Word *) (PBUF 28); // Continue to take BIBITCOUNT BYTE Byblue, Bygreen, Byred; if (Bibitcount == 24) // If it is true color 24-bit picture {char * ptemp; ptemp = PBUF 54; // 54, is the specific color value for each pixel for (J = CY-1; j> = 0; j -) {for (i = 0; i