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"
445 **restrict evaluate_pixels;
448 **restrict random_info;
457 Ensure the image are the same size.
459 assert(images != (Image *) NULL);
460 assert(images->signature == MagickSignature);
461 if (images->debug != MagickFalse)
462 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
463 assert(exception != (ExceptionInfo *) NULL);
464 assert(exception->signature == MagickSignature);
465 for (next=images; next != (Image *) NULL; next=GetNextImageInList(next))
466 if ((next->columns != images->columns) || (next->rows != images->rows))
468 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
469 "ImageWidthsOrHeightsDiffer","`%s'",images->filename);
470 return((Image *) NULL);
473 Initialize evaluate next attributes.
475 evaluate_image=CloneImage(images,images->columns,images->rows,MagickTrue,
477 if (evaluate_image == (Image *) NULL)
478 return((Image *) NULL);
479 if (SetImageStorageClass(evaluate_image,DirectClass,exception) == MagickFalse)
481 evaluate_image=DestroyImage(evaluate_image);
482 return((Image *) NULL);
484 number_images=GetImageListLength(images);
485 evaluate_pixels=AcquirePixelThreadSet(images,number_images);
486 if (evaluate_pixels == (PixelChannels **) NULL)
488 evaluate_image=DestroyImage(evaluate_image);
489 (void) ThrowMagickException(exception,GetMagickModule(),
490 ResourceLimitError,"MemoryAllocationFailed","`%s'",images->filename);
491 return((Image *) NULL);
494 Evaluate image pixels.
498 random_info=AcquireRandomInfoThreadSet();
499 evaluate_view=AcquireCacheView(evaluate_image);
500 if (op == MedianEvaluateOperator)
502 #if defined(MAGICKCORE_OPENMP_SUPPORT)
503 #pragma omp parallel for schedule(static) shared(progress,status)
505 for (y=0; y < (ssize_t) evaluate_image->rows; y++)
514 id = GetOpenMPThreadId();
516 register PixelChannels
525 if (status == MagickFalse)
527 q=QueueCacheViewAuthenticPixels(evaluate_view,0,y,
528 evaluate_image->columns,1,exception);
529 if (q == (Quantum *) NULL)
534 evaluate_pixel=evaluate_pixels[id];
535 for (x=0; x < (ssize_t) evaluate_image->columns; x++)
541 for (j=0; j < (ssize_t) number_images; j++)
542 for (k=0; k < MaxPixelChannels; k++)
543 evaluate_pixel[j].channel[k]=0.0;
545 for (j=0; j < (ssize_t) number_images; j++)
547 register const Quantum
553 image_view=AcquireCacheView(next);
554 p=GetCacheViewVirtualPixels(image_view,x,y,1,1,exception);
555 if (p == (const Quantum *) NULL)
557 image_view=DestroyCacheView(image_view);
560 for (i=0; i < (ssize_t) GetPixelChannels(evaluate_image); i++)
569 channel=GetPixelChannelMapChannel(evaluate_image,i);
570 evaluate_traits=GetPixelChannelMapTraits(evaluate_image,channel);
571 traits=GetPixelChannelMapTraits(next,channel);
572 if ((traits == UndefinedPixelTrait) ||
573 (evaluate_traits == UndefinedPixelTrait))
575 if ((evaluate_traits & UpdatePixelTrait) == 0)
577 evaluate_pixel[j].channel[i]=ApplyEvaluateOperator(
578 random_info[id],GetPixelChannel(evaluate_image,channel,p),op,
579 evaluate_pixel[j].channel[i]);
581 image_view=DestroyCacheView(image_view);
582 next=GetNextImageInList(next);
584 qsort((void *) evaluate_pixel,number_images,sizeof(*evaluate_pixel),
586 for (k=0; k < (ssize_t) GetPixelChannels(evaluate_image); k++)
587 q[k]=ClampToQuantum(evaluate_pixel[j/2].channel[k]);
588 q+=GetPixelChannels(evaluate_image);
590 if (SyncCacheViewAuthenticPixels(evaluate_view,exception) == MagickFalse)
592 if (images->progress_monitor != (MagickProgressMonitor) NULL)
597 #if defined(MAGICKCORE_OPENMP_SUPPORT)
598 #pragma omp critical (MagickCore_EvaluateImages)
600 proceed=SetImageProgress(images,EvaluateImageTag,progress++,
601 evaluate_image->rows);
602 if (proceed == MagickFalse)
609 #if defined(MAGICKCORE_OPENMP_SUPPORT)
610 #pragma omp parallel for schedule(static) shared(progress,status)
612 for (y=0; y < (ssize_t) evaluate_image->rows; y++)
621 id = GetOpenMPThreadId();
627 register PixelChannels
636 if (status == MagickFalse)
638 q=QueueCacheViewAuthenticPixels(evaluate_view,0,y,
639 evaluate_image->columns,1,exception);
640 if (q == (Quantum *) NULL)
645 evaluate_pixel=evaluate_pixels[id];
646 for (j=0; j < (ssize_t) evaluate_image->columns; j++)
647 for (i=0; i < MaxPixelChannels; i++)
648 evaluate_pixel[j].channel[i]=0.0;
650 for (j=0; j < (ssize_t) number_images; j++)
652 register const Quantum
655 image_view=AcquireCacheView(next);
656 p=GetCacheViewVirtualPixels(image_view,0,y,next->columns,1,exception);
657 if (p == (const Quantum *) NULL)
659 image_view=DestroyCacheView(image_view);
662 for (x=0; x < (ssize_t) next->columns; x++)
667 if (GetPixelMask(next,p) != 0)
669 p+=GetPixelChannels(next);
672 for (i=0; i < (ssize_t) GetPixelChannels(next); i++)
681 channel=GetPixelChannelMapChannel(evaluate_image,i);
682 traits=GetPixelChannelMapTraits(next,channel);
683 evaluate_traits=GetPixelChannelMapTraits(evaluate_image,channel);
684 if ((traits == UndefinedPixelTrait) ||
685 (evaluate_traits == UndefinedPixelTrait))
687 if ((traits & UpdatePixelTrait) == 0)
689 evaluate_pixel[x].channel[i]=ApplyEvaluateOperator(
690 random_info[id],GetPixelChannel(evaluate_image,channel,p),j ==
691 0 ? AddEvaluateOperator : op,evaluate_pixel[x].channel[i]);
693 p+=GetPixelChannels(next);
695 image_view=DestroyCacheView(image_view);
696 next=GetNextImageInList(next);
698 for (x=0; x < (ssize_t) evaluate_image->columns; x++)
705 case MeanEvaluateOperator:
707 for (i=0; i < (ssize_t) GetPixelChannels(evaluate_image); i++)
708 evaluate_pixel[x].channel[i]/=(MagickRealType) number_images;
711 case MultiplyEvaluateOperator:
713 for (i=0; i < (ssize_t) GetPixelChannels(evaluate_image); i++)
718 for (j=0; j < (ssize_t) (number_images-1); j++)
719 evaluate_pixel[x].channel[i]*=QuantumScale;
727 for (x=0; x < (ssize_t) evaluate_image->columns; x++)
732 if (GetPixelMask(evaluate_image,q) != 0)
734 q+=GetPixelChannels(evaluate_image);
737 for (i=0; i < (ssize_t) GetPixelChannels(evaluate_image); i++)
745 channel=GetPixelChannelMapChannel(evaluate_image,i);
746 traits=GetPixelChannelMapTraits(evaluate_image,channel);
747 if (traits == UndefinedPixelTrait)
749 if ((traits & UpdatePixelTrait) == 0)
751 q[i]=ClampToQuantum(evaluate_pixel[x].channel[i]);
753 q+=GetPixelChannels(evaluate_image);
755 if (SyncCacheViewAuthenticPixels(evaluate_view,exception) == MagickFalse)
757 if (images->progress_monitor != (MagickProgressMonitor) NULL)
762 #if defined(MAGICKCORE_OPENMP_SUPPORT)
763 #pragma omp critical (MagickCore_EvaluateImages)
765 proceed=SetImageProgress(images,EvaluateImageTag,progress++,
766 evaluate_image->rows);
767 if (proceed == MagickFalse)
772 evaluate_view=DestroyCacheView(evaluate_view);
773 evaluate_pixels=DestroyPixelThreadSet(evaluate_pixels);
774 random_info=DestroyRandomInfoThreadSet(random_info);
775 if (status == MagickFalse)
776 evaluate_image=DestroyImage(evaluate_image);
777 return(evaluate_image);
780 MagickExport MagickBooleanType EvaluateImage(Image *image,
781 const MagickEvaluateOperator op,const double value,ExceptionInfo *exception)
793 **restrict random_info;
798 assert(image != (Image *) NULL);
799 assert(image->signature == MagickSignature);
800 if (image->debug != MagickFalse)
801 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
802 assert(exception != (ExceptionInfo *) NULL);
803 assert(exception->signature == MagickSignature);
804 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
808 random_info=AcquireRandomInfoThreadSet();
809 image_view=AcquireCacheView(image);
810 #if defined(MAGICKCORE_OPENMP_SUPPORT)
811 #pragma omp parallel for schedule(static,4) shared(progress,status)
813 for (y=0; y < (ssize_t) image->rows; y++)
816 id = GetOpenMPThreadId();
824 if (status == MagickFalse)
826 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
827 if (q == (Quantum *) NULL)
832 for (x=0; x < (ssize_t) image->columns; x++)
837 if (GetPixelMask(image,q) != 0)
839 q+=GetPixelChannels(image);
842 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
850 channel=GetPixelChannelMapChannel(image,i);
851 traits=GetPixelChannelMapTraits(image,channel);
852 if (traits == UndefinedPixelTrait)
854 q[i]=ClampToQuantum(ApplyEvaluateOperator(random_info[id],q[i],op,
857 q+=GetPixelChannels(image);
859 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
861 if (image->progress_monitor != (MagickProgressMonitor) NULL)
866 #if defined(MAGICKCORE_OPENMP_SUPPORT)
867 #pragma omp critical (MagickCore_EvaluateImage)
869 proceed=SetImageProgress(image,EvaluateImageTag,progress++,image->rows);
870 if (proceed == MagickFalse)
874 image_view=DestroyCacheView(image_view);
875 random_info=DestroyRandomInfoThreadSet(random_info);
880 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
884 % F u n c t i o n I m a g e %
888 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
890 % FunctionImage() applies a value to the image with an arithmetic, relational,
891 % or logical operator to an image. Use these operations to lighten or darken
892 % an image, to increase or decrease contrast in an image, or to produce the
893 % "negative" of an image.
895 % The format of the FunctionImage method is:
897 % MagickBooleanType FunctionImage(Image *image,
898 % const MagickFunction function,const ssize_t number_parameters,
899 % const double *parameters,ExceptionInfo *exception)
901 % A description of each parameter follows:
903 % o image: the image.
905 % o function: A channel function.
907 % o parameters: one or more parameters.
909 % o exception: return any errors or warnings in this structure.
913 static Quantum ApplyFunction(Quantum pixel,const MagickFunction function,
914 const size_t number_parameters,const double *parameters,
915 ExceptionInfo *exception)
927 case PolynomialFunction:
930 Polynomial: polynomial constants, highest to lowest order (e.g. c0*x^3+
934 for (i=0; i < (ssize_t) number_parameters; i++)
935 result=result*QuantumScale*pixel+parameters[i];
936 result*=QuantumRange;
939 case SinusoidFunction:
948 Sinusoid: frequency, phase, amplitude, bias.
950 frequency=(number_parameters >= 1) ? parameters[0] : 1.0;
951 phase=(number_parameters >= 2) ? parameters[1] : 0.0;
952 amplitude=(number_parameters >= 3) ? parameters[2] : 0.5;
953 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
954 result=(MagickRealType) (QuantumRange*(amplitude*sin((double) (2.0*
955 MagickPI*(frequency*QuantumScale*pixel+phase/360.0)))+bias));
967 Arcsin (peged at range limits for invalid results): width, center,
970 width=(number_parameters >= 1) ? parameters[0] : 1.0;
971 center=(number_parameters >= 2) ? parameters[1] : 0.5;
972 range=(number_parameters >= 3) ? parameters[2] : 1.0;
973 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
974 result=2.0/width*(QuantumScale*pixel-center);
975 if ( result <= -1.0 )
976 result=bias-range/2.0;
979 result=bias+range/2.0;
981 result=(MagickRealType) (range/MagickPI*asin((double) result)+bias);
982 result*=QuantumRange;
994 Arctan: slope, center, range, and bias.
996 slope=(number_parameters >= 1) ? parameters[0] : 1.0;
997 center=(number_parameters >= 2) ? parameters[1] : 0.5;
998 range=(number_parameters >= 3) ? parameters[2] : 1.0;
999 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
1000 result=(MagickRealType) (MagickPI*slope*(QuantumScale*pixel-center));
1001 result=(MagickRealType) (QuantumRange*(range/MagickPI*atan((double)
1005 case UndefinedFunction:
1008 return(ClampToQuantum(result));
1011 MagickExport MagickBooleanType FunctionImage(Image *image,
1012 const MagickFunction function,const size_t number_parameters,
1013 const double *parameters,ExceptionInfo *exception)
1015 #define FunctionImageTag "Function/Image "
1029 assert(image != (Image *) NULL);
1030 assert(image->signature == MagickSignature);
1031 if (image->debug != MagickFalse)
1032 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1033 assert(exception != (ExceptionInfo *) NULL);
1034 assert(exception->signature == MagickSignature);
1035 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1036 return(MagickFalse);
1039 image_view=AcquireCacheView(image);
1040 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1041 #pragma omp parallel for schedule(static,4) shared(progress,status)
1043 for (y=0; y < (ssize_t) image->rows; y++)
1051 if (status == MagickFalse)
1053 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1054 if (q == (Quantum *) NULL)
1059 for (x=0; x < (ssize_t) image->columns; x++)
1064 if (GetPixelMask(image,q) != 0)
1066 q+=GetPixelChannels(image);
1069 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1077 channel=GetPixelChannelMapChannel(image,i);
1078 traits=GetPixelChannelMapTraits(image,channel);
1079 if (traits == UndefinedPixelTrait)
1081 if ((traits & UpdatePixelTrait) == 0)
1083 q[i]=ApplyFunction(q[i],function,number_parameters,parameters,
1086 q+=GetPixelChannels(image);
1088 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1090 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1095 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1096 #pragma omp critical (MagickCore_FunctionImage)
1098 proceed=SetImageProgress(image,FunctionImageTag,progress++,image->rows);
1099 if (proceed == MagickFalse)
1103 image_view=DestroyCacheView(image_view);
1108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1112 % G e t I m a g e E x t r e m a %
1116 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1118 % GetImageExtrema() returns the extrema of one or more image channels.
1120 % The format of the GetImageExtrema method is:
1122 % MagickBooleanType GetImageExtrema(const Image *image,size_t *minima,
1123 % size_t *maxima,ExceptionInfo *exception)
1125 % A description of each parameter follows:
1127 % o image: the image.
1129 % o minima: the minimum value in the channel.
1131 % o maxima: the maximum value in the channel.
1133 % o exception: return any errors or warnings in this structure.
1136 MagickExport MagickBooleanType GetImageExtrema(const Image *image,
1137 size_t *minima,size_t *maxima,ExceptionInfo *exception)
1146 assert(image != (Image *) NULL);
1147 assert(image->signature == MagickSignature);
1148 if (image->debug != MagickFalse)
1149 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1150 status=GetImageRange(image,&min,&max,exception);
1151 *minima=(size_t) ceil(min-0.5);
1152 *maxima=(size_t) floor(max+0.5);
1157 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1161 % G e t I m a g e M e a n %
1165 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1167 % GetImageMean() returns the mean and standard deviation of one or more
1170 % The format of the GetImageMean method is:
1172 % MagickBooleanType GetImageMean(const Image *image,double *mean,
1173 % double *standard_deviation,ExceptionInfo *exception)
1175 % A description of each parameter follows:
1177 % o image: the image.
1179 % o mean: the average value in the channel.
1181 % o standard_deviation: the standard deviation of the channel.
1183 % o exception: return any errors or warnings in this structure.
1186 MagickExport MagickBooleanType GetImageMean(const Image *image,double *mean,
1187 double *standard_deviation,ExceptionInfo *exception)
1190 *channel_statistics;
1198 assert(image != (Image *) NULL);
1199 assert(image->signature == MagickSignature);
1200 if (image->debug != MagickFalse)
1201 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1202 channel_statistics=GetImageStatistics(image,exception);
1203 if (channel_statistics == (ChannelStatistics *) NULL)
1204 return(MagickFalse);
1206 channel_statistics[CompositePixelChannel].mean=0.0;
1207 channel_statistics[CompositePixelChannel].standard_deviation=0.0;
1208 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1216 channel=GetPixelChannelMapChannel(image,i);
1217 traits=GetPixelChannelMapTraits(image,channel);
1218 if (traits == UndefinedPixelTrait)
1220 if ((traits & UpdatePixelTrait) == 0)
1222 channel_statistics[CompositePixelChannel].mean+=channel_statistics[i].mean;
1223 channel_statistics[CompositePixelChannel].standard_deviation+=
1224 channel_statistics[i].variance-channel_statistics[i].mean*
1225 channel_statistics[i].mean;
1228 channel_statistics[CompositePixelChannel].mean/=area;
1229 channel_statistics[CompositePixelChannel].standard_deviation=
1230 sqrt(channel_statistics[CompositePixelChannel].standard_deviation/area);
1231 *mean=channel_statistics[CompositePixelChannel].mean;
1232 *standard_deviation=
1233 channel_statistics[CompositePixelChannel].standard_deviation;
1234 channel_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1235 channel_statistics);
1240 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1244 % G e t I m a g e K u r t o s i s %
1248 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1250 % GetImageKurtosis() returns the kurtosis and skewness of one or more
1253 % The format of the GetImageKurtosis method is:
1255 % MagickBooleanType GetImageKurtosis(const Image *image,double *kurtosis,
1256 % double *skewness,ExceptionInfo *exception)
1258 % A description of each parameter follows:
1260 % o image: the image.
1262 % o kurtosis: the kurtosis of the channel.
1264 % o skewness: the skewness of the channel.
1266 % o exception: return any errors or warnings in this structure.
1269 MagickExport MagickBooleanType GetImageKurtosis(const Image *image,
1270 double *kurtosis,double *skewness,ExceptionInfo *exception)
1289 assert(image != (Image *) NULL);
1290 assert(image->signature == MagickSignature);
1291 if (image->debug != MagickFalse)
1292 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1298 standard_deviation=0.0;
1301 sum_fourth_power=0.0;
1302 image_view=AcquireCacheView(image);
1303 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1304 #pragma omp parallel for schedule(static) shared(status)
1306 for (y=0; y < (ssize_t) image->rows; y++)
1308 register const Quantum
1314 if (status == MagickFalse)
1316 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1317 if (p == (const Quantum *) NULL)
1322 for (x=0; x < (ssize_t) image->columns; x++)
1327 if (GetPixelMask(image,p) != 0)
1329 p+=GetPixelChannels(image);
1332 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1340 channel=GetPixelChannelMapChannel(image,i);
1341 traits=GetPixelChannelMapTraits(image,channel);
1342 if (traits == UndefinedPixelTrait)
1344 if ((traits & UpdatePixelTrait) == 0)
1346 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1347 #pragma omp critical (MagickCore_GetImageKurtosis)
1351 sum_squares+=(double) p[i]*p[i];
1352 sum_cubes+=(double) p[i]*p[i]*p[i];
1353 sum_fourth_power+=(double) p[i]*p[i]*p[i]*p[i];
1357 p+=GetPixelChannels(image);
1360 image_view=DestroyCacheView(image_view);
1366 sum_fourth_power/=area;
1368 standard_deviation=sqrt(sum_squares-(mean*mean));
1369 if (standard_deviation != 0.0)
1371 *kurtosis=sum_fourth_power-4.0*mean*sum_cubes+6.0*mean*mean*sum_squares-
1372 3.0*mean*mean*mean*mean;
1373 *kurtosis/=standard_deviation*standard_deviation*standard_deviation*
1376 *skewness=sum_cubes-3.0*mean*sum_squares+2.0*mean*mean*mean;
1377 *skewness/=standard_deviation*standard_deviation*standard_deviation;
1383 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1387 % G e t I m a g e R a n g e %
1391 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1393 % GetImageRange() returns the range of one or more image channels.
1395 % The format of the GetImageRange method is:
1397 % MagickBooleanType GetImageRange(const Image *image,double *minima,
1398 % double *maxima,ExceptionInfo *exception)
1400 % A description of each parameter follows:
1402 % o image: the image.
1404 % o minima: the minimum value in the channel.
1406 % o maxima: the maximum value in the channel.
1408 % o exception: return any errors or warnings in this structure.
1411 MagickExport MagickBooleanType GetImageRange(const Image *image,double *minima,
1412 double *maxima,ExceptionInfo *exception)
1423 assert(image != (Image *) NULL);
1424 assert(image->signature == MagickSignature);
1425 if (image->debug != MagickFalse)
1426 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1428 *maxima=(-MagickHuge);
1430 image_view=AcquireCacheView(image);
1431 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1432 #pragma omp parallel for schedule(static) shared(status)
1434 for (y=0; y < (ssize_t) image->rows; y++)
1436 register const Quantum
1442 if (status == MagickFalse)
1444 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1445 if (p == (const Quantum *) NULL)
1450 for (x=0; x < (ssize_t) image->columns; x++)
1455 if (GetPixelMask(image,p) != 0)
1457 p+=GetPixelChannels(image);
1460 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1468 channel=GetPixelChannelMapChannel(image,i);
1469 traits=GetPixelChannelMapTraits(image,channel);
1470 if (traits == UndefinedPixelTrait)
1472 if ((traits & UpdatePixelTrait) == 0)
1474 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1475 #pragma omp critical (MagickCore_GetImageRange)
1479 *minima=(double) p[i];
1481 *maxima=(double) p[i];
1484 p+=GetPixelChannels(image);
1487 image_view=DestroyCacheView(image_view);
1492 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1496 % G e t I m a g e S t a t i s t i c s %
1500 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1502 % GetImageStatistics() returns statistics for each channel in the
1503 % image. The statistics include the channel depth, its minima, maxima, mean,
1504 % standard deviation, kurtosis and skewness. You can access the red channel
1505 % mean, for example, like this:
1507 % channel_statistics=GetImageStatistics(image,exception);
1508 % red_mean=channel_statistics[RedPixelChannel].mean;
1510 % Use MagickRelinquishMemory() to free the statistics buffer.
1512 % The format of the GetImageStatistics method is:
1514 % ChannelStatistics *GetImageStatistics(const Image *image,
1515 % ExceptionInfo *exception)
1517 % A description of each parameter follows:
1519 % o image: the image.
1521 % o exception: return any errors or warnings in this structure.
1525 static size_t GetImageChannels(const Image *image)
1534 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1542 channel=GetPixelChannelMapChannel(image,i);
1543 traits=GetPixelChannelMapTraits(image,channel);
1544 if ((traits & UpdatePixelTrait) != 0)
1550 MagickExport ChannelStatistics *GetImageStatistics(const Image *image,
1551 ExceptionInfo *exception)
1554 *channel_statistics;
1575 assert(image != (Image *) NULL);
1576 assert(image->signature == MagickSignature);
1577 if (image->debug != MagickFalse)
1578 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1579 channel_statistics=(ChannelStatistics *) AcquireQuantumMemory(
1580 MaxPixelChannels+1,sizeof(*channel_statistics));
1581 if (channel_statistics == (ChannelStatistics *) NULL)
1582 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1583 (void) ResetMagickMemory(channel_statistics,0,(MaxPixelChannels+1)*
1584 sizeof(*channel_statistics));
1585 for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
1587 channel_statistics[i].depth=1;
1588 channel_statistics[i].maxima=(-MagickHuge);
1589 channel_statistics[i].minima=MagickHuge;
1591 for (y=0; y < (ssize_t) image->rows; y++)
1593 register const Quantum
1599 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1600 if (p == (const Quantum *) NULL)
1602 for (x=0; x < (ssize_t) image->columns; x++)
1607 if (GetPixelMask(image,p) != 0)
1609 p+=GetPixelChannels(image);
1612 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1620 channel=GetPixelChannelMapChannel(image,i);
1621 traits=GetPixelChannelMapTraits(image,channel);
1622 if (traits == UndefinedPixelTrait)
1624 if (channel_statistics[channel].depth != MAGICKCORE_QUANTUM_DEPTH)
1626 depth=channel_statistics[channel].depth;
1627 range=GetQuantumRange(depth);
1628 status=p[i] != ScaleAnyToQuantum(ScaleQuantumToAny(p[i],range),
1629 range) ? MagickTrue : MagickFalse;
1630 if (status != MagickFalse)
1632 channel_statistics[channel].depth++;
1636 if ((double) p[i] < channel_statistics[channel].minima)
1637 channel_statistics[channel].minima=(double) p[i];
1638 if ((double) p[i] > channel_statistics[channel].maxima)
1639 channel_statistics[channel].maxima=(double) p[i];
1640 channel_statistics[channel].sum+=p[i];
1641 channel_statistics[channel].sum_squared+=(double) p[i]*p[i];
1642 channel_statistics[channel].sum_cubed+=(double) p[i]*p[i]*p[i];
1643 channel_statistics[channel].sum_fourth_power+=(double) p[i]*p[i]*p[i]*
1646 p+=GetPixelChannels(image);
1649 area=(double) image->columns*image->rows;
1650 for (i=0; i < (ssize_t) MaxPixelChannels; i++)
1652 channel_statistics[i].sum/=area;
1653 channel_statistics[i].sum_squared/=area;
1654 channel_statistics[i].sum_cubed/=area;
1655 channel_statistics[i].sum_fourth_power/=area;
1656 channel_statistics[i].mean=channel_statistics[i].sum;
1657 channel_statistics[i].variance=channel_statistics[i].sum_squared;
1658 channel_statistics[i].standard_deviation=sqrt(
1659 channel_statistics[i].variance-(channel_statistics[i].mean*
1660 channel_statistics[i].mean));
1662 for (i=0; i < (ssize_t) MaxPixelChannels; i++)
1664 channel_statistics[CompositePixelChannel].depth=(size_t) EvaluateMax(
1665 (double) channel_statistics[CompositePixelChannel].depth,(double)
1666 channel_statistics[i].depth);
1667 channel_statistics[CompositePixelChannel].minima=MagickMin(
1668 channel_statistics[CompositePixelChannel].minima,
1669 channel_statistics[i].minima);
1670 channel_statistics[CompositePixelChannel].maxima=EvaluateMax(
1671 channel_statistics[CompositePixelChannel].maxima,
1672 channel_statistics[i].maxima);
1673 channel_statistics[CompositePixelChannel].sum+=channel_statistics[i].sum;
1674 channel_statistics[CompositePixelChannel].sum_squared+=
1675 channel_statistics[i].sum_squared;
1676 channel_statistics[CompositePixelChannel].sum_cubed+=
1677 channel_statistics[i].sum_cubed;
1678 channel_statistics[CompositePixelChannel].sum_fourth_power+=
1679 channel_statistics[i].sum_fourth_power;
1680 channel_statistics[CompositePixelChannel].mean+=channel_statistics[i].mean;
1681 channel_statistics[CompositePixelChannel].variance+=
1682 channel_statistics[i].variance-channel_statistics[i].mean*
1683 channel_statistics[i].mean;
1684 channel_statistics[CompositePixelChannel].standard_deviation+=
1685 channel_statistics[i].variance-channel_statistics[i].mean*
1686 channel_statistics[i].mean;
1688 channels=GetImageChannels(image);
1689 channel_statistics[CompositePixelChannel].sum/=channels;
1690 channel_statistics[CompositePixelChannel].sum_squared/=channels;
1691 channel_statistics[CompositePixelChannel].sum_cubed/=channels;
1692 channel_statistics[CompositePixelChannel].sum_fourth_power/=channels;
1693 channel_statistics[CompositePixelChannel].mean/=channels;
1694 channel_statistics[CompositePixelChannel].variance/=channels;
1695 channel_statistics[CompositePixelChannel].standard_deviation=
1696 sqrt(channel_statistics[CompositePixelChannel].standard_deviation/channels);
1697 channel_statistics[CompositePixelChannel].kurtosis/=channels;
1698 channel_statistics[CompositePixelChannel].skewness/=channels;
1699 for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
1701 if (channel_statistics[i].standard_deviation == 0.0)
1703 channel_statistics[i].skewness=(channel_statistics[i].sum_cubed-3.0*
1704 channel_statistics[i].mean*channel_statistics[i].sum_squared+2.0*
1705 channel_statistics[i].mean*channel_statistics[i].mean*
1706 channel_statistics[i].mean)/(channel_statistics[i].standard_deviation*
1707 channel_statistics[i].standard_deviation*
1708 channel_statistics[i].standard_deviation);
1709 channel_statistics[i].kurtosis=(channel_statistics[i].sum_fourth_power-4.0*
1710 channel_statistics[i].mean*channel_statistics[i].sum_cubed+6.0*
1711 channel_statistics[i].mean*channel_statistics[i].mean*
1712 channel_statistics[i].sum_squared-3.0*channel_statistics[i].mean*
1713 channel_statistics[i].mean*1.0*channel_statistics[i].mean*
1714 channel_statistics[i].mean)/(channel_statistics[i].standard_deviation*
1715 channel_statistics[i].standard_deviation*
1716 channel_statistics[i].standard_deviation*
1717 channel_statistics[i].standard_deviation)-3.0;
1719 return(channel_statistics);
1723 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1727 % S t a t i s t i c I m a g e %
1731 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1733 % StatisticImage() makes each pixel the min / max / median / mode / etc. of
1734 % the neighborhood of the specified width and height.
1736 % The format of the StatisticImage method is:
1738 % Image *StatisticImage(const Image *image,const StatisticType type,
1739 % const size_t width,const size_t height,ExceptionInfo *exception)
1741 % A description of each parameter follows:
1743 % o image: the image.
1745 % o type: the statistic type (median, mode, etc.).
1747 % o width: the width of the pixel neighborhood.
1749 % o height: the height of the pixel neighborhood.
1751 % o exception: return any errors or warnings in this structure.
1755 typedef struct _SkipNode
1763 typedef struct _SkipList
1772 typedef struct _PixelList
1785 static PixelList *DestroyPixelList(PixelList *pixel_list)
1787 if (pixel_list == (PixelList *) NULL)
1788 return((PixelList *) NULL);
1789 if (pixel_list->skip_list.nodes != (SkipNode *) NULL)
1790 pixel_list->skip_list.nodes=(SkipNode *) RelinquishMagickMemory(
1791 pixel_list->skip_list.nodes);
1792 pixel_list=(PixelList *) RelinquishMagickMemory(pixel_list);
1796 static PixelList **DestroyPixelListThreadSet(PixelList **pixel_list)
1801 assert(pixel_list != (PixelList **) NULL);
1802 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
1803 if (pixel_list[i] != (PixelList *) NULL)
1804 pixel_list[i]=DestroyPixelList(pixel_list[i]);
1805 pixel_list=(PixelList **) RelinquishMagickMemory(pixel_list);
1809 static PixelList *AcquirePixelList(const size_t width,const size_t height)
1814 pixel_list=(PixelList *) AcquireMagickMemory(sizeof(*pixel_list));
1815 if (pixel_list == (PixelList *) NULL)
1817 (void) ResetMagickMemory((void *) pixel_list,0,sizeof(*pixel_list));
1818 pixel_list->length=width*height;
1819 pixel_list->skip_list.nodes=(SkipNode *) AcquireQuantumMemory(65537UL,
1820 sizeof(*pixel_list->skip_list.nodes));
1821 if (pixel_list->skip_list.nodes == (SkipNode *) NULL)
1822 return(DestroyPixelList(pixel_list));
1823 (void) ResetMagickMemory(pixel_list->skip_list.nodes,0,65537UL*
1824 sizeof(*pixel_list->skip_list.nodes));
1825 pixel_list->signature=MagickSignature;
1829 static PixelList **AcquirePixelListThreadSet(const size_t width,
1830 const size_t height)
1841 number_threads=GetOpenMPMaximumThreads();
1842 pixel_list=(PixelList **) AcquireQuantumMemory(number_threads,
1843 sizeof(*pixel_list));
1844 if (pixel_list == (PixelList **) NULL)
1845 return((PixelList **) NULL);
1846 (void) ResetMagickMemory(pixel_list,0,number_threads*sizeof(*pixel_list));
1847 for (i=0; i < (ssize_t) number_threads; i++)
1849 pixel_list[i]=AcquirePixelList(width,height);
1850 if (pixel_list[i] == (PixelList *) NULL)
1851 return(DestroyPixelListThreadSet(pixel_list));
1856 static void AddNodePixelList(PixelList *pixel_list,const size_t color)
1869 Initialize the node.
1871 p=(&pixel_list->skip_list);
1872 p->nodes[color].signature=pixel_list->signature;
1873 p->nodes[color].count=1;
1875 Determine where it belongs in the list.
1878 for (level=p->level; level >= 0; level--)
1880 while (p->nodes[search].next[level] < color)
1881 search=p->nodes[search].next[level];
1882 update[level]=search;
1885 Generate a pseudo-random level for this node.
1887 for (level=0; ; level++)
1889 pixel_list->seed=(pixel_list->seed*42893621L)+1L;
1890 if ((pixel_list->seed & 0x300) != 0x300)
1895 if (level > (p->level+2))
1898 If we're raising the list's level, link back to the root node.
1900 while (level > p->level)
1903 update[p->level]=65536UL;
1906 Link the node into the skip-list.
1910 p->nodes[color].next[level]=p->nodes[update[level]].next[level];
1911 p->nodes[update[level]].next[level]=color;
1912 } while (level-- > 0);
1915 static inline void GetMaximumPixelList(PixelList *pixel_list,Quantum *pixel)
1928 Find the maximum value for each of the color.
1930 p=(&pixel_list->skip_list);
1933 maximum=p->nodes[color].next[0];
1936 color=p->nodes[color].next[0];
1937 if (color > maximum)
1939 count+=p->nodes[color].count;
1940 } while (count < (ssize_t) pixel_list->length);
1941 *pixel=ScaleShortToQuantum((unsigned short) maximum);
1944 static inline void GetMeanPixelList(PixelList *pixel_list,Quantum *pixel)
1959 Find the mean value for each of the color.
1961 p=(&pixel_list->skip_list);
1967 color=p->nodes[color].next[0];
1968 sum+=(MagickRealType) p->nodes[color].count*color;
1969 count+=p->nodes[color].count;
1970 } while (count < (ssize_t) pixel_list->length);
1971 sum/=pixel_list->length;
1972 *pixel=ScaleShortToQuantum((unsigned short) sum);
1975 static inline void GetMedianPixelList(PixelList *pixel_list,Quantum *pixel)
1987 Find the median value for each of the color.
1989 p=(&pixel_list->skip_list);
1994 color=p->nodes[color].next[0];
1995 count+=p->nodes[color].count;
1996 } while (count <= (ssize_t) (pixel_list->length >> 1));
1997 *pixel=ScaleShortToQuantum((unsigned short) color);
2000 static inline void GetMinimumPixelList(PixelList *pixel_list,Quantum *pixel)
2013 Find the minimum value for each of the color.
2015 p=(&pixel_list->skip_list);
2018 minimum=p->nodes[color].next[0];
2021 color=p->nodes[color].next[0];
2022 if (color < minimum)
2024 count+=p->nodes[color].count;
2025 } while (count < (ssize_t) pixel_list->length);
2026 *pixel=ScaleShortToQuantum((unsigned short) minimum);
2029 static inline void GetModePixelList(PixelList *pixel_list,Quantum *pixel)
2043 Make each pixel the 'predominant color' of the specified neighborhood.
2045 p=(&pixel_list->skip_list);
2048 max_count=p->nodes[mode].count;
2052 color=p->nodes[color].next[0];
2053 if (p->nodes[color].count > max_count)
2056 max_count=p->nodes[mode].count;
2058 count+=p->nodes[color].count;
2059 } while (count < (ssize_t) pixel_list->length);
2060 *pixel=ScaleShortToQuantum((unsigned short) mode);
2063 static inline void GetNonpeakPixelList(PixelList *pixel_list,Quantum *pixel)
2077 Finds the non peak value for each of the colors.
2079 p=(&pixel_list->skip_list);
2081 next=p->nodes[color].next[0];
2087 next=p->nodes[color].next[0];
2088 count+=p->nodes[color].count;
2089 } while (count <= (ssize_t) (pixel_list->length >> 1));
2090 if ((previous == 65536UL) && (next != 65536UL))
2093 if ((previous != 65536UL) && (next == 65536UL))
2095 *pixel=ScaleShortToQuantum((unsigned short) color);
2098 static inline void GetStandardDeviationPixelList(PixelList *pixel_list,
2115 Find the standard-deviation value for each of the color.
2117 p=(&pixel_list->skip_list);
2127 color=p->nodes[color].next[0];
2128 sum+=(MagickRealType) p->nodes[color].count*color;
2129 for (i=0; i < (ssize_t) p->nodes[color].count; i++)
2130 sum_squared+=((MagickRealType) color)*((MagickRealType) color);
2131 count+=p->nodes[color].count;
2132 } while (count < (ssize_t) pixel_list->length);
2133 sum/=pixel_list->length;
2134 sum_squared/=pixel_list->length;
2135 *pixel=ScaleShortToQuantum((unsigned short) sqrt(sum_squared-(sum*sum)));
2138 static inline void InsertPixelList(const Image *image,const Quantum pixel,
2139 PixelList *pixel_list)
2147 index=ScaleQuantumToShort(pixel);
2148 signature=pixel_list->skip_list.nodes[index].signature;
2149 if (signature == pixel_list->signature)
2151 pixel_list->skip_list.nodes[index].count++;
2154 AddNodePixelList(pixel_list,index);
2157 static inline MagickRealType MagickAbsoluteValue(const MagickRealType x)
2164 static inline size_t MagickMax(const size_t x,const size_t y)
2171 static void ResetPixelList(PixelList *pixel_list)
2183 Reset the skip-list.
2185 p=(&pixel_list->skip_list);
2186 root=p->nodes+65536UL;
2188 for (level=0; level < 9; level++)
2189 root->next[level]=65536UL;
2190 pixel_list->seed=pixel_list->signature++;
2193 MagickExport Image *StatisticImage(const Image *image,const StatisticType type,
2194 const size_t width,const size_t height,ExceptionInfo *exception)
2196 #define StatisticImageTag "Statistic/Image"
2212 **restrict pixel_list;
2219 Initialize statistics image attributes.
2221 assert(image != (Image *) NULL);
2222 assert(image->signature == MagickSignature);
2223 if (image->debug != MagickFalse)
2224 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2225 assert(exception != (ExceptionInfo *) NULL);
2226 assert(exception->signature == MagickSignature);
2227 statistic_image=CloneImage(image,image->columns,image->rows,MagickTrue,
2229 if (statistic_image == (Image *) NULL)
2230 return((Image *) NULL);
2231 status=SetImageStorageClass(statistic_image,DirectClass,exception);
2232 if (status == MagickFalse)
2234 statistic_image=DestroyImage(statistic_image);
2235 return((Image *) NULL);
2237 pixel_list=AcquirePixelListThreadSet(MagickMax(width,1),MagickMax(height,1));
2238 if (pixel_list == (PixelList **) NULL)
2240 statistic_image=DestroyImage(statistic_image);
2241 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2244 Make each pixel the min / max / median / mode / etc. of the neighborhood.
2246 center=(ssize_t) GetPixelChannels(image)*(image->columns+MagickMax(width,1))*
2247 (MagickMax(height,1)/2L)+GetPixelChannels(image)*(MagickMax(width,1)/2L);
2250 image_view=AcquireCacheView(image);
2251 statistic_view=AcquireCacheView(statistic_image);
2252 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2253 #pragma omp parallel for schedule(static,4) shared(progress,status)
2255 for (y=0; y < (ssize_t) statistic_image->rows; y++)
2258 id = GetOpenMPThreadId();
2260 register const Quantum
2269 if (status == MagickFalse)
2271 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) MagickMax(width,1)/2L),y-
2272 (ssize_t) (MagickMax(height,1)/2L),image->columns+MagickMax(width,1),
2273 MagickMax(height,1),exception);
2274 q=QueueCacheViewAuthenticPixels(statistic_view,0,y,statistic_image->columns, 1,exception);
2275 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2280 for (x=0; x < (ssize_t) statistic_image->columns; x++)
2285 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2297 register const Quantum
2306 channel=GetPixelChannelMapChannel(image,i);
2307 traits=GetPixelChannelMapTraits(image,channel);
2308 statistic_traits=GetPixelChannelMapTraits(statistic_image,channel);
2309 if ((traits == UndefinedPixelTrait) ||
2310 (statistic_traits == UndefinedPixelTrait))
2312 if (((statistic_traits & CopyPixelTrait) != 0) ||
2313 (GetPixelMask(image,p) != 0))
2315 SetPixelChannel(statistic_image,channel,p[center+i],q);
2319 ResetPixelList(pixel_list[id]);
2320 for (v=0; v < (ssize_t) MagickMax(height,1); v++)
2322 for (u=0; u < (ssize_t) MagickMax(width,1); u++)
2324 InsertPixelList(image,pixels[i],pixel_list[id]);
2325 pixels+=GetPixelChannels(image);
2327 pixels+=image->columns*GetPixelChannels(image);
2331 case GradientStatistic:
2337 GetMinimumPixelList(pixel_list[id],&pixel);
2338 minimum=(MagickRealType) pixel;
2339 GetMaximumPixelList(pixel_list[id],&pixel);
2340 maximum=(MagickRealType) pixel;
2341 pixel=ClampToQuantum(MagickAbsoluteValue(maximum-minimum));
2344 case MaximumStatistic:
2346 GetMaximumPixelList(pixel_list[id],&pixel);
2351 GetMeanPixelList(pixel_list[id],&pixel);
2354 case MedianStatistic:
2357 GetMedianPixelList(pixel_list[id],&pixel);
2360 case MinimumStatistic:
2362 GetMinimumPixelList(pixel_list[id],&pixel);
2367 GetModePixelList(pixel_list[id],&pixel);
2370 case NonpeakStatistic:
2372 GetNonpeakPixelList(pixel_list[id],&pixel);
2375 case StandardDeviationStatistic:
2377 GetStandardDeviationPixelList(pixel_list[id],&pixel);
2381 SetPixelChannel(statistic_image,channel,pixel,q);
2383 p+=GetPixelChannels(image);
2384 q+=GetPixelChannels(statistic_image);
2386 if (SyncCacheViewAuthenticPixels(statistic_view,exception) == MagickFalse)
2388 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2393 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2394 #pragma omp critical (MagickCore_StatisticImage)
2396 proceed=SetImageProgress(image,StatisticImageTag,progress++,
2398 if (proceed == MagickFalse)
2402 statistic_view=DestroyCacheView(statistic_view);
2403 image_view=DestroyCacheView(image_view);
2404 pixel_list=DestroyPixelListThreadSet(pixel_list);
2405 return(statistic_image);