BMP file structure analysis

xiaoxiao2021-03-06  19

BMP file structure

Document Description: Since the recent development needs to know the BMP structure, it is collected and learned the BMP structure. The following part is the document collected from the Internet and lists some of its own related code, only for yourself and others. Because this code is now no longer, there is no careful modification, and if there is time, it may consider refactoring.

One.

Introduction to Bitmap

BMP (Bitmap-file) graphic file is a graphic file format in Windows, and all image processing software running in a Windows environment supports BMP image file format. The internal image drawing operations in the Windows system are based on BMP. WINDOWS 3.0 The BMP diagram file format is related to the display device, so this BMP image file is called the device-related bitmap DDB (Device-Dependent Bitmap) file format. The BMP image file after Windows 3.0 has nothing to do with the display device, so this BMP image file format is called the device-free bitmap Dib (Device-Independent Bitmap) format (Note: After Windows 3.0, there is still a DDB bit in the system. Mage, like BitBLT () This function is based on the DDB bitmap. Just if you want to save images in the BMP format to disk files, Microsoft strongly recommends that you save in DIB format), the purpose is to make Windows Any type of display device displays the stored image. BMP bitmap file default file extension is BMP or BMP (sometimes it will also be extended in .dib or .rle).

The BMP image file format has the following four features:

(1)

Unlike most of the image file format, the scan line of the image is in the form of a bottom to top, not from top to bottom.

(2)

BMP file supports the stroke encoding compression algorithm, but is limited to two modes of 4 bits and 8-bit pixel values.

(3)

The scan line of the BMP file is specified at the end of each row, so that the length of the BMP file can be divided by 4.

two.

Image file format

The BMP file consists of four parts: file header, image control information, palette information (or mask information) and bitmap.

1.

Bitmap file header: contains information about file type, file size, storage location

The structure is as follows:

Typedef struct tagbitmapfileheader

{

Word bftype; // file type

DWORD bFSIZE; / / file number number

Word bfReServed1; // Reserved

Word bfreserved2; // retain

DWORD BFOFFBITS;

} BitmapfileHeader, * PbitmapfileHeader;

among them:

(1) Word bftype is used to represent file types. If it is a BMP file, then the value of this position must be "BM" is 0x4D42.

(2) DWORD BFSIZE: Number of bytes representing the entire file

(3) Word bfReserved1: retained

(4) Word bfreserved2: Reserved (5) DWORD BFOFFBITS: Indicates the data information of the bitmap from the offset of the file header, in bytes

2.

Image Control Information: Information including the size, compression type, and color format of bitmap files.

The structure is as follows:

Typedef struct tagbitmapinfoheader

{

DWORD Bisize; // Indicates the size of this structure

Long biwidth; // bit map width

Long biheight; // 位 位 位

Word biplanes; // is always 1, with MSDN explanation

// Specifies the Number of Planes for the Target Device. This value must be set to 1.

Word bibitcount; // bitmap bit score is 1 4 8 16 24 32

DWORD BICOMPRESSION;

DWORD BISIZEIMAGE; / / Indicates the size of the bitmap data area in bytes

Long Bixpelspermeter;

Long biypelspermeter;

DWORD BICLRUSED;

DWORD BICLRIMPORTANT;

} BitmapInfoHeader, * PbitmapInfoHeader;

among them:

(1) DWORD Bisize: Describes the number of words required for the BitmapInfoHeader structure. Note: This value is not necessarily the size of the BitmapInfoHeader structure, which may also be the value of SIZEOF (Bitmapv4Header) or the value of Sizeof (Bitmapv5Header). This is determined according to the format version of this bitmap file, but now, in the current situation, most BMP images are BitmapInfoHeader structure.

(2) long biwidth: Despect the width of the image, with pixels

