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 **) RelinquishMagickMemory(pixels);
148 static MagickPixelPacket **AcquirePixelThreadSet(const Image *image)
160 number_threads=GetOpenMPMaximumThreads();
161 pixels=(MagickPixelPacket **) AcquireQuantumMemory(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++)
472 id = GetOpenMPThreadId();
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 evaluate_pixel=evaluate_pixels[id];
502 for (x=0; x < (ssize_t) evaluate_image->columns; x++)
503 evaluate_pixel[x]=zero;
505 for (i=0; i < (ssize_t) number_images; i++)
507 register const IndexPacket
510 register const PixelPacket
513 image_view=AcquireCacheView(next);
514 p=GetCacheViewVirtualPixels(image_view,0,y,next->columns,1,exception);
515 if (p == (const PixelPacket *) NULL)
517 image_view=DestroyCacheView(image_view);
520 indexes=GetCacheViewVirtualIndexQueue(image_view);
521 for (x=0; x < (ssize_t) next->columns; x++)
523 evaluate_pixel[x].red=ApplyEvaluateOperator(random_info[id],p->red,
524 i == 0 ? AddEvaluateOperator : op,evaluate_pixel[x].red);
525 evaluate_pixel[x].green=ApplyEvaluateOperator(random_info[id],p->green,
526 i == 0 ? AddEvaluateOperator : op,evaluate_pixel[x].green);
527 evaluate_pixel[x].blue=ApplyEvaluateOperator(random_info[id],p->blue,
528 i == 0 ? AddEvaluateOperator : op,evaluate_pixel[x].blue);
529 evaluate_pixel[x].opacity=ApplyEvaluateOperator(random_info[id],
530 p->opacity,i == 0 ? AddEvaluateOperator : op,
531 evaluate_pixel[x].opacity);
532 if (evaluate_image->colorspace == CMYKColorspace)
533 evaluate_pixel[x].index=ApplyEvaluateOperator(random_info[id],
534 indexes[x],i == 0 ? AddEvaluateOperator : op,
535 evaluate_pixel[x].index);
538 image_view=DestroyCacheView(image_view);
539 next=GetNextImageInList(next);
541 if (op == MeanEvaluateOperator)
542 for (x=0; x < (ssize_t) evaluate_image->columns; x++)
544 evaluate_pixel[x].red/=number_images;
545 evaluate_pixel[x].green/=number_images;
546 evaluate_pixel[x].blue/=number_images;
547 evaluate_pixel[x].opacity/=number_images;
548 evaluate_pixel[x].index/=number_images;
550 for (x=0; x < (ssize_t) evaluate_image->columns; x++)
552 q->red=ClampToQuantum(evaluate_pixel[x].red);
553 q->green=ClampToQuantum(evaluate_pixel[x].green);
554 q->blue=ClampToQuantum(evaluate_pixel[x].blue);
555 if (evaluate_image->matte == MagickFalse)
556 q->opacity=ClampToQuantum(evaluate_pixel[x].opacity);
558 q->opacity=ClampToQuantum(QuantumRange-evaluate_pixel[x].opacity);
559 if (evaluate_image->colorspace == CMYKColorspace)
560 evaluate_indexes[x]=ClampToQuantum(evaluate_pixel[x].index);
563 if (SyncCacheViewAuthenticPixels(evaluate_view,exception) == MagickFalse)
565 if (images->progress_monitor != (MagickProgressMonitor) NULL)
570 #if defined(MAGICKCORE_OPENMP_SUPPORT)
571 #pragma omp critical (MagickCore_EvaluateImages)
573 proceed=SetImageProgress(images,EvaluateImageTag,progress++,
574 evaluate_image->rows);
575 if (proceed == MagickFalse)
579 evaluate_view=DestroyCacheView(evaluate_view);
580 evaluate_pixels=DestroyPixelThreadSet(evaluate_pixels);
581 random_info=DestroyRandomInfoThreadSet(random_info);
582 if (status == MagickFalse)
583 evaluate_image=DestroyImage(evaluate_image);
584 return(evaluate_image);
587 MagickExport MagickBooleanType EvaluateImageChannel(Image *image,
588 const ChannelType channel,const MagickEvaluateOperator op,const double value,
589 ExceptionInfo *exception)
601 **restrict random_info;
606 assert(image != (Image *) NULL);
607 assert(image->signature == MagickSignature);
608 if (image->debug != MagickFalse)
609 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
610 assert(exception != (ExceptionInfo *) NULL);
611 assert(exception->signature == MagickSignature);
612 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
614 InheritException(exception,&image->exception);
619 random_info=AcquireRandomInfoThreadSet();
620 image_view=AcquireCacheView(image);
621 #if defined(MAGICKCORE_OPENMP_SUPPORT)
622 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
624 for (y=0; y < (ssize_t) image->rows; y++)
627 id = GetOpenMPThreadId();
638 if (status == MagickFalse)
640 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
641 if (q == (PixelPacket *) NULL)
646 indexes=GetCacheViewAuthenticIndexQueue(image_view);
647 for (x=0; x < (ssize_t) image->columns; x++)
649 if ((channel & RedChannel) != 0)
650 q->red=ClampToQuantum(ApplyEvaluateOperator(random_info[id],q->red,op,
652 if ((channel & GreenChannel) != 0)
653 q->green=ClampToQuantum(ApplyEvaluateOperator(random_info[id],q->green,
655 if ((channel & BlueChannel) != 0)
656 q->blue=ClampToQuantum(ApplyEvaluateOperator(random_info[id],q->blue,op,
658 if ((channel & OpacityChannel) != 0)
660 if (image->matte == MagickFalse)
661 q->opacity=ClampToQuantum(ApplyEvaluateOperator(random_info[id],
662 q->opacity,op,value));
664 q->opacity=ClampToQuantum(QuantumRange-ApplyEvaluateOperator(
665 random_info[id],(Quantum) GetAlphaPixelComponent(q),op,value));
667 if (((channel & IndexChannel) != 0) && (indexes != (IndexPacket *) NULL))
668 indexes[x]=(IndexPacket) ClampToQuantum(ApplyEvaluateOperator(
669 random_info[id],indexes[x],op,value));
672 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
674 if (image->progress_monitor != (MagickProgressMonitor) NULL)
679 #if defined(MAGICKCORE_OPENMP_SUPPORT)
680 #pragma omp critical (MagickCore_EvaluateImageChannel)
682 proceed=SetImageProgress(image,EvaluateImageTag,progress++,image->rows);
683 if (proceed == MagickFalse)
687 image_view=DestroyCacheView(image_view);
688 random_info=DestroyRandomInfoThreadSet(random_info);
693 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
697 % F u n c t i o n I m a g e %
701 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
703 % FunctionImage() applies a value to the image with an arithmetic, relational,
704 % or logical operator to an image. Use these operations to lighten or darken
705 % an image, to increase or decrease contrast in an image, or to produce the
706 % "negative" of an image.
708 % The format of the FunctionImageChannel method is:
710 % MagickBooleanType FunctionImage(Image *image,
711 % const MagickFunction function,const ssize_t number_parameters,
712 % const double *parameters,ExceptionInfo *exception)
713 % MagickBooleanType FunctionImageChannel(Image *image,
714 % const ChannelType channel,const MagickFunction function,
715 % const ssize_t number_parameters,const double *argument,
716 % ExceptionInfo *exception)
718 % A description of each parameter follows:
720 % o image: the image.
722 % o channel: the channel.
724 % o function: A channel function.
726 % o parameters: one or more parameters.
728 % o exception: return any errors or warnings in this structure.
732 static Quantum ApplyFunction(Quantum pixel,const MagickFunction function,
733 const size_t number_parameters,const double *parameters,
734 ExceptionInfo *exception)
746 case PolynomialFunction:
750 * Parameters: polynomial constants, highest to lowest order
751 * For example: c0*x^3 + c1*x^2 + c2*x + c3
754 for (i=0; i < (ssize_t) number_parameters; i++)
755 result = result*QuantumScale*pixel + parameters[i];
756 result *= QuantumRange;
759 case SinusoidFunction:
762 * Parameters: Freq, Phase, Ampl, bias
764 double freq,phase,ampl,bias;
765 freq = ( number_parameters >= 1 ) ? parameters[0] : 1.0;
766 phase = ( number_parameters >= 2 ) ? parameters[1] : 0.0;
767 ampl = ( number_parameters >= 3 ) ? parameters[2] : 0.5;
768 bias = ( number_parameters >= 4 ) ? parameters[3] : 0.5;
769 result=(MagickRealType) (QuantumRange*(ampl*sin((double) (2.0*MagickPI*
770 (freq*QuantumScale*pixel + phase/360.0) )) + bias ) );
775 /* Arcsin Function (peged at range limits for invalid results)
776 * Parameters: Width, Center, Range, Bias
778 double width,range,center,bias;
779 width = ( number_parameters >= 1 ) ? parameters[0] : 1.0;
780 center = ( number_parameters >= 2 ) ? parameters[1] : 0.5;
781 range = ( number_parameters >= 3 ) ? parameters[2] : 1.0;
782 bias = ( number_parameters >= 4 ) ? parameters[3] : 0.5;
783 result = 2.0/width*(QuantumScale*pixel - center);
784 if ( result <= -1.0 )
785 result = bias - range/2.0;
786 else if ( result >= 1.0 )
787 result = bias + range/2.0;
789 result=range/MagickPI*asin((double)result) + bias;
790 result *= QuantumRange;
796 * Parameters: Slope, Center, Range, Bias
798 double slope,range,center,bias;
799 slope = ( number_parameters >= 1 ) ? parameters[0] : 1.0;
800 center = ( number_parameters >= 2 ) ? parameters[1] : 0.5;
801 range = ( number_parameters >= 3 ) ? parameters[2] : 1.0;
802 bias = ( number_parameters >= 4 ) ? parameters[3] : 0.5;
803 result = MagickPI*slope*(QuantumScale*pixel - center);
804 result=(MagickRealType) (QuantumRange*(range/MagickPI*atan((double)
808 case UndefinedFunction:
811 return(ClampToQuantum(result));
814 MagickExport MagickBooleanType FunctionImage(Image *image,
815 const MagickFunction function,const size_t number_parameters,
816 const double *parameters,ExceptionInfo *exception)
821 status=FunctionImageChannel(image,AllChannels,function,number_parameters,
822 parameters,exception);
826 MagickExport MagickBooleanType FunctionImageChannel(Image *image,
827 const ChannelType channel,const MagickFunction function,
828 const size_t number_parameters,const double *parameters,
829 ExceptionInfo *exception)
831 #define FunctionImageTag "Function/Image "
845 assert(image != (Image *) NULL);
846 assert(image->signature == MagickSignature);
847 if (image->debug != MagickFalse)
848 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
849 assert(exception != (ExceptionInfo *) NULL);
850 assert(exception->signature == MagickSignature);
851 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
853 InheritException(exception,&image->exception);
858 image_view=AcquireCacheView(image);
859 #if defined(MAGICKCORE_OPENMP_SUPPORT)
860 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
862 for (y=0; y < (ssize_t) image->rows; y++)
873 if (status == MagickFalse)
875 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
876 if (q == (PixelPacket *) NULL)
881 indexes=GetCacheViewAuthenticIndexQueue(image_view);
882 for (x=0; x < (ssize_t) image->columns; x++)
884 if ((channel & RedChannel) != 0)
885 q->red=ApplyFunction(q->red,function,number_parameters,parameters,
887 if ((channel & GreenChannel) != 0)
888 q->green=ApplyFunction(q->green,function,number_parameters,parameters,
890 if ((channel & BlueChannel) != 0)
891 q->blue=ApplyFunction(q->blue,function,number_parameters,parameters,
893 if ((channel & OpacityChannel) != 0)
895 if (image->matte == MagickFalse)
896 q->opacity=ApplyFunction(q->opacity,function,number_parameters,
897 parameters,exception);
899 q->opacity=(Quantum) QuantumRange-ApplyFunction((Quantum)
900 GetAlphaPixelComponent(q),function,number_parameters,parameters,
903 if (((channel & IndexChannel) != 0) && (indexes != (IndexPacket *) NULL))
904 indexes[x]=(IndexPacket) ApplyFunction(indexes[x],function,
905 number_parameters,parameters,exception);
908 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
910 if (image->progress_monitor != (MagickProgressMonitor) NULL)
915 #if defined(MAGICKCORE_OPENMP_SUPPORT)
916 #pragma omp critical (MagickCore_FunctionImageChannel)
918 proceed=SetImageProgress(image,FunctionImageTag,progress++,image->rows);
919 if (proceed == MagickFalse)
923 image_view=DestroyCacheView(image_view);
928 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
932 + G e t I m a g e C h a n n e l E x t r e m a %
936 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
938 % GetImageChannelExtrema() returns the extrema of one or more image channels.
940 % The format of the GetImageChannelExtrema method is:
942 % MagickBooleanType GetImageChannelExtrema(const Image *image,
943 % const ChannelType channel,size_t *minima,size_t *maxima,
944 % ExceptionInfo *exception)
946 % A description of each parameter follows:
948 % o image: the image.
950 % o channel: the channel.
952 % o minima: the minimum value in the channel.
954 % o maxima: the maximum value in the channel.
956 % o exception: return any errors or warnings in this structure.
960 MagickExport MagickBooleanType GetImageExtrema(const Image *image,
961 size_t *minima,size_t *maxima,ExceptionInfo *exception)
963 return(GetImageChannelExtrema(image,AllChannels,minima,maxima,exception));
966 MagickExport MagickBooleanType GetImageChannelExtrema(const Image *image,
967 const ChannelType channel,size_t *minima,size_t *maxima,
968 ExceptionInfo *exception)
977 assert(image != (Image *) NULL);
978 assert(image->signature == MagickSignature);
979 if (image->debug != MagickFalse)
980 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
981 status=GetImageChannelRange(image,channel,&min,&max,exception);
982 *minima=(size_t) ceil(min-0.5);
983 *maxima=(size_t) floor(max+0.5);
988 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
992 % G e t I m a g e C h a n n e l M e a n %
996 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
998 % GetImageChannelMean() returns the mean and standard deviation of one or more
1001 % The format of the GetImageChannelMean method is:
1003 % MagickBooleanType GetImageChannelMean(const Image *image,
1004 % const ChannelType channel,double *mean,double *standard_deviation,
1005 % ExceptionInfo *exception)
1007 % A description of each parameter follows:
1009 % o image: the image.
1011 % o channel: the channel.
1013 % o mean: the average value in the channel.
1015 % o standard_deviation: the standard deviation of the channel.
1017 % o exception: return any errors or warnings in this structure.
1021 MagickExport MagickBooleanType GetImageMean(const Image *image,double *mean,
1022 double *standard_deviation,ExceptionInfo *exception)
1027 status=GetImageChannelMean(image,AllChannels,mean,standard_deviation,
1032 MagickExport MagickBooleanType GetImageChannelMean(const Image *image,
1033 const ChannelType channel,double *mean,double *standard_deviation,
1034 ExceptionInfo *exception)
1037 *channel_statistics;
1042 assert(image != (Image *) NULL);
1043 assert(image->signature == MagickSignature);
1044 if (image->debug != MagickFalse)
1045 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1046 channel_statistics=GetImageChannelStatistics(image,exception);
1047 if (channel_statistics == (ChannelStatistics *) NULL)
1048 return(MagickFalse);
1050 channel_statistics[AllChannels].mean=0.0;
1051 channel_statistics[AllChannels].standard_deviation=0.0;
1052 if ((channel & RedChannel) != 0)
1054 channel_statistics[AllChannels].mean+=
1055 channel_statistics[RedChannel].mean;
1056 channel_statistics[AllChannels].standard_deviation+=
1057 channel_statistics[RedChannel].variance-
1058 channel_statistics[RedChannel].mean*
1059 channel_statistics[RedChannel].mean;
1062 if ((channel & GreenChannel) != 0)
1064 channel_statistics[AllChannels].mean+=
1065 channel_statistics[GreenChannel].mean;
1066 channel_statistics[AllChannels].standard_deviation+=
1067 channel_statistics[GreenChannel].variance-
1068 channel_statistics[GreenChannel].mean*
1069 channel_statistics[GreenChannel].mean;
1072 if ((channel & BlueChannel) != 0)
1074 channel_statistics[AllChannels].mean+=
1075 channel_statistics[BlueChannel].mean;
1076 channel_statistics[AllChannels].standard_deviation+=
1077 channel_statistics[BlueChannel].variance-
1078 channel_statistics[BlueChannel].mean*
1079 channel_statistics[BlueChannel].mean;
1082 if (((channel & OpacityChannel) != 0) &&
1083 (image->matte != MagickFalse))
1085 channel_statistics[AllChannels].mean+=
1086 channel_statistics[OpacityChannel].mean;
1087 channel_statistics[AllChannels].standard_deviation+=
1088 channel_statistics[OpacityChannel].variance-
1089 channel_statistics[OpacityChannel].mean*
1090 channel_statistics[OpacityChannel].mean;
1093 if (((channel & IndexChannel) != 0) &&
1094 (image->colorspace == CMYKColorspace))
1096 channel_statistics[AllChannels].mean+=
1097 channel_statistics[BlackChannel].mean;
1098 channel_statistics[AllChannels].standard_deviation+=
1099 channel_statistics[BlackChannel].variance-
1100 channel_statistics[BlackChannel].mean*
1101 channel_statistics[BlackChannel].mean;
1104 channel_statistics[AllChannels].mean/=channels;
1105 channel_statistics[AllChannels].standard_deviation=
1106 sqrt(channel_statistics[AllChannels].standard_deviation/channels);
1107 *mean=channel_statistics[AllChannels].mean;
1108 *standard_deviation=channel_statistics[AllChannels].standard_deviation;
1109 channel_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1110 channel_statistics);
1115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1119 % G e t I m a g e C h a n n e l K u r t o s i s %
1123 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1125 % GetImageChannelKurtosis() returns the kurtosis and skewness of one or more
1128 % The format of the GetImageChannelKurtosis method is:
1130 % MagickBooleanType GetImageChannelKurtosis(const Image *image,
1131 % const ChannelType channel,double *kurtosis,double *skewness,
1132 % ExceptionInfo *exception)
1134 % A description of each parameter follows:
1136 % o image: the image.
1138 % o channel: the channel.
1140 % o kurtosis: the kurtosis of the channel.
1142 % o skewness: the skewness of the channel.
1144 % o exception: return any errors or warnings in this structure.
1148 MagickExport MagickBooleanType GetImageKurtosis(const Image *image,
1149 double *kurtosis,double *skewness,ExceptionInfo *exception)
1154 status=GetImageChannelKurtosis(image,AllChannels,kurtosis,skewness,
1159 MagickExport MagickBooleanType GetImageChannelKurtosis(const Image *image,
1160 const ChannelType channel,double *kurtosis,double *skewness,
1161 ExceptionInfo *exception)
1174 assert(image != (Image *) NULL);
1175 assert(image->signature == MagickSignature);
1176 if (image->debug != MagickFalse)
1177 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1182 standard_deviation=0.0;
1185 sum_fourth_power=0.0;
1186 for (y=0; y < (ssize_t) image->rows; y++)
1188 register const IndexPacket
1191 register const PixelPacket
1197 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1198 if (p == (const PixelPacket *) NULL)
1200 indexes=GetVirtualIndexQueue(image);
1201 for (x=0; x < (ssize_t) image->columns; x++)
1203 if ((channel & RedChannel) != 0)
1205 mean+=GetRedPixelComponent(p);
1206 sum_squares+=(double) p->red*GetRedPixelComponent(p);
1207 sum_cubes+=(double) p->red*p->red*GetRedPixelComponent(p);
1208 sum_fourth_power+=(double) p->red*p->red*p->red*
1209 GetRedPixelComponent(p);
1212 if ((channel & GreenChannel) != 0)
1214 mean+=GetGreenPixelComponent(p);
1215 sum_squares+=(double) p->green*GetGreenPixelComponent(p);
1216 sum_cubes+=(double) p->green*p->green*GetGreenPixelComponent(p);
1217 sum_fourth_power+=(double) p->green*p->green*p->green*
1218 GetGreenPixelComponent(p);
1221 if ((channel & BlueChannel) != 0)
1223 mean+=GetBluePixelComponent(p);
1224 sum_squares+=(double) p->blue*GetBluePixelComponent(p);
1225 sum_cubes+=(double) p->blue*p->blue*GetBluePixelComponent(p);
1226 sum_fourth_power+=(double) p->blue*p->blue*p->blue*
1227 GetBluePixelComponent(p);
1230 if ((channel & OpacityChannel) != 0)
1232 mean+=GetOpacityPixelComponent(p);
1233 sum_squares+=(double) p->opacity*GetOpacityPixelComponent(p);
1234 sum_cubes+=(double) p->opacity*p->opacity*GetOpacityPixelComponent(p);
1235 sum_fourth_power+=(double) p->opacity*p->opacity*p->opacity*
1236 GetOpacityPixelComponent(p);
1239 if (((channel & IndexChannel) != 0) &&
1240 (image->colorspace == CMYKColorspace))
1243 sum_squares+=(double) indexes[x]*indexes[x];
1244 sum_cubes+=(double) indexes[x]*indexes[x]*indexes[x];
1245 sum_fourth_power+=(double) indexes[x]*indexes[x]*indexes[x]*
1252 if (y < (ssize_t) image->rows)
1253 return(MagickFalse);
1259 sum_fourth_power/=area;
1261 standard_deviation=sqrt(sum_squares-(mean*mean));
1262 if (standard_deviation != 0.0)
1264 *kurtosis=sum_fourth_power-4.0*mean*sum_cubes+6.0*mean*mean*sum_squares-
1265 3.0*mean*mean*mean*mean;
1266 *kurtosis/=standard_deviation*standard_deviation*standard_deviation*
1269 *skewness=sum_cubes-3.0*mean*sum_squares+2.0*mean*mean*mean;
1270 *skewness/=standard_deviation*standard_deviation*standard_deviation;
1272 return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse);
1276 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1280 % G e t I m a g e C h a n n e l R a n g e %
1284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1286 % GetImageChannelRange() returns the range of one or more image channels.
1288 % The format of the GetImageChannelRange method is:
1290 % MagickBooleanType GetImageChannelRange(const Image *image,
1291 % const ChannelType channel,double *minima,double *maxima,
1292 % ExceptionInfo *exception)
1294 % A description of each parameter follows:
1296 % o image: the image.
1298 % o channel: the channel.
1300 % o minima: the minimum value in the channel.
1302 % o maxima: the maximum value in the channel.
1304 % o exception: return any errors or warnings in this structure.
1308 MagickExport MagickBooleanType GetImageRange(const Image *image,
1309 double *minima,double *maxima,ExceptionInfo *exception)
1311 return(GetImageChannelRange(image,AllChannels,minima,maxima,exception));
1314 MagickExport MagickBooleanType GetImageChannelRange(const Image *image,
1315 const ChannelType channel,double *minima,double *maxima,
1316 ExceptionInfo *exception)
1324 assert(image != (Image *) NULL);
1325 assert(image->signature == MagickSignature);
1326 if (image->debug != MagickFalse)
1327 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1330 GetMagickPixelPacket(image,&pixel);
1331 for (y=0; y < (ssize_t) image->rows; y++)
1333 register const IndexPacket
1336 register const PixelPacket
1342 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1343 if (p == (const PixelPacket *) NULL)
1345 indexes=GetVirtualIndexQueue(image);
1346 for (x=0; x < (ssize_t) image->columns; x++)
1348 SetMagickPixelPacket(image,p,indexes+x,&pixel);
1349 if ((channel & RedChannel) != 0)
1351 if (pixel.red < *minima)
1352 *minima=(double) pixel.red;
1353 if (pixel.red > *maxima)
1354 *maxima=(double) pixel.red;
1356 if ((channel & GreenChannel) != 0)
1358 if (pixel.green < *minima)
1359 *minima=(double) pixel.green;
1360 if (pixel.green > *maxima)
1361 *maxima=(double) pixel.green;
1363 if ((channel & BlueChannel) != 0)
1365 if (pixel.blue < *minima)
1366 *minima=(double) pixel.blue;
1367 if (pixel.blue > *maxima)
1368 *maxima=(double) pixel.blue;
1370 if ((channel & OpacityChannel) != 0)
1372 if (pixel.opacity < *minima)
1373 *minima=(double) pixel.opacity;
1374 if (pixel.opacity > *maxima)
1375 *maxima=(double) pixel.opacity;
1377 if (((channel & IndexChannel) != 0) &&
1378 (image->colorspace == CMYKColorspace))
1380 if ((double) indexes[x] < *minima)
1381 *minima=(double) indexes[x];
1382 if ((double) indexes[x] > *maxima)
1383 *maxima=(double) indexes[x];
1388 return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse);
1392 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1396 % 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 %
1400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1402 % GetImageChannelStatistics() returns statistics for each channel in the
1403 % image. The statistics include the channel depth, its minima, maxima, mean,
1404 % standard deviation, kurtosis and skewness. You can access the red channel
1405 % mean, for example, like this:
1407 % channel_statistics=GetImageChannelStatistics(image,excepton);
1408 % red_mean=channel_statistics[RedChannel].mean;
1410 % Use MagickRelinquishMemory() to free the statistics buffer.
1412 % The format of the GetImageChannelStatistics method is:
1414 % ChannelStatistics *GetImageChannelStatistics(const Image *image,
1415 % ExceptionInfo *exception)
1417 % A description of each parameter follows:
1419 % o image: the image.
1421 % o exception: return any errors or warnings in this structure.
1424 MagickExport ChannelStatistics *GetImageChannelStatistics(const Image *image,
1425 ExceptionInfo *exception)
1428 *channel_statistics;
1452 assert(image != (Image *) NULL);
1453 assert(image->signature == MagickSignature);
1454 if (image->debug != MagickFalse)
1455 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1456 length=AllChannels+1UL;
1457 channel_statistics=(ChannelStatistics *) AcquireQuantumMemory(length,
1458 sizeof(*channel_statistics));
1459 if (channel_statistics == (ChannelStatistics *) NULL)
1460 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1461 (void) ResetMagickMemory(channel_statistics,0,length*
1462 sizeof(*channel_statistics));
1463 for (i=0; i <= AllChannels; i++)
1465 channel_statistics[i].depth=1;
1466 channel_statistics[i].maxima=(-1.0E-37);
1467 channel_statistics[i].minima=1.0E+37;
1469 for (y=0; y < (ssize_t) image->rows; y++)
1471 register const IndexPacket
1474 register const PixelPacket
1480 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1481 if (p == (const PixelPacket *) NULL)
1483 indexes=GetVirtualIndexQueue(image);
1484 for (x=0; x < (ssize_t) image->columns; )
1486 if (channel_statistics[RedChannel].depth != MAGICKCORE_QUANTUM_DEPTH)
1488 depth=channel_statistics[RedChannel].depth;
1489 range=GetQuantumRange(depth);
1490 status=p->red != ScaleAnyToQuantum(ScaleQuantumToAny(p->red,range),
1491 range) ? MagickTrue : MagickFalse;
1492 if (status != MagickFalse)
1494 channel_statistics[RedChannel].depth++;
1498 if (channel_statistics[GreenChannel].depth != MAGICKCORE_QUANTUM_DEPTH)
1500 depth=channel_statistics[GreenChannel].depth;
1501 range=GetQuantumRange(depth);
1502 status=p->green != ScaleAnyToQuantum(ScaleQuantumToAny(p->green,
1503 range),range) ? MagickTrue : MagickFalse;
1504 if (status != MagickFalse)
1506 channel_statistics[GreenChannel].depth++;
1510 if (channel_statistics[BlueChannel].depth != MAGICKCORE_QUANTUM_DEPTH)
1512 depth=channel_statistics[BlueChannel].depth;
1513 range=GetQuantumRange(depth);
1514 status=p->blue != ScaleAnyToQuantum(ScaleQuantumToAny(p->blue,
1515 range),range) ? MagickTrue : MagickFalse;
1516 if (status != MagickFalse)
1518 channel_statistics[BlueChannel].depth++;
1522 if (image->matte != MagickFalse)
1524 if (channel_statistics[OpacityChannel].depth != MAGICKCORE_QUANTUM_DEPTH)
1526 depth=channel_statistics[OpacityChannel].depth;
1527 range=GetQuantumRange(depth);
1528 status=p->opacity != ScaleAnyToQuantum(ScaleQuantumToAny(
1529 p->opacity,range),range) ? MagickTrue : MagickFalse;
1530 if (status != MagickFalse)
1532 channel_statistics[OpacityChannel].depth++;
1537 if (image->colorspace == CMYKColorspace)
1539 if (channel_statistics[BlackChannel].depth != MAGICKCORE_QUANTUM_DEPTH)
1541 depth=channel_statistics[BlackChannel].depth;
1542 range=GetQuantumRange(depth);
1543 status=indexes[x] != ScaleAnyToQuantum(ScaleQuantumToAny(
1544 indexes[x],range),range) ? MagickTrue : MagickFalse;
1545 if (status != MagickFalse)
1547 channel_statistics[BlackChannel].depth++;
1552 if ((double) p->red < channel_statistics[RedChannel].minima)
1553 channel_statistics[RedChannel].minima=(double) GetRedPixelComponent(p);
1554 if ((double) p->red > channel_statistics[RedChannel].maxima)
1555 channel_statistics[RedChannel].maxima=(double) GetRedPixelComponent(p);
1556 channel_statistics[RedChannel].sum+=GetRedPixelComponent(p);
1557 channel_statistics[RedChannel].sum_squared+=(double) p->red*
1558 GetRedPixelComponent(p);
1559 channel_statistics[RedChannel].sum_cubed+=(double) p->red*p->red*
1560 GetRedPixelComponent(p);
1561 channel_statistics[RedChannel].sum_fourth_power+=(double) p->red*p->red*
1562 p->red*GetRedPixelComponent(p);
1563 if ((double) p->green < channel_statistics[GreenChannel].minima)
1564 channel_statistics[GreenChannel].minima=(double)
1565 GetGreenPixelComponent(p);
1566 if ((double) p->green > channel_statistics[GreenChannel].maxima)
1567 channel_statistics[GreenChannel].maxima=(double)
1568 GetGreenPixelComponent(p);
1569 channel_statistics[GreenChannel].sum+=GetGreenPixelComponent(p);
1570 channel_statistics[GreenChannel].sum_squared+=(double) p->green*
1571 GetGreenPixelComponent(p);
1572 channel_statistics[GreenChannel].sum_cubed+=(double) p->green*p->green*
1573 GetGreenPixelComponent(p);
1574 channel_statistics[GreenChannel].sum_fourth_power+=(double) p->green*
1575 p->green*p->green*GetGreenPixelComponent(p);
1576 if ((double) p->blue < channel_statistics[BlueChannel].minima)
1577 channel_statistics[BlueChannel].minima=(double)
1578 GetBluePixelComponent(p);
1579 if ((double) p->blue > channel_statistics[BlueChannel].maxima)
1580 channel_statistics[BlueChannel].maxima=(double)
1581 GetBluePixelComponent(p);
1582 channel_statistics[BlueChannel].sum+=GetBluePixelComponent(p);
1583 channel_statistics[BlueChannel].sum_squared+=(double) p->blue*
1584 GetBluePixelComponent(p);
1585 channel_statistics[BlueChannel].sum_cubed+=(double) p->blue*p->blue*
1586 GetBluePixelComponent(p);
1587 channel_statistics[BlueChannel].sum_fourth_power+=(double) p->blue*
1588 p->blue*p->blue*GetBluePixelComponent(p);
1589 if (image->matte != MagickFalse)
1591 if ((double) p->opacity < channel_statistics[OpacityChannel].minima)
1592 channel_statistics[OpacityChannel].minima=(double)
1593 GetOpacityPixelComponent(p);
1594 if ((double) p->opacity > channel_statistics[OpacityChannel].maxima)
1595 channel_statistics[OpacityChannel].maxima=(double)
1596 GetOpacityPixelComponent(p);
1597 channel_statistics[OpacityChannel].sum+=GetOpacityPixelComponent(p);
1598 channel_statistics[OpacityChannel].sum_squared+=(double)
1599 p->opacity*GetOpacityPixelComponent(p);
1600 channel_statistics[OpacityChannel].sum_cubed+=(double) p->opacity*
1601 p->opacity*GetOpacityPixelComponent(p);
1602 channel_statistics[OpacityChannel].sum_fourth_power+=(double)
1603 p->opacity*p->opacity*p->opacity*GetOpacityPixelComponent(p);
1605 if (image->colorspace == CMYKColorspace)
1607 if ((double) indexes[x] < channel_statistics[BlackChannel].minima)
1608 channel_statistics[BlackChannel].minima=(double) indexes[x];
1609 if ((double) indexes[x] > channel_statistics[BlackChannel].maxima)
1610 channel_statistics[BlackChannel].maxima=(double) indexes[x];
1611 channel_statistics[BlackChannel].sum+=indexes[x];
1612 channel_statistics[BlackChannel].sum_squared+=(double)
1613 indexes[x]*indexes[x];
1614 channel_statistics[BlackChannel].sum_cubed+=(double) indexes[x]*
1615 indexes[x]*indexes[x];
1616 channel_statistics[BlackChannel].sum_fourth_power+=(double)
1617 indexes[x]*indexes[x]*indexes[x]*indexes[x];
1623 area=(double) image->columns*image->rows;
1624 for (i=0; i < AllChannels; i++)
1626 channel_statistics[i].sum/=area;
1627 channel_statistics[i].sum_squared/=area;
1628 channel_statistics[i].sum_cubed/=area;
1629 channel_statistics[i].sum_fourth_power/=area;
1630 channel_statistics[i].mean=channel_statistics[i].sum;
1631 channel_statistics[i].variance=channel_statistics[i].sum_squared;
1632 channel_statistics[i].standard_deviation=sqrt(
1633 channel_statistics[i].variance-(channel_statistics[i].mean*
1634 channel_statistics[i].mean));
1636 for (i=0; i < AllChannels; i++)
1638 channel_statistics[AllChannels].depth=(size_t) MagickMax((double)
1639 channel_statistics[AllChannels].depth,(double)
1640 channel_statistics[i].depth);
1641 channel_statistics[AllChannels].minima=MagickMin(
1642 channel_statistics[AllChannels].minima,channel_statistics[i].minima);
1643 channel_statistics[AllChannels].maxima=MagickMax(
1644 channel_statistics[AllChannels].maxima,channel_statistics[i].maxima);
1645 channel_statistics[AllChannels].sum+=channel_statistics[i].sum;
1646 channel_statistics[AllChannels].sum_squared+=
1647 channel_statistics[i].sum_squared;
1648 channel_statistics[AllChannels].sum_cubed+=channel_statistics[i].sum_cubed;
1649 channel_statistics[AllChannels].sum_fourth_power+=
1650 channel_statistics[i].sum_fourth_power;
1651 channel_statistics[AllChannels].mean+=channel_statistics[i].mean;
1652 channel_statistics[AllChannels].variance+=channel_statistics[i].variance-
1653 channel_statistics[i].mean*channel_statistics[i].mean;
1654 channel_statistics[AllChannels].standard_deviation+=
1655 channel_statistics[i].variance-channel_statistics[i].mean*
1656 channel_statistics[i].mean;
1659 if (image->matte != MagickFalse)
1661 if (image->colorspace == CMYKColorspace)
1663 channel_statistics[AllChannels].sum/=channels;
1664 channel_statistics[AllChannels].sum_squared/=channels;
1665 channel_statistics[AllChannels].sum_cubed/=channels;
1666 channel_statistics[AllChannels].sum_fourth_power/=channels;
1667 channel_statistics[AllChannels].mean/=channels;
1668 channel_statistics[AllChannels].variance/=channels;
1669 channel_statistics[AllChannels].standard_deviation=
1670 sqrt(channel_statistics[AllChannels].standard_deviation/channels);
1671 channel_statistics[AllChannels].kurtosis/=channels;
1672 channel_statistics[AllChannels].skewness/=channels;
1673 for (i=0; i <= AllChannels; i++)
1675 if (channel_statistics[i].standard_deviation == 0.0)
1677 channel_statistics[i].skewness=(channel_statistics[i].sum_cubed-
1678 3.0*channel_statistics[i].mean*channel_statistics[i].sum_squared+
1679 2.0*channel_statistics[i].mean*channel_statistics[i].mean*
1680 channel_statistics[i].mean)/(channel_statistics[i].standard_deviation*
1681 channel_statistics[i].standard_deviation*
1682 channel_statistics[i].standard_deviation);
1683 channel_statistics[i].kurtosis=(channel_statistics[i].sum_fourth_power-
1684 4.0*channel_statistics[i].mean*channel_statistics[i].sum_cubed+
1685 6.0*channel_statistics[i].mean*channel_statistics[i].mean*
1686 channel_statistics[i].sum_squared-3.0*channel_statistics[i].mean*
1687 channel_statistics[i].mean*1.0*channel_statistics[i].mean*
1688 channel_statistics[i].mean)/(channel_statistics[i].standard_deviation*
1689 channel_statistics[i].standard_deviation*
1690 channel_statistics[i].standard_deviation*
1691 channel_statistics[i].standard_deviation)-3.0;
1693 return(channel_statistics);