]> 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,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=BlurImage(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,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=BlurImage(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     *morphology_image;
833
834   assert(image != (const Image *) NULL);
835   assert(image->signature == MagickSignature);
836   if (image->debug != MagickFalse)
837     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
838   assert(exception != (ExceptionInfo *) NULL);
839   assert(exception->signature == MagickSignature);
840   (void) FormatLocaleString(geometry,MaxTextExtent,"blur:%.20gx%.20g",radius,
841     sigma);
842   kernel_info=AcquireKernelInfo(geometry);
843   if (kernel_info == (KernelInfo *) NULL)
844     ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
845   morphology_image=MorphologyImage(image,ConvolveMorphology,1,kernel_info,
846     exception);
847   kernel_info=DestroyKernelInfo(kernel_info);
848   if (morphology_image == (Image *) NULL)
849     return((Image *) NULL);
850   (void) FormatLocaleString(geometry,MaxTextExtent,"blur:%.20gx%.20g+90",radius,
851     sigma);
852   kernel_info=AcquireKernelInfo(geometry);
853   if (kernel_info == (KernelInfo *) NULL)
854     {
855       morphology_image=DestroyImage(morphology_image);
856       ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
857     }
858   blur_image=MorphologyImage(morphology_image,ConvolveMorphology,1,kernel_info,
859     exception);
860   morphology_image=DestroyImage(morphology_image);
861   kernel_info=DestroyKernelInfo(kernel_info);
862   return(blur_image);
863 }
864 \f
865 /*
866 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
867 %                                                                             %
868 %                                                                             %
869 %                                                                             %
870 %     C o n v o l v e I m a g e                                               %
871 %                                                                             %
872 %                                                                             %
873 %                                                                             %
874 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
875 %
876 %  ConvolveImage() applies a custom convolution kernel to the image.
877 %
878 %  The format of the ConvolveImage method is:
879 %
880 %      Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
881 %        ExceptionInfo *exception)
882 %
883 %  A description of each parameter follows:
884 %
885 %    o image: the image.
886 %
887 %    o kernel: the filtering kernel.
888 %
889 %    o exception: return any errors or warnings in this structure.
890 %
891 */
892 MagickExport Image *ConvolveImage(const Image *image,
893   const KernelInfo *kernel_info,ExceptionInfo *exception)
894 {
895   return(MorphologyImage(image,ConvolveMorphology,1,kernel_info,exception));
896 }
897 \f
898 /*
899 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
900 %                                                                             %
901 %                                                                             %
902 %                                                                             %
903 %     D e s p e c k l e I m a g e                                             %
904 %                                                                             %
905 %                                                                             %
906 %                                                                             %
907 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
908 %
909 %  DespeckleImage() reduces the speckle noise in an image while perserving the
910 %  edges of the original image.  A speckle removing filter uses a complementary %  hulling technique (raising pixels that are darker than their surrounding
911 %  neighbors, then complementarily lowering pixels that are brighter than their
912 %  surrounding neighbors) to reduce the speckle index of that image (reference
913 %  Crimmins speckle removal).
914 %
915 %  The format of the DespeckleImage method is:
916 %
917 %      Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
918 %
919 %  A description of each parameter follows:
920 %
921 %    o image: the image.
922 %
923 %    o exception: return any errors or warnings in this structure.
924 %
925 */
926
927 static void Hull(const Image *image,const ssize_t x_offset,
928   const ssize_t y_offset,const size_t columns,const size_t rows,
929   const int polarity,Quantum *restrict f,Quantum *restrict g)
930 {
931   register Quantum
932     *p,
933     *q,
934     *r,
935     *s;
936
937   ssize_t
938     y;
939
940   assert(f != (Quantum *) NULL);
941   assert(g != (Quantum *) NULL);
942   p=f+(columns+2);
943   q=g+(columns+2);
944   r=p+(y_offset*(columns+2)+x_offset);
945 #if defined(MAGICKCORE_OPENMP_SUPPORT)
946   #pragma omp parallel for schedule(static,4) \
947     magick_threads(image,image,1,1)
948 #endif
949   for (y=0; y < (ssize_t) rows; y++)
950   {
951     MagickRealType
952       v;
953
954     register ssize_t
955       i,
956       x;
957
958     i=(2*y+1)+y*columns;
959     if (polarity > 0)
960       for (x=0; x < (ssize_t) columns; x++)
961       {
962         v=(MagickRealType) p[i];
963         if ((MagickRealType) r[i] >= (v+ScaleCharToQuantum(2)))
964           v+=ScaleCharToQuantum(1);
965         q[i]=(Quantum) v;
966         i++;
967       }
968     else
969       for (x=0; x < (ssize_t) columns; x++)
970       {
971         v=(MagickRealType) p[i];
972         if ((MagickRealType) r[i] <= (v-ScaleCharToQuantum(2)))
973           v-=ScaleCharToQuantum(1);
974         q[i]=(Quantum) v;
975         i++;
976       }
977   }
978   p=f+(columns+2);
979   q=g+(columns+2);
980   r=q+(y_offset*(columns+2)+x_offset);
981   s=q-(y_offset*(columns+2)+x_offset);
982 #if defined(MAGICKCORE_OPENMP_SUPPORT)
983   #pragma omp parallel for schedule(static,4) \
984     magick_threads(image,image,1,1)
985 #endif
986   for (y=0; y < (ssize_t) rows; y++)
987   {
988     register ssize_t
989       i,
990       x;
991
992     MagickRealType
993       v;
994
995     i=(2*y+1)+y*columns;
996     if (polarity > 0)
997       for (x=0; x < (ssize_t) columns; x++)
998       {
999         v=(MagickRealType) q[i];
1000         if (((MagickRealType) s[i] >= (v+ScaleCharToQuantum(2))) &&
1001             ((MagickRealType) r[i] > v))
1002           v+=ScaleCharToQuantum(1);
1003         p[i]=(Quantum) v;
1004         i++;
1005       }
1006     else
1007       for (x=0; x < (ssize_t) columns; x++)
1008       {
1009         v=(MagickRealType) q[i];
1010         if (((MagickRealType) s[i] <= (v-ScaleCharToQuantum(2))) &&
1011             ((MagickRealType) r[i] < v))
1012           v-=ScaleCharToQuantum(1);
1013         p[i]=(Quantum) v;
1014         i++;
1015       }
1016   }
1017 }
1018
1019 MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1020 {
1021 #define DespeckleImageTag  "Despeckle/Image"
1022
1023   CacheView
1024     *despeckle_view,
1025     *image_view;
1026
1027   Image
1028     *despeckle_image;
1029
1030   MagickBooleanType
1031     status;
1032
1033   Quantum
1034     *restrict buffer,
1035     *restrict pixels;
1036
1037   register ssize_t
1038     i;
1039
1040   size_t
1041     length;
1042
1043   static const ssize_t
1044     X[4] = {0, 1, 1,-1},
1045     Y[4] = {1, 0, 1, 1};
1046
1047   /*
1048     Allocate despeckled image.
1049   */
1050   assert(image != (const Image *) NULL);
1051   assert(image->signature == MagickSignature);
1052   if (image->debug != MagickFalse)
1053     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1054   assert(exception != (ExceptionInfo *) NULL);
1055   assert(exception->signature == MagickSignature);
1056   despeckle_image=CloneImage(image,0,0,MagickTrue,exception);
1057   if (despeckle_image == (Image *) NULL)
1058     return((Image *) NULL);
1059   status=SetImageStorageClass(despeckle_image,DirectClass,exception);
1060   if (status == MagickFalse)
1061     {
1062       despeckle_image=DestroyImage(despeckle_image);
1063       return((Image *) NULL);
1064     }
1065   /*
1066     Allocate image buffer.
1067   */
1068   length=(size_t) ((image->columns+2)*(image->rows+2));
1069   pixels=(Quantum *) AcquireQuantumMemory(length,sizeof(*pixels));
1070   buffer=(Quantum *) AcquireQuantumMemory(length,sizeof(*buffer));
1071   if ((pixels == (Quantum *) NULL) || (buffer == (Quantum *) NULL))
1072     {
1073       if (buffer != (Quantum *) NULL)
1074         buffer=(Quantum *) RelinquishMagickMemory(buffer);
1075       if (pixels != (Quantum *) NULL)
1076         pixels=(Quantum *) RelinquishMagickMemory(pixels);
1077       despeckle_image=DestroyImage(despeckle_image);
1078       ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1079     }
1080   /*
1081     Reduce speckle in the image.
1082   */
1083   status=MagickTrue;
1084   image_view=AcquireVirtualCacheView(image,exception);
1085   despeckle_view=AcquireAuthenticCacheView(despeckle_image,exception);
1086   for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1087   {
1088     PixelChannel
1089        channel;
1090
1091     PixelTrait
1092       despeckle_traits,
1093       traits;
1094
1095     register ssize_t
1096       k,
1097       x;
1098
1099     ssize_t
1100       j,
1101       y;
1102
1103     if (status == MagickFalse)
1104       continue;
1105     channel=GetPixelChannelChannel(image,i);
1106     traits=GetPixelChannelTraits(image,channel);
1107     despeckle_traits=GetPixelChannelTraits(despeckle_image,channel);
1108     if ((traits == UndefinedPixelTrait) ||
1109         (despeckle_traits == UndefinedPixelTrait))
1110       continue;
1111     if ((despeckle_traits & CopyPixelTrait) != 0)
1112       continue;
1113     (void) ResetMagickMemory(pixels,0,length*sizeof(*pixels));
1114     j=(ssize_t) image->columns+2;
1115     for (y=0; y < (ssize_t) image->rows; y++)
1116     {
1117       register const Quantum
1118         *restrict p;
1119
1120       p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1121       if (p == (const Quantum *) NULL)
1122         {
1123           status=MagickFalse;
1124           continue;
1125         }
1126       j++;
1127       for (x=0; x < (ssize_t) image->columns; x++)
1128       {
1129         pixels[j++]=p[i];
1130         p+=GetPixelChannels(image);
1131       }
1132       j++;
1133     }
1134     (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
1135     for (k=0; k < 4; k++)
1136     {
1137       Hull(image,X[k],Y[k],image->columns,image->rows,1,pixels,buffer);
1138       Hull(image,-X[k],-Y[k],image->columns,image->rows,1,pixels,buffer);
1139       Hull(image,-X[k],-Y[k],image->columns,image->rows,-1,pixels,buffer);
1140       Hull(image,X[k],Y[k],image->columns,image->rows,-1,pixels,buffer);
1141     }
1142     j=(ssize_t) image->columns+2;
1143     for (y=0; y < (ssize_t) image->rows; y++)
1144     {
1145       MagickBooleanType
1146         sync;
1147
1148       register Quantum
1149         *restrict q;
1150
1151       q=GetCacheViewAuthenticPixels(despeckle_view,0,y,despeckle_image->columns,
1152         1,exception);
1153       if (q == (Quantum *) NULL)
1154         {
1155           status=MagickFalse;
1156           continue;
1157         }
1158       j++;
1159       for (x=0; x < (ssize_t) image->columns; x++)
1160       {
1161         SetPixelChannel(despeckle_image,channel,pixels[j++],q);
1162         q+=GetPixelChannels(despeckle_image);
1163       }
1164       sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1165       if (sync == MagickFalse)
1166         status=MagickFalse;
1167       j++;
1168     }
1169     if (image->progress_monitor != (MagickProgressMonitor) NULL)
1170       {
1171         MagickBooleanType
1172           proceed;
1173
1174         proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1175           GetPixelChannels(image));
1176         if (proceed == MagickFalse)
1177           status=MagickFalse;
1178       }
1179   }
1180   despeckle_view=DestroyCacheView(despeckle_view);
1181   image_view=DestroyCacheView(image_view);
1182   buffer=(Quantum *) RelinquishMagickMemory(buffer);
1183   pixels=(Quantum *) RelinquishMagickMemory(pixels);
1184   despeckle_image->type=image->type;
1185   if (status == MagickFalse)
1186     despeckle_image=DestroyImage(despeckle_image);
1187   return(despeckle_image);
1188 }
1189 \f
1190 /*
1191 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1192 %                                                                             %
1193 %                                                                             %
1194 %                                                                             %
1195 %     E d g e I m a g e                                                       %
1196 %                                                                             %
1197 %                                                                             %
1198 %                                                                             %
1199 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1200 %
1201 %  EdgeImage() finds edges in an image.  Radius defines the radius of the
1202 %  convolution filter.  Use a radius of 0 and EdgeImage() selects a suitable
1203 %  radius for you.
1204 %
1205 %  The format of the EdgeImage method is:
1206 %
1207 %      Image *EdgeImage(const Image *image,const double radius,
1208 %        ExceptionInfo *exception)
1209 %
1210 %  A description of each parameter follows:
1211 %
1212 %    o image: the image.
1213 %
1214 %    o radius: the radius of the pixel neighborhood.
1215 %
1216 %    o exception: return any errors or warnings in this structure.
1217 %
1218 */
1219 MagickExport Image *EdgeImage(const Image *image,const double radius,
1220   ExceptionInfo *exception)
1221 {
1222   char
1223     geometry[MaxTextExtent];
1224
1225   KernelInfo
1226     *kernel_info;
1227
1228   Image
1229     *edge_image;
1230
1231   assert(image != (const Image *) NULL);
1232   assert(image->signature == MagickSignature);
1233   if (image->debug != MagickFalse)
1234     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1235   assert(exception != (ExceptionInfo *) NULL);
1236   assert(exception->signature == MagickSignature);
1237   (void) FormatLocaleString(geometry,MaxTextExtent,"laplacian:%.20g",radius);
1238   kernel_info=AcquireKernelInfo(geometry);
1239   if (kernel_info == (KernelInfo *) NULL)
1240     ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1241   edge_image=MorphologyImage(image,ConvolveMorphology,1,kernel_info,exception);
1242   kernel_info=DestroyKernelInfo(kernel_info);
1243   return(edge_image);
1244 }
1245 \f
1246 /*
1247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1248 %                                                                             %
1249 %                                                                             %
1250 %                                                                             %
1251 %     E m b o s s I m a g e                                                   %
1252 %                                                                             %
1253 %                                                                             %
1254 %                                                                             %
1255 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1256 %
1257 %  EmbossImage() returns a grayscale image with a three-dimensional effect.
1258 %  We convolve the image with a Gaussian operator of the given radius and
1259 %  standard deviation (sigma).  For reasonable results, radius should be
1260 %  larger than sigma.  Use a radius of 0 and Emboss() selects a suitable
1261 %  radius for you.
1262 %
1263 %  The format of the EmbossImage method is:
1264 %
1265 %      Image *EmbossImage(const Image *image,const double radius,
1266 %        const double sigma,ExceptionInfo *exception)
1267 %
1268 %  A description of each parameter follows:
1269 %
1270 %    o image: the image.
1271 %
1272 %    o radius: the radius of the pixel neighborhood.
1273 %
1274 %    o sigma: the standard deviation of the Gaussian, in pixels.
1275 %
1276 %    o exception: return any errors or warnings in this structure.
1277 %
1278 */
1279 MagickExport Image *EmbossImage(const Image *image,const double radius,
1280   const double sigma,ExceptionInfo *exception)
1281 {
1282   Image
1283     *emboss_image;
1284
1285   KernelInfo
1286     *kernel_info;
1287
1288   register ssize_t
1289     i;
1290
1291   size_t
1292     width;
1293
1294   ssize_t
1295     j,
1296     k,
1297     u,
1298     v;
1299
1300   assert(image != (const Image *) NULL);
1301   assert(image->signature == MagickSignature);
1302   if (image->debug != MagickFalse)
1303     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1304   assert(exception != (ExceptionInfo *) NULL);
1305   assert(exception->signature == MagickSignature);
1306   width=GetOptimalKernelWidth1D(radius,sigma);
1307   kernel_info=AcquireKernelInfo((const char *) NULL);
1308   if (kernel_info == (KernelInfo *) NULL)
1309     ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1310   kernel_info->width=width;
1311   kernel_info->height=width;
1312   kernel_info->x=(ssize_t) (width-1)/2;
1313   kernel_info->y=(ssize_t) (width-1)/2;
1314   kernel_info->values=(MagickRealType *) MagickAssumeAligned(
1315     AcquireAlignedMemory(kernel_info->width,kernel_info->width*
1316     sizeof(*kernel_info->values)));
1317   if (kernel_info->values == (MagickRealType *) NULL)
1318     {
1319       kernel_info=DestroyKernelInfo(kernel_info);
1320       ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1321     }
1322   j=(ssize_t) (kernel_info->width-1)/2;
1323   k=j;
1324   i=0;
1325   for (v=(-j); v <= j; v++)
1326   {
1327     for (u=(-j); u <= j; u++)
1328     {
1329       kernel_info->values[i]=(MagickRealType) (((u < 0) || (v < 0) ? -8.0 :
1330         8.0)*exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
1331         (2.0*MagickPI*MagickSigma*MagickSigma));
1332       if (u == k)
1333         kernel_info->values[i]=v == k ? 1.0 : 0.0;
1334       i++;
1335     }
1336     k--;
1337   }
1338   emboss_image=MorphologyImage(image,ConvolveMorphology,1,kernel_info,
1339     exception);
1340   kernel_info=DestroyKernelInfo(kernel_info);
1341   if (emboss_image != (Image *) NULL)
1342     (void) EqualizeImage(emboss_image,exception);
1343   return(emboss_image);
1344 }
1345 \f
1346 /*
1347 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1348 %                                                                             %
1349 %                                                                             %
1350 %                                                                             %
1351 %     G a u s s i a n B l u r I m a g e                                       %
1352 %                                                                             %
1353 %                                                                             %
1354 %                                                                             %
1355 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1356 %
1357 %  GaussianBlurImage() blurs an image.  We convolve the image with a
1358 %  Gaussian operator of the given radius and standard deviation (sigma).
1359 %  For reasonable results, the radius should be larger than sigma.  Use a
1360 %  radius of 0 and GaussianBlurImage() selects a suitable radius for you
1361 %
1362 %  The format of the GaussianBlurImage method is:
1363 %
1364 %      Image *GaussianBlurImage(const Image *image,onst double radius,
1365 %        const double sigma,ExceptionInfo *exception)
1366 %
1367 %  A description of each parameter follows:
1368 %
1369 %    o image: the image.
1370 %
1371 %    o radius: the radius of the Gaussian, in pixels, not counting the center
1372 %      pixel.
1373 %
1374 %    o sigma: the standard deviation of the Gaussian, in pixels.
1375 %
1376 %    o exception: return any errors or warnings in this structure.
1377 %
1378 */
1379 MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
1380   const double sigma,ExceptionInfo *exception)
1381 {
1382   char
1383     geometry[MaxTextExtent];
1384
1385   KernelInfo
1386     *kernel_info;
1387
1388   Image
1389     *blur_image;
1390
1391   assert(image != (const Image *) NULL);
1392   assert(image->signature == MagickSignature);
1393   if (image->debug != MagickFalse)
1394     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1395   assert(exception != (ExceptionInfo *) NULL);
1396   assert(exception->signature == MagickSignature);
1397   (void) FormatLocaleString(geometry,MaxTextExtent,"gaussian:%.20gx%.20g",
1398     radius,sigma);
1399   kernel_info=AcquireKernelInfo(geometry);
1400   if (kernel_info == (KernelInfo *) NULL)
1401     ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1402   blur_image=MorphologyImage(image,ConvolveMorphology,1,kernel_info,exception);
1403   kernel_info=DestroyKernelInfo(kernel_info);
1404   return(blur_image);
1405 }
1406 \f
1407 /*
1408 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1409 %                                                                             %
1410 %                                                                             %
1411 %                                                                             %
1412 %     M o t i o n B l u r I m a g e                                           %
1413 %                                                                             %
1414 %                                                                             %
1415 %                                                                             %
1416 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1417 %
1418 %  MotionBlurImage() simulates motion blur.  We convolve the image with a
1419 %  Gaussian operator of the given radius and standard deviation (sigma).
1420 %  For reasonable results, radius should be larger than sigma.  Use a
1421 %  radius of 0 and MotionBlurImage() selects a suitable radius for you.
1422 %  Angle gives the angle of the blurring motion.
1423 %
1424 %  Andrew Protano contributed this effect.
1425 %
1426 %  The format of the MotionBlurImage method is:
1427 %
1428 %    Image *MotionBlurImage(const Image *image,const double radius,
1429 %      const double sigma,const double angle,ExceptionInfo *exception)
1430 %
1431 %  A description of each parameter follows:
1432 %
1433 %    o image: the image.
1434 %
1435 %    o radius: the radius of the Gaussian, in pixels, not counting
1436 %      the center pixel.
1437 %
1438 %    o sigma: the standard deviation of the Gaussian, in pixels.
1439 %
1440 %    o angle: Apply the effect along this angle.
1441 %
1442 %    o exception: return any errors or warnings in this structure.
1443 %
1444 */
1445
1446 static MagickRealType *GetMotionBlurKernel(const size_t width,
1447   const double sigma)
1448 {
1449   MagickRealType
1450     *kernel,
1451     normalize;
1452
1453   register ssize_t
1454     i;
1455
1456   /*
1457    Generate a 1-D convolution kernel.
1458   */
1459   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1460   kernel=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory((size_t)
1461     width,sizeof(*kernel)));
1462   if (kernel == (MagickRealType *) NULL)
1463     return(kernel);
1464   normalize=0.0;
1465   for (i=0; i < (ssize_t) width; i++)
1466   {
1467     kernel[i]=(MagickRealType) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
1468       MagickSigma)))/(MagickSQ2PI*MagickSigma));
1469     normalize+=kernel[i];
1470   }
1471   for (i=0; i < (ssize_t) width; i++)
1472     kernel[i]/=normalize;
1473   return(kernel);
1474 }
1475
1476 MagickExport Image *MotionBlurImage(const Image *image,const double radius,
1477   const double sigma,const double angle,ExceptionInfo *exception)
1478 {
1479 #define BlurImageTag  "Blur/Image"
1480
1481   CacheView
1482     *blur_view,
1483     *image_view,
1484     *motion_view;
1485
1486   Image
1487     *blur_image;
1488
1489   MagickBooleanType
1490     status;
1491
1492   MagickOffsetType
1493     progress;
1494
1495   MagickRealType
1496     *kernel;
1497
1498   OffsetInfo
1499     *offset;
1500
1501   PointInfo
1502     point;
1503
1504   register ssize_t
1505     i;
1506
1507   size_t
1508     width;
1509
1510   ssize_t
1511     y;
1512
1513   assert(image != (Image *) NULL);
1514   assert(image->signature == MagickSignature);
1515   if (image->debug != MagickFalse)
1516     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1517   assert(exception != (ExceptionInfo *) NULL);
1518   width=GetOptimalKernelWidth1D(radius,sigma);
1519   kernel=GetMotionBlurKernel(width,sigma);
1520   if (kernel == (MagickRealType *) NULL)
1521     ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1522   offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
1523   if (offset == (OffsetInfo *) NULL)
1524     {
1525       kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
1526       ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1527     }
1528   blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1529   if (blur_image == (Image *) NULL)
1530     {
1531       kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
1532       offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1533       return((Image *) NULL);
1534     }
1535   if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
1536     {
1537       kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
1538       offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1539       blur_image=DestroyImage(blur_image);
1540       return((Image *) NULL);
1541     }
1542   point.x=(double) width*sin(DegreesToRadians(angle));
1543   point.y=(double) width*cos(DegreesToRadians(angle));
1544   for (i=0; i < (ssize_t) width; i++)
1545   {
1546     offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
1547     offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
1548   }
1549   /*
1550     Motion blur image.
1551   */
1552   status=MagickTrue;
1553   progress=0;
1554   image_view=AcquireVirtualCacheView(image,exception);
1555   motion_view=AcquireVirtualCacheView(image,exception);
1556   blur_view=AcquireAuthenticCacheView(blur_image,exception);
1557 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1558   #pragma omp parallel for schedule(static,4) shared(progress,status) \
1559     magick_threads(image,blur_image,image->rows,1)
1560 #endif
1561   for (y=0; y < (ssize_t) image->rows; y++)
1562   {
1563     register const Quantum
1564       *restrict p;
1565
1566     register Quantum
1567       *restrict q;
1568
1569     register ssize_t
1570       x;
1571
1572     if (status == MagickFalse)
1573       continue;
1574     p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1575     q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
1576       exception);
1577     if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1578       {
1579         status=MagickFalse;
1580         continue;
1581       }
1582     for (x=0; x < (ssize_t) image->columns; x++)
1583     {
1584       register ssize_t
1585         i;
1586
1587       for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1588       {
1589         double
1590           alpha,
1591           gamma,
1592           pixel;
1593
1594         PixelChannel
1595           channel;
1596
1597         PixelTrait
1598           blur_traits,
1599           traits;
1600
1601         register const Quantum
1602           *restrict r;
1603
1604         register MagickRealType
1605           *restrict k;
1606
1607         register ssize_t
1608           j;
1609
1610         channel=GetPixelChannelChannel(image,i);
1611         traits=GetPixelChannelTraits(image,channel);
1612         blur_traits=GetPixelChannelTraits(blur_image,channel);
1613         if ((traits == UndefinedPixelTrait) ||
1614             (blur_traits == UndefinedPixelTrait))
1615           continue;
1616         if (((blur_traits & CopyPixelTrait) != 0) ||
1617             (GetPixelMask(image,p) == 0))
1618           {
1619             SetPixelChannel(blur_image,channel,p[i],q);
1620             continue;
1621           }
1622         k=kernel;
1623         pixel=0.0;
1624         if ((blur_traits & BlendPixelTrait) == 0)
1625           {
1626             for (j=0; j < (ssize_t) width; j++)
1627             {
1628               r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+
1629                 offset[j].y,1,1,exception);
1630               if (r == (const Quantum *) NULL)
1631                 {
1632                   status=MagickFalse;
1633                   continue;
1634                 }
1635               pixel+=(*k)*r[i];
1636               k++;
1637             }
1638             SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
1639             continue;
1640           }
1641         alpha=0.0;
1642         gamma=0.0;
1643         for (j=0; j < (ssize_t) width; j++)
1644         {
1645           r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+offset[j].y,1,
1646             1,exception);
1647           if (r == (const Quantum *) NULL)
1648             {
1649               status=MagickFalse;
1650               continue;
1651             }
1652           alpha=(double) (QuantumScale*GetPixelAlpha(image,r));
1653           pixel+=(*k)*alpha*r[i];
1654           gamma+=(*k)*alpha;
1655           k++;
1656         }
1657         gamma=PerceptibleReciprocal(gamma);
1658         SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
1659       }
1660       p+=GetPixelChannels(image);
1661       q+=GetPixelChannels(blur_image);
1662     }
1663     if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1664       status=MagickFalse;
1665     if (image->progress_monitor != (MagickProgressMonitor) NULL)
1666       {
1667         MagickBooleanType
1668           proceed;
1669
1670 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1671         #pragma omp critical (MagickCore_MotionBlurImage)
1672 #endif
1673         proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
1674         if (proceed == MagickFalse)
1675           status=MagickFalse;
1676       }
1677   }
1678   blur_view=DestroyCacheView(blur_view);
1679   motion_view=DestroyCacheView(motion_view);
1680   image_view=DestroyCacheView(image_view);
1681   kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
1682   offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1683   if (status == MagickFalse)
1684     blur_image=DestroyImage(blur_image);
1685   return(blur_image);
1686 }
1687 \f
1688 /*
1689 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1690 %                                                                             %
1691 %                                                                             %
1692 %                                                                             %
1693 %     P r e v i e w I m a g e                                                 %
1694 %                                                                             %
1695 %                                                                             %
1696 %                                                                             %
1697 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1698 %
1699 %  PreviewImage() tiles 9 thumbnails of the specified image with an image
1700 %  processing operation applied with varying parameters.  This may be helpful
1701 %  pin-pointing an appropriate parameter for a particular image processing
1702 %  operation.
1703 %
1704 %  The format of the PreviewImages method is:
1705 %
1706 %      Image *PreviewImages(const Image *image,const PreviewType preview,
1707 %        ExceptionInfo *exception)
1708 %
1709 %  A description of each parameter follows:
1710 %
1711 %    o image: the image.
1712 %
1713 %    o preview: the image processing operation.
1714 %
1715 %    o exception: return any errors or warnings in this structure.
1716 %
1717 */
1718 MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
1719   ExceptionInfo *exception)
1720 {
1721 #define NumberTiles  9
1722 #define PreviewImageTag  "Preview/Image"
1723 #define DefaultPreviewGeometry  "204x204+10+10"
1724
1725   char
1726     factor[MaxTextExtent],
1727     label[MaxTextExtent];
1728
1729   double
1730     degrees,
1731     gamma,
1732     percentage,
1733     radius,
1734     sigma,
1735     threshold;
1736
1737   extern const char
1738     DefaultTileFrame[];
1739
1740   Image
1741     *images,
1742     *montage_image,
1743     *preview_image,
1744     *thumbnail;
1745
1746   ImageInfo
1747     *preview_info;
1748
1749   MagickBooleanType
1750     proceed;
1751
1752   MontageInfo
1753     *montage_info;
1754
1755   QuantizeInfo
1756     quantize_info;
1757
1758   RectangleInfo
1759     geometry;
1760
1761   register ssize_t
1762     i,
1763     x;
1764
1765   size_t
1766     colors;
1767
1768   ssize_t
1769     y;
1770
1771   /*
1772     Open output image file.
1773   */
1774   assert(image != (Image *) NULL);
1775   assert(image->signature == MagickSignature);
1776   if (image->debug != MagickFalse)
1777     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1778   colors=2;
1779   degrees=0.0;
1780   gamma=(-0.2f);
1781   preview_info=AcquireImageInfo();
1782   SetGeometry(image,&geometry);
1783   (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
1784     &geometry.width,&geometry.height);
1785   images=NewImageList();
1786   percentage=12.5;
1787   GetQuantizeInfo(&quantize_info);
1788   radius=0.0;
1789   sigma=1.0;
1790   threshold=0.0;
1791   x=0;
1792   y=0;
1793   for (i=0; i < NumberTiles; i++)
1794   {
1795     thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
1796     if (thumbnail == (Image *) NULL)
1797       break;
1798     (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
1799       (void *) NULL);
1800     (void) SetImageProperty(thumbnail,"label",DefaultTileLabel,exception);
1801     if (i == (NumberTiles/2))
1802       {
1803         (void) QueryColorCompliance("#dfdfdf",AllCompliance,
1804           &thumbnail->matte_color,exception);
1805         AppendImageToList(&images,thumbnail);
1806         continue;
1807       }
1808     switch (preview)
1809     {
1810       case RotatePreview:
1811       {
1812         degrees+=45.0;
1813         preview_image=RotateImage(thumbnail,degrees,exception);
1814         (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
1815         break;
1816       }
1817       case ShearPreview:
1818       {
1819         degrees+=5.0;
1820         preview_image=ShearImage(thumbnail,degrees,degrees,exception);
1821         (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",degrees,
1822           2.0*degrees);
1823         break;
1824       }
1825       case RollPreview:
1826       {
1827         x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
1828         y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
1829         preview_image=RollImage(thumbnail,x,y,exception);
1830         (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
1831           (double) x,(double) y);
1832         break;
1833       }
1834       case HuePreview:
1835       {
1836         preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1837         if (preview_image == (Image *) NULL)
1838           break;
1839         (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",2.0*
1840           percentage);
1841         (void) ModulateImage(preview_image,factor,exception);
1842         (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
1843         break;
1844       }
1845       case SaturationPreview:
1846       {
1847         preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1848         if (preview_image == (Image *) NULL)
1849           break;
1850         (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",2.0*percentage);
1851         (void) ModulateImage(preview_image,factor,exception);
1852         (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
1853         break;
1854       }
1855       case BrightnessPreview:
1856       {
1857         preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1858         if (preview_image == (Image *) NULL)
1859           break;
1860         (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
1861         (void) ModulateImage(preview_image,factor,exception);
1862         (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
1863         break;
1864       }
1865       case GammaPreview:
1866       default:
1867       {
1868         preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1869         if (preview_image == (Image *) NULL)
1870           break;
1871         gamma+=0.4f;
1872         (void) GammaImage(preview_image,gamma,exception);
1873         (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
1874         break;
1875       }
1876       case SpiffPreview:
1877       {
1878         preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1879         if (preview_image != (Image *) NULL)
1880           for (x=0; x < i; x++)
1881             (void) ContrastImage(preview_image,MagickTrue,exception);
1882         (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
1883           (double) i+1);
1884         break;
1885       }
1886       case DullPreview:
1887       {
1888         preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1889         if (preview_image == (Image *) NULL)
1890           break;
1891         for (x=0; x < i; x++)
1892           (void) ContrastImage(preview_image,MagickFalse,exception);
1893         (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
1894           (double) i+1);
1895         break;
1896       }
1897       case GrayscalePreview:
1898       {
1899         preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1900         if (preview_image == (Image *) NULL)
1901           break;
1902         colors<<=1;
1903         quantize_info.number_colors=colors;
1904         quantize_info.colorspace=GRAYColorspace;
1905         (void) QuantizeImage(&quantize_info,preview_image,exception);
1906         (void) FormatLocaleString(label,MaxTextExtent,
1907           "-colorspace gray -colors %.20g",(double) colors);
1908         break;
1909       }
1910       case QuantizePreview:
1911       {
1912         preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1913         if (preview_image == (Image *) NULL)
1914           break;
1915         colors<<=1;
1916         quantize_info.number_colors=colors;
1917         (void) QuantizeImage(&quantize_info,preview_image,exception);
1918         (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
1919           colors);
1920         break;
1921       }
1922       case DespecklePreview:
1923       {
1924         for (x=0; x < (i-1); x++)
1925         {
1926           preview_image=DespeckleImage(thumbnail,exception);
1927           if (preview_image == (Image *) NULL)
1928             break;
1929           thumbnail=DestroyImage(thumbnail);
1930           thumbnail=preview_image;
1931         }
1932         preview_image=DespeckleImage(thumbnail,exception);
1933         if (preview_image == (Image *) NULL)
1934           break;
1935         (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
1936           (double) i+1);
1937         break;
1938       }
1939       case ReduceNoisePreview:
1940       {
1941         preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
1942           (size_t) radius,exception);
1943         (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
1944         break;
1945       }
1946       case AddNoisePreview:
1947       {
1948         switch ((int) i)
1949         {
1950           case 0:
1951           {
1952             (void) CopyMagickString(factor,"uniform",MaxTextExtent);
1953             break;
1954           }
1955           case 1:
1956           {
1957             (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
1958             break;
1959           }
1960           case 2:
1961           {
1962             (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
1963             break;
1964           }
1965           case 3:
1966           {
1967             (void) CopyMagickString(factor,"impulse",MaxTextExtent);
1968             break;
1969           }
1970           case 4:
1971           {
1972             (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
1973             break;
1974           }
1975           case 5:
1976           {
1977             (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
1978             break;
1979           }
1980           default:
1981           {
1982             (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
1983             break;
1984           }
1985         }
1986         preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
1987           (size_t) i,exception);
1988         (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
1989         break;
1990       }
1991       case SharpenPreview:
1992       {
1993         preview_image=SharpenImage(thumbnail,radius,sigma,exception);
1994         (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",radius,
1995           sigma);
1996         break;
1997       }
1998       case BlurPreview:
1999       {
2000         preview_image=BlurImage(thumbnail,radius,sigma,exception);
2001         (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
2002           sigma);
2003         break;
2004       }
2005       case ThresholdPreview:
2006       {
2007         preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2008         if (preview_image == (Image *) NULL)
2009           break;
2010         (void) BilevelImage(thumbnail,(double) (percentage*((double)
2011           QuantumRange+1.0))/100.0,exception);
2012         (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",(double)
2013           (percentage*((double) QuantumRange+1.0))/100.0);
2014         break;
2015       }
2016       case EdgeDetectPreview:
2017       {
2018         preview_image=EdgeImage(thumbnail,radius,exception);
2019         (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
2020         break;
2021       }
2022       case SpreadPreview:
2023       {
2024         preview_image=SpreadImage(thumbnail,radius,thumbnail->interpolate,
2025           exception);
2026         (void) FormatLocaleString(label,MaxTextExtent,"spread %g",radius+0.5);
2027         break;
2028       }
2029       case SolarizePreview:
2030       {
2031         preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2032         if (preview_image == (Image *) NULL)
2033           break;
2034         (void) SolarizeImage(preview_image,(double) QuantumRange*percentage/
2035           100.0,exception);
2036         (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
2037           (QuantumRange*percentage)/100.0);
2038         break;
2039       }
2040       case ShadePreview:
2041       {
2042         degrees+=10.0;
2043         preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2044           exception);
2045         (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",degrees,
2046           degrees);
2047         break;
2048       }
2049       case RaisePreview:
2050       {
2051         preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2052         if (preview_image == (Image *) NULL)
2053           break;
2054         geometry.width=(size_t) (2*i+2);
2055         geometry.height=(size_t) (2*i+2);
2056         geometry.x=(i-1)/2;
2057         geometry.y=(i-1)/2;
2058         (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
2059         (void) FormatLocaleString(label,MaxTextExtent,
2060           "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
2061           geometry.height,(double) geometry.x,(double) geometry.y);
2062         break;
2063       }
2064       case SegmentPreview:
2065       {
2066         preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2067         if (preview_image == (Image *) NULL)
2068           break;
2069         threshold+=0.4f;
2070         (void) SegmentImage(preview_image,sRGBColorspace,MagickFalse,threshold,
2071           threshold,exception);
2072         (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
2073           threshold,threshold);
2074         break;
2075       }
2076       case SwirlPreview:
2077       {
2078         preview_image=SwirlImage(thumbnail,degrees,image->interpolate,
2079           exception);
2080         (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
2081         degrees+=45.0;
2082         break;
2083       }
2084       case ImplodePreview:
2085       {
2086         degrees+=0.1f;
2087         preview_image=ImplodeImage(thumbnail,degrees,image->interpolate,
2088           exception);
2089         (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
2090         break;
2091       }
2092       case WavePreview:
2093       {
2094         degrees+=5.0f;
2095         preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,
2096           image->interpolate,exception);
2097         (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",0.5*degrees,
2098           2.0*degrees);
2099         break;
2100       }
2101       case OilPaintPreview:
2102       {
2103         preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2104           exception);
2105         (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",radius,
2106           sigma);
2107         break;
2108       }
2109       case CharcoalDrawingPreview:
2110       {
2111         preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
2112           exception);
2113         (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",radius,
2114           sigma);
2115         break;
2116       }
2117       case JPEGPreview:
2118       {
2119         char
2120           filename[MaxTextExtent];
2121
2122         int
2123           file;
2124
2125         MagickBooleanType
2126           status;
2127
2128         preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2129         if (preview_image == (Image *) NULL)
2130           break;
2131         preview_info->quality=(size_t) percentage;
2132         (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
2133           preview_info->quality);
2134         file=AcquireUniqueFileResource(filename);
2135         if (file != -1)
2136           file=close(file)-1;
2137         (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
2138           "jpeg:%s",filename);
2139         status=WriteImage(preview_info,preview_image,exception);
2140         if (status != MagickFalse)
2141           {
2142             Image
2143               *quality_image;
2144
2145             (void) CopyMagickString(preview_info->filename,
2146               preview_image->filename,MaxTextExtent);
2147             quality_image=ReadImage(preview_info,exception);
2148             if (quality_image != (Image *) NULL)
2149               {
2150                 preview_image=DestroyImage(preview_image);
2151                 preview_image=quality_image;
2152               }
2153           }
2154         (void) RelinquishUniqueFileResource(preview_image->filename);
2155         if ((GetBlobSize(preview_image)/1024) >= 1024)
2156           (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
2157             factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2158             1024.0/1024.0);
2159         else
2160           if (GetBlobSize(preview_image) >= 1024)
2161             (void) FormatLocaleString(label,MaxTextExtent,
2162               "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
2163               GetBlobSize(preview_image))/1024.0);
2164           else
2165             (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
2166               factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
2167         break;
2168       }
2169     }
2170     thumbnail=DestroyImage(thumbnail);
2171     percentage+=12.5;
2172     radius+=0.5;
2173     sigma+=0.25;
2174     if (preview_image == (Image *) NULL)
2175       break;
2176     (void) DeleteImageProperty(preview_image,"label");
2177     (void) SetImageProperty(preview_image,"label",label,exception);
2178     AppendImageToList(&images,preview_image);
2179     proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2180       NumberTiles);
2181     if (proceed == MagickFalse)
2182       break;
2183   }
2184   if (images == (Image *) NULL)
2185     {
2186       preview_info=DestroyImageInfo(preview_info);
2187       return((Image *) NULL);
2188     }
2189   /*
2190     Create the montage.
2191   */
2192   montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2193   (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2194   montage_info->shadow=MagickTrue;
2195   (void) CloneString(&montage_info->tile,"3x3");
2196   (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2197   (void) CloneString(&montage_info->frame,DefaultTileFrame);
2198   montage_image=MontageImages(images,montage_info,exception);
2199   montage_info=DestroyMontageInfo(montage_info);
2200   images=DestroyImageList(images);
2201   if (montage_image == (Image *) NULL)
2202     ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2203   if (montage_image->montage != (char *) NULL)
2204     {
2205       /*
2206         Free image directory.
2207       */
2208       montage_image->montage=(char *) RelinquishMagickMemory(
2209         montage_image->montage);
2210       if (image->directory != (char *) NULL)
2211         montage_image->directory=(char *) RelinquishMagickMemory(
2212           montage_image->directory);
2213     }
2214   preview_info=DestroyImageInfo(preview_info);
2215   return(montage_image);
2216 }
2217 \f
2218 /*
2219 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2220 %                                                                             %
2221 %                                                                             %
2222 %                                                                             %
2223 %     R a d i a l B l u r I m a g e                                           %
2224 %                                                                             %
2225 %                                                                             %
2226 %                                                                             %
2227 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2228 %
2229 %  RadialBlurImage() applies a radial blur to the image.
2230 %
2231 %  Andrew Protano contributed this effect.
2232 %
2233 %  The format of the RadialBlurImage method is:
2234 %
2235 %    Image *RadialBlurImage(const Image *image,const double angle,
2236 %      ExceptionInfo *exception)
2237 %
2238 %  A description of each parameter follows:
2239 %
2240 %    o image: the image.
2241 %
2242 %    o angle: the angle of the radial blur.
2243 %
2244 %    o blur: the blur.
2245 %
2246 %    o exception: return any errors or warnings in this structure.
2247 %
2248 */
2249 MagickExport Image *RadialBlurImage(const Image *image,const double angle,
2250   ExceptionInfo *exception)
2251 {
2252   CacheView
2253     *blur_view,
2254     *image_view,
2255     *radial_view;
2256
2257   Image
2258     *blur_image;
2259
2260   MagickBooleanType
2261     status;
2262
2263   MagickOffsetType
2264     progress;
2265
2266   double
2267     blur_radius,
2268     *cos_theta,
2269     offset,
2270     *sin_theta,
2271     theta;
2272
2273   PointInfo
2274     blur_center;
2275
2276   register ssize_t
2277     i;
2278
2279   size_t
2280     n;
2281
2282   ssize_t
2283     y;
2284
2285   /*
2286     Allocate blur image.
2287   */
2288   assert(image != (Image *) NULL);
2289   assert(image->signature == MagickSignature);
2290   if (image->debug != MagickFalse)
2291     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2292   assert(exception != (ExceptionInfo *) NULL);
2293   assert(exception->signature == MagickSignature);
2294   blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2295   if (blur_image == (Image *) NULL)
2296     return((Image *) NULL);
2297   if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2298     {
2299       blur_image=DestroyImage(blur_image);
2300       return((Image *) NULL);
2301     }
2302   blur_center.x=(double) (image->columns-1)/2.0;
2303   blur_center.y=(double) (image->rows-1)/2.0;
2304   blur_radius=hypot(blur_center.x,blur_center.y);
2305   n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
2306   theta=DegreesToRadians(angle)/(double) (n-1);
2307   cos_theta=(double *) AcquireQuantumMemory((size_t) n,
2308     sizeof(*cos_theta));
2309   sin_theta=(double *) AcquireQuantumMemory((size_t) n,
2310     sizeof(*sin_theta));
2311   if ((cos_theta == (double *) NULL) ||
2312       (sin_theta == (double *) NULL))
2313     {
2314       blur_image=DestroyImage(blur_image);
2315       ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2316     }
2317   offset=theta*(double) (n-1)/2.0;
2318   for (i=0; i < (ssize_t) n; i++)
2319   {
2320     cos_theta[i]=cos((double) (theta*i-offset));
2321     sin_theta[i]=sin((double) (theta*i-offset));
2322   }
2323   /*
2324     Radial blur image.
2325   */
2326   status=MagickTrue;
2327   progress=0;
2328   image_view=AcquireVirtualCacheView(image,exception);
2329   radial_view=AcquireVirtualCacheView(image,exception);
2330   blur_view=AcquireAuthenticCacheView(blur_image,exception);
2331 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2332   #pragma omp parallel for schedule(static,4) shared(progress,status) \
2333     magick_threads(image,blur_image,image->rows,1)
2334 #endif
2335   for (y=0; y < (ssize_t) image->rows; y++)
2336   {
2337     register const Quantum
2338       *restrict p;
2339
2340     register Quantum
2341       *restrict q;
2342
2343     register ssize_t
2344       x;
2345
2346     if (status == MagickFalse)
2347       continue;
2348     p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2349     q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2350       exception);
2351     if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2352       {
2353         status=MagickFalse;
2354         continue;
2355       }
2356     for (x=0; x < (ssize_t) image->columns; x++)
2357     {
2358       double
2359         radius;
2360
2361       PointInfo
2362         center;
2363
2364       register ssize_t
2365         i;
2366
2367       size_t
2368         step;
2369
2370       center.x=(double) x-blur_center.x;
2371       center.y=(double) y-blur_center.y;
2372       radius=hypot((double) center.x,center.y);
2373       if (radius == 0)
2374         step=1;
2375       else
2376         {
2377           step=(size_t) (blur_radius/radius);
2378           if (step == 0)
2379             step=1;
2380           else
2381             if (step >= n)
2382               step=n-1;
2383         }
2384       for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2385       {
2386         double
2387           gamma,
2388           pixel;
2389
2390         PixelChannel
2391           channel;
2392
2393         PixelTrait
2394           blur_traits,
2395           traits;
2396
2397         register const Quantum
2398           *restrict r;
2399
2400         register ssize_t
2401           j;
2402
2403         channel=GetPixelChannelChannel(image,i);
2404         traits=GetPixelChannelTraits(image,channel);
2405         blur_traits=GetPixelChannelTraits(blur_image,channel);
2406         if ((traits == UndefinedPixelTrait) ||
2407             (blur_traits == UndefinedPixelTrait))
2408           continue;
2409         if (((blur_traits & CopyPixelTrait) != 0) ||
2410             (GetPixelMask(image,p) == 0))
2411           {
2412             SetPixelChannel(blur_image,channel,p[i],q);
2413             continue;
2414           }
2415         gamma=0.0;
2416         pixel=0.0;
2417         if ((blur_traits & BlendPixelTrait) == 0)
2418           {
2419             for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2420             {
2421               r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2422                 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2423                 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2424                 1,1,exception);
2425               if (r == (const Quantum *) NULL)
2426                 {
2427                   status=MagickFalse;
2428                   continue;
2429                 }
2430               pixel+=r[i];
2431               gamma++;
2432             }
2433             gamma=PerceptibleReciprocal(gamma);
2434             SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2435             continue;
2436           }
2437         for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2438         {
2439           r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2440             center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2441             (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2442             1,1,exception);
2443           if (r == (const Quantum *) NULL)
2444             {
2445               status=MagickFalse;
2446               continue;
2447             }
2448           pixel+=GetPixelAlpha(image,r)*r[i];
2449           gamma+=GetPixelAlpha(image,r);
2450         }
2451         gamma=PerceptibleReciprocal(gamma);
2452         SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2453       }
2454       p+=GetPixelChannels(image);
2455       q+=GetPixelChannels(blur_image);
2456     }
2457     if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2458       status=MagickFalse;
2459     if (image->progress_monitor != (MagickProgressMonitor) NULL)
2460       {
2461         MagickBooleanType
2462           proceed;
2463
2464 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2465         #pragma omp critical (MagickCore_RadialBlurImage)
2466 #endif
2467         proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2468         if (proceed == MagickFalse)
2469           status=MagickFalse;
2470       }
2471   }
2472   blur_view=DestroyCacheView(blur_view);
2473   radial_view=DestroyCacheView(radial_view);
2474   image_view=DestroyCacheView(image_view);
2475   cos_theta=(double *) RelinquishMagickMemory(cos_theta);
2476   sin_theta=(double *) RelinquishMagickMemory(sin_theta);
2477   if (status == MagickFalse)
2478     blur_image=DestroyImage(blur_image);
2479   return(blur_image);
2480 }
2481 \f
2482 /*
2483 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2484 %                                                                             %
2485 %                                                                             %
2486 %                                                                             %
2487 %     S e l e c t i v e B l u r I m a g e                                     %
2488 %                                                                             %
2489 %                                                                             %
2490 %                                                                             %
2491 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2492 %
2493 %  SelectiveBlurImage() selectively blur pixels within a contrast threshold.
2494 %  It is similar to the unsharpen mask that sharpens everything with contrast
2495 %  above a certain threshold.
2496 %
2497 %  The format of the SelectiveBlurImage method is:
2498 %
2499 %      Image *SelectiveBlurImage(const Image *image,const double radius,
2500 %        const double sigma,const double threshold,ExceptionInfo *exception)
2501 %
2502 %  A description of each parameter follows:
2503 %
2504 %    o image: the image.
2505 %
2506 %    o radius: the radius of the Gaussian, in pixels, not counting the center
2507 %      pixel.
2508 %
2509 %    o sigma: the standard deviation of the Gaussian, in pixels.
2510 %
2511 %    o threshold: only pixels within this contrast threshold are included
2512 %      in the blur operation.
2513 %
2514 %    o exception: return any errors or warnings in this structure.
2515 %
2516 */
2517 MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
2518   const double sigma,const double threshold,ExceptionInfo *exception)
2519 {
2520 #define SelectiveBlurImageTag  "SelectiveBlur/Image"
2521
2522   CacheView
2523     *blur_view,
2524     *image_view,
2525     *luminance_view;
2526
2527   Image
2528     *blur_image,
2529     *luminance_image;
2530
2531   MagickBooleanType
2532     status;
2533
2534   MagickOffsetType
2535     progress;
2536
2537   MagickRealType
2538     *kernel;
2539
2540   register ssize_t
2541     i;
2542
2543   size_t
2544     width;
2545
2546   ssize_t
2547     center,
2548     j,
2549     u,
2550     v,
2551     y;
2552
2553   /*
2554     Initialize blur image attributes.
2555   */
2556   assert(image != (Image *) NULL);
2557   assert(image->signature == MagickSignature);
2558   if (image->debug != MagickFalse)
2559     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2560   assert(exception != (ExceptionInfo *) NULL);
2561   assert(exception->signature == MagickSignature);
2562   width=GetOptimalKernelWidth1D(radius,sigma);
2563   kernel=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory((size_t)
2564     width,width*sizeof(*kernel)));
2565   if (kernel == (MagickRealType *) NULL)
2566     ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2567   j=(ssize_t) (width-1)/2;
2568   i=0;
2569   for (v=(-j); v <= j; v++)
2570   {
2571     for (u=(-j); u <= j; u++)
2572       kernel[i++]=(MagickRealType) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
2573         MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
2574   }
2575   if (image->debug != MagickFalse)
2576     {
2577       char
2578         format[MaxTextExtent],
2579         *message;
2580
2581       register const MagickRealType
2582         *k;
2583
2584       ssize_t
2585         u,
2586         v;
2587
2588       (void) LogMagickEvent(TransformEvent,GetMagickModule(),
2589         "  SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
2590         width);
2591       message=AcquireString("");
2592       k=kernel;
2593       for (v=0; v < (ssize_t) width; v++)
2594       {
2595         *message='\0';
2596         (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
2597         (void) ConcatenateString(&message,format);
2598         for (u=0; u < (ssize_t) width; u++)
2599         {
2600           (void) FormatLocaleString(format,MaxTextExtent,"%+f ",(double) *k++);
2601           (void) ConcatenateString(&message,format);
2602         }
2603         (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
2604       }
2605       message=DestroyString(message);
2606     }
2607   blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2608   if (blur_image == (Image *) NULL)
2609     return((Image *) NULL);
2610   if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2611     {
2612       blur_image=DestroyImage(blur_image);
2613       kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2614       return((Image *) NULL);
2615     }
2616   luminance_image=CloneImage(image,0,0,MagickTrue,exception);
2617   if (luminance_image == (Image *) NULL)
2618     {
2619       blur_image=DestroyImage(blur_image);
2620       kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2621       return((Image *) NULL);
2622     }
2623   status=TransformImageColorspace(luminance_image,GRAYColorspace,exception);
2624   if (status == MagickFalse)
2625     {
2626       luminance_image=DestroyImage(luminance_image);
2627       blur_image=DestroyImage(blur_image);
2628       kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2629       return((Image *) NULL);
2630     }
2631   /*
2632     Threshold blur image.
2633   */
2634   status=MagickTrue;
2635   progress=0;
2636   center=(ssize_t) (GetPixelChannels(image)*(image->columns+width)*
2637     ((width-1)/2L)+GetPixelChannels(image)*((width-1)/2L));
2638   image_view=AcquireVirtualCacheView(image,exception);
2639   luminance_view=AcquireVirtualCacheView(luminance_image,exception);
2640   blur_view=AcquireAuthenticCacheView(blur_image,exception);
2641 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2642   #pragma omp parallel for schedule(static,4) shared(progress,status) \
2643     magick_threads(image,blur_image,image->rows,1)
2644 #endif
2645   for (y=0; y < (ssize_t) image->rows; y++)
2646   {
2647     double
2648       contrast;
2649
2650     MagickBooleanType
2651       sync;
2652
2653     register const Quantum
2654       *restrict l,
2655       *restrict p;
2656
2657     register Quantum
2658       *restrict q;
2659
2660     register ssize_t
2661       x;
2662
2663     if (status == MagickFalse)
2664       continue;
2665     p=GetCacheViewVirtualPixels(image_view,-((ssize_t) (width-1)/2L),y-(ssize_t)
2666       ((width-1)/2L),image->columns+width,width,exception);
2667     l=GetCacheViewVirtualPixels(luminance_view,-((ssize_t) (width-1)/2L),y-
2668       (ssize_t) ((width-1)/2L),luminance_image->columns+width,width,exception);
2669     q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2670       exception);
2671     if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2672       {
2673         status=MagickFalse;
2674         continue;
2675       }
2676     for (x=0; x < (ssize_t) image->columns; x++)
2677     {
2678       double
2679         intensity;
2680
2681       register ssize_t
2682         i;
2683
2684       intensity=GetPixelIntensity(image,p+center);
2685       for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2686       {
2687         double
2688           alpha,
2689           gamma,
2690           pixel;
2691
2692         PixelChannel
2693           channel;
2694
2695         PixelTrait
2696           blur_traits,
2697           traits;
2698
2699         register const MagickRealType
2700           *restrict k;
2701
2702         register const Quantum
2703           *restrict luminance_pixels,
2704           *restrict pixels;
2705
2706         register ssize_t
2707           u;
2708
2709         ssize_t
2710           v;
2711
2712         channel=GetPixelChannelChannel(image,i);
2713         traits=GetPixelChannelTraits(image,channel);
2714         blur_traits=GetPixelChannelTraits(blur_image,channel);
2715         if ((traits == UndefinedPixelTrait) ||
2716             (blur_traits == UndefinedPixelTrait))
2717           continue;
2718         if (((blur_traits & CopyPixelTrait) != 0) ||
2719             (GetPixelMask(image,p+center) == 0))
2720           {
2721             SetPixelChannel(blur_image,channel,p[center+i],q);
2722             continue;
2723           }
2724         k=kernel;
2725         pixel=0.0;
2726         pixels=p;
2727         luminance_pixels=l;
2728         gamma=0.0;
2729         if ((blur_traits & BlendPixelTrait) == 0)
2730           {
2731             for (v=0; v < (ssize_t) width; v++)
2732             {
2733               for (u=0; u < (ssize_t) width; u++)
2734               {
2735                 contrast=GetPixelIntensity(luminance_image,luminance_pixels)-
2736                   intensity;
2737                 if (fabs(contrast) < threshold)
2738                   {
2739                     pixel+=(*k)*pixels[i];
2740                     gamma+=(*k);
2741                   }
2742                 k++;
2743                 pixels+=GetPixelChannels(image);
2744                 luminance_pixels+=GetPixelChannels(luminance_image);
2745               }
2746               pixels+=(image->columns-1)*GetPixelChannels(image);
2747               luminance_pixels+=luminance_image->columns*
2748                 GetPixelChannels(luminance_image);
2749             }
2750             if (fabs((double) gamma) < MagickEpsilon)
2751               {
2752                 SetPixelChannel(blur_image,channel,p[center+i],q);
2753                 continue;
2754               }
2755             gamma=PerceptibleReciprocal(gamma);
2756             SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2757             continue;
2758           }
2759         for (v=0; v < (ssize_t) width; v++)
2760         {
2761           for (u=0; u < (ssize_t) width; u++)
2762           {
2763             contrast=GetPixelIntensity(image,pixels)-intensity;
2764             if (fabs(contrast) < threshold)
2765               {
2766                 alpha=(double) (QuantumScale*GetPixelAlpha(image,pixels));
2767                 pixel+=(*k)*alpha*pixels[i];
2768                 gamma+=(*k)*alpha;
2769               }
2770             k++;
2771             pixels+=GetPixelChannels(image);
2772             luminance_pixels+=GetPixelChannels(luminance_image);
2773           }
2774           pixels+=(image->columns-1)*GetPixelChannels(image);
2775           luminance_pixels+=luminance_image->columns*
2776             GetPixelChannels(luminance_image);
2777         }
2778         if (fabs((double) gamma) < MagickEpsilon)
2779           {
2780             SetPixelChannel(blur_image,channel,p[center+i],q);
2781             continue;
2782           }
2783         gamma=PerceptibleReciprocal(gamma);
2784         SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2785       }
2786       p+=GetPixelChannels(image);
2787       l+=GetPixelChannels(luminance_image);
2788       q+=GetPixelChannels(blur_image);
2789     }
2790     sync=SyncCacheViewAuthenticPixels(blur_view,exception);
2791     if (sync == MagickFalse)
2792       status=MagickFalse;
2793     if (image->progress_monitor != (MagickProgressMonitor) NULL)
2794       {
2795         MagickBooleanType
2796           proceed;
2797
2798 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2799         #pragma omp critical (MagickCore_SelectiveBlurImage)
2800 #endif
2801         proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
2802           image->rows);
2803         if (proceed == MagickFalse)
2804           status=MagickFalse;
2805       }
2806   }
2807   blur_image->type=image->type;
2808   blur_view=DestroyCacheView(blur_view);
2809   image_view=DestroyCacheView(image_view);
2810   luminance_image=DestroyImage(luminance_image);
2811   kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2812   if (status == MagickFalse)
2813     blur_image=DestroyImage(blur_image);
2814   return(blur_image);
2815 }
2816 \f
2817 /*
2818 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2819 %                                                                             %
2820 %                                                                             %
2821 %                                                                             %
2822 %     S h a d e I m a g e                                                     %
2823 %                                                                             %
2824 %                                                                             %
2825 %                                                                             %
2826 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2827 %
2828 %  ShadeImage() shines a distant light on an image to create a
2829 %  three-dimensional effect. You control the positioning of the light with
2830 %  azimuth and elevation; azimuth is measured in degrees off the x axis
2831 %  and elevation is measured in pixels above the Z axis.
2832 %
2833 %  The format of the ShadeImage method is:
2834 %
2835 %      Image *ShadeImage(const Image *image,const MagickBooleanType gray,
2836 %        const double azimuth,const double elevation,ExceptionInfo *exception)
2837 %
2838 %  A description of each parameter follows:
2839 %
2840 %    o image: the image.
2841 %
2842 %    o gray: A value other than zero shades the intensity of each pixel.
2843 %
2844 %    o azimuth, elevation:  Define the light source direction.
2845 %
2846 %    o exception: return any errors or warnings in this structure.
2847 %
2848 */
2849 MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
2850   const double azimuth,const double elevation,ExceptionInfo *exception)
2851 {
2852 #define ShadeImageTag  "Shade/Image"
2853
2854   CacheView
2855     *image_view,
2856     *shade_view;
2857
2858   Image
2859     *linear_image,
2860     *shade_image;
2861
2862   MagickBooleanType
2863     status;
2864
2865   MagickOffsetType
2866     progress;
2867
2868   PrimaryInfo
2869     light;
2870
2871   ssize_t
2872     y;
2873
2874   /*
2875     Initialize shaded image attributes.
2876   */
2877   assert(image != (const Image *) NULL);
2878   assert(image->signature == MagickSignature);
2879   if (image->debug != MagickFalse)
2880     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2881   assert(exception != (ExceptionInfo *) NULL);
2882   assert(exception->signature == MagickSignature);
2883   linear_image=CloneImage(image,0,0,MagickTrue,exception);
2884   shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2885   if ((linear_image == (Image *) NULL) || (shade_image == (Image *) NULL))
2886     {
2887       if (linear_image != (Image *) NULL)
2888         linear_image=DestroyImage(linear_image);
2889       if (shade_image != (Image *) NULL)
2890         shade_image=DestroyImage(shade_image);
2891       return((Image *) NULL);
2892     }
2893   if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
2894     {
2895       linear_image=DestroyImage(linear_image);
2896       shade_image=DestroyImage(shade_image);
2897       return((Image *) NULL);
2898     }
2899   /*
2900     Compute the light vector.
2901   */
2902   light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
2903     cos(DegreesToRadians(elevation));
2904   light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
2905     cos(DegreesToRadians(elevation));
2906   light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
2907   /*
2908     Shade image.
2909   */
2910   status=MagickTrue;
2911   progress=0;
2912   image_view=AcquireVirtualCacheView(linear_image,exception);
2913   shade_view=AcquireAuthenticCacheView(shade_image,exception);
2914 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2915   #pragma omp parallel for schedule(static,4) shared(progress,status) \
2916     magick_threads(linear_image,shade_image,linear_image->rows,1)
2917 #endif
2918   for (y=0; y < (ssize_t) linear_image->rows; y++)
2919   {
2920     double
2921       distance,
2922       normal_distance,
2923       shade;
2924
2925     PrimaryInfo
2926       normal;
2927
2928     register const Quantum
2929       *restrict center,
2930       *restrict p,
2931       *restrict post,
2932       *restrict pre;
2933
2934     register Quantum
2935       *restrict q;
2936
2937     register ssize_t
2938       x;
2939
2940     if (status == MagickFalse)
2941       continue;
2942     p=GetCacheViewVirtualPixels(image_view,-1,y-1,linear_image->columns+2,3,
2943       exception);
2944     q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
2945       exception);
2946     if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2947       {
2948         status=MagickFalse;
2949         continue;
2950       }
2951     /*
2952       Shade this row of pixels.
2953     */
2954     normal.z=2.0*(double) QuantumRange;  /* constant Z of surface normal */
2955     pre=p+GetPixelChannels(linear_image);
2956     center=pre+(linear_image->columns+2)*GetPixelChannels(linear_image);
2957     post=center+(linear_image->columns+2)*GetPixelChannels(linear_image);
2958     for (x=0; x < (ssize_t) linear_image->columns; x++)
2959     {
2960       register ssize_t
2961         i;
2962
2963       /*
2964         Determine the surface normal and compute shading.
2965       */
2966       normal.x=(double) (
2967         GetPixelIntensity(linear_image,pre-GetPixelChannels(linear_image))+
2968         GetPixelIntensity(linear_image,center-GetPixelChannels(linear_image))+
2969         GetPixelIntensity(linear_image,post-GetPixelChannels(linear_image))-
2970         GetPixelIntensity(linear_image,pre+GetPixelChannels(linear_image))-
2971         GetPixelIntensity(linear_image,center+GetPixelChannels(linear_image))-
2972         GetPixelIntensity(linear_image,post+GetPixelChannels(linear_image)));
2973       normal.y=(double) (
2974         GetPixelIntensity(linear_image,post-GetPixelChannels(linear_image))+
2975         GetPixelIntensity(linear_image,post)+
2976         GetPixelIntensity(linear_image,post+GetPixelChannels(linear_image))-
2977         GetPixelIntensity(linear_image,pre-GetPixelChannels(linear_image))-
2978         GetPixelIntensity(linear_image,pre)-
2979         GetPixelIntensity(linear_image,pre+GetPixelChannels(linear_image)));
2980       if ((normal.x == 0.0) && (normal.y == 0.0))
2981         shade=light.z;
2982       else
2983         {
2984           shade=0.0;
2985           distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
2986           if (distance > MagickEpsilon)
2987             {
2988               normal_distance=normal.x*normal.x+normal.y*normal.y+
2989                 normal.z*normal.z;
2990               if (normal_distance > (MagickEpsilon*MagickEpsilon))
2991                 shade=distance/sqrt((double) normal_distance);
2992             }
2993         }
2994       for (i=0; i < (ssize_t) GetPixelChannels(linear_image); i++)
2995       {
2996         PixelChannel
2997           channel;
2998
2999         PixelTrait
3000           shade_traits,
3001           traits;
3002
3003         channel=GetPixelChannelChannel(linear_image,i);
3004         traits=GetPixelChannelTraits(linear_image,channel);
3005         shade_traits=GetPixelChannelTraits(shade_image,channel);
3006         if ((traits == UndefinedPixelTrait) ||
3007             (shade_traits == UndefinedPixelTrait))
3008           continue;
3009         if (((shade_traits & CopyPixelTrait) != 0) ||
3010             (GetPixelMask(linear_image,center) == 0))
3011           {
3012             SetPixelChannel(shade_image,channel,center[i],q);
3013             continue;
3014           }
3015         if (gray != MagickFalse)
3016           {
3017             SetPixelChannel(shade_image,channel,ClampToQuantum(shade),q);
3018             continue;
3019           }
3020         SetPixelChannel(shade_image,channel,ClampToQuantum(QuantumScale*shade*
3021           center[i]),q);
3022       }
3023       pre+=GetPixelChannels(linear_image);
3024       center+=GetPixelChannels(linear_image);
3025       post+=GetPixelChannels(linear_image);
3026       q+=GetPixelChannels(shade_image);
3027     }
3028     if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3029       status=MagickFalse;
3030     if (image->progress_monitor != (MagickProgressMonitor) NULL)
3031       {
3032         MagickBooleanType
3033           proceed;
3034
3035 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3036         #pragma omp critical (MagickCore_ShadeImage)
3037 #endif
3038         proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3039         if (proceed == MagickFalse)
3040           status=MagickFalse;
3041       }
3042   }
3043   shade_view=DestroyCacheView(shade_view);
3044   image_view=DestroyCacheView(image_view);
3045   linear_image=DestroyImage(linear_image);
3046   if (status == MagickFalse)
3047     shade_image=DestroyImage(shade_image);
3048   return(shade_image);
3049 }
3050 \f
3051 /*
3052 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3053 %                                                                             %
3054 %                                                                             %
3055 %                                                                             %
3056 %     S h a r p e n I m a g e                                                 %
3057 %                                                                             %
3058 %                                                                             %
3059 %                                                                             %
3060 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3061 %
3062 %  SharpenImage() sharpens the image.  We convolve the image with a Gaussian
3063 %  operator of the given radius and standard deviation (sigma).  For
3064 %  reasonable results, radius should be larger than sigma.  Use a radius of 0
3065 %  and SharpenImage() selects a suitable radius for you.
3066 %
3067 %  Using a separable kernel would be faster, but the negative weights cancel
3068 %  out on the corners of the kernel producing often undesirable ringing in the
3069 %  filtered result; this can be avoided by using a 2D gaussian shaped image
3070 %  sharpening kernel instead.
3071 %
3072 %  The format of the SharpenImage method is:
3073 %
3074 %    Image *SharpenImage(const Image *image,const double radius,
3075 %      const double sigma,ExceptionInfo *exception)
3076 %
3077 %  A description of each parameter follows:
3078 %
3079 %    o image: the image.
3080 %
3081 %    o radius: the radius of the Gaussian, in pixels, not counting the center
3082 %      pixel.
3083 %
3084 %    o sigma: the standard deviation of the Laplacian, in pixels.
3085 %
3086 %    o exception: return any errors or warnings in this structure.
3087 %
3088 */
3089 MagickExport Image *SharpenImage(const Image *image,const double radius,
3090   const double sigma,ExceptionInfo *exception)
3091 {
3092   char
3093     geometry[MaxTextExtent];
3094
3095   KernelInfo
3096     *kernel_info;
3097
3098   Image
3099     *sharp_image;
3100
3101   assert(image != (const Image *) NULL);
3102   assert(image->signature == MagickSignature);
3103   if (image->debug != MagickFalse)
3104     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3105   assert(exception != (ExceptionInfo *) NULL);
3106   assert(exception->signature == MagickSignature);
3107   (void) FormatLocaleString(geometry,MaxTextExtent,"LoG:%.20gx%.20g",
3108     radius,sigma);
3109   kernel_info=AcquireKernelInfo(geometry);
3110   if (kernel_info == (KernelInfo *) NULL)
3111     ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3112   ScaleGeometryKernelInfo(kernel_info,"56!,100%");
3113   sharp_image=MorphologyImage(image,ConvolveMorphology,1,kernel_info,
3114     exception);
3115   kernel_info=DestroyKernelInfo(kernel_info);
3116   return(sharp_image);
3117 }
3118 \f
3119 /*
3120 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3121 %                                                                             %
3122 %                                                                             %
3123 %                                                                             %
3124 %     S p r e a d I m a g e                                                   %
3125 %                                                                             %
3126 %                                                                             %
3127 %                                                                             %
3128 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3129 %
3130 %  SpreadImage() is a special effects method that randomly displaces each
3131 %  pixel in a block defined by the radius parameter.
3132 %
3133 %  The format of the SpreadImage method is:
3134 %
3135 %      Image *SpreadImage(const Image *image,const double radius,
3136 %        const PixelInterpolateMethod method,ExceptionInfo *exception)
3137 %
3138 %  A description of each parameter follows:
3139 %
3140 %    o image: the image.
3141 %
3142 %    o radius:  choose a random pixel in a neighborhood of this extent.
3143 %
3144 %    o method:  the pixel interpolation method.
3145 %
3146 %    o exception: return any errors or warnings in this structure.
3147 %
3148 */
3149 MagickExport Image *SpreadImage(const Image *image,const double radius,
3150   const PixelInterpolateMethod method,ExceptionInfo *exception)
3151 {
3152 #define SpreadImageTag  "Spread/Image"
3153
3154   CacheView
3155     *image_view,
3156     *spread_view;
3157
3158   Image
3159     *spread_image;
3160
3161   MagickBooleanType
3162     status;
3163
3164   MagickOffsetType
3165     progress;
3166
3167   RandomInfo
3168     **restrict random_info;
3169
3170   size_t
3171     width;
3172
3173   ssize_t
3174     y;
3175
3176 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3177   unsigned long
3178     key;
3179 #endif
3180
3181   /*
3182     Initialize spread image attributes.
3183   */
3184   assert(image != (Image *) NULL);
3185   assert(image->signature == MagickSignature);
3186   if (image->debug != MagickFalse)
3187     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3188   assert(exception != (ExceptionInfo *) NULL);
3189   assert(exception->signature == MagickSignature);
3190   spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3191     exception);
3192   if (spread_image == (Image *) NULL)
3193     return((Image *) NULL);
3194   if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
3195     {
3196       spread_image=DestroyImage(spread_image);
3197       return((Image *) NULL);
3198     }
3199   /*
3200     Spread image.
3201   */
3202   status=MagickTrue;
3203   progress=0;
3204   width=GetOptimalKernelWidth1D(radius,0.5);
3205   random_info=AcquireRandomInfoThreadSet();
3206   image_view=AcquireVirtualCacheView(image,exception);
3207   spread_view=AcquireAuthenticCacheView(spread_image,exception);
3208 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3209   key=GetRandomSecretKey(random_info[0]);
3210   #pragma omp parallel for schedule(static,4) shared(progress,status) \
3211     magick_threads(image,spread_image,image->rows,key == ~0UL)
3212 #endif
3213   for (y=0; y < (ssize_t) image->rows; y++)
3214   {
3215     const int
3216       id = GetOpenMPThreadId();
3217
3218     register const Quantum
3219       *restrict p;
3220
3221     register Quantum
3222       *restrict q;
3223
3224     register ssize_t
3225       x;
3226
3227     if (status == MagickFalse)
3228       continue;
3229     p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3230     q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
3231       exception);
3232     if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3233       {
3234         status=MagickFalse;
3235         continue;
3236       }
3237     for (x=0; x < (ssize_t) image->columns; x++)
3238     {
3239       PointInfo
3240         point;
3241
3242       point.x=GetPseudoRandomValue(random_info[id]);
3243       point.y=GetPseudoRandomValue(random_info[id]);
3244       status=InterpolatePixelChannels(image,image_view,spread_image,method,
3245         (double) x+width*point.x-0.5,(double) y+width*point.y-0.5,q,exception);
3246       q+=GetPixelChannels(spread_image);
3247     }
3248     if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
3249       status=MagickFalse;
3250     if (image->progress_monitor != (MagickProgressMonitor) NULL)
3251       {
3252         MagickBooleanType
3253           proceed;
3254
3255 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3256         #pragma omp critical (MagickCore_SpreadImage)
3257 #endif
3258         proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3259         if (proceed == MagickFalse)
3260           status=MagickFalse;
3261       }
3262   }
3263   spread_view=DestroyCacheView(spread_view);
3264   image_view=DestroyCacheView(image_view);
3265   random_info=DestroyRandomInfoThreadSet(random_info);
3266   if (status == MagickFalse)
3267     spread_image=DestroyImage(spread_image);
3268   return(spread_image);
3269 }
3270 \f
3271 /*
3272 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3273 %                                                                             %
3274 %                                                                             %
3275 %                                                                             %
3276 %     U n s h a r p M a s k I m a g e                                         %
3277 %                                                                             %
3278 %                                                                             %
3279 %                                                                             %
3280 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3281 %
3282 %  UnsharpMaskImage() sharpens one or more image channels.  We convolve the
3283 %  image with a Gaussian operator of the given radius and standard deviation
3284 %  (sigma).  For reasonable results, radius should be larger than sigma.  Use a
3285 %  radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
3286 %
3287 %  The format of the UnsharpMaskImage method is:
3288 %
3289 %    Image *UnsharpMaskImage(const Image *image,const double radius,
3290 %      const double sigma,const double gain,ExceptionInfo *exception)
3291 %
3292 %  A description of each parameter follows:
3293 %
3294 %    o image: the image.
3295 %
3296 %    o radius: the radius of the Gaussian, in pixels, not counting the center
3297 %      pixel.
3298 %
3299 %    o sigma: the standard deviation of the Gaussian, in pixels.
3300 %
3301 %    o gain: the percentage of the difference between the original and the
3302 %      blur image that is added back into the original.
3303 %
3304 %    o exception: return any errors or warnings in this structure.
3305 %
3306 */
3307 MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
3308   const double sigma,const double gain,ExceptionInfo *exception)
3309 {
3310   char
3311     geometry[MaxTextExtent];
3312
3313   KernelInfo
3314     *kernel_info;
3315
3316   Image
3317     *unsharp_image;
3318
3319   assert(image != (const Image *) NULL);
3320   assert(image->signature == MagickSignature);
3321   if (image->debug != MagickFalse)
3322     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3323   assert(exception != (ExceptionInfo *) NULL);
3324   assert(exception->signature == MagickSignature);
3325   (void) FormatLocaleString(geometry,MaxTextExtent,"Blur:%.20gx%.20g>",
3326     radius,sigma);
3327   kernel_info=AcquireKernelInfo(geometry);
3328   if (kernel_info == (KernelInfo *) NULL)
3329     ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3330   (void) FormatLocaleString(geometry,MaxTextExtent,"%.20g,%.20g%%",
3331     -100.0+gain*100.0,200.0-gain*100.0);
3332   ScaleGeometryKernelInfo(kernel_info,geometry);
3333   unsharp_image=MorphologyImage(image,ConvolveMorphology,1,kernel_info,
3334     exception);
3335   kernel_info=DestroyKernelInfo(kernel_info);
3336   return(unsharp_image);
3337 }