2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % SSSSS TTTTT AAA TTTTT IIIII SSSSS TTTTT IIIII CCCC %
7 % SS T A A T I SS T I C %
8 % SSS T AAAAA T I SSS T I C %
9 % SS T A A T I SS T I C %
10 % SSSSS T A A T IIIII SSSSS T IIIII CCCC %
13 % MagickCore Image Statistical Methods %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/property.h"
45 #include "MagickCore/animate.h"
46 #include "MagickCore/blob.h"
47 #include "MagickCore/blob-private.h"
48 #include "MagickCore/cache.h"
49 #include "MagickCore/cache-private.h"
50 #include "MagickCore/cache-view.h"
51 #include "MagickCore/client.h"
52 #include "MagickCore/color.h"
53 #include "MagickCore/color-private.h"
54 #include "MagickCore/colorspace.h"
55 #include "MagickCore/colorspace-private.h"
56 #include "MagickCore/composite.h"
57 #include "MagickCore/composite-private.h"
58 #include "MagickCore/compress.h"
59 #include "MagickCore/constitute.h"
60 #include "MagickCore/display.h"
61 #include "MagickCore/draw.h"
62 #include "MagickCore/enhance.h"
63 #include "MagickCore/exception.h"
64 #include "MagickCore/exception-private.h"
65 #include "MagickCore/gem.h"
66 #include "MagickCore/gem-private.h"
67 #include "MagickCore/geometry.h"
68 #include "MagickCore/list.h"
69 #include "MagickCore/image-private.h"
70 #include "MagickCore/magic.h"
71 #include "MagickCore/magick.h"
72 #include "MagickCore/memory_.h"
73 #include "MagickCore/module.h"
74 #include "MagickCore/monitor.h"
75 #include "MagickCore/monitor-private.h"
76 #include "MagickCore/option.h"
77 #include "MagickCore/paint.h"
78 #include "MagickCore/pixel-accessor.h"
79 #include "MagickCore/profile.h"
80 #include "MagickCore/quantize.h"
81 #include "MagickCore/quantum-private.h"
82 #include "MagickCore/random_.h"
83 #include "MagickCore/random-private.h"
84 #include "MagickCore/resource_.h"
85 #include "MagickCore/segment.h"
86 #include "MagickCore/semaphore.h"
87 #include "MagickCore/signature-private.h"
88 #include "MagickCore/statistic.h"
89 #include "MagickCore/string_.h"
90 #include "MagickCore/thread-private.h"
91 #include "MagickCore/timer.h"
92 #include "MagickCore/utility.h"
93 #include "MagickCore/version.h"
96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
100 % E v a l u a t e I m a g e %
104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 % EvaluateImage() applies a value to the image with an arithmetic, relational,
107 % or logical operator to an image. Use these operations to lighten or darken
108 % an image, to increase or decrease contrast in an image, or to produce the
109 % "negative" of an image.
111 % The format of the EvaluateImage method is:
113 % MagickBooleanType EvaluateImage(Image *image,
114 % const MagickEvaluateOperator op,const double value,
115 % ExceptionInfo *exception)
116 % MagickBooleanType EvaluateImages(Image *images,
117 % const MagickEvaluateOperator op,const double value,
118 % ExceptionInfo *exception)
120 % A description of each parameter follows:
122 % o image: the image.
124 % o op: A channel op.
126 % o value: A value value.
128 % o exception: return any errors or warnings in this structure.
132 typedef struct _PixelChannels
135 channel[CompositePixelChannel];
138 static PixelChannels **DestroyPixelThreadSet(PixelChannels **pixels)
143 assert(pixels != (PixelChannels **) NULL);
144 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
145 if (pixels[i] != (PixelChannels *) NULL)
146 pixels[i]=(PixelChannels *) RelinquishMagickMemory(pixels[i]);
147 pixels=(PixelChannels **) RelinquishMagickMemory(pixels);
151 static PixelChannels **AcquirePixelThreadSet(const Image *image,
152 const size_t number_images)
164 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
165 pixels=(PixelChannels **) AcquireQuantumMemory(number_threads,
167 if (pixels == (PixelChannels **) NULL)
168 return((PixelChannels **) NULL);
169 (void) ResetMagickMemory(pixels,0,number_threads*sizeof(*pixels));
170 for (i=0; i < (ssize_t) number_threads; i++)
175 length=image->columns;
176 if (length < number_images)
177 length=number_images;
178 pixels[i]=(PixelChannels *) AcquireQuantumMemory(length,sizeof(**pixels));
179 if (pixels[i] == (PixelChannels *) NULL)
180 return(DestroyPixelThreadSet(pixels));
181 for (j=0; j < (ssize_t) length; j++)
186 for (k=0; k < MaxPixelChannels; k++)
187 pixels[i][j].channel[k]=0.0;
193 static inline double EvaluateMax(const double x,const double y)
200 #if defined(__cplusplus) || defined(c_plusplus)
204 static int IntensityCompare(const void *x,const void *y)
216 color_1=(const PixelChannels *) x;
217 color_2=(const PixelChannels *) y;
219 for (i=0; i < MaxPixelChannels; i++)
220 distance+=color_1->channel[i]-(MagickRealType) color_2->channel[i];
221 return(distance < 0 ? -1 : distance > 0 ? 1 : 0);
224 #if defined(__cplusplus) || defined(c_plusplus)
228 static inline double MagickMin(const double x,const double y)
235 static MagickRealType ApplyEvaluateOperator(RandomInfo *random_info,
236 Quantum pixel,const MagickEvaluateOperator op,const MagickRealType value)
244 case UndefinedEvaluateOperator:
246 case AbsEvaluateOperator:
248 result=(MagickRealType) fabs((double) (pixel+value));
251 case AddEvaluateOperator:
253 result=(MagickRealType) (pixel+value);
256 case AddModulusEvaluateOperator:
259 This returns a 'floored modulus' of the addition which is a positive
260 result. It differs from % or fmod() that returns a 'truncated modulus'
261 result, where floor() is replaced by trunc() and could return a
262 negative result (which is clipped).
265 result-=(QuantumRange+1.0)*floor((double) result/(QuantumRange+1.0));
268 case AndEvaluateOperator:
270 result=(MagickRealType) ((size_t) pixel & (size_t) (value+0.5));
273 case CosineEvaluateOperator:
275 result=(MagickRealType) (QuantumRange*(0.5*cos((double) (2.0*MagickPI*
276 QuantumScale*pixel*value))+0.5));
279 case DivideEvaluateOperator:
281 result=pixel/(value == 0.0 ? 1.0 : value);
284 case ExponentialEvaluateOperator:
286 result=(MagickRealType) (QuantumRange*exp((double) (value*QuantumScale*
290 case GaussianNoiseEvaluateOperator:
292 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
293 GaussianNoise,value);
296 case ImpulseNoiseEvaluateOperator:
298 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
302 case LaplacianNoiseEvaluateOperator:
304 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
305 LaplacianNoise,value);
308 case LeftShiftEvaluateOperator:
310 result=(MagickRealType) ((size_t) pixel << (size_t) (value+0.5));
313 case LogEvaluateOperator:
315 result=(MagickRealType) (QuantumRange*log((double) (QuantumScale*value*
316 pixel+1.0))/log((double) (value+1.0)));
319 case MaxEvaluateOperator:
321 result=(MagickRealType) EvaluateMax((double) pixel,value);
324 case MeanEvaluateOperator:
326 result=(MagickRealType) (pixel+value);
329 case MedianEvaluateOperator:
331 result=(MagickRealType) (pixel+value);
334 case MinEvaluateOperator:
336 result=(MagickRealType) MagickMin((double) pixel,value);
339 case MultiplicativeNoiseEvaluateOperator:
341 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
342 MultiplicativeGaussianNoise,value);
345 case MultiplyEvaluateOperator:
347 result=(MagickRealType) (value*pixel);
350 case OrEvaluateOperator:
352 result=(MagickRealType) ((size_t) pixel | (size_t) (value+0.5));
355 case PoissonNoiseEvaluateOperator:
357 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
361 case PowEvaluateOperator:
363 result=(MagickRealType) (QuantumRange*pow((double) (QuantumScale*pixel),
367 case RightShiftEvaluateOperator:
369 result=(MagickRealType) ((size_t) pixel >> (size_t) (value+0.5));
372 case SetEvaluateOperator:
377 case SineEvaluateOperator:
379 result=(MagickRealType) (QuantumRange*(0.5*sin((double) (2.0*MagickPI*
380 QuantumScale*pixel*value))+0.5));
383 case SubtractEvaluateOperator:
385 result=(MagickRealType) (pixel-value);
388 case SumEvaluateOperator:
390 result=(MagickRealType) (pixel+value);
393 case ThresholdEvaluateOperator:
395 result=(MagickRealType) (((MagickRealType) pixel <= value) ? 0 :
399 case ThresholdBlackEvaluateOperator:
401 result=(MagickRealType) (((MagickRealType) pixel <= value) ? 0 : pixel);
404 case ThresholdWhiteEvaluateOperator:
406 result=(MagickRealType) (((MagickRealType) pixel > value) ? QuantumRange :
410 case UniformNoiseEvaluateOperator:
412 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
416 case XorEvaluateOperator:
418 result=(MagickRealType) ((size_t) pixel ^ (size_t) (value+0.5));
425 MagickExport Image *EvaluateImages(const Image *images,
426 const MagickEvaluateOperator op,ExceptionInfo *exception)
428 #define EvaluateImageTag "Evaluate/Image"
446 **restrict evaluate_pixels;
449 **restrict random_info;
461 Ensure the image are the same size.
463 assert(images != (Image *) NULL);
464 assert(images->signature == MagickSignature);
465 if (images->debug != MagickFalse)
466 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
467 assert(exception != (ExceptionInfo *) NULL);
468 assert(exception->signature == MagickSignature);
469 for (next=images; next != (Image *) NULL; next=GetNextImageInList(next))
470 if ((next->columns != images->columns) || (next->rows != images->rows))
472 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
473 "ImageWidthsOrHeightsDiffer","'%s'",images->filename);
474 return((Image *) NULL);
477 Initialize evaluate next attributes.
479 evaluate_image=CloneImage(images,images->columns,images->rows,MagickTrue,
481 if (evaluate_image == (Image *) NULL)
482 return((Image *) NULL);
483 if (SetImageStorageClass(evaluate_image,DirectClass,exception) == MagickFalse)
485 evaluate_image=DestroyImage(evaluate_image);
486 return((Image *) NULL);
488 number_images=GetImageListLength(images);
489 evaluate_pixels=AcquirePixelThreadSet(images,number_images);
490 if (evaluate_pixels == (PixelChannels **) NULL)
492 evaluate_image=DestroyImage(evaluate_image);
493 (void) ThrowMagickException(exception,GetMagickModule(),
494 ResourceLimitError,"MemoryAllocationFailed","'%s'",images->filename);
495 return((Image *) NULL);
498 Evaluate image pixels.
502 random_info=AcquireRandomInfoThreadSet();
503 key=GetRandomSecretKey(random_info[0]);
504 evaluate_view=AcquireAuthenticCacheView(evaluate_image,exception);
505 if (op == MedianEvaluateOperator)
507 #if defined(MAGICKCORE_OPENMP_SUPPORT)
508 #pragma omp parallel for schedule(static) shared(progress,status) \
509 if (((evaluate_image->rows*evaluate_image->columns) > 8192) && (key == ~0UL)) \
510 num_threads(GetMagickResourceLimit(ThreadResource))
512 for (y=0; y < (ssize_t) evaluate_image->rows; y++)
521 id = GetOpenMPThreadId();
523 register PixelChannels
532 if (status == MagickFalse)
534 q=QueueCacheViewAuthenticPixels(evaluate_view,0,y,
535 evaluate_image->columns,1,exception);
536 if (q == (Quantum *) NULL)
541 evaluate_pixel=evaluate_pixels[id];
542 for (x=0; x < (ssize_t) evaluate_image->columns; x++)
548 for (j=0; j < (ssize_t) number_images; j++)
549 for (k=0; k < MaxPixelChannels; k++)
550 evaluate_pixel[j].channel[k]=0.0;
552 for (j=0; j < (ssize_t) number_images; j++)
554 register const Quantum
560 image_view=AcquireVirtualCacheView(next,exception);
561 p=GetCacheViewVirtualPixels(image_view,x,y,1,1,exception);
562 if (p == (const Quantum *) NULL)
564 image_view=DestroyCacheView(image_view);
567 for (i=0; i < (ssize_t) GetPixelChannels(evaluate_image); i++)
576 channel=GetPixelChannelMapChannel(evaluate_image,i);
577 evaluate_traits=GetPixelChannelMapTraits(evaluate_image,channel);
578 traits=GetPixelChannelMapTraits(next,channel);
579 if ((traits == UndefinedPixelTrait) ||
580 (evaluate_traits == UndefinedPixelTrait))
582 if ((evaluate_traits & UpdatePixelTrait) == 0)
584 evaluate_pixel[j].channel[i]=ApplyEvaluateOperator(
585 random_info[id],GetPixelChannel(evaluate_image,channel,p),op,
586 evaluate_pixel[j].channel[i]);
588 image_view=DestroyCacheView(image_view);
589 next=GetNextImageInList(next);
591 qsort((void *) evaluate_pixel,number_images,sizeof(*evaluate_pixel),
593 for (k=0; k < (ssize_t) GetPixelChannels(evaluate_image); k++)
594 q[k]=ClampToQuantum(evaluate_pixel[j/2].channel[k]);
595 q+=GetPixelChannels(evaluate_image);
597 if (SyncCacheViewAuthenticPixels(evaluate_view,exception) == MagickFalse)
599 if (images->progress_monitor != (MagickProgressMonitor) NULL)
604 #if defined(MAGICKCORE_OPENMP_SUPPORT)
605 #pragma omp critical (MagickCore_EvaluateImages)
607 proceed=SetImageProgress(images,EvaluateImageTag,progress++,
608 evaluate_image->rows);
609 if (proceed == MagickFalse)
616 #if defined(MAGICKCORE_OPENMP_SUPPORT)
617 #pragma omp parallel for schedule(static) shared(progress,status) \
618 if (((evaluate_image->rows*evaluate_image->columns) > 8192) && (key == ~0UL)) \
619 num_threads(GetMagickResourceLimit(ThreadResource))
621 for (y=0; y < (ssize_t) evaluate_image->rows; y++)
630 id = GetOpenMPThreadId();
636 register PixelChannels
645 if (status == MagickFalse)
647 q=QueueCacheViewAuthenticPixels(evaluate_view,0,y,
648 evaluate_image->columns,1,exception);
649 if (q == (Quantum *) NULL)
654 evaluate_pixel=evaluate_pixels[id];
655 for (j=0; j < (ssize_t) evaluate_image->columns; j++)
656 for (i=0; i < MaxPixelChannels; i++)
657 evaluate_pixel[j].channel[i]=0.0;
659 for (j=0; j < (ssize_t) number_images; j++)
661 register const Quantum
664 image_view=AcquireVirtualCacheView(next,exception);
665 p=GetCacheViewVirtualPixels(image_view,0,y,next->columns,1,exception);
666 if (p == (const Quantum *) NULL)
668 image_view=DestroyCacheView(image_view);
671 for (x=0; x < (ssize_t) next->columns; x++)
676 if (GetPixelMask(next,p) != 0)
678 p+=GetPixelChannels(next);
681 for (i=0; i < (ssize_t) GetPixelChannels(next); i++)
690 channel=GetPixelChannelMapChannel(evaluate_image,i);
691 traits=GetPixelChannelMapTraits(next,channel);
692 evaluate_traits=GetPixelChannelMapTraits(evaluate_image,channel);
693 if ((traits == UndefinedPixelTrait) ||
694 (evaluate_traits == UndefinedPixelTrait))
696 if ((traits & UpdatePixelTrait) == 0)
698 evaluate_pixel[x].channel[i]=ApplyEvaluateOperator(
699 random_info[id],GetPixelChannel(evaluate_image,channel,p),j ==
700 0 ? AddEvaluateOperator : op,evaluate_pixel[x].channel[i]);
702 p+=GetPixelChannels(next);
704 image_view=DestroyCacheView(image_view);
705 next=GetNextImageInList(next);
707 for (x=0; x < (ssize_t) evaluate_image->columns; x++)
714 case MeanEvaluateOperator:
716 for (i=0; i < (ssize_t) GetPixelChannels(evaluate_image); i++)
717 evaluate_pixel[x].channel[i]/=(MagickRealType) number_images;
720 case MultiplyEvaluateOperator:
722 for (i=0; i < (ssize_t) GetPixelChannels(evaluate_image); i++)
727 for (j=0; j < (ssize_t) (number_images-1); j++)
728 evaluate_pixel[x].channel[i]*=QuantumScale;
736 for (x=0; x < (ssize_t) evaluate_image->columns; x++)
741 if (GetPixelMask(evaluate_image,q) != 0)
743 q+=GetPixelChannels(evaluate_image);
746 for (i=0; i < (ssize_t) GetPixelChannels(evaluate_image); i++)
754 channel=GetPixelChannelMapChannel(evaluate_image,i);
755 traits=GetPixelChannelMapTraits(evaluate_image,channel);
756 if (traits == UndefinedPixelTrait)
758 if ((traits & UpdatePixelTrait) == 0)
760 q[i]=ClampToQuantum(evaluate_pixel[x].channel[i]);
762 q+=GetPixelChannels(evaluate_image);
764 if (SyncCacheViewAuthenticPixels(evaluate_view,exception) == MagickFalse)
766 if (images->progress_monitor != (MagickProgressMonitor) NULL)
771 #if defined(MAGICKCORE_OPENMP_SUPPORT)
772 #pragma omp critical (MagickCore_EvaluateImages)
774 proceed=SetImageProgress(images,EvaluateImageTag,progress++,
775 evaluate_image->rows);
776 if (proceed == MagickFalse)
781 evaluate_view=DestroyCacheView(evaluate_view);
782 evaluate_pixels=DestroyPixelThreadSet(evaluate_pixels);
783 random_info=DestroyRandomInfoThreadSet(random_info);
784 if (status == MagickFalse)
785 evaluate_image=DestroyImage(evaluate_image);
786 return(evaluate_image);
789 MagickExport MagickBooleanType EvaluateImage(Image *image,
790 const MagickEvaluateOperator op,const double value,ExceptionInfo *exception)
802 **restrict random_info;
810 assert(image != (Image *) NULL);
811 assert(image->signature == MagickSignature);
812 if (image->debug != MagickFalse)
813 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
814 assert(exception != (ExceptionInfo *) NULL);
815 assert(exception->signature == MagickSignature);
816 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
820 random_info=AcquireRandomInfoThreadSet();
821 key=GetRandomSecretKey(random_info[0]);
822 image_view=AcquireAuthenticCacheView(image,exception);
823 #if defined(MAGICKCORE_OPENMP_SUPPORT)
824 #pragma omp parallel for schedule(static,4) shared(progress,status) \
825 if (((image->rows*image->columns) > 8192) && (key == ~0UL)) \
826 num_threads(GetMagickResourceLimit(ThreadResource))
828 for (y=0; y < (ssize_t) image->rows; y++)
831 id = GetOpenMPThreadId();
839 if (status == MagickFalse)
841 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
842 if (q == (Quantum *) NULL)
847 for (x=0; x < (ssize_t) image->columns; x++)
852 if (GetPixelMask(image,q) != 0)
854 q+=GetPixelChannels(image);
857 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
865 channel=GetPixelChannelMapChannel(image,i);
866 traits=GetPixelChannelMapTraits(image,channel);
867 if (traits == UndefinedPixelTrait)
869 if ((traits & CopyPixelTrait) != 0)
871 q[i]=ClampToQuantum(ApplyEvaluateOperator(random_info[id],q[i],op,
874 q+=GetPixelChannels(image);
876 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
878 if (image->progress_monitor != (MagickProgressMonitor) NULL)
883 #if defined(MAGICKCORE_OPENMP_SUPPORT)
884 #pragma omp critical (MagickCore_EvaluateImage)
886 proceed=SetImageProgress(image,EvaluateImageTag,progress++,image->rows);
887 if (proceed == MagickFalse)
891 image_view=DestroyCacheView(image_view);
892 random_info=DestroyRandomInfoThreadSet(random_info);
897 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
901 % F u n c t i o n I m a g e %
905 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
907 % FunctionImage() applies a value to the image with an arithmetic, relational,
908 % or logical operator to an image. Use these operations to lighten or darken
909 % an image, to increase or decrease contrast in an image, or to produce the
910 % "negative" of an image.
912 % The format of the FunctionImage method is:
914 % MagickBooleanType FunctionImage(Image *image,
915 % const MagickFunction function,const ssize_t number_parameters,
916 % const double *parameters,ExceptionInfo *exception)
918 % A description of each parameter follows:
920 % o image: the image.
922 % o function: A channel function.
924 % o parameters: one or more parameters.
926 % o exception: return any errors or warnings in this structure.
930 static Quantum ApplyFunction(Quantum pixel,const MagickFunction function,
931 const size_t number_parameters,const double *parameters,
932 ExceptionInfo *exception)
944 case PolynomialFunction:
947 Polynomial: polynomial constants, highest to lowest order (e.g. c0*x^3+
951 for (i=0; i < (ssize_t) number_parameters; i++)
952 result=result*QuantumScale*pixel+parameters[i];
953 result*=QuantumRange;
956 case SinusoidFunction:
965 Sinusoid: frequency, phase, amplitude, bias.
967 frequency=(number_parameters >= 1) ? parameters[0] : 1.0;
968 phase=(number_parameters >= 2) ? parameters[1] : 0.0;
969 amplitude=(number_parameters >= 3) ? parameters[2] : 0.5;
970 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
971 result=(MagickRealType) (QuantumRange*(amplitude*sin((double) (2.0*
972 MagickPI*(frequency*QuantumScale*pixel+phase/360.0)))+bias));
984 Arcsin (peged at range limits for invalid results): width, center,
987 width=(number_parameters >= 1) ? parameters[0] : 1.0;
988 center=(number_parameters >= 2) ? parameters[1] : 0.5;
989 range=(number_parameters >= 3) ? parameters[2] : 1.0;
990 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
991 result=2.0/width*(QuantumScale*pixel-center);
992 if ( result <= -1.0 )
993 result=bias-range/2.0;
996 result=bias+range/2.0;
998 result=(MagickRealType) (range/MagickPI*asin((double) result)+bias);
999 result*=QuantumRange;
1002 case ArctanFunction:
1011 Arctan: slope, center, range, and bias.
1013 slope=(number_parameters >= 1) ? parameters[0] : 1.0;
1014 center=(number_parameters >= 2) ? parameters[1] : 0.5;
1015 range=(number_parameters >= 3) ? parameters[2] : 1.0;
1016 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
1017 result=(MagickRealType) (MagickPI*slope*(QuantumScale*pixel-center));
1018 result=(MagickRealType) (QuantumRange*(range/MagickPI*atan((double)
1022 case UndefinedFunction:
1025 return(ClampToQuantum(result));
1028 MagickExport MagickBooleanType FunctionImage(Image *image,
1029 const MagickFunction function,const size_t number_parameters,
1030 const double *parameters,ExceptionInfo *exception)
1032 #define FunctionImageTag "Function/Image "
1046 assert(image != (Image *) NULL);
1047 assert(image->signature == MagickSignature);
1048 if (image->debug != MagickFalse)
1049 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1050 assert(exception != (ExceptionInfo *) NULL);
1051 assert(exception->signature == MagickSignature);
1052 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1053 return(MagickFalse);
1056 image_view=AcquireAuthenticCacheView(image,exception);
1057 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1058 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1059 if ((image->rows*image->columns) > 8192) \
1060 num_threads(GetMagickResourceLimit(ThreadResource))
1062 for (y=0; y < (ssize_t) image->rows; y++)
1070 if (status == MagickFalse)
1072 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1073 if (q == (Quantum *) NULL)
1078 for (x=0; x < (ssize_t) image->columns; x++)
1083 if (GetPixelMask(image,q) != 0)
1085 q+=GetPixelChannels(image);
1088 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1096 channel=GetPixelChannelMapChannel(image,i);
1097 traits=GetPixelChannelMapTraits(image,channel);
1098 if (traits == UndefinedPixelTrait)
1100 if ((traits & UpdatePixelTrait) == 0)
1102 q[i]=ApplyFunction(q[i],function,number_parameters,parameters,
1105 q+=GetPixelChannels(image);
1107 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1109 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1114 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1115 #pragma omp critical (MagickCore_FunctionImage)
1117 proceed=SetImageProgress(image,FunctionImageTag,progress++,image->rows);
1118 if (proceed == MagickFalse)
1122 image_view=DestroyCacheView(image_view);
1127 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1131 % G e t I m a g e E x t r e m a %
1135 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1137 % GetImageExtrema() returns the extrema of one or more image channels.
1139 % The format of the GetImageExtrema method is:
1141 % MagickBooleanType GetImageExtrema(const Image *image,size_t *minima,
1142 % size_t *maxima,ExceptionInfo *exception)
1144 % A description of each parameter follows:
1146 % o image: the image.
1148 % o minima: the minimum value in the channel.
1150 % o maxima: the maximum value in the channel.
1152 % o exception: return any errors or warnings in this structure.
1155 MagickExport MagickBooleanType GetImageExtrema(const Image *image,
1156 size_t *minima,size_t *maxima,ExceptionInfo *exception)
1165 assert(image != (Image *) NULL);
1166 assert(image->signature == MagickSignature);
1167 if (image->debug != MagickFalse)
1168 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1169 status=GetImageRange(image,&min,&max,exception);
1170 *minima=(size_t) ceil(min-0.5);
1171 *maxima=(size_t) floor(max+0.5);
1176 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1180 % G e t I m a g e M e a n %
1184 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1186 % GetImageMean() returns the mean and standard deviation of one or more
1189 % The format of the GetImageMean method is:
1191 % MagickBooleanType GetImageMean(const Image *image,double *mean,
1192 % double *standard_deviation,ExceptionInfo *exception)
1194 % A description of each parameter follows:
1196 % o image: the image.
1198 % o mean: the average value in the channel.
1200 % o standard_deviation: the standard deviation of the channel.
1202 % o exception: return any errors or warnings in this structure.
1205 MagickExport MagickBooleanType GetImageMean(const Image *image,double *mean,
1206 double *standard_deviation,ExceptionInfo *exception)
1209 *channel_statistics;
1217 assert(image != (Image *) NULL);
1218 assert(image->signature == MagickSignature);
1219 if (image->debug != MagickFalse)
1220 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1221 channel_statistics=GetImageStatistics(image,exception);
1222 if (channel_statistics == (ChannelStatistics *) NULL)
1223 return(MagickFalse);
1225 channel_statistics[CompositePixelChannel].mean=0.0;
1226 channel_statistics[CompositePixelChannel].standard_deviation=0.0;
1227 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1235 channel=GetPixelChannelMapChannel(image,i);
1236 traits=GetPixelChannelMapTraits(image,channel);
1237 if (traits == UndefinedPixelTrait)
1239 if ((traits & UpdatePixelTrait) == 0)
1241 channel_statistics[CompositePixelChannel].mean+=channel_statistics[i].mean;
1242 channel_statistics[CompositePixelChannel].standard_deviation+=
1243 channel_statistics[i].variance-channel_statistics[i].mean*
1244 channel_statistics[i].mean;
1247 channel_statistics[CompositePixelChannel].mean/=area;
1248 channel_statistics[CompositePixelChannel].standard_deviation=
1249 sqrt(channel_statistics[CompositePixelChannel].standard_deviation/area);
1250 *mean=channel_statistics[CompositePixelChannel].mean;
1251 *standard_deviation=
1252 channel_statistics[CompositePixelChannel].standard_deviation;
1253 channel_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1254 channel_statistics);
1259 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1263 % G e t I m a g e K u r t o s i s %
1267 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1269 % GetImageKurtosis() returns the kurtosis and skewness of one or more
1272 % The format of the GetImageKurtosis method is:
1274 % MagickBooleanType GetImageKurtosis(const Image *image,double *kurtosis,
1275 % double *skewness,ExceptionInfo *exception)
1277 % A description of each parameter follows:
1279 % o image: the image.
1281 % o kurtosis: the kurtosis of the channel.
1283 % o skewness: the skewness of the channel.
1285 % o exception: return any errors or warnings in this structure.
1288 MagickExport MagickBooleanType GetImageKurtosis(const Image *image,
1289 double *kurtosis,double *skewness,ExceptionInfo *exception)
1308 assert(image != (Image *) NULL);
1309 assert(image->signature == MagickSignature);
1310 if (image->debug != MagickFalse)
1311 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1317 standard_deviation=0.0;
1320 sum_fourth_power=0.0;
1321 image_view=AcquireVirtualCacheView(image,exception);
1322 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1323 #pragma omp parallel for schedule(static) shared(status) \
1324 if ((image->rows*image->columns) > 8192) \
1325 num_threads(GetMagickResourceLimit(ThreadResource))
1327 for (y=0; y < (ssize_t) image->rows; y++)
1329 register const Quantum
1335 if (status == MagickFalse)
1337 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1338 if (p == (const Quantum *) NULL)
1343 for (x=0; x < (ssize_t) image->columns; x++)
1348 if (GetPixelMask(image,p) != 0)
1350 p+=GetPixelChannels(image);
1353 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1361 channel=GetPixelChannelMapChannel(image,i);
1362 traits=GetPixelChannelMapTraits(image,channel);
1363 if (traits == UndefinedPixelTrait)
1365 if ((traits & UpdatePixelTrait) == 0)
1367 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1368 #pragma omp critical (MagickCore_GetImageKurtosis)
1372 sum_squares+=(double) p[i]*p[i];
1373 sum_cubes+=(double) p[i]*p[i]*p[i];
1374 sum_fourth_power+=(double) p[i]*p[i]*p[i]*p[i];
1378 p+=GetPixelChannels(image);
1381 image_view=DestroyCacheView(image_view);
1387 sum_fourth_power/=area;
1389 standard_deviation=sqrt(sum_squares-(mean*mean));
1390 if (standard_deviation != 0.0)
1392 *kurtosis=sum_fourth_power-4.0*mean*sum_cubes+6.0*mean*mean*sum_squares-
1393 3.0*mean*mean*mean*mean;
1394 *kurtosis/=standard_deviation*standard_deviation*standard_deviation*
1397 *skewness=sum_cubes-3.0*mean*sum_squares+2.0*mean*mean*mean;
1398 *skewness/=standard_deviation*standard_deviation*standard_deviation;
1404 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1408 % G e t I m a g e R a n g e %
1412 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1414 % GetImageRange() returns the range of one or more image channels.
1416 % The format of the GetImageRange method is:
1418 % MagickBooleanType GetImageRange(const Image *image,double *minima,
1419 % double *maxima,ExceptionInfo *exception)
1421 % A description of each parameter follows:
1423 % o image: the image.
1425 % o minima: the minimum value in the channel.
1427 % o maxima: the maximum value in the channel.
1429 % o exception: return any errors or warnings in this structure.
1432 MagickExport MagickBooleanType GetImageRange(const Image *image,double *minima,
1433 double *maxima,ExceptionInfo *exception)
1444 assert(image != (Image *) NULL);
1445 assert(image->signature == MagickSignature);
1446 if (image->debug != MagickFalse)
1447 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1449 *maxima=(-MagickHuge);
1451 image_view=AcquireVirtualCacheView(image,exception);
1452 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1453 #pragma omp parallel for schedule(static) shared(status) \
1454 if ((image->rows*image->columns) > 8192) \
1455 num_threads(GetMagickResourceLimit(ThreadResource))
1457 for (y=0; y < (ssize_t) image->rows; y++)
1459 register const Quantum
1465 if (status == MagickFalse)
1467 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1468 if (p == (const Quantum *) NULL)
1473 for (x=0; x < (ssize_t) image->columns; x++)
1478 if (GetPixelMask(image,p) != 0)
1480 p+=GetPixelChannels(image);
1483 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1491 channel=GetPixelChannelMapChannel(image,i);
1492 traits=GetPixelChannelMapTraits(image,channel);
1493 if (traits == UndefinedPixelTrait)
1495 if ((traits & UpdatePixelTrait) == 0)
1497 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1498 #pragma omp critical (MagickCore_GetImageRange)
1501 if ((double) p[i] < *minima)
1502 *minima=(double) p[i];
1503 if ((double) p[i] > *maxima)
1504 *maxima=(double) p[i];
1507 p+=GetPixelChannels(image);
1510 image_view=DestroyCacheView(image_view);
1515 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1519 % G e t I m a g e S t a t i s t i c s %
1523 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1525 % GetImageStatistics() returns statistics for each channel in the
1526 % image. The statistics include the channel depth, its minima, maxima, mean,
1527 % standard deviation, kurtosis and skewness. You can access the red channel
1528 % mean, for example, like this:
1530 % channel_statistics=GetImageStatistics(image,exception);
1531 % red_mean=channel_statistics[RedPixelChannel].mean;
1533 % Use MagickRelinquishMemory() to free the statistics buffer.
1535 % The format of the GetImageStatistics method is:
1537 % ChannelStatistics *GetImageStatistics(const Image *image,
1538 % ExceptionInfo *exception)
1540 % A description of each parameter follows:
1542 % o image: the image.
1544 % o exception: return any errors or warnings in this structure.
1548 static size_t GetImageChannels(const Image *image)
1557 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1565 channel=GetPixelChannelMapChannel(image,i);
1566 traits=GetPixelChannelMapTraits(image,channel);
1567 if ((traits & UpdatePixelTrait) != 0)
1573 MagickExport ChannelStatistics *GetImageStatistics(const Image *image,
1574 ExceptionInfo *exception)
1577 *channel_statistics;
1598 assert(image != (Image *) NULL);
1599 assert(image->signature == MagickSignature);
1600 if (image->debug != MagickFalse)
1601 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1602 channel_statistics=(ChannelStatistics *) AcquireQuantumMemory(
1603 MaxPixelChannels+1,sizeof(*channel_statistics));
1604 if (channel_statistics == (ChannelStatistics *) NULL)
1605 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1606 (void) ResetMagickMemory(channel_statistics,0,(MaxPixelChannels+1)*
1607 sizeof(*channel_statistics));
1608 for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
1610 channel_statistics[i].depth=1;
1611 channel_statistics[i].maxima=(-MagickHuge);
1612 channel_statistics[i].minima=MagickHuge;
1614 for (y=0; y < (ssize_t) image->rows; y++)
1616 register const Quantum
1622 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1623 if (p == (const Quantum *) NULL)
1625 for (x=0; x < (ssize_t) image->columns; x++)
1630 if (GetPixelMask(image,p) != 0)
1632 p+=GetPixelChannels(image);
1635 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1643 channel=GetPixelChannelMapChannel(image,i);
1644 traits=GetPixelChannelMapTraits(image,channel);
1645 if (traits == UndefinedPixelTrait)
1647 if (channel_statistics[channel].depth != MAGICKCORE_QUANTUM_DEPTH)
1649 depth=channel_statistics[channel].depth;
1650 range=GetQuantumRange(depth);
1651 status=p[i] != ScaleAnyToQuantum(ScaleQuantumToAny(p[i],range),
1652 range) ? MagickTrue : MagickFalse;
1653 if (status != MagickFalse)
1655 channel_statistics[channel].depth++;
1659 if ((double) p[i] < channel_statistics[channel].minima)
1660 channel_statistics[channel].minima=(double) p[i];
1661 if ((double) p[i] > channel_statistics[channel].maxima)
1662 channel_statistics[channel].maxima=(double) p[i];
1663 channel_statistics[channel].sum+=p[i];
1664 channel_statistics[channel].sum_squared+=(double) p[i]*p[i];
1665 channel_statistics[channel].sum_cubed+=(double) p[i]*p[i]*p[i];
1666 channel_statistics[channel].sum_fourth_power+=(double) p[i]*p[i]*p[i]*
1669 p+=GetPixelChannels(image);
1672 area=(double) image->columns*image->rows;
1673 for (i=0; i < (ssize_t) MaxPixelChannels; i++)
1675 channel_statistics[i].sum/=area;
1676 channel_statistics[i].sum_squared/=area;
1677 channel_statistics[i].sum_cubed/=area;
1678 channel_statistics[i].sum_fourth_power/=area;
1679 channel_statistics[i].mean=channel_statistics[i].sum;
1680 channel_statistics[i].variance=channel_statistics[i].sum_squared;
1681 channel_statistics[i].standard_deviation=sqrt(
1682 channel_statistics[i].variance-(channel_statistics[i].mean*
1683 channel_statistics[i].mean));
1685 for (i=0; i < (ssize_t) MaxPixelChannels; i++)
1687 channel_statistics[CompositePixelChannel].depth=(size_t) EvaluateMax(
1688 (double) channel_statistics[CompositePixelChannel].depth,(double)
1689 channel_statistics[i].depth);
1690 channel_statistics[CompositePixelChannel].minima=MagickMin(
1691 channel_statistics[CompositePixelChannel].minima,
1692 channel_statistics[i].minima);
1693 channel_statistics[CompositePixelChannel].maxima=EvaluateMax(
1694 channel_statistics[CompositePixelChannel].maxima,
1695 channel_statistics[i].maxima);
1696 channel_statistics[CompositePixelChannel].sum+=channel_statistics[i].sum;
1697 channel_statistics[CompositePixelChannel].sum_squared+=
1698 channel_statistics[i].sum_squared;
1699 channel_statistics[CompositePixelChannel].sum_cubed+=
1700 channel_statistics[i].sum_cubed;
1701 channel_statistics[CompositePixelChannel].sum_fourth_power+=
1702 channel_statistics[i].sum_fourth_power;
1703 channel_statistics[CompositePixelChannel].mean+=channel_statistics[i].mean;
1704 channel_statistics[CompositePixelChannel].variance+=
1705 channel_statistics[i].variance-channel_statistics[i].mean*
1706 channel_statistics[i].mean;
1707 channel_statistics[CompositePixelChannel].standard_deviation+=
1708 channel_statistics[i].variance-channel_statistics[i].mean*
1709 channel_statistics[i].mean;
1711 channels=GetImageChannels(image);
1712 channel_statistics[CompositePixelChannel].sum/=channels;
1713 channel_statistics[CompositePixelChannel].sum_squared/=channels;
1714 channel_statistics[CompositePixelChannel].sum_cubed/=channels;
1715 channel_statistics[CompositePixelChannel].sum_fourth_power/=channels;
1716 channel_statistics[CompositePixelChannel].mean/=channels;
1717 channel_statistics[CompositePixelChannel].variance/=channels;
1718 channel_statistics[CompositePixelChannel].standard_deviation=
1719 sqrt(channel_statistics[CompositePixelChannel].standard_deviation/channels);
1720 channel_statistics[CompositePixelChannel].kurtosis/=channels;
1721 channel_statistics[CompositePixelChannel].skewness/=channels;
1722 for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
1724 if (channel_statistics[i].standard_deviation == 0.0)
1726 channel_statistics[i].skewness=(channel_statistics[i].sum_cubed-3.0*
1727 channel_statistics[i].mean*channel_statistics[i].sum_squared+2.0*
1728 channel_statistics[i].mean*channel_statistics[i].mean*
1729 channel_statistics[i].mean)/(channel_statistics[i].standard_deviation*
1730 channel_statistics[i].standard_deviation*
1731 channel_statistics[i].standard_deviation);
1732 channel_statistics[i].kurtosis=(channel_statistics[i].sum_fourth_power-4.0*
1733 channel_statistics[i].mean*channel_statistics[i].sum_cubed+6.0*
1734 channel_statistics[i].mean*channel_statistics[i].mean*
1735 channel_statistics[i].sum_squared-3.0*channel_statistics[i].mean*
1736 channel_statistics[i].mean*1.0*channel_statistics[i].mean*
1737 channel_statistics[i].mean)/(channel_statistics[i].standard_deviation*
1738 channel_statistics[i].standard_deviation*
1739 channel_statistics[i].standard_deviation*
1740 channel_statistics[i].standard_deviation)-3.0;
1742 return(channel_statistics);
1746 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1750 % S t a t i s t i c I m a g e %
1754 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1756 % StatisticImage() makes each pixel the min / max / median / mode / etc. of
1757 % the neighborhood of the specified width and height.
1759 % The format of the StatisticImage method is:
1761 % Image *StatisticImage(const Image *image,const StatisticType type,
1762 % const size_t width,const size_t height,ExceptionInfo *exception)
1764 % A description of each parameter follows:
1766 % o image: the image.
1768 % o type: the statistic type (median, mode, etc.).
1770 % o width: the width of the pixel neighborhood.
1772 % o height: the height of the pixel neighborhood.
1774 % o exception: return any errors or warnings in this structure.
1778 typedef struct _SkipNode
1786 typedef struct _SkipList
1795 typedef struct _PixelList
1808 static PixelList *DestroyPixelList(PixelList *pixel_list)
1810 if (pixel_list == (PixelList *) NULL)
1811 return((PixelList *) NULL);
1812 if (pixel_list->skip_list.nodes != (SkipNode *) NULL)
1813 pixel_list->skip_list.nodes=(SkipNode *) RelinquishMagickMemory(
1814 pixel_list->skip_list.nodes);
1815 pixel_list=(PixelList *) RelinquishMagickMemory(pixel_list);
1819 static PixelList **DestroyPixelListThreadSet(PixelList **pixel_list)
1824 assert(pixel_list != (PixelList **) NULL);
1825 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
1826 if (pixel_list[i] != (PixelList *) NULL)
1827 pixel_list[i]=DestroyPixelList(pixel_list[i]);
1828 pixel_list=(PixelList **) RelinquishMagickMemory(pixel_list);
1832 static PixelList *AcquirePixelList(const size_t width,const size_t height)
1837 pixel_list=(PixelList *) AcquireMagickMemory(sizeof(*pixel_list));
1838 if (pixel_list == (PixelList *) NULL)
1840 (void) ResetMagickMemory((void *) pixel_list,0,sizeof(*pixel_list));
1841 pixel_list->length=width*height;
1842 pixel_list->skip_list.nodes=(SkipNode *) AcquireQuantumMemory(65537UL,
1843 sizeof(*pixel_list->skip_list.nodes));
1844 if (pixel_list->skip_list.nodes == (SkipNode *) NULL)
1845 return(DestroyPixelList(pixel_list));
1846 (void) ResetMagickMemory(pixel_list->skip_list.nodes,0,65537UL*
1847 sizeof(*pixel_list->skip_list.nodes));
1848 pixel_list->signature=MagickSignature;
1852 static PixelList **AcquirePixelListThreadSet(const size_t width,
1853 const size_t height)
1864 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
1865 pixel_list=(PixelList **) AcquireQuantumMemory(number_threads,
1866 sizeof(*pixel_list));
1867 if (pixel_list == (PixelList **) NULL)
1868 return((PixelList **) NULL);
1869 (void) ResetMagickMemory(pixel_list,0,number_threads*sizeof(*pixel_list));
1870 for (i=0; i < (ssize_t) number_threads; i++)
1872 pixel_list[i]=AcquirePixelList(width,height);
1873 if (pixel_list[i] == (PixelList *) NULL)
1874 return(DestroyPixelListThreadSet(pixel_list));
1879 static void AddNodePixelList(PixelList *pixel_list,const size_t color)
1892 Initialize the node.
1894 p=(&pixel_list->skip_list);
1895 p->nodes[color].signature=pixel_list->signature;
1896 p->nodes[color].count=1;
1898 Determine where it belongs in the list.
1901 for (level=p->level; level >= 0; level--)
1903 while (p->nodes[search].next[level] < color)
1904 search=p->nodes[search].next[level];
1905 update[level]=search;
1908 Generate a pseudo-random level for this node.
1910 for (level=0; ; level++)
1912 pixel_list->seed=(pixel_list->seed*42893621L)+1L;
1913 if ((pixel_list->seed & 0x300) != 0x300)
1918 if (level > (p->level+2))
1921 If we're raising the list's level, link back to the root node.
1923 while (level > p->level)
1926 update[p->level]=65536UL;
1929 Link the node into the skip-list.
1933 p->nodes[color].next[level]=p->nodes[update[level]].next[level];
1934 p->nodes[update[level]].next[level]=color;
1935 } while (level-- > 0);
1938 static inline void GetMaximumPixelList(PixelList *pixel_list,Quantum *pixel)
1951 Find the maximum value for each of the color.
1953 p=(&pixel_list->skip_list);
1956 maximum=p->nodes[color].next[0];
1959 color=p->nodes[color].next[0];
1960 if (color > maximum)
1962 count+=p->nodes[color].count;
1963 } while (count < (ssize_t) pixel_list->length);
1964 *pixel=ScaleShortToQuantum((unsigned short) maximum);
1967 static inline void GetMeanPixelList(PixelList *pixel_list,Quantum *pixel)
1982 Find the mean value for each of the color.
1984 p=(&pixel_list->skip_list);
1990 color=p->nodes[color].next[0];
1991 sum+=(MagickRealType) p->nodes[color].count*color;
1992 count+=p->nodes[color].count;
1993 } while (count < (ssize_t) pixel_list->length);
1994 sum/=pixel_list->length;
1995 *pixel=ScaleShortToQuantum((unsigned short) sum);
1998 static inline void GetMedianPixelList(PixelList *pixel_list,Quantum *pixel)
2010 Find the median value for each of the color.
2012 p=(&pixel_list->skip_list);
2017 color=p->nodes[color].next[0];
2018 count+=p->nodes[color].count;
2019 } while (count <= (ssize_t) (pixel_list->length >> 1));
2020 *pixel=ScaleShortToQuantum((unsigned short) color);
2023 static inline void GetMinimumPixelList(PixelList *pixel_list,Quantum *pixel)
2036 Find the minimum value for each of the color.
2038 p=(&pixel_list->skip_list);
2041 minimum=p->nodes[color].next[0];
2044 color=p->nodes[color].next[0];
2045 if (color < minimum)
2047 count+=p->nodes[color].count;
2048 } while (count < (ssize_t) pixel_list->length);
2049 *pixel=ScaleShortToQuantum((unsigned short) minimum);
2052 static inline void GetModePixelList(PixelList *pixel_list,Quantum *pixel)
2066 Make each pixel the 'predominant color' of the specified neighborhood.
2068 p=(&pixel_list->skip_list);
2071 max_count=p->nodes[mode].count;
2075 color=p->nodes[color].next[0];
2076 if (p->nodes[color].count > max_count)
2079 max_count=p->nodes[mode].count;
2081 count+=p->nodes[color].count;
2082 } while (count < (ssize_t) pixel_list->length);
2083 *pixel=ScaleShortToQuantum((unsigned short) mode);
2086 static inline void GetNonpeakPixelList(PixelList *pixel_list,Quantum *pixel)
2100 Finds the non peak value for each of the colors.
2102 p=(&pixel_list->skip_list);
2104 next=p->nodes[color].next[0];
2110 next=p->nodes[color].next[0];
2111 count+=p->nodes[color].count;
2112 } while (count <= (ssize_t) (pixel_list->length >> 1));
2113 if ((previous == 65536UL) && (next != 65536UL))
2116 if ((previous != 65536UL) && (next == 65536UL))
2118 *pixel=ScaleShortToQuantum((unsigned short) color);
2121 static inline void GetStandardDeviationPixelList(PixelList *pixel_list,
2138 Find the standard-deviation value for each of the color.
2140 p=(&pixel_list->skip_list);
2150 color=p->nodes[color].next[0];
2151 sum+=(MagickRealType) p->nodes[color].count*color;
2152 for (i=0; i < (ssize_t) p->nodes[color].count; i++)
2153 sum_squared+=((MagickRealType) color)*((MagickRealType) color);
2154 count+=p->nodes[color].count;
2155 } while (count < (ssize_t) pixel_list->length);
2156 sum/=pixel_list->length;
2157 sum_squared/=pixel_list->length;
2158 *pixel=ScaleShortToQuantum((unsigned short) sqrt(sum_squared-(sum*sum)));
2161 static inline void InsertPixelList(const Image *image,const Quantum pixel,
2162 PixelList *pixel_list)
2170 index=ScaleQuantumToShort(pixel);
2171 signature=pixel_list->skip_list.nodes[index].signature;
2172 if (signature == pixel_list->signature)
2174 pixel_list->skip_list.nodes[index].count++;
2177 AddNodePixelList(pixel_list,index);
2180 static inline MagickRealType MagickAbsoluteValue(const MagickRealType x)
2187 static inline size_t MagickMax(const size_t x,const size_t y)
2194 static void ResetPixelList(PixelList *pixel_list)
2206 Reset the skip-list.
2208 p=(&pixel_list->skip_list);
2209 root=p->nodes+65536UL;
2211 for (level=0; level < 9; level++)
2212 root->next[level]=65536UL;
2213 pixel_list->seed=pixel_list->signature++;
2216 MagickExport Image *StatisticImage(const Image *image,const StatisticType type,
2217 const size_t width,const size_t height,ExceptionInfo *exception)
2219 #define StatisticImageTag "Statistic/Image"
2235 **restrict pixel_list;
2242 Initialize statistics image attributes.
2244 assert(image != (Image *) NULL);
2245 assert(image->signature == MagickSignature);
2246 if (image->debug != MagickFalse)
2247 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2248 assert(exception != (ExceptionInfo *) NULL);
2249 assert(exception->signature == MagickSignature);
2250 statistic_image=CloneImage(image,image->columns,image->rows,MagickTrue,
2252 if (statistic_image == (Image *) NULL)
2253 return((Image *) NULL);
2254 status=SetImageStorageClass(statistic_image,DirectClass,exception);
2255 if (status == MagickFalse)
2257 statistic_image=DestroyImage(statistic_image);
2258 return((Image *) NULL);
2260 pixel_list=AcquirePixelListThreadSet(MagickMax(width,1),MagickMax(height,1));
2261 if (pixel_list == (PixelList **) NULL)
2263 statistic_image=DestroyImage(statistic_image);
2264 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2267 Make each pixel the min / max / median / mode / etc. of the neighborhood.
2269 center=(ssize_t) GetPixelChannels(image)*(image->columns+MagickMax(width,1))*
2270 (MagickMax(height,1)/2L)+GetPixelChannels(image)*(MagickMax(width,1)/2L);
2273 image_view=AcquireVirtualCacheView(image,exception);
2274 statistic_view=AcquireAuthenticCacheView(statistic_image,exception);
2275 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2276 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2277 if ((statistic_image->rows*statistic_image->columns) > 8192) \
2278 num_threads(GetMagickResourceLimit(ThreadResource))
2280 for (y=0; y < (ssize_t) statistic_image->rows; y++)
2283 id = GetOpenMPThreadId();
2285 register const Quantum
2294 if (status == MagickFalse)
2296 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) MagickMax(width,1)/2L),y-
2297 (ssize_t) (MagickMax(height,1)/2L),image->columns+MagickMax(width,1),
2298 MagickMax(height,1),exception);
2299 q=QueueCacheViewAuthenticPixels(statistic_view,0,y,statistic_image->columns, 1,exception);
2300 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2305 for (x=0; x < (ssize_t) statistic_image->columns; x++)
2310 if (GetPixelMask(image,p) != 0)
2312 p+=GetPixelChannels(image);
2313 q+=GetPixelChannels(statistic_image);
2316 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2328 register const Quantum
2337 channel=GetPixelChannelMapChannel(image,i);
2338 traits=GetPixelChannelMapTraits(image,channel);
2339 statistic_traits=GetPixelChannelMapTraits(statistic_image,channel);
2340 if ((traits == UndefinedPixelTrait) ||
2341 (statistic_traits == UndefinedPixelTrait))
2343 if ((statistic_traits & CopyPixelTrait) != 0)
2345 SetPixelChannel(statistic_image,channel,p[center+i],q);
2349 ResetPixelList(pixel_list[id]);
2350 for (v=0; v < (ssize_t) MagickMax(height,1); v++)
2352 for (u=0; u < (ssize_t) MagickMax(width,1); u++)
2354 InsertPixelList(image,pixels[i],pixel_list[id]);
2355 pixels+=GetPixelChannels(image);
2357 pixels+=image->columns*GetPixelChannels(image);
2361 case GradientStatistic:
2367 GetMinimumPixelList(pixel_list[id],&pixel);
2368 minimum=(MagickRealType) pixel;
2369 GetMaximumPixelList(pixel_list[id],&pixel);
2370 maximum=(MagickRealType) pixel;
2371 pixel=ClampToQuantum(MagickAbsoluteValue(maximum-minimum));
2374 case MaximumStatistic:
2376 GetMaximumPixelList(pixel_list[id],&pixel);
2381 GetMeanPixelList(pixel_list[id],&pixel);
2384 case MedianStatistic:
2387 GetMedianPixelList(pixel_list[id],&pixel);
2390 case MinimumStatistic:
2392 GetMinimumPixelList(pixel_list[id],&pixel);
2397 GetModePixelList(pixel_list[id],&pixel);
2400 case NonpeakStatistic:
2402 GetNonpeakPixelList(pixel_list[id],&pixel);
2405 case StandardDeviationStatistic:
2407 GetStandardDeviationPixelList(pixel_list[id],&pixel);
2411 SetPixelChannel(statistic_image,channel,pixel,q);
2413 p+=GetPixelChannels(image);
2414 q+=GetPixelChannels(statistic_image);
2416 if (SyncCacheViewAuthenticPixels(statistic_view,exception) == MagickFalse)
2418 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2423 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2424 #pragma omp critical (MagickCore_StatisticImage)
2426 proceed=SetImageProgress(image,StatisticImageTag,progress++,
2428 if (proceed == MagickFalse)
2432 statistic_view=DestroyCacheView(statistic_view);
2433 image_view=DestroyCacheView(image_view);
2434 pixel_list=DestroyPixelListThreadSet(pixel_list);
2435 return(statistic_image);