我一直在尝试让我的程序放大图像。我在为缩放图像分配新空间时遇到了一些问题,但我认为它已修复。我遇到的问题是,当我试图从临时内存持有人发回我的图像时,程序崩溃了。
加载的图像放置在我的struct Image. 像素位于 中
img->pixels,高度为img->height,宽度为img->width。但是我不知道为什么当我将像素从我的转移tmp2 struct到我的img struct时候程序会崩溃,而当我做相反的事情时它不会崩溃。这是代码:
void makeBigger(Image *img, int scale) {
Image *tmp2;
tmp2 = (Image*)malloc(sizeof(Image));
tmp2->height = img->height*scale;
tmp2->width = img->width*scale;
tmp2->pixels = (Pixel**)malloc(sizeof(Pixel*)*tmp2->height);
for (unsigned int i = 0; i < img->height; i++)
{
tmp2->pixels[i] = (Pixel*)malloc(sizeof(Pixel)*tmp2->width);
for (unsigned int j = 0; j < img->width; j++)
{
tmp2->pixels[i][j] = img->pixels[i][j];
}
}
free(img->pixels);
//scaling up the struct's height and width
img->height *= scale; …Run Code Online (Sandbox Code Playgroud)