]> granicus.if.org Git - imagemagick/blobdiff - MagickCore/quantize.c
(no commit message)
[imagemagick] / MagickCore / quantize.c
index 65a660c6991c5d8e5c0c6a644da0d280eb45e6c6..5cba535ff0c1ff23695d8259fa6d63e9e6d35ec3 100644 (file)
@@ -13,7 +13,7 @@
 %    MagickCore Methods to Reduce the Number of Unique Colors in an Image     %
 %                                                                             %
 %                           Software Design                                   %
-%                             John Cristy                                     %
+%                                Cristy                                       %
 %                              July 1992                                      %
 %                                                                             %
 %                                                                             %
@@ -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++;
           }
@@ -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++;
           }
@@ -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)
@@ -2767,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.
@@ -3128,7 +3130,8 @@ 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->depth > 3))
+  if ((cube_info->colors > cube_info->maximum_colors) && 
+      (cube_info->nodes > 128))
     {
       MagickRealType
         *quantize_error;
@@ -3145,7 +3148,7 @@ static void ReduceImageColors(const Image *image,CubeInfo *cube_info)
           qsort(quantize_error,cube_info->nodes,sizeof(MagickRealType),
             MagickRealTypeCompare);
           cube_info->next_threshold=quantize_error[MagickMax(cube_info->nodes-
-            cube_info->maximum_colors,0)];
+            110*(cube_info->maximum_colors+1)/100,0)];
           quantize_error=(MagickRealType *) RelinquishMagickMemory(
             quantize_error);
         }