zoukankan      html  css  js  c++  java
  • 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.
    ", path);
            return -1;
        }
        
        //读取文件头信息
        read(fd, &f_header, sizeof(struct bmp_header));
        printf("type:0x%x(%c%c)
    ", f_header.type, f_header.type&0xff, (f_header.type>>8)&0xff);
        printf("total_size:%d
    ", f_header.total_size);
        printf("reserved1:%d
    ", f_header.reserved1);
        printf("reserved2:%d
    ", f_header.reserved2);
        printf("valid_offset:%d
    ", f_header.valid_offset);
    
        read(fd, &f_info, sizeof(struct bmp_info));
        printf("info_size:%d
    ", f_info.info_size);
        printf("bmp_%d
    ", abs(f_info.bmp_width));
        printf("bmp_height:%d
    ", abs(f_info.bmp_height));
        printf("planes:%d
    ", f_info.planes);
        printf("bitcount:%d
    ", f_info.bitcount);
    
        printf("compression:%d
    ", f_info.compression);
        printf("img_size:%d
    ", f_info.img_size);
        printf("x_pix_meter:%d
    ", f_info.x_pix_meter);
        printf("y_pix_meter:%d
    ", f_info.y_pix_meter);
        printf("color_used:%d
    ", f_info.color_used);
        printf("color_mportants:%d
    ", 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 !!!
    ");
            exit(-1);        
        } else {
            printf("read bmp file: %s
    ", 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_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

  • 相关阅读:
    在开发项目中有些常用的的实用代码(ps:平时看着无关紧要的,却很容易忘记)
    C#中Socket编程解决应用程序直接的通信
    关于textjs的tree带复选框的树
    extjs中第一次访问有效,第二次访问出现部分组件无法显示的,动态改变组件的label值的方法,ExtJs中组件最好少使用ID属性(推荐更多使用Name属性)
    ExtJS中动态设置TextField的readOnly属性
    Tomcat根目录下work文件夹的作用
    Numpy包简单介绍
    数字图像处理-傅立叶变换
    数字图像处理-边缘检测
    matplotlib的一些代码
  • 原文地址:https://www.cnblogs.com/vedic/p/13608566.html
Copyright © 2011-2022 走看看