【问题标题】:BMP-format Where are 2 bytes?BMP 格式 2 个字节在哪里?
【发布时间】:2021-04-07 11:22:20
【问题描述】:

我有一张 .bmp 图片(24 位)。它很小(65x65),大小等于 12796 字节。 但是让我们计算:

  • 标头 54 字节

  • 像素为 65*65*3 字节

  • 每行填充 1*65 字节。


== 12794 哪里可以有 2 个额外的字节? 第二个问题:我是否使用正确的计算填充公式?

int padding = (4 - bmp.width % 4) % 4 * sizeof(PIXEL);

我使用这个结构:

#pragma pack(push, 1)
struct BMP {
    int8_t id0;
    int8_t id1;
    int32_t filesize;        
    int32_t reserved;     //int32  == 2 int16
    int32_t headersize;      
    int32_t infoSize;        
    int32_t width;           
    int32_t height;           
    int16_t biPlanes;       
    int16_t bits;            
    int32_t biCompression;   
    int32_t biSizeImage;     
    int32_t biXPelsPerMeter; 
    int32_t biYPelsPerMeter; 
    int32_t biClrUsed;       
    int32_t biClrImportant;  
};
#pragma pack(pop)
    
#pragma pack(push, 1)
struct PIXEL {
    char B;
    char G;
    char R;
};
#pragma pack(pop)

【问题讨论】:

  • 好吧,既然没有人有答案,我想确认它应该是 12794 字节。因为您的图像宽度为 65,每个像素为 24 字节,所以每行应为 65 * 3 = 195 字节,最接近的 4 倍数是 196,这意味着每行有一个字节的填充。最终大小为 54(标题)+ 196 * 65 = 12794。
  • en.wikipedia.org/wiki/BMP_file_format#/media/… 显示标题和图像数据之间的可选间隙,所以我会查看您在headersize 中读取的值(在图像File Offset to PixelArray 中)并确保它是 54 而不是56.
  • 另一种选择,前提是您自己制作,将特定颜色放在图像的四个角上,然后手动查看图像数据并找到它们。这应该可以让你找出你的 2 个额外字节隐藏在哪里。

标签: c++ bmp


【解决方案1】:

bmp.width像素 为单位。一个 BMP 行被填充到 4 个字节。要从像素到字节,请将其乘以 bitsPerPixel/8。然后将其填充到 4。

类似这样的:

int rowSizeInBytes = ((bmp.bits * bmp.width + 31) / 32) * 4;

那么,你不应该假设标题大小是 54,而是取自 bmp.headersize

假设标题大小为 54,对于 65x65 24 位图像,我们得到每行 196 字节 * 65 行 + 54 = 12794。所以这意味着您的文件末尾有 2 个额外字节。

【讨论】:

  • 是的。我的老师说,有时 bmp 文件会在最后创建 2 个额外的零值字节。他说这对于 BMP-reader-program 更方便。)我认为,它还将所有文件大小填充为 4,因为 12794 不能被 4 整除,但 12796 是这样。
  • 是的,可能是,但还要检查 bmp.headersize 是否真的包含 54。
  • 我已经检查过了。正好包含 54 个字节。
猜你喜欢
  • 2011-03-15
  • 1970-01-01
  • 2021-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
  • 1970-01-01
相关资源
最近更新 更多