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-2010 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/monitor-private.h"
76 #include "magick/option.h"
77 #include "magick/paint.h"
78 #include "magick/pixel-private.h"
79 #include "magick/profile.h"
80 #include "magick/quantize.h"
81 #include "magick/random_.h"
82 #include "magick/segment.h"
83 #include "magick/semaphore.h"
84 #include "magick/signature-private.h"
85 #include "magick/statistic.h"
86 #include "magick/string_.h"
87 #include "magick/thread-private.h"
88 #include "magick/timer.h"
89 #include "magick/utility.h"
90 #include "magick/version.h"
93 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97 % A v e r a g e I m a g e s %
101 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103 % AverageImages() takes a set of images and averages them together. Each
104 % image in the set must have the same width and height. AverageImages()
105 % returns a single image with each corresponding pixel component of each
106 % image averaged. On failure, a NULL image is returned and exception
107 % describes the reason for the failure.
109 % The format of the AverageImages method is:
111 % Image *AverageImages(Image *image,ExceptionInfo *exception)
113 % A description of each parameter follows:
115 % o image: the image sequence.
117 % o exception: return any errors or warnings in this structure.
121 static MagickPixelPacket **DestroyPixelThreadSet(MagickPixelPacket **pixels)
126 assert(pixels != (MagickPixelPacket **) NULL);
127 for (i=0; i < (long) GetOpenMPMaximumThreads(); i++)
128 if (pixels[i] != (MagickPixelPacket *) NULL)
129 pixels[i]=(MagickPixelPacket *) RelinquishMagickMemory(pixels[i]);
130 pixels=(MagickPixelPacket **) RelinquishAlignedMemory(pixels);
134 static MagickPixelPacket **AcquirePixelThreadSet(const Image *image)
146 number_threads=GetOpenMPMaximumThreads();
147 pixels=(MagickPixelPacket **) AcquireAlignedMemory(number_threads,
149 if (pixels == (MagickPixelPacket **) NULL)
150 return((MagickPixelPacket **) NULL);
151 (void) ResetMagickMemory(pixels,0,number_threads*sizeof(*pixels));
152 for (i=0; i < (long) number_threads; i++)
154 pixels[i]=(MagickPixelPacket *) AcquireQuantumMemory(image->columns,
156 if (pixels[i] == (MagickPixelPacket *) NULL)
157 return(DestroyPixelThreadSet(pixels));
158 for (j=0; j < (long) image->columns; j++)
159 GetMagickPixelPacket(image,&pixels[i][j]);
164 MagickExport Image *AverageImages(const Image *image,ExceptionInfo *exception)
166 #define AverageImageTag "Average/Image"
185 **restrict average_pixels,
192 Ensure the image are the same size.
194 assert(image != (Image *) NULL);
195 assert(image->signature == MagickSignature);
196 if (image->debug != MagickFalse)
197 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
198 assert(exception != (ExceptionInfo *) NULL);
199 assert(exception->signature == MagickSignature);
200 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
201 if ((next->columns != image->columns) || (next->rows != image->rows))
202 ThrowImageException(OptionError,"ImageWidthsOrHeightsDiffer");
204 Initialize average next attributes.
206 average_image=CloneImage(image,image->columns,image->rows,MagickTrue,
208 if (average_image == (Image *) NULL)
209 return((Image *) NULL);
210 if (SetImageStorageClass(average_image,DirectClass) == MagickFalse)
212 InheritException(exception,&average_image->exception);
213 average_image=DestroyImage(average_image);
214 return((Image *) NULL);
216 average_pixels=AcquirePixelThreadSet(image);
217 if (average_pixels == (MagickPixelPacket **) NULL)
219 average_image=DestroyImage(average_image);
220 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
223 Average image pixels.
227 GetMagickPixelPacket(image,&zero);
228 number_images=GetImageListLength(image);
229 average_view=AcquireCacheView(average_image);
230 #if defined(MAGICKCORE_OPENMP_SUPPORT)
231 #pragma omp parallel for schedule(dynamic) shared(progress,status)
233 for (y=0; y < (long) average_image->rows; y++)
245 *restrict average_indexes;
252 register MagickPixelPacket
258 if (status == MagickFalse)
260 q=QueueCacheViewAuthenticPixels(average_view,0,y,average_image->columns,1,
262 if (q == (PixelPacket *) NULL)
267 average_indexes=GetCacheViewAuthenticIndexQueue(average_view);
269 id=GetOpenMPThreadId();
270 average_pixel=average_pixels[id];
271 for (x=0; x < (long) average_image->columns; x++)
272 average_pixel[x]=zero;
274 for (i=0; i < (long) number_images; i++)
276 register const IndexPacket
279 register const PixelPacket
282 image_view=AcquireCacheView(next);
283 p=GetCacheViewVirtualPixels(image_view,0,y,next->columns,1,exception);
284 if (p == (const PixelPacket *) NULL)
286 image_view=DestroyCacheView(image_view);
289 indexes=GetCacheViewVirtualIndexQueue(image_view);
290 for (x=0; x < (long) next->columns; x++)
292 SetMagickPixelPacket(next,p,indexes+x,&pixel);
293 average_pixel[x].red+=QuantumScale*pixel.red;
294 average_pixel[x].green+=QuantumScale*pixel.green;
295 average_pixel[x].blue+=QuantumScale*pixel.blue;
296 average_pixel[x].opacity+=QuantumScale*pixel.opacity;
297 if (average_image->colorspace == CMYKColorspace)
298 average_pixel[x].index+=QuantumScale*pixel.index;
301 image_view=DestroyCacheView(image_view);
302 next=GetNextImageInList(next);
304 for (x=0; x < (long) average_image->columns; x++)
306 average_pixel[x].red=(MagickRealType) (QuantumRange*
307 average_pixel[x].red/number_images);
308 average_pixel[x].green=(MagickRealType) (QuantumRange*
309 average_pixel[x].green/number_images);
310 average_pixel[x].blue=(MagickRealType) (QuantumRange*
311 average_pixel[x].blue/number_images);
312 average_pixel[x].opacity=(MagickRealType) (QuantumRange*
313 average_pixel[x].opacity/number_images);
314 if (average_image->colorspace == CMYKColorspace)
315 average_pixel[x].index=(MagickRealType) (QuantumRange*
316 average_pixel[x].index/number_images);
317 SetPixelPacket(average_image,&average_pixel[x],q,average_indexes+x);
320 if (SyncCacheViewAuthenticPixels(average_view,exception) == MagickFalse)
322 if (image->progress_monitor != (MagickProgressMonitor) NULL)
327 #if defined(MAGICKCORE_OPENMP_SUPPORT)
328 #pragma omp critical (MagickCore_AverageImages)
330 proceed=SetImageProgress(image,AverageImageTag,progress++,
331 average_image->rows);
332 if (proceed == MagickFalse)
336 average_view=DestroyCacheView(average_view);
337 average_pixels=DestroyPixelThreadSet(average_pixels);
338 if (status == MagickFalse)
339 average_image=DestroyImage(average_image);
340 return(average_image);
344 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
348 + G e t I m a g e C h a n n e l E x t r e m a %
352 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
354 % GetImageChannelExtrema() returns the extrema of one or more image channels.
356 % The format of the GetImageChannelExtrema method is:
358 % MagickBooleanType GetImageChannelExtrema(const Image *image,
359 % const ChannelType channel,unsigned long *minima,unsigned long *maxima,
360 % ExceptionInfo *exception)
362 % A description of each parameter follows:
364 % o image: the image.
366 % o channel: the channel.
368 % o minima: the minimum value in the channel.
370 % o maxima: the maximum value in the channel.
372 % o exception: return any errors or warnings in this structure.
376 MagickExport MagickBooleanType GetImageExtrema(const Image *image,
377 unsigned long *minima,unsigned long *maxima,ExceptionInfo *exception)
379 return(GetImageChannelExtrema(image,AllChannels,minima,maxima,exception));
382 MagickExport MagickBooleanType GetImageChannelExtrema(const Image *image,
383 const ChannelType channel,unsigned long *minima,unsigned long *maxima,
384 ExceptionInfo *exception)
393 assert(image != (Image *) NULL);
394 assert(image->signature == MagickSignature);
395 if (image->debug != MagickFalse)
396 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
397 status=GetImageChannelRange(image,channel,&min,&max,exception);
398 *minima=(unsigned long) (min+0.5);
399 *maxima=(unsigned long) (max+0.5);
404 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
408 % G e t I m a g e C h a n n e l M e a n %
412 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
414 % GetImageChannelMean() returns the mean and standard deviation of one or more
417 % The format of the GetImageChannelMean method is:
419 % MagickBooleanType GetImageChannelMean(const Image *image,
420 % const ChannelType channel,double *mean,double *standard_deviation,
421 % ExceptionInfo *exception)
423 % A description of each parameter follows:
425 % o image: the image.
427 % o channel: the channel.
429 % o mean: the average value in the channel.
431 % o standard_deviation: the standard deviation of the channel.
433 % o exception: return any errors or warnings in this structure.
437 MagickExport MagickBooleanType GetImageMean(const Image *image,double *mean,
438 double *standard_deviation,ExceptionInfo *exception)
443 status=GetImageChannelMean(image,AllChannels,mean,standard_deviation,
448 MagickExport MagickBooleanType GetImageChannelMean(const Image *image,
449 const ChannelType channel,double *mean,double *standard_deviation,
450 ExceptionInfo *exception)
458 assert(image != (Image *) NULL);
459 assert(image->signature == MagickSignature);
460 if (image->debug != MagickFalse)
461 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
463 *standard_deviation=0.0;
465 for (y=0; y < (long) image->rows; y++)
467 register const IndexPacket
470 register const PixelPacket
476 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
477 if (p == (const PixelPacket *) NULL)
479 indexes=GetVirtualIndexQueue(image);
480 for (x=0; x < (long) image->columns; x++)
482 if ((channel & RedChannel) != 0)
484 *mean+=GetRedPixelComponent(p);
485 *standard_deviation+=(double) p->red*GetRedPixelComponent(p);
488 if ((channel & GreenChannel) != 0)
490 *mean+=GetGreenPixelComponent(p);
491 *standard_deviation+=(double) p->green*GetGreenPixelComponent(p);
494 if ((channel & BlueChannel) != 0)
496 *mean+=GetBluePixelComponent(p);
497 *standard_deviation+=(double) p->blue*GetBluePixelComponent(p);
500 if ((channel & OpacityChannel) != 0)
502 *mean+=GetOpacityPixelComponent(p);
503 *standard_deviation+=(double) p->opacity*GetOpacityPixelComponent(p);
506 if (((channel & IndexChannel) != 0) &&
507 (image->colorspace == CMYKColorspace))
510 *standard_deviation+=(double) indexes[x]*indexes[x];
516 if (y < (long) image->rows)
521 *standard_deviation/=area;
523 *standard_deviation=sqrt(*standard_deviation-(*mean*(*mean)));
524 return(y == (long) image->rows ? MagickTrue : MagickFalse);
528 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
532 % G e t I m a g e C h a n n e l K u r t o s i s %
536 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
538 % GetImageChannelKurtosis() returns the kurtosis and skewness of one or more
541 % The format of the GetImageChannelKurtosis method is:
543 % MagickBooleanType GetImageChannelKurtosis(const Image *image,
544 % const ChannelType channel,double *kurtosis,double *skewness,
545 % ExceptionInfo *exception)
547 % A description of each parameter follows:
549 % o image: the image.
551 % o channel: the channel.
553 % o kurtosis: the kurtosis of the channel.
555 % o skewness: the skewness of the channel.
557 % o exception: return any errors or warnings in this structure.
561 MagickExport MagickBooleanType GetImageKurtosis(const Image *image,
562 double *kurtosis,double *skewness,ExceptionInfo *exception)
567 status=GetImageChannelKurtosis(image,AllChannels,kurtosis,skewness,
572 MagickExport MagickBooleanType GetImageChannelKurtosis(const Image *image,
573 const ChannelType channel,double *kurtosis,double *skewness,
574 ExceptionInfo *exception)
587 assert(image != (Image *) NULL);
588 assert(image->signature == MagickSignature);
589 if (image->debug != MagickFalse)
590 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
595 standard_deviation=0.0;
598 sum_fourth_power=0.0;
599 for (y=0; y < (long) image->rows; y++)
601 register const IndexPacket
604 register const PixelPacket
610 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
611 if (p == (const PixelPacket *) NULL)
613 indexes=GetVirtualIndexQueue(image);
614 for (x=0; x < (long) image->columns; x++)
616 if ((channel & RedChannel) != 0)
618 mean+=GetRedPixelComponent(p);
619 sum_squares+=(double) p->red*GetRedPixelComponent(p);
620 sum_cubes+=(double) p->red*p->red*GetRedPixelComponent(p);
621 sum_fourth_power+=(double) p->red*p->red*p->red*
622 GetRedPixelComponent(p);
625 if ((channel & GreenChannel) != 0)
627 mean+=GetGreenPixelComponent(p);
628 sum_squares+=(double) p->green*GetGreenPixelComponent(p);
629 sum_cubes+=(double) p->green*p->green*GetGreenPixelComponent(p);
630 sum_fourth_power+=(double) p->green*p->green*p->green*
631 GetGreenPixelComponent(p);
634 if ((channel & BlueChannel) != 0)
636 mean+=GetBluePixelComponent(p);
637 sum_squares+=(double) p->blue*GetBluePixelComponent(p);
638 sum_cubes+=(double) p->blue*p->blue*GetBluePixelComponent(p);
639 sum_fourth_power+=(double) p->blue*p->blue*p->blue*
640 GetBluePixelComponent(p);
643 if ((channel & OpacityChannel) != 0)
645 mean+=GetOpacityPixelComponent(p);
646 sum_squares+=(double) p->opacity*GetOpacityPixelComponent(p);
647 sum_cubes+=(double) p->opacity*p->opacity*GetOpacityPixelComponent(p);
648 sum_fourth_power+=(double) p->opacity*p->opacity*p->opacity*
649 GetOpacityPixelComponent(p);
652 if (((channel & IndexChannel) != 0) &&
653 (image->colorspace == CMYKColorspace))
656 sum_squares+=(double) indexes[x]*indexes[x];
657 sum_cubes+=(double) indexes[x]*indexes[x]*indexes[x];
658 sum_fourth_power+=(double) indexes[x]*indexes[x]*indexes[x]*
665 if (y < (long) image->rows)
672 sum_fourth_power/=area;
674 standard_deviation=sqrt(sum_squares-(mean*mean));
675 if (standard_deviation != 0.0)
677 *kurtosis=sum_fourth_power-4.0*mean*sum_cubes+6.0*mean*mean*sum_squares-
678 3.0*mean*mean*mean*mean;
679 *kurtosis/=standard_deviation*standard_deviation*standard_deviation*
682 *skewness=sum_cubes-3.0*mean*sum_squares+2.0*mean*mean*mean;
683 *skewness/=standard_deviation*standard_deviation*standard_deviation;
685 return(y == (long) image->rows ? MagickTrue : MagickFalse);
689 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
693 % G e t I m a g e C h a n n e l R a n g e %
697 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
699 % GetImageChannelRange() returns the range of one or more image channels.
701 % The format of the GetImageChannelRange method is:
703 % MagickBooleanType GetImageChannelRange(const Image *image,
704 % const ChannelType channel,double *minima,double *maxima,
705 % ExceptionInfo *exception)
707 % A description of each parameter follows:
709 % o image: the image.
711 % o channel: the channel.
713 % o minima: the minimum value in the channel.
715 % o maxima: the maximum value in the channel.
717 % o exception: return any errors or warnings in this structure.
721 MagickExport MagickBooleanType GetImageRange(const Image *image,
722 double *minima,double *maxima,ExceptionInfo *exception)
724 return(GetImageChannelRange(image,AllChannels,minima,maxima,exception));
727 MagickExport MagickBooleanType GetImageChannelRange(const Image *image,
728 const ChannelType channel,double *minima,double *maxima,
729 ExceptionInfo *exception)
737 assert(image != (Image *) NULL);
738 assert(image->signature == MagickSignature);
739 if (image->debug != MagickFalse)
740 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
743 GetMagickPixelPacket(image,&pixel);
744 for (y=0; y < (long) image->rows; y++)
746 register const IndexPacket
749 register const PixelPacket
755 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
756 if (p == (const PixelPacket *) NULL)
758 indexes=GetVirtualIndexQueue(image);
759 for (x=0; x < (long) image->columns; x++)
761 SetMagickPixelPacket(image,p,indexes+x,&pixel);
762 if ((channel & RedChannel) != 0)
764 if (pixel.red < *minima)
765 *minima=(double) pixel.red;
766 if (pixel.red > *maxima)
767 *maxima=(double) pixel.red;
769 if ((channel & GreenChannel) != 0)
771 if (pixel.green < *minima)
772 *minima=(double) pixel.green;
773 if (pixel.green > *maxima)
774 *maxima=(double) pixel.green;
776 if ((channel & BlueChannel) != 0)
778 if (pixel.blue < *minima)
779 *minima=(double) pixel.blue;
780 if (pixel.blue > *maxima)
781 *maxima=(double) pixel.blue;
783 if ((channel & OpacityChannel) != 0)
785 if (pixel.opacity < *minima)
786 *minima=(double) pixel.opacity;
787 if (pixel.opacity > *maxima)
788 *maxima=(double) pixel.opacity;
790 if (((channel & IndexChannel) != 0) &&
791 (image->colorspace == CMYKColorspace))
793 if ((double) indexes[x] < *minima)
794 *minima=(double) indexes[x];
795 if ((double) indexes[x] > *maxima)
796 *maxima=(double) indexes[x];
801 return(y == (long) image->rows ? MagickTrue : MagickFalse);
805 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
809 % 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 %
813 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
815 % GetImageChannelStatistics() returns statistics for each channel in the
816 % image. The statistics include the channel depth, its minima, maxima, mean,
817 % standard deviation, kurtosis and skewness. You can access the red channel
818 % mean, for example, like this:
820 % channel_statistics=GetImageChannelStatistics(image,excepton);
821 % red_mean=channel_statistics[RedChannel].mean;
823 % Use MagickRelinquishMemory() to free the statistics buffer.
825 % The format of the GetImageChannelStatistics method is:
827 % ChannelStatistics *GetImageChannelStatistics(const Image *image,
828 % ExceptionInfo *exception)
830 % A description of each parameter follows:
832 % o image: the image.
834 % o exception: return any errors or warnings in this structure.
838 static inline double MagickMax(const double x,const double y)
845 static inline double MagickMin(const double x,const double y)
852 MagickExport ChannelStatistics *GetImageChannelStatistics(const Image *image,
853 ExceptionInfo *exception)
882 assert(image != (Image *) NULL);
883 assert(image->signature == MagickSignature);
884 if (image->debug != MagickFalse)
885 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
886 length=AllChannels+1UL;
887 channel_statistics=(ChannelStatistics *) AcquireQuantumMemory(length,
888 sizeof(*channel_statistics));
889 if (channel_statistics == (ChannelStatistics *) NULL)
890 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
891 (void) ResetMagickMemory(channel_statistics,0,length*
892 sizeof(*channel_statistics));
893 for (i=0; i <= AllChannels; i++)
895 channel_statistics[i].depth=1;
896 channel_statistics[i].maxima=(-1.0E-37);
897 channel_statistics[i].minima=1.0E+37;
898 channel_statistics[i].mean=0.0;
899 channel_statistics[i].standard_deviation=0.0;
900 channel_statistics[i].kurtosis=0.0;
901 channel_statistics[i].skewness=0.0;
903 for (y=0; y < (long) image->rows; y++)
905 register const IndexPacket
908 register const PixelPacket
914 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
915 if (p == (const PixelPacket *) NULL)
917 indexes=GetVirtualIndexQueue(image);
918 for (x=0; x < (long) image->columns; )
920 if (channel_statistics[RedChannel].depth != MAGICKCORE_QUANTUM_DEPTH)
922 depth=channel_statistics[RedChannel].depth;
923 range=GetQuantumRange(depth);
924 status=p->red != ScaleAnyToQuantum(ScaleQuantumToAny(p->red,range),
925 range) ? MagickTrue : MagickFalse;
926 if (status != MagickFalse)
928 channel_statistics[RedChannel].depth++;
932 if (channel_statistics[GreenChannel].depth != MAGICKCORE_QUANTUM_DEPTH)
934 depth=channel_statistics[GreenChannel].depth;
935 range=GetQuantumRange(depth);
936 status=p->green != ScaleAnyToQuantum(ScaleQuantumToAny(p->green,
937 range),range) ? MagickTrue : MagickFalse;
938 if (status != MagickFalse)
940 channel_statistics[GreenChannel].depth++;
944 if (channel_statistics[BlueChannel].depth != MAGICKCORE_QUANTUM_DEPTH)
946 depth=channel_statistics[BlueChannel].depth;
947 range=GetQuantumRange(depth);
948 status=p->blue != ScaleAnyToQuantum(ScaleQuantumToAny(p->blue,
949 range),range) ? MagickTrue : MagickFalse;
950 if (status != MagickFalse)
952 channel_statistics[BlueChannel].depth++;
956 if (image->matte != MagickFalse)
958 if (channel_statistics[OpacityChannel].depth != MAGICKCORE_QUANTUM_DEPTH)
960 depth=channel_statistics[OpacityChannel].depth;
961 range=GetQuantumRange(depth);
962 status=p->opacity != ScaleAnyToQuantum(ScaleQuantumToAny(
963 p->opacity,range),range) ? MagickTrue : MagickFalse;
964 if (status != MagickFalse)
966 channel_statistics[OpacityChannel].depth++;
971 if (image->colorspace == CMYKColorspace)
973 if (channel_statistics[BlackChannel].depth != MAGICKCORE_QUANTUM_DEPTH)
975 depth=channel_statistics[BlackChannel].depth;
976 range=GetQuantumRange(depth);
977 status=indexes[x] != ScaleAnyToQuantum(ScaleQuantumToAny(
978 indexes[x],range),range) ? MagickTrue : MagickFalse;
979 if (status != MagickFalse)
981 channel_statistics[BlackChannel].depth++;
986 if ((double) p->red < channel_statistics[RedChannel].minima)
987 channel_statistics[RedChannel].minima=(double) GetRedPixelComponent(p);
988 if ((double) p->red > channel_statistics[RedChannel].maxima)
989 channel_statistics[RedChannel].maxima=(double) GetRedPixelComponent(p);
990 channel_statistics[RedChannel].mean+=GetRedPixelComponent(p);
991 channel_statistics[RedChannel].standard_deviation+=(double) p->red*
992 GetRedPixelComponent(p);
993 channel_statistics[RedChannel].kurtosis+=(double) p->red*p->red*
994 p->red*GetRedPixelComponent(p);
995 channel_statistics[RedChannel].skewness+=(double) p->red*p->red*
996 GetRedPixelComponent(p);
997 if ((double) p->green < channel_statistics[GreenChannel].minima)
998 channel_statistics[GreenChannel].minima=(double)
999 GetGreenPixelComponent(p);
1000 if ((double) p->green > channel_statistics[GreenChannel].maxima)
1001 channel_statistics[GreenChannel].maxima=(double)
1002 GetGreenPixelComponent(p);
1003 channel_statistics[GreenChannel].mean+=GetGreenPixelComponent(p);
1004 channel_statistics[GreenChannel].standard_deviation+=(double) p->green*
1005 GetGreenPixelComponent(p);
1006 channel_statistics[GreenChannel].kurtosis+=(double) p->green*p->green*
1007 p->green*GetGreenPixelComponent(p);
1008 channel_statistics[GreenChannel].skewness+=(double) p->green*p->green*
1009 GetGreenPixelComponent(p);
1010 if ((double) p->blue < channel_statistics[BlueChannel].minima)
1011 channel_statistics[BlueChannel].minima=(double)
1012 GetBluePixelComponent(p);
1013 if ((double) p->blue > channel_statistics[BlueChannel].maxima)
1014 channel_statistics[BlueChannel].maxima=(double)
1015 GetBluePixelComponent(p);
1016 channel_statistics[BlueChannel].mean+=GetBluePixelComponent(p);
1017 channel_statistics[BlueChannel].standard_deviation+=(double) p->blue*
1018 GetBluePixelComponent(p);
1019 channel_statistics[BlueChannel].kurtosis+=(double) p->blue*p->blue*
1020 p->blue*GetBluePixelComponent(p);
1021 channel_statistics[BlueChannel].skewness+=(double) p->blue*p->blue*
1022 GetBluePixelComponent(p);
1023 if (image->matte != MagickFalse)
1025 if ((double) p->opacity < channel_statistics[OpacityChannel].minima)
1026 channel_statistics[OpacityChannel].minima=(double)
1027 GetOpacityPixelComponent(p);
1028 if ((double) p->opacity > channel_statistics[OpacityChannel].maxima)
1029 channel_statistics[OpacityChannel].maxima=(double)
1030 GetOpacityPixelComponent(p);
1031 channel_statistics[OpacityChannel].mean+=GetOpacityPixelComponent(p);
1032 channel_statistics[OpacityChannel].standard_deviation+=(double)
1033 p->opacity*GetOpacityPixelComponent(p);
1034 channel_statistics[OpacityChannel].kurtosis+=(double) p->opacity*
1035 p->opacity*p->opacity*GetOpacityPixelComponent(p);
1036 channel_statistics[OpacityChannel].skewness+=(double) p->opacity*
1037 p->opacity*GetOpacityPixelComponent(p);
1039 if (image->colorspace == CMYKColorspace)
1041 if ((double) indexes[x] < channel_statistics[BlackChannel].minima)
1042 channel_statistics[BlackChannel].minima=(double) indexes[x];
1043 if ((double) indexes[x] > channel_statistics[BlackChannel].maxima)
1044 channel_statistics[BlackChannel].maxima=(double) indexes[x];
1045 channel_statistics[BlackChannel].mean+=indexes[x];
1046 channel_statistics[BlackChannel].standard_deviation+=(double)
1047 indexes[x]*indexes[x];
1048 channel_statistics[BlackChannel].kurtosis+=(double) indexes[x]*
1049 indexes[x]*indexes[x]*indexes[x];
1050 channel_statistics[BlackChannel].skewness+=(double) indexes[x]*
1051 indexes[x]*indexes[x];
1057 area=(double) image->columns*image->rows;
1058 for (i=0; i < AllChannels; i++)
1060 channel_statistics[i].mean/=area;
1061 channel_statistics[i].standard_deviation/=area;
1062 channel_statistics[i].kurtosis/=area;
1063 channel_statistics[i].skewness/=area;
1065 for (i=0; i < AllChannels; i++)
1067 channel_statistics[AllChannels].depth=(unsigned long) MagickMax((double)
1068 channel_statistics[AllChannels].depth,(double)
1069 channel_statistics[i].depth);
1070 channel_statistics[AllChannels].minima=MagickMin(
1071 channel_statistics[AllChannels].minima,channel_statistics[i].minima);
1072 channel_statistics[AllChannels].maxima=MagickMax(
1073 channel_statistics[AllChannels].maxima,channel_statistics[i].maxima);
1074 channel_statistics[AllChannels].mean+=channel_statistics[i].mean;
1075 channel_statistics[AllChannels].standard_deviation+=
1076 channel_statistics[i].standard_deviation;
1077 channel_statistics[AllChannels].kurtosis+=channel_statistics[i].kurtosis;
1078 channel_statistics[AllChannels].skewness+=channel_statistics[i].skewness;
1081 if (image->colorspace == CMYKColorspace)
1083 channel_statistics[AllChannels].mean/=channels;
1084 channel_statistics[AllChannels].standard_deviation/=channels;
1085 channel_statistics[AllChannels].kurtosis/=channels;
1086 channel_statistics[AllChannels].skewness/=channels;
1087 for (i=0; i <= AllChannels; i++)
1090 sum_squares=channel_statistics[i].standard_deviation;
1092 sum_cubes=channel_statistics[i].skewness;
1093 channel_statistics[i].standard_deviation=sqrt(
1094 channel_statistics[i].standard_deviation-
1095 (channel_statistics[i].mean*channel_statistics[i].mean));
1096 if (channel_statistics[i].standard_deviation == 0.0)
1098 channel_statistics[i].kurtosis=0.0;
1099 channel_statistics[i].skewness=0.0;
1103 channel_statistics[i].skewness=(channel_statistics[i].skewness-
1104 3.0*channel_statistics[i].mean*sum_squares+
1105 2.0*channel_statistics[i].mean*channel_statistics[i].mean*
1106 channel_statistics[i].mean)/
1107 (channel_statistics[i].standard_deviation*
1108 channel_statistics[i].standard_deviation*
1109 channel_statistics[i].standard_deviation);
1110 channel_statistics[i].kurtosis=(channel_statistics[i].kurtosis-
1111 4.0*channel_statistics[i].mean*sum_cubes+
1112 6.0*channel_statistics[i].mean*channel_statistics[i].mean*sum_squares-
1113 3.0*channel_statistics[i].mean*channel_statistics[i].mean*
1114 1.0*channel_statistics[i].mean*channel_statistics[i].mean)/
1115 (channel_statistics[i].standard_deviation*
1116 channel_statistics[i].standard_deviation*
1117 channel_statistics[i].standard_deviation*
1118 channel_statistics[i].standard_deviation)-3.0;
1121 return(channel_statistics);