]> granicus.if.org Git - php/commitdiff
Change gdImageTrueColorToPalette() to return success/failure
authorChristoph M. Becker <cmbecker69@gmx.de>
Mon, 3 Oct 2016 09:38:22 +0000 (11:38 +0200)
committerChristoph M. Becker <cmbecker69@gmx.de>
Mon, 3 Oct 2016 10:02:31 +0000 (12:02 +0200)
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.

ext/gd/gd.c
ext/gd/libgd/gd.h
ext/gd/libgd/gd_topal.c

index f8569779baaeb3392edd5ca68711bdbb7fbd819d..7d47978a4c39026b6c84be5da0e004a79e7ca283 100644 (file)
@@ -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++) {
index 4b6f86e28c33575ef83373f7e1cfa5761262ac8b..123c23b7d9730b6c456ea75f0905b10798b1a3fa 100644 (file)
@@ -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
index 9aebc9e1a4ee9e772beecd0788aa60d90ccc9e9b..5b848556561213a98b1528dd80681f4838e23eeb 100644 (file)
@@ -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;
 }