2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % EEEEE FFFFF FFFFF EEEEE CCCC TTTTT %
8 % EEE FFF FFF EEE C T %
10 % EEEEE F F EEEEE CCCC T %
13 % MagickCore Image Effects Methods %
20 % Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/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"
95 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99 % A d a p t i v e B l u r I m a g e %
103 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
111 % The format of the AdaptiveBlurImage method is:
113 % Image *AdaptiveBlurImage(const Image *image,const double radius,
114 % const double sigma,ExceptionInfo *exception)
116 % A description of each parameter follows:
118 % o image: the image.
120 % o radius: the radius of the Gaussian, in pixels, not counting the center
123 % o sigma: the standard deviation of the Laplacian, in pixels.
125 % o exception: return any errors or warnings in this structure.
129 MagickExport MagickBooleanType AdaptiveLevelImage(Image *image,
130 const char *levels,ExceptionInfo *exception)
149 if (levels == (char *) NULL)
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;
157 if ((flags & XiValue) != 0)
158 gamma=geometry_info.xi;
159 if ((flags & PercentValue) != 0)
161 black_point*=(double) image->columns*image->rows/100.0;
162 white_point*=(double) image->columns*image->rows/100.0;
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);
169 status=LevelizeImage(image,black_point,white_point,gamma,exception);
173 MagickExport Image *AdaptiveBlurImage(const Image *image,const double radius,
174 const double sigma,ExceptionInfo *exception)
176 #define AdaptiveBlurImageTag "Convolve/Image"
177 #define MagickSigma (fabs(sigma) < MagickEpsilon ? MagickEpsilon : sigma)
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)
225 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
227 blur_image=DestroyImage(blur_image);
228 return((Image *) NULL);
231 Edge detect the image brighness channel, level, blur, and level again.
233 edge_image=EdgeImage(image,radius,exception);
234 if (edge_image == (Image *) NULL)
236 blur_image=DestroyImage(blur_image);
237 return((Image *) NULL);
239 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
240 gaussian_image=BlurImage(edge_image,radius,sigma,exception);
241 if (gaussian_image != (Image *) NULL)
243 edge_image=DestroyImage(edge_image);
244 edge_image=gaussian_image;
246 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
248 Create a set of kernels from maximum (radius,sigma) to minimum.
250 width=GetOptimalKernelWidth2D(radius,sigma);
251 kernel=(MagickRealType **) MagickAssumeAligned(AcquireAlignedMemory((size_t)
252 width,sizeof(*kernel)));
253 if (kernel == (MagickRealType **) NULL)
255 edge_image=DestroyImage(edge_image);
256 blur_image=DestroyImage(blur_image);
257 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
259 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
260 for (i=0; i < (ssize_t) width; i+=2)
262 kernel[i]=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory(
263 (size_t) (width-i),(width-i)*sizeof(**kernel)));
264 if (kernel[i] == (MagickRealType *) NULL)
267 j=(ssize_t) (width-i-1)/2;
269 for (v=(-j); v <= j; v++)
271 for (u=(-j); u <= j; u++)
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];
279 kernel[i][(j-1)/2]+=(1.0-normalize);
280 if (sigma < MagickEpsilon)
281 kernel[i][(j-1)/2]=1.0;
283 if (i < (ssize_t) width)
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");
293 Adaptively blur image.
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)
304 for (y=0; y < (ssize_t) blur_image->rows; y++)
306 register const Quantum
315 if (status == MagickFalse)
317 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
318 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
320 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
325 for (x=0; x < (ssize_t) blur_image->columns; x++)
327 register const Quantum
337 j=(ssize_t) ceil((double) width*QuantumScale*
338 GetPixelIntensity(edge_image,r)-0.5);
342 if (j > (ssize_t) width)
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)
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++)
366 register const MagickRealType
369 register const Quantum
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))
384 if (((blur_traits & CopyPixelTrait) != 0) ||
385 (GetPixelReadMask(image,p+center) == 0))
387 SetPixelChannel(blur_image,channel,p[center+i],q);
394 if ((blur_traits & BlendPixelTrait) == 0)
399 for (v=0; v < (ssize_t) (width-j); v++)
401 for (u=0; u < (ssize_t) (width-j); u++)
403 pixel+=(*k)*pixels[i];
406 pixels+=GetPixelChannels(image);
409 gamma=PerceptibleReciprocal(gamma);
410 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
416 for (v=0; v < (ssize_t) (width-j); v++)
418 for (u=0; u < (ssize_t) (width-j); u++)
420 alpha=(double) (QuantumScale*GetPixelAlpha(image,pixels));
421 pixel+=(*k)*alpha*pixels[i];
424 pixels+=GetPixelChannels(image);
427 gamma=PerceptibleReciprocal(gamma);
428 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
430 q+=GetPixelChannels(blur_image);
431 r+=GetPixelChannels(edge_image);
433 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
435 if (image->progress_monitor != (MagickProgressMonitor) NULL)
440 #if defined(MAGICKCORE_OPENMP_SUPPORT)
441 #pragma omp critical (MagickCore_AdaptiveBlurImage)
443 proceed=SetImageProgress(image,AdaptiveBlurImageTag,progress++,
445 if (proceed == MagickFalse)
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);
463 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
467 % A d a p t i v e S h a r p e n I m a g e %
471 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
479 % The format of the AdaptiveSharpenImage method is:
481 % Image *AdaptiveSharpenImage(const Image *image,const double radius,
482 % const double sigma,ExceptionInfo *exception)
484 % A description of each parameter follows:
486 % o image: the image.
488 % o radius: the radius of the Gaussian, in pixels, not counting the center
491 % o sigma: the standard deviation of the Laplacian, in pixels.
493 % o exception: return any errors or warnings in this structure.
496 MagickExport Image *AdaptiveSharpenImage(const Image *image,const double radius,
497 const double sigma,ExceptionInfo *exception)
499 #define AdaptiveSharpenImageTag "Convolve/Image"
500 #define MagickSigma (fabs(sigma) < MagickEpsilon ? MagickEpsilon : sigma)
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)
548 if (SetImageStorageClass(sharp_image,DirectClass,exception) == MagickFalse)
550 sharp_image=DestroyImage(sharp_image);
551 return((Image *) NULL);
554 Edge detect the image brighness channel, level, sharp, and level again.
556 edge_image=EdgeImage(image,radius,exception);
557 if (edge_image == (Image *) NULL)
559 sharp_image=DestroyImage(sharp_image);
560 return((Image *) NULL);
562 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
563 gaussian_image=BlurImage(edge_image,radius,sigma,exception);
564 if (gaussian_image != (Image *) NULL)
566 edge_image=DestroyImage(edge_image);
567 edge_image=gaussian_image;
569 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
571 Create a set of kernels from maximum (radius,sigma) to minimum.
573 width=GetOptimalKernelWidth2D(radius,sigma);
574 kernel=(MagickRealType **) MagickAssumeAligned(AcquireAlignedMemory((size_t)
575 width,sizeof(*kernel)));
576 if (kernel == (MagickRealType **) NULL)
578 edge_image=DestroyImage(edge_image);
579 sharp_image=DestroyImage(sharp_image);
580 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
582 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
583 for (i=0; i < (ssize_t) width; i+=2)
585 kernel[i]=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory(
586 (size_t) (width-i),(width-i)*sizeof(**kernel)));
587 if (kernel[i] == (MagickRealType *) NULL)
590 j=(ssize_t) (width-i)/2;
592 for (v=(-j); v <= j; v++)
594 for (u=(-j); u <= j; u++)
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];
602 kernel[i][(k-1)/2]=(double) ((-2.0)*normalize);
603 if (sigma < MagickEpsilon)
604 kernel[i][(k-1)/2]=1.0;
606 if (i < (ssize_t) width)
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");
616 Adaptively sharpen image.
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)
627 for (y=0; y < (ssize_t) sharp_image->rows; y++)
629 register const Quantum
638 if (status == MagickFalse)
640 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
641 q=QueueCacheViewAuthenticPixels(sharp_view,0,y,sharp_image->columns,1,
643 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
648 for (x=0; x < (ssize_t) sharp_image->columns; x++)
650 register const Quantum
660 j=(ssize_t) ceil((double) width*(1.0-QuantumScale*
661 GetPixelIntensity(edge_image,r))-0.5);
665 if (j > (ssize_t) width)
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)
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++)
689 register const MagickRealType
692 register const Quantum
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))
707 if (((sharp_traits & CopyPixelTrait) != 0) ||
708 (GetPixelReadMask(image,p+center) == 0))
710 SetPixelChannel(sharp_image,channel,p[center+i],q);
717 if ((sharp_traits & BlendPixelTrait) == 0)
722 for (v=0; v < (ssize_t) (width-j); v++)
724 for (u=0; u < (ssize_t) (width-j); u++)
726 pixel+=(*k)*pixels[i];
729 pixels+=GetPixelChannels(image);
732 gamma=PerceptibleReciprocal(gamma);
733 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
739 for (v=0; v < (ssize_t) (width-j); v++)
741 for (u=0; u < (ssize_t) (width-j); u++)
743 alpha=(double) (QuantumScale*GetPixelAlpha(image,pixels));
744 pixel+=(*k)*alpha*pixels[i];
747 pixels+=GetPixelChannels(image);
750 gamma=PerceptibleReciprocal(gamma);
751 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
753 q+=GetPixelChannels(sharp_image);
754 r+=GetPixelChannels(edge_image);
756 if (SyncCacheViewAuthenticPixels(sharp_view,exception) == MagickFalse)
758 if (image->progress_monitor != (MagickProgressMonitor) NULL)
763 #if defined(MAGICKCORE_OPENMP_SUPPORT)
764 #pragma omp critical (MagickCore_AdaptiveSharpenImage)
766 proceed=SetImageProgress(image,AdaptiveSharpenImageTag,progress++,
768 if (proceed == MagickFalse)
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);
786 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
790 % B l u r I m a g e %
794 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
801 % The format of the BlurImage method is:
803 % Image *BlurImage(const Image *image,const double radius,
804 % const double sigma,ExceptionInfo *exception)
806 % A description of each parameter follows:
808 % o image: the image.
810 % o radius: the radius of the Gaussian, in pixels, not counting the center
813 % o sigma: the standard deviation of the Gaussian, in pixels.
815 % o exception: return any errors or warnings in this structure.
818 MagickExport Image *BlurImage(const Image *image,const double radius,
819 const double sigma,ExceptionInfo *exception)
822 geometry[MaxTextExtent];
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);
848 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
852 % C o n v o l v e I m a g e %
856 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
858 % ConvolveImage() applies a custom convolution kernel to the image.
860 % The format of the ConvolveImage method is:
862 % Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
863 % ExceptionInfo *exception)
865 % A description of each parameter follows:
867 % o image: the image.
869 % o kernel: the filtering kernel.
871 % o exception: return any errors or warnings in this structure.
874 MagickExport Image *ConvolveImage(const Image *image,
875 const KernelInfo *kernel_info,ExceptionInfo *exception)
880 convolve_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
881 UndefinedCompositeOp,0.0,exception);
882 return(convolve_image);
886 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
890 % D e s p e c k l e I m a g e %
894 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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).
902 % The format of the DespeckleImage method is:
904 % Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
906 % A description of each parameter follows:
908 % o image: the image.
910 % o exception: return any errors or warnings in this structure.
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)
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);
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)
940 for (y=0; y < (ssize_t) rows; y++)
951 for (x=0; x < (ssize_t) columns; x++)
953 v=(MagickRealType) p[i];
954 if ((MagickRealType) r[i] >= (v+ScaleCharToQuantum(2)))
955 v+=ScaleCharToQuantum(1);
960 for (x=0; x < (ssize_t) columns; x++)
962 v=(MagickRealType) p[i];
963 if ((MagickRealType) r[i] <= (v-ScaleCharToQuantum(2)))
964 v-=ScaleCharToQuantum(1);
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)
977 for (y=0; y < (ssize_t) rows; y++)
988 for (x=0; x < (ssize_t) columns; x++)
990 v=(MagickRealType) q[i];
991 if (((MagickRealType) s[i] >= (v+ScaleCharToQuantum(2))) &&
992 ((MagickRealType) r[i] > v))
993 v+=ScaleCharToQuantum(1);
998 for (x=0; x < (ssize_t) columns; x++)
1000 v=(MagickRealType) q[i];
1001 if (((MagickRealType) s[i] <= (v-ScaleCharToQuantum(2))) &&
1002 ((MagickRealType) r[i] < v))
1003 v-=ScaleCharToQuantum(1);
1010 MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1012 #define DespeckleImageTag "Despeckle/Image"
1034 static const ssize_t
1035 X[4] = {0, 1, 1,-1},
1036 Y[4] = {1, 0, 1, 1};
1039 Allocate despeckled image.
1041 assert(image != (const Image *) NULL);
1042 assert(image->signature == MagickSignature);
1043 if (image->debug != MagickFalse)
1044 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1045 assert(exception != (ExceptionInfo *) NULL);
1046 assert(exception->signature == MagickSignature);
1047 despeckle_image=CloneImage(image,0,0,MagickTrue,exception);
1048 if (despeckle_image == (Image *) NULL)
1049 return((Image *) NULL);
1050 status=SetImageStorageClass(despeckle_image,DirectClass,exception);
1051 if (status == MagickFalse)
1053 despeckle_image=DestroyImage(despeckle_image);
1054 return((Image *) NULL);
1057 Allocate image buffer.
1059 length=(size_t) ((image->columns+2)*(image->rows+2));
1060 pixels=(Quantum *) AcquireQuantumMemory(length,sizeof(*pixels));
1061 buffer=(Quantum *) AcquireQuantumMemory(length,sizeof(*buffer));
1062 if ((pixels == (Quantum *) NULL) || (buffer == (Quantum *) NULL))
1064 if (buffer != (Quantum *) NULL)
1065 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1066 if (pixels != (Quantum *) NULL)
1067 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1068 despeckle_image=DestroyImage(despeckle_image);
1069 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1072 Reduce speckle in the image.
1075 image_view=AcquireVirtualCacheView(image,exception);
1076 despeckle_view=AcquireAuthenticCacheView(despeckle_image,exception);
1077 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1094 if (status == MagickFalse)
1096 channel=GetPixelChannelChannel(image,i);
1097 traits=GetPixelChannelTraits(image,channel);
1098 despeckle_traits=GetPixelChannelTraits(despeckle_image,channel);
1099 if ((traits == UndefinedPixelTrait) ||
1100 (despeckle_traits == UndefinedPixelTrait))
1102 if ((despeckle_traits & CopyPixelTrait) != 0)
1104 (void) ResetMagickMemory(pixels,0,length*sizeof(*pixels));
1105 j=(ssize_t) image->columns+2;
1106 for (y=0; y < (ssize_t) image->rows; y++)
1108 register const Quantum
1111 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1112 if (p == (const Quantum *) NULL)
1118 for (x=0; x < (ssize_t) image->columns; x++)
1121 p+=GetPixelChannels(image);
1125 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
1126 for (k=0; k < 4; k++)
1128 Hull(image,X[k],Y[k],image->columns,image->rows,1,pixels,buffer);
1129 Hull(image,-X[k],-Y[k],image->columns,image->rows,1,pixels,buffer);
1130 Hull(image,-X[k],-Y[k],image->columns,image->rows,-1,pixels,buffer);
1131 Hull(image,X[k],Y[k],image->columns,image->rows,-1,pixels,buffer);
1133 j=(ssize_t) image->columns+2;
1134 for (y=0; y < (ssize_t) image->rows; y++)
1142 q=GetCacheViewAuthenticPixels(despeckle_view,0,y,despeckle_image->columns,
1144 if (q == (Quantum *) NULL)
1150 for (x=0; x < (ssize_t) image->columns; x++)
1152 SetPixelChannel(despeckle_image,channel,pixels[j++],q);
1153 q+=GetPixelChannels(despeckle_image);
1155 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1156 if (sync == MagickFalse)
1160 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1165 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1166 GetPixelChannels(image));
1167 if (proceed == MagickFalse)
1171 despeckle_view=DestroyCacheView(despeckle_view);
1172 image_view=DestroyCacheView(image_view);
1173 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1174 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1175 despeckle_image->type=image->type;
1176 if (status == MagickFalse)
1177 despeckle_image=DestroyImage(despeckle_image);
1178 return(despeckle_image);
1182 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1186 % E d g e I m a g e %
1190 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1192 % EdgeImage() finds edges in an image. Radius defines the radius of the
1193 % convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1196 % The format of the EdgeImage method is:
1198 % Image *EdgeImage(const Image *image,const double radius,
1199 % ExceptionInfo *exception)
1201 % A description of each parameter follows:
1203 % o image: the image.
1205 % o radius: the radius of the pixel neighborhood.
1207 % o exception: return any errors or warnings in this structure.
1210 MagickExport Image *EdgeImage(const Image *image,const double radius,
1211 ExceptionInfo *exception)
1225 assert(image != (const Image *) NULL);
1226 assert(image->signature == MagickSignature);
1227 if (image->debug != MagickFalse)
1228 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1229 assert(exception != (ExceptionInfo *) NULL);
1230 assert(exception->signature == MagickSignature);
1231 width=GetOptimalKernelWidth1D(radius,0.5);
1232 kernel_info=AcquireKernelInfo((const char *) NULL);
1233 if (kernel_info == (KernelInfo *) NULL)
1234 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1235 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1236 kernel_info->width=width;
1237 kernel_info->height=width;
1238 kernel_info->x=(ssize_t) (kernel_info->width-1)/2;
1239 kernel_info->y=(ssize_t) (kernel_info->height-1)/2;
1240 kernel_info->signature=MagickSignature;
1241 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
1242 AcquireAlignedMemory(kernel_info->width,kernel_info->height*
1243 sizeof(*kernel_info->values)));
1244 if (kernel_info->values == (MagickRealType *) NULL)
1246 kernel_info=DestroyKernelInfo(kernel_info);
1247 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1249 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
1250 kernel_info->values[i]=(-1.0);
1251 kernel_info->values[i/2]=(double) kernel_info->width*kernel_info->height-1.0;
1252 edge_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1253 UndefinedCompositeOp,0.0,exception);
1254 kernel_info=DestroyKernelInfo(kernel_info);
1259 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1263 % E m b o s s I m a g e %
1267 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1269 % EmbossImage() returns a grayscale image with a three-dimensional effect.
1270 % We convolve the image with a Gaussian operator of the given radius and
1271 % standard deviation (sigma). For reasonable results, radius should be
1272 % larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1275 % The format of the EmbossImage method is:
1277 % Image *EmbossImage(const Image *image,const double radius,
1278 % const double sigma,ExceptionInfo *exception)
1280 % A description of each parameter follows:
1282 % o image: the image.
1284 % o radius: the radius of the pixel neighborhood.
1286 % o sigma: the standard deviation of the Gaussian, in pixels.
1288 % o exception: return any errors or warnings in this structure.
1291 MagickExport Image *EmbossImage(const Image *image,const double radius,
1292 const double sigma,ExceptionInfo *exception)
1316 assert(image != (const Image *) NULL);
1317 assert(image->signature == MagickSignature);
1318 if (image->debug != MagickFalse)
1319 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1320 assert(exception != (ExceptionInfo *) NULL);
1321 assert(exception->signature == MagickSignature);
1322 width=GetOptimalKernelWidth1D(radius,sigma);
1323 kernel_info=AcquireKernelInfo((const char *) NULL);
1324 if (kernel_info == (KernelInfo *) NULL)
1325 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1326 kernel_info->width=width;
1327 kernel_info->height=width;
1328 kernel_info->x=(ssize_t) (width-1)/2;
1329 kernel_info->y=(ssize_t) (width-1)/2;
1330 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
1331 AcquireAlignedMemory(kernel_info->width,kernel_info->width*
1332 sizeof(*kernel_info->values)));
1333 if (kernel_info->values == (MagickRealType *) NULL)
1335 kernel_info=DestroyKernelInfo(kernel_info);
1336 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1338 j=(ssize_t) (kernel_info->width-1)/2;
1341 for (v=(-j); v <= j; v++)
1343 for (u=(-j); u <= j; u++)
1345 kernel_info->values[i]=(MagickRealType) (((u < 0) || (v < 0) ? -8.0 :
1346 8.0)*exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
1347 (2.0*MagickPI*MagickSigma*MagickSigma));
1349 kernel_info->values[i]=v == k ? 1.0 : 0.0;
1355 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
1356 normalize+=kernel_info->values[i];
1357 gamma=PerceptibleReciprocal(normalize);
1358 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
1359 kernel_info->values[i]*=gamma;
1360 emboss_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1361 UndefinedCompositeOp,0.0,exception);
1362 kernel_info=DestroyKernelInfo(kernel_info);
1363 if (emboss_image != (Image *) NULL)
1364 (void) EqualizeImage(emboss_image,exception);
1365 return(emboss_image);
1369 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1373 % G a u s s i a n B l u r I m a g e %
1377 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1379 % GaussianBlurImage() blurs an image. We convolve the image with a
1380 % Gaussian operator of the given radius and standard deviation (sigma).
1381 % For reasonable results, the radius should be larger than sigma. Use a
1382 % radius of 0 and GaussianBlurImage() selects a suitable radius for you
1384 % The format of the GaussianBlurImage method is:
1386 % Image *GaussianBlurImage(const Image *image,onst double radius,
1387 % const double sigma,ExceptionInfo *exception)
1389 % A description of each parameter follows:
1391 % o image: the image.
1393 % o radius: the radius of the Gaussian, in pixels, not counting the center
1396 % o sigma: the standard deviation of the Gaussian, in pixels.
1398 % o exception: return any errors or warnings in this structure.
1401 MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
1402 const double sigma,ExceptionInfo *exception)
1405 geometry[MaxTextExtent];
1413 assert(image != (const Image *) NULL);
1414 assert(image->signature == MagickSignature);
1415 if (image->debug != MagickFalse)
1416 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1417 assert(exception != (ExceptionInfo *) NULL);
1418 assert(exception->signature == MagickSignature);
1419 (void) FormatLocaleString(geometry,MaxTextExtent,"gaussian:%.20gx%.20g",
1421 kernel_info=AcquireKernelInfo(geometry);
1422 if (kernel_info == (KernelInfo *) NULL)
1423 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1424 blur_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1425 UndefinedCompositeOp,0.0,exception);
1426 kernel_info=DestroyKernelInfo(kernel_info);
1431 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1435 % M o t i o n B l u r I m a g e %
1439 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1441 % MotionBlurImage() simulates motion blur. We convolve the image with a
1442 % Gaussian operator of the given radius and standard deviation (sigma).
1443 % For reasonable results, radius should be larger than sigma. Use a
1444 % radius of 0 and MotionBlurImage() selects a suitable radius for you.
1445 % Angle gives the angle of the blurring motion.
1447 % Andrew Protano contributed this effect.
1449 % The format of the MotionBlurImage method is:
1451 % Image *MotionBlurImage(const Image *image,const double radius,
1452 % const double sigma,const double angle,ExceptionInfo *exception)
1454 % A description of each parameter follows:
1456 % o image: the image.
1458 % o radius: the radius of the Gaussian, in pixels, not counting
1461 % o sigma: the standard deviation of the Gaussian, in pixels.
1463 % o angle: Apply the effect along this angle.
1465 % o exception: return any errors or warnings in this structure.
1469 static MagickRealType *GetMotionBlurKernel(const size_t width,
1480 Generate a 1-D convolution kernel.
1482 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1483 kernel=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory((size_t)
1484 width,sizeof(*kernel)));
1485 if (kernel == (MagickRealType *) NULL)
1488 for (i=0; i < (ssize_t) width; i++)
1490 kernel[i]=(MagickRealType) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
1491 MagickSigma)))/(MagickSQ2PI*MagickSigma));
1492 normalize+=kernel[i];
1494 for (i=0; i < (ssize_t) width; i++)
1495 kernel[i]/=normalize;
1499 MagickExport Image *MotionBlurImage(const Image *image,const double radius,
1500 const double sigma,const double angle,ExceptionInfo *exception)
1502 #define BlurImageTag "Blur/Image"
1536 assert(image != (Image *) NULL);
1537 assert(image->signature == MagickSignature);
1538 if (image->debug != MagickFalse)
1539 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1540 assert(exception != (ExceptionInfo *) NULL);
1541 width=GetOptimalKernelWidth1D(radius,sigma);
1542 kernel=GetMotionBlurKernel(width,sigma);
1543 if (kernel == (MagickRealType *) NULL)
1544 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1545 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
1546 if (offset == (OffsetInfo *) NULL)
1548 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
1549 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1551 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1552 if (blur_image == (Image *) NULL)
1554 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
1555 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1556 return((Image *) NULL);
1558 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
1560 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
1561 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1562 blur_image=DestroyImage(blur_image);
1563 return((Image *) NULL);
1565 point.x=(double) width*sin(DegreesToRadians(angle));
1566 point.y=(double) width*cos(DegreesToRadians(angle));
1567 for (i=0; i < (ssize_t) width; i++)
1569 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
1570 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
1577 image_view=AcquireVirtualCacheView(image,exception);
1578 motion_view=AcquireVirtualCacheView(image,exception);
1579 blur_view=AcquireAuthenticCacheView(blur_image,exception);
1580 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1581 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1582 magick_threads(image,blur_image,image->rows,1)
1584 for (y=0; y < (ssize_t) image->rows; y++)
1586 register const Quantum
1595 if (status == MagickFalse)
1597 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1598 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
1600 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1605 for (x=0; x < (ssize_t) image->columns; x++)
1610 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1624 register const Quantum
1627 register MagickRealType
1633 channel=GetPixelChannelChannel(image,i);
1634 traits=GetPixelChannelTraits(image,channel);
1635 blur_traits=GetPixelChannelTraits(blur_image,channel);
1636 if ((traits == UndefinedPixelTrait) ||
1637 (blur_traits == UndefinedPixelTrait))
1639 if (((blur_traits & CopyPixelTrait) != 0) ||
1640 (GetPixelReadMask(image,p) == 0))
1642 SetPixelChannel(blur_image,channel,p[i],q);
1647 if ((blur_traits & BlendPixelTrait) == 0)
1649 for (j=0; j < (ssize_t) width; j++)
1651 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+
1652 offset[j].y,1,1,exception);
1653 if (r == (const Quantum *) NULL)
1661 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
1666 for (j=0; j < (ssize_t) width; j++)
1668 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+offset[j].y,1,
1670 if (r == (const Quantum *) NULL)
1675 alpha=(double) (QuantumScale*GetPixelAlpha(image,r));
1676 pixel+=(*k)*alpha*r[i];
1680 gamma=PerceptibleReciprocal(gamma);
1681 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
1683 p+=GetPixelChannels(image);
1684 q+=GetPixelChannels(blur_image);
1686 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1688 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1693 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1694 #pragma omp critical (MagickCore_MotionBlurImage)
1696 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
1697 if (proceed == MagickFalse)
1701 blur_view=DestroyCacheView(blur_view);
1702 motion_view=DestroyCacheView(motion_view);
1703 image_view=DestroyCacheView(image_view);
1704 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
1705 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1706 if (status == MagickFalse)
1707 blur_image=DestroyImage(blur_image);
1712 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1716 % P r e v i e w I m a g e %
1720 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1722 % PreviewImage() tiles 9 thumbnails of the specified image with an image
1723 % processing operation applied with varying parameters. This may be helpful
1724 % pin-pointing an appropriate parameter for a particular image processing
1727 % The format of the PreviewImages method is:
1729 % Image *PreviewImages(const Image *image,const PreviewType preview,
1730 % ExceptionInfo *exception)
1732 % A description of each parameter follows:
1734 % o image: the image.
1736 % o preview: the image processing operation.
1738 % o exception: return any errors or warnings in this structure.
1741 MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
1742 ExceptionInfo *exception)
1744 #define NumberTiles 9
1745 #define PreviewImageTag "Preview/Image"
1746 #define DefaultPreviewGeometry "204x204+10+10"
1749 factor[MaxTextExtent],
1750 label[MaxTextExtent];
1795 Open output image file.
1797 assert(image != (Image *) NULL);
1798 assert(image->signature == MagickSignature);
1799 if (image->debug != MagickFalse)
1800 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1804 preview_info=AcquireImageInfo();
1805 SetGeometry(image,&geometry);
1806 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
1807 &geometry.width,&geometry.height);
1808 images=NewImageList();
1810 GetQuantizeInfo(&quantize_info);
1816 for (i=0; i < NumberTiles; i++)
1818 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
1819 if (thumbnail == (Image *) NULL)
1821 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
1823 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel,exception);
1824 if (i == (NumberTiles/2))
1826 (void) QueryColorCompliance("#dfdfdf",AllCompliance,
1827 &thumbnail->matte_color,exception);
1828 AppendImageToList(&images,thumbnail);
1836 preview_image=RotateImage(thumbnail,degrees,exception);
1837 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
1843 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
1844 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",degrees,
1850 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
1851 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
1852 preview_image=RollImage(thumbnail,x,y,exception);
1853 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
1854 (double) x,(double) y);
1859 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1860 if (preview_image == (Image *) NULL)
1862 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",2.0*
1864 (void) ModulateImage(preview_image,factor,exception);
1865 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
1868 case SaturationPreview:
1870 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1871 if (preview_image == (Image *) NULL)
1873 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",2.0*percentage);
1874 (void) ModulateImage(preview_image,factor,exception);
1875 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
1878 case BrightnessPreview:
1880 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1881 if (preview_image == (Image *) NULL)
1883 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
1884 (void) ModulateImage(preview_image,factor,exception);
1885 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
1891 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1892 if (preview_image == (Image *) NULL)
1895 (void) GammaImage(preview_image,gamma,exception);
1896 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
1901 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1902 if (preview_image != (Image *) NULL)
1903 for (x=0; x < i; x++)
1904 (void) ContrastImage(preview_image,MagickTrue,exception);
1905 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
1911 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1912 if (preview_image == (Image *) NULL)
1914 for (x=0; x < i; x++)
1915 (void) ContrastImage(preview_image,MagickFalse,exception);
1916 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
1920 case GrayscalePreview:
1922 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1923 if (preview_image == (Image *) NULL)
1926 quantize_info.number_colors=colors;
1927 quantize_info.colorspace=GRAYColorspace;
1928 (void) QuantizeImage(&quantize_info,preview_image,exception);
1929 (void) FormatLocaleString(label,MaxTextExtent,
1930 "-colorspace gray -colors %.20g",(double) colors);
1933 case QuantizePreview:
1935 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1936 if (preview_image == (Image *) NULL)
1939 quantize_info.number_colors=colors;
1940 (void) QuantizeImage(&quantize_info,preview_image,exception);
1941 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
1945 case DespecklePreview:
1947 for (x=0; x < (i-1); x++)
1949 preview_image=DespeckleImage(thumbnail,exception);
1950 if (preview_image == (Image *) NULL)
1952 thumbnail=DestroyImage(thumbnail);
1953 thumbnail=preview_image;
1955 preview_image=DespeckleImage(thumbnail,exception);
1956 if (preview_image == (Image *) NULL)
1958 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
1962 case ReduceNoisePreview:
1964 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
1965 (size_t) radius,exception);
1966 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
1969 case AddNoisePreview:
1975 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
1980 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
1985 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
1990 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
1995 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2000 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2005 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2009 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2010 (size_t) i,exception);
2011 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
2014 case SharpenPreview:
2016 preview_image=SharpenImage(thumbnail,radius,sigma,exception);
2017 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",radius,
2023 preview_image=BlurImage(thumbnail,radius,sigma,exception);
2024 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
2028 case ThresholdPreview:
2030 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2031 if (preview_image == (Image *) NULL)
2033 (void) BilevelImage(thumbnail,(double) (percentage*((double)
2034 QuantumRange+1.0))/100.0,exception);
2035 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",(double)
2036 (percentage*((double) QuantumRange+1.0))/100.0);
2039 case EdgeDetectPreview:
2041 preview_image=EdgeImage(thumbnail,radius,exception);
2042 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
2047 preview_image=SpreadImage(thumbnail,radius,thumbnail->interpolate,
2049 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",radius+0.5);
2052 case SolarizePreview:
2054 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2055 if (preview_image == (Image *) NULL)
2057 (void) SolarizeImage(preview_image,(double) QuantumRange*percentage/
2059 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
2060 (QuantumRange*percentage)/100.0);
2066 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2068 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",degrees,
2074 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2075 if (preview_image == (Image *) NULL)
2077 geometry.width=(size_t) (2*i+2);
2078 geometry.height=(size_t) (2*i+2);
2081 (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
2082 (void) FormatLocaleString(label,MaxTextExtent,
2083 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
2084 geometry.height,(double) geometry.x,(double) geometry.y);
2087 case SegmentPreview:
2089 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2090 if (preview_image == (Image *) NULL)
2093 (void) SegmentImage(preview_image,sRGBColorspace,MagickFalse,threshold,
2094 threshold,exception);
2095 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
2096 threshold,threshold);
2101 preview_image=SwirlImage(thumbnail,degrees,image->interpolate,
2103 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
2107 case ImplodePreview:
2110 preview_image=ImplodeImage(thumbnail,degrees,image->interpolate,
2112 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
2118 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,
2119 image->interpolate,exception);
2120 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",0.5*degrees,
2124 case OilPaintPreview:
2126 preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2128 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",radius,
2132 case CharcoalDrawingPreview:
2134 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
2136 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",radius,
2143 filename[MaxTextExtent];
2151 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2152 if (preview_image == (Image *) NULL)
2154 preview_info->quality=(size_t) percentage;
2155 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
2156 preview_info->quality);
2157 file=AcquireUniqueFileResource(filename);
2160 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
2161 "jpeg:%s",filename);
2162 status=WriteImage(preview_info,preview_image,exception);
2163 if (status != MagickFalse)
2168 (void) CopyMagickString(preview_info->filename,
2169 preview_image->filename,MaxTextExtent);
2170 quality_image=ReadImage(preview_info,exception);
2171 if (quality_image != (Image *) NULL)
2173 preview_image=DestroyImage(preview_image);
2174 preview_image=quality_image;
2177 (void) RelinquishUniqueFileResource(preview_image->filename);
2178 if ((GetBlobSize(preview_image)/1024) >= 1024)
2179 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
2180 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2183 if (GetBlobSize(preview_image) >= 1024)
2184 (void) FormatLocaleString(label,MaxTextExtent,
2185 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
2186 GetBlobSize(preview_image))/1024.0);
2188 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
2189 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
2193 thumbnail=DestroyImage(thumbnail);
2197 if (preview_image == (Image *) NULL)
2199 (void) DeleteImageProperty(preview_image,"label");
2200 (void) SetImageProperty(preview_image,"label",label,exception);
2201 AppendImageToList(&images,preview_image);
2202 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2204 if (proceed == MagickFalse)
2207 if (images == (Image *) NULL)
2209 preview_info=DestroyImageInfo(preview_info);
2210 return((Image *) NULL);
2215 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2216 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2217 montage_info->shadow=MagickTrue;
2218 (void) CloneString(&montage_info->tile,"3x3");
2219 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2220 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2221 montage_image=MontageImages(images,montage_info,exception);
2222 montage_info=DestroyMontageInfo(montage_info);
2223 images=DestroyImageList(images);
2224 if (montage_image == (Image *) NULL)
2225 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2226 if (montage_image->montage != (char *) NULL)
2229 Free image directory.
2231 montage_image->montage=(char *) RelinquishMagickMemory(
2232 montage_image->montage);
2233 if (image->directory != (char *) NULL)
2234 montage_image->directory=(char *) RelinquishMagickMemory(
2235 montage_image->directory);
2237 preview_info=DestroyImageInfo(preview_info);
2238 return(montage_image);
2242 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2246 % R a d i a l B l u r I m a g e %
2250 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2252 % RadialBlurImage() applies a radial blur to the image.
2254 % Andrew Protano contributed this effect.
2256 % The format of the RadialBlurImage method is:
2258 % Image *RadialBlurImage(const Image *image,const double angle,
2259 % ExceptionInfo *exception)
2261 % A description of each parameter follows:
2263 % o image: the image.
2265 % o angle: the angle of the radial blur.
2269 % o exception: return any errors or warnings in this structure.
2272 MagickExport Image *RadialBlurImage(const Image *image,const double angle,
2273 ExceptionInfo *exception)
2309 Allocate blur image.
2311 assert(image != (Image *) NULL);
2312 assert(image->signature == MagickSignature);
2313 if (image->debug != MagickFalse)
2314 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2315 assert(exception != (ExceptionInfo *) NULL);
2316 assert(exception->signature == MagickSignature);
2317 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2318 if (blur_image == (Image *) NULL)
2319 return((Image *) NULL);
2320 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2322 blur_image=DestroyImage(blur_image);
2323 return((Image *) NULL);
2325 blur_center.x=(double) (image->columns-1)/2.0;
2326 blur_center.y=(double) (image->rows-1)/2.0;
2327 blur_radius=hypot(blur_center.x,blur_center.y);
2328 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
2329 theta=DegreesToRadians(angle)/(double) (n-1);
2330 cos_theta=(double *) AcquireQuantumMemory((size_t) n,
2331 sizeof(*cos_theta));
2332 sin_theta=(double *) AcquireQuantumMemory((size_t) n,
2333 sizeof(*sin_theta));
2334 if ((cos_theta == (double *) NULL) ||
2335 (sin_theta == (double *) NULL))
2337 blur_image=DestroyImage(blur_image);
2338 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2340 offset=theta*(double) (n-1)/2.0;
2341 for (i=0; i < (ssize_t) n; i++)
2343 cos_theta[i]=cos((double) (theta*i-offset));
2344 sin_theta[i]=sin((double) (theta*i-offset));
2351 image_view=AcquireVirtualCacheView(image,exception);
2352 radial_view=AcquireVirtualCacheView(image,exception);
2353 blur_view=AcquireAuthenticCacheView(blur_image,exception);
2354 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2355 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2356 magick_threads(image,blur_image,image->rows,1)
2358 for (y=0; y < (ssize_t) image->rows; y++)
2360 register const Quantum
2369 if (status == MagickFalse)
2371 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2372 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2374 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2379 for (x=0; x < (ssize_t) image->columns; x++)
2393 center.x=(double) x-blur_center.x;
2394 center.y=(double) y-blur_center.y;
2395 radius=hypot((double) center.x,center.y);
2400 step=(size_t) (blur_radius/radius);
2407 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2420 register const Quantum
2426 channel=GetPixelChannelChannel(image,i);
2427 traits=GetPixelChannelTraits(image,channel);
2428 blur_traits=GetPixelChannelTraits(blur_image,channel);
2429 if ((traits == UndefinedPixelTrait) ||
2430 (blur_traits == UndefinedPixelTrait))
2432 if (((blur_traits & CopyPixelTrait) != 0) ||
2433 (GetPixelReadMask(image,p) == 0))
2435 SetPixelChannel(blur_image,channel,p[i],q);
2440 if ((blur_traits & BlendPixelTrait) == 0)
2442 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2444 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2445 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2446 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2448 if (r == (const Quantum *) NULL)
2456 gamma=PerceptibleReciprocal(gamma);
2457 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2460 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2462 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2463 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2464 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2466 if (r == (const Quantum *) NULL)
2471 pixel+=GetPixelAlpha(image,r)*r[i];
2472 gamma+=GetPixelAlpha(image,r);
2474 gamma=PerceptibleReciprocal(gamma);
2475 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2477 p+=GetPixelChannels(image);
2478 q+=GetPixelChannels(blur_image);
2480 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2482 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2487 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2488 #pragma omp critical (MagickCore_RadialBlurImage)
2490 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2491 if (proceed == MagickFalse)
2495 blur_view=DestroyCacheView(blur_view);
2496 radial_view=DestroyCacheView(radial_view);
2497 image_view=DestroyCacheView(image_view);
2498 cos_theta=(double *) RelinquishMagickMemory(cos_theta);
2499 sin_theta=(double *) RelinquishMagickMemory(sin_theta);
2500 if (status == MagickFalse)
2501 blur_image=DestroyImage(blur_image);
2506 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2510 % S e l e c t i v e B l u r I m a g e %
2514 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2516 % SelectiveBlurImage() selectively blur pixels within a contrast threshold.
2517 % It is similar to the unsharpen mask that sharpens everything with contrast
2518 % above a certain threshold.
2520 % The format of the SelectiveBlurImage method is:
2522 % Image *SelectiveBlurImage(const Image *image,const double radius,
2523 % const double sigma,const double threshold,ExceptionInfo *exception)
2525 % A description of each parameter follows:
2527 % o image: the image.
2529 % o radius: the radius of the Gaussian, in pixels, not counting the center
2532 % o sigma: the standard deviation of the Gaussian, in pixels.
2534 % o threshold: only pixels within this contrast threshold are included
2535 % in the blur operation.
2537 % o exception: return any errors or warnings in this structure.
2540 MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
2541 const double sigma,const double threshold,ExceptionInfo *exception)
2543 #define SelectiveBlurImageTag "SelectiveBlur/Image"
2577 Initialize blur image attributes.
2579 assert(image != (Image *) NULL);
2580 assert(image->signature == MagickSignature);
2581 if (image->debug != MagickFalse)
2582 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2583 assert(exception != (ExceptionInfo *) NULL);
2584 assert(exception->signature == MagickSignature);
2585 width=GetOptimalKernelWidth1D(radius,sigma);
2586 kernel=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory((size_t)
2587 width,width*sizeof(*kernel)));
2588 if (kernel == (MagickRealType *) NULL)
2589 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2590 j=(ssize_t) (width-1)/2;
2592 for (v=(-j); v <= j; v++)
2594 for (u=(-j); u <= j; u++)
2595 kernel[i++]=(MagickRealType) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
2596 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
2598 if (image->debug != MagickFalse)
2601 format[MaxTextExtent],
2604 register const MagickRealType
2611 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
2612 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
2614 message=AcquireString("");
2616 for (v=0; v < (ssize_t) width; v++)
2619 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
2620 (void) ConcatenateString(&message,format);
2621 for (u=0; u < (ssize_t) width; u++)
2623 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",(double) *k++);
2624 (void) ConcatenateString(&message,format);
2626 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
2628 message=DestroyString(message);
2630 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2631 if (blur_image == (Image *) NULL)
2632 return((Image *) NULL);
2633 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2635 blur_image=DestroyImage(blur_image);
2636 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2637 return((Image *) NULL);
2639 luminance_image=CloneImage(image,0,0,MagickTrue,exception);
2640 if (luminance_image == (Image *) NULL)
2642 blur_image=DestroyImage(blur_image);
2643 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2644 return((Image *) NULL);
2646 status=TransformImageColorspace(luminance_image,GRAYColorspace,exception);
2647 if (status == MagickFalse)
2649 luminance_image=DestroyImage(luminance_image);
2650 blur_image=DestroyImage(blur_image);
2651 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2652 return((Image *) NULL);
2655 Threshold blur image.
2659 center=(ssize_t) (GetPixelChannels(image)*(image->columns+width)*
2660 ((width-1)/2L)+GetPixelChannels(image)*((width-1)/2L));
2661 image_view=AcquireVirtualCacheView(image,exception);
2662 luminance_view=AcquireVirtualCacheView(luminance_image,exception);
2663 blur_view=AcquireAuthenticCacheView(blur_image,exception);
2664 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2665 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2666 magick_threads(image,blur_image,image->rows,1)
2668 for (y=0; y < (ssize_t) image->rows; y++)
2676 register const Quantum
2686 if (status == MagickFalse)
2688 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) (width-1)/2L),y-(ssize_t)
2689 ((width-1)/2L),image->columns+width,width,exception);
2690 l=GetCacheViewVirtualPixels(luminance_view,-((ssize_t) (width-1)/2L),y-
2691 (ssize_t) ((width-1)/2L),luminance_image->columns+width,width,exception);
2692 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2694 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2699 for (x=0; x < (ssize_t) image->columns; x++)
2707 intensity=GetPixelIntensity(image,p+center);
2708 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2722 register const MagickRealType
2725 register const Quantum
2726 *restrict luminance_pixels,
2735 channel=GetPixelChannelChannel(image,i);
2736 traits=GetPixelChannelTraits(image,channel);
2737 blur_traits=GetPixelChannelTraits(blur_image,channel);
2738 if ((traits == UndefinedPixelTrait) ||
2739 (blur_traits == UndefinedPixelTrait))
2741 if (((blur_traits & CopyPixelTrait) != 0) ||
2742 (GetPixelReadMask(image,p+center) == 0))
2744 SetPixelChannel(blur_image,channel,p[center+i],q);
2752 if ((blur_traits & BlendPixelTrait) == 0)
2754 for (v=0; v < (ssize_t) width; v++)
2756 for (u=0; u < (ssize_t) width; u++)
2758 contrast=GetPixelIntensity(luminance_image,luminance_pixels)-
2760 if (fabs(contrast) < threshold)
2762 pixel+=(*k)*pixels[i];
2766 pixels+=GetPixelChannels(image);
2767 luminance_pixels+=GetPixelChannels(luminance_image);
2769 pixels+=(image->columns-1)*GetPixelChannels(image);
2770 luminance_pixels+=luminance_image->columns*
2771 GetPixelChannels(luminance_image);
2773 if (fabs((double) gamma) < MagickEpsilon)
2775 SetPixelChannel(blur_image,channel,p[center+i],q);
2778 gamma=PerceptibleReciprocal(gamma);
2779 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2782 for (v=0; v < (ssize_t) width; v++)
2784 for (u=0; u < (ssize_t) width; u++)
2786 contrast=GetPixelIntensity(image,pixels)-intensity;
2787 if (fabs(contrast) < threshold)
2789 alpha=(double) (QuantumScale*GetPixelAlpha(image,pixels));
2790 pixel+=(*k)*alpha*pixels[i];
2794 pixels+=GetPixelChannels(image);
2795 luminance_pixels+=GetPixelChannels(luminance_image);
2797 pixels+=(image->columns-1)*GetPixelChannels(image);
2798 luminance_pixels+=luminance_image->columns*
2799 GetPixelChannels(luminance_image);
2801 if (fabs((double) gamma) < MagickEpsilon)
2803 SetPixelChannel(blur_image,channel,p[center+i],q);
2806 gamma=PerceptibleReciprocal(gamma);
2807 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2809 p+=GetPixelChannels(image);
2810 l+=GetPixelChannels(luminance_image);
2811 q+=GetPixelChannels(blur_image);
2813 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
2814 if (sync == MagickFalse)
2816 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2821 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2822 #pragma omp critical (MagickCore_SelectiveBlurImage)
2824 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
2826 if (proceed == MagickFalse)
2830 blur_image->type=image->type;
2831 blur_view=DestroyCacheView(blur_view);
2832 image_view=DestroyCacheView(image_view);
2833 luminance_image=DestroyImage(luminance_image);
2834 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2835 if (status == MagickFalse)
2836 blur_image=DestroyImage(blur_image);
2841 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2845 % S h a d e I m a g e %
2849 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2851 % ShadeImage() shines a distant light on an image to create a
2852 % three-dimensional effect. You control the positioning of the light with
2853 % azimuth and elevation; azimuth is measured in degrees off the x axis
2854 % and elevation is measured in pixels above the Z axis.
2856 % The format of the ShadeImage method is:
2858 % Image *ShadeImage(const Image *image,const MagickBooleanType gray,
2859 % const double azimuth,const double elevation,ExceptionInfo *exception)
2861 % A description of each parameter follows:
2863 % o image: the image.
2865 % o gray: A value other than zero shades the intensity of each pixel.
2867 % o azimuth, elevation: Define the light source direction.
2869 % o exception: return any errors or warnings in this structure.
2872 MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
2873 const double azimuth,const double elevation,ExceptionInfo *exception)
2875 #define ShadeImageTag "Shade/Image"
2898 Initialize shaded image attributes.
2900 assert(image != (const Image *) NULL);
2901 assert(image->signature == MagickSignature);
2902 if (image->debug != MagickFalse)
2903 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2904 assert(exception != (ExceptionInfo *) NULL);
2905 assert(exception->signature == MagickSignature);
2906 linear_image=CloneImage(image,0,0,MagickTrue,exception);
2907 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2908 if ((linear_image == (Image *) NULL) || (shade_image == (Image *) NULL))
2910 if (linear_image != (Image *) NULL)
2911 linear_image=DestroyImage(linear_image);
2912 if (shade_image != (Image *) NULL)
2913 shade_image=DestroyImage(shade_image);
2914 return((Image *) NULL);
2916 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
2918 linear_image=DestroyImage(linear_image);
2919 shade_image=DestroyImage(shade_image);
2920 return((Image *) NULL);
2923 Compute the light vector.
2925 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
2926 cos(DegreesToRadians(elevation));
2927 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
2928 cos(DegreesToRadians(elevation));
2929 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
2935 image_view=AcquireVirtualCacheView(linear_image,exception);
2936 shade_view=AcquireAuthenticCacheView(shade_image,exception);
2937 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2938 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2939 magick_threads(linear_image,shade_image,linear_image->rows,1)
2941 for (y=0; y < (ssize_t) linear_image->rows; y++)
2951 register const Quantum
2963 if (status == MagickFalse)
2965 p=GetCacheViewVirtualPixels(image_view,-1,y-1,linear_image->columns+2,3,
2967 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
2969 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2975 Shade this row of pixels.
2977 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
2978 pre=p+GetPixelChannels(linear_image);
2979 center=pre+(linear_image->columns+2)*GetPixelChannels(linear_image);
2980 post=center+(linear_image->columns+2)*GetPixelChannels(linear_image);
2981 for (x=0; x < (ssize_t) linear_image->columns; x++)
2987 Determine the surface normal and compute shading.
2990 GetPixelIntensity(linear_image,pre-GetPixelChannels(linear_image))+
2991 GetPixelIntensity(linear_image,center-GetPixelChannels(linear_image))+
2992 GetPixelIntensity(linear_image,post-GetPixelChannels(linear_image))-
2993 GetPixelIntensity(linear_image,pre+GetPixelChannels(linear_image))-
2994 GetPixelIntensity(linear_image,center+GetPixelChannels(linear_image))-
2995 GetPixelIntensity(linear_image,post+GetPixelChannels(linear_image)));
2997 GetPixelIntensity(linear_image,post-GetPixelChannels(linear_image))+
2998 GetPixelIntensity(linear_image,post)+
2999 GetPixelIntensity(linear_image,post+GetPixelChannels(linear_image))-
3000 GetPixelIntensity(linear_image,pre-GetPixelChannels(linear_image))-
3001 GetPixelIntensity(linear_image,pre)-
3002 GetPixelIntensity(linear_image,pre+GetPixelChannels(linear_image)));
3003 if ((normal.x == 0.0) && (normal.y == 0.0))
3008 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3009 if (distance > MagickEpsilon)
3011 normal_distance=normal.x*normal.x+normal.y*normal.y+
3013 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3014 shade=distance/sqrt((double) normal_distance);
3017 for (i=0; i < (ssize_t) GetPixelChannels(linear_image); i++)
3026 channel=GetPixelChannelChannel(linear_image,i);
3027 traits=GetPixelChannelTraits(linear_image,channel);
3028 shade_traits=GetPixelChannelTraits(shade_image,channel);
3029 if ((traits == UndefinedPixelTrait) ||
3030 (shade_traits == UndefinedPixelTrait))
3032 if (((shade_traits & CopyPixelTrait) != 0) ||
3033 (GetPixelReadMask(linear_image,center) == 0))
3035 SetPixelChannel(shade_image,channel,center[i],q);
3038 if (gray != MagickFalse)
3040 SetPixelChannel(shade_image,channel,ClampToQuantum(shade),q);
3043 SetPixelChannel(shade_image,channel,ClampToQuantum(QuantumScale*shade*
3046 pre+=GetPixelChannels(linear_image);
3047 center+=GetPixelChannels(linear_image);
3048 post+=GetPixelChannels(linear_image);
3049 q+=GetPixelChannels(shade_image);
3051 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3053 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3058 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3059 #pragma omp critical (MagickCore_ShadeImage)
3061 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3062 if (proceed == MagickFalse)
3066 shade_view=DestroyCacheView(shade_view);
3067 image_view=DestroyCacheView(image_view);
3068 linear_image=DestroyImage(linear_image);
3069 if (status == MagickFalse)
3070 shade_image=DestroyImage(shade_image);
3071 return(shade_image);
3075 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3079 % S h a r p e n I m a g e %
3083 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3085 % SharpenImage() sharpens the image. We convolve the image with a Gaussian
3086 % operator of the given radius and standard deviation (sigma). For
3087 % reasonable results, radius should be larger than sigma. Use a radius of 0
3088 % and SharpenImage() selects a suitable radius for you.
3090 % Using a separable kernel would be faster, but the negative weights cancel
3091 % out on the corners of the kernel producing often undesirable ringing in the
3092 % filtered result; this can be avoided by using a 2D gaussian shaped image
3093 % sharpening kernel instead.
3095 % The format of the SharpenImage method is:
3097 % Image *SharpenImage(const Image *image,const double radius,
3098 % const double sigma,ExceptionInfo *exception)
3100 % A description of each parameter follows:
3102 % o image: the image.
3104 % o radius: the radius of the Gaussian, in pixels, not counting the center
3107 % o sigma: the standard deviation of the Laplacian, in pixels.
3109 % o exception: return any errors or warnings in this structure.
3112 MagickExport Image *SharpenImage(const Image *image,const double radius,
3113 const double sigma,ExceptionInfo *exception)
3136 assert(image != (const Image *) NULL);
3137 assert(image->signature == MagickSignature);
3138 if (image->debug != MagickFalse)
3139 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3140 assert(exception != (ExceptionInfo *) NULL);
3141 assert(exception->signature == MagickSignature);
3142 width=GetOptimalKernelWidth2D(radius,sigma);
3143 kernel_info=AcquireKernelInfo((const char *) NULL);
3144 if (kernel_info == (KernelInfo *) NULL)
3145 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3146 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3147 kernel_info->width=width;
3148 kernel_info->height=width;
3149 kernel_info->x=(ssize_t) (width-1)/2;
3150 kernel_info->y=(ssize_t) (width-1)/2;
3151 kernel_info->signature=MagickSignature;
3152 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
3153 AcquireAlignedMemory(kernel_info->width,kernel_info->height*
3154 sizeof(*kernel_info->values)));
3155 if (kernel_info->values == (MagickRealType *) NULL)
3157 kernel_info=DestroyKernelInfo(kernel_info);
3158 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3161 j=(ssize_t) (kernel_info->width-1)/2;
3163 for (v=(-j); v <= j; v++)
3165 for (u=(-j); u <= j; u++)
3167 kernel_info->values[i]=(MagickRealType) (-exp(-((double) u*u+v*v)/(2.0*
3168 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3169 normalize+=kernel_info->values[i];
3173 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
3175 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
3176 normalize+=kernel_info->values[i];
3177 gamma=PerceptibleReciprocal(normalize);
3178 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
3179 kernel_info->values[i]*=gamma;
3180 sharp_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
3181 UndefinedCompositeOp,0.0,exception);
3182 kernel_info=DestroyKernelInfo(kernel_info);
3183 return(sharp_image);
3187 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3191 % S p r e a d I m a g e %
3195 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3197 % SpreadImage() is a special effects method that randomly displaces each
3198 % pixel in a block defined by the radius parameter.
3200 % The format of the SpreadImage method is:
3202 % Image *SpreadImage(const Image *image,const double radius,
3203 % const PixelInterpolateMethod method,ExceptionInfo *exception)
3205 % A description of each parameter follows:
3207 % o image: the image.
3209 % o radius: choose a random pixel in a neighborhood of this extent.
3211 % o method: the pixel interpolation method.
3213 % o exception: return any errors or warnings in this structure.
3216 MagickExport Image *SpreadImage(const Image *image,const double radius,
3217 const PixelInterpolateMethod method,ExceptionInfo *exception)
3219 #define SpreadImageTag "Spread/Image"
3235 **restrict random_info;
3243 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3249 Initialize spread image attributes.
3251 assert(image != (Image *) NULL);
3252 assert(image->signature == MagickSignature);
3253 if (image->debug != MagickFalse)
3254 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3255 assert(exception != (ExceptionInfo *) NULL);
3256 assert(exception->signature == MagickSignature);
3257 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3259 if (spread_image == (Image *) NULL)
3260 return((Image *) NULL);
3261 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
3263 spread_image=DestroyImage(spread_image);
3264 return((Image *) NULL);
3271 width=GetOptimalKernelWidth1D(radius,0.5);
3272 random_info=AcquireRandomInfoThreadSet();
3273 image_view=AcquireVirtualCacheView(image,exception);
3274 spread_view=AcquireAuthenticCacheView(spread_image,exception);
3275 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3276 key=GetRandomSecretKey(random_info[0]);
3277 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3278 magick_threads(image,spread_image,image->rows,key == ~0UL)
3280 for (y=0; y < (ssize_t) image->rows; y++)
3283 id = GetOpenMPThreadId();
3291 if (status == MagickFalse)
3293 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
3295 if (q == (Quantum *) NULL)
3300 for (x=0; x < (ssize_t) image->columns; x++)
3305 point.x=GetPseudoRandomValue(random_info[id]);
3306 point.y=GetPseudoRandomValue(random_info[id]);
3307 status=InterpolatePixelChannels(image,image_view,spread_image,method,
3308 (double) x+width*point.x-0.5,(double) y+width*point.y-0.5,q,exception);
3309 q+=GetPixelChannels(spread_image);
3311 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
3313 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3318 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3319 #pragma omp critical (MagickCore_SpreadImage)
3321 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3322 if (proceed == MagickFalse)
3326 spread_view=DestroyCacheView(spread_view);
3327 image_view=DestroyCacheView(image_view);
3328 random_info=DestroyRandomInfoThreadSet(random_info);
3329 if (status == MagickFalse)
3330 spread_image=DestroyImage(spread_image);
3331 return(spread_image);
3335 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3339 % U n s h a r p M a s k I m a g e %
3343 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3345 % UnsharpMaskImage() sharpens one or more image channels. We convolve the
3346 % image with a Gaussian operator of the given radius and standard deviation
3347 % (sigma). For reasonable results, radius should be larger than sigma. Use a
3348 % radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
3350 % The format of the UnsharpMaskImage method is:
3352 % Image *UnsharpMaskImage(const Image *image,const double radius,
3353 % const double sigma,const double amount,const double threshold,
3354 % ExceptionInfo *exception)
3356 % A description of each parameter follows:
3358 % o image: the image.
3360 % o radius: the radius of the Gaussian, in pixels, not counting the center
3363 % o sigma: the standard deviation of the Gaussian, in pixels.
3365 % o gain: the percentage of the difference between the original and the
3366 % blur image that is added back into the original.
3368 % o threshold: the threshold in pixels needed to apply the diffence gain.
3370 % o exception: return any errors or warnings in this structure.
3373 MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
3374 const double sigma,const double gain,const double threshold,
3375 ExceptionInfo *exception)
3377 #define SharpenImageTag "Sharpen/Image"
3398 assert(image != (const Image *) NULL);
3399 assert(image->signature == MagickSignature);
3400 if (image->debug != MagickFalse)
3401 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3402 assert(exception != (ExceptionInfo *) NULL);
3403 unsharp_image=BlurImage(image,radius,sigma,exception);
3404 if (unsharp_image == (Image *) NULL)
3405 return((Image *) NULL);
3406 quantum_threshold=(double) QuantumRange*threshold;
3412 image_view=AcquireVirtualCacheView(image,exception);
3413 unsharp_view=AcquireAuthenticCacheView(unsharp_image,exception);
3414 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3415 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3416 magick_threads(image,unsharp_image,image->rows,1)
3418 for (y=0; y < (ssize_t) image->rows; y++)
3420 register const Quantum
3429 if (status == MagickFalse)
3431 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3432 q=QueueCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
3434 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3439 for (x=0; x < (ssize_t) image->columns; x++)
3444 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3456 channel=GetPixelChannelChannel(image,i);
3457 traits=GetPixelChannelTraits(image,channel);
3458 unsharp_traits=GetPixelChannelTraits(unsharp_image,channel);
3459 if ((traits == UndefinedPixelTrait) ||
3460 (unsharp_traits == UndefinedPixelTrait))
3462 if (((unsharp_traits & CopyPixelTrait) != 0) ||
3463 (GetPixelReadMask(image,p) != 0))
3465 SetPixelChannel(unsharp_image,channel,p[i],q);
3468 pixel=p[i]-(double) GetPixelChannel(unsharp_image,channel,q);
3469 if (fabs(2.0*pixel) < quantum_threshold)
3470 pixel=(double) p[i];
3472 pixel=(double) p[i]+gain*pixel;
3473 SetPixelChannel(unsharp_image,channel,ClampToQuantum(pixel),q);
3475 p+=GetPixelChannels(image);
3476 q+=GetPixelChannels(unsharp_image);
3478 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
3480 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3485 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3486 #pragma omp critical (MagickCore_UnsharpMaskImage)
3488 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
3489 if (proceed == MagickFalse)
3493 unsharp_image->type=image->type;
3494 unsharp_view=DestroyCacheView(unsharp_view);
3495 image_view=DestroyCacheView(image_view);
3496 if (status == MagickFalse)
3497 unsharp_image=DestroyImage(unsharp_image);
3498 return(unsharp_image);