2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % SSSSS TTTTT AAA TTTTT IIIII SSSSS TTTTT IIIII CCCC %
7 % SS T A A T I SS T I C %
8 % SSS T AAAAA T I SSS T I C %
9 % SS T A A T I SS T I C %
10 % SSSSS T A A T IIIII SSSSS T IIIII CCCC %
13 % MagickCore Image Statistical Methods %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/property.h"
45 #include "MagickCore/animate.h"
46 #include "MagickCore/blob.h"
47 #include "MagickCore/blob-private.h"
48 #include "MagickCore/cache.h"
49 #include "MagickCore/cache-private.h"
50 #include "MagickCore/cache-view.h"
51 #include "MagickCore/client.h"
52 #include "MagickCore/color.h"
53 #include "MagickCore/color-private.h"
54 #include "MagickCore/colorspace.h"
55 #include "MagickCore/colorspace-private.h"
56 #include "MagickCore/composite.h"
57 #include "MagickCore/composite-private.h"
58 #include "MagickCore/compress.h"
59 #include "MagickCore/constitute.h"
60 #include "MagickCore/display.h"
61 #include "MagickCore/draw.h"
62 #include "MagickCore/enhance.h"
63 #include "MagickCore/exception.h"
64 #include "MagickCore/exception-private.h"
65 #include "MagickCore/gem.h"
66 #include "MagickCore/gem-private.h"
67 #include "MagickCore/geometry.h"
68 #include "MagickCore/list.h"
69 #include "MagickCore/image-private.h"
70 #include "MagickCore/magic.h"
71 #include "MagickCore/magick.h"
72 #include "MagickCore/memory_.h"
73 #include "MagickCore/module.h"
74 #include "MagickCore/monitor.h"
75 #include "MagickCore/monitor-private.h"
76 #include "MagickCore/option.h"
77 #include "MagickCore/paint.h"
78 #include "MagickCore/pixel-accessor.h"
79 #include "MagickCore/profile.h"
80 #include "MagickCore/quantize.h"
81 #include "MagickCore/quantum-private.h"
82 #include "MagickCore/random_.h"
83 #include "MagickCore/random-private.h"
84 #include "MagickCore/segment.h"
85 #include "MagickCore/semaphore.h"
86 #include "MagickCore/signature-private.h"
87 #include "MagickCore/statistic.h"
88 #include "MagickCore/string_.h"
89 #include "MagickCore/thread-private.h"
90 #include "MagickCore/timer.h"
91 #include "MagickCore/utility.h"
92 #include "MagickCore/version.h"
95 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99 % E v a l u a t e I m a g e %
103 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105 % EvaluateImage() applies a value to the image with an arithmetic, relational,
106 % or logical operator to an image. Use these operations to lighten or darken
107 % an image, to increase or decrease contrast in an image, or to produce the
108 % "negative" of an image.
110 % The format of the EvaluateImage method is:
112 % MagickBooleanType EvaluateImage(Image *image,
113 % const MagickEvaluateOperator op,const double value,
114 % ExceptionInfo *exception)
115 % MagickBooleanType EvaluateImages(Image *images,
116 % const MagickEvaluateOperator op,const double value,
117 % ExceptionInfo *exception)
119 % A description of each parameter follows:
121 % o image: the image.
123 % o op: A channel op.
125 % o value: A value value.
127 % o exception: return any errors or warnings in this structure.
131 typedef struct _PixelChannels
134 channel[CompositePixelChannel];
137 static PixelChannels **DestroyPixelThreadSet(PixelChannels **pixels)
142 assert(pixels != (PixelChannels **) NULL);
143 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
144 if (pixels[i] != (PixelChannels *) NULL)
145 pixels[i]=(PixelChannels *) RelinquishMagickMemory(pixels[i]);
146 pixels=(PixelChannels **) RelinquishMagickMemory(pixels);
150 static PixelChannels **AcquirePixelThreadSet(const Image *image,
151 const size_t number_images)
163 number_threads=GetOpenMPMaximumThreads();
164 pixels=(PixelChannels **) AcquireQuantumMemory(number_threads,
166 if (pixels == (PixelChannels **) NULL)
167 return((PixelChannels **) NULL);
168 (void) ResetMagickMemory(pixels,0,number_threads*sizeof(*pixels));
169 for (i=0; i < (ssize_t) number_threads; i++)
174 length=image->columns;
175 if (length < number_images)
176 length=number_images;
177 pixels[i]=(PixelChannels *) AcquireQuantumMemory(length,sizeof(**pixels));
178 if (pixels[i] == (PixelChannels *) NULL)
179 return(DestroyPixelThreadSet(pixels));
180 for (j=0; j < (ssize_t) length; j++)
185 for (k=0; k < MaxPixelChannels; k++)
186 pixels[i][j].channel[k]=0.0;
192 static inline double EvaluateMax(const double x,const double y)
199 #if defined(__cplusplus) || defined(c_plusplus)
203 static int IntensityCompare(const void *x,const void *y)
215 color_1=(const PixelChannels *) x;
216 color_2=(const PixelChannels *) y;
218 for (i=0; i < MaxPixelChannels; i++)
219 distance+=color_1->channel[i]-(MagickRealType) color_2->channel[i];
220 return(distance < 0 ? -1 : distance > 0 ? 1 : 0);
223 #if defined(__cplusplus) || defined(c_plusplus)
227 static inline double MagickMin(const double x,const double y)
234 static MagickRealType ApplyEvaluateOperator(RandomInfo *random_info,
235 Quantum pixel,const MagickEvaluateOperator op,const MagickRealType value)
243 case UndefinedEvaluateOperator:
245 case AbsEvaluateOperator:
247 result=(MagickRealType) fabs((double) (pixel+value));
250 case AddEvaluateOperator:
252 result=(MagickRealType) (pixel+value);
255 case AddModulusEvaluateOperator:
258 This returns a 'floored modulus' of the addition which is a positive
259 result. It differs from % or fmod() that returns a 'truncated modulus'
260 result, where floor() is replaced by trunc() and could return a
261 negative result (which is clipped).
264 result-=(QuantumRange+1.0)*floor((double) result/(QuantumRange+1.0));
267 case AndEvaluateOperator:
269 result=(MagickRealType) ((size_t) pixel & (size_t) (value+0.5));
272 case CosineEvaluateOperator:
274 result=(MagickRealType) (QuantumRange*(0.5*cos((double) (2.0*MagickPI*
275 QuantumScale*pixel*value))+0.5));
278 case DivideEvaluateOperator:
280 result=pixel/(value == 0.0 ? 1.0 : value);
283 case ExponentialEvaluateOperator:
285 result=(MagickRealType) (QuantumRange*exp((double) (value*QuantumScale*
289 case GaussianNoiseEvaluateOperator:
291 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
292 GaussianNoise,value);
295 case ImpulseNoiseEvaluateOperator:
297 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
301 case LaplacianNoiseEvaluateOperator:
303 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
304 LaplacianNoise,value);
307 case LeftShiftEvaluateOperator:
309 result=(MagickRealType) ((size_t) pixel << (size_t) (value+0.5));
312 case LogEvaluateOperator:
314 result=(MagickRealType) (QuantumRange*log((double) (QuantumScale*value*
315 pixel+1.0))/log((double) (value+1.0)));
318 case MaxEvaluateOperator:
320 result=(MagickRealType) EvaluateMax((double) pixel,value);
323 case MeanEvaluateOperator:
325 result=(MagickRealType) (pixel+value);
328 case MedianEvaluateOperator:
330 result=(MagickRealType) (pixel+value);
333 case MinEvaluateOperator:
335 result=(MagickRealType) MagickMin((double) pixel,value);
338 case MultiplicativeNoiseEvaluateOperator:
340 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
341 MultiplicativeGaussianNoise,value);
344 case MultiplyEvaluateOperator:
346 result=(MagickRealType) (value*pixel);
349 case OrEvaluateOperator:
351 result=(MagickRealType) ((size_t) pixel | (size_t) (value+0.5));
354 case PoissonNoiseEvaluateOperator:
356 result=(MagickRealType) GenerateDifferentialNoise(random_info,pixel,
360 case PowEvaluateOperator:
362 result=(MagickRealType) (QuantumRange*pow((double) (QuantumScale*pixel),
366 case RightShiftEvaluateOperator:
368 result=(MagickRealType) ((size_t) pixel >> (size_t) (value+0.5));
371 case SetEvaluateOperator:
376 case SineEvaluateOperator:
378 result=(MagickRealType) (QuantumRange*(0.5*sin((double) (2.0*MagickPI*
379 QuantumScale*pixel*value))+0.5));
382 case SubtractEvaluateOperator:
384 result=(MagickRealType) (pixel-value);
387 case 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(static) 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(static) 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 if (GetPixelMask(evaluate_image,p) != 0)
664 p+=GetPixelChannels(evaluate_image);
667 for (i=0; i < (ssize_t) GetPixelChannels(next); i++)
676 channel=GetPixelChannelMapChannel(evaluate_image,i);
677 traits=GetPixelChannelMapTraits(next,channel);
678 evaluate_traits=GetPixelChannelMapTraits(evaluate_image,channel);
679 if ((traits == UndefinedPixelTrait) ||
680 (evaluate_traits == UndefinedPixelTrait))
682 if ((traits & UpdatePixelTrait) == 0)
684 evaluate_pixel[x].channel[i]=ApplyEvaluateOperator(
685 random_info[id],GetPixelChannel(evaluate_image,channel,p),j ==
686 0 ? AddEvaluateOperator : op,evaluate_pixel[x].channel[i]);
688 p+=GetPixelChannels(next);
690 image_view=DestroyCacheView(image_view);
691 next=GetNextImageInList(next);
693 for (x=0; x < (ssize_t) evaluate_image->columns; x++)
700 case MeanEvaluateOperator:
702 for (i=0; i < (ssize_t) GetPixelChannels(evaluate_image); i++)
703 evaluate_pixel[x].channel[i]/=(MagickRealType) number_images;
706 case MultiplyEvaluateOperator:
708 for (i=0; i < (ssize_t) GetPixelChannels(evaluate_image); i++)
713 for (j=0; j < (ssize_t) (number_images-1); j++)
714 evaluate_pixel[x].channel[i]*=QuantumScale;
722 for (x=0; x < (ssize_t) evaluate_image->columns; x++)
727 if (GetPixelMask(evaluate_image,q) != 0)
729 q+=GetPixelChannels(evaluate_image);
732 for (i=0; i < (ssize_t) GetPixelChannels(evaluate_image); i++)
740 channel=GetPixelChannelMapChannel(evaluate_image,i);
741 traits=GetPixelChannelMapTraits(evaluate_image,channel);
742 if (traits == UndefinedPixelTrait)
744 if ((traits & UpdatePixelTrait) == 0)
746 q[i]=ClampToQuantum(evaluate_pixel[x].channel[i]);
748 q+=GetPixelChannels(evaluate_image);
750 if (SyncCacheViewAuthenticPixels(evaluate_view,exception) == MagickFalse)
752 if (images->progress_monitor != (MagickProgressMonitor) NULL)
757 #if defined(MAGICKCORE_OPENMP_SUPPORT)
758 #pragma omp critical (MagickCore_EvaluateImages)
760 proceed=SetImageProgress(images,EvaluateImageTag,progress++,
761 evaluate_image->rows);
762 if (proceed == MagickFalse)
767 evaluate_view=DestroyCacheView(evaluate_view);
768 evaluate_pixels=DestroyPixelThreadSet(evaluate_pixels);
769 random_info=DestroyRandomInfoThreadSet(random_info);
770 if (status == MagickFalse)
771 evaluate_image=DestroyImage(evaluate_image);
772 return(evaluate_image);
775 MagickExport MagickBooleanType EvaluateImage(Image *image,
776 const MagickEvaluateOperator op,const double value,ExceptionInfo *exception)
788 **restrict random_info;
793 assert(image != (Image *) NULL);
794 assert(image->signature == MagickSignature);
795 if (image->debug != MagickFalse)
796 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
797 assert(exception != (ExceptionInfo *) NULL);
798 assert(exception->signature == MagickSignature);
799 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
803 random_info=AcquireRandomInfoThreadSet();
804 image_view=AcquireCacheView(image);
805 #if defined(MAGICKCORE_OPENMP_SUPPORT)
806 #pragma omp parallel for schedule(static,4) shared(progress,status)
808 for (y=0; y < (ssize_t) image->rows; y++)
811 id = GetOpenMPThreadId();
819 if (status == MagickFalse)
821 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
822 if (q == (Quantum *) NULL)
827 for (x=0; x < (ssize_t) image->columns; x++)
832 if (GetPixelMask(image,q) != 0)
834 q+=GetPixelChannels(image);
837 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
845 channel=GetPixelChannelMapChannel(image,i);
846 traits=GetPixelChannelMapTraits(image,channel);
847 if (traits == UndefinedPixelTrait)
849 q[i]=ClampToQuantum(ApplyEvaluateOperator(random_info[id],q[i],op,
852 q+=GetPixelChannels(image);
854 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
856 if (image->progress_monitor != (MagickProgressMonitor) NULL)
861 #if defined(MAGICKCORE_OPENMP_SUPPORT)
862 #pragma omp critical (MagickCore_EvaluateImage)
864 proceed=SetImageProgress(image,EvaluateImageTag,progress++,image->rows);
865 if (proceed == MagickFalse)
869 image_view=DestroyCacheView(image_view);
870 random_info=DestroyRandomInfoThreadSet(random_info);
875 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
879 % F u n c t i o n I m a g e %
883 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
885 % FunctionImage() applies a value to the image with an arithmetic, relational,
886 % or logical operator to an image. Use these operations to lighten or darken
887 % an image, to increase or decrease contrast in an image, or to produce the
888 % "negative" of an image.
890 % The format of the FunctionImage method is:
892 % MagickBooleanType FunctionImage(Image *image,
893 % const MagickFunction function,const ssize_t number_parameters,
894 % const double *parameters,ExceptionInfo *exception)
896 % A description of each parameter follows:
898 % o image: the image.
900 % o function: A channel function.
902 % o parameters: one or more parameters.
904 % o exception: return any errors or warnings in this structure.
908 static Quantum ApplyFunction(Quantum pixel,const MagickFunction function,
909 const size_t number_parameters,const double *parameters,
910 ExceptionInfo *exception)
922 case PolynomialFunction:
925 Polynomial: polynomial constants, highest to lowest order (e.g. c0*x^3+
929 for (i=0; i < (ssize_t) number_parameters; i++)
930 result=result*QuantumScale*pixel+parameters[i];
931 result*=QuantumRange;
934 case SinusoidFunction:
943 Sinusoid: frequency, phase, amplitude, bias.
945 frequency=(number_parameters >= 1) ? parameters[0] : 1.0;
946 phase=(number_parameters >= 2) ? parameters[1] : 0.0;
947 amplitude=(number_parameters >= 3) ? parameters[2] : 0.5;
948 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
949 result=(MagickRealType) (QuantumRange*(amplitude*sin((double) (2.0*
950 MagickPI*(frequency*QuantumScale*pixel+phase/360.0)))+bias));
962 Arcsin (peged at range limits for invalid results): width, center,
965 width=(number_parameters >= 1) ? parameters[0] : 1.0;
966 center=(number_parameters >= 2) ? parameters[1] : 0.5;
967 range=(number_parameters >= 3) ? parameters[2] : 1.0;
968 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
969 result=2.0/width*(QuantumScale*pixel-center);
970 if ( result <= -1.0 )
971 result=bias-range/2.0;
974 result=bias+range/2.0;
976 result=(MagickRealType) (range/MagickPI*asin((double) result)+bias);
977 result*=QuantumRange;
989 Arctan: slope, center, range, and bias.
991 slope=(number_parameters >= 1) ? parameters[0] : 1.0;
992 center=(number_parameters >= 2) ? parameters[1] : 0.5;
993 range=(number_parameters >= 3) ? parameters[2] : 1.0;
994 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
995 result=(MagickRealType) (MagickPI*slope*(QuantumScale*pixel-center));
996 result=(MagickRealType) (QuantumRange*(range/MagickPI*atan((double)
1000 case UndefinedFunction:
1003 return(ClampToQuantum(result));
1006 MagickExport MagickBooleanType FunctionImage(Image *image,
1007 const MagickFunction function,const size_t number_parameters,
1008 const double *parameters,ExceptionInfo *exception)
1010 #define FunctionImageTag "Function/Image "
1024 assert(image != (Image *) NULL);
1025 assert(image->signature == MagickSignature);
1026 if (image->debug != MagickFalse)
1027 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1028 assert(exception != (ExceptionInfo *) NULL);
1029 assert(exception->signature == MagickSignature);
1030 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1031 return(MagickFalse);
1034 image_view=AcquireCacheView(image);
1035 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1036 #pragma omp parallel for schedule(static,4) shared(progress,status)
1038 for (y=0; y < (ssize_t) image->rows; y++)
1046 if (status == MagickFalse)
1048 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1049 if (q == (Quantum *) NULL)
1054 for (x=0; x < (ssize_t) image->columns; x++)
1059 if (GetPixelMask(image,q) != 0)
1061 q+=GetPixelChannels(image);
1064 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1072 channel=GetPixelChannelMapChannel(image,i);
1073 traits=GetPixelChannelMapTraits(image,channel);
1074 if (traits == UndefinedPixelTrait)
1076 if ((traits & UpdatePixelTrait) == 0)
1078 q[i]=ApplyFunction(q[i],function,number_parameters,parameters,
1081 q+=GetPixelChannels(image);
1083 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1085 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1090 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1091 #pragma omp critical (MagickCore_FunctionImage)
1093 proceed=SetImageProgress(image,FunctionImageTag,progress++,image->rows);
1094 if (proceed == MagickFalse)
1098 image_view=DestroyCacheView(image_view);
1103 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1107 % G e t I m a g e E x t r e m a %
1111 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1113 % GetImageExtrema() returns the extrema of one or more image channels.
1115 % The format of the GetImageExtrema method is:
1117 % MagickBooleanType GetImageExtrema(const Image *image,size_t *minima,
1118 % size_t *maxima,ExceptionInfo *exception)
1120 % A description of each parameter follows:
1122 % o image: the image.
1124 % o minima: the minimum value in the channel.
1126 % o maxima: the maximum value in the channel.
1128 % o exception: return any errors or warnings in this structure.
1131 MagickExport MagickBooleanType GetImageExtrema(const Image *image,
1132 size_t *minima,size_t *maxima,ExceptionInfo *exception)
1141 assert(image != (Image *) NULL);
1142 assert(image->signature == MagickSignature);
1143 if (image->debug != MagickFalse)
1144 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1145 status=GetImageRange(image,&min,&max,exception);
1146 *minima=(size_t) ceil(min-0.5);
1147 *maxima=(size_t) floor(max+0.5);
1152 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1156 % G e t I m a g e M e a n %
1160 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1162 % GetImageMean() returns the mean and standard deviation of one or more
1165 % The format of the GetImageMean method is:
1167 % MagickBooleanType GetImageMean(const Image *image,double *mean,
1168 % double *standard_deviation,ExceptionInfo *exception)
1170 % A description of each parameter follows:
1172 % o image: the image.
1174 % o mean: the average value in the channel.
1176 % o standard_deviation: the standard deviation of the channel.
1178 % o exception: return any errors or warnings in this structure.
1181 MagickExport MagickBooleanType GetImageMean(const Image *image,double *mean,
1182 double *standard_deviation,ExceptionInfo *exception)
1185 *channel_statistics;
1193 assert(image != (Image *) NULL);
1194 assert(image->signature == MagickSignature);
1195 if (image->debug != MagickFalse)
1196 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1197 channel_statistics=GetImageStatistics(image,exception);
1198 if (channel_statistics == (ChannelStatistics *) NULL)
1199 return(MagickFalse);
1201 channel_statistics[CompositePixelChannel].mean=0.0;
1202 channel_statistics[CompositePixelChannel].standard_deviation=0.0;
1203 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1211 channel=GetPixelChannelMapChannel(image,i);
1212 traits=GetPixelChannelMapTraits(image,channel);
1213 if (traits == UndefinedPixelTrait)
1215 if ((traits & UpdatePixelTrait) == 0)
1217 channel_statistics[CompositePixelChannel].mean+=channel_statistics[i].mean;
1218 channel_statistics[CompositePixelChannel].standard_deviation+=
1219 channel_statistics[i].variance-channel_statistics[i].mean*
1220 channel_statistics[i].mean;
1223 channel_statistics[CompositePixelChannel].mean/=area;
1224 channel_statistics[CompositePixelChannel].standard_deviation=
1225 sqrt(channel_statistics[CompositePixelChannel].standard_deviation/area);
1226 *mean=channel_statistics[CompositePixelChannel].mean;
1227 *standard_deviation=
1228 channel_statistics[CompositePixelChannel].standard_deviation;
1229 channel_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1230 channel_statistics);
1235 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1239 % G e t I m a g e K u r t o s i s %
1243 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1245 % GetImageKurtosis() returns the kurtosis and skewness of one or more
1248 % The format of the GetImageKurtosis method is:
1250 % MagickBooleanType GetImageKurtosis(const Image *image,double *kurtosis,
1251 % double *skewness,ExceptionInfo *exception)
1253 % A description of each parameter follows:
1255 % o image: the image.
1257 % o kurtosis: the kurtosis of the channel.
1259 % o skewness: the skewness of the channel.
1261 % o exception: return any errors or warnings in this structure.
1264 MagickExport MagickBooleanType GetImageKurtosis(const Image *image,
1265 double *kurtosis,double *skewness,ExceptionInfo *exception)
1284 assert(image != (Image *) NULL);
1285 assert(image->signature == MagickSignature);
1286 if (image->debug != MagickFalse)
1287 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1293 standard_deviation=0.0;
1296 sum_fourth_power=0.0;
1297 image_view=AcquireCacheView(image);
1298 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1299 #pragma omp parallel for schedule(static) shared(status)
1301 for (y=0; y < (ssize_t) image->rows; y++)
1303 register const Quantum
1309 if (status == MagickFalse)
1311 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1312 if (p == (const Quantum *) NULL)
1317 for (x=0; x < (ssize_t) image->columns; x++)
1322 if (GetPixelMask(image,p) != 0)
1324 p+=GetPixelChannels(image);
1327 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1335 channel=GetPixelChannelMapChannel(image,i);
1336 traits=GetPixelChannelMapTraits(image,channel);
1337 if (traits == UndefinedPixelTrait)
1339 if ((traits & UpdatePixelTrait) == 0)
1341 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1342 #pragma omp critical (MagickCore_GetImageKurtosis)
1346 sum_squares+=(double) p[i]*p[i];
1347 sum_cubes+=(double) p[i]*p[i]*p[i];
1348 sum_fourth_power+=(double) p[i]*p[i]*p[i]*p[i];
1352 p+=GetPixelChannels(image);
1355 image_view=DestroyCacheView(image_view);
1361 sum_fourth_power/=area;
1363 standard_deviation=sqrt(sum_squares-(mean*mean));
1364 if (standard_deviation != 0.0)
1366 *kurtosis=sum_fourth_power-4.0*mean*sum_cubes+6.0*mean*mean*sum_squares-
1367 3.0*mean*mean*mean*mean;
1368 *kurtosis/=standard_deviation*standard_deviation*standard_deviation*
1371 *skewness=sum_cubes-3.0*mean*sum_squares+2.0*mean*mean*mean;
1372 *skewness/=standard_deviation*standard_deviation*standard_deviation;
1378 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1382 % G e t I m a g e R a n g e %
1386 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1388 % GetImageRange() returns the range of one or more image channels.
1390 % The format of the GetImageRange method is:
1392 % MagickBooleanType GetImageRange(const Image *image,double *minima,
1393 % double *maxima,ExceptionInfo *exception)
1395 % A description of each parameter follows:
1397 % o image: the image.
1399 % o minima: the minimum value in the channel.
1401 % o maxima: the maximum value in the channel.
1403 % o exception: return any errors or warnings in this structure.
1406 MagickExport MagickBooleanType GetImageRange(const Image *image,double *minima,
1407 double *maxima,ExceptionInfo *exception)
1418 assert(image != (Image *) NULL);
1419 assert(image->signature == MagickSignature);
1420 if (image->debug != MagickFalse)
1421 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1423 *maxima=(-MagickHuge);
1425 image_view=AcquireCacheView(image);
1426 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1427 #pragma omp parallel for schedule(static) shared(status)
1429 for (y=0; y < (ssize_t) image->rows; y++)
1431 register const Quantum
1437 if (status == MagickFalse)
1439 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1440 if (p == (const Quantum *) NULL)
1445 for (x=0; x < (ssize_t) image->columns; x++)
1450 if (GetPixelMask(image,p) != 0)
1452 p+=GetPixelChannels(image);
1455 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1463 channel=GetPixelChannelMapChannel(image,i);
1464 traits=GetPixelChannelMapTraits(image,channel);
1465 if (traits == UndefinedPixelTrait)
1467 if ((traits & UpdatePixelTrait) == 0)
1469 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1470 #pragma omp critical (MagickCore_GetImageRange)
1474 *minima=(double) p[i];
1476 *maxima=(double) p[i];
1479 p+=GetPixelChannels(image);
1482 image_view=DestroyCacheView(image_view);
1487 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1491 % G e t I m a g e S t a t i s t i c s %
1495 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1497 % GetImageStatistics() returns statistics for each channel in the
1498 % image. The statistics include the channel depth, its minima, maxima, mean,
1499 % standard deviation, kurtosis and skewness. You can access the red channel
1500 % mean, for example, like this:
1502 % channel_statistics=GetImageStatistics(image,exception);
1503 % red_mean=channel_statistics[RedPixelChannel].mean;
1505 % Use MagickRelinquishMemory() to free the statistics buffer.
1507 % The format of the GetImageStatistics method is:
1509 % ChannelStatistics *GetImageStatistics(const Image *image,
1510 % ExceptionInfo *exception)
1512 % A description of each parameter follows:
1514 % o image: the image.
1516 % o exception: return any errors or warnings in this structure.
1520 static size_t GetImageChannels(const Image *image)
1529 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1537 channel=GetPixelChannelMapChannel(image,i);
1538 traits=GetPixelChannelMapTraits(image,channel);
1539 if ((traits & UpdatePixelTrait) != 0)
1545 MagickExport ChannelStatistics *GetImageStatistics(const Image *image,
1546 ExceptionInfo *exception)
1549 *channel_statistics;
1570 assert(image != (Image *) NULL);
1571 assert(image->signature == MagickSignature);
1572 if (image->debug != MagickFalse)
1573 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1574 channel_statistics=(ChannelStatistics *) AcquireQuantumMemory(
1575 MaxPixelChannels+1,sizeof(*channel_statistics));
1576 if (channel_statistics == (ChannelStatistics *) NULL)
1577 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1578 (void) ResetMagickMemory(channel_statistics,0,(MaxPixelChannels+1)*
1579 sizeof(*channel_statistics));
1580 for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
1582 channel_statistics[i].depth=1;
1583 channel_statistics[i].maxima=(-MagickHuge);
1584 channel_statistics[i].minima=MagickHuge;
1586 for (y=0; y < (ssize_t) image->rows; y++)
1588 register const Quantum
1594 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1595 if (p == (const Quantum *) NULL)
1597 for (x=0; x < (ssize_t) image->columns; x++)
1602 if (GetPixelMask(image,p) != 0)
1604 p+=GetPixelChannels(image);
1607 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1615 channel=GetPixelChannelMapChannel(image,i);
1616 traits=GetPixelChannelMapTraits(image,channel);
1617 if (traits == UndefinedPixelTrait)
1619 if (channel_statistics[channel].depth != MAGICKCORE_QUANTUM_DEPTH)
1621 depth=channel_statistics[channel].depth;
1622 range=GetQuantumRange(depth);
1623 status=p[i] != ScaleAnyToQuantum(ScaleQuantumToAny(p[i],range),
1624 range) ? MagickTrue : MagickFalse;
1625 if (status != MagickFalse)
1627 channel_statistics[channel].depth++;
1631 if ((double) p[i] < channel_statistics[channel].minima)
1632 channel_statistics[channel].minima=(double) p[i];
1633 if ((double) p[i] > channel_statistics[channel].maxima)
1634 channel_statistics[channel].maxima=(double) p[i];
1635 channel_statistics[channel].sum+=p[i];
1636 channel_statistics[channel].sum_squared+=(double) p[i]*p[i];
1637 channel_statistics[channel].sum_cubed+=(double) p[i]*p[i]*p[i];
1638 channel_statistics[channel].sum_fourth_power+=(double) p[i]*p[i]*p[i]*
1641 p+=GetPixelChannels(image);
1644 area=(double) image->columns*image->rows;
1645 for (i=0; i < (ssize_t) MaxPixelChannels; i++)
1647 channel_statistics[i].sum/=area;
1648 channel_statistics[i].sum_squared/=area;
1649 channel_statistics[i].sum_cubed/=area;
1650 channel_statistics[i].sum_fourth_power/=area;
1651 channel_statistics[i].mean=channel_statistics[i].sum;
1652 channel_statistics[i].variance=channel_statistics[i].sum_squared;
1653 channel_statistics[i].standard_deviation=sqrt(
1654 channel_statistics[i].variance-(channel_statistics[i].mean*
1655 channel_statistics[i].mean));
1657 for (i=0; i < (ssize_t) MaxPixelChannels; i++)
1659 channel_statistics[CompositePixelChannel].depth=(size_t) EvaluateMax(
1660 (double) channel_statistics[CompositePixelChannel].depth,(double)
1661 channel_statistics[i].depth);
1662 channel_statistics[CompositePixelChannel].minima=MagickMin(
1663 channel_statistics[CompositePixelChannel].minima,
1664 channel_statistics[i].minima);
1665 channel_statistics[CompositePixelChannel].maxima=EvaluateMax(
1666 channel_statistics[CompositePixelChannel].maxima,
1667 channel_statistics[i].maxima);
1668 channel_statistics[CompositePixelChannel].sum+=channel_statistics[i].sum;
1669 channel_statistics[CompositePixelChannel].sum_squared+=
1670 channel_statistics[i].sum_squared;
1671 channel_statistics[CompositePixelChannel].sum_cubed+=
1672 channel_statistics[i].sum_cubed;
1673 channel_statistics[CompositePixelChannel].sum_fourth_power+=
1674 channel_statistics[i].sum_fourth_power;
1675 channel_statistics[CompositePixelChannel].mean+=channel_statistics[i].mean;
1676 channel_statistics[CompositePixelChannel].variance+=
1677 channel_statistics[i].variance-channel_statistics[i].mean*
1678 channel_statistics[i].mean;
1679 channel_statistics[CompositePixelChannel].standard_deviation+=
1680 channel_statistics[i].variance-channel_statistics[i].mean*
1681 channel_statistics[i].mean;
1683 channels=GetImageChannels(image);
1684 channel_statistics[CompositePixelChannel].sum/=channels;
1685 channel_statistics[CompositePixelChannel].sum_squared/=channels;
1686 channel_statistics[CompositePixelChannel].sum_cubed/=channels;
1687 channel_statistics[CompositePixelChannel].sum_fourth_power/=channels;
1688 channel_statistics[CompositePixelChannel].mean/=channels;
1689 channel_statistics[CompositePixelChannel].variance/=channels;
1690 channel_statistics[CompositePixelChannel].standard_deviation=
1691 sqrt(channel_statistics[CompositePixelChannel].standard_deviation/channels);
1692 channel_statistics[CompositePixelChannel].kurtosis/=channels;
1693 channel_statistics[CompositePixelChannel].skewness/=channels;
1694 for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
1696 if (channel_statistics[i].standard_deviation == 0.0)
1698 channel_statistics[i].skewness=(channel_statistics[i].sum_cubed-3.0*
1699 channel_statistics[i].mean*channel_statistics[i].sum_squared+2.0*
1700 channel_statistics[i].mean*channel_statistics[i].mean*
1701 channel_statistics[i].mean)/(channel_statistics[i].standard_deviation*
1702 channel_statistics[i].standard_deviation*
1703 channel_statistics[i].standard_deviation);
1704 channel_statistics[i].kurtosis=(channel_statistics[i].sum_fourth_power-4.0*
1705 channel_statistics[i].mean*channel_statistics[i].sum_cubed+6.0*
1706 channel_statistics[i].mean*channel_statistics[i].mean*
1707 channel_statistics[i].sum_squared-3.0*channel_statistics[i].mean*
1708 channel_statistics[i].mean*1.0*channel_statistics[i].mean*
1709 channel_statistics[i].mean)/(channel_statistics[i].standard_deviation*
1710 channel_statistics[i].standard_deviation*
1711 channel_statistics[i].standard_deviation*
1712 channel_statistics[i].standard_deviation)-3.0;
1714 return(channel_statistics);
1718 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1722 % S t a t i s t i c I m a g e %
1726 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1728 % StatisticImage() makes each pixel the min / max / median / mode / etc. of
1729 % the neighborhood of the specified width and height.
1731 % The format of the StatisticImage method is:
1733 % Image *StatisticImage(const Image *image,const StatisticType type,
1734 % const size_t width,const size_t height,ExceptionInfo *exception)
1736 % A description of each parameter follows:
1738 % o image: the image.
1740 % o type: the statistic type (median, mode, etc.).
1742 % o width: the width of the pixel neighborhood.
1744 % o height: the height of the pixel neighborhood.
1746 % o exception: return any errors or warnings in this structure.
1750 typedef struct _SkipNode
1758 typedef struct _SkipList
1767 typedef struct _PixelList
1780 static PixelList *DestroyPixelList(PixelList *pixel_list)
1782 if (pixel_list == (PixelList *) NULL)
1783 return((PixelList *) NULL);
1784 if (pixel_list->skip_list.nodes != (SkipNode *) NULL)
1785 pixel_list->skip_list.nodes=(SkipNode *) RelinquishMagickMemory(
1786 pixel_list->skip_list.nodes);
1787 pixel_list=(PixelList *) RelinquishMagickMemory(pixel_list);
1791 static PixelList **DestroyPixelListThreadSet(PixelList **pixel_list)
1796 assert(pixel_list != (PixelList **) NULL);
1797 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
1798 if (pixel_list[i] != (PixelList *) NULL)
1799 pixel_list[i]=DestroyPixelList(pixel_list[i]);
1800 pixel_list=(PixelList **) RelinquishMagickMemory(pixel_list);
1804 static PixelList *AcquirePixelList(const size_t width,const size_t height)
1809 pixel_list=(PixelList *) AcquireMagickMemory(sizeof(*pixel_list));
1810 if (pixel_list == (PixelList *) NULL)
1812 (void) ResetMagickMemory((void *) pixel_list,0,sizeof(*pixel_list));
1813 pixel_list->length=width*height;
1814 pixel_list->skip_list.nodes=(SkipNode *) AcquireQuantumMemory(65537UL,
1815 sizeof(*pixel_list->skip_list.nodes));
1816 if (pixel_list->skip_list.nodes == (SkipNode *) NULL)
1817 return(DestroyPixelList(pixel_list));
1818 (void) ResetMagickMemory(pixel_list->skip_list.nodes,0,65537UL*
1819 sizeof(*pixel_list->skip_list.nodes));
1820 pixel_list->signature=MagickSignature;
1824 static PixelList **AcquirePixelListThreadSet(const size_t width,
1825 const size_t height)
1836 number_threads=GetOpenMPMaximumThreads();
1837 pixel_list=(PixelList **) AcquireQuantumMemory(number_threads,
1838 sizeof(*pixel_list));
1839 if (pixel_list == (PixelList **) NULL)
1840 return((PixelList **) NULL);
1841 (void) ResetMagickMemory(pixel_list,0,number_threads*sizeof(*pixel_list));
1842 for (i=0; i < (ssize_t) number_threads; i++)
1844 pixel_list[i]=AcquirePixelList(width,height);
1845 if (pixel_list[i] == (PixelList *) NULL)
1846 return(DestroyPixelListThreadSet(pixel_list));
1851 static void AddNodePixelList(PixelList *pixel_list,const size_t color)
1864 Initialize the node.
1866 p=(&pixel_list->skip_list);
1867 p->nodes[color].signature=pixel_list->signature;
1868 p->nodes[color].count=1;
1870 Determine where it belongs in the list.
1873 for (level=p->level; level >= 0; level--)
1875 while (p->nodes[search].next[level] < color)
1876 search=p->nodes[search].next[level];
1877 update[level]=search;
1880 Generate a pseudo-random level for this node.
1882 for (level=0; ; level++)
1884 pixel_list->seed=(pixel_list->seed*42893621L)+1L;
1885 if ((pixel_list->seed & 0x300) != 0x300)
1890 if (level > (p->level+2))
1893 If we're raising the list's level, link back to the root node.
1895 while (level > p->level)
1898 update[p->level]=65536UL;
1901 Link the node into the skip-list.
1905 p->nodes[color].next[level]=p->nodes[update[level]].next[level];
1906 p->nodes[update[level]].next[level]=color;
1907 } while (level-- > 0);
1910 static inline void GetMaximumPixelList(PixelList *pixel_list,Quantum *pixel)
1923 Find the maximum value for each of the color.
1925 p=(&pixel_list->skip_list);
1928 maximum=p->nodes[color].next[0];
1931 color=p->nodes[color].next[0];
1932 if (color > maximum)
1934 count+=p->nodes[color].count;
1935 } while (count < (ssize_t) pixel_list->length);
1936 *pixel=ScaleShortToQuantum((unsigned short) maximum);
1939 static inline void GetMeanPixelList(PixelList *pixel_list,Quantum *pixel)
1954 Find the mean value for each of the color.
1956 p=(&pixel_list->skip_list);
1962 color=p->nodes[color].next[0];
1963 sum+=(MagickRealType) p->nodes[color].count*color;
1964 count+=p->nodes[color].count;
1965 } while (count < (ssize_t) pixel_list->length);
1966 sum/=pixel_list->length;
1967 *pixel=ScaleShortToQuantum((unsigned short) sum);
1970 static inline void GetMedianPixelList(PixelList *pixel_list,Quantum *pixel)
1982 Find the median value for each of the color.
1984 p=(&pixel_list->skip_list);
1989 color=p->nodes[color].next[0];
1990 count+=p->nodes[color].count;
1991 } while (count <= (ssize_t) (pixel_list->length >> 1));
1992 *pixel=ScaleShortToQuantum((unsigned short) color);
1995 static inline void GetMinimumPixelList(PixelList *pixel_list,Quantum *pixel)
2008 Find the minimum value for each of the color.
2010 p=(&pixel_list->skip_list);
2013 minimum=p->nodes[color].next[0];
2016 color=p->nodes[color].next[0];
2017 if (color < minimum)
2019 count+=p->nodes[color].count;
2020 } while (count < (ssize_t) pixel_list->length);
2021 *pixel=ScaleShortToQuantum((unsigned short) minimum);
2024 static inline void GetModePixelList(PixelList *pixel_list,Quantum *pixel)
2038 Make each pixel the 'predominant color' of the specified neighborhood.
2040 p=(&pixel_list->skip_list);
2043 max_count=p->nodes[mode].count;
2047 color=p->nodes[color].next[0];
2048 if (p->nodes[color].count > max_count)
2051 max_count=p->nodes[mode].count;
2053 count+=p->nodes[color].count;
2054 } while (count < (ssize_t) pixel_list->length);
2055 *pixel=ScaleShortToQuantum((unsigned short) mode);
2058 static inline void GetNonpeakPixelList(PixelList *pixel_list,Quantum *pixel)
2072 Finds the non peak value for each of the colors.
2074 p=(&pixel_list->skip_list);
2076 next=p->nodes[color].next[0];
2082 next=p->nodes[color].next[0];
2083 count+=p->nodes[color].count;
2084 } while (count <= (ssize_t) (pixel_list->length >> 1));
2085 if ((previous == 65536UL) && (next != 65536UL))
2088 if ((previous != 65536UL) && (next == 65536UL))
2090 *pixel=ScaleShortToQuantum((unsigned short) color);
2093 static inline void GetStandardDeviationPixelList(PixelList *pixel_list,
2110 Find the standard-deviation value for each of the color.
2112 p=(&pixel_list->skip_list);
2122 color=p->nodes[color].next[0];
2123 sum+=(MagickRealType) p->nodes[color].count*color;
2124 for (i=0; i < (ssize_t) p->nodes[color].count; i++)
2125 sum_squared+=((MagickRealType) color)*((MagickRealType) color);
2126 count+=p->nodes[color].count;
2127 } while (count < (ssize_t) pixel_list->length);
2128 sum/=pixel_list->length;
2129 sum_squared/=pixel_list->length;
2130 *pixel=ScaleShortToQuantum((unsigned short) sqrt(sum_squared-(sum*sum)));
2133 static inline void InsertPixelList(const Image *image,const Quantum pixel,
2134 PixelList *pixel_list)
2142 index=ScaleQuantumToShort(pixel);
2143 signature=pixel_list->skip_list.nodes[index].signature;
2144 if (signature == pixel_list->signature)
2146 pixel_list->skip_list.nodes[index].count++;
2149 AddNodePixelList(pixel_list,index);
2152 static inline MagickRealType MagickAbsoluteValue(const MagickRealType x)
2159 static inline size_t MagickMax(const size_t x,const size_t y)
2166 static void ResetPixelList(PixelList *pixel_list)
2178 Reset the skip-list.
2180 p=(&pixel_list->skip_list);
2181 root=p->nodes+65536UL;
2183 for (level=0; level < 9; level++)
2184 root->next[level]=65536UL;
2185 pixel_list->seed=pixel_list->signature++;
2188 MagickExport Image *StatisticImage(const Image *image,const StatisticType type,
2189 const size_t width,const size_t height,ExceptionInfo *exception)
2191 #define StatisticImageTag "Statistic/Image"
2207 **restrict pixel_list;
2214 Initialize statistics image attributes.
2216 assert(image != (Image *) NULL);
2217 assert(image->signature == MagickSignature);
2218 if (image->debug != MagickFalse)
2219 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2220 assert(exception != (ExceptionInfo *) NULL);
2221 assert(exception->signature == MagickSignature);
2222 statistic_image=CloneImage(image,image->columns,image->rows,MagickTrue,
2224 if (statistic_image == (Image *) NULL)
2225 return((Image *) NULL);
2226 status=SetImageStorageClass(statistic_image,DirectClass,exception);
2227 if (status == MagickFalse)
2229 statistic_image=DestroyImage(statistic_image);
2230 return((Image *) NULL);
2232 pixel_list=AcquirePixelListThreadSet(MagickMax(width,1),MagickMax(height,1));
2233 if (pixel_list == (PixelList **) NULL)
2235 statistic_image=DestroyImage(statistic_image);
2236 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2239 Make each pixel the min / max / median / mode / etc. of the neighborhood.
2241 center=(ssize_t) GetPixelChannels(image)*(image->columns+MagickMax(width,1))*
2242 (MagickMax(height,1)/2L)+GetPixelChannels(image)*(MagickMax(width,1)/2L);
2245 image_view=AcquireCacheView(image);
2246 statistic_view=AcquireCacheView(statistic_image);
2247 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2248 #pragma omp parallel for schedule(static,4) shared(progress,status)
2250 for (y=0; y < (ssize_t) statistic_image->rows; y++)
2253 id = GetOpenMPThreadId();
2255 register const Quantum
2264 if (status == MagickFalse)
2266 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) MagickMax(width,1)/2L),y-
2267 (ssize_t) (MagickMax(height,1)/2L),image->columns+MagickMax(width,1),
2268 MagickMax(height,1),exception);
2269 q=QueueCacheViewAuthenticPixels(statistic_view,0,y,statistic_image->columns, 1,exception);
2270 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2275 for (x=0; x < (ssize_t) statistic_image->columns; x++)
2280 if (GetPixelMask(image,p) != 0)
2282 p+=GetPixelChannels(image);
2283 q+=GetPixelChannels(statistic_image);
2286 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2298 register const Quantum
2307 channel=GetPixelChannelMapChannel(image,i);
2308 traits=GetPixelChannelMapTraits(image,channel);
2309 statistic_traits=GetPixelChannelMapTraits(statistic_image,channel);
2310 if ((traits == UndefinedPixelTrait) ||
2311 (statistic_traits == UndefinedPixelTrait))
2313 if ((statistic_traits & CopyPixelTrait) != 0)
2315 SetPixelChannel(statistic_image,channel,p[center+i],q);
2319 ResetPixelList(pixel_list[id]);
2320 for (v=0; v < (ssize_t) MagickMax(height,1); v++)
2322 for (u=0; u < (ssize_t) MagickMax(width,1); u++)
2324 InsertPixelList(image,pixels[i],pixel_list[id]);
2325 pixels+=GetPixelChannels(image);
2327 pixels+=image->columns*GetPixelChannels(image);
2331 case GradientStatistic:
2337 GetMinimumPixelList(pixel_list[id],&pixel);
2338 minimum=(MagickRealType) pixel;
2339 GetMaximumPixelList(pixel_list[id],&pixel);
2340 maximum=(MagickRealType) pixel;
2341 pixel=ClampToQuantum(MagickAbsoluteValue(maximum-minimum));
2344 case MaximumStatistic:
2346 GetMaximumPixelList(pixel_list[id],&pixel);
2351 GetMeanPixelList(pixel_list[id],&pixel);
2354 case MedianStatistic:
2357 GetMedianPixelList(pixel_list[id],&pixel);
2360 case MinimumStatistic:
2362 GetMinimumPixelList(pixel_list[id],&pixel);
2367 GetModePixelList(pixel_list[id],&pixel);
2370 case NonpeakStatistic:
2372 GetNonpeakPixelList(pixel_list[id],&pixel);
2375 case StandardDeviationStatistic:
2377 GetStandardDeviationPixelList(pixel_list[id],&pixel);
2381 SetPixelChannel(statistic_image,channel,pixel,q);
2383 p+=GetPixelChannels(image);
2384 q+=GetPixelChannels(statistic_image);
2386 if (SyncCacheViewAuthenticPixels(statistic_view,exception) == MagickFalse)
2388 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2393 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2394 #pragma omp critical (MagickCore_StatisticImage)
2396 proceed=SetImageProgress(image,StatisticImageTag,progress++,
2398 if (proceed == MagickFalse)
2402 statistic_view=DestroyCacheView(statistic_view);
2403 image_view=DestroyCacheView(image_view);
2404 pixel_list=DestroyPixelListThreadSet(pixel_list);
2405 return(statistic_image);