From: Christoph M. Becker Date: Mon, 3 Oct 2016 09:38:22 +0000 (+0200) Subject: Change gdImageTrueColorToPalette() to return success/failure X-Git-Tag: php-7.2.0alpha1~1216 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ec591b555f6a902c429f51804cbfcb1c67f8333b;p=php Change gdImageTrueColorToPalette() to return success/failure We're porting the relevant changes from . We also check the return value in the PHP binding, and throw E_WARNING if the conversion failed. --- diff --git a/ext/gd/gd.c b/ext/gd/gd.c index f8569779ba..7d47978a4c 100644 --- a/ext/gd/gd.c +++ b/ext/gd/gd.c @@ -1554,9 +1554,12 @@ PHP_FUNCTION(imagetruecolortopalette) php_error_docref(NULL, E_WARNING, "Number of colors has to be greater than zero and no more than %d", INT_MAX); RETURN_FALSE; } - gdImageTrueColorToPalette(im, dither, (int)ncolors); - - RETURN_TRUE; + if (gdImageTrueColorToPalette(im, dither, (int)ncolors)) { + RETURN_TRUE; + } else { + php_error_docref(NULL, E_WARNING, "Couldn't convert to palette"); + RETURN_FALSE; + } } /* }}} */ @@ -4039,7 +4042,10 @@ static void _php_image_bw_convert(gdImagePtr im_org, gdIOCtx *out, int threshold } if (im_org->trueColor) { - gdImageTrueColorToPalette(im_org, 1, 256); + if (!gdImageTrueColorToPalette(im_org, 1, 256)) { + php_error_docref(NULL, E_WARNING, "Unable to convert to palette"); + return; + } } for (y = 0; y < dest_height; y++) { diff --git a/ext/gd/libgd/gd.h b/ext/gd/libgd/gd.h index 4b6f86e28c..123c23b7d9 100644 --- a/ext/gd/libgd/gd.h +++ b/ext/gd/libgd/gd.h @@ -554,7 +554,7 @@ void gdImageColorDeallocate(gdImagePtr im, int color); gdImagePtr gdImageCreatePaletteFromTrueColor (gdImagePtr im, int ditherFlag, int colorsWanted); -void gdImageTrueColorToPalette(gdImagePtr im, int ditherFlag, int colorsWanted); +int gdImageTrueColorToPalette(gdImagePtr im, int ditherFlag, int colorsWanted); int gdImagePaletteToTrueColor(gdImagePtr src); /* An attempt at getting the results of gdImageTrueColorToPalette diff --git a/ext/gd/libgd/gd_topal.c b/ext/gd/libgd/gd_topal.c index 9aebc9e1a4..5b84855656 100644 --- a/ext/gd/libgd/gd_topal.c +++ b/ext/gd/libgd/gd_topal.c @@ -1426,28 +1426,30 @@ zeroHistogram (hist3d histogram) } } -static void gdImageTrueColorToPaletteBody (gdImagePtr oim, int dither, int colorsWanted, gdImagePtr *cimP); +static int gdImageTrueColorToPaletteBody (gdImagePtr oim, int dither, int colorsWanted, gdImagePtr *cimP); gdImagePtr gdImageCreatePaletteFromTrueColor (gdImagePtr im, int dither, int colorsWanted) { gdImagePtr nim; - gdImageTrueColorToPaletteBody(im, dither, colorsWanted, &nim); - return nim; + if (TRUE == gdImageTrueColorToPaletteBody(im, dither, colorsWanted, &nim)) { + return nim; + } + return NULL; } -void gdImageTrueColorToPalette (gdImagePtr im, int dither, int colorsWanted) +int gdImageTrueColorToPalette (gdImagePtr im, int dither, int colorsWanted) { - gdImageTrueColorToPaletteBody(im, dither, colorsWanted, 0); + return gdImageTrueColorToPaletteBody(im, dither, colorsWanted, 0); } /* * Module initialization routine for 2-pass color quantization. */ -static void gdImageTrueColorToPaletteBody (gdImagePtr oim, int dither, int colorsWanted, gdImagePtr *cimP) +static int gdImageTrueColorToPaletteBody (gdImagePtr oim, int dither, int colorsWanted, gdImagePtr *cimP) { my_cquantize_ptr cquantize = NULL; - int i; + int i, conversionSucceeded=0; /* Allocate the JPEG palette-storage */ size_t arraysize; @@ -1457,7 +1459,7 @@ static void gdImageTrueColorToPaletteBody (gdImagePtr oim, int dither, int color nim = gdImageCreate(oim->sx, oim->sy); *cimP = nim; if (!nim) { - return; + return FALSE; } } else { nim = oim; @@ -1469,7 +1471,7 @@ static void gdImageTrueColorToPaletteBody (gdImagePtr oim, int dither, int color gdImageCopy(nim, oim, 0, 0, 0, 0, oim->sx, oim->sy); *cimP = nim; } - return; + return TRUE; } /* If we have a transparent color (the alphaless mode of transparency), we @@ -1602,6 +1604,7 @@ static void gdImageTrueColorToPaletteBody (gdImagePtr oim, int dither, int color } /* Success! Get rid of the truecolor image data. */ + conversionSucceeded = TRUE; if (!cimP) { oim->trueColor = 0; /* Junk the truecolor pixels */ @@ -1612,9 +1615,10 @@ static void gdImageTrueColorToPaletteBody (gdImagePtr oim, int dither, int color gdFree (oim->tpixels); oim->tpixels = 0; } - goto success; + goto freeQuantizeData; /* Tediously free stuff. */ outOfMemory: + conversionSucceeded = FALSE; if (oim->trueColor) { if (!cimP) { @@ -1636,7 +1640,7 @@ outOfMemory: *cimP = 0; } } -success: +freeQuantizeData: for (i = 0; i < HIST_C0_ELEMS; i++) { if (cquantize->histogram[i]) @@ -1660,6 +1664,7 @@ success: { gdFree (cquantize); } + return conversionSucceeded; }