Commits

pornel  committed 789580e

API change: Return whether gdImageTrueColorToPalette() succeeded

  • Participants
  • Parent commits 88031ae
  • Branches liq

Comments (0)

Files changed (2)

 
 	DIFFERENCES: gdImageCreatePaletteFromTrueColor creates and
 	returns a new image. gdImageTrueColorToPalette modifies
-	an existing image, and the truecolor pixels are discarded. */
+	an existing image, and the truecolor pixels are discarded.
+
+    gdImageTrueColorToPalette() returns TRUE on success, FALSE on failure.
+
+  */
 
 BGD_DECLARE(gdImagePtr) gdImageCreatePaletteFromTrueColor (gdImagePtr im, int ditherFlag,
 				  int colorsWanted);
 
-BGD_DECLARE(void) gdImageTrueColorToPalette (gdImagePtr im, int ditherFlag,
+BGD_DECLARE(int) gdImageTrueColorToPalette (gdImagePtr im, int ditherFlag,
 				  int colorsWanted);
 
-BGD_DECLARE(void) gdImageTrueColorToPalette (gdImagePtr im, int ditherFlag,
-          int colorsWanted);
-
 /*
   Selects quantization method used for subsequent gdImageTrueColorToPalette calls.
   See gdPaletteQuantizationMethod enum (e.g. GD_QUANT_NEUQUANT, GD_QUANT_LIQ).
   Speed is from 1 (highest quality) to 10 (fastest).
   Speed 0 selects method-specific default (recommended).
+
+  Returns FALSE if the given method is invalid or not available.
 */
-BGD_DECLARE(void) gdImageTrueColorToPaletteSetMethod (gdImagePtr im, int method, int speed);
+BGD_DECLARE(int) gdImageTrueColorToPaletteSetMethod (gdImagePtr im, int method, int speed);
 
 /*
   Chooses quality range that subsequent call to gdImageTrueColorToPalette will aim for.

File src/gd_topal.c

   See gdPaletteQuantizationMethod enum (e.g. GD_QUANT_NEUQUANT, GD_QUANT_LIQ).
   Speed is from 1 (highest quality) to 10 (fastest).
   Speed 0 selects method-specific default (recommended).
+
+  Returns FALSE if the given method is invalid or not available.
 */
-BGD_DECLARE(void) gdImageTrueColorToPaletteSetMethod (gdImagePtr im, int method, int speed)
+BGD_DECLARE(int) gdImageTrueColorToPaletteSetMethod (gdImagePtr im, int method, int speed)
 {
+  #ifndef HAVE_LIBIMAGEQUANT_H
+    if (method == GD_QUANT_LIQ)
+      {
+        return FALSE;
+      }
+  #endif
+
     if (method >= GD_QUANT_DEFAULT && method <= GD_QUANT_LIQ)
       {
         im->paletteQuantizationMethod = method;
           }
         im->paletteQuantizationSpeed = speed;
       }
+    return TRUE;
 }
 
 /*
       }
 }
 
-static void gdImageTrueColorToPaletteBody (gdImagePtr oim, int dither, int colorsWanted, gdImagePtr *cimP);
+static int gdImageTrueColorToPaletteBody (gdImagePtr oim, int dither, int colorsWanted, gdImagePtr *cimP);
 
 BGD_DECLARE(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;
 }
 
-BGD_DECLARE(void) gdImageTrueColorToPalette (gdImagePtr im, int dither, int colorsWanted)
+BGD_DECLARE(int) gdImageTrueColorToPalette (gdImagePtr im, int dither, int colorsWanted)
 {
-	gdImageTrueColorToPaletteBody(im, dither, colorsWanted, 0);
+	return gdImageTrueColorToPaletteBody(im, dither, colorsWanted, 0);
 }
 
 #ifdef HAVE_LIBIMAGEQUANT_H
  * 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;
       *cimP = nim;
       if (!nim)
         {
-          return;
+          return FALSE;
         }
     }
   else
           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
           gdImageCopy(oim, nim, 0, 0, 0, 0, oim->sx, oim->sy);
           gdImageDestroy(nim);
         }
-      return;
+      return TRUE;
     }
 
 
             {
               free_truecolor_image_data(oim);
             }
-          return;
+          return TRUE;
         }
     }
 #endif
     }
 
   /* Success! Get rid of the truecolor image data. */
+  conversionSucceeded = TRUE;
   if (!cimP)
     {
       free_truecolor_image_data(oim);
     }
 
-  goto success;
+  goto freeQuantizeData;
   /* Tediously free stuff. */
 outOfMemory:
+  conversionSucceeded = FALSE;
   if (oim->trueColor)
     {
       if (!cimP)
         }
     }
 
-success:
+freeQuantizeData:
   if (cquantize)
     {
       if (cquantize->histogram)
         }
       gdFree (cquantize);
     }
+
+  return conversionSucceeded;
 }
 
 #endif