]> granicus.if.org Git - imagemagick/blob - MagickCore/effect.c
(no commit message)
[imagemagick] / MagickCore / effect.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                   EEEEE  FFFFF  FFFFF  EEEEE  CCCC  TTTTT                   %
7 %                   E      F      F      E     C        T                     %
8 %                   EEE    FFF    FFF    EEE   C        T                     %
9 %                   E      F      F      E     C        T                     %
10 %                   EEEEE  F      F      EEEEE  CCCC    T                     %
11 %                                                                             %
12 %                                                                             %
13 %                       MagickCore Image Effects Methods                      %
14 %                                                                             %
15 %                               Software Design                               %
16 %                                 John Cristy                                 %
17 %                                 October 1996                                %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2013 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/accelerate.h"
45 #include "MagickCore/blob.h"
46 #include "MagickCore/cache-view.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colorspace.h"
50 #include "MagickCore/constitute.h"
51 #include "MagickCore/decorate.h"
52 #include "MagickCore/distort.h"
53 #include "MagickCore/draw.h"
54 #include "MagickCore/enhance.h"
55 #include "MagickCore/exception.h"
56 #include "MagickCore/exception-private.h"
57 #include "MagickCore/effect.h"
58 #include "MagickCore/fx.h"
59 #include "MagickCore/gem.h"
60 #include "MagickCore/gem-private.h"
61 #include "MagickCore/geometry.h"
62 #include "MagickCore/image-private.h"
63 #include "MagickCore/list.h"
64 #include "MagickCore/log.h"
65 #include "MagickCore/memory_.h"
66 #include "MagickCore/memory-private.h"
67 #include "MagickCore/monitor.h"
68 #include "MagickCore/monitor-private.h"
69 #include "MagickCore/montage.h"
70 #include "MagickCore/morphology.h"
71 #include "MagickCore/paint.h"
72 #include "MagickCore/pixel-accessor.h"
73 #include "MagickCore/pixel-private.h"
74 #include "MagickCore/property.h"
75 #include "MagickCore/quantize.h"
76 #include "MagickCore/quantum.h"
77 #include "MagickCore/quantum-private.h"
78 #include "MagickCore/random_.h"
79 #include "MagickCore/random-private.h"
80 #include "MagickCore/resample.h"
81 #include "MagickCore/resample-private.h"
82 #include "MagickCore/resize.h"
83 #include "MagickCore/resource_.h"
84 #include "MagickCore/segment.h"
85 #include "MagickCore/shear.h"
86 #include "MagickCore/signature-private.h"
87 #include "MagickCore/statistic.h"
88 #include "MagickCore/string_.h"
89 #include "MagickCore/thread-private.h"
90 #include "MagickCore/transform.h"
91 #include "MagickCore/threshold.h"
92 \f
93 /*
94 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
95 %                                                                             %
96 %                                                                             %
97 %                                                                             %
98 %     A d a p t i v e B l u r I m a g e                                       %
99 %                                                                             %
100 %                                                                             %
101 %                                                                             %
102 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103 %
104 %  AdaptiveBlurImage() adaptively blurs the image by blurring less
105 %  intensely near image edges and more intensely far from edges.  We blur the
106 %  image with a Gaussian operator of the given radius and standard deviation
107 %  (sigma).  For reasonable results, radius should be larger than sigma.  Use a
108 %  radius of 0 and AdaptiveBlurImage() selects a suitable radius for you.
109 %
110 %  The format of the AdaptiveBlurImage method is:
111 %
112 %      Image *AdaptiveBlurImage(const Image *image,const double radius,
113 %        const double sigma,ExceptionInfo *exception)
114 %
115 %  A description of each parameter follows:
116 %
117 %    o image: the image.
118 %
119 %    o radius: the radius of the Gaussian, in pixels, not counting the center
120 %      pixel.
121 %
122 %    o sigma: the standard deviation of the Laplacian, in pixels.
123 %
124 %    o exception: return any errors or warnings in this structure.
125 %
126 */
127
128 MagickExport MagickBooleanType AdaptiveLevelImage(Image *image,
129   const char *levels,ExceptionInfo *exception)
130 {
131   double
132     black_point,
133     gamma,
134     white_point;
135
136   GeometryInfo
137     geometry_info;
138
139   MagickBooleanType
140     status;
141
142   MagickStatusType
143     flags;
144
145   /*
146     Parse levels.
147   */
148   if (levels == (char *) NULL)
149     return(MagickFalse);
150   flags=ParseGeometry(levels,&geometry_info);
151   black_point=geometry_info.rho;
152   white_point=(double) QuantumRange;
153   if ((flags & SigmaValue) != 0)
154     white_point=geometry_info.sigma;
155   gamma=1.0;
156   if ((flags & XiValue) != 0)
157     gamma=geometry_info.xi;
158   if ((flags & PercentValue) != 0)
159     {
160       black_point*=(double) image->columns*image->rows/100.0;
161       white_point*=(double) image->columns*image->rows/100.0;
162     }
163   if ((flags & SigmaValue) == 0)
164     white_point=(double) QuantumRange-black_point;
165   if ((flags & AspectValue ) == 0)
166     status=LevelImage(image,black_point,white_point,gamma,exception);
167   else
168     status=LevelizeImage(image,black_point,white_point,gamma,exception);
169   return(status);
170 }
171
172 MagickExport Image *AdaptiveBlurImage(const Image *image,const double radius,
173   const double sigma,ExceptionInfo *exception)
174 {
175 #define AdaptiveBlurImageTag  "Convolve/Image"
176 #define MagickSigma  (fabs(sigma) < MagickEpsilon ? MagickEpsilon : sigma)
177
178   CacheView
179     *blur_view,
180     *edge_view,
181     *image_view;
182
183   double
184     normalize;
185
186   Image
187     *blur_image,
188     *edge_image,
189     *gaussian_image;
190
191   MagickBooleanType
192     status;
193
194   MagickOffsetType
195     progress;
196
197   MagickRealType
198     **kernel;
199
200   register ssize_t
201     i;
202
203   size_t
204     width;
205
206   ssize_t
207     j,
208     k,
209     u,
210     v,
211     y;
212
213   assert(image != (const Image *) NULL);
214   assert(image->signature == MagickSignature);
215   if (image->debug != MagickFalse)
216     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
217   assert(exception != (ExceptionInfo *) NULL);
218   assert(exception->signature == MagickSignature);
219   blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
220   if (blur_image == (Image *) NULL)
221     return((Image *) NULL);
222   if (fabs(sigma) < MagickEpsilon)
223     return(blur_image);
224   if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
225     {
226       blur_image=DestroyImage(blur_image);
227       return((Image *) NULL);
228     }
229   /*
230     Edge detect the image brighness channel, level, blur, and level again.
231   */
232   edge_image=EdgeImage(image,radius,sigma,exception);
233   if (edge_image == (Image *) NULL)
234     {
235       blur_image=DestroyImage(blur_image);
236       return((Image *) NULL);
237     }
238   (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
239   gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
240   if (gaussian_image != (Image *) NULL)
241     {
242       edge_image=DestroyImage(edge_image);
243       edge_image=gaussian_image;
244     }
245   (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
246   /*
247     Create a set of kernels from maximum (radius,sigma) to minimum.
248   */
249   width=GetOptimalKernelWidth2D(radius,sigma);
250   kernel=(MagickRealType **) MagickAssumeAligned(AcquireAlignedMemory((size_t)
251     width,sizeof(*kernel)));
252   if (kernel == (MagickRealType  **) NULL)
253     {
254       edge_image=DestroyImage(edge_image);
255       blur_image=DestroyImage(blur_image);
256       ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
257     }
258   (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
259   for (i=0; i < (ssize_t) width; i+=2)
260   {
261     kernel[i]=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory(
262       (size_t) (width-i),(width-i)*sizeof(**kernel)));
263     if (kernel[i] == (MagickRealType *) NULL)
264       break;
265     normalize=0.0;
266     j=(ssize_t) (width-i)/2;
267     k=0;
268     for (v=(-j); v <= j; v++)
269     {
270       for (u=(-j); u <= j; u++)
271       {
272         kernel[i][k]=(MagickRealType) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
273           MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
274         normalize+=kernel[i][k];
275         k++;
276       }
277     }
278     if (fabs(normalize) < MagickEpsilon)
279       normalize=MagickEpsilon;
280     normalize=PerceptibleReciprocal(normalize);
281     for (k=0; k < (j*j); k++)
282       kernel[i][k]=normalize*kernel[i][k];
283   }
284   if (i < (ssize_t) width)
285     {
286       for (i-=2; i >= 0; i-=2)
287         kernel[i]=(MagickRealType *) RelinquishAlignedMemory(kernel[i]);
288       kernel=(MagickRealType **) RelinquishAlignedMemory(kernel);
289       edge_image=DestroyImage(edge_image);
290       blur_image=DestroyImage(blur_image);
291       ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
292     }
293   /*
294     Adaptively blur image.
295   */
296   status=MagickTrue;
297   progress=0;
298   image_view=AcquireVirtualCacheView(image,exception);
299   edge_view=AcquireVirtualCacheView(edge_image,exception);
300   blur_view=AcquireAuthenticCacheView(blur_image,exception);
301 #if defined(MAGICKCORE_OPENMP_SUPPORT)
302   #pragma omp parallel for schedule(static,4) shared(progress,status) \
303     magick_threads(image,blur_image,blur_image->rows,1)
304 #endif
305   for (y=0; y < (ssize_t) blur_image->rows; y++)
306   {
307     register const Quantum
308       *restrict r;
309
310     register Quantum
311       *restrict q;
312
313     register ssize_t
314       x;
315
316     if (status == MagickFalse)
317       continue;
318     r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
319     q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
320       exception);
321     if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
322       {
323         status=MagickFalse;
324         continue;
325       }
326     for (x=0; x < (ssize_t) blur_image->columns; x++)
327     {
328       register const Quantum
329         *restrict p;
330
331       register ssize_t
332         i;
333
334       ssize_t
335         center,
336         j;
337
338       j=(ssize_t) ceil((double) width*QuantumScale*
339         GetPixelIntensity(edge_image,r)-0.5);
340       if (j < 0)
341         j=0;
342       else
343         if (j > (ssize_t) width)
344           j=(ssize_t) width;
345       if ((j & 0x01) != 0)
346         j--;
347       p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
348         (ssize_t) ((width-j)/2L),width-j,width-j,exception);
349       if (p == (const Quantum *) NULL)
350         break;
351       center=(ssize_t) GetPixelChannels(image)*(width-j)*((width-j)/2L)+
352         GetPixelChannels(image)*((width-j)/2L);
353       for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
354       {
355         double
356           alpha,
357           gamma,
358           pixel;
359
360         PixelChannel
361           channel;
362
363         PixelTrait
364           blur_traits,
365           traits;
366
367         register const MagickRealType
368           *restrict k;
369
370         register const Quantum
371           *restrict pixels;
372
373         register ssize_t
374           u;
375
376         ssize_t
377           v;
378
379         channel=GetPixelChannelChannel(image,i);
380         traits=GetPixelChannelTraits(image,channel);
381         blur_traits=GetPixelChannelTraits(blur_image,channel);
382         if ((traits == UndefinedPixelTrait) ||
383             (blur_traits == UndefinedPixelTrait))
384           continue;
385         if (((blur_traits & CopyPixelTrait) != 0) ||
386             (GetPixelMask(image,p+center) == 0))
387           {
388             SetPixelChannel(blur_image,channel,p[center+i],q);
389             continue;
390           }
391         k=kernel[j];
392         pixels=p;
393         pixel=0.0;
394         gamma=0.0;
395         if ((blur_traits & BlendPixelTrait) == 0)
396           {
397             /*
398               No alpha blending.
399             */
400             for (v=0; v < (ssize_t) (width-j); v++)
401             {
402               for (u=0; u < (ssize_t) (width-j); u++)
403               {
404                 pixel+=(*k)*pixels[i];
405                 gamma+=(*k);
406                 k++;
407                 pixels+=GetPixelChannels(image);
408               }
409             }
410             gamma=PerceptibleReciprocal(gamma);
411             SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
412             continue;
413           }
414         /*
415           Alpha blending.
416         */
417         for (v=0; v < (ssize_t) (width-j); v++)
418         {
419           for (u=0; u < (ssize_t) (width-j); u++)
420           {
421             alpha=(double) (QuantumScale*GetPixelAlpha(image,pixels));
422             pixel+=(*k)*alpha*pixels[i];
423             gamma+=(*k)*alpha;
424             k++;
425             pixels+=GetPixelChannels(image);
426           }
427         }
428         gamma=PerceptibleReciprocal(gamma);
429         SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
430       }
431       q+=GetPixelChannels(blur_image);
432       r+=GetPixelChannels(edge_image);
433     }
434     if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
435       status=MagickFalse;
436     if (image->progress_monitor != (MagickProgressMonitor) NULL)
437       {
438         MagickBooleanType
439           proceed;
440
441 #if defined(MAGICKCORE_OPENMP_SUPPORT)
442         #pragma omp critical (MagickCore_AdaptiveBlurImage)
443 #endif
444         proceed=SetImageProgress(image,AdaptiveBlurImageTag,progress++,
445           image->rows);
446         if (proceed == MagickFalse)
447           status=MagickFalse;
448       }
449   }
450   blur_image->type=image->type;
451   blur_view=DestroyCacheView(blur_view);
452   edge_view=DestroyCacheView(edge_view);
453   image_view=DestroyCacheView(image_view);
454   edge_image=DestroyImage(edge_image);
455   for (i=0; i < (ssize_t) width;  i+=2)
456     kernel[i]=(MagickRealType *) RelinquishAlignedMemory(kernel[i]);
457   kernel=(MagickRealType **) RelinquishAlignedMemory(kernel);
458   if (status == MagickFalse)
459     blur_image=DestroyImage(blur_image);
460   return(blur_image);
461 }
462 \f
463 /*
464 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
465 %                                                                             %
466 %                                                                             %
467 %                                                                             %
468 %     A d a p t i v e S h a r p e n I m a g e                                 %
469 %                                                                             %
470 %                                                                             %
471 %                                                                             %
472 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
473 %
474 %  AdaptiveSharpenImage() adaptively sharpens the image by sharpening more
475 %  intensely near image edges and less intensely far from edges. We sharpen the
476 %  image with a Gaussian operator of the given radius and standard deviation
477 %  (sigma).  For reasonable results, radius should be larger than sigma.  Use a
478 %  radius of 0 and AdaptiveSharpenImage() selects a suitable radius for you.
479 %
480 %  The format of the AdaptiveSharpenImage method is:
481 %
482 %      Image *AdaptiveSharpenImage(const Image *image,const double radius,
483 %        const double sigma,ExceptionInfo *exception)
484 %
485 %  A description of each parameter follows:
486 %
487 %    o image: the image.
488 %
489 %    o radius: the radius of the Gaussian, in pixels, not counting the center
490 %      pixel.
491 %
492 %    o sigma: the standard deviation of the Laplacian, in pixels.
493 %
494 %    o exception: return any errors or warnings in this structure.
495 %
496 */
497 MagickExport Image *AdaptiveSharpenImage(const Image *image,const double radius,
498   const double sigma,ExceptionInfo *exception)
499 {
500 #define AdaptiveSharpenImageTag  "Convolve/Image"
501 #define MagickSigma  (fabs(sigma) < MagickEpsilon ? MagickEpsilon : sigma)
502
503   CacheView
504     *sharp_view,
505     *edge_view,
506     *image_view;
507
508   double
509     normalize;
510
511   Image
512     *sharp_image,
513     *edge_image,
514     *gaussian_image;
515
516   MagickBooleanType
517     status;
518
519   MagickOffsetType
520     progress;
521
522   MagickRealType
523     **kernel;
524
525   register ssize_t
526     i;
527
528   size_t
529     width;
530
531   ssize_t
532     j,
533     k,
534     u,
535     v,
536     y;
537
538   assert(image != (const Image *) NULL);
539   assert(image->signature == MagickSignature);
540   if (image->debug != MagickFalse)
541     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
542   assert(exception != (ExceptionInfo *) NULL);
543   assert(exception->signature == MagickSignature);
544   sharp_image=CloneImage(image,0,0,MagickTrue,exception);
545   if (sharp_image == (Image *) NULL)
546     return((Image *) NULL);
547   if (fabs(sigma) < MagickEpsilon)
548     return(sharp_image);
549   if (SetImageStorageClass(sharp_image,DirectClass,exception) == MagickFalse)
550     {
551       sharp_image=DestroyImage(sharp_image);
552       return((Image *) NULL);
553     }
554   /*
555     Edge detect the image brighness channel, level, sharp, and level again.
556   */
557   edge_image=EdgeImage(image,radius,sigma,exception);
558   if (edge_image == (Image *) NULL)
559     {
560       sharp_image=DestroyImage(sharp_image);
561       return((Image *) NULL);
562     }
563   (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
564   gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
565   if (gaussian_image != (Image *) NULL)
566     {
567       edge_image=DestroyImage(edge_image);
568       edge_image=gaussian_image;
569     }
570   (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
571   /*
572     Create a set of kernels from maximum (radius,sigma) to minimum.
573   */
574   width=GetOptimalKernelWidth2D(radius,sigma);
575   kernel=(MagickRealType **) MagickAssumeAligned(AcquireAlignedMemory((size_t)
576     width,sizeof(*kernel)));
577   if (kernel == (MagickRealType **) NULL)
578     {
579       edge_image=DestroyImage(edge_image);
580       sharp_image=DestroyImage(sharp_image);
581       ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
582     }
583   (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
584   for (i=0; i < (ssize_t) width; i+=2)
585   {
586     kernel[i]=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory(
587       (size_t) (width-i),(width-i)*sizeof(**kernel)));
588     if (kernel[i] == (MagickRealType *) NULL)
589       break;
590     normalize=0.0;
591     j=(ssize_t) (width-i)/2;
592     k=0;
593     for (v=(-j); v <= j; v++)
594     {
595       for (u=(-j); u <= j; u++)
596       {
597         kernel[i][k]=(MagickRealType) (-exp(-((double) u*u+v*v)/(2.0*
598           MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
599         normalize+=kernel[i][k];
600         k++;
601       }
602     }
603     if (fabs(normalize) < MagickEpsilon)
604       normalize=MagickEpsilon;
605     normalize=PerceptibleReciprocal(normalize);
606     for (k=0; k < (j*j); k++)
607       kernel[i][k]=normalize*kernel[i][k];
608   }
609   if (i < (ssize_t) width)
610     {
611       for (i-=2; i >= 0; i-=2)
612         kernel[i]=(MagickRealType *) RelinquishAlignedMemory(kernel[i]);
613       kernel=(MagickRealType **) RelinquishAlignedMemory(kernel);
614       edge_image=DestroyImage(edge_image);
615       sharp_image=DestroyImage(sharp_image);
616       ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
617     }
618   /*
619     Adaptively sharpen image.
620   */
621   status=MagickTrue;
622   progress=0;
623   image_view=AcquireVirtualCacheView(image,exception);
624   edge_view=AcquireVirtualCacheView(edge_image,exception);
625   sharp_view=AcquireAuthenticCacheView(sharp_image,exception);
626 #if defined(MAGICKCORE_OPENMP_SUPPORT)
627   #pragma omp parallel for schedule(static,4) shared(progress,status) \
628     magick_threads(image,sharp_image,sharp_image->rows,1)
629 #endif
630   for (y=0; y < (ssize_t) sharp_image->rows; y++)
631   {
632     register const Quantum
633       *restrict r;
634
635     register Quantum
636       *restrict q;
637
638     register ssize_t
639       x;
640
641     if (status == MagickFalse)
642       continue;
643     r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
644     q=QueueCacheViewAuthenticPixels(sharp_view,0,y,sharp_image->columns,1,
645       exception);
646     if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
647       {
648         status=MagickFalse;
649         continue;
650       }
651     for (x=0; x < (ssize_t) sharp_image->columns; x++)
652     {
653       register const Quantum
654         *restrict p;
655
656       register ssize_t
657         i;
658
659       ssize_t
660         center,
661         j;
662
663       j=(ssize_t) ceil((double) width*QuantumScale*
664         GetPixelIntensity(edge_image,r)-0.5);
665       if (j < 0)
666         j=0;
667       else
668         if (j > (ssize_t) width)
669           j=(ssize_t) width;
670       if ((j & 0x01) != 0)
671         j--;
672       p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
673         (ssize_t) ((width-j)/2L),width-j,width-j,exception);
674       if (p == (const Quantum *) NULL)
675         break;
676       center=(ssize_t) GetPixelChannels(image)*(width-j)*((width-j)/2L)+
677         GetPixelChannels(image)*((width-j)/2);
678       for (i=0; i < (ssize_t) GetPixelChannels(sharp_image); i++)
679       {
680         double
681           alpha,
682           gamma,
683           pixel;
684
685         PixelChannel
686           channel;
687
688         PixelTrait
689           sharp_traits,
690           traits;
691
692         register const MagickRealType
693           *restrict k;
694
695         register const Quantum
696           *restrict pixels;
697
698         register ssize_t
699           u;
700
701         ssize_t
702           v;
703
704         channel=GetPixelChannelChannel(image,i);
705         traits=GetPixelChannelTraits(image,channel);
706         sharp_traits=GetPixelChannelTraits(sharp_image,channel);
707         if ((traits == UndefinedPixelTrait) ||
708             (sharp_traits == UndefinedPixelTrait))
709           continue;
710         if (((sharp_traits & CopyPixelTrait) != 0) ||
711             (GetPixelMask(image,p+center) == 0))
712           {
713             SetPixelChannel(sharp_image,channel,p[center+i],q);
714             continue;
715           }
716         k=kernel[j];
717         pixels=p;
718         pixel=0.0;
719         gamma=0.0;
720         if ((sharp_traits & BlendPixelTrait) == 0)
721           {
722             /*
723               No alpha blending.
724             */
725             for (v=0; v < (ssize_t) (width-j); v++)
726             {
727               for (u=0; u < (ssize_t) (width-j); u++)
728               {
729                 pixel+=(*k)*pixels[i];
730                 gamma+=(*k);
731                 k++;
732                 pixels+=GetPixelChannels(image);
733               }
734             }
735             gamma=PerceptibleReciprocal(gamma);
736             SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
737             continue;
738           }
739         /*
740           Alpha blending.
741         */
742         for (v=0; v < (ssize_t) (width-j); v++)
743         {
744           for (u=0; u < (ssize_t) (width-j); u++)
745           {
746             alpha=(double) (QuantumScale*GetPixelAlpha(image,pixels));
747             pixel+=(*k)*alpha*pixels[i];
748             gamma+=(*k)*alpha;
749             k++;
750             pixels+=GetPixelChannels(image);
751           }
752         }
753         gamma=PerceptibleReciprocal(gamma);
754         SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
755       }
756       q+=GetPixelChannels(sharp_image);
757       r+=GetPixelChannels(edge_image);
758     }
759     if (SyncCacheViewAuthenticPixels(sharp_view,exception) == MagickFalse)
760       status=MagickFalse;
761     if (image->progress_monitor != (MagickProgressMonitor) NULL)
762       {
763         MagickBooleanType
764           proceed;
765
766 #if defined(MAGICKCORE_OPENMP_SUPPORT)
767         #pragma omp critical (MagickCore_AdaptiveSharpenImage)
768 #endif
769         proceed=SetImageProgress(image,AdaptiveSharpenImageTag,progress++,
770           image->rows);
771         if (proceed == MagickFalse)
772           status=MagickFalse;
773       }
774   }
775   sharp_image->type=image->type;
776   sharp_view=DestroyCacheView(sharp_view);
777   edge_view=DestroyCacheView(edge_view);
778   image_view=DestroyCacheView(image_view);
779   edge_image=DestroyImage(edge_image);
780   for (i=0; i < (ssize_t) width;  i+=2)
781     kernel[i]=(MagickRealType *) RelinquishAlignedMemory(kernel[i]);
782   kernel=(MagickRealType **) RelinquishAlignedMemory(kernel);
783   if (status == MagickFalse)
784     sharp_image=DestroyImage(sharp_image);
785   return(sharp_image);
786 }
787 \f
788 /*
789 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
790 %                                                                             %
791 %                                                                             %
792 %                                                                             %
793 %     B l u r I m a g e                                                       %
794 %                                                                             %
795 %                                                                             %
796 %                                                                             %
797 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
798 %
799 %  BlurImage() blurs an image.  We convolve the image with a Gaussian operator
800 %  of the given radius and standard deviation (sigma).  For reasonable results,
801 %  the radius should be larger than sigma.  Use a radius of 0 and BlurImage()
802 %  selects a suitable radius for you.
803 %
804 %  The format of the BlurImage method is:
805 %
806 %      Image *BlurImage(const Image *image,const double radius,
807 %        const double sigma,ExceptionInfo *exception)
808 %
809 %  A description of each parameter follows:
810 %
811 %    o image: the image.
812 %
813 %    o radius: the radius of the Gaussian, in pixels, not counting the center
814 %      pixel.
815 %
816 %    o sigma: the standard deviation of the Gaussian, in pixels.
817 %
818 %    o exception: return any errors or warnings in this structure.
819 %
820 */
821 MagickExport Image *BlurImage(const Image *image,const double radius,
822   const double sigma,ExceptionInfo *exception)
823 {
824   char
825     geometry[MaxTextExtent];
826
827   KernelInfo
828     *kernel_info;
829
830   Image
831     *blur_image;
832
833   assert(image != (const Image *) NULL);
834   assert(image->signature == MagickSignature);
835   if (image->debug != MagickFalse)
836     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
837   assert(exception != (ExceptionInfo *) NULL);
838   assert(exception->signature == MagickSignature);
839   (void) FormatLocaleString(geometry,MaxTextExtent,
840     "blur:%.20gx%.20g;blur:%.20gx%.20g+90",radius,sigma,radius,sigma);
841   kernel_info=AcquireKernelInfo(geometry);
842   if (kernel_info == (KernelInfo *) NULL)
843     ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
844   blur_image=MorphologyImage(image,ConvolveMorphology,1,kernel_info,exception);
845   kernel_info=DestroyKernelInfo(kernel_info);
846   return(blur_image);
847 }
848 \f
849 /*
850 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
851 %                                                                             %
852 %                                                                             %
853 %                                                                             %
854 %     C o n v o l v e I m a g e                                               %
855 %                                                                             %
856 %                                                                             %
857 %                                                                             %
858 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
859 %
860 %  ConvolveImage() applies a custom convolution kernel to the image.
861 %
862 %  The format of the ConvolveImage method is:
863 %
864 %      Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
865 %        ExceptionInfo *exception)
866 %
867 %  A description of each parameter follows:
868 %
869 %    o image: the image.
870 %
871 %    o kernel: the filtering kernel.
872 %
873 %    o exception: return any errors or warnings in this structure.
874 %
875 */
876 MagickExport Image *ConvolveImage(const Image *image,
877   const KernelInfo *kernel_info,ExceptionInfo *exception)
878 {
879   return(MorphologyImage(image,ConvolveMorphology,1,kernel_info,exception));
880 }
881 \f
882 /*
883 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
884 %                                                                             %
885 %                                                                             %
886 %                                                                             %
887 %     D e s p e c k l e I m a g e                                             %
888 %                                                                             %
889 %                                                                             %
890 %                                                                             %
891 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
892 %
893 %  DespeckleImage() reduces the speckle noise in an image while perserving the
894 %  edges of the original image.  A speckle removing filter uses a complementary %  hulling technique (raising pixels that are darker than their surrounding
895 %  neighbors, then complementarily lowering pixels that are brighter than their
896 %  surrounding neighbors) to reduce the speckle index of that image (reference
897 %  Crimmins speckle removal).
898 %
899 %  The format of the DespeckleImage method is:
900 %
901 %      Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
902 %
903 %  A description of each parameter follows:
904 %
905 %    o image: the image.
906 %
907 %    o exception: return any errors or warnings in this structure.
908 %
909 */
910
911 static void Hull(const Image *image,const ssize_t x_offset,
912   const ssize_t y_offset,const size_t columns,const size_t rows,
913   const int polarity,Quantum *restrict f,Quantum *restrict g)
914 {
915   register Quantum
916     *p,
917     *q,
918     *r,
919     *s;
920
921   ssize_t
922     y;
923
924   assert(f != (Quantum *) NULL);
925   assert(g != (Quantum *) NULL);
926   p=f+(columns+2);
927   q=g+(columns+2);
928   r=p+(y_offset*(columns+2)+x_offset);
929 #if defined(MAGICKCORE_OPENMP_SUPPORT)
930   #pragma omp parallel for schedule(static,4) \
931     magick_threads(image,image,1,1)
932 #endif
933   for (y=0; y < (ssize_t) rows; y++)
934   {
935     MagickRealType
936       v;
937
938     register ssize_t
939       i,
940       x;
941
942     i=(2*y+1)+y*columns;
943     if (polarity > 0)
944       for (x=0; x < (ssize_t) columns; x++)
945       {
946         v=(MagickRealType) p[i];
947         if ((MagickRealType) r[i] >= (v+ScaleCharToQuantum(2)))
948           v+=ScaleCharToQuantum(1);
949         q[i]=(Quantum) v;
950         i++;
951       }
952     else
953       for (x=0; x < (ssize_t) columns; x++)
954       {
955         v=(MagickRealType) p[i];
956         if ((MagickRealType) r[i] <= (v-ScaleCharToQuantum(2)))
957           v-=ScaleCharToQuantum(1);
958         q[i]=(Quantum) v;
959         i++;
960       }
961   }
962   p=f+(columns+2);
963   q=g+(columns+2);
964   r=q+(y_offset*(columns+2)+x_offset);
965   s=q-(y_offset*(columns+2)+x_offset);
966 #if defined(MAGICKCORE_OPENMP_SUPPORT)
967   #pragma omp parallel for schedule(static,4) \
968     magick_threads(image,image,1,1)
969 #endif
970   for (y=0; y < (ssize_t) rows; y++)
971   {
972     register ssize_t
973       i,
974       x;
975
976     MagickRealType
977       v;
978
979     i=(2*y+1)+y*columns;
980     if (polarity > 0)
981       for (x=0; x < (ssize_t) columns; x++)
982       {
983         v=(MagickRealType) q[i];
984         if (((MagickRealType) s[i] >= (v+ScaleCharToQuantum(2))) &&
985             ((MagickRealType) r[i] > v))
986           v+=ScaleCharToQuantum(1);
987         p[i]=(Quantum) v;
988         i++;
989       }
990     else
991       for (x=0; x < (ssize_t) columns; x++)
992       {
993         v=(MagickRealType) q[i];
994         if (((MagickRealType) s[i] <= (v-ScaleCharToQuantum(2))) &&
995             ((MagickRealType) r[i] < v))
996           v-=ScaleCharToQuantum(1);
997         p[i]=(Quantum) v;
998         i++;
999       }
1000   }
1001 }
1002
1003 MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1004 {
1005 #define DespeckleImageTag  "Despeckle/Image"
1006
1007   CacheView
1008     *despeckle_view,
1009     *image_view;
1010
1011   Image
1012     *despeckle_image;
1013
1014   MagickBooleanType
1015     status;
1016
1017   Quantum
1018     *restrict buffer,
1019     *restrict pixels;
1020
1021   register ssize_t
1022     i;
1023
1024   size_t
1025     length;
1026
1027   static const ssize_t
1028     X[4] = {0, 1, 1,-1},
1029     Y[4] = {1, 0, 1, 1};
1030
1031   /*
1032     Allocate despeckled image.
1033   */
1034   assert(image != (const Image *) NULL);
1035   assert(image->signature == MagickSignature);
1036   if (image->debug != MagickFalse)
1037     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1038   assert(exception != (ExceptionInfo *) NULL);
1039   assert(exception->signature == MagickSignature);
1040   despeckle_image=CloneImage(image,0,0,MagickTrue,exception);
1041   if (despeckle_image == (Image *) NULL)
1042     return((Image *) NULL);
1043   status=SetImageStorageClass(despeckle_image,DirectClass,exception);
1044   if (status == MagickFalse)
1045     {
1046       despeckle_image=DestroyImage(despeckle_image);
1047       return((Image *) NULL);
1048     }
1049   /*
1050     Allocate image buffer.
1051   */
1052   length=(size_t) ((image->columns+2)*(image->rows+2));
1053   pixels=(Quantum *) AcquireQuantumMemory(length,sizeof(*pixels));
1054   buffer=(Quantum *) AcquireQuantumMemory(length,sizeof(*buffer));
1055   if ((pixels == (Quantum *) NULL) || (buffer == (Quantum *) NULL))
1056     {
1057       if (buffer != (Quantum *) NULL)
1058         buffer=(Quantum *) RelinquishMagickMemory(buffer);
1059       if (pixels != (Quantum *) NULL)
1060         pixels=(Quantum *) RelinquishMagickMemory(pixels);
1061       despeckle_image=DestroyImage(despeckle_image);
1062       ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1063     }
1064   /*
1065     Reduce speckle in the image.
1066   */
1067   status=MagickTrue;
1068   image_view=AcquireVirtualCacheView(image,exception);
1069   despeckle_view=AcquireAuthenticCacheView(despeckle_image,exception);
1070   for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1071   {
1072     PixelChannel
1073        channel;
1074
1075     PixelTrait
1076       despeckle_traits,
1077       traits;
1078
1079     register ssize_t
1080       k,
1081       x;
1082
1083     ssize_t
1084       j,
1085       y;
1086
1087     if (status == MagickFalse)
1088       continue;
1089     channel=GetPixelChannelChannel(image,i);
1090     traits=GetPixelChannelTraits(image,channel);
1091     despeckle_traits=GetPixelChannelTraits(despeckle_image,channel);
1092     if ((traits == UndefinedPixelTrait) ||
1093         (despeckle_traits == UndefinedPixelTrait))
1094       continue;
1095     if ((despeckle_traits & CopyPixelTrait) != 0)
1096       continue;
1097     (void) ResetMagickMemory(pixels,0,length*sizeof(*pixels));
1098     j=(ssize_t) image->columns+2;
1099     for (y=0; y < (ssize_t) image->rows; y++)
1100     {
1101       register const Quantum
1102         *restrict p;
1103
1104       p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1105       if (p == (const Quantum *) NULL)
1106         {
1107           status=MagickFalse;
1108           continue;
1109         }
1110       j++;
1111       for (x=0; x < (ssize_t) image->columns; x++)
1112       {
1113         pixels[j++]=p[i];
1114         p+=GetPixelChannels(image);
1115       }
1116       j++;
1117     }
1118     (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
1119     for (k=0; k < 4; k++)
1120     {
1121       Hull(image,X[k],Y[k],image->columns,image->rows,1,pixels,buffer);
1122       Hull(image,-X[k],-Y[k],image->columns,image->rows,1,pixels,buffer);
1123       Hull(image,-X[k],-Y[k],image->columns,image->rows,-1,pixels,buffer);
1124       Hull(image,X[k],Y[k],image->columns,image->rows,-1,pixels,buffer);
1125     }
1126     j=(ssize_t) image->columns+2;
1127     for (y=0; y < (ssize_t) image->rows; y++)
1128     {
1129       MagickBooleanType
1130         sync;
1131
1132       register Quantum
1133         *restrict q;
1134
1135       q=GetCacheViewAuthenticPixels(despeckle_view,0,y,despeckle_image->columns,
1136         1,exception);
1137       if (q == (Quantum *) NULL)
1138         {
1139           status=MagickFalse;
1140           continue;
1141         }
1142       j++;
1143       for (x=0; x < (ssize_t) image->columns; x++)
1144       {
1145         SetPixelChannel(despeckle_image,channel,pixels[j++],q);
1146         q+=GetPixelChannels(despeckle_image);
1147       }
1148       sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1149       if (sync == MagickFalse)
1150         status=MagickFalse;
1151       j++;
1152     }
1153     if (image->progress_monitor != (MagickProgressMonitor) NULL)
1154       {
1155         MagickBooleanType
1156           proceed;
1157
1158         proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1159           GetPixelChannels(image));
1160         if (proceed == MagickFalse)
1161           status=MagickFalse;
1162       }
1163   }
1164   despeckle_view=DestroyCacheView(despeckle_view);
1165   image_view=DestroyCacheView(image_view);
1166   buffer=(Quantum *) RelinquishMagickMemory(buffer);
1167   pixels=(Quantum *) RelinquishMagickMemory(pixels);
1168   despeckle_image->type=image->type;
1169   if (status == MagickFalse)
1170     despeckle_image=DestroyImage(despeckle_image);
1171   return(despeckle_image);
1172 }
1173 \f
1174 /*
1175 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1176 %                                                                             %
1177 %                                                                             %
1178 %                                                                             %
1179 %     E d g e I m a g e                                                       %
1180 %                                                                             %
1181 %                                                                             %
1182 %                                                                             %
1183 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1184 %
1185 %  EdgeImage() finds edges in an image.  Radius defines the radius of the
1186 %  convolution filter.  Use a radius of 0 and EdgeImage() selects a suitable
1187 %  radius for you.
1188 %
1189 %  The format of the EdgeImage method is:
1190 %
1191 %      Image *EdgeImage(const Image *image,const double radius,
1192 %        const double sigma,ExceptionInfo *exception)
1193 %
1194 %  A description of each parameter follows:
1195 %
1196 %    o image: the image.
1197 %
1198 %    o radius: the radius of the pixel neighborhood.
1199 %
1200 %    o sigma: the standard deviation of the Gaussian, in pixels.
1201 %
1202 %    o exception: return any errors or warnings in this structure.
1203 %
1204 */
1205 MagickExport Image *EdgeImage(const Image *image,const double radius,
1206   const double sigma,ExceptionInfo *exception)
1207 {
1208   Image
1209     *edge_image;
1210
1211   KernelInfo
1212     *kernel_info;
1213
1214   register ssize_t
1215     i;
1216
1217   size_t
1218     width;
1219
1220   ssize_t
1221     j,
1222     u,
1223     v;
1224
1225   assert(image != (const Image *) NULL);
1226   assert(image->signature == MagickSignature);
1227   if (image->debug != MagickFalse)
1228     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1229   assert(exception != (ExceptionInfo *) NULL);
1230   assert(exception->signature == MagickSignature);
1231   width=GetOptimalKernelWidth1D(radius,sigma);
1232   kernel_info=AcquireKernelInfo((const char *) NULL);
1233   if (kernel_info == (KernelInfo *) NULL)
1234     ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1235   kernel_info->width=width;
1236   kernel_info->height=width;
1237   kernel_info->x=(ssize_t) (width-1)/2;
1238   kernel_info->y=(ssize_t) (width-1)/2;
1239   kernel_info->values=(MagickRealType *) MagickAssumeAligned(
1240     AcquireAlignedMemory(kernel_info->width,kernel_info->width*
1241     sizeof(*kernel_info->values)));
1242   if (kernel_info->values == (MagickRealType *) NULL)
1243     {
1244       kernel_info=DestroyKernelInfo(kernel_info);
1245       ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1246     }
1247   j=(ssize_t) (kernel_info->width-1)/2;
1248   i=0;
1249   for (v=(-j); v <= j; v++)
1250   {
1251     for (u=(-j); u <= j; u++)
1252     {
1253       kernel_info->values[i]=(MagickRealType) (-1.0);
1254       i++;
1255     }
1256   }
1257   kernel_info->values[(i-1)/2]=(MagickRealType) (width*width-1.0);
1258   edge_image=ConvolveImage(image,kernel_info,exception);
1259   kernel_info=DestroyKernelInfo(kernel_info);
1260   return(edge_image);
1261 }
1262 \f
1263 /*
1264 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1265 %                                                                             %
1266 %                                                                             %
1267 %                                                                             %
1268 %     E m b o s s I m a g e                                                   %
1269 %                                                                             %
1270 %                                                                             %
1271 %                                                                             %
1272 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1273 %
1274 %  EmbossImage() returns a grayscale image with a three-dimensional effect.
1275 %  We convolve the image with a Gaussian operator of the given radius and
1276 %  standard deviation (sigma).  For reasonable results, radius should be
1277 %  larger than sigma.  Use a radius of 0 and Emboss() selects a suitable
1278 %  radius for you.
1279 %
1280 %  The format of the EmbossImage method is:
1281 %
1282 %      Image *EmbossImage(const Image *image,const double radius,
1283 %        const double sigma,ExceptionInfo *exception)
1284 %
1285 %  A description of each parameter follows:
1286 %
1287 %    o image: the image.
1288 %
1289 %    o radius: the radius of the pixel neighborhood.
1290 %
1291 %    o sigma: the standard deviation of the Gaussian, in pixels.
1292 %
1293 %    o exception: return any errors or warnings in this structure.
1294 %
1295 */
1296 MagickExport Image *EmbossImage(const Image *image,const double radius,
1297   const double sigma,ExceptionInfo *exception)
1298 {
1299   Image
1300     *emboss_image;
1301
1302   KernelInfo
1303     *kernel_info;
1304
1305   register ssize_t
1306     i;
1307
1308   size_t
1309     width;
1310
1311   ssize_t
1312     j,
1313     k,
1314     u,
1315     v;
1316
1317   assert(image != (const Image *) NULL);
1318   assert(image->signature == MagickSignature);
1319   if (image->debug != MagickFalse)
1320     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1321   assert(exception != (ExceptionInfo *) NULL);
1322   assert(exception->signature == MagickSignature);
1323   width=GetOptimalKernelWidth1D(radius,sigma);
1324   kernel_info=AcquireKernelInfo((const char *) NULL);
1325   if (kernel_info == (KernelInfo *) NULL)
1326     ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1327   kernel_info->width=width;
1328   kernel_info->height=width;
1329   kernel_info->x=(ssize_t) (width-1)/2;
1330   kernel_info->y=(ssize_t) (width-1)/2;
1331   kernel_info->values=(MagickRealType *) MagickAssumeAligned(
1332     AcquireAlignedMemory(kernel_info->width,kernel_info->width*
1333     sizeof(*kernel_info->values)));
1334   if (kernel_info->values == (MagickRealType *) NULL)
1335     {
1336       kernel_info=DestroyKernelInfo(kernel_info);
1337       ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1338     }
1339   j=(ssize_t) (kernel_info->width-1)/2;
1340   k=j;
1341   i=0;
1342   for (v=(-j); v <= j; v++)
1343   {
1344     for (u=(-j); u <= j; u++)
1345     {
1346       kernel_info->values[i]=(MagickRealType) (((u < 0) || (v < 0) ? -8.0 :
1347         8.0)*exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
1348         (2.0*MagickPI*MagickSigma*MagickSigma));
1349       if (u != k)
1350         kernel_info->values[i]=0.0;
1351       i++;
1352     }
1353     k--;
1354   }
1355   emboss_image=ConvolveImage(image,kernel_info,exception);
1356   kernel_info=DestroyKernelInfo(kernel_info);
1357   if (emboss_image != (Image *) NULL)
1358     (void) EqualizeImage(emboss_image,exception);
1359   return(emboss_image);
1360 }
1361 \f
1362 /*
1363 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1364 %                                                                             %
1365 %                                                                             %
1366 %                                                                             %
1367 %     G a u s s i a n B l u r I m a g e                                       %
1368 %                                                                             %
1369 %                                                                             %
1370 %                                                                             %
1371 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1372 %
1373 %  GaussianBlurImage() blurs an image.  We convolve the image with a
1374 %  Gaussian operator of the given radius and standard deviation (sigma).
1375 %  For reasonable results, the radius should be larger than sigma.  Use a
1376 %  radius of 0 and GaussianBlurImage() selects a suitable radius for you
1377 %
1378 %  The format of the GaussianBlurImage method is:
1379 %
1380 %      Image *GaussianBlurImage(const Image *image,onst double radius,
1381 %        const double sigma,ExceptionInfo *exception)
1382 %
1383 %  A description of each parameter follows:
1384 %
1385 %    o image: the image.
1386 %
1387 %    o radius: the radius of the Gaussian, in pixels, not counting the center
1388 %      pixel.
1389 %
1390 %    o sigma: the standard deviation of the Gaussian, in pixels.
1391 %
1392 %    o exception: return any errors or warnings in this structure.
1393 %
1394 */
1395 MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
1396   const double sigma,ExceptionInfo *exception)
1397 {
1398   char
1399     geometry[MaxTextExtent];
1400
1401   KernelInfo
1402     *kernel_info;
1403
1404   Image
1405     *blur_image;
1406
1407   assert(image != (const Image *) NULL);
1408   assert(image->signature == MagickSignature);
1409   if (image->debug != MagickFalse)
1410     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1411   assert(exception != (ExceptionInfo *) NULL);
1412   assert(exception->signature == MagickSignature);
1413   (void) FormatLocaleString(geometry,MaxTextExtent,"gaussian:%.20gx%.20g",
1414     radius,sigma);
1415   kernel_info=AcquireKernelInfo(geometry);
1416   if (kernel_info == (KernelInfo *) NULL)
1417     ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1418   blur_image=MorphologyImage(image,ConvolveMorphology,1,kernel_info,exception);
1419   kernel_info=DestroyKernelInfo(kernel_info);
1420   return(blur_image);
1421 }
1422 \f
1423 /*
1424 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1425 %                                                                             %
1426 %                                                                             %
1427 %                                                                             %
1428 %     M o t i o n B l u r I m a g e                                           %
1429 %                                                                             %
1430 %                                                                             %
1431 %                                                                             %
1432 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1433 %
1434 %  MotionBlurImage() simulates motion blur.  We convolve the image with a
1435 %  Gaussian operator of the given radius and standard deviation (sigma).
1436 %  For reasonable results, radius should be larger than sigma.  Use a
1437 %  radius of 0 and MotionBlurImage() selects a suitable radius for you.
1438 %  Angle gives the angle of the blurring motion.
1439 %
1440 %  Andrew Protano contributed this effect.
1441 %
1442 %  The format of the MotionBlurImage method is:
1443 %
1444 %    Image *MotionBlurImage(const Image *image,const double radius,
1445 %      const double sigma,const double angle,ExceptionInfo *exception)
1446 %
1447 %  A description of each parameter follows:
1448 %
1449 %    o image: the image.
1450 %
1451 %    o radius: the radius of the Gaussian, in pixels, not counting
1452 %      the center pixel.
1453 %
1454 %    o sigma: the standard deviation of the Gaussian, in pixels.
1455 %
1456 %    o angle: Apply the effect along this angle.
1457 %
1458 %    o exception: return any errors or warnings in this structure.
1459 %
1460 */
1461
1462 static MagickRealType *GetMotionBlurKernel(const size_t width,
1463   const double sigma)
1464 {
1465   MagickRealType
1466     *kernel,
1467     normalize;
1468
1469   register ssize_t
1470     i;
1471
1472   /*
1473    Generate a 1-D convolution kernel.
1474   */
1475   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1476   kernel=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory((size_t)
1477     width,sizeof(*kernel)));
1478   if (kernel == (MagickRealType *) NULL)
1479     return(kernel);
1480   normalize=0.0;
1481   for (i=0; i < (ssize_t) width; i++)
1482   {
1483     kernel[i]=(MagickRealType) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
1484       MagickSigma)))/(MagickSQ2PI*MagickSigma));
1485     normalize+=kernel[i];
1486   }
1487   for (i=0; i < (ssize_t) width; i++)
1488     kernel[i]/=normalize;
1489   return(kernel);
1490 }
1491
1492 MagickExport Image *MotionBlurImage(const Image *image,const double radius,
1493   const double sigma,const double angle,ExceptionInfo *exception)
1494 {
1495 #define BlurImageTag  "Blur/Image"
1496
1497   CacheView
1498     *blur_view,
1499     *image_view,
1500     *motion_view;
1501
1502   Image
1503     *blur_image;
1504
1505   MagickBooleanType
1506     status;
1507
1508   MagickOffsetType
1509     progress;
1510
1511   MagickRealType
1512     *kernel;
1513
1514   OffsetInfo
1515     *offset;
1516
1517   PointInfo
1518     point;
1519
1520   register ssize_t
1521     i;
1522
1523   size_t
1524     width;
1525
1526   ssize_t
1527     y;
1528
1529   assert(image != (Image *) NULL);
1530   assert(image->signature == MagickSignature);
1531   if (image->debug != MagickFalse)
1532     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1533   assert(exception != (ExceptionInfo *) NULL);
1534   width=GetOptimalKernelWidth1D(radius,sigma);
1535   kernel=GetMotionBlurKernel(width,sigma);
1536   if (kernel == (MagickRealType *) NULL)
1537     ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1538   offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
1539   if (offset == (OffsetInfo *) NULL)
1540     {
1541       kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
1542       ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1543     }
1544   blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1545   if (blur_image == (Image *) NULL)
1546     {
1547       kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
1548       offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1549       return((Image *) NULL);
1550     }
1551   if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
1552     {
1553       kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
1554       offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1555       blur_image=DestroyImage(blur_image);
1556       return((Image *) NULL);
1557     }
1558   point.x=(double) width*sin(DegreesToRadians(angle));
1559   point.y=(double) width*cos(DegreesToRadians(angle));
1560   for (i=0; i < (ssize_t) width; i++)
1561   {
1562     offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
1563     offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
1564   }
1565   /*
1566     Motion blur image.
1567   */
1568   status=MagickTrue;
1569   progress=0;
1570   image_view=AcquireVirtualCacheView(image,exception);
1571   motion_view=AcquireVirtualCacheView(image,exception);
1572   blur_view=AcquireAuthenticCacheView(blur_image,exception);
1573 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1574   #pragma omp parallel for schedule(static,4) shared(progress,status) \
1575     magick_threads(image,blur_image,image->rows,1)
1576 #endif
1577   for (y=0; y < (ssize_t) image->rows; y++)
1578   {
1579     register const Quantum
1580       *restrict p;
1581
1582     register Quantum
1583       *restrict q;
1584
1585     register ssize_t
1586       x;
1587
1588     if (status == MagickFalse)
1589       continue;
1590     p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1591     q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
1592       exception);
1593     if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1594       {
1595         status=MagickFalse;
1596         continue;
1597       }
1598     for (x=0; x < (ssize_t) image->columns; x++)
1599     {
1600       register ssize_t
1601         i;
1602
1603       for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1604       {
1605         double
1606           alpha,
1607           gamma,
1608           pixel;
1609
1610         PixelChannel
1611           channel;
1612
1613         PixelTrait
1614           blur_traits,
1615           traits;
1616
1617         register const Quantum
1618           *restrict r;
1619
1620         register MagickRealType
1621           *restrict k;
1622
1623         register ssize_t
1624           j;
1625
1626         channel=GetPixelChannelChannel(image,i);
1627         traits=GetPixelChannelTraits(image,channel);
1628         blur_traits=GetPixelChannelTraits(blur_image,channel);
1629         if ((traits == UndefinedPixelTrait) ||
1630             (blur_traits == UndefinedPixelTrait))
1631           continue;
1632         if (((blur_traits & CopyPixelTrait) != 0) ||
1633             (GetPixelMask(image,p) == 0))
1634           {
1635             SetPixelChannel(blur_image,channel,p[i],q);
1636             continue;
1637           }
1638         k=kernel;
1639         pixel=0.0;
1640         if ((blur_traits & BlendPixelTrait) == 0)
1641           {
1642             for (j=0; j < (ssize_t) width; j++)
1643             {
1644               r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+
1645                 offset[j].y,1,1,exception);
1646               if (r == (const Quantum *) NULL)
1647                 {
1648                   status=MagickFalse;
1649                   continue;
1650                 }
1651               pixel+=(*k)*r[i];
1652               k++;
1653             }
1654             SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
1655             continue;
1656           }
1657         alpha=0.0;
1658         gamma=0.0;
1659         for (j=0; j < (ssize_t) width; j++)
1660         {
1661           r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+offset[j].y,1,
1662             1,exception);
1663           if (r == (const Quantum *) NULL)
1664             {
1665               status=MagickFalse;
1666               continue;
1667             }
1668           alpha=(double) (QuantumScale*GetPixelAlpha(image,r));
1669           pixel+=(*k)*alpha*r[i];
1670           gamma+=(*k)*alpha;
1671           k++;
1672         }
1673         gamma=PerceptibleReciprocal(gamma);
1674         SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
1675       }
1676       p+=GetPixelChannels(image);
1677       q+=GetPixelChannels(blur_image);
1678     }
1679     if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1680       status=MagickFalse;
1681     if (image->progress_monitor != (MagickProgressMonitor) NULL)
1682       {
1683         MagickBooleanType
1684           proceed;
1685
1686 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1687         #pragma omp critical (MagickCore_MotionBlurImage)
1688 #endif
1689         proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
1690         if (proceed == MagickFalse)
1691           status=MagickFalse;
1692       }
1693   }
1694   blur_view=DestroyCacheView(blur_view);
1695   motion_view=DestroyCacheView(motion_view);
1696   image_view=DestroyCacheView(image_view);
1697   kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
1698   offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1699   if (status == MagickFalse)
1700     blur_image=DestroyImage(blur_image);
1701   return(blur_image);
1702 }
1703 \f
1704 /*
1705 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1706 %                                                                             %
1707 %                                                                             %
1708 %                                                                             %
1709 %     P r e v i e w I m a g e                                                 %
1710 %                                                                             %
1711 %                                                                             %
1712 %                                                                             %
1713 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1714 %
1715 %  PreviewImage() tiles 9 thumbnails of the specified image with an image
1716 %  processing operation applied with varying parameters.  This may be helpful
1717 %  pin-pointing an appropriate parameter for a particular image processing
1718 %  operation.
1719 %
1720 %  The format of the PreviewImages method is:
1721 %
1722 %      Image *PreviewImages(const Image *image,const PreviewType preview,
1723 %        ExceptionInfo *exception)
1724 %
1725 %  A description of each parameter follows:
1726 %
1727 %    o image: the image.
1728 %
1729 %    o preview: the image processing operation.
1730 %
1731 %    o exception: return any errors or warnings in this structure.
1732 %
1733 */
1734 MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
1735   ExceptionInfo *exception)
1736 {
1737 #define NumberTiles  9
1738 #define PreviewImageTag  "Preview/Image"
1739 #define DefaultPreviewGeometry  "204x204+10+10"
1740
1741   char
1742     factor[MaxTextExtent],
1743     label[MaxTextExtent];
1744
1745   double
1746     degrees,
1747     gamma,
1748     percentage,
1749     radius,
1750     sigma,
1751     threshold;
1752
1753   extern const char
1754     DefaultTileFrame[];
1755
1756   Image
1757     *images,
1758     *montage_image,
1759     *preview_image,
1760     *thumbnail;
1761
1762   ImageInfo
1763     *preview_info;
1764
1765   MagickBooleanType
1766     proceed;
1767
1768   MontageInfo
1769     *montage_info;
1770
1771   QuantizeInfo
1772     quantize_info;
1773
1774   RectangleInfo
1775     geometry;
1776
1777   register ssize_t
1778     i,
1779     x;
1780
1781   size_t
1782     colors;
1783
1784   ssize_t
1785     y;
1786
1787   /*
1788     Open output image file.
1789   */
1790   assert(image != (Image *) NULL);
1791   assert(image->signature == MagickSignature);
1792   if (image->debug != MagickFalse)
1793     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1794   colors=2;
1795   degrees=0.0;
1796   gamma=(-0.2f);
1797   preview_info=AcquireImageInfo();
1798   SetGeometry(image,&geometry);
1799   (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
1800     &geometry.width,&geometry.height);
1801   images=NewImageList();
1802   percentage=12.5;
1803   GetQuantizeInfo(&quantize_info);
1804   radius=0.0;
1805   sigma=1.0;
1806   threshold=0.0;
1807   x=0;
1808   y=0;
1809   for (i=0; i < NumberTiles; i++)
1810   {
1811     thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
1812     if (thumbnail == (Image *) NULL)
1813       break;
1814     (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
1815       (void *) NULL);
1816     (void) SetImageProperty(thumbnail,"label",DefaultTileLabel,exception);
1817     if (i == (NumberTiles/2))
1818       {
1819         (void) QueryColorCompliance("#dfdfdf",AllCompliance,
1820           &thumbnail->matte_color,exception);
1821         AppendImageToList(&images,thumbnail);
1822         continue;
1823       }
1824     switch (preview)
1825     {
1826       case RotatePreview:
1827       {
1828         degrees+=45.0;
1829         preview_image=RotateImage(thumbnail,degrees,exception);
1830         (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
1831         break;
1832       }
1833       case ShearPreview:
1834       {
1835         degrees+=5.0;
1836         preview_image=ShearImage(thumbnail,degrees,degrees,exception);
1837         (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",degrees,
1838           2.0*degrees);
1839         break;
1840       }
1841       case RollPreview:
1842       {
1843         x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
1844         y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
1845         preview_image=RollImage(thumbnail,x,y,exception);
1846         (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
1847           (double) x,(double) y);
1848         break;
1849       }
1850       case HuePreview:
1851       {
1852         preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1853         if (preview_image == (Image *) NULL)
1854           break;
1855         (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",2.0*
1856           percentage);
1857         (void) ModulateImage(preview_image,factor,exception);
1858         (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
1859         break;
1860       }
1861       case SaturationPreview:
1862       {
1863         preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1864         if (preview_image == (Image *) NULL)
1865           break;
1866         (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",2.0*percentage);
1867         (void) ModulateImage(preview_image,factor,exception);
1868         (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
1869         break;
1870       }
1871       case BrightnessPreview:
1872       {
1873         preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1874         if (preview_image == (Image *) NULL)
1875           break;
1876         (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
1877         (void) ModulateImage(preview_image,factor,exception);
1878         (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
1879         break;
1880       }
1881       case GammaPreview:
1882       default:
1883       {
1884         preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1885         if (preview_image == (Image *) NULL)
1886           break;
1887         gamma+=0.4f;
1888         (void) GammaImage(preview_image,gamma,exception);
1889         (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
1890         break;
1891       }
1892       case SpiffPreview:
1893       {
1894         preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1895         if (preview_image != (Image *) NULL)
1896           for (x=0; x < i; x++)
1897             (void) ContrastImage(preview_image,MagickTrue,exception);
1898         (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
1899           (double) i+1);
1900         break;
1901       }
1902       case DullPreview:
1903       {
1904         preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1905         if (preview_image == (Image *) NULL)
1906           break;
1907         for (x=0; x < i; x++)
1908           (void) ContrastImage(preview_image,MagickFalse,exception);
1909         (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
1910           (double) i+1);
1911         break;
1912       }
1913       case GrayscalePreview:
1914       {
1915         preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1916         if (preview_image == (Image *) NULL)
1917           break;
1918         colors<<=1;
1919         quantize_info.number_colors=colors;
1920         quantize_info.colorspace=GRAYColorspace;
1921         (void) QuantizeImage(&quantize_info,preview_image,exception);
1922         (void) FormatLocaleString(label,MaxTextExtent,
1923           "-colorspace gray -colors %.20g",(double) colors);
1924         break;
1925       }
1926       case QuantizePreview:
1927       {
1928         preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1929         if (preview_image == (Image *) NULL)
1930           break;
1931         colors<<=1;
1932         quantize_info.number_colors=colors;
1933         (void) QuantizeImage(&quantize_info,preview_image,exception);
1934         (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
1935           colors);
1936         break;
1937       }
1938       case DespecklePreview:
1939       {
1940         for (x=0; x < (i-1); x++)
1941         {
1942           preview_image=DespeckleImage(thumbnail,exception);
1943           if (preview_image == (Image *) NULL)
1944             break;
1945           thumbnail=DestroyImage(thumbnail);
1946           thumbnail=preview_image;
1947         }
1948         preview_image=DespeckleImage(thumbnail,exception);
1949         if (preview_image == (Image *) NULL)
1950           break;
1951         (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
1952           (double) i+1);
1953         break;
1954       }
1955       case ReduceNoisePreview:
1956       {
1957         preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
1958           (size_t) radius,exception);
1959         (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
1960         break;
1961       }
1962       case AddNoisePreview:
1963       {
1964         switch ((int) i)
1965         {
1966           case 0:
1967           {
1968             (void) CopyMagickString(factor,"uniform",MaxTextExtent);
1969             break;
1970           }
1971           case 1:
1972           {
1973             (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
1974             break;
1975           }
1976           case 2:
1977           {
1978             (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
1979             break;
1980           }
1981           case 3:
1982           {
1983             (void) CopyMagickString(factor,"impulse",MaxTextExtent);
1984             break;
1985           }
1986           case 4:
1987           {
1988             (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
1989             break;
1990           }
1991           case 5:
1992           {
1993             (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
1994             break;
1995           }
1996           default:
1997           {
1998             (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
1999             break;
2000           }
2001         }
2002         preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2003           (size_t) i,exception);
2004         (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
2005         break;
2006       }
2007       case SharpenPreview:
2008       {
2009         preview_image=SharpenImage(thumbnail,radius,sigma,exception);
2010         (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",radius,
2011           sigma);
2012         break;
2013       }
2014       case BlurPreview:
2015       {
2016         preview_image=BlurImage(thumbnail,radius,sigma,exception);
2017         (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
2018           sigma);
2019         break;
2020       }
2021       case ThresholdPreview:
2022       {
2023         preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2024         if (preview_image == (Image *) NULL)
2025           break;
2026         (void) BilevelImage(thumbnail,(double) (percentage*((double)
2027           QuantumRange+1.0))/100.0,exception);
2028         (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",(double)
2029           (percentage*((double) QuantumRange+1.0))/100.0);
2030         break;
2031       }
2032       case EdgeDetectPreview:
2033       {
2034         preview_image=EdgeImage(thumbnail,radius,sigma,exception);
2035         (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
2036         break;
2037       }
2038       case SpreadPreview:
2039       {
2040         preview_image=SpreadImage(thumbnail,radius,thumbnail->interpolate,
2041           exception);
2042         (void) FormatLocaleString(label,MaxTextExtent,"spread %g",radius+0.5);
2043         break;
2044       }
2045       case SolarizePreview:
2046       {
2047         preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2048         if (preview_image == (Image *) NULL)
2049           break;
2050         (void) SolarizeImage(preview_image,(double) QuantumRange*percentage/
2051           100.0,exception);
2052         (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
2053           (QuantumRange*percentage)/100.0);
2054         break;
2055       }
2056       case ShadePreview:
2057       {
2058         degrees+=10.0;
2059         preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2060           exception);
2061         (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",degrees,
2062           degrees);
2063         break;
2064       }
2065       case RaisePreview:
2066       {
2067         preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2068         if (preview_image == (Image *) NULL)
2069           break;
2070         geometry.width=(size_t) (2*i+2);
2071         geometry.height=(size_t) (2*i+2);
2072         geometry.x=(i-1)/2;
2073         geometry.y=(i-1)/2;
2074         (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
2075         (void) FormatLocaleString(label,MaxTextExtent,
2076           "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
2077           geometry.height,(double) geometry.x,(double) geometry.y);
2078         break;
2079       }
2080       case SegmentPreview:
2081       {
2082         preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2083         if (preview_image == (Image *) NULL)
2084           break;
2085         threshold+=0.4f;
2086         (void) SegmentImage(preview_image,sRGBColorspace,MagickFalse,threshold,
2087           threshold,exception);
2088         (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
2089           threshold,threshold);
2090         break;
2091       }
2092       case SwirlPreview:
2093       {
2094         preview_image=SwirlImage(thumbnail,degrees,image->interpolate,
2095           exception);
2096         (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
2097         degrees+=45.0;
2098         break;
2099       }
2100       case ImplodePreview:
2101       {
2102         degrees+=0.1f;
2103         preview_image=ImplodeImage(thumbnail,degrees,image->interpolate,
2104           exception);
2105         (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
2106         break;
2107       }
2108       case WavePreview:
2109       {
2110         degrees+=5.0f;
2111         preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,
2112           image->interpolate,exception);
2113         (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",0.5*degrees,
2114           2.0*degrees);
2115         break;
2116       }
2117       case OilPaintPreview:
2118       {
2119         preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2120           exception);
2121         (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",radius,
2122           sigma);
2123         break;
2124       }
2125       case CharcoalDrawingPreview:
2126       {
2127         preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
2128           exception);
2129         (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",radius,
2130           sigma);
2131         break;
2132       }
2133       case JPEGPreview:
2134       {
2135         char
2136           filename[MaxTextExtent];
2137
2138         int
2139           file;
2140
2141         MagickBooleanType
2142           status;
2143
2144         preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2145         if (preview_image == (Image *) NULL)
2146           break;
2147         preview_info->quality=(size_t) percentage;
2148         (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
2149           preview_info->quality);
2150         file=AcquireUniqueFileResource(filename);
2151         if (file != -1)
2152           file=close(file)-1;
2153         (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
2154           "jpeg:%s",filename);
2155         status=WriteImage(preview_info,preview_image,exception);
2156         if (status != MagickFalse)
2157           {
2158             Image
2159               *quality_image;
2160
2161             (void) CopyMagickString(preview_info->filename,
2162               preview_image->filename,MaxTextExtent);
2163             quality_image=ReadImage(preview_info,exception);
2164             if (quality_image != (Image *) NULL)
2165               {
2166                 preview_image=DestroyImage(preview_image);
2167                 preview_image=quality_image;
2168               }
2169           }
2170         (void) RelinquishUniqueFileResource(preview_image->filename);
2171         if ((GetBlobSize(preview_image)/1024) >= 1024)
2172           (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
2173             factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2174             1024.0/1024.0);
2175         else
2176           if (GetBlobSize(preview_image) >= 1024)
2177             (void) FormatLocaleString(label,MaxTextExtent,
2178               "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
2179               GetBlobSize(preview_image))/1024.0);
2180           else
2181             (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
2182               factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
2183         break;
2184       }
2185     }
2186     thumbnail=DestroyImage(thumbnail);
2187     percentage+=12.5;
2188     radius+=0.5;
2189     sigma+=0.25;
2190     if (preview_image == (Image *) NULL)
2191       break;
2192     (void) DeleteImageProperty(preview_image,"label");
2193     (void) SetImageProperty(preview_image,"label",label,exception);
2194     AppendImageToList(&images,preview_image);
2195     proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2196       NumberTiles);
2197     if (proceed == MagickFalse)
2198       break;
2199   }
2200   if (images == (Image *) NULL)
2201     {
2202       preview_info=DestroyImageInfo(preview_info);
2203       return((Image *) NULL);
2204     }
2205   /*
2206     Create the montage.
2207   */
2208   montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2209   (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2210   montage_info->shadow=MagickTrue;
2211   (void) CloneString(&montage_info->tile,"3x3");
2212   (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2213   (void) CloneString(&montage_info->frame,DefaultTileFrame);
2214   montage_image=MontageImages(images,montage_info,exception);
2215   montage_info=DestroyMontageInfo(montage_info);
2216   images=DestroyImageList(images);
2217   if (montage_image == (Image *) NULL)
2218     ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2219   if (montage_image->montage != (char *) NULL)
2220     {
2221       /*
2222         Free image directory.
2223       */
2224       montage_image->montage=(char *) RelinquishMagickMemory(
2225         montage_image->montage);
2226       if (image->directory != (char *) NULL)
2227         montage_image->directory=(char *) RelinquishMagickMemory(
2228           montage_image->directory);
2229     }
2230   preview_info=DestroyImageInfo(preview_info);
2231   return(montage_image);
2232 }
2233 \f
2234 /*
2235 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2236 %                                                                             %
2237 %                                                                             %
2238 %                                                                             %
2239 %     R a d i a l B l u r I m a g e                                           %
2240 %                                                                             %
2241 %                                                                             %
2242 %                                                                             %
2243 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2244 %
2245 %  RadialBlurImage() applies a radial blur to the image.
2246 %
2247 %  Andrew Protano contributed this effect.
2248 %
2249 %  The format of the RadialBlurImage method is:
2250 %
2251 %    Image *RadialBlurImage(const Image *image,const double angle,
2252 %      ExceptionInfo *exception)
2253 %
2254 %  A description of each parameter follows:
2255 %
2256 %    o image: the image.
2257 %
2258 %    o angle: the angle of the radial blur.
2259 %
2260 %    o blur: the blur.
2261 %
2262 %    o exception: return any errors or warnings in this structure.
2263 %
2264 */
2265 MagickExport Image *RadialBlurImage(const Image *image,const double angle,
2266   ExceptionInfo *exception)
2267 {
2268   CacheView
2269     *blur_view,
2270     *image_view,
2271     *radial_view;
2272
2273   Image
2274     *blur_image;
2275
2276   MagickBooleanType
2277     status;
2278
2279   MagickOffsetType
2280     progress;
2281
2282   double
2283     blur_radius,
2284     *cos_theta,
2285     offset,
2286     *sin_theta,
2287     theta;
2288
2289   PointInfo
2290     blur_center;
2291
2292   register ssize_t
2293     i;
2294
2295   size_t
2296     n;
2297
2298   ssize_t
2299     y;
2300
2301   /*
2302     Allocate blur image.
2303   */
2304   assert(image != (Image *) NULL);
2305   assert(image->signature == MagickSignature);
2306   if (image->debug != MagickFalse)
2307     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2308   assert(exception != (ExceptionInfo *) NULL);
2309   assert(exception->signature == MagickSignature);
2310   blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2311   if (blur_image == (Image *) NULL)
2312     return((Image *) NULL);
2313   if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2314     {
2315       blur_image=DestroyImage(blur_image);
2316       return((Image *) NULL);
2317     }
2318   blur_center.x=(double) (image->columns-1)/2.0;
2319   blur_center.y=(double) (image->rows-1)/2.0;
2320   blur_radius=hypot(blur_center.x,blur_center.y);
2321   n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
2322   theta=DegreesToRadians(angle)/(double) (n-1);
2323   cos_theta=(double *) AcquireQuantumMemory((size_t) n,
2324     sizeof(*cos_theta));
2325   sin_theta=(double *) AcquireQuantumMemory((size_t) n,
2326     sizeof(*sin_theta));
2327   if ((cos_theta == (double *) NULL) ||
2328       (sin_theta == (double *) NULL))
2329     {
2330       blur_image=DestroyImage(blur_image);
2331       ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2332     }
2333   offset=theta*(double) (n-1)/2.0;
2334   for (i=0; i < (ssize_t) n; i++)
2335   {
2336     cos_theta[i]=cos((double) (theta*i-offset));
2337     sin_theta[i]=sin((double) (theta*i-offset));
2338   }
2339   /*
2340     Radial blur image.
2341   */
2342   status=MagickTrue;
2343   progress=0;
2344   image_view=AcquireVirtualCacheView(image,exception);
2345   radial_view=AcquireVirtualCacheView(image,exception);
2346   blur_view=AcquireAuthenticCacheView(blur_image,exception);
2347 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2348   #pragma omp parallel for schedule(static,4) shared(progress,status) \
2349     magick_threads(image,blur_image,image->rows,1)
2350 #endif
2351   for (y=0; y < (ssize_t) image->rows; y++)
2352   {
2353     register const Quantum
2354       *restrict p;
2355
2356     register Quantum
2357       *restrict q;
2358
2359     register ssize_t
2360       x;
2361
2362     if (status == MagickFalse)
2363       continue;
2364     p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2365     q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2366       exception);
2367     if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2368       {
2369         status=MagickFalse;
2370         continue;
2371       }
2372     for (x=0; x < (ssize_t) image->columns; x++)
2373     {
2374       double
2375         radius;
2376
2377       PointInfo
2378         center;
2379
2380       register ssize_t
2381         i;
2382
2383       size_t
2384         step;
2385
2386       center.x=(double) x-blur_center.x;
2387       center.y=(double) y-blur_center.y;
2388       radius=hypot((double) center.x,center.y);
2389       if (radius == 0)
2390         step=1;
2391       else
2392         {
2393           step=(size_t) (blur_radius/radius);
2394           if (step == 0)
2395             step=1;
2396           else
2397             if (step >= n)
2398               step=n-1;
2399         }
2400       for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2401       {
2402         double
2403           gamma,
2404           pixel;
2405
2406         PixelChannel
2407           channel;
2408
2409         PixelTrait
2410           blur_traits,
2411           traits;
2412
2413         register const Quantum
2414           *restrict r;
2415
2416         register ssize_t
2417           j;
2418
2419         channel=GetPixelChannelChannel(image,i);
2420         traits=GetPixelChannelTraits(image,channel);
2421         blur_traits=GetPixelChannelTraits(blur_image,channel);
2422         if ((traits == UndefinedPixelTrait) ||
2423             (blur_traits == UndefinedPixelTrait))
2424           continue;
2425         if (((blur_traits & CopyPixelTrait) != 0) ||
2426             (GetPixelMask(image,p) == 0))
2427           {
2428             SetPixelChannel(blur_image,channel,p[i],q);
2429             continue;
2430           }
2431         gamma=0.0;
2432         pixel=0.0;
2433         if ((blur_traits & BlendPixelTrait) == 0)
2434           {
2435             for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2436             {
2437               r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2438                 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2439                 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2440                 1,1,exception);
2441               if (r == (const Quantum *) NULL)
2442                 {
2443                   status=MagickFalse;
2444                   continue;
2445                 }
2446               pixel+=r[i];
2447               gamma++;
2448             }
2449             gamma=PerceptibleReciprocal(gamma);
2450             SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2451             continue;
2452           }
2453         for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2454         {
2455           r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2456             center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2457             (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2458             1,1,exception);
2459           if (r == (const Quantum *) NULL)
2460             {
2461               status=MagickFalse;
2462               continue;
2463             }
2464           pixel+=GetPixelAlpha(image,r)*r[i];
2465           gamma+=GetPixelAlpha(image,r);
2466         }
2467         gamma=PerceptibleReciprocal(gamma);
2468         SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2469       }
2470       p+=GetPixelChannels(image);
2471       q+=GetPixelChannels(blur_image);
2472     }
2473     if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2474       status=MagickFalse;
2475     if (image->progress_monitor != (MagickProgressMonitor) NULL)
2476       {
2477         MagickBooleanType
2478           proceed;
2479
2480 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2481         #pragma omp critical (MagickCore_RadialBlurImage)
2482 #endif
2483         proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2484         if (proceed == MagickFalse)
2485           status=MagickFalse;
2486       }
2487   }
2488   blur_view=DestroyCacheView(blur_view);
2489   radial_view=DestroyCacheView(radial_view);
2490   image_view=DestroyCacheView(image_view);
2491   cos_theta=(double *) RelinquishMagickMemory(cos_theta);
2492   sin_theta=(double *) RelinquishMagickMemory(sin_theta);
2493   if (status == MagickFalse)
2494     blur_image=DestroyImage(blur_image);
2495   return(blur_image);
2496 }
2497 \f
2498 /*
2499 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2500 %                                                                             %
2501 %                                                                             %
2502 %                                                                             %
2503 %     S e l e c t i v e B l u r I m a g e                                     %
2504 %                                                                             %
2505 %                                                                             %
2506 %                                                                             %
2507 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2508 %
2509 %  SelectiveBlurImage() selectively blur pixels within a contrast threshold.
2510 %  It is similar to the unsharpen mask that sharpens everything with contrast
2511 %  above a certain threshold.
2512 %
2513 %  The format of the SelectiveBlurImage method is:
2514 %
2515 %      Image *SelectiveBlurImage(const Image *image,const double radius,
2516 %        const double sigma,const double threshold,ExceptionInfo *exception)
2517 %
2518 %  A description of each parameter follows:
2519 %
2520 %    o image: the image.
2521 %
2522 %    o radius: the radius of the Gaussian, in pixels, not counting the center
2523 %      pixel.
2524 %
2525 %    o sigma: the standard deviation of the Gaussian, in pixels.
2526 %
2527 %    o threshold: only pixels within this contrast threshold are included
2528 %      in the blur operation.
2529 %
2530 %    o exception: return any errors or warnings in this structure.
2531 %
2532 */
2533 MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
2534   const double sigma,const double threshold,ExceptionInfo *exception)
2535 {
2536 #define SelectiveBlurImageTag  "SelectiveBlur/Image"
2537
2538   CacheView
2539     *blur_view,
2540     *image_view,
2541     *luminance_view;
2542
2543   Image
2544     *blur_image,
2545     *luminance_image;
2546
2547   MagickBooleanType
2548     status;
2549
2550   MagickOffsetType
2551     progress;
2552
2553   MagickRealType
2554     *kernel;
2555
2556   register ssize_t
2557     i;
2558
2559   size_t
2560     width;
2561
2562   ssize_t
2563     center,
2564     j,
2565     u,
2566     v,
2567     y;
2568
2569   /*
2570     Initialize blur image attributes.
2571   */
2572   assert(image != (Image *) NULL);
2573   assert(image->signature == MagickSignature);
2574   if (image->debug != MagickFalse)
2575     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2576   assert(exception != (ExceptionInfo *) NULL);
2577   assert(exception->signature == MagickSignature);
2578   width=GetOptimalKernelWidth1D(radius,sigma);
2579   kernel=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory((size_t)
2580     width,width*sizeof(*kernel)));
2581   if (kernel == (MagickRealType *) NULL)
2582     ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2583   j=(ssize_t) (width-1)/2;
2584   i=0;
2585   for (v=(-j); v <= j; v++)
2586   {
2587     for (u=(-j); u <= j; u++)
2588       kernel[i++]=(MagickRealType) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
2589         MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
2590   }
2591   if (image->debug != MagickFalse)
2592     {
2593       char
2594         format[MaxTextExtent],
2595         *message;
2596
2597       register const MagickRealType
2598         *k;
2599
2600       ssize_t
2601         u,
2602         v;
2603
2604       (void) LogMagickEvent(TransformEvent,GetMagickModule(),
2605         "  SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
2606         width);
2607       message=AcquireString("");
2608       k=kernel;
2609       for (v=0; v < (ssize_t) width; v++)
2610       {
2611         *message='\0';
2612         (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
2613         (void) ConcatenateString(&message,format);
2614         for (u=0; u < (ssize_t) width; u++)
2615         {
2616           (void) FormatLocaleString(format,MaxTextExtent,"%+f ",(double) *k++);
2617           (void) ConcatenateString(&message,format);
2618         }
2619         (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
2620       }
2621       message=DestroyString(message);
2622     }
2623   blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2624   if (blur_image == (Image *) NULL)
2625     return((Image *) NULL);
2626   if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2627     {
2628       blur_image=DestroyImage(blur_image);
2629       kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2630       return((Image *) NULL);
2631     }
2632   luminance_image=CloneImage(image,0,0,MagickTrue,exception);
2633   if (luminance_image == (Image *) NULL)
2634     {
2635       blur_image=DestroyImage(blur_image);
2636       kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2637       return((Image *) NULL);
2638     }
2639   status=TransformImageColorspace(luminance_image,GRAYColorspace,exception);
2640   if (status == MagickFalse)
2641     {
2642       luminance_image=DestroyImage(luminance_image);
2643       blur_image=DestroyImage(blur_image);
2644       kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2645       return((Image *) NULL);
2646     }
2647   /*
2648     Threshold blur image.
2649   */
2650   status=MagickTrue;
2651   progress=0;
2652   center=(ssize_t) (GetPixelChannels(image)*(image->columns+width)*
2653     ((width-1)/2L)+GetPixelChannels(image)*((width-1)/2L));
2654   image_view=AcquireVirtualCacheView(image,exception);
2655   luminance_view=AcquireVirtualCacheView(luminance_image,exception);
2656   blur_view=AcquireAuthenticCacheView(blur_image,exception);
2657 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2658   #pragma omp parallel for schedule(static,4) shared(progress,status) \
2659     magick_threads(image,blur_image,image->rows,1)
2660 #endif
2661   for (y=0; y < (ssize_t) image->rows; y++)
2662   {
2663     double
2664       contrast;
2665
2666     MagickBooleanType
2667       sync;
2668
2669     register const Quantum
2670       *restrict l,
2671       *restrict p;
2672
2673     register Quantum
2674       *restrict q;
2675
2676     register ssize_t
2677       x;
2678
2679     if (status == MagickFalse)
2680       continue;
2681     p=GetCacheViewVirtualPixels(image_view,-((ssize_t) (width-1)/2L),y-(ssize_t)
2682       ((width-1)/2L),image->columns+width,width,exception);
2683     l=GetCacheViewVirtualPixels(luminance_view,-((ssize_t) (width-1)/2L),y-
2684       (ssize_t) ((width-1)/2L),luminance_image->columns+width,width,exception);
2685     q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2686       exception);
2687     if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2688       {
2689         status=MagickFalse;
2690         continue;
2691       }
2692     for (x=0; x < (ssize_t) image->columns; x++)
2693     {
2694       double
2695         intensity;
2696
2697       register ssize_t
2698         i;
2699
2700       intensity=GetPixelIntensity(image,p+center);
2701       for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2702       {
2703         double
2704           alpha,
2705           gamma,
2706           pixel;
2707
2708         PixelChannel
2709           channel;
2710
2711         PixelTrait
2712           blur_traits,
2713           traits;
2714
2715         register const MagickRealType
2716           *restrict k;
2717
2718         register const Quantum
2719           *restrict luminance_pixels,
2720           *restrict pixels;
2721
2722         register ssize_t
2723           u;
2724
2725         ssize_t
2726           v;
2727
2728         channel=GetPixelChannelChannel(image,i);
2729         traits=GetPixelChannelTraits(image,channel);
2730         blur_traits=GetPixelChannelTraits(blur_image,channel);
2731         if ((traits == UndefinedPixelTrait) ||
2732             (blur_traits == UndefinedPixelTrait))
2733           continue;
2734         if (((blur_traits & CopyPixelTrait) != 0) ||
2735             (GetPixelMask(image,p+center) == 0))
2736           {
2737             SetPixelChannel(blur_image,channel,p[center+i],q);
2738             continue;
2739           }
2740         k=kernel;
2741         pixel=0.0;
2742         pixels=p;
2743         luminance_pixels=l;
2744         gamma=0.0;
2745         if ((blur_traits & BlendPixelTrait) == 0)
2746           {
2747             for (v=0; v < (ssize_t) width; v++)
2748             {
2749               for (u=0; u < (ssize_t) width; u++)
2750               {
2751                 contrast=GetPixelIntensity(luminance_image,luminance_pixels)-
2752                   intensity;
2753                 if (fabs(contrast) < threshold)
2754                   {
2755                     pixel+=(*k)*pixels[i];
2756                     gamma+=(*k);
2757                   }
2758                 k++;
2759                 pixels+=GetPixelChannels(image);
2760                 luminance_pixels+=GetPixelChannels(luminance_image);
2761               }
2762               pixels+=(image->columns-1)*GetPixelChannels(image);
2763               luminance_pixels+=luminance_image->columns*
2764                 GetPixelChannels(luminance_image);
2765             }
2766             if (fabs((double) gamma) < MagickEpsilon)
2767               {
2768                 SetPixelChannel(blur_image,channel,p[center+i],q);
2769                 continue;
2770               }
2771             gamma=PerceptibleReciprocal(gamma);
2772             SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2773             continue;
2774           }
2775         for (v=0; v < (ssize_t) width; v++)
2776         {
2777           for (u=0; u < (ssize_t) width; u++)
2778           {
2779             contrast=GetPixelIntensity(image,pixels)-intensity;
2780             if (fabs(contrast) < threshold)
2781               {
2782                 alpha=(double) (QuantumScale*GetPixelAlpha(image,pixels));
2783                 pixel+=(*k)*alpha*pixels[i];
2784                 gamma+=(*k)*alpha;
2785               }
2786             k++;
2787             pixels+=GetPixelChannels(image);
2788             luminance_pixels+=GetPixelChannels(luminance_image);
2789           }
2790           pixels+=(image->columns-1)*GetPixelChannels(image);
2791           luminance_pixels+=luminance_image->columns*
2792             GetPixelChannels(luminance_image);
2793         }
2794         if (fabs((double) gamma) < MagickEpsilon)
2795           {
2796             SetPixelChannel(blur_image,channel,p[center+i],q);
2797             continue;
2798           }
2799         gamma=PerceptibleReciprocal(gamma);
2800         SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2801       }
2802       p+=GetPixelChannels(image);
2803       l+=GetPixelChannels(luminance_image);
2804       q+=GetPixelChannels(blur_image);
2805     }
2806     sync=SyncCacheViewAuthenticPixels(blur_view,exception);
2807     if (sync == MagickFalse)
2808       status=MagickFalse;
2809     if (image->progress_monitor != (MagickProgressMonitor) NULL)
2810       {
2811         MagickBooleanType
2812           proceed;
2813
2814 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2815         #pragma omp critical (MagickCore_SelectiveBlurImage)
2816 #endif
2817         proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
2818           image->rows);
2819         if (proceed == MagickFalse)
2820           status=MagickFalse;
2821       }
2822   }
2823   blur_image->type=image->type;
2824   blur_view=DestroyCacheView(blur_view);
2825   image_view=DestroyCacheView(image_view);
2826   luminance_image=DestroyImage(luminance_image);
2827   kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2828   if (status == MagickFalse)
2829     blur_image=DestroyImage(blur_image);
2830   return(blur_image);
2831 }
2832 \f
2833 /*
2834 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2835 %                                                                             %
2836 %                                                                             %
2837 %                                                                             %
2838 %     S h a d e I m a g e                                                     %
2839 %                                                                             %
2840 %                                                                             %
2841 %                                                                             %
2842 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2843 %
2844 %  ShadeImage() shines a distant light on an image to create a
2845 %  three-dimensional effect. You control the positioning of the light with
2846 %  azimuth and elevation; azimuth is measured in degrees off the x axis
2847 %  and elevation is measured in pixels above the Z axis.
2848 %
2849 %  The format of the ShadeImage method is:
2850 %
2851 %      Image *ShadeImage(const Image *image,const MagickBooleanType gray,
2852 %        const double azimuth,const double elevation,ExceptionInfo *exception)
2853 %
2854 %  A description of each parameter follows:
2855 %
2856 %    o image: the image.
2857 %
2858 %    o gray: A value other than zero shades the intensity of each pixel.
2859 %
2860 %    o azimuth, elevation:  Define the light source direction.
2861 %
2862 %    o exception: return any errors or warnings in this structure.
2863 %
2864 */
2865 MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
2866   const double azimuth,const double elevation,ExceptionInfo *exception)
2867 {
2868 #define ShadeImageTag  "Shade/Image"
2869
2870   CacheView
2871     *image_view,
2872     *shade_view;
2873
2874   Image
2875     *linear_image,
2876     *shade_image;
2877
2878   MagickBooleanType
2879     status;
2880
2881   MagickOffsetType
2882     progress;
2883
2884   PrimaryInfo
2885     light;
2886
2887   ssize_t
2888     y;
2889
2890   /*
2891     Initialize shaded image attributes.
2892   */
2893   assert(image != (const Image *) NULL);
2894   assert(image->signature == MagickSignature);
2895   if (image->debug != MagickFalse)
2896     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2897   assert(exception != (ExceptionInfo *) NULL);
2898   assert(exception->signature == MagickSignature);
2899   linear_image=CloneImage(image,0,0,MagickTrue,exception);
2900   shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2901   if ((linear_image == (Image *) NULL) || (shade_image == (Image *) NULL))
2902     {
2903       if (linear_image != (Image *) NULL)
2904         linear_image=DestroyImage(linear_image);
2905       if (shade_image != (Image *) NULL)
2906         shade_image=DestroyImage(shade_image);
2907       return((Image *) NULL);
2908     }
2909   if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
2910     {
2911       linear_image=DestroyImage(linear_image);
2912       shade_image=DestroyImage(shade_image);
2913       return((Image *) NULL);
2914     }
2915   /*
2916     Compute the light vector.
2917   */
2918   light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
2919     cos(DegreesToRadians(elevation));
2920   light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
2921     cos(DegreesToRadians(elevation));
2922   light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
2923   /*
2924     Shade image.
2925   */
2926   status=MagickTrue;
2927   progress=0;
2928   image_view=AcquireVirtualCacheView(linear_image,exception);
2929   shade_view=AcquireAuthenticCacheView(shade_image,exception);
2930 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2931   #pragma omp parallel for schedule(static,4) shared(progress,status) \
2932     magick_threads(linear_image,shade_image,linear_image->rows,1)
2933 #endif
2934   for (y=0; y < (ssize_t) linear_image->rows; y++)
2935   {
2936     double
2937       distance,
2938       normal_distance,
2939       shade;
2940
2941     PrimaryInfo
2942       normal;
2943
2944     register const Quantum
2945       *restrict center,
2946       *restrict p,
2947       *restrict post,
2948       *restrict pre;
2949
2950     register Quantum
2951       *restrict q;
2952
2953     register ssize_t
2954       x;
2955
2956     if (status == MagickFalse)
2957       continue;
2958     p=GetCacheViewVirtualPixels(image_view,-1,y-1,linear_image->columns+2,3,
2959       exception);
2960     q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
2961       exception);
2962     if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2963       {
2964         status=MagickFalse;
2965         continue;
2966       }
2967     /*
2968       Shade this row of pixels.
2969     */
2970     normal.z=2.0*(double) QuantumRange;  /* constant Z of surface normal */
2971     pre=p+GetPixelChannels(linear_image);
2972     center=pre+(linear_image->columns+2)*GetPixelChannels(linear_image);
2973     post=center+(linear_image->columns+2)*GetPixelChannels(linear_image);
2974     for (x=0; x < (ssize_t) linear_image->columns; x++)
2975     {
2976       register ssize_t
2977         i;
2978
2979       /*
2980         Determine the surface normal and compute shading.
2981       */
2982       normal.x=(double) (
2983         GetPixelIntensity(linear_image,pre-GetPixelChannels(linear_image))+
2984         GetPixelIntensity(linear_image,center-GetPixelChannels(linear_image))+
2985         GetPixelIntensity(linear_image,post-GetPixelChannels(linear_image))-
2986         GetPixelIntensity(linear_image,pre+GetPixelChannels(linear_image))-
2987         GetPixelIntensity(linear_image,center+GetPixelChannels(linear_image))-
2988         GetPixelIntensity(linear_image,post+GetPixelChannels(linear_image)));
2989       normal.y=(double) (
2990         GetPixelIntensity(linear_image,post-GetPixelChannels(linear_image))+
2991         GetPixelIntensity(linear_image,post)+
2992         GetPixelIntensity(linear_image,post+GetPixelChannels(linear_image))-
2993         GetPixelIntensity(linear_image,pre-GetPixelChannels(linear_image))-
2994         GetPixelIntensity(linear_image,pre)-
2995         GetPixelIntensity(linear_image,pre+GetPixelChannels(linear_image)));
2996       if ((normal.x == 0.0) && (normal.y == 0.0))
2997         shade=light.z;
2998       else
2999         {
3000           shade=0.0;
3001           distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3002           if (distance > MagickEpsilon)
3003             {
3004               normal_distance=normal.x*normal.x+normal.y*normal.y+
3005                 normal.z*normal.z;
3006               if (normal_distance > (MagickEpsilon*MagickEpsilon))
3007                 shade=distance/sqrt((double) normal_distance);
3008             }
3009         }
3010       for (i=0; i < (ssize_t) GetPixelChannels(linear_image); i++)
3011       {
3012         PixelChannel
3013           channel;
3014
3015         PixelTrait
3016           shade_traits,
3017           traits;
3018
3019         channel=GetPixelChannelChannel(linear_image,i);
3020         traits=GetPixelChannelTraits(linear_image,channel);
3021         shade_traits=GetPixelChannelTraits(shade_image,channel);
3022         if ((traits == UndefinedPixelTrait) ||
3023             (shade_traits == UndefinedPixelTrait))
3024           continue;
3025         if (((shade_traits & CopyPixelTrait) != 0) ||
3026             (GetPixelMask(linear_image,center) == 0))
3027           {
3028             SetPixelChannel(shade_image,channel,center[i],q);
3029             continue;
3030           }
3031         if (gray != MagickFalse)
3032           {
3033             SetPixelChannel(shade_image,channel,ClampToQuantum(shade),q);
3034             continue;
3035           }
3036         SetPixelChannel(shade_image,channel,ClampToQuantum(QuantumScale*shade*
3037           center[i]),q);
3038       }
3039       pre+=GetPixelChannels(linear_image);
3040       center+=GetPixelChannels(linear_image);
3041       post+=GetPixelChannels(linear_image);
3042       q+=GetPixelChannels(shade_image);
3043     }
3044     if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3045       status=MagickFalse;
3046     if (image->progress_monitor != (MagickProgressMonitor) NULL)
3047       {
3048         MagickBooleanType
3049           proceed;
3050
3051 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3052         #pragma omp critical (MagickCore_ShadeImage)
3053 #endif
3054         proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3055         if (proceed == MagickFalse)
3056           status=MagickFalse;
3057       }
3058   }
3059   shade_view=DestroyCacheView(shade_view);
3060   image_view=DestroyCacheView(image_view);
3061   linear_image=DestroyImage(linear_image);
3062   if (status == MagickFalse)
3063     shade_image=DestroyImage(shade_image);
3064   return(shade_image);
3065 }
3066 \f
3067 /*
3068 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3069 %                                                                             %
3070 %                                                                             %
3071 %                                                                             %
3072 %     S h a r p e n I m a g e                                                 %
3073 %                                                                             %
3074 %                                                                             %
3075 %                                                                             %
3076 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3077 %
3078 %  SharpenImage() sharpens the image.  We convolve the image with a Gaussian
3079 %  operator of the given radius and standard deviation (sigma).  For
3080 %  reasonable results, radius should be larger than sigma.  Use a radius of 0
3081 %  and SharpenImage() selects a suitable radius for you.
3082 %
3083 %  Using a separable kernel would be faster, but the negative weights cancel
3084 %  out on the corners of the kernel producing often undesirable ringing in the
3085 %  filtered result; this can be avoided by using a 2D gaussian shaped image
3086 %  sharpening kernel instead.
3087 %
3088 %  The format of the SharpenImage method is:
3089 %
3090 %    Image *SharpenImage(const Image *image,const double radius,
3091 %      const double sigma,ExceptionInfo *exception)
3092 %
3093 %  A description of each parameter follows:
3094 %
3095 %    o image: the image.
3096 %
3097 %    o radius: the radius of the Gaussian, in pixels, not counting the center
3098 %      pixel.
3099 %
3100 %    o sigma: the standard deviation of the Laplacian, in pixels.
3101 %
3102 %    o exception: return any errors or warnings in this structure.
3103 %
3104 */
3105 MagickExport Image *SharpenImage(const Image *image,const double radius,
3106   const double sigma,ExceptionInfo *exception)
3107 {
3108   char
3109     geometry[MaxTextExtent];
3110
3111   KernelInfo
3112     *kernel_info;
3113
3114   Image
3115     *sharpen_image;
3116
3117   assert(image != (const Image *) NULL);
3118   assert(image->signature == MagickSignature);
3119   if (image->debug != MagickFalse)
3120     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3121   assert(exception != (ExceptionInfo *) NULL);
3122   assert(exception->signature == MagickSignature);
3123   (void) FormatLocaleString(geometry,MaxTextExtent,"LoG:%.20gx%.20g",
3124     radius,sigma);
3125   kernel_info=AcquireKernelInfo(geometry);
3126   if (kernel_info == (KernelInfo *) NULL)
3127     ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3128   ScaleGeometryKernelInfo(kernel_info,"!,100%");
3129   sharpen_image=MorphologyImage(image,ConvolveMorphology,1,kernel_info,
3130     exception);
3131   kernel_info=DestroyKernelInfo(kernel_info);
3132   return(sharpen_image);
3133 }
3134 \f
3135 /*
3136 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3137 %                                                                             %
3138 %                                                                             %
3139 %                                                                             %
3140 %     S p r e a d I m a g e                                                   %
3141 %                                                                             %
3142 %                                                                             %
3143 %                                                                             %
3144 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3145 %
3146 %  SpreadImage() is a special effects method that randomly displaces each
3147 %  pixel in a block defined by the radius parameter.
3148 %
3149 %  The format of the SpreadImage method is:
3150 %
3151 %      Image *SpreadImage(const Image *image,const double radius,
3152 %        const PixelInterpolateMethod method,ExceptionInfo *exception)
3153 %
3154 %  A description of each parameter follows:
3155 %
3156 %    o image: the image.
3157 %
3158 %    o radius:  choose a random pixel in a neighborhood of this extent.
3159 %
3160 %    o method:  the pixel interpolation method.
3161 %
3162 %    o exception: return any errors or warnings in this structure.
3163 %
3164 */
3165 MagickExport Image *SpreadImage(const Image *image,const double radius,
3166   const PixelInterpolateMethod method,ExceptionInfo *exception)
3167 {
3168 #define SpreadImageTag  "Spread/Image"
3169
3170   CacheView
3171     *image_view,
3172     *spread_view;
3173
3174   Image
3175     *spread_image;
3176
3177   MagickBooleanType
3178     status;
3179
3180   MagickOffsetType
3181     progress;
3182
3183   RandomInfo
3184     **restrict random_info;
3185
3186   size_t
3187     width;
3188
3189   ssize_t
3190     y;
3191
3192 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3193   unsigned long
3194     key;
3195 #endif
3196
3197   /*
3198     Initialize spread image attributes.
3199   */
3200   assert(image != (Image *) NULL);
3201   assert(image->signature == MagickSignature);
3202   if (image->debug != MagickFalse)
3203     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3204   assert(exception != (ExceptionInfo *) NULL);
3205   assert(exception->signature == MagickSignature);
3206   spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3207     exception);
3208   if (spread_image == (Image *) NULL)
3209     return((Image *) NULL);
3210   if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
3211     {
3212       spread_image=DestroyImage(spread_image);
3213       return((Image *) NULL);
3214     }
3215   /*
3216     Spread image.
3217   */
3218   status=MagickTrue;
3219   progress=0;
3220   width=GetOptimalKernelWidth1D(radius,0.5);
3221   random_info=AcquireRandomInfoThreadSet();
3222   image_view=AcquireVirtualCacheView(image,exception);
3223   spread_view=AcquireAuthenticCacheView(spread_image,exception);
3224 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3225   key=GetRandomSecretKey(random_info[0]);
3226   #pragma omp parallel for schedule(static,4) shared(progress,status) \
3227     magick_threads(image,spread_image,image->rows,key == ~0UL)
3228 #endif
3229   for (y=0; y < (ssize_t) image->rows; y++)
3230   {
3231     const int
3232       id = GetOpenMPThreadId();
3233
3234     register const Quantum
3235       *restrict p;
3236
3237     register Quantum
3238       *restrict q;
3239
3240     register ssize_t
3241       x;
3242
3243     if (status == MagickFalse)
3244       continue;
3245     p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3246     q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
3247       exception);
3248     if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3249       {
3250         status=MagickFalse;
3251         continue;
3252       }
3253     for (x=0; x < (ssize_t) image->columns; x++)
3254     {
3255       PointInfo
3256         point;
3257
3258       point.x=GetPseudoRandomValue(random_info[id]);
3259       point.y=GetPseudoRandomValue(random_info[id]);
3260       status=InterpolatePixelChannels(image,image_view,spread_image,method,
3261         (double) x+width*point.x-0.5,(double) y+width*point.y-0.5,q,exception);
3262       q+=GetPixelChannels(spread_image);
3263     }
3264     if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
3265       status=MagickFalse;
3266     if (image->progress_monitor != (MagickProgressMonitor) NULL)
3267       {
3268         MagickBooleanType
3269           proceed;
3270
3271 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3272         #pragma omp critical (MagickCore_SpreadImage)
3273 #endif
3274         proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3275         if (proceed == MagickFalse)
3276           status=MagickFalse;
3277       }
3278   }
3279   spread_view=DestroyCacheView(spread_view);
3280   image_view=DestroyCacheView(image_view);
3281   random_info=DestroyRandomInfoThreadSet(random_info);
3282   if (status == MagickFalse)
3283     spread_image=DestroyImage(spread_image);
3284   return(spread_image);
3285 }
3286 \f
3287 /*
3288 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3289 %                                                                             %
3290 %                                                                             %
3291 %                                                                             %
3292 %     U n s h a r p M a s k I m a g e                                         %
3293 %                                                                             %
3294 %                                                                             %
3295 %                                                                             %
3296 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3297 %
3298 %  UnsharpMaskImage() sharpens one or more image channels.  We convolve the
3299 %  image with a Gaussian operator of the given radius and standard deviation
3300 %  (sigma).  For reasonable results, radius should be larger than sigma.  Use a
3301 %  radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
3302 %
3303 %  The format of the UnsharpMaskImage method is:
3304 %
3305 %    Image *UnsharpMaskImage(const Image *image,const double radius,
3306 %      const double sigma,const double gain,ExceptionInfo *exception)
3307 %
3308 %  A description of each parameter follows:
3309 %
3310 %    o image: the image.
3311 %
3312 %    o radius: the radius of the Gaussian, in pixels, not counting the center
3313 %      pixel.
3314 %
3315 %    o sigma: the standard deviation of the Gaussian, in pixels.
3316 %
3317 %    o gain: the percentage of the difference between the original and the
3318 %      blur image that is added back into the original.
3319 %
3320 %    o exception: return any errors or warnings in this structure.
3321 %
3322 */
3323 MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
3324   const double sigma,const double gain,ExceptionInfo *exception)
3325 {
3326   char
3327     geometry[MaxTextExtent];
3328
3329   KernelInfo
3330     *kernel_info;
3331
3332   Image
3333     *unsharp_image;
3334
3335   assert(image != (const Image *) NULL);
3336   assert(image->signature == MagickSignature);
3337   if (image->debug != MagickFalse)
3338     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3339   assert(exception != (ExceptionInfo *) NULL);
3340   assert(exception->signature == MagickSignature);
3341   (void) FormatLocaleString(geometry,MaxTextExtent,"Blur:%.20gx%.20g>",
3342     radius,sigma);
3343   kernel_info=AcquireKernelInfo(geometry);
3344   if (kernel_info == (KernelInfo *) NULL)
3345     ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3346   (void) FormatLocaleString(geometry,MaxTextExtent,"%.20g,%.20g%%",
3347     -100.0+gain*100.0,200.0-gain*100.0);
3348   ScaleGeometryKernelInfo(kernel_info,geometry);
3349   unsharp_image=MorphologyImage(image,ConvolveMorphology,1,kernel_info,
3350     exception);
3351   kernel_info=DestroyKernelInfo(kernel_info);
3352   return(unsharp_image);
3353 }