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-2011 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/property.h"
45 #include "MagickCore/animate.h"
46 #include "MagickCore/blob.h"
47 #include "MagickCore/blob-private.h"
48 #include "MagickCore/cache.h"
49 #include "MagickCore/cache-private.h"
50 #include "MagickCore/cache-view.h"
51 #include "MagickCore/client.h"
52 #include "MagickCore/color.h"
53 #include "MagickCore/color-private.h"
54 #include "MagickCore/colorspace.h"
55 #include "MagickCore/colorspace-private.h"
56 #include "MagickCore/composite.h"
57 #include "MagickCore/composite-private.h"
58 #include "MagickCore/compress.h"
59 #include "MagickCore/constitute.h"
60 #include "MagickCore/display.h"
61 #include "MagickCore/draw.h"
62 #include "MagickCore/enhance.h"
63 #include "MagickCore/exception.h"
64 #include "MagickCore/exception-private.h"
65 #include "MagickCore/gem.h"
66 #include "MagickCore/gem-private.h"
67 #include "MagickCore/geometry.h"
68 #include "MagickCore/list.h"
69 #include "MagickCore/image-private.h"
70 #include "MagickCore/magic.h"
71 #include "MagickCore/magick.h"
72 #include "MagickCore/memory_.h"
73 #include "MagickCore/module.h"
74 #include "MagickCore/monitor.h"
75 #include "MagickCore/monitor-private.h"
76 #include "MagickCore/option.h"
77 #include "MagickCore/paint.h"
78 #include "MagickCore/pixel-accessor.h"
79 #include "MagickCore/profile.h"
80 #include "MagickCore/quantize.h"
81 #include "MagickCore/quantum-private.h"
82 #include "MagickCore/random_.h"
83 #include "MagickCore/random-private.h"
84 #include "MagickCore/segment.h"
85 #include "MagickCore/semaphore.h"
86 #include "MagickCore/signature-private.h"
87 #include "MagickCore/statistic.h"
88 #include "MagickCore/string_.h"
89 #include "MagickCore/thread-private.h"
90 #include "MagickCore/timer.h"
91 #include "MagickCore/utility.h"
92 #include "MagickCore/version.h"
95 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99 % E v a l u a t e I m a g e %
103 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105 % EvaluateImage() applies a value to the image with an arithmetic, relational,
106 % or logical operator to an image. Use these operations to lighten or darken
107 % an image, to increase or decrease contrast in an image, or to produce the
108 % "negative" of an image.
110 % The format of the EvaluateImage method is:
112 % MagickBooleanType EvaluateImage(Image *image,
113 % const MagickEvaluateOperator op,const double value,
114 % ExceptionInfo *exception)
115 % MagickBooleanType EvaluateImages(Image *images,
116 % const MagickEvaluateOperator op,const double value,
117 % ExceptionInfo *exception)
119 % A description of each parameter follows:
121 % o image: the image.
123 % o op: A channel op.
125 % o value: A value value.
127 % o exception: return any errors or warnings in this structure.
131 typedef struct _PixelChannels
134 channel[CompositePixelChannel];
137 static PixelChannels **DestroyPixelThreadSet(PixelChannels **pixels)
142 assert(pixels != (PixelChannels **) NULL);
143 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
144 if (pixels[i] != (PixelChannels *) NULL)
145 pixels[i]=(PixelChannels *) RelinquishMagickMemory(pixels[i]);
146 pixels=(PixelChannels **) RelinquishMagickMemory(pixels);
150 static PixelChannels **AcquirePixelThreadSet(const Image *image,
151 const size_t number_images)
163 number_threads=GetOpenMPMaximumThreads();
164 pixels=(PixelChannels **) AcquireQuantumMemory(number_threads,
166 if (pixels == (PixelChannels **) NULL)
167 return((PixelChannels **) NULL);
168 (void) ResetMagickMemory(pixels,0,number_threads*sizeof(*pixels));
169 for (i=0; i < (ssize_t) number_threads; i++)
174 length=image->columns;
175 if (length < number_images)
176 length=number_images;
177 pixels[i]=(PixelChannels *) AcquireQuantumMemory(length,sizeof(**pixels));
178 if (pixels[i] == (PixelChannels *) NULL)
179 return(DestroyPixelThreadSet(pixels));
180 for (j=0; j < (ssize_t) length; j++)
185 for (k=0; k < MaxPixelChannels; k++)
186 pixels[i][j].channel[k]=0.0;
192 static inline double EvaluateMax(const double x,const double y)
199 #if defined(__cplusplus) || defined(c_plusplus)
203 static int IntensityCompare(const void *x,const void *y)
215 color_1=(const PixelChannels *) x;
216 color_2=(const PixelChannels *) y;
218 for (i=0; i < MaxPixelChannels; i++)
219 distance+=color_1->channel[i]-(MagickRealType) color_2->channel[i];
220 return(distance < 0 ? -1 : distance > 0 ? 1 : 0);
223 #if defined(__cplusplus) || defined(c_plusplus)
227 static inline double MagickMin(const double x,const double y)
234 static MagickRealType ApplyEvaluateOperator(RandomInfo *random_info,
235 Quantum pixel,const MagickEvaluateOperator op,const MagickRealType value)
243 case UndefinedEvaluateOperator:
245 case AbsEvaluateOperator:
247 result=(MagickRealType) fabs((double) (pixel+value));
250 case AddEvaluateOperator:
252 result=(MagickRealType) (pixel+value);
255 case AddModulusEvaluateOperator:
258 This returns a 'floored modulus' of the addition which is a positive
259 result. It differs from % or fmod() that returns a 'truncated modulus'
260 result, where floor() is replaced by trunc() and could return a
261 negative result (which is clipped).
264 result-=(QuantumRange+1.0)*floor((double) result/(QuantumRange+1.0));
267 case AndEvaluateOperator:
269 result=(MagickRealType) ((size_t) pixel & (size_t) (value+0.5));
272 case CosineEvaluateOperator:
274 result=(MagickRealType) (QuantumRange*(0.5*cos((double) (2.0*MagickPI*
275 QuantumScale*pixel*value))+0.5));
278 case DivideEvaluateOperator:
280 result=pixel/(value == 0.0 ? 1.0 : value);
283 case ExponentialEvaluateOperator:
285 result=(MagickRealType) (QuantumRange*exp((double) (value*QuantumScale*
289 case GaussianNoiseEvaluateOperator:
291 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
292 GaussianNoise,value);
295 case ImpulseNoiseEvaluateOperator:
297 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
301 case LaplacianNoiseEvaluateOperator:
303 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
304 LaplacianNoise,value);
307 case LeftShiftEvaluateOperator:
309 result=(MagickRealType) ((size_t) pixel << (size_t) (value+0.5));
312 case LogEvaluateOperator:
314 result=(MagickRealType) (QuantumRange*log((double) (QuantumScale*value*
315 pixel+1.0))/log((double) (value+1.0)));
318 case MaxEvaluateOperator:
320 result=(MagickRealType) EvaluateMax((double) pixel,value);
323 case MeanEvaluateOperator:
325 result=(MagickRealType) (pixel+value);
328 case MedianEvaluateOperator:
330 result=(MagickRealType) (pixel+value);
333 case MinEvaluateOperator:
335 result=(MagickRealType) MagickMin((double) pixel,value);
338 case MultiplicativeNoiseEvaluateOperator:
340 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
341 MultiplicativeGaussianNoise,value);
344 case MultiplyEvaluateOperator:
346 result=(MagickRealType) (value*pixel);
349 case OrEvaluateOperator:
351 result=(MagickRealType) ((size_t) pixel | (size_t) (value+0.5));
354 case PoissonNoiseEvaluateOperator:
356 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
360 case PowEvaluateOperator:
362 result=(MagickRealType) (QuantumRange*pow((double) (QuantumScale*pixel),
366 case RightShiftEvaluateOperator:
368 result=(MagickRealType) ((size_t) pixel >> (size_t) (value+0.5));
371 case SetEvaluateOperator:
376 case SineEvaluateOperator:
378 result=(MagickRealType) (QuantumRange*(0.5*sin((double) (2.0*MagickPI*
379 QuantumScale*pixel*value))+0.5));
382 case SubtractEvaluateOperator:
384 result=(MagickRealType) (pixel-value);
387 case ThresholdEvaluateOperator:
389 result=(MagickRealType) (((MagickRealType) pixel <= value) ? 0 :
393 case ThresholdBlackEvaluateOperator:
395 result=(MagickRealType) (((MagickRealType) pixel <= value) ? 0 : pixel);
398 case ThresholdWhiteEvaluateOperator:
400 result=(MagickRealType) (((MagickRealType) pixel > value) ? QuantumRange :
404 case UniformNoiseEvaluateOperator:
406 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
410 case XorEvaluateOperator:
412 result=(MagickRealType) ((size_t) pixel ^ (size_t) (value+0.5));
419 MagickExport Image *EvaluateImages(const Image *images,
420 const MagickEvaluateOperator op,ExceptionInfo *exception)
422 #define EvaluateImageTag "Evaluate/Image"
440 **restrict evaluate_pixels;
443 **restrict random_info;
452 Ensure the image are the same size.
454 assert(images != (Image *) NULL);
455 assert(images->signature == MagickSignature);
456 if (images->debug != MagickFalse)
457 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
458 assert(exception != (ExceptionInfo *) NULL);
459 assert(exception->signature == MagickSignature);
460 for (next=images; next != (Image *) NULL; next=GetNextImageInList(next))
461 if ((next->columns != images->columns) || (next->rows != images->rows))
463 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
464 "ImageWidthsOrHeightsDiffer","`%s'",images->filename);
465 return((Image *) NULL);
468 Initialize evaluate next attributes.
470 evaluate_image=CloneImage(images,images->columns,images->rows,MagickTrue,
472 if (evaluate_image == (Image *) NULL)
473 return((Image *) NULL);
474 if (SetImageStorageClass(evaluate_image,DirectClass,exception) == MagickFalse)
476 evaluate_image=DestroyImage(evaluate_image);
477 return((Image *) NULL);
479 number_images=GetImageListLength(images);
480 evaluate_pixels=AcquirePixelThreadSet(images,number_images);
481 if (evaluate_pixels == (PixelChannels **) NULL)
483 evaluate_image=DestroyImage(evaluate_image);
484 (void) ThrowMagickException(exception,GetMagickModule(),
485 ResourceLimitError,"MemoryAllocationFailed","`%s'",images->filename);
486 return((Image *) NULL);
489 Evaluate image pixels.
493 random_info=AcquireRandomInfoThreadSet();
494 evaluate_view=AcquireCacheView(evaluate_image);
495 if (op == MedianEvaluateOperator)
497 #if defined(MAGICKCORE_OPENMP_SUPPORT)
498 #pragma omp parallel for schedule(dynamic) shared(progress,status)
500 for (y=0; y < (ssize_t) evaluate_image->rows; y++)
509 id = GetOpenMPThreadId();
511 register PixelChannels
520 if (status == MagickFalse)
522 q=QueueCacheViewAuthenticPixels(evaluate_view,0,y,
523 evaluate_image->columns,1,exception);
524 if (q == (Quantum *) NULL)
529 evaluate_pixel=evaluate_pixels[id];
530 for (x=0; x < (ssize_t) evaluate_image->columns; x++)
536 for (j=0; j < (ssize_t) number_images; j++)
537 for (k=0; k < MaxPixelChannels; k++)
538 evaluate_pixel[j].channel[k]=0.0;
540 for (j=0; j < (ssize_t) number_images; j++)
542 register const Quantum
548 image_view=AcquireCacheView(next);
549 p=GetCacheViewVirtualPixels(image_view,x,y,1,1,exception);
550 if (p == (const Quantum *) NULL)
552 image_view=DestroyCacheView(image_view);
555 for (i=0; i < (ssize_t) GetPixelChannels(evaluate_image); i++)
564 channel=GetPixelChannelMapChannel(evaluate_image,i);
565 evaluate_traits=GetPixelChannelMapTraits(evaluate_image,channel);
566 traits=GetPixelChannelMapTraits(next,channel);
567 if ((traits == UndefinedPixelTrait) ||
568 (evaluate_traits == UndefinedPixelTrait))
570 if ((evaluate_traits & UpdatePixelTrait) == 0)
572 evaluate_pixel[j].channel[i]=ApplyEvaluateOperator(
573 random_info[id],GetPixelChannel(evaluate_image,channel,p),op,
574 evaluate_pixel[j].channel[i]);
576 image_view=DestroyCacheView(image_view);
577 next=GetNextImageInList(next);
579 qsort((void *) evaluate_pixel,number_images,sizeof(*evaluate_pixel),
581 for (k=0; k < (ssize_t) GetPixelChannels(evaluate_image); k++)
582 q[k]=ClampToQuantum(evaluate_pixel[j/2].channel[k]);
583 q+=GetPixelChannels(evaluate_image);
585 if (SyncCacheViewAuthenticPixels(evaluate_view,exception) == MagickFalse)
587 if (images->progress_monitor != (MagickProgressMonitor) NULL)
592 #if defined(MAGICKCORE_OPENMP_SUPPORT)
593 #pragma omp critical (MagickCore_EvaluateImages)
595 proceed=SetImageProgress(images,EvaluateImageTag,progress++,
596 evaluate_image->rows);
597 if (proceed == MagickFalse)
604 #if defined(MAGICKCORE_OPENMP_SUPPORT)
605 #pragma omp parallel for schedule(dynamic) shared(progress,status)
607 for (y=0; y < (ssize_t) evaluate_image->rows; y++)
616 id = GetOpenMPThreadId();
622 register PixelChannels
631 if (status == MagickFalse)
633 q=QueueCacheViewAuthenticPixels(evaluate_view,0,y,
634 evaluate_image->columns,1,exception);
635 if (q == (Quantum *) NULL)
640 evaluate_pixel=evaluate_pixels[id];
641 for (j=0; j < (ssize_t) evaluate_image->columns; j++)
642 for (i=0; i < MaxPixelChannels; i++)
643 evaluate_pixel[j].channel[i]=0.0;
645 for (j=0; j < (ssize_t) number_images; j++)
647 register const Quantum
650 image_view=AcquireCacheView(next);
651 p=GetCacheViewVirtualPixels(image_view,0,y,next->columns,1,exception);
652 if (p == (const Quantum *) NULL)
654 image_view=DestroyCacheView(image_view);
657 for (x=0; x < (ssize_t) next->columns; x++)
662 for (i=0; i < (ssize_t) GetPixelChannels(evaluate_image); i++)
671 channel=GetPixelChannelMapChannel(evaluate_image,i);
672 traits=GetPixelChannelMapTraits(next,channel);
673 evaluate_traits=GetPixelChannelMapTraits(evaluate_image,channel);
674 if ((traits == UndefinedPixelTrait) ||
675 (evaluate_traits == UndefinedPixelTrait))
677 if ((traits & UpdatePixelTrait) == 0)
679 evaluate_pixel[x].channel[i]=ApplyEvaluateOperator(
680 random_info[id],GetPixelChannel(evaluate_image,channel,p),j ==
681 0 ? AddEvaluateOperator : op,evaluate_pixel[x].channel[i]);
683 p+=GetPixelChannels(next);
685 image_view=DestroyCacheView(image_view);
686 next=GetNextImageInList(next);
688 for (x=0; x < (ssize_t) evaluate_image->columns; x++)
695 case MeanEvaluateOperator:
697 for (i=0; i < (ssize_t) GetPixelChannels(evaluate_image); i++)
698 evaluate_pixel[x].channel[i]/=(MagickRealType) number_images;
701 case MultiplyEvaluateOperator:
703 for (i=0; i < (ssize_t) GetPixelChannels(evaluate_image); i++)
708 for (j=0; j < (ssize_t) (number_images-1); j++)
709 evaluate_pixel[x].channel[i]*=QuantumScale;
717 for (x=0; x < (ssize_t) evaluate_image->columns; x++)
722 for (i=0; i < (ssize_t) GetPixelChannels(evaluate_image); i++)
730 channel=GetPixelChannelMapChannel(evaluate_image,i);
731 traits=GetPixelChannelMapTraits(evaluate_image,channel);
732 if (traits == UndefinedPixelTrait)
734 if ((traits & UpdatePixelTrait) == 0)
736 q[i]=ClampToQuantum(evaluate_pixel[x].channel[i]);
738 q+=GetPixelChannels(evaluate_image);
740 if (SyncCacheViewAuthenticPixels(evaluate_view,exception) == MagickFalse)
742 if (images->progress_monitor != (MagickProgressMonitor) NULL)
747 #if defined(MAGICKCORE_OPENMP_SUPPORT)
748 #pragma omp critical (MagickCore_EvaluateImages)
750 proceed=SetImageProgress(images,EvaluateImageTag,progress++,
751 evaluate_image->rows);
752 if (proceed == MagickFalse)
757 evaluate_view=DestroyCacheView(evaluate_view);
758 evaluate_pixels=DestroyPixelThreadSet(evaluate_pixels);
759 random_info=DestroyRandomInfoThreadSet(random_info);
760 if (status == MagickFalse)
761 evaluate_image=DestroyImage(evaluate_image);
762 return(evaluate_image);
765 MagickExport MagickBooleanType EvaluateImage(Image *image,
766 const MagickEvaluateOperator op,const double value,ExceptionInfo *exception)
778 **restrict random_info;
783 assert(image != (Image *) NULL);
784 assert(image->signature == MagickSignature);
785 if (image->debug != MagickFalse)
786 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
787 assert(exception != (ExceptionInfo *) NULL);
788 assert(exception->signature == MagickSignature);
789 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
793 random_info=AcquireRandomInfoThreadSet();
794 image_view=AcquireCacheView(image);
795 #if defined(MAGICKCORE_OPENMP_SUPPORT)
796 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
798 for (y=0; y < (ssize_t) image->rows; y++)
801 id = GetOpenMPThreadId();
809 if (status == MagickFalse)
811 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
812 if (q == (Quantum *) NULL)
817 for (x=0; x < (ssize_t) image->columns; x++)
822 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
830 channel=GetPixelChannelMapChannel(image,i);
831 traits=GetPixelChannelMapTraits(image,channel);
832 if (traits == UndefinedPixelTrait)
834 q[i]=ClampToQuantum(ApplyEvaluateOperator(random_info[id],q[i],op,
837 q+=GetPixelChannels(image);
839 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
841 if (image->progress_monitor != (MagickProgressMonitor) NULL)
846 #if defined(MAGICKCORE_OPENMP_SUPPORT)
847 #pragma omp critical (MagickCore_EvaluateImage)
849 proceed=SetImageProgress(image,EvaluateImageTag,progress++,image->rows);
850 if (proceed == MagickFalse)
854 image_view=DestroyCacheView(image_view);
855 random_info=DestroyRandomInfoThreadSet(random_info);
860 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
864 % F u n c t i o n I m a g e %
868 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
870 % FunctionImage() applies a value to the image with an arithmetic, relational,
871 % or logical operator to an image. Use these operations to lighten or darken
872 % an image, to increase or decrease contrast in an image, or to produce the
873 % "negative" of an image.
875 % The format of the FunctionImage method is:
877 % MagickBooleanType FunctionImage(Image *image,
878 % const MagickFunction function,const ssize_t number_parameters,
879 % const double *parameters,ExceptionInfo *exception)
881 % A description of each parameter follows:
883 % o image: the image.
885 % o function: A channel function.
887 % o parameters: one or more parameters.
889 % o exception: return any errors or warnings in this structure.
893 static Quantum ApplyFunction(Quantum pixel,const MagickFunction function,
894 const size_t number_parameters,const double *parameters,
895 ExceptionInfo *exception)
907 case PolynomialFunction:
910 Polynomial: polynomial constants, highest to lowest order (e.g. c0*x^3+
914 for (i=0; i < (ssize_t) number_parameters; i++)
915 result=result*QuantumScale*pixel+parameters[i];
916 result*=QuantumRange;
919 case SinusoidFunction:
928 Sinusoid: frequency, phase, amplitude, bias.
930 frequency=(number_parameters >= 1) ? parameters[0] : 1.0;
931 phase=(number_parameters >= 2) ? parameters[1] : 0.0;
932 amplitude=(number_parameters >= 3) ? parameters[2] : 0.5;
933 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
934 result=(MagickRealType) (QuantumRange*(amplitude*sin((double) (2.0*
935 MagickPI*(frequency*QuantumScale*pixel+phase/360.0)))+bias));
947 Arcsin (peged at range limits for invalid results): width, center,
950 width=(number_parameters >= 1) ? parameters[0] : 1.0;
951 center=(number_parameters >= 2) ? parameters[1] : 0.5;
952 range=(number_parameters >= 3) ? parameters[2] : 1.0;
953 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
954 result=2.0/width*(QuantumScale*pixel-center);
955 if ( result <= -1.0 )
956 result=bias-range/2.0;
959 result=bias+range/2.0;
961 result=(MagickRealType) (range/MagickPI*asin((double) result)+bias);
962 result*=QuantumRange;
974 Arctan: slope, center, range, and bias.
976 slope=(number_parameters >= 1) ? parameters[0] : 1.0;
977 center=(number_parameters >= 2) ? parameters[1] : 0.5;
978 range=(number_parameters >= 3) ? parameters[2] : 1.0;
979 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
980 result=(MagickRealType) (MagickPI*slope*(QuantumScale*pixel-center));
981 result=(MagickRealType) (QuantumRange*(range/MagickPI*atan((double)
985 case UndefinedFunction:
988 return(ClampToQuantum(result));
991 MagickExport MagickBooleanType FunctionImage(Image *image,
992 const MagickFunction function,const size_t number_parameters,
993 const double *parameters,ExceptionInfo *exception)
995 #define FunctionImageTag "Function/Image "
1009 assert(image != (Image *) NULL);
1010 assert(image->signature == MagickSignature);
1011 if (image->debug != MagickFalse)
1012 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1013 assert(exception != (ExceptionInfo *) NULL);
1014 assert(exception->signature == MagickSignature);
1015 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1016 return(MagickFalse);
1019 image_view=AcquireCacheView(image);
1020 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1021 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1023 for (y=0; y < (ssize_t) image->rows; y++)
1031 if (status == MagickFalse)
1033 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1034 if (q == (Quantum *) NULL)
1039 for (x=0; x < (ssize_t) image->columns; x++)
1044 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1052 channel=GetPixelChannelMapChannel(image,i);
1053 traits=GetPixelChannelMapTraits(image,channel);
1054 if (traits == UndefinedPixelTrait)
1056 if ((traits & UpdatePixelTrait) == 0)
1058 q[i]=ApplyFunction(q[i],function,number_parameters,parameters,
1061 q+=GetPixelChannels(image);
1063 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1065 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1070 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1071 #pragma omp critical (MagickCore_FunctionImage)
1073 proceed=SetImageProgress(image,FunctionImageTag,progress++,image->rows);
1074 if (proceed == MagickFalse)
1078 image_view=DestroyCacheView(image_view);
1083 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1087 % G e t I m a g e E x t r e m a %
1091 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1093 % GetImageExtrema() returns the extrema of one or more image channels.
1095 % The format of the GetImageExtrema method is:
1097 % MagickBooleanType GetImageExtrema(const Image *image,size_t *minima,
1098 % size_t *maxima,ExceptionInfo *exception)
1100 % A description of each parameter follows:
1102 % o image: the image.
1104 % o minima: the minimum value in the channel.
1106 % o maxima: the maximum value in the channel.
1108 % o exception: return any errors or warnings in this structure.
1111 MagickExport MagickBooleanType GetImageExtrema(const Image *image,
1112 size_t *minima,size_t *maxima,ExceptionInfo *exception)
1121 assert(image != (Image *) NULL);
1122 assert(image->signature == MagickSignature);
1123 if (image->debug != MagickFalse)
1124 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1125 status=GetImageRange(image,&min,&max,exception);
1126 *minima=(size_t) ceil(min-0.5);
1127 *maxima=(size_t) floor(max+0.5);
1132 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1136 % G e t I m a g e M e a n %
1140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1142 % GetImageMean() returns the mean and standard deviation of one or more
1145 % The format of the GetImageMean method is:
1147 % MagickBooleanType GetImageMean(const Image *image,double *mean,
1148 % double *standard_deviation,ExceptionInfo *exception)
1150 % A description of each parameter follows:
1152 % o image: the image.
1154 % o mean: the average value in the channel.
1156 % o standard_deviation: the standard deviation of the channel.
1158 % o exception: return any errors or warnings in this structure.
1161 MagickExport MagickBooleanType GetImageMean(const Image *image,double *mean,
1162 double *standard_deviation,ExceptionInfo *exception)
1165 *channel_statistics;
1173 assert(image != (Image *) NULL);
1174 assert(image->signature == MagickSignature);
1175 if (image->debug != MagickFalse)
1176 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1177 channel_statistics=GetImageStatistics(image,exception);
1178 if (channel_statistics == (ChannelStatistics *) NULL)
1179 return(MagickFalse);
1181 channel_statistics[CompositePixelChannel].mean=0.0;
1182 channel_statistics[CompositePixelChannel].standard_deviation=0.0;
1183 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1191 channel=GetPixelChannelMapChannel(image,i);
1192 traits=GetPixelChannelMapTraits(image,channel);
1193 if (traits == UndefinedPixelTrait)
1195 if ((traits & UpdatePixelTrait) == 0)
1197 channel_statistics[CompositePixelChannel].mean+=channel_statistics[i].mean;
1198 channel_statistics[CompositePixelChannel].standard_deviation+=
1199 channel_statistics[i].variance-channel_statistics[i].mean*
1200 channel_statistics[i].mean;
1203 channel_statistics[CompositePixelChannel].mean/=area;
1204 channel_statistics[CompositePixelChannel].standard_deviation=
1205 sqrt(channel_statistics[CompositePixelChannel].standard_deviation/area);
1206 *mean=channel_statistics[CompositePixelChannel].mean;
1207 *standard_deviation=
1208 channel_statistics[CompositePixelChannel].standard_deviation;
1209 channel_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1210 channel_statistics);
1215 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1219 % G e t I m a g e K u r t o s i s %
1223 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1225 % GetImageKurtosis() returns the kurtosis and skewness of one or more
1228 % The format of the GetImageKurtosis method is:
1230 % MagickBooleanType GetImageKurtosis(const Image *image,double *kurtosis,
1231 % double *skewness,ExceptionInfo *exception)
1233 % A description of each parameter follows:
1235 % o image: the image.
1237 % o kurtosis: the kurtosis of the channel.
1239 % o skewness: the skewness of the channel.
1241 % o exception: return any errors or warnings in this structure.
1244 MagickExport MagickBooleanType GetImageKurtosis(const Image *image,
1245 double *kurtosis,double *skewness,ExceptionInfo *exception)
1264 assert(image != (Image *) NULL);
1265 assert(image->signature == MagickSignature);
1266 if (image->debug != MagickFalse)
1267 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1273 standard_deviation=0.0;
1276 sum_fourth_power=0.0;
1277 image_view=AcquireCacheView(image);
1278 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1279 #pragma omp parallel for schedule(dynamic) shared(status) omp_throttle(1)
1281 for (y=0; y < (ssize_t) image->rows; y++)
1283 register const Quantum
1289 if (status == MagickFalse)
1291 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1292 if (p == (const Quantum *) NULL)
1297 for (x=0; x < (ssize_t) image->columns; x++)
1302 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1310 channel=GetPixelChannelMapChannel(image,i);
1311 traits=GetPixelChannelMapTraits(image,channel);
1312 if (traits == UndefinedPixelTrait)
1314 if ((traits & UpdatePixelTrait) == 0)
1316 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1317 #pragma omp critical (MagickCore_GetImageKurtosis)
1321 sum_squares+=(double) p[i]*p[i];
1322 sum_cubes+=(double) p[i]*p[i]*p[i];
1323 sum_fourth_power+=(double) p[i]*p[i]*p[i]*p[i];
1327 p+=GetPixelChannels(image);
1330 image_view=DestroyCacheView(image_view);
1336 sum_fourth_power/=area;
1338 standard_deviation=sqrt(sum_squares-(mean*mean));
1339 if (standard_deviation != 0.0)
1341 *kurtosis=sum_fourth_power-4.0*mean*sum_cubes+6.0*mean*mean*sum_squares-
1342 3.0*mean*mean*mean*mean;
1343 *kurtosis/=standard_deviation*standard_deviation*standard_deviation*
1346 *skewness=sum_cubes-3.0*mean*sum_squares+2.0*mean*mean*mean;
1347 *skewness/=standard_deviation*standard_deviation*standard_deviation;
1353 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1357 % G e t I m a g e R a n g e %
1361 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1363 % GetImageRange() returns the range of one or more image channels.
1365 % The format of the GetImageRange method is:
1367 % MagickBooleanType GetImageRange(const Image *image,double *minima,
1368 % double *maxima,ExceptionInfo *exception)
1370 % A description of each parameter follows:
1372 % o image: the image.
1374 % o minima: the minimum value in the channel.
1376 % o maxima: the maximum value in the channel.
1378 % o exception: return any errors or warnings in this structure.
1381 MagickExport MagickBooleanType GetImageRange(const Image *image,double *minima,
1382 double *maxima,ExceptionInfo *exception)
1393 assert(image != (Image *) NULL);
1394 assert(image->signature == MagickSignature);
1395 if (image->debug != MagickFalse)
1396 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1398 *maxima=(-MagickHuge);
1400 image_view=AcquireCacheView(image);
1401 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1402 #pragma omp parallel for schedule(dynamic) shared(status) omp_throttle(1)
1404 for (y=0; y < (ssize_t) image->rows; y++)
1406 register const Quantum
1412 if (status == MagickFalse)
1414 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1415 if (p == (const Quantum *) NULL)
1420 for (x=0; x < (ssize_t) image->columns; x++)
1425 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1433 channel=GetPixelChannelMapChannel(image,i);
1434 traits=GetPixelChannelMapTraits(image,channel);
1435 if (traits == UndefinedPixelTrait)
1437 if ((traits & UpdatePixelTrait) == 0)
1439 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1440 #pragma omp critical (MagickCore_GetImageRange)
1444 *minima=(double) p[i];
1446 *maxima=(double) p[i];
1449 p+=GetPixelChannels(image);
1452 image_view=DestroyCacheView(image_view);
1457 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1461 % G e t I m a g e S t a t i s t i c s %
1465 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1467 % GetImageStatistics() returns statistics for each channel in the
1468 % image. The statistics include the channel depth, its minima, maxima, mean,
1469 % standard deviation, kurtosis and skewness. You can access the red channel
1470 % mean, for example, like this:
1472 % channel_statistics=GetImageStatistics(image,exception);
1473 % red_mean=channel_statistics[RedPixelChannel].mean;
1475 % Use MagickRelinquishMemory() to free the statistics buffer.
1477 % The format of the GetImageStatistics method is:
1479 % ChannelStatistics *GetImageStatistics(const Image *image,
1480 % ExceptionInfo *exception)
1482 % A description of each parameter follows:
1484 % o image: the image.
1486 % o exception: return any errors or warnings in this structure.
1490 static size_t GetImageChannels(const Image *image)
1499 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1507 channel=GetPixelChannelMapChannel(image,i);
1508 traits=GetPixelChannelMapTraits(image,channel);
1509 if ((traits & UpdatePixelTrait) != 0)
1515 MagickExport ChannelStatistics *GetImageStatistics(const Image *image,
1516 ExceptionInfo *exception)
1519 *channel_statistics;
1540 assert(image != (Image *) NULL);
1541 assert(image->signature == MagickSignature);
1542 if (image->debug != MagickFalse)
1543 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1544 channel_statistics=(ChannelStatistics *) AcquireQuantumMemory(
1545 MaxPixelChannels+1,sizeof(*channel_statistics));
1546 if (channel_statistics == (ChannelStatistics *) NULL)
1547 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1548 (void) ResetMagickMemory(channel_statistics,0,(MaxPixelChannels+1)*
1549 sizeof(*channel_statistics));
1550 for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
1552 channel_statistics[i].depth=1;
1553 channel_statistics[i].maxima=(-MagickHuge);
1554 channel_statistics[i].minima=MagickHuge;
1556 for (y=0; y < (ssize_t) image->rows; y++)
1558 register const Quantum
1564 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1565 if (p == (const Quantum *) NULL)
1567 for (x=0; x < (ssize_t) image->columns; x++)
1572 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1580 channel=GetPixelChannelMapChannel(image,i);
1581 traits=GetPixelChannelMapTraits(image,channel);
1582 if (traits == UndefinedPixelTrait)
1584 if (channel_statistics[i].depth != MAGICKCORE_QUANTUM_DEPTH)
1586 depth=channel_statistics[i].depth;
1587 range=GetQuantumRange(depth);
1588 status=p[i] != ScaleAnyToQuantum(ScaleQuantumToAny(p[i],range),
1589 range) ? MagickTrue : MagickFalse;
1590 if (status != MagickFalse)
1592 channel_statistics[i].depth++;
1596 if ((double) p[i] < channel_statistics[i].minima)
1597 channel_statistics[i].minima=(double) p[i];
1598 if ((double) p[i] > channel_statistics[i].maxima)
1599 channel_statistics[i].maxima=(double) p[i];
1600 channel_statistics[i].sum+=p[i];
1601 channel_statistics[i].sum_squared+=(double) p[i]*p[i];
1602 channel_statistics[i].sum_cubed+=(double) p[i]*p[i]*p[i];
1603 channel_statistics[i].sum_fourth_power+=(double) p[i]*p[i]*p[i]*p[i];
1605 p+=GetPixelChannels(image);
1608 area=(double) image->columns*image->rows;
1609 for (i=0; i < (ssize_t) MaxPixelChannels; i++)
1611 channel_statistics[i].sum/=area;
1612 channel_statistics[i].sum_squared/=area;
1613 channel_statistics[i].sum_cubed/=area;
1614 channel_statistics[i].sum_fourth_power/=area;
1615 channel_statistics[i].mean=channel_statistics[i].sum;
1616 channel_statistics[i].variance=channel_statistics[i].sum_squared;
1617 channel_statistics[i].standard_deviation=sqrt(
1618 channel_statistics[i].variance-(channel_statistics[i].mean*
1619 channel_statistics[i].mean));
1621 for (i=0; i < (ssize_t) MaxPixelChannels; i++)
1623 channel_statistics[CompositePixelChannel].depth=(size_t) EvaluateMax(
1624 (double) channel_statistics[CompositePixelChannel].depth,(double)
1625 channel_statistics[i].depth);
1626 channel_statistics[CompositePixelChannel].minima=MagickMin(
1627 channel_statistics[CompositePixelChannel].minima,
1628 channel_statistics[i].minima);
1629 channel_statistics[CompositePixelChannel].maxima=EvaluateMax(
1630 channel_statistics[CompositePixelChannel].maxima,
1631 channel_statistics[i].maxima);
1632 channel_statistics[CompositePixelChannel].sum+=channel_statistics[i].sum;
1633 channel_statistics[CompositePixelChannel].sum_squared+=
1634 channel_statistics[i].sum_squared;
1635 channel_statistics[CompositePixelChannel].sum_cubed+=
1636 channel_statistics[i].sum_cubed;
1637 channel_statistics[CompositePixelChannel].sum_fourth_power+=
1638 channel_statistics[i].sum_fourth_power;
1639 channel_statistics[CompositePixelChannel].mean+=channel_statistics[i].mean;
1640 channel_statistics[CompositePixelChannel].variance+=
1641 channel_statistics[i].variance-channel_statistics[i].mean*
1642 channel_statistics[i].mean;
1643 channel_statistics[CompositePixelChannel].standard_deviation+=
1644 channel_statistics[i].variance-channel_statistics[i].mean*
1645 channel_statistics[i].mean;
1647 channels=GetImageChannels(image);
1648 channel_statistics[CompositePixelChannel].sum/=channels;
1649 channel_statistics[CompositePixelChannel].sum_squared/=channels;
1650 channel_statistics[CompositePixelChannel].sum_cubed/=channels;
1651 channel_statistics[CompositePixelChannel].sum_fourth_power/=channels;
1652 channel_statistics[CompositePixelChannel].mean/=channels;
1653 channel_statistics[CompositePixelChannel].variance/=channels;
1654 channel_statistics[CompositePixelChannel].standard_deviation=
1655 sqrt(channel_statistics[CompositePixelChannel].standard_deviation/channels);
1656 channel_statistics[CompositePixelChannel].kurtosis/=channels;
1657 channel_statistics[CompositePixelChannel].skewness/=channels;
1658 for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
1660 if (channel_statistics[i].standard_deviation == 0.0)
1662 channel_statistics[i].skewness=(channel_statistics[i].sum_cubed-3.0*
1663 channel_statistics[i].mean*channel_statistics[i].sum_squared+2.0*
1664 channel_statistics[i].mean*channel_statistics[i].mean*
1665 channel_statistics[i].mean)/(channel_statistics[i].standard_deviation*
1666 channel_statistics[i].standard_deviation*
1667 channel_statistics[i].standard_deviation);
1668 channel_statistics[i].kurtosis=(channel_statistics[i].sum_fourth_power-4.0*
1669 channel_statistics[i].mean*channel_statistics[i].sum_cubed+6.0*
1670 channel_statistics[i].mean*channel_statistics[i].mean*
1671 channel_statistics[i].sum_squared-3.0*channel_statistics[i].mean*
1672 channel_statistics[i].mean*1.0*channel_statistics[i].mean*
1673 channel_statistics[i].mean)/(channel_statistics[i].standard_deviation*
1674 channel_statistics[i].standard_deviation*
1675 channel_statistics[i].standard_deviation*
1676 channel_statistics[i].standard_deviation)-3.0;
1678 return(channel_statistics);
1682 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1686 % S t a t i s t i c I m a g e %
1690 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1692 % StatisticImage() makes each pixel the min / max / median / mode / etc. of
1693 % the neighborhood of the specified width and height.
1695 % The format of the StatisticImage method is:
1697 % Image *StatisticImage(const Image *image,const StatisticType type,
1698 % const size_t width,const size_t height,ExceptionInfo *exception)
1700 % A description of each parameter follows:
1702 % o image: the image.
1704 % o type: the statistic type (median, mode, etc.).
1706 % o width: the width of the pixel neighborhood.
1708 % o height: the height of the pixel neighborhood.
1710 % o exception: return any errors or warnings in this structure.
1714 typedef struct _SkipNode
1722 typedef struct _SkipList
1731 typedef struct _PixelList
1744 static PixelList *DestroyPixelList(PixelList *pixel_list)
1746 if (pixel_list == (PixelList *) NULL)
1747 return((PixelList *) NULL);
1748 if (pixel_list->skip_list.nodes != (SkipNode *) NULL)
1749 pixel_list->skip_list.nodes=(SkipNode *) RelinquishMagickMemory(
1750 pixel_list->skip_list.nodes);
1751 pixel_list=(PixelList *) RelinquishMagickMemory(pixel_list);
1755 static PixelList **DestroyPixelListThreadSet(PixelList **pixel_list)
1760 assert(pixel_list != (PixelList **) NULL);
1761 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
1762 if (pixel_list[i] != (PixelList *) NULL)
1763 pixel_list[i]=DestroyPixelList(pixel_list[i]);
1764 pixel_list=(PixelList **) RelinquishMagickMemory(pixel_list);
1768 static PixelList *AcquirePixelList(const size_t width,const size_t height)
1773 pixel_list=(PixelList *) AcquireMagickMemory(sizeof(*pixel_list));
1774 if (pixel_list == (PixelList *) NULL)
1776 (void) ResetMagickMemory((void *) pixel_list,0,sizeof(*pixel_list));
1777 pixel_list->length=width*height;
1778 pixel_list->skip_list.nodes=(SkipNode *) AcquireQuantumMemory(65537UL,
1779 sizeof(*pixel_list->skip_list.nodes));
1780 if (pixel_list->skip_list.nodes == (SkipNode *) NULL)
1781 return(DestroyPixelList(pixel_list));
1782 (void) ResetMagickMemory(pixel_list->skip_list.nodes,0,65537UL*
1783 sizeof(*pixel_list->skip_list.nodes));
1784 pixel_list->signature=MagickSignature;
1788 static PixelList **AcquirePixelListThreadSet(const size_t width,
1789 const size_t height)
1800 number_threads=GetOpenMPMaximumThreads();
1801 pixel_list=(PixelList **) AcquireQuantumMemory(number_threads,
1802 sizeof(*pixel_list));
1803 if (pixel_list == (PixelList **) NULL)
1804 return((PixelList **) NULL);
1805 (void) ResetMagickMemory(pixel_list,0,number_threads*sizeof(*pixel_list));
1806 for (i=0; i < (ssize_t) number_threads; i++)
1808 pixel_list[i]=AcquirePixelList(width,height);
1809 if (pixel_list[i] == (PixelList *) NULL)
1810 return(DestroyPixelListThreadSet(pixel_list));
1815 static void AddNodePixelList(PixelList *pixel_list,const size_t color)
1828 Initialize the node.
1830 p=(&pixel_list->skip_list);
1831 p->nodes[color].signature=pixel_list->signature;
1832 p->nodes[color].count=1;
1834 Determine where it belongs in the list.
1837 for (level=p->level; level >= 0; level--)
1839 while (p->nodes[search].next[level] < color)
1840 search=p->nodes[search].next[level];
1841 update[level]=search;
1844 Generate a pseudo-random level for this node.
1846 for (level=0; ; level++)
1848 pixel_list->seed=(pixel_list->seed*42893621L)+1L;
1849 if ((pixel_list->seed & 0x300) != 0x300)
1854 if (level > (p->level+2))
1857 If we're raising the list's level, link back to the root node.
1859 while (level > p->level)
1862 update[p->level]=65536UL;
1865 Link the node into the skip-list.
1869 p->nodes[color].next[level]=p->nodes[update[level]].next[level];
1870 p->nodes[update[level]].next[level]=color;
1871 } while (level-- > 0);
1874 static inline void GetMaximumPixelList(PixelList *pixel_list,Quantum *pixel)
1887 Find the maximum value for each of the color.
1889 p=(&pixel_list->skip_list);
1892 maximum=p->nodes[color].next[0];
1895 color=p->nodes[color].next[0];
1896 if (color > maximum)
1898 count+=p->nodes[color].count;
1899 } while (count < (ssize_t) pixel_list->length);
1900 *pixel=ScaleShortToQuantum((unsigned short) maximum);
1903 static inline void GetMeanPixelList(PixelList *pixel_list,Quantum *pixel)
1918 Find the mean value for each of the color.
1920 p=(&pixel_list->skip_list);
1926 color=p->nodes[color].next[0];
1927 sum+=(MagickRealType) p->nodes[color].count*color;
1928 count+=p->nodes[color].count;
1929 } while (count < (ssize_t) pixel_list->length);
1930 sum/=pixel_list->length;
1931 *pixel=ScaleShortToQuantum((unsigned short) sum);
1934 static inline void GetMedianPixelList(PixelList *pixel_list,Quantum *pixel)
1946 Find the median value for each of the color.
1948 p=(&pixel_list->skip_list);
1953 color=p->nodes[color].next[0];
1954 count+=p->nodes[color].count;
1955 } while (count <= (ssize_t) (pixel_list->length >> 1));
1956 *pixel=ScaleShortToQuantum((unsigned short) color);
1959 static inline void GetMinimumPixelList(PixelList *pixel_list,Quantum *pixel)
1972 Find the minimum value for each of the color.
1974 p=(&pixel_list->skip_list);
1977 minimum=p->nodes[color].next[0];
1980 color=p->nodes[color].next[0];
1981 if (color < minimum)
1983 count+=p->nodes[color].count;
1984 } while (count < (ssize_t) pixel_list->length);
1985 *pixel=ScaleShortToQuantum((unsigned short) minimum);
1988 static inline void GetModePixelList(PixelList *pixel_list,Quantum *pixel)
2002 Make each pixel the 'predominant color' of the specified neighborhood.
2004 p=(&pixel_list->skip_list);
2007 max_count=p->nodes[mode].count;
2011 color=p->nodes[color].next[0];
2012 if (p->nodes[color].count > max_count)
2015 max_count=p->nodes[mode].count;
2017 count+=p->nodes[color].count;
2018 } while (count < (ssize_t) pixel_list->length);
2019 *pixel=ScaleShortToQuantum((unsigned short) mode);
2022 static inline void GetNonpeakPixelList(PixelList *pixel_list,Quantum *pixel)
2036 Finds the non peak value for each of the colors.
2038 p=(&pixel_list->skip_list);
2040 next=p->nodes[color].next[0];
2046 next=p->nodes[color].next[0];
2047 count+=p->nodes[color].count;
2048 } while (count <= (ssize_t) (pixel_list->length >> 1));
2049 if ((previous == 65536UL) && (next != 65536UL))
2052 if ((previous != 65536UL) && (next == 65536UL))
2054 *pixel=ScaleShortToQuantum((unsigned short) color);
2057 static inline void GetStandardDeviationPixelList(PixelList *pixel_list,
2074 Find the standard-deviation value for each of the color.
2076 p=(&pixel_list->skip_list);
2086 color=p->nodes[color].next[0];
2087 sum+=(MagickRealType) p->nodes[color].count*color;
2088 for (i=0; i < (ssize_t) p->nodes[color].count; i++)
2089 sum_squared+=((MagickRealType) color)*((MagickRealType) color);
2090 count+=p->nodes[color].count;
2091 } while (count < (ssize_t) pixel_list->length);
2092 sum/=pixel_list->length;
2093 sum_squared/=pixel_list->length;
2094 *pixel=ScaleShortToQuantum((unsigned short) sqrt(sum_squared-(sum*sum)));
2097 static inline void InsertPixelList(const Image *image,const Quantum pixel,
2098 PixelList *pixel_list)
2106 index=ScaleQuantumToShort(pixel);
2107 signature=pixel_list->skip_list.nodes[index].signature;
2108 if (signature == pixel_list->signature)
2110 pixel_list->skip_list.nodes[index].count++;
2113 AddNodePixelList(pixel_list,index);
2116 static inline MagickRealType MagickAbsoluteValue(const MagickRealType x)
2123 static inline size_t MagickMax(const size_t x,const size_t y)
2130 static void ResetPixelList(PixelList *pixel_list)
2142 Reset the skip-list.
2144 p=(&pixel_list->skip_list);
2145 root=p->nodes+65536UL;
2147 for (level=0; level < 9; level++)
2148 root->next[level]=65536UL;
2149 pixel_list->seed=pixel_list->signature++;
2152 MagickExport Image *StatisticImage(const Image *image,const StatisticType type,
2153 const size_t width,const size_t height,ExceptionInfo *exception)
2155 #define StatisticImageTag "Statistic/Image"
2171 **restrict pixel_list;
2178 Initialize statistics image attributes.
2180 assert(image != (Image *) NULL);
2181 assert(image->signature == MagickSignature);
2182 if (image->debug != MagickFalse)
2183 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2184 assert(exception != (ExceptionInfo *) NULL);
2185 assert(exception->signature == MagickSignature);
2186 statistic_image=CloneImage(image,image->columns,image->rows,MagickTrue,
2188 if (statistic_image == (Image *) NULL)
2189 return((Image *) NULL);
2190 status=SetImageStorageClass(statistic_image,DirectClass,exception);
2191 if (status == MagickFalse)
2193 statistic_image=DestroyImage(statistic_image);
2194 return((Image *) NULL);
2196 pixel_list=AcquirePixelListThreadSet(MagickMax(width,1),MagickMax(height,1));
2197 if (pixel_list == (PixelList **) NULL)
2199 statistic_image=DestroyImage(statistic_image);
2200 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2203 Make each pixel the min / max / median / mode / etc. of the neighborhood.
2205 center=(ssize_t) GetPixelChannels(image)*(image->columns+MagickMax(width,1))*
2206 (MagickMax(height,1)/2L)+GetPixelChannels(image)*(MagickMax(width,1)/2L);
2209 image_view=AcquireCacheView(image);
2210 statistic_view=AcquireCacheView(statistic_image);
2211 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2212 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2214 for (y=0; y < (ssize_t) statistic_image->rows; y++)
2217 id = GetOpenMPThreadId();
2219 register const Quantum
2228 if (status == MagickFalse)
2230 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) MagickMax(width,1)/2L),y-
2231 (ssize_t) (MagickMax(height,1)/2L),image->columns+MagickMax(width,1),
2232 MagickMax(height,1),exception);
2233 q=QueueCacheViewAuthenticPixels(statistic_view,0,y,statistic_image->columns, 1,exception);
2234 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2239 for (x=0; x < (ssize_t) statistic_image->columns; x++)
2244 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2256 register const Quantum
2265 channel=GetPixelChannelMapChannel(image,i);
2266 traits=GetPixelChannelMapTraits(image,channel);
2267 statistic_traits=GetPixelChannelMapTraits(statistic_image,channel);
2268 if ((traits == UndefinedPixelTrait) ||
2269 (statistic_traits == UndefinedPixelTrait))
2271 if ((statistic_traits & CopyPixelTrait) != 0)
2273 SetPixelChannel(statistic_image,channel,p[center+i],q);
2277 ResetPixelList(pixel_list[id]);
2278 for (v=0; v < (ssize_t) MagickMax(height,1); v++)
2280 for (u=0; u < (ssize_t) MagickMax(width,1); u++)
2282 InsertPixelList(image,pixels[i],pixel_list[id]);
2283 pixels+=GetPixelChannels(image);
2285 pixels+=image->columns*GetPixelChannels(image);
2289 case GradientStatistic:
2295 GetMinimumPixelList(pixel_list[id],&pixel);
2296 minimum=(MagickRealType) pixel;
2297 GetMaximumPixelList(pixel_list[id],&pixel);
2298 maximum=(MagickRealType) pixel;
2299 pixel=ClampToQuantum(MagickAbsoluteValue(maximum-minimum));
2302 case MaximumStatistic:
2304 GetMaximumPixelList(pixel_list[id],&pixel);
2309 GetMeanPixelList(pixel_list[id],&pixel);
2312 case MedianStatistic:
2315 GetMedianPixelList(pixel_list[id],&pixel);
2318 case MinimumStatistic:
2320 GetMinimumPixelList(pixel_list[id],&pixel);
2325 GetModePixelList(pixel_list[id],&pixel);
2328 case NonpeakStatistic:
2330 GetNonpeakPixelList(pixel_list[id],&pixel);
2333 case StandardDeviationStatistic:
2335 GetStandardDeviationPixelList(pixel_list[id],&pixel);
2339 SetPixelChannel(statistic_image,channel,pixel,q);
2341 p+=GetPixelChannels(image);
2342 q+=GetPixelChannels(statistic_image);
2344 if (SyncCacheViewAuthenticPixels(statistic_view,exception) == MagickFalse)
2346 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2351 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2352 #pragma omp critical (MagickCore_StatisticImage)
2354 proceed=SetImageProgress(image,StatisticImageTag,progress++,
2356 if (proceed == MagickFalse)
2360 statistic_view=DestroyCacheView(statistic_view);
2361 image_view=DestroyCacheView(image_view);
2362 pixel_list=DestroyPixelListThreadSet(pixel_list);
2363 return(statistic_image);