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-2012 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/paint.h"
72 #include "MagickCore/pixel-accessor.h"
73 #include "MagickCore/pixel-private.h"
74 #include "MagickCore/property.h"
75 #include "MagickCore/quantize.h"
76 #include "MagickCore/quantum.h"
77 #include "MagickCore/quantum-private.h"
78 #include "MagickCore/random_.h"
79 #include "MagickCore/random-private.h"
80 #include "MagickCore/resample.h"
81 #include "MagickCore/resample-private.h"
82 #include "MagickCore/resize.h"
83 #include "MagickCore/resource_.h"
84 #include "MagickCore/segment.h"
85 #include "MagickCore/shear.h"
86 #include "MagickCore/signature-private.h"
87 #include "MagickCore/statistic.h"
88 #include "MagickCore/string_.h"
89 #include "MagickCore/thread-private.h"
90 #include "MagickCore/transform.h"
91 #include "MagickCore/threshold.h"
94 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98 % A d a p t i v e B l u r I m a g e %
102 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104 % AdaptiveBlurImage() adaptively blurs the image by blurring less
105 % intensely near image edges and more intensely far from edges. We blur the
106 % image with a Gaussian operator of the given radius and standard deviation
107 % (sigma). For reasonable results, radius should be larger than sigma. Use a
108 % radius of 0 and AdaptiveBlurImage() selects a suitable radius for you.
110 % The format of the AdaptiveBlurImage method is:
112 % Image *AdaptiveBlurImage(const Image *image,const double radius,
113 % const double sigma,ExceptionInfo *exception)
115 % A description of each parameter follows:
117 % o image: the image.
119 % o radius: the radius of the Gaussian, in pixels, not counting the center
122 % o sigma: the standard deviation of the Laplacian, in pixels.
124 % o exception: return any errors or warnings in this structure.
128 MagickExport MagickBooleanType AdaptiveLevelImage(Image *image,
129 const char *levels,ExceptionInfo *exception)
148 if (levels == (char *) NULL)
150 flags=ParseGeometry(levels,&geometry_info);
151 black_point=geometry_info.rho;
152 white_point=(double) QuantumRange;
153 if ((flags & SigmaValue) != 0)
154 white_point=geometry_info.sigma;
156 if ((flags & XiValue) != 0)
157 gamma=geometry_info.xi;
158 if ((flags & PercentValue) != 0)
160 black_point*=(double) image->columns*image->rows/100.0;
161 white_point*=(double) image->columns*image->rows/100.0;
163 if ((flags & SigmaValue) == 0)
164 white_point=(double) QuantumRange-black_point;
165 if ((flags & AspectValue ) == 0)
166 status=LevelImage(image,black_point,white_point,gamma,exception);
168 status=LevelizeImage(image,black_point,white_point,gamma,exception);
172 MagickExport Image *AdaptiveBlurImage(const Image *image,const double radius,
173 const double sigma,ExceptionInfo *exception)
175 #define AdaptiveBlurImageTag "Convolve/Image"
176 #define MagickSigma (fabs(sigma) < MagickEpsilon ? MagickEpsilon : sigma)
211 assert(image != (const Image *) NULL);
212 assert(image->signature == MagickSignature);
213 if (image->debug != MagickFalse)
214 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
215 assert(exception != (ExceptionInfo *) NULL);
216 assert(exception->signature == MagickSignature);
217 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
218 if (blur_image == (Image *) NULL)
219 return((Image *) NULL);
220 if (fabs(sigma) < MagickEpsilon)
222 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
224 blur_image=DestroyImage(blur_image);
225 return((Image *) NULL);
228 Edge detect the image brighness channel, level, blur, and level again.
230 edge_image=EdgeImage(image,radius,sigma,exception);
231 if (edge_image == (Image *) NULL)
233 blur_image=DestroyImage(blur_image);
234 return((Image *) NULL);
236 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
237 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
238 if (gaussian_image != (Image *) NULL)
240 edge_image=DestroyImage(edge_image);
241 edge_image=gaussian_image;
243 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
245 Create a set of kernels from maximum (radius,sigma) to minimum.
247 width=GetOptimalKernelWidth2D(radius,sigma);
248 kernel=(MagickRealType **) MagickAssumeAligned(AcquireAlignedMemory((size_t)
249 width,sizeof(*kernel)));
250 if (kernel == (MagickRealType **) NULL)
252 edge_image=DestroyImage(edge_image);
253 blur_image=DestroyImage(blur_image);
254 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
256 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
257 for (i=0; i < (ssize_t) width; i+=2)
259 kernel[i]=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory(
260 (size_t) (width-i),(width-i)*sizeof(**kernel)));
261 if (kernel[i] == (MagickRealType *) NULL)
264 j=(ssize_t) (width-i)/2;
266 for (v=(-j); v <= j; v++)
268 for (u=(-j); u <= j; u++)
270 kernel[i][k]=(MagickRealType) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
271 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
272 normalize+=kernel[i][k];
276 if (fabs(normalize) < MagickEpsilon)
277 normalize=MagickEpsilon;
278 normalize=MagickEpsilonReciprocal(normalize);
279 for (k=0; k < (j*j); k++)
280 kernel[i][k]=normalize*kernel[i][k];
282 if (i < (ssize_t) width)
284 for (i-=2; i >= 0; i-=2)
285 kernel[i]=(MagickRealType *) RelinquishAlignedMemory(kernel[i]);
286 kernel=(MagickRealType **) RelinquishAlignedMemory(kernel);
287 edge_image=DestroyImage(edge_image);
288 blur_image=DestroyImage(blur_image);
289 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
292 Adaptively blur image.
296 image_view=AcquireVirtualCacheView(image,exception);
297 edge_view=AcquireVirtualCacheView(edge_image,exception);
298 blur_view=AcquireAuthenticCacheView(blur_image,exception);
299 #if defined(MAGICKCORE_OPENMP_SUPPORT)
300 #pragma omp parallel for schedule(static,4) shared(progress,status) \
301 dynamic_number_threads(image,image->columns,image->rows,1)
303 for (y=0; y < (ssize_t) blur_image->rows; y++)
305 register const Quantum
314 if (status == MagickFalse)
316 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
317 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
319 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
324 for (x=0; x < (ssize_t) blur_image->columns; x++)
326 register const Quantum
336 j=(ssize_t) ceil((double) width*QuantumScale*
337 GetPixelIntensity(edge_image,r)-0.5);
341 if (j > (ssize_t) width)
345 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
346 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
347 if (p == (const Quantum *) NULL)
349 center=(ssize_t) GetPixelChannels(image)*(width-j)*((width-j)/2L)+
350 GetPixelChannels(image)*((width-j)/2L);
351 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
365 register const MagickRealType
368 register const Quantum
377 channel=GetPixelChannelChannel(image,i);
378 traits=GetPixelChannelTraits(image,channel);
379 blur_traits=GetPixelChannelTraits(blur_image,channel);
380 if ((traits == UndefinedPixelTrait) ||
381 (blur_traits == UndefinedPixelTrait))
383 if (((blur_traits & CopyPixelTrait) != 0) ||
384 (GetPixelMask(image,p) != 0))
386 SetPixelChannel(blur_image,channel,p[center+i],q);
393 if ((blur_traits & BlendPixelTrait) == 0)
398 for (v=0; v < (ssize_t) (width-j); v++)
400 for (u=0; u < (ssize_t) (width-j); u++)
402 pixel+=(*k)*pixels[i];
405 pixels+=GetPixelChannels(image);
408 gamma=MagickEpsilonReciprocal(gamma);
409 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
415 for (v=0; v < (ssize_t) (width-j); v++)
417 for (u=0; u < (ssize_t) (width-j); u++)
419 alpha=(double) (QuantumScale*GetPixelAlpha(image,pixels));
420 pixel+=(*k)*alpha*pixels[i];
423 pixels+=GetPixelChannels(image);
426 gamma=MagickEpsilonReciprocal(gamma);
427 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
429 q+=GetPixelChannels(blur_image);
430 r+=GetPixelChannels(edge_image);
432 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
434 if (image->progress_monitor != (MagickProgressMonitor) NULL)
439 #if defined(MAGICKCORE_OPENMP_SUPPORT)
440 #pragma omp critical (MagickCore_AdaptiveBlurImage)
442 proceed=SetImageProgress(image,AdaptiveBlurImageTag,progress++,
444 if (proceed == MagickFalse)
448 blur_image->type=image->type;
449 blur_view=DestroyCacheView(blur_view);
450 edge_view=DestroyCacheView(edge_view);
451 image_view=DestroyCacheView(image_view);
452 edge_image=DestroyImage(edge_image);
453 for (i=0; i < (ssize_t) width; i+=2)
454 kernel[i]=(MagickRealType *) RelinquishAlignedMemory(kernel[i]);
455 kernel=(MagickRealType **) RelinquishAlignedMemory(kernel);
456 if (status == MagickFalse)
457 blur_image=DestroyImage(blur_image);
462 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
466 % A d a p t i v e S h a r p e n I m a g e %
470 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
472 % AdaptiveSharpenImage() adaptively sharpens the image by sharpening more
473 % intensely near image edges and less intensely far from edges. We sharpen the
474 % image with a Gaussian operator of the given radius and standard deviation
475 % (sigma). For reasonable results, radius should be larger than sigma. Use a
476 % radius of 0 and AdaptiveSharpenImage() selects a suitable radius for you.
478 % The format of the AdaptiveSharpenImage method is:
480 % Image *AdaptiveSharpenImage(const Image *image,const double radius,
481 % const double sigma,ExceptionInfo *exception)
483 % A description of each parameter follows:
485 % o image: the image.
487 % o radius: the radius of the Gaussian, in pixels, not counting the center
490 % o sigma: the standard deviation of the Laplacian, in pixels.
492 % o exception: return any errors or warnings in this structure.
495 MagickExport Image *AdaptiveSharpenImage(const Image *image,const double radius,
496 const double sigma,ExceptionInfo *exception)
498 #define AdaptiveSharpenImageTag "Convolve/Image"
499 #define MagickSigma (fabs(sigma) < MagickEpsilon ? MagickEpsilon : sigma)
534 assert(image != (const Image *) NULL);
535 assert(image->signature == MagickSignature);
536 if (image->debug != MagickFalse)
537 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
538 assert(exception != (ExceptionInfo *) NULL);
539 assert(exception->signature == MagickSignature);
540 sharp_image=CloneImage(image,0,0,MagickTrue,exception);
541 if (sharp_image == (Image *) NULL)
542 return((Image *) NULL);
543 if (fabs(sigma) < MagickEpsilon)
545 if (SetImageStorageClass(sharp_image,DirectClass,exception) == MagickFalse)
547 sharp_image=DestroyImage(sharp_image);
548 return((Image *) NULL);
551 Edge detect the image brighness channel, level, sharp, and level again.
553 edge_image=EdgeImage(image,radius,sigma,exception);
554 if (edge_image == (Image *) NULL)
556 sharp_image=DestroyImage(sharp_image);
557 return((Image *) NULL);
559 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
560 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
561 if (gaussian_image != (Image *) NULL)
563 edge_image=DestroyImage(edge_image);
564 edge_image=gaussian_image;
566 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
568 Create a set of kernels from maximum (radius,sigma) to minimum.
570 width=GetOptimalKernelWidth2D(radius,sigma);
571 kernel=(MagickRealType **) MagickAssumeAligned(AcquireAlignedMemory((size_t)
572 width,sizeof(*kernel)));
573 if (kernel == (MagickRealType **) NULL)
575 edge_image=DestroyImage(edge_image);
576 sharp_image=DestroyImage(sharp_image);
577 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
579 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
580 for (i=0; i < (ssize_t) width; i+=2)
582 kernel[i]=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory(
583 (size_t) (width-i),(width-i)*sizeof(**kernel)));
584 if (kernel[i] == (MagickRealType *) NULL)
587 j=(ssize_t) (width-i)/2;
589 for (v=(-j); v <= j; v++)
591 for (u=(-j); u <= j; u++)
593 kernel[i][k]=(MagickRealType) (-exp(-((double) u*u+v*v)/(2.0*
594 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
595 normalize+=kernel[i][k];
599 if (fabs(normalize) < MagickEpsilon)
600 normalize=MagickEpsilon;
601 normalize=MagickEpsilonReciprocal(normalize);
602 for (k=0; k < (j*j); k++)
603 kernel[i][k]=normalize*kernel[i][k];
605 if (i < (ssize_t) width)
607 for (i-=2; i >= 0; i-=2)
608 kernel[i]=(MagickRealType *) RelinquishAlignedMemory(kernel[i]);
609 kernel=(MagickRealType **) RelinquishAlignedMemory(kernel);
610 edge_image=DestroyImage(edge_image);
611 sharp_image=DestroyImage(sharp_image);
612 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
615 Adaptively sharpen image.
619 image_view=AcquireVirtualCacheView(image,exception);
620 edge_view=AcquireVirtualCacheView(edge_image,exception);
621 sharp_view=AcquireAuthenticCacheView(sharp_image,exception);
622 #if defined(MAGICKCORE_OPENMP_SUPPORT)
623 #pragma omp parallel for schedule(static,4) shared(progress,status) \
624 dynamic_number_threads(image,image->columns,image->rows,1)
626 for (y=0; y < (ssize_t) sharp_image->rows; y++)
628 register const Quantum
637 if (status == MagickFalse)
639 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
640 q=QueueCacheViewAuthenticPixels(sharp_view,0,y,sharp_image->columns,1,
642 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
647 for (x=0; x < (ssize_t) sharp_image->columns; x++)
649 register const Quantum
659 j=(ssize_t) ceil((double) width*QuantumScale*
660 GetPixelIntensity(edge_image,r)-0.5);
664 if (j > (ssize_t) width)
668 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
669 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
670 if (p == (const Quantum *) NULL)
672 center=(ssize_t) GetPixelChannels(image)*(width-j)*((width-j)/2L)+
673 GetPixelChannels(image)*((width-j)/2);
674 for (i=0; i < (ssize_t) GetPixelChannels(sharp_image); i++)
688 register const MagickRealType
691 register const Quantum
700 channel=GetPixelChannelChannel(image,i);
701 traits=GetPixelChannelTraits(image,channel);
702 sharp_traits=GetPixelChannelTraits(sharp_image,channel);
703 if ((traits == UndefinedPixelTrait) ||
704 (sharp_traits == UndefinedPixelTrait))
706 if (((sharp_traits & CopyPixelTrait) != 0) ||
707 (GetPixelMask(image,p) != 0))
709 SetPixelChannel(sharp_image,channel,p[center+i],q);
716 if ((sharp_traits & BlendPixelTrait) == 0)
721 for (v=0; v < (ssize_t) (width-j); v++)
723 for (u=0; u < (ssize_t) (width-j); u++)
725 pixel+=(*k)*pixels[i];
728 pixels+=GetPixelChannels(image);
731 gamma=MagickEpsilonReciprocal(gamma);
732 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
738 for (v=0; v < (ssize_t) (width-j); v++)
740 for (u=0; u < (ssize_t) (width-j); u++)
742 alpha=(double) (QuantumScale*GetPixelAlpha(image,pixels));
743 pixel+=(*k)*alpha*pixels[i];
746 pixels+=GetPixelChannels(image);
749 gamma=MagickEpsilonReciprocal(gamma);
750 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
752 q+=GetPixelChannels(sharp_image);
753 r+=GetPixelChannels(edge_image);
755 if (SyncCacheViewAuthenticPixels(sharp_view,exception) == MagickFalse)
757 if (image->progress_monitor != (MagickProgressMonitor) NULL)
762 #if defined(MAGICKCORE_OPENMP_SUPPORT)
763 #pragma omp critical (MagickCore_AdaptiveSharpenImage)
765 proceed=SetImageProgress(image,AdaptiveSharpenImageTag,progress++,
767 if (proceed == MagickFalse)
771 sharp_image->type=image->type;
772 sharp_view=DestroyCacheView(sharp_view);
773 edge_view=DestroyCacheView(edge_view);
774 image_view=DestroyCacheView(image_view);
775 edge_image=DestroyImage(edge_image);
776 for (i=0; i < (ssize_t) width; i+=2)
777 kernel[i]=(MagickRealType *) RelinquishAlignedMemory(kernel[i]);
778 kernel=(MagickRealType **) RelinquishAlignedMemory(kernel);
779 if (status == MagickFalse)
780 sharp_image=DestroyImage(sharp_image);
785 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
789 % B l u r I m a g e %
793 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
795 % BlurImage() blurs an image. We convolve the image with a Gaussian operator
796 % of the given radius and standard deviation (sigma). For reasonable results,
797 % the radius should be larger than sigma. Use a radius of 0 and BlurImage()
798 % selects a suitable radius for you.
800 % BlurImage() differs from GaussianBlurImage() in that it uses a separable
801 % kernel which is faster but mathematically equivalent to the non-separable
804 % The format of the BlurImage method is:
806 % Image *BlurImage(const Image *image,const double radius,
807 % const double sigma,ExceptionInfo *exception)
809 % A description of each parameter follows:
811 % o image: the image.
813 % o radius: the radius of the Gaussian, in pixels, not counting the center
816 % o sigma: the standard deviation of the Gaussian, in pixels.
818 % o exception: return any errors or warnings in this structure.
822 static MagickRealType *GetBlurKernel(const size_t width,const double sigma)
836 Generate a 1-D convolution kernel.
838 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
839 kernel=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory((size_t)
840 width,sizeof(*kernel)));
841 if (kernel == (MagickRealType *) NULL)
846 for (k=(-j); k <= j; k++)
848 kernel[i]=(MagickRealType) (exp(-((double) k*k)/(2.0*MagickSigma*
849 MagickSigma))/(MagickSQ2PI*MagickSigma));
850 normalize+=kernel[i];
853 for (i=0; i < (ssize_t) width; i++)
854 kernel[i]/=normalize;
858 MagickExport Image *BlurImage(const Image *image,const double radius,
859 const double sigma,ExceptionInfo *exception)
861 #define BlurImageTag "Blur/Image"
891 Initialize blur image attributes.
893 assert(image != (Image *) NULL);
894 assert(image->signature == MagickSignature);
895 if (image->debug != MagickFalse)
896 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
897 assert(exception != (ExceptionInfo *) NULL);
898 assert(exception->signature == MagickSignature);
899 blur_image=CloneImage(image,0,0,MagickTrue,exception);
900 if (blur_image == (Image *) NULL)
901 return((Image *) NULL);
902 if (fabs(sigma) < MagickEpsilon)
904 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
906 blur_image=DestroyImage(blur_image);
907 return((Image *) NULL);
909 width=GetOptimalKernelWidth1D(radius,sigma);
910 kernel=GetBlurKernel(width,sigma);
911 if (kernel == (MagickRealType *) NULL)
913 blur_image=DestroyImage(blur_image);
914 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
916 if (image->debug != MagickFalse)
919 format[MaxTextExtent],
922 register const MagickRealType
925 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
926 " blur image with kernel width %.20g:",(double) width);
927 message=AcquireString("");
929 for (i=0; i < (ssize_t) width; i++)
932 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) i);
933 (void) ConcatenateString(&message,format);
934 (void) FormatLocaleString(format,MaxTextExtent,"%g ",(double) *k++);
935 (void) ConcatenateString(&message,format);
936 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
938 message=DestroyString(message);
945 center=(ssize_t) GetPixelChannels(image)*(width/2L);
946 image_view=AcquireVirtualCacheView(image,exception);
947 blur_view=AcquireAuthenticCacheView(blur_image,exception);
948 #if defined(MAGICKCORE_OPENMP_SUPPORT)
949 #pragma omp parallel for schedule(static,4) shared(progress,status) \
950 dynamic_number_threads(image,image->columns,image->rows,1)
952 for (y=0; y < (ssize_t) image->rows; y++)
954 register const Quantum
963 if (status == MagickFalse)
965 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y,
966 image->columns+width,1,exception);
967 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
969 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
974 for (x=0; x < (ssize_t) image->columns; x++)
979 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
993 register const MagickRealType
996 register const Quantum
1002 channel=GetPixelChannelChannel(image,i);
1003 traits=GetPixelChannelTraits(image,channel);
1004 blur_traits=GetPixelChannelTraits(blur_image,channel);
1005 if ((traits == UndefinedPixelTrait) ||
1006 (blur_traits == UndefinedPixelTrait))
1008 if (((blur_traits & CopyPixelTrait) != 0) ||
1009 (GetPixelMask(image,p) != 0))
1011 SetPixelChannel(blur_image,channel,p[center+i],q);
1017 if ((blur_traits & BlendPixelTrait) == 0)
1022 for (u=0; u < (ssize_t) width; u++)
1024 pixel+=(*k)*pixels[i];
1026 pixels+=GetPixelChannels(image);
1028 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
1035 for (u=0; u < (ssize_t) width; u++)
1037 alpha=(double) (QuantumScale*GetPixelAlpha(image,pixels));
1038 pixel+=(*k)*alpha*pixels[i];
1041 pixels+=GetPixelChannels(image);
1043 gamma=MagickEpsilonReciprocal(gamma);
1044 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
1046 p+=GetPixelChannels(image);
1047 q+=GetPixelChannels(blur_image);
1049 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1051 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1056 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1057 #pragma omp critical (MagickCore_BlurImage)
1059 proceed=SetImageProgress(image,BlurImageTag,progress++,blur_image->rows+
1060 blur_image->columns);
1061 if (proceed == MagickFalse)
1065 blur_view=DestroyCacheView(blur_view);
1066 image_view=DestroyCacheView(image_view);
1070 center=(ssize_t) GetPixelChannels(blur_image)*(width/2L);
1071 image_view=AcquireVirtualCacheView(blur_image,exception);
1072 blur_view=AcquireAuthenticCacheView(blur_image,exception);
1073 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1074 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1075 dynamic_number_threads(image,image->columns,image->rows,1)
1077 for (x=0; x < (ssize_t) blur_image->columns; x++)
1079 register const Quantum
1088 if (status == MagickFalse)
1090 p=GetCacheViewVirtualPixels(image_view,x,-((ssize_t) width/2L),1,
1091 blur_image->rows+width,exception);
1092 q=GetCacheViewAuthenticPixels(blur_view,x,0,1,blur_image->rows,exception);
1093 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1098 for (y=0; y < (ssize_t) blur_image->rows; y++)
1103 for (i=0; i < (ssize_t) GetPixelChannels(blur_image); i++)
1117 register const MagickRealType
1120 register const Quantum
1126 channel=GetPixelChannelChannel(blur_image,i);
1127 traits=GetPixelChannelTraits(blur_image,channel);
1128 blur_traits=GetPixelChannelTraits(blur_image,channel);
1129 if ((traits == UndefinedPixelTrait) ||
1130 (blur_traits == UndefinedPixelTrait))
1132 if (((blur_traits & CopyPixelTrait) != 0) ||
1133 (GetPixelMask(image,p) != 0))
1135 SetPixelChannel(blur_image,channel,p[center+i],q);
1141 if ((blur_traits & BlendPixelTrait) == 0)
1146 for (u=0; u < (ssize_t) width; u++)
1148 pixel+=(*k)*pixels[i];
1150 pixels+=GetPixelChannels(blur_image);
1152 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
1159 for (u=0; u < (ssize_t) width; u++)
1161 alpha=(double) (QuantumScale*GetPixelAlpha(blur_image,
1163 pixel+=(*k)*alpha*pixels[i];
1166 pixels+=GetPixelChannels(blur_image);
1168 gamma=MagickEpsilonReciprocal(gamma);
1169 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
1171 p+=GetPixelChannels(blur_image);
1172 q+=GetPixelChannels(blur_image);
1174 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1176 if (blur_image->progress_monitor != (MagickProgressMonitor) NULL)
1181 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1182 #pragma omp critical (MagickCore_BlurImage)
1184 proceed=SetImageProgress(blur_image,BlurImageTag,progress++,
1185 blur_image->rows+blur_image->columns);
1186 if (proceed == MagickFalse)
1190 blur_view=DestroyCacheView(blur_view);
1191 image_view=DestroyCacheView(image_view);
1192 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
1193 blur_image->type=image->type;
1194 if (status == MagickFalse)
1195 blur_image=DestroyImage(blur_image);
1200 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1204 % C o n v o l v e I m a g e %
1208 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1210 % ConvolveImage() applies a custom convolution kernel to the image.
1212 % The format of the ConvolveImage method is:
1214 % Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
1215 % ExceptionInfo *exception)
1217 % A description of each parameter follows:
1219 % o image: the image.
1221 % o kernel: the filtering kernel.
1223 % o exception: return any errors or warnings in this structure.
1226 MagickExport Image *ConvolveImage(const Image *image,
1227 const KernelInfo *kernel_info,ExceptionInfo *exception)
1229 return(MorphologyImage(image,CorrelateMorphology,1,kernel_info,exception));
1233 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1237 % D e s p e c k l e I m a g e %
1241 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1243 % DespeckleImage() reduces the speckle noise in an image while perserving the
1244 % edges of the original image. A speckle removing filter uses a complementary % hulling technique (raising pixels that are darker than their surrounding
1245 % neighbors, then complementarily lowering pixels that are brighter than their
1246 % surrounding neighbors) to reduce the speckle index of that image (reference
1247 % Crimmins speckle removal).
1249 % The format of the DespeckleImage method is:
1251 % Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1253 % A description of each parameter follows:
1255 % o image: the image.
1257 % o exception: return any errors or warnings in this structure.
1261 static void Hull(const Image *image,const ssize_t x_offset,
1262 const ssize_t y_offset,const size_t columns,const size_t rows,
1263 const int polarity,Quantum *restrict f,Quantum *restrict g)
1274 assert(f != (Quantum *) NULL);
1275 assert(g != (Quantum *) NULL);
1278 r=p+(y_offset*(columns+2)+x_offset);
1279 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1280 #pragma omp parallel for schedule(static) \
1281 dynamic_number_threads(image,columns,rows,1)
1283 for (y=0; y < (ssize_t) rows; y++)
1292 i=(2*y+1)+y*columns;
1294 for (x=0; x < (ssize_t) columns; x++)
1296 v=(SignedQuantum) p[i];
1297 if ((SignedQuantum) r[i] >= (v+ScaleCharToQuantum(2)))
1298 v+=ScaleCharToQuantum(1);
1303 for (x=0; x < (ssize_t) columns; x++)
1305 v=(SignedQuantum) p[i];
1306 if ((SignedQuantum) r[i] <= (v-ScaleCharToQuantum(2)))
1307 v-=ScaleCharToQuantum(1);
1314 r=q+(y_offset*(columns+2)+x_offset);
1315 s=q-(y_offset*(columns+2)+x_offset);
1316 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1317 #pragma omp parallel for schedule(static) \
1318 dynamic_number_threads(image,columns,rows,1)
1320 for (y=0; y < (ssize_t) rows; y++)
1329 i=(2*y+1)+y*columns;
1331 for (x=0; x < (ssize_t) columns; x++)
1333 v=(SignedQuantum) q[i];
1334 if (((SignedQuantum) s[i] >= (v+ScaleCharToQuantum(2))) &&
1335 ((SignedQuantum) r[i] > v))
1336 v+=ScaleCharToQuantum(1);
1341 for (x=0; x < (ssize_t) columns; x++)
1343 v=(SignedQuantum) q[i];
1344 if (((SignedQuantum) s[i] <= (v-ScaleCharToQuantum(2))) &&
1345 ((SignedQuantum) r[i] < v))
1346 v-=ScaleCharToQuantum(1);
1353 MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1355 #define DespeckleImageTag "Despeckle/Image"
1377 static const ssize_t
1378 X[4] = {0, 1, 1,-1},
1379 Y[4] = {1, 0, 1, 1};
1382 Allocate despeckled image.
1384 assert(image != (const Image *) NULL);
1385 assert(image->signature == MagickSignature);
1386 if (image->debug != MagickFalse)
1387 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1388 assert(exception != (ExceptionInfo *) NULL);
1389 assert(exception->signature == MagickSignature);
1390 despeckle_image=CloneImage(image,0,0,MagickTrue,exception);
1391 if (despeckle_image == (Image *) NULL)
1392 return((Image *) NULL);
1393 status=SetImageStorageClass(despeckle_image,DirectClass,exception);
1394 if (status == MagickFalse)
1396 despeckle_image=DestroyImage(despeckle_image);
1397 return((Image *) NULL);
1400 Allocate image buffer.
1402 length=(size_t) ((image->columns+2)*(image->rows+2));
1403 pixels=(Quantum *) AcquireQuantumMemory(length,sizeof(*pixels));
1404 buffer=(Quantum *) AcquireQuantumMemory(length,sizeof(*buffer));
1405 if ((pixels == (Quantum *) NULL) || (buffer == (Quantum *) NULL))
1407 if (buffer != (Quantum *) NULL)
1408 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1409 if (pixels != (Quantum *) NULL)
1410 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1411 despeckle_image=DestroyImage(despeckle_image);
1412 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1415 Reduce speckle in the image.
1418 image_view=AcquireVirtualCacheView(image,exception);
1419 despeckle_view=AcquireAuthenticCacheView(despeckle_image,exception);
1420 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1437 if (status == MagickFalse)
1439 channel=GetPixelChannelChannel(image,i);
1440 traits=GetPixelChannelTraits(image,channel);
1441 despeckle_traits=GetPixelChannelTraits(despeckle_image,channel);
1442 if ((traits == UndefinedPixelTrait) ||
1443 (despeckle_traits == UndefinedPixelTrait))
1445 if ((despeckle_traits & CopyPixelTrait) != 0)
1447 (void) ResetMagickMemory(pixels,0,length*sizeof(*pixels));
1448 j=(ssize_t) image->columns+2;
1449 for (y=0; y < (ssize_t) image->rows; y++)
1451 register const Quantum
1454 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1455 if (p == (const Quantum *) NULL)
1461 for (x=0; x < (ssize_t) image->columns; x++)
1464 p+=GetPixelChannels(image);
1468 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
1469 for (k=0; k < 4; k++)
1471 Hull(image,X[k],Y[k],image->columns,image->rows,1,pixels,buffer);
1472 Hull(image,-X[k],-Y[k],image->columns,image->rows,1,pixels,buffer);
1473 Hull(image,-X[k],-Y[k],image->columns,image->rows,-1,pixels,buffer);
1474 Hull(image,X[k],Y[k],image->columns,image->rows,-1,pixels,buffer);
1476 j=(ssize_t) image->columns+2;
1477 for (y=0; y < (ssize_t) image->rows; y++)
1485 q=QueueCacheViewAuthenticPixels(despeckle_view,0,y,
1486 despeckle_image->columns,1,exception);
1487 if (q == (Quantum *) NULL)
1493 for (x=0; x < (ssize_t) image->columns; x++)
1495 SetPixelChannel(despeckle_image,channel,pixels[j++],q);
1496 q+=GetPixelChannels(despeckle_image);
1498 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1499 if (sync == MagickFalse)
1503 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1508 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1509 GetPixelChannels(image));
1510 if (proceed == MagickFalse)
1514 despeckle_view=DestroyCacheView(despeckle_view);
1515 image_view=DestroyCacheView(image_view);
1516 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1517 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1518 despeckle_image->type=image->type;
1519 if (status == MagickFalse)
1520 despeckle_image=DestroyImage(despeckle_image);
1521 return(despeckle_image);
1525 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1529 % E d g e I m a g e %
1533 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1535 % EdgeImage() finds edges in an image. Radius defines the radius of the
1536 % convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1539 % The format of the EdgeImage method is:
1541 % Image *EdgeImage(const Image *image,const double radius,
1542 % const double sigma,ExceptionInfo *exception)
1544 % A description of each parameter follows:
1546 % o image: the image.
1548 % o radius: the radius of the pixel neighborhood.
1550 % o sigma: the standard deviation of the Gaussian, in pixels.
1552 % o exception: return any errors or warnings in this structure.
1555 MagickExport Image *EdgeImage(const Image *image,const double radius,
1556 const double sigma,ExceptionInfo *exception)
1575 assert(image != (const Image *) NULL);
1576 assert(image->signature == MagickSignature);
1577 if (image->debug != MagickFalse)
1578 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1579 assert(exception != (ExceptionInfo *) NULL);
1580 assert(exception->signature == MagickSignature);
1581 width=GetOptimalKernelWidth1D(radius,sigma);
1582 kernel_info=AcquireKernelInfo((const char *) NULL);
1583 if (kernel_info == (KernelInfo *) NULL)
1584 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1585 kernel_info->width=width;
1586 kernel_info->height=width;
1587 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
1588 AcquireAlignedMemory(kernel_info->width,kernel_info->width*
1589 sizeof(*kernel_info->values)));
1590 if (kernel_info->values == (MagickRealType *) NULL)
1592 kernel_info=DestroyKernelInfo(kernel_info);
1593 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1595 j=(ssize_t) kernel_info->width/2;
1597 for (v=(-j); v <= j; v++)
1599 for (u=(-j); u <= j; u++)
1601 kernel_info->values[i]=(MagickRealType) (-1.0);
1605 kernel_info->values[i/2]=(MagickRealType) (width*width-1.0);
1606 edge_image=ConvolveImage(image,kernel_info,exception);
1607 if (edge_image != (Image *) NULL)
1608 (void) ClampImage(edge_image,exception);
1609 kernel_info=DestroyKernelInfo(kernel_info);
1614 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1618 % E m b o s s I m a g e %
1622 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1624 % EmbossImage() returns a grayscale image with a three-dimensional effect.
1625 % We convolve the image with a Gaussian operator of the given radius and
1626 % standard deviation (sigma). For reasonable results, radius should be
1627 % larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1630 % The format of the EmbossImage method is:
1632 % Image *EmbossImage(const Image *image,const double radius,
1633 % const double sigma,ExceptionInfo *exception)
1635 % A description of each parameter follows:
1637 % o image: the image.
1639 % o radius: the radius of the pixel neighborhood.
1641 % o sigma: the standard deviation of the Gaussian, in pixels.
1643 % o exception: return any errors or warnings in this structure.
1646 MagickExport Image *EmbossImage(const Image *image,const double radius,
1647 const double sigma,ExceptionInfo *exception)
1667 assert(image != (const Image *) NULL);
1668 assert(image->signature == MagickSignature);
1669 if (image->debug != MagickFalse)
1670 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1671 assert(exception != (ExceptionInfo *) NULL);
1672 assert(exception->signature == MagickSignature);
1673 width=GetOptimalKernelWidth1D(radius,sigma);
1674 kernel_info=AcquireKernelInfo((const char *) NULL);
1675 if (kernel_info == (KernelInfo *) NULL)
1676 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1677 kernel_info->width=width;
1678 kernel_info->height=width;
1679 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
1680 AcquireAlignedMemory(kernel_info->width,kernel_info->width*
1681 sizeof(*kernel_info->values)));
1682 if (kernel_info->values == (MagickRealType *) NULL)
1684 kernel_info=DestroyKernelInfo(kernel_info);
1685 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1687 j=(ssize_t) kernel_info->width/2;
1690 for (v=(-j); v <= j; v++)
1692 for (u=(-j); u <= j; u++)
1694 kernel_info->values[i]=(MagickRealType) (((u < 0) || (v < 0) ? -8.0 :
1695 8.0)*exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
1696 (2.0*MagickPI*MagickSigma*MagickSigma));
1698 kernel_info->values[i]=0.0;
1703 emboss_image=ConvolveImage(image,kernel_info,exception);
1704 kernel_info=DestroyKernelInfo(kernel_info);
1705 if (emboss_image != (Image *) NULL)
1706 (void) EqualizeImage(emboss_image,exception);
1707 return(emboss_image);
1711 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1715 % G a u s s i a n B l u r I m a g e %
1719 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1721 % GaussianBlurImage() blurs an image. We convolve the image with a
1722 % Gaussian operator of the given radius and standard deviation (sigma).
1723 % For reasonable results, the radius should be larger than sigma. Use a
1724 % radius of 0 and GaussianBlurImage() selects a suitable radius for you
1726 % The format of the GaussianBlurImage method is:
1728 % Image *GaussianBlurImage(const Image *image,onst double radius,
1729 % const double sigma,ExceptionInfo *exception)
1731 % A description of each parameter follows:
1733 % o image: the image.
1735 % o radius: the radius of the Gaussian, in pixels, not counting the center
1738 % o sigma: the standard deviation of the Gaussian, in pixels.
1740 % o exception: return any errors or warnings in this structure.
1743 MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
1744 const double sigma,ExceptionInfo *exception)
1763 assert(image != (const Image *) NULL);
1764 assert(image->signature == MagickSignature);
1765 if (image->debug != MagickFalse)
1766 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1767 assert(exception != (ExceptionInfo *) NULL);
1768 assert(exception->signature == MagickSignature);
1769 width=GetOptimalKernelWidth2D(radius,sigma);
1770 kernel_info=AcquireKernelInfo((const char *) NULL);
1771 if (kernel_info == (KernelInfo *) NULL)
1772 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1773 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1774 kernel_info->width=width;
1775 kernel_info->height=width;
1776 kernel_info->signature=MagickSignature;
1777 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
1778 AcquireAlignedMemory(kernel_info->width,kernel_info->width*
1779 sizeof(*kernel_info->values)));
1780 if (kernel_info->values == (MagickRealType *) NULL)
1782 kernel_info=DestroyKernelInfo(kernel_info);
1783 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1785 j=(ssize_t) kernel_info->width/2;
1787 for (v=(-j); v <= j; v++)
1789 for (u=(-j); u <= j; u++)
1791 kernel_info->values[i]=(MagickRealType) (exp(-((double) u*u+v*v)/(2.0*
1792 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
1796 blur_image=ConvolveImage(image,kernel_info,exception);
1797 kernel_info=DestroyKernelInfo(kernel_info);
1802 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1806 % M o t i o n B l u r I m a g e %
1810 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1812 % MotionBlurImage() simulates motion blur. We convolve the image with a
1813 % Gaussian operator of the given radius and standard deviation (sigma).
1814 % For reasonable results, radius should be larger than sigma. Use a
1815 % radius of 0 and MotionBlurImage() selects a suitable radius for you.
1816 % Angle gives the angle of the blurring motion.
1818 % Andrew Protano contributed this effect.
1820 % The format of the MotionBlurImage method is:
1822 % Image *MotionBlurImage(const Image *image,const double radius,
1823 % const double sigma,const double angle,ExceptionInfo *exception)
1825 % A description of each parameter follows:
1827 % o image: the image.
1829 % o radius: the radius of the Gaussian, in pixels, not counting
1832 % o sigma: the standard deviation of the Gaussian, in pixels.
1834 % o angle: Apply the effect along this angle.
1836 % o exception: return any errors or warnings in this structure.
1840 static MagickRealType *GetMotionBlurKernel(const size_t width,
1851 Generate a 1-D convolution kernel.
1853 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1854 kernel=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory((size_t)
1855 width,sizeof(*kernel)));
1856 if (kernel == (MagickRealType *) NULL)
1859 for (i=0; i < (ssize_t) width; i++)
1861 kernel[i]=(MagickRealType) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
1862 MagickSigma)))/(MagickSQ2PI*MagickSigma));
1863 normalize+=kernel[i];
1865 for (i=0; i < (ssize_t) width; i++)
1866 kernel[i]/=normalize;
1870 MagickExport Image *MotionBlurImage(const Image *image,const double radius,
1871 const double sigma,const double angle,ExceptionInfo *exception)
1905 assert(image != (Image *) NULL);
1906 assert(image->signature == MagickSignature);
1907 if (image->debug != MagickFalse)
1908 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1909 assert(exception != (ExceptionInfo *) NULL);
1910 width=GetOptimalKernelWidth1D(radius,sigma);
1911 kernel=GetMotionBlurKernel(width,sigma);
1912 if (kernel == (MagickRealType *) NULL)
1913 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1914 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
1915 if (offset == (OffsetInfo *) NULL)
1917 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
1918 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1920 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1921 if (blur_image == (Image *) NULL)
1923 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
1924 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1925 return((Image *) NULL);
1927 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
1929 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
1930 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1931 blur_image=DestroyImage(blur_image);
1932 return((Image *) NULL);
1934 point.x=(double) width*sin(DegreesToRadians(angle));
1935 point.y=(double) width*cos(DegreesToRadians(angle));
1936 for (i=0; i < (ssize_t) width; i++)
1938 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
1939 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
1946 image_view=AcquireVirtualCacheView(image,exception);
1947 motion_view=AcquireVirtualCacheView(image,exception);
1948 blur_view=AcquireAuthenticCacheView(blur_image,exception);
1949 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1950 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1951 dynamic_number_threads(image,image->columns,image->rows,1)
1953 for (y=0; y < (ssize_t) image->rows; y++)
1955 register const Quantum
1964 if (status == MagickFalse)
1966 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1967 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
1969 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1974 for (x=0; x < (ssize_t) image->columns; x++)
1979 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1993 register const Quantum
1996 register MagickRealType
2002 channel=GetPixelChannelChannel(image,i);
2003 traits=GetPixelChannelTraits(image,channel);
2004 blur_traits=GetPixelChannelTraits(blur_image,channel);
2005 if ((traits == UndefinedPixelTrait) ||
2006 (blur_traits == UndefinedPixelTrait))
2008 if (((blur_traits & CopyPixelTrait) != 0) ||
2009 (GetPixelMask(image,p) != 0))
2011 SetPixelChannel(blur_image,channel,p[i],q);
2016 if ((blur_traits & BlendPixelTrait) == 0)
2018 for (j=0; j < (ssize_t) width; j++)
2020 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+
2021 offset[j].y,1,1,exception);
2022 if (r == (const Quantum *) NULL)
2030 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
2035 for (j=0; j < (ssize_t) width; j++)
2037 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+offset[j].y,1,
2039 if (r == (const Quantum *) NULL)
2044 alpha=(double) (QuantumScale*GetPixelAlpha(image,r));
2045 pixel+=(*k)*alpha*r[i];
2049 gamma=MagickEpsilonReciprocal(gamma);
2050 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2052 p+=GetPixelChannels(image);
2053 q+=GetPixelChannels(blur_image);
2055 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2057 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2062 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2063 #pragma omp critical (MagickCore_MotionBlurImage)
2065 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2066 if (proceed == MagickFalse)
2070 blur_view=DestroyCacheView(blur_view);
2071 motion_view=DestroyCacheView(motion_view);
2072 image_view=DestroyCacheView(image_view);
2073 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2074 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2075 if (status == MagickFalse)
2076 blur_image=DestroyImage(blur_image);
2081 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2085 % P r e v i e w I m a g e %
2089 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2091 % PreviewImage() tiles 9 thumbnails of the specified image with an image
2092 % processing operation applied with varying parameters. This may be helpful
2093 % pin-pointing an appropriate parameter for a particular image processing
2096 % The format of the PreviewImages method is:
2098 % Image *PreviewImages(const Image *image,const PreviewType preview,
2099 % ExceptionInfo *exception)
2101 % A description of each parameter follows:
2103 % o image: the image.
2105 % o preview: the image processing operation.
2107 % o exception: return any errors or warnings in this structure.
2110 MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2111 ExceptionInfo *exception)
2113 #define NumberTiles 9
2114 #define PreviewImageTag "Preview/Image"
2115 #define DefaultPreviewGeometry "204x204+10+10"
2118 factor[MaxTextExtent],
2119 label[MaxTextExtent];
2164 Open output image file.
2166 assert(image != (Image *) NULL);
2167 assert(image->signature == MagickSignature);
2168 if (image->debug != MagickFalse)
2169 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2173 preview_info=AcquireImageInfo();
2174 SetGeometry(image,&geometry);
2175 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2176 &geometry.width,&geometry.height);
2177 images=NewImageList();
2179 GetQuantizeInfo(&quantize_info);
2185 for (i=0; i < NumberTiles; i++)
2187 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2188 if (thumbnail == (Image *) NULL)
2190 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2192 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel,exception);
2193 if (i == (NumberTiles/2))
2195 (void) QueryColorCompliance("#dfdfdf",AllCompliance,
2196 &thumbnail->matte_color,exception);
2197 AppendImageToList(&images,thumbnail);
2205 preview_image=RotateImage(thumbnail,degrees,exception);
2206 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
2212 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
2213 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",
2214 degrees,2.0*degrees);
2219 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2220 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
2221 preview_image=RollImage(thumbnail,x,y,exception);
2222 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
2223 (double) x,(double) y);
2228 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2229 if (preview_image == (Image *) NULL)
2231 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",
2233 (void) ModulateImage(preview_image,factor,exception);
2234 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2237 case SaturationPreview:
2239 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2240 if (preview_image == (Image *) NULL)
2242 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",
2244 (void) ModulateImage(preview_image,factor,exception);
2245 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2248 case BrightnessPreview:
2250 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2251 if (preview_image == (Image *) NULL)
2253 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
2254 (void) ModulateImage(preview_image,factor,exception);
2255 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2261 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2262 if (preview_image == (Image *) NULL)
2265 (void) GammaImage(preview_image,gamma,exception);
2266 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
2271 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2272 if (preview_image != (Image *) NULL)
2273 for (x=0; x < i; x++)
2274 (void) ContrastImage(preview_image,MagickTrue,exception);
2275 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
2281 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2282 if (preview_image == (Image *) NULL)
2284 for (x=0; x < i; x++)
2285 (void) ContrastImage(preview_image,MagickFalse,exception);
2286 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
2290 case GrayscalePreview:
2292 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2293 if (preview_image == (Image *) NULL)
2296 quantize_info.number_colors=colors;
2297 quantize_info.colorspace=GRAYColorspace;
2298 (void) QuantizeImage(&quantize_info,preview_image,exception);
2299 (void) FormatLocaleString(label,MaxTextExtent,
2300 "-colorspace gray -colors %.20g",(double) colors);
2303 case QuantizePreview:
2305 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2306 if (preview_image == (Image *) NULL)
2309 quantize_info.number_colors=colors;
2310 (void) QuantizeImage(&quantize_info,preview_image,exception);
2311 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
2315 case DespecklePreview:
2317 for (x=0; x < (i-1); x++)
2319 preview_image=DespeckleImage(thumbnail,exception);
2320 if (preview_image == (Image *) NULL)
2322 thumbnail=DestroyImage(thumbnail);
2323 thumbnail=preview_image;
2325 preview_image=DespeckleImage(thumbnail,exception);
2326 if (preview_image == (Image *) NULL)
2328 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
2332 case ReduceNoisePreview:
2334 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2335 (size_t) radius,exception);
2336 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
2339 case AddNoisePreview:
2345 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2350 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2355 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2360 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2365 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2370 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2375 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2379 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2380 (size_t) i,exception);
2381 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
2384 case SharpenPreview:
2386 preview_image=SharpenImage(thumbnail,radius,sigma,exception);
2387 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",
2393 preview_image=BlurImage(thumbnail,radius,sigma,exception);
2394 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
2398 case ThresholdPreview:
2400 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2401 if (preview_image == (Image *) NULL)
2403 (void) BilevelImage(thumbnail,(double) (percentage*((double)
2404 QuantumRange+1.0))/100.0,exception);
2405 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",
2406 (double) (percentage*((double) QuantumRange+1.0))/100.0);
2409 case EdgeDetectPreview:
2411 preview_image=EdgeImage(thumbnail,radius,sigma,exception);
2412 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
2417 preview_image=SpreadImage(thumbnail,radius,thumbnail->interpolate,
2419 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",
2423 case SolarizePreview:
2425 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2426 if (preview_image == (Image *) NULL)
2428 (void) SolarizeImage(preview_image,(double) QuantumRange*
2429 percentage/100.0,exception);
2430 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
2431 (QuantumRange*percentage)/100.0);
2437 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2439 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",
2445 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2446 if (preview_image == (Image *) NULL)
2448 geometry.width=(size_t) (2*i+2);
2449 geometry.height=(size_t) (2*i+2);
2452 (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
2453 (void) FormatLocaleString(label,MaxTextExtent,
2454 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
2455 geometry.height,(double) geometry.x,(double) geometry.y);
2458 case SegmentPreview:
2460 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2461 if (preview_image == (Image *) NULL)
2464 (void) SegmentImage(preview_image,sRGBColorspace,MagickFalse,threshold,
2465 threshold,exception);
2466 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
2467 threshold,threshold);
2472 preview_image=SwirlImage(thumbnail,degrees,image->interpolate,
2474 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
2478 case ImplodePreview:
2481 preview_image=ImplodeImage(thumbnail,degrees,image->interpolate,
2483 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
2489 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,
2490 image->interpolate,exception);
2491 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",
2492 0.5*degrees,2.0*degrees);
2495 case OilPaintPreview:
2497 preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2499 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2503 case CharcoalDrawingPreview:
2505 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
2507 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2514 filename[MaxTextExtent];
2522 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2523 if (preview_image == (Image *) NULL)
2525 preview_info->quality=(size_t) percentage;
2526 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
2527 preview_info->quality);
2528 file=AcquireUniqueFileResource(filename);
2531 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
2532 "jpeg:%s",filename);
2533 status=WriteImage(preview_info,preview_image,exception);
2534 if (status != MagickFalse)
2539 (void) CopyMagickString(preview_info->filename,
2540 preview_image->filename,MaxTextExtent);
2541 quality_image=ReadImage(preview_info,exception);
2542 if (quality_image != (Image *) NULL)
2544 preview_image=DestroyImage(preview_image);
2545 preview_image=quality_image;
2548 (void) RelinquishUniqueFileResource(preview_image->filename);
2549 if ((GetBlobSize(preview_image)/1024) >= 1024)
2550 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
2551 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2554 if (GetBlobSize(preview_image) >= 1024)
2555 (void) FormatLocaleString(label,MaxTextExtent,
2556 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
2557 GetBlobSize(preview_image))/1024.0);
2559 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
2560 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
2564 thumbnail=DestroyImage(thumbnail);
2568 if (preview_image == (Image *) NULL)
2570 (void) DeleteImageProperty(preview_image,"label");
2571 (void) SetImageProperty(preview_image,"label",label,exception);
2572 AppendImageToList(&images,preview_image);
2573 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2575 if (proceed == MagickFalse)
2578 if (images == (Image *) NULL)
2580 preview_info=DestroyImageInfo(preview_info);
2581 return((Image *) NULL);
2586 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2587 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2588 montage_info->shadow=MagickTrue;
2589 (void) CloneString(&montage_info->tile,"3x3");
2590 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2591 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2592 montage_image=MontageImages(images,montage_info,exception);
2593 montage_info=DestroyMontageInfo(montage_info);
2594 images=DestroyImageList(images);
2595 if (montage_image == (Image *) NULL)
2596 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2597 if (montage_image->montage != (char *) NULL)
2600 Free image directory.
2602 montage_image->montage=(char *) RelinquishMagickMemory(
2603 montage_image->montage);
2604 if (image->directory != (char *) NULL)
2605 montage_image->directory=(char *) RelinquishMagickMemory(
2606 montage_image->directory);
2608 preview_info=DestroyImageInfo(preview_info);
2609 return(montage_image);
2613 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2617 % R a d i a l B l u r I m a g e %
2621 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2623 % RadialBlurImage() applies a radial blur to the image.
2625 % Andrew Protano contributed this effect.
2627 % The format of the RadialBlurImage method is:
2629 % Image *RadialBlurImage(const Image *image,const double angle,
2630 % ExceptionInfo *exception)
2632 % A description of each parameter follows:
2634 % o image: the image.
2636 % o angle: the angle of the radial blur.
2640 % o exception: return any errors or warnings in this structure.
2643 MagickExport Image *RadialBlurImage(const Image *image,const double angle,
2644 ExceptionInfo *exception)
2680 Allocate blur image.
2682 assert(image != (Image *) NULL);
2683 assert(image->signature == MagickSignature);
2684 if (image->debug != MagickFalse)
2685 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2686 assert(exception != (ExceptionInfo *) NULL);
2687 assert(exception->signature == MagickSignature);
2688 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2689 if (blur_image == (Image *) NULL)
2690 return((Image *) NULL);
2691 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2693 blur_image=DestroyImage(blur_image);
2694 return((Image *) NULL);
2696 blur_center.x=(double) image->columns/2.0;
2697 blur_center.y=(double) image->rows/2.0;
2698 blur_radius=hypot(blur_center.x,blur_center.y);
2699 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
2700 theta=DegreesToRadians(angle)/(double) (n-1);
2701 cos_theta=(double *) AcquireQuantumMemory((size_t) n,
2702 sizeof(*cos_theta));
2703 sin_theta=(double *) AcquireQuantumMemory((size_t) n,
2704 sizeof(*sin_theta));
2705 if ((cos_theta == (double *) NULL) ||
2706 (sin_theta == (double *) NULL))
2708 blur_image=DestroyImage(blur_image);
2709 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2711 offset=theta*(double) (n-1)/2.0;
2712 for (i=0; i < (ssize_t) n; i++)
2714 cos_theta[i]=cos((double) (theta*i-offset));
2715 sin_theta[i]=sin((double) (theta*i-offset));
2722 image_view=AcquireVirtualCacheView(image,exception);
2723 radial_view=AcquireVirtualCacheView(image,exception);
2724 blur_view=AcquireAuthenticCacheView(blur_image,exception);
2725 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2726 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2727 dynamic_number_threads(image,image->columns,image->rows,1)
2729 for (y=0; y < (ssize_t) image->rows; y++)
2731 register const Quantum
2740 if (status == MagickFalse)
2742 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2743 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2745 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2750 for (x=0; x < (ssize_t) image->columns; x++)
2764 center.x=(double) x-blur_center.x;
2765 center.y=(double) y-blur_center.y;
2766 radius=hypot((double) center.x,center.y);
2771 step=(size_t) (blur_radius/radius);
2778 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2791 register const Quantum
2797 channel=GetPixelChannelChannel(image,i);
2798 traits=GetPixelChannelTraits(image,channel);
2799 blur_traits=GetPixelChannelTraits(blur_image,channel);
2800 if ((traits == UndefinedPixelTrait) ||
2801 (blur_traits == UndefinedPixelTrait))
2803 if (((blur_traits & CopyPixelTrait) != 0) ||
2804 (GetPixelMask(image,p) != 0))
2806 SetPixelChannel(blur_image,channel,p[i],q);
2811 if ((blur_traits & BlendPixelTrait) == 0)
2813 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2815 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2816 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2817 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2819 if (r == (const Quantum *) NULL)
2827 gamma=MagickEpsilonReciprocal(gamma);
2828 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2831 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2833 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2834 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2835 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2837 if (r == (const Quantum *) NULL)
2842 pixel+=GetPixelAlpha(image,r)*r[i];
2843 gamma+=GetPixelAlpha(image,r);
2845 gamma=MagickEpsilonReciprocal(gamma);
2846 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2848 p+=GetPixelChannels(image);
2849 q+=GetPixelChannels(blur_image);
2851 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2853 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2858 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2859 #pragma omp critical (MagickCore_RadialBlurImage)
2861 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2862 if (proceed == MagickFalse)
2866 blur_view=DestroyCacheView(blur_view);
2867 radial_view=DestroyCacheView(radial_view);
2868 image_view=DestroyCacheView(image_view);
2869 cos_theta=(double *) RelinquishMagickMemory(cos_theta);
2870 sin_theta=(double *) RelinquishMagickMemory(sin_theta);
2871 if (status == MagickFalse)
2872 blur_image=DestroyImage(blur_image);
2877 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2881 % S e l e c t i v e B l u r I m a g e %
2885 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2887 % SelectiveBlurImage() selectively blur pixels within a contrast threshold.
2888 % It is similar to the unsharpen mask that sharpens everything with contrast
2889 % above a certain threshold.
2891 % The format of the SelectiveBlurImage method is:
2893 % Image *SelectiveBlurImage(const Image *image,const double radius,
2894 % const double sigma,const double threshold,ExceptionInfo *exception)
2896 % A description of each parameter follows:
2898 % o image: the image.
2900 % o radius: the radius of the Gaussian, in pixels, not counting the center
2903 % o sigma: the standard deviation of the Gaussian, in pixels.
2905 % o threshold: only pixels within this contrast threshold are included
2906 % in the blur operation.
2908 % o exception: return any errors or warnings in this structure.
2911 MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
2912 const double sigma,const double threshold,ExceptionInfo *exception)
2914 #define SelectiveBlurImageTag "SelectiveBlur/Image"
2948 Initialize blur image attributes.
2950 assert(image != (Image *) NULL);
2951 assert(image->signature == MagickSignature);
2952 if (image->debug != MagickFalse)
2953 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2954 assert(exception != (ExceptionInfo *) NULL);
2955 assert(exception->signature == MagickSignature);
2956 width=GetOptimalKernelWidth1D(radius,sigma);
2957 kernel=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory((size_t)
2958 width,width*sizeof(*kernel)));
2959 if (kernel == (MagickRealType *) NULL)
2960 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2961 j=(ssize_t) width/2;
2963 for (v=(-j); v <= j; v++)
2965 for (u=(-j); u <= j; u++)
2966 kernel[i++]=(MagickRealType) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
2967 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
2969 if (image->debug != MagickFalse)
2972 format[MaxTextExtent],
2975 register const MagickRealType
2982 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
2983 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
2985 message=AcquireString("");
2987 for (v=0; v < (ssize_t) width; v++)
2990 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
2991 (void) ConcatenateString(&message,format);
2992 for (u=0; u < (ssize_t) width; u++)
2994 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",(double) *k++);
2995 (void) ConcatenateString(&message,format);
2997 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
2999 message=DestroyString(message);
3001 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3002 if (blur_image == (Image *) NULL)
3003 return((Image *) NULL);
3004 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
3006 blur_image=DestroyImage(blur_image);
3007 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3008 return((Image *) NULL);
3010 luminance_image=CloneImage(image,0,0,MagickTrue,exception);
3011 if (luminance_image == (Image *) NULL)
3013 blur_image=DestroyImage(blur_image);
3014 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3015 return((Image *) NULL);
3017 status=TransformImageColorspace(luminance_image,GRAYColorspace,exception);
3018 if (status == MagickFalse)
3020 luminance_image=DestroyImage(luminance_image);
3021 blur_image=DestroyImage(blur_image);
3022 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3023 return((Image *) NULL);
3026 Threshold blur image.
3030 center=(ssize_t) (GetPixelChannels(image)*(image->columns+width)*(width/2L)+
3031 GetPixelChannels(image)*(width/2L));
3032 image_view=AcquireVirtualCacheView(image,exception);
3033 luminance_view=AcquireVirtualCacheView(luminance_image,exception);
3034 blur_view=AcquireAuthenticCacheView(blur_image,exception);
3035 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3036 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3037 dynamic_number_threads(image,image->columns,image->rows,1)
3039 for (y=0; y < (ssize_t) image->rows; y++)
3047 register const Quantum
3057 if (status == MagickFalse)
3059 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
3060 (width/2L),image->columns+width,width,exception);
3061 l=GetCacheViewVirtualPixels(luminance_view,-((ssize_t) width/2L),y-(ssize_t)
3062 (width/2L),luminance_image->columns+width,width,exception);
3063 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3065 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3070 for (x=0; x < (ssize_t) image->columns; x++)
3078 intensity=GetPixelIntensity(image,p+center);
3079 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3093 register const MagickRealType
3096 register const Quantum
3097 *restrict luminance_pixels,
3106 channel=GetPixelChannelChannel(image,i);
3107 traits=GetPixelChannelTraits(image,channel);
3108 blur_traits=GetPixelChannelTraits(blur_image,channel);
3109 if ((traits == UndefinedPixelTrait) ||
3110 (blur_traits == UndefinedPixelTrait))
3112 if (((blur_traits & CopyPixelTrait) != 0) ||
3113 (GetPixelMask(image,p) != 0))
3115 SetPixelChannel(blur_image,channel,p[center+i],q);
3123 if ((blur_traits & BlendPixelTrait) == 0)
3125 for (v=0; v < (ssize_t) width; v++)
3127 for (u=0; u < (ssize_t) width; u++)
3129 contrast=GetPixelIntensity(luminance_image,luminance_pixels)-
3131 if (fabs(contrast) < threshold)
3133 pixel+=(*k)*pixels[i];
3137 pixels+=GetPixelChannels(image);
3138 luminance_pixels+=GetPixelChannels(luminance_image);
3140 pixels+=image->columns*GetPixelChannels(image);
3141 luminance_pixels+=luminance_image->columns*
3142 GetPixelChannels(luminance_image);
3144 if (fabs((double) gamma) < MagickEpsilon)
3146 SetPixelChannel(blur_image,channel,p[center+i],q);
3149 gamma=MagickEpsilonReciprocal(gamma);
3150 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3153 for (v=0; v < (ssize_t) width; v++)
3155 for (u=0; u < (ssize_t) width; u++)
3157 contrast=GetPixelIntensity(image,pixels)-intensity;
3158 if (fabs(contrast) < threshold)
3160 alpha=(double) (QuantumScale*
3161 GetPixelAlpha(image,pixels));
3162 pixel+=(*k)*alpha*pixels[i];
3166 pixels+=GetPixelChannels(image);
3167 luminance_pixels+=GetPixelChannels(luminance_image);
3169 pixels+=image->columns*GetPixelChannels(image);
3170 luminance_pixels+=luminance_image->columns*
3171 GetPixelChannels(luminance_image);
3173 if (fabs((double) gamma) < MagickEpsilon)
3175 SetPixelChannel(blur_image,channel,p[center+i],q);
3178 gamma=MagickEpsilonReciprocal(gamma);
3179 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3181 p+=GetPixelChannels(image);
3182 l+=GetPixelChannels(luminance_image);
3183 q+=GetPixelChannels(blur_image);
3185 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3186 if (sync == MagickFalse)
3188 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3193 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3194 #pragma omp critical (MagickCore_SelectiveBlurImage)
3196 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3198 if (proceed == MagickFalse)
3202 blur_image->type=image->type;
3203 blur_view=DestroyCacheView(blur_view);
3204 image_view=DestroyCacheView(image_view);
3205 luminance_image=DestroyImage(luminance_image);
3206 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3207 if (status == MagickFalse)
3208 blur_image=DestroyImage(blur_image);
3213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3217 % S h a d e I m a g e %
3221 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3223 % ShadeImage() shines a distant light on an image to create a
3224 % three-dimensional effect. You control the positioning of the light with
3225 % azimuth and elevation; azimuth is measured in degrees off the x axis
3226 % and elevation is measured in pixels above the Z axis.
3228 % The format of the ShadeImage method is:
3230 % Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3231 % const double azimuth,const double elevation,ExceptionInfo *exception)
3233 % A description of each parameter follows:
3235 % o image: the image.
3237 % o gray: A value other than zero shades the intensity of each pixel.
3239 % o azimuth, elevation: Define the light source direction.
3241 % o exception: return any errors or warnings in this structure.
3244 MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3245 const double azimuth,const double elevation,ExceptionInfo *exception)
3247 #define ShadeImageTag "Shade/Image"
3269 Initialize shaded image attributes.
3271 assert(image != (const Image *) NULL);
3272 assert(image->signature == MagickSignature);
3273 if (image->debug != MagickFalse)
3274 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3275 assert(exception != (ExceptionInfo *) NULL);
3276 assert(exception->signature == MagickSignature);
3277 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3278 if (shade_image == (Image *) NULL)
3279 return((Image *) NULL);
3280 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
3282 shade_image=DestroyImage(shade_image);
3283 return((Image *) NULL);
3286 Compute the light vector.
3288 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3289 cos(DegreesToRadians(elevation));
3290 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3291 cos(DegreesToRadians(elevation));
3292 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3298 image_view=AcquireVirtualCacheView(image,exception);
3299 shade_view=AcquireAuthenticCacheView(shade_image,exception);
3300 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3301 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3302 dynamic_number_threads(image,image->columns,image->rows,1)
3304 for (y=0; y < (ssize_t) image->rows; y++)
3314 register const Quantum
3326 if (status == MagickFalse)
3328 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
3329 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3331 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3337 Shade this row of pixels.
3339 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
3340 pre=p+GetPixelChannels(image);
3341 center=pre+(image->columns+2)*GetPixelChannels(image);
3342 post=center+(image->columns+2)*GetPixelChannels(image);
3343 for (x=0; x < (ssize_t) image->columns; x++)
3349 Determine the surface normal and compute shading.
3351 normal.x=(double) (GetPixelIntensity(image,pre-GetPixelChannels(image))+
3352 GetPixelIntensity(image,center-GetPixelChannels(image))+
3353 GetPixelIntensity(image,post-GetPixelChannels(image))-
3354 GetPixelIntensity(image,pre+GetPixelChannels(image))-
3355 GetPixelIntensity(image,center+GetPixelChannels(image))-
3356 GetPixelIntensity(image,post+GetPixelChannels(image)));
3357 normal.y=(double) (GetPixelIntensity(image,post-GetPixelChannels(image))+
3358 GetPixelIntensity(image,post)+GetPixelIntensity(image,post+
3359 GetPixelChannels(image))-GetPixelIntensity(image,pre-
3360 GetPixelChannels(image))-GetPixelIntensity(image,pre)-
3361 GetPixelIntensity(image,pre+GetPixelChannels(image)));
3362 if ((normal.x == 0.0) && (normal.y == 0.0))
3367 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3368 if (distance > MagickEpsilon)
3371 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
3372 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3373 shade=distance/sqrt((double) normal_distance);
3376 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3385 channel=GetPixelChannelChannel(image,i);
3386 traits=GetPixelChannelTraits(image,channel);
3387 shade_traits=GetPixelChannelTraits(shade_image,channel);
3388 if ((traits == UndefinedPixelTrait) ||
3389 (shade_traits == UndefinedPixelTrait))
3391 if (((shade_traits & CopyPixelTrait) != 0) ||
3392 (GetPixelMask(image,p) != 0))
3394 SetPixelChannel(shade_image,channel,center[i],q);
3397 if (gray != MagickFalse)
3399 SetPixelChannel(shade_image,channel,ClampToQuantum(shade),q);
3402 SetPixelChannel(shade_image,channel,ClampToQuantum(QuantumScale*shade*
3405 pre+=GetPixelChannels(image);
3406 center+=GetPixelChannels(image);
3407 post+=GetPixelChannels(image);
3408 q+=GetPixelChannels(shade_image);
3410 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3412 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3417 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3418 #pragma omp critical (MagickCore_ShadeImage)
3420 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3421 if (proceed == MagickFalse)
3425 shade_view=DestroyCacheView(shade_view);
3426 image_view=DestroyCacheView(image_view);
3427 if (status == MagickFalse)
3428 shade_image=DestroyImage(shade_image);
3429 return(shade_image);
3433 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3437 % S h a r p e n I m a g e %
3441 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3443 % SharpenImage() sharpens the image. We convolve the image with a Gaussian
3444 % operator of the given radius and standard deviation (sigma). For
3445 % reasonable results, radius should be larger than sigma. Use a radius of 0
3446 % and SharpenImage() selects a suitable radius for you.
3448 % Using a separable kernel would be faster, but the negative weights cancel
3449 % out on the corners of the kernel producing often undesirable ringing in the
3450 % filtered result; this can be avoided by using a 2D gaussian shaped image
3451 % sharpening kernel instead.
3453 % The format of the SharpenImage method is:
3455 % Image *SharpenImage(const Image *image,const double radius,
3456 % const double sigma,ExceptionInfo *exception)
3458 % A description of each parameter follows:
3460 % o image: the image.
3462 % o radius: the radius of the Gaussian, in pixels, not counting the center
3465 % o sigma: the standard deviation of the Laplacian, in pixels.
3467 % o exception: return any errors or warnings in this structure.
3470 MagickExport Image *SharpenImage(const Image *image,const double radius,
3471 const double sigma,ExceptionInfo *exception)
3493 assert(image != (const Image *) NULL);
3494 assert(image->signature == MagickSignature);
3495 if (image->debug != MagickFalse)
3496 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3497 assert(exception != (ExceptionInfo *) NULL);
3498 assert(exception->signature == MagickSignature);
3499 width=GetOptimalKernelWidth2D(radius,sigma);
3500 kernel_info=AcquireKernelInfo((const char *) NULL);
3501 if (kernel_info == (KernelInfo *) NULL)
3502 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3503 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3504 kernel_info->width=width;
3505 kernel_info->height=width;
3506 kernel_info->signature=MagickSignature;
3507 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
3508 AcquireAlignedMemory(kernel_info->width,kernel_info->width*
3509 sizeof(*kernel_info->values)));
3510 if (kernel_info->values == (MagickRealType *) NULL)
3512 kernel_info=DestroyKernelInfo(kernel_info);
3513 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3516 j=(ssize_t) kernel_info->width/2;
3518 for (v=(-j); v <= j; v++)
3520 for (u=(-j); u <= j; u++)
3522 kernel_info->values[i]=(MagickRealType) (-exp(-((double) u*u+v*v)/(2.0*
3523 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3524 normalize+=kernel_info->values[i];
3528 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
3529 sharp_image=ConvolveImage(image,kernel_info,exception);
3530 if (sharp_image != (Image *) NULL)
3531 (void) ClampImage(sharp_image,exception);
3532 kernel_info=DestroyKernelInfo(kernel_info);
3533 return(sharp_image);
3537 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3541 % S p r e a d I m a g e %
3545 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3547 % SpreadImage() is a special effects method that randomly displaces each
3548 % pixel in a block defined by the radius parameter.
3550 % The format of the SpreadImage method is:
3552 % Image *SpreadImage(const Image *image,const double radius,
3553 % const PixelInterpolateMethod method,ExceptionInfo *exception)
3555 % A description of each parameter follows:
3557 % o image: the image.
3559 % o radius: choose a random pixel in a neighborhood of this extent.
3561 % o method: the pixel interpolation method.
3563 % o exception: return any errors or warnings in this structure.
3566 MagickExport Image *SpreadImage(const Image *image,const double radius,
3567 const PixelInterpolateMethod method,ExceptionInfo *exception)
3569 #define SpreadImageTag "Spread/Image"
3585 **restrict random_info;
3597 Initialize spread image attributes.
3599 assert(image != (Image *) NULL);
3600 assert(image->signature == MagickSignature);
3601 if (image->debug != MagickFalse)
3602 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3603 assert(exception != (ExceptionInfo *) NULL);
3604 assert(exception->signature == MagickSignature);
3605 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3607 if (spread_image == (Image *) NULL)
3608 return((Image *) NULL);
3609 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
3611 spread_image=DestroyImage(spread_image);
3612 return((Image *) NULL);
3619 width=GetOptimalKernelWidth1D(radius,0.5);
3620 random_info=AcquireRandomInfoThreadSet();
3621 key=GetRandomSecretKey(random_info[0]);
3622 image_view=AcquireVirtualCacheView(image,exception);
3623 spread_view=AcquireAuthenticCacheView(spread_image,exception);
3624 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3625 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3626 dynamic_number_threads(image,image->columns,image->rows,key == ~0UL)
3628 for (y=0; y < (ssize_t) image->rows; y++)
3631 id = GetOpenMPThreadId();
3633 register const Quantum
3642 if (status == MagickFalse)
3644 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3645 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
3647 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3652 for (x=0; x < (ssize_t) image->columns; x++)
3657 point.x=GetPseudoRandomValue(random_info[id]);
3658 point.y=GetPseudoRandomValue(random_info[id]);
3659 status=InterpolatePixelChannels(image,image_view,spread_image,method,
3660 (double) x+width*point.x-0.5,(double) y+width*point.y-0.5,q,exception);
3661 q+=GetPixelChannels(spread_image);
3663 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
3665 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3670 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3671 #pragma omp critical (MagickCore_SpreadImage)
3673 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3674 if (proceed == MagickFalse)
3678 spread_view=DestroyCacheView(spread_view);
3679 image_view=DestroyCacheView(image_view);
3680 random_info=DestroyRandomInfoThreadSet(random_info);
3681 return(spread_image);
3685 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3689 % U n s h a r p M a s k I m a g e %
3693 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3695 % UnsharpMaskImage() sharpens one or more image channels. We convolve the
3696 % image with a Gaussian operator of the given radius and standard deviation
3697 % (sigma). For reasonable results, radius should be larger than sigma. Use a
3698 % radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
3700 % The format of the UnsharpMaskImage method is:
3702 % Image *UnsharpMaskImage(const Image *image,const double radius,
3703 % const double sigma,const double amount,const double threshold,
3704 % ExceptionInfo *exception)
3706 % A description of each parameter follows:
3708 % o image: the image.
3710 % o radius: the radius of the Gaussian, in pixels, not counting the center
3713 % o sigma: the standard deviation of the Gaussian, in pixels.
3715 % o amount: the percentage of the difference between the original and the
3716 % blur image that is added back into the original.
3718 % o threshold: the threshold in pixels needed to apply the diffence amount.
3720 % o exception: return any errors or warnings in this structure.
3723 MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
3724 const double sigma,const double amount,const double threshold,
3725 ExceptionInfo *exception)
3727 #define SharpenImageTag "Sharpen/Image"
3748 assert(image != (const Image *) NULL);
3749 assert(image->signature == MagickSignature);
3750 if (image->debug != MagickFalse)
3751 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3752 assert(exception != (ExceptionInfo *) NULL);
3753 unsharp_image=BlurImage(image,radius,sigma,exception);
3754 if (unsharp_image == (Image *) NULL)
3755 return((Image *) NULL);
3756 quantum_threshold=(double) QuantumRange*threshold;
3762 image_view=AcquireVirtualCacheView(image,exception);
3763 unsharp_view=AcquireAuthenticCacheView(unsharp_image,exception);
3764 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3765 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3766 dynamic_number_threads(image,image->columns,image->rows,1)
3768 for (y=0; y < (ssize_t) image->rows; y++)
3770 register const Quantum
3779 if (status == MagickFalse)
3781 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3782 q=QueueCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
3784 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3789 for (x=0; x < (ssize_t) image->columns; x++)
3794 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3806 channel=GetPixelChannelChannel(image,i);
3807 traits=GetPixelChannelTraits(image,channel);
3808 unsharp_traits=GetPixelChannelTraits(unsharp_image,channel);
3809 if ((traits == UndefinedPixelTrait) ||
3810 (unsharp_traits == UndefinedPixelTrait))
3812 if (((unsharp_traits & CopyPixelTrait) != 0) ||
3813 (GetPixelMask(image,p) != 0))
3815 SetPixelChannel(unsharp_image,channel,p[i],q);
3818 pixel=p[i]-(double) GetPixelChannel(unsharp_image,channel,q);
3819 if (fabs(2.0*pixel) < quantum_threshold)
3820 pixel=(double) p[i];
3822 pixel=(double) p[i]+amount*pixel;
3823 SetPixelChannel(unsharp_image,channel,ClampToQuantum(pixel),q);
3825 p+=GetPixelChannels(image);
3826 q+=GetPixelChannels(unsharp_image);
3828 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
3830 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3835 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3836 #pragma omp critical (MagickCore_UnsharpMaskImage)
3838 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
3839 if (proceed == MagickFalse)
3843 unsharp_image->type=image->type;
3844 unsharp_view=DestroyCacheView(unsharp_view);
3845 if (unsharp_image != (Image *) NULL)
3846 (void) ClampImage(unsharp_image,exception);
3847 image_view=DestroyCacheView(image_view);
3848 if (status == MagickFalse)
3849 unsharp_image=DestroyImage(unsharp_image);
3850 return(unsharp_image);