Blame SOURCES/rhbz1633602-quantize.patch

137bb0
diff -up ImageMagick-6.7.8-9/magick/quantize.c.quantize ImageMagick-6.7.8-9/magick/quantize.c
137bb0
--- ImageMagick-6.7.8-9/magick/quantize.c.quantize	2012-08-09 02:12:12.000000000 +0200
137bb0
+++ ImageMagick-6.7.8-9/magick/quantize.c	2018-10-23 14:16:48.484639782 +0200
137bb0
@@ -333,7 +333,9 @@ static MagickBooleanType
137bb0
   SetGrayscaleImage(Image *);
137bb0
 
137bb0
 static size_t
137bb0
-  DefineImageColormap(Image *,CubeInfo *,NodeInfo *);
137bb0
+  DefineImageColormap(Image *,CubeInfo *,NodeInfo *),
137bb0
+  QuantizeErrorFlatten(const Image *,const CubeInfo *,const NodeInfo *,
137bb0
+    const ssize_t,const size_t,MagickRealType *);
137bb0
 
137bb0
 static void
137bb0
   ClosestColor(const Image *,CubeInfo *,const NodeInfo *),
137bb0
@@ -2882,6 +2884,66 @@ MagickExport MagickBooleanType QuantizeI
137bb0
 %                                                                             %
137bb0
 %                                                                             %
137bb0
 %                                                                             %
137bb0
++   Q u a n t i z e E r r o r F l a t t e n                                   %
137bb0
+%                                                                             %
137bb0
+%                                                                             %
137bb0
+%                                                                             %
137bb0
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
137bb0
+%
137bb0
+%  QuantizeErrorFlatten() traverses the color cube and flattens the quantization
137bb0
+%  error into a sorted array.  This accelerates the color reduction process.
137bb0
+%
137bb0
+%  Contributed by Yoya.
137bb0
+%
137bb0
+%  The format of the QuantizeImages method is:
137bb0
+%
137bb0
+%      size_t QuantizeErrorFlatten(const Image *image,const CubeInfo *cube_info,
137bb0
+%        const NodeInfo *node_info,const ssize_t offset,const size_t extent,
137bb0
+%        MagickRealType *quantize_error)
137bb0
+%
137bb0
+%  A description of each parameter follows.
137bb0
+%
137bb0
+%    o image: the image.
137bb0
+%
137bb0
+%    o cube_info: A pointer to the Cube structure.
137bb0
+%
137bb0
+%    o node_info: pointer to node in color cube tree that is current pointer.
137bb0
+%
137bb0
+%    o offset: quantize error offset.
137bb0
+%
137bb0
+%    o extent: quantize error offset maximum limit.
137bb0
+%
137bb0
+%    o quantize_error: the quantization error vector.
137bb0
+%
137bb0
+*/
137bb0
+static size_t QuantizeErrorFlatten(const Image *image,const CubeInfo *cube_info,
137bb0
+  const NodeInfo *node_info,const ssize_t offset,const size_t extent,
137bb0
+  MagickRealType *quantize_error)
137bb0
+{
137bb0
+  register ssize_t
137bb0
+    i;
137bb0
+
137bb0
+  size_t
137bb0
+    n,
137bb0
+    number_children;
137bb0
+
137bb0
+  if (offset >= (ssize_t) extent)
137bb0
+    return(0);
137bb0
+  quantize_error[offset]=node_info->quantize_error;
137bb0
+  n=1;
137bb0
+  number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
137bb0
+  for (i=0; i < (ssize_t) number_children ; i++)
137bb0
+    if (node_info->child[i] != (NodeInfo *) NULL)
137bb0
+      n+=QuantizeErrorFlatten(image,cube_info,node_info->child[i],offset+n,
137bb0
+        extent,quantize_error);
137bb0
+  return(n);
137bb0
+}
137bb0
+ 
137bb0
+/*
137bb0
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
137bb0
+%                                                                             %
137bb0
+%                                                                             %
137bb0
+%                                                                             %
137bb0
 +   R e d u c e                                                               %
137bb0
 %                                                                             %
137bb0
 %                                                                             %
137bb0
@@ -2988,6 +3050,22 @@ static void Reduce(const Image *image,Cu
137bb0
 %    o cube_info: A pointer to the Cube structure.
137bb0
 %
137bb0
 */
137bb0
+
137bb0
+static int MagickRealTypeCompare(const void *error_p,const void *error_q)
137bb0
+{
137bb0
+  MagickRealType
137bb0
+    *p,
137bb0
+    *q;
137bb0
+
137bb0
+  p=(MagickRealType *) error_p;
137bb0
+  q=(MagickRealType *) error_q;
137bb0
+  if (*p > *q)
137bb0
+    return(1);
137bb0
+  if (fabs((double) (*q-*p)) <= MagickEpsilon)
137bb0
+    return(0);
137bb0
+  return(-1);
137bb0
+}
137bb0
+
137bb0
 static void ReduceImageColors(const Image *image,CubeInfo *cube_info)
137bb0
 {
137bb0
 #define ReduceImageTag  "Reduce/Image"
137bb0
@@ -3002,6 +3080,29 @@ static void ReduceImageColors(const Imag
137bb0
     span;
137bb0
 
137bb0
   cube_info->next_threshold=0.0;
137bb0
+  if (cube_info->colors > cube_info->maximum_colors)
137bb0
+    {
137bb0
+      MagickRealType
137bb0
+        *quantize_error;
137bb0
+
137bb0
+      /*
137bb0
+        Enable rapid reduction of the number of unique colors.
137bb0
+      */
137bb0
+      quantize_error=(MagickRealType *) AcquireQuantumMemory(cube_info->nodes,
137bb0
+        sizeof(*quantize_error));
137bb0
+      if (quantize_error != (MagickRealType *) NULL)
137bb0
+        {
137bb0
+          (void) QuantizeErrorFlatten(image,cube_info,cube_info->root,0,
137bb0
+            cube_info->nodes,quantize_error);
137bb0
+          qsort(quantize_error,cube_info->nodes,sizeof(MagickRealType),
137bb0
+            MagickRealTypeCompare);
137bb0
+          if (cube_info->nodes > (110*(cube_info->maximum_colors+1)/100))
137bb0
+            cube_info->next_threshold=quantize_error[cube_info->nodes-110*
137bb0
+              (cube_info->maximum_colors+1)/100];
137bb0
+          quantize_error=(MagickRealType *) RelinquishMagickMemory(
137bb0
+            quantize_error);
137bb0
+        }
137bb0
+  }
137bb0
   for (span=cube_info->colors; cube_info->colors > cube_info->maximum_colors; )
137bb0
   {
137bb0
     cube_info->pruning_threshold=cube_info->next_threshold;