CS50课程pset5:C中的缩放位图

时间:2014-03-19 06:50:21

标签: c bitmap scale stretch cs50

我在网上做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

    }
}

2 个答案:

答案 0 :(得分:1)

您需要通过未缩放的宽度来回退原始(未缩放)文件中的一行。就此而言,您也需要迭代原始维度。

另外,sizeof可能没有做你想要的。它将产生biWidth变量本身的大小(可能是一个整数,所以可能是32位,因此sizeof(int)将产生4)。删除sizeof

答案 1 :(得分:0)

真的需要,但我相信会有更优雅的解决方案

int oWidth = bi.biWidth; //store original width
int oHeight = abs(bi.biHeight); //store original height
bi.biWidth = bi.biWidth * n;
bi.biHeight = bi.biHeight * n;    
.
.
.
if (m < n-1)
{
    fseek(inptr, -(oWidth*3), SEEK_CUR);
}
相关问题