BMP格式解析

一、介绍

  BMP文件格式,又称为位图,是Windows系统中广泛使用的图像文件格式。
  BMP文件的数据分为四个部分:

  • bmp文件头(bmp file header):提供文件的格式、大小等信息
  • 位图信息头(bitmap information):提供图像数据的尺寸、位平面数、压缩方式、颜色索引等信息
  • 调色板(color palette):可选,如使用索引来表示图像,调色板就是索引与其对应的颜色的映射表
  • 位图数据(bitmap data):图像数据

 

二、代码示例

#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

struct bmp_header
{ 
    unsigned short type; //文件类型
    unsigned int total_size; //整个位图大小, 头部 + 图像数据 单位字节
    unsigned short reserved1;
    unsigned short reserved2;
    unsigned int valid_offset; //图像数据偏移量
} __attribute__((packed));

struct bmp_info  
{   
    unsigned int info_size; //该结构体大小, 固定40字节 
    unsigned int bmp_width; //图片宽度
    unsigned int bmp_height; //图片高度
    unsigned short planes;  //总是1
    unsigned short bitcount; //像素多少位表示  
    unsigned int compression;  // 0:BI_RGB 不压缩; ...... 
    unsigned int img_size; //图像有效数据大小, 单位字节
    int x_pix_meter; //水平分辨率, 像素/米 表示  
    int y_pix_meter;  //垂直分辨率, 像素/米 表示  
    unsigned int color_used; //位图实际使用彩色表中的颜色索引数,一般0
    unsigned int color_mportants; //color_used 上面使用的索引值重要数, 0表示都重要  
}__attribute__((packed));

int bmp_analyze(unsigned char *path)
{
    int fd = -1, i;
    struct bmp_header f_header;
    struct bmp_info  f_info;

    //打开bmp图片
    fd = open(path, O_RDONLY);
    if (fd < 0) {
        printf("open %s error.\n", path);
        return -1;
    }
    
    //读取文件头信息
    read(fd, &f_header, sizeof(struct bmp_header));
    printf("type:0x%x(%c%c)\n", f_header.type, f_header.type&0xff, (f_header.type>>8)&0xff);
    printf("total_size:%d\n", f_header.total_size);
    printf("reserved1:%d\n", f_header.reserved1);
    printf("reserved2:%d\n", f_header.reserved2);
    printf("valid_offset:%d\n", f_header.valid_offset);

    read(fd, &f_info, sizeof(struct bmp_info));
    printf("info_size:%d\n", f_info.info_size);
    printf("bmp_width:%d\n", abs(f_info.bmp_width));
    printf("bmp_height:%d\n", abs(f_info.bmp_height));
    printf("planes:%d\n", f_info.planes);
    printf("bitcount:%d\n", f_info.bitcount);

    printf("compression:%d\n", f_info.compression);
    printf("img_size:%d\n", f_info.img_size);
    printf("x_pix_meter:%d\n", f_info.x_pix_meter);
    printf("y_pix_meter:%d\n", f_info.y_pix_meter);
    printf("color_used:%d\n", f_info.color_used);
    printf("color_mportants:%d\n", f_info.color_mportants);

    //关闭打开的文件
    close(fd);

    return 0;
}

int main(int argc, char **argv)
{
    unsigned char *path = argv[1];

    if(path == NULL) {
        printf("invalid file path !!!\n");
        exit(-1);        
    } else {
        printf("read bmp file: %s\n", path);
    }
    
    bmp_analyze(path);

    return 0;
}

 

三、 效果

 ./a.out 24x32_3.bmp 
read bmp file: 24x32_3.bmp
type:0x4d42(BM)
total_size:3126
reserved1:0
reserved2:0
valid_offset:54
info_size:40
bmp_width:24
bmp_height:32
planes:1
bitcount:32
compression:0
img_size:3072
x_pix_meter:2834
y_pix_meter:2834
color_used:0
color_mportants:0

 

四、附件解析 

链接: https://files-cdn.cnblogs.com/files/vedic/24x32_3.bmp    

 

 

 注意格式是ARGB8888, 只不过小端存储(低字节放在低地址),  图片数据大小 24*32*4字节 = 3072,  加上 头部 14 + 40 = 3126 字节

后续会使用该BMP图片打水印在YUV上: https://www.cnblogs.com/vedic/p/13637747.html

posted @ 2020-09-03 16:58  Vedic  阅读(1622)  评论(0编辑  收藏  举报