我只想说我是C.Alright的新手,因此,我在圣诞节假期期间的任务是制作一个以各种方式操纵PNG图像的程序。我已经完成了大多数事情,但是在尝试编写应该放大图像的程序时遇到了问题。我已经尝试过了,但已经有些失望了。虽然我很确定这都是错误的...
void enlargeImage(Image plain, char *imageInput[])
{
Image tempImage;
Pixel** pixels;
int scale = 2;
pixels = malloc(plain.height * sizeof(Pixel*) *scale);
for (int i = 0; i < plain.height; i++)
{
pixels[i] = malloc(plain.width * sizeof(Pixel*) * scale);
}
tempImage.pixels = pixels;
tempImage.height = plain.height * scale; //Can I even do this?? Or is it completely wrong?
tempImage.width = plain.width * scale;
// I've tried a few variations of this code
for (int height = 0; height < plain.height; height++)
{
for (int width = 0; width < plain.width; width++)
{
tempImage.pixels[height][width] = plain.pixels[height][width];
}
}
writeImage(imageInput, &tempImage); //This is a function written by my teachers. This is also where I get an error. I'm suspecting it's because I've doubled the size of tempImage ??
free(tempImage.pixels);
}
如果有人可以帮助我,我将非常感激^^
谢谢!
最佳答案
1.分配应如下所示:
tempImage.height = plain.height * scale;
tempImage.width = plain.width * scale;
pixels = malloc(tempImage.height * sizeof(Pixel*));
if (pixels == NULL) return;
for (int i = 0; i < tempImage.height; i++)
{
pixels[i] = malloc(tempImage.width * sizeof(Pixel));
if (pixels[i] == NULL)
{
for (int j = 0; j < i; j++) free(pixels[j]);
free(pixels);
return;
}
}
tempImage.pixels = pixels;
要点是:
通过在分配之前计算
tempImage.height
和tempImage.width
来避免进行两次乘法对。尽管
sizeof(char)
定义为1,因此将其乘以无害,但似乎会造成混淆,并使程序阅读更困难。pixels[i]
的元素类型为Pixel
。因此,应乘以sizeof(Pixel)
而不是第二个sizeof(Pixel*)
中的malloc()
。为所有行分配内存。您的程序仅分配给行的前半部分。
应该检查
malloc()
的返回值,以避免取消引用NULL
(在malloc()
失败时从tempImage
返回)并调用未定义的行为。2.转换应如下所示:
for (int height = 0; height < tempImage.height; height++)
{
for (int width = 0; width < tempImage.width; width++)
{
tempImage.pixels[height][width] = plain.pixels[height / scale][width / scale];
}
}
要点是:
设置目标图像的所有像素的值(
malloc()
)。通过free(tempImage.pixels);
分配的缓冲区的初始值不确定,使用它们将调用未定义的行为。注意不要访问(读或写)数组范围之外的内容,否则将调用未定义的行为。
3.您要通过
free(tempImage.pixels);
释放行列表,但是应通过添加来释放每行的数据for (int i = 0; i < tempImage.height; i++)
{
free(tempImage.pixels[i]);
}
在
tempImage.pixels
行之前。请注意,
pixels
和free()
指向相同的数组,因此您不必(也不必)对两个都使用free()
:仅对其中一个使用writeImage
。4.不知道
writeImage
的实际签名,是否相信void enlargeImage(Image plain, char *imageInput[])
和
writeImage(imageInput, &tempImage);
看起来很奇怪您确定
char *imageInput
的第一个参数应该是指向字符的指针,而不是指向这样的字符的指针吗?