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