我正在在线上 CS50 课程,我需要缩放位图。我可以水平拉伸它,但给我带来麻烦的是如何垂直拉伸它。我将图像的分辨率加倍,但拉伸仅发生在图像的下半部分,而图像的上半部分是空白的。我已经尝试在 reddit 和此处搜索 fseek,但无法弄清楚为什么图像仅水平拉伸。
这是我的代码的一部分:
n = 2; // scale up by factor 2
bi.biWidth = bi.biWidth * n; // double width
bi.biHeight = bi.biHeight * n; //double hight
//iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
for (int m = 0 ; m < n; m++) // repeat process n-times to copy lines vertically
{
// iterate over pixels in scanline
for (int j = 0; j < bi.biWidth; j++)
{
// temporary storage for RGB values to be copied
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// write RGB triple to outfile n-times to stretch horizontally
for (int k = 0; k < n; k++)
{
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
}
fseek(inptr, -sizeof(bi.biWidth), SEEK_CUR); // go back to the beginning of the line
}
}