]> granicus.if.org Git - imagemagick/blobdiff - MagickCore/quantize.c
(no commit message)
[imagemagick] / MagickCore / quantize.c
index 83cc6aa18a8ba9bdfcb7e034d405061f9279e01f..5cba535ff0c1ff23695d8259fa6d63e9e6d35ec3 100644 (file)
 %    MagickCore Methods to Reduce the Number of Unique Colors in an Image     %
 %                                                                             %
 %                           Software Design                                   %
-%                             John Cristy                                     %
+%                                Cristy                                       %
 %                              July 1992                                      %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization      %
 %  dedicated to making software imaging solutions freely available.           %
 %                                                                             %
 %  You may not use this file except in compliance with the License.  You may  %
@@ -61,9 +61,8 @@
 %
 %  The algorithm maps this domain onto a tree in which each node
 %  represents a cube within that domain.  In the following discussion
-%  these cubes are defined by the coordinate of two opposite vertices:
-%  The vertex nearest the origin in RGB space and the vertex farthest from
-%  the origin.
+%  these cubes are defined by the coordinate of two opposite vertices (vertex
+%  nearest the origin in RGB space and the vertex farthest from the origin).
 %
 %  The tree's root node represents the entire domain, (0,0,0) through
 %  (Cmax,Cmax,Cmax).  Each lower level in the tree is generated by
