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-2013 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/property.h"
45 #include "MagickCore/animate.h"
46 #include "MagickCore/blob.h"
47 #include "MagickCore/blob-private.h"
48 #include "MagickCore/cache.h"
49 #include "MagickCore/cache-private.h"
50 #include "MagickCore/cache-view.h"
51 #include "MagickCore/client.h"
52 #include "MagickCore/color.h"
53 #include "MagickCore/color-private.h"
54 #include "MagickCore/colorspace.h"
55 #include "MagickCore/colorspace-private.h"
56 #include "MagickCore/composite.h"
57 #include "MagickCore/composite-private.h"
58 #include "MagickCore/compress.h"
59 #include "MagickCore/constitute.h"
60 #include "MagickCore/display.h"
61 #include "MagickCore/draw.h"
62 #include "MagickCore/enhance.h"
63 #include "MagickCore/exception.h"
64 #include "MagickCore/exception-private.h"
65 #include "MagickCore/gem.h"
66 #include "MagickCore/gem-private.h"
67 #include "MagickCore/geometry.h"
68 #include "MagickCore/list.h"
69 #include "MagickCore/image-private.h"
70 #include "MagickCore/magic.h"
71 #include "MagickCore/magick.h"
72 #include "MagickCore/memory_.h"
73 #include "MagickCore/module.h"
74 #include "MagickCore/monitor.h"
75 #include "MagickCore/monitor-private.h"
76 #include "MagickCore/option.h"
77 #include "MagickCore/paint.h"
78 #include "MagickCore/pixel-accessor.h"
79 #include "MagickCore/profile.h"
80 #include "MagickCore/quantize.h"
81 #include "MagickCore/quantum-private.h"
82 #include "MagickCore/random_.h"
83 #include "MagickCore/random-private.h"
84 #include "MagickCore/resource_.h"
85 #include "MagickCore/segment.h"
86 #include "MagickCore/semaphore.h"
87 #include "MagickCore/signature-private.h"
88 #include "MagickCore/statistic.h"
89 #include "MagickCore/string_.h"
90 #include "MagickCore/thread-private.h"
91 #include "MagickCore/timer.h"
92 #include "MagickCore/utility.h"
93 #include "MagickCore/version.h"
96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
100 % E v a l u a t e I m a g e %
104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 % EvaluateImage() applies a value to the image with an arithmetic, relational,
107 % or logical operator to an image. Use these operations to lighten or darken
108 % an image, to increase or decrease contrast in an image, or to produce the
109 % "negative" of an image.
111 % The format of the EvaluateImage method is:
113 % MagickBooleanType EvaluateImage(Image *image,
114 % const MagickEvaluateOperator op,const double value,
115 % ExceptionInfo *exception)
116 % MagickBooleanType EvaluateImages(Image *images,
117 % const MagickEvaluateOperator op,const double value,
118 % ExceptionInfo *exception)
120 % A description of each parameter follows:
122 % o image: the image.
124 % o op: A channel op.
126 % o value: A value value.
128 % o exception: return any errors or warnings in this structure.
132 typedef struct _PixelChannels
135 channel[CompositePixelChannel];
138 static PixelChannels **DestroyPixelThreadSet(PixelChannels **pixels)
143 assert(pixels != (PixelChannels **) NULL);
144 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
145 if (pixels[i] != (PixelChannels *) NULL)
146 pixels[i]=(PixelChannels *) RelinquishMagickMemory(pixels[i]);
147 pixels=(PixelChannels **) RelinquishMagickMemory(pixels);
151 static PixelChannels **AcquirePixelThreadSet(const Image *image,
152 const size_t number_images)
164 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
165 pixels=(PixelChannels **) AcquireQuantumMemory(number_threads,
167 if (pixels == (PixelChannels **) NULL)
168 return((PixelChannels **) NULL);
169 (void) ResetMagickMemory(pixels,0,number_threads*sizeof(*pixels));
170 for (i=0; i < (ssize_t) number_threads; i++)
175 length=image->columns;
176 if (length < number_images)
177 length=number_images;
178 pixels[i]=(PixelChannels *) AcquireQuantumMemory(length,sizeof(**pixels));
179 if (pixels[i] == (PixelChannels *) NULL)
180 return(DestroyPixelThreadSet(pixels));
181 for (j=0; j < (ssize_t) length; j++)
186 for (k=0; k < MaxPixelChannels; k++)
187 pixels[i][j].channel[k]=0.0;
193 static inline double EvaluateMax(const double x,const double y)
200 #if defined(__cplusplus) || defined(c_plusplus)
204 static int IntensityCompare(const void *x,const void *y)
216 color_1=(const PixelChannels *) x;
217 color_2=(const PixelChannels *) y;
219 for (i=0; i < MaxPixelChannels; i++)
220 distance+=color_1->channel[i]-(double) color_2->channel[i];
221 return(distance < 0 ? -1 : distance > 0 ? 1 : 0);
224 #if defined(__cplusplus) || defined(c_plusplus)
228 static inline double MagickMin(const double x,const double y)
235 static double ApplyEvaluateOperator(RandomInfo *random_info,const Quantum pixel,
236 const MagickEvaluateOperator op,const double value)
244 case UndefinedEvaluateOperator:
246 case AbsEvaluateOperator:
248 result=(double) fabs((double) (pixel+value));
251 case AddEvaluateOperator:
253 result=(double) (pixel+value);
256 case AddModulusEvaluateOperator:
259 This returns a 'floored modulus' of the addition which is a positive
260 result. It differs from % or fmod() that returns a 'truncated modulus'
261 result, where floor() is replaced by trunc() and could return a
262 negative result (which is clipped).
265 result-=(QuantumRange+1.0)*floor((double) result/(QuantumRange+1.0));
268 case AndEvaluateOperator:
270 result=(double) ((size_t) pixel & (size_t) (value+0.5));
273 case CosineEvaluateOperator:
275 result=(double) (QuantumRange*(0.5*cos((double) (2.0*MagickPI*
276 QuantumScale*pixel*value))+0.5));
279 case DivideEvaluateOperator:
281 result=pixel/(value == 0.0 ? 1.0 : value);
284 case ExponentialEvaluateOperator:
286 result=(double) (QuantumRange*exp((double) (value*QuantumScale*pixel)));
289 case GaussianNoiseEvaluateOperator:
291 result=(double) GenerateDifferentialNoise(random_info,pixel,
292 GaussianNoise,value);
295 case ImpulseNoiseEvaluateOperator:
297 result=(double) GenerateDifferentialNoise(random_info,pixel,ImpulseNoise,
301 case LaplacianNoiseEvaluateOperator:
303 result=(double) GenerateDifferentialNoise(random_info,pixel,
304 LaplacianNoise,value);
307 case LeftShiftEvaluateOperator:
309 result=(double) ((size_t) pixel << (size_t) (value+0.5));
312 case LogEvaluateOperator:
314 if ((QuantumScale*pixel) >= MagickEpsilon)
315 result=(double) (QuantumRange*log((double) (QuantumScale*value*pixel+
316 1.0))/log((double) (value+1.0)));
319 case MaxEvaluateOperator:
321 result=(double) EvaluateMax((double) pixel,value);
324 case MeanEvaluateOperator:
326 result=(double) (pixel+value);
329 case MedianEvaluateOperator:
331 result=(double) (pixel+value);
334 case MinEvaluateOperator:
336 result=(double) MagickMin((double) pixel,value);
339 case MultiplicativeNoiseEvaluateOperator:
341 result=(double) GenerateDifferentialNoise(random_info,pixel,
342 MultiplicativeGaussianNoise,value);
345 case MultiplyEvaluateOperator:
347 result=(double) (value*pixel);
350 case OrEvaluateOperator:
352 result=(double) ((size_t) pixel | (size_t) (value+0.5));
355 case PoissonNoiseEvaluateOperator:
357 result=(double) GenerateDifferentialNoise(random_info,pixel,PoissonNoise,
361 case PowEvaluateOperator:
363 result=(double) (QuantumRange*pow((double) (QuantumScale*pixel),(double)
367 case RightShiftEvaluateOperator:
369 result=(double) ((size_t) pixel >> (size_t) (value+0.5));
372 case SetEvaluateOperator:
377 case SineEvaluateOperator:
379 result=(double) (QuantumRange*(0.5*sin((double) (2.0*MagickPI*
380 QuantumScale*pixel*value))+0.5));
383 case SubtractEvaluateOperator:
385 result=(double) (pixel-value);
388 case SumEvaluateOperator:
390 result=(double) (pixel+value);
393 case ThresholdEvaluateOperator:
395 result=(double) (((double) pixel <= value) ? 0 : QuantumRange);
398 case ThresholdBlackEvaluateOperator:
400 result=(double) (((double) pixel <= value) ? 0 : pixel);
403 case ThresholdWhiteEvaluateOperator:
405 result=(double) (((double) pixel > value) ? QuantumRange : pixel);
408 case UniformNoiseEvaluateOperator:
410 result=(double) GenerateDifferentialNoise(random_info,pixel,UniformNoise,
414 case XorEvaluateOperator:
416 result=(double) ((size_t) pixel ^ (size_t) (value+0.5));
423 MagickExport Image *EvaluateImages(const Image *images,
424 const MagickEvaluateOperator op,ExceptionInfo *exception)
426 #define EvaluateImageTag "Evaluate/Image"
441 **restrict evaluate_pixels;
444 **restrict random_info;
452 #if defined(MAGICKCORE_OPENMP_SUPPORT)
457 assert(images != (Image *) NULL);
458 assert(images->signature == MagickSignature);
459 if (images->debug != MagickFalse)
460 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
461 assert(exception != (ExceptionInfo *) NULL);
462 assert(exception->signature == MagickSignature);
463 image=CloneImage(images,images->columns,images->rows,MagickTrue,
465 if (image == (Image *) NULL)
466 return((Image *) NULL);
467 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
469 image=DestroyImage(image);
470 return((Image *) NULL);
472 number_images=GetImageListLength(images);
473 evaluate_pixels=AcquirePixelThreadSet(images,number_images);
474 if (evaluate_pixels == (PixelChannels **) NULL)
476 image=DestroyImage(image);
477 (void) ThrowMagickException(exception,GetMagickModule(),
478 ResourceLimitError,"MemoryAllocationFailed","`%s'",images->filename);
479 return((Image *) NULL);
482 Evaluate image pixels.
486 random_info=AcquireRandomInfoThreadSet();
487 #if defined(MAGICKCORE_OPENMP_SUPPORT)
488 key=GetRandomSecretKey(random_info[0]);
490 evaluate_view=AcquireAuthenticCacheView(image,exception);
491 if (op == MedianEvaluateOperator)
493 #if defined(MAGICKCORE_OPENMP_SUPPORT)
494 #pragma omp parallel for schedule(static,4) shared(progress,status) \
495 magick_threads(image,images,image->rows,key == ~0UL)
497 for (y=0; y < (ssize_t) image->rows; y++)
506 id = GetOpenMPThreadId();
508 register PixelChannels
517 if (status == MagickFalse)
519 q=QueueCacheViewAuthenticPixels(evaluate_view,0,y,image->columns,1,
521 if (q == (Quantum *) NULL)
526 evaluate_pixel=evaluate_pixels[id];
527 for (x=0; x < (ssize_t) image->columns; x++)
533 for (j=0; j < (ssize_t) number_images; j++)
534 for (k=0; k < MaxPixelChannels; k++)
535 evaluate_pixel[j].channel[k]=0.0;
537 for (j=0; j < (ssize_t) number_images; j++)
539 register const Quantum
545 image_view=AcquireVirtualCacheView(next,exception);
546 p=GetCacheViewVirtualPixels(image_view,x,y,1,1,exception);
547 if (p == (const Quantum *) NULL)
549 image_view=DestroyCacheView(image_view);
552 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
554 PixelChannel channel=GetPixelChannelChannel(image,i);
555 PixelTrait evaluate_traits=GetPixelChannelTraits(image,channel);
556 PixelTrait traits=GetPixelChannelTraits(next,channel);
557 if ((traits == UndefinedPixelTrait) ||
558 (evaluate_traits == UndefinedPixelTrait))
560 if ((evaluate_traits & UpdatePixelTrait) == 0)
562 evaluate_pixel[j].channel[i]=ApplyEvaluateOperator(
563 random_info[id],GetPixelChannel(image,channel,p),op,
564 evaluate_pixel[j].channel[i]);
566 image_view=DestroyCacheView(image_view);
567 next=GetNextImageInList(next);
569 qsort((void *) evaluate_pixel,number_images,sizeof(*evaluate_pixel),
571 for (k=0; k < (ssize_t) GetPixelChannels(image); k++)
572 q[k]=ClampToQuantum(evaluate_pixel[j/2].channel[k]);
573 q+=GetPixelChannels(image);
575 if (SyncCacheViewAuthenticPixels(evaluate_view,exception) == MagickFalse)
577 if (images->progress_monitor != (MagickProgressMonitor) NULL)
582 #if defined(MAGICKCORE_OPENMP_SUPPORT)
583 #pragma omp critical (MagickCore_EvaluateImages)
585 proceed=SetImageProgress(images,EvaluateImageTag,progress++,
587 if (proceed == MagickFalse)
594 #if defined(MAGICKCORE_OPENMP_SUPPORT)
595 #pragma omp parallel for schedule(static,4) shared(progress,status) \
596 magick_threads(image,images,image->rows,key == ~0UL)
598 for (y=0; y < (ssize_t) image->rows; y++)
607 id = GetOpenMPThreadId();
613 register PixelChannels
622 if (status == MagickFalse)
624 q=QueueCacheViewAuthenticPixels(evaluate_view,0,y,image->columns,1,
626 if (q == (Quantum *) NULL)
631 evaluate_pixel=evaluate_pixels[id];
632 for (j=0; j < (ssize_t) image->columns; j++)
633 for (i=0; i < MaxPixelChannels; i++)
634 evaluate_pixel[j].channel[i]=0.0;
636 for (j=0; j < (ssize_t) number_images; j++)
638 register const Quantum
641 image_view=AcquireVirtualCacheView(next,exception);
642 p=GetCacheViewVirtualPixels(image_view,0,y,next->columns,1,exception);
643 if (p == (const Quantum *) NULL)
645 image_view=DestroyCacheView(image_view);
648 for (x=0; x < (ssize_t) next->columns; x++)
653 if (GetPixelMask(next,p) != 0)
655 p+=GetPixelChannels(next);
658 for (i=0; i < (ssize_t) GetPixelChannels(next); i++)
660 PixelChannel channel=GetPixelChannelChannel(image,i);
661 PixelTrait traits=GetPixelChannelTraits(next,channel);
662 PixelTrait evaluate_traits=GetPixelChannelTraits(image,channel);
663 if ((traits == UndefinedPixelTrait) ||
664 (evaluate_traits == UndefinedPixelTrait))
666 if ((traits & UpdatePixelTrait) == 0)
668 evaluate_pixel[x].channel[i]=ApplyEvaluateOperator(
669 random_info[id],GetPixelChannel(image,channel,p),j == 0 ?
670 AddEvaluateOperator : op,evaluate_pixel[x].channel[i]);
672 p+=GetPixelChannels(next);
674 image_view=DestroyCacheView(image_view);
675 next=GetNextImageInList(next);
677 for (x=0; x < (ssize_t) image->columns; x++)
684 case MeanEvaluateOperator:
686 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
687 evaluate_pixel[x].channel[i]/=(double) number_images;
690 case MultiplyEvaluateOperator:
692 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
697 for (j=0; j < (ssize_t) (number_images-1); j++)
698 evaluate_pixel[x].channel[i]*=QuantumScale;
706 for (x=0; x < (ssize_t) image->columns; x++)
711 if (GetPixelMask(image,q) != 0)
713 q+=GetPixelChannels(image);
716 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
718 PixelChannel channel=GetPixelChannelChannel(image,i);
719 PixelTrait traits=GetPixelChannelTraits(image,channel);
720 if (traits == UndefinedPixelTrait)
722 if ((traits & UpdatePixelTrait) == 0)
724 q[i]=ClampToQuantum(evaluate_pixel[x].channel[i]);
726 q+=GetPixelChannels(image);
728 if (SyncCacheViewAuthenticPixels(evaluate_view,exception) == MagickFalse)
730 if (images->progress_monitor != (MagickProgressMonitor) NULL)
735 #if defined(MAGICKCORE_OPENMP_SUPPORT)
736 #pragma omp critical (MagickCore_EvaluateImages)
738 proceed=SetImageProgress(images,EvaluateImageTag,progress++,
740 if (proceed == MagickFalse)
745 evaluate_view=DestroyCacheView(evaluate_view);
746 evaluate_pixels=DestroyPixelThreadSet(evaluate_pixels);
747 random_info=DestroyRandomInfoThreadSet(random_info);
748 if (status == MagickFalse)
749 image=DestroyImage(image);
753 MagickExport MagickBooleanType EvaluateImage(Image *image,
754 const MagickEvaluateOperator op,const double value,ExceptionInfo *exception)
766 **restrict random_info;
771 #if defined(MAGICKCORE_OPENMP_SUPPORT)
776 assert(image != (Image *) NULL);
777 assert(image->signature == MagickSignature);
778 if (image->debug != MagickFalse)
779 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
780 assert(exception != (ExceptionInfo *) NULL);
781 assert(exception->signature == MagickSignature);
782 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
786 random_info=AcquireRandomInfoThreadSet();
787 #if defined(MAGICKCORE_OPENMP_SUPPORT)
788 key=GetRandomSecretKey(random_info[0]);
790 image_view=AcquireAuthenticCacheView(image,exception);
791 #if defined(MAGICKCORE_OPENMP_SUPPORT)
792 #pragma omp parallel for schedule(static,4) shared(progress,status) \
793 magick_threads(image,image,image->rows,key == ~0UL)
795 for (y=0; y < (ssize_t) image->rows; y++)
798 id = GetOpenMPThreadId();
806 if (status == MagickFalse)
808 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
809 if (q == (Quantum *) NULL)
814 for (x=0; x < (ssize_t) image->columns; x++)
819 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
821 PixelChannel channel=GetPixelChannelChannel(image,i);
822 PixelTrait traits=GetPixelChannelTraits(image,channel);
823 if (traits == UndefinedPixelTrait)
825 if (((traits & CopyPixelTrait) != 0) ||
826 (GetPixelMask(image,q) != 0))
828 q[i]=ClampToQuantum(ApplyEvaluateOperator(random_info[id],q[i],op,
831 q+=GetPixelChannels(image);
833 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
835 if (image->progress_monitor != (MagickProgressMonitor) NULL)
840 #if defined(MAGICKCORE_OPENMP_SUPPORT)
841 #pragma omp critical (MagickCore_EvaluateImage)
843 proceed=SetImageProgress(image,EvaluateImageTag,progress++,image->rows);
844 if (proceed == MagickFalse)
848 image_view=DestroyCacheView(image_view);
849 random_info=DestroyRandomInfoThreadSet(random_info);
854 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
858 % F u n c t i o n I m a g e %
862 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
864 % FunctionImage() applies a value to the image with an arithmetic, relational,
865 % or logical operator to an image. Use these operations to lighten or darken
866 % an image, to increase or decrease contrast in an image, or to produce the
867 % "negative" of an image.
869 % The format of the FunctionImage method is:
871 % MagickBooleanType FunctionImage(Image *image,
872 % const MagickFunction function,const ssize_t number_parameters,
873 % const double *parameters,ExceptionInfo *exception)
875 % A description of each parameter follows:
877 % o image: the image.
879 % o function: A channel function.
881 % o parameters: one or more parameters.
883 % o exception: return any errors or warnings in this structure.
887 static Quantum ApplyFunction(Quantum pixel,const MagickFunction function,
888 const size_t number_parameters,const double *parameters,
889 ExceptionInfo *exception)
901 case PolynomialFunction:
904 Polynomial: polynomial constants, highest to lowest order (e.g. c0*x^3+
908 for (i=0; i < (ssize_t) number_parameters; i++)
909 result=result*QuantumScale*pixel+parameters[i];
910 result*=QuantumRange;
913 case SinusoidFunction:
922 Sinusoid: frequency, phase, amplitude, bias.
924 frequency=(number_parameters >= 1) ? parameters[0] : 1.0;
925 phase=(number_parameters >= 2) ? parameters[1] : 0.0;
926 amplitude=(number_parameters >= 3) ? parameters[2] : 0.5;
927 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
928 result=(double) (QuantumRange*(amplitude*sin((double) (2.0*
929 MagickPI*(frequency*QuantumScale*pixel+phase/360.0)))+bias));
941 Arcsin (peged at range limits for invalid results): width, center,
944 width=(number_parameters >= 1) ? parameters[0] : 1.0;
945 center=(number_parameters >= 2) ? parameters[1] : 0.5;
946 range=(number_parameters >= 3) ? parameters[2] : 1.0;
947 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
948 result=2.0/width*(QuantumScale*pixel-center);
949 if ( result <= -1.0 )
950 result=bias-range/2.0;
953 result=bias+range/2.0;
955 result=(double) (range/MagickPI*asin((double) result)+bias);
956 result*=QuantumRange;
968 Arctan: slope, center, range, and bias.
970 slope=(number_parameters >= 1) ? parameters[0] : 1.0;
971 center=(number_parameters >= 2) ? parameters[1] : 0.5;
972 range=(number_parameters >= 3) ? parameters[2] : 1.0;
973 bias=(number_parameters >= 4) ? parameters[3] : 0.5;
974 result=(double) (MagickPI*slope*(QuantumScale*pixel-center));
975 result=(double) (QuantumRange*(range/MagickPI*atan((double)
979 case UndefinedFunction:
982 return(ClampToQuantum(result));
985 MagickExport MagickBooleanType FunctionImage(Image *image,
986 const MagickFunction function,const size_t number_parameters,
987 const double *parameters,ExceptionInfo *exception)
989 #define FunctionImageTag "Function/Image "
1003 assert(image != (Image *) NULL);
1004 assert(image->signature == MagickSignature);
1005 if (image->debug != MagickFalse)
1006 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1007 assert(exception != (ExceptionInfo *) NULL);
1008 assert(exception->signature == MagickSignature);
1009 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1010 return(MagickFalse);
1013 image_view=AcquireAuthenticCacheView(image,exception);
1014 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1015 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1016 magick_threads(image,image,image->rows,1)
1018 for (y=0; y < (ssize_t) image->rows; y++)
1026 if (status == MagickFalse)
1028 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1029 if (q == (Quantum *) NULL)
1034 for (x=0; x < (ssize_t) image->columns; x++)
1039 if (GetPixelMask(image,q) != 0)
1041 q+=GetPixelChannels(image);
1044 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1046 PixelChannel channel=GetPixelChannelChannel(image,i);
1047 PixelTrait traits=GetPixelChannelTraits(image,channel);
1048 if (traits == UndefinedPixelTrait)
1050 if ((traits & UpdatePixelTrait) == 0)
1052 q[i]=ApplyFunction(q[i],function,number_parameters,parameters,
1055 q+=GetPixelChannels(image);
1057 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1059 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1064 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1065 #pragma omp critical (MagickCore_FunctionImage)
1067 proceed=SetImageProgress(image,FunctionImageTag,progress++,image->rows);
1068 if (proceed == MagickFalse)
1072 image_view=DestroyCacheView(image_view);
1077 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1081 % G e t I m a g e E x t r e m a %
1085 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1087 % GetImageExtrema() returns the extrema of one or more image channels.
1089 % The format of the GetImageExtrema method is:
1091 % MagickBooleanType GetImageExtrema(const Image *image,size_t *minima,
1092 % size_t *maxima,ExceptionInfo *exception)
1094 % A description of each parameter follows:
1096 % o image: the image.
1098 % o minima: the minimum value in the channel.
1100 % o maxima: the maximum value in the channel.
1102 % o exception: return any errors or warnings in this structure.
1105 MagickExport MagickBooleanType GetImageExtrema(const Image *image,
1106 size_t *minima,size_t *maxima,ExceptionInfo *exception)
1115 assert(image != (Image *) NULL);
1116 assert(image->signature == MagickSignature);
1117 if (image->debug != MagickFalse)
1118 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1119 status=GetImageRange(image,&min,&max,exception);
1120 *minima=(size_t) ceil(min-0.5);
1121 *maxima=(size_t) floor(max+0.5);
1126 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1130 % G e t I m a g e M e a n %
1134 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1136 % GetImageMean() returns the mean and standard deviation of one or more image
1139 % The format of the GetImageMean method is:
1141 % MagickBooleanType GetImageMean(const Image *image,double *mean,
1142 % double *standard_deviation,ExceptionInfo *exception)
1144 % A description of each parameter follows:
1146 % o image: the image.
1148 % o mean: the average value in the channel.
1150 % o standard_deviation: the standard deviation of the channel.
1152 % o exception: return any errors or warnings in this structure.
1155 MagickExport MagickBooleanType GetImageMean(const Image *image,double *mean,
1156 double *standard_deviation,ExceptionInfo *exception)
1162 *channel_statistics;
1167 assert(image != (Image *) NULL);
1168 assert(image->signature == MagickSignature);
1169 if (image->debug != MagickFalse)
1170 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1171 channel_statistics=GetImageStatistics(image,exception);
1172 if (channel_statistics == (ChannelStatistics *) NULL)
1173 return(MagickFalse);
1175 channel_statistics[CompositePixelChannel].mean=0.0;
1176 channel_statistics[CompositePixelChannel].standard_deviation=0.0;
1177 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1179 PixelChannel channel=GetPixelChannelChannel(image,i);
1180 PixelTrait traits=GetPixelChannelTraits(image,channel);
1181 if (traits == UndefinedPixelTrait)
1183 if ((traits & UpdatePixelTrait) == 0)
1185 channel_statistics[CompositePixelChannel].mean+=channel_statistics[i].mean;
1186 channel_statistics[CompositePixelChannel].standard_deviation+=
1187 channel_statistics[i].variance-channel_statistics[i].mean*
1188 channel_statistics[i].mean;
1191 channel_statistics[CompositePixelChannel].mean/=area;
1192 channel_statistics[CompositePixelChannel].standard_deviation=
1193 sqrt(channel_statistics[CompositePixelChannel].standard_deviation/area);
1194 *mean=channel_statistics[CompositePixelChannel].mean;
1195 *standard_deviation=
1196 channel_statistics[CompositePixelChannel].standard_deviation;
1197 channel_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1198 channel_statistics);
1203 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1207 % G e t I m a g e K u r t o s i s %
1211 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1213 % GetImageKurtosis() returns the kurtosis and skewness of one or more image
1216 % The format of the GetImageKurtosis method is:
1218 % MagickBooleanType GetImageKurtosis(const Image *image,double *kurtosis,
1219 % double *skewness,ExceptionInfo *exception)
1221 % A description of each parameter follows:
1223 % o image: the image.
1225 % o kurtosis: the kurtosis of the channel.
1227 % o skewness: the skewness of the channel.
1229 % o exception: return any errors or warnings in this structure.
1232 MagickExport MagickBooleanType GetImageKurtosis(const Image *image,
1233 double *kurtosis,double *skewness,ExceptionInfo *exception)
1252 assert(image != (Image *) NULL);
1253 assert(image->signature == MagickSignature);
1254 if (image->debug != MagickFalse)
1255 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1261 standard_deviation=0.0;
1264 sum_fourth_power=0.0;
1265 image_view=AcquireVirtualCacheView(image,exception);
1266 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1267 #pragma omp parallel for schedule(static,4) shared(status) \
1268 magick_threads(image,image,image->rows,1)
1270 for (y=0; y < (ssize_t) image->rows; y++)
1272 register const Quantum
1278 if (status == MagickFalse)
1280 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1281 if (p == (const Quantum *) NULL)
1286 for (x=0; x < (ssize_t) image->columns; x++)
1291 if (GetPixelMask(image,p) != 0)
1293 p+=GetPixelChannels(image);
1296 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1298 PixelChannel channel=GetPixelChannelChannel(image,i);
1299 PixelTrait traits=GetPixelChannelTraits(image,channel);
1300 if (traits == UndefinedPixelTrait)
1302 if ((traits & UpdatePixelTrait) == 0)
1304 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1305 #pragma omp critical (MagickCore_GetImageKurtosis)
1309 sum_squares+=(double) p[i]*p[i];
1310 sum_cubes+=(double) p[i]*p[i]*p[i];
1311 sum_fourth_power+=(double) p[i]*p[i]*p[i]*p[i];
1315 p+=GetPixelChannels(image);
1318 image_view=DestroyCacheView(image_view);
1324 sum_fourth_power/=area;
1326 standard_deviation=sqrt(sum_squares-(mean*mean));
1327 if (standard_deviation != 0.0)
1329 *kurtosis=sum_fourth_power-4.0*mean*sum_cubes+6.0*mean*mean*sum_squares-
1330 3.0*mean*mean*mean*mean;
1331 *kurtosis/=standard_deviation*standard_deviation*standard_deviation*
1334 *skewness=sum_cubes-3.0*mean*sum_squares+2.0*mean*mean*mean;
1335 *skewness/=standard_deviation*standard_deviation*standard_deviation;
1341 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1345 % G e t I m a g e R a n g e %
1349 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1351 % GetImageRange() returns the range of one or more image channels.
1353 % The format of the GetImageRange method is:
1355 % MagickBooleanType GetImageRange(const Image *image,double *minima,
1356 % double *maxima,ExceptionInfo *exception)
1358 % A description of each parameter follows:
1360 % o image: the image.
1362 % o minima: the minimum value in the channel.
1364 % o maxima: the maximum value in the channel.
1366 % o exception: return any errors or warnings in this structure.
1369 MagickExport MagickBooleanType GetImageRange(const Image *image,double *minima,
1370 double *maxima,ExceptionInfo *exception)
1382 assert(image != (Image *) NULL);
1383 assert(image->signature == MagickSignature);
1384 if (image->debug != MagickFalse)
1385 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1387 initialize=MagickTrue;
1390 image_view=AcquireVirtualCacheView(image,exception);
1391 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1392 #pragma omp parallel for schedule(static,4) shared(status,initialize) \
1393 magick_threads(image,image,image->rows,1)
1395 for (y=0; y < (ssize_t) image->rows; y++)
1397 register const Quantum
1403 if (status == MagickFalse)
1405 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1406 if (p == (const Quantum *) NULL)
1411 for (x=0; x < (ssize_t) image->columns; x++)
1416 if (GetPixelMask(image,p) != 0)
1418 p+=GetPixelChannels(image);
1421 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1423 PixelChannel channel=GetPixelChannelChannel(image,i);
1424 PixelTrait traits=GetPixelChannelTraits(image,channel);
1425 if (traits == UndefinedPixelTrait)
1427 if ((traits & UpdatePixelTrait) == 0)
1429 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1430 #pragma omp critical (MagickCore_GetImageRange)
1433 if (initialize != MagickFalse)
1435 *minima=(double) p[i];
1436 *maxima=(double) p[i];
1437 initialize=MagickFalse;
1441 if ((double) p[i] < *minima)
1442 *minima=(double) p[i];
1443 if ((double) p[i] > *maxima)
1444 *maxima=(double) p[i];
1448 p+=GetPixelChannels(image);
1451 image_view=DestroyCacheView(image_view);
1456 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1460 % G e t I m a g e S t a t i s t i c s %
1464 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1466 % GetImageStatistics() returns statistics for each channel in the image. The
1467 % statistics include the channel depth, its minima, maxima, mean, standard
1468 % deviation, kurtosis and skewness. You can access the red channel mean, for
1469 % example, like this:
1471 % channel_statistics=GetImageStatistics(image,exception);
1472 % red_mean=channel_statistics[RedPixelChannel].mean;
1474 % Use MagickRelinquishMemory() to free the statistics buffer.
1476 % The format of the GetImageStatistics method is:
1478 % ChannelStatistics *GetImageStatistics(const Image *image,
1479 % ExceptionInfo *exception)
1481 % A description of each parameter follows:
1483 % o image: the image.
1485 % o exception: return any errors or warnings in this structure.
1489 static size_t GetImageChannels(const Image *image)
1498 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1500 PixelChannel channel=GetPixelChannelChannel(image,i);
1501 PixelTrait traits=GetPixelChannelTraits(image,channel);
1502 if (traits != UndefinedPixelTrait)
1508 MagickExport ChannelStatistics *GetImageStatistics(const Image *image,
1509 ExceptionInfo *exception)
1512 *channel_statistics;
1530 assert(image != (Image *) NULL);
1531 assert(image->signature == MagickSignature);
1532 if (image->debug != MagickFalse)
1533 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1534 channel_statistics=(ChannelStatistics *) AcquireQuantumMemory(
1535 MaxPixelChannels+1,sizeof(*channel_statistics));
1536 if (channel_statistics == (ChannelStatistics *) NULL)
1537 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1538 (void) ResetMagickMemory(channel_statistics,0,(MaxPixelChannels+1)*
1539 sizeof(*channel_statistics));
1540 for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
1542 channel_statistics[i].depth=1;
1543 channel_statistics[i].maxima=(-MagickHuge);
1544 channel_statistics[i].minima=MagickHuge;
1546 for (y=0; y < (ssize_t) image->rows; y++)
1548 register const Quantum
1554 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1555 if (p == (const Quantum *) NULL)
1557 for (x=0; x < (ssize_t) image->columns; x++)
1562 if (GetPixelMask(image,p) != 0)
1564 p+=GetPixelChannels(image);
1567 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1569 PixelChannel channel=GetPixelChannelChannel(image,i);
1570 PixelTrait traits=GetPixelChannelTraits(image,channel);
1571 if (traits == UndefinedPixelTrait)
1573 if (channel_statistics[channel].depth != MAGICKCORE_QUANTUM_DEPTH)
1575 depth=channel_statistics[channel].depth;
1576 range=GetQuantumRange(depth);
1577 status=p[i] != ScaleAnyToQuantum(ScaleQuantumToAny(p[i],range),
1578 range) ? MagickTrue : MagickFalse;
1579 if (status != MagickFalse)
1581 channel_statistics[channel].depth++;
1586 if ((double) p[i] < channel_statistics[channel].minima)
1587 channel_statistics[channel].minima=(double) p[i];
1588 if ((double) p[i] > channel_statistics[channel].maxima)
1589 channel_statistics[channel].maxima=(double) p[i];
1590 channel_statistics[channel].sum+=p[i];
1591 channel_statistics[channel].sum_squared+=(double) p[i]*p[i];
1592 channel_statistics[channel].sum_cubed+=(double) p[i]*p[i]*p[i];
1593 channel_statistics[channel].sum_fourth_power+=(double) p[i]*p[i]*p[i]*
1595 channel_statistics[channel].area++;
1597 p+=GetPixelChannels(image);
1600 for (i=0; i < (ssize_t) MaxPixelChannels; i++)
1605 area=PerceptibleReciprocal(channel_statistics[i].area);
1606 channel_statistics[i].sum*=area;
1607 channel_statistics[i].sum_squared*=area;
1608 channel_statistics[i].sum_cubed*=area;
1609 channel_statistics[i].sum_fourth_power*=area;
1610 channel_statistics[i].mean=channel_statistics[i].sum;
1611 channel_statistics[i].variance=channel_statistics[i].sum_squared;
1612 channel_statistics[i].standard_deviation=sqrt(
1613 channel_statistics[i].variance-(channel_statistics[i].mean*
1614 channel_statistics[i].mean));
1616 for (i=0; i < (ssize_t) MaxPixelChannels; i++)
1618 channel_statistics[CompositePixelChannel].area+=channel_statistics[i].area;
1619 channel_statistics[CompositePixelChannel].minima=MagickMin(
1620 channel_statistics[CompositePixelChannel].minima,
1621 channel_statistics[i].minima);
1622 channel_statistics[CompositePixelChannel].maxima=EvaluateMax(
1623 channel_statistics[CompositePixelChannel].maxima,
1624 channel_statistics[i].maxima);
1625 channel_statistics[CompositePixelChannel].sum+=channel_statistics[i].sum;
1626 channel_statistics[CompositePixelChannel].sum_squared+=
1627 channel_statistics[i].sum_squared;
1628 channel_statistics[CompositePixelChannel].sum_cubed+=
1629 channel_statistics[i].sum_cubed;
1630 channel_statistics[CompositePixelChannel].sum_fourth_power+=
1631 channel_statistics[i].sum_fourth_power;
1632 channel_statistics[CompositePixelChannel].mean+=channel_statistics[i].mean;
1633 channel_statistics[CompositePixelChannel].variance+=
1634 channel_statistics[i].variance-channel_statistics[i].mean*
1635 channel_statistics[i].mean;
1636 channel_statistics[CompositePixelChannel].standard_deviation+=
1637 channel_statistics[i].variance-channel_statistics[i].mean*
1638 channel_statistics[i].mean;
1640 channels=GetImageChannels(image);
1641 channel_statistics[CompositePixelChannel].area/=channels;
1642 channel_statistics[CompositePixelChannel].sum/=channels;
1643 channel_statistics[CompositePixelChannel].sum_squared/=channels;
1644 channel_statistics[CompositePixelChannel].sum_cubed/=channels;
1645 channel_statistics[CompositePixelChannel].sum_fourth_power/=channels;
1646 channel_statistics[CompositePixelChannel].mean/=channels;
1647 channel_statistics[CompositePixelChannel].variance/=channels;
1648 channel_statistics[CompositePixelChannel].standard_deviation=
1649 sqrt(channel_statistics[CompositePixelChannel].standard_deviation/channels);
1650 channel_statistics[CompositePixelChannel].kurtosis/=channels;
1651 channel_statistics[CompositePixelChannel].skewness/=channels;
1652 for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
1657 if (channel_statistics[i].standard_deviation == 0.0)
1659 standard_deviation=PerceptibleReciprocal(
1660 channel_statistics[i].standard_deviation);
1661 channel_statistics[i].skewness=(channel_statistics[i].sum_cubed-3.0*
1662 channel_statistics[i].mean*channel_statistics[i].sum_squared+2.0*
1663 channel_statistics[i].mean*channel_statistics[i].mean*
1664 channel_statistics[i].mean)*(standard_deviation*standard_deviation*
1665 standard_deviation);
1666 channel_statistics[i].kurtosis=(channel_statistics[i].sum_fourth_power-4.0*
1667 channel_statistics[i].mean*channel_statistics[i].sum_cubed+6.0*
1668 channel_statistics[i].mean*channel_statistics[i].mean*
1669 channel_statistics[i].sum_squared-3.0*channel_statistics[i].mean*
1670 channel_statistics[i].mean*1.0*channel_statistics[i].mean*
1671 channel_statistics[i].mean)*(standard_deviation*standard_deviation*
1672 standard_deviation*standard_deviation)-3.0;
1674 return(channel_statistics);
1678 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1682 % P o l y n o m i a l I m a g e %
1686 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1688 % PolynomialImage() returns a new image where each pixel is the sum of the
1689 % pixels in the image sequence after applying its corresponding terms
1690 % (coefficient and degree pairs).
1692 % The format of the PolynomialImage method is:
1694 % Image *PolynomialImage(const Image *images,const size_t number_terms,
1695 % const double *terms,ExceptionInfo *exception)
1697 % A description of each parameter follows:
1699 % o images: the image sequence.
1701 % o number_terms: the number of terms in the list. The actual list length
1702 % is 2 x number_terms + 1 (the constant).
1704 % o terms: the list of polynomial coefficients and degree pairs and a
1707 % o exception: return any errors or warnings in this structure.
1711 MagickExport Image *PolynomialImage(const Image *images,
1712 const size_t number_terms,const double *terms,ExceptionInfo *exception)
1714 #define PolynomialImageTag "Polynomial/Image"
1729 **restrict polynomial_pixels;
1737 assert(images != (Image *) NULL);
1738 assert(images->signature == MagickSignature);
1739 if (images->debug != MagickFalse)
1740 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
1741 assert(exception != (ExceptionInfo *) NULL);
1742 assert(exception->signature == MagickSignature);
1743 image=CloneImage(images,images->columns,images->rows,MagickTrue,
1745 if (image == (Image *) NULL)
1746 return((Image *) NULL);
1747 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1749 image=DestroyImage(image);
1750 return((Image *) NULL);
1752 number_images=GetImageListLength(images);
1753 polynomial_pixels=AcquirePixelThreadSet(images,number_images);
1754 if (polynomial_pixels == (PixelChannels **) NULL)
1756 image=DestroyImage(image);
1757 (void) ThrowMagickException(exception,GetMagickModule(),
1758 ResourceLimitError,"MemoryAllocationFailed","`%s'",images->filename);
1759 return((Image *) NULL);
1762 Polynomial image pixels.
1766 polynomial_view=AcquireAuthenticCacheView(image,exception);
1767 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1768 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1769 magick_threads(image,image,image->rows,1)
1771 for (y=0; y < (ssize_t) image->rows; y++)
1780 id = GetOpenMPThreadId();
1786 register PixelChannels
1795 if (status == MagickFalse)
1797 q=QueueCacheViewAuthenticPixels(polynomial_view,0,y,image->columns,1,
1799 if (q == (Quantum *) NULL)
1804 polynomial_pixel=polynomial_pixels[id];
1805 for (j=0; j < (ssize_t) image->columns; j++)
1806 for (i=0; i < MaxPixelChannels; i++)
1807 polynomial_pixel[j].channel[i]=0.0;
1809 for (j=0; j < (ssize_t) number_images; j++)
1811 register const Quantum
1814 if (j >= (ssize_t) number_terms)
1816 image_view=AcquireVirtualCacheView(next,exception);
1817 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1818 if (p == (const Quantum *) NULL)
1820 image_view=DestroyCacheView(image_view);
1823 for (x=0; x < (ssize_t) image->columns; x++)
1828 if (GetPixelMask(next,p) != 0)
1830 p+=GetPixelChannels(next);
1833 for (i=0; i < (ssize_t) GetPixelChannels(next); i++)
1839 PixelChannel channel=GetPixelChannelChannel(image,i);
1840 PixelTrait traits=GetPixelChannelTraits(next,channel);
1841 PixelTrait polynomial_traits=GetPixelChannelTraits(image,channel);
1842 if ((traits == UndefinedPixelTrait) ||
1843 (polynomial_traits == UndefinedPixelTrait))
1845 if ((traits & UpdatePixelTrait) == 0)
1847 coefficient=(MagickRealType) terms[2*i];
1848 degree=(MagickRealType) terms[(i << 1)+1];
1849 polynomial_pixel[x].channel[i]+=coefficient*
1850 pow(QuantumScale*GetPixelChannel(image,channel,p),degree);
1852 p+=GetPixelChannels(next);
1854 image_view=DestroyCacheView(image_view);
1855 next=GetNextImageInList(next);
1857 for (x=0; x < (ssize_t) image->columns; x++)
1862 if (GetPixelMask(image,q) != 0)
1864 q+=GetPixelChannels(image);
1867 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1869 PixelChannel channel=GetPixelChannelChannel(image,i);
1870 PixelTrait traits=GetPixelChannelTraits(image,channel);
1871 if (traits == UndefinedPixelTrait)
1873 if ((traits & UpdatePixelTrait) == 0)
1875 q[i]=ClampToQuantum(QuantumRange*polynomial_pixel[x].channel[i]);
1877 q+=GetPixelChannels(image);
1879 if (SyncCacheViewAuthenticPixels(polynomial_view,exception) == MagickFalse)
1881 if (images->progress_monitor != (MagickProgressMonitor) NULL)
1886 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1887 #pragma omp critical (MagickCore_PolynomialImages)
1889 proceed=SetImageProgress(images,PolynomialImageTag,progress++,
1891 if (proceed == MagickFalse)
1895 polynomial_view=DestroyCacheView(polynomial_view);
1896 polynomial_pixels=DestroyPixelThreadSet(polynomial_pixels);
1897 if (status == MagickFalse)
1898 image=DestroyImage(image);
1903 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1907 % S t a t i s t i c I m a g e %
1911 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1913 % StatisticImage() makes each pixel the min / max / median / mode / etc. of
1914 % the neighborhood of the specified width and height.
1916 % The format of the StatisticImage method is:
1918 % Image *StatisticImage(const Image *image,const StatisticType type,
1919 % const size_t width,const size_t height,ExceptionInfo *exception)
1921 % A description of each parameter follows:
1923 % o image: the image.
1925 % o type: the statistic type (median, mode, etc.).
1927 % o width: the width of the pixel neighborhood.
1929 % o height: the height of the pixel neighborhood.
1931 % o exception: return any errors or warnings in this structure.
1935 typedef struct _SkipNode
1943 typedef struct _SkipList
1952 typedef struct _PixelList
1965 static PixelList *DestroyPixelList(PixelList *pixel_list)
1967 if (pixel_list == (PixelList *) NULL)
1968 return((PixelList *) NULL);
1969 if (pixel_list->skip_list.nodes != (SkipNode *) NULL)
1970 pixel_list->skip_list.nodes=(SkipNode *) RelinquishMagickMemory(
1971 pixel_list->skip_list.nodes);
1972 pixel_list=(PixelList *) RelinquishMagickMemory(pixel_list);
1976 static PixelList **DestroyPixelListThreadSet(PixelList **pixel_list)
1981 assert(pixel_list != (PixelList **) NULL);
1982 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
1983 if (pixel_list[i] != (PixelList *) NULL)
1984 pixel_list[i]=DestroyPixelList(pixel_list[i]);
1985 pixel_list=(PixelList **) RelinquishMagickMemory(pixel_list);
1989 static PixelList *AcquirePixelList(const size_t width,const size_t height)
1994 pixel_list=(PixelList *) AcquireMagickMemory(sizeof(*pixel_list));
1995 if (pixel_list == (PixelList *) NULL)
1997 (void) ResetMagickMemory((void *) pixel_list,0,sizeof(*pixel_list));
1998 pixel_list->length=width*height;
1999 pixel_list->skip_list.nodes=(SkipNode *) AcquireQuantumMemory(65537UL,
2000 sizeof(*pixel_list->skip_list.nodes));
2001 if (pixel_list->skip_list.nodes == (SkipNode *) NULL)
2002 return(DestroyPixelList(pixel_list));
2003 (void) ResetMagickMemory(pixel_list->skip_list.nodes,0,65537UL*
2004 sizeof(*pixel_list->skip_list.nodes));
2005 pixel_list->signature=MagickSignature;
2009 static PixelList **AcquirePixelListThreadSet(const size_t width,
2010 const size_t height)
2021 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
2022 pixel_list=(PixelList **) AcquireQuantumMemory(number_threads,
2023 sizeof(*pixel_list));
2024 if (pixel_list == (PixelList **) NULL)
2025 return((PixelList **) NULL);
2026 (void) ResetMagickMemory(pixel_list,0,number_threads*sizeof(*pixel_list));
2027 for (i=0; i < (ssize_t) number_threads; i++)
2029 pixel_list[i]=AcquirePixelList(width,height);
2030 if (pixel_list[i] == (PixelList *) NULL)
2031 return(DestroyPixelListThreadSet(pixel_list));
2036 static void AddNodePixelList(PixelList *pixel_list,const size_t color)
2049 Initialize the node.
2051 p=(&pixel_list->skip_list);
2052 p->nodes[color].signature=pixel_list->signature;
2053 p->nodes[color].count=1;
2055 Determine where it belongs in the list.
2058 for (level=p->level; level >= 0; level--)
2060 while (p->nodes[search].next[level] < color)
2061 search=p->nodes[search].next[level];
2062 update[level]=search;
2065 Generate a pseudo-random level for this node.
2067 for (level=0; ; level++)
2069 pixel_list->seed=(pixel_list->seed*42893621L)+1L;
2070 if ((pixel_list->seed & 0x300) != 0x300)
2075 if (level > (p->level+2))
2078 If we're raising the list's level, link back to the root node.
2080 while (level > p->level)
2083 update[p->level]=65536UL;
2086 Link the node into the skip-list.
2090 p->nodes[color].next[level]=p->nodes[update[level]].next[level];
2091 p->nodes[update[level]].next[level]=color;
2092 } while (level-- > 0);
2095 static inline void GetMaximumPixelList(PixelList *pixel_list,Quantum *pixel)
2108 Find the maximum value for each of the color.
2110 p=(&pixel_list->skip_list);
2113 maximum=p->nodes[color].next[0];
2116 color=p->nodes[color].next[0];
2117 if (color > maximum)
2119 count+=p->nodes[color].count;
2120 } while (count < (ssize_t) pixel_list->length);
2121 *pixel=ScaleShortToQuantum((unsigned short) maximum);
2124 static inline void GetMeanPixelList(PixelList *pixel_list,Quantum *pixel)
2139 Find the mean value for each of the color.
2141 p=(&pixel_list->skip_list);
2147 color=p->nodes[color].next[0];
2148 sum+=(double) p->nodes[color].count*color;
2149 count+=p->nodes[color].count;
2150 } while (count < (ssize_t) pixel_list->length);
2151 sum/=pixel_list->length;
2152 *pixel=ScaleShortToQuantum((unsigned short) sum);
2155 static inline void GetMedianPixelList(PixelList *pixel_list,Quantum *pixel)
2167 Find the median value for each of the color.
2169 p=(&pixel_list->skip_list);
2174 color=p->nodes[color].next[0];
2175 count+=p->nodes[color].count;
2176 } while (count <= (ssize_t) (pixel_list->length >> 1));
2177 *pixel=ScaleShortToQuantum((unsigned short) color);
2180 static inline void GetMinimumPixelList(PixelList *pixel_list,Quantum *pixel)
2193 Find the minimum value for each of the color.
2195 p=(&pixel_list->skip_list);
2198 minimum=p->nodes[color].next[0];
2201 color=p->nodes[color].next[0];
2202 if (color < minimum)
2204 count+=p->nodes[color].count;
2205 } while (count < (ssize_t) pixel_list->length);
2206 *pixel=ScaleShortToQuantum((unsigned short) minimum);
2209 static inline void GetModePixelList(PixelList *pixel_list,Quantum *pixel)
2223 Make each pixel the 'predominant color' of the specified neighborhood.
2225 p=(&pixel_list->skip_list);
2228 max_count=p->nodes[mode].count;
2232 color=p->nodes[color].next[0];
2233 if (p->nodes[color].count > max_count)
2236 max_count=p->nodes[mode].count;
2238 count+=p->nodes[color].count;
2239 } while (count < (ssize_t) pixel_list->length);
2240 *pixel=ScaleShortToQuantum((unsigned short) mode);
2243 static inline void GetNonpeakPixelList(PixelList *pixel_list,Quantum *pixel)
2257 Finds the non peak value for each of the colors.
2259 p=(&pixel_list->skip_list);
2261 next=p->nodes[color].next[0];
2267 next=p->nodes[color].next[0];
2268 count+=p->nodes[color].count;
2269 } while (count <= (ssize_t) (pixel_list->length >> 1));
2270 if ((previous == 65536UL) && (next != 65536UL))
2273 if ((previous != 65536UL) && (next == 65536UL))
2275 *pixel=ScaleShortToQuantum((unsigned short) color);
2278 static inline void GetStandardDeviationPixelList(PixelList *pixel_list,
2295 Find the standard-deviation value for each of the color.
2297 p=(&pixel_list->skip_list);
2307 color=p->nodes[color].next[0];
2308 sum+=(double) p->nodes[color].count*color;
2309 for (i=0; i < (ssize_t) p->nodes[color].count; i++)
2310 sum_squared+=((double) color)*((double) color);
2311 count+=p->nodes[color].count;
2312 } while (count < (ssize_t) pixel_list->length);
2313 sum/=pixel_list->length;
2314 sum_squared/=pixel_list->length;
2315 *pixel=ScaleShortToQuantum((unsigned short) sqrt(sum_squared-(sum*sum)));
2318 static inline void InsertPixelList(const Image *image,const Quantum pixel,
2319 PixelList *pixel_list)
2327 index=ScaleQuantumToShort(pixel);
2328 signature=pixel_list->skip_list.nodes[index].signature;
2329 if (signature == pixel_list->signature)
2331 pixel_list->skip_list.nodes[index].count++;
2334 AddNodePixelList(pixel_list,index);
2337 static inline double MagickAbsoluteValue(const double x)
2344 static inline size_t MagickMax(const size_t x,const size_t y)
2351 static void ResetPixelList(PixelList *pixel_list)
2363 Reset the skip-list.
2365 p=(&pixel_list->skip_list);
2366 root=p->nodes+65536UL;
2368 for (level=0; level < 9; level++)
2369 root->next[level]=65536UL;
2370 pixel_list->seed=pixel_list->signature++;
2373 MagickExport Image *StatisticImage(const Image *image,const StatisticType type,
2374 const size_t width,const size_t height,ExceptionInfo *exception)
2376 #define StatisticImageTag "Statistic/Image"
2392 **restrict pixel_list;
2399 Initialize statistics image attributes.
2401 assert(image != (Image *) NULL);
2402 assert(image->signature == MagickSignature);
2403 if (image->debug != MagickFalse)
2404 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2405 assert(exception != (ExceptionInfo *) NULL);
2406 assert(exception->signature == MagickSignature);
2407 statistic_image=CloneImage(image,image->columns,image->rows,MagickTrue,
2409 if (statistic_image == (Image *) NULL)
2410 return((Image *) NULL);
2411 status=SetImageStorageClass(statistic_image,DirectClass,exception);
2412 if (status == MagickFalse)
2414 statistic_image=DestroyImage(statistic_image);
2415 return((Image *) NULL);
2417 pixel_list=AcquirePixelListThreadSet(MagickMax(width,1),MagickMax(height,1));
2418 if (pixel_list == (PixelList **) NULL)
2420 statistic_image=DestroyImage(statistic_image);
2421 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2424 Make each pixel the min / max / median / mode / etc. of the neighborhood.
2426 center=(ssize_t) GetPixelChannels(image)*(image->columns+MagickMax(width,1))*
2427 (MagickMax(height,1)/2L)+GetPixelChannels(image)*(MagickMax(width,1)/2L);
2430 image_view=AcquireVirtualCacheView(image,exception);
2431 statistic_view=AcquireAuthenticCacheView(statistic_image,exception);
2432 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2433 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2434 magick_threads(image,statistic_image,statistic_image->rows,1)
2436 for (y=0; y < (ssize_t) statistic_image->rows; y++)
2439 id = GetOpenMPThreadId();
2441 register const Quantum
2450 if (status == MagickFalse)
2452 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) MagickMax(width,1)/2L),y-
2453 (ssize_t) (MagickMax(height,1)/2L),image->columns+MagickMax(width,1),
2454 MagickMax(height,1),exception);
2455 q=QueueCacheViewAuthenticPixels(statistic_view,0,y,statistic_image->columns, 1,exception);
2456 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2461 for (x=0; x < (ssize_t) statistic_image->columns; x++)
2466 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2471 register const Quantum
2480 PixelChannel channel=GetPixelChannelChannel(image,i);
2481 PixelTrait traits=GetPixelChannelTraits(image,channel);
2482 PixelTrait statistic_traits=GetPixelChannelTraits(statistic_image,
2484 if ((traits == UndefinedPixelTrait) ||
2485 (statistic_traits == UndefinedPixelTrait))
2487 if (((statistic_traits & CopyPixelTrait) != 0) ||
2488 (GetPixelMask(image,p) != 0))
2490 SetPixelChannel(statistic_image,channel,p[center+i],q);
2494 ResetPixelList(pixel_list[id]);
2495 for (v=0; v < (ssize_t) MagickMax(height,1); v++)
2497 for (u=0; u < (ssize_t) MagickMax(width,1); u++)
2499 InsertPixelList(image,pixels[i],pixel_list[id]);
2500 pixels+=GetPixelChannels(image);
2502 pixels+=(image->columns-1)*GetPixelChannels(image);
2506 case GradientStatistic:
2512 GetMinimumPixelList(pixel_list[id],&pixel);
2513 minimum=(double) pixel;
2514 GetMaximumPixelList(pixel_list[id],&pixel);
2515 maximum=(double) pixel;
2516 pixel=ClampToQuantum(MagickAbsoluteValue(maximum-minimum));
2519 case MaximumStatistic:
2521 GetMaximumPixelList(pixel_list[id],&pixel);
2526 GetMeanPixelList(pixel_list[id],&pixel);
2529 case MedianStatistic:
2532 GetMedianPixelList(pixel_list[id],&pixel);
2535 case MinimumStatistic:
2537 GetMinimumPixelList(pixel_list[id],&pixel);
2542 GetModePixelList(pixel_list[id],&pixel);
2545 case NonpeakStatistic:
2547 GetNonpeakPixelList(pixel_list[id],&pixel);
2550 case StandardDeviationStatistic:
2552 GetStandardDeviationPixelList(pixel_list[id],&pixel);
2556 SetPixelChannel(statistic_image,channel,pixel,q);
2558 p+=GetPixelChannels(image);
2559 q+=GetPixelChannels(statistic_image);
2561 if (SyncCacheViewAuthenticPixels(statistic_view,exception) == MagickFalse)
2563 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2568 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2569 #pragma omp critical (MagickCore_StatisticImage)
2571 proceed=SetImageProgress(image,StatisticImageTag,progress++,
2573 if (proceed == MagickFalse)
2577 statistic_view=DestroyCacheView(statistic_view);
2578 image_view=DestroyCacheView(image_view);
2579 pixel_list=DestroyPixelListThreadSet(pixel_list);
2580 if (status == MagickFalse)
2581 statistic_image=DestroyImage(statistic_image);
2582 return(statistic_image);