Commits

pajoye  committed ef9f329

- #58, free neuquant and temp buffer on exit

  • Participants
  • Parent commits e018992
  • Branches GD-2.0

Comments (0)

Files changed (1)

File src/gd_nnquant.c

 	nn_quant *nnq = NULL;
 
 	int row;
-	unsigned char *rgba;
+	unsigned char *rgba = NULL;
 	gdImagePtr dst;
 
 	/* Default it to 3 */
 	 */
 	rgba = (unsigned char *) gdMalloc(gdImageSX(im) * gdImageSY(im) * 4);
 	if (!rgba) {
-		return NULL;
+		goto done;
 	}
+
 	d = rgba;
 	for (row = 0; row < gdImageSY(im); row++) {
 		int *p = im->tpixels[row];
 
 	nnq = (nn_quant *) gdMalloc(sizeof(nn_quant));
 	if (!nnq) {
-		return NULL;
+		goto done;
 	}
 
 	initnet(nnq, rgba, gdImageSY(im) * gdImageSX(im) * 4, sample_factor, newcolors);
 	inxbuild(nnq); 
 	/* remapping colormap to eliminate opaque tRNS-chunk entries... */
 	for (top_idx = newcolors-1, bot_idx = x = 0;  x < newcolors;  ++x) {
-		if (map[x][3] == 255) /* maxval */
+		if (map[x][3] == 255) { /* maxval */
 			remap[x] = top_idx--;
-		else
+		} else {
 			remap[x] = bot_idx++;
+		}
 	}
 	if (bot_idx != top_idx + 1) {
 		fprintf(stderr,
 				"  internal logic error: remapped bot_idx = %d, top_idx = %d\n",
 				bot_idx, top_idx);
 		fflush(stderr);
-		return NULL;
+		goto done;
 	}
 
 	dst = gdImageCreate(gdImageSX(im), gdImageSY(im));
 	if (!dst) {
-		return NULL;
+		goto done;
 	}
 
 	for (x = 0; x < newcolors; ++x) {
 					];
 		}
 	}
+
+done:
+	if (rgba) {
+		gdFree(rgba);
+	}
+
+	if (nnq) {
+		gdFree(nnq);
+	}
 	return dst;
 }