c - 如何修复 bfSize 字段中的 header

标签 c image-resizing cs50 bmp

我正在参加哈佛 CS50 的第 4 周。我目前正在研究调整大小 - 即按特定因子(1 到 100 之间)放大现有 BMP。

我的代码准确地编译和调整图像大小,但是当运行 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/

相关文章:

c - 是否可以实现不执行任何系统调用的 C 程序?

将 for 循环转换为 if

c# - 如何使用 imagemagick 调整图像大小?

ios - 调整图像大小但保留硬边

c - 安装C库后"cc1: error:/usr/local/include: not a directory"

c - 为什么将浮点值转换为 int 时值会发生变化

objective-c - 从Blender导出模型以在iOS App中使用

c++ - 定义 "library-safe"代码的概念

jquery - 使用 jQuery fileupload 裁剪图像

CS-50 pset4 恢复 : Why getting segmentation fault?