我正在参加哈佛 CS50 的第 4 周。我目前正在研究调整大小 - 即按特定因子(1 到 100 之间)放大现有 BMP。
我的代码准确地编译和调整图像大小,但是当运行 CS50 的 Check50 时,它指示 bfSize 字段中出现意外错误,例如头字段 bfSize 中应为 0xae,而不是 0x5a。
我对编程完全陌生,所以请原谅我提出任何愚蠢的问题。
这是我测试代码时 Check50 返回的结果:
:) resize.c and bmp.h exist.
:) resize.c compiles.
:) doesn't resize small.bmp when n is 1
:( resizes small.bmp correctly when n is 2
expected 0xae, not 0x5a in header field bfSize
:( resizes small.bmp correctly when n is 3
expected 0x132, not 0x5a in header field bfSize
:( resizes small.bmp correctly when n is 4
expected 0x1e6, not 0x5a in header field bfSize
:( resizes small.bmp correctly when n is 5
expected 0x306, not 0x5a in header field bfSize
:( resizes large.bmp correctly when n is 2
expected 0x6f6, not 0x1e6 in header field bfSize
:( resizes smiley.bmp correctly when n is 2
expected 0x336, not 0xf6 in header field bfSize
:( resizes smiley.bmp correctly when n is 3
expected 0x6f6, not 0xf6 in header field bfSize```
最佳答案
我没有small.bmp(所以我无法尝试),但我建议更改:
newBf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
至
newBf.bfSize = newBi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
您仍在使用旧的 bi
而不是 newBi
来计算 newBf.bfSize
。
关于c - 如何修复 bfSize 字段中的 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57445172/