BMP picture information data analysis example

The BMP (Bitmap) file format is also a bitmap

Next, I use a 1-bit picture to study the data format of the BMP file, find the corresponding information, beginners, please point out if there are any mistakes, thank you.

1. File information (14 bytes)

typedef struct tag_bitmap_file_header{
	unsigned short 		file_type;//文件类型,BMP 文件值固定为 0x4D42,存储为小端模式,转换成 ASCII 就是 “BM”
	unsigned int 		file_size;//整个文件的大小,以字节为单位
	unsigned short 		reserved1;//保留
	unsigned short 		reserved2;//保留
	unsigned int 		offset_bits;//位图数据在文件中的偏移值,等于 “文件信息+位图信息+调色板信息”
} __attribute__((packed)) bitmap_file_header;

The size of this structure is fixed at 14 bytes

A byte is equal to 8 bits of binary bits. Short occupies 2 bytes, int occupies 4 bytes, and char occupies 1. Find the position on my image data as follows:

The file size 0x50=80 bytes matches the picture, and the data location is 0x3E=62

2. Bitmap information (40 bytes)

typedef struct tag_bitmap_info_header {
	unsigned int 		bitmap_info_size;//位图信息的大小,固定为 40
	int 				bitmap_width;//位图宽度
	int 				bitmap_height;//位图高度,影响位图数据的存储方式。值为正数时,从位图数据的最后一行开始保存,也就是文件位图数据的第一行是图片数据的最后一行,这种存储方式支持压缩。值为负数时,文件位图数据的第一行就是图片数据的第一行
	unsigned short 		planes;//位图的位面数,固定为 1
	unsigned short 		image_depth;//位图的图像深度,(1,4,8,16,24,32)表示位图数据中,几个二进制位表示一个像素点,如 8 bits 表示,8 个二进制位表示一个像素点
	unsigned int 		compression;//位图压缩类型,必须是 0(BI_RGB 不压缩 ),  1(BI_RLE8 压缩类型 ),2(BI_RLE4压缩类型)
	unsigned int 		image_size;//位图的数据大小
	int 				x_pels_permeter;//指定位图目标设备的水平打印分辨率,表示水平方向每米的像素点数量,可以是 0
	int 				y_pels_permeter;//指定位图目标设备的垂直打印分辨率,表示垂直方向每米的像素点数量,可以是 0
	unsigned int 		color_used;//位图实际使用的颜色表中的颜色所有数,为0使用所有调色板项
	unsigned int 		color_important;
} __attribute__((packed)) bitmap_info_header;//重要的颜色数量,值通常等于 color_used,值为 0 时表示所有颜色都重要

 The size of this structure is fixed at 40 bytes

Size 0x28 = 40 Width 0x21 = 33 Height 0x02 = 2 Bit depth 0x01 = 1 Compatible (the yellow boxes at the end and beginning of the two lines are together)

3. Palette information (xx bytes)

typedef struct tag_bitmap_palette {
	unsigned char 		blue;//该颜色的蓝色分量
	unsigned char 		green;//该颜色的绿色分量
	unsigned char 		red;//该颜色的红色分量
	unsigned char 		reserved;//保留字节
} __attribute__((packed)) bitmap_palette;

The size of this structure is fixed at  4  bytes.

Optional, a palette is a mapping table of indices and their corresponding colors. When it is less than or equal to 8bit, it must have palette information, and the data stores the index value of the color value corresponding to the palette color. The bmp 1-bit data stores the index value of the palette, and the data stored in more than 8 bits.

This picture saves 2 colors, index 0 is #ffffff, index 1 is #000000

4. Bitmap data (xx bytes)

When a BMP file stores bitmap data, the image is scanned in the order of scanning from left to right within a row and from bottom to top between rows. Windows stipulates that in an image file, the number of bytes occupied by a scanning line of an image must be a multiple of 4 (that is, in word units), and if it is insufficient, fill it with 0

Color depth 1 bit, 8 pixels occupy 1 byte; (11111111)

Color depth 4 bits, 2 pixels occupy 1 byte; (1111) (0000)

8-bit color depth, 1 pixel occupies 1 byte; 

The color depth is 24 bits, and 1 pixel occupies 3 bytes; 

      For bitmaps such as BMP, the requirement is 4-byte alignment, that is, the number of bytes in each row must be an integer multiple of 4, so it is satisfied to align downwards with 4 bytes as the alignment unit, so the number of bytes in each row is: ( 8Bit = 1Byte)

  head rows_len = (((biWidth * biBitCount) / 8 + 3) / 4) * 4

 When the bit width of the BMP image is less than 8, multiple pixels only occupy 1Byte, so the number of bytes except 8 can be shifted, and because the number of bytes must be an integer multiple of 4, 3 * 8 needs to become 31, then the number of bytes in the line is :

ulong rows_len = (((biWidth * biBitCount)  + 31) / 32) * 4 或

ulong rows_len = ((biWidth*biBitCount + 31) >>5)<<2;

So scan the actual length of the row (including padding) rows_len = 8, start to find the data position:

 Starting from 62, the picture is scanned from bottom to top, so the red box is the data of the second line of the picture, the yellow box is the data of the first line, and the last two bytes are filled. (The color depth is 1-8. The data stored is the index of the palette, and the data stored beyond is the actual color data. Note that the order of the palette is bgr. For example, do not reverse the order when fetching data bgr)

Color depth is 1, width and height: 33*2 == 66 pixels, 1 pixel occupies 1 binary bit, take one line as an example:

00 00 ff ff 80 00 00 00

00000000 00000000 11111111 11111111 10000000 00000000 0000000 00000000

00000000 00000000 11111111 11111111 1 These 33 bits are data, corresponding to the palette index, so the picture is from white to black, as shown below:

Reference: https://blog.csdn.net/u012313335/article/details/80432155

https://blog.csdn.net/lk_luck/article/details/115122351

Guess you like

Origin blog.csdn.net/qq_40015157/article/details/127427732