Util: Improve mImageLoadPNG memory cleanup

This commit is contained in:
Vicki Pfau 2023-04-17 22:31:03 -07:00
parent 80a8074608
commit 2c84689345
1 changed files with 17 additions and 19 deletions

View File

@ -113,6 +113,11 @@ static struct mImage* mImageLoadPNG(struct VFile* vf) {
unsigned height = png_get_image_height(png, info); unsigned height = png_get_image_height(png, info);
struct mImage* image = calloc(1, sizeof(*image)); struct mImage* image = calloc(1, sizeof(*image));
if (!image) {
PNGReadClose(png, info, end);
return NULL;
}
bool ok = true;
image->width = width; image->width = width;
image->height = height; image->height = height;
@ -124,10 +129,7 @@ static struct mImage* mImageLoadPNG(struct VFile* vf) {
image->depth = 4; image->depth = 4;
image->data = malloc(width * height * 4); image->data = malloc(width * height * 4);
if (!PNGReadPixels(png, info, image->data, width, height, width)) { if (!PNGReadPixels(png, info, image->data, width, height, width)) {
free(image->data); ok = false;
free(image);
PNGReadClose(png, info, end);
return NULL;
} }
break; break;
case 4: case 4:
@ -135,10 +137,7 @@ static struct mImage* mImageLoadPNG(struct VFile* vf) {
image->depth = 4; image->depth = 4;
image->data = malloc(width * height * 4); image->data = malloc(width * height * 4);
if (!PNGReadPixelsA(png, info, image->data, width, height, width)) { if (!PNGReadPixelsA(png, info, image->data, width, height, width)) {
free(image->data); ok = false;
free(image);
PNGReadClose(png, info, end);
return NULL;
} }
break; break;
case 1: case 1:
@ -151,7 +150,8 @@ static struct mImage* mImageLoadPNG(struct VFile* vf) {
int trnsCount = 0; int trnsCount = 0;
image->format = mCOLOR_PAL8; image->format = mCOLOR_PAL8;
if (png_get_PLTE(png, info, &palette, &count) == 0) { if (png_get_PLTE(png, info, &palette, &count) == 0) {
return NULL; ok = false;
break;
} }
if (count > 256) { if (count > 256) {
count = 256; count = 256;
@ -180,22 +180,20 @@ static struct mImage* mImageLoadPNG(struct VFile* vf) {
image->depth = 1; image->depth = 1;
image->data = malloc(width * height); image->data = malloc(width * height);
if (!PNGReadPixels8(png, info, image->data, width, height, width)) { if (!PNGReadPixels8(png, info, image->data, width, height, width)) {
if (image->palette) { ok = false;
free(image->palette);
}
free(image->data);
free(image);
PNGReadClose(png, info, end);
return NULL;
} }
break; break;
default: default:
// Not supported yet // Not supported yet
free(image); ok = false;
PNGReadClose(png, info, end); break;
return NULL;
} }
PNGReadClose(png, info, end); PNGReadClose(png, info, end);
if (!ok) {
mImageDestroy(image);
image = NULL;
}
return image; return image;
} }
#endif #endif