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/property.h"
73 #include "MagickCore/quantize.h"
74 #include "MagickCore/quantum.h"
75 #include "MagickCore/quantum-private.h"
76 #include "MagickCore/random_.h"
77 #include "MagickCore/random-private.h"
78 #include "MagickCore/resample.h"
79 #include "MagickCore/resample-private.h"
80 #include "MagickCore/resize.h"
81 #include "MagickCore/resource_.h"
82 #include "MagickCore/segment.h"
83 #include "MagickCore/shear.h"
84 #include "MagickCore/signature-private.h"
85 #include "MagickCore/statistic.h"
86 #include "MagickCore/string_.h"
87 #include "MagickCore/thread-private.h"
88 #include "MagickCore/transform.h"
89 #include "MagickCore/threshold.h"
92 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96 % A d a p t i v e B l u r I m a g e %
100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102 % AdaptiveBlurImage() adaptively blurs the image by blurring less
103 % intensely near image edges and more intensely far from edges. We blur the
104 % image with a Gaussian operator of the given radius and standard deviation
105 % (sigma). For reasonable results, radius should be larger than sigma. Use a
106 % radius of 0 and AdaptiveBlurImage() selects a suitable radius for you.
108 % The format of the AdaptiveBlurImage method is:
110 % Image *AdaptiveBlurImage(const Image *image,const double radius,
111 % const double sigma,const double bias,ExceptionInfo *exception)
113 % A description of each parameter follows:
115 % o image: the image.
117 % o radius: the radius of the Gaussian, in pixels, not counting the center
120 % 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,const double bias,ExceptionInfo *exception)
175 #define AdaptiveBlurImageTag "Convolve/Image"
176 #define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : 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=(double **) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
249 if (kernel == (double **) NULL)
251 edge_image=DestroyImage(edge_image);
252 blur_image=DestroyImage(blur_image);
253 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
255 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
256 for (i=0; i < (ssize_t) width; i+=2)
258 kernel[i]=(double *) AcquireAlignedMemory((size_t) (width-i),(width-i)*
260 if (kernel[i] == (double *) NULL)
263 j=(ssize_t) (width-i)/2;
265 for (v=(-j); v <= j; v++)
267 for (u=(-j); u <= j; u++)
269 kernel[i][k]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
270 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
271 normalize+=kernel[i][k];
275 if (fabs(normalize) <= MagickEpsilon)
277 normalize=1.0/normalize;
278 for (k=0; k < (j*j); k++)
279 kernel[i][k]=normalize*kernel[i][k];
281 if (i < (ssize_t) width)
283 for (i-=2; i >= 0; i-=2)
284 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
285 kernel=(double **) RelinquishAlignedMemory(kernel);
286 edge_image=DestroyImage(edge_image);
287 blur_image=DestroyImage(blur_image);
288 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
291 Adaptively blur image.
295 image_view=AcquireCacheView(image);
296 edge_view=AcquireCacheView(edge_image);
297 blur_view=AcquireCacheView(blur_image);
298 #if defined(MAGICKCORE_OPENMP_SUPPORT)
299 #pragma omp parallel for schedule(static,4) shared(progress,status)
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 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
363 register const double
366 register const Quantum
375 channel=GetPixelChannelMapChannel(image,i);
376 traits=GetPixelChannelMapTraits(image,channel);
377 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
378 if ((traits == UndefinedPixelTrait) ||
379 (blur_traits == UndefinedPixelTrait))
381 if (((blur_traits & CopyPixelTrait) != 0) ||
382 (GetPixelMask(image,q) != 0))
384 SetPixelChannel(blur_image,channel,p[center+i],q);
391 if ((blur_traits & BlendPixelTrait) == 0)
396 for (v=0; v < (ssize_t) (width-j); v++)
398 for (u=0; u < (ssize_t) (width-j); u++)
400 pixel+=(*k)*pixels[i];
403 pixels+=GetPixelChannels(image);
406 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
407 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
413 for (v=0; v < (ssize_t) (width-j); v++)
415 for (u=0; u < (ssize_t) (width-j); u++)
417 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
418 pixel+=(*k)*alpha*pixels[i];
421 pixels+=GetPixelChannels(image);
424 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
425 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
427 q+=GetPixelChannels(blur_image);
428 r+=GetPixelChannels(edge_image);
430 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
432 if (image->progress_monitor != (MagickProgressMonitor) NULL)
437 #if defined(MAGICKCORE_OPENMP_SUPPORT)
438 #pragma omp critical (MagickCore_AdaptiveBlurImage)
440 proceed=SetImageProgress(image,AdaptiveBlurImageTag,progress++,
442 if (proceed == MagickFalse)
446 blur_image->type=image->type;
447 blur_view=DestroyCacheView(blur_view);
448 edge_view=DestroyCacheView(edge_view);
449 image_view=DestroyCacheView(image_view);
450 edge_image=DestroyImage(edge_image);
451 for (i=0; i < (ssize_t) width; i+=2)
452 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
453 kernel=(double **) RelinquishAlignedMemory(kernel);
454 if (status == MagickFalse)
455 blur_image=DestroyImage(blur_image);
460 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
464 % A d a p t i v e S h a r p e n I m a g e %
468 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
470 % AdaptiveSharpenImage() adaptively sharpens the image by sharpening more
471 % intensely near image edges and less intensely far from edges. We sharpen the
472 % image with a Gaussian operator of the given radius and standard deviation
473 % (sigma). For reasonable results, radius should be larger than sigma. Use a
474 % radius of 0 and AdaptiveSharpenImage() selects a suitable radius for you.
476 % The format of the AdaptiveSharpenImage method is:
478 % Image *AdaptiveSharpenImage(const Image *image,const double radius,
479 % const double sigma,const double bias,ExceptionInfo *exception)
481 % A description of each parameter follows:
483 % o image: the image.
485 % o radius: the radius of the Gaussian, in pixels, not counting the center
488 % 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,const double bias,ExceptionInfo *exception)
498 #define AdaptiveSharpenImageTag "Convolve/Image"
499 #define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : 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=(double **) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
572 if (kernel == (double **) NULL)
574 edge_image=DestroyImage(edge_image);
575 sharp_image=DestroyImage(sharp_image);
576 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
578 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
579 for (i=0; i < (ssize_t) width; i+=2)
581 kernel[i]=(double *) AcquireAlignedMemory((size_t) (width-i),(width-i)*
583 if (kernel[i] == (double *) NULL)
586 j=(ssize_t) (width-i)/2;
588 for (v=(-j); v <= j; v++)
590 for (u=(-j); u <= j; u++)
592 kernel[i][k]=(double) (-exp(-((double) u*u+v*v)/(2.0*MagickSigma*
593 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
594 normalize+=kernel[i][k];
598 if (fabs(normalize) <= MagickEpsilon)
600 normalize=1.0/normalize;
601 for (k=0; k < (j*j); k++)
602 kernel[i][k]=normalize*kernel[i][k];
604 if (i < (ssize_t) width)
606 for (i-=2; i >= 0; i-=2)
607 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
608 kernel=(double **) RelinquishAlignedMemory(kernel);
609 edge_image=DestroyImage(edge_image);
610 sharp_image=DestroyImage(sharp_image);
611 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
614 Adaptively sharpen image.
618 image_view=AcquireCacheView(image);
619 edge_view=AcquireCacheView(edge_image);
620 sharp_view=AcquireCacheView(sharp_image);
621 #if defined(MAGICKCORE_OPENMP_SUPPORT)
622 #pragma omp parallel for schedule(static,4) shared(progress,status)
624 for (y=0; y < (ssize_t) sharp_image->rows; y++)
626 register const Quantum
635 if (status == MagickFalse)
637 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
638 q=QueueCacheViewAuthenticPixels(sharp_view,0,y,sharp_image->columns,1,
640 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
645 for (x=0; x < (ssize_t) sharp_image->columns; x++)
647 register const Quantum
657 j=(ssize_t) ceil((double) width*QuantumScale*
658 GetPixelIntensity(edge_image,r)-0.5);
662 if (j > (ssize_t) width)
666 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
667 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
668 if (p == (const Quantum *) NULL)
670 center=(ssize_t) GetPixelChannels(image)*(width-j)*((width-j)/2L)+
671 GetPixelChannels(image)*((width-j)/2);
672 for (i=0; i < (ssize_t) GetPixelChannels(sharp_image); i++)
686 register const double
689 register const Quantum
698 channel=GetPixelChannelMapChannel(image,i);
699 traits=GetPixelChannelMapTraits(image,channel);
700 sharp_traits=GetPixelChannelMapTraits(sharp_image,channel);
701 if ((traits == UndefinedPixelTrait) ||
702 (sharp_traits == UndefinedPixelTrait))
704 if (((sharp_traits & CopyPixelTrait) != 0) ||
705 (GetPixelMask(image,q) != 0))
707 SetPixelChannel(sharp_image,channel,p[center+i],q);
714 if ((sharp_traits & BlendPixelTrait) == 0)
719 for (v=0; v < (ssize_t) (width-j); v++)
721 for (u=0; u < (ssize_t) (width-j); u++)
723 pixel+=(*k)*pixels[i];
726 pixels+=GetPixelChannels(image);
729 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
730 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
736 for (v=0; v < (ssize_t) (width-j); v++)
738 for (u=0; u < (ssize_t) (width-j); u++)
740 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
741 pixel+=(*k)*alpha*pixels[i];
744 pixels+=GetPixelChannels(image);
747 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
748 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
750 q+=GetPixelChannels(sharp_image);
751 r+=GetPixelChannels(edge_image);
753 if (SyncCacheViewAuthenticPixels(sharp_view,exception) == MagickFalse)
755 if (image->progress_monitor != (MagickProgressMonitor) NULL)
760 #if defined(MAGICKCORE_OPENMP_SUPPORT)
761 #pragma omp critical (MagickCore_AdaptiveSharpenImage)
763 proceed=SetImageProgress(image,AdaptiveSharpenImageTag,progress++,
765 if (proceed == MagickFalse)
769 sharp_image->type=image->type;
770 sharp_view=DestroyCacheView(sharp_view);
771 edge_view=DestroyCacheView(edge_view);
772 image_view=DestroyCacheView(image_view);
773 edge_image=DestroyImage(edge_image);
774 for (i=0; i < (ssize_t) width; i+=2)
775 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
776 kernel=(double **) RelinquishAlignedMemory(kernel);
777 if (status == MagickFalse)
778 sharp_image=DestroyImage(sharp_image);
783 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
787 % B l u r I m a g e %
791 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
793 % BlurImage() blurs an image. We convolve the image with a Gaussian operator
794 % of the given radius and standard deviation (sigma). For reasonable results,
795 % the radius should be larger than sigma. Use a radius of 0 and BlurImage()
796 % selects a suitable radius for you.
798 % BlurImage() differs from GaussianBlurImage() in that it uses a separable
799 % kernel which is faster but mathematically equivalent to the non-separable
802 % The format of the BlurImage method is:
804 % Image *BlurImage(const Image *image,const double radius,
805 % const double sigma,const double bias,ExceptionInfo *exception)
807 % A description of each parameter follows:
809 % o image: the image.
811 % o radius: the radius of the Gaussian, in pixels, not counting the center
814 % o sigma: the standard deviation of the Gaussian, in pixels.
818 % o exception: return any errors or warnings in this structure.
822 static double *GetBlurKernel(const size_t width,const double sigma)
836 Generate a 1-D convolution kernel.
838 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
839 kernel=(double *) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
840 if (kernel == (double *) NULL)
845 for (k=(-j); k <= j; k++)
847 kernel[i]=(double) (exp(-((double) k*k)/(2.0*MagickSigma*MagickSigma))/
848 (MagickSQ2PI*MagickSigma));
849 normalize+=kernel[i];
852 for (i=0; i < (ssize_t) width; i++)
853 kernel[i]/=normalize;
857 MagickExport Image *BlurImage(const Image *image,const double radius,
858 const double sigma,const double bias,ExceptionInfo *exception)
860 #define BlurImageTag "Blur/Image"
890 Initialize blur image attributes.
892 assert(image != (Image *) NULL);
893 assert(image->signature == MagickSignature);
894 if (image->debug != MagickFalse)
895 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
896 assert(exception != (ExceptionInfo *) NULL);
897 assert(exception->signature == MagickSignature);
898 blur_image=CloneImage(image,0,0,MagickTrue,exception);
899 if (blur_image == (Image *) NULL)
900 return((Image *) NULL);
901 if (fabs(sigma) <= MagickEpsilon)
903 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
905 blur_image=DestroyImage(blur_image);
906 return((Image *) NULL);
908 width=GetOptimalKernelWidth1D(radius,sigma);
909 kernel=GetBlurKernel(width,sigma);
910 if (kernel == (double *) NULL)
912 blur_image=DestroyImage(blur_image);
913 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
915 if (image->debug != MagickFalse)
918 format[MaxTextExtent],
921 register const double
924 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
925 " blur image with kernel width %.20g:",(double) width);
926 message=AcquireString("");
928 for (i=0; i < (ssize_t) width; i++)
931 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) i);
932 (void) ConcatenateString(&message,format);
933 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
934 (void) ConcatenateString(&message,format);
935 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
937 message=DestroyString(message);
944 center=(ssize_t) GetPixelChannels(image)*(width/2L);
945 image_view=AcquireCacheView(image);
946 blur_view=AcquireCacheView(blur_image);
947 #if defined(MAGICKCORE_OPENMP_SUPPORT)
948 #pragma omp parallel for schedule(static,4) shared(progress,status)
950 for (y=0; y < (ssize_t) image->rows; y++)
952 register const Quantum
961 if (status == MagickFalse)
963 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y,
964 image->columns+width,1,exception);
965 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
967 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
972 for (x=0; x < (ssize_t) image->columns; x++)
977 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
991 register const double
994 register const Quantum
1000 channel=GetPixelChannelMapChannel(image,i);
1001 traits=GetPixelChannelMapTraits(image,channel);
1002 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
1003 if ((traits == UndefinedPixelTrait) ||
1004 (blur_traits == UndefinedPixelTrait))
1006 if (((blur_traits & CopyPixelTrait) != 0) ||
1007 (GetPixelMask(image,p) != 0))
1009 SetPixelChannel(blur_image,channel,p[center+i],q);
1015 if ((blur_traits & BlendPixelTrait) == 0)
1020 for (u=0; u < (ssize_t) width; u++)
1022 pixel+=(*k)*pixels[i];
1024 pixels+=GetPixelChannels(image);
1026 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
1033 for (u=0; u < (ssize_t) width; u++)
1035 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
1036 pixel+=(*k)*alpha*pixels[i];
1039 pixels+=GetPixelChannels(image);
1041 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1042 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
1044 p+=GetPixelChannels(image);
1045 q+=GetPixelChannels(blur_image);
1047 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1049 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1054 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1055 #pragma omp critical (MagickCore_BlurImage)
1057 proceed=SetImageProgress(image,BlurImageTag,progress++,blur_image->rows+
1058 blur_image->columns);
1059 if (proceed == MagickFalse)
1063 blur_view=DestroyCacheView(blur_view);
1064 image_view=DestroyCacheView(image_view);
1068 center=(ssize_t) GetPixelChannels(blur_image)*(width/2L);
1069 image_view=AcquireCacheView(blur_image);
1070 blur_view=AcquireCacheView(blur_image);
1071 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1072 #pragma omp parallel for schedule(static,4) shared(progress,status)
1074 for (x=0; x < (ssize_t) blur_image->columns; x++)
1076 register const Quantum
1085 if (status == MagickFalse)
1087 p=GetCacheViewVirtualPixels(image_view,x,-((ssize_t) width/2L),1,
1088 blur_image->rows+width,exception);
1089 q=GetCacheViewAuthenticPixels(blur_view,x,0,1,blur_image->rows,exception);
1090 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1095 for (y=0; y < (ssize_t) blur_image->rows; y++)
1100 for (i=0; i < (ssize_t) GetPixelChannels(blur_image); i++)
1114 register const double
1117 register const Quantum
1123 channel=GetPixelChannelMapChannel(blur_image,i);
1124 traits=GetPixelChannelMapTraits(blur_image,channel);
1125 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
1126 if ((traits == UndefinedPixelTrait) ||
1127 (blur_traits == UndefinedPixelTrait))
1129 if (((blur_traits & CopyPixelTrait) != 0) ||
1130 (GetPixelMask(blur_image,p) != 0))
1132 SetPixelChannel(blur_image,channel,p[center+i],q);
1138 if ((blur_traits & BlendPixelTrait) == 0)
1143 for (u=0; u < (ssize_t) width; u++)
1145 pixel+=(*k)*pixels[i];
1147 pixels+=GetPixelChannels(blur_image);
1149 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
1156 for (u=0; u < (ssize_t) width; u++)
1158 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(blur_image,
1160 pixel+=(*k)*alpha*pixels[i];
1163 pixels+=GetPixelChannels(blur_image);
1165 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1166 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
1168 p+=GetPixelChannels(blur_image);
1169 q+=GetPixelChannels(blur_image);
1171 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1173 if (blur_image->progress_monitor != (MagickProgressMonitor) NULL)
1178 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1179 #pragma omp critical (MagickCore_BlurImage)
1181 proceed=SetImageProgress(blur_image,BlurImageTag,progress++,
1182 blur_image->rows+blur_image->columns);
1183 if (proceed == MagickFalse)
1187 blur_view=DestroyCacheView(blur_view);
1188 image_view=DestroyCacheView(image_view);
1189 kernel=(double *) RelinquishAlignedMemory(kernel);
1190 blur_image->type=image->type;
1191 if (status == MagickFalse)
1192 blur_image=DestroyImage(blur_image);
1197 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1201 % C o n v o l v e I m a g e %
1205 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1207 % ConvolveImage() applies a custom convolution kernel to the image.
1209 % The format of the ConvolveImage method is:
1211 % Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
1212 % ExceptionInfo *exception)
1214 % A description of each parameter follows:
1216 % o image: the image.
1218 % o kernel: the filtering kernel.
1220 % o exception: return any errors or warnings in this structure.
1223 MagickExport Image *ConvolveImage(const Image *image,
1224 const KernelInfo *kernel_info,ExceptionInfo *exception)
1226 return(MorphologyImage(image,ConvolveMorphology,1,kernel_info,exception));
1230 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1234 % D e s p e c k l e I m a g e %
1238 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1240 % DespeckleImage() reduces the speckle noise in an image while perserving the
1241 % edges of the original image. A speckle removing filter uses a complementary % hulling technique (raising pixels that are darker than their surrounding
1242 % neighbors, then complementarily lowering pixels that are brighter than their
1243 % surrounding neighbors) to reduce the speckle index of that image (reference
1244 % Crimmins speckle removal).
1246 % The format of the DespeckleImage method is:
1248 % Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1250 % A description of each parameter follows:
1252 % o image: the image.
1254 % o exception: return any errors or warnings in this structure.
1258 static void Hull(const ssize_t x_offset,const ssize_t y_offset,
1259 const size_t columns,const size_t rows,const int polarity,Quantum *restrict f,
1260 Quantum *restrict g)
1271 assert(f != (Quantum *) NULL);
1272 assert(g != (Quantum *) NULL);
1275 r=p+(y_offset*(columns+2)+x_offset);
1276 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1277 #pragma omp parallel for schedule(static)
1279 for (y=0; y < (ssize_t) rows; y++)
1288 i=(2*y+1)+y*columns;
1290 for (x=0; x < (ssize_t) columns; x++)
1292 v=(SignedQuantum) p[i];
1293 if ((SignedQuantum) r[i] >= (v+ScaleCharToQuantum(2)))
1294 v+=ScaleCharToQuantum(1);
1299 for (x=0; x < (ssize_t) columns; x++)
1301 v=(SignedQuantum) p[i];
1302 if ((SignedQuantum) r[i] <= (v-ScaleCharToQuantum(2)))
1303 v-=ScaleCharToQuantum(1);
1310 r=q+(y_offset*(columns+2)+x_offset);
1311 s=q-(y_offset*(columns+2)+x_offset);
1312 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1313 #pragma omp parallel for schedule(static)
1315 for (y=0; y < (ssize_t) rows; y++)
1324 i=(2*y+1)+y*columns;
1326 for (x=0; x < (ssize_t) columns; x++)
1328 v=(SignedQuantum) q[i];
1329 if (((SignedQuantum) s[i] >= (v+ScaleCharToQuantum(2))) &&
1330 ((SignedQuantum) r[i] > v))
1331 v+=ScaleCharToQuantum(1);
1336 for (x=0; x < (ssize_t) columns; x++)
1338 v=(SignedQuantum) q[i];
1339 if (((SignedQuantum) s[i] <= (v-ScaleCharToQuantum(2))) &&
1340 ((SignedQuantum) r[i] < v))
1341 v-=ScaleCharToQuantum(1);
1348 MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1350 #define DespeckleImageTag "Despeckle/Image"
1372 static const ssize_t
1373 X[4] = {0, 1, 1,-1},
1374 Y[4] = {1, 0, 1, 1};
1377 Allocate despeckled image.
1379 assert(image != (const Image *) NULL);
1380 assert(image->signature == MagickSignature);
1381 if (image->debug != MagickFalse)
1382 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1383 assert(exception != (ExceptionInfo *) NULL);
1384 assert(exception->signature == MagickSignature);
1385 despeckle_image=CloneImage(image,0,0,MagickTrue,exception);
1386 if (despeckle_image == (Image *) NULL)
1387 return((Image *) NULL);
1388 status=SetImageStorageClass(despeckle_image,DirectClass,exception);
1389 if (status == MagickFalse)
1391 despeckle_image=DestroyImage(despeckle_image);
1392 return((Image *) NULL);
1395 Allocate image buffer.
1397 length=(size_t) ((image->columns+2)*(image->rows+2));
1398 pixels=(Quantum *) AcquireQuantumMemory(length,sizeof(*pixels));
1399 buffer=(Quantum *) AcquireQuantumMemory(length,sizeof(*buffer));
1400 if ((pixels == (Quantum *) NULL) || (buffer == (Quantum *) NULL))
1402 if (buffer != (Quantum *) NULL)
1403 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1404 if (pixels != (Quantum *) NULL)
1405 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1406 despeckle_image=DestroyImage(despeckle_image);
1407 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1410 Reduce speckle in the image.
1413 image_view=AcquireCacheView(image);
1414 despeckle_view=AcquireCacheView(despeckle_image);
1415 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1432 if (status == MagickFalse)
1434 channel=GetPixelChannelMapChannel(image,i);
1435 traits=GetPixelChannelMapTraits(image,channel);
1436 despeckle_traits=GetPixelChannelMapTraits(despeckle_image,channel);
1437 if ((traits == UndefinedPixelTrait) ||
1438 (despeckle_traits == UndefinedPixelTrait))
1440 if ((despeckle_traits & CopyPixelTrait) != 0)
1442 (void) ResetMagickMemory(pixels,0,length*sizeof(*pixels));
1443 j=(ssize_t) image->columns+2;
1444 for (y=0; y < (ssize_t) image->rows; y++)
1446 register const Quantum
1449 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1450 if (p == (const Quantum *) NULL)
1456 for (x=0; x < (ssize_t) image->columns; x++)
1459 p+=GetPixelChannels(image);
1463 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
1464 for (k=0; k < 4; k++)
1466 Hull(X[k],Y[k],image->columns,image->rows,1,pixels,buffer);
1467 Hull(-X[k],-Y[k],image->columns,image->rows,1,pixels,buffer);
1468 Hull(-X[k],-Y[k],image->columns,image->rows,-1,pixels,buffer);
1469 Hull(X[k],Y[k],image->columns,image->rows,-1,pixels,buffer);
1471 j=(ssize_t) image->columns+2;
1472 for (y=0; y < (ssize_t) image->rows; y++)
1480 q=QueueCacheViewAuthenticPixels(despeckle_view,0,y,
1481 despeckle_image->columns,1,exception);
1482 if (q == (Quantum *) NULL)
1488 for (x=0; x < (ssize_t) image->columns; x++)
1490 SetPixelChannel(despeckle_image,channel,pixels[j++],q);
1491 q+=GetPixelChannels(despeckle_image);
1493 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1494 if (sync == MagickFalse)
1498 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1503 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1504 GetPixelChannels(image));
1505 if (proceed == MagickFalse)
1509 despeckle_view=DestroyCacheView(despeckle_view);
1510 image_view=DestroyCacheView(image_view);
1511 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1512 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1513 despeckle_image->type=image->type;
1514 if (status == MagickFalse)
1515 despeckle_image=DestroyImage(despeckle_image);
1516 return(despeckle_image);
1520 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1524 % E d g e I m a g e %
1528 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1530 % EdgeImage() finds edges in an image. Radius defines the radius of the
1531 % convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1534 % The format of the EdgeImage method is:
1536 % Image *EdgeImage(const Image *image,const double radius,
1537 % const double sigma,ExceptionInfo *exception)
1539 % A description of each parameter follows:
1541 % o image: the image.
1543 % o radius: the radius of the pixel neighborhood.
1545 % o sigma: the standard deviation of the Gaussian, in pixels.
1547 % o exception: return any errors or warnings in this structure.
1550 MagickExport Image *EdgeImage(const Image *image,const double radius,
1551 const double sigma,ExceptionInfo *exception)
1570 assert(image != (const Image *) NULL);
1571 assert(image->signature == MagickSignature);
1572 if (image->debug != MagickFalse)
1573 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1574 assert(exception != (ExceptionInfo *) NULL);
1575 assert(exception->signature == MagickSignature);
1576 width=GetOptimalKernelWidth1D(radius,sigma);
1577 kernel_info=AcquireKernelInfo((const char *) NULL);
1578 if (kernel_info == (KernelInfo *) NULL)
1579 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1580 kernel_info->width=width;
1581 kernel_info->height=width;
1582 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1583 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1584 if (kernel_info->values == (MagickRealType *) NULL)
1586 kernel_info=DestroyKernelInfo(kernel_info);
1587 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1589 j=(ssize_t) kernel_info->width/2;
1591 for (v=(-j); v <= j; v++)
1593 for (u=(-j); u <= j; u++)
1595 kernel_info->values[i]=(-1.0);
1599 kernel_info->values[i/2]=(double) (width*width-1.0);
1600 kernel_info->bias=image->bias; /* FUTURE: User bias on a edge image? */
1601 edge_image=ConvolveImage(image,kernel_info,exception);
1602 kernel_info=DestroyKernelInfo(kernel_info);
1607 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1611 % E m b o s s I m a g e %
1615 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1617 % EmbossImage() returns a grayscale image with a three-dimensional effect.
1618 % We convolve the image with a Gaussian operator of the given radius and
1619 % standard deviation (sigma). For reasonable results, radius should be
1620 % larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1623 % The format of the EmbossImage method is:
1625 % Image *EmbossImage(const Image *image,const double radius,
1626 % const double sigma,ExceptionInfo *exception)
1628 % A description of each parameter follows:
1630 % o image: the image.
1632 % o radius: the radius of the pixel neighborhood.
1634 % o sigma: the standard deviation of the Gaussian, in pixels.
1636 % o exception: return any errors or warnings in this structure.
1639 MagickExport Image *EmbossImage(const Image *image,const double radius,
1640 const double sigma,ExceptionInfo *exception)
1660 assert(image != (const Image *) NULL);
1661 assert(image->signature == MagickSignature);
1662 if (image->debug != MagickFalse)
1663 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1664 assert(exception != (ExceptionInfo *) NULL);
1665 assert(exception->signature == MagickSignature);
1666 width=GetOptimalKernelWidth1D(radius,sigma);
1667 kernel_info=AcquireKernelInfo((const char *) NULL);
1668 if (kernel_info == (KernelInfo *) NULL)
1669 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1670 kernel_info->width=width;
1671 kernel_info->height=width;
1672 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1673 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1674 if (kernel_info->values == (MagickRealType *) NULL)
1676 kernel_info=DestroyKernelInfo(kernel_info);
1677 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1679 j=(ssize_t) kernel_info->width/2;
1682 for (v=(-j); v <= j; v++)
1684 for (u=(-j); u <= j; u++)
1686 kernel_info->values[i]=(double) (((u < 0) || (v < 0) ? -8.0 : 8.0)*
1687 exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
1688 (2.0*MagickPI*MagickSigma*MagickSigma));
1690 kernel_info->values[i]=0.0;
1695 kernel_info->bias=image->bias; /* FUTURE: user bias on an edge image */
1696 emboss_image=ConvolveImage(image,kernel_info,exception);
1697 kernel_info=DestroyKernelInfo(kernel_info);
1698 if (emboss_image != (Image *) NULL)
1699 (void) EqualizeImage(emboss_image,exception);
1700 return(emboss_image);
1704 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1708 % G a u s s i a n B l u r I m a g e %
1712 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1714 % GaussianBlurImage() blurs an image. We convolve the image with a
1715 % Gaussian operator of the given radius and standard deviation (sigma).
1716 % For reasonable results, the radius should be larger than sigma. Use a
1717 % radius of 0 and GaussianBlurImage() selects a suitable radius for you
1719 % The format of the GaussianBlurImage method is:
1721 % Image *GaussianBlurImage(const Image *image,onst double radius,
1722 % const double sigma,ExceptionInfo *exception)
1724 % A description of each parameter follows:
1726 % o image: the image.
1728 % o radius: the radius of the Gaussian, in pixels, not counting the center
1731 % o sigma: the standard deviation of the Gaussian, in pixels.
1733 % o exception: return any errors or warnings in this structure.
1736 MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
1737 const double sigma,ExceptionInfo *exception)
1756 assert(image != (const Image *) NULL);
1757 assert(image->signature == MagickSignature);
1758 if (image->debug != MagickFalse)
1759 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1760 assert(exception != (ExceptionInfo *) NULL);
1761 assert(exception->signature == MagickSignature);
1762 width=GetOptimalKernelWidth2D(radius,sigma);
1763 kernel_info=AcquireKernelInfo((const char *) NULL);
1764 if (kernel_info == (KernelInfo *) NULL)
1765 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1766 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1767 kernel_info->width=width;
1768 kernel_info->height=width;
1769 kernel_info->signature=MagickSignature;
1770 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1771 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1772 if (kernel_info->values == (MagickRealType *) NULL)
1774 kernel_info=DestroyKernelInfo(kernel_info);
1775 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1777 j=(ssize_t) kernel_info->width/2;
1779 for (v=(-j); v <= j; v++)
1781 for (u=(-j); u <= j; u++)
1783 kernel_info->values[i]=(double) (exp(-((double) u*u+v*v)/(2.0*
1784 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
1788 blur_image=ConvolveImage(image,kernel_info,exception);
1789 kernel_info=DestroyKernelInfo(kernel_info);
1794 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1798 % M o t i o n B l u r I m a g e %
1802 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1804 % MotionBlurImage() simulates motion blur. We convolve the image with a
1805 % Gaussian operator of the given radius and standard deviation (sigma).
1806 % For reasonable results, radius should be larger than sigma. Use a
1807 % radius of 0 and MotionBlurImage() selects a suitable radius for you.
1808 % Angle gives the angle of the blurring motion.
1810 % Andrew Protano contributed this effect.
1812 % The format of the MotionBlurImage method is:
1814 % Image *MotionBlurImage(const Image *image,const double radius,
1815 % const double sigma,const double angle,const double bias,
1816 % ExceptionInfo *exception)
1818 % A description of each parameter follows:
1820 % o image: the image.
1822 % o radius: the radius of the Gaussian, in pixels, not counting
1825 % o sigma: the standard deviation of the Gaussian, in pixels.
1827 % o angle: Apply the effect along this angle.
1831 % o exception: return any errors or warnings in this structure.
1835 static double *GetMotionBlurKernel(const size_t width,const double sigma)
1845 Generate a 1-D convolution kernel.
1847 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1848 kernel=(double *) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
1849 if (kernel == (double *) NULL)
1852 for (i=0; i < (ssize_t) width; i++)
1854 kernel[i]=(double) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
1855 MagickSigma)))/(MagickSQ2PI*MagickSigma));
1856 normalize+=kernel[i];
1858 for (i=0; i < (ssize_t) width; i++)
1859 kernel[i]/=normalize;
1863 MagickExport Image *MotionBlurImage(const Image *image,const double radius,
1864 const double sigma,const double angle,const double bias,
1865 ExceptionInfo *exception)
1899 assert(image != (Image *) NULL);
1900 assert(image->signature == MagickSignature);
1901 if (image->debug != MagickFalse)
1902 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1903 assert(exception != (ExceptionInfo *) NULL);
1904 width=GetOptimalKernelWidth1D(radius,sigma);
1905 kernel=GetMotionBlurKernel(width,sigma);
1906 if (kernel == (double *) NULL)
1907 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1908 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
1909 if (offset == (OffsetInfo *) NULL)
1911 kernel=(double *) RelinquishAlignedMemory(kernel);
1912 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1914 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1915 if (blur_image == (Image *) NULL)
1917 kernel=(double *) RelinquishAlignedMemory(kernel);
1918 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1919 return((Image *) NULL);
1921 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
1923 kernel=(double *) RelinquishAlignedMemory(kernel);
1924 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1925 blur_image=DestroyImage(blur_image);
1926 return((Image *) NULL);
1928 point.x=(double) width*sin(DegreesToRadians(angle));
1929 point.y=(double) width*cos(DegreesToRadians(angle));
1930 for (i=0; i < (ssize_t) width; i++)
1932 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
1933 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
1940 image_view=AcquireCacheView(image);
1941 motion_view=AcquireCacheView(image);
1942 blur_view=AcquireCacheView(blur_image);
1943 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1944 #pragma omp parallel for schedule(static,4) shared(progress,status)
1946 for (y=0; y < (ssize_t) image->rows; y++)
1948 register const Quantum
1957 if (status == MagickFalse)
1959 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1960 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
1962 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1967 for (x=0; x < (ssize_t) image->columns; x++)
1972 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1986 register const Quantum
1995 channel=GetPixelChannelMapChannel(image,i);
1996 traits=GetPixelChannelMapTraits(image,channel);
1997 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
1998 if ((traits == UndefinedPixelTrait) ||
1999 (blur_traits == UndefinedPixelTrait))
2001 if (((blur_traits & CopyPixelTrait) != 0) ||
2002 (GetPixelMask(image,p) != 0))
2004 SetPixelChannel(blur_image,channel,p[i],q);
2009 if ((blur_traits & BlendPixelTrait) == 0)
2011 for (j=0; j < (ssize_t) width; j++)
2013 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+
2014 offset[j].y,1,1,exception);
2015 if (r == (const Quantum *) NULL)
2023 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
2028 for (j=0; j < (ssize_t) width; j++)
2030 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+offset[j].y,1,
2032 if (r == (const Quantum *) NULL)
2037 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,r));
2038 pixel+=(*k)*alpha*r[i];
2042 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
2043 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2045 p+=GetPixelChannels(image);
2046 q+=GetPixelChannels(blur_image);
2048 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2050 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2055 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2056 #pragma omp critical (MagickCore_MotionBlurImage)
2058 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2059 if (proceed == MagickFalse)
2063 blur_view=DestroyCacheView(blur_view);
2064 motion_view=DestroyCacheView(motion_view);
2065 image_view=DestroyCacheView(image_view);
2066 kernel=(double *) RelinquishAlignedMemory(kernel);
2067 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2068 if (status == MagickFalse)
2069 blur_image=DestroyImage(blur_image);
2074 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2078 % P r e v i e w I m a g e %
2082 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2084 % PreviewImage() tiles 9 thumbnails of the specified image with an image
2085 % processing operation applied with varying parameters. This may be helpful
2086 % pin-pointing an appropriate parameter for a particular image processing
2089 % The format of the PreviewImages method is:
2091 % Image *PreviewImages(const Image *image,const PreviewType preview,
2092 % ExceptionInfo *exception)
2094 % A description of each parameter follows:
2096 % o image: the image.
2098 % o preview: the image processing operation.
2100 % o exception: return any errors or warnings in this structure.
2103 MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2104 ExceptionInfo *exception)
2106 #define NumberTiles 9
2107 #define PreviewImageTag "Preview/Image"
2108 #define DefaultPreviewGeometry "204x204+10+10"
2111 factor[MaxTextExtent],
2112 label[MaxTextExtent];
2154 Open output image file.
2156 assert(image != (Image *) NULL);
2157 assert(image->signature == MagickSignature);
2158 if (image->debug != MagickFalse)
2159 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2163 preview_info=AcquireImageInfo();
2164 SetGeometry(image,&geometry);
2165 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2166 &geometry.width,&geometry.height);
2167 images=NewImageList();
2169 GetQuantizeInfo(&quantize_info);
2175 for (i=0; i < NumberTiles; i++)
2177 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2178 if (thumbnail == (Image *) NULL)
2180 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2182 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel,exception);
2183 if (i == (NumberTiles/2))
2185 (void) QueryColorCompliance("#dfdfdf",AllCompliance,
2186 &thumbnail->matte_color,exception);
2187 AppendImageToList(&images,thumbnail);
2195 preview_image=RotateImage(thumbnail,degrees,exception);
2196 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
2202 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
2203 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",
2204 degrees,2.0*degrees);
2209 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2210 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
2211 preview_image=RollImage(thumbnail,x,y,exception);
2212 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
2213 (double) x,(double) y);
2218 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2219 if (preview_image == (Image *) NULL)
2221 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",
2223 (void) ModulateImage(preview_image,factor,exception);
2224 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2227 case SaturationPreview:
2229 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2230 if (preview_image == (Image *) NULL)
2232 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",
2234 (void) ModulateImage(preview_image,factor,exception);
2235 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2238 case BrightnessPreview:
2240 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2241 if (preview_image == (Image *) NULL)
2243 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
2244 (void) ModulateImage(preview_image,factor,exception);
2245 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2251 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2252 if (preview_image == (Image *) NULL)
2255 (void) GammaImage(preview_image,gamma,exception);
2256 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
2261 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2262 if (preview_image != (Image *) NULL)
2263 for (x=0; x < i; x++)
2264 (void) ContrastImage(preview_image,MagickTrue,exception);
2265 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
2271 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2272 if (preview_image == (Image *) NULL)
2274 for (x=0; x < i; x++)
2275 (void) ContrastImage(preview_image,MagickFalse,exception);
2276 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
2280 case GrayscalePreview:
2282 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2283 if (preview_image == (Image *) NULL)
2286 quantize_info.number_colors=colors;
2287 quantize_info.colorspace=GRAYColorspace;
2288 (void) QuantizeImage(&quantize_info,preview_image,exception);
2289 (void) FormatLocaleString(label,MaxTextExtent,
2290 "-colorspace gray -colors %.20g",(double) colors);
2293 case QuantizePreview:
2295 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2296 if (preview_image == (Image *) NULL)
2299 quantize_info.number_colors=colors;
2300 (void) QuantizeImage(&quantize_info,preview_image,exception);
2301 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
2305 case DespecklePreview:
2307 for (x=0; x < (i-1); x++)
2309 preview_image=DespeckleImage(thumbnail,exception);
2310 if (preview_image == (Image *) NULL)
2312 thumbnail=DestroyImage(thumbnail);
2313 thumbnail=preview_image;
2315 preview_image=DespeckleImage(thumbnail,exception);
2316 if (preview_image == (Image *) NULL)
2318 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
2322 case ReduceNoisePreview:
2324 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2325 (size_t) radius,exception);
2326 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
2329 case AddNoisePreview:
2335 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2340 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2345 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2350 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2355 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2360 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2365 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2369 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2370 (size_t) i,exception);
2371 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
2374 case SharpenPreview:
2376 /* FUTURE: user bias on sharpen! This is non-sensical! */
2377 preview_image=SharpenImage(thumbnail,radius,sigma,image->bias,
2379 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",
2385 /* FUTURE: user bias on blur! This is non-sensical! */
2386 preview_image=BlurImage(thumbnail,radius,sigma,image->bias,exception);
2387 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
2391 case ThresholdPreview:
2393 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2394 if (preview_image == (Image *) NULL)
2396 (void) BilevelImage(thumbnail,(double) (percentage*((MagickRealType)
2397 QuantumRange+1.0))/100.0,exception);
2398 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",
2399 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
2402 case EdgeDetectPreview:
2404 preview_image=EdgeImage(thumbnail,radius,sigma,exception);
2405 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
2410 preview_image=SpreadImage(thumbnail,radius,thumbnail->interpolate,
2412 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",
2416 case SolarizePreview:
2418 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2419 if (preview_image == (Image *) NULL)
2421 (void) SolarizeImage(preview_image,(double) QuantumRange*
2422 percentage/100.0,exception);
2423 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
2424 (QuantumRange*percentage)/100.0);
2430 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2432 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",
2438 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2439 if (preview_image == (Image *) NULL)
2441 geometry.width=(size_t) (2*i+2);
2442 geometry.height=(size_t) (2*i+2);
2445 (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
2446 (void) FormatLocaleString(label,MaxTextExtent,
2447 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
2448 geometry.height,(double) geometry.x,(double) geometry.y);
2451 case SegmentPreview:
2453 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2454 if (preview_image == (Image *) NULL)
2457 (void) SegmentImage(preview_image,RGBColorspace,MagickFalse,threshold,
2458 threshold,exception);
2459 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
2460 threshold,threshold);
2465 preview_image=SwirlImage(thumbnail,degrees,image->interpolate,
2467 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
2471 case ImplodePreview:
2474 preview_image=ImplodeImage(thumbnail,degrees,image->interpolate,
2476 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
2482 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,
2483 image->interpolate,exception);
2484 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",
2485 0.5*degrees,2.0*degrees);
2488 case OilPaintPreview:
2490 preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2492 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2496 case CharcoalDrawingPreview:
2498 /* FUTURE: user bias on charcoal! This is non-sensical! */
2499 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
2500 image->bias,exception);
2501 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2508 filename[MaxTextExtent];
2516 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2517 if (preview_image == (Image *) NULL)
2519 preview_info->quality=(size_t) percentage;
2520 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
2521 preview_info->quality);
2522 file=AcquireUniqueFileResource(filename);
2525 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
2526 "jpeg:%s",filename);
2527 status=WriteImage(preview_info,preview_image,exception);
2528 if (status != MagickFalse)
2533 (void) CopyMagickString(preview_info->filename,
2534 preview_image->filename,MaxTextExtent);
2535 quality_image=ReadImage(preview_info,exception);
2536 if (quality_image != (Image *) NULL)
2538 preview_image=DestroyImage(preview_image);
2539 preview_image=quality_image;
2542 (void) RelinquishUniqueFileResource(preview_image->filename);
2543 if ((GetBlobSize(preview_image)/1024) >= 1024)
2544 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
2545 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2548 if (GetBlobSize(preview_image) >= 1024)
2549 (void) FormatLocaleString(label,MaxTextExtent,
2550 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
2551 GetBlobSize(preview_image))/1024.0);
2553 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
2554 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
2558 thumbnail=DestroyImage(thumbnail);
2562 if (preview_image == (Image *) NULL)
2564 (void) DeleteImageProperty(preview_image,"label");
2565 (void) SetImageProperty(preview_image,"label",label,exception);
2566 AppendImageToList(&images,preview_image);
2567 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2569 if (proceed == MagickFalse)
2572 if (images == (Image *) NULL)
2574 preview_info=DestroyImageInfo(preview_info);
2575 return((Image *) NULL);
2580 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2581 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2582 montage_info->shadow=MagickTrue;
2583 (void) CloneString(&montage_info->tile,"3x3");
2584 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2585 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2586 montage_image=MontageImages(images,montage_info,exception);
2587 montage_info=DestroyMontageInfo(montage_info);
2588 images=DestroyImageList(images);
2589 if (montage_image == (Image *) NULL)
2590 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2591 if (montage_image->montage != (char *) NULL)
2594 Free image directory.
2596 montage_image->montage=(char *) RelinquishMagickMemory(
2597 montage_image->montage);
2598 if (image->directory != (char *) NULL)
2599 montage_image->directory=(char *) RelinquishMagickMemory(
2600 montage_image->directory);
2602 preview_info=DestroyImageInfo(preview_info);
2603 return(montage_image);
2607 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2611 % R a d i a l B l u r I m a g e %
2615 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2617 % RadialBlurImage() applies a radial blur to the image.
2619 % Andrew Protano contributed this effect.
2621 % The format of the RadialBlurImage method is:
2623 % Image *RadialBlurImage(const Image *image,const double angle,
2624 % const double blur,ExceptionInfo *exception)
2626 % A description of each parameter follows:
2628 % o image: the image.
2630 % o angle: the angle of the radial blur.
2634 % o exception: return any errors or warnings in this structure.
2637 MagickExport Image *RadialBlurImage(const Image *image,const double angle,
2638 const double bias,ExceptionInfo *exception)
2674 Allocate blur image.
2676 assert(image != (Image *) NULL);
2677 assert(image->signature == MagickSignature);
2678 if (image->debug != MagickFalse)
2679 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2680 assert(exception != (ExceptionInfo *) NULL);
2681 assert(exception->signature == MagickSignature);
2682 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2683 if (blur_image == (Image *) NULL)
2684 return((Image *) NULL);
2685 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2687 blur_image=DestroyImage(blur_image);
2688 return((Image *) NULL);
2690 blur_center.x=(double) image->columns/2.0;
2691 blur_center.y=(double) image->rows/2.0;
2692 blur_radius=hypot(blur_center.x,blur_center.y);
2693 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
2694 theta=DegreesToRadians(angle)/(MagickRealType) (n-1);
2695 cos_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2696 sizeof(*cos_theta));
2697 sin_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2698 sizeof(*sin_theta));
2699 if ((cos_theta == (MagickRealType *) NULL) ||
2700 (sin_theta == (MagickRealType *) NULL))
2702 blur_image=DestroyImage(blur_image);
2703 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2705 offset=theta*(MagickRealType) (n-1)/2.0;
2706 for (i=0; i < (ssize_t) n; i++)
2708 cos_theta[i]=cos((double) (theta*i-offset));
2709 sin_theta[i]=sin((double) (theta*i-offset));
2716 image_view=AcquireCacheView(image);
2717 radial_view=AcquireCacheView(image);
2718 blur_view=AcquireCacheView(blur_image);
2719 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2720 #pragma omp parallel for schedule(static,4) shared(progress,status)
2722 for (y=0; y < (ssize_t) image->rows; y++)
2724 register const Quantum
2733 if (status == MagickFalse)
2735 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2736 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2738 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2743 for (x=0; x < (ssize_t) image->columns; x++)
2757 center.x=(double) x-blur_center.x;
2758 center.y=(double) y-blur_center.y;
2759 radius=hypot((double) center.x,center.y);
2764 step=(size_t) (blur_radius/radius);
2771 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2784 register const Quantum
2790 channel=GetPixelChannelMapChannel(image,i);
2791 traits=GetPixelChannelMapTraits(image,channel);
2792 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
2793 if ((traits == UndefinedPixelTrait) ||
2794 (blur_traits == UndefinedPixelTrait))
2796 if (((blur_traits & CopyPixelTrait) != 0) ||
2797 (GetPixelMask(image,p) != 0))
2799 SetPixelChannel(blur_image,channel,p[i],q);
2804 if ((blur_traits & BlendPixelTrait) == 0)
2806 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2808 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2809 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2810 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2812 if (r == (const Quantum *) NULL)
2820 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
2821 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2824 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2826 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2827 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2828 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2830 if (r == (const Quantum *) NULL)
2835 pixel+=GetPixelAlpha(image,r)*r[i];
2836 gamma+=GetPixelAlpha(image,r);
2838 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
2839 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2841 p+=GetPixelChannels(image);
2842 q+=GetPixelChannels(blur_image);
2844 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2846 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2851 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2852 #pragma omp critical (MagickCore_RadialBlurImage)
2854 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2855 if (proceed == MagickFalse)
2859 blur_view=DestroyCacheView(blur_view);
2860 radial_view=DestroyCacheView(radial_view);
2861 image_view=DestroyCacheView(image_view);
2862 cos_theta=(MagickRealType *) RelinquishMagickMemory(cos_theta);
2863 sin_theta=(MagickRealType *) RelinquishMagickMemory(sin_theta);
2864 if (status == MagickFalse)
2865 blur_image=DestroyImage(blur_image);
2870 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2874 % S e l e c t i v e B l u r I m a g e %
2878 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2880 % SelectiveBlurImage() selectively blur pixels within a contrast threshold.
2881 % It is similar to the unsharpen mask that sharpens everything with contrast
2882 % above a certain threshold.
2884 % The format of the SelectiveBlurImage method is:
2886 % Image *SelectiveBlurImage(const Image *image,const double radius,
2887 % const double sigma,const double threshold,const double bias,
2888 % ExceptionInfo *exception)
2890 % A description of each parameter follows:
2892 % o image: the image.
2894 % o radius: the radius of the Gaussian, in pixels, not counting the center
2897 % o sigma: the standard deviation of the Gaussian, in pixels.
2899 % o threshold: only pixels within this contrast threshold are included
2900 % in the blur operation.
2904 % o exception: return any errors or warnings in this structure.
2907 MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
2908 const double sigma,const double threshold,const double bias,
2909 ExceptionInfo *exception)
2911 #define SelectiveBlurImageTag "SelectiveBlur/Image"
2943 Initialize blur image attributes.
2945 assert(image != (Image *) NULL);
2946 assert(image->signature == MagickSignature);
2947 if (image->debug != MagickFalse)
2948 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2949 assert(exception != (ExceptionInfo *) NULL);
2950 assert(exception->signature == MagickSignature);
2951 width=GetOptimalKernelWidth1D(radius,sigma);
2952 kernel=(double *) AcquireAlignedMemory((size_t) width,width*sizeof(*kernel));
2953 if (kernel == (double *) NULL)
2954 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2955 j=(ssize_t) width/2;
2957 for (v=(-j); v <= j; v++)
2959 for (u=(-j); u <= j; u++)
2960 kernel[i++]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
2961 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
2963 if (image->debug != MagickFalse)
2966 format[MaxTextExtent],
2969 register const double
2976 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
2977 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
2979 message=AcquireString("");
2981 for (v=0; v < (ssize_t) width; v++)
2984 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
2985 (void) ConcatenateString(&message,format);
2986 for (u=0; u < (ssize_t) width; u++)
2988 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",*k++);
2989 (void) ConcatenateString(&message,format);
2991 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
2993 message=DestroyString(message);
2995 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2996 if (blur_image == (Image *) NULL)
2997 return((Image *) NULL);
2998 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
3000 blur_image=DestroyImage(blur_image);
3001 return((Image *) NULL);
3004 Threshold blur image.
3008 center=(ssize_t) (GetPixelChannels(image)*(image->columns+width)*(width/2L)+
3009 GetPixelChannels(image)*(width/2L));
3010 image_view=AcquireCacheView(image);
3011 blur_view=AcquireCacheView(blur_image);
3012 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3013 #pragma omp parallel for schedule(static,4) shared(progress,status)
3015 for (y=0; y < (ssize_t) image->rows; y++)
3023 register const Quantum
3032 if (status == MagickFalse)
3034 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
3035 (width/2L),image->columns+width,width,exception);
3036 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3038 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3043 for (x=0; x < (ssize_t) image->columns; x++)
3048 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3063 register const double
3066 register const Quantum
3075 channel=GetPixelChannelMapChannel(image,i);
3076 traits=GetPixelChannelMapTraits(image,channel);
3077 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
3078 if ((traits == UndefinedPixelTrait) ||
3079 (blur_traits == UndefinedPixelTrait))
3081 if (((blur_traits & CopyPixelTrait) != 0) ||
3082 (GetPixelMask(image,p) != 0))
3084 SetPixelChannel(blur_image,channel,p[center+i],q);
3090 intensity=(MagickRealType) GetPixelIntensity(image,p+center);
3092 if ((blur_traits & BlendPixelTrait) == 0)
3094 for (v=0; v < (ssize_t) width; v++)
3096 for (u=0; u < (ssize_t) width; u++)
3098 contrast=GetPixelIntensity(image,pixels)-intensity;
3099 if (fabs(contrast) < threshold)
3101 pixel+=(*k)*pixels[i];
3105 pixels+=GetPixelChannels(image);
3107 pixels+=image->columns*GetPixelChannels(image);
3109 if (fabs((double) gamma) < MagickEpsilon)
3111 SetPixelChannel(blur_image,channel,p[center+i],q);
3114 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3115 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3118 for (v=0; v < (ssize_t) width; v++)
3120 for (u=0; u < (ssize_t) width; u++)
3122 contrast=GetPixelIntensity(image,pixels)-intensity;
3123 if (fabs(contrast) < threshold)
3125 alpha=(MagickRealType) (QuantumScale*
3126 GetPixelAlpha(image,pixels));
3127 pixel+=(*k)*alpha*pixels[i];
3131 pixels+=GetPixelChannels(image);
3133 pixels+=image->columns*GetPixelChannels(image);
3135 if (fabs((double) gamma) < MagickEpsilon)
3137 SetPixelChannel(blur_image,channel,p[center+i],q);
3140 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3141 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3143 p+=GetPixelChannels(image);
3144 q+=GetPixelChannels(blur_image);
3146 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3147 if (sync == MagickFalse)
3149 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3154 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3155 #pragma omp critical (MagickCore_SelectiveBlurImage)
3157 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3159 if (proceed == MagickFalse)
3163 blur_image->type=image->type;
3164 blur_view=DestroyCacheView(blur_view);
3165 image_view=DestroyCacheView(image_view);
3166 kernel=(double *) RelinquishAlignedMemory(kernel);
3167 if (status == MagickFalse)
3168 blur_image=DestroyImage(blur_image);
3173 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3177 % S h a d e I m a g e %
3181 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3183 % ShadeImage() shines a distant light on an image to create a
3184 % three-dimensional effect. You control the positioning of the light with
3185 % azimuth and elevation; azimuth is measured in degrees off the x axis
3186 % and elevation is measured in pixels above the Z axis.
3188 % The format of the ShadeImage method is:
3190 % Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3191 % const double azimuth,const double elevation,ExceptionInfo *exception)
3193 % A description of each parameter follows:
3195 % o image: the image.
3197 % o gray: A value other than zero shades the intensity of each pixel.
3199 % o azimuth, elevation: Define the light source direction.
3201 % o exception: return any errors or warnings in this structure.
3204 MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3205 const double azimuth,const double elevation,ExceptionInfo *exception)
3207 #define ShadeImageTag "Shade/Image"
3229 Initialize shaded image attributes.
3231 assert(image != (const Image *) NULL);
3232 assert(image->signature == MagickSignature);
3233 if (image->debug != MagickFalse)
3234 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3235 assert(exception != (ExceptionInfo *) NULL);
3236 assert(exception->signature == MagickSignature);
3237 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3238 if (shade_image == (Image *) NULL)
3239 return((Image *) NULL);
3240 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
3242 shade_image=DestroyImage(shade_image);
3243 return((Image *) NULL);
3246 Compute the light vector.
3248 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3249 cos(DegreesToRadians(elevation));
3250 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3251 cos(DegreesToRadians(elevation));
3252 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3258 image_view=AcquireCacheView(image);
3259 shade_view=AcquireCacheView(shade_image);
3260 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3261 #pragma omp parallel for schedule(static,4) shared(progress,status)
3263 for (y=0; y < (ssize_t) image->rows; y++)
3273 register const Quantum
3285 if (status == MagickFalse)
3287 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
3288 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3290 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3296 Shade this row of pixels.
3298 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
3299 pre=p+GetPixelChannels(image);
3300 center=pre+(image->columns+2)*GetPixelChannels(image);
3301 post=center+(image->columns+2)*GetPixelChannels(image);
3302 for (x=0; x < (ssize_t) image->columns; x++)
3308 Determine the surface normal and compute shading.
3310 normal.x=(double) (GetPixelIntensity(image,pre-GetPixelChannels(image))+
3311 GetPixelIntensity(image,center-GetPixelChannels(image))+
3312 GetPixelIntensity(image,post-GetPixelChannels(image))-
3313 GetPixelIntensity(image,pre+GetPixelChannels(image))-
3314 GetPixelIntensity(image,center+GetPixelChannels(image))-
3315 GetPixelIntensity(image,post+GetPixelChannels(image)));
3316 normal.y=(double) (GetPixelIntensity(image,post-GetPixelChannels(image))+
3317 GetPixelIntensity(image,post)+GetPixelIntensity(image,post+
3318 GetPixelChannels(image))-GetPixelIntensity(image,pre-
3319 GetPixelChannels(image))-GetPixelIntensity(image,pre)-
3320 GetPixelIntensity(image,pre+GetPixelChannels(image)));
3321 if ((normal.x == 0.0) && (normal.y == 0.0))
3326 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3327 if (distance > MagickEpsilon)
3330 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
3331 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3332 shade=distance/sqrt((double) normal_distance);
3335 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3344 channel=GetPixelChannelMapChannel(image,i);
3345 traits=GetPixelChannelMapTraits(image,channel);
3346 shade_traits=GetPixelChannelMapTraits(shade_image,channel);
3347 if ((traits == UndefinedPixelTrait) ||
3348 (shade_traits == UndefinedPixelTrait))
3350 if (((shade_traits & CopyPixelTrait) != 0) ||
3351 (GetPixelMask(image,pre) != 0))
3353 SetPixelChannel(shade_image,channel,center[i],q);
3356 if (gray != MagickFalse)
3358 SetPixelChannel(shade_image,channel,ClampToQuantum(shade),q);
3361 SetPixelChannel(shade_image,channel,ClampToQuantum(QuantumScale*shade*
3364 pre+=GetPixelChannels(image);
3365 center+=GetPixelChannels(image);
3366 post+=GetPixelChannels(image);
3367 q+=GetPixelChannels(shade_image);
3369 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3371 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3376 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3377 #pragma omp critical (MagickCore_ShadeImage)
3379 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3380 if (proceed == MagickFalse)
3384 shade_view=DestroyCacheView(shade_view);
3385 image_view=DestroyCacheView(image_view);
3386 if (status == MagickFalse)
3387 shade_image=DestroyImage(shade_image);
3388 return(shade_image);
3392 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3396 % S h a r p e n I m a g e %
3400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3402 % SharpenImage() sharpens the image. We convolve the image with a Gaussian
3403 % operator of the given radius and standard deviation (sigma). For
3404 % reasonable results, radius should be larger than sigma. Use a radius of 0
3405 % and SharpenImage() selects a suitable radius for you.
3407 % Using a separable kernel would be faster, but the negative weights cancel
3408 % out on the corners of the kernel producing often undesirable ringing in the
3409 % filtered result; this can be avoided by using a 2D gaussian shaped image
3410 % sharpening kernel instead.
3412 % The format of the SharpenImage method is:
3414 % Image *SharpenImage(const Image *image,const double radius,
3415 % const double sigma,const double bias,ExceptionInfo *exception)
3417 % A description of each parameter follows:
3419 % o image: the image.
3421 % o radius: the radius of the Gaussian, in pixels, not counting the center
3424 % o sigma: the standard deviation of the Laplacian, in pixels.
3428 % o exception: return any errors or warnings in this structure.
3431 MagickExport Image *SharpenImage(const Image *image,const double radius,
3432 const double sigma,const double bias,ExceptionInfo *exception)
3454 assert(image != (const Image *) NULL);
3455 assert(image->signature == MagickSignature);
3456 if (image->debug != MagickFalse)
3457 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3458 assert(exception != (ExceptionInfo *) NULL);
3459 assert(exception->signature == MagickSignature);
3460 width=GetOptimalKernelWidth2D(radius,sigma);
3461 kernel_info=AcquireKernelInfo((const char *) NULL);
3462 if (kernel_info == (KernelInfo *) NULL)
3463 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3464 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3465 kernel_info->width=width;
3466 kernel_info->height=width;
3467 kernel_info->bias=bias; /* FUTURE: user bias - non-sensical! */
3468 kernel_info->signature=MagickSignature;
3469 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
3470 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
3471 if (kernel_info->values == (MagickRealType *) NULL)
3473 kernel_info=DestroyKernelInfo(kernel_info);
3474 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3477 j=(ssize_t) kernel_info->width/2;
3479 for (v=(-j); v <= j; v++)
3481 for (u=(-j); u <= j; u++)
3483 kernel_info->values[i]=(double) (-exp(-((double) u*u+v*v)/(2.0*
3484 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3485 normalize+=kernel_info->values[i];
3489 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
3490 sharp_image=ConvolveImage(image,kernel_info,exception);
3491 kernel_info=DestroyKernelInfo(kernel_info);
3492 return(sharp_image);
3496 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3500 % S p r e a d I m a g e %
3504 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3506 % SpreadImage() is a special effects method that randomly displaces each
3507 % pixel in a block defined by the radius parameter.
3509 % The format of the SpreadImage method is:
3511 % Image *SpreadImage(const Image *image,const double radius,
3512 % const PixelInterpolateMethod method,ExceptionInfo *exception)
3514 % A description of each parameter follows:
3516 % o image: the image.
3518 % o radius: choose a random pixel in a neighborhood of this extent.
3520 % o method: the pixel interpolation method.
3522 % o exception: return any errors or warnings in this structure.
3525 MagickExport Image *SpreadImage(const Image *image,const double radius,
3526 const PixelInterpolateMethod method,ExceptionInfo *exception)
3528 #define SpreadImageTag "Spread/Image"
3544 **restrict random_info;
3553 Initialize spread image attributes.
3555 assert(image != (Image *) NULL);
3556 assert(image->signature == MagickSignature);
3557 if (image->debug != MagickFalse)
3558 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3559 assert(exception != (ExceptionInfo *) NULL);
3560 assert(exception->signature == MagickSignature);
3561 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3563 if (spread_image == (Image *) NULL)
3564 return((Image *) NULL);
3565 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
3567 spread_image=DestroyImage(spread_image);
3568 return((Image *) NULL);
3575 width=GetOptimalKernelWidth1D(radius,0.5);
3576 random_info=AcquireRandomInfoThreadSet();
3577 image_view=AcquireCacheView(image);
3578 spread_view=AcquireCacheView(spread_image);
3579 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3580 #pragma omp parallel for schedule(static,8) shared(progress,status)
3582 for (y=0; y < (ssize_t) image->rows; y++)
3585 id = GetOpenMPThreadId();
3587 register const Quantum
3596 if (status == MagickFalse)
3598 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3599 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
3601 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3606 for (x=0; x < (ssize_t) image->columns; x++)
3611 point.x=GetPseudoRandomValue(random_info[id]);
3612 point.y=GetPseudoRandomValue(random_info[id]);
3613 status=InterpolatePixelChannels(image,image_view,spread_image,method,
3614 (double) x+width*point.x-0.5,(double) y+width*point.y-0.5,q,exception);
3615 q+=GetPixelChannels(spread_image);
3617 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
3619 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3624 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3625 #pragma omp critical (MagickCore_SpreadImage)
3627 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3628 if (proceed == MagickFalse)
3632 spread_view=DestroyCacheView(spread_view);
3633 image_view=DestroyCacheView(image_view);
3634 random_info=DestroyRandomInfoThreadSet(random_info);
3635 return(spread_image);
3639 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3643 % U n s h a r p M a s k I m a g e %
3647 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3649 % UnsharpMaskImage() sharpens one or more image channels. We convolve the
3650 % image with a Gaussian operator of the given radius and standard deviation
3651 % (sigma). For reasonable results, radius should be larger than sigma. Use a
3652 % radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
3654 % The format of the UnsharpMaskImage method is:
3656 % Image *UnsharpMaskImage(const Image *image,const double radius,
3657 % const double sigma,const double amount,const double threshold,
3658 % ExceptionInfo *exception)
3660 % A description of each parameter follows:
3662 % o image: the image.
3664 % o radius: the radius of the Gaussian, in pixels, not counting the center
3667 % o sigma: the standard deviation of the Gaussian, in pixels.
3669 % o amount: the percentage of the difference between the original and the
3670 % blur image that is added back into the original.
3672 % o threshold: the threshold in pixels needed to apply the diffence amount.
3674 % o exception: return any errors or warnings in this structure.
3677 MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
3678 const double sigma,const double amount,const double threshold,
3679 ExceptionInfo *exception)
3681 #define SharpenImageTag "Sharpen/Image"
3702 assert(image != (const Image *) NULL);
3703 assert(image->signature == MagickSignature);
3704 if (image->debug != MagickFalse)
3705 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3706 assert(exception != (ExceptionInfo *) NULL);
3707 unsharp_image=BlurImage(image,radius,sigma,image->bias,exception);
3708 if (unsharp_image == (Image *) NULL)
3709 return((Image *) NULL);
3710 quantum_threshold=(MagickRealType) QuantumRange*threshold;
3716 image_view=AcquireCacheView(image);
3717 unsharp_view=AcquireCacheView(unsharp_image);
3718 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3719 #pragma omp parallel for schedule(static,4) shared(progress,status)
3721 for (y=0; y < (ssize_t) image->rows; y++)
3723 register const Quantum
3732 if (status == MagickFalse)
3734 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3735 q=QueueCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
3737 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3742 for (x=0; x < (ssize_t) image->columns; x++)
3747 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3759 channel=GetPixelChannelMapChannel(image,i);
3760 traits=GetPixelChannelMapTraits(image,channel);
3761 unsharp_traits=GetPixelChannelMapTraits(unsharp_image,channel);
3762 if ((traits == UndefinedPixelTrait) ||
3763 (unsharp_traits == UndefinedPixelTrait))
3765 if (((unsharp_traits & CopyPixelTrait) != 0) ||
3766 (GetPixelMask(image,p) != 0))
3768 SetPixelChannel(unsharp_image,channel,p[i],q);
3771 pixel=p[i]-(MagickRealType) GetPixelChannel(unsharp_image,channel,q);
3772 if (fabs(2.0*pixel) < quantum_threshold)
3773 pixel=(MagickRealType) p[i];
3775 pixel=(MagickRealType) p[i]+amount*pixel;
3776 SetPixelChannel(unsharp_image,channel,ClampToQuantum(pixel),q);
3778 p+=GetPixelChannels(image);
3779 q+=GetPixelChannels(unsharp_image);
3781 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
3783 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3788 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3789 #pragma omp critical (MagickCore_UnsharpMaskImage)
3791 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
3792 if (proceed == MagickFalse)
3796 unsharp_image->type=image->type;
3797 unsharp_view=DestroyCacheView(unsharp_view);
3798 image_view=DestroyCacheView(image_view);
3799 if (status == MagickFalse)
3800 unsharp_image=DestroyImage(unsharp_image);
3801 return(unsharp_image);