c - 我的程序存在问题,无法正确调整图像大小

标签 c cs50

该代码应该采用 3 个参数并输出按 1 到 100 之间的指定因子缩放的图像,目前我得到的图像与预期输出不匹配。

我想说问题出在循环中的某个地方,或者可能出在新的文件大小调整中。

即使系数设置为 1,我也会遇到这些问题。

// set new height and width of BMP
bi_New.biHeight = bi.biHeight * factor;
bi_New.biWidth = bi.biWidth * factor;

// calculate padding for old file and new file
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int padding_New = (4 - (bi_New.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

// set the file size for the new file
bf_New.bfSize = 54 + (bi_New.biWidth * sizeof(RGBTRIPLE) + padding_New) * abs(bi_New.biHeight);
bi_New.biSizeImage = bf_New.bfSize - 54;


// write outfile's BITMAPFILEHEADER
fwrite(&bf_New, sizeof(BITMAPFILEHEADER), 1, outptr);

// write outfile's BITMAPINFOHEADER
fwrite(&bi_New, sizeof(BITMAPINFOHEADER), 1, outptr);

// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
    {
    // iterate over pixels in scanline
    for (int j = 0; j < bi.biWidth; j++)
        {
        // intialise counter to print rows by amount of the factor
        int counter = 0;

        // while loop to keep continuing until factor is less than or equal to counter
        while (counter < factor)
            {
            // iterate over pixels in scanline
            for(int k = 0; k < bi.biWidth; k++)
                {
                // temporary storage
                RGBTRIPLE triple;

                // declare pixel counter
                int pixel_counter = 0;

                // read RGB triple from infile
                fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

                // write RGB triple to outfile and use a while loop to iterate the same pixel by factor times
                while (pixel_counter < factor)
                    {
                    fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                    pixel_counter++;
                    }
                }
            // add new padding
            for (int l = 0; l < padding_New; l++)
                {
                fputc(0x00, outptr);
                }

            // seek back to the beginning of row in input file, but not after iteration of printing
            if (counter < (factor - 1))
                {
                fseek(inptr, -(bi.biWidth * sizeof(RGBTRIPLE)), SEEK_CUR);
                }
            counter++;
            }
        }

    // skip over padding, if any
    fseek(inptr, padding, SEEK_CUR);
    }

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// success
return 0;
}

最佳答案

j 循环是罪魁祸首。程序将处理每条扫描线 width * Factor 次,而不是 factor 次。它为每条扫描线添加了额外的宽度迭代。

关于c - 我的程序存在问题,无法正确调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56782138/

相关文章:

c - 在 ANSI C 中使用指针替换字符

c - printf() 格式化为十六进制

c++ - 混合使用 C 和 C++ 代码会在 GCC 中产生意外行为

c - 从类型 't_result' 分配给类型 'int' 时,类型不兼容,为什么?

CS50 从 card.raw 恢复 jpg 图像

C - 存储到空指针错误,段错误

python - 从查询集创建 Django 表单

无法获取 '' 的值 i '' on the second ' 'FOR''

c - 如何在C中的二维数组中输入一个字符串?

Python/MySQL : write to database successful ONLY one time