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=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]-(MagickRealType) 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 MagickRealType ApplyEvaluateOperator(RandomInfo *random_info,
236 Quantum pixel,const MagickEvaluateOperator op,const MagickRealType value)
244 case UndefinedEvaluateOperator:
246 case AbsEvaluateOperator:
248 result=(MagickRealType) fabs((double) (pixel+value));
251 case AddEvaluateOperator:
253 result=(MagickRealType) (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=(MagickRealType) ((size_t) pixel & (size_t) (value+0.5));
273 case CosineEvaluateOperator:
275 result=(MagickRealType) (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=(MagickRealType) (QuantumRange*exp((double) (value*QuantumScale*
290 case GaussianNoiseEvaluateOperator:
292 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
293 GaussianNoise,value);
296 case ImpulseNoiseEvaluateOperator:
298 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
302 case LaplacianNoiseEvaluateOperator:
304 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
305 LaplacianNoise,value);
308 case LeftShiftEvaluateOperator:
310 result=(MagickRealType) ((size_t) pixel << (size_t) (value+0.5));
313 case LogEvaluateOperator:
315 if ((QuantumScale*pixel) >= MagickEpsilon)
316 result=(MagickRealType) (QuantumRange*log((double) (QuantumScale*value*
317 pixel+1.0))/log((double) (value+1.0)));
320 case MaxEvaluateOperator:
322 result=(MagickRealType) EvaluateMax((double) pixel,value);
325 case MeanEvaluateOperator:
327 result=(MagickRealType) (pixel+value);
330 case MedianEvaluateOperator:
332 result=(MagickRealType) (pixel+value);
335 case MinEvaluateOperator:
337 result=(MagickRealType) MagickMin((double) pixel,value);
340 case MultiplicativeNoiseEvaluateOperator:
342 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
343 MultiplicativeGaussianNoise,value);
346 case MultiplyEvaluateOperator:
348 result=(MagickRealType) (value*pixel);
351 case OrEvaluateOperator:
353 result=(MagickRealType) ((size_t) pixel | (size_t) (value+0.5));
356 case PoissonNoiseEvaluateOperator:
358 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
362 case PowEvaluateOperator:
364 result=(MagickRealType) (QuantumRange*pow((double) (QuantumScale*pixel),
368 case RightShiftEvaluateOperator:
370 result=(MagickRealType) ((size_t) pixel >> (size_t) (value+0.5));
373 case SetEvaluateOperator:
378 case SineEvaluateOperator:
380 result=(MagickRealType) (QuantumRange*(0.5*sin((double) (2.0*MagickPI*
381 QuantumScale*pixel*value))+0.5));
384 case SubtractEvaluateOperator:
386 result=(MagickRealType) (pixel-value);
389 case SumEvaluateOperator:
391 result=(MagickRealType) (pixel+value);
394 case ThresholdEvaluateOperator:
396 result=(MagickRealType) (((MagickRealType) pixel <= value) ? 0 :
400 case ThresholdBlackEvaluateOperator:
402 result=(MagickRealType) (((MagickRealType) pixel <= value) ? 0 : pixel);
405 case ThresholdWhiteEvaluateOperator:
407 result=(MagickRealType) (((MagickRealType) pixel > value) ? QuantumRange :
411 case UniformNoiseEvaluateOperator:
413 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
417 case XorEvaluateOperator:
419 result=(MagickRealType) ((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]/=(MagickRealType) 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 if (GetPixelMask(image,q) != 0)
852 q+=GetPixelChannels(image);
855 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
863 channel=GetPixelChannelMapChannel(image,i);
864 traits=GetPixelChannelMapTraits(image,channel);
865 if (traits == UndefinedPixelTrait)
867 if ((traits & CopyPixelTrait) != 0)
869 q[i]=ClampToQuantum(ApplyEvaluateOperator(random_info[id],q[i],op,
872 q+=GetPixelChannels(image);
874 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
876 if (image->progress_monitor != (MagickProgressMonitor) NULL)
881 #if defined(MAGICKCORE_OPENMP_SUPPORT)
882 #pragma omp critical (MagickCore_EvaluateImage)
884 proceed=SetImageProgress(image,EvaluateImageTag,progress++,image->rows);
885 if (proceed == MagickFalse)
889 image_view=DestroyCacheView(image_view);
890 random_info=DestroyRandomInfoThreadSet(random_info);
895 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
899 % F u n c t i o n I m a g e %
903 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
905 % FunctionImage() applies a value to the image with an arithmetic, relational,
906 % or logical operator to an image. Use these operations to lighten or darken
907 % an image, to increase or decrease contrast in an image, or to produce the
908 % "negative" of an image.
910 % The format of the FunctionImage method is:
912 % MagickBooleanType FunctionImage(Image *image,
913 % const MagickFunction function,const ssize_t number_parameters,
914 % const double *parameters,ExceptionInfo *exception)
916 % A description of each parameter follows:
918 % o image: the image.
920 % o function: A channel function.
922 % o parameters: one or more parameters.
924 % o exception: return any errors or warnings in this structure.
928 static Quantum ApplyFunction(Quantum pixel,const MagickFunction function,
929 const size_t number_parameters,const double *parameters,
930 ExceptionInfo *exception)
942 case PolynomialFunction:
945 Polynomial: polynomial constants, highest to lowest order (e.g. c0*x^3+
949 for (i=0; i < (ssize_t) number_parameters; i++)
950 result=result*QuantumScale*pixel+parameters[i];
951 result*=QuantumRange;
954 case SinusoidFunction:
963 Sinusoid: frequency, phase, amplitude, bias.
965 frequency=(number_parameters >= 1) ? parameters[0] : 1.0;
966 phase=(number_parameters >= 2) ? parameters[1] : 0.0;
967 amplitude=(number_parameters >= 3) ? parameters[2] : 0.5;
968 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
969 result=(MagickRealType) (QuantumRange*(amplitude*sin((double) (2.0*
970 MagickPI*(frequency*QuantumScale*pixel+phase/360.0)))+bias));
982 Arcsin (peged at range limits for invalid results): width, center,
985 width=(number_parameters >= 1) ? parameters[0] : 1.0;
986 center=(number_parameters >= 2) ? parameters[1] : 0.5;
987 range=(number_parameters >= 3) ? parameters[2] : 1.0;
988 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
989 result=2.0/width*(QuantumScale*pixel-center);
990 if ( result <= -1.0 )
991 result=bias-range/2.0;
994 result=bias+range/2.0;
996 result=(MagickRealType) (range/MagickPI*asin((double) result)+bias);
997 result*=QuantumRange;
1000 case ArctanFunction:
1009 Arctan: slope, center, range, and bias.
1011 slope=(number_parameters >= 1) ? parameters[0] : 1.0;
1012 center=(number_parameters >= 2) ? parameters[1] : 0.5;
1013 range=(number_parameters >= 3) ? parameters[2] : 1.0;
1014 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
1015 result=(MagickRealType) (MagickPI*slope*(QuantumScale*pixel-center));
1016 result=(MagickRealType) (QuantumRange*(range/MagickPI*atan((double)
1020 case UndefinedFunction:
1023 return(ClampToQuantum(result));
1026 MagickExport MagickBooleanType FunctionImage(Image *image,
1027 const MagickFunction function,const size_t number_parameters,
1028 const double *parameters,ExceptionInfo *exception)
1030 #define FunctionImageTag "Function/Image "
1044 assert(image != (Image *) NULL);
1045 assert(image->signature == MagickSignature);
1046 if (image->debug != MagickFalse)
1047 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1048 assert(exception != (ExceptionInfo *) NULL);
1049 assert(exception->signature == MagickSignature);
1050 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1051 return(MagickFalse);
1054 image_view=AcquireAuthenticCacheView(image,exception);
1055 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1056 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1057 dynamic_number_threads(image,image->columns,image->rows,1)
1059 for (y=0; y < (ssize_t) image->rows; y++)
1067 if (status == MagickFalse)
1069 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1070 if (q == (Quantum *) NULL)
1075 for (x=0; x < (ssize_t) image->columns; x++)
1080 if (GetPixelMask(image,q) != 0)
1082 q+=GetPixelChannels(image);
1085 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1093 channel=GetPixelChannelMapChannel(image,i);
1094 traits=GetPixelChannelMapTraits(image,channel);
1095 if (traits == UndefinedPixelTrait)
1097 if ((traits & UpdatePixelTrait) == 0)
1099 q[i]=ApplyFunction(q[i],function,number_parameters,parameters,
1102 q+=GetPixelChannels(image);
1104 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1106 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1111 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1112 #pragma omp critical (MagickCore_FunctionImage)
1114 proceed=SetImageProgress(image,FunctionImageTag,progress++,image->rows);
1115 if (proceed == MagickFalse)
1119 image_view=DestroyCacheView(image_view);
1124 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1128 % G e t I m a g e E x t r e m a %
1132 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1134 % GetImageExtrema() returns the extrema of one or more image channels.
1136 % The format of the GetImageExtrema method is:
1138 % MagickBooleanType GetImageExtrema(const Image *image,size_t *minima,
1139 % size_t *maxima,ExceptionInfo *exception)
1141 % A description of each parameter follows:
1143 % o image: the image.
1145 % o minima: the minimum value in the channel.
1147 % o maxima: the maximum value in the channel.
1149 % o exception: return any errors or warnings in this structure.
1152 MagickExport MagickBooleanType GetImageExtrema(const Image *image,
1153 size_t *minima,size_t *maxima,ExceptionInfo *exception)
1162 assert(image != (Image *) NULL);
1163 assert(image->signature == MagickSignature);
1164 if (image->debug != MagickFalse)
1165 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1166 status=GetImageRange(image,&min,&max,exception);
1167 *minima=(size_t) ceil(min-0.5);
1168 *maxima=(size_t) floor(max+0.5);
1173 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1177 % G e t I m a g e M e a n %
1181 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1183 % GetImageMean() returns the mean and standard deviation of one or more
1186 % The format of the GetImageMean method is:
1188 % MagickBooleanType GetImageMean(const Image *image,double *mean,
1189 % double *standard_deviation,ExceptionInfo *exception)
1191 % A description of each parameter follows:
1193 % o image: the image.
1195 % o mean: the average value in the channel.
1197 % o standard_deviation: the standard deviation of the channel.
1199 % o exception: return any errors or warnings in this structure.
1202 MagickExport MagickBooleanType GetImageMean(const Image *image,double *mean,
1203 double *standard_deviation,ExceptionInfo *exception)
1206 *channel_statistics;
1214 assert(image != (Image *) NULL);
1215 assert(image->signature == MagickSignature);
1216 if (image->debug != MagickFalse)
1217 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1218 channel_statistics=GetImageStatistics(image,exception);
1219 if (channel_statistics == (ChannelStatistics *) NULL)
1220 return(MagickFalse);
1222 channel_statistics[CompositePixelChannel].mean=0.0;
1223 channel_statistics[CompositePixelChannel].standard_deviation=0.0;
1224 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1232 channel=GetPixelChannelMapChannel(image,i);
1233 traits=GetPixelChannelMapTraits(image,channel);
1234 if (traits == UndefinedPixelTrait)
1236 if ((traits & UpdatePixelTrait) == 0)
1238 channel_statistics[CompositePixelChannel].mean+=channel_statistics[i].mean;
1239 channel_statistics[CompositePixelChannel].standard_deviation+=
1240 channel_statistics[i].variance-channel_statistics[i].mean*
1241 channel_statistics[i].mean;
1244 channel_statistics[CompositePixelChannel].mean/=area;
1245 channel_statistics[CompositePixelChannel].standard_deviation=
1246 sqrt(channel_statistics[CompositePixelChannel].standard_deviation/area);
1247 *mean=channel_statistics[CompositePixelChannel].mean;
1248 *standard_deviation=
1249 channel_statistics[CompositePixelChannel].standard_deviation;
1250 channel_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1251 channel_statistics);
1256 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1260 % G e t I m a g e K u r t o s i s %
1264 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1266 % GetImageKurtosis() returns the kurtosis and skewness of one or more
1269 % The format of the GetImageKurtosis method is:
1271 % MagickBooleanType GetImageKurtosis(const Image *image,double *kurtosis,
1272 % double *skewness,ExceptionInfo *exception)
1274 % A description of each parameter follows:
1276 % o image: the image.
1278 % o kurtosis: the kurtosis of the channel.
1280 % o skewness: the skewness of the channel.
1282 % o exception: return any errors or warnings in this structure.
1285 MagickExport MagickBooleanType GetImageKurtosis(const Image *image,
1286 double *kurtosis,double *skewness,ExceptionInfo *exception)
1305 assert(image != (Image *) NULL);
1306 assert(image->signature == MagickSignature);
1307 if (image->debug != MagickFalse)
1308 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1314 standard_deviation=0.0;
1317 sum_fourth_power=0.0;
1318 image_view=AcquireVirtualCacheView(image,exception);
1319 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1320 #pragma omp parallel for schedule(static) shared(status) \
1321 dynamic_number_threads(image,image->columns,image->rows,1)
1323 for (y=0; y < (ssize_t) image->rows; y++)
1325 register const Quantum
1331 if (status == MagickFalse)
1333 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1334 if (p == (const Quantum *) NULL)
1339 for (x=0; x < (ssize_t) image->columns; x++)
1344 if (GetPixelMask(image,p) != 0)
1346 p+=GetPixelChannels(image);
1349 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1357 channel=GetPixelChannelMapChannel(image,i);
1358 traits=GetPixelChannelMapTraits(image,channel);
1359 if (traits == UndefinedPixelTrait)
1361 if ((traits & UpdatePixelTrait) == 0)
1363 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1364 #pragma omp critical (MagickCore_GetImageKurtosis)
1368 sum_squares+=(double) p[i]*p[i];
1369 sum_cubes+=(double) p[i]*p[i]*p[i];
1370 sum_fourth_power+=(double) p[i]*p[i]*p[i]*p[i];
1374 p+=GetPixelChannels(image);
1377 image_view=DestroyCacheView(image_view);
1383 sum_fourth_power/=area;
1385 standard_deviation=sqrt(sum_squares-(mean*mean));
1386 if (standard_deviation != 0.0)
1388 *kurtosis=sum_fourth_power-4.0*mean*sum_cubes+6.0*mean*mean*sum_squares-
1389 3.0*mean*mean*mean*mean;
1390 *kurtosis/=standard_deviation*standard_deviation*standard_deviation*
1393 *skewness=sum_cubes-3.0*mean*sum_squares+2.0*mean*mean*mean;
1394 *skewness/=standard_deviation*standard_deviation*standard_deviation;
1400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1404 % G e t I m a g e R a n g e %
1408 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1410 % GetImageRange() returns the range of one or more image channels.
1412 % The format of the GetImageRange method is:
1414 % MagickBooleanType GetImageRange(const Image *image,double *minima,
1415 % double *maxima,ExceptionInfo *exception)
1417 % A description of each parameter follows:
1419 % o image: the image.
1421 % o minima: the minimum value in the channel.
1423 % o maxima: the maximum value in the channel.
1425 % o exception: return any errors or warnings in this structure.
1428 MagickExport MagickBooleanType GetImageRange(const Image *image,double *minima,
1429 double *maxima,ExceptionInfo *exception)
1441 assert(image != (Image *) NULL);
1442 assert(image->signature == MagickSignature);
1443 if (image->debug != MagickFalse)
1444 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1446 initialize=MagickTrue;
1449 image_view=AcquireVirtualCacheView(image,exception);
1450 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1451 #pragma omp parallel for schedule(static) shared(status,initialize) \
1452 dynamic_number_threads(image,image->columns,image->rows,1)
1454 for (y=0; y < (ssize_t) image->rows; y++)
1456 register const Quantum
1462 if (status == MagickFalse)
1464 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1465 if (p == (const Quantum *) NULL)
1470 for (x=0; x < (ssize_t) image->columns; x++)
1475 if (GetPixelMask(image,p) != 0)
1477 p+=GetPixelChannels(image);
1480 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1488 channel=GetPixelChannelMapChannel(image,i);
1489 traits=GetPixelChannelMapTraits(image,channel);
1490 if (traits == UndefinedPixelTrait)
1492 if ((traits & UpdatePixelTrait) == 0)
1494 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1495 #pragma omp critical (MagickCore_GetImageRange)
1498 if (initialize != MagickFalse)
1500 *minima=(double) p[i];
1501 *maxima=(double) p[i];
1502 initialize=MagickFalse;
1506 if ((double) p[i] < *minima)
1507 *minima=(double) p[i];
1508 if ((double) p[i] > *maxima)
1509 *maxima=(double) p[i];
1513 p+=GetPixelChannels(image);
1516 image_view=DestroyCacheView(image_view);
1521 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1525 % G e t I m a g e S t a t i s t i c s %
1529 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1531 % GetImageStatistics() returns statistics for each channel in the
1532 % image. The statistics include the channel depth, its minima, maxima, mean,
1533 % standard deviation, kurtosis and skewness. You can access the red channel
1534 % mean, for example, like this:
1536 % channel_statistics=GetImageStatistics(image,exception);
1537 % red_mean=channel_statistics[RedPixelChannel].mean;
1539 % Use MagickRelinquishMemory() to free the statistics buffer.
1541 % The format of the GetImageStatistics method is:
1543 % ChannelStatistics *GetImageStatistics(const Image *image,
1544 % ExceptionInfo *exception)
1546 % A description of each parameter follows:
1548 % o image: the image.
1550 % o exception: return any errors or warnings in this structure.
1554 static size_t GetImageChannels(const Image *image)
1563 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1571 channel=GetPixelChannelMapChannel(image,i);
1572 traits=GetPixelChannelMapTraits(image,channel);
1573 if ((traits & UpdatePixelTrait) != 0)
1579 MagickExport ChannelStatistics *GetImageStatistics(const Image *image,
1580 ExceptionInfo *exception)
1583 *channel_statistics;
1605 assert(image != (Image *) NULL);
1606 assert(image->signature == MagickSignature);
1607 if (image->debug != MagickFalse)
1608 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1609 channel_statistics=(ChannelStatistics *) AcquireQuantumMemory(
1610 MaxPixelChannels+1,sizeof(*channel_statistics));
1611 if (channel_statistics == (ChannelStatistics *) NULL)
1612 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1613 (void) ResetMagickMemory(channel_statistics,0,(MaxPixelChannels+1)*
1614 sizeof(*channel_statistics));
1615 for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
1617 channel_statistics[i].depth=1;
1618 channel_statistics[i].maxima=0.0;
1619 channel_statistics[i].minima=0.0;
1621 initialize=MagickTrue;
1622 for (y=0; y < (ssize_t) image->rows; y++)
1624 register const Quantum
1630 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1631 if (p == (const Quantum *) NULL)
1633 for (x=0; x < (ssize_t) image->columns; x++)
1638 if (GetPixelMask(image,p) != 0)
1640 p+=GetPixelChannels(image);
1643 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1651 channel=GetPixelChannelMapChannel(image,i);
1652 traits=GetPixelChannelMapTraits(image,channel);
1653 if (traits == UndefinedPixelTrait)
1655 if (channel_statistics[channel].depth != MAGICKCORE_QUANTUM_DEPTH)
1657 depth=channel_statistics[channel].depth;
1658 range=GetQuantumRange(depth);
1659 status=p[i] != ScaleAnyToQuantum(ScaleQuantumToAny(p[i],range),
1660 range) ? MagickTrue : MagickFalse;
1661 if (status != MagickFalse)
1663 channel_statistics[channel].depth++;
1668 if (initialize != MagickFalse)
1670 channel_statistics[channel].minima=(double) p[i];
1671 channel_statistics[channel].maxima=(double) p[i];
1672 initialize=MagickFalse;
1676 if ((double) p[i] < channel_statistics[channel].minima)
1677 channel_statistics[channel].minima=(double) p[i];
1678 if ((double) p[i] > channel_statistics[channel].maxima)
1679 channel_statistics[channel].maxima=(double) p[i];
1681 channel_statistics[channel].sum+=p[i];
1682 channel_statistics[channel].sum_squared+=(double) p[i]*p[i];
1683 channel_statistics[channel].sum_cubed+=(double) p[i]*p[i]*p[i];
1684 channel_statistics[channel].sum_fourth_power+=(double) p[i]*p[i]*p[i]*
1687 p+=GetPixelChannels(image);
1690 area=(double) image->columns*image->rows;
1691 for (i=0; i < (ssize_t) MaxPixelChannels; i++)
1693 channel_statistics[i].sum/=area;
1694 channel_statistics[i].sum_squared/=area;
1695 channel_statistics[i].sum_cubed/=area;
1696 channel_statistics[i].sum_fourth_power/=area;
1697 channel_statistics[i].mean=channel_statistics[i].sum;
1698 channel_statistics[i].variance=channel_statistics[i].sum_squared;
1699 channel_statistics[i].standard_deviation=sqrt(
1700 channel_statistics[i].variance-(channel_statistics[i].mean*
1701 channel_statistics[i].mean));
1703 for (i=0; i < (ssize_t) MaxPixelChannels; i++)
1705 channel_statistics[CompositePixelChannel].depth=(size_t) EvaluateMax(
1706 (double) channel_statistics[CompositePixelChannel].depth,(double)
1707 channel_statistics[i].depth);
1708 channel_statistics[CompositePixelChannel].minima=MagickMin(
1709 channel_statistics[CompositePixelChannel].minima,
1710 channel_statistics[i].minima);
1711 channel_statistics[CompositePixelChannel].maxima=EvaluateMax(
1712 channel_statistics[CompositePixelChannel].maxima,
1713 channel_statistics[i].maxima);
1714 channel_statistics[CompositePixelChannel].sum+=channel_statistics[i].sum;
1715 channel_statistics[CompositePixelChannel].sum_squared+=
1716 channel_statistics[i].sum_squared;
1717 channel_statistics[CompositePixelChannel].sum_cubed+=
1718 channel_statistics[i].sum_cubed;
1719 channel_statistics[CompositePixelChannel].sum_fourth_power+=
1720 channel_statistics[i].sum_fourth_power;
1721 channel_statistics[CompositePixelChannel].mean+=channel_statistics[i].mean;
1722 channel_statistics[CompositePixelChannel].variance+=
1723 channel_statistics[i].variance-channel_statistics[i].mean*
1724 channel_statistics[i].mean;
1725 channel_statistics[CompositePixelChannel].standard_deviation+=
1726 channel_statistics[i].variance-channel_statistics[i].mean*
1727 channel_statistics[i].mean;
1729 channels=GetImageChannels(image);
1730 channel_statistics[CompositePixelChannel].sum/=channels;
1731 channel_statistics[CompositePixelChannel].sum_squared/=channels;
1732 channel_statistics[CompositePixelChannel].sum_cubed/=channels;
1733 channel_statistics[CompositePixelChannel].sum_fourth_power/=channels;
1734 channel_statistics[CompositePixelChannel].mean/=channels;
1735 channel_statistics[CompositePixelChannel].variance/=channels;
1736 channel_statistics[CompositePixelChannel].standard_deviation=
1737 sqrt(channel_statistics[CompositePixelChannel].standard_deviation/channels);
1738 channel_statistics[CompositePixelChannel].kurtosis/=channels;
1739 channel_statistics[CompositePixelChannel].skewness/=channels;
1740 for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
1742 if (channel_statistics[i].standard_deviation == 0.0)
1744 channel_statistics[i].skewness=(channel_statistics[i].sum_cubed-3.0*
1745 channel_statistics[i].mean*channel_statistics[i].sum_squared+2.0*
1746 channel_statistics[i].mean*channel_statistics[i].mean*
1747 channel_statistics[i].mean)/(channel_statistics[i].standard_deviation*
1748 channel_statistics[i].standard_deviation*
1749 channel_statistics[i].standard_deviation);
1750 channel_statistics[i].kurtosis=(channel_statistics[i].sum_fourth_power-4.0*
1751 channel_statistics[i].mean*channel_statistics[i].sum_cubed+6.0*
1752 channel_statistics[i].mean*channel_statistics[i].mean*
1753 channel_statistics[i].sum_squared-3.0*channel_statistics[i].mean*
1754 channel_statistics[i].mean*1.0*channel_statistics[i].mean*
1755 channel_statistics[i].mean)/(channel_statistics[i].standard_deviation*
1756 channel_statistics[i].standard_deviation*
1757 channel_statistics[i].standard_deviation*
1758 channel_statistics[i].standard_deviation)-3.0;
1760 return(channel_statistics);
1764 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1768 % S t a t i s t i c I m a g e %
1772 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1774 % StatisticImage() makes each pixel the min / max / median / mode / etc. of
1775 % the neighborhood of the specified width and height.
1777 % The format of the StatisticImage method is:
1779 % Image *StatisticImage(const Image *image,const StatisticType type,
1780 % const size_t width,const size_t height,ExceptionInfo *exception)
1782 % A description of each parameter follows:
1784 % o image: the image.
1786 % o type: the statistic type (median, mode, etc.).
1788 % o width: the width of the pixel neighborhood.
1790 % o height: the height of the pixel neighborhood.
1792 % o exception: return any errors or warnings in this structure.
1796 typedef struct _SkipNode
1804 typedef struct _SkipList
1813 typedef struct _PixelList
1826 static PixelList *DestroyPixelList(PixelList *pixel_list)
1828 if (pixel_list == (PixelList *) NULL)
1829 return((PixelList *) NULL);
1830 if (pixel_list->skip_list.nodes != (SkipNode *) NULL)
1831 pixel_list->skip_list.nodes=(SkipNode *) RelinquishMagickMemory(
1832 pixel_list->skip_list.nodes);
1833 pixel_list=(PixelList *) RelinquishMagickMemory(pixel_list);
1837 static PixelList **DestroyPixelListThreadSet(PixelList **pixel_list)
1842 assert(pixel_list != (PixelList **) NULL);
1843 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
1844 if (pixel_list[i] != (PixelList *) NULL)
1845 pixel_list[i]=DestroyPixelList(pixel_list[i]);
1846 pixel_list=(PixelList **) RelinquishMagickMemory(pixel_list);
1850 static PixelList *AcquirePixelList(const size_t width,const size_t height)
1855 pixel_list=(PixelList *) AcquireMagickMemory(sizeof(*pixel_list));
1856 if (pixel_list == (PixelList *) NULL)
1858 (void) ResetMagickMemory((void *) pixel_list,0,sizeof(*pixel_list));
1859 pixel_list->length=width*height;
1860 pixel_list->skip_list.nodes=(SkipNode *) AcquireQuantumMemory(65537UL,
1861 sizeof(*pixel_list->skip_list.nodes));
1862 if (pixel_list->skip_list.nodes == (SkipNode *) NULL)
1863 return(DestroyPixelList(pixel_list));
1864 (void) ResetMagickMemory(pixel_list->skip_list.nodes,0,65537UL*
1865 sizeof(*pixel_list->skip_list.nodes));
1866 pixel_list->signature=MagickSignature;
1870 static PixelList **AcquirePixelListThreadSet(const size_t width,
1871 const size_t height)
1882 number_threads=GetMagickResourceLimit(ThreadResource);
1883 pixel_list=(PixelList **) AcquireQuantumMemory(number_threads,
1884 sizeof(*pixel_list));
1885 if (pixel_list == (PixelList **) NULL)
1886 return((PixelList **) NULL);
1887 (void) ResetMagickMemory(pixel_list,0,number_threads*sizeof(*pixel_list));
1888 for (i=0; i < (ssize_t) number_threads; i++)
1890 pixel_list[i]=AcquirePixelList(width,height);
1891 if (pixel_list[i] == (PixelList *) NULL)
1892 return(DestroyPixelListThreadSet(pixel_list));
1897 static void AddNodePixelList(PixelList *pixel_list,const size_t color)
1910 Initialize the node.
1912 p=(&pixel_list->skip_list);
1913 p->nodes[color].signature=pixel_list->signature;
1914 p->nodes[color].count=1;
1916 Determine where it belongs in the list.
1919 for (level=p->level; level >= 0; level--)
1921 while (p->nodes[search].next[level] < color)
1922 search=p->nodes[search].next[level];
1923 update[level]=search;
1926 Generate a pseudo-random level for this node.
1928 for (level=0; ; level++)
1930 pixel_list->seed=(pixel_list->seed*42893621L)+1L;
1931 if ((pixel_list->seed & 0x300) != 0x300)
1936 if (level > (p->level+2))
1939 If we're raising the list's level, link back to the root node.
1941 while (level > p->level)
1944 update[p->level]=65536UL;
1947 Link the node into the skip-list.
1951 p->nodes[color].next[level]=p->nodes[update[level]].next[level];
1952 p->nodes[update[level]].next[level]=color;
1953 } while (level-- > 0);
1956 static inline void GetMaximumPixelList(PixelList *pixel_list,Quantum *pixel)
1969 Find the maximum value for each of the color.
1971 p=(&pixel_list->skip_list);
1974 maximum=p->nodes[color].next[0];
1977 color=p->nodes[color].next[0];
1978 if (color > maximum)
1980 count+=p->nodes[color].count;
1981 } while (count < (ssize_t) pixel_list->length);
1982 *pixel=ScaleShortToQuantum((unsigned short) maximum);
1985 static inline void GetMeanPixelList(PixelList *pixel_list,Quantum *pixel)
2000 Find the mean value for each of the color.
2002 p=(&pixel_list->skip_list);
2008 color=p->nodes[color].next[0];
2009 sum+=(MagickRealType) p->nodes[color].count*color;
2010 count+=p->nodes[color].count;
2011 } while (count < (ssize_t) pixel_list->length);
2012 sum/=pixel_list->length;
2013 *pixel=ScaleShortToQuantum((unsigned short) sum);
2016 static inline void GetMedianPixelList(PixelList *pixel_list,Quantum *pixel)
2028 Find the median value for each of the color.
2030 p=(&pixel_list->skip_list);
2035 color=p->nodes[color].next[0];
2036 count+=p->nodes[color].count;
2037 } while (count <= (ssize_t) (pixel_list->length >> 1));
2038 *pixel=ScaleShortToQuantum((unsigned short) color);
2041 static inline void GetMinimumPixelList(PixelList *pixel_list,Quantum *pixel)
2054 Find the minimum value for each of the color.
2056 p=(&pixel_list->skip_list);
2059 minimum=p->nodes[color].next[0];
2062 color=p->nodes[color].next[0];
2063 if (color < minimum)
2065 count+=p->nodes[color].count;
2066 } while (count < (ssize_t) pixel_list->length);
2067 *pixel=ScaleShortToQuantum((unsigned short) minimum);
2070 static inline void GetModePixelList(PixelList *pixel_list,Quantum *pixel)
2084 Make each pixel the 'predominant color' of the specified neighborhood.
2086 p=(&pixel_list->skip_list);
2089 max_count=p->nodes[mode].count;
2093 color=p->nodes[color].next[0];
2094 if (p->nodes[color].count > max_count)
2097 max_count=p->nodes[mode].count;
2099 count+=p->nodes[color].count;
2100 } while (count < (ssize_t) pixel_list->length);
2101 *pixel=ScaleShortToQuantum((unsigned short) mode);
2104 static inline void GetNonpeakPixelList(PixelList *pixel_list,Quantum *pixel)
2118 Finds the non peak value for each of the colors.
2120 p=(&pixel_list->skip_list);
2122 next=p->nodes[color].next[0];
2128 next=p->nodes[color].next[0];
2129 count+=p->nodes[color].count;
2130 } while (count <= (ssize_t) (pixel_list->length >> 1));
2131 if ((previous == 65536UL) && (next != 65536UL))
2134 if ((previous != 65536UL) && (next == 65536UL))
2136 *pixel=ScaleShortToQuantum((unsigned short) color);
2139 static inline void GetStandardDeviationPixelList(PixelList *pixel_list,
2156 Find the standard-deviation value for each of the color.
2158 p=(&pixel_list->skip_list);
2168 color=p->nodes[color].next[0];
2169 sum+=(MagickRealType) p->nodes[color].count*color;
2170 for (i=0; i < (ssize_t) p->nodes[color].count; i++)
2171 sum_squared+=((MagickRealType) color)*((MagickRealType) color);
2172 count+=p->nodes[color].count;
2173 } while (count < (ssize_t) pixel_list->length);
2174 sum/=pixel_list->length;
2175 sum_squared/=pixel_list->length;
2176 *pixel=ScaleShortToQuantum((unsigned short) sqrt(sum_squared-(sum*sum)));
2179 static inline void InsertPixelList(const Image *image,const Quantum pixel,
2180 PixelList *pixel_list)
2188 index=ScaleQuantumToShort(pixel);
2189 signature=pixel_list->skip_list.nodes[index].signature;
2190 if (signature == pixel_list->signature)
2192 pixel_list->skip_list.nodes[index].count++;
2195 AddNodePixelList(pixel_list,index);
2198 static inline MagickRealType MagickAbsoluteValue(const MagickRealType x)
2205 static inline size_t MagickMax(const size_t x,const size_t y)
2212 static void ResetPixelList(PixelList *pixel_list)
2224 Reset the skip-list.
2226 p=(&pixel_list->skip_list);
2227 root=p->nodes+65536UL;
2229 for (level=0; level < 9; level++)
2230 root->next[level]=65536UL;
2231 pixel_list->seed=pixel_list->signature++;
2234 MagickExport Image *StatisticImage(const Image *image,const StatisticType type,
2235 const size_t width,const size_t height,ExceptionInfo *exception)
2237 #define StatisticImageTag "Statistic/Image"
2253 **restrict pixel_list;
2260 Initialize statistics image attributes.
2262 assert(image != (Image *) NULL);
2263 assert(image->signature == MagickSignature);
2264 if (image->debug != MagickFalse)
2265 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2266 assert(exception != (ExceptionInfo *) NULL);
2267 assert(exception->signature == MagickSignature);
2268 statistic_image=CloneImage(image,image->columns,image->rows,MagickTrue,
2270 if (statistic_image == (Image *) NULL)
2271 return((Image *) NULL);
2272 status=SetImageStorageClass(statistic_image,DirectClass,exception);
2273 if (status == MagickFalse)
2275 statistic_image=DestroyImage(statistic_image);
2276 return((Image *) NULL);
2278 pixel_list=AcquirePixelListThreadSet(MagickMax(width,1),MagickMax(height,1));
2279 if (pixel_list == (PixelList **) NULL)
2281 statistic_image=DestroyImage(statistic_image);
2282 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2285 Make each pixel the min / max / median / mode / etc. of the neighborhood.
2287 center=(ssize_t) GetPixelChannels(image)*(image->columns+MagickMax(width,1))*
2288 (MagickMax(height,1)/2L)+GetPixelChannels(image)*(MagickMax(width,1)/2L);
2291 image_view=AcquireVirtualCacheView(image,exception);
2292 statistic_view=AcquireAuthenticCacheView(statistic_image,exception);
2293 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2294 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2295 dynamic_number_threads(image,image->columns,image->rows,1)
2297 for (y=0; y < (ssize_t) statistic_image->rows; y++)
2300 id = GetOpenMPThreadId();
2302 register const Quantum
2311 if (status == MagickFalse)
2313 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) MagickMax(width,1)/2L),y-
2314 (ssize_t) (MagickMax(height,1)/2L),image->columns+MagickMax(width,1),
2315 MagickMax(height,1),exception);
2316 q=QueueCacheViewAuthenticPixels(statistic_view,0,y,statistic_image->columns, 1,exception);
2317 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2322 for (x=0; x < (ssize_t) statistic_image->columns; x++)
2327 if (GetPixelMask(image,p) != 0)
2329 p+=GetPixelChannels(image);
2330 q+=GetPixelChannels(statistic_image);
2333 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2345 register const Quantum
2354 channel=GetPixelChannelMapChannel(image,i);
2355 traits=GetPixelChannelMapTraits(image,channel);
2356 statistic_traits=GetPixelChannelMapTraits(statistic_image,channel);
2357 if ((traits == UndefinedPixelTrait) ||
2358 (statistic_traits == UndefinedPixelTrait))
2360 if ((statistic_traits & CopyPixelTrait) != 0)
2362 SetPixelChannel(statistic_image,channel,p[center+i],q);
2366 ResetPixelList(pixel_list[id]);
2367 for (v=0; v < (ssize_t) MagickMax(height,1); v++)
2369 for (u=0; u < (ssize_t) MagickMax(width,1); u++)
2371 InsertPixelList(image,pixels[i],pixel_list[id]);
2372 pixels+=GetPixelChannels(image);
2374 pixels+=image->columns*GetPixelChannels(image);
2378 case GradientStatistic:
2384 GetMinimumPixelList(pixel_list[id],&pixel);
2385 minimum=(MagickRealType) pixel;
2386 GetMaximumPixelList(pixel_list[id],&pixel);
2387 maximum=(MagickRealType) pixel;
2388 pixel=ClampToQuantum(MagickAbsoluteValue(maximum-minimum));
2391 case MaximumStatistic:
2393 GetMaximumPixelList(pixel_list[id],&pixel);
2398 GetMeanPixelList(pixel_list[id],&pixel);
2401 case MedianStatistic:
2404 GetMedianPixelList(pixel_list[id],&pixel);
2407 case MinimumStatistic:
2409 GetMinimumPixelList(pixel_list[id],&pixel);
2414 GetModePixelList(pixel_list[id],&pixel);
2417 case NonpeakStatistic:
2419 GetNonpeakPixelList(pixel_list[id],&pixel);
2422 case StandardDeviationStatistic:
2424 GetStandardDeviationPixelList(pixel_list[id],&pixel);
2428 SetPixelChannel(statistic_image,channel,pixel,q);
2430 p+=GetPixelChannels(image);
2431 q+=GetPixelChannels(statistic_image);
2433 if (SyncCacheViewAuthenticPixels(statistic_view,exception) == MagickFalse)
2435 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2440 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2441 #pragma omp critical (MagickCore_StatisticImage)
2443 proceed=SetImageProgress(image,StatisticImageTag,progress++,
2445 if (proceed == MagickFalse)
2449 statistic_view=DestroyCacheView(statistic_view);
2450 image_view=DestroyCacheView(image_view);
2451 pixel_list=DestroyPixelListThreadSet(pixel_list);
2452 return(statistic_image);