无法正确调整 .bmp 图像的大小(CS50 pset 4,调整大小,不太舒服)
Posted
技术标签:
【中文标题】无法正确调整 .bmp 图像的大小(CS50 pset 4,调整大小,不太舒服)【英文标题】:Cannot resize .bmp images properly (CS50 pset 4, resize, less comfortable) 【发布时间】:2017-10-30 20:08:02 【问题描述】:我目前正在参加 CS50 课程,但我完全被调整大小问题所困扰(不太舒服)。任务是制作一个程序,该程序接受一个输入 .bmp 文件,将其缩放 n 次并将其写入一个输出 .bmp 文件。但是我的代码只是有点扭曲了输入图像并且根本没有改变大小。 这是为测试提供的 bmp(3x3 像素,只是在此处放大):https://imgur.com/1v55tjz 当我尝试调整它的大小时,它只是变成了这个(仍然是 3x3):https://imgur.com/JAKQMfY
这是调整大小的代码:
#include <stdio.h>
#include <stdlib.h>
#include "bmp.h"
int main(int argc, char *argv[])
// ensure proper usage
if (argc != 4)
fprintf(stderr, "Usage: ./copy infile outfile\n");
return 1;
// remember filenames
int n = atoi(argv[1]);
char *infile = argv[2];
char *outfile = argv[3];
// open input file
FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
fprintf(stderr, "Could not open %s.\n", infile);
return 2;
// open output file
FILE *outptr = fopen(outfile, "w");
if (outptr == NULL)
fclose(inptr);
fprintf(stderr, "Could not create %s.\n", outfile);
return 3;
// read infile's BITMAPFILEHEADER
BITMAPFILEHEADER bf;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
// ensure infile is (likely) a 24-bit uncompressed BMP 4.0
if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
bi.biBitCount != 24 || bi.biCompression != 0)
fclose(outptr);
fclose(inptr);
fprintf(stderr, "Unsupported file format.\n");
return 4;
// create file- and infoheader for output file
BITMAPFILEHEADER outBF = bf;
BITMAPINFOHEADER outBI = bi;
//Scale width and height by n
outBI.biWidth *= n;
outBI.biHeight *= n;
//Calculate padding for input and output file
int outPadding = (4 - (outBI.biWidth * sizeof(RGBTRIPLE) % 4) % 4);
int inPadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
//Calculate biSizeImage and bfSize for output file
outBI.biSizeImage = ((sizeof(RGBTRIPLE) * outBI.biWidth) + outPadding * abs(outBI.biHeight));
outBF.bfSize = outBI.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// write outfile's BITMAPFILEHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
//Instantiate two counters to keep track of whether we have reached the n - 1 times of the recopy method and to be able to fseek to the next line
int countIterations = 1;
int countPositionInFile = 0;
// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
countIterations = 1;
for(int j = 0; j < n; j++)
// iterate over pixels in scanline
for (int k = 0; k < bi.biWidth; k++)
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// resize horizontally by writing every pixel in row n times
for(int l = 0; l < n; l++)
// write RGB triple to outfile
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
// skip over padding, if any
fseek(inptr, inPadding, SEEK_CUR);
//Add padding for this line
for (int l = 0; l < outPadding; l++)
fputc(0x00, outptr);
if(countIterations == n)
countPositionInFile++;
int newPosition = (sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (countPositionInFile * (bi.biWidth + inPadding)));
// return the "cursor" to the start of the file
fseek(inptr, newPosition, SEEK_SET);
countIterations++;
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// success
return 0;
这是 bmp.h 文件:
/**
* BMP-related data types based on Microsoft's own.
*/
#include <stdint.h>
/**
* Common Data Types
*
* The data types in this section are essentially aliases for C/C++
* primitive data types.
*
* Adapted from https://msdn.microsoft.com/en-us/library/cc230309.aspx.
* See http://en.wikipedia.org/wiki/Stdint.h for more on stdint.h.
*/
typedef uint8_t BYTE;
typedef uint32_t DWORD;
typedef int32_t LONG;
typedef uint16_t WORD;
/**
* BITMAPFILEHEADER
*
* The BITMAPFILEHEADER structure contains information about the type, size,
* and layout of a file that contains a DIB [device-independent bitmap].
*
* Adapted from https://msdn.microsoft.com/en-us/library/dd183374(v=vs.85).aspx.
*/
typedef struct
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
__attribute__((__packed__))
BITMAPFILEHEADER;
/**
* BITMAPINFOHEADER
*
* The BITMAPINFOHEADER structure contains information about the
* dimensions and color format of a DIB [device-independent bitmap].
*
* Adapted from https://msdn.microsoft.com/en-us/library/dd183376(v=vs.85).aspx.
*/
typedef struct
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
__attribute__((__packed__))
BITMAPINFOHEADER;
/**
* RGBTRIPLE
*
* This structure describes a color consisting of relative intensities of
* red, green, and blue.
*
* Adapted from https://msdn.microsoft.com/en-us/library/dd162939(v=vs.85).aspx.
*/
typedef struct
BYTE rgbtBlue;
BYTE rgbtGreen;
BYTE rgbtRed;
__attribute__((__packed__))
RGBTRIPLE;
我希望有人对这项任务有经验并且可以提供帮助,我现在感觉很困,但也许我只是盯着它太久了。 提前致谢!
【问题讨论】:
我删除了之前关于行填充的评论,因为您似乎已经解决了这个问题。您的问题是调试问题,而不是关于 C 的问题。您可以将问题分解:第一步是制作新尺寸的图像,无论像素值如何。当你做对了,你明智地只有几个像素,所以你可以检查光栅生成的每个循环。 【参考方案1】:文件应该用二进制标志打开:
FILE *inptr = fopen(infile, "rb");
FILE *outptr = fopen(outfile, "wb");
您忽略了outBI
和outBF
,您正在将旧标题写入新文件:
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);//old header
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);//old header
这里使用了两种不同的方法来计算填充:
int outPadding = (4 - (outBI.biWidth * sizeof(RGBTRIPLE) % 4) % 4);
int inPadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
^ ^
他们不可能都是对的,除非是偶然的。在这种情况下,我们使用 24 位图,您可以将填充计算为
int outPadding = outBI.biWidth % 4;
int inPadding = bi.biWidth % 4;
这将匹配您使用 fseek(inptr, inPadding, SEEK_CUR);
和 for(...) fputc(0x00, outptr);
跳过填充的方式
我无法弄清楚这段代码:
int newPosition = (sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)
+ (countPositionInFile * (bi.biWidth + inPadding)));<==remove
fseek(inptr, newPosition, SEEK_SET);<==remove
应该是 (bi.biWidth * 3)
或者您可以删除它并使用 ftell
在开始读取该行之前保存位置,然后使用 fseek
回到那个位置。
位图高度应该从下到上读取,但在这种情况下它没有任何区别。
【讨论】:
非常感谢您抽出宝贵时间写出如此详尽的答案! :) 这一切都有很大帮助,我已经修复了它,所以它现在可以工作了。非常感谢!以上是关于无法正确调整 .bmp 图像的大小(CS50 pset 4,调整大小,不太舒服)的主要内容,如果未能解决你的问题,请参考以下文章