com php-src: Change gdImageTrueColorToPalette() to return success/failure: ext/gd/gd.c ext/gd/libgd/gd.h ext/gd/libgd/gd_topal.c

From: Date: Mon, 03 Oct 2016 10:02:31 +0000
Subject: com php-src: Change gdImageTrueColorToPalette() to return success/failure: ext/gd/gd.c ext/gd/libgd/gd.h ext/gd/libgd/gd_topal.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-94818@lists.php.net to get a copy of this message
Commit: ec591b555f6a902c429f51804cbfcb1c67f8333b Author: Christoph M. Becker <cmbecker69@gmx.de> Mon, 3 Oct 2016 11:38:22 +0200 Parents: 82a8e57f37b9f28357c9ab9fc3df88bafd46ef62 Branches: master Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=ec591b555f6a902c429f51804cbfcb1c67f8333b Log: Change gdImageTrueColorToPalette() to return success/failure We're porting the relevant changes from <https://github.com/libgd/libgd/commit/34a00a40>. We also check the return value in the PHP binding, and throw E_WARNING if the conversion failed. Changed paths: M ext/gd/gd.c M ext/gd/libgd/gd.h M ext/gd/libgd/gd_topal.c Diff: diff --git a/ext/gd/gd.c b/ext/gd/gd.c index f856977..7d47978 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 4b6f86e..123c23b 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 9aebc9e..5b84855 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; }

« previous php.cvs (#94818) next »