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/monitor.h"
67 #include "MagickCore/monitor-private.h"
68 #include "MagickCore/montage.h"
69 #include "MagickCore/morphology.h"
70 #include "MagickCore/paint.h"
71 #include "MagickCore/pixel-accessor.h"
72 #include "MagickCore/pixel-private.h"
73 #include "MagickCore/property.h"
74 #include "MagickCore/quantize.h"
75 #include "MagickCore/quantum.h"
76 #include "MagickCore/quantum-private.h"
77 #include "MagickCore/random_.h"
78 #include "MagickCore/random-private.h"
79 #include "MagickCore/resample.h"
80 #include "MagickCore/resample-private.h"
81 #include "MagickCore/resize.h"
82 #include "MagickCore/resource_.h"
83 #include "MagickCore/segment.h"
84 #include "MagickCore/shear.h"
85 #include "MagickCore/signature-private.h"
86 #include "MagickCore/statistic.h"
87 #include "MagickCore/string_.h"
88 #include "MagickCore/thread-private.h"
89 #include "MagickCore/transform.h"
90 #include "MagickCore/threshold.h"
93 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97 % A d a p t i v e B l u r I m a g e %
101 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103 % AdaptiveBlurImage() adaptively blurs the image by blurring less
104 % intensely near image edges and more intensely far from edges. We blur the
105 % image with a Gaussian operator of the given radius and standard deviation
106 % (sigma). For reasonable results, radius should be larger than sigma. Use a
107 % radius of 0 and AdaptiveBlurImage() selects a suitable radius for you.
109 % The format of the AdaptiveBlurImage method is:
111 % Image *AdaptiveBlurImage(const Image *image,const double radius,
112 % const double sigma,ExceptionInfo *exception)
114 % A description of each parameter follows:
116 % o image: the image.
118 % o radius: the radius of the Gaussian, in pixels, not counting the center
121 % o sigma: the standard deviation of the Laplacian, in pixels.
123 % o exception: return any errors or warnings in this structure.
127 MagickExport MagickBooleanType AdaptiveLevelImage(Image *image,
128 const char *levels,ExceptionInfo *exception)
147 if (levels == (char *) NULL)
149 flags=ParseGeometry(levels,&geometry_info);
150 black_point=geometry_info.rho;
151 white_point=(double) QuantumRange;
152 if ((flags & SigmaValue) != 0)
153 white_point=geometry_info.sigma;
155 if ((flags & XiValue) != 0)
156 gamma=geometry_info.xi;
157 if ((flags & PercentValue) != 0)
159 black_point*=(double) image->columns*image->rows/100.0;
160 white_point*=(double) image->columns*image->rows/100.0;
162 if ((flags & SigmaValue) == 0)
163 white_point=(double) QuantumRange-black_point;
164 if ((flags & AspectValue ) == 0)
165 status=LevelImage(image,black_point,white_point,gamma,exception);
167 status=LevelizeImage(image,black_point,white_point,gamma,exception);
171 MagickExport Image *AdaptiveBlurImage(const Image *image,const double radius,
172 const double sigma,ExceptionInfo *exception)
174 #define AdaptiveBlurImageTag "Convolve/Image"
175 #define MagickSigma (fabs(sigma) < MagickEpsilon ? MagickEpsilon : sigma)
210 assert(image != (const Image *) NULL);
211 assert(image->signature == MagickSignature);
212 if (image->debug != MagickFalse)
213 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
214 assert(exception != (ExceptionInfo *) NULL);
215 assert(exception->signature == MagickSignature);
216 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
217 if (blur_image == (Image *) NULL)
218 return((Image *) NULL);
219 if (fabs(sigma) < MagickEpsilon)
221 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
223 blur_image=DestroyImage(blur_image);
224 return((Image *) NULL);
227 Edge detect the image brighness channel, level, blur, and level again.
229 edge_image=EdgeImage(image,radius,sigma,exception);
230 if (edge_image == (Image *) NULL)
232 blur_image=DestroyImage(blur_image);
233 return((Image *) NULL);
235 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
236 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
237 if (gaussian_image != (Image *) NULL)
239 edge_image=DestroyImage(edge_image);
240 edge_image=gaussian_image;
242 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
244 Create a set of kernels from maximum (radius,sigma) to minimum.
246 width=GetOptimalKernelWidth2D(radius,sigma);
247 kernel=(double **) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
248 if (kernel == (double **) NULL)
250 edge_image=DestroyImage(edge_image);
251 blur_image=DestroyImage(blur_image);
252 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
254 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
255 for (i=0; i < (ssize_t) width; i+=2)
257 kernel[i]=(double *) AcquireAlignedMemory((size_t) (width-i),(width-i)*
259 if (kernel[i] == (double *) NULL)
262 j=(ssize_t) (width-i)/2;
264 for (v=(-j); v <= j; v++)
266 for (u=(-j); u <= j; u++)
268 kernel[i][k]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
269 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
270 normalize+=kernel[i][k];
274 if (fabs(normalize) < MagickEpsilon)
275 normalize=MagickEpsilon;
276 normalize=MagickEpsilonReciprocal(normalize);
277 for (k=0; k < (j*j); k++)
278 kernel[i][k]=normalize*kernel[i][k];
280 if (i < (ssize_t) width)
282 for (i-=2; i >= 0; i-=2)
283 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
284 kernel=(double **) RelinquishAlignedMemory(kernel);
285 edge_image=DestroyImage(edge_image);
286 blur_image=DestroyImage(blur_image);
287 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
290 Adaptively blur image.
294 image_view=AcquireVirtualCacheView(image,exception);
295 edge_view=AcquireVirtualCacheView(edge_image,exception);
296 blur_view=AcquireAuthenticCacheView(blur_image,exception);
297 #if defined(MAGICKCORE_OPENMP_SUPPORT)
298 #pragma omp parallel for schedule(static,4) shared(progress,status) \
299 dynamic_number_threads(image,image->columns,image->rows,1)
301 for (y=0; y < (ssize_t) blur_image->rows; y++)
303 register const Quantum
312 if (status == MagickFalse)
314 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
315 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
317 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
322 for (x=0; x < (ssize_t) blur_image->columns; x++)
324 register const Quantum
334 j=(ssize_t) ceil((double) width*QuantumScale*
335 GetPixelIntensity(edge_image,r)-0.5);
339 if (j > (ssize_t) width)
343 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
344 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
345 if (p == (const Quantum *) NULL)
347 center=(ssize_t) GetPixelChannels(image)*(width-j)*((width-j)/2L)+
348 GetPixelChannels(image)*((width-j)/2L);
349 if (GetPixelMask(image,p) != 0)
351 q+=GetPixelChannels(blur_image);
352 r+=GetPixelChannels(edge_image);
355 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
369 register const double
372 register const Quantum
381 channel=GetPixelChannelMapChannel(image,i);
382 traits=GetPixelChannelMapTraits(image,channel);
383 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
384 if ((traits == UndefinedPixelTrait) ||
385 (blur_traits == UndefinedPixelTrait))
387 if ((blur_traits & CopyPixelTrait) != 0)
389 SetPixelChannel(blur_image,channel,p[center+i],q);
396 if ((blur_traits & BlendPixelTrait) == 0)
401 for (v=0; v < (ssize_t) (width-j); v++)
403 for (u=0; u < (ssize_t) (width-j); u++)
405 pixel+=(*k)*pixels[i];
408 pixels+=GetPixelChannels(image);
411 gamma=MagickEpsilonReciprocal(gamma);
412 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
418 for (v=0; v < (ssize_t) (width-j); v++)
420 for (u=0; u < (ssize_t) (width-j); u++)
422 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
423 pixel+=(*k)*alpha*pixels[i];
426 pixels+=GetPixelChannels(image);
429 gamma=MagickEpsilonReciprocal(gamma);
430 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
432 q+=GetPixelChannels(blur_image);
433 r+=GetPixelChannels(edge_image);
435 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
437 if (image->progress_monitor != (MagickProgressMonitor) NULL)
442 #if defined(MAGICKCORE_OPENMP_SUPPORT)
443 #pragma omp critical (MagickCore_AdaptiveBlurImage)
445 proceed=SetImageProgress(image,AdaptiveBlurImageTag,progress++,
447 if (proceed == MagickFalse)
451 blur_image->type=image->type;
452 blur_view=DestroyCacheView(blur_view);
453 edge_view=DestroyCacheView(edge_view);
454 image_view=DestroyCacheView(image_view);
455 edge_image=DestroyImage(edge_image);
456 for (i=0; i < (ssize_t) width; i+=2)
457 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
458 kernel=(double **) RelinquishAlignedMemory(kernel);
459 if (status == MagickFalse)
460 blur_image=DestroyImage(blur_image);
465 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
469 % A d a p t i v e S h a r p e n I m a g e %
473 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
475 % AdaptiveSharpenImage() adaptively sharpens the image by sharpening more
476 % intensely near image edges and less intensely far from edges. We sharpen the
477 % image with a Gaussian operator of the given radius and standard deviation
478 % (sigma). For reasonable results, radius should be larger than sigma. Use a
479 % radius of 0 and AdaptiveSharpenImage() selects a suitable radius for you.
481 % The format of the AdaptiveSharpenImage method is:
483 % Image *AdaptiveSharpenImage(const Image *image,const double radius,
484 % const double sigma,ExceptionInfo *exception)
486 % A description of each parameter follows:
488 % o image: the image.
490 % o radius: the radius of the Gaussian, in pixels, not counting the center
493 % o sigma: the standard deviation of the Laplacian, in pixels.
495 % o exception: return any errors or warnings in this structure.
498 MagickExport Image *AdaptiveSharpenImage(const Image *image,const double radius,
499 const double sigma,ExceptionInfo *exception)
501 #define AdaptiveSharpenImageTag "Convolve/Image"
502 #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,sigma,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=GaussianBlurImage(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=(double **) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
575 if (kernel == (double **) NULL)
577 edge_image=DestroyImage(edge_image);
578 sharp_image=DestroyImage(sharp_image);
579 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
581 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
582 for (i=0; i < (ssize_t) width; i+=2)
584 kernel[i]=(double *) AcquireAlignedMemory((size_t) (width-i),(width-i)*
586 if (kernel[i] == (double *) NULL)
589 j=(ssize_t) (width-i)/2;
591 for (v=(-j); v <= j; v++)
593 for (u=(-j); u <= j; u++)
595 kernel[i][k]=(double) (-exp(-((double) u*u+v*v)/(2.0*MagickSigma*
596 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
597 normalize+=kernel[i][k];
601 if (fabs(normalize) < MagickEpsilon)
602 normalize=MagickEpsilon;
603 normalize=MagickEpsilonReciprocal(normalize);
604 for (k=0; k < (j*j); k++)
605 kernel[i][k]=normalize*kernel[i][k];
607 if (i < (ssize_t) width)
609 for (i-=2; i >= 0; i-=2)
610 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
611 kernel=(double **) RelinquishAlignedMemory(kernel);
612 edge_image=DestroyImage(edge_image);
613 sharp_image=DestroyImage(sharp_image);
614 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
617 Adaptively sharpen image.
621 image_view=AcquireVirtualCacheView(image,exception);
622 edge_view=AcquireVirtualCacheView(edge_image,exception);
623 sharp_view=AcquireAuthenticCacheView(sharp_image,exception);
624 #if defined(MAGICKCORE_OPENMP_SUPPORT)
625 #pragma omp parallel for schedule(static,4) shared(progress,status) \
626 dynamic_number_threads(image,image->columns,image->rows,1)
628 for (y=0; y < (ssize_t) sharp_image->rows; y++)
630 register const Quantum
639 if (status == MagickFalse)
641 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
642 q=QueueCacheViewAuthenticPixels(sharp_view,0,y,sharp_image->columns,1,
644 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
649 for (x=0; x < (ssize_t) sharp_image->columns; x++)
651 register const Quantum
661 j=(ssize_t) ceil((double) width*QuantumScale*
662 GetPixelIntensity(edge_image,r)-0.5);
666 if (j > (ssize_t) width)
670 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
671 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
672 if (p == (const Quantum *) NULL)
674 center=(ssize_t) GetPixelChannels(image)*(width-j)*((width-j)/2L)+
675 GetPixelChannels(image)*((width-j)/2);
676 if (GetPixelMask(image,p) != 0)
678 q+=GetPixelChannels(sharp_image);
679 r+=GetPixelChannels(edge_image);
682 for (i=0; i < (ssize_t) GetPixelChannels(sharp_image); i++)
696 register const double
699 register const Quantum
708 channel=GetPixelChannelMapChannel(image,i);
709 traits=GetPixelChannelMapTraits(image,channel);
710 sharp_traits=GetPixelChannelMapTraits(sharp_image,channel);
711 if ((traits == UndefinedPixelTrait) ||
712 (sharp_traits == UndefinedPixelTrait))
714 if ((sharp_traits & CopyPixelTrait) != 0)
716 SetPixelChannel(sharp_image,channel,p[center+i],q);
723 if ((sharp_traits & BlendPixelTrait) == 0)
728 for (v=0; v < (ssize_t) (width-j); v++)
730 for (u=0; u < (ssize_t) (width-j); u++)
732 pixel+=(*k)*pixels[i];
735 pixels+=GetPixelChannels(image);
738 gamma=MagickEpsilonReciprocal(gamma);
739 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
745 for (v=0; v < (ssize_t) (width-j); v++)
747 for (u=0; u < (ssize_t) (width-j); u++)
749 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
750 pixel+=(*k)*alpha*pixels[i];
753 pixels+=GetPixelChannels(image);
756 gamma=MagickEpsilonReciprocal(gamma);
757 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
759 q+=GetPixelChannels(sharp_image);
760 r+=GetPixelChannels(edge_image);
762 if (SyncCacheViewAuthenticPixels(sharp_view,exception) == MagickFalse)
764 if (image->progress_monitor != (MagickProgressMonitor) NULL)
769 #if defined(MAGICKCORE_OPENMP_SUPPORT)
770 #pragma omp critical (MagickCore_AdaptiveSharpenImage)
772 proceed=SetImageProgress(image,AdaptiveSharpenImageTag,progress++,
774 if (proceed == MagickFalse)
778 sharp_image->type=image->type;
779 sharp_view=DestroyCacheView(sharp_view);
780 edge_view=DestroyCacheView(edge_view);
781 image_view=DestroyCacheView(image_view);
782 edge_image=DestroyImage(edge_image);
783 for (i=0; i < (ssize_t) width; i+=2)
784 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
785 kernel=(double **) RelinquishAlignedMemory(kernel);
786 if (status == MagickFalse)
787 sharp_image=DestroyImage(sharp_image);
792 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
796 % B l u r I m a g e %
800 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
802 % BlurImage() blurs an image. We convolve the image with a Gaussian operator
803 % of the given radius and standard deviation (sigma). For reasonable results,
804 % the radius should be larger than sigma. Use a radius of 0 and BlurImage()
805 % selects a suitable radius for you.
807 % BlurImage() differs from GaussianBlurImage() in that it uses a separable
808 % kernel which is faster but mathematically equivalent to the non-separable
811 % The format of the BlurImage method is:
813 % Image *BlurImage(const Image *image,const double radius,
814 % const double sigma,ExceptionInfo *exception)
816 % A description of each parameter follows:
818 % o image: the image.
820 % o radius: the radius of the Gaussian, in pixels, not counting the center
823 % o sigma: the standard deviation of the Gaussian, in pixels.
825 % o exception: return any errors or warnings in this structure.
829 static double *GetBlurKernel(const size_t width,const double sigma)
843 Generate a 1-D convolution kernel.
845 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
846 kernel=(double *) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
847 if (kernel == (double *) NULL)
852 for (k=(-j); k <= j; k++)
854 kernel[i]=(double) (exp(-((double) k*k)/(2.0*MagickSigma*MagickSigma))/
855 (MagickSQ2PI*MagickSigma));
856 normalize+=kernel[i];
859 for (i=0; i < (ssize_t) width; i++)
860 kernel[i]/=normalize;
864 MagickExport Image *BlurImage(const Image *image,const double radius,
865 const double sigma,ExceptionInfo *exception)
867 #define BlurImageTag "Blur/Image"
897 Initialize blur image attributes.
899 assert(image != (Image *) NULL);
900 assert(image->signature == MagickSignature);
901 if (image->debug != MagickFalse)
902 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
903 assert(exception != (ExceptionInfo *) NULL);
904 assert(exception->signature == MagickSignature);
905 blur_image=CloneImage(image,0,0,MagickTrue,exception);
906 if (blur_image == (Image *) NULL)
907 return((Image *) NULL);
908 if (fabs(sigma) < MagickEpsilon)
910 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
912 blur_image=DestroyImage(blur_image);
913 return((Image *) NULL);
915 width=GetOptimalKernelWidth1D(radius,sigma);
916 kernel=GetBlurKernel(width,sigma);
917 if (kernel == (double *) NULL)
919 blur_image=DestroyImage(blur_image);
920 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
922 if (image->debug != MagickFalse)
925 format[MaxTextExtent],
928 register const double
931 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
932 " blur image with kernel width %.20g:",(double) width);
933 message=AcquireString("");
935 for (i=0; i < (ssize_t) width; i++)
938 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) i);
939 (void) ConcatenateString(&message,format);
940 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
941 (void) ConcatenateString(&message,format);
942 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
944 message=DestroyString(message);
951 center=(ssize_t) GetPixelChannels(image)*(width/2L);
952 image_view=AcquireVirtualCacheView(image,exception);
953 blur_view=AcquireAuthenticCacheView(blur_image,exception);
954 #if defined(MAGICKCORE_OPENMP_SUPPORT)
955 #pragma omp parallel for schedule(static,4) shared(progress,status) \
956 dynamic_number_threads(image,image->columns,image->rows,1)
958 for (y=0; y < (ssize_t) image->rows; y++)
960 register const Quantum
969 if (status == MagickFalse)
971 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y,
972 image->columns+width,1,exception);
973 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
975 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
980 for (x=0; x < (ssize_t) image->columns; x++)
985 if (GetPixelMask(image,p) != 0)
987 p+=GetPixelChannels(image);
988 q+=GetPixelChannels(blur_image);
991 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1005 register const double
1008 register const Quantum
1014 channel=GetPixelChannelMapChannel(image,i);
1015 traits=GetPixelChannelMapTraits(image,channel);
1016 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
1017 if ((traits == UndefinedPixelTrait) ||
1018 (blur_traits == UndefinedPixelTrait))
1020 if ((blur_traits & CopyPixelTrait) != 0)
1022 SetPixelChannel(blur_image,channel,p[center+i],q);
1028 if ((blur_traits & BlendPixelTrait) == 0)
1033 for (u=0; u < (ssize_t) width; u++)
1035 pixel+=(*k)*pixels[i];
1037 pixels+=GetPixelChannels(image);
1039 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
1046 for (u=0; u < (ssize_t) width; u++)
1048 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
1049 pixel+=(*k)*alpha*pixels[i];
1052 pixels+=GetPixelChannels(image);
1054 gamma=MagickEpsilonReciprocal(gamma);
1055 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
1057 p+=GetPixelChannels(image);
1058 q+=GetPixelChannels(blur_image);
1060 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1062 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1067 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1068 #pragma omp critical (MagickCore_BlurImage)
1070 proceed=SetImageProgress(image,BlurImageTag,progress++,blur_image->rows+
1071 blur_image->columns);
1072 if (proceed == MagickFalse)
1076 blur_view=DestroyCacheView(blur_view);
1077 image_view=DestroyCacheView(image_view);
1081 center=(ssize_t) GetPixelChannels(blur_image)*(width/2L);
1082 image_view=AcquireVirtualCacheView(blur_image,exception);
1083 blur_view=AcquireAuthenticCacheView(blur_image,exception);
1084 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1085 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1086 dynamic_number_threads(image,image->columns,image->rows,1)
1088 for (x=0; x < (ssize_t) blur_image->columns; x++)
1090 register const Quantum
1099 if (status == MagickFalse)
1101 p=GetCacheViewVirtualPixels(image_view,x,-((ssize_t) width/2L),1,
1102 blur_image->rows+width,exception);
1103 q=GetCacheViewAuthenticPixels(blur_view,x,0,1,blur_image->rows,exception);
1104 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1109 for (y=0; y < (ssize_t) blur_image->rows; y++)
1114 if (GetPixelMask(image,p) != 0)
1116 p+=GetPixelChannels(blur_image);
1117 q+=GetPixelChannels(blur_image);
1120 for (i=0; i < (ssize_t) GetPixelChannels(blur_image); i++)
1134 register const double
1137 register const Quantum
1143 channel=GetPixelChannelMapChannel(blur_image,i);
1144 traits=GetPixelChannelMapTraits(blur_image,channel);
1145 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
1146 if ((traits == UndefinedPixelTrait) ||
1147 (blur_traits == UndefinedPixelTrait))
1149 if ((blur_traits & CopyPixelTrait) != 0)
1151 SetPixelChannel(blur_image,channel,p[center+i],q);
1157 if ((blur_traits & BlendPixelTrait) == 0)
1162 for (u=0; u < (ssize_t) width; u++)
1164 pixel+=(*k)*pixels[i];
1166 pixels+=GetPixelChannels(blur_image);
1168 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
1175 for (u=0; u < (ssize_t) width; u++)
1177 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(blur_image,
1179 pixel+=(*k)*alpha*pixels[i];
1182 pixels+=GetPixelChannels(blur_image);
1184 gamma=MagickEpsilonReciprocal(gamma);
1185 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
1187 p+=GetPixelChannels(blur_image);
1188 q+=GetPixelChannels(blur_image);
1190 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1192 if (blur_image->progress_monitor != (MagickProgressMonitor) NULL)
1197 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1198 #pragma omp critical (MagickCore_BlurImage)
1200 proceed=SetImageProgress(blur_image,BlurImageTag,progress++,
1201 blur_image->rows+blur_image->columns);
1202 if (proceed == MagickFalse)
1206 blur_view=DestroyCacheView(blur_view);
1207 image_view=DestroyCacheView(image_view);
1208 kernel=(double *) RelinquishAlignedMemory(kernel);
1209 blur_image->type=image->type;
1210 if (status == MagickFalse)
1211 blur_image=DestroyImage(blur_image);
1216 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1220 % C o n v o l v e I m a g e %
1224 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1226 % ConvolveImage() applies a custom convolution kernel to the image.
1228 % The format of the ConvolveImage method is:
1230 % Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
1231 % ExceptionInfo *exception)
1233 % A description of each parameter follows:
1235 % o image: the image.
1237 % o kernel: the filtering kernel.
1239 % o exception: return any errors or warnings in this structure.
1242 MagickExport Image *ConvolveImage(const Image *image,
1243 const KernelInfo *kernel_info,ExceptionInfo *exception)
1245 return(MorphologyImage(image,CorrelateMorphology,1,kernel_info,exception));
1249 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1253 % D e s p e c k l e I m a g e %
1257 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1259 % DespeckleImage() reduces the speckle noise in an image while perserving the
1260 % edges of the original image. A speckle removing filter uses a complementary % hulling technique (raising pixels that are darker than their surrounding
1261 % neighbors, then complementarily lowering pixels that are brighter than their
1262 % surrounding neighbors) to reduce the speckle index of that image (reference
1263 % Crimmins speckle removal).
1265 % The format of the DespeckleImage method is:
1267 % Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1269 % A description of each parameter follows:
1271 % o image: the image.
1273 % o exception: return any errors or warnings in this structure.
1277 static void Hull(const Image *image,const ssize_t x_offset,
1278 const ssize_t y_offset,const size_t columns,const size_t rows,
1279 const int polarity,Quantum *restrict f,Quantum *restrict g)
1290 assert(f != (Quantum *) NULL);
1291 assert(g != (Quantum *) NULL);
1294 r=p+(y_offset*(columns+2)+x_offset);
1295 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1296 #pragma omp parallel for schedule(static) \
1297 dynamic_number_threads(image,columns,rows,1)
1299 for (y=0; y < (ssize_t) rows; y++)
1308 i=(2*y+1)+y*columns;
1310 for (x=0; x < (ssize_t) columns; x++)
1312 v=(SignedQuantum) p[i];
1313 if ((SignedQuantum) r[i] >= (v+ScaleCharToQuantum(2)))
1314 v+=ScaleCharToQuantum(1);
1319 for (x=0; x < (ssize_t) columns; x++)
1321 v=(SignedQuantum) p[i];
1322 if ((SignedQuantum) r[i] <= (v-ScaleCharToQuantum(2)))
1323 v-=ScaleCharToQuantum(1);
1330 r=q+(y_offset*(columns+2)+x_offset);
1331 s=q-(y_offset*(columns+2)+x_offset);
1332 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1333 #pragma omp parallel for schedule(static) \
1334 dynamic_number_threads(image,columns,rows,1)
1336 for (y=0; y < (ssize_t) rows; y++)
1345 i=(2*y+1)+y*columns;
1347 for (x=0; x < (ssize_t) columns; x++)
1349 v=(SignedQuantum) q[i];
1350 if (((SignedQuantum) s[i] >= (v+ScaleCharToQuantum(2))) &&
1351 ((SignedQuantum) r[i] > v))
1352 v+=ScaleCharToQuantum(1);
1357 for (x=0; x < (ssize_t) columns; x++)
1359 v=(SignedQuantum) q[i];
1360 if (((SignedQuantum) s[i] <= (v-ScaleCharToQuantum(2))) &&
1361 ((SignedQuantum) r[i] < v))
1362 v-=ScaleCharToQuantum(1);
1369 MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1371 #define DespeckleImageTag "Despeckle/Image"
1393 static const ssize_t
1394 X[4] = {0, 1, 1,-1},
1395 Y[4] = {1, 0, 1, 1};
1398 Allocate despeckled image.
1400 assert(image != (const Image *) NULL);
1401 assert(image->signature == MagickSignature);
1402 if (image->debug != MagickFalse)
1403 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1404 assert(exception != (ExceptionInfo *) NULL);
1405 assert(exception->signature == MagickSignature);
1406 despeckle_image=CloneImage(image,0,0,MagickTrue,exception);
1407 if (despeckle_image == (Image *) NULL)
1408 return((Image *) NULL);
1409 status=SetImageStorageClass(despeckle_image,DirectClass,exception);
1410 if (status == MagickFalse)
1412 despeckle_image=DestroyImage(despeckle_image);
1413 return((Image *) NULL);
1416 Allocate image buffer.
1418 length=(size_t) ((image->columns+2)*(image->rows+2));
1419 pixels=(Quantum *) AcquireQuantumMemory(length,sizeof(*pixels));
1420 buffer=(Quantum *) AcquireQuantumMemory(length,sizeof(*buffer));
1421 if ((pixels == (Quantum *) NULL) || (buffer == (Quantum *) NULL))
1423 if (buffer != (Quantum *) NULL)
1424 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1425 if (pixels != (Quantum *) NULL)
1426 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1427 despeckle_image=DestroyImage(despeckle_image);
1428 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1431 Reduce speckle in the image.
1434 image_view=AcquireVirtualCacheView(image,exception);
1435 despeckle_view=AcquireAuthenticCacheView(despeckle_image,exception);
1436 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1453 if (status == MagickFalse)
1455 channel=GetPixelChannelMapChannel(image,i);
1456 traits=GetPixelChannelMapTraits(image,channel);
1457 despeckle_traits=GetPixelChannelMapTraits(despeckle_image,channel);
1458 if ((traits == UndefinedPixelTrait) ||
1459 (despeckle_traits == UndefinedPixelTrait))
1461 if ((despeckle_traits & CopyPixelTrait) != 0)
1463 (void) ResetMagickMemory(pixels,0,length*sizeof(*pixels));
1464 j=(ssize_t) image->columns+2;
1465 for (y=0; y < (ssize_t) image->rows; y++)
1467 register const Quantum
1470 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1471 if (p == (const Quantum *) NULL)
1477 for (x=0; x < (ssize_t) image->columns; x++)
1480 p+=GetPixelChannels(image);
1484 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
1485 for (k=0; k < 4; k++)
1487 Hull(image,X[k],Y[k],image->columns,image->rows,1,pixels,buffer);
1488 Hull(image,-X[k],-Y[k],image->columns,image->rows,1,pixels,buffer);
1489 Hull(image,-X[k],-Y[k],image->columns,image->rows,-1,pixels,buffer);
1490 Hull(image,X[k],Y[k],image->columns,image->rows,-1,pixels,buffer);
1492 j=(ssize_t) image->columns+2;
1493 for (y=0; y < (ssize_t) image->rows; y++)
1501 q=QueueCacheViewAuthenticPixels(despeckle_view,0,y,
1502 despeckle_image->columns,1,exception);
1503 if (q == (Quantum *) NULL)
1509 for (x=0; x < (ssize_t) image->columns; x++)
1511 SetPixelChannel(despeckle_image,channel,pixels[j++],q);
1512 q+=GetPixelChannels(despeckle_image);
1514 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1515 if (sync == MagickFalse)
1519 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1524 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1525 GetPixelChannels(image));
1526 if (proceed == MagickFalse)
1530 despeckle_view=DestroyCacheView(despeckle_view);
1531 image_view=DestroyCacheView(image_view);
1532 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1533 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1534 despeckle_image->type=image->type;
1535 if (status == MagickFalse)
1536 despeckle_image=DestroyImage(despeckle_image);
1537 return(despeckle_image);
1541 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1545 % E d g e I m a g e %
1549 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1551 % EdgeImage() finds edges in an image. Radius defines the radius of the
1552 % convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1555 % The format of the EdgeImage method is:
1557 % Image *EdgeImage(const Image *image,const double radius,
1558 % const double sigma,ExceptionInfo *exception)
1560 % A description of each parameter follows:
1562 % o image: the image.
1564 % o radius: the radius of the pixel neighborhood.
1566 % o sigma: the standard deviation of the Gaussian, in pixels.
1568 % o exception: return any errors or warnings in this structure.
1571 MagickExport Image *EdgeImage(const Image *image,const double radius,
1572 const double sigma,ExceptionInfo *exception)
1591 assert(image != (const Image *) NULL);
1592 assert(image->signature == MagickSignature);
1593 if (image->debug != MagickFalse)
1594 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1595 assert(exception != (ExceptionInfo *) NULL);
1596 assert(exception->signature == MagickSignature);
1597 width=GetOptimalKernelWidth1D(radius,sigma);
1598 kernel_info=AcquireKernelInfo((const char *) NULL);
1599 if (kernel_info == (KernelInfo *) NULL)
1600 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1601 kernel_info->width=width;
1602 kernel_info->height=width;
1603 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
1604 kernel_info->width*sizeof(*kernel_info->values));
1605 if (kernel_info->values == (double *) NULL)
1607 kernel_info=DestroyKernelInfo(kernel_info);
1608 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1610 j=(ssize_t) kernel_info->width/2;
1612 for (v=(-j); v <= j; v++)
1614 for (u=(-j); u <= j; u++)
1616 kernel_info->values[i]=(-1.0);
1620 kernel_info->values[i/2]=(double) (width*width-1.0);
1621 edge_image=ConvolveImage(image,kernel_info,exception);
1622 kernel_info=DestroyKernelInfo(kernel_info);
1627 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1631 % E m b o s s I m a g e %
1635 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1637 % EmbossImage() returns a grayscale image with a three-dimensional effect.
1638 % We convolve the image with a Gaussian operator of the given radius and
1639 % standard deviation (sigma). For reasonable results, radius should be
1640 % larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1643 % The format of the EmbossImage method is:
1645 % Image *EmbossImage(const Image *image,const double radius,
1646 % const double sigma,ExceptionInfo *exception)
1648 % A description of each parameter follows:
1650 % o image: the image.
1652 % o radius: the radius of the pixel neighborhood.
1654 % o sigma: the standard deviation of the Gaussian, in pixels.
1656 % o exception: return any errors or warnings in this structure.
1659 MagickExport Image *EmbossImage(const Image *image,const double radius,
1660 const double sigma,ExceptionInfo *exception)
1680 assert(image != (const Image *) NULL);
1681 assert(image->signature == MagickSignature);
1682 if (image->debug != MagickFalse)
1683 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1684 assert(exception != (ExceptionInfo *) NULL);
1685 assert(exception->signature == MagickSignature);
1686 width=GetOptimalKernelWidth1D(radius,sigma);
1687 kernel_info=AcquireKernelInfo((const char *) NULL);
1688 if (kernel_info == (KernelInfo *) NULL)
1689 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1690 kernel_info->width=width;
1691 kernel_info->height=width;
1692 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
1693 kernel_info->width*sizeof(*kernel_info->values));
1694 if (kernel_info->values == (double *) NULL)
1696 kernel_info=DestroyKernelInfo(kernel_info);
1697 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1699 j=(ssize_t) kernel_info->width/2;
1702 for (v=(-j); v <= j; v++)
1704 for (u=(-j); u <= j; u++)
1706 kernel_info->values[i]=(double) (((u < 0) || (v < 0) ? -8.0 : 8.0)*
1707 exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
1708 (2.0*MagickPI*MagickSigma*MagickSigma));
1710 kernel_info->values[i]=0.0;
1715 emboss_image=ConvolveImage(image,kernel_info,exception);
1716 kernel_info=DestroyKernelInfo(kernel_info);
1717 if (emboss_image != (Image *) NULL)
1718 (void) EqualizeImage(emboss_image,exception);
1719 return(emboss_image);
1723 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1727 % G a u s s i a n B l u r I m a g e %
1731 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1733 % GaussianBlurImage() blurs an image. We convolve the image with a
1734 % Gaussian operator of the given radius and standard deviation (sigma).
1735 % For reasonable results, the radius should be larger than sigma. Use a
1736 % radius of 0 and GaussianBlurImage() selects a suitable radius for you
1738 % The format of the GaussianBlurImage method is:
1740 % Image *GaussianBlurImage(const Image *image,onst double radius,
1741 % const double sigma,ExceptionInfo *exception)
1743 % A description of each parameter follows:
1745 % o image: the image.
1747 % o radius: the radius of the Gaussian, in pixels, not counting the center
1750 % o sigma: the standard deviation of the Gaussian, in pixels.
1752 % o exception: return any errors or warnings in this structure.
1755 MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
1756 const double sigma,ExceptionInfo *exception)
1775 assert(image != (const Image *) NULL);
1776 assert(image->signature == MagickSignature);
1777 if (image->debug != MagickFalse)
1778 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1779 assert(exception != (ExceptionInfo *) NULL);
1780 assert(exception->signature == MagickSignature);
1781 width=GetOptimalKernelWidth2D(radius,sigma);
1782 kernel_info=AcquireKernelInfo((const char *) NULL);
1783 if (kernel_info == (KernelInfo *) NULL)
1784 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1785 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1786 kernel_info->width=width;
1787 kernel_info->height=width;
1788 kernel_info->signature=MagickSignature;
1789 kernel_info->values=(double *) AcquireAlignedMemory(
1790 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1791 if (kernel_info->values == (double *) NULL)
1793 kernel_info=DestroyKernelInfo(kernel_info);
1794 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1796 j=(ssize_t) kernel_info->width/2;
1798 for (v=(-j); v <= j; v++)
1800 for (u=(-j); u <= j; u++)
1802 kernel_info->values[i]=(double) (exp(-((double) u*u+v*v)/(2.0*
1803 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
1807 blur_image=ConvolveImage(image,kernel_info,exception);
1808 kernel_info=DestroyKernelInfo(kernel_info);
1813 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1817 % M o t i o n B l u r I m a g e %
1821 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1823 % MotionBlurImage() simulates motion blur. We convolve the image with a
1824 % Gaussian operator of the given radius and standard deviation (sigma).
1825 % For reasonable results, radius should be larger than sigma. Use a
1826 % radius of 0 and MotionBlurImage() selects a suitable radius for you.
1827 % Angle gives the angle of the blurring motion.
1829 % Andrew Protano contributed this effect.
1831 % The format of the MotionBlurImage method is:
1833 % Image *MotionBlurImage(const Image *image,const double radius,
1834 % const double sigma,const double angle,ExceptionInfo *exception)
1836 % A description of each parameter follows:
1838 % o image: the image.
1840 % o radius: the radius of the Gaussian, in pixels, not counting
1843 % o sigma: the standard deviation of the Gaussian, in pixels.
1845 % o angle: Apply the effect along this angle.
1847 % o exception: return any errors or warnings in this structure.
1851 static double *GetMotionBlurKernel(const size_t width,const double sigma)
1861 Generate a 1-D convolution kernel.
1863 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1864 kernel=(double *) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
1865 if (kernel == (double *) NULL)
1868 for (i=0; i < (ssize_t) width; i++)
1870 kernel[i]=(double) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
1871 MagickSigma)))/(MagickSQ2PI*MagickSigma));
1872 normalize+=kernel[i];
1874 for (i=0; i < (ssize_t) width; i++)
1875 kernel[i]/=normalize;
1879 MagickExport Image *MotionBlurImage(const Image *image,const double radius,
1880 const double sigma,const double angle,ExceptionInfo *exception)
1914 assert(image != (Image *) NULL);
1915 assert(image->signature == MagickSignature);
1916 if (image->debug != MagickFalse)
1917 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1918 assert(exception != (ExceptionInfo *) NULL);
1919 width=GetOptimalKernelWidth1D(radius,sigma);
1920 kernel=GetMotionBlurKernel(width,sigma);
1921 if (kernel == (double *) NULL)
1922 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1923 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
1924 if (offset == (OffsetInfo *) NULL)
1926 kernel=(double *) RelinquishAlignedMemory(kernel);
1927 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1929 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1930 if (blur_image == (Image *) NULL)
1932 kernel=(double *) RelinquishAlignedMemory(kernel);
1933 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1934 return((Image *) NULL);
1936 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
1938 kernel=(double *) RelinquishAlignedMemory(kernel);
1939 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1940 blur_image=DestroyImage(blur_image);
1941 return((Image *) NULL);
1943 point.x=(double) width*sin(DegreesToRadians(angle));
1944 point.y=(double) width*cos(DegreesToRadians(angle));
1945 for (i=0; i < (ssize_t) width; i++)
1947 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
1948 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
1955 image_view=AcquireVirtualCacheView(image,exception);
1956 motion_view=AcquireVirtualCacheView(image,exception);
1957 blur_view=AcquireAuthenticCacheView(blur_image,exception);
1958 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1959 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1960 dynamic_number_threads(image,image->columns,image->rows,1)
1962 for (y=0; y < (ssize_t) image->rows; y++)
1964 register const Quantum
1973 if (status == MagickFalse)
1975 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1976 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
1978 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1983 for (x=0; x < (ssize_t) image->columns; x++)
1988 if (GetPixelMask(image,p) != 0)
1990 p+=GetPixelChannels(image);
1991 q+=GetPixelChannels(blur_image);
1994 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2008 register const Quantum
2017 channel=GetPixelChannelMapChannel(image,i);
2018 traits=GetPixelChannelMapTraits(image,channel);
2019 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
2020 if ((traits == UndefinedPixelTrait) ||
2021 (blur_traits == UndefinedPixelTrait))
2023 if ((blur_traits & CopyPixelTrait) != 0)
2025 SetPixelChannel(blur_image,channel,p[i],q);
2030 if ((blur_traits & BlendPixelTrait) == 0)
2032 for (j=0; j < (ssize_t) width; j++)
2034 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+
2035 offset[j].y,1,1,exception);
2036 if (r == (const Quantum *) NULL)
2044 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
2049 for (j=0; j < (ssize_t) width; j++)
2051 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+offset[j].y,1,
2053 if (r == (const Quantum *) NULL)
2058 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,r));
2059 pixel+=(*k)*alpha*r[i];
2063 gamma=MagickEpsilonReciprocal(gamma);
2064 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2066 p+=GetPixelChannels(image);
2067 q+=GetPixelChannels(blur_image);
2069 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2071 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2076 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2077 #pragma omp critical (MagickCore_MotionBlurImage)
2079 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2080 if (proceed == MagickFalse)
2084 blur_view=DestroyCacheView(blur_view);
2085 motion_view=DestroyCacheView(motion_view);
2086 image_view=DestroyCacheView(image_view);
2087 kernel=(double *) RelinquishAlignedMemory(kernel);
2088 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2089 if (status == MagickFalse)
2090 blur_image=DestroyImage(blur_image);
2095 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2099 % P r e v i e w I m a g e %
2103 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2105 % PreviewImage() tiles 9 thumbnails of the specified image with an image
2106 % processing operation applied with varying parameters. This may be helpful
2107 % pin-pointing an appropriate parameter for a particular image processing
2110 % The format of the PreviewImages method is:
2112 % Image *PreviewImages(const Image *image,const PreviewType preview,
2113 % ExceptionInfo *exception)
2115 % A description of each parameter follows:
2117 % o image: the image.
2119 % o preview: the image processing operation.
2121 % o exception: return any errors or warnings in this structure.
2124 MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2125 ExceptionInfo *exception)
2127 #define NumberTiles 9
2128 #define PreviewImageTag "Preview/Image"
2129 #define DefaultPreviewGeometry "204x204+10+10"
2132 factor[MaxTextExtent],
2133 label[MaxTextExtent];
2178 Open output image file.
2180 assert(image != (Image *) NULL);
2181 assert(image->signature == MagickSignature);
2182 if (image->debug != MagickFalse)
2183 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2187 preview_info=AcquireImageInfo();
2188 SetGeometry(image,&geometry);
2189 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2190 &geometry.width,&geometry.height);
2191 images=NewImageList();
2193 GetQuantizeInfo(&quantize_info);
2199 for (i=0; i < NumberTiles; i++)
2201 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2202 if (thumbnail == (Image *) NULL)
2204 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2206 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel,exception);
2207 if (i == (NumberTiles/2))
2209 (void) QueryColorCompliance("#dfdfdf",AllCompliance,
2210 &thumbnail->matte_color,exception);
2211 AppendImageToList(&images,thumbnail);
2219 preview_image=RotateImage(thumbnail,degrees,exception);
2220 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
2226 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
2227 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",
2228 degrees,2.0*degrees);
2233 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2234 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
2235 preview_image=RollImage(thumbnail,x,y,exception);
2236 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
2237 (double) x,(double) y);
2242 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2243 if (preview_image == (Image *) NULL)
2245 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",
2247 (void) ModulateImage(preview_image,factor,exception);
2248 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2251 case SaturationPreview:
2253 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2254 if (preview_image == (Image *) NULL)
2256 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",
2258 (void) ModulateImage(preview_image,factor,exception);
2259 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2262 case BrightnessPreview:
2264 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2265 if (preview_image == (Image *) NULL)
2267 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
2268 (void) ModulateImage(preview_image,factor,exception);
2269 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2275 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2276 if (preview_image == (Image *) NULL)
2279 (void) GammaImage(preview_image,gamma,exception);
2280 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
2285 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2286 if (preview_image != (Image *) NULL)
2287 for (x=0; x < i; x++)
2288 (void) ContrastImage(preview_image,MagickTrue,exception);
2289 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
2295 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2296 if (preview_image == (Image *) NULL)
2298 for (x=0; x < i; x++)
2299 (void) ContrastImage(preview_image,MagickFalse,exception);
2300 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
2304 case GrayscalePreview:
2306 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2307 if (preview_image == (Image *) NULL)
2310 quantize_info.number_colors=colors;
2311 quantize_info.colorspace=GRAYColorspace;
2312 (void) QuantizeImage(&quantize_info,preview_image,exception);
2313 (void) FormatLocaleString(label,MaxTextExtent,
2314 "-colorspace gray -colors %.20g",(double) colors);
2317 case QuantizePreview:
2319 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2320 if (preview_image == (Image *) NULL)
2323 quantize_info.number_colors=colors;
2324 (void) QuantizeImage(&quantize_info,preview_image,exception);
2325 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
2329 case DespecklePreview:
2331 for (x=0; x < (i-1); x++)
2333 preview_image=DespeckleImage(thumbnail,exception);
2334 if (preview_image == (Image *) NULL)
2336 thumbnail=DestroyImage(thumbnail);
2337 thumbnail=preview_image;
2339 preview_image=DespeckleImage(thumbnail,exception);
2340 if (preview_image == (Image *) NULL)
2342 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
2346 case ReduceNoisePreview:
2348 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2349 (size_t) radius,exception);
2350 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
2353 case AddNoisePreview:
2359 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2364 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2369 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2374 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2379 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2384 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2389 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2393 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2394 (size_t) i,exception);
2395 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
2398 case SharpenPreview:
2400 preview_image=SharpenImage(thumbnail,radius,sigma,exception);
2401 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",
2407 preview_image=BlurImage(thumbnail,radius,sigma,exception);
2408 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
2412 case ThresholdPreview:
2414 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2415 if (preview_image == (Image *) NULL)
2417 (void) BilevelImage(thumbnail,(double) (percentage*((MagickRealType)
2418 QuantumRange+1.0))/100.0,exception);
2419 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",
2420 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
2423 case EdgeDetectPreview:
2425 preview_image=EdgeImage(thumbnail,radius,sigma,exception);
2426 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
2431 preview_image=SpreadImage(thumbnail,radius,thumbnail->interpolate,
2433 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",
2437 case SolarizePreview:
2439 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2440 if (preview_image == (Image *) NULL)
2442 (void) SolarizeImage(preview_image,(double) QuantumRange*
2443 percentage/100.0,exception);
2444 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
2445 (QuantumRange*percentage)/100.0);
2451 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2453 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",
2459 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2460 if (preview_image == (Image *) NULL)
2462 geometry.width=(size_t) (2*i+2);
2463 geometry.height=(size_t) (2*i+2);
2466 (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
2467 (void) FormatLocaleString(label,MaxTextExtent,
2468 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
2469 geometry.height,(double) geometry.x,(double) geometry.y);
2472 case SegmentPreview:
2474 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2475 if (preview_image == (Image *) NULL)
2478 (void) SegmentImage(preview_image,sRGBColorspace,MagickFalse,threshold,
2479 threshold,exception);
2480 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
2481 threshold,threshold);
2486 preview_image=SwirlImage(thumbnail,degrees,image->interpolate,
2488 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
2492 case ImplodePreview:
2495 preview_image=ImplodeImage(thumbnail,degrees,image->interpolate,
2497 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
2503 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,
2504 image->interpolate,exception);
2505 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",
2506 0.5*degrees,2.0*degrees);
2509 case OilPaintPreview:
2511 preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2513 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2517 case CharcoalDrawingPreview:
2519 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
2521 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2528 filename[MaxTextExtent];
2536 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2537 if (preview_image == (Image *) NULL)
2539 preview_info->quality=(size_t) percentage;
2540 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
2541 preview_info->quality);
2542 file=AcquireUniqueFileResource(filename);
2545 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
2546 "jpeg:%s",filename);
2547 status=WriteImage(preview_info,preview_image,exception);
2548 if (status != MagickFalse)
2553 (void) CopyMagickString(preview_info->filename,
2554 preview_image->filename,MaxTextExtent);
2555 quality_image=ReadImage(preview_info,exception);
2556 if (quality_image != (Image *) NULL)
2558 preview_image=DestroyImage(preview_image);
2559 preview_image=quality_image;
2562 (void) RelinquishUniqueFileResource(preview_image->filename);
2563 if ((GetBlobSize(preview_image)/1024) >= 1024)
2564 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
2565 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2568 if (GetBlobSize(preview_image) >= 1024)
2569 (void) FormatLocaleString(label,MaxTextExtent,
2570 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
2571 GetBlobSize(preview_image))/1024.0);
2573 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
2574 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
2578 thumbnail=DestroyImage(thumbnail);
2582 if (preview_image == (Image *) NULL)
2584 (void) DeleteImageProperty(preview_image,"label");
2585 (void) SetImageProperty(preview_image,"label",label,exception);
2586 AppendImageToList(&images,preview_image);
2587 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2589 if (proceed == MagickFalse)
2592 if (images == (Image *) NULL)
2594 preview_info=DestroyImageInfo(preview_info);
2595 return((Image *) NULL);
2600 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2601 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2602 montage_info->shadow=MagickTrue;
2603 (void) CloneString(&montage_info->tile,"3x3");
2604 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2605 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2606 montage_image=MontageImages(images,montage_info,exception);
2607 montage_info=DestroyMontageInfo(montage_info);
2608 images=DestroyImageList(images);
2609 if (montage_image == (Image *) NULL)
2610 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2611 if (montage_image->montage != (char *) NULL)
2614 Free image directory.
2616 montage_image->montage=(char *) RelinquishMagickMemory(
2617 montage_image->montage);
2618 if (image->directory != (char *) NULL)
2619 montage_image->directory=(char *) RelinquishMagickMemory(
2620 montage_image->directory);
2622 preview_info=DestroyImageInfo(preview_info);
2623 return(montage_image);
2627 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2631 % R a d i a l B l u r I m a g e %
2635 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2637 % RadialBlurImage() applies a radial blur to the image.
2639 % Andrew Protano contributed this effect.
2641 % The format of the RadialBlurImage method is:
2643 % Image *RadialBlurImage(const Image *image,const double angle,
2644 % ExceptionInfo *exception)
2646 % A description of each parameter follows:
2648 % o image: the image.
2650 % o angle: the angle of the radial blur.
2654 % o exception: return any errors or warnings in this structure.
2657 MagickExport Image *RadialBlurImage(const Image *image,const double angle,
2658 ExceptionInfo *exception)
2694 Allocate blur image.
2696 assert(image != (Image *) NULL);
2697 assert(image->signature == MagickSignature);
2698 if (image->debug != MagickFalse)
2699 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2700 assert(exception != (ExceptionInfo *) NULL);
2701 assert(exception->signature == MagickSignature);
2702 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2703 if (blur_image == (Image *) NULL)
2704 return((Image *) NULL);
2705 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2707 blur_image=DestroyImage(blur_image);
2708 return((Image *) NULL);
2710 blur_center.x=(double) image->columns/2.0;
2711 blur_center.y=(double) image->rows/2.0;
2712 blur_radius=hypot(blur_center.x,blur_center.y);
2713 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
2714 theta=DegreesToRadians(angle)/(MagickRealType) (n-1);
2715 cos_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2716 sizeof(*cos_theta));
2717 sin_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2718 sizeof(*sin_theta));
2719 if ((cos_theta == (MagickRealType *) NULL) ||
2720 (sin_theta == (MagickRealType *) NULL))
2722 blur_image=DestroyImage(blur_image);
2723 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2725 offset=theta*(MagickRealType) (n-1)/2.0;
2726 for (i=0; i < (ssize_t) n; i++)
2728 cos_theta[i]=cos((double) (theta*i-offset));
2729 sin_theta[i]=sin((double) (theta*i-offset));
2736 image_view=AcquireVirtualCacheView(image,exception);
2737 radial_view=AcquireVirtualCacheView(image,exception);
2738 blur_view=AcquireAuthenticCacheView(blur_image,exception);
2739 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2740 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2741 dynamic_number_threads(image,image->columns,image->rows,1)
2743 for (y=0; y < (ssize_t) image->rows; y++)
2745 register const Quantum
2754 if (status == MagickFalse)
2756 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2757 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2759 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2764 for (x=0; x < (ssize_t) image->columns; x++)
2778 center.x=(double) x-blur_center.x;
2779 center.y=(double) y-blur_center.y;
2780 radius=hypot((double) center.x,center.y);
2785 step=(size_t) (blur_radius/radius);
2792 if (GetPixelMask(image,p) != 0)
2794 p+=GetPixelChannels(image);
2795 q+=GetPixelChannels(blur_image);
2798 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2811 register const Quantum
2817 channel=GetPixelChannelMapChannel(image,i);
2818 traits=GetPixelChannelMapTraits(image,channel);
2819 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
2820 if ((traits == UndefinedPixelTrait) ||
2821 (blur_traits == UndefinedPixelTrait))
2823 if ((blur_traits & CopyPixelTrait) != 0)
2825 SetPixelChannel(blur_image,channel,p[i],q);
2830 if ((blur_traits & BlendPixelTrait) == 0)
2832 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2834 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2835 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2836 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2838 if (r == (const Quantum *) NULL)
2846 gamma=MagickEpsilonReciprocal(gamma);
2847 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2850 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2852 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2853 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2854 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2856 if (r == (const Quantum *) NULL)
2861 pixel+=GetPixelAlpha(image,r)*r[i];
2862 gamma+=GetPixelAlpha(image,r);
2864 gamma=MagickEpsilonReciprocal(gamma);
2865 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2867 p+=GetPixelChannels(image);
2868 q+=GetPixelChannels(blur_image);
2870 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2872 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2877 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2878 #pragma omp critical (MagickCore_RadialBlurImage)
2880 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2881 if (proceed == MagickFalse)
2885 blur_view=DestroyCacheView(blur_view);
2886 radial_view=DestroyCacheView(radial_view);
2887 image_view=DestroyCacheView(image_view);
2888 cos_theta=(MagickRealType *) RelinquishMagickMemory(cos_theta);
2889 sin_theta=(MagickRealType *) RelinquishMagickMemory(sin_theta);
2890 if (status == MagickFalse)
2891 blur_image=DestroyImage(blur_image);
2896 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2900 % S e l e c t i v e B l u r I m a g e %
2904 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2906 % SelectiveBlurImage() selectively blur pixels within a contrast threshold.
2907 % It is similar to the unsharpen mask that sharpens everything with contrast
2908 % above a certain threshold.
2910 % The format of the SelectiveBlurImage method is:
2912 % Image *SelectiveBlurImage(const Image *image,const double radius,
2913 % const double sigma,const double threshold,ExceptionInfo *exception)
2915 % A description of each parameter follows:
2917 % o image: the image.
2919 % o radius: the radius of the Gaussian, in pixels, not counting the center
2922 % o sigma: the standard deviation of the Gaussian, in pixels.
2924 % o threshold: only pixels within this contrast threshold are included
2925 % in the blur operation.
2927 % o exception: return any errors or warnings in this structure.
2930 MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
2931 const double sigma,const double threshold,ExceptionInfo *exception)
2933 #define SelectiveBlurImageTag "SelectiveBlur/Image"
2965 Initialize blur image attributes.
2967 assert(image != (Image *) NULL);
2968 assert(image->signature == MagickSignature);
2969 if (image->debug != MagickFalse)
2970 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2971 assert(exception != (ExceptionInfo *) NULL);
2972 assert(exception->signature == MagickSignature);
2973 width=GetOptimalKernelWidth1D(radius,sigma);
2974 kernel=(double *) AcquireAlignedMemory((size_t) width,width*sizeof(*kernel));
2975 if (kernel == (double *) NULL)
2976 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2977 j=(ssize_t) width/2;
2979 for (v=(-j); v <= j; v++)
2981 for (u=(-j); u <= j; u++)
2982 kernel[i++]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
2983 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
2985 if (image->debug != MagickFalse)
2988 format[MaxTextExtent],
2991 register const double
2998 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
2999 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
3001 message=AcquireString("");
3003 for (v=0; v < (ssize_t) width; v++)
3006 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
3007 (void) ConcatenateString(&message,format);
3008 for (u=0; u < (ssize_t) width; u++)
3010 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",*k++);
3011 (void) ConcatenateString(&message,format);
3013 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
3015 message=DestroyString(message);
3017 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3018 if (blur_image == (Image *) NULL)
3019 return((Image *) NULL);
3020 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
3022 blur_image=DestroyImage(blur_image);
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 blur_view=AcquireAuthenticCacheView(blur_image,exception);
3034 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3035 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3036 dynamic_number_threads(image,image->columns,image->rows,1)
3038 for (y=0; y < (ssize_t) image->rows; y++)
3046 register const Quantum
3055 if (status == MagickFalse)
3057 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
3058 (width/2L),image->columns+width,width,exception);
3059 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3061 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3066 for (x=0; x < (ssize_t) image->columns; x++)
3071 if (GetPixelMask(image,p) != 0)
3073 p+=GetPixelChannels(image);
3074 q+=GetPixelChannels(blur_image);
3077 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3092 register const double
3095 register const Quantum
3104 channel=GetPixelChannelMapChannel(image,i);
3105 traits=GetPixelChannelMapTraits(image,channel);
3106 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
3107 if ((traits == UndefinedPixelTrait) ||
3108 (blur_traits == UndefinedPixelTrait))
3110 if ((blur_traits & CopyPixelTrait) != 0)
3112 SetPixelChannel(blur_image,channel,p[center+i],q);
3118 intensity=(MagickRealType) GetPixelIntensity(image,p+center);
3120 if ((blur_traits & BlendPixelTrait) == 0)
3122 for (v=0; v < (ssize_t) width; v++)
3124 for (u=0; u < (ssize_t) width; u++)
3126 contrast=GetPixelIntensity(image,pixels)-intensity;
3127 if (fabs(contrast) < threshold)
3129 pixel+=(*k)*pixels[i];
3133 pixels+=GetPixelChannels(image);
3135 pixels+=image->columns*GetPixelChannels(image);
3137 if (fabs((double) gamma) < MagickEpsilon)
3139 SetPixelChannel(blur_image,channel,p[center+i],q);
3142 gamma=MagickEpsilonReciprocal(gamma);
3143 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3146 for (v=0; v < (ssize_t) width; v++)
3148 for (u=0; u < (ssize_t) width; u++)
3150 contrast=GetPixelIntensity(image,pixels)-intensity;
3151 if (fabs(contrast) < threshold)
3153 alpha=(MagickRealType) (QuantumScale*
3154 GetPixelAlpha(image,pixels));
3155 pixel+=(*k)*alpha*pixels[i];
3159 pixels+=GetPixelChannels(image);
3161 pixels+=image->columns*GetPixelChannels(image);
3163 if (fabs((double) gamma) < MagickEpsilon)
3165 SetPixelChannel(blur_image,channel,p[center+i],q);
3168 gamma=MagickEpsilonReciprocal(gamma);
3169 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3171 p+=GetPixelChannels(image);
3172 q+=GetPixelChannels(blur_image);
3174 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3175 if (sync == MagickFalse)
3177 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3182 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3183 #pragma omp critical (MagickCore_SelectiveBlurImage)
3185 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3187 if (proceed == MagickFalse)
3191 blur_image->type=image->type;
3192 blur_view=DestroyCacheView(blur_view);
3193 image_view=DestroyCacheView(image_view);
3194 kernel=(double *) RelinquishAlignedMemory(kernel);
3195 if (status == MagickFalse)
3196 blur_image=DestroyImage(blur_image);
3201 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3205 % S h a d e I m a g e %
3209 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3211 % ShadeImage() shines a distant light on an image to create a
3212 % three-dimensional effect. You control the positioning of the light with
3213 % azimuth and elevation; azimuth is measured in degrees off the x axis
3214 % and elevation is measured in pixels above the Z axis.
3216 % The format of the ShadeImage method is:
3218 % Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3219 % const double azimuth,const double elevation,ExceptionInfo *exception)
3221 % A description of each parameter follows:
3223 % o image: the image.
3225 % o gray: A value other than zero shades the intensity of each pixel.
3227 % o azimuth, elevation: Define the light source direction.
3229 % o exception: return any errors or warnings in this structure.
3232 MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3233 const double azimuth,const double elevation,ExceptionInfo *exception)
3235 #define ShadeImageTag "Shade/Image"
3257 Initialize shaded image attributes.
3259 assert(image != (const Image *) NULL);
3260 assert(image->signature == MagickSignature);
3261 if (image->debug != MagickFalse)
3262 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3263 assert(exception != (ExceptionInfo *) NULL);
3264 assert(exception->signature == MagickSignature);
3265 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3266 if (shade_image == (Image *) NULL)
3267 return((Image *) NULL);
3268 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
3270 shade_image=DestroyImage(shade_image);
3271 return((Image *) NULL);
3274 Compute the light vector.
3276 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3277 cos(DegreesToRadians(elevation));
3278 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3279 cos(DegreesToRadians(elevation));
3280 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3286 image_view=AcquireVirtualCacheView(image,exception);
3287 shade_view=AcquireAuthenticCacheView(shade_image,exception);
3288 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3289 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3290 dynamic_number_threads(image,image->columns,image->rows,1)
3292 for (y=0; y < (ssize_t) image->rows; y++)
3302 register const Quantum
3314 if (status == MagickFalse)
3316 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
3317 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3319 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3325 Shade this row of pixels.
3327 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
3328 pre=p+GetPixelChannels(image);
3329 center=pre+(image->columns+2)*GetPixelChannels(image);
3330 post=center+(image->columns+2)*GetPixelChannels(image);
3331 for (x=0; x < (ssize_t) image->columns; x++)
3337 Determine the surface normal and compute shading.
3339 normal.x=(double) (GetPixelIntensity(image,pre-GetPixelChannels(image))+
3340 GetPixelIntensity(image,center-GetPixelChannels(image))+
3341 GetPixelIntensity(image,post-GetPixelChannels(image))-
3342 GetPixelIntensity(image,pre+GetPixelChannels(image))-
3343 GetPixelIntensity(image,center+GetPixelChannels(image))-
3344 GetPixelIntensity(image,post+GetPixelChannels(image)));
3345 normal.y=(double) (GetPixelIntensity(image,post-GetPixelChannels(image))+
3346 GetPixelIntensity(image,post)+GetPixelIntensity(image,post+
3347 GetPixelChannels(image))-GetPixelIntensity(image,pre-
3348 GetPixelChannels(image))-GetPixelIntensity(image,pre)-
3349 GetPixelIntensity(image,pre+GetPixelChannels(image)));
3350 if ((normal.x == 0.0) && (normal.y == 0.0))
3355 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3356 if (distance > MagickEpsilon)
3359 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
3360 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3361 shade=distance/sqrt((double) normal_distance);
3364 if (GetPixelMask(image,p) != 0)
3366 pre+=GetPixelChannels(image);
3367 center+=GetPixelChannels(image);
3368 post+=GetPixelChannels(image);
3369 q+=GetPixelChannels(shade_image);
3372 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3381 channel=GetPixelChannelMapChannel(image,i);
3382 traits=GetPixelChannelMapTraits(image,channel);
3383 shade_traits=GetPixelChannelMapTraits(shade_image,channel);
3384 if ((traits == UndefinedPixelTrait) ||
3385 (shade_traits == UndefinedPixelTrait))
3387 if ((shade_traits & CopyPixelTrait) != 0)
3389 SetPixelChannel(shade_image,channel,center[i],q);
3392 if (gray != MagickFalse)
3394 SetPixelChannel(shade_image,channel,ClampToQuantum(shade),q);
3397 SetPixelChannel(shade_image,channel,ClampToQuantum(QuantumScale*shade*
3400 pre+=GetPixelChannels(image);
3401 center+=GetPixelChannels(image);
3402 post+=GetPixelChannels(image);
3403 q+=GetPixelChannels(shade_image);
3405 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3407 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3412 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3413 #pragma omp critical (MagickCore_ShadeImage)
3415 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3416 if (proceed == MagickFalse)
3420 shade_view=DestroyCacheView(shade_view);
3421 image_view=DestroyCacheView(image_view);
3422 if (status == MagickFalse)
3423 shade_image=DestroyImage(shade_image);
3424 return(shade_image);
3428 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3432 % S h a r p e n I m a g e %
3436 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3438 % SharpenImage() sharpens the image. We convolve the image with a Gaussian
3439 % operator of the given radius and standard deviation (sigma). For
3440 % reasonable results, radius should be larger than sigma. Use a radius of 0
3441 % and SharpenImage() selects a suitable radius for you.
3443 % Using a separable kernel would be faster, but the negative weights cancel
3444 % out on the corners of the kernel producing often undesirable ringing in the
3445 % filtered result; this can be avoided by using a 2D gaussian shaped image
3446 % sharpening kernel instead.
3448 % The format of the SharpenImage method is:
3450 % Image *SharpenImage(const Image *image,const double radius,
3451 % const double sigma,ExceptionInfo *exception)
3453 % A description of each parameter follows:
3455 % o image: the image.
3457 % o radius: the radius of the Gaussian, in pixels, not counting the center
3460 % o sigma: the standard deviation of the Laplacian, in pixels.
3462 % o exception: return any errors or warnings in this structure.
3465 MagickExport Image *SharpenImage(const Image *image,const double radius,
3466 const double sigma,ExceptionInfo *exception)
3488 assert(image != (const Image *) NULL);
3489 assert(image->signature == MagickSignature);
3490 if (image->debug != MagickFalse)
3491 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3492 assert(exception != (ExceptionInfo *) NULL);
3493 assert(exception->signature == MagickSignature);
3494 width=GetOptimalKernelWidth2D(radius,sigma);
3495 kernel_info=AcquireKernelInfo((const char *) NULL);
3496 if (kernel_info == (KernelInfo *) NULL)
3497 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3498 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3499 kernel_info->width=width;
3500 kernel_info->height=width;
3501 kernel_info->signature=MagickSignature;
3502 kernel_info->values=(double *) AcquireAlignedMemory(
3503 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
3504 if (kernel_info->values == (double *) NULL)
3506 kernel_info=DestroyKernelInfo(kernel_info);
3507 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3510 j=(ssize_t) kernel_info->width/2;
3512 for (v=(-j); v <= j; v++)
3514 for (u=(-j); u <= j; u++)
3516 kernel_info->values[i]=(double) (-exp(-((double) u*u+v*v)/(2.0*
3517 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3518 normalize+=kernel_info->values[i];
3522 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
3523 sharp_image=ConvolveImage(image,kernel_info,exception);
3524 kernel_info=DestroyKernelInfo(kernel_info);
3525 return(sharp_image);
3529 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3533 % S p r e a d I m a g e %
3537 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3539 % SpreadImage() is a special effects method that randomly displaces each
3540 % pixel in a block defined by the radius parameter.
3542 % The format of the SpreadImage method is:
3544 % Image *SpreadImage(const Image *image,const double radius,
3545 % const PixelInterpolateMethod method,ExceptionInfo *exception)
3547 % A description of each parameter follows:
3549 % o image: the image.
3551 % o radius: choose a random pixel in a neighborhood of this extent.
3553 % o method: the pixel interpolation method.
3555 % o exception: return any errors or warnings in this structure.
3558 MagickExport Image *SpreadImage(const Image *image,const double radius,
3559 const PixelInterpolateMethod method,ExceptionInfo *exception)
3561 #define SpreadImageTag "Spread/Image"
3577 **restrict random_info;
3589 Initialize spread image attributes.
3591 assert(image != (Image *) NULL);
3592 assert(image->signature == MagickSignature);
3593 if (image->debug != MagickFalse)
3594 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3595 assert(exception != (ExceptionInfo *) NULL);
3596 assert(exception->signature == MagickSignature);
3597 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3599 if (spread_image == (Image *) NULL)
3600 return((Image *) NULL);
3601 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
3603 spread_image=DestroyImage(spread_image);
3604 return((Image *) NULL);
3611 width=GetOptimalKernelWidth1D(radius,0.5);
3612 random_info=AcquireRandomInfoThreadSet();
3613 key=GetRandomSecretKey(random_info[0]);
3614 image_view=AcquireVirtualCacheView(image,exception);
3615 spread_view=AcquireAuthenticCacheView(spread_image,exception);
3616 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3617 #pragma omp parallel for schedule(static,8) shared(progress,status) \
3618 dynamic_number_threads(image,image->columns,image->rows,key == ~0UL)
3620 for (y=0; y < (ssize_t) image->rows; y++)
3623 id = GetOpenMPThreadId();
3625 register const Quantum
3634 if (status == MagickFalse)
3636 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3637 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
3639 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3644 for (x=0; x < (ssize_t) image->columns; x++)
3649 point.x=GetPseudoRandomValue(random_info[id]);
3650 point.y=GetPseudoRandomValue(random_info[id]);
3651 status=InterpolatePixelChannels(image,image_view,spread_image,method,
3652 (double) x+width*point.x-0.5,(double) y+width*point.y-0.5,q,exception);
3653 q+=GetPixelChannels(spread_image);
3655 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
3657 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3662 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3663 #pragma omp critical (MagickCore_SpreadImage)
3665 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3666 if (proceed == MagickFalse)
3670 spread_view=DestroyCacheView(spread_view);
3671 image_view=DestroyCacheView(image_view);
3672 random_info=DestroyRandomInfoThreadSet(random_info);
3673 return(spread_image);
3677 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3681 % U n s h a r p M a s k I m a g e %
3685 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3687 % UnsharpMaskImage() sharpens one or more image channels. We convolve the
3688 % image with a Gaussian operator of the given radius and standard deviation
3689 % (sigma). For reasonable results, radius should be larger than sigma. Use a
3690 % radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
3692 % The format of the UnsharpMaskImage method is:
3694 % Image *UnsharpMaskImage(const Image *image,const double radius,
3695 % const double sigma,const double amount,const double threshold,
3696 % ExceptionInfo *exception)
3698 % A description of each parameter follows:
3700 % o image: the image.
3702 % o radius: the radius of the Gaussian, in pixels, not counting the center
3705 % o sigma: the standard deviation of the Gaussian, in pixels.
3707 % o amount: the percentage of the difference between the original and the
3708 % blur image that is added back into the original.
3710 % o threshold: the threshold in pixels needed to apply the diffence amount.
3712 % o exception: return any errors or warnings in this structure.
3715 MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
3716 const double sigma,const double amount,const double threshold,
3717 ExceptionInfo *exception)
3719 #define SharpenImageTag "Sharpen/Image"
3740 assert(image != (const Image *) NULL);
3741 assert(image->signature == MagickSignature);
3742 if (image->debug != MagickFalse)
3743 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3744 assert(exception != (ExceptionInfo *) NULL);
3745 unsharp_image=BlurImage(image,radius,sigma,exception);
3746 if (unsharp_image == (Image *) NULL)
3747 return((Image *) NULL);
3748 quantum_threshold=(MagickRealType) QuantumRange*threshold;
3754 image_view=AcquireVirtualCacheView(image,exception);
3755 unsharp_view=AcquireAuthenticCacheView(unsharp_image,exception);
3756 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3757 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3758 dynamic_number_threads(image,image->columns,image->rows,1)
3760 for (y=0; y < (ssize_t) image->rows; y++)
3762 register const Quantum
3771 if (status == MagickFalse)
3773 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3774 q=QueueCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
3776 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3781 for (x=0; x < (ssize_t) image->columns; x++)
3786 if (GetPixelMask(image,p) != 0)
3788 p+=GetPixelChannels(image);
3789 q+=GetPixelChannels(unsharp_image);
3792 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3804 channel=GetPixelChannelMapChannel(image,i);
3805 traits=GetPixelChannelMapTraits(image,channel);
3806 unsharp_traits=GetPixelChannelMapTraits(unsharp_image,channel);
3807 if ((traits == UndefinedPixelTrait) ||
3808 (unsharp_traits == UndefinedPixelTrait))
3810 if ((unsharp_traits & CopyPixelTrait) != 0)
3812 SetPixelChannel(unsharp_image,channel,p[i],q);
3815 pixel=p[i]-(MagickRealType) GetPixelChannel(unsharp_image,channel,q);
3816 if (fabs(2.0*pixel) < quantum_threshold)
3817 pixel=(MagickRealType) p[i];
3819 pixel=(MagickRealType) p[i]+amount*pixel;
3820 SetPixelChannel(unsharp_image,channel,ClampToQuantum(pixel),q);
3822 p+=GetPixelChannels(image);
3823 q+=GetPixelChannels(unsharp_image);
3825 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
3827 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3832 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3833 #pragma omp critical (MagickCore_UnsharpMaskImage)
3835 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
3836 if (proceed == MagickFalse)
3840 unsharp_image->type=image->type;
3841 unsharp_view=DestroyCacheView(unsharp_view);
3842 image_view=DestroyCacheView(image_view);
3843 if (status == MagickFalse)
3844 unsharp_image=DestroyImage(unsharp_image);
3845 return(unsharp_image);