2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % H H IIIII SSSSS TTTTT OOO GGGG RRRR AAA M M %
7 % H H I SS T O O G R R A A MM MM %
8 % HHHHH I SSS T O O G GG RRRR AAAAA M M M %
9 % H H I SS T O O G G R R A A M M %
10 % H H IIIII SSSSS T OOO GGG R R A A M M %
13 % MagickCore Histogram Methods %
21 % Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization %
22 % dedicated to making software imaging solutions freely available. %
24 % You may not use this file except in compliance with the License. You may %
25 % obtain a copy of the License at %
27 % http://www.imagemagick.org/script/license.php %
29 % Unless required by applicable law or agreed to in writing, software %
30 % distributed under the License is distributed on an "AS IS" BASIS, %
31 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32 % See the License for the specific language governing permissions and %
33 % limitations under the License. %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/cache-view.h"
45 #include "MagickCore/color-private.h"
46 #include "MagickCore/enhance.h"
47 #include "MagickCore/exception.h"
48 #include "MagickCore/exception-private.h"
49 #include "MagickCore/hashmap.h"
50 #include "MagickCore/histogram.h"
51 #include "MagickCore/image.h"
52 #include "MagickCore/list.h"
53 #include "MagickCore/memory_.h"
54 #include "MagickCore/monitor-private.h"
55 #include "MagickCore/pixel-accessor.h"
56 #include "MagickCore/prepress.h"
57 #include "MagickCore/quantize.h"
58 #include "MagickCore/registry.h"
59 #include "MagickCore/semaphore.h"
60 #include "MagickCore/splay-tree.h"
61 #include "MagickCore/statistic.h"
62 #include "MagickCore/string_.h"
67 #define MaxTreeDepth 8
68 #define NodesInAList 1536
73 typedef struct _NodeInfo
97 typedef struct _CubeInfo
120 Forward declarations.
126 *GetNodeInfo(CubeInfo *,const size_t);
129 DestroyColorCube(const Image *,NodeInfo *);
132 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
136 + C l a s s i f y I m a g e C o l o r s %
140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142 % ClassifyImageColors() builds a populated CubeInfo tree for the specified
143 % image. The returned tree should be deallocated using DestroyCubeInfo()
144 % once it is no longer needed.
146 % The format of the ClassifyImageColors() method is:
148 % CubeInfo *ClassifyImageColors(const Image *image,
149 % ExceptionInfo *exception)
151 % A description of each parameter follows.
153 % o image: the image.
155 % o exception: return any errors or warnings in this structure.
159 static inline size_t ColorToNodeId(const Image *image,
160 const PixelInfo *pixel,size_t index)
166 ((ScaleQuantumToChar(ClampToQuantum(pixel->red)) >> index) & 0x01) |
167 ((ScaleQuantumToChar(ClampToQuantum(pixel->green)) >> index) & 0x01) << 1 |
168 ((ScaleQuantumToChar(ClampToQuantum(pixel->blue)) >> index) & 0x01) << 2);
169 if (image->alpha_trait == BlendPixelTrait)
170 id|=((ScaleQuantumToChar(ClampToQuantum(pixel->alpha)) >> index) &
175 static CubeInfo *ClassifyImageColors(const Image *image,
176 ExceptionInfo *exception)
178 #define EvaluateImageTag " Compute image colors... "
196 register const Quantum
212 Initialize color description tree.
214 assert(image != (const Image *) NULL);
215 assert(image->signature == MagickSignature);
216 if (image->debug != MagickFalse)
217 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
218 cube_info=GetCubeInfo();
219 if (cube_info == (CubeInfo *) NULL)
221 (void) ThrowMagickException(exception,GetMagickModule(),
222 ResourceLimitError,"MemoryAllocationFailed","'%s'",image->filename);
225 GetPixelInfo(image,&pixel);
226 GetPixelInfo(image,&target);
227 image_view=AcquireVirtualCacheView(image,exception);
228 for (y=0; y < (ssize_t) image->rows; y++)
230 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
231 if (p == (const Quantum *) NULL)
233 for (x=0; x < (ssize_t) image->columns; x++)
236 Start at the root and proceed level by level.
238 node_info=cube_info->root;
239 index=MaxTreeDepth-1;
240 for (level=1; level < MaxTreeDepth; level++)
242 GetPixelInfoPixel(image,p,&pixel);
243 id=ColorToNodeId(image,&pixel,index);
244 if (node_info->child[id] == (NodeInfo *) NULL)
246 node_info->child[id]=GetNodeInfo(cube_info,level);
247 if (node_info->child[id] == (NodeInfo *) NULL)
249 (void) ThrowMagickException(exception,GetMagickModule(),
250 ResourceLimitError,"MemoryAllocationFailed","'%s'",
255 node_info=node_info->child[id];
258 for (i=0; i < (ssize_t) node_info->number_unique; i++)
260 target=node_info->list[i];
261 if (IsPixelInfoEquivalent(&pixel,&target) != MagickFalse)
264 if (i < (ssize_t) node_info->number_unique)
265 node_info->list[i].count++;
268 if (node_info->number_unique == 0)
269 node_info->list=(PixelInfo *) AcquireMagickMemory(
270 sizeof(*node_info->list));
272 node_info->list=(PixelInfo *) ResizeQuantumMemory(node_info->list,
273 (size_t) (i+1),sizeof(*node_info->list));
274 if (node_info->list == (PixelInfo *) NULL)
276 (void) ThrowMagickException(exception,GetMagickModule(),
277 ResourceLimitError,"MemoryAllocationFailed","'%s'",
281 node_info->list[i]=pixel;
282 node_info->list[i].red=(double) GetPixelRed(image,p);
283 node_info->list[i].green=(double) GetPixelGreen(image,p);
284 node_info->list[i].blue=(double) GetPixelBlue(image,p);
285 if (image->colorspace == CMYKColorspace)
286 node_info->list[i].black=(double) GetPixelBlack(image,p);
287 node_info->list[i].alpha=(double) GetPixelAlpha(image,p);
288 node_info->list[i].count=1;
289 node_info->number_unique++;
292 p+=GetPixelChannels(image);
294 proceed=SetImageProgress(image,EvaluateImageTag,(MagickOffsetType) y,
296 if (proceed == MagickFalse)
299 image_view=DestroyCacheView(image_view);
304 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
308 + D e f i n e I m a g e H i s t o g r a m %
312 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
314 % DefineImageHistogram() traverses the color cube tree and notes each colormap
315 % entry. A colormap entry is any node in the color cube tree where the
316 % of unique colors is not zero.
318 % The format of the DefineImageHistogram method is:
320 % DefineImageHistogram(const Image *image,NodeInfo *node_info,
321 % PixelInfo **unique_colors)
323 % A description of each parameter follows.
325 % o image: the image.
327 % o node_info: the address of a structure of type NodeInfo which points to a
328 % node in the color cube tree that is to be pruned.
330 % o histogram: the image histogram.
333 static void DefineImageHistogram(const Image *image,NodeInfo *node_info,
334 PixelInfo **histogram)
343 Traverse any children.
345 number_children=image->alpha_trait != BlendPixelTrait ? 8UL : 16UL;
346 for (i=0; i < (ssize_t) number_children; i++)
347 if (node_info->child[i] != (NodeInfo *) NULL)
348 DefineImageHistogram(image,node_info->child[i],histogram);
349 if (node_info->level == (MaxTreeDepth-1))
355 for (i=0; i < (ssize_t) node_info->number_unique; i++)
365 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
369 + D e s t r o y C u b e I n f o %
373 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
375 % DestroyCubeInfo() deallocates memory associated with a CubeInfo structure.
377 % The format of the DestroyCubeInfo method is:
379 % DestroyCubeInfo(const Image *image,CubeInfo *cube_info)
381 % A description of each parameter follows:
383 % o image: the image.
385 % o cube_info: the address of a structure of type CubeInfo.
388 static CubeInfo *DestroyCubeInfo(const Image *image,CubeInfo *cube_info)
394 Release color cube tree storage.
396 DestroyColorCube(image,cube_info->root);
399 nodes=cube_info->node_queue->next;
400 cube_info->node_queue=(Nodes *)
401 RelinquishMagickMemory(cube_info->node_queue);
402 cube_info->node_queue=nodes;
403 } while (cube_info->node_queue != (Nodes *) NULL);
404 return((CubeInfo *) RelinquishMagickMemory(cube_info));
408 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
412 + D e s t r o y C o l o r C u b e %
416 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
418 % DestroyColorCube() traverses the color cube tree and frees the list of
421 % The format of the DestroyColorCube method is:
423 % void DestroyColorCube(const Image *image,const NodeInfo *node_info)
425 % A description of each parameter follows.
427 % o image: the image.
429 % o node_info: the address of a structure of type NodeInfo which points to a
430 % node in the color cube tree that is to be pruned.
433 static void DestroyColorCube(const Image *image,NodeInfo *node_info)
442 Traverse any children.
444 number_children=image->alpha_trait != BlendPixelTrait ? 8UL : 16UL;
445 for (i=0; i < (ssize_t) number_children; i++)
446 if (node_info->child[i] != (NodeInfo *) NULL)
447 DestroyColorCube(image,node_info->child[i]);
448 if (node_info->list != (PixelInfo *) NULL)
449 node_info->list=(PixelInfo *) RelinquishMagickMemory(node_info->list);
453 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
457 + G e t C u b e I n f o %
461 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
463 % GetCubeInfo() initializes the CubeInfo data structure.
465 % The format of the GetCubeInfo method is:
467 % cube_info=GetCubeInfo()
469 % A description of each parameter follows.
471 % o cube_info: A pointer to the Cube structure.
474 static CubeInfo *GetCubeInfo(void)
480 Initialize tree to describe color cube.
482 cube_info=(CubeInfo *) AcquireMagickMemory(sizeof(*cube_info));
483 if (cube_info == (CubeInfo *) NULL)
484 return((CubeInfo *) NULL);
485 (void) ResetMagickMemory(cube_info,0,sizeof(*cube_info));
487 Initialize root node.
489 cube_info->root=GetNodeInfo(cube_info,0);
490 if (cube_info->root == (NodeInfo *) NULL)
491 return((CubeInfo *) NULL);
496 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
500 % G e t I m a g e H i s t o g r a m %
504 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
506 % GetImageHistogram() returns the unique colors in an image.
508 % The format of the GetImageHistogram method is:
510 % size_t GetImageHistogram(const Image *image,
511 % size_t *number_colors,ExceptionInfo *exception)
513 % A description of each parameter follows.
515 % o image: the image.
517 % o file: Write a histogram of the color distribution to this file handle.
519 % o exception: return any errors or warnings in this structure.
522 MagickExport PixelInfo *GetImageHistogram(const Image *image,
523 size_t *number_colors,ExceptionInfo *exception)
532 histogram=(PixelInfo *) NULL;
533 cube_info=ClassifyImageColors(image,exception);
534 if (cube_info != (CubeInfo *) NULL)
536 histogram=(PixelInfo *) AcquireQuantumMemory((size_t) cube_info->colors,
538 if (histogram == (PixelInfo *) NULL)
539 (void) ThrowMagickException(exception,GetMagickModule(),
540 ResourceLimitError,"MemoryAllocationFailed","'%s'",image->filename);
546 *number_colors=cube_info->colors;
548 DefineImageHistogram(image,cube_info->root,&root);
551 cube_info=DestroyCubeInfo(image,cube_info);
556 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
560 + G e t N o d e I n f o %
564 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
566 % GetNodeInfo() allocates memory for a new node in the color cube tree and
567 % presets all fields to zero.
569 % The format of the GetNodeInfo method is:
571 % NodeInfo *GetNodeInfo(CubeInfo *cube_info,const size_t level)
573 % A description of each parameter follows.
575 % o cube_info: A pointer to the CubeInfo structure.
577 % o level: Specifies the level in the storage_class the node resides.
580 static NodeInfo *GetNodeInfo(CubeInfo *cube_info,const size_t level)
585 if (cube_info->free_nodes == 0)
591 Allocate a new nodes of nodes.
593 nodes=(Nodes *) AcquireMagickMemory(sizeof(*nodes));
594 if (nodes == (Nodes *) NULL)
595 return((NodeInfo *) NULL);
596 nodes->next=cube_info->node_queue;
597 cube_info->node_queue=nodes;
598 cube_info->node_info=nodes->nodes;
599 cube_info->free_nodes=NodesInAList;
601 cube_info->free_nodes--;
602 node_info=cube_info->node_info++;
603 (void) ResetMagickMemory(node_info,0,sizeof(*node_info));
604 node_info->level=level;
609 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
613 % I s H i s t o g r a m I m a g e %
617 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
619 % IsHistogramImage() returns MagickTrue if the image has 1024 unique colors or
622 % The format of the IsHistogramImage method is:
624 % MagickBooleanType IsHistogramImage(const Image *image,
625 % ExceptionInfo *exception)
627 % A description of each parameter follows.
629 % o image: the image.
631 % o exception: return any errors or warnings in this structure.
634 MagickExport MagickBooleanType IsHistogramImage(const Image *image,
635 ExceptionInfo *exception)
637 #define MaximumUniqueColors 1024
649 register const Quantum
669 assert(image != (Image *) NULL);
670 assert(image->signature == MagickSignature);
671 if (image->debug != MagickFalse)
672 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
673 if ((image->storage_class == PseudoClass) && (image->colors <= 256))
675 if (image->storage_class == PseudoClass)
678 Initialize color description tree.
680 cube_info=GetCubeInfo();
681 if (cube_info == (CubeInfo *) NULL)
683 (void) ThrowMagickException(exception,GetMagickModule(),
684 ResourceLimitError,"MemoryAllocationFailed","'%s'",image->filename);
687 GetPixelInfo(image,&pixel);
688 GetPixelInfo(image,&target);
689 image_view=AcquireVirtualCacheView(image,exception);
690 for (y=0; y < (ssize_t) image->rows; y++)
692 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
693 if (p == (const Quantum *) NULL)
695 for (x=0; x < (ssize_t) image->columns; x++)
698 Start at the root and proceed level by level.
700 node_info=cube_info->root;
701 index=MaxTreeDepth-1;
702 for (level=1; level < MaxTreeDepth; level++)
704 GetPixelInfoPixel(image,p,&pixel);
705 id=ColorToNodeId(image,&pixel,index);
706 if (node_info->child[id] == (NodeInfo *) NULL)
708 node_info->child[id]=GetNodeInfo(cube_info,level);
709 if (node_info->child[id] == (NodeInfo *) NULL)
711 (void) ThrowMagickException(exception,GetMagickModule(),
712 ResourceLimitError,"MemoryAllocationFailed","'%s'",
717 node_info=node_info->child[id];
720 if (level < MaxTreeDepth)
722 for (i=0; i < (ssize_t) node_info->number_unique; i++)
724 target=node_info->list[i];
725 if (IsPixelInfoEquivalent(&pixel,&target) != MagickFalse)
728 if (i < (ssize_t) node_info->number_unique)
729 node_info->list[i].count++;
733 Add this unique color to the color list.
735 if (node_info->number_unique == 0)
736 node_info->list=(PixelInfo *) AcquireMagickMemory(
737 sizeof(*node_info->list));
739 node_info->list=(PixelInfo *) ResizeQuantumMemory(node_info->list,
740 (size_t) (i+1),sizeof(*node_info->list));
741 if (node_info->list == (PixelInfo *) NULL)
743 (void) ThrowMagickException(exception,GetMagickModule(),
744 ResourceLimitError,"MemoryAllocationFailed","'%s'",
748 node_info->list[i].red=(double) GetPixelRed(image,p);
749 node_info->list[i].green=(double) GetPixelGreen(image,p);
750 node_info->list[i].blue=(double) GetPixelBlue(image,p);
751 if (image->colorspace == CMYKColorspace)
752 node_info->list[i].black=(double) GetPixelBlack(image,p);
753 node_info->list[i].alpha=(double) GetPixelAlpha(image,p);
754 node_info->list[i].count=1;
755 node_info->number_unique++;
757 if (cube_info->colors > MaximumUniqueColors)
760 p+=GetPixelChannels(image);
762 if (x < (ssize_t) image->columns)
765 image_view=DestroyCacheView(image_view);
766 cube_info=DestroyCubeInfo(image,cube_info);
767 return(y < (ssize_t) image->rows ? MagickFalse : MagickTrue);
771 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
775 % I s P a l e t t e I m a g e %
779 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
781 % IsPaletteImage() returns MagickTrue if the image is PseudoClass and has 256
782 % unique colors or less.
784 % The format of the IsPaletteImage method is:
786 % MagickBooleanType IsPaletteImage(const Image *image,
787 % ExceptionInfo *exception)
789 % A description of each parameter follows.
791 % o image: the image.
793 % o exception: return any errors or warnings in this structure.
796 MagickExport MagickBooleanType IsPaletteImage(const Image *image,
797 ExceptionInfo *exception)
809 register const Quantum
829 assert(image != (Image *) NULL);
830 assert(image->signature == MagickSignature);
831 if (image->debug != MagickFalse)
832 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
833 if ((image->storage_class == PseudoClass) && (image->colors <= 256))
835 if (image->storage_class == PseudoClass)
838 Initialize color description tree.
840 cube_info=GetCubeInfo();
841 if (cube_info == (CubeInfo *) NULL)
843 (void) ThrowMagickException(exception,GetMagickModule(),
844 ResourceLimitError,"MemoryAllocationFailed","'%s'",image->filename);
847 GetPixelInfo(image,&pixel);
848 GetPixelInfo(image,&target);
849 image_view=AcquireVirtualCacheView(image,exception);
850 for (y=0; y < (ssize_t) image->rows; y++)
852 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
853 if (p == (const Quantum *) NULL)
855 for (x=0; x < (ssize_t) image->columns; x++)
858 Start at the root and proceed level by level.
860 node_info=cube_info->root;
861 index=MaxTreeDepth-1;
862 for (level=1; level < MaxTreeDepth; level++)
864 GetPixelInfoPixel(image,p,&pixel);
865 id=ColorToNodeId(image,&pixel,index);
866 if (node_info->child[id] == (NodeInfo *) NULL)
868 node_info->child[id]=GetNodeInfo(cube_info,level);
869 if (node_info->child[id] == (NodeInfo *) NULL)
871 (void) ThrowMagickException(exception,GetMagickModule(),
872 ResourceLimitError,"MemoryAllocationFailed","'%s'",
877 node_info=node_info->child[id];
880 if (level < MaxTreeDepth)
882 for (i=0; i < (ssize_t) node_info->number_unique; i++)
884 target=node_info->list[i];
885 if (IsPixelInfoEquivalent(&pixel,&target) != MagickFalse)
888 if (i < (ssize_t) node_info->number_unique)
889 node_info->list[i].count++;
893 Add this unique color to the color list.
895 if (node_info->number_unique == 0)
896 node_info->list=(PixelInfo *) AcquireMagickMemory(
897 sizeof(*node_info->list));
899 node_info->list=(PixelInfo *) ResizeQuantumMemory(node_info->list,
900 (size_t) (i+1),sizeof(*node_info->list));
901 if (node_info->list == (PixelInfo *) NULL)
903 (void) ThrowMagickException(exception,GetMagickModule(),
904 ResourceLimitError,"MemoryAllocationFailed","'%s'",
908 node_info->list[i]=pixel;
909 node_info->list[i].red=(double) GetPixelRed(image,p);
910 node_info->list[i].green=(double) GetPixelGreen(image,p);
911 node_info->list[i].blue=(double) GetPixelBlue(image,p);
912 if (image->colorspace == CMYKColorspace)
913 node_info->list[i].black=(double) GetPixelBlack(image,p);
914 node_info->list[i].alpha=(double) GetPixelAlpha(image,p);
915 node_info->list[i].count=1;
916 node_info->number_unique++;
918 if (cube_info->colors > 256)
921 p+=GetPixelChannels(image);
923 if (x < (ssize_t) image->columns)
926 image_view=DestroyCacheView(image_view);
927 cube_info=DestroyCubeInfo(image,cube_info);
928 return(y < (ssize_t) image->rows ? MagickFalse : MagickTrue);
932 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
936 % M i n M a x S t r e t c h I m a g e %
940 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
942 % MinMaxStretchImage() uses the exact minimum and maximum values found in
943 % each of the channels given, as the BlackPoint and WhitePoint to linearly
944 % stretch the colors (and histogram) of the image. The stretch points are
945 % also moved further inward by the adjustment values given.
947 % If the adjustment values are both zero this function is equivalent to a
948 % perfect normalization (or autolevel) of the image.
950 % Each channel is stretched independantally of each other (producing color
951 % distortion) unless the special 'SyncChannels' flag is also provided in the
952 % channels setting. If this flag is present the minimum and maximum point
953 % will be extracted from all the given channels, and those channels will be
954 % stretched by exactly the same amount (preventing color distortion).
956 % In the special case that only ONE value is found in a channel of the image
957 % that value is not stretched, that value is left as is.
959 % The 'SyncChannels' is turned on in the 'DefaultChannels' setting by
962 % The format of the MinMaxStretchImage method is:
964 % MagickBooleanType MinMaxStretchImage(Image *image,const double black,
965 % const double white,const double gamma,ExceptionInfo *exception)
967 % A description of each parameter follows:
969 % o image: The image to auto-level
971 % o black, white: move the black / white point inward from the minimum and
972 % maximum points by this color value.
974 % o gamma: the gamma.
976 % o exception: return any errors or warnings in this structure.
979 MagickExport MagickBooleanType MinMaxStretchImage(Image *image,
980 const double black,const double white,const double gamma,
981 ExceptionInfo *exception)
994 if (image->channel_mask == DefaultChannels)
997 Auto-level all channels equally.
999 (void) GetImageRange(image,&min,&max,exception);
1002 if (fabs(min-max) >= MagickEpsilon)
1003 status&=LevelImage(image,min,max,gamma,exception);
1004 return(status != 0 ? MagickTrue : MagickFalse);
1007 Auto-level each channel separately.
1009 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1020 channel=GetPixelChannelChannel(image,i);
1021 traits=GetPixelChannelTraits(image,channel);
1022 if ((traits & UpdatePixelTrait) == 0)
1024 channel_mask=SetImageChannelMask(image,(ChannelType) (1 << i));
1025 status&=GetImageRange(image,&min,&max,exception);
1028 if (fabs(min-max) >= MagickEpsilon)
1029 status&=LevelImage(image,min,max,gamma,exception);
1030 (void) SetImageChannelMask(image,channel_mask);
1032 return(status != 0 ? MagickTrue : MagickFalse);
1036 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1040 % G e t N u m b e r C o l o r s %
1044 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1046 % GetNumberColors() returns the number of unique colors in an image.
1048 % The format of the GetNumberColors method is:
1050 % size_t GetNumberColors(const Image *image,FILE *file,
1051 % ExceptionInfo *exception)
1053 % A description of each parameter follows.
1055 % o image: the image.
1057 % o file: Write a histogram of the color distribution to this file handle.
1059 % o exception: return any errors or warnings in this structure.
1063 #if defined(__cplusplus) || defined(c_plusplus)
1067 static int HistogramCompare(const void *x,const void *y)
1073 color_1=(const PixelInfo *) x;
1074 color_2=(const PixelInfo *) y;
1075 if (color_2->red != color_1->red)
1076 return((int) color_1->red-(int) color_2->red);
1077 if (color_2->green != color_1->green)
1078 return((int) color_1->green-(int) color_2->green);
1079 if (color_2->blue != color_1->blue)
1080 return((int) color_1->blue-(int) color_2->blue);
1081 return((int) color_2->count-(int) color_1->count);
1084 #if defined(__cplusplus) || defined(c_plusplus)
1088 MagickExport size_t GetNumberColors(const Image *image,FILE *file,
1089 ExceptionInfo *exception)
1091 #define HistogramImageTag "Histogram/Image"
1094 color[MaxTextExtent],
1096 tuple[MaxTextExtent];
1117 if (file == (FILE *) NULL)
1122 cube_info=ClassifyImageColors(image,exception);
1123 if (cube_info != (CubeInfo *) NULL)
1124 number_colors=cube_info->colors;
1125 cube_info=DestroyCubeInfo(image,cube_info);
1126 return(number_colors);
1128 histogram=GetImageHistogram(image,&number_colors,exception);
1129 if (histogram == (PixelInfo *) NULL)
1130 return(number_colors);
1131 qsort((void *) histogram,(size_t) number_colors,sizeof(*histogram),
1133 GetPixelInfo(image,&pixel);
1136 for (i=0; i < (ssize_t) number_colors; i++)
1139 (void) CopyMagickString(tuple,"(",MaxTextExtent);
1140 ConcatenateColorComponent(&pixel,RedPixelChannel,X11Compliance,tuple);
1141 (void) ConcatenateMagickString(tuple,",",MaxTextExtent);
1142 ConcatenateColorComponent(&pixel,GreenPixelChannel,X11Compliance,tuple);
1143 (void) ConcatenateMagickString(tuple,",",MaxTextExtent);
1144 ConcatenateColorComponent(&pixel,BluePixelChannel,X11Compliance,tuple);
1145 if (pixel.colorspace == CMYKColorspace)
1147 (void) ConcatenateMagickString(tuple,",",MaxTextExtent);
1148 ConcatenateColorComponent(&pixel,BlackPixelChannel,X11Compliance,
1151 if (pixel.alpha_trait == BlendPixelTrait)
1153 (void) ConcatenateMagickString(tuple,",",MaxTextExtent);
1154 ConcatenateColorComponent(&pixel,AlphaPixelChannel,X11Compliance,
1157 (void) ConcatenateMagickString(tuple,")",MaxTextExtent);
1158 (void) QueryColorname(image,&pixel,SVGCompliance,color,exception);
1159 GetColorTuple(&pixel,MagickTrue,hex);
1160 (void) FormatLocaleFile(file,"%10.20g",(double) ((MagickOffsetType)
1162 (void) FormatLocaleFile(file,": %s %s %s\n",tuple,hex,color);
1163 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1168 proceed=SetImageProgress(image,HistogramImageTag,(MagickOffsetType) i,
1170 if (proceed == MagickFalse)
1175 (void) fflush(file);
1176 histogram=(PixelInfo *) RelinquishMagickMemory(histogram);
1177 if (status == MagickFalse)
1179 return(number_colors);
1183 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1187 % U n i q u e I m a g e C o l o r s %
1191 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1193 % UniqueImageColors() returns the unique colors of an image.
1195 % The format of the UniqueImageColors method is:
1197 % Image *UniqueImageColors(const Image *image,ExceptionInfo *exception)
1199 % A description of each parameter follows.
1201 % o image: the image.
1203 % o exception: return any errors or warnings in this structure.
1207 static void UniqueColorsToImage(Image *unique_image,CacheView *unique_view,
1208 CubeInfo *cube_info,const NodeInfo *node_info,ExceptionInfo *exception)
1210 #define UniqueColorsImageTag "UniqueColors/Image"
1222 Traverse any children.
1224 number_children=unique_image->alpha_trait != BlendPixelTrait ? 8UL : 16UL;
1225 for (i=0; i < (ssize_t) number_children; i++)
1226 if (node_info->child[i] != (NodeInfo *) NULL)
1227 UniqueColorsToImage(unique_image,unique_view,cube_info,
1228 node_info->child[i],exception);
1229 if (node_info->level == (MaxTreeDepth-1))
1239 for (i=0; i < (ssize_t) node_info->number_unique; i++)
1241 q=QueueCacheViewAuthenticPixels(unique_view,cube_info->x,0,1,1,
1243 if (q == (Quantum *) NULL)
1245 SetPixelRed(unique_image,ClampToQuantum(p->red),q);
1246 SetPixelGreen(unique_image,ClampToQuantum(p->green),q);
1247 SetPixelBlue(unique_image,ClampToQuantum(p->blue),q);
1248 SetPixelAlpha(unique_image,ClampToQuantum(p->alpha),q);
1249 if (unique_image->colorspace == CMYKColorspace)
1250 SetPixelBlack(unique_image,ClampToQuantum(p->black),q);
1251 if (SyncCacheViewAuthenticPixels(unique_view,exception) == MagickFalse)
1256 if (unique_image->progress_monitor != (MagickProgressMonitor) NULL)
1261 proceed=SetImageProgress(unique_image,UniqueColorsImageTag,
1262 cube_info->progress,cube_info->colors);
1263 if (proceed == MagickFalse)
1266 cube_info->progress++;
1267 if (status == MagickFalse)
1272 MagickExport Image *UniqueImageColors(const Image *image,
1273 ExceptionInfo *exception)
1284 cube_info=ClassifyImageColors(image,exception);
1285 if (cube_info == (CubeInfo *) NULL)
1286 return((Image *) NULL);
1287 unique_image=CloneImage(image,cube_info->colors,1,MagickTrue,exception);
1288 if (unique_image == (Image *) NULL)
1289 return(unique_image);
1290 if (SetImageStorageClass(unique_image,DirectClass,exception) == MagickFalse)
1292 unique_image=DestroyImage(unique_image);
1293 return((Image *) NULL);
1295 unique_view=AcquireAuthenticCacheView(unique_image,exception);
1296 UniqueColorsToImage(unique_image,unique_view,cube_info,cube_info->root,
1298 unique_view=DestroyCacheView(unique_view);
1299 if (cube_info->colors < MaxColormapSize)
1304 quantize_info=AcquireQuantizeInfo((ImageInfo *) NULL);
1305 quantize_info->number_colors=MaxColormapSize;
1306 quantize_info->dither_method=NoDitherMethod;
1307 quantize_info->tree_depth=8;
1308 (void) QuantizeImage(quantize_info,unique_image,exception);
1309 quantize_info=DestroyQuantizeInfo(quantize_info);
1311 cube_info=DestroyCubeInfo(image,cube_info);
1312 return(unique_image);