我在哈佛大学CS50的第4周。我目前正在研究调整大小-因此将现有的BMP扩大一个特定的因子(介于1到100之间)。

我的代码可以准确编译并调整图像大小,但是在运行CS50的Check50时,它会在bfSize字段中指示意外错误,例如标头字段bfSize中应为0xae,而不是0x5a。

我是编程的新手,所以请原谅我任何愚蠢的问题。

这是我的代码的链接:https://raw.githubusercontent.com/me50/NinaIMW/cs50/problems/2019/x/resize/less/resize.c?token=ALYWHW7TNZRPFYTB73JSUCK5LBMXC

这是我测试代码时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/

10-12 17:32