【问题标题】:CS50 course pset5: scale bitmap in CCS50 课程 pset5:在 C 中缩放位图
【发布时间】:2014-03-19 06:50:21
【问题描述】:

我正在在线上 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

    }
}

【问题讨论】:

  • 将整行读入 RAM,操作 RAM 中的数据,然后将其从 RAM 中写回可能更简单。这样一来,您就不必再为寻找而烦恼了。
  • 有人问过here

标签: c bitmap scale stretch cs50


【解决方案1】:

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

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

【讨论】:

  • 不要使用sizeof
【解决方案2】:

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

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);
}

【讨论】:

  • 3 看起来很可疑。 ... 哦。它是 RGB 三元组的大小。那应该是#defined 常量或enum 常量。
猜你喜欢
  • 1970-01-01
  • 2017-01-08
  • 2016-11-15
  • 2022-11-24
  • 2019-07-14
  • 1970-01-01
  • 1970-01-01
  • 2021-07-31
  • 2020-10-04
相关资源
最近更新 更多