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-2012 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,
236 Quantum pixel,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*
290 case GaussianNoiseEvaluateOperator:
292 result=(double) GenerateDifferentialNoise(random_info,pixel,
293 GaussianNoise,value);
296 case ImpulseNoiseEvaluateOperator:
298 result=(double) GenerateDifferentialNoise(random_info,pixel,
302 case LaplacianNoiseEvaluateOperator:
304 result=(double) GenerateDifferentialNoise(random_info,pixel,
305 LaplacianNoise,value);
308 case LeftShiftEvaluateOperator:
310 result=(double) ((size_t) pixel << (size_t) (value+0.5));
313 case LogEvaluateOperator:
315 if ((QuantumScale*pixel) >= MagickEpsilon)
316 result=(double) (QuantumRange*log((double) (QuantumScale*value*
317 pixel+1.0))/log((double) (value+1.0)));
320 case MaxEvaluateOperator:
322 result=(double) EvaluateMax((double) pixel,value);
325 case MeanEvaluateOperator:
327 result=(double) (pixel+value);
330 case MedianEvaluateOperator:
332 result=(double) (pixel+value);
335 case MinEvaluateOperator:
337 result=(double) MagickMin((double) pixel,value);
340 case MultiplicativeNoiseEvaluateOperator:
342 result=(double) GenerateDifferentialNoise(random_info,pixel,
343 MultiplicativeGaussianNoise,value);
346 case MultiplyEvaluateOperator:
348 result=(double) (value*pixel);
351 case OrEvaluateOperator:
353 result=(double) ((size_t) pixel | (size_t) (value+0.5));
356 case PoissonNoiseEvaluateOperator:
358 result=(double) GenerateDifferentialNoise(random_info,pixel,
362 case PowEvaluateOperator:
364 result=(double) (QuantumRange*pow((double) (QuantumScale*pixel),
368 case RightShiftEvaluateOperator:
370 result=(double) ((size_t) pixel >> (size_t) (value+0.5));
373 case SetEvaluateOperator:
378 case SineEvaluateOperator:
380 result=(double) (QuantumRange*(0.5*sin((double) (2.0*MagickPI*
381 QuantumScale*pixel*value))+0.5));
384 case SubtractEvaluateOperator:
386 result=(double) (pixel-value);
389 case SumEvaluateOperator:
391 result=(double) (pixel+value);
394 case ThresholdEvaluateOperator:
396 result=(double) (((double) pixel <= value) ? 0 :
400 case ThresholdBlackEvaluateOperator:
402 result=(double) (((double) pixel <= value) ? 0 : pixel);
405 case ThresholdWhiteEvaluateOperator:
407 result=(double) (((double) pixel > value) ? QuantumRange :
411 case UniformNoiseEvaluateOperator:
413 result=(double) GenerateDifferentialNoise(random_info,pixel,
417 case XorEvaluateOperator:
419 result=(double) ((size_t) pixel ^ (size_t) (value+0.5));
426 MagickExport Image *EvaluateImages(const Image *images,
427 const MagickEvaluateOperator op,ExceptionInfo *exception)
429 #define EvaluateImageTag "Evaluate/Image"
447 **restrict evaluate_pixels;
450 **restrict random_info;
462 Ensure the image are the same size.
464 assert(images != (Image *) NULL);
465 assert(images->signature == MagickSignature);
466 if (images->debug != MagickFalse)
467 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
468 assert(exception != (ExceptionInfo *) NULL);
469 assert(exception->signature == MagickSignature);
470 for (next=images; next != (Image *) NULL; next=GetNextImageInList(next))
471 if ((next->columns != images->columns) || (next->rows != images->rows))
473 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
474 "ImageWidthsOrHeightsDiffer","'%s'",images->filename);
475 return((Image *) NULL);
478 Initialize evaluate next attributes.
480 image=CloneImage(images,images->columns,images->rows,MagickTrue,
482 if (image == (Image *) NULL)
483 return((Image *) NULL);
484 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
486 image=DestroyImage(image);
487 return((Image *) NULL);
489 number_images=GetImageListLength(images);
490 evaluate_pixels=AcquirePixelThreadSet(images,number_images);
491 if (evaluate_pixels == (PixelChannels **) NULL)
493 image=DestroyImage(image);
494 (void) ThrowMagickException(exception,GetMagickModule(),
495 ResourceLimitError,"MemoryAllocationFailed","'%s'",images->filename);
496 return((Image *) NULL);
499 Evaluate image pixels.
503 random_info=AcquireRandomInfoThreadSet();
504 key=GetRandomSecretKey(random_info[0]);
505 evaluate_view=AcquireAuthenticCacheView(image,exception);
506 if (op == MedianEvaluateOperator)
508 #if defined(MAGICKCORE_OPENMP_SUPPORT)
509 #pragma omp parallel for schedule(static) shared(progress,status) \
510 dynamic_number_threads(image,image->columns,image->rows,key == ~0UL)
512 for (y=0; y < (ssize_t) image->rows; y++)
521 id = GetOpenMPThreadId();
523 register PixelChannels
532 if (status == MagickFalse)
534 q=QueueCacheViewAuthenticPixels(evaluate_view,0,y,
535 image->columns,1,exception);
536 if (q == (Quantum *) NULL)
541 evaluate_pixel=evaluate_pixels[id];
542 for (x=0; x < (ssize_t) image->columns; x++)
548 for (j=0; j < (ssize_t) number_images; j++)
549 for (k=0; k < MaxPixelChannels; k++)
550 evaluate_pixel[j].channel[k]=0.0;
552 for (j=0; j < (ssize_t) number_images; j++)
554 register const Quantum
560 image_view=AcquireVirtualCacheView(next,exception);
561 p=GetCacheViewVirtualPixels(image_view,x,y,1,1,exception);
562 if (p == (const Quantum *) NULL)
564 image_view=DestroyCacheView(image_view);
567 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
576 channel=GetPixelChannelMapChannel(image,i);
577 evaluate_traits=GetPixelChannelMapTraits(image,channel);
578 traits=GetPixelChannelMapTraits(next,channel);
579 if ((traits == UndefinedPixelTrait) ||
580 (evaluate_traits == UndefinedPixelTrait))
582 if ((evaluate_traits & UpdatePixelTrait) == 0)
584 evaluate_pixel[j].channel[i]=ApplyEvaluateOperator(
585 random_info[id],GetPixelChannel(image,channel,p),op,
586 evaluate_pixel[j].channel[i]);
588 image_view=DestroyCacheView(image_view);
589 next=GetNextImageInList(next);
591 qsort((void *) evaluate_pixel,number_images,sizeof(*evaluate_pixel),
593 for (k=0; k < (ssize_t) GetPixelChannels(image); k++)
594 q[k]=ClampToQuantum(evaluate_pixel[j/2].channel[k]);
595 q+=GetPixelChannels(image);
597 if (SyncCacheViewAuthenticPixels(evaluate_view,exception) == MagickFalse)
599 if (images->progress_monitor != (MagickProgressMonitor) NULL)
604 #if defined(MAGICKCORE_OPENMP_SUPPORT)
605 #pragma omp critical (MagickCore_EvaluateImages)
607 proceed=SetImageProgress(images,EvaluateImageTag,progress++,
609 if (proceed == MagickFalse)
616 #if defined(MAGICKCORE_OPENMP_SUPPORT)
617 #pragma omp parallel for schedule(static) shared(progress,status) \
618 dynamic_number_threads(image,image->columns,image->rows,key == ~0UL)
620 for (y=0; y < (ssize_t) image->rows; y++)
629 id = GetOpenMPThreadId();
635 register PixelChannels
644 if (status == MagickFalse)
646 q=QueueCacheViewAuthenticPixels(evaluate_view,0,y,
647 image->columns,1,exception);
648 if (q == (Quantum *) NULL)
653 evaluate_pixel=evaluate_pixels[id];
654 for (j=0; j < (ssize_t) image->columns; j++)
655 for (i=0; i < MaxPixelChannels; i++)
656 evaluate_pixel[j].channel[i]=0.0;
658 for (j=0; j < (ssize_t) number_images; j++)
660 register const Quantum
663 image_view=AcquireVirtualCacheView(next,exception);
664 p=GetCacheViewVirtualPixels(image_view,0,y,next->columns,1,exception);
665 if (p == (const Quantum *) NULL)
667 image_view=DestroyCacheView(image_view);
670 for (x=0; x < (ssize_t) next->columns; x++)
675 if (GetPixelMask(next,p) != 0)
677 p+=GetPixelChannels(next);
680 for (i=0; i < (ssize_t) GetPixelChannels(next); i++)
689 channel=GetPixelChannelMapChannel(image,i);
690 traits=GetPixelChannelMapTraits(next,channel);
691 evaluate_traits=GetPixelChannelMapTraits(image,channel);
692 if ((traits == UndefinedPixelTrait) ||
693 (evaluate_traits == UndefinedPixelTrait))
695 if ((traits & UpdatePixelTrait) == 0)
697 evaluate_pixel[x].channel[i]=ApplyEvaluateOperator(
698 random_info[id],GetPixelChannel(image,channel,p),j ==
699 0 ? AddEvaluateOperator : op,evaluate_pixel[x].channel[i]);
701 p+=GetPixelChannels(next);
703 image_view=DestroyCacheView(image_view);
704 next=GetNextImageInList(next);
706 for (x=0; x < (ssize_t) image->columns; x++)
713 case MeanEvaluateOperator:
715 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
716 evaluate_pixel[x].channel[i]/=(double) number_images;
719 case MultiplyEvaluateOperator:
721 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
726 for (j=0; j < (ssize_t) (number_images-1); j++)
727 evaluate_pixel[x].channel[i]*=QuantumScale;
735 for (x=0; x < (ssize_t) image->columns; x++)
740 if (GetPixelMask(image,q) != 0)
742 q+=GetPixelChannels(image);
745 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
753 channel=GetPixelChannelMapChannel(image,i);
754 traits=GetPixelChannelMapTraits(image,channel);
755 if (traits == UndefinedPixelTrait)
757 if ((traits & UpdatePixelTrait) == 0)
759 q[i]=ClampToQuantum(evaluate_pixel[x].channel[i]);
761 q+=GetPixelChannels(image);
763 if (SyncCacheViewAuthenticPixels(evaluate_view,exception) == MagickFalse)
765 if (images->progress_monitor != (MagickProgressMonitor) NULL)
770 #if defined(MAGICKCORE_OPENMP_SUPPORT)
771 #pragma omp critical (MagickCore_EvaluateImages)
773 proceed=SetImageProgress(images,EvaluateImageTag,progress++,
775 if (proceed == MagickFalse)
780 evaluate_view=DestroyCacheView(evaluate_view);
781 evaluate_pixels=DestroyPixelThreadSet(evaluate_pixels);
782 random_info=DestroyRandomInfoThreadSet(random_info);
783 if (status == MagickFalse)
784 image=DestroyImage(image);
788 MagickExport MagickBooleanType EvaluateImage(Image *image,
789 const MagickEvaluateOperator op,const double value,ExceptionInfo *exception)
801 **restrict random_info;
809 assert(image != (Image *) NULL);
810 assert(image->signature == MagickSignature);
811 if (image->debug != MagickFalse)
812 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
813 assert(exception != (ExceptionInfo *) NULL);
814 assert(exception->signature == MagickSignature);
815 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
819 random_info=AcquireRandomInfoThreadSet();
820 key=GetRandomSecretKey(random_info[0]);
821 image_view=AcquireAuthenticCacheView(image,exception);
822 #if defined(MAGICKCORE_OPENMP_SUPPORT)
823 #pragma omp parallel for schedule(static,4) shared(progress,status) \
824 dynamic_number_threads(image,image->columns,image->rows,key == ~0UL)
826 for (y=0; y < (ssize_t) image->rows; y++)
829 id = GetOpenMPThreadId();
837 if (status == MagickFalse)
839 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
840 if (q == (Quantum *) NULL)
845 for (x=0; x < (ssize_t) image->columns; x++)
850 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
858 channel=GetPixelChannelMapChannel(image,i);
859 traits=GetPixelChannelMapTraits(image,channel);
860 if (traits == UndefinedPixelTrait)
862 if (((traits & CopyPixelTrait) != 0) ||
863 (GetPixelMask(image,q) != 0))
865 q[i]=ClampToQuantum(ApplyEvaluateOperator(random_info[id],q[i],op,
868 q+=GetPixelChannels(image);
870 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
872 if (image->progress_monitor != (MagickProgressMonitor) NULL)
877 #if defined(MAGICKCORE_OPENMP_SUPPORT)
878 #pragma omp critical (MagickCore_EvaluateImage)
880 proceed=SetImageProgress(image,EvaluateImageTag,progress++,image->rows);
881 if (proceed == MagickFalse)
885 image_view=DestroyCacheView(image_view);
886 random_info=DestroyRandomInfoThreadSet(random_info);
891 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
895 % F u n c t i o n I m a g e %
899 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
901 % FunctionImage() applies a value to the image with an arithmetic, relational,
902 % or logical operator to an image. Use these operations to lighten or darken
903 % an image, to increase or decrease contrast in an image, or to produce the
904 % "negative" of an image.
906 % The format of the FunctionImage method is:
908 % MagickBooleanType FunctionImage(Image *image,
909 % const MagickFunction function,const ssize_t number_parameters,
910 % const double *parameters,ExceptionInfo *exception)
912 % A description of each parameter follows:
914 % o image: the image.
916 % o function: A channel function.
918 % o parameters: one or more parameters.
920 % o exception: return any errors or warnings in this structure.
924 static Quantum ApplyFunction(Quantum pixel,const MagickFunction function,
925 const size_t number_parameters,const double *parameters,
926 ExceptionInfo *exception)
938 case PolynomialFunction:
941 Polynomial: polynomial constants, highest to lowest order (e.g. c0*x^3+
945 for (i=0; i < (ssize_t) number_parameters; i++)
946 result=result*QuantumScale*pixel+parameters[i];
947 result*=QuantumRange;
950 case SinusoidFunction:
959 Sinusoid: frequency, phase, amplitude, bias.
961 frequency=(number_parameters >= 1) ? parameters[0] : 1.0;
962 phase=(number_parameters >= 2) ? parameters[1] : 0.0;
963 amplitude=(number_parameters >= 3) ? parameters[2] : 0.5;
964 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
965 result=(double) (QuantumRange*(amplitude*sin((double) (2.0*
966 MagickPI*(frequency*QuantumScale*pixel+phase/360.0)))+bias));
978 Arcsin (peged at range limits for invalid results): width, center,
981 width=(number_parameters >= 1) ? parameters[0] : 1.0;
982 center=(number_parameters >= 2) ? parameters[1] : 0.5;
983 range=(number_parameters >= 3) ? parameters[2] : 1.0;
984 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
985 result=2.0/width*(QuantumScale*pixel-center);
986 if ( result <= -1.0 )
987 result=bias-range/2.0;
990 result=bias+range/2.0;
992 result=(double) (range/MagickPI*asin((double) result)+bias);
993 result*=QuantumRange;
1005 Arctan: slope, center, range, and bias.
1007 slope=(number_parameters >= 1) ? parameters[0] : 1.0;
1008 center=(number_parameters >= 2) ? parameters[1] : 0.5;
1009 range=(number_parameters >= 3) ? parameters[2] : 1.0;
1010 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
1011 result=(double) (MagickPI*slope*(QuantumScale*pixel-center));
1012 result=(double) (QuantumRange*(range/MagickPI*atan((double)
1016 case UndefinedFunction:
1019 return(ClampToQuantum(result));
1022 MagickExport MagickBooleanType FunctionImage(Image *image,
1023 const MagickFunction function,const size_t number_parameters,
1024 const double *parameters,ExceptionInfo *exception)
1026 #define FunctionImageTag "Function/Image "
1040 assert(image != (Image *) NULL);
1041 assert(image->signature == MagickSignature);
1042 if (image->debug != MagickFalse)
1043 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1044 assert(exception != (ExceptionInfo *) NULL);
1045 assert(exception->signature == MagickSignature);
1046 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1047 return(MagickFalse);
1050 image_view=AcquireAuthenticCacheView(image,exception);
1051 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1052 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1053 dynamic_number_threads(image,image->columns,image->rows,1)
1055 for (y=0; y < (ssize_t) image->rows; y++)
1063 if (status == MagickFalse)
1065 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1066 if (q == (Quantum *) NULL)
1071 for (x=0; x < (ssize_t) image->columns; x++)
1076 if (GetPixelMask(image,q) != 0)
1078 q+=GetPixelChannels(image);
1081 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1089 channel=GetPixelChannelMapChannel(image,i);
1090 traits=GetPixelChannelMapTraits(image,channel);
1091 if (traits == UndefinedPixelTrait)
1093 if ((traits & UpdatePixelTrait) == 0)
1095 q[i]=ApplyFunction(q[i],function,number_parameters,parameters,
1098 q+=GetPixelChannels(image);
1100 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1102 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1107 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1108 #pragma omp critical (MagickCore_FunctionImage)
1110 proceed=SetImageProgress(image,FunctionImageTag,progress++,image->rows);
1111 if (proceed == MagickFalse)
1115 image_view=DestroyCacheView(image_view);
1120 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1124 % G e t I m a g e E x t r e m a %
1128 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1130 % GetImageExtrema() returns the extrema of one or more image channels.
1132 % The format of the GetImageExtrema method is:
1134 % MagickBooleanType GetImageExtrema(const Image *image,size_t *minima,
1135 % size_t *maxima,ExceptionInfo *exception)
1137 % A description of each parameter follows:
1139 % o image: the image.
1141 % o minima: the minimum value in the channel.
1143 % o maxima: the maximum value in the channel.
1145 % o exception: return any errors or warnings in this structure.
1148 MagickExport MagickBooleanType GetImageExtrema(const Image *image,
1149 size_t *minima,size_t *maxima,ExceptionInfo *exception)
1158 assert(image != (Image *) NULL);
1159 assert(image->signature == MagickSignature);
1160 if (image->debug != MagickFalse)
1161 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1162 status=GetImageRange(image,&min,&max,exception);
1163 *minima=(size_t) ceil(min-0.5);
1164 *maxima=(size_t) floor(max+0.5);
1169 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1173 % G e t I m a g e M e a n %
1177 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1179 % GetImageMean() returns the mean and standard deviation of one or more image
1182 % The format of the GetImageMean method is:
1184 % MagickBooleanType GetImageMean(const Image *image,double *mean,
1185 % double *standard_deviation,ExceptionInfo *exception)
1187 % A description of each parameter follows:
1189 % o image: the image.
1191 % o mean: the average value in the channel.
1193 % o standard_deviation: the standard deviation of the channel.
1195 % o exception: return any errors or warnings in this structure.
1198 MagickExport MagickBooleanType GetImageMean(const Image *image,double *mean,
1199 double *standard_deviation,ExceptionInfo *exception)
1205 *channel_statistics;
1210 assert(image != (Image *) NULL);
1211 assert(image->signature == MagickSignature);
1212 if (image->debug != MagickFalse)
1213 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1214 channel_statistics=GetImageStatistics(image,exception);
1215 if (channel_statistics == (ChannelStatistics *) NULL)
1216 return(MagickFalse);
1218 channel_statistics[CompositePixelChannel].mean=0.0;
1219 channel_statistics[CompositePixelChannel].standard_deviation=0.0;
1220 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1228 channel=GetPixelChannelMapChannel(image,i);
1229 traits=GetPixelChannelMapTraits(image,channel);
1230 if (traits == UndefinedPixelTrait)
1232 if ((traits & UpdatePixelTrait) == 0)
1234 channel_statistics[CompositePixelChannel].mean+=channel_statistics[i].mean;
1235 channel_statistics[CompositePixelChannel].standard_deviation+=
1236 channel_statistics[i].variance-channel_statistics[i].mean*
1237 channel_statistics[i].mean;
1240 channel_statistics[CompositePixelChannel].mean/=area;
1241 channel_statistics[CompositePixelChannel].standard_deviation=
1242 sqrt(channel_statistics[CompositePixelChannel].standard_deviation/area);
1243 *mean=channel_statistics[CompositePixelChannel].mean;
1244 *standard_deviation=
1245 channel_statistics[CompositePixelChannel].standard_deviation;
1246 channel_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1247 channel_statistics);
1252 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1256 % G e t I m a g e K u r t o s i s %
1260 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1262 % GetImageKurtosis() returns the kurtosis and skewness of one or more
1265 % The format of the GetImageKurtosis method is:
1267 % MagickBooleanType GetImageKurtosis(const Image *image,double *kurtosis,
1268 % double *skewness,ExceptionInfo *exception)
1270 % A description of each parameter follows:
1272 % o image: the image.
1274 % o kurtosis: the kurtosis of the channel.
1276 % o skewness: the skewness of the channel.
1278 % o exception: return any errors or warnings in this structure.
1281 MagickExport MagickBooleanType GetImageKurtosis(const Image *image,
1282 double *kurtosis,double *skewness,ExceptionInfo *exception)
1301 assert(image != (Image *) NULL);
1302 assert(image->signature == MagickSignature);
1303 if (image->debug != MagickFalse)
1304 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1310 standard_deviation=0.0;
1313 sum_fourth_power=0.0;
1314 image_view=AcquireVirtualCacheView(image,exception);
1315 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1316 #pragma omp parallel for schedule(static) shared(status) \
1317 dynamic_number_threads(image,image->columns,image->rows,1)
1319 for (y=0; y < (ssize_t) image->rows; y++)
1321 register const Quantum
1327 if (status == MagickFalse)
1329 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1330 if (p == (const Quantum *) NULL)
1335 for (x=0; x < (ssize_t) image->columns; x++)
1340 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1348 channel=GetPixelChannelMapChannel(image,i);
1349 traits=GetPixelChannelMapTraits(image,channel);
1350 if (traits == UndefinedPixelTrait)
1352 if (((traits & UpdatePixelTrait) == 0) ||
1353 (GetPixelMask(image,p) != 0))
1355 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1356 #pragma omp critical (MagickCore_GetImageKurtosis)
1360 sum_squares+=(double) p[i]*p[i];
1361 sum_cubes+=(double) p[i]*p[i]*p[i];
1362 sum_fourth_power+=(double) p[i]*p[i]*p[i]*p[i];
1366 p+=GetPixelChannels(image);
1369 image_view=DestroyCacheView(image_view);
1375 sum_fourth_power/=area;
1377 standard_deviation=sqrt(sum_squares-(mean*mean));
1378 if (standard_deviation != 0.0)
1380 *kurtosis=sum_fourth_power-4.0*mean*sum_cubes+6.0*mean*mean*sum_squares-
1381 3.0*mean*mean*mean*mean;
1382 *kurtosis/=standard_deviation*standard_deviation*standard_deviation*
1385 *skewness=sum_cubes-3.0*mean*sum_squares+2.0*mean*mean*mean;
1386 *skewness/=standard_deviation*standard_deviation*standard_deviation;
1392 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1396 % G e t I m a g e R a n g e %
1400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1402 % GetImageRange() returns the range of one or more image channels.
1404 % The format of the GetImageRange method is:
1406 % MagickBooleanType GetImageRange(const Image *image,double *minima,
1407 % double *maxima,ExceptionInfo *exception)
1409 % A description of each parameter follows:
1411 % o image: the image.
1413 % o minima: the minimum value in the channel.
1415 % o maxima: the maximum value in the channel.
1417 % o exception: return any errors or warnings in this structure.
1420 MagickExport MagickBooleanType GetImageRange(const Image *image,double *minima,
1421 double *maxima,ExceptionInfo *exception)
1433 assert(image != (Image *) NULL);
1434 assert(image->signature == MagickSignature);
1435 if (image->debug != MagickFalse)
1436 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1438 initialize=MagickTrue;
1441 image_view=AcquireVirtualCacheView(image,exception);
1442 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1443 #pragma omp parallel for schedule(static) shared(status,initialize) \
1444 dynamic_number_threads(image,image->columns,image->rows,1)
1446 for (y=0; y < (ssize_t) image->rows; y++)
1448 register const Quantum
1454 if (status == MagickFalse)
1456 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1457 if (p == (const Quantum *) NULL)
1462 for (x=0; x < (ssize_t) image->columns; x++)
1467 if (GetPixelMask(image,p) != 0)
1469 p+=GetPixelChannels(image);
1472 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1480 channel=GetPixelChannelMapChannel(image,i);
1481 traits=GetPixelChannelMapTraits(image,channel);
1482 if (traits == UndefinedPixelTrait)
1484 if ((traits & UpdatePixelTrait) == 0)
1486 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1487 #pragma omp critical (MagickCore_GetImageRange)
1490 if (initialize != MagickFalse)
1492 *minima=(double) p[i];
1493 *maxima=(double) p[i];
1494 initialize=MagickFalse;
1498 if ((double) p[i] < *minima)
1499 *minima=(double) p[i];
1500 if ((double) p[i] > *maxima)
1501 *maxima=(double) p[i];
1505 p+=GetPixelChannels(image);
1508 image_view=DestroyCacheView(image_view);
1513 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1517 % G e t I m a g e S t a t i s t i c s %
1521 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1523 % GetImageStatistics() returns statistics for each channel in the image. The
1524 % statistics include the channel depth, its minima, maxima, mean, standard
1525 % deviation, kurtosis and skewness. You can access the red channel mean, for
1526 % example, like this:
1528 % channel_statistics=GetImageStatistics(image,exception);
1529 % red_mean=channel_statistics[RedPixelChannel].mean;
1531 % Use MagickRelinquishMemory() to free the statistics buffer.
1533 % The format of the GetImageStatistics method is:
1535 % ChannelStatistics *GetImageStatistics(const Image *image,
1536 % ExceptionInfo *exception)
1538 % A description of each parameter follows:
1540 % o image: the image.
1542 % o exception: return any errors or warnings in this structure.
1546 static size_t GetImageChannels(const Image *image)
1555 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1563 channel=GetPixelChannelMapChannel(image,i);
1564 traits=GetPixelChannelMapTraits(image,channel);
1565 if ((traits & UpdatePixelTrait) != 0)
1571 MagickExport ChannelStatistics *GetImageStatistics(const Image *image,
1572 ExceptionInfo *exception)
1575 *channel_statistics;
1593 assert(image != (Image *) NULL);
1594 assert(image->signature == MagickSignature);
1595 if (image->debug != MagickFalse)
1596 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1597 channel_statistics=(ChannelStatistics *) AcquireQuantumMemory(
1598 MaxPixelChannels+1,sizeof(*channel_statistics));
1599 if (channel_statistics == (ChannelStatistics *) NULL)
1600 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1601 (void) ResetMagickMemory(channel_statistics,0,(MaxPixelChannels+1)*
1602 sizeof(*channel_statistics));
1603 for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
1605 channel_statistics[i].depth=1;
1606 channel_statistics[i].maxima=(-MagickHuge);
1607 channel_statistics[i].minima=MagickHuge;
1609 for (y=0; y < (ssize_t) image->rows; y++)
1611 register const Quantum
1617 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1618 if (p == (const Quantum *) NULL)
1620 for (x=0; x < (ssize_t) image->columns; x++)
1625 if (GetPixelMask(image,p) != 0)
1627 p+=GetPixelChannels(image);
1630 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1638 channel=GetPixelChannelMapChannel(image,i);
1639 traits=GetPixelChannelMapTraits(image,channel);
1640 if (traits == UndefinedPixelTrait)
1642 if (channel_statistics[channel].depth != MAGICKCORE_QUANTUM_DEPTH)
1644 depth=channel_statistics[channel].depth;
1645 range=GetQuantumRange(depth);
1646 status=p[i] != ScaleAnyToQuantum(ScaleQuantumToAny(p[i],range),
1647 range) ? MagickTrue : MagickFalse;
1648 if (status != MagickFalse)
1650 channel_statistics[channel].depth++;
1655 if ((double) p[i] < channel_statistics[channel].minima)
1656 channel_statistics[channel].minima=(double) p[i];
1657 if ((double) p[i] > channel_statistics[channel].maxima)
1658 channel_statistics[channel].maxima=(double) p[i];
1659 channel_statistics[channel].sum+=p[i];
1660 channel_statistics[channel].sum_squared+=(double) p[i]*p[i];
1661 channel_statistics[channel].sum_cubed+=(double) p[i]*p[i]*p[i];
1662 channel_statistics[channel].sum_fourth_power+=(double) p[i]*p[i]*p[i]*
1664 channel_statistics[channel].area++;
1666 p+=GetPixelChannels(image);
1669 for (i=0; i < (ssize_t) MaxPixelChannels; i++)
1674 area=MagickEpsilonReciprocal(channel_statistics[i].area);
1675 channel_statistics[i].sum*=area;
1676 channel_statistics[i].sum_squared*=area;
1677 channel_statistics[i].sum_cubed*=area;
1678 channel_statistics[i].sum_fourth_power*=area;
1679 channel_statistics[i].mean=channel_statistics[i].sum;
1680 channel_statistics[i].variance=channel_statistics[i].sum_squared;
1681 channel_statistics[i].standard_deviation=sqrt(
1682 channel_statistics[i].variance-(channel_statistics[i].mean*
1683 channel_statistics[i].mean));
1685 for (i=0; i < (ssize_t) MaxPixelChannels; i++)
1687 channel_statistics[CompositePixelChannel].depth=(size_t) EvaluateMax(
1688 (double) channel_statistics[CompositePixelChannel].depth,(double)
1689 channel_statistics[i].depth);
1690 channel_statistics[CompositePixelChannel].minima=MagickMin(
1691 channel_statistics[CompositePixelChannel].minima,
1692 channel_statistics[i].minima);
1693 channel_statistics[CompositePixelChannel].maxima=EvaluateMax(
1694 channel_statistics[CompositePixelChannel].maxima,
1695 channel_statistics[i].maxima);
1696 channel_statistics[CompositePixelChannel].sum+=channel_statistics[i].sum;
1697 channel_statistics[CompositePixelChannel].sum_squared+=
1698 channel_statistics[i].sum_squared;
1699 channel_statistics[CompositePixelChannel].sum_cubed+=
1700 channel_statistics[i].sum_cubed;
1701 channel_statistics[CompositePixelChannel].sum_fourth_power+=
1702 channel_statistics[i].sum_fourth_power;
1703 channel_statistics[CompositePixelChannel].mean+=channel_statistics[i].mean;
1704 channel_statistics[CompositePixelChannel].variance+=
1705 channel_statistics[i].variance-channel_statistics[i].mean*
1706 channel_statistics[i].mean;
1707 channel_statistics[CompositePixelChannel].standard_deviation+=
1708 channel_statistics[i].variance-channel_statistics[i].mean*
1709 channel_statistics[i].mean;
1711 channels=GetImageChannels(image);
1712 channel_statistics[CompositePixelChannel].sum/=channels;
1713 channel_statistics[CompositePixelChannel].sum_squared/=channels;
1714 channel_statistics[CompositePixelChannel].sum_cubed/=channels;
1715 channel_statistics[CompositePixelChannel].sum_fourth_power/=channels;
1716 channel_statistics[CompositePixelChannel].mean/=channels;
1717 channel_statistics[CompositePixelChannel].variance/=channels;
1718 channel_statistics[CompositePixelChannel].standard_deviation=
1719 sqrt(channel_statistics[CompositePixelChannel].standard_deviation/channels);
1720 channel_statistics[CompositePixelChannel].kurtosis/=channels;
1721 channel_statistics[CompositePixelChannel].skewness/=channels;
1722 for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
1727 standard_deviation=MagickEpsilonReciprocal(
1728 channel_statistics[i].standard_deviation);
1729 channel_statistics[i].skewness=(channel_statistics[i].sum_cubed-3.0*
1730 channel_statistics[i].mean*channel_statistics[i].sum_squared+2.0*
1731 channel_statistics[i].mean*channel_statistics[i].mean*
1732 channel_statistics[i].mean)*(standard_deviation*standard_deviation*
1733 standard_deviation);
1734 channel_statistics[i].kurtosis=(channel_statistics[i].sum_fourth_power-4.0*
1735 channel_statistics[i].mean*channel_statistics[i].sum_cubed+6.0*
1736 channel_statistics[i].mean*channel_statistics[i].mean*
1737 channel_statistics[i].sum_squared-3.0*channel_statistics[i].mean*
1738 channel_statistics[i].mean*1.0*channel_statistics[i].mean*
1739 channel_statistics[i].mean)*(standard_deviation*standard_deviation*
1740 standard_deviation*standard_deviation)-3.0;
1742 return(channel_statistics);
1746 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1750 % S t a t i s t i c I m a g e %
1754 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1756 % StatisticImage() makes each pixel the min / max / median / mode / etc. of
1757 % the neighborhood of the specified width and height.
1759 % The format of the StatisticImage method is:
1761 % Image *StatisticImage(const Image *image,const StatisticType type,
1762 % const size_t width,const size_t height,ExceptionInfo *exception)
1764 % A description of each parameter follows:
1766 % o image: the image.
1768 % o type: the statistic type (median, mode, etc.).
1770 % o width: the width of the pixel neighborhood.
1772 % o height: the height of the pixel neighborhood.
1774 % o exception: return any errors or warnings in this structure.
1778 typedef struct _SkipNode
1786 typedef struct _SkipList
1795 typedef struct _PixelList
1808 static PixelList *DestroyPixelList(PixelList *pixel_list)
1810 if (pixel_list == (PixelList *) NULL)
1811 return((PixelList *) NULL);
1812 if (pixel_list->skip_list.nodes != (SkipNode *) NULL)
1813 pixel_list->skip_list.nodes=(SkipNode *) RelinquishMagickMemory(
1814 pixel_list->skip_list.nodes);
1815 pixel_list=(PixelList *) RelinquishMagickMemory(pixel_list);
1819 static PixelList **DestroyPixelListThreadSet(PixelList **pixel_list)
1824 assert(pixel_list != (PixelList **) NULL);
1825 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
1826 if (pixel_list[i] != (PixelList *) NULL)
1827 pixel_list[i]=DestroyPixelList(pixel_list[i]);
1828 pixel_list=(PixelList **) RelinquishMagickMemory(pixel_list);
1832 static PixelList *AcquirePixelList(const size_t width,const size_t height)
1837 pixel_list=(PixelList *) AcquireMagickMemory(sizeof(*pixel_list));
1838 if (pixel_list == (PixelList *) NULL)
1840 (void) ResetMagickMemory((void *) pixel_list,0,sizeof(*pixel_list));
1841 pixel_list->length=width*height;
1842 pixel_list->skip_list.nodes=(SkipNode *) AcquireQuantumMemory(65537UL,
1843 sizeof(*pixel_list->skip_list.nodes));
1844 if (pixel_list->skip_list.nodes == (SkipNode *) NULL)
1845 return(DestroyPixelList(pixel_list));
1846 (void) ResetMagickMemory(pixel_list->skip_list.nodes,0,65537UL*
1847 sizeof(*pixel_list->skip_list.nodes));
1848 pixel_list->signature=MagickSignature;
1852 static PixelList **AcquirePixelListThreadSet(const size_t width,
1853 const size_t height)
1864 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
1865 pixel_list=(PixelList **) AcquireQuantumMemory(number_threads,
1866 sizeof(*pixel_list));
1867 if (pixel_list == (PixelList **) NULL)
1868 return((PixelList **) NULL);
1869 (void) ResetMagickMemory(pixel_list,0,number_threads*sizeof(*pixel_list));
1870 for (i=0; i < (ssize_t) number_threads; i++)
1872 pixel_list[i]=AcquirePixelList(width,height);
1873 if (pixel_list[i] == (PixelList *) NULL)
1874 return(DestroyPixelListThreadSet(pixel_list));
1879 static void AddNodePixelList(PixelList *pixel_list,const size_t color)
1892 Initialize the node.
1894 p=(&pixel_list->skip_list);
1895 p->nodes[color].signature=pixel_list->signature;
1896 p->nodes[color].count=1;
1898 Determine where it belongs in the list.
1901 for (level=p->level; level >= 0; level--)
1903 while (p->nodes[search].next[level] < color)
1904 search=p->nodes[search].next[level];
1905 update[level]=search;
1908 Generate a pseudo-random level for this node.
1910 for (level=0; ; level++)
1912 pixel_list->seed=(pixel_list->seed*42893621L)+1L;
1913 if ((pixel_list->seed & 0x300) != 0x300)
1918 if (level > (p->level+2))
1921 If we're raising the list's level, link back to the root node.
1923 while (level > p->level)
1926 update[p->level]=65536UL;
1929 Link the node into the skip-list.
1933 p->nodes[color].next[level]=p->nodes[update[level]].next[level];
1934 p->nodes[update[level]].next[level]=color;
1935 } while (level-- > 0);
1938 static inline void GetMaximumPixelList(PixelList *pixel_list,Quantum *pixel)
1951 Find the maximum value for each of the color.
1953 p=(&pixel_list->skip_list);
1956 maximum=p->nodes[color].next[0];
1959 color=p->nodes[color].next[0];
1960 if (color > maximum)
1962 count+=p->nodes[color].count;
1963 } while (count < (ssize_t) pixel_list->length);
1964 *pixel=ScaleShortToQuantum((unsigned short) maximum);
1967 static inline void GetMeanPixelList(PixelList *pixel_list,Quantum *pixel)
1982 Find the mean value for each of the color.
1984 p=(&pixel_list->skip_list);
1990 color=p->nodes[color].next[0];
1991 sum+=(double) p->nodes[color].count*color;
1992 count+=p->nodes[color].count;
1993 } while (count < (ssize_t) pixel_list->length);
1994 sum/=pixel_list->length;
1995 *pixel=ScaleShortToQuantum((unsigned short) sum);
1998 static inline void GetMedianPixelList(PixelList *pixel_list,Quantum *pixel)
2010 Find the median value for each of the color.
2012 p=(&pixel_list->skip_list);
2017 color=p->nodes[color].next[0];
2018 count+=p->nodes[color].count;
2019 } while (count <= (ssize_t) (pixel_list->length >> 1));
2020 *pixel=ScaleShortToQuantum((unsigned short) color);
2023 static inline void GetMinimumPixelList(PixelList *pixel_list,Quantum *pixel)
2036 Find the minimum value for each of the color.
2038 p=(&pixel_list->skip_list);
2041 minimum=p->nodes[color].next[0];
2044 color=p->nodes[color].next[0];
2045 if (color < minimum)
2047 count+=p->nodes[color].count;
2048 } while (count < (ssize_t) pixel_list->length);
2049 *pixel=ScaleShortToQuantum((unsigned short) minimum);
2052 static inline void GetModePixelList(PixelList *pixel_list,Quantum *pixel)
2066 Make each pixel the 'predominant color' of the specified neighborhood.
2068 p=(&pixel_list->skip_list);
2071 max_count=p->nodes[mode].count;
2075 color=p->nodes[color].next[0];
2076 if (p->nodes[color].count > max_count)
2079 max_count=p->nodes[mode].count;
2081 count+=p->nodes[color].count;
2082 } while (count < (ssize_t) pixel_list->length);
2083 *pixel=ScaleShortToQuantum((unsigned short) mode);
2086 static inline void GetNonpeakPixelList(PixelList *pixel_list,Quantum *pixel)
2100 Finds the non peak value for each of the colors.
2102 p=(&pixel_list->skip_list);
2104 next=p->nodes[color].next[0];
2110 next=p->nodes[color].next[0];
2111 count+=p->nodes[color].count;
2112 } while (count <= (ssize_t) (pixel_list->length >> 1));
2113 if ((previous == 65536UL) && (next != 65536UL))
2116 if ((previous != 65536UL) && (next == 65536UL))
2118 *pixel=ScaleShortToQuantum((unsigned short) color);
2121 static inline void GetStandardDeviationPixelList(PixelList *pixel_list,
2138 Find the standard-deviation value for each of the color.
2140 p=(&pixel_list->skip_list);
2150 color=p->nodes[color].next[0];
2151 sum+=(double) p->nodes[color].count*color;
2152 for (i=0; i < (ssize_t) p->nodes[color].count; i++)
2153 sum_squared+=((double) color)*((double) color);
2154 count+=p->nodes[color].count;
2155 } while (count < (ssize_t) pixel_list->length);
2156 sum/=pixel_list->length;
2157 sum_squared/=pixel_list->length;
2158 *pixel=ScaleShortToQuantum((unsigned short) sqrt(sum_squared-(sum*sum)));
2161 static inline void InsertPixelList(const Image *image,const Quantum pixel,
2162 PixelList *pixel_list)
2170 index=ScaleQuantumToShort(pixel);
2171 signature=pixel_list->skip_list.nodes[index].signature;
2172 if (signature == pixel_list->signature)
2174 pixel_list->skip_list.nodes[index].count++;
2177 AddNodePixelList(pixel_list,index);
2180 static inline double MagickAbsoluteValue(const double x)
2187 static inline size_t MagickMax(const size_t x,const size_t y)
2194 static void ResetPixelList(PixelList *pixel_list)
2206 Reset the skip-list.
2208 p=(&pixel_list->skip_list);
2209 root=p->nodes+65536UL;
2211 for (level=0; level < 9; level++)
2212 root->next[level]=65536UL;
2213 pixel_list->seed=pixel_list->signature++;
2216 MagickExport Image *StatisticImage(const Image *image,const StatisticType type,
2217 const size_t width,const size_t height,ExceptionInfo *exception)
2219 #define StatisticImageTag "Statistic/Image"
2235 **restrict pixel_list;
2242 Initialize statistics image attributes.
2244 assert(image != (Image *) NULL);
2245 assert(image->signature == MagickSignature);
2246 if (image->debug != MagickFalse)
2247 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2248 assert(exception != (ExceptionInfo *) NULL);
2249 assert(exception->signature == MagickSignature);
2250 statistic_image=CloneImage(image,image->columns,image->rows,MagickTrue,
2252 if (statistic_image == (Image *) NULL)
2253 return((Image *) NULL);
2254 status=SetImageStorageClass(statistic_image,DirectClass,exception);
2255 if (status == MagickFalse)
2257 statistic_image=DestroyImage(statistic_image);
2258 return((Image *) NULL);
2260 pixel_list=AcquirePixelListThreadSet(MagickMax(width,1),MagickMax(height,1));
2261 if (pixel_list == (PixelList **) NULL)
2263 statistic_image=DestroyImage(statistic_image);
2264 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2267 Make each pixel the min / max / median / mode / etc. of the neighborhood.
2269 center=(ssize_t) GetPixelChannels(image)*(image->columns+MagickMax(width,1))*
2270 (MagickMax(height,1)/2L)+GetPixelChannels(image)*(MagickMax(width,1)/2L);
2273 image_view=AcquireVirtualCacheView(image,exception);
2274 statistic_view=AcquireAuthenticCacheView(statistic_image,exception);
2275 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2276 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2277 dynamic_number_threads(image,image->columns,image->rows,1)
2279 for (y=0; y < (ssize_t) statistic_image->rows; y++)
2282 id = GetOpenMPThreadId();
2284 register const Quantum
2293 if (status == MagickFalse)
2295 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) MagickMax(width,1)/2L),y-
2296 (ssize_t) (MagickMax(height,1)/2L),image->columns+MagickMax(width,1),
2297 MagickMax(height,1),exception);
2298 q=QueueCacheViewAuthenticPixels(statistic_view,0,y,statistic_image->columns, 1,exception);
2299 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2304 for (x=0; x < (ssize_t) statistic_image->columns; x++)
2309 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2321 register const Quantum
2330 channel=GetPixelChannelMapChannel(image,i);
2331 traits=GetPixelChannelMapTraits(image,channel);
2332 statistic_traits=GetPixelChannelMapTraits(statistic_image,channel);
2333 if ((traits == UndefinedPixelTrait) ||
2334 (statistic_traits == UndefinedPixelTrait))
2336 if (((statistic_traits & CopyPixelTrait) != 0) ||
2337 (GetPixelMask(image,p) != 0))
2339 SetPixelChannel(statistic_image,channel,p[center+i],q);
2343 ResetPixelList(pixel_list[id]);
2344 for (v=0; v < (ssize_t) MagickMax(height,1); v++)
2346 for (u=0; u < (ssize_t) MagickMax(width,1); u++)
2348 InsertPixelList(image,pixels[i],pixel_list[id]);
2349 pixels+=GetPixelChannels(image);
2351 pixels+=image->columns*GetPixelChannels(image);
2355 case GradientStatistic:
2361 GetMinimumPixelList(pixel_list[id],&pixel);
2362 minimum=(double) pixel;
2363 GetMaximumPixelList(pixel_list[id],&pixel);
2364 maximum=(double) pixel;
2365 pixel=ClampToQuantum(MagickAbsoluteValue(maximum-minimum));
2368 case MaximumStatistic:
2370 GetMaximumPixelList(pixel_list[id],&pixel);
2375 GetMeanPixelList(pixel_list[id],&pixel);
2378 case MedianStatistic:
2381 GetMedianPixelList(pixel_list[id],&pixel);
2384 case MinimumStatistic:
2386 GetMinimumPixelList(pixel_list[id],&pixel);
2391 GetModePixelList(pixel_list[id],&pixel);
2394 case NonpeakStatistic:
2396 GetNonpeakPixelList(pixel_list[id],&pixel);
2399 case StandardDeviationStatistic:
2401 GetStandardDeviationPixelList(pixel_list[id],&pixel);
2405 SetPixelChannel(statistic_image,channel,pixel,q);
2407 p+=GetPixelChannels(image);
2408 q+=GetPixelChannels(statistic_image);
2410 if (SyncCacheViewAuthenticPixels(statistic_view,exception) == MagickFalse)
2412 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2417 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2418 #pragma omp critical (MagickCore_StatisticImage)
2420 proceed=SetImageProgress(image,StatisticImageTag,progress++,
2422 if (proceed == MagickFalse)
2426 statistic_view=DestroyCacheView(statistic_view);
2427 image_view=DestroyCacheView(image_view);
2428 pixel_list=DestroyPixelListThreadSet(pixel_list);
2429 return(statistic_image);