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-2018 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 % https://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/accelerate-private.h"
45 #include "MagickCore/animate.h"
46 #include "MagickCore/artifact.h"
47 #include "MagickCore/blob.h"
48 #include "MagickCore/blob-private.h"
49 #include "MagickCore/cache.h"
50 #include "MagickCore/cache-private.h"
51 #include "MagickCore/cache-view.h"
52 #include "MagickCore/client.h"
53 #include "MagickCore/color.h"
54 #include "MagickCore/color-private.h"
55 #include "MagickCore/colorspace.h"
56 #include "MagickCore/colorspace-private.h"
57 #include "MagickCore/composite.h"
58 #include "MagickCore/composite-private.h"
59 #include "MagickCore/compress.h"
60 #include "MagickCore/constitute.h"
61 #include "MagickCore/display.h"
62 #include "MagickCore/draw.h"
63 #include "MagickCore/enhance.h"
64 #include "MagickCore/exception.h"
65 #include "MagickCore/exception-private.h"
66 #include "MagickCore/gem.h"
67 #include "MagickCore/gem-private.h"
68 #include "MagickCore/geometry.h"
69 #include "MagickCore/list.h"
70 #include "MagickCore/image-private.h"
71 #include "MagickCore/magic.h"
72 #include "MagickCore/magick.h"
73 #include "MagickCore/memory_.h"
74 #include "MagickCore/module.h"
75 #include "MagickCore/monitor.h"
76 #include "MagickCore/monitor-private.h"
77 #include "MagickCore/option.h"
78 #include "MagickCore/paint.h"
79 #include "MagickCore/pixel-accessor.h"
80 #include "MagickCore/profile.h"
81 #include "MagickCore/property.h"
82 #include "MagickCore/quantize.h"
83 #include "MagickCore/quantum-private.h"
84 #include "MagickCore/random_.h"
85 #include "MagickCore/random-private.h"
86 #include "MagickCore/resource_.h"
87 #include "MagickCore/segment.h"
88 #include "MagickCore/semaphore.h"
89 #include "MagickCore/signature-private.h"
90 #include "MagickCore/statistic.h"
91 #include "MagickCore/string_.h"
92 #include "MagickCore/thread-private.h"
93 #include "MagickCore/timer.h"
94 #include "MagickCore/utility.h"
95 #include "MagickCore/version.h"
98 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102 % E v a l u a t e I m a g e %
106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 % EvaluateImage() applies a value to the image with an arithmetic, relational,
109 % or logical operator to an image. Use these operations to lighten or darken
110 % an image, to increase or decrease contrast in an image, or to produce the
111 % "negative" of an image.
113 % The format of the EvaluateImage method is:
115 % MagickBooleanType EvaluateImage(Image *image,
116 % const MagickEvaluateOperator op,const double value,
117 % ExceptionInfo *exception)
118 % MagickBooleanType EvaluateImages(Image *images,
119 % const MagickEvaluateOperator op,const double value,
120 % ExceptionInfo *exception)
122 % A description of each parameter follows:
124 % o image: the image.
126 % o op: A channel op.
128 % o value: A value value.
130 % o exception: return any errors or warnings in this structure.
134 typedef struct _PixelChannels
137 channel[CompositePixelChannel];
140 static PixelChannels **DestroyPixelThreadSet(PixelChannels **pixels)
145 assert(pixels != (PixelChannels **) NULL);
146 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
147 if (pixels[i] != (PixelChannels *) NULL)
148 pixels[i]=(PixelChannels *) RelinquishMagickMemory(pixels[i]);
149 pixels=(PixelChannels **) RelinquishMagickMemory(pixels);
153 static PixelChannels **AcquirePixelThreadSet(const Image *image)
164 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
165 pixels=(PixelChannels **) AcquireQuantumMemory(number_threads,
167 if (pixels == (PixelChannels **) NULL)
168 return((PixelChannels **) NULL);
169 (void) memset(pixels,0,number_threads*sizeof(*pixels));
170 for (i=0; i < (ssize_t) number_threads; i++)
175 pixels[i]=(PixelChannels *) AcquireQuantumMemory(image->columns,
177 if (pixels[i] == (PixelChannels *) NULL)
178 return(DestroyPixelThreadSet(pixels));
179 for (j=0; j < (ssize_t) image->columns; j++)
184 for (k=0; k < MaxPixelChannels; k++)
185 pixels[i][j].channel[k]=0.0;
191 static inline double EvaluateMax(const double x,const double y)
198 #if defined(__cplusplus) || defined(c_plusplus)
202 static int IntensityCompare(const void *x,const void *y)
214 color_1=(const PixelChannels *) x;
215 color_2=(const PixelChannels *) y;
217 for (i=0; i < MaxPixelChannels; i++)
218 distance+=color_1->channel[i]-(double) color_2->channel[i];
219 return(distance < 0 ? -1 : distance > 0 ? 1 : 0);
222 #if defined(__cplusplus) || defined(c_plusplus)
226 static double ApplyEvaluateOperator(RandomInfo *random_info,const Quantum pixel,
227 const MagickEvaluateOperator op,const double value)
235 case UndefinedEvaluateOperator:
237 case AbsEvaluateOperator:
239 result=(double) fabs((double) (pixel+value));
242 case AddEvaluateOperator:
244 result=(double) (pixel+value);
247 case AddModulusEvaluateOperator:
250 This returns a 'floored modulus' of the addition which is a positive
251 result. It differs from % or fmod() that returns a 'truncated modulus'
252 result, where floor() is replaced by trunc() and could return a
253 negative result (which is clipped).
256 result-=(QuantumRange+1.0)*floor((double) result/(QuantumRange+1.0));
259 case AndEvaluateOperator:
261 result=(double) ((size_t) pixel & (size_t) (value+0.5));
264 case CosineEvaluateOperator:
266 result=(double) (QuantumRange*(0.5*cos((double) (2.0*MagickPI*
267 QuantumScale*pixel*value))+0.5));
270 case DivideEvaluateOperator:
272 result=pixel/(value == 0.0 ? 1.0 : value);
275 case ExponentialEvaluateOperator:
277 result=(double) (QuantumRange*exp((double) (value*QuantumScale*pixel)));
280 case GaussianNoiseEvaluateOperator:
282 result=(double) GenerateDifferentialNoise(random_info,pixel,
283 GaussianNoise,value);
286 case ImpulseNoiseEvaluateOperator:
288 result=(double) GenerateDifferentialNoise(random_info,pixel,ImpulseNoise,
292 case LaplacianNoiseEvaluateOperator:
294 result=(double) GenerateDifferentialNoise(random_info,pixel,
295 LaplacianNoise,value);
298 case LeftShiftEvaluateOperator:
300 result=(double) ((size_t) pixel << (size_t) (value+0.5));
303 case LogEvaluateOperator:
305 if ((QuantumScale*pixel) >= MagickEpsilon)
306 result=(double) (QuantumRange*log((double) (QuantumScale*value*pixel+
307 1.0))/log((double) (value+1.0)));
310 case MaxEvaluateOperator:
312 result=(double) EvaluateMax((double) pixel,value);
315 case MeanEvaluateOperator:
317 result=(double) (pixel+value);
320 case MedianEvaluateOperator:
322 result=(double) (pixel+value);
325 case MinEvaluateOperator:
327 result=(double) MagickMin((double) pixel,value);
330 case MultiplicativeNoiseEvaluateOperator:
332 result=(double) GenerateDifferentialNoise(random_info,pixel,
333 MultiplicativeGaussianNoise,value);
336 case MultiplyEvaluateOperator:
338 result=(double) (value*pixel);
341 case OrEvaluateOperator:
343 result=(double) ((size_t) pixel | (size_t) (value+0.5));
346 case PoissonNoiseEvaluateOperator:
348 result=(double) GenerateDifferentialNoise(random_info,pixel,PoissonNoise,
352 case PowEvaluateOperator:
354 result=(double) (QuantumRange*pow((double) (QuantumScale*pixel),(double)
358 case RightShiftEvaluateOperator:
360 result=(double) ((size_t) pixel >> (size_t) (value+0.5));
363 case RootMeanSquareEvaluateOperator:
365 result=(double) (pixel*pixel+value);
368 case SetEvaluateOperator:
373 case SineEvaluateOperator:
375 result=(double) (QuantumRange*(0.5*sin((double) (2.0*MagickPI*
376 QuantumScale*pixel*value))+0.5));
379 case SubtractEvaluateOperator:
381 result=(double) (pixel-value);
384 case SumEvaluateOperator:
386 result=(double) (pixel+value);
389 case ThresholdEvaluateOperator:
391 result=(double) (((double) pixel <= value) ? 0 : QuantumRange);
394 case ThresholdBlackEvaluateOperator:
396 result=(double) (((double) pixel <= value) ? 0 : pixel);
399 case ThresholdWhiteEvaluateOperator:
401 result=(double) (((double) pixel > value) ? QuantumRange : pixel);
404 case UniformNoiseEvaluateOperator:
406 result=(double) GenerateDifferentialNoise(random_info,pixel,UniformNoise,
410 case XorEvaluateOperator:
412 result=(double) ((size_t) pixel ^ (size_t) (value+0.5));
419 static Image *AcquireImageCanvas(const Image *images,ExceptionInfo *exception)
430 columns=images->columns;
432 for (p=images; p != (Image *) NULL; p=p->next)
434 if (p->number_channels > q->number_channels)
436 if (p->columns > columns)
441 return(CloneImage(q,columns,rows,MagickTrue,exception));
444 MagickExport Image *EvaluateImages(const Image *images,
445 const MagickEvaluateOperator op,ExceptionInfo *exception)
447 #define EvaluateImageTag "Evaluate/Image"
462 **magick_restrict evaluate_pixels;
465 **magick_restrict random_info;
473 #if defined(MAGICKCORE_OPENMP_SUPPORT)
478 assert(images != (Image *) NULL);
479 assert(images->signature == MagickCoreSignature);
480 if (images->debug != MagickFalse)
481 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
482 assert(exception != (ExceptionInfo *) NULL);
483 assert(exception->signature == MagickCoreSignature);
484 image=AcquireImageCanvas(images,exception);
485 if (image == (Image *) NULL)
486 return((Image *) NULL);
487 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
489 image=DestroyImage(image);
490 return((Image *) NULL);
492 number_images=GetImageListLength(images);
493 evaluate_pixels=AcquirePixelThreadSet(images);
494 if (evaluate_pixels == (PixelChannels **) NULL)
496 image=DestroyImage(image);
497 (void) ThrowMagickException(exception,GetMagickModule(),
498 ResourceLimitError,"MemoryAllocationFailed","`%s'",images->filename);
499 return((Image *) NULL);
502 Evaluate image pixels.
506 random_info=AcquireRandomInfoThreadSet();
507 evaluate_view=AcquireAuthenticCacheView(image,exception);
508 if (op == MedianEvaluateOperator)
510 #if defined(MAGICKCORE_OPENMP_SUPPORT)
511 key=GetRandomSecretKey(random_info[0]);
512 #pragma omp parallel for schedule(static) shared(progress,status) \
513 magick_number_threads(image,images,image->rows,key == ~0UL)
515 for (y=0; y < (ssize_t) image->rows; y++)
524 id = GetOpenMPThreadId();
526 register PixelChannels
535 if (status == MagickFalse)
537 q=QueueCacheViewAuthenticPixels(evaluate_view,0,y,image->columns,1,
539 if (q == (Quantum *) NULL)
544 evaluate_pixel=evaluate_pixels[id];
545 for (x=0; x < (ssize_t) image->columns; x++)
551 for (j=0; j < (ssize_t) number_images; j++)
552 for (k=0; k < MaxPixelChannels; k++)
553 evaluate_pixel[j].channel[k]=0.0;
555 for (j=0; j < (ssize_t) number_images; j++)
557 register const Quantum
563 image_view=AcquireVirtualCacheView(next,exception);
564 p=GetCacheViewVirtualPixels(image_view,x,y,1,1,exception);
565 if (p == (const Quantum *) NULL)
567 image_view=DestroyCacheView(image_view);
570 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
572 PixelChannel channel = GetPixelChannelChannel(image,i);
573 PixelTrait evaluate_traits=GetPixelChannelTraits(image,channel);
574 PixelTrait traits = GetPixelChannelTraits(next,channel);
575 if ((traits == UndefinedPixelTrait) ||
576 (evaluate_traits == UndefinedPixelTrait))
578 if ((traits & UpdatePixelTrait) == 0)
580 evaluate_pixel[j].channel[i]=ApplyEvaluateOperator(
581 random_info[id],GetPixelChannel(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(image); k++)
590 q[k]=ClampToQuantum(evaluate_pixel[j/2].channel[k]);
591 q+=GetPixelChannels(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++,
605 if (proceed == MagickFalse)
612 #if defined(MAGICKCORE_OPENMP_SUPPORT)
613 key=GetRandomSecretKey(random_info[0]);
614 #pragma omp parallel for schedule(static) shared(progress,status) \
615 magick_number_threads(image,images,image->rows,key == ~0UL)
617 for (y=0; y < (ssize_t) image->rows; y++)
626 id = GetOpenMPThreadId();
632 register PixelChannels
641 if (status == MagickFalse)
643 q=QueueCacheViewAuthenticPixels(evaluate_view,0,y,image->columns,1,
645 if (q == (Quantum *) NULL)
650 evaluate_pixel=evaluate_pixels[id];
651 for (j=0; j < (ssize_t) image->columns; j++)
652 for (i=0; i < MaxPixelChannels; i++)
653 evaluate_pixel[j].channel[i]=0.0;
655 for (j=0; j < (ssize_t) number_images; j++)
657 register const Quantum
660 image_view=AcquireVirtualCacheView(next,exception);
661 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,
663 if (p == (const Quantum *) NULL)
665 image_view=DestroyCacheView(image_view);
668 for (x=0; x < (ssize_t) image->columns; x++)
673 for (i=0; i < (ssize_t) GetPixelChannels(next); i++)
675 PixelChannel channel = GetPixelChannelChannel(image,i);
676 PixelTrait traits = GetPixelChannelTraits(next,channel);
677 PixelTrait evaluate_traits=GetPixelChannelTraits(image,channel);
678 if ((traits == UndefinedPixelTrait) ||
679 (evaluate_traits == UndefinedPixelTrait))
681 if ((traits & UpdatePixelTrait) == 0)
683 evaluate_pixel[x].channel[i]=ApplyEvaluateOperator(
684 random_info[id],GetPixelChannel(image,channel,p),j == 0 ?
685 AddEvaluateOperator : op,evaluate_pixel[x].channel[i]);
687 p+=GetPixelChannels(next);
689 image_view=DestroyCacheView(image_view);
690 next=GetNextImageInList(next);
692 for (x=0; x < (ssize_t) image->columns; x++)
699 case MeanEvaluateOperator:
701 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
702 evaluate_pixel[x].channel[i]/=(double) number_images;
705 case MultiplyEvaluateOperator:
707 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
712 for (j=0; j < (ssize_t) (number_images-1); j++)
713 evaluate_pixel[x].channel[i]*=QuantumScale;
717 case RootMeanSquareEvaluateOperator:
719 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
720 evaluate_pixel[x].channel[i]=sqrt(evaluate_pixel[x].channel[i]/
728 for (x=0; x < (ssize_t) image->columns; x++)
733 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
735 PixelChannel channel = GetPixelChannelChannel(image,i);
736 PixelTrait traits = GetPixelChannelTraits(image,channel);
737 if (traits == UndefinedPixelTrait)
739 if ((traits & UpdatePixelTrait) == 0)
741 q[i]=ClampToQuantum(evaluate_pixel[x].channel[i]);
743 q+=GetPixelChannels(image);
745 if (SyncCacheViewAuthenticPixels(evaluate_view,exception) == MagickFalse)
747 if (images->progress_monitor != (MagickProgressMonitor) NULL)
752 #if defined(MAGICKCORE_OPENMP_SUPPORT)
753 #pragma omp critical (MagickCore_EvaluateImages)
755 proceed=SetImageProgress(images,EvaluateImageTag,progress++,
757 if (proceed == MagickFalse)
762 evaluate_view=DestroyCacheView(evaluate_view);
763 evaluate_pixels=DestroyPixelThreadSet(evaluate_pixels);
764 random_info=DestroyRandomInfoThreadSet(random_info);
765 if (status == MagickFalse)
766 image=DestroyImage(image);
770 MagickExport MagickBooleanType EvaluateImage(Image *image,
771 const MagickEvaluateOperator op,const double value,ExceptionInfo *exception)
783 **magick_restrict random_info;
788 #if defined(MAGICKCORE_OPENMP_SUPPORT)
793 assert(image != (Image *) NULL);
794 assert(image->signature == MagickCoreSignature);
795 if (image->debug != MagickFalse)
796 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
797 assert(exception != (ExceptionInfo *) NULL);
798 assert(exception->signature == MagickCoreSignature);
799 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
803 random_info=AcquireRandomInfoThreadSet();
804 image_view=AcquireAuthenticCacheView(image,exception);
805 #if defined(MAGICKCORE_OPENMP_SUPPORT)
806 key=GetRandomSecretKey(random_info[0]);
807 #pragma omp parallel for schedule(static) shared(progress,status) \
808 magick_number_threads(image,image,image->rows,key == ~0UL)
810 for (y=0; y < (ssize_t) image->rows; y++)
813 id = GetOpenMPThreadId();
821 if (status == MagickFalse)
823 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
824 if (q == (Quantum *) NULL)
829 for (x=0; x < (ssize_t) image->columns; x++)
837 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
839 PixelChannel channel = GetPixelChannelChannel(image,i);
840 PixelTrait traits = GetPixelChannelTraits(image,channel);
841 if (traits == UndefinedPixelTrait)
843 if ((traits & CopyPixelTrait) != 0)
845 if ((traits & UpdatePixelTrait) == 0)
847 result=ApplyEvaluateOperator(random_info[id],q[i],op,value);
848 if (op == MeanEvaluateOperator)
850 q[i]=ClampToQuantum(result);
852 q+=GetPixelChannels(image);
854 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
856 if (image->progress_monitor != (MagickProgressMonitor) NULL)
861 #if defined(MAGICKCORE_OPENMP_SUPPORT)
862 #pragma omp critical (MagickCore_EvaluateImage)
864 proceed=SetImageProgress(image,EvaluateImageTag,progress++,image->rows);
865 if (proceed == MagickFalse)
869 image_view=DestroyCacheView(image_view);
870 random_info=DestroyRandomInfoThreadSet(random_info);
875 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
879 % F u n c t i o n I m a g e %
883 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
885 % FunctionImage() applies a value to the image with an arithmetic, relational,
886 % or logical operator to an image. Use these operations to lighten or darken
887 % an image, to increase or decrease contrast in an image, or to produce the
888 % "negative" of an image.
890 % The format of the FunctionImage method is:
892 % MagickBooleanType FunctionImage(Image *image,
893 % const MagickFunction function,const ssize_t number_parameters,
894 % const double *parameters,ExceptionInfo *exception)
896 % A description of each parameter follows:
898 % o image: the image.
900 % o function: A channel function.
902 % o parameters: one or more parameters.
904 % o exception: return any errors or warnings in this structure.
908 static Quantum ApplyFunction(Quantum pixel,const MagickFunction function,
909 const size_t number_parameters,const double *parameters,
910 ExceptionInfo *exception)
922 case PolynomialFunction:
925 Polynomial: polynomial constants, highest to lowest order (e.g. c0*x^3+
929 for (i=0; i < (ssize_t) number_parameters; i++)
930 result=result*QuantumScale*pixel+parameters[i];
931 result*=QuantumRange;
934 case SinusoidFunction:
943 Sinusoid: frequency, phase, amplitude, bias.
945 frequency=(number_parameters >= 1) ? parameters[0] : 1.0;
946 phase=(number_parameters >= 2) ? parameters[1] : 0.0;
947 amplitude=(number_parameters >= 3) ? parameters[2] : 0.5;
948 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
949 result=(double) (QuantumRange*(amplitude*sin((double) (2.0*
950 MagickPI*(frequency*QuantumScale*pixel+phase/360.0)))+bias));
962 Arcsin (peged at range limits for invalid results): width, center,
965 width=(number_parameters >= 1) ? parameters[0] : 1.0;
966 center=(number_parameters >= 2) ? parameters[1] : 0.5;
967 range=(number_parameters >= 3) ? parameters[2] : 1.0;
968 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
969 result=2.0/width*(QuantumScale*pixel-center);
970 if ( result <= -1.0 )
971 result=bias-range/2.0;
974 result=bias+range/2.0;
976 result=(double) (range/MagickPI*asin((double) result)+bias);
977 result*=QuantumRange;
989 Arctan: slope, center, range, and bias.
991 slope=(number_parameters >= 1) ? parameters[0] : 1.0;
992 center=(number_parameters >= 2) ? parameters[1] : 0.5;
993 range=(number_parameters >= 3) ? parameters[2] : 1.0;
994 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
995 result=(double) (MagickPI*slope*(QuantumScale*pixel-center));
996 result=(double) (QuantumRange*(range/MagickPI*atan((double)
1000 case UndefinedFunction:
1003 return(ClampToQuantum(result));
1006 MagickExport MagickBooleanType FunctionImage(Image *image,
1007 const MagickFunction function,const size_t number_parameters,
1008 const double *parameters,ExceptionInfo *exception)
1010 #define FunctionImageTag "Function/Image "
1024 assert(image != (Image *) NULL);
1025 assert(image->signature == MagickCoreSignature);
1026 if (image->debug != MagickFalse)
1027 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1028 assert(exception != (ExceptionInfo *) NULL);
1029 assert(exception->signature == MagickCoreSignature);
1030 #if defined(MAGICKCORE_OPENCL_SUPPORT)
1031 if (AccelerateFunctionImage(image,function,number_parameters,parameters,
1032 exception) != MagickFalse)
1035 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1036 return(MagickFalse);
1039 image_view=AcquireAuthenticCacheView(image,exception);
1040 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1041 #pragma omp parallel for schedule(static) shared(progress,status) \
1042 magick_number_threads(image,image,image->rows,1)
1044 for (y=0; y < (ssize_t) image->rows; y++)
1052 if (status == MagickFalse)
1054 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1055 if (q == (Quantum *) NULL)
1060 for (x=0; x < (ssize_t) image->columns; x++)
1065 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1067 PixelChannel channel = GetPixelChannelChannel(image,i);
1068 PixelTrait traits = GetPixelChannelTraits(image,channel);
1069 if (traits == UndefinedPixelTrait)
1071 if ((traits & UpdatePixelTrait) == 0)
1073 q[i]=ApplyFunction(q[i],function,number_parameters,parameters,
1076 q+=GetPixelChannels(image);
1078 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1080 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1085 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1086 #pragma omp critical (MagickCore_FunctionImage)
1088 proceed=SetImageProgress(image,FunctionImageTag,progress++,image->rows);
1089 if (proceed == MagickFalse)
1093 image_view=DestroyCacheView(image_view);
1098 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1102 % G e t I m a g e E n t r o p y %
1106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1108 % GetImageEntropy() returns the entropy of one or more image channels.
1110 % The format of the GetImageEntropy method is:
1112 % MagickBooleanType GetImageEntropy(const Image *image,double *entropy,
1113 % ExceptionInfo *exception)
1115 % A description of each parameter follows:
1117 % o image: the image.
1119 % o entropy: the average entropy of the selected channels.
1121 % o exception: return any errors or warnings in this structure.
1124 MagickExport MagickBooleanType GetImageEntropy(const Image *image,
1125 double *entropy,ExceptionInfo *exception)
1128 *channel_statistics;
1130 assert(image != (Image *) NULL);
1131 assert(image->signature == MagickCoreSignature);
1132 if (image->debug != MagickFalse)
1133 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1134 channel_statistics=GetImageStatistics(image,exception);
1135 if (channel_statistics == (ChannelStatistics *) NULL)
1136 return(MagickFalse);
1137 *entropy=channel_statistics[CompositePixelChannel].entropy;
1138 channel_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1139 channel_statistics);
1144 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1148 % G e t I m a g e E x t r e m a %
1152 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1154 % GetImageExtrema() returns the extrema of one or more image channels.
1156 % The format of the GetImageExtrema method is:
1158 % MagickBooleanType GetImageExtrema(const Image *image,size_t *minima,
1159 % size_t *maxima,ExceptionInfo *exception)
1161 % A description of each parameter follows:
1163 % o image: the image.
1165 % o minima: the minimum value in the channel.
1167 % o maxima: the maximum value in the channel.
1169 % o exception: return any errors or warnings in this structure.
1172 MagickExport MagickBooleanType GetImageExtrema(const Image *image,
1173 size_t *minima,size_t *maxima,ExceptionInfo *exception)
1182 assert(image != (Image *) NULL);
1183 assert(image->signature == MagickCoreSignature);
1184 if (image->debug != MagickFalse)
1185 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1186 status=GetImageRange(image,&min,&max,exception);
1187 *minima=(size_t) ceil(min-0.5);
1188 *maxima=(size_t) floor(max+0.5);
1193 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1197 % G e t I m a g e K u r t o s i s %
1201 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1203 % GetImageKurtosis() returns the kurtosis and skewness of one or more image
1206 % The format of the GetImageKurtosis method is:
1208 % MagickBooleanType GetImageKurtosis(const Image *image,double *kurtosis,
1209 % double *skewness,ExceptionInfo *exception)
1211 % A description of each parameter follows:
1213 % o image: the image.
1215 % o kurtosis: the kurtosis of the channel.
1217 % o skewness: the skewness of the channel.
1219 % o exception: return any errors or warnings in this structure.
1222 MagickExport MagickBooleanType GetImageKurtosis(const Image *image,
1223 double *kurtosis,double *skewness,ExceptionInfo *exception)
1226 *channel_statistics;
1228 assert(image != (Image *) NULL);
1229 assert(image->signature == MagickCoreSignature);
1230 if (image->debug != MagickFalse)
1231 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1232 channel_statistics=GetImageStatistics(image,exception);
1233 if (channel_statistics == (ChannelStatistics *) NULL)
1234 return(MagickFalse);
1235 *kurtosis=channel_statistics[CompositePixelChannel].kurtosis;
1236 *skewness=channel_statistics[CompositePixelChannel].skewness;
1237 channel_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1238 channel_statistics);
1243 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1247 % G e t I m a g e M e a n %
1251 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1253 % GetImageMean() returns the mean and standard deviation of one or more image
1256 % The format of the GetImageMean method is:
1258 % MagickBooleanType GetImageMean(const Image *image,double *mean,
1259 % double *standard_deviation,ExceptionInfo *exception)
1261 % A description of each parameter follows:
1263 % o image: the image.
1265 % o mean: the average value in the channel.
1267 % o standard_deviation: the standard deviation of the channel.
1269 % o exception: return any errors or warnings in this structure.
1272 MagickExport MagickBooleanType GetImageMean(const Image *image,double *mean,
1273 double *standard_deviation,ExceptionInfo *exception)
1276 *channel_statistics;
1278 assert(image != (Image *) NULL);
1279 assert(image->signature == MagickCoreSignature);
1280 if (image->debug != MagickFalse)
1281 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1282 channel_statistics=GetImageStatistics(image,exception);
1283 if (channel_statistics == (ChannelStatistics *) NULL)
1284 return(MagickFalse);
1285 *mean=channel_statistics[CompositePixelChannel].mean;
1286 *standard_deviation=
1287 channel_statistics[CompositePixelChannel].standard_deviation;
1288 channel_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1289 channel_statistics);
1294 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1298 % G e t I m a g e M o m e n t s %
1302 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1304 % GetImageMoments() returns the normalized moments of one or more image
1307 % The format of the GetImageMoments method is:
1309 % ChannelMoments *GetImageMoments(const Image *image,
1310 % ExceptionInfo *exception)
1312 % A description of each parameter follows:
1314 % o image: the image.
1316 % o exception: return any errors or warnings in this structure.
1320 static size_t GetImageChannels(const Image *image)
1329 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1331 PixelChannel channel = GetPixelChannelChannel(image,i);
1332 PixelTrait traits = GetPixelChannelTraits(image,channel);
1333 if (traits == UndefinedPixelTrait)
1335 if ((traits & UpdatePixelTrait) == 0)
1339 return((size_t) (channels == 0 ? 1 : channels));
1342 MagickExport ChannelMoments *GetImageMoments(const Image *image,
1343 ExceptionInfo *exception)
1345 #define MaxNumberImageMoments 8
1354 M00[MaxPixelChannels+1],
1355 M01[MaxPixelChannels+1],
1356 M02[MaxPixelChannels+1],
1357 M03[MaxPixelChannels+1],
1358 M10[MaxPixelChannels+1],
1359 M11[MaxPixelChannels+1],
1360 M12[MaxPixelChannels+1],
1361 M20[MaxPixelChannels+1],
1362 M21[MaxPixelChannels+1],
1363 M22[MaxPixelChannels+1],
1364 M30[MaxPixelChannels+1];
1367 centroid[MaxPixelChannels+1];
1373 assert(image != (Image *) NULL);
1374 assert(image->signature == MagickCoreSignature);
1375 if (image->debug != MagickFalse)
1376 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1377 channel_moments=(ChannelMoments *) AcquireQuantumMemory(MaxPixelChannels+1,
1378 sizeof(*channel_moments));
1379 if (channel_moments == (ChannelMoments *) NULL)
1380 return(channel_moments);
1381 (void) memset(channel_moments,0,(MaxPixelChannels+1)*
1382 sizeof(*channel_moments));
1383 (void) memset(centroid,0,sizeof(centroid));
1384 (void) memset(M00,0,sizeof(M00));
1385 (void) memset(M01,0,sizeof(M01));
1386 (void) memset(M02,0,sizeof(M02));
1387 (void) memset(M03,0,sizeof(M03));
1388 (void) memset(M10,0,sizeof(M10));
1389 (void) memset(M11,0,sizeof(M11));
1390 (void) memset(M12,0,sizeof(M12));
1391 (void) memset(M20,0,sizeof(M20));
1392 (void) memset(M21,0,sizeof(M21));
1393 (void) memset(M22,0,sizeof(M22));
1394 (void) memset(M30,0,sizeof(M30));
1395 image_view=AcquireVirtualCacheView(image,exception);
1396 for (y=0; y < (ssize_t) image->rows; y++)
1398 register const Quantum
1405 Compute center of mass (centroid).
1407 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1408 if (p == (const Quantum *) NULL)
1410 for (x=0; x < (ssize_t) image->columns; x++)
1415 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1417 PixelChannel channel = GetPixelChannelChannel(image,i);
1418 PixelTrait traits = GetPixelChannelTraits(image,channel);
1419 if (traits == UndefinedPixelTrait)
1421 if ((traits & UpdatePixelTrait) == 0)
1423 M00[channel]+=QuantumScale*p[i];
1424 M00[MaxPixelChannels]+=QuantumScale*p[i];
1425 M10[channel]+=x*QuantumScale*p[i];
1426 M10[MaxPixelChannels]+=x*QuantumScale*p[i];
1427 M01[channel]+=y*QuantumScale*p[i];
1428 M01[MaxPixelChannels]+=y*QuantumScale*p[i];
1430 p+=GetPixelChannels(image);
1433 for (channel=0; channel <= MaxPixelChannels; channel++)
1436 Compute center of mass (centroid).
1438 if (M00[channel] < MagickEpsilon)
1440 M00[channel]+=MagickEpsilon;
1441 centroid[channel].x=(double) image->columns/2.0;
1442 centroid[channel].y=(double) image->rows/2.0;
1445 M00[channel]+=MagickEpsilon;
1446 centroid[channel].x=M10[channel]/M00[channel];
1447 centroid[channel].y=M01[channel]/M00[channel];
1449 for (y=0; y < (ssize_t) image->rows; y++)
1451 register const Quantum
1458 Compute the image moments.
1460 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1461 if (p == (const Quantum *) NULL)
1463 for (x=0; x < (ssize_t) image->columns; x++)
1468 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1470 PixelChannel channel = GetPixelChannelChannel(image,i);
1471 PixelTrait traits = GetPixelChannelTraits(image,channel);
1472 if (traits == UndefinedPixelTrait)
1474 if ((traits & UpdatePixelTrait) == 0)
1476 M11[channel]+=(x-centroid[channel].x)*(y-centroid[channel].y)*
1478 M11[MaxPixelChannels]+=(x-centroid[channel].x)*(y-centroid[channel].y)*
1480 M20[channel]+=(x-centroid[channel].x)*(x-centroid[channel].x)*
1482 M20[MaxPixelChannels]+=(x-centroid[channel].x)*(x-centroid[channel].x)*
1484 M02[channel]+=(y-centroid[channel].y)*(y-centroid[channel].y)*
1486 M02[MaxPixelChannels]+=(y-centroid[channel].y)*(y-centroid[channel].y)*
1488 M21[channel]+=(x-centroid[channel].x)*(x-centroid[channel].x)*
1489 (y-centroid[channel].y)*QuantumScale*p[i];
1490 M21[MaxPixelChannels]+=(x-centroid[channel].x)*(x-centroid[channel].x)*
1491 (y-centroid[channel].y)*QuantumScale*p[i];
1492 M12[channel]+=(x-centroid[channel].x)*(y-centroid[channel].y)*
1493 (y-centroid[channel].y)*QuantumScale*p[i];
1494 M12[MaxPixelChannels]+=(x-centroid[channel].x)*(y-centroid[channel].y)*
1495 (y-centroid[channel].y)*QuantumScale*p[i];
1496 M22[channel]+=(x-centroid[channel].x)*(x-centroid[channel].x)*
1497 (y-centroid[channel].y)*(y-centroid[channel].y)*QuantumScale*p[i];
1498 M22[MaxPixelChannels]+=(x-centroid[channel].x)*(x-centroid[channel].x)*
1499 (y-centroid[channel].y)*(y-centroid[channel].y)*QuantumScale*p[i];
1500 M30[channel]+=(x-centroid[channel].x)*(x-centroid[channel].x)*
1501 (x-centroid[channel].x)*QuantumScale*p[i];
1502 M30[MaxPixelChannels]+=(x-centroid[channel].x)*(x-centroid[channel].x)*
1503 (x-centroid[channel].x)*QuantumScale*p[i];
1504 M03[channel]+=(y-centroid[channel].y)*(y-centroid[channel].y)*
1505 (y-centroid[channel].y)*QuantumScale*p[i];
1506 M03[MaxPixelChannels]+=(y-centroid[channel].y)*(y-centroid[channel].y)*
1507 (y-centroid[channel].y)*QuantumScale*p[i];
1509 p+=GetPixelChannels(image);
1512 M00[MaxPixelChannels]/=GetImageChannels(image);
1513 M01[MaxPixelChannels]/=GetImageChannels(image);
1514 M02[MaxPixelChannels]/=GetImageChannels(image);
1515 M03[MaxPixelChannels]/=GetImageChannels(image);
1516 M10[MaxPixelChannels]/=GetImageChannels(image);
1517 M11[MaxPixelChannels]/=GetImageChannels(image);
1518 M12[MaxPixelChannels]/=GetImageChannels(image);
1519 M20[MaxPixelChannels]/=GetImageChannels(image);
1520 M21[MaxPixelChannels]/=GetImageChannels(image);
1521 M22[MaxPixelChannels]/=GetImageChannels(image);
1522 M30[MaxPixelChannels]/=GetImageChannels(image);
1523 for (channel=0; channel <= MaxPixelChannels; channel++)
1526 Compute elliptical angle, major and minor axes, eccentricity, & intensity.
1528 channel_moments[channel].centroid=centroid[channel];
1529 channel_moments[channel].ellipse_axis.x=sqrt((2.0/M00[channel])*
1530 ((M20[channel]+M02[channel])+sqrt(4.0*M11[channel]*M11[channel]+
1531 (M20[channel]-M02[channel])*(M20[channel]-M02[channel]))));
1532 channel_moments[channel].ellipse_axis.y=sqrt((2.0/M00[channel])*
1533 ((M20[channel]+M02[channel])-sqrt(4.0*M11[channel]*M11[channel]+
1534 (M20[channel]-M02[channel])*(M20[channel]-M02[channel]))));
1535 channel_moments[channel].ellipse_angle=RadiansToDegrees(0.5*atan(2.0*
1536 M11[channel]/(M20[channel]-M02[channel]+MagickEpsilon)));
1537 if (fabs(M11[channel]) < MagickEpsilon)
1539 if (fabs(M20[channel]-M02[channel]) < MagickEpsilon)
1540 channel_moments[channel].ellipse_angle+=0.0;
1542 if ((M20[channel]-M02[channel]) < 0.0)
1543 channel_moments[channel].ellipse_angle+=90.0;
1545 channel_moments[channel].ellipse_angle+=0.0;
1548 if (M11[channel] < 0.0)
1550 if (fabs(M20[channel]-M02[channel]) < MagickEpsilon)
1551 channel_moments[channel].ellipse_angle+=0.0;
1553 if ((M20[channel]-M02[channel]) < 0.0)
1554 channel_moments[channel].ellipse_angle+=90.0;
1556 channel_moments[channel].ellipse_angle+=180.0;
1560 if (fabs(M20[channel]-M02[channel]) < MagickEpsilon)
1561 channel_moments[channel].ellipse_angle+=0.0;
1563 if ((M20[channel]-M02[channel]) < 0.0)
1564 channel_moments[channel].ellipse_angle+=90.0;
1566 channel_moments[channel].ellipse_angle+=0.0;
1568 channel_moments[channel].ellipse_eccentricity=sqrt(1.0-(
1569 channel_moments[channel].ellipse_axis.y/
1570 (channel_moments[channel].ellipse_axis.x+MagickEpsilon)));
1571 channel_moments[channel].ellipse_intensity=M00[channel]/
1572 (MagickPI*channel_moments[channel].ellipse_axis.x*
1573 channel_moments[channel].ellipse_axis.y+MagickEpsilon);
1575 for (channel=0; channel <= MaxPixelChannels; channel++)
1578 Normalize image moments.
1582 M11[channel]/=pow(M00[channel],1.0+(1.0+1.0)/2.0);
1583 M20[channel]/=pow(M00[channel],1.0+(2.0+0.0)/2.0);
1584 M02[channel]/=pow(M00[channel],1.0+(0.0+2.0)/2.0);
1585 M21[channel]/=pow(M00[channel],1.0+(2.0+1.0)/2.0);
1586 M12[channel]/=pow(M00[channel],1.0+(1.0+2.0)/2.0);
1587 M22[channel]/=pow(M00[channel],1.0+(2.0+2.0)/2.0);
1588 M30[channel]/=pow(M00[channel],1.0+(3.0+0.0)/2.0);
1589 M03[channel]/=pow(M00[channel],1.0+(0.0+3.0)/2.0);
1592 image_view=DestroyCacheView(image_view);
1593 for (channel=0; channel <= MaxPixelChannels; channel++)
1596 Compute Hu invariant moments.
1598 channel_moments[channel].invariant[0]=M20[channel]+M02[channel];
1599 channel_moments[channel].invariant[1]=(M20[channel]-M02[channel])*
1600 (M20[channel]-M02[channel])+4.0*M11[channel]*M11[channel];
1601 channel_moments[channel].invariant[2]=(M30[channel]-3.0*M12[channel])*
1602 (M30[channel]-3.0*M12[channel])+(3.0*M21[channel]-M03[channel])*
1603 (3.0*M21[channel]-M03[channel]);
1604 channel_moments[channel].invariant[3]=(M30[channel]+M12[channel])*
1605 (M30[channel]+M12[channel])+(M21[channel]+M03[channel])*
1606 (M21[channel]+M03[channel]);
1607 channel_moments[channel].invariant[4]=(M30[channel]-3.0*M12[channel])*
1608 (M30[channel]+M12[channel])*((M30[channel]+M12[channel])*
1609 (M30[channel]+M12[channel])-3.0*(M21[channel]+M03[channel])*
1610 (M21[channel]+M03[channel]))+(3.0*M21[channel]-M03[channel])*
1611 (M21[channel]+M03[channel])*(3.0*(M30[channel]+M12[channel])*
1612 (M30[channel]+M12[channel])-(M21[channel]+M03[channel])*
1613 (M21[channel]+M03[channel]));
1614 channel_moments[channel].invariant[5]=(M20[channel]-M02[channel])*
1615 ((M30[channel]+M12[channel])*(M30[channel]+M12[channel])-
1616 (M21[channel]+M03[channel])*(M21[channel]+M03[channel]))+
1617 4.0*M11[channel]*(M30[channel]+M12[channel])*(M21[channel]+M03[channel]);
1618 channel_moments[channel].invariant[6]=(3.0*M21[channel]-M03[channel])*
1619 (M30[channel]+M12[channel])*((M30[channel]+M12[channel])*
1620 (M30[channel]+M12[channel])-3.0*(M21[channel]+M03[channel])*
1621 (M21[channel]+M03[channel]))-(M30[channel]-3*M12[channel])*
1622 (M21[channel]+M03[channel])*(3.0*(M30[channel]+M12[channel])*
1623 (M30[channel]+M12[channel])-(M21[channel]+M03[channel])*
1624 (M21[channel]+M03[channel]));
1625 channel_moments[channel].invariant[7]=M11[channel]*((M30[channel]+
1626 M12[channel])*(M30[channel]+M12[channel])-(M03[channel]+M21[channel])*
1627 (M03[channel]+M21[channel]))-(M20[channel]-M02[channel])*
1628 (M30[channel]+M12[channel])*(M03[channel]+M21[channel]);
1630 if (y < (ssize_t) image->rows)
1631 channel_moments=(ChannelMoments *) RelinquishMagickMemory(channel_moments);
1632 return(channel_moments);
1636 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1640 % G e t I m a g e C h a n n e l P e r c e p t u a l H a s h %
1644 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1646 % GetImagePerceptualHash() returns the perceptual hash of one or more
1649 % The format of the GetImagePerceptualHash method is:
1651 % ChannelPerceptualHash *GetImagePerceptualHash(const Image *image,
1652 % ExceptionInfo *exception)
1654 % A description of each parameter follows:
1656 % o image: the image.
1658 % o exception: return any errors or warnings in this structure.
1662 static inline double MagickLog10(const double x)
1664 #define Log10Epsilon (1.0e-11)
1666 if (fabs(x) < Log10Epsilon)
1667 return(log10(Log10Epsilon));
1668 return(log10(fabs(x)));
1671 MagickExport ChannelPerceptualHash *GetImagePerceptualHash(const Image *image,
1672 ExceptionInfo *exception)
1674 ChannelPerceptualHash
1693 perceptual_hash=(ChannelPerceptualHash *) AcquireQuantumMemory(
1694 MaxPixelChannels+1UL,sizeof(*perceptual_hash));
1695 if (perceptual_hash == (ChannelPerceptualHash *) NULL)
1696 return((ChannelPerceptualHash *) NULL);
1697 artifact=GetImageArtifact(image,"phash:colorspaces");
1698 if (artifact != NULL)
1699 colorspaces=AcquireString(artifact);
1701 colorspaces=AcquireString("sRGB,HCLp");
1702 perceptual_hash[0].number_colorspaces=0;
1703 perceptual_hash[0].number_channels=0;
1705 for (i=0; (p=StringToken(",",&q)) != (char *) NULL; i++)
1720 if (i >= MaximumNumberOfPerceptualColorspaces)
1722 colorspace=ParseCommandOption(MagickColorspaceOptions,MagickFalse,p);
1725 perceptual_hash[0].colorspace[i]=(ColorspaceType) colorspace;
1726 hash_image=BlurImage(image,0.0,1.0,exception);
1727 if (hash_image == (Image *) NULL)
1729 hash_image->depth=8;
1730 status=TransformImageColorspace(hash_image,(ColorspaceType) colorspace,
1732 if (status == MagickFalse)
1734 moments=GetImageMoments(hash_image,exception);
1735 perceptual_hash[0].number_colorspaces++;
1736 perceptual_hash[0].number_channels+=GetImageChannels(hash_image);
1737 hash_image=DestroyImage(hash_image);
1738 if (moments == (ChannelMoments *) NULL)
1740 for (channel=0; channel <= MaxPixelChannels; channel++)
1741 for (j=0; j < MaximumNumberOfImageMoments; j++)
1742 perceptual_hash[channel].phash[i][j]=
1743 (-MagickLog10(moments[channel].invariant[j]));
1744 moments=(ChannelMoments *) RelinquishMagickMemory(moments);
1746 colorspaces=DestroyString(colorspaces);
1747 return(perceptual_hash);
1751 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1755 % G e t I m a g e R a n g e %
1759 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1761 % GetImageRange() returns the range of one or more image channels.
1763 % The format of the GetImageRange method is:
1765 % MagickBooleanType GetImageRange(const Image *image,double *minima,
1766 % double *maxima,ExceptionInfo *exception)
1768 % A description of each parameter follows:
1770 % o image: the image.
1772 % o minima: the minimum value in the channel.
1774 % o maxima: the maximum value in the channel.
1776 % o exception: return any errors or warnings in this structure.
1779 MagickExport MagickBooleanType GetImageRange(const Image *image,double *minima,
1780 double *maxima,ExceptionInfo *exception)
1792 assert(image != (Image *) NULL);
1793 assert(image->signature == MagickCoreSignature);
1794 if (image->debug != MagickFalse)
1795 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1797 initialize=MagickTrue;
1800 image_view=AcquireVirtualCacheView(image,exception);
1801 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1802 #pragma omp parallel for schedule(static) shared(status,initialize) \
1803 magick_number_threads(image,image,image->rows,1)
1805 for (y=0; y < (ssize_t) image->rows; y++)
1814 register const Quantum
1820 if (status == MagickFalse)
1822 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1823 if (p == (const Quantum *) NULL)
1828 row_initialize=MagickTrue;
1829 for (x=0; x < (ssize_t) image->columns; x++)
1834 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1836 PixelChannel channel = GetPixelChannelChannel(image,i);
1837 PixelTrait traits = GetPixelChannelTraits(image,channel);
1838 if (traits == UndefinedPixelTrait)
1840 if ((traits & UpdatePixelTrait) == 0)
1842 if (row_initialize != MagickFalse)
1844 row_minima=(double) p[i];
1845 row_maxima=(double) p[i];
1846 row_initialize=MagickFalse;
1850 if ((double) p[i] < row_minima)
1851 row_minima=(double) p[i];
1852 if ((double) p[i] > row_maxima)
1853 row_maxima=(double) p[i];
1856 p+=GetPixelChannels(image);
1858 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1859 #pragma omp critical (MagickCore_GetImageRange)
1862 if (initialize != MagickFalse)
1866 initialize=MagickFalse;
1870 if (row_minima < *minima)
1872 if (row_maxima > *maxima)
1877 image_view=DestroyCacheView(image_view);
1882 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1886 % G e t I m a g e S t a t i s t i c s %
1890 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1892 % GetImageStatistics() returns statistics for each channel in the image. The
1893 % statistics include the channel depth, its minima, maxima, mean, standard
1894 % deviation, kurtosis and skewness. You can access the red channel mean, for
1895 % example, like this:
1897 % channel_statistics=GetImageStatistics(image,exception);
1898 % red_mean=channel_statistics[RedPixelChannel].mean;
1900 % Use MagickRelinquishMemory() to free the statistics buffer.
1902 % The format of the GetImageStatistics method is:
1904 % ChannelStatistics *GetImageStatistics(const Image *image,
1905 % ExceptionInfo *exception)
1907 % A description of each parameter follows:
1909 % o image: the image.
1911 % o exception: return any errors or warnings in this structure.
1914 MagickExport ChannelStatistics *GetImageStatistics(const Image *image,
1915 ExceptionInfo *exception)
1918 *channel_statistics;
1940 assert(image != (Image *) NULL);
1941 assert(image->signature == MagickCoreSignature);
1942 if (image->debug != MagickFalse)
1943 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1944 histogram=(double *) AcquireQuantumMemory(MaxMap+1UL,GetPixelChannels(image)*
1945 sizeof(*histogram));
1946 channel_statistics=(ChannelStatistics *) AcquireQuantumMemory(
1947 MaxPixelChannels+1,sizeof(*channel_statistics));
1948 if ((channel_statistics == (ChannelStatistics *) NULL) ||
1949 (histogram == (double *) NULL))
1951 if (histogram != (double *) NULL)
1952 histogram=(double *) RelinquishMagickMemory(histogram);
1953 if (channel_statistics != (ChannelStatistics *) NULL)
1954 channel_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1955 channel_statistics);
1956 return(channel_statistics);
1958 (void) memset(channel_statistics,0,(MaxPixelChannels+1)*
1959 sizeof(*channel_statistics));
1960 for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
1962 channel_statistics[i].depth=1;
1963 channel_statistics[i].maxima=(-MagickMaximumValue);
1964 channel_statistics[i].minima=MagickMaximumValue;
1966 (void) memset(histogram,0,(MaxMap+1)*GetPixelChannels(image)*
1967 sizeof(*histogram));
1968 for (y=0; y < (ssize_t) image->rows; y++)
1970 register const Quantum
1977 Compute pixel statistics.
1979 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1980 if (p == (const Quantum *) NULL)
1982 for (x=0; x < (ssize_t) image->columns; x++)
1987 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1989 PixelChannel channel = GetPixelChannelChannel(image,i);
1990 PixelTrait traits = GetPixelChannelTraits(image,channel);
1991 if (traits == UndefinedPixelTrait)
1993 if ((traits & UpdatePixelTrait) == 0)
1995 if (channel_statistics[channel].depth != MAGICKCORE_QUANTUM_DEPTH)
1997 depth=channel_statistics[channel].depth;
1998 range=GetQuantumRange(depth);
1999 status=p[i] != ScaleAnyToQuantum(ScaleQuantumToAny(p[i],range),
2000 range) ? MagickTrue : MagickFalse;
2001 if (status != MagickFalse)
2003 channel_statistics[channel].depth++;
2008 if ((double) p[i] < channel_statistics[channel].minima)
2009 channel_statistics[channel].minima=(double) p[i];
2010 if ((double) p[i] > channel_statistics[channel].maxima)
2011 channel_statistics[channel].maxima=(double) p[i];
2012 channel_statistics[channel].sum+=p[i];
2013 channel_statistics[channel].sum_squared+=(double) p[i]*p[i];
2014 channel_statistics[channel].sum_cubed+=(double) p[i]*p[i]*p[i];
2015 channel_statistics[channel].sum_fourth_power+=(double) p[i]*p[i]*p[i]*
2017 channel_statistics[channel].area++;
2018 if ((double) p[i] < channel_statistics[CompositePixelChannel].minima)
2019 channel_statistics[CompositePixelChannel].minima=(double) p[i];
2020 if ((double) p[i] > channel_statistics[CompositePixelChannel].maxima)
2021 channel_statistics[CompositePixelChannel].maxima=(double) p[i];
2022 histogram[GetPixelChannels(image)*ScaleQuantumToMap(
2023 ClampToQuantum((double) p[i]))+i]++;
2024 channel_statistics[CompositePixelChannel].sum+=(double) p[i];
2025 channel_statistics[CompositePixelChannel].sum_squared+=(double)
2027 channel_statistics[CompositePixelChannel].sum_cubed+=(double)
2029 channel_statistics[CompositePixelChannel].sum_fourth_power+=(double)
2030 p[i]*p[i]*p[i]*p[i];
2031 channel_statistics[CompositePixelChannel].area++;
2033 p+=GetPixelChannels(image);
2036 for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
2039 Normalize pixel statistics.
2041 area=PerceptibleReciprocal(channel_statistics[i].area);
2042 channel_statistics[i].sum*=area;
2043 channel_statistics[i].sum_squared*=area;
2044 channel_statistics[i].sum_cubed*=area;
2045 channel_statistics[i].sum_fourth_power*=area;
2046 channel_statistics[i].mean=channel_statistics[i].sum;
2047 channel_statistics[i].variance=channel_statistics[i].sum_squared;
2048 standard_deviation=sqrt(channel_statistics[i].variance-
2049 (channel_statistics[i].mean*channel_statistics[i].mean));
2050 standard_deviation=sqrt(PerceptibleReciprocal(channel_statistics[i].area-
2051 1.0)*channel_statistics[i].area*standard_deviation*standard_deviation);
2052 channel_statistics[i].standard_deviation=standard_deviation;
2054 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2063 Compute pixel entropy.
2065 PixelChannel channel = GetPixelChannelChannel(image,i);
2067 for (j=0; j <= (ssize_t) MaxMap; j++)
2068 if (histogram[GetPixelChannels(image)*j+i] > 0.0)
2070 area=PerceptibleReciprocal(channel_statistics[channel].area);
2071 for (j=0; j <= (ssize_t) MaxMap; j++)
2076 count=area*histogram[GetPixelChannels(image)*j+i];
2077 channel_statistics[channel].entropy+=-count*MagickLog10(count)*
2078 PerceptibleReciprocal(MagickLog10(number_bins));
2079 channel_statistics[CompositePixelChannel].entropy+=-count*
2080 MagickLog10(count)*PerceptibleReciprocal(MagickLog10(number_bins))/
2081 GetPixelChannels(image);
2084 histogram=(double *) RelinquishMagickMemory(histogram);
2085 for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
2088 Compute kurtosis & skewness statistics.
2090 standard_deviation=PerceptibleReciprocal(
2091 channel_statistics[i].standard_deviation);
2092 channel_statistics[i].skewness=(channel_statistics[i].sum_cubed-3.0*
2093 channel_statistics[i].mean*channel_statistics[i].sum_squared+2.0*
2094 channel_statistics[i].mean*channel_statistics[i].mean*
2095 channel_statistics[i].mean)*(standard_deviation*standard_deviation*
2096 standard_deviation);
2097 channel_statistics[i].kurtosis=(channel_statistics[i].sum_fourth_power-4.0*
2098 channel_statistics[i].mean*channel_statistics[i].sum_cubed+6.0*
2099 channel_statistics[i].mean*channel_statistics[i].mean*
2100 channel_statistics[i].sum_squared-3.0*channel_statistics[i].mean*
2101 channel_statistics[i].mean*1.0*channel_statistics[i].mean*
2102 channel_statistics[i].mean)*(standard_deviation*standard_deviation*
2103 standard_deviation*standard_deviation)-3.0;
2105 channel_statistics[CompositePixelChannel].mean=0.0;
2106 channel_statistics[CompositePixelChannel].standard_deviation=0.0;
2107 channel_statistics[CompositePixelChannel].entropy=0.0;
2108 for (i=0; i < (ssize_t) MaxPixelChannels; i++)
2110 channel_statistics[CompositePixelChannel].mean+=
2111 channel_statistics[i].mean;
2112 channel_statistics[CompositePixelChannel].standard_deviation+=
2113 channel_statistics[i].standard_deviation;
2114 channel_statistics[CompositePixelChannel].entropy+=
2115 channel_statistics[i].entropy;
2117 channel_statistics[CompositePixelChannel].mean/=(double)
2118 GetImageChannels(image);
2119 channel_statistics[CompositePixelChannel].standard_deviation/=(double)
2120 GetImageChannels(image);
2121 channel_statistics[CompositePixelChannel].entropy/=(double)
2122 GetImageChannels(image);
2123 if (y < (ssize_t) image->rows)
2124 channel_statistics=(ChannelStatistics *) RelinquishMagickMemory(
2125 channel_statistics);
2126 return(channel_statistics);
2130 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2134 % P o l y n o m i a l I m a g e %
2138 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2140 % PolynomialImage() returns a new image where each pixel is the sum of the
2141 % pixels in the image sequence after applying its corresponding terms
2142 % (coefficient and degree pairs).
2144 % The format of the PolynomialImage method is:
2146 % Image *PolynomialImage(const Image *images,const size_t number_terms,
2147 % const double *terms,ExceptionInfo *exception)
2149 % A description of each parameter follows:
2151 % o images: the image sequence.
2153 % o number_terms: the number of terms in the list. The actual list length
2154 % is 2 x number_terms + 1 (the constant).
2156 % o terms: the list of polynomial coefficients and degree pairs and a
2159 % o exception: return any errors or warnings in this structure.
2163 MagickExport Image *PolynomialImage(const Image *images,
2164 const size_t number_terms,const double *terms,ExceptionInfo *exception)
2166 #define PolynomialImageTag "Polynomial/Image"
2181 **magick_restrict polynomial_pixels;
2189 assert(images != (Image *) NULL);
2190 assert(images->signature == MagickCoreSignature);
2191 if (images->debug != MagickFalse)
2192 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
2193 assert(exception != (ExceptionInfo *) NULL);
2194 assert(exception->signature == MagickCoreSignature);
2195 image=AcquireImageCanvas(images,exception);
2196 if (image == (Image *) NULL)
2197 return((Image *) NULL);
2198 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
2200 image=DestroyImage(image);
2201 return((Image *) NULL);
2203 number_images=GetImageListLength(images);
2204 polynomial_pixels=AcquirePixelThreadSet(images);
2205 if (polynomial_pixels == (PixelChannels **) NULL)
2207 image=DestroyImage(image);
2208 (void) ThrowMagickException(exception,GetMagickModule(),
2209 ResourceLimitError,"MemoryAllocationFailed","`%s'",images->filename);
2210 return((Image *) NULL);
2213 Polynomial image pixels.
2217 polynomial_view=AcquireAuthenticCacheView(image,exception);
2218 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2219 #pragma omp parallel for schedule(static) shared(progress,status) \
2220 magick_number_threads(image,image,image->rows,1)
2222 for (y=0; y < (ssize_t) image->rows; y++)
2231 id = GetOpenMPThreadId();
2237 register PixelChannels
2246 if (status == MagickFalse)
2248 q=QueueCacheViewAuthenticPixels(polynomial_view,0,y,image->columns,1,
2250 if (q == (Quantum *) NULL)
2255 polynomial_pixel=polynomial_pixels[id];
2256 for (j=0; j < (ssize_t) image->columns; j++)
2257 for (i=0; i < MaxPixelChannels; i++)
2258 polynomial_pixel[j].channel[i]=0.0;
2260 for (j=0; j < (ssize_t) number_images; j++)
2262 register const Quantum
2265 if (j >= (ssize_t) number_terms)
2267 image_view=AcquireVirtualCacheView(next,exception);
2268 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2269 if (p == (const Quantum *) NULL)
2271 image_view=DestroyCacheView(image_view);
2274 for (x=0; x < (ssize_t) image->columns; x++)
2279 for (i=0; i < (ssize_t) GetPixelChannels(next); i++)
2285 PixelChannel channel = GetPixelChannelChannel(image,i);
2286 PixelTrait traits = GetPixelChannelTraits(next,channel);
2287 PixelTrait polynomial_traits=GetPixelChannelTraits(image,channel);
2288 if ((traits == UndefinedPixelTrait) ||
2289 (polynomial_traits == UndefinedPixelTrait))
2291 if ((traits & UpdatePixelTrait) == 0)
2293 coefficient=(MagickRealType) terms[2*j];
2294 degree=(MagickRealType) terms[(j << 1)+1];
2295 polynomial_pixel[x].channel[i]+=coefficient*
2296 pow(QuantumScale*GetPixelChannel(image,channel,p),degree);
2298 p+=GetPixelChannels(next);
2300 image_view=DestroyCacheView(image_view);
2301 next=GetNextImageInList(next);
2303 for (x=0; x < (ssize_t) image->columns; x++)
2308 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2310 PixelChannel channel = GetPixelChannelChannel(image,i);
2311 PixelTrait traits = GetPixelChannelTraits(image,channel);
2312 if (traits == UndefinedPixelTrait)
2314 if ((traits & UpdatePixelTrait) == 0)
2316 q[i]=ClampToQuantum(QuantumRange*polynomial_pixel[x].channel[i]);
2318 q+=GetPixelChannels(image);
2320 if (SyncCacheViewAuthenticPixels(polynomial_view,exception) == MagickFalse)
2322 if (images->progress_monitor != (MagickProgressMonitor) NULL)
2327 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2328 #pragma omp critical (MagickCore_PolynomialImages)
2330 proceed=SetImageProgress(images,PolynomialImageTag,progress++,
2332 if (proceed == MagickFalse)
2336 polynomial_view=DestroyCacheView(polynomial_view);
2337 polynomial_pixels=DestroyPixelThreadSet(polynomial_pixels);
2338 if (status == MagickFalse)
2339 image=DestroyImage(image);
2344 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2348 % S t a t i s t i c I m a g e %
2352 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2354 % StatisticImage() makes each pixel the min / max / median / mode / etc. of
2355 % the neighborhood of the specified width and height.
2357 % The format of the StatisticImage method is:
2359 % Image *StatisticImage(const Image *image,const StatisticType type,
2360 % const size_t width,const size_t height,ExceptionInfo *exception)
2362 % A description of each parameter follows:
2364 % o image: the image.
2366 % o type: the statistic type (median, mode, etc.).
2368 % o width: the width of the pixel neighborhood.
2370 % o height: the height of the pixel neighborhood.
2372 % o exception: return any errors or warnings in this structure.
2376 typedef struct _SkipNode
2384 typedef struct _SkipList
2393 typedef struct _PixelList
2406 static PixelList *DestroyPixelList(PixelList *pixel_list)
2408 if (pixel_list == (PixelList *) NULL)
2409 return((PixelList *) NULL);
2410 if (pixel_list->skip_list.nodes != (SkipNode *) NULL)
2411 pixel_list->skip_list.nodes=(SkipNode *) RelinquishAlignedMemory(
2412 pixel_list->skip_list.nodes);
2413 pixel_list=(PixelList *) RelinquishMagickMemory(pixel_list);
2417 static PixelList **DestroyPixelListThreadSet(PixelList **pixel_list)
2422 assert(pixel_list != (PixelList **) NULL);
2423 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
2424 if (pixel_list[i] != (PixelList *) NULL)
2425 pixel_list[i]=DestroyPixelList(pixel_list[i]);
2426 pixel_list=(PixelList **) RelinquishMagickMemory(pixel_list);
2430 static PixelList *AcquirePixelList(const size_t width,const size_t height)
2435 pixel_list=(PixelList *) AcquireMagickMemory(sizeof(*pixel_list));
2436 if (pixel_list == (PixelList *) NULL)
2438 (void) memset((void *) pixel_list,0,sizeof(*pixel_list));
2439 pixel_list->length=width*height;
2440 pixel_list->skip_list.nodes=(SkipNode *) AcquireAlignedMemory(65537UL,
2441 sizeof(*pixel_list->skip_list.nodes));
2442 if (pixel_list->skip_list.nodes == (SkipNode *) NULL)
2443 return(DestroyPixelList(pixel_list));
2444 (void) memset(pixel_list->skip_list.nodes,0,65537UL*
2445 sizeof(*pixel_list->skip_list.nodes));
2446 pixel_list->signature=MagickCoreSignature;
2450 static PixelList **AcquirePixelListThreadSet(const size_t width,
2451 const size_t height)
2462 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
2463 pixel_list=(PixelList **) AcquireQuantumMemory(number_threads,
2464 sizeof(*pixel_list));
2465 if (pixel_list == (PixelList **) NULL)
2466 return((PixelList **) NULL);
2467 (void) memset(pixel_list,0,number_threads*sizeof(*pixel_list));
2468 for (i=0; i < (ssize_t) number_threads; i++)
2470 pixel_list[i]=AcquirePixelList(width,height);
2471 if (pixel_list[i] == (PixelList *) NULL)
2472 return(DestroyPixelListThreadSet(pixel_list));
2477 static void AddNodePixelList(PixelList *pixel_list,const size_t color)
2490 Initialize the node.
2492 p=(&pixel_list->skip_list);
2493 p->nodes[color].signature=pixel_list->signature;
2494 p->nodes[color].count=1;
2496 Determine where it belongs in the list.
2499 for (level=p->level; level >= 0; level--)
2501 while (p->nodes[search].next[level] < color)
2502 search=p->nodes[search].next[level];
2503 update[level]=search;
2506 Generate a pseudo-random level for this node.
2508 for (level=0; ; level++)
2510 pixel_list->seed=(pixel_list->seed*42893621L)+1L;
2511 if ((pixel_list->seed & 0x300) != 0x300)
2516 if (level > (p->level+2))
2519 If we're raising the list's level, link back to the root node.
2521 while (level > p->level)
2524 update[p->level]=65536UL;
2527 Link the node into the skip-list.
2531 p->nodes[color].next[level]=p->nodes[update[level]].next[level];
2532 p->nodes[update[level]].next[level]=color;
2533 } while (level-- > 0);
2536 static inline void GetMaximumPixelList(PixelList *pixel_list,Quantum *pixel)
2549 Find the maximum value for each of the color.
2551 p=(&pixel_list->skip_list);
2554 maximum=p->nodes[color].next[0];
2557 color=p->nodes[color].next[0];
2558 if (color > maximum)
2560 count+=p->nodes[color].count;
2561 } while (count < (ssize_t) pixel_list->length);
2562 *pixel=ScaleShortToQuantum((unsigned short) maximum);
2565 static inline void GetMeanPixelList(PixelList *pixel_list,Quantum *pixel)
2580 Find the mean value for each of the color.
2582 p=(&pixel_list->skip_list);
2588 color=p->nodes[color].next[0];
2589 sum+=(double) p->nodes[color].count*color;
2590 count+=p->nodes[color].count;
2591 } while (count < (ssize_t) pixel_list->length);
2592 sum/=pixel_list->length;
2593 *pixel=ScaleShortToQuantum((unsigned short) sum);
2596 static inline void GetMedianPixelList(PixelList *pixel_list,Quantum *pixel)
2608 Find the median value for each of the color.
2610 p=(&pixel_list->skip_list);
2615 color=p->nodes[color].next[0];
2616 count+=p->nodes[color].count;
2617 } while (count <= (ssize_t) (pixel_list->length >> 1));
2618 *pixel=ScaleShortToQuantum((unsigned short) color);
2621 static inline void GetMinimumPixelList(PixelList *pixel_list,Quantum *pixel)
2634 Find the minimum value for each of the color.
2636 p=(&pixel_list->skip_list);
2639 minimum=p->nodes[color].next[0];
2642 color=p->nodes[color].next[0];
2643 if (color < minimum)
2645 count+=p->nodes[color].count;
2646 } while (count < (ssize_t) pixel_list->length);
2647 *pixel=ScaleShortToQuantum((unsigned short) minimum);
2650 static inline void GetModePixelList(PixelList *pixel_list,Quantum *pixel)
2664 Make each pixel the 'predominant color' of the specified neighborhood.
2666 p=(&pixel_list->skip_list);
2669 max_count=p->nodes[mode].count;
2673 color=p->nodes[color].next[0];
2674 if (p->nodes[color].count > max_count)
2677 max_count=p->nodes[mode].count;
2679 count+=p->nodes[color].count;
2680 } while (count < (ssize_t) pixel_list->length);
2681 *pixel=ScaleShortToQuantum((unsigned short) mode);
2684 static inline void GetNonpeakPixelList(PixelList *pixel_list,Quantum *pixel)
2698 Finds the non peak value for each of the colors.
2700 p=(&pixel_list->skip_list);
2702 next=p->nodes[color].next[0];
2708 next=p->nodes[color].next[0];
2709 count+=p->nodes[color].count;
2710 } while (count <= (ssize_t) (pixel_list->length >> 1));
2711 if ((previous == 65536UL) && (next != 65536UL))
2714 if ((previous != 65536UL) && (next == 65536UL))
2716 *pixel=ScaleShortToQuantum((unsigned short) color);
2719 static inline void GetRootMeanSquarePixelList(PixelList *pixel_list,
2735 Find the root mean square value for each of the color.
2737 p=(&pixel_list->skip_list);
2743 color=p->nodes[color].next[0];
2744 sum+=(double) (p->nodes[color].count*color*color);
2745 count+=p->nodes[color].count;
2746 } while (count < (ssize_t) pixel_list->length);
2747 sum/=pixel_list->length;
2748 *pixel=ScaleShortToQuantum((unsigned short) sqrt(sum));
2751 static inline void GetStandardDeviationPixelList(PixelList *pixel_list,
2768 Find the standard-deviation value for each of the color.
2770 p=(&pixel_list->skip_list);
2780 color=p->nodes[color].next[0];
2781 sum+=(double) p->nodes[color].count*color;
2782 for (i=0; i < (ssize_t) p->nodes[color].count; i++)
2783 sum_squared+=((double) color)*((double) color);
2784 count+=p->nodes[color].count;
2785 } while (count < (ssize_t) pixel_list->length);
2786 sum/=pixel_list->length;
2787 sum_squared/=pixel_list->length;
2788 *pixel=ScaleShortToQuantum((unsigned short) sqrt(sum_squared-(sum*sum)));
2791 static inline void InsertPixelList(const Quantum pixel,PixelList *pixel_list)
2799 index=ScaleQuantumToShort(pixel);
2800 signature=pixel_list->skip_list.nodes[index].signature;
2801 if (signature == pixel_list->signature)
2803 pixel_list->skip_list.nodes[index].count++;
2806 AddNodePixelList(pixel_list,index);
2809 static void ResetPixelList(PixelList *pixel_list)
2821 Reset the skip-list.
2823 p=(&pixel_list->skip_list);
2824 root=p->nodes+65536UL;
2826 for (level=0; level < 9; level++)
2827 root->next[level]=65536UL;
2828 pixel_list->seed=pixel_list->signature++;
2831 MagickExport Image *StatisticImage(const Image *image,const StatisticType type,
2832 const size_t width,const size_t height,ExceptionInfo *exception)
2834 #define StatisticImageTag "Statistic/Image"
2850 **magick_restrict pixel_list;
2857 Initialize statistics image attributes.
2859 assert(image != (Image *) NULL);
2860 assert(image->signature == MagickCoreSignature);
2861 if (image->debug != MagickFalse)
2862 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2863 assert(exception != (ExceptionInfo *) NULL);
2864 assert(exception->signature == MagickCoreSignature);
2865 statistic_image=CloneImage(image,0,0,MagickTrue,
2867 if (statistic_image == (Image *) NULL)
2868 return((Image *) NULL);
2869 status=SetImageStorageClass(statistic_image,DirectClass,exception);
2870 if (status == MagickFalse)
2872 statistic_image=DestroyImage(statistic_image);
2873 return((Image *) NULL);
2875 pixel_list=AcquirePixelListThreadSet(MagickMax(width,1),MagickMax(height,1));
2876 if (pixel_list == (PixelList **) NULL)
2878 statistic_image=DestroyImage(statistic_image);
2879 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2882 Make each pixel the min / max / median / mode / etc. of the neighborhood.
2884 center=(ssize_t) GetPixelChannels(image)*(image->columns+MagickMax(width,1))*
2885 (MagickMax(height,1)/2L)+GetPixelChannels(image)*(MagickMax(width,1)/2L);
2888 image_view=AcquireVirtualCacheView(image,exception);
2889 statistic_view=AcquireAuthenticCacheView(statistic_image,exception);
2890 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2891 #pragma omp parallel for schedule(static) shared(progress,status) \
2892 magick_number_threads(image,statistic_image,statistic_image->rows,1)
2894 for (y=0; y < (ssize_t) statistic_image->rows; y++)
2897 id = GetOpenMPThreadId();
2899 register const Quantum
2908 if (status == MagickFalse)
2910 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) MagickMax(width,1)/2L),y-
2911 (ssize_t) (MagickMax(height,1)/2L),image->columns+MagickMax(width,1),
2912 MagickMax(height,1),exception);
2913 q=QueueCacheViewAuthenticPixels(statistic_view,0,y,statistic_image->columns, 1,exception);
2914 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2919 for (x=0; x < (ssize_t) statistic_image->columns; x++)
2924 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2929 register const Quantum
2930 *magick_restrict pixels;
2938 PixelChannel channel = GetPixelChannelChannel(image,i);
2939 PixelTrait traits = GetPixelChannelTraits(image,channel);
2940 PixelTrait statistic_traits=GetPixelChannelTraits(statistic_image,
2942 if ((traits == UndefinedPixelTrait) ||
2943 (statistic_traits == UndefinedPixelTrait))
2945 if (((statistic_traits & CopyPixelTrait) != 0) ||
2946 (GetPixelWriteMask(image,p) <= (QuantumRange/2)))
2948 SetPixelChannel(statistic_image,channel,p[center+i],q);
2951 if ((statistic_traits & UpdatePixelTrait) == 0)
2954 ResetPixelList(pixel_list[id]);
2955 for (v=0; v < (ssize_t) MagickMax(height,1); v++)
2957 for (u=0; u < (ssize_t) MagickMax(width,1); u++)
2959 InsertPixelList(pixels[i],pixel_list[id]);
2960 pixels+=GetPixelChannels(image);
2962 pixels+=GetPixelChannels(image)*image->columns;
2966 case GradientStatistic:
2972 GetMinimumPixelList(pixel_list[id],&pixel);
2973 minimum=(double) pixel;
2974 GetMaximumPixelList(pixel_list[id],&pixel);
2975 maximum=(double) pixel;
2976 pixel=ClampToQuantum(MagickAbsoluteValue(maximum-minimum));
2979 case MaximumStatistic:
2981 GetMaximumPixelList(pixel_list[id],&pixel);
2986 GetMeanPixelList(pixel_list[id],&pixel);
2989 case MedianStatistic:
2992 GetMedianPixelList(pixel_list[id],&pixel);
2995 case MinimumStatistic:
2997 GetMinimumPixelList(pixel_list[id],&pixel);
3002 GetModePixelList(pixel_list[id],&pixel);
3005 case NonpeakStatistic:
3007 GetNonpeakPixelList(pixel_list[id],&pixel);
3010 case RootMeanSquareStatistic:
3012 GetRootMeanSquarePixelList(pixel_list[id],&pixel);
3015 case StandardDeviationStatistic:
3017 GetStandardDeviationPixelList(pixel_list[id],&pixel);
3021 SetPixelChannel(statistic_image,channel,pixel,q);
3023 p+=GetPixelChannels(image);
3024 q+=GetPixelChannels(statistic_image);
3026 if (SyncCacheViewAuthenticPixels(statistic_view,exception) == MagickFalse)
3028 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3033 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3034 #pragma omp critical (MagickCore_StatisticImage)
3036 proceed=SetImageProgress(image,StatisticImageTag,progress++,
3038 if (proceed == MagickFalse)
3042 statistic_view=DestroyCacheView(statistic_view);
3043 image_view=DestroyCacheView(image_view);
3044 pixel_list=DestroyPixelListThreadSet(pixel_list);
3045 if (status == MagickFalse)
3046 statistic_image=DestroyImage(statistic_image);
3047 return(statistic_image);