图像调整大小程序输出失真的图像

时间:2019-01-24 20:29:22

标签: c image-resizing

我正在使用c进行图像缩放。它应该将图片放大1到100之间的整数倍。我遇到的问题是输出图像最终是原始图像的变形版本,并且我很难确定导致它的原因。我尝试使用printf跟踪所有函数和变量,它们似乎都正常运行。发生的情况是,对于正方形中间有不同颜色正方形的正方形图像的输入,我得到了一个放大正方形(按比例)的输出,但是中间的正方形变成了一个矩形并移动到靠近图片的顶部。我的感觉是问题出在嵌套循环中。任何帮助表示赞赏。如果您想查看变形的快照,请告诉我。

谢谢!

代码如下:

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "bmp.h"

int main (int argc, char *argv[])
{
// ensure proper usage
 if (argc != 4)
{
    fprintf(stderr, "Usage: ./resize n infile outfile\n");
    return 1;
}

// check for number in first argument
for (int i = 0 , n = strlen(argv[1]); i < n; i++)
{
    int k = isdigit(argv[1][i]);
    if (k == 0)
    {
        fprintf(stderr, "Cannot use %s for scale. Please enter a valid value\n", argv[1]);
        return 1;
    }
}

//remember scale
int scale = atoi(argv[1]);

// check for value of first argument
if (scale > 100)
{
    fprintf(stderr, "%i is greater than 100\n", scale);
    return 1;
}

// remember filenames
char *infile = argv[2];
char *outfile = argv[3];


// open input file
FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
{
    fprintf(stderr, "Could not open %s.\n", infile);
    return 2;
}

// open output file
FILE *outptr = fopen(outfile, "w");
if (outptr == NULL)
{
    fclose(inptr);
    fprintf(stderr, "Could not create %s.\n", outfile);
    return 3;
}

// read infile's BITMAPFILEHEADER
BITMAPFILEHEADER bf;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);

// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);

// ensure infile is (likely) a 24-bit uncompressed BMP 4.0
if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
    bi.biBitCount != 24 || bi.biCompression != 0)
{
    fclose(outptr);
    fclose(inptr);
    fprintf(stderr, "Unsupported file format.\n");
    return 4;
}

// determine padding for scanlines
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

 // update header
BITMAPFILEHEADER outbf = bf;
BITMAPINFOHEADER outbi = bi;

//define new dimensions
outbi.biWidth = scale * bi.biWidth;
outbi.biHeight = scale * bi.biHeight;

//define new padding
int outpadding = (4 - (outbi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

//define new sizes
outbi.biSizeImage = ((sizeof(RGBTRIPLE) * outbi.biWidth) + outpadding) * abs(outbi.biHeight);
outbf.bfSize = outbi.biSizeImage + bf.bfOffBits;

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

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

// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    for (int h = 0; h < scale; h++)
    {

        // iterate over pixels in scanline
        for (int j = 0; j < bi.biWidth; j++)
        {
            // temporary storage
            RGBTRIPLE triple;

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

            // write pixel per scale
            for (int w = 0; w < scale; w++)
            {
                // write RGB triple to outfile
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }
        }

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

        // then add it back (to demonstrate how)
        for (int k = 0; k < outpadding; k++)
        {
            fputc(0x00, outptr);
        }
    }
}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// success
return 0;

}

1 个答案:

答案 0 :(得分:0)

您正在读取每条重复的水平线的输入,这就是正方形向上移动的原因。

应将其移出垂直刻度环。我建议您为一条扫描线分配内存,将整行读入缓冲区,然后从缓冲区写入。

// iterate over infile's scanlines

// TEMPORARY STORAGE
RGBTRIPLE *buff = malloc (bi.biWidth * sizeof(RGBTRIPLE));

for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    // THIS MOVED UP OUT OF THE LOOP
    // read RGB triple from infile
    fread(buff, sizeof(RGBTRIPLE), bi.biWidth, inptr);

    for (int h = 0; h < scale; h++)
    {
        // iterate over pixels in scanline
        for (int j = 0; j < bi.biWidth; j++)
        {
            // write pixel per scale
            for (int w = 0; w < scale; w++)
            {
                // write RGB triple to outfile - FROM BUFFER
                fwrite(&buff[j], sizeof(RGBTRIPLE), 1, outptr);
            }
        }

        // then add it back (to demonstrate how)
        for (int k = 0; k < outpadding; k++)
        {
            fputc(0x00, outptr);
        }
    }
    // MOVED DOWN OUT OF LOOP
    // skip over padding, if any
    fseek(inptr, padding, SEEK_CUR);
}
free(buff);

代码应检查fread的返回值,即读取的项目数。因为没有,所以您没有注意到没有更多的数据正在读取,并且您的RGBTRIPLE triple继续保持其先前读取的值。