我只想说我是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.heighttempImage.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行之前。
请注意,pixelsfree()指向相同的数组,因此您不必(也不必)对两个都使用free():仅对其中一个使用writeImage

4.不知道writeImage的实际签名,是否相信

void enlargeImage(Image plain, char *imageInput[])




writeImage(imageInput, &tempImage);


看起来很奇怪您确定char *imageInput的第一个参数应该是指向字符的指针,而不是指向这样的字符的指针吗?

09-26 22:15