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-2014 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 "MagickCore/studio.h"
44 #include "MagickCore/property.h"
45 #include "MagickCore/animate.h"
46 #include "MagickCore/blob.h"
47 #include "MagickCore/blob-private.h"
48 #include "MagickCore/cache.h"
49 #include "MagickCore/cache-private.h"
50 #include "MagickCore/cache-view.h"
51 #include "MagickCore/client.h"
52 #include "MagickCore/color.h"
53 #include "MagickCore/color-private.h"
54 #include "MagickCore/colorspace.h"
55 #include "MagickCore/colorspace-private.h"
56 #include "MagickCore/composite.h"
57 #include "MagickCore/composite-private.h"
58 #include "MagickCore/compress.h"
59 #include "MagickCore/constitute.h"
60 #include "MagickCore/display.h"
61 #include "MagickCore/draw.h"
62 #include "MagickCore/enhance.h"
63 #include "MagickCore/exception.h"
64 #include "MagickCore/exception-private.h"
65 #include "MagickCore/gem.h"
66 #include "MagickCore/gem-private.h"
67 #include "MagickCore/geometry.h"
68 #include "MagickCore/list.h"
69 #include "MagickCore/image-private.h"
70 #include "MagickCore/magic.h"
71 #include "MagickCore/magick.h"
72 #include "MagickCore/memory_.h"
73 #include "MagickCore/module.h"
74 #include "MagickCore/monitor.h"
75 #include "MagickCore/monitor-private.h"
76 #include "MagickCore/option.h"
77 #include "MagickCore/paint.h"
78 #include "MagickCore/pixel-accessor.h"
79 #include "MagickCore/profile.h"
80 #include "MagickCore/quantize.h"
81 #include "MagickCore/quantum-private.h"
82 #include "MagickCore/random_.h"
83 #include "MagickCore/random-private.h"
84 #include "MagickCore/resource_.h"
85 #include "MagickCore/segment.h"
86 #include "MagickCore/semaphore.h"
87 #include "MagickCore/signature-private.h"
88 #include "MagickCore/statistic.h"
89 #include "MagickCore/string_.h"
90 #include "MagickCore/thread-private.h"
91 #include "MagickCore/timer.h"
92 #include "MagickCore/utility.h"
93 #include "MagickCore/version.h"
96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
100 % E v a l u a t e I m a g e %
104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 % EvaluateImage() applies a value to the image with an arithmetic, relational,
107 % or logical operator to an image. Use these operations to lighten or darken
108 % an image, to increase or decrease contrast in an image, or to produce the
109 % "negative" of an image.
111 % The format of the EvaluateImage method is:
113 % MagickBooleanType EvaluateImage(Image *image,
114 % const MagickEvaluateOperator op,const double value,
115 % ExceptionInfo *exception)
116 % MagickBooleanType EvaluateImages(Image *images,
117 % const MagickEvaluateOperator op,const double value,
118 % ExceptionInfo *exception)
120 % A description of each parameter follows:
122 % o image: the image.
124 % o op: A channel op.
126 % o value: A value value.
128 % o exception: return any errors or warnings in this structure.
132 typedef struct _PixelChannels
135 channel[CompositePixelChannel];
138 static PixelChannels **DestroyPixelThreadSet(PixelChannels **pixels)
143 assert(pixels != (PixelChannels **) NULL);
144 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
145 if (pixels[i] != (PixelChannels *) NULL)
146 pixels[i]=(PixelChannels *) RelinquishMagickMemory(pixels[i]);
147 pixels=(PixelChannels **) RelinquishMagickMemory(pixels);
151 static PixelChannels **AcquirePixelThreadSet(const Image *image,
152 const size_t number_images)
164 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
165 pixels=(PixelChannels **) AcquireQuantumMemory(number_threads,
167 if (pixels == (PixelChannels **) NULL)
168 return((PixelChannels **) NULL);
169 (void) ResetMagickMemory(pixels,0,number_threads*sizeof(*pixels));
170 for (i=0; i < (ssize_t) number_threads; i++)
175 length=image->columns;
176 if (length < number_images)
177 length=number_images;
178 pixels[i]=(PixelChannels *) AcquireQuantumMemory(length,sizeof(**pixels));
179 if (pixels[i] == (PixelChannels *) NULL)
180 return(DestroyPixelThreadSet(pixels));
181 for (j=0; j < (ssize_t) length; j++)
186 for (k=0; k < MaxPixelChannels; k++)
187 pixels[i][j].channel[k]=0.0;
193 static inline double EvaluateMax(const double x,const double y)
200 #if defined(__cplusplus) || defined(c_plusplus)
204 static int IntensityCompare(const void *x,const void *y)
216 color_1=(const PixelChannels *) x;
217 color_2=(const PixelChannels *) y;
219 for (i=0; i < MaxPixelChannels; i++)
220 distance+=color_1->channel[i]-(double) color_2->channel[i];
221 return(distance < 0 ? -1 : distance > 0 ? 1 : 0);
224 #if defined(__cplusplus) || defined(c_plusplus)
228 static inline double MagickMin(const double x,const double y)
235 static double ApplyEvaluateOperator(RandomInfo *random_info,const Quantum pixel,
236 const MagickEvaluateOperator op,const double value)
244 case UndefinedEvaluateOperator:
246 case AbsEvaluateOperator:
248 result=(double) fabs((double) (pixel+value));
251 case AddEvaluateOperator:
253 result=(double) (pixel+value);
256 case AddModulusEvaluateOperator:
259 This returns a 'floored modulus' of the addition which is a positive
260 result. It differs from % or fmod() that returns a 'truncated modulus'
261 result, where floor() is replaced by trunc() and could return a
262 negative result (which is clipped).
265 result-=(QuantumRange+1.0)*floor((double) result/(QuantumRange+1.0));
268 case AndEvaluateOperator:
270 result=(double) ((size_t) pixel & (size_t) (value+0.5));
273 case CosineEvaluateOperator:
275 result=(double) (QuantumRange*(0.5*cos((double) (2.0*MagickPI*
276 QuantumScale*pixel*value))+0.5));
279 case DivideEvaluateOperator:
281 result=pixel/(value == 0.0 ? 1.0 : value);
284 case ExponentialEvaluateOperator:
286 result=(double) (QuantumRange*exp((double) (value*QuantumScale*pixel)));
289 case GaussianNoiseEvaluateOperator:
291 result=(double) GenerateDifferentialNoise(random_info,pixel,
292 GaussianNoise,value);
295 case ImpulseNoiseEvaluateOperator:
297 result=(double) GenerateDifferentialNoise(random_info,pixel,ImpulseNoise,
301 case LaplacianNoiseEvaluateOperator:
303 result=(double) GenerateDifferentialNoise(random_info,pixel,
304 LaplacianNoise,value);
307 case LeftShiftEvaluateOperator:
309 result=(double) ((size_t) pixel << (size_t) (value+0.5));
312 case LogEvaluateOperator:
314 if ((QuantumScale*pixel) >= MagickEpsilon)
315 result=(double) (QuantumRange*log((double) (QuantumScale*value*pixel+
316 1.0))/log((double) (value+1.0)));
319 case MaxEvaluateOperator:
321 result=(double) EvaluateMax((double) pixel,value);
324 case MeanEvaluateOperator:
326 result=(double) (pixel+value);
329 case MedianEvaluateOperator:
331 result=(double) (pixel+value);
334 case MinEvaluateOperator:
336 result=(double) MagickMin((double) pixel,value);
339 case MultiplicativeNoiseEvaluateOperator:
341 result=(double) GenerateDifferentialNoise(random_info,pixel,
342 MultiplicativeGaussianNoise,value);
345 case MultiplyEvaluateOperator:
347 result=(double) (value*pixel);
350 case OrEvaluateOperator:
352 result=(double) ((size_t) pixel | (size_t) (value+0.5));
355 case PoissonNoiseEvaluateOperator:
357 result=(double) GenerateDifferentialNoise(random_info,pixel,PoissonNoise,
361 case PowEvaluateOperator:
363 result=(double) (QuantumRange*pow((double) (QuantumScale*pixel),(double)
367 case RightShiftEvaluateOperator:
369 result=(double) ((size_t) pixel >> (size_t) (value+0.5));
372 case SetEvaluateOperator:
377 case SineEvaluateOperator:
379 result=(double) (QuantumRange*(0.5*sin((double) (2.0*MagickPI*
380 QuantumScale*pixel*value))+0.5));
383 case SubtractEvaluateOperator:
385 result=(double) (pixel-value);
388 case SumEvaluateOperator:
390 result=(double) (pixel+value);
393 case ThresholdEvaluateOperator:
395 result=(double) (((double) pixel <= value) ? 0 : QuantumRange);
398 case ThresholdBlackEvaluateOperator:
400 result=(double) (((double) pixel <= value) ? 0 : pixel);
403 case ThresholdWhiteEvaluateOperator:
405 result=(double) (((double) pixel > value) ? QuantumRange : pixel);
408 case UniformNoiseEvaluateOperator:
410 result=(double) GenerateDifferentialNoise(random_info,pixel,UniformNoise,
414 case XorEvaluateOperator:
416 result=(double) ((size_t) pixel ^ (size_t) (value+0.5));
423 MagickExport Image *EvaluateImages(const Image *images,
424 const MagickEvaluateOperator op,ExceptionInfo *exception)
426 #define EvaluateImageTag "Evaluate/Image"
441 **restrict evaluate_pixels;
444 **restrict random_info;
452 #if defined(MAGICKCORE_OPENMP_SUPPORT)
457 assert(images != (Image *) NULL);
458 assert(images->signature == MagickSignature);
459 if (images->debug != MagickFalse)
460 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
461 assert(exception != (ExceptionInfo *) NULL);
462 assert(exception->signature == MagickSignature);
463 image=CloneImage(images,images->columns,images->rows,MagickTrue,
465 if (image == (Image *) NULL)
466 return((Image *) NULL);
467 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
469 image=DestroyImage(image);
470 return((Image *) NULL);
472 number_images=GetImageListLength(images);
473 evaluate_pixels=AcquirePixelThreadSet(images,number_images);
474 if (evaluate_pixels == (PixelChannels **) NULL)
476 image=DestroyImage(image);
477 (void) ThrowMagickException(exception,GetMagickModule(),
478 ResourceLimitError,"MemoryAllocationFailed","`%s'",images->filename);
479 return((Image *) NULL);
482 Evaluate image pixels.
486 random_info=AcquireRandomInfoThreadSet();
487 #if defined(MAGICKCORE_OPENMP_SUPPORT)
488 key=GetRandomSecretKey(random_info[0]);
490 evaluate_view=AcquireAuthenticCacheView(image,exception);
491 if (op == MedianEvaluateOperator)
493 #if defined(MAGICKCORE_OPENMP_SUPPORT)
494 #pragma omp parallel for schedule(static,4) shared(progress,status) \
495 magick_threads(image,images,image->rows,key == ~0UL)
497 for (y=0; y < (ssize_t) image->rows; y++)
506 id = GetOpenMPThreadId();
508 register PixelChannels
517 if (status == MagickFalse)
519 q=QueueCacheViewAuthenticPixels(evaluate_view,0,y,image->columns,1,
521 if (q == (Quantum *) NULL)
526 evaluate_pixel=evaluate_pixels[id];
527 for (x=0; x < (ssize_t) image->columns; x++)
533 for (j=0; j < (ssize_t) number_images; j++)
534 for (k=0; k < MaxPixelChannels; k++)
535 evaluate_pixel[j].channel[k]=0.0;
537 for (j=0; j < (ssize_t) number_images; j++)
539 register const Quantum
545 image_view=AcquireVirtualCacheView(next,exception);
546 p=GetCacheViewVirtualPixels(image_view,x,y,1,1,exception);
547 if (p == (const Quantum *) NULL)
549 image_view=DestroyCacheView(image_view);
552 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
554 PixelChannel channel=GetPixelChannelChannel(image,i);
555 PixelTrait evaluate_traits=GetPixelChannelTraits(image,channel);
556 PixelTrait traits=GetPixelChannelTraits(next,channel);
557 if ((traits == UndefinedPixelTrait) ||
558 (evaluate_traits == UndefinedPixelTrait))
560 if ((evaluate_traits & UpdatePixelTrait) == 0)
562 evaluate_pixel[j].channel[i]=ApplyEvaluateOperator(
563 random_info[id],GetPixelChannel(image,channel,p),op,
564 evaluate_pixel[j].channel[i]);
566 image_view=DestroyCacheView(image_view);
567 next=GetNextImageInList(next);
569 qsort((void *) evaluate_pixel,number_images,sizeof(*evaluate_pixel),
571 for (k=0; k < (ssize_t) GetPixelChannels(image); k++)
572 q[k]=ClampToQuantum(evaluate_pixel[j/2].channel[k]);
573 q+=GetPixelChannels(image);
575 if (SyncCacheViewAuthenticPixels(evaluate_view,exception) == MagickFalse)
577 if (images->progress_monitor != (MagickProgressMonitor) NULL)
582 #if defined(MAGICKCORE_OPENMP_SUPPORT)
583 #pragma omp critical (MagickCore_EvaluateImages)
585 proceed=SetImageProgress(images,EvaluateImageTag,progress++,
587 if (proceed == MagickFalse)
594 #if defined(MAGICKCORE_OPENMP_SUPPORT)
595 #pragma omp parallel for schedule(static,4) shared(progress,status) \
596 magick_threads(image,images,image->rows,key == ~0UL)
598 for (y=0; y < (ssize_t) image->rows; y++)
607 id = GetOpenMPThreadId();
613 register PixelChannels
622 if (status == MagickFalse)
624 q=QueueCacheViewAuthenticPixels(evaluate_view,0,y,image->columns,1,
626 if (q == (Quantum *) NULL)
631 evaluate_pixel=evaluate_pixels[id];
632 for (j=0; j < (ssize_t) image->columns; j++)
633 for (i=0; i < MaxPixelChannels; i++)
634 evaluate_pixel[j].channel[i]=0.0;
636 for (j=0; j < (ssize_t) number_images; j++)
638 register const Quantum
641 image_view=AcquireVirtualCacheView(next,exception);
642 p=GetCacheViewVirtualPixels(image_view,0,y,next->columns,1,exception);
643 if (p == (const Quantum *) NULL)
645 image_view=DestroyCacheView(image_view);
648 for (x=0; x < (ssize_t) next->columns; x++)
653 if (GetPixelReadMask(next,p) == 0)
655 p+=GetPixelChannels(next);
658 for (i=0; i < (ssize_t) GetPixelChannels(next); i++)
660 PixelChannel channel=GetPixelChannelChannel(image,i);
661 PixelTrait traits=GetPixelChannelTraits(next,channel);
662 PixelTrait evaluate_traits=GetPixelChannelTraits(image,channel);
663 if ((traits == UndefinedPixelTrait) ||
664 (evaluate_traits == UndefinedPixelTrait))
666 if ((traits & UpdatePixelTrait) == 0)
668 evaluate_pixel[x].channel[i]=ApplyEvaluateOperator(
669 random_info[id],GetPixelChannel(image,channel,p),j == 0 ?
670 AddEvaluateOperator : op,evaluate_pixel[x].channel[i]);
672 p+=GetPixelChannels(next);
674 image_view=DestroyCacheView(image_view);
675 next=GetNextImageInList(next);
677 for (x=0; x < (ssize_t) image->columns; x++)
684 case MeanEvaluateOperator:
686 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
687 evaluate_pixel[x].channel[i]/=(double) number_images;
690 case MultiplyEvaluateOperator:
692 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
697 for (j=0; j < (ssize_t) (number_images-1); j++)
698 evaluate_pixel[x].channel[i]*=QuantumScale;
706 for (x=0; x < (ssize_t) image->columns; x++)
711 if (GetPixelReadMask(image,q) == 0)
713 q+=GetPixelChannels(image);
716 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
718 PixelChannel channel=GetPixelChannelChannel(image,i);
719 PixelTrait traits=GetPixelChannelTraits(image,channel);
720 if (traits == UndefinedPixelTrait)
722 if ((traits & UpdatePixelTrait) == 0)
724 q[i]=ClampToQuantum(evaluate_pixel[x].channel[i]);
726 q+=GetPixelChannels(image);
728 if (SyncCacheViewAuthenticPixels(evaluate_view,exception) == MagickFalse)
730 if (images->progress_monitor != (MagickProgressMonitor) NULL)
735 #if defined(MAGICKCORE_OPENMP_SUPPORT)
736 #pragma omp critical (MagickCore_EvaluateImages)
738 proceed=SetImageProgress(images,EvaluateImageTag,progress++,
740 if (proceed == MagickFalse)
745 evaluate_view=DestroyCacheView(evaluate_view);
746 evaluate_pixels=DestroyPixelThreadSet(evaluate_pixels);
747 random_info=DestroyRandomInfoThreadSet(random_info);
748 if (status == MagickFalse)
749 image=DestroyImage(image);
753 MagickExport MagickBooleanType EvaluateImage(Image *image,
754 const MagickEvaluateOperator op,const double value,ExceptionInfo *exception)
766 **restrict random_info;
771 #if defined(MAGICKCORE_OPENMP_SUPPORT)
776 assert(image != (Image *) NULL);
777 assert(image->signature == MagickSignature);
778 if (image->debug != MagickFalse)
779 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
780 assert(exception != (ExceptionInfo *) NULL);
781 assert(exception->signature == MagickSignature);
782 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
786 random_info=AcquireRandomInfoThreadSet();
787 #if defined(MAGICKCORE_OPENMP_SUPPORT)
788 key=GetRandomSecretKey(random_info[0]);
790 image_view=AcquireAuthenticCacheView(image,exception);
791 #if defined(MAGICKCORE_OPENMP_SUPPORT)
792 #pragma omp parallel for schedule(static,4) shared(progress,status) \
793 magick_threads(image,image,image->rows,key == ~0UL)
795 for (y=0; y < (ssize_t) image->rows; y++)
798 id = GetOpenMPThreadId();
806 if (status == MagickFalse)
808 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
809 if (q == (Quantum *) NULL)
814 for (x=0; x < (ssize_t) image->columns; x++)
819 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
821 PixelChannel channel=GetPixelChannelChannel(image,i);
822 PixelTrait traits=GetPixelChannelTraits(image,channel);
823 if (traits == UndefinedPixelTrait)
825 if (((traits & CopyPixelTrait) != 0) ||
826 (GetPixelReadMask(image,q) == 0))
828 q[i]=ClampToQuantum(ApplyEvaluateOperator(random_info[id],q[i],op,
831 q+=GetPixelChannels(image);
833 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
835 if (image->progress_monitor != (MagickProgressMonitor) NULL)
840 #if defined(MAGICKCORE_OPENMP_SUPPORT)
841 #pragma omp critical (MagickCore_EvaluateImage)
843 proceed=SetImageProgress(image,EvaluateImageTag,progress++,image->rows);
844 if (proceed == MagickFalse)
848 image_view=DestroyCacheView(image_view);
849 random_info=DestroyRandomInfoThreadSet(random_info);
854 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
858 % F u n c t i o n I m a g e %
862 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
864 % FunctionImage() applies a value to the image with an arithmetic, relational,
865 % or logical operator to an image. Use these operations to lighten or darken
866 % an image, to increase or decrease contrast in an image, or to produce the
867 % "negative" of an image.
869 % The format of the FunctionImage method is:
871 % MagickBooleanType FunctionImage(Image *image,
872 % const MagickFunction function,const ssize_t number_parameters,
873 % const double *parameters,ExceptionInfo *exception)
875 % A description of each parameter follows:
877 % o image: the image.
879 % o function: A channel function.
881 % o parameters: one or more parameters.
883 % o exception: return any errors or warnings in this structure.
887 static Quantum ApplyFunction(Quantum pixel,const MagickFunction function,
888 const size_t number_parameters,const double *parameters,
889 ExceptionInfo *exception)
901 case PolynomialFunction:
904 Polynomial: polynomial constants, highest to lowest order (e.g. c0*x^3+
908 for (i=0; i < (ssize_t) number_parameters; i++)
909 result=result*QuantumScale*pixel+parameters[i];
910 result*=QuantumRange;
913 case SinusoidFunction:
922 Sinusoid: frequency, phase, amplitude, bias.
924 frequency=(number_parameters >= 1) ? parameters[0] : 1.0;
925 phase=(number_parameters >= 2) ? parameters[1] : 0.0;
926 amplitude=(number_parameters >= 3) ? parameters[2] : 0.5;
927 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
928 result=(double) (QuantumRange*(amplitude*sin((double) (2.0*
929 MagickPI*(frequency*QuantumScale*pixel+phase/360.0)))+bias));
941 Arcsin (peged at range limits for invalid results): width, center,
944 width=(number_parameters >= 1) ? parameters[0] : 1.0;
945 center=(number_parameters >= 2) ? parameters[1] : 0.5;
946 range=(number_parameters >= 3) ? parameters[2] : 1.0;
947 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
948 result=2.0/width*(QuantumScale*pixel-center);
949 if ( result <= -1.0 )
950 result=bias-range/2.0;
953 result=bias+range/2.0;
955 result=(double) (range/MagickPI*asin((double) result)+bias);
956 result*=QuantumRange;
968 Arctan: slope, center, range, and bias.
970 slope=(number_parameters >= 1) ? parameters[0] : 1.0;
971 center=(number_parameters >= 2) ? parameters[1] : 0.5;
972 range=(number_parameters >= 3) ? parameters[2] : 1.0;
973 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
974 result=(double) (MagickPI*slope*(QuantumScale*pixel-center));
975 result=(double) (QuantumRange*(range/MagickPI*atan((double)
979 case UndefinedFunction:
982 return(ClampToQuantum(result));
985 MagickExport MagickBooleanType FunctionImage(Image *image,
986 const MagickFunction function,const size_t number_parameters,
987 const double *parameters,ExceptionInfo *exception)
989 #define FunctionImageTag "Function/Image "
1003 assert(image != (Image *) NULL);
1004 assert(image->signature == MagickSignature);
1005 if (image->debug != MagickFalse)
1006 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1007 assert(exception != (ExceptionInfo *) NULL);
1008 assert(exception->signature == MagickSignature);
1009 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1010 return(MagickFalse);
1013 image_view=AcquireAuthenticCacheView(image,exception);
1014 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1015 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1016 magick_threads(image,image,image->rows,1)
1018 for (y=0; y < (ssize_t) image->rows; y++)
1026 if (status == MagickFalse)
1028 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1029 if (q == (Quantum *) NULL)
1034 for (x=0; x < (ssize_t) image->columns; x++)
1039 if (GetPixelReadMask(image,q) == 0)
1041 q+=GetPixelChannels(image);
1044 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1046 PixelChannel channel=GetPixelChannelChannel(image,i);
1047 PixelTrait traits=GetPixelChannelTraits(image,channel);
1048 if (traits == UndefinedPixelTrait)
1050 if ((traits & UpdatePixelTrait) == 0)
1052 q[i]=ApplyFunction(q[i],function,number_parameters,parameters,
1055 q+=GetPixelChannels(image);
1057 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1059 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1064 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1065 #pragma omp critical (MagickCore_FunctionImage)
1067 proceed=SetImageProgress(image,FunctionImageTag,progress++,image->rows);
1068 if (proceed == MagickFalse)
1072 image_view=DestroyCacheView(image_view);
1077 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1081 % G e t I m a g e E x t r e m a %
1085 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1087 % GetImageExtrema() returns the extrema of one or more image channels.
1089 % The format of the GetImageExtrema method is:
1091 % MagickBooleanType GetImageExtrema(const Image *image,size_t *minima,
1092 % size_t *maxima,ExceptionInfo *exception)
1094 % A description of each parameter follows:
1096 % o image: the image.
1098 % o minima: the minimum value in the channel.
1100 % o maxima: the maximum value in the channel.
1102 % o exception: return any errors or warnings in this structure.
1105 MagickExport MagickBooleanType GetImageExtrema(const Image *image,
1106 size_t *minima,size_t *maxima,ExceptionInfo *exception)
1115 assert(image != (Image *) NULL);
1116 assert(image->signature == MagickSignature);
1117 if (image->debug != MagickFalse)
1118 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1119 status=GetImageRange(image,&min,&max,exception);
1120 *minima=(size_t) ceil(min-0.5);
1121 *maxima=(size_t) floor(max+0.5);
1126 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1130 % G e t I m a g e K u r t o s i s %
1134 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1136 % GetImageKurtosis() returns the kurtosis and skewness of one or more image
1139 % The format of the GetImageKurtosis method is:
1141 % MagickBooleanType GetImageKurtosis(const Image *image,double *kurtosis,
1142 % double *skewness,ExceptionInfo *exception)
1144 % A description of each parameter follows:
1146 % o image: the image.
1148 % o kurtosis: the kurtosis of the channel.
1150 % o skewness: the skewness of the channel.
1152 % o exception: return any errors or warnings in this structure.
1155 MagickExport MagickBooleanType GetImageKurtosis(const Image *image,
1156 double *kurtosis,double *skewness,ExceptionInfo *exception)
1175 assert(image != (Image *) NULL);
1176 assert(image->signature == MagickSignature);
1177 if (image->debug != MagickFalse)
1178 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1184 standard_deviation=0.0;
1187 sum_fourth_power=0.0;
1188 image_view=AcquireVirtualCacheView(image,exception);
1189 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1190 #pragma omp parallel for schedule(static,4) shared(status) \
1191 magick_threads(image,image,image->rows,1)
1193 for (y=0; y < (ssize_t) image->rows; y++)
1195 register const Quantum
1201 if (status == MagickFalse)
1203 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1204 if (p == (const Quantum *) NULL)
1209 for (x=0; x < (ssize_t) image->columns; x++)
1214 if (GetPixelReadMask(image,p) == 0)
1216 p+=GetPixelChannels(image);
1219 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1221 PixelChannel channel=GetPixelChannelChannel(image,i);
1222 PixelTrait traits=GetPixelChannelTraits(image,channel);
1223 if (traits == UndefinedPixelTrait)
1225 if ((traits & UpdatePixelTrait) == 0)
1227 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1228 #pragma omp critical (MagickCore_GetImageKurtosis)
1232 sum_squares+=(double) p[i]*p[i];
1233 sum_cubes+=(double) p[i]*p[i]*p[i];
1234 sum_fourth_power+=(double) p[i]*p[i]*p[i]*p[i];
1238 p+=GetPixelChannels(image);
1241 image_view=DestroyCacheView(image_view);
1247 sum_fourth_power/=area;
1249 standard_deviation=sqrt(sum_squares-(mean*mean));
1250 if (standard_deviation != 0.0)
1252 *kurtosis=sum_fourth_power-4.0*mean*sum_cubes+6.0*mean*mean*sum_squares-
1253 3.0*mean*mean*mean*mean;
1254 *kurtosis/=standard_deviation*standard_deviation*standard_deviation*
1257 *skewness=sum_cubes-3.0*mean*sum_squares+2.0*mean*mean*mean;
1258 *skewness/=standard_deviation*standard_deviation*standard_deviation;
1264 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1268 % G e t I m a g e M e a n %
1272 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1274 % GetImageMean() returns the mean and standard deviation of one or more image
1277 % The format of the GetImageMean method is:
1279 % MagickBooleanType GetImageMean(const Image *image,double *mean,
1280 % double *standard_deviation,ExceptionInfo *exception)
1282 % A description of each parameter follows:
1284 % o image: the image.
1286 % o mean: the average value in the channel.
1288 % o standard_deviation: the standard deviation of the channel.
1290 % o exception: return any errors or warnings in this structure.
1293 MagickExport MagickBooleanType GetImageMean(const Image *image,double *mean,
1294 double *standard_deviation,ExceptionInfo *exception)
1300 *channel_statistics;
1305 assert(image != (Image *) NULL);
1306 assert(image->signature == MagickSignature);
1307 if (image->debug != MagickFalse)
1308 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1309 channel_statistics=GetImageStatistics(image,exception);
1310 if (channel_statistics == (ChannelStatistics *) NULL)
1311 return(MagickFalse);
1313 channel_statistics[CompositePixelChannel].mean=0.0;
1314 channel_statistics[CompositePixelChannel].standard_deviation=0.0;
1315 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1317 PixelChannel channel=GetPixelChannelChannel(image,i);
1318 PixelTrait traits=GetPixelChannelTraits(image,channel);
1319 if (traits == UndefinedPixelTrait)
1321 if ((traits & UpdatePixelTrait) == 0)
1323 channel_statistics[CompositePixelChannel].mean+=channel_statistics[i].mean;
1324 channel_statistics[CompositePixelChannel].standard_deviation+=
1325 channel_statistics[i].variance-channel_statistics[i].mean*
1326 channel_statistics[i].mean;
1329 channel_statistics[CompositePixelChannel].mean/=area;
1330 channel_statistics[CompositePixelChannel].standard_deviation=
1331 sqrt(channel_statistics[CompositePixelChannel].standard_deviation/area);
1332 *mean=channel_statistics[CompositePixelChannel].mean;
1333 *standard_deviation=
1334 channel_statistics[CompositePixelChannel].standard_deviation;
1335 channel_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1336 channel_statistics);
1341 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1345 % G e t I m a g e M o m e n t s %
1349 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1351 % GetImageMoments() returns the moments of one or more image channels.
1353 % The format of the GetImageMoments method is:
1355 % ChannelMoments *GetImageMoments(const Image *image,
1356 % ExceptionInfo *exception)
1358 % A description of each parameter follows:
1360 % o image: the image.
1362 % o exception: return any errors or warnings in this structure.
1365 MagickExport ChannelMoments *GetImageMoments(const Image *image,
1366 ExceptionInfo *exception)
1368 #define MaxNumberImageMoments 8
1377 M00[MaxPixelChannels+1],
1378 M01[MaxPixelChannels+1],
1379 M02[MaxPixelChannels+1],
1380 M03[MaxPixelChannels+1],
1381 M10[MaxPixelChannels+1],
1382 M11[MaxPixelChannels+1],
1383 M12[MaxPixelChannels+1],
1384 M20[MaxPixelChannels+1],
1385 M21[MaxPixelChannels+1],
1386 M22[MaxPixelChannels+1],
1387 M30[MaxPixelChannels+1];
1390 centroid[MaxPixelChannels+1];
1396 assert(image != (Image *) NULL);
1397 assert(image->signature == MagickSignature);
1398 if (image->debug != MagickFalse)
1399 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1400 channel_moments=(ChannelMoments *) AcquireQuantumMemory(MaxPixelChannels+1,
1401 sizeof(*channel_moments));
1402 if (channel_moments == (ChannelMoments *) NULL)
1403 return(channel_moments);
1404 (void) ResetMagickMemory(channel_moments,0,(MaxPixelChannels+1)*
1405 sizeof(*channel_moments));
1406 (void) ResetMagickMemory(centroid,0,sizeof(centroid));
1407 (void) ResetMagickMemory(M00,0,sizeof(M00));
1408 (void) ResetMagickMemory(M01,0,sizeof(M01));
1409 (void) ResetMagickMemory(M02,0,sizeof(M02));
1410 (void) ResetMagickMemory(M03,0,sizeof(M03));
1411 (void) ResetMagickMemory(M10,0,sizeof(M10));
1412 (void) ResetMagickMemory(M11,0,sizeof(M11));
1413 (void) ResetMagickMemory(M12,0,sizeof(M12));
1414 (void) ResetMagickMemory(M20,0,sizeof(M20));
1415 (void) ResetMagickMemory(M21,0,sizeof(M21));
1416 (void) ResetMagickMemory(M22,0,sizeof(M22));
1417 (void) ResetMagickMemory(M30,0,sizeof(M30));
1418 image_view=AcquireVirtualCacheView(image,exception);
1419 for (y=0; y < (ssize_t) image->rows; y++)
1421 register const Quantum
1428 Compute center of mass (centroid).
1430 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1431 if (p == (const Quantum *) NULL)
1433 for (x=0; x < (ssize_t) image->columns; x++)
1438 if (GetPixelReadMask(image,p) == 0)
1440 p+=GetPixelChannels(image);
1443 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1445 PixelChannel channel=GetPixelChannelChannel(image,i);
1446 PixelTrait traits=GetPixelChannelTraits(image,channel);
1447 if (traits == UndefinedPixelTrait)
1449 if ((traits & UpdatePixelTrait) == 0)
1452 M10[channel]+=x*p[i];
1453 M01[channel]+=y*p[i];
1455 p+=GetPixelChannels(image);
1458 for (channel=0; channel <= MaxPixelChannels; channel++)
1461 Compute center of mass (centroid).
1463 if (fabs(M00[channel]) < MagickEpsilon)
1465 centroid[channel].x=M10[channel]/M00[channel];
1466 centroid[channel].y=M01[channel]/M00[channel];
1468 for (y=0; y < (ssize_t) image->rows; y++)
1470 register const Quantum
1477 Compute the image moments.
1479 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1480 if (p == (const Quantum *) NULL)
1482 for (x=0; x < (ssize_t) image->columns; x++)
1487 if (GetPixelReadMask(image,p) == 0)
1489 p+=GetPixelChannels(image);
1492 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1494 PixelChannel channel=GetPixelChannelChannel(image,i);
1495 PixelTrait traits=GetPixelChannelTraits(image,channel);
1496 if (traits == UndefinedPixelTrait)
1498 if ((traits & UpdatePixelTrait) == 0)
1500 M11[channel]+=(x-centroid[channel].x)*(y-centroid[channel].y)*p[i];
1501 M20[channel]+=(x-centroid[channel].x)*(x-centroid[channel].x)*p[i];
1502 M02[channel]+=(y-centroid[channel].y)*(y-centroid[channel].y)*p[i];
1503 M21[channel]+=(x-centroid[channel].x)*(x-centroid[channel].x)*
1504 (y-centroid[channel].y)*p[i];
1505 M12[channel]+=(x-centroid[channel].x)*(y-centroid[channel].y)*
1506 (y-centroid[channel].y)*p[i];
1507 M22[channel]+=(x-centroid[channel].x)*(x-centroid[channel].x)*
1508 (y-centroid[channel].y)*(y-centroid[channel].y)*p[i];
1509 M30[channel]+=(x-centroid[channel].x)*(x-centroid[channel].x)*
1510 (x-centroid[channel].x)*p[i];
1511 M03[channel]+=(y-centroid[channel].y)*(y-centroid[channel].y)*
1512 (y-centroid[channel].y)*p[i];
1514 p+=GetPixelChannels(image);
1517 for (channel=0; channel <= MaxPixelChannels; channel++)
1520 Compute elliptical angle, major and minor axes, eccentricity, & intensity.
1522 if (fabs(M00[channel]) < MagickEpsilon)
1524 channel_moments[channel].centroid=centroid[channel];
1525 channel_moments[channel].ellipse_axis.x=sqrt((2.0/M00[channel])*
1526 ((M20[channel]+M02[channel])+sqrt(4.0*M11[channel]*M11[channel]+
1527 (M20[channel]-M02[channel])*(M20[channel]-M02[channel]))));
1528 channel_moments[channel].ellipse_axis.y=sqrt((2.0/M00[channel])*
1529 ((M20[channel]+M02[channel])-sqrt(4.0*M11[channel]*M11[channel]+
1530 (M20[channel]-M02[channel])*(M20[channel]-M02[channel]))));
1531 channel_moments[channel].ellipse_angle=RadiansToDegrees(0.5*atan(2.0*
1532 M11[channel]/(M20[channel]-M02[channel])));
1533 channel_moments[channel].ellipse_eccentricity=sqrt(1.0-(
1534 channel_moments[channel].ellipse_axis.y/
1535 channel_moments[channel].ellipse_axis.x));
1536 channel_moments[channel].ellipse_intensity=M00[channel]/(MagickPI*
1537 channel_moments[channel].ellipse_axis.x*
1538 channel_moments[channel].ellipse_axis.y);
1540 for (channel=0; channel <= MaxPixelChannels; channel++)
1543 Normalize image moments.
1545 if (fabs(M00[channel]) < MagickEpsilon)
1547 M11[channel]/=pow(M00[channel],(1.0+(1.0+1.0)/2.0));
1548 M20[channel]/=pow(M00[channel],(1.0+(2.0+0.0)/2.0));
1549 M02[channel]/=pow(M00[channel],(1.0+(0.0+2.0)/2.0));
1550 M21[channel]/=pow(M00[channel],(1.0+(2.0+1.0)/2.0));
1551 M12[channel]/=pow(M00[channel],(1.0+(1.0+2.0)/2.0));
1552 M22[channel]/=pow(M00[channel],(1.0+(2.0+2.0)/2.0));
1553 M30[channel]/=pow(M00[channel],(1.0+(3.0+0.0)/2.0));
1554 M03[channel]/=pow(M00[channel],(1.0+(0.0+3.0)/2.0));
1556 image_view=DestroyCacheView(image_view);
1557 for (channel=0; channel <= MaxPixelChannels; channel++)
1560 Compute Hu invariant moments.
1562 if (fabs(M00[channel]) < MagickEpsilon)
1564 channel_moments[channel].I[0]=M20[channel]+M02[channel];
1565 channel_moments[channel].I[1]=(M20[channel]-M02[channel])*
1566 (M20[channel]-M02[channel])+4.0*M11[channel]*M11[channel];
1567 channel_moments[channel].I[2]=(M30[channel]-3.0*M12[channel])*
1568 (M30[channel]-3.0*M12[channel])+(3.0*M21[channel]-M03[channel])*
1569 (3.0*M21[channel]-M03[channel]);
1570 channel_moments[channel].I[3]=(M30[channel]+M12[channel])*
1571 (M30[channel]+M12[channel])+(M21[channel]+M03[channel])*
1572 (M21[channel]+M03[channel]);
1573 channel_moments[channel].I[4]=(M30[channel]-3.0*M12[channel])*
1574 (M30[channel]+M12[channel])*((M30[channel]+M12[channel])*
1575 (M30[channel]+M12[channel])-3.0*(M21[channel]+M03[channel])*
1576 (M21[channel]+M03[channel]))+(3.0*M21[channel]-M03[channel])*
1577 (M21[channel]+M03[channel])*(3.0*(M30[channel]+M12[channel])*
1578 (M30[channel]+M12[channel])-(M21[channel]+M03[channel])*
1579 (M21[channel]+M03[channel]));
1580 channel_moments[channel].I[5]=(M20[channel]-M02[channel])*
1581 ((M30[channel]+M12[channel])*(M30[channel]+M12[channel])-
1582 (M21[channel]+M03[channel])*(M21[channel]+M03[channel]))+
1583 4.0*M11[channel]*(M30[channel]+M12[channel])*(M21[channel]+M03[channel]);
1584 channel_moments[channel].I[6]=(3.0*M21[channel]-M03[channel])*
1585 (M30[channel]+M12[channel])*((M30[channel]+M12[channel])*
1586 (M30[channel]+M12[channel])-3.0*(M21[channel]+M03[channel])*
1587 (M21[channel]+M03[channel]))-(M30[channel]-3*M12[channel])*
1588 (M21[channel]+M03[channel])*(3.0*(M30[channel]+M12[channel])*
1589 (M30[channel]+M12[channel])-(M21[channel]+M03[channel])*
1590 (M21[channel]+M03[channel]));
1591 channel_moments[channel].I[7]=M11[channel]*((M30[channel]+M12[channel])*
1592 (M30[channel]+M12[channel])-(M03[channel]+M21[channel])*
1593 (M03[channel]+M21[channel]))-(M20[channel]-M02[channel])*
1594 (M30[channel]+M12[channel])*(M03[channel]+M21[channel]);
1596 if (y < (ssize_t) image->rows)
1597 channel_moments=(ChannelMoments *) RelinquishMagickMemory(channel_moments);
1598 return(channel_moments);
1602 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1606 % G e t I m a g e R a n g e %
1610 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1612 % GetImageRange() returns the range of one or more image channels.
1614 % The format of the GetImageRange method is:
1616 % MagickBooleanType GetImageRange(const Image *image,double *minima,
1617 % double *maxima,ExceptionInfo *exception)
1619 % A description of each parameter follows:
1621 % o image: the image.
1623 % o minima: the minimum value in the channel.
1625 % o maxima: the maximum value in the channel.
1627 % o exception: return any errors or warnings in this structure.
1630 MagickExport MagickBooleanType GetImageRange(const Image *image,double *minima,
1631 double *maxima,ExceptionInfo *exception)
1643 assert(image != (Image *) NULL);
1644 assert(image->signature == MagickSignature);
1645 if (image->debug != MagickFalse)
1646 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1648 initialize=MagickTrue;
1651 image_view=AcquireVirtualCacheView(image,exception);
1652 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1653 #pragma omp parallel for schedule(static,4) shared(status,initialize) \
1654 magick_threads(image,image,image->rows,1)
1656 for (y=0; y < (ssize_t) image->rows; y++)
1658 register const Quantum
1664 if (status == MagickFalse)
1666 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1667 if (p == (const Quantum *) NULL)
1672 for (x=0; x < (ssize_t) image->columns; x++)
1677 if (GetPixelReadMask(image,p) == 0)
1679 p+=GetPixelChannels(image);
1682 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1684 PixelChannel channel=GetPixelChannelChannel(image,i);
1685 PixelTrait traits=GetPixelChannelTraits(image,channel);
1686 if (traits == UndefinedPixelTrait)
1688 if ((traits & UpdatePixelTrait) == 0)
1690 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1691 #pragma omp critical (MagickCore_GetImageRange)
1694 if (initialize != MagickFalse)
1696 *minima=(double) p[i];
1697 *maxima=(double) p[i];
1698 initialize=MagickFalse;
1702 if ((double) p[i] < *minima)
1703 *minima=(double) p[i];
1704 if ((double) p[i] > *maxima)
1705 *maxima=(double) p[i];
1709 p+=GetPixelChannels(image);
1712 image_view=DestroyCacheView(image_view);
1717 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1721 % G e t I m a g e S t a t i s t i c s %
1725 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1727 % GetImageStatistics() returns statistics for each channel in the image. The
1728 % statistics include the channel depth, its minima, maxima, mean, standard
1729 % deviation, kurtosis and skewness. You can access the red channel mean, for
1730 % example, like this:
1732 % channel_statistics=GetImageStatistics(image,exception);
1733 % red_mean=channel_statistics[RedPixelChannel].mean;
1735 % Use MagickRelinquishMemory() to free the statistics buffer.
1737 % The format of the GetImageStatistics method is:
1739 % ChannelStatistics *GetImageStatistics(const Image *image,
1740 % ExceptionInfo *exception)
1742 % A description of each parameter follows:
1744 % o image: the image.
1746 % o exception: return any errors or warnings in this structure.
1750 static size_t GetImageChannels(const Image *image)
1759 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1761 PixelChannel channel=GetPixelChannelChannel(image,i);
1762 PixelTrait traits=GetPixelChannelTraits(image,channel);
1763 if (traits != UndefinedPixelTrait)
1769 MagickExport ChannelStatistics *GetImageStatistics(const Image *image,
1770 ExceptionInfo *exception)
1773 *channel_statistics;
1791 assert(image != (Image *) NULL);
1792 assert(image->signature == MagickSignature);
1793 if (image->debug != MagickFalse)
1794 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1795 channel_statistics=(ChannelStatistics *) AcquireQuantumMemory(
1796 MaxPixelChannels+1,sizeof(*channel_statistics));
1797 if (channel_statistics == (ChannelStatistics *) NULL)
1798 return(channel_statistics);
1799 (void) ResetMagickMemory(channel_statistics,0,(MaxPixelChannels+1)*
1800 sizeof(*channel_statistics));
1801 for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
1803 channel_statistics[i].depth=1;
1804 channel_statistics[i].maxima=(-MagickHuge);
1805 channel_statistics[i].minima=MagickHuge;
1807 for (y=0; y < (ssize_t) image->rows; y++)
1809 register const Quantum
1815 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1816 if (p == (const Quantum *) NULL)
1818 for (x=0; x < (ssize_t) image->columns; x++)
1823 if (GetPixelReadMask(image,p) == 0)
1825 p+=GetPixelChannels(image);
1828 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1830 PixelChannel channel=GetPixelChannelChannel(image,i);
1831 PixelTrait traits=GetPixelChannelTraits(image,channel);
1832 if (traits == UndefinedPixelTrait)
1834 if (channel_statistics[channel].depth != MAGICKCORE_QUANTUM_DEPTH)
1836 depth=channel_statistics[channel].depth;
1837 range=GetQuantumRange(depth);
1838 status=p[i] != ScaleAnyToQuantum(ScaleQuantumToAny(p[i],range),
1839 range) ? MagickTrue : MagickFalse;
1840 if (status != MagickFalse)
1842 channel_statistics[channel].depth++;
1847 if ((double) p[i] < channel_statistics[channel].minima)
1848 channel_statistics[channel].minima=(double) p[i];
1849 if ((double) p[i] > channel_statistics[channel].maxima)
1850 channel_statistics[channel].maxima=(double) p[i];
1851 channel_statistics[channel].sum+=p[i];
1852 channel_statistics[channel].sum_squared+=(double) p[i]*p[i];
1853 channel_statistics[channel].sum_cubed+=(double) p[i]*p[i]*p[i];
1854 channel_statistics[channel].sum_fourth_power+=(double) p[i]*p[i]*p[i]*
1856 channel_statistics[channel].area++;
1858 p+=GetPixelChannels(image);
1861 for (i=0; i < (ssize_t) MaxPixelChannels; i++)
1866 area=PerceptibleReciprocal(channel_statistics[i].area);
1867 channel_statistics[i].sum*=area;
1868 channel_statistics[i].sum_squared*=area;
1869 channel_statistics[i].sum_cubed*=area;
1870 channel_statistics[i].sum_fourth_power*=area;
1871 channel_statistics[i].mean=channel_statistics[i].sum;
1872 channel_statistics[i].variance=channel_statistics[i].sum_squared;
1873 channel_statistics[i].standard_deviation=sqrt(
1874 channel_statistics[i].variance-(channel_statistics[i].mean*
1875 channel_statistics[i].mean));
1877 for (i=0; i < (ssize_t) MaxPixelChannels; i++)
1879 channel_statistics[CompositePixelChannel].area+=channel_statistics[i].area;
1880 channel_statistics[CompositePixelChannel].minima=MagickMin(
1881 channel_statistics[CompositePixelChannel].minima,
1882 channel_statistics[i].minima);
1883 channel_statistics[CompositePixelChannel].maxima=EvaluateMax(
1884 channel_statistics[CompositePixelChannel].maxima,
1885 channel_statistics[i].maxima);
1886 channel_statistics[CompositePixelChannel].sum+=channel_statistics[i].sum;
1887 channel_statistics[CompositePixelChannel].sum_squared+=
1888 channel_statistics[i].sum_squared;
1889 channel_statistics[CompositePixelChannel].sum_cubed+=
1890 channel_statistics[i].sum_cubed;
1891 channel_statistics[CompositePixelChannel].sum_fourth_power+=
1892 channel_statistics[i].sum_fourth_power;
1893 channel_statistics[CompositePixelChannel].mean+=channel_statistics[i].mean;
1894 channel_statistics[CompositePixelChannel].variance+=
1895 channel_statistics[i].variance-channel_statistics[i].mean*
1896 channel_statistics[i].mean;
1897 channel_statistics[CompositePixelChannel].standard_deviation+=
1898 channel_statistics[i].variance-channel_statistics[i].mean*
1899 channel_statistics[i].mean;
1901 channels=GetImageChannels(image);
1902 channel_statistics[CompositePixelChannel].area/=channels;
1903 channel_statistics[CompositePixelChannel].sum/=channels;
1904 channel_statistics[CompositePixelChannel].sum_squared/=channels;
1905 channel_statistics[CompositePixelChannel].sum_cubed/=channels;
1906 channel_statistics[CompositePixelChannel].sum_fourth_power/=channels;
1907 channel_statistics[CompositePixelChannel].mean/=channels;
1908 channel_statistics[CompositePixelChannel].variance/=channels;
1909 channel_statistics[CompositePixelChannel].standard_deviation=
1910 sqrt(channel_statistics[CompositePixelChannel].standard_deviation/channels);
1911 channel_statistics[CompositePixelChannel].kurtosis/=channels;
1912 channel_statistics[CompositePixelChannel].skewness/=channels;
1913 for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
1918 if (channel_statistics[i].standard_deviation == 0.0)
1920 standard_deviation=PerceptibleReciprocal(
1921 channel_statistics[i].standard_deviation);
1922 channel_statistics[i].skewness=(channel_statistics[i].sum_cubed-3.0*
1923 channel_statistics[i].mean*channel_statistics[i].sum_squared+2.0*
1924 channel_statistics[i].mean*channel_statistics[i].mean*
1925 channel_statistics[i].mean)*(standard_deviation*standard_deviation*
1926 standard_deviation);
1927 channel_statistics[i].kurtosis=(channel_statistics[i].sum_fourth_power-4.0*
1928 channel_statistics[i].mean*channel_statistics[i].sum_cubed+6.0*
1929 channel_statistics[i].mean*channel_statistics[i].mean*
1930 channel_statistics[i].sum_squared-3.0*channel_statistics[i].mean*
1931 channel_statistics[i].mean*1.0*channel_statistics[i].mean*
1932 channel_statistics[i].mean)*(standard_deviation*standard_deviation*
1933 standard_deviation*standard_deviation)-3.0;
1935 if (y < (ssize_t) image->rows)
1936 channel_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1937 channel_statistics);
1938 return(channel_statistics);
1942 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1946 % P o l y n o m i a l I m a g e %
1950 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1952 % PolynomialImage() returns a new image where each pixel is the sum of the
1953 % pixels in the image sequence after applying its corresponding terms
1954 % (coefficient and degree pairs).
1956 % The format of the PolynomialImage method is:
1958 % Image *PolynomialImage(const Image *images,const size_t number_terms,
1959 % const double *terms,ExceptionInfo *exception)
1961 % A description of each parameter follows:
1963 % o images: the image sequence.
1965 % o number_terms: the number of terms in the list. The actual list length
1966 % is 2 x number_terms + 1 (the constant).
1968 % o terms: the list of polynomial coefficients and degree pairs and a
1971 % o exception: return any errors or warnings in this structure.
1975 MagickExport Image *PolynomialImage(const Image *images,
1976 const size_t number_terms,const double *terms,ExceptionInfo *exception)
1978 #define PolynomialImageTag "Polynomial/Image"
1993 **restrict polynomial_pixels;
2001 assert(images != (Image *) NULL);
2002 assert(images->signature == MagickSignature);
2003 if (images->debug != MagickFalse)
2004 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
2005 assert(exception != (ExceptionInfo *) NULL);
2006 assert(exception->signature == MagickSignature);
2007 image=CloneImage(images,images->columns,images->rows,MagickTrue,
2009 if (image == (Image *) NULL)
2010 return((Image *) NULL);
2011 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
2013 image=DestroyImage(image);
2014 return((Image *) NULL);
2016 number_images=GetImageListLength(images);
2017 polynomial_pixels=AcquirePixelThreadSet(images,number_images);
2018 if (polynomial_pixels == (PixelChannels **) NULL)
2020 image=DestroyImage(image);
2021 (void) ThrowMagickException(exception,GetMagickModule(),
2022 ResourceLimitError,"MemoryAllocationFailed","`%s'",images->filename);
2023 return((Image *) NULL);
2026 Polynomial image pixels.
2030 polynomial_view=AcquireAuthenticCacheView(image,exception);
2031 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2032 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2033 magick_threads(image,image,image->rows,1)
2035 for (y=0; y < (ssize_t) image->rows; y++)
2044 id = GetOpenMPThreadId();
2050 register PixelChannels
2059 if (status == MagickFalse)
2061 q=QueueCacheViewAuthenticPixels(polynomial_view,0,y,image->columns,1,
2063 if (q == (Quantum *) NULL)
2068 polynomial_pixel=polynomial_pixels[id];
2069 for (j=0; j < (ssize_t) image->columns; j++)
2070 for (i=0; i < MaxPixelChannels; i++)
2071 polynomial_pixel[j].channel[i]=0.0;
2073 for (j=0; j < (ssize_t) number_images; j++)
2075 register const Quantum
2078 if (j >= (ssize_t) number_terms)
2080 image_view=AcquireVirtualCacheView(next,exception);
2081 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2082 if (p == (const Quantum *) NULL)
2084 image_view=DestroyCacheView(image_view);
2087 for (x=0; x < (ssize_t) image->columns; x++)
2092 if (GetPixelReadMask(next,p) == 0)
2094 p+=GetPixelChannels(next);
2097 for (i=0; i < (ssize_t) GetPixelChannels(next); i++)
2103 PixelChannel channel=GetPixelChannelChannel(image,i);
2104 PixelTrait traits=GetPixelChannelTraits(next,channel);
2105 PixelTrait polynomial_traits=GetPixelChannelTraits(image,channel);
2106 if ((traits == UndefinedPixelTrait) ||
2107 (polynomial_traits == UndefinedPixelTrait))
2109 if ((traits & UpdatePixelTrait) == 0)
2111 coefficient=(MagickRealType) terms[2*i];
2112 degree=(MagickRealType) terms[(i << 1)+1];
2113 polynomial_pixel[x].channel[i]+=coefficient*
2114 pow(QuantumScale*GetPixelChannel(image,channel,p),degree);
2116 p+=GetPixelChannels(next);
2118 image_view=DestroyCacheView(image_view);
2119 next=GetNextImageInList(next);
2121 for (x=0; x < (ssize_t) image->columns; x++)
2126 if (GetPixelReadMask(image,q) == 0)
2128 q+=GetPixelChannels(image);
2131 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2133 PixelChannel channel=GetPixelChannelChannel(image,i);
2134 PixelTrait traits=GetPixelChannelTraits(image,channel);
2135 if (traits == UndefinedPixelTrait)
2137 if ((traits & UpdatePixelTrait) == 0)
2139 q[i]=ClampToQuantum(QuantumRange*polynomial_pixel[x].channel[i]);
2141 q+=GetPixelChannels(image);
2143 if (SyncCacheViewAuthenticPixels(polynomial_view,exception) == MagickFalse)
2145 if (images->progress_monitor != (MagickProgressMonitor) NULL)
2150 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2151 #pragma omp critical (MagickCore_PolynomialImages)
2153 proceed=SetImageProgress(images,PolynomialImageTag,progress++,
2155 if (proceed == MagickFalse)
2159 polynomial_view=DestroyCacheView(polynomial_view);
2160 polynomial_pixels=DestroyPixelThreadSet(polynomial_pixels);
2161 if (status == MagickFalse)
2162 image=DestroyImage(image);
2167 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2171 % S t a t i s t i c I m a g e %
2175 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2177 % StatisticImage() makes each pixel the min / max / median / mode / etc. of
2178 % the neighborhood of the specified width and height.
2180 % The format of the StatisticImage method is:
2182 % Image *StatisticImage(const Image *image,const StatisticType type,
2183 % const size_t width,const size_t height,ExceptionInfo *exception)
2185 % A description of each parameter follows:
2187 % o image: the image.
2189 % o type: the statistic type (median, mode, etc.).
2191 % o width: the width of the pixel neighborhood.
2193 % o height: the height of the pixel neighborhood.
2195 % o exception: return any errors or warnings in this structure.
2199 typedef struct _SkipNode
2207 typedef struct _SkipList
2216 typedef struct _PixelList
2229 static PixelList *DestroyPixelList(PixelList *pixel_list)
2231 if (pixel_list == (PixelList *) NULL)
2232 return((PixelList *) NULL);
2233 if (pixel_list->skip_list.nodes != (SkipNode *) NULL)
2234 pixel_list->skip_list.nodes=(SkipNode *) RelinquishMagickMemory(
2235 pixel_list->skip_list.nodes);
2236 pixel_list=(PixelList *) RelinquishMagickMemory(pixel_list);
2240 static PixelList **DestroyPixelListThreadSet(PixelList **pixel_list)
2245 assert(pixel_list != (PixelList **) NULL);
2246 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
2247 if (pixel_list[i] != (PixelList *) NULL)
2248 pixel_list[i]=DestroyPixelList(pixel_list[i]);
2249 pixel_list=(PixelList **) RelinquishMagickMemory(pixel_list);
2253 static PixelList *AcquirePixelList(const size_t width,const size_t height)
2258 pixel_list=(PixelList *) AcquireMagickMemory(sizeof(*pixel_list));
2259 if (pixel_list == (PixelList *) NULL)
2261 (void) ResetMagickMemory((void *) pixel_list,0,sizeof(*pixel_list));
2262 pixel_list->length=width*height;
2263 pixel_list->skip_list.nodes=(SkipNode *) AcquireQuantumMemory(65537UL,
2264 sizeof(*pixel_list->skip_list.nodes));
2265 if (pixel_list->skip_list.nodes == (SkipNode *) NULL)
2266 return(DestroyPixelList(pixel_list));
2267 (void) ResetMagickMemory(pixel_list->skip_list.nodes,0,65537UL*
2268 sizeof(*pixel_list->skip_list.nodes));
2269 pixel_list->signature=MagickSignature;
2273 static PixelList **AcquirePixelListThreadSet(const size_t width,
2274 const size_t height)
2285 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
2286 pixel_list=(PixelList **) AcquireQuantumMemory(number_threads,
2287 sizeof(*pixel_list));
2288 if (pixel_list == (PixelList **) NULL)
2289 return((PixelList **) NULL);
2290 (void) ResetMagickMemory(pixel_list,0,number_threads*sizeof(*pixel_list));
2291 for (i=0; i < (ssize_t) number_threads; i++)
2293 pixel_list[i]=AcquirePixelList(width,height);
2294 if (pixel_list[i] == (PixelList *) NULL)
2295 return(DestroyPixelListThreadSet(pixel_list));
2300 static void AddNodePixelList(PixelList *pixel_list,const size_t color)
2313 Initialize the node.
2315 p=(&pixel_list->skip_list);
2316 p->nodes[color].signature=pixel_list->signature;
2317 p->nodes[color].count=1;
2319 Determine where it belongs in the list.
2322 for (level=p->level; level >= 0; level--)
2324 while (p->nodes[search].next[level] < color)
2325 search=p->nodes[search].next[level];
2326 update[level]=search;
2329 Generate a pseudo-random level for this node.
2331 for (level=0; ; level++)
2333 pixel_list->seed=(pixel_list->seed*42893621L)+1L;
2334 if ((pixel_list->seed & 0x300) != 0x300)
2339 if (level > (p->level+2))
2342 If we're raising the list's level, link back to the root node.
2344 while (level > p->level)
2347 update[p->level]=65536UL;
2350 Link the node into the skip-list.
2354 p->nodes[color].next[level]=p->nodes[update[level]].next[level];
2355 p->nodes[update[level]].next[level]=color;
2356 } while (level-- > 0);
2359 static inline void GetMaximumPixelList(PixelList *pixel_list,Quantum *pixel)
2372 Find the maximum value for each of the color.
2374 p=(&pixel_list->skip_list);
2377 maximum=p->nodes[color].next[0];
2380 color=p->nodes[color].next[0];
2381 if (color > maximum)
2383 count+=p->nodes[color].count;
2384 } while (count < (ssize_t) pixel_list->length);
2385 *pixel=ScaleShortToQuantum((unsigned short) maximum);
2388 static inline void GetMeanPixelList(PixelList *pixel_list,Quantum *pixel)
2403 Find the mean value for each of the color.
2405 p=(&pixel_list->skip_list);
2411 color=p->nodes[color].next[0];
2412 sum+=(double) p->nodes[color].count*color;
2413 count+=p->nodes[color].count;
2414 } while (count < (ssize_t) pixel_list->length);
2415 sum/=pixel_list->length;
2416 *pixel=ScaleShortToQuantum((unsigned short) sum);
2419 static inline void GetMedianPixelList(PixelList *pixel_list,Quantum *pixel)
2431 Find the median value for each of the color.
2433 p=(&pixel_list->skip_list);
2438 color=p->nodes[color].next[0];
2439 count+=p->nodes[color].count;
2440 } while (count <= (ssize_t) (pixel_list->length >> 1));
2441 *pixel=ScaleShortToQuantum((unsigned short) color);
2444 static inline void GetMinimumPixelList(PixelList *pixel_list,Quantum *pixel)
2457 Find the minimum value for each of the color.
2459 p=(&pixel_list->skip_list);
2462 minimum=p->nodes[color].next[0];
2465 color=p->nodes[color].next[0];
2466 if (color < minimum)
2468 count+=p->nodes[color].count;
2469 } while (count < (ssize_t) pixel_list->length);
2470 *pixel=ScaleShortToQuantum((unsigned short) minimum);
2473 static inline void GetModePixelList(PixelList *pixel_list,Quantum *pixel)
2487 Make each pixel the 'predominant color' of the specified neighborhood.
2489 p=(&pixel_list->skip_list);
2492 max_count=p->nodes[mode].count;
2496 color=p->nodes[color].next[0];
2497 if (p->nodes[color].count > max_count)
2500 max_count=p->nodes[mode].count;
2502 count+=p->nodes[color].count;
2503 } while (count < (ssize_t) pixel_list->length);
2504 *pixel=ScaleShortToQuantum((unsigned short) mode);
2507 static inline void GetNonpeakPixelList(PixelList *pixel_list,Quantum *pixel)
2521 Finds the non peak value for each of the colors.
2523 p=(&pixel_list->skip_list);
2525 next=p->nodes[color].next[0];
2531 next=p->nodes[color].next[0];
2532 count+=p->nodes[color].count;
2533 } while (count <= (ssize_t) (pixel_list->length >> 1));
2534 if ((previous == 65536UL) && (next != 65536UL))
2537 if ((previous != 65536UL) && (next == 65536UL))
2539 *pixel=ScaleShortToQuantum((unsigned short) color);
2542 static inline void GetStandardDeviationPixelList(PixelList *pixel_list,
2559 Find the standard-deviation value for each of the color.
2561 p=(&pixel_list->skip_list);
2571 color=p->nodes[color].next[0];
2572 sum+=(double) p->nodes[color].count*color;
2573 for (i=0; i < (ssize_t) p->nodes[color].count; i++)
2574 sum_squared+=((double) color)*((double) color);
2575 count+=p->nodes[color].count;
2576 } while (count < (ssize_t) pixel_list->length);
2577 sum/=pixel_list->length;
2578 sum_squared/=pixel_list->length;
2579 *pixel=ScaleShortToQuantum((unsigned short) sqrt(sum_squared-(sum*sum)));
2582 static inline void InsertPixelList(const Quantum pixel,PixelList *pixel_list)
2590 index=ScaleQuantumToShort(pixel);
2591 signature=pixel_list->skip_list.nodes[index].signature;
2592 if (signature == pixel_list->signature)
2594 pixel_list->skip_list.nodes[index].count++;
2597 AddNodePixelList(pixel_list,index);
2600 static inline double MagickAbsoluteValue(const double x)
2607 static inline size_t MagickMax(const size_t x,const size_t y)
2614 static void ResetPixelList(PixelList *pixel_list)
2626 Reset the skip-list.
2628 p=(&pixel_list->skip_list);
2629 root=p->nodes+65536UL;
2631 for (level=0; level < 9; level++)
2632 root->next[level]=65536UL;
2633 pixel_list->seed=pixel_list->signature++;
2636 MagickExport Image *StatisticImage(const Image *image,const StatisticType type,
2637 const size_t width,const size_t height,ExceptionInfo *exception)
2639 #define StatisticImageTag "Statistic/Image"
2655 **restrict pixel_list;
2662 Initialize statistics image attributes.
2664 assert(image != (Image *) NULL);
2665 assert(image->signature == MagickSignature);
2666 if (image->debug != MagickFalse)
2667 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2668 assert(exception != (ExceptionInfo *) NULL);
2669 assert(exception->signature == MagickSignature);
2670 statistic_image=CloneImage(image,image->columns,image->rows,MagickTrue,
2672 if (statistic_image == (Image *) NULL)
2673 return((Image *) NULL);
2674 status=SetImageStorageClass(statistic_image,DirectClass,exception);
2675 if (status == MagickFalse)
2677 statistic_image=DestroyImage(statistic_image);
2678 return((Image *) NULL);
2680 pixel_list=AcquirePixelListThreadSet(MagickMax(width,1),MagickMax(height,1));
2681 if (pixel_list == (PixelList **) NULL)
2683 statistic_image=DestroyImage(statistic_image);
2684 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2687 Make each pixel the min / max / median / mode / etc. of the neighborhood.
2689 center=(ssize_t) GetPixelChannels(image)*(image->columns+MagickMax(width,1))*
2690 (MagickMax(height,1)/2L)+GetPixelChannels(image)*(MagickMax(width,1)/2L);
2693 image_view=AcquireVirtualCacheView(image,exception);
2694 statistic_view=AcquireAuthenticCacheView(statistic_image,exception);
2695 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2696 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2697 magick_threads(image,statistic_image,statistic_image->rows,1)
2699 for (y=0; y < (ssize_t) statistic_image->rows; y++)
2702 id = GetOpenMPThreadId();
2704 register const Quantum
2713 if (status == MagickFalse)
2715 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) MagickMax(width,1)/2L),y-
2716 (ssize_t) (MagickMax(height,1)/2L),image->columns+MagickMax(width,1),
2717 MagickMax(height,1),exception);
2718 q=QueueCacheViewAuthenticPixels(statistic_view,0,y,statistic_image->columns, 1,exception);
2719 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2724 for (x=0; x < (ssize_t) statistic_image->columns; x++)
2729 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2734 register const Quantum
2743 PixelChannel channel=GetPixelChannelChannel(image,i);
2744 PixelTrait traits=GetPixelChannelTraits(image,channel);
2745 PixelTrait statistic_traits=GetPixelChannelTraits(statistic_image,
2747 if ((traits == UndefinedPixelTrait) ||
2748 (statistic_traits == UndefinedPixelTrait))
2750 if (((statistic_traits & CopyPixelTrait) != 0) ||
2751 (GetPixelReadMask(image,p) == 0))
2753 SetPixelChannel(statistic_image,channel,p[center+i],q);
2757 ResetPixelList(pixel_list[id]);
2758 for (v=0; v < (ssize_t) MagickMax(height,1); v++)
2760 for (u=0; u < (ssize_t) MagickMax(width,1); u++)
2762 InsertPixelList(pixels[i],pixel_list[id]);
2763 pixels+=GetPixelChannels(image);
2765 pixels+=(image->columns-1)*GetPixelChannels(image);
2769 case GradientStatistic:
2775 GetMinimumPixelList(pixel_list[id],&pixel);
2776 minimum=(double) pixel;
2777 GetMaximumPixelList(pixel_list[id],&pixel);
2778 maximum=(double) pixel;
2779 pixel=ClampToQuantum(MagickAbsoluteValue(maximum-minimum));
2782 case MaximumStatistic:
2784 GetMaximumPixelList(pixel_list[id],&pixel);
2789 GetMeanPixelList(pixel_list[id],&pixel);
2792 case MedianStatistic:
2795 GetMedianPixelList(pixel_list[id],&pixel);
2798 case MinimumStatistic:
2800 GetMinimumPixelList(pixel_list[id],&pixel);
2805 GetModePixelList(pixel_list[id],&pixel);
2808 case NonpeakStatistic:
2810 GetNonpeakPixelList(pixel_list[id],&pixel);
2813 case StandardDeviationStatistic:
2815 GetStandardDeviationPixelList(pixel_list[id],&pixel);
2819 SetPixelChannel(statistic_image,channel,pixel,q);
2821 p+=GetPixelChannels(image);
2822 q+=GetPixelChannels(statistic_image);
2824 if (SyncCacheViewAuthenticPixels(statistic_view,exception) == MagickFalse)
2826 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2831 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2832 #pragma omp critical (MagickCore_StatisticImage)
2834 proceed=SetImageProgress(image,StatisticImageTag,progress++,
2836 if (proceed == MagickFalse)
2840 statistic_view=DestroyCacheView(statistic_view);
2841 image_view=DestroyCacheView(image_view);
2842 pixel_list=DestroyPixelListThreadSet(pixel_list);
2843 if (status == MagickFalse)
2844 statistic_image=DestroyImage(statistic_image);
2845 return(statistic_image);