在这个作业中,我必须编辑过滤图像的代码。其余代码已经提供并且是正确的。我的代码可以编译,但图像没有改变,我不知道为什么。
我仍然对内存分配感到困惑。我不太了解 malloc 和 calloc 之间的区别,也不知道这里哪个更可取。我已经包含了注释掉的代码行,以表明我已经尝试了两种方法,结果相同。如果有人可以在这种情况下解释 malloc 和 calloc 及其用途,我将不胜感激!
// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE(*tmp)[width] = malloc(height * width * sizeof(RGBTRIPLE));
// RGBTRIPLE(*tmp)[width] = calloc(height, width * sizeof(RGBTRIPLE));
if (tmp == NULL)
{
return;
}
for (int i = 0; i < height; i++)
{
// Copy integers from old image into new image
for (int j = 0; j < width; j++)
{
tmp[i][j].rgbtRed = image[i][width - (j + 1)].rgbtRed;
tmp[i][j].rgbtGreen = image[i][width - (j + 1)].rgbtGreen;
tmp[i][j].rgbtBlue = image[i][width - (j + 1)].rgbtBlue;
}
}
image = tmp;
free(tmp);
return;
}