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 Statistical 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/random-private.h"
83 #include "magick/segment.h"
84 #include "magick/semaphore.h"
85 #include "magick/signature-private.h"
86 #include "magick/statistic.h"
87 #include "magick/string_.h"
88 #include "magick/thread-private.h"
89 #include "magick/timer.h"
90 #include "magick/utility.h"
91 #include "magick/version.h"
94 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98 % E v a l u a t e I m a g e %
102 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104 % EvaluateImage() applies a value to the image with an arithmetic, relational,
105 % or logical operator to an image. Use these operations to lighten or darken
106 % an image, to increase or decrease contrast in an image, or to produce the
107 % "negative" of an image.
109 % The format of the EvaluateImageChannel method is:
111 % MagickBooleanType EvaluateImage(Image *image,
112 % const MagickEvaluateOperator op,const double value,
113 % ExceptionInfo *exception)
114 % MagickBooleanType EvaluateImages(Image *images,
115 % const MagickEvaluateOperator op,const double value,
116 % ExceptionInfo *exception)
117 % MagickBooleanType EvaluateImageChannel(Image *image,
118 % const ChannelType channel,const MagickEvaluateOperator op,
119 % const double value,ExceptionInfo *exception)
121 % A description of each parameter follows:
123 % o image: the image.
125 % o channel: the channel.
127 % o op: A channel op.
129 % o value: A value value.
131 % o exception: return any errors or warnings in this structure.
135 static MagickPixelPacket **DestroyPixelThreadSet(MagickPixelPacket **pixels)
140 assert(pixels != (MagickPixelPacket **) NULL);
141 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
142 if (pixels[i] != (MagickPixelPacket *) NULL)
143 pixels[i]=(MagickPixelPacket *) RelinquishMagickMemory(pixels[i]);
144 pixels=(MagickPixelPacket **) RelinquishAlignedMemory(pixels);
148 static MagickPixelPacket **AcquirePixelThreadSet(const Image *image)
160 number_threads=GetOpenMPMaximumThreads();
161 pixels=(MagickPixelPacket **) AcquireAlignedMemory(number_threads,
163 if (pixels == (MagickPixelPacket **) NULL)
164 return((MagickPixelPacket **) NULL);
165 (void) ResetMagickMemory(pixels,0,number_threads*sizeof(*pixels));
166 for (i=0; i < (ssize_t) number_threads; i++)
168 pixels[i]=(MagickPixelPacket *) AcquireQuantumMemory(image->columns,
170 if (pixels[i] == (MagickPixelPacket *) NULL)
171 return(DestroyPixelThreadSet(pixels));
172 for (j=0; j < (ssize_t) image->columns; j++)
173 GetMagickPixelPacket(image,&pixels[i][j]);
178 static inline double MagickMax(const double x,const double y)
185 static inline double MagickMin(const double x,const double y)
192 static MagickRealType ApplyEvaluateOperator(RandomInfo *random_info,
193 Quantum pixel,const MagickEvaluateOperator op,const MagickRealType value)
201 case UndefinedEvaluateOperator:
203 case AbsEvaluateOperator:
205 result=(MagickRealType) fabs((double) (pixel+value));
208 case AddEvaluateOperator:
210 result=(MagickRealType) (pixel+value);
213 case AddModulusEvaluateOperator:
216 This returns a 'floored modulus' of the addition which is a
217 positive result. It differs from % or fmod() which returns a
218 'truncated modulus' result, where floor() is replaced by trunc()
219 and could return a negative result (which is clipped).
222 result-=(QuantumRange+1.0)*floor((double) result/(QuantumRange+1.0));
225 case AndEvaluateOperator:
227 result=(MagickRealType) ((size_t) pixel & (size_t)
231 case CosineEvaluateOperator:
233 result=(MagickRealType) (QuantumRange*(0.5*cos((double) (2.0*MagickPI*
234 QuantumScale*pixel*value))+0.5));
237 case DivideEvaluateOperator:
239 result=pixel/(value == 0.0 ? 1.0 : value);
242 case GaussianNoiseEvaluateOperator:
244 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
245 GaussianNoise,value);
248 case ImpulseNoiseEvaluateOperator:
250 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
254 case LaplacianNoiseEvaluateOperator:
256 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
257 LaplacianNoise,value);
260 case LeftShiftEvaluateOperator:
262 result=(MagickRealType) ((size_t) pixel << (size_t)
266 case LogEvaluateOperator:
268 result=(MagickRealType) (QuantumRange*log((double) (QuantumScale*value*
269 pixel+1.0))/log((double) (value+1.0)));
272 case MaxEvaluateOperator:
274 result=(MagickRealType) MagickMax((double) pixel,value);
277 case MeanEvaluateOperator:
279 result=(MagickRealType) (pixel+value);
282 case MinEvaluateOperator:
284 result=(MagickRealType) MagickMin((double) pixel,value);
287 case MultiplicativeNoiseEvaluateOperator:
289 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
290 MultiplicativeGaussianNoise,value);
293 case MultiplyEvaluateOperator:
295 result=(MagickRealType) (value*pixel);
298 case OrEvaluateOperator:
300 result=(MagickRealType) ((size_t) pixel | (size_t)
304 case PoissonNoiseEvaluateOperator:
306 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
310 case PowEvaluateOperator:
312 result=(MagickRealType) (QuantumRange*pow((double) (QuantumScale*pixel),
316 case RightShiftEvaluateOperator:
318 result=(MagickRealType) ((size_t) pixel >> (size_t)
322 case SetEvaluateOperator:
327 case SineEvaluateOperator:
329 result=(MagickRealType) (QuantumRange*(0.5*sin((double) (2.0*MagickPI*
330 QuantumScale*pixel*value))+0.5));
333 case SubtractEvaluateOperator:
335 result=(MagickRealType) (pixel-value);
338 case ThresholdEvaluateOperator:
340 result=(MagickRealType) (((MagickRealType) pixel <= value) ? 0 :
344 case ThresholdBlackEvaluateOperator:
346 result=(MagickRealType) (((MagickRealType) pixel <= value) ? 0 : pixel);
349 case ThresholdWhiteEvaluateOperator:
351 result=(MagickRealType) (((MagickRealType) pixel > value) ? QuantumRange :
355 case UniformNoiseEvaluateOperator:
357 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
361 case XorEvaluateOperator:
363 result=(MagickRealType) ((size_t) pixel ^ (size_t)
371 MagickExport MagickBooleanType EvaluateImage(Image *image,
372 const MagickEvaluateOperator op,const double value,ExceptionInfo *exception)
377 status=EvaluateImageChannel(image,AllChannels,op,value,exception);
381 MagickExport Image *EvaluateImages(const Image *images,
382 const MagickEvaluateOperator op,ExceptionInfo *exception)
384 #define EvaluateImageTag "Evaluate/Image"
402 **restrict evaluate_pixels,
406 **restrict random_info;
415 Ensure the image are the same size.
417 assert(images != (Image *) NULL);
418 assert(images->signature == MagickSignature);
419 if (images->debug != MagickFalse)
420 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
421 assert(exception != (ExceptionInfo *) NULL);
422 assert(exception->signature == MagickSignature);
423 for (next=images; next != (Image *) NULL; next=GetNextImageInList(next))
424 if ((next->columns != images->columns) || (next->rows != images->rows))
426 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
427 "ImageWidthsOrHeightsDiffer","`%s'",images->filename);
428 return((Image *) NULL);
431 Initialize evaluate next attributes.
433 evaluate_image=CloneImage(images,images->columns,images->rows,MagickTrue,
435 if (evaluate_image == (Image *) NULL)
436 return((Image *) NULL);
437 if (SetImageStorageClass(evaluate_image,DirectClass) == MagickFalse)
439 InheritException(exception,&evaluate_image->exception);
440 evaluate_image=DestroyImage(evaluate_image);
441 return((Image *) NULL);
443 evaluate_pixels=AcquirePixelThreadSet(images);
444 if (evaluate_pixels == (MagickPixelPacket **) NULL)
446 evaluate_image=DestroyImage(evaluate_image);
447 (void) ThrowMagickException(exception,GetMagickModule(),
448 ResourceLimitError,"MemoryAllocationFailed","`%s'",images->filename);
449 return((Image *) NULL);
452 Evaluate image pixels.
456 GetMagickPixelPacket(images,&zero);
457 random_info=AcquireRandomInfoThreadSet();
458 number_images=GetImageListLength(images);
459 evaluate_view=AcquireCacheView(evaluate_image);
460 #if defined(MAGICKCORE_OPENMP_SUPPORT)
461 #pragma omp parallel for schedule(dynamic) shared(progress,status)
463 for (y=0; y < (ssize_t) evaluate_image->rows; y++)
478 *restrict evaluate_indexes;
484 register MagickPixelPacket
490 if (status == MagickFalse)
492 q=QueueCacheViewAuthenticPixels(evaluate_view,0,y,evaluate_image->columns,1,
494 if (q == (PixelPacket *) NULL)
499 evaluate_indexes=GetCacheViewAuthenticIndexQueue(evaluate_view);
501 id=GetOpenMPThreadId();
502 evaluate_pixel=evaluate_pixels[id];
503 for (x=0; x < (ssize_t) evaluate_image->columns; x++)
504 evaluate_pixel[x]=zero;
506 for (i=0; i < (ssize_t) number_images; i++)
508 register const IndexPacket
511 register const PixelPacket
514 image_view=AcquireCacheView(next);
515 p=GetCacheViewVirtualPixels(image_view,0,y,next->columns,1,exception);
516 if (p == (const PixelPacket *) NULL)
518 image_view=DestroyCacheView(image_view);
521 indexes=GetCacheViewVirtualIndexQueue(image_view);
522 for (x=0; x < (ssize_t) next->columns; x++)
524 evaluate_pixel[x].red=ApplyEvaluateOperator(random_info[id],p->red,
525 i == 0 ? AddEvaluateOperator : op,evaluate_pixel[x].red);
526 evaluate_pixel[x].green=ApplyEvaluateOperator(random_info[id],p->green,
527 i == 0 ? AddEvaluateOperator : op,evaluate_pixel[x].green);
528 evaluate_pixel[x].blue=ApplyEvaluateOperator(random_info[id],p->blue,
529 i == 0 ? AddEvaluateOperator : op,evaluate_pixel[x].blue);
530 evaluate_pixel[x].opacity=ApplyEvaluateOperator(random_info[id],
531 p->opacity,i == 0 ? AddEvaluateOperator : op,
532 evaluate_pixel[x].opacity);
533 if (evaluate_image->colorspace == CMYKColorspace)
534 evaluate_pixel[x].index=ApplyEvaluateOperator(random_info[id],
535 indexes[x],i == 0 ? AddEvaluateOperator : op,
536 evaluate_pixel[x].index);
539 image_view=DestroyCacheView(image_view);
540 next=GetNextImageInList(next);
542 if (op == MeanEvaluateOperator)
543 for (x=0; x < (ssize_t) evaluate_image->columns; x++)
545 evaluate_pixel[x].red/=number_images;
546 evaluate_pixel[x].green/=number_images;
547 evaluate_pixel[x].blue/=number_images;
548 evaluate_pixel[x].opacity/=number_images;
549 evaluate_pixel[x].index/=number_images;
551 for (x=0; x < (ssize_t) evaluate_image->columns; x++)
553 q->red=ClampToQuantum(evaluate_pixel[x].red);
554 q->green=ClampToQuantum(evaluate_pixel[x].green);
555 q->blue=ClampToQuantum(evaluate_pixel[x].blue);
556 if (evaluate_image->matte == MagickFalse)
557 q->opacity=ClampToQuantum(evaluate_pixel[x].opacity);
559 q->opacity=ClampToQuantum(QuantumRange-evaluate_pixel[x].opacity);
560 if (evaluate_image->colorspace == CMYKColorspace)
561 evaluate_indexes[x]=ClampToQuantum(evaluate_pixel[x].index);
564 if (SyncCacheViewAuthenticPixels(evaluate_view,exception) == MagickFalse)
566 if (images->progress_monitor != (MagickProgressMonitor) NULL)
571 #if defined(MAGICKCORE_OPENMP_SUPPORT)
572 #pragma omp critical (MagickCore_EvaluateImages)
574 proceed=SetImageProgress(images,EvaluateImageTag,progress++,
575 evaluate_image->rows);
576 if (proceed == MagickFalse)
580 evaluate_view=DestroyCacheView(evaluate_view);
581 evaluate_pixels=DestroyPixelThreadSet(evaluate_pixels);
582 random_info=DestroyRandomInfoThreadSet(random_info);
583 if (status == MagickFalse)
584 evaluate_image=DestroyImage(evaluate_image);
585 return(evaluate_image);
588 MagickExport MagickBooleanType EvaluateImageChannel(Image *image,
589 const ChannelType channel,const MagickEvaluateOperator op,const double value,
590 ExceptionInfo *exception)
602 **restrict random_info;
607 assert(image != (Image *) NULL);
608 assert(image->signature == MagickSignature);
609 if (image->debug != MagickFalse)
610 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
611 assert(exception != (ExceptionInfo *) NULL);
612 assert(exception->signature == MagickSignature);
613 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
615 InheritException(exception,&image->exception);
620 random_info=AcquireRandomInfoThreadSet();
621 image_view=AcquireCacheView(image);
622 #if defined(MAGICKCORE_OPENMP_SUPPORT)
623 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
625 for (y=0; y < (ssize_t) image->rows; y++)
639 if (status == MagickFalse)
641 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
642 if (q == (PixelPacket *) NULL)
647 indexes=GetCacheViewAuthenticIndexQueue(image_view);
648 id=GetOpenMPThreadId();
649 for (x=0; x < (ssize_t) image->columns; x++)
651 if ((channel & RedChannel) != 0)
652 q->red=ClampToQuantum(ApplyEvaluateOperator(random_info[id],q->red,op,
654 if ((channel & GreenChannel) != 0)
655 q->green=ClampToQuantum(ApplyEvaluateOperator(random_info[id],q->green,
657 if ((channel & BlueChannel) != 0)
658 q->blue=ClampToQuantum(ApplyEvaluateOperator(random_info[id],q->blue,op,
660 if ((channel & OpacityChannel) != 0)
662 if (image->matte == MagickFalse)
663 q->opacity=ClampToQuantum(ApplyEvaluateOperator(random_info[id],
664 q->opacity,op,value));
666 q->opacity=ClampToQuantum(QuantumRange-ApplyEvaluateOperator(
667 random_info[id],(Quantum) GetAlphaPixelComponent(q),op,value));
669 if (((channel & IndexChannel) != 0) && (indexes != (IndexPacket *) NULL))
670 indexes[x]=(IndexPacket) ClampToQuantum(ApplyEvaluateOperator(
671 random_info[id],indexes[x],op,value));
674 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
676 if (image->progress_monitor != (MagickProgressMonitor) NULL)
681 #if defined(MAGICKCORE_OPENMP_SUPPORT)
682 #pragma omp critical (MagickCore_EvaluateImageChannel)
684 proceed=SetImageProgress(image,EvaluateImageTag,progress++,image->rows);
685 if (proceed == MagickFalse)
689 image_view=DestroyCacheView(image_view);
690 random_info=DestroyRandomInfoThreadSet(random_info);
695 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
699 % F u n c t i o n I m a g e %
703 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
705 % FunctionImage() applies a value to the image with an arithmetic, relational,
706 % or logical operator to an image. Use these operations to lighten or darken
707 % an image, to increase or decrease contrast in an image, or to produce the
708 % "negative" of an image.
710 % The format of the FunctionImageChannel method is:
712 % MagickBooleanType FunctionImage(Image *image,
713 % const MagickFunction function,const ssize_t number_parameters,
714 % const double *parameters,ExceptionInfo *exception)
715 % MagickBooleanType FunctionImageChannel(Image *image,
716 % const ChannelType channel,const MagickFunction function,
717 % const ssize_t number_parameters,const double *argument,
718 % ExceptionInfo *exception)
720 % A description of each parameter follows:
722 % o image: the image.
724 % o channel: the channel.
726 % o function: A channel function.
728 % o parameters: one or more parameters.
730 % o exception: return any errors or warnings in this structure.
734 static Quantum ApplyFunction(Quantum pixel,const MagickFunction function,
735 const size_t number_parameters,const double *parameters,
736 ExceptionInfo *exception)
748 case PolynomialFunction:
752 * Parameters: polynomial constants, highest to lowest order
753 * For example: c0*x^3 + c1*x^2 + c2*x + c3
756 for (i=0; i < (ssize_t) number_parameters; i++)
757 result = result*QuantumScale*pixel + parameters[i];
758 result *= QuantumRange;
761 case SinusoidFunction:
764 * Parameters: Freq, Phase, Ampl, bias
766 double freq,phase,ampl,bias;
767 freq = ( number_parameters >= 1 ) ? parameters[0] : 1.0;
768 phase = ( number_parameters >= 2 ) ? parameters[1] : 0.0;
769 ampl = ( number_parameters >= 3 ) ? parameters[2] : 0.5;
770 bias = ( number_parameters >= 4 ) ? parameters[3] : 0.5;
771 result=(MagickRealType) (QuantumRange*(ampl*sin((double) (2.0*MagickPI*
772 (freq*QuantumScale*pixel + phase/360.0) )) + bias ) );
777 /* Arcsin Function (peged at range limits for invalid results)
778 * Parameters: Width, Center, Range, Bias
780 double width,range,center,bias;
781 width = ( number_parameters >= 1 ) ? parameters[0] : 1.0;
782 center = ( number_parameters >= 2 ) ? parameters[1] : 0.5;
783 range = ( number_parameters >= 3 ) ? parameters[2] : 1.0;
784 bias = ( number_parameters >= 4 ) ? parameters[3] : 0.5;
785 result = 2.0/width*(QuantumScale*pixel - center);
786 if ( result <= -1.0 )
787 result = bias - range/2.0;
788 else if ( result >= 1.0 )
789 result = bias + range/2.0;
791 result=range/MagickPI*asin((double)result) + bias;
792 result *= QuantumRange;
798 * Parameters: Slope, Center, Range, Bias
800 double slope,range,center,bias;
801 slope = ( number_parameters >= 1 ) ? parameters[0] : 1.0;
802 center = ( number_parameters >= 2 ) ? parameters[1] : 0.5;
803 range = ( number_parameters >= 3 ) ? parameters[2] : 1.0;
804 bias = ( number_parameters >= 4 ) ? parameters[3] : 0.5;
805 result = MagickPI*slope*(QuantumScale*pixel - center);
806 result=(MagickRealType) (QuantumRange*(range/MagickPI*atan((double)
810 case UndefinedFunction:
813 return(ClampToQuantum(result));
816 MagickExport MagickBooleanType FunctionImage(Image *image,
817 const MagickFunction function,const size_t number_parameters,
818 const double *parameters,ExceptionInfo *exception)
823 status=FunctionImageChannel(image,AllChannels,function,number_parameters,
824 parameters,exception);
828 MagickExport MagickBooleanType FunctionImageChannel(Image *image,
829 const ChannelType channel,const MagickFunction function,
830 const size_t number_parameters,const double *parameters,
831 ExceptionInfo *exception)
833 #define FunctionImageTag "Function/Image "
847 assert(image != (Image *) NULL);
848 assert(image->signature == MagickSignature);
849 if (image->debug != MagickFalse)
850 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
851 assert(exception != (ExceptionInfo *) NULL);
852 assert(exception->signature == MagickSignature);
853 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
855 InheritException(exception,&image->exception);
860 image_view=AcquireCacheView(image);
861 #if defined(MAGICKCORE_OPENMP_SUPPORT)
862 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
864 for (y=0; y < (ssize_t) image->rows; y++)
875 if (status == MagickFalse)
877 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
878 if (q == (PixelPacket *) NULL)
883 indexes=GetCacheViewAuthenticIndexQueue(image_view);
884 for (x=0; x < (ssize_t) image->columns; x++)
886 if ((channel & RedChannel) != 0)
887 q->red=ApplyFunction(q->red,function,number_parameters,parameters,
889 if ((channel & GreenChannel) != 0)
890 q->green=ApplyFunction(q->green,function,number_parameters,parameters,
892 if ((channel & BlueChannel) != 0)
893 q->blue=ApplyFunction(q->blue,function,number_parameters,parameters,
895 if ((channel & OpacityChannel) != 0)
897 if (image->matte == MagickFalse)
898 q->opacity=ApplyFunction(q->opacity,function,number_parameters,
899 parameters,exception);
901 q->opacity=(Quantum) QuantumRange-ApplyFunction((Quantum)
902 GetAlphaPixelComponent(q),function,number_parameters,parameters,
905 if (((channel & IndexChannel) != 0) && (indexes != (IndexPacket *) NULL))
906 indexes[x]=(IndexPacket) ApplyFunction(indexes[x],function,
907 number_parameters,parameters,exception);
910 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
912 if (image->progress_monitor != (MagickProgressMonitor) NULL)
917 #if defined(MAGICKCORE_OPENMP_SUPPORT)
918 #pragma omp critical (MagickCore_FunctionImageChannel)
920 proceed=SetImageProgress(image,FunctionImageTag,progress++,image->rows);
921 if (proceed == MagickFalse)
925 image_view=DestroyCacheView(image_view);
930 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
934 + G e t I m a g e C h a n n e l E x t r e m a %
938 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
940 % GetImageChannelExtrema() returns the extrema of one or more image channels.
942 % The format of the GetImageChannelExtrema method is:
944 % MagickBooleanType GetImageChannelExtrema(const Image *image,
945 % const ChannelType channel,size_t *minima,size_t *maxima,
946 % ExceptionInfo *exception)
948 % A description of each parameter follows:
950 % o image: the image.
952 % o channel: the channel.
954 % o minima: the minimum value in the channel.
956 % o maxima: the maximum value in the channel.
958 % o exception: return any errors or warnings in this structure.
962 MagickExport MagickBooleanType GetImageExtrema(const Image *image,
963 size_t *minima,size_t *maxima,ExceptionInfo *exception)
965 return(GetImageChannelExtrema(image,AllChannels,minima,maxima,exception));
968 MagickExport MagickBooleanType GetImageChannelExtrema(const Image *image,
969 const ChannelType channel,size_t *minima,size_t *maxima,
970 ExceptionInfo *exception)
979 assert(image != (Image *) NULL);
980 assert(image->signature == MagickSignature);
981 if (image->debug != MagickFalse)
982 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
983 status=GetImageChannelRange(image,channel,&min,&max,exception);
984 *minima=(size_t) ceil(min-0.5);
985 *maxima=(size_t) floor(max+0.5);
990 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
994 % G e t I m a g e C h a n n e l M e a n %
998 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1000 % GetImageChannelMean() returns the mean and standard deviation of one or more
1003 % The format of the GetImageChannelMean method is:
1005 % MagickBooleanType GetImageChannelMean(const Image *image,
1006 % const ChannelType channel,double *mean,double *standard_deviation,
1007 % ExceptionInfo *exception)
1009 % A description of each parameter follows:
1011 % o image: the image.
1013 % o channel: the channel.
1015 % o mean: the average value in the channel.
1017 % o standard_deviation: the standard deviation of the channel.
1019 % o exception: return any errors or warnings in this structure.
1023 MagickExport MagickBooleanType GetImageMean(const Image *image,double *mean,
1024 double *standard_deviation,ExceptionInfo *exception)
1029 status=GetImageChannelMean(image,AllChannels,mean,standard_deviation,
1034 MagickExport MagickBooleanType GetImageChannelMean(const Image *image,
1035 const ChannelType channel,double *mean,double *standard_deviation,
1036 ExceptionInfo *exception)
1039 *channel_statistics;
1044 assert(image != (Image *) NULL);
1045 assert(image->signature == MagickSignature);
1046 if (image->debug != MagickFalse)
1047 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1048 channel_statistics=GetImageChannelStatistics(image,exception);
1049 if (channel_statistics == (ChannelStatistics *) NULL)
1050 return(MagickFalse);
1052 channel_statistics[AllChannels].mean=0.0;
1053 channel_statistics[AllChannels].standard_deviation=0.0;
1054 if ((channel & RedChannel) != 0)
1056 channel_statistics[AllChannels].mean+=
1057 channel_statistics[RedChannel].mean;
1058 channel_statistics[AllChannels].standard_deviation+=
1059 channel_statistics[RedChannel].variance-
1060 channel_statistics[RedChannel].mean*
1061 channel_statistics[RedChannel].mean;
1064 if ((channel & GreenChannel) != 0)
1066 channel_statistics[AllChannels].mean+=
1067 channel_statistics[GreenChannel].mean;
1068 channel_statistics[AllChannels].standard_deviation+=
1069 channel_statistics[GreenChannel].variance-
1070 channel_statistics[GreenChannel].mean*
1071 channel_statistics[GreenChannel].mean;
1074 if ((channel & BlueChannel) != 0)
1076 channel_statistics[AllChannels].mean+=
1077 channel_statistics[BlueChannel].mean;
1078 channel_statistics[AllChannels].standard_deviation+=
1079 channel_statistics[BlueChannel].variance-
1080 channel_statistics[BlueChannel].mean*
1081 channel_statistics[BlueChannel].mean;
1084 if (((channel & OpacityChannel) != 0) &&
1085 (image->matte != MagickFalse))
1087 channel_statistics[AllChannels].mean+=
1088 channel_statistics[OpacityChannel].mean;
1089 channel_statistics[AllChannels].standard_deviation+=
1090 channel_statistics[OpacityChannel].variance-
1091 channel_statistics[OpacityChannel].mean*
1092 channel_statistics[OpacityChannel].mean;
1095 if (((channel & IndexChannel) != 0) &&
1096 (image->colorspace == CMYKColorspace))
1098 channel_statistics[AllChannels].mean+=
1099 channel_statistics[BlackChannel].mean;
1100 channel_statistics[AllChannels].standard_deviation+=
1101 channel_statistics[BlackChannel].variance-
1102 channel_statistics[BlackChannel].mean*
1103 channel_statistics[BlackChannel].mean;
1106 channel_statistics[AllChannels].mean/=channels;
1107 channel_statistics[AllChannels].standard_deviation=
1108 sqrt(channel_statistics[AllChannels].standard_deviation/channels);
1109 *mean=channel_statistics[AllChannels].mean;
1110 *standard_deviation=channel_statistics[AllChannels].standard_deviation;
1111 channel_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1112 channel_statistics);
1117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1121 % G e t I m a g e C h a n n e l K u r t o s i s %
1125 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1127 % GetImageChannelKurtosis() returns the kurtosis and skewness of one or more
1130 % The format of the GetImageChannelKurtosis method is:
1132 % MagickBooleanType GetImageChannelKurtosis(const Image *image,
1133 % const ChannelType channel,double *kurtosis,double *skewness,
1134 % ExceptionInfo *exception)
1136 % A description of each parameter follows:
1138 % o image: the image.
1140 % o channel: the channel.
1142 % o kurtosis: the kurtosis of the channel.
1144 % o skewness: the skewness of the channel.
1146 % o exception: return any errors or warnings in this structure.
1150 MagickExport MagickBooleanType GetImageKurtosis(const Image *image,
1151 double *kurtosis,double *skewness,ExceptionInfo *exception)
1156 status=GetImageChannelKurtosis(image,AllChannels,kurtosis,skewness,
1161 MagickExport MagickBooleanType GetImageChannelKurtosis(const Image *image,
1162 const ChannelType channel,double *kurtosis,double *skewness,
1163 ExceptionInfo *exception)
1176 assert(image != (Image *) NULL);
1177 assert(image->signature == MagickSignature);
1178 if (image->debug != MagickFalse)
1179 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1184 standard_deviation=0.0;
1187 sum_fourth_power=0.0;
1188 for (y=0; y < (ssize_t) image->rows; y++)
1190 register const IndexPacket
1193 register const PixelPacket
1199 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1200 if (p == (const PixelPacket *) NULL)
1202 indexes=GetVirtualIndexQueue(image);
1203 for (x=0; x < (ssize_t) image->columns; x++)
1205 if ((channel & RedChannel) != 0)
1207 mean+=GetRedPixelComponent(p);
1208 sum_squares+=(double) p->red*GetRedPixelComponent(p);
1209 sum_cubes+=(double) p->red*p->red*GetRedPixelComponent(p);
1210 sum_fourth_power+=(double) p->red*p->red*p->red*
1211 GetRedPixelComponent(p);
1214 if ((channel & GreenChannel) != 0)
1216 mean+=GetGreenPixelComponent(p);
1217 sum_squares+=(double) p->green*GetGreenPixelComponent(p);
1218 sum_cubes+=(double) p->green*p->green*GetGreenPixelComponent(p);
1219 sum_fourth_power+=(double) p->green*p->green*p->green*
1220 GetGreenPixelComponent(p);
1223 if ((channel & BlueChannel) != 0)
1225 mean+=GetBluePixelComponent(p);
1226 sum_squares+=(double) p->blue*GetBluePixelComponent(p);
1227 sum_cubes+=(double) p->blue*p->blue*GetBluePixelComponent(p);
1228 sum_fourth_power+=(double) p->blue*p->blue*p->blue*
1229 GetBluePixelComponent(p);
1232 if ((channel & OpacityChannel) != 0)
1234 mean+=GetOpacityPixelComponent(p);
1235 sum_squares+=(double) p->opacity*GetOpacityPixelComponent(p);
1236 sum_cubes+=(double) p->opacity*p->opacity*GetOpacityPixelComponent(p);
1237 sum_fourth_power+=(double) p->opacity*p->opacity*p->opacity*
1238 GetOpacityPixelComponent(p);
1241 if (((channel & IndexChannel) != 0) &&
1242 (image->colorspace == CMYKColorspace))
1245 sum_squares+=(double) indexes[x]*indexes[x];
1246 sum_cubes+=(double) indexes[x]*indexes[x]*indexes[x];
1247 sum_fourth_power+=(double) indexes[x]*indexes[x]*indexes[x]*
1254 if (y < (ssize_t) image->rows)
1255 return(MagickFalse);
1261 sum_fourth_power/=area;
1263 standard_deviation=sqrt(sum_squares-(mean*mean));
1264 if (standard_deviation != 0.0)
1266 *kurtosis=sum_fourth_power-4.0*mean*sum_cubes+6.0*mean*mean*sum_squares-
1267 3.0*mean*mean*mean*mean;
1268 *kurtosis/=standard_deviation*standard_deviation*standard_deviation*
1271 *skewness=sum_cubes-3.0*mean*sum_squares+2.0*mean*mean*mean;
1272 *skewness/=standard_deviation*standard_deviation*standard_deviation;
1274 return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse);
1278 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1282 % G e t I m a g e C h a n n e l R a n g e %
1286 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1288 % GetImageChannelRange() returns the range of one or more image channels.
1290 % The format of the GetImageChannelRange method is:
1292 % MagickBooleanType GetImageChannelRange(const Image *image,
1293 % const ChannelType channel,double *minima,double *maxima,
1294 % ExceptionInfo *exception)
1296 % A description of each parameter follows:
1298 % o image: the image.
1300 % o channel: the channel.
1302 % o minima: the minimum value in the channel.
1304 % o maxima: the maximum value in the channel.
1306 % o exception: return any errors or warnings in this structure.
1310 MagickExport MagickBooleanType GetImageRange(const Image *image,
1311 double *minima,double *maxima,ExceptionInfo *exception)
1313 return(GetImageChannelRange(image,AllChannels,minima,maxima,exception));
1316 MagickExport MagickBooleanType GetImageChannelRange(const Image *image,
1317 const ChannelType channel,double *minima,double *maxima,
1318 ExceptionInfo *exception)
1326 assert(image != (Image *) NULL);
1327 assert(image->signature == MagickSignature);
1328 if (image->debug != MagickFalse)
1329 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1332 GetMagickPixelPacket(image,&pixel);
1333 for (y=0; y < (ssize_t) image->rows; y++)
1335 register const IndexPacket
1338 register const PixelPacket
1344 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1345 if (p == (const PixelPacket *) NULL)
1347 indexes=GetVirtualIndexQueue(image);
1348 for (x=0; x < (ssize_t) image->columns; x++)
1350 SetMagickPixelPacket(image,p,indexes+x,&pixel);
1351 if ((channel & RedChannel) != 0)
1353 if (pixel.red < *minima)
1354 *minima=(double) pixel.red;
1355 if (pixel.red > *maxima)
1356 *maxima=(double) pixel.red;
1358 if ((channel & GreenChannel) != 0)
1360 if (pixel.green < *minima)
1361 *minima=(double) pixel.green;
1362 if (pixel.green > *maxima)
1363 *maxima=(double) pixel.green;
1365 if ((channel & BlueChannel) != 0)
1367 if (pixel.blue < *minima)
1368 *minima=(double) pixel.blue;
1369 if (pixel.blue > *maxima)
1370 *maxima=(double) pixel.blue;
1372 if ((channel & OpacityChannel) != 0)
1374 if (pixel.opacity < *minima)
1375 *minima=(double) pixel.opacity;
1376 if (pixel.opacity > *maxima)
1377 *maxima=(double) pixel.opacity;
1379 if (((channel & IndexChannel) != 0) &&
1380 (image->colorspace == CMYKColorspace))
1382 if ((double) indexes[x] < *minima)
1383 *minima=(double) indexes[x];
1384 if ((double) indexes[x] > *maxima)
1385 *maxima=(double) indexes[x];
1390 return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse);
1394 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1398 % 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 %
1402 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1404 % GetImageChannelStatistics() returns statistics for each channel in the
1405 % image. The statistics include the channel depth, its minima, maxima, mean,
1406 % standard deviation, kurtosis and skewness. You can access the red channel
1407 % mean, for example, like this:
1409 % channel_statistics=GetImageChannelStatistics(image,excepton);
1410 % red_mean=channel_statistics[RedChannel].mean;
1412 % Use MagickRelinquishMemory() to free the statistics buffer.
1414 % The format of the GetImageChannelStatistics method is:
1416 % ChannelStatistics *GetImageChannelStatistics(const Image *image,
1417 % ExceptionInfo *exception)
1419 % A description of each parameter follows:
1421 % o image: the image.
1423 % o exception: return any errors or warnings in this structure.
1426 MagickExport ChannelStatistics *GetImageChannelStatistics(const Image *image,
1427 ExceptionInfo *exception)
1430 *channel_statistics;
1454 assert(image != (Image *) NULL);
1455 assert(image->signature == MagickSignature);
1456 if (image->debug != MagickFalse)
1457 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1458 length=AllChannels+1UL;
1459 channel_statistics=(ChannelStatistics *) AcquireQuantumMemory(length,
1460 sizeof(*channel_statistics));
1461 if (channel_statistics == (ChannelStatistics *) NULL)
1462 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1463 (void) ResetMagickMemory(channel_statistics,0,length*
1464 sizeof(*channel_statistics));
1465 for (i=0; i <= AllChannels; i++)
1467 channel_statistics[i].depth=1;
1468 channel_statistics[i].maxima=(-1.0E-37);
1469 channel_statistics[i].minima=1.0E+37;
1471 for (y=0; y < (ssize_t) image->rows; y++)
1473 register const IndexPacket
1476 register const PixelPacket
1482 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1483 if (p == (const PixelPacket *) NULL)
1485 indexes=GetVirtualIndexQueue(image);
1486 for (x=0; x < (ssize_t) image->columns; )
1488 if (channel_statistics[RedChannel].depth != MAGICKCORE_QUANTUM_DEPTH)
1490 depth=channel_statistics[RedChannel].depth;
1491 range=GetQuantumRange(depth);
1492 status=p->red != ScaleAnyToQuantum(ScaleQuantumToAny(p->red,range),
1493 range) ? MagickTrue : MagickFalse;
1494 if (status != MagickFalse)
1496 channel_statistics[RedChannel].depth++;
1500 if (channel_statistics[GreenChannel].depth != MAGICKCORE_QUANTUM_DEPTH)
1502 depth=channel_statistics[GreenChannel].depth;
1503 range=GetQuantumRange(depth);
1504 status=p->green != ScaleAnyToQuantum(ScaleQuantumToAny(p->green,
1505 range),range) ? MagickTrue : MagickFalse;
1506 if (status != MagickFalse)
1508 channel_statistics[GreenChannel].depth++;
1512 if (channel_statistics[BlueChannel].depth != MAGICKCORE_QUANTUM_DEPTH)
1514 depth=channel_statistics[BlueChannel].depth;
1515 range=GetQuantumRange(depth);
1516 status=p->blue != ScaleAnyToQuantum(ScaleQuantumToAny(p->blue,
1517 range),range) ? MagickTrue : MagickFalse;
1518 if (status != MagickFalse)
1520 channel_statistics[BlueChannel].depth++;
1524 if (image->matte != MagickFalse)
1526 if (channel_statistics[OpacityChannel].depth != MAGICKCORE_QUANTUM_DEPTH)
1528 depth=channel_statistics[OpacityChannel].depth;
1529 range=GetQuantumRange(depth);
1530 status=p->opacity != ScaleAnyToQuantum(ScaleQuantumToAny(
1531 p->opacity,range),range) ? MagickTrue : MagickFalse;
1532 if (status != MagickFalse)
1534 channel_statistics[OpacityChannel].depth++;
1539 if (image->colorspace == CMYKColorspace)
1541 if (channel_statistics[BlackChannel].depth != MAGICKCORE_QUANTUM_DEPTH)
1543 depth=channel_statistics[BlackChannel].depth;
1544 range=GetQuantumRange(depth);
1545 status=indexes[x] != ScaleAnyToQuantum(ScaleQuantumToAny(
1546 indexes[x],range),range) ? MagickTrue : MagickFalse;
1547 if (status != MagickFalse)
1549 channel_statistics[BlackChannel].depth++;
1554 if ((double) p->red < channel_statistics[RedChannel].minima)
1555 channel_statistics[RedChannel].minima=(double) GetRedPixelComponent(p);
1556 if ((double) p->red > channel_statistics[RedChannel].maxima)
1557 channel_statistics[RedChannel].maxima=(double) GetRedPixelComponent(p);
1558 channel_statistics[RedChannel].sum+=GetRedPixelComponent(p);
1559 channel_statistics[RedChannel].sum_squared+=(double) p->red*
1560 GetRedPixelComponent(p);
1561 channel_statistics[RedChannel].sum_cubed+=(double) p->red*p->red*
1562 GetRedPixelComponent(p);
1563 channel_statistics[RedChannel].sum_fourth_power+=(double) p->red*p->red*
1564 p->red*GetRedPixelComponent(p);
1565 if ((double) p->green < channel_statistics[GreenChannel].minima)
1566 channel_statistics[GreenChannel].minima=(double)
1567 GetGreenPixelComponent(p);
1568 if ((double) p->green > channel_statistics[GreenChannel].maxima)
1569 channel_statistics[GreenChannel].maxima=(double)
1570 GetGreenPixelComponent(p);
1571 channel_statistics[GreenChannel].sum+=GetGreenPixelComponent(p);
1572 channel_statistics[GreenChannel].sum_squared+=(double) p->green*
1573 GetGreenPixelComponent(p);
1574 channel_statistics[GreenChannel].sum_cubed+=(double) p->green*p->green*
1575 GetGreenPixelComponent(p);
1576 channel_statistics[GreenChannel].sum_fourth_power+=(double) p->green*
1577 p->green*p->green*GetGreenPixelComponent(p);
1578 if ((double) p->blue < channel_statistics[BlueChannel].minima)
1579 channel_statistics[BlueChannel].minima=(double)
1580 GetBluePixelComponent(p);
1581 if ((double) p->blue > channel_statistics[BlueChannel].maxima)
1582 channel_statistics[BlueChannel].maxima=(double)
1583 GetBluePixelComponent(p);
1584 channel_statistics[BlueChannel].sum+=GetBluePixelComponent(p);
1585 channel_statistics[BlueChannel].sum_squared+=(double) p->blue*
1586 GetBluePixelComponent(p);
1587 channel_statistics[BlueChannel].sum_cubed+=(double) p->blue*p->blue*
1588 GetBluePixelComponent(p);
1589 channel_statistics[BlueChannel].sum_fourth_power+=(double) p->blue*
1590 p->blue*p->blue*GetBluePixelComponent(p);
1591 if (image->matte != MagickFalse)
1593 if ((double) p->opacity < channel_statistics[OpacityChannel].minima)
1594 channel_statistics[OpacityChannel].minima=(double)
1595 GetOpacityPixelComponent(p);
1596 if ((double) p->opacity > channel_statistics[OpacityChannel].maxima)
1597 channel_statistics[OpacityChannel].maxima=(double)
1598 GetOpacityPixelComponent(p);
1599 channel_statistics[OpacityChannel].sum+=GetOpacityPixelComponent(p);
1600 channel_statistics[OpacityChannel].sum_squared+=(double)
1601 p->opacity*GetOpacityPixelComponent(p);
1602 channel_statistics[OpacityChannel].sum_cubed+=(double) p->opacity*
1603 p->opacity*GetOpacityPixelComponent(p);
1604 channel_statistics[OpacityChannel].sum_fourth_power+=(double)
1605 p->opacity*p->opacity*p->opacity*GetOpacityPixelComponent(p);
1607 if (image->colorspace == CMYKColorspace)
1609 if ((double) indexes[x] < channel_statistics[BlackChannel].minima)
1610 channel_statistics[BlackChannel].minima=(double) indexes[x];
1611 if ((double) indexes[x] > channel_statistics[BlackChannel].maxima)
1612 channel_statistics[BlackChannel].maxima=(double) indexes[x];
1613 channel_statistics[BlackChannel].sum+=indexes[x];
1614 channel_statistics[BlackChannel].sum_squared+=(double)
1615 indexes[x]*indexes[x];
1616 channel_statistics[BlackChannel].sum_cubed+=(double) indexes[x]*
1617 indexes[x]*indexes[x];
1618 channel_statistics[BlackChannel].sum_fourth_power+=(double)
1619 indexes[x]*indexes[x]*indexes[x]*indexes[x];
1625 area=(double) image->columns*image->rows;
1626 for (i=0; i < AllChannels; i++)
1628 channel_statistics[i].sum/=area;
1629 channel_statistics[i].sum_squared/=area;
1630 channel_statistics[i].sum_cubed/=area;
1631 channel_statistics[i].sum_fourth_power/=area;
1632 channel_statistics[i].mean=channel_statistics[i].sum;
1633 channel_statistics[i].variance=channel_statistics[i].sum_squared;
1634 channel_statistics[i].standard_deviation=sqrt(
1635 channel_statistics[i].variance-(channel_statistics[i].mean*
1636 channel_statistics[i].mean));
1638 for (i=0; i < AllChannels; i++)
1640 channel_statistics[AllChannels].depth=(size_t) MagickMax((double)
1641 channel_statistics[AllChannels].depth,(double)
1642 channel_statistics[i].depth);
1643 channel_statistics[AllChannels].minima=MagickMin(
1644 channel_statistics[AllChannels].minima,channel_statistics[i].minima);
1645 channel_statistics[AllChannels].maxima=MagickMax(
1646 channel_statistics[AllChannels].maxima,channel_statistics[i].maxima);
1647 channel_statistics[AllChannels].sum+=channel_statistics[i].sum;
1648 channel_statistics[AllChannels].sum_squared+=
1649 channel_statistics[i].sum_squared;
1650 channel_statistics[AllChannels].sum_cubed+=channel_statistics[i].sum_cubed;
1651 channel_statistics[AllChannels].sum_fourth_power+=
1652 channel_statistics[i].sum_fourth_power;
1653 channel_statistics[AllChannels].mean+=channel_statistics[i].mean;
1654 channel_statistics[AllChannels].variance+=channel_statistics[i].variance-
1655 channel_statistics[i].mean*channel_statistics[i].mean;
1656 channel_statistics[AllChannels].standard_deviation+=
1657 channel_statistics[i].variance-channel_statistics[i].mean*
1658 channel_statistics[i].mean;
1661 if (image->matte != MagickFalse)
1663 if (image->colorspace == CMYKColorspace)
1665 channel_statistics[AllChannels].sum/=channels;
1666 channel_statistics[AllChannels].sum_squared/=channels;
1667 channel_statistics[AllChannels].sum_cubed/=channels;
1668 channel_statistics[AllChannels].sum_fourth_power/=channels;
1669 channel_statistics[AllChannels].mean/=channels;
1670 channel_statistics[AllChannels].variance/=channels;
1671 channel_statistics[AllChannels].standard_deviation=
1672 sqrt(channel_statistics[AllChannels].standard_deviation/channels);
1673 channel_statistics[AllChannels].kurtosis/=channels;
1674 channel_statistics[AllChannels].skewness/=channels;
1675 for (i=0; i <= AllChannels; i++)
1677 if (channel_statistics[i].standard_deviation == 0.0)
1679 channel_statistics[i].skewness=(channel_statistics[i].sum_cubed-
1680 3.0*channel_statistics[i].mean*channel_statistics[i].sum_squared+
1681 2.0*channel_statistics[i].mean*channel_statistics[i].mean*
1682 channel_statistics[i].mean)/(channel_statistics[i].standard_deviation*
1683 channel_statistics[i].standard_deviation*
1684 channel_statistics[i].standard_deviation);
1685 channel_statistics[i].kurtosis=(channel_statistics[i].sum_fourth_power-
1686 4.0*channel_statistics[i].mean*channel_statistics[i].sum_cubed+
1687 6.0*channel_statistics[i].mean*channel_statistics[i].mean*
1688 channel_statistics[i].sum_squared-3.0*channel_statistics[i].mean*
1689 channel_statistics[i].mean*1.0*channel_statistics[i].mean*
1690 channel_statistics[i].mean)/(channel_statistics[i].standard_deviation*
1691 channel_statistics[i].standard_deviation*
1692 channel_statistics[i].standard_deviation*
1693 channel_statistics[i].standard_deviation)-3.0;
1695 return(channel_statistics);