【问题标题】:Problems setting unsigned int for bitmap为位图设置 unsigned int 的问题
【发布时间】:2013-05-29 09:10:00
【问题描述】:

这是我的位图信息标头的文件结构

// the info header to store reveleant bitmap data
struct InfoHeader
{
    unsigned int biSize;        // the bit size
    unsigned int width;     
    unsigned int height; 
    unsigned short biPlanes;    // number planes
    unsigned short biBitCount;  // number of bits for each pixel
    unsigned int biCompression; // compression used
    unsigned int size;  // size of image
    unsigned int biXPelsPerMeter;       // unit of measurement for x
    unsigned int biYPelsPerMeter;       // unit of measurement for y
    unsigned int biClrUsed; // number of colours
    unsigned int biClrImportant;
};

我相信它符合http://en.wikipedia.org/wiki/BMP_file_format上表示的结构

但是当我尝试通过

设置图像的大小时
bitmapInfoHeader.size = (width*height*3);

宽度和高度都是无符号整数,大小总是设置为 0。 我已经玩了很久了,有什么帮助吗?

编辑:宽度和高度都通过参数传递给方法(writeBitmapFile)并且都被初始化(我已经测试过了)

谢谢。

【问题讨论】:

  • 您使用 C 还是 C++ 作为编程语言?
  • 请重新标记您的问题...哪种编程语言等...像这样的 awnser 是:widthheight 为 0。
  • 不应该是bitmapInfoHeader.size = (bitmapInfoHeader.width*bitmapInfoHeader.height*3);吗?你有没有初始化过widthheight?如果是这样,你能构造一个SSCCE 来重现问题吗?
  • 对不起,伙计们,它是 C++,宽度和高度都通过参数传递给方法(是的,宽度和高度都有值) 括号只是为了澄清之前要进行的计算将其分配给 .size 值。并且@Dukeling 无论如何,宽度和高度值都已分配给 infoheader.width 和 infoheader.height 值,它们完全相同,所以没关系:)
  • 我修好了!结果发现 FileHeader 中的位对齐已关闭,需要使用 #pragma pack(2) 将 BitmapFileHeader 包含在内并使用 #pragma pack() 将其关闭,这确保了在读取二进制数据时结构中的短值正确对齐

标签: c++ bitmap size int


【解决方案1】:

由 OP 于 5 月 29 日关闭。他通过从 .BMP 文件(使用 fread 或等效文件)执行大量 slurp 来填充结构,并且他的结构字段在内存中的布局不匹配文件中字段的布局。这里没有什么令人兴奋的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-27
    • 2011-04-08
    • 2016-06-26
    • 2021-08-27
    • 1970-01-01
    • 2011-05-19
    • 2013-08-01
    • 1970-01-01
    相关资源
    最近更新 更多