gpt4 book ai didi

c - 图像大小调整程序输出扭曲的图像

转载 作者:太空宇宙 更新时间:2023-11-04 04:13:37 25 4
gpt4 key购买 nike

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

谢谢!

代码如下:

#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;

最佳答案

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

它应该被移出垂直缩放循环。我建议你为一个扫描线分配内存,将整行读入缓冲区,然后从缓冲区写入。

// 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 三元组 继续保持其先前读取的值。

关于c - 图像大小调整程序输出扭曲的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54354846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com