@@ -676,8 +675,8 @@ static MagickBooleanType AssignImageColors(Image *image,CubeInfo *cube_info,
       q=image->colormap;
       for (i=0; i < (ssize_t) image->colors; i++)
       {
-        intensity=(double) ((double) GetPixelInfoIntensity(q) <
-          ((double) QuantumRange/2.0) ? 0 : QuantumRange);
+        intensity=(double) (GetPixelInfoLuma(q) < (QuantumRange/2.0) ? 0 :
+          QuantumRange);
         q->red=intensity;
         q->green=intensity;
         q->blue=intensity;
@@ -759,8 +758,6 @@ static inline void SetAssociatedAlpha(const Image *image,CubeInfo *cube_info)
 
   associate_alpha=image->alpha_trait == BlendPixelTrait ? MagickTrue :
     MagickFalse;
-  if (cube_info->quantize_info->colorspace == TransparentColorspace)
-    associate_alpha=MagickFalse;
   if ((cube_info->quantize_info->number_colors == 2) &&
       (cube_info->quantize_info->colorspace == GRAYColorspace))
     associate_alpha=MagickFalse;
@@ -869,9 +866,12 @@ static MagickBooleanType ClassifyImageColors(CubeInfo *cube_info,
             */
             node_info->child[id]=GetNodeInfo(cube_info,id,level,node_info);
             if (node_info->child[id] == (NodeInfo *) NULL)
-              (void) ThrowMagickException(exception,GetMagickModule(),
-                ResourceLimitError,"MemoryAllocationFailed","`%s'",
-                image->filename);
+              {
+                (void) ThrowMagickException(exception,GetMagickModule(),
+                  ResourceLimitError,"MemoryAllocationFailed","`%s'",
+                  image->filename);
+                continue;
+              }
             if (level == MaxTreeDepth)
               cube_info->colors++;
           }
@@ -884,8 +884,8 @@ static MagickBooleanType ClassifyImageColors(CubeInfo *cube_info,
         error.blue=QuantumScale*(pixel.blue-mid.blue);
         if (cube_info->associate_alpha != MagickFalse)
           error.alpha=QuantumScale*(pixel.alpha-mid.alpha);
-        node_info->quantize_error+=sqrt((double) (count*error.red*error.red+
-          count*error.green*error.green+count*error.blue*error.blue+count*
+        node_info->quantize_error+=count*sqrt((double) (error.red*error.red+
+          error.green*error.green+error.blue*error.blue+
           error.alpha*error.alpha));
         cube_info->root->quantize_error+=node_info->quantize_error;
         index--;
@@ -965,9 +965,12 @@ static MagickBooleanType ClassifyImageColors(CubeInfo *cube_info,
             */
             node_info->child[id]=GetNodeInfo(cube_info,id,level,node_info);
             if (node_info->child[id] == (NodeInfo *) NULL)
-              (void) ThrowMagickException(exception,GetMagickModule(),
-                ResourceLimitError,"MemoryAllocationFailed","%s",
-                image->filename);
+              {
+                (void) ThrowMagickException(exception,GetMagickModule(),
+                  ResourceLimitError,"MemoryAllocationFailed","%s",
+                  image->filename);
+                continue;
+              }
             if (level == cube_info->depth)
               cube_info->colors++;
           }
@@ -980,8 +983,8 @@ static MagickBooleanType ClassifyImageColors(CubeInfo *cube_info,
         error.blue=QuantumScale*(pixel.blue-mid.blue);
         if (cube_info->associate_alpha != MagickFalse)
           error.alpha=QuantumScale*(pixel.alpha-mid.alpha);
-        node_info->quantize_error+=sqrt((double) (count*error.red*error.red+
-          count*error.green*error.green+count*error.blue*error.blue+count*
+        node_info->quantize_error+=count*sqrt((double) (error.red*error.red+
+          error.green*error.green+error.blue*error.blue+
           error.alpha*error.alpha));
         cube_info->root->quantize_error+=node_info->quantize_error;
         index--;
@@ -1007,7 +1010,7 @@ static MagickBooleanType ClassifyImageColors(CubeInfo *cube_info,
   if ((cube_info->quantize_info->colorspace != UndefinedColorspace) &&
       (cube_info->quantize_info->colorspace != CMYKColorspace))
     (void) TransformImageColorspace((Image *) image,sRGBColorspace,exception);
-  return(MagickTrue);
+  return(y < (ssize_t) image->rows ? MagickFalse : MagickTrue);
 }
 \f
 /*
@@ -1649,9 +1652,6 @@ static MagickBooleanType FloydSteinbergDither(Image *image,CubeInfo *cube_info,
           MagickBooleanType
             proceed;
 
-#if defined(MAGICKCORE_OPENMP_SUPPORT)
-          #pragma omp critical (MagickCore_FloydSteinbergDither)
-#endif
           proceed=SetImageProgress(image,DitherImageTag,(MagickOffsetType) y,
             image->rows);
           if (proceed == MagickFalse)
@@ -1866,7 +1866,6 @@ static MagickBooleanType RiemersmaDither(Image *image,CacheView *image_view,
               break;
             node_info=node_info->child[id];
           }
-          node_info=node_info->parent;
           /*
             Find closest color among siblings and their children.
           */
@@ -2768,6 +2767,8 @@ MagickExport MagickBooleanType QuantizeImage(const QuantizeInfo *quantize_info,
         depth--;
       if ((image->alpha_trait == BlendPixelTrait) && (depth > 5))
         depth--;
+      if (IsImageGray(image,exception) != MagickFalse)
+        depth=MaxTreeDepth;
     }
   /*
     Initialize color cube.
@@ -2936,6 +2937,63 @@ MagickExport MagickBooleanType QuantizeImages(const QuantizeInfo *quantize_info,
 %                                                                             %
 %                                                                             %
 %                                                                             %
++   Q u a n t i z e E r r o r F l a t t e n                                   %
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+%  QuantizeErrorFlatten() traverses the color cube and flattens the quantization
+%  error into a sorted 1D array.  This accelerates the color reduction process.
+%
+%  Contributed by Yoya.
+%
+%  The format of the QuantizeImages method is:
+%
+%      size_t QuantizeErrorFlatten(const Image *image,const CubeInfo *cube_info,
+%        const NodeInfo *node_info,const ssize_t offset,
+%        MagickRealType *quantize_error)
+%
+%  A description of each parameter follows.
+%
+%    o image: the image.
+%
+%    o cube_info: A pointer to the Cube structure.
+%
+%    o node_info: pointer to node in color cube tree that is current pointer.
+%
+%    o offset: quantize error offset.
+%
+%    o quantize_error: the quantization error vector.
+%
+*/
+static size_t QuantizeErrorFlatten(const Image *image,const CubeInfo *cube_info,
+  const NodeInfo *node_info,const ssize_t offset,MagickRealType *quantize_error)
+{
+  register ssize_t
+    i;
+
+  size_t
+    n,
+    number_children;
+
+  if (offset >= (ssize_t) cube_info->nodes)
+    return(0);
+  quantize_error[offset]=node_info->quantize_error;
+  n=1;
+  number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
+  for (i=0; i < (ssize_t) number_children ; i++)
+    if (node_info->child[i] != (NodeInfo *) NULL)
+      n+=QuantizeErrorFlatten(image,cube_info,node_info->child[i],offset+n,
+        quantize_error);
+  return(n);
+}
+\f
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%                                                                             %
+%                                                                             %
+%                                                                             %
 +   R e d u c e                                                               %
 %                                                                             %
 %                                                                             %
@@ -3042,6 +3100,22 @@ static void Reduce(const Image *image,CubeInfo *cube_info,
 %    o cube_info: A pointer to the Cube structure.
 %
 */
+
+static int MagickRealTypeCompare(const void *error_p,const void *error_q)
+{
+  MagickRealType
+    *p,
+    *q;
+
+  p=(MagickRealType *) error_p;
+  q=(MagickRealType *) error_q;
+  if (*p > *q)
+    return(1);
+  if (fabs((double) (*q-*p)) <= MagickEpsilon)
+    return(0);
+  return(-1);
+}
+
 static void ReduceImageColors(const Image *image,CubeInfo *cube_info)
 {
 #define ReduceImageTag  "Reduce/Image"
@@ -3056,6 +3130,29 @@ static void ReduceImageColors(const Image *image,CubeInfo *cube_info)
     span;
 
   cube_info->next_threshold=0.0;
+  if ((cube_info->colors > cube_info->maximum_colors) && 
+      (cube_info->nodes > 128))
+    {
+      MagickRealType
+        *quantize_error;
+
+      /*
+        Enable rapid reduction of the number of unique colors.
+      */
+      quantize_error=(MagickRealType *) AcquireQuantumMemory(cube_info->nodes,
+        sizeof(*quantize_error));
+      if (quantize_error != (MagickRealType *) NULL)
+        {
+          (void) QuantizeErrorFlatten(image,cube_info,cube_info->root,0,
+            quantize_error);
+          qsort(quantize_error,cube_info->nodes,sizeof(MagickRealType),
+            MagickRealTypeCompare);
+          cube_info->next_threshold=quantize_error[MagickMax(cube_info->nodes-
+            110*(cube_info->maximum_colors+1)/100,0)];
+          quantize_error=(MagickRealType *) RelinquishMagickMemory(
+            quantize_error);
+        }
+  }
   for (span=cube_info->colors; cube_info->colors > cube_info->maximum_colors; )
   {
     cube_info->pruning_threshold=cube_info->next_threshold;