2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % SSSSS TTTTT AAA TTTTT IIIII SSSSS TTTTT IIIII CCCC %
7 % SS T A A T I SS T I C %
8 % SSS T AAAAA T I SSS T I C %
9 % SS T A A T I SS T I C %
10 % SSSSS T A A T IIIII SSSSS T IIIII CCCC %
13 % MagickCore Image Methods %
20 % Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "magick/studio.h"
44 #include "magick/property.h"
45 #include "magick/animate.h"
46 #include "magick/blob.h"
47 #include "magick/blob-private.h"
48 #include "magick/cache.h"
49 #include "magick/cache-private.h"
50 #include "magick/cache-view.h"
51 #include "magick/client.h"
52 #include "magick/color.h"
53 #include "magick/color-private.h"
54 #include "magick/colorspace.h"
55 #include "magick/colorspace-private.h"
56 #include "magick/composite.h"
57 #include "magick/composite-private.h"
58 #include "magick/compress.h"
59 #include "magick/constitute.h"
60 #include "magick/deprecate.h"
61 #include "magick/display.h"
62 #include "magick/draw.h"
63 #include "magick/enhance.h"
64 #include "magick/exception.h"
65 #include "magick/exception-private.h"
66 #include "magick/gem.h"
67 #include "magick/geometry.h"
68 #include "magick/list.h"
69 #include "magick/image-private.h"
70 #include "magick/magic.h"
71 #include "magick/magick.h"
72 #include "magick/memory_.h"
73 #include "magick/module.h"
74 #include "magick/monitor.h"
75 #include "magick/option.h"
76 #include "magick/paint.h"
77 #include "magick/pixel-private.h"
78 #include "magick/profile.h"
79 #include "magick/quantize.h"
80 #include "magick/random_.h"
81 #include "magick/segment.h"
82 #include "magick/semaphore.h"
83 #include "magick/signature-private.h"
84 #include "magick/statistic.h"
85 #include "magick/string_.h"
86 #include "magick/thread-private.h"
87 #include "magick/timer.h"
88 #include "magick/utility.h"
89 #include "magick/version.h"
92 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96 + G e t I m a g e B o u n d i n g B o x %
100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102 % GetImageBoundingBox() returns the bounding box of an image canvas.
104 % The format of the GetImageBoundingBox method is:
106 % RectangleInfo GetImageBoundingBox(const Image *image,
107 % ExceptionInfo *exception)
109 % A description of each parameter follows:
111 % o bounds: Method GetImageBoundingBox returns the bounding box of an
114 % o image: the image.
116 % o exception: return any errors or warnings in this structure.
119 MagickExport RectangleInfo GetImageBoundingBox(const Image *image,
120 ExceptionInfo *exception)
135 register const PixelPacket
141 assert(image != (Image *) NULL);
142 assert(image->signature == MagickSignature);
143 if (image->debug != MagickFalse)
144 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
147 bounds.x=(long) image->columns;
148 bounds.y=(long) image->rows;
149 GetMagickPixelPacket(image,&target[0]);
150 image_view=AcquireCacheView(image);
151 p=GetCacheViewVirtualPixels(image_view,0,0,1,1,exception);
152 if (p == (const PixelPacket *) NULL)
154 image_view=DestroyCacheView(image_view);
157 SetMagickPixelPacket(image,p,GetCacheViewAuthenticIndexQueue(image_view),
159 GetMagickPixelPacket(image,&target[1]);
160 p=GetCacheViewVirtualPixels(image_view,(long) image->columns-1,0,1,1,
162 SetMagickPixelPacket(image,p,GetCacheViewAuthenticIndexQueue(image_view),
164 GetMagickPixelPacket(image,&target[2]);
165 p=GetCacheViewVirtualPixels(image_view,0,(long) image->rows-1,1,1,exception);
166 SetMagickPixelPacket(image,p,GetCacheViewAuthenticIndexQueue(image_view),
169 GetMagickPixelPacket(image,&zero);
170 #if defined(MAGICKCORE_OPENMP_SUPPORT)
171 #pragma omp parallel for schedule(static,1) shared(status)
173 for (y=0; y < (long) image->rows; y++)
181 register const IndexPacket
184 register const PixelPacket
190 if (status == MagickFalse)
192 #if defined(HAVE_OPENMP)
193 # pragma omp critical (MagickCore_GetImageBoundingBox)
196 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
197 if (p == (const PixelPacket *) NULL)
202 indexes=GetCacheViewVirtualIndexQueue(image_view);
204 for (x=0; x < (long) image->columns; x++)
206 SetMagickPixelPacket(image,p,indexes+x,&pixel);
207 if ((x < bounding_box.x) &&
208 (IsMagickColorSimilar(&pixel,&target[0]) == MagickFalse))
210 if ((x > (long) bounding_box.width) &&
211 (IsMagickColorSimilar(&pixel,&target[1]) == MagickFalse))
212 bounding_box.width=(unsigned long) x;
213 if ((y < bounding_box.y) &&
214 (IsMagickColorSimilar(&pixel,&target[0]) == MagickFalse))
216 if ((y > (long) bounding_box.height) &&
217 (IsMagickColorSimilar(&pixel,&target[2]) == MagickFalse))
218 bounding_box.height=(unsigned long) y;
221 #if defined(HAVE_OPENMP)
222 # pragma omp critical (MagickCore_GetImageBoundingBox)
225 if (bounding_box.x < bounds.x)
226 bounds.x=bounding_box.x;
227 if (bounding_box.y < bounds.y)
228 bounds.y=bounding_box.y;
229 if (bounding_box.width > bounds.width)
230 bounds.width=bounding_box.width;
231 if (bounding_box.height > bounds.height)
232 bounds.height=bounding_box.height;
235 image_view=DestroyCacheView(image_view);
236 if ((bounds.width == 0) || (bounds.height == 0))
237 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
238 "GeometryDoesNotContainImage","`%s'",image->filename);
241 bounds.width-=(bounds.x-1);
242 bounds.height-=(bounds.y-1);
248 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
252 % G e t I m a g e C h a n n e l D e p t h %
256 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
258 % GetImageChannelDepth() returns the depth of a particular image channel.
260 % The format of the GetImageChannelDepth method is:
262 % unsigned long GetImageDepth(const Image *image,ExceptionInfo *exception)
263 % unsigned long GetImageChannelDepth(const Image *image,
264 % const ChannelType channel,ExceptionInfo *exception)
266 % A description of each parameter follows:
268 % o image: the image.
270 % o channel: the channel.
272 % o exception: return any errors or warnings in this structure.
276 MagickExport unsigned long GetImageDepth(const Image *image,
277 ExceptionInfo *exception)
279 return(GetImageChannelDepth(image,AllChannels,exception));
282 MagickExport unsigned long GetImageChannelDepth(const Image *image,
283 const ChannelType channel,ExceptionInfo *exception)
305 assert(image != (Image *) NULL);
306 assert(image->signature == MagickSignature);
307 if (image->debug != MagickFalse)
308 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
309 number_threads=GetOpenMPMaximumThreads();
310 current_depth=(unsigned long *) AcquireQuantumMemory(number_threads,
311 sizeof(*current_depth));
312 if (current_depth == (unsigned long *) NULL)
313 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
315 for (id=0; id < (long) number_threads; id++)
317 if ((image->storage_class == PseudoClass) && (image->matte == MagickFalse))
319 register const PixelPacket
326 #if defined(MAGICKCORE_OPENMP_SUPPORT)
327 #pragma omp parallel for schedule(static,1) shared(status)
329 for (i=0; i < (long) image->colors; i++)
331 if (status == MagickFalse)
333 id=GetOpenMPThreadId();
334 while (current_depth[id] < MAGICKCORE_QUANTUM_DEPTH)
343 range=GetQuantumRange(current_depth[id]);
344 if ((channel & RedChannel) != 0)
345 status|=p->red != ScaleAnyToQuantum(ScaleQuantumToAny(p->red,
347 if ((channel & GreenChannel) != 0)
348 status|=p->green != ScaleAnyToQuantum(ScaleQuantumToAny(p->green,
350 if ((channel & BlueChannel) != 0)
351 status|=p->blue != ScaleAnyToQuantum(ScaleQuantumToAny(p->blue,
359 depth=current_depth[0];
360 for (id=1; id < (long) number_threads; id++)
361 if (depth < current_depth[id])
362 depth=current_depth[id];
363 current_depth=(unsigned long *) RelinquishMagickMemory(current_depth);
366 image_view=AcquireCacheView(image);
367 #if defined(MAGICKCORE_OPENMP_SUPPORT)
368 #pragma omp parallel for schedule(static,1) shared(status)
370 for (y=0; y < (long) image->rows; y++)
372 register const IndexPacket
375 register const PixelPacket
382 if (status == MagickFalse)
384 id=GetOpenMPThreadId();
385 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
386 if (p == (const PixelPacket *) NULL)
388 indexes=GetCacheViewVirtualIndexQueue(image_view);
389 for (x=0; x < (long) image->columns; x++)
391 while (current_depth[id] < MAGICKCORE_QUANTUM_DEPTH)
400 range=GetQuantumRange(current_depth[id]);
401 if ((channel & RedChannel) != 0)
402 status|=p->red != ScaleAnyToQuantum(ScaleQuantumToAny(p->red,range),
404 if ((channel & GreenChannel) != 0)
405 status|=p->green != ScaleAnyToQuantum(ScaleQuantumToAny(p->green,
407 if ((channel & BlueChannel) != 0)
408 status|=p->blue != ScaleAnyToQuantum(ScaleQuantumToAny(p->blue,range),
410 if (((channel & OpacityChannel) != 0) && (image->matte != MagickFalse))
411 status|=p->opacity != ScaleAnyToQuantum(ScaleQuantumToAny(p->opacity,
413 if (((channel & IndexChannel) != 0) &&
414 (image->colorspace == CMYKColorspace))
415 status|=indexes[x] != ScaleAnyToQuantum(ScaleQuantumToAny(indexes[x],
423 if (current_depth[id] == MAGICKCORE_QUANTUM_DEPTH)
426 image_view=DestroyCacheView(image_view);
427 depth=current_depth[0];
428 for (id=1; id < (long) number_threads; id++)
429 if (depth < current_depth[id])
430 depth=current_depth[id];
431 current_depth=(unsigned long *) RelinquishMagickMemory(current_depth);
436 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
440 + G e t I m a g e C h a n n e l E x t r e m a %
444 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
446 % GetImageChannelExtrema() returns the extrema of one or more image channels.
448 % The format of the GetImageChannelExtrema method is:
450 % MagickBooleanType GetImageChannelExtrema(const Image *image,
451 % const ChannelType channel,unsigned long *minima,unsigned long *maxima,
452 % ExceptionInfo *exception)
454 % A description of each parameter follows:
456 % o image: the image.
458 % o channel: the channel.
460 % o minima: the minimum value in the channel.
462 % o maxima: the maximum value in the channel.
464 % o exception: return any errors or warnings in this structure.
468 MagickExport MagickBooleanType GetImageExtrema(const Image *image,
469 unsigned long *minima,unsigned long *maxima,ExceptionInfo *exception)
471 return(GetImageChannelExtrema(image,AllChannels,minima,maxima,exception));
474 MagickExport MagickBooleanType GetImageChannelExtrema(const Image *image,
475 const ChannelType channel,unsigned long *minima,unsigned long *maxima,
476 ExceptionInfo *exception)
485 assert(image != (Image *) NULL);
486 assert(image->signature == MagickSignature);
487 if (image->debug != MagickFalse)
488 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
489 status=GetImageChannelRange(image,channel,&min,&max,exception);
490 *minima=(unsigned long) (min+0.5);
491 *maxima=(unsigned long) (max+0.5);
496 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
500 % G e t I m a g e C h a n n e l M e a n %
504 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
506 % GetImageChannelMean() returns the mean and standard deviation of one or more
509 % The format of the GetImageChannelMean method is:
511 % MagickBooleanType GetImageChannelMean(const Image *image,
512 % const ChannelType channel,double *mean,double *standard_deviation,
513 % ExceptionInfo *exception)
515 % A description of each parameter follows:
517 % o image: the image.
519 % o channel: the channel.
521 % o mean: the average value in the channel.
523 % o standard_deviation: the standard deviation of the channel.
525 % o exception: return any errors or warnings in this structure.
529 MagickExport MagickBooleanType GetImageMean(const Image *image,double *mean,
530 double *standard_deviation,ExceptionInfo *exception)
535 status=GetImageChannelMean(image,AllChannels,mean,standard_deviation,
540 MagickExport MagickBooleanType GetImageChannelMean(const Image *image,
541 const ChannelType channel,double *mean,double *standard_deviation,
542 ExceptionInfo *exception)
550 assert(image != (Image *) NULL);
551 assert(image->signature == MagickSignature);
552 if (image->debug != MagickFalse)
553 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
555 *standard_deviation=0.0;
557 for (y=0; y < (long) image->rows; y++)
559 register const IndexPacket
562 register const PixelPacket
568 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
569 if (p == (const PixelPacket *) NULL)
571 indexes=GetVirtualIndexQueue(image);
572 for (x=0; x < (long) image->columns; x++)
574 if ((channel & RedChannel) != 0)
577 *standard_deviation+=(double) p->red*p->red;
580 if ((channel & GreenChannel) != 0)
583 *standard_deviation+=(double) p->green*p->green;
586 if ((channel & BlueChannel) != 0)
589 *standard_deviation+=(double) p->blue*p->blue;
592 if ((channel & OpacityChannel) != 0)
595 *standard_deviation+=(double) p->opacity*p->opacity;
598 if (((channel & IndexChannel) != 0) &&
599 (image->colorspace == CMYKColorspace))
602 *standard_deviation+=(double) indexes[x]*indexes[x];
608 if (y < (long) image->rows)
613 *standard_deviation/=area;
615 *standard_deviation=sqrt(*standard_deviation-(*mean*(*mean)));
616 return(y == (long) image->rows ? MagickTrue : MagickFalse);
620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
624 % G e t I m a g e C h a n n e l K u r t o s i s %
628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
630 % GetImageChannelKurtosis() returns the kurtosis and skewness of one or more
633 % The format of the GetImageChannelKurtosis method is:
635 % MagickBooleanType GetImageChannelKurtosis(const Image *image,
636 % const ChannelType channel,double *kurtosis,double *skewness,
637 % ExceptionInfo *exception)
639 % A description of each parameter follows:
641 % o image: the image.
643 % o channel: the channel.
645 % o kurtosis: the kurtosis of the channel.
647 % o skewness: the skewness of the channel.
649 % o exception: return any errors or warnings in this structure.
653 MagickExport MagickBooleanType GetImageKurtosis(const Image *image,
654 double *kurtosis,double *skewness,ExceptionInfo *exception)
659 status=GetImageChannelKurtosis(image,AllChannels,kurtosis,skewness,
664 MagickExport MagickBooleanType GetImageChannelKurtosis(const Image *image,
665 const ChannelType channel,double *kurtosis,double *skewness,
666 ExceptionInfo *exception)
679 assert(image != (Image *) NULL);
680 assert(image->signature == MagickSignature);
681 if (image->debug != MagickFalse)
682 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
687 standard_deviation=0.0;
690 sum_fourth_power=0.0;
691 for (y=0; y < (long) image->rows; y++)
693 register const IndexPacket
696 register const PixelPacket
702 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
703 if (p == (const PixelPacket *) NULL)
705 indexes=GetVirtualIndexQueue(image);
706 for (x=0; x < (long) image->columns; x++)
708 if ((channel & RedChannel) != 0)
711 sum_squares+=(double) p->red*p->red;
712 sum_cubes+=(double) p->red*p->red*p->red;
713 sum_fourth_power+=(double) p->red*p->red*p->red*p->red;
716 if ((channel & GreenChannel) != 0)
719 sum_squares+=(double) p->green*p->green;
720 sum_cubes+=(double) p->green*p->green*p->green;
721 sum_fourth_power+=(double) p->green*p->green*p->green*p->green;
724 if ((channel & BlueChannel) != 0)
727 sum_squares+=(double) p->blue*p->blue;
728 sum_cubes+=(double) p->blue*p->blue*p->blue;
729 sum_fourth_power+=(double) p->blue*p->blue*p->blue*p->blue;
732 if ((channel & OpacityChannel) != 0)
735 sum_squares+=(double) p->opacity*p->opacity;
736 sum_cubes+=(double) p->opacity*p->opacity*p->opacity;
737 sum_fourth_power+=(double) p->opacity*p->opacity*p->opacity*
741 if (((channel & IndexChannel) != 0) &&
742 (image->colorspace == CMYKColorspace))
745 sum_squares+=(double) indexes[x]*indexes[x];
746 sum_cubes+=(double) indexes[x]*indexes[x]*indexes[x];
747 sum_fourth_power+=(double) indexes[x]*indexes[x]*indexes[x]*
754 if (y < (long) image->rows)
761 sum_fourth_power/=area;
763 standard_deviation=sqrt(sum_squares-(mean*mean));
764 if (standard_deviation != 0.0)
766 *kurtosis=sum_fourth_power-4.0*mean*sum_cubes+6.0*mean*mean*sum_squares-
767 3.0*mean*mean*mean*mean;
768 *kurtosis/=standard_deviation*standard_deviation*standard_deviation*
771 *skewness=sum_cubes-3.0*mean*sum_squares+2.0*mean*mean*mean;
772 *skewness/=standard_deviation*standard_deviation*standard_deviation;
774 return(y == (long) image->rows ? MagickTrue : MagickFalse);
778 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
782 % G e t I m a g e C h a n n e l R a n g e %
786 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
788 % GetImageChannelRange() returns the range of one or more image channels.
790 % The format of the GetImageChannelRange method is:
792 % MagickBooleanType GetImageChannelRange(const Image *image,
793 % const ChannelType channel,double *minima,double *maxima,
794 % ExceptionInfo *exception)
796 % A description of each parameter follows:
798 % o image: the image.
800 % o channel: the channel.
802 % o minima: the minimum value in the channel.
804 % o maxima: the maximum value in the channel.
806 % o exception: return any errors or warnings in this structure.
810 MagickExport MagickBooleanType GetImageRange(const Image *image,
811 double *minima,double *maxima,ExceptionInfo *exception)
813 return(GetImageChannelRange(image,AllChannels,minima,maxima,exception));
816 MagickExport MagickBooleanType GetImageChannelRange(const Image *image,
817 const ChannelType channel,double *minima,double *maxima,
818 ExceptionInfo *exception)
826 assert(image != (Image *) NULL);
827 assert(image->signature == MagickSignature);
828 if (image->debug != MagickFalse)
829 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
832 GetMagickPixelPacket(image,&pixel);
833 for (y=0; y < (long) image->rows; y++)
835 register const IndexPacket
838 register const PixelPacket
844 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
845 if (p == (const PixelPacket *) NULL)
847 indexes=GetVirtualIndexQueue(image);
848 for (x=0; x < (long) image->columns; x++)
850 SetMagickPixelPacket(image,p,indexes+x,&pixel);
851 if ((channel & RedChannel) != 0)
853 if (pixel.red < *minima)
854 *minima=(double) pixel.red;
855 if (pixel.red > *maxima)
856 *maxima=(double) pixel.red;
858 if ((channel & GreenChannel) != 0)
860 if (pixel.green < *minima)
861 *minima=(double) pixel.green;
862 if (pixel.green > *maxima)
863 *maxima=(double) pixel.green;
865 if ((channel & BlueChannel) != 0)
867 if (pixel.blue < *minima)
868 *minima=(double) pixel.blue;
869 if (pixel.blue > *maxima)
870 *maxima=(double) pixel.blue;
872 if ((channel & OpacityChannel) != 0)
874 if (pixel.opacity < *minima)
875 *minima=(double) pixel.opacity;
876 if (pixel.opacity > *maxima)
877 *maxima=(double) pixel.opacity;
879 if (((channel & IndexChannel) != 0) &&
880 (image->colorspace == CMYKColorspace))
882 if ((double) indexes[x] < *minima)
883 *minima=(double) indexes[x];
884 if ((double) indexes[x] > *maxima)
885 *maxima=(double) indexes[x];
890 return(y == (long) image->rows ? MagickTrue : MagickFalse);
894 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
898 % G e t I m a g e C h a n n e l S t a t i s t i c s %
902 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
904 % GetImageChannelStatistics() returns statistics for each channel in the
905 % image. The statistics include the channel depth, its minima, maxima, mean,
906 % standard deviation, kurtosis and skewness. You can access the red channel
907 % mean, for example, like this:
909 % channel_statistics=GetImageChannelStatistics(image,excepton);
910 % red_mean=channel_statistics[RedChannel].mean;
912 % Use MagickRelinquishMemory() to free the statistics buffer.
914 % The format of the GetImageChannelStatistics method is:
916 % ChannelStatistics *GetImageChannelStatistics(const Image *image,
917 % ExceptionInfo *exception)
919 % A description of each parameter follows:
921 % o image: the image.
923 % o exception: return any errors or warnings in this structure.
927 static inline double MagickMax(const double x,const double y)
934 static inline double MagickMin(const double x,const double y)
941 MagickExport ChannelStatistics *GetImageChannelStatistics(const Image *image,
942 ExceptionInfo *exception)
971 assert(image != (Image *) NULL);
972 assert(image->signature == MagickSignature);
973 if (image->debug != MagickFalse)
974 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
975 length=AllChannels+1UL;
976 channel_statistics=(ChannelStatistics *) AcquireQuantumMemory(length,
977 sizeof(*channel_statistics));
978 if (channel_statistics == (ChannelStatistics *) NULL)
979 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
980 (void) ResetMagickMemory(channel_statistics,0,length*
981 sizeof(*channel_statistics));
982 for (i=0; i <= AllChannels; i++)
984 channel_statistics[i].depth=1;
985 channel_statistics[i].maxima=(-1.0E-37);
986 channel_statistics[i].minima=1.0E+37;
987 channel_statistics[i].mean=0.0;
988 channel_statistics[i].standard_deviation=0.0;
989 channel_statistics[i].kurtosis=0.0;
990 channel_statistics[i].skewness=0.0;
992 for (y=0; y < (long) image->rows; y++)
994 register const IndexPacket
997 register const PixelPacket
1003 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1004 if (p == (const PixelPacket *) NULL)
1006 indexes=GetVirtualIndexQueue(image);
1007 for (x=0; x < (long) image->columns; )
1009 if (channel_statistics[RedChannel].depth != MAGICKCORE_QUANTUM_DEPTH)
1011 depth=channel_statistics[RedChannel].depth;
1012 range=GetQuantumRange(depth);
1013 status=p->red != ScaleAnyToQuantum(ScaleQuantumToAny(p->red,range),
1014 range) ? MagickTrue : MagickFalse;
1015 if (status != MagickFalse)
1017 channel_statistics[RedChannel].depth++;
1021 if (channel_statistics[GreenChannel].depth != MAGICKCORE_QUANTUM_DEPTH)
1023 depth=channel_statistics[GreenChannel].depth;
1024 range=GetQuantumRange(depth);
1025 status=p->green != ScaleAnyToQuantum(ScaleQuantumToAny(p->green,
1026 range),range) ? MagickTrue : MagickFalse;
1027 if (status != MagickFalse)
1029 channel_statistics[GreenChannel].depth++;
1033 if (channel_statistics[BlueChannel].depth != MAGICKCORE_QUANTUM_DEPTH)
1035 depth=channel_statistics[BlueChannel].depth;
1036 range=GetQuantumRange(depth);
1037 status=p->blue != ScaleAnyToQuantum(ScaleQuantumToAny(p->blue,
1038 range),range) ? MagickTrue : MagickFalse;
1039 if (status != MagickFalse)
1041 channel_statistics[BlueChannel].depth++;
1045 if (image->matte != MagickFalse)
1047 if (channel_statistics[OpacityChannel].depth != MAGICKCORE_QUANTUM_DEPTH)
1049 depth=channel_statistics[OpacityChannel].depth;
1050 range=GetQuantumRange(depth);
1051 status=p->opacity != ScaleAnyToQuantum(ScaleQuantumToAny(
1052 p->opacity,range),range) ? MagickTrue : MagickFalse;
1053 if (status != MagickFalse)
1055 channel_statistics[OpacityChannel].depth++;
1060 if (image->colorspace == CMYKColorspace)
1062 if (channel_statistics[BlackChannel].depth != MAGICKCORE_QUANTUM_DEPTH)
1064 depth=channel_statistics[BlackChannel].depth;
1065 range=GetQuantumRange(depth);
1066 status=indexes[x] != ScaleAnyToQuantum(ScaleQuantumToAny(
1067 indexes[x],range),range) ? MagickTrue : MagickFalse;
1068 if (status != MagickFalse)
1070 channel_statistics[BlackChannel].depth++;
1075 if ((double) p->red < channel_statistics[RedChannel].minima)
1076 channel_statistics[RedChannel].minima=(double) p->red;
1077 if ((double) p->red > channel_statistics[RedChannel].maxima)
1078 channel_statistics[RedChannel].maxima=(double) p->red;
1079 channel_statistics[RedChannel].mean+=p->red;
1080 channel_statistics[RedChannel].standard_deviation+=(double) p->red*p->red;
1081 channel_statistics[RedChannel].kurtosis+=(double) p->red*p->red*
1083 channel_statistics[RedChannel].skewness+=(double) p->red*p->red*p->red;
1084 if ((double) p->green < channel_statistics[GreenChannel].minima)
1085 channel_statistics[GreenChannel].minima=(double) p->green;
1086 if ((double) p->green > channel_statistics[GreenChannel].maxima)
1087 channel_statistics[GreenChannel].maxima=(double) p->green;
1088 channel_statistics[GreenChannel].mean+=p->green;
1089 channel_statistics[GreenChannel].standard_deviation+=(double) p->green*
1091 channel_statistics[GreenChannel].kurtosis+=(double) p->green*p->green*
1093 channel_statistics[GreenChannel].skewness+=(double) p->green*p->green*
1095 if ((double) p->blue < channel_statistics[BlueChannel].minima)
1096 channel_statistics[BlueChannel].minima=(double) p->blue;
1097 if ((double) p->blue > channel_statistics[BlueChannel].maxima)
1098 channel_statistics[BlueChannel].maxima=(double) p->blue;
1099 channel_statistics[BlueChannel].mean+=p->blue;
1100 channel_statistics[BlueChannel].standard_deviation+=(double) p->blue*
1102 channel_statistics[BlueChannel].kurtosis+=(double) p->blue*p->blue*
1104 channel_statistics[BlueChannel].skewness+=(double) p->blue*p->blue*
1106 if (image->matte != MagickFalse)
1108 if ((double) p->opacity < channel_statistics[OpacityChannel].minima)
1109 channel_statistics[OpacityChannel].minima=(double) p->opacity;
1110 if ((double) p->opacity > channel_statistics[OpacityChannel].maxima)
1111 channel_statistics[OpacityChannel].maxima=(double) p->opacity;
1112 channel_statistics[OpacityChannel].mean+=p->opacity;
1113 channel_statistics[OpacityChannel].standard_deviation+=(double)
1114 p->opacity*p->opacity;
1115 channel_statistics[OpacityChannel].kurtosis+=(double) p->opacity*
1116 p->opacity*p->opacity*p->opacity;
1117 channel_statistics[OpacityChannel].skewness+=(double) p->opacity*
1118 p->opacity*p->opacity;
1120 if (image->colorspace == CMYKColorspace)
1122 if ((double) indexes[x] < channel_statistics[BlackChannel].minima)
1123 channel_statistics[BlackChannel].minima=(double) indexes[x];
1124 if ((double) indexes[x] > channel_statistics[BlackChannel].maxima)
1125 channel_statistics[BlackChannel].maxima=(double) indexes[x];
1126 channel_statistics[BlackChannel].mean+=indexes[x];
1127 channel_statistics[BlackChannel].standard_deviation+=(double)
1128 indexes[x]*indexes[x];
1129 channel_statistics[BlackChannel].kurtosis+=(double) indexes[x]*
1130 indexes[x]*indexes[x]*indexes[x];
1131 channel_statistics[BlackChannel].skewness+=(double) indexes[x]*
1132 indexes[x]*indexes[x];
1138 area=(double) image->columns*image->rows;
1139 for (i=0; i < AllChannels; i++)
1141 channel_statistics[i].mean/=area;
1142 channel_statistics[i].standard_deviation/=area;
1143 channel_statistics[i].kurtosis/=area;
1144 channel_statistics[i].skewness/=area;
1146 for (i=0; i < AllChannels; i++)
1148 channel_statistics[AllChannels].depth=(unsigned long) MagickMax((double)
1149 channel_statistics[AllChannels].depth,(double)
1150 channel_statistics[i].depth);
1151 channel_statistics[AllChannels].minima=MagickMin(
1152 channel_statistics[AllChannels].minima,channel_statistics[i].minima);
1153 channel_statistics[AllChannels].maxima=MagickMax(
1154 channel_statistics[AllChannels].maxima,channel_statistics[i].maxima);
1155 channel_statistics[AllChannels].mean+=channel_statistics[i].mean;
1156 channel_statistics[AllChannels].standard_deviation+=
1157 channel_statistics[i].standard_deviation;
1158 channel_statistics[AllChannels].kurtosis+=channel_statistics[i].kurtosis;
1159 channel_statistics[AllChannels].skewness+=channel_statistics[i].skewness;
1162 if (image->colorspace == CMYKColorspace)
1164 channel_statistics[AllChannels].mean/=channels;
1165 channel_statistics[AllChannels].standard_deviation/=channels;
1166 channel_statistics[AllChannels].kurtosis/=channels;
1167 channel_statistics[AllChannels].skewness/=channels;
1168 for (i=0; i <= AllChannels; i++)
1171 sum_squares=channel_statistics[i].standard_deviation;
1173 sum_cubes=channel_statistics[i].skewness;
1174 channel_statistics[i].standard_deviation=sqrt(
1175 channel_statistics[i].standard_deviation-
1176 (channel_statistics[i].mean*channel_statistics[i].mean));
1177 if (channel_statistics[i].standard_deviation == 0.0)
1179 channel_statistics[i].kurtosis=0.0;
1180 channel_statistics[i].skewness=0.0;
1184 channel_statistics[i].skewness=(channel_statistics[i].skewness-
1185 3.0*channel_statistics[i].mean*sum_squares+
1186 2.0*channel_statistics[i].mean*channel_statistics[i].mean*
1187 channel_statistics[i].mean)/
1188 (channel_statistics[i].standard_deviation*
1189 channel_statistics[i].standard_deviation*
1190 channel_statistics[i].standard_deviation);
1191 channel_statistics[i].kurtosis=(channel_statistics[i].kurtosis-
1192 4.0*channel_statistics[i].mean*sum_cubes+
1193 6.0*channel_statistics[i].mean*channel_statistics[i].mean*sum_squares-
1194 3.0*channel_statistics[i].mean*channel_statistics[i].mean*
1195 1.0*channel_statistics[i].mean*channel_statistics[i].mean)/
1196 (channel_statistics[i].standard_deviation*
1197 channel_statistics[i].standard_deviation*
1198 channel_statistics[i].standard_deviation*
1199 channel_statistics[i].standard_deviation)-3.0;
1202 return(channel_statistics);
1206 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1210 % G e t I m a g e Q u a n t u m D e p t h %
1214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1216 % GetImageQuantumDepth() returns the depth of the image rounded to a legal
1217 % quantum depth: 8, 16, or 32.
1219 % The format of the GetImageQuantumDepth method is:
1221 % unsigned long GetImageQuantumDepth(const Image *image,
1222 % const MagickBooleanType constrain)
1224 % A description of each parameter follows:
1226 % o image: the image.
1228 % o constrain: A value other than MagickFalse, constrains the depth to
1229 % a maximum of MAGICKCORE_QUANTUM_DEPTH.
1232 MagickExport unsigned long GetImageQuantumDepth(const Image *image,
1233 const MagickBooleanType constrain)
1250 if (constrain != MagickFalse)
1251 depth=(unsigned long) MagickMin((double) depth,(double)
1252 MAGICKCORE_QUANTUM_DEPTH);
1257 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1261 % S e t I m a g e C h a n n e l D e p t h %
1265 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1267 % SetImageChannelDepth() sets the depth of the image.
1269 % The format of the SetImageChannelDepth method is:
1271 % MagickBooleanType SetImageDepth(Image *image,const unsigned long depth)
1272 % MagickBooleanType SetImageChannelDepth(Image *image,
1273 % const ChannelType channel,const unsigned long depth)
1275 % A description of each parameter follows:
1277 % o image: the image.
1279 % o channel: the channel.
1281 % o depth: the image depth.
1285 MagickExport MagickBooleanType SetImageDepth(Image *image,
1286 const unsigned long depth)
1288 return(SetImageChannelDepth(image,AllChannels,depth));
1291 MagickExport MagickBooleanType SetImageChannelDepth(Image *image,
1292 const ChannelType channel,const unsigned long depth)
1309 assert(image != (Image *) NULL);
1310 if (image->debug != MagickFalse)
1311 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1312 assert(image->signature == MagickSignature);
1313 if (GetImageDepth(image,&image->exception) <= (unsigned long)
1314 MagickMin((double) depth,(double) MAGICKCORE_QUANTUM_DEPTH))
1320 Scale pixels to desired depth.
1323 range=GetQuantumRange(depth);
1324 exception=(&image->exception);
1325 image_view=AcquireCacheView(image);
1326 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1327 #pragma omp parallel for schedule(static,1) shared(status)
1329 for (y=0; y < (long) image->rows; y++)
1331 register IndexPacket
1332 *__restrict indexes;
1337 register PixelPacket
1340 if (status == MagickFalse)
1342 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
1344 if (q == (PixelPacket *) NULL)
1349 indexes=GetCacheViewAuthenticIndexQueue(image_view);
1350 for (x=0; x < (long) image->columns; x++)
1352 if ((channel & RedChannel) != 0)
1353 q->red=ScaleAnyToQuantum(ScaleQuantumToAny(q->red,range),range);
1354 if ((channel & GreenChannel) != 0)
1355 q->green=ScaleAnyToQuantum(ScaleQuantumToAny(q->green,range),range);
1356 if ((channel & BlueChannel) != 0)
1357 q->blue=ScaleAnyToQuantum(ScaleQuantumToAny(q->blue,range),range);
1358 if (((channel & OpacityChannel) != 0) && (image->matte != MagickFalse))
1359 q->opacity=ScaleAnyToQuantum(ScaleQuantumToAny(q->opacity,range),range);
1360 if (((channel & IndexChannel) != 0) &&
1361 (image->colorspace == CMYKColorspace))
1362 indexes[x]=ScaleAnyToQuantum(ScaleQuantumToAny(indexes[x],range),range);
1365 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1371 image_view=DestroyCacheView(image_view);
1372 if (image->storage_class == PseudoClass)
1380 register PixelPacket
1384 range=GetQuantumRange(depth);
1385 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1386 #pragma omp parallel for schedule(static,1) shared(status)
1388 for (i=0; i < (long) image->colors; i++)
1390 if ((channel & RedChannel) != 0)
1391 p->red=ScaleAnyToQuantum(ScaleQuantumToAny(p->red,range),range);
1392 if ((channel & GreenChannel) != 0)
1393 p->green=ScaleAnyToQuantum(ScaleQuantumToAny(p->green,range),range);
1394 if ((channel & BlueChannel) != 0)
1395 p->blue=ScaleAnyToQuantum(ScaleQuantumToAny(p->blue,range),range);
1396 if ((channel & OpacityChannel) != 0)
1397 p->opacity=ScaleAnyToQuantum(ScaleQuantumToAny(p->opacity,range),