CS50 类(class) pset5 : scale bitmap in C

标签 c bitmap scale stretch cs50

我正在在线学习 CS50 类(class),我需要缩放位图。我可以水平拉伸(stretch)它,但给我带来麻烦的是如何垂直拉伸(stretch)它。我将图像的分辨率加倍,但拉伸(stretch)只发生在图像的下半部分,图像的上半部分是空白的。我已经尝试在 reddit 和此处搜索 fseek,但无法弄清楚为什么图像仅水平拉伸(stretch)。

这是我的部分代码:

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

    }
}

最佳答案

您需要按未缩放 宽度进行fseek 以从原始(未缩放)文件中倒回一行。就此而言,您还需要迭代原始维度。

另外,sizeof 可能没有按照您的意愿进行。它将产生 biWidth 变量本身的大小(大概是一个整数,所以可能是 32 位,所以 sizeof(int) 将产生 4)。删除 sizeof

关于CS50 类(class) pset5 : scale bitmap in C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22498399/

相关文章:

机器人 : scaling bitmap to fit imageview

python - 如何在不使用 matplotlib 缩放图像图中的图像的情况下重新缩放轴?

C 的 HTML 解析器库

c - 如何使用C语言的linux restart功能来关闭Linux(Ubuntu)系统?

c++ - 错误 : The term 'bootstrap-vcpkg.bat' is not recognized : While Install C and C++ libraries for Visual Studio 2017 on Win10

java - Android:BitmapFactory.decodeByteArray - 降低图像质量

objective-c - 什么会导致 CFSocket 自行关闭?

android - 如何使用 Canvas 创建一个巨大的白色位图?

c# - RenderTransform RotateTransform 模糊图像

python - 确定 Python 中特定数字的精度和小数位数