BMP图片头部信息

BMP图片是未经压缩的图片,其内容包括头信息和颜色有效信息

头信息包括下面几个部分:文件头,信息头,调色板

我们可以使用一个结构体来表示头部信息

/* 
 * 这句话的意思是地址采用1字节对齐 
 * 由于gcc默认是4字节对齐,而我们这里需要和bmp的头部信息一一对应,所以应该采用1字节对齐方式
 */
#pragma pack (1) 

typedef struct { 
    uint8_t bf_type[2]; // 2Bytes,必须为"BM",即0x424D 才是Windows位图文件
    uint32_t bf_size; // 4Bytes,整个BMP文件的大小
    uint16_t bf_reserved1; // 2Bytes,保留,为0
    uint16_t bf_reserved2; // 2Bytes,保留,为0
    uint32_t bf_off_bits; // 4Bytes,文件起始位置到图像像素数据的字节偏移量
} t_bmp_file_header;

typedef struct {
    uint32_t bi_size; // 4Bytes,INFOHEADER结构体大小,存在其他版本I NFOHEADER,用作区分
    uint32_t bi_width; // 4Bytes,图像宽度(以像素为单位)
    int32_t bi_height; // 4Bytes,图像高度,+:图像存储顺序为Bottom2Top,-:Top2Bottom
    uint16_t bi_planes; // 2Bytes,图像数据平面,BMP存储RGB数据,因此总为1
    uint16_t bi_bit_count; // 2Bytes,图像像素位数
    uint32_t bi_compression; // 4Bytes,0:不压缩,1:RLE8,2:RLE4
    uint32_t bi_image_size; // 4Bytes,4字节对齐的图像数据大小
    uint32_t bi_x_pels_per_meter; // 4Bytes,用象素/米表示的水平分辨率
    uint32_t bi_y_pels_per_meter; // 4Bytes,用象素/米表示的垂直分辨率
    uint32_t bi_clr_used; // 4Bytes,实际使用的调色板索引数,0:使用所有的调色板索引
    uint32_t bi_clr_important; // 4Bytes,重要的调色板索引数,0:所有的调色板索引都重要
} t_bmp_info_hearder;

typedef struct {
    uint8_t bp_blue; // 指定蓝色强度
    uint8_t bp_green; // 指定绿色强度
    uint8_t bp_red; // 指定红色强度
    uint8_t bp_reserved; //保留,设置为0
} t_bmp_pallet;

typedef struct {
    t_bmp_file_header bmp_file_header;
    t_bmp_info_hearder bmp_info_hearder;
    t_bmp_pallet bmp_pallet;
} t_bmp_header;

/* 取消地址对齐 */
#pragma pack ()

其中需要注意的是int32_t bi_height;

如果bi_height为正,那么图像数据从下到上扫描

如果bi_height为负,那么图像数据从上到下扫描

猜你喜欢

转载自blog.csdn.net/weixin_42462202/article/details/84889571