bmp文件格式与保存

BMP文件由三部分组成,分辨是文件头,DIM头和像素数据。具体格式如下:

基本介绍

1. 文件头 14个字节

signature: 为文件标志位,恒为0X4242

FileSize:是指整个文件的大小

REservedx:保留位恒为0

FileOffset to Pixel: 像素数据在图片中的位置

2. DIB头 40字节

DIB HEADER SIZE: DIB头大小这里为40字节

ImageWidth:图像宽度

ImageHeight:图像高度

Planes:平面数恒为1

Bit Per Pixel:每个像素bit数,例如RGB为24bit

Compression:是否压缩,不压缩则为0

imageSize:原始像素数据的大小

X Pixels per meter: 打印信息横向打印分辨率

Y pixels per meter: 打印信息纵向打印分辨率

colors in color Tabel:索引图像颜色数,非索引图像则为0

Important color Tabel count:重要颜色数量,这里设为0

3. 图像数据

对于非颜色索引图像,一般按照大端模式存储,即BGRA的顺序存储。

4. 其他

bmp图像可以采用索引颜色的存储方式,并可以进行压缩,本文不对此用法进行介绍。

更详细用法参考:音视频编解码--BMP格式 - 知乎 (zhihu.com)

代码

博主在这写了一个简单的bmpLib可以支持简单的bmp图像读写,(不支持压缩和颜色索引)。

blanklog/bmpLib: the simple Bmp Lib. It can read and write bmp Image, only support uncompression Image. (github.com)

 基本的头结构:

    struct BitMapFileHeader {
      uint16_t bfType=0x4d42;      // Fixed value,0x4d42
      uint32_t bfSize;             // File size in bytes, usually zero for uncompressed BMP
      uint16_t bfReserved1=0;        // Always zero
      uint16_t bfReserved2=0;        // Always zero
      uint32_t bfOffBits=54;          // File offset to the start of the pixel data
    };

    struct BitMapInfoHeader {
      uint32_t biSize=40;         // Size of this header in bytes, we only support 40
      int32_t biWidth;              // Image width in pixels
      int32_t biHeight;             // Image height in pixels
      uint16_t biPlanes=1;          // Always 1
      uint16_t biBitCount;          // Number of bits per pixel, we only support 8 and 24
      uint32_t biCompression=0;     // Compression type, 0 for uncompressed, usually zero
      uint32_t biSizeImage;       // Used only when biCompression is not 0, usually zero
      int32_t biXPelsPerMeter=1204; // Horizontal resolution in pixels per meter
      int32_t biYPelsPerMeter=1024; // Vertical resolution in pixels per meter
      uint32_t biClrUsed=0;         // Number of colors used
      uint32_t biClrImportant=0;    // Number of important colors
    };

二进制文件下载。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

blanklog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值