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/segment.h"
85 #include "MagickCore/semaphore.h"
86 #include "MagickCore/signature-private.h"
87 #include "MagickCore/statistic.h"
88 #include "MagickCore/string_.h"
89 #include "MagickCore/thread-private.h"
90 #include "MagickCore/timer.h"
91 #include "MagickCore/utility.h"
92 #include "MagickCore/version.h"
95 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99 % E v a l u a t e I m a g e %
103 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105 % EvaluateImage() applies a value to the image with an arithmetic, relational,
106 % or logical operator to an image. Use these operations to lighten or darken
107 % an image, to increase or decrease contrast in an image, or to produce the
108 % "negative" of an image.
110 % The format of the EvaluateImage method is:
112 % MagickBooleanType EvaluateImage(Image *image,
113 % const MagickEvaluateOperator op,const double value,
114 % ExceptionInfo *exception)
115 % MagickBooleanType EvaluateImages(Image *images,
116 % const MagickEvaluateOperator op,const double value,
117 % ExceptionInfo *exception)
119 % A description of each parameter follows:
121 % o image: the image.
123 % o op: A channel op.
125 % o value: A value value.
127 % o exception: return any errors or warnings in this structure.
131 typedef struct _PixelChannels
134 channel[CompositePixelChannel];
137 static PixelChannels **DestroyPixelThreadSet(PixelChannels **pixels)
142 assert(pixels != (PixelChannels **) NULL);
143 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
144 if (pixels[i] != (PixelChannels *) NULL)
145 pixels[i]=(PixelChannels *) RelinquishMagickMemory(pixels[i]);
146 pixels=(PixelChannels **) RelinquishMagickMemory(pixels);
150 static PixelChannels **AcquirePixelThreadSet(const Image *image,
151 const size_t number_images)
163 number_threads=GetOpenMPMaximumThreads();
164 pixels=(PixelChannels **) AcquireQuantumMemory(number_threads,
166 if (pixels == (PixelChannels **) NULL)
167 return((PixelChannels **) NULL);
168 (void) ResetMagickMemory(pixels,0,number_threads*sizeof(*pixels));
169 for (i=0; i < (ssize_t) number_threads; i++)
174 length=image->columns;
175 if (length < number_images)
176 length=number_images;
177 pixels[i]=(PixelChannels *) AcquireQuantumMemory(length,sizeof(**pixels));
178 if (pixels[i] == (PixelChannels *) NULL)
179 return(DestroyPixelThreadSet(pixels));
180 for (j=0; j < (ssize_t) length; j++)
185 for (k=0; k < MaxPixelChannels; k++)
186 pixels[i][j].channel[k]=0.0;
192 static inline double EvaluateMax(const double x,const double y)
199 #if defined(__cplusplus) || defined(c_plusplus)
203 static int IntensityCompare(const void *x,const void *y)
215 color_1=(const PixelChannels *) x;
216 color_2=(const PixelChannels *) y;
218 for (i=0; i < MaxPixelChannels; i++)
219 distance+=color_1->channel[i]-(MagickRealType) color_2->channel[i];
220 return(distance < 0 ? -1 : distance > 0 ? 1 : 0);
223 #if defined(__cplusplus) || defined(c_plusplus)
227 static inline double MagickMin(const double x,const double y)
234 static MagickRealType ApplyEvaluateOperator(RandomInfo *random_info,
235 Quantum pixel,const MagickEvaluateOperator op,const MagickRealType value)
243 case UndefinedEvaluateOperator:
245 case AbsEvaluateOperator:
247 result=(MagickRealType) fabs((double) (pixel+value));
250 case AddEvaluateOperator:
252 result=(MagickRealType) (pixel+value);
255 case AddModulusEvaluateOperator:
258 This returns a 'floored modulus' of the addition which is a positive
259 result. It differs from % or fmod() that returns a 'truncated modulus'
260 result, where floor() is replaced by trunc() and could return a
261 negative result (which is clipped).
264 result-=(QuantumRange+1.0)*floor((double) result/(QuantumRange+1.0));
267 case AndEvaluateOperator:
269 result=(MagickRealType) ((size_t) pixel & (size_t) (value+0.5));
272 case CosineEvaluateOperator:
274 result=(MagickRealType) (QuantumRange*(0.5*cos((double) (2.0*MagickPI*
275 QuantumScale*pixel*value))+0.5));
278 case DivideEvaluateOperator:
280 result=pixel/(value == 0.0 ? 1.0 : value);
283 case ExponentialEvaluateOperator:
285 result=(MagickRealType) (QuantumRange*exp((double) (value*QuantumScale*
289 case GaussianNoiseEvaluateOperator:
291 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
292 GaussianNoise,value);
295 case ImpulseNoiseEvaluateOperator:
297 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
301 case LaplacianNoiseEvaluateOperator:
303 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
304 LaplacianNoise,value);
307 case LeftShiftEvaluateOperator:
309 result=(MagickRealType) ((size_t) pixel << (size_t) (value+0.5));
312 case LogEvaluateOperator:
314 result=(MagickRealType) (QuantumRange*log((double) (QuantumScale*value*
315 pixel+1.0))/log((double) (value+1.0)));
318 case MaxEvaluateOperator:
320 result=(MagickRealType) EvaluateMax((double) pixel,value);
323 case MeanEvaluateOperator:
325 result=(MagickRealType) (pixel+value);
328 case MedianEvaluateOperator:
330 result=(MagickRealType) (pixel+value);
333 case MinEvaluateOperator:
335 result=(MagickRealType) MagickMin((double) pixel,value);
338 case MultiplicativeNoiseEvaluateOperator:
340 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
341 MultiplicativeGaussianNoise,value);
344 case MultiplyEvaluateOperator:
346 result=(MagickRealType) (value*pixel);
349 case OrEvaluateOperator:
351 result=(MagickRealType) ((size_t) pixel | (size_t) (value+0.5));
354 case PoissonNoiseEvaluateOperator:
356 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
360 case PowEvaluateOperator:
362 result=(MagickRealType) (QuantumRange*pow((double) (QuantumScale*pixel),
366 case RightShiftEvaluateOperator:
368 result=(MagickRealType) ((size_t) pixel >> (size_t) (value+0.5));
371 case SetEvaluateOperator:
376 case SineEvaluateOperator:
378 result=(MagickRealType) (QuantumRange*(0.5*sin((double) (2.0*MagickPI*
379 QuantumScale*pixel*value))+0.5));
382 case SubtractEvaluateOperator:
384 result=(MagickRealType) (pixel-value);
387 case SumEvaluateOperator:
389 result=(MagickRealType) (pixel+value);
392 case ThresholdEvaluateOperator:
394 result=(MagickRealType) (((MagickRealType) pixel <= value) ? 0 :
398 case ThresholdBlackEvaluateOperator:
400 result=(MagickRealType) (((MagickRealType) pixel <= value) ? 0 : pixel);
403 case ThresholdWhiteEvaluateOperator:
405 result=(MagickRealType) (((MagickRealType) pixel > value) ? QuantumRange :
409 case UniformNoiseEvaluateOperator:
411 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
415 case XorEvaluateOperator:
417 result=(MagickRealType) ((size_t) pixel ^ (size_t) (value+0.5));
424 MagickExport Image *EvaluateImages(const Image *images,
425 const MagickEvaluateOperator op,ExceptionInfo *exception)
427 #define EvaluateImageTag "Evaluate/Image"
446 **restrict evaluate_pixels;
449 **restrict random_info;
458 Ensure the image are the same size.
460 assert(images != (Image *) NULL);
461 assert(images->signature == MagickSignature);
462 if (images->debug != MagickFalse)
463 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
464 assert(exception != (ExceptionInfo *) NULL);
465 assert(exception->signature == MagickSignature);
466 for (next=images; next != (Image *) NULL; next=GetNextImageInList(next))
467 if ((next->columns != images->columns) || (next->rows != images->rows))
469 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
470 "ImageWidthsOrHeightsDiffer","`%s'",images->filename);
471 return((Image *) NULL);
474 Initialize evaluate next attributes.
476 evaluate_image=CloneImage(images,images->columns,images->rows,MagickTrue,
478 if (evaluate_image == (Image *) NULL)
479 return((Image *) NULL);
480 if (SetImageStorageClass(evaluate_image,DirectClass,exception) == MagickFalse)
482 evaluate_image=DestroyImage(evaluate_image);
483 return((Image *) NULL);
485 number_images=GetImageListLength(images);
486 evaluate_pixels=AcquirePixelThreadSet(images,number_images);
487 if (evaluate_pixels == (PixelChannels **) NULL)
489 evaluate_image=DestroyImage(evaluate_image);
490 (void) ThrowMagickException(exception,GetMagickModule(),
491 ResourceLimitError,"MemoryAllocationFailed","`%s'",images->filename);
492 return((Image *) NULL);
495 Evaluate image pixels.
499 random_info=AcquireRandomInfoThreadSet();
500 concurrent=GetRandomSecretKey(random_info[0]) == ~0UL ? MagickTrue :
502 evaluate_view=AcquireCacheView(evaluate_image);
503 if (op == MedianEvaluateOperator)
505 #if defined(MAGICKCORE_OPENMP_SUPPORT)
506 #pragma omp parallel for schedule(static) shared(progress,status) omp_concurrent(concurrent)
508 for (y=0; y < (ssize_t) evaluate_image->rows; y++)
517 id = GetOpenMPThreadId();
519 register PixelChannels
528 if (status == MagickFalse)
530 q=QueueCacheViewAuthenticPixels(evaluate_view,0,y,
531 evaluate_image->columns,1,exception);
532 if (q == (Quantum *) NULL)
537 evaluate_pixel=evaluate_pixels[id];
538 for (x=0; x < (ssize_t) evaluate_image->columns; x++)
544 for (j=0; j < (ssize_t) number_images; j++)
545 for (k=0; k < MaxPixelChannels; k++)
546 evaluate_pixel[j].channel[k]=0.0;
548 for (j=0; j < (ssize_t) number_images; j++)
550 register const Quantum
556 image_view=AcquireCacheView(next);
557 p=GetCacheViewVirtualPixels(image_view,x,y,1,1,exception);
558 if (p == (const Quantum *) NULL)
560 image_view=DestroyCacheView(image_view);
563 for (i=0; i < (ssize_t) GetPixelChannels(evaluate_image); i++)
572 channel=GetPixelChannelMapChannel(evaluate_image,i);
573 evaluate_traits=GetPixelChannelMapTraits(evaluate_image,channel);
574 traits=GetPixelChannelMapTraits(next,channel);
575 if ((traits == UndefinedPixelTrait) ||
576 (evaluate_traits == UndefinedPixelTrait))
578 if ((evaluate_traits & UpdatePixelTrait) == 0)
580 evaluate_pixel[j].channel[i]=ApplyEvaluateOperator(
581 random_info[id],GetPixelChannel(evaluate_image,channel,p),op,
582 evaluate_pixel[j].channel[i]);
584 image_view=DestroyCacheView(image_view);
585 next=GetNextImageInList(next);
587 qsort((void *) evaluate_pixel,number_images,sizeof(*evaluate_pixel),
589 for (k=0; k < (ssize_t) GetPixelChannels(evaluate_image); k++)
590 q[k]=ClampToQuantum(evaluate_pixel[j/2].channel[k]);
591 q+=GetPixelChannels(evaluate_image);
593 if (SyncCacheViewAuthenticPixels(evaluate_view,exception) == MagickFalse)
595 if (images->progress_monitor != (MagickProgressMonitor) NULL)
600 #if defined(MAGICKCORE_OPENMP_SUPPORT)
601 #pragma omp critical (MagickCore_EvaluateImages)
603 proceed=SetImageProgress(images,EvaluateImageTag,progress++,
604 evaluate_image->rows);
605 if (proceed == MagickFalse)
612 #if defined(MAGICKCORE_OPENMP_SUPPORT)
613 #pragma omp parallel for schedule(static) shared(progress,status) omp_concurrent(concurrent)
615 for (y=0; y < (ssize_t) evaluate_image->rows; y++)
624 id = GetOpenMPThreadId();
630 register PixelChannels
639 if (status == MagickFalse)
641 q=QueueCacheViewAuthenticPixels(evaluate_view,0,y,
642 evaluate_image->columns,1,exception);
643 if (q == (Quantum *) NULL)
648 evaluate_pixel=evaluate_pixels[id];
649 for (j=0; j < (ssize_t) evaluate_image->columns; j++)
650 for (i=0; i < MaxPixelChannels; i++)
651 evaluate_pixel[j].channel[i]=0.0;
653 for (j=0; j < (ssize_t) number_images; j++)
655 register const Quantum
658 image_view=AcquireCacheView(next);
659 p=GetCacheViewVirtualPixels(image_view,0,y,next->columns,1,exception);
660 if (p == (const Quantum *) NULL)
662 image_view=DestroyCacheView(image_view);
665 for (x=0; x < (ssize_t) next->columns; x++)
670 if (GetPixelMask(next,p) != 0)
672 p+=GetPixelChannels(next);
675 for (i=0; i < (ssize_t) GetPixelChannels(next); i++)
684 channel=GetPixelChannelMapChannel(evaluate_image,i);
685 traits=GetPixelChannelMapTraits(next,channel);
686 evaluate_traits=GetPixelChannelMapTraits(evaluate_image,channel);
687 if ((traits == UndefinedPixelTrait) ||
688 (evaluate_traits == UndefinedPixelTrait))
690 if ((traits & UpdatePixelTrait) == 0)
692 evaluate_pixel[x].channel[i]=ApplyEvaluateOperator(
693 random_info[id],GetPixelChannel(evaluate_image,channel,p),j ==
694 0 ? AddEvaluateOperator : op,evaluate_pixel[x].channel[i]);
696 p+=GetPixelChannels(next);
698 image_view=DestroyCacheView(image_view);
699 next=GetNextImageInList(next);
701 for (x=0; x < (ssize_t) evaluate_image->columns; x++)
708 case MeanEvaluateOperator:
710 for (i=0; i < (ssize_t) GetPixelChannels(evaluate_image); i++)
711 evaluate_pixel[x].channel[i]/=(MagickRealType) number_images;
714 case MultiplyEvaluateOperator:
716 for (i=0; i < (ssize_t) GetPixelChannels(evaluate_image); i++)
721 for (j=0; j < (ssize_t) (number_images-1); j++)
722 evaluate_pixel[x].channel[i]*=QuantumScale;
730 for (x=0; x < (ssize_t) evaluate_image->columns; x++)
735 if (GetPixelMask(evaluate_image,q) != 0)
737 q+=GetPixelChannels(evaluate_image);
740 for (i=0; i < (ssize_t) GetPixelChannels(evaluate_image); i++)
748 channel=GetPixelChannelMapChannel(evaluate_image,i);
749 traits=GetPixelChannelMapTraits(evaluate_image,channel);
750 if (traits == UndefinedPixelTrait)
752 if ((traits & UpdatePixelTrait) == 0)
754 q[i]=ClampToQuantum(evaluate_pixel[x].channel[i]);
756 q+=GetPixelChannels(evaluate_image);
758 if (SyncCacheViewAuthenticPixels(evaluate_view,exception) == MagickFalse)
760 if (images->progress_monitor != (MagickProgressMonitor) NULL)
765 #if defined(MAGICKCORE_OPENMP_SUPPORT)
766 #pragma omp critical (MagickCore_EvaluateImages)
768 proceed=SetImageProgress(images,EvaluateImageTag,progress++,
769 evaluate_image->rows);
770 if (proceed == MagickFalse)
775 evaluate_view=DestroyCacheView(evaluate_view);
776 evaluate_pixels=DestroyPixelThreadSet(evaluate_pixels);
777 random_info=DestroyRandomInfoThreadSet(random_info);
778 if (status == MagickFalse)
779 evaluate_image=DestroyImage(evaluate_image);
780 return(evaluate_image);
783 MagickExport MagickBooleanType EvaluateImage(Image *image,
784 const MagickEvaluateOperator op,const double value,ExceptionInfo *exception)
797 **restrict random_info;
802 assert(image != (Image *) NULL);
803 assert(image->signature == MagickSignature);
804 if (image->debug != MagickFalse)
805 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
806 assert(exception != (ExceptionInfo *) NULL);
807 assert(exception->signature == MagickSignature);
808 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
812 random_info=AcquireRandomInfoThreadSet();
813 concurrent=GetRandomSecretKey(random_info[0]) == ~0UL ? MagickTrue :
815 image_view=AcquireCacheView(image);
816 #if defined(MAGICKCORE_OPENMP_SUPPORT)
817 #pragma omp parallel for schedule(static,4) shared(progress,status) omp_concurrent(concurrent)
819 for (y=0; y < (ssize_t) image->rows; y++)
822 id = GetOpenMPThreadId();
830 if (status == MagickFalse)
832 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
833 if (q == (Quantum *) NULL)
838 for (x=0; x < (ssize_t) image->columns; x++)
843 if (GetPixelMask(image,q) != 0)
845 q+=GetPixelChannels(image);
848 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
856 channel=GetPixelChannelMapChannel(image,i);
857 traits=GetPixelChannelMapTraits(image,channel);
858 if (traits == UndefinedPixelTrait)
860 q[i]=ClampToQuantum(ApplyEvaluateOperator(random_info[id],q[i],op,
863 q+=GetPixelChannels(image);
865 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
867 if (image->progress_monitor != (MagickProgressMonitor) NULL)
872 #if defined(MAGICKCORE_OPENMP_SUPPORT)
873 #pragma omp critical (MagickCore_EvaluateImage)
875 proceed=SetImageProgress(image,EvaluateImageTag,progress++,image->rows);
876 if (proceed == MagickFalse)
880 image_view=DestroyCacheView(image_view);
881 random_info=DestroyRandomInfoThreadSet(random_info);
886 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
890 % F u n c t i o n I m a g e %
894 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
896 % FunctionImage() applies a value to the image with an arithmetic, relational,
897 % or logical operator to an image. Use these operations to lighten or darken
898 % an image, to increase or decrease contrast in an image, or to produce the
899 % "negative" of an image.
901 % The format of the FunctionImage method is:
903 % MagickBooleanType FunctionImage(Image *image,
904 % const MagickFunction function,const ssize_t number_parameters,
905 % const double *parameters,ExceptionInfo *exception)
907 % A description of each parameter follows:
909 % o image: the image.
911 % o function: A channel function.
913 % o parameters: one or more parameters.
915 % o exception: return any errors or warnings in this structure.
919 static Quantum ApplyFunction(Quantum pixel,const MagickFunction function,
920 const size_t number_parameters,const double *parameters,
921 ExceptionInfo *exception)
933 case PolynomialFunction:
936 Polynomial: polynomial constants, highest to lowest order (e.g. c0*x^3+
940 for (i=0; i < (ssize_t) number_parameters; i++)
941 result=result*QuantumScale*pixel+parameters[i];
942 result*=QuantumRange;
945 case SinusoidFunction:
954 Sinusoid: frequency, phase, amplitude, bias.
956 frequency=(number_parameters >= 1) ? parameters[0] : 1.0;
957 phase=(number_parameters >= 2) ? parameters[1] : 0.0;
958 amplitude=(number_parameters >= 3) ? parameters[2] : 0.5;
959 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
960 result=(MagickRealType) (QuantumRange*(amplitude*sin((double) (2.0*
961 MagickPI*(frequency*QuantumScale*pixel+phase/360.0)))+bias));
973 Arcsin (peged at range limits for invalid results): width, center,
976 width=(number_parameters >= 1) ? parameters[0] : 1.0;
977 center=(number_parameters >= 2) ? parameters[1] : 0.5;
978 range=(number_parameters >= 3) ? parameters[2] : 1.0;
979 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
980 result=2.0/width*(QuantumScale*pixel-center);
981 if ( result <= -1.0 )
982 result=bias-range/2.0;
985 result=bias+range/2.0;
987 result=(MagickRealType) (range/MagickPI*asin((double) result)+bias);
988 result*=QuantumRange;
1000 Arctan: slope, center, range, and bias.
1002 slope=(number_parameters >= 1) ? parameters[0] : 1.0;
1003 center=(number_parameters >= 2) ? parameters[1] : 0.5;
1004 range=(number_parameters >= 3) ? parameters[2] : 1.0;
1005 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
1006 result=(MagickRealType) (MagickPI*slope*(QuantumScale*pixel-center));
1007 result=(MagickRealType) (QuantumRange*(range/MagickPI*atan((double)
1011 case UndefinedFunction:
1014 return(ClampToQuantum(result));
1017 MagickExport MagickBooleanType FunctionImage(Image *image,
1018 const MagickFunction function,const size_t number_parameters,
1019 const double *parameters,ExceptionInfo *exception)
1021 #define FunctionImageTag "Function/Image "
1035 assert(image != (Image *) NULL);
1036 assert(image->signature == MagickSignature);
1037 if (image->debug != MagickFalse)
1038 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1039 assert(exception != (ExceptionInfo *) NULL);
1040 assert(exception->signature == MagickSignature);
1041 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1042 return(MagickFalse);
1045 image_view=AcquireCacheView(image);
1046 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1047 #pragma omp parallel for schedule(static,4) shared(progress,status)
1049 for (y=0; y < (ssize_t) image->rows; y++)
1057 if (status == MagickFalse)
1059 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1060 if (q == (Quantum *) NULL)
1065 for (x=0; x < (ssize_t) image->columns; x++)
1070 if (GetPixelMask(image,q) != 0)
1072 q+=GetPixelChannels(image);
1075 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1083 channel=GetPixelChannelMapChannel(image,i);
1084 traits=GetPixelChannelMapTraits(image,channel);
1085 if (traits == UndefinedPixelTrait)
1087 if ((traits & UpdatePixelTrait) == 0)
1089 q[i]=ApplyFunction(q[i],function,number_parameters,parameters,
1092 q+=GetPixelChannels(image);
1094 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1096 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1101 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1102 #pragma omp critical (MagickCore_FunctionImage)
1104 proceed=SetImageProgress(image,FunctionImageTag,progress++,image->rows);
1105 if (proceed == MagickFalse)
1109 image_view=DestroyCacheView(image_view);
1114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1118 % G e t I m a g e E x t r e m a %
1122 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1124 % GetImageExtrema() returns the extrema of one or more image channels.
1126 % The format of the GetImageExtrema method is:
1128 % MagickBooleanType GetImageExtrema(const Image *image,size_t *minima,
1129 % size_t *maxima,ExceptionInfo *exception)
1131 % A description of each parameter follows:
1133 % o image: the image.
1135 % o minima: the minimum value in the channel.
1137 % o maxima: the maximum value in the channel.
1139 % o exception: return any errors or warnings in this structure.
1142 MagickExport MagickBooleanType GetImageExtrema(const Image *image,
1143 size_t *minima,size_t *maxima,ExceptionInfo *exception)
1152 assert(image != (Image *) NULL);
1153 assert(image->signature == MagickSignature);
1154 if (image->debug != MagickFalse)
1155 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1156 status=GetImageRange(image,&min,&max,exception);
1157 *minima=(size_t) ceil(min-0.5);
1158 *maxima=(size_t) floor(max+0.5);
1163 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1167 % G e t I m a g e M e a n %
1171 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1173 % GetImageMean() returns the mean and standard deviation of one or more
1176 % The format of the GetImageMean method is:
1178 % MagickBooleanType GetImageMean(const Image *image,double *mean,
1179 % double *standard_deviation,ExceptionInfo *exception)
1181 % A description of each parameter follows:
1183 % o image: the image.
1185 % o mean: the average value in the channel.
1187 % o standard_deviation: the standard deviation of the channel.
1189 % o exception: return any errors or warnings in this structure.
1192 MagickExport MagickBooleanType GetImageMean(const Image *image,double *mean,
1193 double *standard_deviation,ExceptionInfo *exception)
1196 *channel_statistics;
1204 assert(image != (Image *) NULL);
1205 assert(image->signature == MagickSignature);
1206 if (image->debug != MagickFalse)
1207 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1208 channel_statistics=GetImageStatistics(image,exception);
1209 if (channel_statistics == (ChannelStatistics *) NULL)
1210 return(MagickFalse);
1212 channel_statistics[CompositePixelChannel].mean=0.0;
1213 channel_statistics[CompositePixelChannel].standard_deviation=0.0;
1214 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1222 channel=GetPixelChannelMapChannel(image,i);
1223 traits=GetPixelChannelMapTraits(image,channel);
1224 if (traits == UndefinedPixelTrait)
1226 if ((traits & UpdatePixelTrait) == 0)
1228 channel_statistics[CompositePixelChannel].mean+=channel_statistics[i].mean;
1229 channel_statistics[CompositePixelChannel].standard_deviation+=
1230 channel_statistics[i].variance-channel_statistics[i].mean*
1231 channel_statistics[i].mean;
1234 channel_statistics[CompositePixelChannel].mean/=area;
1235 channel_statistics[CompositePixelChannel].standard_deviation=
1236 sqrt(channel_statistics[CompositePixelChannel].standard_deviation/area);
1237 *mean=channel_statistics[CompositePixelChannel].mean;
1238 *standard_deviation=
1239 channel_statistics[CompositePixelChannel].standard_deviation;
1240 channel_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1241 channel_statistics);
1246 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1250 % G e t I m a g e K u r t o s i s %
1254 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1256 % GetImageKurtosis() returns the kurtosis and skewness of one or more
1259 % The format of the GetImageKurtosis method is:
1261 % MagickBooleanType GetImageKurtosis(const Image *image,double *kurtosis,
1262 % double *skewness,ExceptionInfo *exception)
1264 % A description of each parameter follows:
1266 % o image: the image.
1268 % o kurtosis: the kurtosis of the channel.
1270 % o skewness: the skewness of the channel.
1272 % o exception: return any errors or warnings in this structure.
1275 MagickExport MagickBooleanType GetImageKurtosis(const Image *image,
1276 double *kurtosis,double *skewness,ExceptionInfo *exception)
1295 assert(image != (Image *) NULL);
1296 assert(image->signature == MagickSignature);
1297 if (image->debug != MagickFalse)
1298 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1304 standard_deviation=0.0;
1307 sum_fourth_power=0.0;
1308 image_view=AcquireCacheView(image);
1309 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1310 #pragma omp parallel for schedule(static) shared(status)
1312 for (y=0; y < (ssize_t) image->rows; y++)
1314 register const Quantum
1320 if (status == MagickFalse)
1322 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1323 if (p == (const Quantum *) NULL)
1328 for (x=0; x < (ssize_t) image->columns; x++)
1333 if (GetPixelMask(image,p) != 0)
1335 p+=GetPixelChannels(image);
1338 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1346 channel=GetPixelChannelMapChannel(image,i);
1347 traits=GetPixelChannelMapTraits(image,channel);
1348 if (traits == UndefinedPixelTrait)
1350 if ((traits & UpdatePixelTrait) == 0)
1352 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1353 #pragma omp critical (MagickCore_GetImageKurtosis)
1357 sum_squares+=(double) p[i]*p[i];
1358 sum_cubes+=(double) p[i]*p[i]*p[i];
1359 sum_fourth_power+=(double) p[i]*p[i]*p[i]*p[i];
1363 p+=GetPixelChannels(image);
1366 image_view=DestroyCacheView(image_view);
1372 sum_fourth_power/=area;
1374 standard_deviation=sqrt(sum_squares-(mean*mean));
1375 if (standard_deviation != 0.0)
1377 *kurtosis=sum_fourth_power-4.0*mean*sum_cubes+6.0*mean*mean*sum_squares-
1378 3.0*mean*mean*mean*mean;
1379 *kurtosis/=standard_deviation*standard_deviation*standard_deviation*
1382 *skewness=sum_cubes-3.0*mean*sum_squares+2.0*mean*mean*mean;
1383 *skewness/=standard_deviation*standard_deviation*standard_deviation;
1389 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1393 % G e t I m a g e R a n g e %
1397 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1399 % GetImageRange() returns the range of one or more image channels.
1401 % The format of the GetImageRange method is:
1403 % MagickBooleanType GetImageRange(const Image *image,double *minima,
1404 % double *maxima,ExceptionInfo *exception)
1406 % A description of each parameter follows:
1408 % o image: the image.
1410 % o minima: the minimum value in the channel.
1412 % o maxima: the maximum value in the channel.
1414 % o exception: return any errors or warnings in this structure.
1417 MagickExport MagickBooleanType GetImageRange(const Image *image,double *minima,
1418 double *maxima,ExceptionInfo *exception)
1429 assert(image != (Image *) NULL);
1430 assert(image->signature == MagickSignature);
1431 if (image->debug != MagickFalse)
1432 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1434 *maxima=(-MagickHuge);
1436 image_view=AcquireCacheView(image);
1437 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1438 #pragma omp parallel for schedule(static) shared(status)
1440 for (y=0; y < (ssize_t) image->rows; y++)
1442 register const Quantum
1448 if (status == MagickFalse)
1450 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1451 if (p == (const Quantum *) NULL)
1456 for (x=0; x < (ssize_t) image->columns; x++)
1461 if (GetPixelMask(image,p) != 0)
1463 p+=GetPixelChannels(image);
1466 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1474 channel=GetPixelChannelMapChannel(image,i);
1475 traits=GetPixelChannelMapTraits(image,channel);
1476 if (traits == UndefinedPixelTrait)
1478 if ((traits & UpdatePixelTrait) == 0)
1480 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1481 #pragma omp critical (MagickCore_GetImageRange)
1484 if ((double) p[i] < *minima)
1485 *minima=(double) p[i];
1486 if ((double) p[i] > *maxima)
1487 *maxima=(double) p[i];
1490 p+=GetPixelChannels(image);
1493 image_view=DestroyCacheView(image_view);
1498 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1502 % G e t I m a g e S t a t i s t i c s %
1506 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1508 % GetImageStatistics() returns statistics for each channel in the
1509 % image. The statistics include the channel depth, its minima, maxima, mean,
1510 % standard deviation, kurtosis and skewness. You can access the red channel
1511 % mean, for example, like this:
1513 % channel_statistics=GetImageStatistics(image,exception);
1514 % red_mean=channel_statistics[RedPixelChannel].mean;
1516 % Use MagickRelinquishMemory() to free the statistics buffer.
1518 % The format of the GetImageStatistics method is:
1520 % ChannelStatistics *GetImageStatistics(const Image *image,
1521 % ExceptionInfo *exception)
1523 % A description of each parameter follows:
1525 % o image: the image.
1527 % o exception: return any errors or warnings in this structure.
1531 static size_t GetImageChannels(const Image *image)
1540 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1548 channel=GetPixelChannelMapChannel(image,i);
1549 traits=GetPixelChannelMapTraits(image,channel);
1550 if ((traits & UpdatePixelTrait) != 0)
1556 MagickExport ChannelStatistics *GetImageStatistics(const Image *image,
1557 ExceptionInfo *exception)
1560 *channel_statistics;
1581 assert(image != (Image *) NULL);
1582 assert(image->signature == MagickSignature);
1583 if (image->debug != MagickFalse)
1584 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1585 channel_statistics=(ChannelStatistics *) AcquireQuantumMemory(
1586 MaxPixelChannels+1,sizeof(*channel_statistics));
1587 if (channel_statistics == (ChannelStatistics *) NULL)
1588 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1589 (void) ResetMagickMemory(channel_statistics,0,(MaxPixelChannels+1)*
1590 sizeof(*channel_statistics));
1591 for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
1593 channel_statistics[i].depth=1;
1594 channel_statistics[i].maxima=(-MagickHuge);
1595 channel_statistics[i].minima=MagickHuge;
1597 for (y=0; y < (ssize_t) image->rows; y++)
1599 register const Quantum
1605 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1606 if (p == (const Quantum *) NULL)
1608 for (x=0; x < (ssize_t) image->columns; x++)
1613 if (GetPixelMask(image,p) != 0)
1615 p+=GetPixelChannels(image);
1618 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1626 channel=GetPixelChannelMapChannel(image,i);
1627 traits=GetPixelChannelMapTraits(image,channel);
1628 if (traits == UndefinedPixelTrait)
1630 if (channel_statistics[channel].depth != MAGICKCORE_QUANTUM_DEPTH)
1632 depth=channel_statistics[channel].depth;
1633 range=GetQuantumRange(depth);
1634 status=p[i] != ScaleAnyToQuantum(ScaleQuantumToAny(p[i],range),
1635 range) ? MagickTrue : MagickFalse;
1636 if (status != MagickFalse)
1638 channel_statistics[channel].depth++;
1642 if ((double) p[i] < channel_statistics[channel].minima)
1643 channel_statistics[channel].minima=(double) p[i];
1644 if ((double) p[i] > channel_statistics[channel].maxima)
1645 channel_statistics[channel].maxima=(double) p[i];
1646 channel_statistics[channel].sum+=p[i];
1647 channel_statistics[channel].sum_squared+=(double) p[i]*p[i];
1648 channel_statistics[channel].sum_cubed+=(double) p[i]*p[i]*p[i];
1649 channel_statistics[channel].sum_fourth_power+=(double) p[i]*p[i]*p[i]*
1652 p+=GetPixelChannels(image);
1655 area=(double) image->columns*image->rows;
1656 for (i=0; i < (ssize_t) MaxPixelChannels; i++)
1658 channel_statistics[i].sum/=area;
1659 channel_statistics[i].sum_squared/=area;
1660 channel_statistics[i].sum_cubed/=area;
1661 channel_statistics[i].sum_fourth_power/=area;
1662 channel_statistics[i].mean=channel_statistics[i].sum;
1663 channel_statistics[i].variance=channel_statistics[i].sum_squared;
1664 channel_statistics[i].standard_deviation=sqrt(
1665 channel_statistics[i].variance-(channel_statistics[i].mean*
1666 channel_statistics[i].mean));
1668 for (i=0; i < (ssize_t) MaxPixelChannels; i++)
1670 channel_statistics[CompositePixelChannel].depth=(size_t) EvaluateMax(
1671 (double) channel_statistics[CompositePixelChannel].depth,(double)
1672 channel_statistics[i].depth);
1673 channel_statistics[CompositePixelChannel].minima=MagickMin(
1674 channel_statistics[CompositePixelChannel].minima,
1675 channel_statistics[i].minima);
1676 channel_statistics[CompositePixelChannel].maxima=EvaluateMax(
1677 channel_statistics[CompositePixelChannel].maxima,
1678 channel_statistics[i].maxima);
1679 channel_statistics[CompositePixelChannel].sum+=channel_statistics[i].sum;
1680 channel_statistics[CompositePixelChannel].sum_squared+=
1681 channel_statistics[i].sum_squared;
1682 channel_statistics[CompositePixelChannel].sum_cubed+=
1683 channel_statistics[i].sum_cubed;
1684 channel_statistics[CompositePixelChannel].sum_fourth_power+=
1685 channel_statistics[i].sum_fourth_power;
1686 channel_statistics[CompositePixelChannel].mean+=channel_statistics[i].mean;
1687 channel_statistics[CompositePixelChannel].variance+=
1688 channel_statistics[i].variance-channel_statistics[i].mean*
1689 channel_statistics[i].mean;
1690 channel_statistics[CompositePixelChannel].standard_deviation+=
1691 channel_statistics[i].variance-channel_statistics[i].mean*
1692 channel_statistics[i].mean;
1694 channels=GetImageChannels(image);
1695 channel_statistics[CompositePixelChannel].sum/=channels;
1696 channel_statistics[CompositePixelChannel].sum_squared/=channels;
1697 channel_statistics[CompositePixelChannel].sum_cubed/=channels;
1698 channel_statistics[CompositePixelChannel].sum_fourth_power/=channels;
1699 channel_statistics[CompositePixelChannel].mean/=channels;
1700 channel_statistics[CompositePixelChannel].variance/=channels;
1701 channel_statistics[CompositePixelChannel].standard_deviation=
1702 sqrt(channel_statistics[CompositePixelChannel].standard_deviation/channels);
1703 channel_statistics[CompositePixelChannel].kurtosis/=channels;
1704 channel_statistics[CompositePixelChannel].skewness/=channels;
1705 for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
1707 if (channel_statistics[i].standard_deviation == 0.0)
1709 channel_statistics[i].skewness=(channel_statistics[i].sum_cubed-3.0*
1710 channel_statistics[i].mean*channel_statistics[i].sum_squared+2.0*
1711 channel_statistics[i].mean*channel_statistics[i].mean*
1712 channel_statistics[i].mean)/(channel_statistics[i].standard_deviation*
1713 channel_statistics[i].standard_deviation*
1714 channel_statistics[i].standard_deviation);
1715 channel_statistics[i].kurtosis=(channel_statistics[i].sum_fourth_power-4.0*
1716 channel_statistics[i].mean*channel_statistics[i].sum_cubed+6.0*
1717 channel_statistics[i].mean*channel_statistics[i].mean*
1718 channel_statistics[i].sum_squared-3.0*channel_statistics[i].mean*
1719 channel_statistics[i].mean*1.0*channel_statistics[i].mean*
1720 channel_statistics[i].mean)/(channel_statistics[i].standard_deviation*
1721 channel_statistics[i].standard_deviation*
1722 channel_statistics[i].standard_deviation*
1723 channel_statistics[i].standard_deviation)-3.0;
1725 return(channel_statistics);
1729 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1733 % S t a t i s t i c I m a g e %
1737 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1739 % StatisticImage() makes each pixel the min / max / median / mode / etc. of
1740 % the neighborhood of the specified width and height.
1742 % The format of the StatisticImage method is:
1744 % Image *StatisticImage(const Image *image,const StatisticType type,
1745 % const size_t width,const size_t height,ExceptionInfo *exception)
1747 % A description of each parameter follows:
1749 % o image: the image.
1751 % o type: the statistic type (median, mode, etc.).
1753 % o width: the width of the pixel neighborhood.
1755 % o height: the height of the pixel neighborhood.
1757 % o exception: return any errors or warnings in this structure.
1761 typedef struct _SkipNode
1769 typedef struct _SkipList
1778 typedef struct _PixelList
1791 static PixelList *DestroyPixelList(PixelList *pixel_list)
1793 if (pixel_list == (PixelList *) NULL)
1794 return((PixelList *) NULL);
1795 if (pixel_list->skip_list.nodes != (SkipNode *) NULL)
1796 pixel_list->skip_list.nodes=(SkipNode *) RelinquishMagickMemory(
1797 pixel_list->skip_list.nodes);
1798 pixel_list=(PixelList *) RelinquishMagickMemory(pixel_list);
1802 static PixelList **DestroyPixelListThreadSet(PixelList **pixel_list)
1807 assert(pixel_list != (PixelList **) NULL);
1808 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
1809 if (pixel_list[i] != (PixelList *) NULL)
1810 pixel_list[i]=DestroyPixelList(pixel_list[i]);
1811 pixel_list=(PixelList **) RelinquishMagickMemory(pixel_list);
1815 static PixelList *AcquirePixelList(const size_t width,const size_t height)
1820 pixel_list=(PixelList *) AcquireMagickMemory(sizeof(*pixel_list));
1821 if (pixel_list == (PixelList *) NULL)
1823 (void) ResetMagickMemory((void *) pixel_list,0,sizeof(*pixel_list));
1824 pixel_list->length=width*height;
1825 pixel_list->skip_list.nodes=(SkipNode *) AcquireQuantumMemory(65537UL,
1826 sizeof(*pixel_list->skip_list.nodes));
1827 if (pixel_list->skip_list.nodes == (SkipNode *) NULL)
1828 return(DestroyPixelList(pixel_list));
1829 (void) ResetMagickMemory(pixel_list->skip_list.nodes,0,65537UL*
1830 sizeof(*pixel_list->skip_list.nodes));
1831 pixel_list->signature=MagickSignature;
1835 static PixelList **AcquirePixelListThreadSet(const size_t width,
1836 const size_t height)
1847 number_threads=GetOpenMPMaximumThreads();
1848 pixel_list=(PixelList **) AcquireQuantumMemory(number_threads,
1849 sizeof(*pixel_list));
1850 if (pixel_list == (PixelList **) NULL)
1851 return((PixelList **) NULL);
1852 (void) ResetMagickMemory(pixel_list,0,number_threads*sizeof(*pixel_list));
1853 for (i=0; i < (ssize_t) number_threads; i++)
1855 pixel_list[i]=AcquirePixelList(width,height);
1856 if (pixel_list[i] == (PixelList *) NULL)
1857 return(DestroyPixelListThreadSet(pixel_list));
1862 static void AddNodePixelList(PixelList *pixel_list,const size_t color)
1875 Initialize the node.
1877 p=(&pixel_list->skip_list);
1878 p->nodes[color].signature=pixel_list->signature;
1879 p->nodes[color].count=1;
1881 Determine where it belongs in the list.
1884 for (level=p->level; level >= 0; level--)
1886 while (p->nodes[search].next[level] < color)
1887 search=p->nodes[search].next[level];
1888 update[level]=search;
1891 Generate a pseudo-random level for this node.
1893 for (level=0; ; level++)
1895 pixel_list->seed=(pixel_list->seed*42893621L)+1L;
1896 if ((pixel_list->seed & 0x300) != 0x300)
1901 if (level > (p->level+2))
1904 If we're raising the list's level, link back to the root node.
1906 while (level > p->level)
1909 update[p->level]=65536UL;
1912 Link the node into the skip-list.
1916 p->nodes[color].next[level]=p->nodes[update[level]].next[level];
1917 p->nodes[update[level]].next[level]=color;
1918 } while (level-- > 0);
1921 static inline void GetMaximumPixelList(PixelList *pixel_list,Quantum *pixel)
1934 Find the maximum value for each of the color.
1936 p=(&pixel_list->skip_list);
1939 maximum=p->nodes[color].next[0];
1942 color=p->nodes[color].next[0];
1943 if (color > maximum)
1945 count+=p->nodes[color].count;
1946 } while (count < (ssize_t) pixel_list->length);
1947 *pixel=ScaleShortToQuantum((unsigned short) maximum);
1950 static inline void GetMeanPixelList(PixelList *pixel_list,Quantum *pixel)
1965 Find the mean value for each of the color.
1967 p=(&pixel_list->skip_list);
1973 color=p->nodes[color].next[0];
1974 sum+=(MagickRealType) p->nodes[color].count*color;
1975 count+=p->nodes[color].count;
1976 } while (count < (ssize_t) pixel_list->length);
1977 sum/=pixel_list->length;
1978 *pixel=ScaleShortToQuantum((unsigned short) sum);
1981 static inline void GetMedianPixelList(PixelList *pixel_list,Quantum *pixel)
1993 Find the median value for each of the color.
1995 p=(&pixel_list->skip_list);
2000 color=p->nodes[color].next[0];
2001 count+=p->nodes[color].count;
2002 } while (count <= (ssize_t) (pixel_list->length >> 1));
2003 *pixel=ScaleShortToQuantum((unsigned short) color);
2006 static inline void GetMinimumPixelList(PixelList *pixel_list,Quantum *pixel)
2019 Find the minimum value for each of the color.
2021 p=(&pixel_list->skip_list);
2024 minimum=p->nodes[color].next[0];
2027 color=p->nodes[color].next[0];
2028 if (color < minimum)
2030 count+=p->nodes[color].count;
2031 } while (count < (ssize_t) pixel_list->length);
2032 *pixel=ScaleShortToQuantum((unsigned short) minimum);
2035 static inline void GetModePixelList(PixelList *pixel_list,Quantum *pixel)
2049 Make each pixel the 'predominant color' of the specified neighborhood.
2051 p=(&pixel_list->skip_list);
2054 max_count=p->nodes[mode].count;
2058 color=p->nodes[color].next[0];
2059 if (p->nodes[color].count > max_count)
2062 max_count=p->nodes[mode].count;
2064 count+=p->nodes[color].count;
2065 } while (count < (ssize_t) pixel_list->length);
2066 *pixel=ScaleShortToQuantum((unsigned short) mode);
2069 static inline void GetNonpeakPixelList(PixelList *pixel_list,Quantum *pixel)
2083 Finds the non peak value for each of the colors.
2085 p=(&pixel_list->skip_list);
2087 next=p->nodes[color].next[0];
2093 next=p->nodes[color].next[0];
2094 count+=p->nodes[color].count;
2095 } while (count <= (ssize_t) (pixel_list->length >> 1));
2096 if ((previous == 65536UL) && (next != 65536UL))
2099 if ((previous != 65536UL) && (next == 65536UL))
2101 *pixel=ScaleShortToQuantum((unsigned short) color);
2104 static inline void GetStandardDeviationPixelList(PixelList *pixel_list,
2121 Find the standard-deviation value for each of the color.
2123 p=(&pixel_list->skip_list);
2133 color=p->nodes[color].next[0];
2134 sum+=(MagickRealType) p->nodes[color].count*color;
2135 for (i=0; i < (ssize_t) p->nodes[color].count; i++)
2136 sum_squared+=((MagickRealType) color)*((MagickRealType) color);
2137 count+=p->nodes[color].count;
2138 } while (count < (ssize_t) pixel_list->length);
2139 sum/=pixel_list->length;
2140 sum_squared/=pixel_list->length;
2141 *pixel=ScaleShortToQuantum((unsigned short) sqrt(sum_squared-(sum*sum)));
2144 static inline void InsertPixelList(const Image *image,const Quantum pixel,
2145 PixelList *pixel_list)
2153 index=ScaleQuantumToShort(pixel);
2154 signature=pixel_list->skip_list.nodes[index].signature;
2155 if (signature == pixel_list->signature)
2157 pixel_list->skip_list.nodes[index].count++;
2160 AddNodePixelList(pixel_list,index);
2163 static inline MagickRealType MagickAbsoluteValue(const MagickRealType x)
2170 static inline size_t MagickMax(const size_t x,const size_t y)
2177 static void ResetPixelList(PixelList *pixel_list)
2189 Reset the skip-list.
2191 p=(&pixel_list->skip_list);
2192 root=p->nodes+65536UL;
2194 for (level=0; level < 9; level++)
2195 root->next[level]=65536UL;
2196 pixel_list->seed=pixel_list->signature++;
2199 MagickExport Image *StatisticImage(const Image *image,const StatisticType type,
2200 const size_t width,const size_t height,ExceptionInfo *exception)
2202 #define StatisticImageTag "Statistic/Image"
2218 **restrict pixel_list;
2225 Initialize statistics image attributes.
2227 assert(image != (Image *) NULL);
2228 assert(image->signature == MagickSignature);
2229 if (image->debug != MagickFalse)
2230 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2231 assert(exception != (ExceptionInfo *) NULL);
2232 assert(exception->signature == MagickSignature);
2233 statistic_image=CloneImage(image,image->columns,image->rows,MagickTrue,
2235 if (statistic_image == (Image *) NULL)
2236 return((Image *) NULL);
2237 status=SetImageStorageClass(statistic_image,DirectClass,exception);
2238 if (status == MagickFalse)
2240 statistic_image=DestroyImage(statistic_image);
2241 return((Image *) NULL);
2243 pixel_list=AcquirePixelListThreadSet(MagickMax(width,1),MagickMax(height,1));
2244 if (pixel_list == (PixelList **) NULL)
2246 statistic_image=DestroyImage(statistic_image);
2247 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2250 Make each pixel the min / max / median / mode / etc. of the neighborhood.
2252 center=(ssize_t) GetPixelChannels(image)*(image->columns+MagickMax(width,1))*
2253 (MagickMax(height,1)/2L)+GetPixelChannels(image)*(MagickMax(width,1)/2L);
2256 image_view=AcquireCacheView(image);
2257 statistic_view=AcquireCacheView(statistic_image);
2258 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2259 #pragma omp parallel for schedule(static,4) shared(progress,status)
2261 for (y=0; y < (ssize_t) statistic_image->rows; y++)
2264 id = GetOpenMPThreadId();
2266 register const Quantum
2275 if (status == MagickFalse)
2277 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) MagickMax(width,1)/2L),y-
2278 (ssize_t) (MagickMax(height,1)/2L),image->columns+MagickMax(width,1),
2279 MagickMax(height,1),exception);
2280 q=QueueCacheViewAuthenticPixels(statistic_view,0,y,statistic_image->columns, 1,exception);
2281 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2286 for (x=0; x < (ssize_t) statistic_image->columns; x++)
2291 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2303 register const Quantum
2312 channel=GetPixelChannelMapChannel(image,i);
2313 traits=GetPixelChannelMapTraits(image,channel);
2314 statistic_traits=GetPixelChannelMapTraits(statistic_image,channel);
2315 if ((traits == UndefinedPixelTrait) ||
2316 (statistic_traits == UndefinedPixelTrait))
2318 if (((statistic_traits & CopyPixelTrait) != 0) ||
2319 (GetPixelMask(image,p) != 0))
2321 SetPixelChannel(statistic_image,channel,p[center+i],q);
2325 ResetPixelList(pixel_list[id]);
2326 for (v=0; v < (ssize_t) MagickMax(height,1); v++)
2328 for (u=0; u < (ssize_t) MagickMax(width,1); u++)
2330 InsertPixelList(image,pixels[i],pixel_list[id]);
2331 pixels+=GetPixelChannels(image);
2333 pixels+=image->columns*GetPixelChannels(image);
2337 case GradientStatistic:
2343 GetMinimumPixelList(pixel_list[id],&pixel);
2344 minimum=(MagickRealType) pixel;
2345 GetMaximumPixelList(pixel_list[id],&pixel);
2346 maximum=(MagickRealType) pixel;
2347 pixel=ClampToQuantum(MagickAbsoluteValue(maximum-minimum));
2350 case MaximumStatistic:
2352 GetMaximumPixelList(pixel_list[id],&pixel);
2357 GetMeanPixelList(pixel_list[id],&pixel);
2360 case MedianStatistic:
2363 GetMedianPixelList(pixel_list[id],&pixel);
2366 case MinimumStatistic:
2368 GetMinimumPixelList(pixel_list[id],&pixel);
2373 GetModePixelList(pixel_list[id],&pixel);
2376 case NonpeakStatistic:
2378 GetNonpeakPixelList(pixel_list[id],&pixel);
2381 case StandardDeviationStatistic:
2383 GetStandardDeviationPixelList(pixel_list[id],&pixel);
2387 SetPixelChannel(statistic_image,channel,pixel,q);
2389 p+=GetPixelChannels(image);
2390 q+=GetPixelChannels(statistic_image);
2392 if (SyncCacheViewAuthenticPixels(statistic_view,exception) == MagickFalse)
2394 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2399 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2400 #pragma omp critical (MagickCore_StatisticImage)
2402 proceed=SetImageProgress(image,StatisticImageTag,progress++,
2404 if (proceed == MagickFalse)
2408 statistic_view=DestroyCacheView(statistic_view);
2409 image_view=DestroyCacheView(image_view);
2410 pixel_list=DestroyPixelListThreadSet(pixel_list);
2411 return(statistic_image);