]> granicus.if.org Git - imagemagick/blob - MagickCore/statistic.c
(no commit message)
[imagemagick] / MagickCore / statistic.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
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        %
11 %                                                                             %
12 %                                                                             %
13 %                     MagickCore Image Statistical Methods                    %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                   Cristy                                    %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization      %
21 %  dedicated to making software imaging solutions freely available.           %
22 %                                                                             %
23 %  You may not use this file except in compliance with the License.  You may  %
24 %  obtain a copy of the License at                                            %
25 %                                                                             %
26 %    http://www.imagemagick.org/script/license.php                            %
27 %                                                                             %
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.                                             %
33 %                                                                             %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 %
38 */
39 \f
40 /*
41   Include declarations.
42 */
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"
94 \f
95 /*
96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97 %                                                                             %
98 %                                                                             %
99 %                                                                             %
100 %     E v a l u a t e I m a g e                                               %
101 %                                                                             %
102 %                                                                             %
103 %                                                                             %
104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105 %
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.
110 %
111 %  The format of the EvaluateImage method is:
112 %
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)
119 %
120 %  A description of each parameter follows:
121 %
122 %    o image: the image.
123 %
124 %    o op: A channel op.
125 %
126 %    o value: A value value.
127 %
128 %    o exception: return any errors or warnings in this structure.
129 %
130 */
131
132 typedef struct _PixelChannels
133 {
134   double
135     channel[CompositePixelChannel];
136 } PixelChannels;
137
138 static PixelChannels **DestroyPixelThreadSet(PixelChannels **pixels)
139 {
140   register ssize_t
141     i;
142
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);
148   return(pixels);
149 }
150
151 static PixelChannels **AcquirePixelThreadSet(const Image *image,
152   const size_t number_images)
153 {
154   register ssize_t
155     i;
156
157   PixelChannels
158     **pixels;
159
160   size_t
161     length,
162     number_threads;
163
164   number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
165   pixels=(PixelChannels **) AcquireQuantumMemory(number_threads,
166     sizeof(*pixels));
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++)
171   {
172     register ssize_t
173       j;
174
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++)
182     {
183       register ssize_t
184         k;
185
186       for (k=0; k < MaxPixelChannels; k++)
187         pixels[i][j].channel[k]=0.0;
188     }
189   }
190   return(pixels);
191 }
192
193 static inline double EvaluateMax(const double x,const double y)
194 {
195   if (x > y)
196     return(x);
197   return(y);
198 }
199
200 #if defined(__cplusplus) || defined(c_plusplus)
201 extern "C" {
202 #endif
203
204 static int IntensityCompare(const void *x,const void *y)
205 {
206   const PixelChannels
207     *color_1,
208     *color_2;
209
210   double
211     distance;
212
213   register ssize_t
214     i;
215
216   color_1=(const PixelChannels *) x;
217   color_2=(const PixelChannels *) y;
218   distance=0.0;
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);
222 }
223
224 #if defined(__cplusplus) || defined(c_plusplus)
225 }
226 #endif
227
228 static inline double MagickMin(const double x,const double y)
229 {
230   if (x < y)
231     return(x);
232   return(y);
233 }
234
235 static double ApplyEvaluateOperator(RandomInfo *random_info,const Quantum pixel,
236   const MagickEvaluateOperator op,const double value)
237 {
238   double
239     result;
240
241   result=0.0;
242   switch (op)
243   {
244     case UndefinedEvaluateOperator:
245       break;
246     case AbsEvaluateOperator:
247     {
248       result=(double) fabs((double) (pixel+value));
249       break;
250     }
251     case AddEvaluateOperator:
252     {
253       result=(double) (pixel+value);
254       break;
255     }
256     case AddModulusEvaluateOperator:
257     {
258       /*
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).
263       */
264       result=pixel+value;
265       result-=(QuantumRange+1.0)*floor((double) result/(QuantumRange+1.0));
266       break;
267     }
268     case AndEvaluateOperator:
269     {
270       result=(double) ((size_t) pixel & (size_t) (value+0.5));
271       break;
272     }
273     case CosineEvaluateOperator:
274     {
275       result=(double) (QuantumRange*(0.5*cos((double) (2.0*MagickPI*
276         QuantumScale*pixel*value))+0.5));
277       break;
278     }
279     case DivideEvaluateOperator:
280     {
281       result=pixel/(value == 0.0 ? 1.0 : value);
282       break;
283     }
284     case ExponentialEvaluateOperator:
285     {
286       result=(double) (QuantumRange*exp((double) (value*QuantumScale*pixel)));
287       break;
288     }
289     case GaussianNoiseEvaluateOperator:
290     {
291       result=(double) GenerateDifferentialNoise(random_info,pixel,
292         GaussianNoise,value);
293       break;
294     }
295     case ImpulseNoiseEvaluateOperator:
296     {
297       result=(double) GenerateDifferentialNoise(random_info,pixel,ImpulseNoise,
298         value);
299       break;
300     }
301     case LaplacianNoiseEvaluateOperator:
302     {
303       result=(double) GenerateDifferentialNoise(random_info,pixel,
304         LaplacianNoise,value);
305       break;
306     }
307     case LeftShiftEvaluateOperator:
308     {
309       result=(double) ((size_t) pixel << (size_t) (value+0.5));
310       break;
311     }
312     case LogEvaluateOperator:
313     {
314       if ((QuantumScale*pixel) >= MagickEpsilon)
315         result=(double) (QuantumRange*log((double) (QuantumScale*value*pixel+
316           1.0))/log((double) (value+1.0)));
317       break;
318     }
319     case MaxEvaluateOperator:
320     {
321       result=(double) EvaluateMax((double) pixel,value);
322       break;
323     }
324     case MeanEvaluateOperator:
325     {
326       result=(double) (pixel+value);
327       break;
328     }
329     case MedianEvaluateOperator:
330     {
331       result=(double) (pixel+value);
332       break;
333     }
334     case MinEvaluateOperator:
335     {
336       result=(double) MagickMin((double) pixel,value);
337       break;
338     }
339     case MultiplicativeNoiseEvaluateOperator:
340     {
341       result=(double) GenerateDifferentialNoise(random_info,pixel,
342         MultiplicativeGaussianNoise,value);
343       break;
344     }
345     case MultiplyEvaluateOperator:
346     {
347       result=(double) (value*pixel);
348       break;
349     }
350     case OrEvaluateOperator:
351     {
352       result=(double) ((size_t) pixel | (size_t) (value+0.5));
353       break;
354     }
355     case PoissonNoiseEvaluateOperator:
356     {
357       result=(double) GenerateDifferentialNoise(random_info,pixel,PoissonNoise,
358         value);
359       break;
360     }
361     case PowEvaluateOperator:
362     {
363       result=(double) (QuantumRange*pow((double) (QuantumScale*pixel),(double)
364         value));
365       break;
366     }
367     case RightShiftEvaluateOperator:
368     {
369       result=(double) ((size_t) pixel >> (size_t) (value+0.5));
370       break;
371     }
372     case SetEvaluateOperator:
373     {
374       result=value;
375       break;
376     }
377     case SineEvaluateOperator:
378     {
379       result=(double) (QuantumRange*(0.5*sin((double) (2.0*MagickPI*
380         QuantumScale*pixel*value))+0.5));
381       break;
382     }
383     case SubtractEvaluateOperator:
384     {
385       result=(double) (pixel-value);
386       break;
387     }
388     case SumEvaluateOperator:
389     {
390       result=(double) (pixel+value);
391       break;
392     }
393     case ThresholdEvaluateOperator:
394     {
395       result=(double) (((double) pixel <= value) ? 0 : QuantumRange);
396       break;
397     }
398     case ThresholdBlackEvaluateOperator:
399     {
400       result=(double) (((double) pixel <= value) ? 0 : pixel);
401       break;
402     }
403     case ThresholdWhiteEvaluateOperator:
404     {
405       result=(double) (((double) pixel > value) ? QuantumRange : pixel);
406       break;
407     }
408     case UniformNoiseEvaluateOperator:
409     {
410       result=(double) GenerateDifferentialNoise(random_info,pixel,UniformNoise,
411         value);
412       break;
413     }
414     case XorEvaluateOperator:
415     {
416       result=(double) ((size_t) pixel ^ (size_t) (value+0.5));
417       break;
418     }
419   }
420   return(result);
421 }
422
423 MagickExport Image *EvaluateImages(const Image *images,
424   const MagickEvaluateOperator op,ExceptionInfo *exception)
425 {
426 #define EvaluateImageTag  "Evaluate/Image"
427
428   CacheView
429     *evaluate_view;
430
431   Image
432     *image;
433
434   MagickBooleanType
435     status;
436
437   MagickOffsetType
438     progress;
439
440   PixelChannels
441     **restrict evaluate_pixels;
442
443   RandomInfo
444     **restrict random_info;
445
446   size_t
447     number_images;
448
449   ssize_t
450     y;
451
452 #if defined(MAGICKCORE_OPENMP_SUPPORT)
453   unsigned long
454     key;
455 #endif
456
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,
464     exception);
465   if (image == (Image *) NULL)
466     return((Image *) NULL);
467   if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
468     {
469       image=DestroyImage(image);
470       return((Image *) NULL);
471     }
472   number_images=GetImageListLength(images);
473   evaluate_pixels=AcquirePixelThreadSet(images,number_images);
474   if (evaluate_pixels == (PixelChannels **) NULL)
475     {
476       image=DestroyImage(image);
477       (void) ThrowMagickException(exception,GetMagickModule(),
478         ResourceLimitError,"MemoryAllocationFailed","`%s'",images->filename);
479       return((Image *) NULL);
480     }
481   /*
482     Evaluate image pixels.
483   */
484   status=MagickTrue;
485   progress=0;
486   random_info=AcquireRandomInfoThreadSet();
487 #if defined(MAGICKCORE_OPENMP_SUPPORT)
488   key=GetRandomSecretKey(random_info[0]);
489 #endif
490   evaluate_view=AcquireAuthenticCacheView(image,exception);
491   if (op == MedianEvaluateOperator)
492     {
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)
496 #endif
497       for (y=0; y < (ssize_t) image->rows; y++)
498       {
499         CacheView
500           *image_view;
501
502         const Image
503           *next;
504
505         const int
506           id = GetOpenMPThreadId();
507
508         register PixelChannels
509           *evaluate_pixel;
510
511         register Quantum
512           *restrict q;
513
514         register ssize_t
515           x;
516
517         if (status == MagickFalse)
518           continue;
519         q=QueueCacheViewAuthenticPixels(evaluate_view,0,y,image->columns,1,
520           exception);
521         if (q == (Quantum *) NULL)
522           {
523             status=MagickFalse;
524             continue;
525           }
526         evaluate_pixel=evaluate_pixels[id];
527         for (x=0; x < (ssize_t) image->columns; x++)
528         {
529           register ssize_t
530             j,
531             k;
532
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;
536           next=images;
537           for (j=0; j < (ssize_t) number_images; j++)
538           {
539             register const Quantum
540               *p;
541
542             register ssize_t
543               i;
544
545             image_view=AcquireVirtualCacheView(next,exception);
546             p=GetCacheViewVirtualPixels(image_view,x,y,1,1,exception);
547             if (p == (const Quantum *) NULL)
548               {
549                 image_view=DestroyCacheView(image_view);
550                 break;
551               }
552             for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
553             {
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))
559                 continue;
560               if ((evaluate_traits & UpdatePixelTrait) == 0)
561                 continue;
562               evaluate_pixel[j].channel[i]=ApplyEvaluateOperator(
563                 random_info[id],GetPixelChannel(image,channel,p),op,
564                 evaluate_pixel[j].channel[i]);
565             }
566             image_view=DestroyCacheView(image_view);
567             next=GetNextImageInList(next);
568           }
569           qsort((void *) evaluate_pixel,number_images,sizeof(*evaluate_pixel),
570             IntensityCompare);
571           for (k=0; k < (ssize_t) GetPixelChannels(image); k++)
572             q[k]=ClampToQuantum(evaluate_pixel[j/2].channel[k]);
573           q+=GetPixelChannels(image);
574         }
575         if (SyncCacheViewAuthenticPixels(evaluate_view,exception) == MagickFalse)
576           status=MagickFalse;
577         if (images->progress_monitor != (MagickProgressMonitor) NULL)
578           {
579             MagickBooleanType
580               proceed;
581
582 #if   defined(MAGICKCORE_OPENMP_SUPPORT)
583             #pragma omp critical (MagickCore_EvaluateImages)
584 #endif
585             proceed=SetImageProgress(images,EvaluateImageTag,progress++,
586               image->rows);
587             if (proceed == MagickFalse)
588               status=MagickFalse;
589           }
590       }
591     }
592   else
593     {
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)
597 #endif
598       for (y=0; y < (ssize_t) image->rows; y++)
599       {
600         CacheView
601           *image_view;
602
603         const Image
604           *next;
605
606         const int
607           id = GetOpenMPThreadId();
608
609         register ssize_t
610           i,
611           x;
612
613         register PixelChannels
614           *evaluate_pixel;
615
616         register Quantum
617           *restrict q;
618
619         ssize_t
620           j;
621
622         if (status == MagickFalse)
623           continue;
624         q=QueueCacheViewAuthenticPixels(evaluate_view,0,y,image->columns,1,
625           exception);
626         if (q == (Quantum *) NULL)
627           {
628             status=MagickFalse;
629             continue;
630           }
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;
635         next=images;
636         for (j=0; j < (ssize_t) number_images; j++)
637         {
638           register const Quantum
639             *p;
640
641           image_view=AcquireVirtualCacheView(next,exception);
642           p=GetCacheViewVirtualPixels(image_view,0,y,next->columns,1,exception);
643           if (p == (const Quantum *) NULL)
644             {
645               image_view=DestroyCacheView(image_view);
646               break;
647             }
648           for (x=0; x < (ssize_t) next->columns; x++)
649           {
650             register ssize_t
651               i;
652
653             if (GetPixelReadMask(next,p) == 0)
654               {
655                 p+=GetPixelChannels(next);
656                 continue;
657               }
658             for (i=0; i < (ssize_t) GetPixelChannels(next); i++)
659             {
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))
665                 continue;
666               if ((traits & UpdatePixelTrait) == 0)
667                 continue;
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]);
671             }
672             p+=GetPixelChannels(next);
673           }
674           image_view=DestroyCacheView(image_view);
675           next=GetNextImageInList(next);
676         }
677         for (x=0; x < (ssize_t) image->columns; x++)
678         {
679           register ssize_t
680              i;
681
682           switch (op)
683           {
684             case MeanEvaluateOperator:
685             {
686               for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
687                 evaluate_pixel[x].channel[i]/=(double) number_images;
688               break;
689             }
690             case MultiplyEvaluateOperator:
691             {
692               for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
693               {
694                 register ssize_t
695                   j;
696
697                 for (j=0; j < (ssize_t) (number_images-1); j++)
698                   evaluate_pixel[x].channel[i]*=QuantumScale;
699               }
700               break;
701             }
702             default:
703               break;
704           }
705         }
706         for (x=0; x < (ssize_t) image->columns; x++)
707         {
708           register ssize_t
709             i;
710
711           if (GetPixelReadMask(image,q) == 0)
712             {
713               q+=GetPixelChannels(image);
714               continue;
715             }
716           for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
717           {
718             PixelChannel channel=GetPixelChannelChannel(image,i);
719             PixelTrait traits=GetPixelChannelTraits(image,channel);
720             if (traits == UndefinedPixelTrait)
721               continue;
722             if ((traits & UpdatePixelTrait) == 0)
723               continue;
724             q[i]=ClampToQuantum(evaluate_pixel[x].channel[i]);
725           }
726           q+=GetPixelChannels(image);
727         }
728         if (SyncCacheViewAuthenticPixels(evaluate_view,exception) == MagickFalse)
729           status=MagickFalse;
730         if (images->progress_monitor != (MagickProgressMonitor) NULL)
731           {
732             MagickBooleanType
733               proceed;
734
735 #if   defined(MAGICKCORE_OPENMP_SUPPORT)
736             #pragma omp critical (MagickCore_EvaluateImages)
737 #endif
738             proceed=SetImageProgress(images,EvaluateImageTag,progress++,
739               image->rows);
740             if (proceed == MagickFalse)
741               status=MagickFalse;
742           }
743       }
744     }
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);
750   return(image);
751 }
752
753 MagickExport MagickBooleanType EvaluateImage(Image *image,
754   const MagickEvaluateOperator op,const double value,ExceptionInfo *exception)
755 {
756   CacheView
757     *image_view;
758
759   MagickBooleanType
760     status;
761
762   MagickOffsetType
763     progress;
764
765   RandomInfo
766     **restrict random_info;
767
768   ssize_t
769     y;
770
771 #if defined(MAGICKCORE_OPENMP_SUPPORT)
772   unsigned long
773     key;
774 #endif
775
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)
783     return(MagickFalse);
784   status=MagickTrue;
785   progress=0;
786   random_info=AcquireRandomInfoThreadSet();
787 #if defined(MAGICKCORE_OPENMP_SUPPORT)
788   key=GetRandomSecretKey(random_info[0]);
789 #endif
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)
794 #endif
795   for (y=0; y < (ssize_t) image->rows; y++)
796   {
797     const int
798       id = GetOpenMPThreadId();
799
800     register Quantum
801       *restrict q;
802
803     register ssize_t
804       x;
805
806     if (status == MagickFalse)
807       continue;
808     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
809     if (q == (Quantum *) NULL)
810       {
811         status=MagickFalse;
812         continue;
813       }
814     for (x=0; x < (ssize_t) image->columns; x++)
815     {
816       register ssize_t
817         i;
818
819       for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
820       {
821         PixelChannel channel=GetPixelChannelChannel(image,i);
822         PixelTrait traits=GetPixelChannelTraits(image,channel);
823         if (traits == UndefinedPixelTrait)
824           continue;
825         if (((traits & CopyPixelTrait) != 0) ||
826             (GetPixelReadMask(image,q) == 0))
827           continue;
828         q[i]=ClampToQuantum(ApplyEvaluateOperator(random_info[id],q[i],op,
829           value));
830       }
831       q+=GetPixelChannels(image);
832     }
833     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
834       status=MagickFalse;
835     if (image->progress_monitor != (MagickProgressMonitor) NULL)
836       {
837         MagickBooleanType
838           proceed;
839
840 #if defined(MAGICKCORE_OPENMP_SUPPORT)
841         #pragma omp critical (MagickCore_EvaluateImage)
842 #endif
843         proceed=SetImageProgress(image,EvaluateImageTag,progress++,image->rows);
844         if (proceed == MagickFalse)
845           status=MagickFalse;
846       }
847   }
848   image_view=DestroyCacheView(image_view);
849   random_info=DestroyRandomInfoThreadSet(random_info);
850   return(status);
851 }
852 \f
853 /*
854 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
855 %                                                                             %
856 %                                                                             %
857 %                                                                             %
858 %     F u n c t i o n I m a g e                                               %
859 %                                                                             %
860 %                                                                             %
861 %                                                                             %
862 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
863 %
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.
868 %
869 %  The format of the FunctionImage method is:
870 %
871 %      MagickBooleanType FunctionImage(Image *image,
872 %        const MagickFunction function,const ssize_t number_parameters,
873 %        const double *parameters,ExceptionInfo *exception)
874 %
875 %  A description of each parameter follows:
876 %
877 %    o image: the image.
878 %
879 %    o function: A channel function.
880 %
881 %    o parameters: one or more parameters.
882 %
883 %    o exception: return any errors or warnings in this structure.
884 %
885 */
886
887 static Quantum ApplyFunction(Quantum pixel,const MagickFunction function,
888   const size_t number_parameters,const double *parameters,
889   ExceptionInfo *exception)
890 {
891   double
892     result;
893
894   register ssize_t
895     i;
896
897   (void) exception;
898   result=0.0;
899   switch (function)
900   {
901     case PolynomialFunction:
902     {
903       /*
904         Polynomial: polynomial constants, highest to lowest order (e.g. c0*x^3+
905         c1*x^2+c2*x+c3).
906       */
907       result=0.0;
908       for (i=0; i < (ssize_t) number_parameters; i++)
909         result=result*QuantumScale*pixel+parameters[i];
910       result*=QuantumRange;
911       break;
912     }
913     case SinusoidFunction:
914     {
915       double
916         amplitude,
917         bias,
918         frequency,
919         phase;
920
921       /*
922         Sinusoid: frequency, phase, amplitude, bias.
923       */
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));
930       break;
931     }
932     case ArcsinFunction:
933     {
934       double
935         bias,
936         center,
937         range,
938         width;
939
940       /*
941         Arcsin (peged at range limits for invalid results): width, center,
942         range, and bias.
943       */
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;
951       else
952         if (result >= 1.0)
953           result=bias+range/2.0;
954         else
955           result=(double) (range/MagickPI*asin((double) result)+bias);
956       result*=QuantumRange;
957       break;
958     }
959     case ArctanFunction:
960     {
961       double
962         center,
963         bias,
964         range,
965         slope;
966
967       /*
968         Arctan: slope, center, range, and bias.
969       */
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)
976         result)+bias));
977       break;
978     }
979     case UndefinedFunction:
980       break;
981   }
982   return(ClampToQuantum(result));
983 }
984
985 MagickExport MagickBooleanType FunctionImage(Image *image,
986   const MagickFunction function,const size_t number_parameters,
987   const double *parameters,ExceptionInfo *exception)
988 {
989 #define FunctionImageTag  "Function/Image "
990
991   CacheView
992     *image_view;
993
994   MagickBooleanType
995     status;
996
997   MagickOffsetType
998     progress;
999
1000   ssize_t
1001     y;
1002
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);
1011   status=MagickTrue;
1012   progress=0;
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)
1017 #endif
1018   for (y=0; y < (ssize_t) image->rows; y++)
1019   {
1020     register Quantum
1021       *restrict q;
1022
1023     register ssize_t
1024       x;
1025
1026     if (status == MagickFalse)
1027       continue;
1028     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1029     if (q == (Quantum *) NULL)
1030       {
1031         status=MagickFalse;
1032         continue;
1033       }
1034     for (x=0; x < (ssize_t) image->columns; x++)
1035     {
1036       register ssize_t
1037         i;
1038
1039       if (GetPixelReadMask(image,q) == 0)
1040         {
1041           q+=GetPixelChannels(image);
1042           continue;
1043         }
1044       for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1045       {
1046         PixelChannel channel=GetPixelChannelChannel(image,i);
1047         PixelTrait traits=GetPixelChannelTraits(image,channel);
1048         if (traits == UndefinedPixelTrait)
1049           continue;
1050         if ((traits & UpdatePixelTrait) == 0)
1051           continue;
1052         q[i]=ApplyFunction(q[i],function,number_parameters,parameters,
1053           exception);
1054       }
1055       q+=GetPixelChannels(image);
1056     }
1057     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1058       status=MagickFalse;
1059     if (image->progress_monitor != (MagickProgressMonitor) NULL)
1060       {
1061         MagickBooleanType
1062           proceed;
1063
1064 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1065         #pragma omp critical (MagickCore_FunctionImage)
1066 #endif
1067         proceed=SetImageProgress(image,FunctionImageTag,progress++,image->rows);
1068         if (proceed == MagickFalse)
1069           status=MagickFalse;
1070       }
1071   }
1072   image_view=DestroyCacheView(image_view);
1073   return(status);
1074 }
1075 \f
1076 /*
1077 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1078 %                                                                             %
1079 %                                                                             %
1080 %                                                                             %
1081 %   G e t I m a g e E x t r e m a                                             %
1082 %                                                                             %
1083 %                                                                             %
1084 %                                                                             %
1085 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1086 %
1087 %  GetImageExtrema() returns the extrema of one or more image channels.
1088 %
1089 %  The format of the GetImageExtrema method is:
1090 %
1091 %      MagickBooleanType GetImageExtrema(const Image *image,size_t *minima,
1092 %        size_t *maxima,ExceptionInfo *exception)
1093 %
1094 %  A description of each parameter follows:
1095 %
1096 %    o image: the image.
1097 %
1098 %    o minima: the minimum value in the channel.
1099 %
1100 %    o maxima: the maximum value in the channel.
1101 %
1102 %    o exception: return any errors or warnings in this structure.
1103 %
1104 */
1105 MagickExport MagickBooleanType GetImageExtrema(const Image *image,
1106   size_t *minima,size_t *maxima,ExceptionInfo *exception)
1107 {
1108   double
1109     max,
1110     min;
1111
1112   MagickBooleanType
1113     status;
1114
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);
1122   return(status);
1123 }
1124 \f
1125 /*
1126 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1127 %                                                                             %
1128 %                                                                             %
1129 %                                                                             %
1130 %   G e t I m a g e K u r t o s i s                                           %
1131 %                                                                             %
1132 %                                                                             %
1133 %                                                                             %
1134 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1135 %
1136 %  GetImageKurtosis() returns the kurtosis and skewness of one or more image
1137 %  channels.
1138 %
1139 %  The format of the GetImageKurtosis method is:
1140 %
1141 %      MagickBooleanType GetImageKurtosis(const Image *image,double *kurtosis,
1142 %        double *skewness,ExceptionInfo *exception)
1143 %
1144 %  A description of each parameter follows:
1145 %
1146 %    o image: the image.
1147 %
1148 %    o kurtosis: the kurtosis of the channel.
1149 %
1150 %    o skewness: the skewness of the channel.
1151 %
1152 %    o exception: return any errors or warnings in this structure.
1153 %
1154 */
1155 MagickExport MagickBooleanType GetImageKurtosis(const Image *image,
1156   double *kurtosis,double *skewness,ExceptionInfo *exception)
1157 {
1158   CacheView
1159     *image_view;
1160
1161   double
1162     area,
1163     mean,
1164     standard_deviation,
1165     sum_squares,
1166     sum_cubes,
1167     sum_fourth_power;
1168
1169   MagickBooleanType
1170     status;
1171
1172   ssize_t
1173     y;
1174
1175   assert(image != (Image *) NULL);
1176   assert(image->signature == MagickSignature);
1177   if (image->debug != MagickFalse)
1178     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1179   status=MagickTrue;
1180   *kurtosis=0.0;
1181   *skewness=0.0;
1182   area=0.0;
1183   mean=0.0;
1184   standard_deviation=0.0;
1185   sum_squares=0.0;
1186   sum_cubes=0.0;
1187   sum_fourth_power=0.0;
1188   image_view=AcquireVirtualCacheView(image,exception);
1189 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1190   #pragma omp parallel for schedule(static,4) shared(status) \
1191     magick_threads(image,image,image->rows,1)
1192 #endif
1193   for (y=0; y < (ssize_t) image->rows; y++)
1194   {
1195     register const Quantum
1196       *restrict p;
1197
1198     register ssize_t
1199       x;
1200
1201     if (status == MagickFalse)
1202       continue;
1203     p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1204     if (p == (const Quantum *) NULL)
1205       {
1206         status=MagickFalse;
1207         continue;
1208       }
1209     for (x=0; x < (ssize_t) image->columns; x++)
1210     {
1211       register ssize_t
1212         i;
1213
1214       if (GetPixelReadMask(image,p) == 0)
1215         {
1216           p+=GetPixelChannels(image);
1217           continue;
1218         }
1219       for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1220       {
1221         PixelChannel channel=GetPixelChannelChannel(image,i);
1222         PixelTrait traits=GetPixelChannelTraits(image,channel);
1223         if (traits == UndefinedPixelTrait)
1224           continue;
1225         if ((traits & UpdatePixelTrait) == 0)
1226           continue;
1227 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1228         #pragma omp critical (MagickCore_GetImageKurtosis)
1229 #endif
1230         {
1231           mean+=p[i];
1232           sum_squares+=(double) p[i]*p[i];
1233           sum_cubes+=(double) p[i]*p[i]*p[i];
1234           sum_fourth_power+=(double) p[i]*p[i]*p[i]*p[i];
1235           area++;
1236         }
1237       }
1238       p+=GetPixelChannels(image);
1239     }
1240   }
1241   image_view=DestroyCacheView(image_view);
1242   if (area != 0.0)
1243     {
1244       mean/=area;
1245       sum_squares/=area;
1246       sum_cubes/=area;
1247       sum_fourth_power/=area;
1248     }
1249   standard_deviation=sqrt(sum_squares-(mean*mean));
1250   if (standard_deviation != 0.0)
1251     {
1252       *kurtosis=sum_fourth_power-4.0*mean*sum_cubes+6.0*mean*mean*sum_squares-
1253         3.0*mean*mean*mean*mean;
1254       *kurtosis/=standard_deviation*standard_deviation*standard_deviation*
1255         standard_deviation;
1256       *kurtosis-=3.0;
1257       *skewness=sum_cubes-3.0*mean*sum_squares+2.0*mean*mean*mean;
1258       *skewness/=standard_deviation*standard_deviation*standard_deviation;
1259     }
1260   return(status);
1261 }
1262 \f
1263 /*
1264 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1265 %                                                                             %
1266 %                                                                             %
1267 %                                                                             %
1268 %   G e t I m a g e M e a n                                                   %
1269 %                                                                             %
1270 %                                                                             %
1271 %                                                                             %
1272 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1273 %
1274 %  GetImageMean() returns the mean and standard deviation of one or more image
1275 %  channels.
1276 %
1277 %  The format of the GetImageMean method is:
1278 %
1279 %      MagickBooleanType GetImageMean(const Image *image,double *mean,
1280 %        double *standard_deviation,ExceptionInfo *exception)
1281 %
1282 %  A description of each parameter follows:
1283 %
1284 %    o image: the image.
1285 %
1286 %    o mean: the average value in the channel.
1287 %
1288 %    o standard_deviation: the standard deviation of the channel.
1289 %
1290 %    o exception: return any errors or warnings in this structure.
1291 %
1292 */
1293 MagickExport MagickBooleanType GetImageMean(const Image *image,double *mean,
1294   double *standard_deviation,ExceptionInfo *exception)
1295 {
1296   double
1297     area;
1298
1299   ChannelStatistics
1300     *channel_statistics;
1301
1302   register ssize_t
1303     i;
1304
1305   assert(image != (Image *) NULL);
1306   assert(image->signature == MagickSignature);
1307   if (image->debug != MagickFalse)
1308     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1309   channel_statistics=GetImageStatistics(image,exception);
1310   if (channel_statistics == (ChannelStatistics *) NULL)
1311     return(MagickFalse);
1312   area=0.0;
1313   channel_statistics[CompositePixelChannel].mean=0.0;
1314   channel_statistics[CompositePixelChannel].standard_deviation=0.0;
1315   for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1316   {
1317     PixelChannel channel=GetPixelChannelChannel(image,i);
1318     PixelTrait traits=GetPixelChannelTraits(image,channel);
1319     if (traits == UndefinedPixelTrait)
1320       continue;
1321     if ((traits & UpdatePixelTrait) == 0)
1322       continue;
1323     channel_statistics[CompositePixelChannel].mean+=channel_statistics[i].mean;
1324     channel_statistics[CompositePixelChannel].standard_deviation+=
1325       channel_statistics[i].variance-channel_statistics[i].mean*
1326       channel_statistics[i].mean;
1327     area++;
1328   }
1329   channel_statistics[CompositePixelChannel].mean/=area;
1330   channel_statistics[CompositePixelChannel].standard_deviation=
1331     sqrt(channel_statistics[CompositePixelChannel].standard_deviation/area);
1332   *mean=channel_statistics[CompositePixelChannel].mean;
1333   *standard_deviation=
1334     channel_statistics[CompositePixelChannel].standard_deviation;
1335   channel_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1336     channel_statistics);
1337   return(MagickTrue);
1338 }
1339 \f
1340 /*
1341 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1342 %                                                                             %
1343 %                                                                             %
1344 %                                                                             %
1345 %   G e t I m a g e M o m e n t s                                             %
1346 %                                                                             %
1347 %                                                                             %
1348 %                                                                             %
1349 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1350 %
1351 %  GetImageMoments() returns the moments of one or more image channels.
1352 %
1353 %  The format of the GetImageMoments method is:
1354 %
1355 %      ChannelMoments *GetImageMoments(const Image *image,
1356 %        ExceptionInfo *exception)
1357 %
1358 %  A description of each parameter follows:
1359 %
1360 %    o image: the image.
1361 %
1362 %    o exception: return any errors or warnings in this structure.
1363 %
1364 */
1365 MagickExport ChannelMoments *GetImageMoments(const Image *image,
1366   ExceptionInfo *exception)
1367 {
1368 #define MaxNumberImageMoments  8
1369
1370   CacheView
1371     *image_view;
1372
1373   ChannelMoments
1374     *channel_moments;
1375
1376   double
1377     M00[MaxPixelChannels+1],
1378     M01[MaxPixelChannels+1],
1379     M02[MaxPixelChannels+1],
1380     M03[MaxPixelChannels+1],
1381     M10[MaxPixelChannels+1],
1382     M11[MaxPixelChannels+1],
1383     M12[MaxPixelChannels+1],
1384     M20[MaxPixelChannels+1],
1385     M21[MaxPixelChannels+1],
1386     M22[MaxPixelChannels+1],
1387     M30[MaxPixelChannels+1];
1388
1389   PointInfo
1390     centroid[MaxPixelChannels+1];
1391
1392   ssize_t
1393     channel,
1394     y;
1395
1396   assert(image != (Image *) NULL);
1397   assert(image->signature == MagickSignature);
1398   if (image->debug != MagickFalse)
1399     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1400   channel_moments=(ChannelMoments *) AcquireQuantumMemory(MaxPixelChannels+1,
1401     sizeof(*channel_moments));
1402   if (channel_moments == (ChannelMoments *) NULL)
1403     return(channel_moments);
1404   (void) ResetMagickMemory(channel_moments,0,(MaxPixelChannels+1)*
1405     sizeof(*channel_moments));
1406   (void) ResetMagickMemory(centroid,0,sizeof(centroid));
1407   (void) ResetMagickMemory(M00,0,sizeof(M00));
1408   (void) ResetMagickMemory(M01,0,sizeof(M01));
1409   (void) ResetMagickMemory(M02,0,sizeof(M02));
1410   (void) ResetMagickMemory(M03,0,sizeof(M03));
1411   (void) ResetMagickMemory(M10,0,sizeof(M10));
1412   (void) ResetMagickMemory(M11,0,sizeof(M11));
1413   (void) ResetMagickMemory(M12,0,sizeof(M12));
1414   (void) ResetMagickMemory(M20,0,sizeof(M20));
1415   (void) ResetMagickMemory(M21,0,sizeof(M21));
1416   (void) ResetMagickMemory(M22,0,sizeof(M22));
1417   (void) ResetMagickMemory(M30,0,sizeof(M30));
1418   image_view=AcquireVirtualCacheView(image,exception);
1419   for (y=0; y < (ssize_t) image->rows; y++)
1420   {
1421     register const Quantum
1422       *restrict p;
1423
1424     register ssize_t
1425       x;
1426
1427     /*
1428       Compute center of mass (centroid).
1429     */
1430     p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1431     if (p == (const Quantum *) NULL)
1432       break;
1433     for (x=0; x < (ssize_t) image->columns; x++)
1434     {
1435       register ssize_t
1436         i;
1437
1438       if (GetPixelReadMask(image,p) == 0)
1439         {
1440           p+=GetPixelChannels(image);
1441           continue;
1442         }
1443       for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1444       {
1445         PixelChannel channel=GetPixelChannelChannel(image,i);
1446         PixelTrait traits=GetPixelChannelTraits(image,channel);
1447         if (traits == UndefinedPixelTrait)
1448           continue;
1449         if ((traits & UpdatePixelTrait) == 0)
1450           continue;
1451         M00[channel]+=p[i];
1452         M10[channel]+=x*p[i];
1453         M01[channel]+=y*p[i];
1454       }
1455       p+=GetPixelChannels(image);
1456     }
1457   }
1458   for (channel=0; channel <= MaxPixelChannels; channel++)
1459   {
1460     /*
1461        Compute center of mass (centroid).
1462     */
1463     if (fabs(M00[channel]) < MagickEpsilon)
1464       continue;
1465     centroid[channel].x=M10[channel]/M00[channel];
1466     centroid[channel].y=M01[channel]/M00[channel];
1467   }
1468   for (y=0; y < (ssize_t) image->rows; y++)
1469   {
1470     register const Quantum
1471       *restrict p;
1472
1473     register ssize_t
1474       x;
1475
1476     /*
1477       Compute the image moments.
1478     */
1479     p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1480     if (p == (const Quantum *) NULL)
1481       break;
1482     for (x=0; x < (ssize_t) image->columns; x++)
1483     {
1484       register ssize_t
1485         i;
1486
1487       if (GetPixelReadMask(image,p) == 0)
1488         {
1489           p+=GetPixelChannels(image);
1490           continue;
1491         }
1492       for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1493       {
1494         PixelChannel channel=GetPixelChannelChannel(image,i);
1495         PixelTrait traits=GetPixelChannelTraits(image,channel);
1496         if (traits == UndefinedPixelTrait)
1497           continue;
1498         if ((traits & UpdatePixelTrait) == 0)
1499           continue;
1500         M11[channel]+=(x-centroid[channel].x)*(y-centroid[channel].y)*p[i];
1501         M20[channel]+=(x-centroid[channel].x)*(x-centroid[channel].x)*p[i];
1502         M02[channel]+=(y-centroid[channel].y)*(y-centroid[channel].y)*p[i];
1503         M21[channel]+=(x-centroid[channel].x)*(x-centroid[channel].x)*
1504           (y-centroid[channel].y)*p[i];
1505         M12[channel]+=(x-centroid[channel].x)*(y-centroid[channel].y)*
1506           (y-centroid[channel].y)*p[i];
1507         M22[channel]+=(x-centroid[channel].x)*(x-centroid[channel].x)*
1508           (y-centroid[channel].y)*(y-centroid[channel].y)*p[i];
1509         M30[channel]+=(x-centroid[channel].x)*(x-centroid[channel].x)*
1510           (x-centroid[channel].x)*p[i];
1511         M03[channel]+=(y-centroid[channel].y)*(y-centroid[channel].y)*
1512           (y-centroid[channel].y)*p[i];
1513       }
1514       p+=GetPixelChannels(image);
1515     }
1516   }
1517   for (channel=0; channel <= MaxPixelChannels; channel++)
1518   {
1519     /*
1520       Normalize image moments.
1521     */
1522     if (fabs(M00[channel]) < MagickEpsilon)
1523       continue;
1524     M11[channel]/=pow(M00[channel],(1.0+(1.0+1.0)/2.0));
1525     M20[channel]/=pow(M00[channel],(1.0+(2.0+0.0)/2.0));
1526     M02[channel]/=pow(M00[channel],(1.0+(0.0+2.0)/2.0));
1527     M21[channel]/=pow(M00[channel],(1.0+(2.0+1.0)/2.0));
1528     M12[channel]/=pow(M00[channel],(1.0+(1.0+2.0)/2.0));
1529     M22[channel]/=pow(M00[channel],(1.0+(2.0+2.0)/2.0));
1530     M30[channel]/=pow(M00[channel],(1.0+(3.0+0.0)/2.0));
1531     M03[channel]/=pow(M00[channel],(1.0+(0.0+3.0)/2.0));
1532   }
1533   image_view=DestroyCacheView(image_view);
1534   for (channel=0; channel <= MaxPixelChannels; channel++)
1535   {
1536     /*
1537       Compute Hu invariant moments.
1538     */
1539     if (fabs(M00[channel]) < MagickEpsilon)
1540       continue;
1541    channel_moments[channel].I[0]=M20[channel]+M02[channel];
1542     channel_moments[channel].I[1]=(M20[channel]-M02[channel])*
1543       (M20[channel]-M02[channel])+4.0*M11[channel]*M11[channel];
1544     channel_moments[channel].I[2]=(M30[channel]-3.0*M12[channel])*
1545       (M30[channel]-3.0*M12[channel])+(3.0*M21[channel]-M03[channel])*
1546       (3.0*M21[channel]-M03[channel]);
1547     channel_moments[channel].I[3]=(M30[channel]+M12[channel])*
1548       (M30[channel]+M12[channel])+(M21[channel]+M03[channel])*
1549       (M21[channel]+M03[channel]);
1550     channel_moments[channel].I[4]=(M30[channel]-3.0*M12[channel])*
1551       (M30[channel]+M12[channel])*((M30[channel]+M12[channel])*
1552       (M30[channel]+M12[channel])-3.0*(M21[channel]+M03[channel])*
1553       (M21[channel]+M03[channel]))+(3.0*M21[channel]-M03[channel])*
1554       (M21[channel]+M03[channel])*(3.0*(M30[channel]+M12[channel])*
1555       (M30[channel]+M12[channel])-(M21[channel]+M03[channel])*
1556       (M21[channel]+M03[channel]));
1557     channel_moments[channel].I[5]=(M20[channel]-M02[channel])*
1558       ((M30[channel]+M12[channel])*(M30[channel]+M12[channel])-
1559       (M21[channel]+M03[channel])*(M21[channel]+M03[channel]))+
1560       4.0*M11[channel]*(M30[channel]+M12[channel])*(M21[channel]+M03[channel]);
1561     channel_moments[channel].I[6]=(3.0*M21[channel]-M03[channel])*
1562       (M30[channel]+M12[channel])*((M30[channel]+M12[channel])*
1563       (M30[channel]+M12[channel])-3.0*(M21[channel]+M03[channel])*
1564       (M21[channel]+M03[channel]))-(M30[channel]-3*M12[channel])*
1565       (M21[channel]+M03[channel])*(3.0*(M30[channel]+M12[channel])*
1566       (M30[channel]+M12[channel])-(M21[channel]+M03[channel])*
1567       (M21[channel]+M03[channel]));
1568     channel_moments[channel].I[7]=M11[channel]*((M30[channel]+M12[channel])*
1569       (M30[channel]+M12[channel])-(M03[channel]+M21[channel])*
1570       (M03[channel]+M21[channel]))-(M20[channel]-M02[channel])*
1571       (M30[channel]+M12[channel])*(M03[channel]+M21[channel]);
1572     channel_moments[channel].centroid=centroid[channel];
1573     channel_moments[channel].ellipse_axis.x=sqrt((2.0/M00[channel])*
1574       ((M20[channel]+M02[channel])+sqrt(4.0*M11[channel]*M11[channel]+
1575       (M20[channel]-M02[channel])*(M20[channel]-M02[channel]))));
1576     channel_moments[channel].ellipse_axis.y=sqrt((2.0/M00[channel])*
1577       ((M20[channel]+M02[channel])-sqrt(4.0*M11[channel]*M11[channel]+
1578       (M20[channel]-M02[channel])*(M20[channel]-M02[channel]))));
1579     channel_moments[channel].ellipse_angle=RadiansToDegrees(0.5*atan(2.0*
1580       M11[channel]/(M20[channel]-M02[channel])));
1581     channel_moments[channel].ellipse_eccentricity=sqrt(1.0-(
1582       channel_moments[channel].ellipse_axis.y/
1583       channel_moments[channel].ellipse_axis.x));
1584     channel_moments[channel].ellipse_intensity=M00[channel]/(MagickPI*
1585       channel_moments[channel].ellipse_axis.x*
1586       channel_moments[channel].ellipse_axis.y);
1587   }
1588   if (y < (ssize_t) image->rows)
1589     channel_moments=(ChannelMoments *) RelinquishMagickMemory(channel_moments);
1590   return(channel_moments);
1591 }
1592 \f
1593 /*
1594 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1595 %                                                                             %
1596 %                                                                             %
1597 %                                                                             %
1598 %   G e t I m a g e R a n g e                                                 %
1599 %                                                                             %
1600 %                                                                             %
1601 %                                                                             %
1602 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1603 %
1604 %  GetImageRange() returns the range of one or more image channels.
1605 %
1606 %  The format of the GetImageRange method is:
1607 %
1608 %      MagickBooleanType GetImageRange(const Image *image,double *minima,
1609 %        double *maxima,ExceptionInfo *exception)
1610 %
1611 %  A description of each parameter follows:
1612 %
1613 %    o image: the image.
1614 %
1615 %    o minima: the minimum value in the channel.
1616 %
1617 %    o maxima: the maximum value in the channel.
1618 %
1619 %    o exception: return any errors or warnings in this structure.
1620 %
1621 */
1622 MagickExport MagickBooleanType GetImageRange(const Image *image,double *minima,
1623   double *maxima,ExceptionInfo *exception)
1624 {
1625   CacheView
1626     *image_view;
1627
1628   MagickBooleanType
1629     initialize,
1630     status;
1631
1632   ssize_t
1633     y;
1634
1635   assert(image != (Image *) NULL);
1636   assert(image->signature == MagickSignature);
1637   if (image->debug != MagickFalse)
1638     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1639   status=MagickTrue;
1640   initialize=MagickTrue;
1641   *maxima=0.0;
1642   *minima=0.0;
1643   image_view=AcquireVirtualCacheView(image,exception);
1644 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1645   #pragma omp parallel for schedule(static,4) shared(status,initialize) \
1646     magick_threads(image,image,image->rows,1)
1647 #endif
1648   for (y=0; y < (ssize_t) image->rows; y++)
1649   {
1650     register const Quantum
1651       *restrict p;
1652
1653     register ssize_t
1654       x;
1655
1656     if (status == MagickFalse)
1657       continue;
1658     p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1659     if (p == (const Quantum *) NULL)
1660       {
1661         status=MagickFalse;
1662         continue;
1663       }
1664     for (x=0; x < (ssize_t) image->columns; x++)
1665     {
1666       register ssize_t
1667         i;
1668
1669       if (GetPixelReadMask(image,p) == 0)
1670         {
1671           p+=GetPixelChannels(image);
1672           continue;
1673         }
1674       for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1675       {
1676         PixelChannel channel=GetPixelChannelChannel(image,i);
1677         PixelTrait traits=GetPixelChannelTraits(image,channel);
1678         if (traits == UndefinedPixelTrait)
1679           continue;
1680         if ((traits & UpdatePixelTrait) == 0)
1681           continue;
1682 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1683         #pragma omp critical (MagickCore_GetImageRange)
1684 #endif
1685         {
1686           if (initialize != MagickFalse)
1687             {
1688               *minima=(double) p[i];
1689               *maxima=(double) p[i];
1690               initialize=MagickFalse;
1691             }
1692           else
1693             {
1694               if ((double) p[i] < *minima)
1695                 *minima=(double) p[i];
1696               if ((double) p[i] > *maxima)
1697                 *maxima=(double) p[i];
1698            }
1699         }
1700       }
1701       p+=GetPixelChannels(image);
1702     }
1703   }
1704   image_view=DestroyCacheView(image_view);
1705   return(status);
1706 }
1707 \f
1708 /*
1709 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1710 %                                                                             %
1711 %                                                                             %
1712 %                                                                             %
1713 %   G e t I m a g e S t a t i s t i c s                                       %
1714 %                                                                             %
1715 %                                                                             %
1716 %                                                                             %
1717 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1718 %
1719 %  GetImageStatistics() returns statistics for each channel in the image.  The
1720 %  statistics include the channel depth, its minima, maxima, mean, standard
1721 %  deviation, kurtosis and skewness.  You can access the red channel mean, for
1722 %  example, like this:
1723 %
1724 %      channel_statistics=GetImageStatistics(image,exception);
1725 %      red_mean=channel_statistics[RedPixelChannel].mean;
1726 %
1727 %  Use MagickRelinquishMemory() to free the statistics buffer.
1728 %
1729 %  The format of the GetImageStatistics method is:
1730 %
1731 %      ChannelStatistics *GetImageStatistics(const Image *image,
1732 %        ExceptionInfo *exception)
1733 %
1734 %  A description of each parameter follows:
1735 %
1736 %    o image: the image.
1737 %
1738 %    o exception: return any errors or warnings in this structure.
1739 %
1740 */
1741
1742 static size_t GetImageChannels(const Image *image)
1743 {
1744   register ssize_t
1745     i;
1746
1747   size_t
1748     channels;
1749
1750   channels=0;
1751   for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1752   {
1753     PixelChannel channel=GetPixelChannelChannel(image,i);
1754     PixelTrait traits=GetPixelChannelTraits(image,channel);
1755     if (traits != UndefinedPixelTrait)
1756       channels++;
1757   }
1758   return(channels);
1759 }
1760
1761 MagickExport ChannelStatistics *GetImageStatistics(const Image *image,
1762   ExceptionInfo *exception)
1763 {
1764   ChannelStatistics
1765     *channel_statistics;
1766
1767   MagickStatusType
1768     status;
1769
1770   QuantumAny
1771     range;
1772
1773   register ssize_t
1774     i;
1775
1776   size_t
1777     channels,
1778     depth;
1779
1780   ssize_t
1781     y;
1782
1783   assert(image != (Image *) NULL);
1784   assert(image->signature == MagickSignature);
1785   if (image->debug != MagickFalse)
1786     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1787   channel_statistics=(ChannelStatistics *) AcquireQuantumMemory(
1788     MaxPixelChannels+1,sizeof(*channel_statistics));
1789   if (channel_statistics == (ChannelStatistics *) NULL)
1790     return(channel_statistics);
1791   (void) ResetMagickMemory(channel_statistics,0,(MaxPixelChannels+1)*
1792     sizeof(*channel_statistics));
1793   for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
1794   {
1795     channel_statistics[i].depth=1;
1796     channel_statistics[i].maxima=(-MagickHuge);
1797     channel_statistics[i].minima=MagickHuge;
1798   }
1799   for (y=0; y < (ssize_t) image->rows; y++)
1800   {
1801     register const Quantum
1802       *restrict p;
1803
1804     register ssize_t
1805       x;
1806
1807     p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1808     if (p == (const Quantum *) NULL)
1809       break;
1810     for (x=0; x < (ssize_t) image->columns; x++)
1811     {
1812       register ssize_t
1813         i;
1814
1815       if (GetPixelReadMask(image,p) == 0)
1816         {
1817           p+=GetPixelChannels(image);
1818           continue;
1819         }
1820       for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1821       {
1822         PixelChannel channel=GetPixelChannelChannel(image,i);
1823         PixelTrait traits=GetPixelChannelTraits(image,channel);
1824         if (traits == UndefinedPixelTrait)
1825           continue;
1826         if (channel_statistics[channel].depth != MAGICKCORE_QUANTUM_DEPTH)
1827           {
1828             depth=channel_statistics[channel].depth;
1829             range=GetQuantumRange(depth);
1830             status=p[i] != ScaleAnyToQuantum(ScaleQuantumToAny(p[i],range),
1831               range) ? MagickTrue : MagickFalse;
1832             if (status != MagickFalse)
1833               {
1834                 channel_statistics[channel].depth++;
1835                 i--;
1836                 continue;
1837               }
1838           }
1839         if ((double) p[i] < channel_statistics[channel].minima)
1840           channel_statistics[channel].minima=(double) p[i];
1841         if ((double) p[i] > channel_statistics[channel].maxima)
1842           channel_statistics[channel].maxima=(double) p[i];
1843         channel_statistics[channel].sum+=p[i];
1844         channel_statistics[channel].sum_squared+=(double) p[i]*p[i];
1845         channel_statistics[channel].sum_cubed+=(double) p[i]*p[i]*p[i];
1846         channel_statistics[channel].sum_fourth_power+=(double) p[i]*p[i]*p[i]*
1847           p[i];
1848         channel_statistics[channel].area++;
1849       }
1850       p+=GetPixelChannels(image);
1851     }
1852   }
1853   for (i=0; i < (ssize_t) MaxPixelChannels; i++)
1854   {
1855     double
1856       area;
1857
1858     area=PerceptibleReciprocal(channel_statistics[i].area);
1859     channel_statistics[i].sum*=area;
1860     channel_statistics[i].sum_squared*=area;
1861     channel_statistics[i].sum_cubed*=area;
1862     channel_statistics[i].sum_fourth_power*=area;
1863     channel_statistics[i].mean=channel_statistics[i].sum;
1864     channel_statistics[i].variance=channel_statistics[i].sum_squared;
1865     channel_statistics[i].standard_deviation=sqrt(
1866       channel_statistics[i].variance-(channel_statistics[i].mean*
1867       channel_statistics[i].mean));
1868   }
1869   for (i=0; i < (ssize_t) MaxPixelChannels; i++)
1870   {
1871     channel_statistics[CompositePixelChannel].area+=channel_statistics[i].area;
1872     channel_statistics[CompositePixelChannel].minima=MagickMin(
1873       channel_statistics[CompositePixelChannel].minima,
1874       channel_statistics[i].minima);
1875     channel_statistics[CompositePixelChannel].maxima=EvaluateMax(
1876       channel_statistics[CompositePixelChannel].maxima,
1877       channel_statistics[i].maxima);
1878     channel_statistics[CompositePixelChannel].sum+=channel_statistics[i].sum;
1879     channel_statistics[CompositePixelChannel].sum_squared+=
1880       channel_statistics[i].sum_squared;
1881     channel_statistics[CompositePixelChannel].sum_cubed+=
1882       channel_statistics[i].sum_cubed;
1883     channel_statistics[CompositePixelChannel].sum_fourth_power+=
1884       channel_statistics[i].sum_fourth_power;
1885     channel_statistics[CompositePixelChannel].mean+=channel_statistics[i].mean;
1886     channel_statistics[CompositePixelChannel].variance+=
1887       channel_statistics[i].variance-channel_statistics[i].mean*
1888       channel_statistics[i].mean;
1889     channel_statistics[CompositePixelChannel].standard_deviation+=
1890       channel_statistics[i].variance-channel_statistics[i].mean*
1891       channel_statistics[i].mean;
1892   }
1893   channels=GetImageChannels(image);
1894   channel_statistics[CompositePixelChannel].area/=channels;
1895   channel_statistics[CompositePixelChannel].sum/=channels;
1896   channel_statistics[CompositePixelChannel].sum_squared/=channels;
1897   channel_statistics[CompositePixelChannel].sum_cubed/=channels;
1898   channel_statistics[CompositePixelChannel].sum_fourth_power/=channels;
1899   channel_statistics[CompositePixelChannel].mean/=channels;
1900   channel_statistics[CompositePixelChannel].variance/=channels;
1901   channel_statistics[CompositePixelChannel].standard_deviation=
1902     sqrt(channel_statistics[CompositePixelChannel].standard_deviation/channels);
1903   channel_statistics[CompositePixelChannel].kurtosis/=channels;
1904   channel_statistics[CompositePixelChannel].skewness/=channels;
1905   for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
1906   {
1907     double
1908       standard_deviation;
1909
1910     if (channel_statistics[i].standard_deviation == 0.0)
1911       continue;
1912     standard_deviation=PerceptibleReciprocal(
1913       channel_statistics[i].standard_deviation);
1914     channel_statistics[i].skewness=(channel_statistics[i].sum_cubed-3.0*
1915       channel_statistics[i].mean*channel_statistics[i].sum_squared+2.0*
1916       channel_statistics[i].mean*channel_statistics[i].mean*
1917       channel_statistics[i].mean)*(standard_deviation*standard_deviation*
1918       standard_deviation);
1919     channel_statistics[i].kurtosis=(channel_statistics[i].sum_fourth_power-4.0*
1920       channel_statistics[i].mean*channel_statistics[i].sum_cubed+6.0*
1921       channel_statistics[i].mean*channel_statistics[i].mean*
1922       channel_statistics[i].sum_squared-3.0*channel_statistics[i].mean*
1923       channel_statistics[i].mean*1.0*channel_statistics[i].mean*
1924       channel_statistics[i].mean)*(standard_deviation*standard_deviation*
1925       standard_deviation*standard_deviation)-3.0;
1926   }
1927   if (y < (ssize_t) image->rows)
1928     channel_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1929       channel_statistics);
1930   return(channel_statistics);
1931 }
1932 \f
1933 /*
1934 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1935 %                                                                             %
1936 %                                                                             %
1937 %                                                                             %
1938 %     P o l y n o m i a l I m a g e                                           %
1939 %                                                                             %
1940 %                                                                             %
1941 %                                                                             %
1942 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1943 %
1944 %  PolynomialImage() returns a new image where each pixel is the sum of the
1945 %  pixels in the image sequence after applying its corresponding terms
1946 %  (coefficient and degree pairs).
1947 %
1948 %  The format of the PolynomialImage method is:
1949 %
1950 %      Image *PolynomialImage(const Image *images,const size_t number_terms,
1951 %        const double *terms,ExceptionInfo *exception)
1952 %
1953 %  A description of each parameter follows:
1954 %
1955 %    o images: the image sequence.
1956 %
1957 %    o number_terms: the number of terms in the list.  The actual list length
1958 %      is 2 x number_terms + 1 (the constant).
1959 %
1960 %    o terms: the list of polynomial coefficients and degree pairs and a
1961 %      constant.
1962 %
1963 %    o exception: return any errors or warnings in this structure.
1964 %
1965 */
1966
1967 MagickExport Image *PolynomialImage(const Image *images,
1968   const size_t number_terms,const double *terms,ExceptionInfo *exception)
1969 {
1970 #define PolynomialImageTag  "Polynomial/Image"
1971
1972   CacheView
1973     *polynomial_view;
1974
1975   Image
1976     *image;
1977
1978   MagickBooleanType
1979     status;
1980
1981   MagickOffsetType
1982     progress;
1983
1984   PixelChannels
1985     **restrict polynomial_pixels;
1986
1987   size_t
1988     number_images;
1989
1990   ssize_t
1991     y;
1992
1993   assert(images != (Image *) NULL);
1994   assert(images->signature == MagickSignature);
1995   if (images->debug != MagickFalse)
1996     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
1997   assert(exception != (ExceptionInfo *) NULL);
1998   assert(exception->signature == MagickSignature);
1999   image=CloneImage(images,images->columns,images->rows,MagickTrue,
2000     exception);
2001   if (image == (Image *) NULL)
2002     return((Image *) NULL);
2003   if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
2004     {
2005       image=DestroyImage(image);
2006       return((Image *) NULL);
2007     }
2008   number_images=GetImageListLength(images);
2009   polynomial_pixels=AcquirePixelThreadSet(images,number_images);
2010   if (polynomial_pixels == (PixelChannels **) NULL)
2011     {
2012       image=DestroyImage(image);
2013       (void) ThrowMagickException(exception,GetMagickModule(),
2014         ResourceLimitError,"MemoryAllocationFailed","`%s'",images->filename);
2015       return((Image *) NULL);
2016     }
2017   /*
2018     Polynomial image pixels.
2019   */
2020   status=MagickTrue;
2021   progress=0;
2022   polynomial_view=AcquireAuthenticCacheView(image,exception);
2023 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2024   #pragma omp parallel for schedule(static,4) shared(progress,status) \
2025     magick_threads(image,image,image->rows,1)
2026 #endif
2027   for (y=0; y < (ssize_t) image->rows; y++)
2028   {
2029     CacheView
2030       *image_view;
2031
2032     const Image
2033       *next;
2034
2035     const int
2036       id = GetOpenMPThreadId();
2037
2038     register ssize_t
2039       i,
2040       x;
2041
2042     register PixelChannels
2043       *polynomial_pixel;
2044
2045     register Quantum
2046       *restrict q;
2047
2048     ssize_t
2049       j;
2050
2051     if (status == MagickFalse)
2052       continue;
2053     q=QueueCacheViewAuthenticPixels(polynomial_view,0,y,image->columns,1,
2054       exception);
2055     if (q == (Quantum *) NULL)
2056       {
2057         status=MagickFalse;
2058         continue;
2059       }
2060     polynomial_pixel=polynomial_pixels[id];
2061     for (j=0; j < (ssize_t) image->columns; j++)
2062       for (i=0; i < MaxPixelChannels; i++)
2063         polynomial_pixel[j].channel[i]=0.0;
2064     next=images;
2065     for (j=0; j < (ssize_t) number_images; j++)
2066     {
2067       register const Quantum
2068         *p;
2069
2070       if (j >= (ssize_t) number_terms)
2071         continue;
2072       image_view=AcquireVirtualCacheView(next,exception);
2073       p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2074       if (p == (const Quantum *) NULL)
2075         {
2076           image_view=DestroyCacheView(image_view);
2077           break;
2078         }
2079       for (x=0; x < (ssize_t) image->columns; x++)
2080       {
2081         register ssize_t
2082           i;
2083
2084         if (GetPixelReadMask(next,p) == 0)
2085           {
2086             p+=GetPixelChannels(next);
2087             continue;
2088           }
2089         for (i=0; i < (ssize_t) GetPixelChannels(next); i++)
2090         {
2091           MagickRealType
2092             coefficient,
2093             degree;
2094
2095           PixelChannel channel=GetPixelChannelChannel(image,i);
2096           PixelTrait traits=GetPixelChannelTraits(next,channel);
2097           PixelTrait polynomial_traits=GetPixelChannelTraits(image,channel);
2098           if ((traits == UndefinedPixelTrait) ||
2099               (polynomial_traits == UndefinedPixelTrait))
2100             continue;
2101           if ((traits & UpdatePixelTrait) == 0)
2102             continue;
2103           coefficient=(MagickRealType) terms[2*i];
2104           degree=(MagickRealType) terms[(i << 1)+1];
2105           polynomial_pixel[x].channel[i]+=coefficient*
2106             pow(QuantumScale*GetPixelChannel(image,channel,p),degree);
2107         }
2108         p+=GetPixelChannels(next);
2109       }
2110       image_view=DestroyCacheView(image_view);
2111       next=GetNextImageInList(next);
2112     }
2113     for (x=0; x < (ssize_t) image->columns; x++)
2114     {
2115       register ssize_t
2116         i;
2117
2118       if (GetPixelReadMask(image,q) == 0)
2119         {
2120           q+=GetPixelChannels(image);
2121           continue;
2122         }
2123       for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2124       {
2125         PixelChannel channel=GetPixelChannelChannel(image,i);
2126         PixelTrait traits=GetPixelChannelTraits(image,channel);
2127         if (traits == UndefinedPixelTrait)
2128           continue;
2129         if ((traits & UpdatePixelTrait) == 0)
2130           continue;
2131         q[i]=ClampToQuantum(QuantumRange*polynomial_pixel[x].channel[i]);
2132       }
2133       q+=GetPixelChannels(image);
2134     }
2135     if (SyncCacheViewAuthenticPixels(polynomial_view,exception) == MagickFalse)
2136       status=MagickFalse;
2137     if (images->progress_monitor != (MagickProgressMonitor) NULL)
2138       {
2139         MagickBooleanType
2140           proceed;
2141
2142 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2143         #pragma omp critical (MagickCore_PolynomialImages)
2144 #endif
2145         proceed=SetImageProgress(images,PolynomialImageTag,progress++,
2146           image->rows);
2147         if (proceed == MagickFalse)
2148           status=MagickFalse;
2149       }
2150   }
2151   polynomial_view=DestroyCacheView(polynomial_view);
2152   polynomial_pixels=DestroyPixelThreadSet(polynomial_pixels);
2153   if (status == MagickFalse)
2154     image=DestroyImage(image);
2155   return(image);
2156 }
2157 \f
2158 /*
2159 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2160 %                                                                             %
2161 %                                                                             %
2162 %                                                                             %
2163 %     S t a t i s t i c I m a g e                                             %
2164 %                                                                             %
2165 %                                                                             %
2166 %                                                                             %
2167 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2168 %
2169 %  StatisticImage() makes each pixel the min / max / median / mode / etc. of
2170 %  the neighborhood of the specified width and height.
2171 %
2172 %  The format of the StatisticImage method is:
2173 %
2174 %      Image *StatisticImage(const Image *image,const StatisticType type,
2175 %        const size_t width,const size_t height,ExceptionInfo *exception)
2176 %
2177 %  A description of each parameter follows:
2178 %
2179 %    o image: the image.
2180 %
2181 %    o type: the statistic type (median, mode, etc.).
2182 %
2183 %    o width: the width of the pixel neighborhood.
2184 %
2185 %    o height: the height of the pixel neighborhood.
2186 %
2187 %    o exception: return any errors or warnings in this structure.
2188 %
2189 */
2190
2191 typedef struct _SkipNode
2192 {
2193   size_t
2194     next[9],
2195     count,
2196     signature;
2197 } SkipNode;
2198
2199 typedef struct _SkipList
2200 {
2201   ssize_t
2202     level;
2203
2204   SkipNode
2205     *nodes;
2206 } SkipList;
2207
2208 typedef struct _PixelList
2209 {
2210   size_t
2211     length,
2212     seed;
2213
2214   SkipList
2215     skip_list;
2216
2217   size_t
2218     signature;
2219 } PixelList;
2220
2221 static PixelList *DestroyPixelList(PixelList *pixel_list)
2222 {
2223   if (pixel_list == (PixelList *) NULL)
2224     return((PixelList *) NULL);
2225   if (pixel_list->skip_list.nodes != (SkipNode *) NULL)
2226     pixel_list->skip_list.nodes=(SkipNode *) RelinquishMagickMemory(
2227       pixel_list->skip_list.nodes);
2228   pixel_list=(PixelList *) RelinquishMagickMemory(pixel_list);
2229   return(pixel_list);
2230 }
2231
2232 static PixelList **DestroyPixelListThreadSet(PixelList **pixel_list)
2233 {
2234   register ssize_t
2235     i;
2236
2237   assert(pixel_list != (PixelList **) NULL);
2238   for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
2239     if (pixel_list[i] != (PixelList *) NULL)
2240       pixel_list[i]=DestroyPixelList(pixel_list[i]);
2241   pixel_list=(PixelList **) RelinquishMagickMemory(pixel_list);
2242   return(pixel_list);
2243 }
2244
2245 static PixelList *AcquirePixelList(const size_t width,const size_t height)
2246 {
2247   PixelList
2248     *pixel_list;
2249
2250   pixel_list=(PixelList *) AcquireMagickMemory(sizeof(*pixel_list));
2251   if (pixel_list == (PixelList *) NULL)
2252     return(pixel_list);
2253   (void) ResetMagickMemory((void *) pixel_list,0,sizeof(*pixel_list));
2254   pixel_list->length=width*height;
2255   pixel_list->skip_list.nodes=(SkipNode *) AcquireQuantumMemory(65537UL,
2256     sizeof(*pixel_list->skip_list.nodes));
2257   if (pixel_list->skip_list.nodes == (SkipNode *) NULL)
2258     return(DestroyPixelList(pixel_list));
2259   (void) ResetMagickMemory(pixel_list->skip_list.nodes,0,65537UL*
2260     sizeof(*pixel_list->skip_list.nodes));
2261   pixel_list->signature=MagickSignature;
2262   return(pixel_list);
2263 }
2264
2265 static PixelList **AcquirePixelListThreadSet(const size_t width,
2266   const size_t height)
2267 {
2268   PixelList
2269     **pixel_list;
2270
2271   register ssize_t
2272     i;
2273
2274   size_t
2275     number_threads;
2276
2277   number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
2278   pixel_list=(PixelList **) AcquireQuantumMemory(number_threads,
2279     sizeof(*pixel_list));
2280   if (pixel_list == (PixelList **) NULL)
2281     return((PixelList **) NULL);
2282   (void) ResetMagickMemory(pixel_list,0,number_threads*sizeof(*pixel_list));
2283   for (i=0; i < (ssize_t) number_threads; i++)
2284   {
2285     pixel_list[i]=AcquirePixelList(width,height);
2286     if (pixel_list[i] == (PixelList *) NULL)
2287       return(DestroyPixelListThreadSet(pixel_list));
2288   }
2289   return(pixel_list);
2290 }
2291
2292 static void AddNodePixelList(PixelList *pixel_list,const size_t color)
2293 {
2294   register SkipList
2295     *p;
2296
2297   register ssize_t
2298     level;
2299
2300   size_t
2301     search,
2302     update[9];
2303
2304   /*
2305     Initialize the node.
2306   */
2307   p=(&pixel_list->skip_list);
2308   p->nodes[color].signature=pixel_list->signature;
2309   p->nodes[color].count=1;
2310   /*
2311     Determine where it belongs in the list.
2312   */
2313   search=65536UL;
2314   for (level=p->level; level >= 0; level--)
2315   {
2316     while (p->nodes[search].next[level] < color)
2317       search=p->nodes[search].next[level];
2318     update[level]=search;
2319   }
2320   /*
2321     Generate a pseudo-random level for this node.
2322   */
2323   for (level=0; ; level++)
2324   {
2325     pixel_list->seed=(pixel_list->seed*42893621L)+1L;
2326     if ((pixel_list->seed & 0x300) != 0x300)
2327       break;
2328   }
2329   if (level > 8)
2330     level=8;
2331   if (level > (p->level+2))
2332     level=p->level+2;
2333   /*
2334     If we're raising the list's level, link back to the root node.
2335   */
2336   while (level > p->level)
2337   {
2338     p->level++;
2339     update[p->level]=65536UL;
2340   }
2341   /*
2342     Link the node into the skip-list.
2343   */
2344   do
2345   {
2346     p->nodes[color].next[level]=p->nodes[update[level]].next[level];
2347     p->nodes[update[level]].next[level]=color;
2348   } while (level-- > 0);
2349 }
2350
2351 static inline void GetMaximumPixelList(PixelList *pixel_list,Quantum *pixel)
2352 {
2353   register SkipList
2354     *p;
2355
2356   size_t
2357     color,
2358     maximum;
2359
2360   ssize_t
2361     count;
2362
2363   /*
2364     Find the maximum value for each of the color.
2365   */
2366   p=(&pixel_list->skip_list);
2367   color=65536L;
2368   count=0;
2369   maximum=p->nodes[color].next[0];
2370   do
2371   {
2372     color=p->nodes[color].next[0];
2373     if (color > maximum)
2374       maximum=color;
2375     count+=p->nodes[color].count;
2376   } while (count < (ssize_t) pixel_list->length);
2377   *pixel=ScaleShortToQuantum((unsigned short) maximum);
2378 }
2379
2380 static inline void GetMeanPixelList(PixelList *pixel_list,Quantum *pixel)
2381 {
2382   double
2383     sum;
2384
2385   register SkipList
2386     *p;
2387
2388   size_t
2389     color;
2390
2391   ssize_t
2392     count;
2393
2394   /*
2395     Find the mean value for each of the color.
2396   */
2397   p=(&pixel_list->skip_list);
2398   color=65536L;
2399   count=0;
2400   sum=0.0;
2401   do
2402   {
2403     color=p->nodes[color].next[0];
2404     sum+=(double) p->nodes[color].count*color;
2405     count+=p->nodes[color].count;
2406   } while (count < (ssize_t) pixel_list->length);
2407   sum/=pixel_list->length;
2408   *pixel=ScaleShortToQuantum((unsigned short) sum);
2409 }
2410
2411 static inline void GetMedianPixelList(PixelList *pixel_list,Quantum *pixel)
2412 {
2413   register SkipList
2414     *p;
2415
2416   size_t
2417     color;
2418
2419   ssize_t
2420     count;
2421
2422   /*
2423     Find the median value for each of the color.
2424   */
2425   p=(&pixel_list->skip_list);
2426   color=65536L;
2427   count=0;
2428   do
2429   {
2430     color=p->nodes[color].next[0];
2431     count+=p->nodes[color].count;
2432   } while (count <= (ssize_t) (pixel_list->length >> 1));
2433   *pixel=ScaleShortToQuantum((unsigned short) color);
2434 }
2435
2436 static inline void GetMinimumPixelList(PixelList *pixel_list,Quantum *pixel)
2437 {
2438   register SkipList
2439     *p;
2440
2441   size_t
2442     color,
2443     minimum;
2444
2445   ssize_t
2446     count;
2447
2448   /*
2449     Find the minimum value for each of the color.
2450   */
2451   p=(&pixel_list->skip_list);
2452   count=0;
2453   color=65536UL;
2454   minimum=p->nodes[color].next[0];
2455   do
2456   {
2457     color=p->nodes[color].next[0];
2458     if (color < minimum)
2459       minimum=color;
2460     count+=p->nodes[color].count;
2461   } while (count < (ssize_t) pixel_list->length);
2462   *pixel=ScaleShortToQuantum((unsigned short) minimum);
2463 }
2464
2465 static inline void GetModePixelList(PixelList *pixel_list,Quantum *pixel)
2466 {
2467   register SkipList
2468     *p;
2469
2470   size_t
2471     color,
2472     max_count,
2473     mode;
2474
2475   ssize_t
2476     count;
2477
2478   /*
2479     Make each pixel the 'predominant color' of the specified neighborhood.
2480   */
2481   p=(&pixel_list->skip_list);
2482   color=65536L;
2483   mode=color;
2484   max_count=p->nodes[mode].count;
2485   count=0;
2486   do
2487   {
2488     color=p->nodes[color].next[0];
2489     if (p->nodes[color].count > max_count)
2490       {
2491         mode=color;
2492         max_count=p->nodes[mode].count;
2493       }
2494     count+=p->nodes[color].count;
2495   } while (count < (ssize_t) pixel_list->length);
2496   *pixel=ScaleShortToQuantum((unsigned short) mode);
2497 }
2498
2499 static inline void GetNonpeakPixelList(PixelList *pixel_list,Quantum *pixel)
2500 {
2501   register SkipList
2502     *p;
2503
2504   size_t
2505     color,
2506     next,
2507     previous;
2508
2509   ssize_t
2510     count;
2511
2512   /*
2513     Finds the non peak value for each of the colors.
2514   */
2515   p=(&pixel_list->skip_list);
2516   color=65536L;
2517   next=p->nodes[color].next[0];
2518   count=0;
2519   do
2520   {
2521     previous=color;
2522     color=next;
2523     next=p->nodes[color].next[0];
2524     count+=p->nodes[color].count;
2525   } while (count <= (ssize_t) (pixel_list->length >> 1));
2526   if ((previous == 65536UL) && (next != 65536UL))
2527     color=next;
2528   else
2529     if ((previous != 65536UL) && (next == 65536UL))
2530       color=previous;
2531   *pixel=ScaleShortToQuantum((unsigned short) color);
2532 }
2533
2534 static inline void GetStandardDeviationPixelList(PixelList *pixel_list,
2535   Quantum *pixel)
2536 {
2537   double
2538     sum,
2539     sum_squared;
2540
2541   register SkipList
2542     *p;
2543
2544   size_t
2545     color;
2546
2547   ssize_t
2548     count;
2549
2550   /*
2551     Find the standard-deviation value for each of the color.
2552   */
2553   p=(&pixel_list->skip_list);
2554   color=65536L;
2555   count=0;
2556   sum=0.0;
2557   sum_squared=0.0;
2558   do
2559   {
2560     register ssize_t
2561       i;
2562
2563     color=p->nodes[color].next[0];
2564     sum+=(double) p->nodes[color].count*color;
2565     for (i=0; i < (ssize_t) p->nodes[color].count; i++)
2566       sum_squared+=((double) color)*((double) color);
2567     count+=p->nodes[color].count;
2568   } while (count < (ssize_t) pixel_list->length);
2569   sum/=pixel_list->length;
2570   sum_squared/=pixel_list->length;
2571   *pixel=ScaleShortToQuantum((unsigned short) sqrt(sum_squared-(sum*sum)));
2572 }
2573
2574 static inline void InsertPixelList(const Quantum pixel,PixelList *pixel_list)
2575 {
2576   size_t
2577     signature;
2578
2579   unsigned short
2580     index;
2581
2582   index=ScaleQuantumToShort(pixel);
2583   signature=pixel_list->skip_list.nodes[index].signature;
2584   if (signature == pixel_list->signature)
2585     {
2586       pixel_list->skip_list.nodes[index].count++;
2587       return;
2588     }
2589   AddNodePixelList(pixel_list,index);
2590 }
2591
2592 static inline double MagickAbsoluteValue(const double x)
2593 {
2594   if (x < 0)
2595     return(-x);
2596   return(x);
2597 }
2598
2599 static inline size_t MagickMax(const size_t x,const size_t y)
2600 {
2601   if (x > y)
2602     return(x);
2603   return(y);
2604 }
2605
2606 static void ResetPixelList(PixelList *pixel_list)
2607 {
2608   int
2609     level;
2610
2611   register SkipNode
2612     *root;
2613
2614   register SkipList
2615     *p;
2616
2617   /*
2618     Reset the skip-list.
2619   */
2620   p=(&pixel_list->skip_list);
2621   root=p->nodes+65536UL;
2622   p->level=0;
2623   for (level=0; level < 9; level++)
2624     root->next[level]=65536UL;
2625   pixel_list->seed=pixel_list->signature++;
2626 }
2627
2628 MagickExport Image *StatisticImage(const Image *image,const StatisticType type,
2629   const size_t width,const size_t height,ExceptionInfo *exception)
2630 {
2631 #define StatisticImageTag  "Statistic/Image"
2632
2633   CacheView
2634     *image_view,
2635     *statistic_view;
2636
2637   Image
2638     *statistic_image;
2639
2640   MagickBooleanType
2641     status;
2642
2643   MagickOffsetType
2644     progress;
2645
2646   PixelList
2647     **restrict pixel_list;
2648
2649   ssize_t
2650     center,
2651     y;
2652
2653   /*
2654     Initialize statistics image attributes.
2655   */
2656   assert(image != (Image *) NULL);
2657   assert(image->signature == MagickSignature);
2658   if (image->debug != MagickFalse)
2659     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2660   assert(exception != (ExceptionInfo *) NULL);
2661   assert(exception->signature == MagickSignature);
2662   statistic_image=CloneImage(image,image->columns,image->rows,MagickTrue,
2663     exception);
2664   if (statistic_image == (Image *) NULL)
2665     return((Image *) NULL);
2666   status=SetImageStorageClass(statistic_image,DirectClass,exception);
2667   if (status == MagickFalse)
2668     {
2669       statistic_image=DestroyImage(statistic_image);
2670       return((Image *) NULL);
2671     }
2672   pixel_list=AcquirePixelListThreadSet(MagickMax(width,1),MagickMax(height,1));
2673   if (pixel_list == (PixelList **) NULL)
2674     {
2675       statistic_image=DestroyImage(statistic_image);
2676       ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2677     }
2678   /*
2679     Make each pixel the min / max / median / mode / etc. of the neighborhood.
2680   */
2681   center=(ssize_t) GetPixelChannels(image)*(image->columns+MagickMax(width,1))*
2682     (MagickMax(height,1)/2L)+GetPixelChannels(image)*(MagickMax(width,1)/2L);
2683   status=MagickTrue;
2684   progress=0;
2685   image_view=AcquireVirtualCacheView(image,exception);
2686   statistic_view=AcquireAuthenticCacheView(statistic_image,exception);
2687 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2688   #pragma omp parallel for schedule(static,4) shared(progress,status) \
2689     magick_threads(image,statistic_image,statistic_image->rows,1)
2690 #endif
2691   for (y=0; y < (ssize_t) statistic_image->rows; y++)
2692   {
2693     const int
2694       id = GetOpenMPThreadId();
2695
2696     register const Quantum
2697       *restrict p;
2698
2699     register Quantum
2700       *restrict q;
2701
2702     register ssize_t
2703       x;
2704
2705     if (status == MagickFalse)
2706       continue;
2707     p=GetCacheViewVirtualPixels(image_view,-((ssize_t) MagickMax(width,1)/2L),y-
2708       (ssize_t) (MagickMax(height,1)/2L),image->columns+MagickMax(width,1),
2709       MagickMax(height,1),exception);
2710     q=QueueCacheViewAuthenticPixels(statistic_view,0,y,statistic_image->columns,      1,exception);
2711     if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2712       {
2713         status=MagickFalse;
2714         continue;
2715       }
2716     for (x=0; x < (ssize_t) statistic_image->columns; x++)
2717     {
2718       register ssize_t
2719         i;
2720
2721       for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2722       {
2723         Quantum
2724           pixel;
2725
2726         register const Quantum
2727           *restrict pixels;
2728
2729         register ssize_t
2730           u;
2731
2732         ssize_t
2733           v;
2734
2735         PixelChannel channel=GetPixelChannelChannel(image,i);
2736         PixelTrait traits=GetPixelChannelTraits(image,channel);
2737         PixelTrait statistic_traits=GetPixelChannelTraits(statistic_image,
2738           channel);
2739         if ((traits == UndefinedPixelTrait) ||
2740             (statistic_traits == UndefinedPixelTrait))
2741           continue;
2742         if (((statistic_traits & CopyPixelTrait) != 0) ||
2743             (GetPixelReadMask(image,p) == 0))
2744           {
2745             SetPixelChannel(statistic_image,channel,p[center+i],q);
2746             continue;
2747           }
2748         pixels=p;
2749         ResetPixelList(pixel_list[id]);
2750         for (v=0; v < (ssize_t) MagickMax(height,1); v++)
2751         {
2752           for (u=0; u < (ssize_t) MagickMax(width,1); u++)
2753           {
2754             InsertPixelList(pixels[i],pixel_list[id]);
2755             pixels+=GetPixelChannels(image);
2756           }
2757           pixels+=(image->columns-1)*GetPixelChannels(image);
2758         }
2759         switch (type)
2760         {
2761           case GradientStatistic:
2762           {
2763             double
2764               maximum,
2765               minimum;
2766
2767             GetMinimumPixelList(pixel_list[id],&pixel);
2768             minimum=(double) pixel;
2769             GetMaximumPixelList(pixel_list[id],&pixel);
2770             maximum=(double) pixel;
2771             pixel=ClampToQuantum(MagickAbsoluteValue(maximum-minimum));
2772             break;
2773           }
2774           case MaximumStatistic:
2775           {
2776             GetMaximumPixelList(pixel_list[id],&pixel);
2777             break;
2778           }
2779           case MeanStatistic:
2780           {
2781             GetMeanPixelList(pixel_list[id],&pixel);
2782             break;
2783           }
2784           case MedianStatistic:
2785           default:
2786           {
2787             GetMedianPixelList(pixel_list[id],&pixel);
2788             break;
2789           }
2790           case MinimumStatistic:
2791           {
2792             GetMinimumPixelList(pixel_list[id],&pixel);
2793             break;
2794           }
2795           case ModeStatistic:
2796           {
2797             GetModePixelList(pixel_list[id],&pixel);
2798             break;
2799           }
2800           case NonpeakStatistic:
2801           {
2802             GetNonpeakPixelList(pixel_list[id],&pixel);
2803             break;
2804           }
2805           case StandardDeviationStatistic:
2806           {
2807             GetStandardDeviationPixelList(pixel_list[id],&pixel);
2808             break;
2809           }
2810         }
2811         SetPixelChannel(statistic_image,channel,pixel,q);
2812       }
2813       p+=GetPixelChannels(image);
2814       q+=GetPixelChannels(statistic_image);
2815     }
2816     if (SyncCacheViewAuthenticPixels(statistic_view,exception) == MagickFalse)
2817       status=MagickFalse;
2818     if (image->progress_monitor != (MagickProgressMonitor) NULL)
2819       {
2820         MagickBooleanType
2821           proceed;
2822
2823 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2824         #pragma omp critical (MagickCore_StatisticImage)
2825 #endif
2826         proceed=SetImageProgress(image,StatisticImageTag,progress++,
2827           image->rows);
2828         if (proceed == MagickFalse)
2829           status=MagickFalse;
2830       }
2831   }
2832   statistic_view=DestroyCacheView(statistic_view);
2833   image_view=DestroyCacheView(image_view);
2834   pixel_list=DestroyPixelListThreadSet(pixel_list);
2835   if (status == MagickFalse)
2836     statistic_image=DestroyImage(statistic_image);
2837   return(statistic_image);
2838 }