(3) Long biheight; describes the height of the image, in pixels. Note: This value has another place in addition to the height of the image, is indicating that the image is a bitmap, or a positive bitmap. If the value is a positive, the image is reverse, and if the value is a negative number, the image is forwarded. Most BMP files are inverted bitmaps, that is, the height value is a positive number. (Note: When the height value is a negative number (forward image), the image will not be compressed (that is, the BiCompression member will not be BI_RLE8 or BI_RLE4).

(5) WORD BIPLANES; for the target device, the value of the position, the value will always be set to 1

(6) Word BibitCount; Description Bit number / pixels, its value is 1, 4, 8, 16, 24, or 32

(7) DWORD BICOMPRESSION; describes the type of image data compression. Its value can be one of the following:

a.

BI_RGB: No compression

b.

BI_RLE8: Each pixel 8 bit RLE compression encoding, compressed format consists of 2 bytes (repeated pixel count and color index);

C.

BI_RLE4: Each pixel 4 bit of RLE compression encoding, compressed format consists of 2 bytes

d.

Bi_bitfields: The bit of each pixel is determined by the specified mask.

(8) DWORD BisizeImage; Despect the size of the image, in bytes. When using the BI_RGB format, it can be set to 0

(9) Long Bixpelspermeter; Description Horizontal resolution, indicate (10) long biypelspermeter; description vertical resolution, indicate pixels / m

(11) DWORD BICLRUSED; Describe all the color palette items in the colorful table in the color table actually used by the bitmap,

(12) DWORD BICLRIMPORTANT; indicates the number of color indexes that have important impact on image display, if it is 0, it is important.

3.

Palette or mask

If the 8-bit graph is stored, the palette is stored; 16 and 32-bit bitmap store the RGB color mask, which is stored in DWORD size.

The RGBQUAD structure describes the color of the relative intensity of R, G, and is defined as follows:

Typedef struct tagrgbquad

{

BYTE RGBBLUE;

BYTE RGBGREEN;

BYTE RGBRED;

BYTE RGBRESERVED;

} RGBQUAD;

among them:

RGBBLUE: Specify blue intensity

RGBGreen: Specify green intensity

RGBRED: Specify red intensity

RGBRESERVED: Reserved, set to 0

4.

Bitmap data

Each scan of the image consists of a continuous byte indicating the image pixel, and the number of bytes of each row depends on the number of images of the image and the image width represented by the pixel. The scanning is stored by the bottom, which means that the first byte in the array represents the pixel in the lower left corner of the bitmap, and the last byte represents the pixels in the upper right corner of the bit. (Only for DIB, if it is forward DIB, the scanning is stored in the top, and the origin of the reverse DIB is in the lower left corner of the image, and the origin of the DIB is in the upper left corner of the image. At the same time, each scanned byte number must be 4 integrity, that is, DWORD alignment.

The following is a brief description of various bitmap formats, as well as a simple row solution function. For the function, you will probably explain:

The parameters of each bitmap format are consistent:

Vector & row_color_VEC Decoding each pixel value of the line saved

Uint row needs to decode bitmap row line

INT32 WIDTH bitmap width

In addition, it is to be noted that the interfaces and platforms related to the files called in the following functions are related to the purpose of the list of functions, mainly to explain how 4 bytes aligned and corresponding bitmark decoding.

Note: The role of getoffbits (file_counter) is to get the start position of bitmap point array data.

three.

1bit graph

This bitmap format has two colors, which is black and white by default, you can also define these two colors. There will be two palette items in the palette, called index 0 and index 1. Each bit in the image data point represents a pixel. If a bit is 0, the RGB value of the index 0 is displayed, and if the bit is 1, the RGB value of the index 1 is used.

Boolean BmpfileDecoder :: Decoderowby1bit (Vector & row_color_vec, uint row, int32 width)

{

Uint32 bytes_read = 0;

RGBQUAD QUAD [2];

Uint32 file_counter = 0;

Getoffbits (file_counter);

File_counter - = (SIZEOF (RGBQUAD) * 2);

// read RGB Quadif (! m_gw_filemanage-> readfile (m_folder_type, m_filename, (void huge *) quad, file_counter, sizeof (rgbquad) * 2, bytes_read, m_device_id)))

Return False;

// read row

Getoffbits (file_counter);

Int rowbytes = (width 7) / 8;

Rowbytes = (Rowbytes 3) / 4;

Rowbytes * = 4;

FILE_COUNTER = (Row * Rowbytes);

BYTE * ROW_BUFFER = New byte [Rowbytes];

IF (row_buffer == null)

{

DELETE [] Row_Buffer;

Row_buffer = 0;

Return False;

}

IF (! m_gw_filemanage-> readfile (m_folder_type, m_filename, row_buffer, file_counter, rowbytes, bytes_read, m_device_id))

{

Return False;

}

// Add PELS

BYTE INDEX, B, G, R;

For (int m = 0, n = 0; n

{

INT offset = 7;

DO

{

INDEX = ((Row_Buffer [M] >> Offset & 0x01);

B = quad [index] .RGBBLUE;

G = quad [index] .rgbgreen

R = quad [index] .RGBRED;

Row_color_vec.pushback (Color (R, G, B));

N ;

OFFSET -;

WHILE ((n = 0));

}

DELETE [] Row_Buffer;

Row_buffer = 0;

Return True;

}

four.

4BIT bitmap

This bitmap format has a maximum of 16 colors. Each pixel is represented by 4 digits, and uses these 4 bits as a set of color tables to find the color of the pixel. For example, if the first byte in the bitmap is 0x1f, it indicates that there are two pixels, the color of the first pixel is found in the second entry of the color table, and the color of the second pixel is Find in the 16th entry of the color table. At this point, there will be 16 RGB items in the palette. Corresponds to index 0 to index 15.

Boolean BmpFileDecoder :: Decoderowby4bit (Vector & row_color_vec, uint row, int32 width)

{

Uint32 bytes_read = 0;

RGBQUAD QUAD [16];

Uint32 file_counter = 0;

Getoffbits (file_counter);

File_counter - = (Sizeof (rgbquad) * 16);

// read RGB quad

IF (! m_gw_filemanage-> readfile (m_folder_type, m_filename, (void huge *) quad, file_counter, sizeof (rgbquad) * 16, bytes_read, m_device_id))))

{

Return False;

}

// read row

Getoffbits (file_counter);

INT Rowbytes = (Width 1) / 2; Rowbytes = (Rowbytes 3) / 4;

Rowbytes * = 4;

FILE_COUNTER = (Row * Rowbytes);

BYTE * ROW_BUFFER = New byte [Rowbytes];

IF (row_buffer == null)

{

DELETE [] Row_Buffer;

Row_buffer = 0;

Return False;

}

IF (! m_gw_filemanage-> readfile (m_folder_type, m_filename, row_buffer, file_counter, rowbytes, bytes_read, m_device_id))

{

Return False;

}

// Add PELS

For (int m = 0, n = 0; n

{

BYTE INDEX = ((Row_Buffer [M] >> 4) & 0x0f);

BYTE B = quad [index] .rgbblue;

Byte g = quad [index] .rgbgreen

BYTE R = quad [index] .rgbred;

Row_color_vec.pushback (Color (R, G, B));

N ;

IF (n

{

Index = (Row_Buffer [M] & 0x0f);

B = quad [index] .RGBBLUE;

G = quad [index] .rgbgreen

R = quad [index] .RGBRED;

Row_color_vec.pushback (Color (R, G, B));

N ;

}

M ;

}

DELETE [] Row_Buffer;

Row_buffer = 0;

Return True;

}

Fives.

8bit graph

This bitmap format has up to 256 colors. Each pixel is represented by 8 bits, and uses this 8-bit as an entry for the color table to find the color of the pixel. For example, if the first byte in the bitmap is 0x1f, the color of this pixel is found in the colorful table. At this time, by default, 256 RGB items will be included in the palette, corresponding to index 0 to index 255.

Boolean BmpFileDecoder :: Decoderowby8bit (Vector & row_color_vec, uint row, int32 width)

{

Uint32 bytes_read = 0;

RGBQUAD QUAD [256];

Uint32 file_counter = 0;

Getoffbits (file_counter);

File_counter - = (SizeOf (RGBQUAD) * 256);

// read RGB quad

IF (! m_gw_filemanage-> readfile (m_folder_type, m_filename, (void huge *) quad, file_counter, sizeof (rgbquad) * 256, bytes_read, m_device_id)))

Return False;

// read row

Getoffbits (file_counter);

Int rowbytes = (width 3) / 4;

Rowbytes * = 4;

FILE_COUNTER = (Row * Rowbytes);

BYTE * ROW_BUFFER = New byte [Rowbytes];

IF (row_buffer == null) {

DELETE [] Row_Buffer;

Row_buffer = 0;

Return False;

}

IF (! m_gw_filemanage-> readfile (m_folder_type, m_filename, row_buffer, file_counter, rowbytes, bytes_read, m_device_id))

Return False;

// Add PELS

For (int m = 0; M

{

BYTE INDEX = ROW_BUFFER [M];

BYTE B = quad [index] .rgbblue;

Byte g = quad [index] .rgbgreen

BYTE R = quad [index] .rgbred;

Row_color_vec.pushback (Color (R, G, B));

}

DELETE [] Row_Buffer;

Row_buffer = 0;

Return True;

}

six.

16bit map

This bitmap format has up to 216 colors. Each of the pigments is represented by 16 bits (2 bytes). This format is called high color, or is called enhanced 16-bit color, or 64K color. It is more complicated. When the value of the BiCompression member is bi_rgb, it does not have a palette. In the 16-bit, the lowest 5 indicates the blue component, the middle of the middle indicates the green component, and the high 5 represents the red component, which takes up 15 bits, the highest reservation, set to 0. This format is also referred to as a 555 16-bit bitmap. If the value of BiCompression is bi_bitfields, then the situation is complicated, the first is that the original palette is occupied by three DWORD variables, called red, green, and blue mask. It is used to describe the position occupied by red, green and blue components in 16 bits. In Windows 95 (or 98), the system accepts two types of format: 555 and 565, under the 555 format, red, green, and blue mask are: 0x7c00, 0x03e0, 0x001F, and under 565 format They are: 0xF800, 0x07E0, 0x001F, respectively. After reading a pixel, you can use the mask "and" on the pixel value, thereby extracting the desired color components. In the NT system, there is no format limit, but it is only possible to overlap between the requirements mask. (Note: This format is used in trouble, but because its display effect is close to the true color, the image data is much smaller than the real color image, so it is more used for game software) .

Boolean BmpfileDecoder :: Decoderowby16bit (Vector & row_color_vec, uint row, int32 width)

{

Uint32 bytes_read = 0;

Int rowbytes = (width * 2 3) / 4;

Rowbytes * = 4;

// byte * row_buffer = new byte [rowbytes];

UINT16 * ROW_Buffer = new uint16 [rowbytes / 2];

IF (row_buffer == null)

{

DELETE [] Row_Buffer;

Row_buffer = 0;

Return False;

}

Uint32 file_counter = 0;

Getoffbits (file_counter); file_counter = (row * rowbytes);

// read row

IF (! m_gw_filemanage-> readfile (m_folder_type, m_filename, row_buffer, file_counter, rowbytes, bytes_read, m_device_id))

{

DELETE [] Row_Buffer;

Row_buffer = 0;

Return False;

}

// Add PELS

BicompressionType BicompressionType;

GetBicompressionType (BicompressionType);

IF (bi_rgb == bicompressionType)

{

// 555 Format

For (int N = 0; n

{

UINT16 RGB = ROW_BUFFER [N];

UINT16 TMP = 0;

// red

TMP = (RGB & 0x7C00) >> 7;

BYTE R = TMP;

// Green

TMP = (RGB & 0x03E0) >> 2;

BYTE G = TMP;

// blue

TMP = ((RGB & 0x001F) << 3);

BYTE B = TMP;

Row_color_vec.pushback (Color (R, G, B));

}

}

Else

{

DELETE [] Row_Buffer;

Row_buffer = 0;

Return False;

}

DELETE [] Row_Buffer;

Row_buffer = 0;

Return True;

}

Seven.

24bit bitmap

This bitmap format has a maximum of 224 colors. This bitmap does not have a palette (Bmicolors member size is 0), in the bit array, each byte represents a pixel, respectively corresponds to color R, G, B.

Boolean BmpFileDecoder :: Decoderowby24bit (Vector & row_color_vec, uint row, int32 width)

{

Int rowbytes = (width * 3 3) / 4;

Rowbytes * = 4;

BYTE * ROW_BUFFER = New byte [Rowbytes];

IF (row_buffer == null)

{

DELETE [] Row_Buffer;

Row_buffer = 0;

Return False;

}

Uint32 file_counter = 0;

Getoffbits (file_counter);

FILE_COUNTER = (Row * Rowbytes);

Uint32 bytes_read = 0;

// read row

IF (! m_gw_filemanage-> readfile (m_folder_type, m_filename, row_buffer, file_counter, rowbytes, bytes_read, m_device_id))

{

DELETE [] Row_Buffer;

Row_buffer = 0;

Return False;

}

// Add PELS

For (int m = 0, n = 0; m

{

Byte b = row_buffer [n];

Byte g = row_buffer [n 1]; byte r = row_buffer [n 2];

Row_color_vec.pushback (Color (R, G, B));

}

DELETE [] Row_Buffer;

Row_buffer = 0;

Return True;

}

Eight.

32bit bitmap

This bitmap format has up to 232 colors. The structure of this bitmap is very similar to the 16-bit bitmap structure. When the value of BiCompression is Bi_RGB, it does not have a palette. There are 24 bits in 32 bits to store RGB values, the order is: up to 8 reservations Red 8, green 8, blue 8 bits. This format is also a 888 32 bitmap. If the value of BiCompression is BI_BITFIELDS, the original palette will occupy three DWORD variables, become red, green, blue mask, which is used to describe red, green, and blue components in 32-bit positions. . In Windows 95 (OR 98), the system only accepts 888 format, that is, the value of three masks will only be: 0xff0000, 0xff00, 0xff. In the NT system, you only need to pay attention to the mass transfer between the mask.

Boolean BmpfileDecoder :: Decoderowby32bit (Vector & row_color_vec, uint row, int32 width)

{

Uint32 bytes_read = 0;

INT rowbytes = width * 4;

UINT32 * ROW_BUFFER = New uint32 [width];

IF (row_buffer == null)

{

DELETE [] Row_Buffer;

Row_buffer = 0;

Return False;

}

Uint32 file_counter = 0;

Getoffbits (file_counter);

FILE_COUNTER = (Row * Rowbytes);

// read row

IF (! m_gw_filemanage-> readfile (m_folder_type, m_filename, row_buffer, file_counter, rowbytes, bytes_read, m_device_id))

{

DELETE [] Row_Buffer;

Row_buffer = 0;

Return False;

}

// Add PELS

BicompressionType BicompressionType;

GetBicompressionType (BicompressionType);

IF (bi_rgb == bicompressionType)

{

For (int N = 0; n

{

UINT32 TMP = 0;

TMP = row_buffer [n] & 0x000000FF;

BYTE B = TMP;

TMP = row_buffer [n] & 0x0000FF00;

BYTE G = TMP >> 8;

TMP = row_buffer [n] & 0x00ff0000;

BYTE R = TMP >> 16;

Row_color_vec.pushback (Color (R, G, B));

}

}

Else

{

DELETE [] Row_Buffer;

Row_buffer = 0;

Return False;

}

DELETE [] Row_Buffer;

Row_buffer = 0;

Return True;}

转载请注明原文地址:https://www.9cbs.com/read-40301.html

New Post(0)