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,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.
122 % o exception: return any errors or warnings in this structure.
126 MagickExport MagickBooleanType AdaptiveLevelImage(Image *image,
127 const char *levels,ExceptionInfo *exception)
146 if (levels == (char *) NULL)
148 flags=ParseGeometry(levels,&geometry_info);
149 black_point=geometry_info.rho;
150 white_point=(double) QuantumRange;
151 if ((flags & SigmaValue) != 0)
152 white_point=geometry_info.sigma;
154 if ((flags & XiValue) != 0)
155 gamma=geometry_info.xi;
156 if ((flags & PercentValue) != 0)
158 black_point*=(double) image->columns*image->rows/100.0;
159 white_point*=(double) image->columns*image->rows/100.0;
161 if ((flags & SigmaValue) == 0)
162 white_point=(double) QuantumRange-black_point;
163 if ((flags & AspectValue ) == 0)
164 status=LevelImage(image,black_point,white_point,gamma,exception);
166 status=LevelizeImage(image,black_point,white_point,gamma,exception);
170 MagickExport Image *AdaptiveBlurImage(const Image *image,const double radius,
171 const double sigma,ExceptionInfo *exception)
173 #define AdaptiveBlurImageTag "Convolve/Image"
174 #define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
209 assert(image != (const Image *) NULL);
210 assert(image->signature == MagickSignature);
211 if (image->debug != MagickFalse)
212 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
213 assert(exception != (ExceptionInfo *) NULL);
214 assert(exception->signature == MagickSignature);
215 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
216 if (blur_image == (Image *) NULL)
217 return((Image *) NULL);
218 if (fabs(sigma) <= MagickEpsilon)
220 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
222 blur_image=DestroyImage(blur_image);
223 return((Image *) NULL);
226 Edge detect the image brighness channel, level, blur, and level again.
228 edge_image=EdgeImage(image,radius,sigma,exception);
229 if (edge_image == (Image *) NULL)
231 blur_image=DestroyImage(blur_image);
232 return((Image *) NULL);
234 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
235 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
236 if (gaussian_image != (Image *) NULL)
238 edge_image=DestroyImage(edge_image);
239 edge_image=gaussian_image;
241 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
243 Create a set of kernels from maximum (radius,sigma) to minimum.
245 width=GetOptimalKernelWidth2D(radius,sigma);
246 kernel=(double **) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
247 if (kernel == (double **) NULL)
249 edge_image=DestroyImage(edge_image);
250 blur_image=DestroyImage(blur_image);
251 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
253 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
254 for (i=0; i < (ssize_t) width; i+=2)
256 kernel[i]=(double *) AcquireAlignedMemory((size_t) (width-i),(width-i)*
258 if (kernel[i] == (double *) NULL)
261 j=(ssize_t) (width-i)/2;
263 for (v=(-j); v <= j; v++)
265 for (u=(-j); u <= j; u++)
267 kernel[i][k]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
268 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
269 normalize+=kernel[i][k];
273 if (fabs(normalize) <= MagickEpsilon)
275 normalize=1.0/normalize;
276 for (k=0; k < (j*j); k++)
277 kernel[i][k]=normalize*kernel[i][k];
279 if (i < (ssize_t) width)
281 for (i-=2; i >= 0; i-=2)
282 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
283 kernel=(double **) RelinquishAlignedMemory(kernel);
284 edge_image=DestroyImage(edge_image);
285 blur_image=DestroyImage(blur_image);
286 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
289 Adaptively blur image.
293 image_view=AcquireVirtualCacheView(image,exception);
294 edge_view=AcquireVirtualCacheView(edge_image,exception);
295 blur_view=AcquireAuthenticCacheView(blur_image,exception);
296 #if defined(MAGICKCORE_OPENMP_SUPPORT)
297 #pragma omp parallel for schedule(static,4) shared(progress,status) \
298 dynamic_number_threads(image,image->columns,image->rows,1)
300 for (y=0; y < (ssize_t) blur_image->rows; y++)
302 register const Quantum
311 if (status == MagickFalse)
313 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
314 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
316 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
321 for (x=0; x < (ssize_t) blur_image->columns; x++)
323 register const Quantum
333 j=(ssize_t) ceil((double) width*QuantumScale*
334 GetPixelIntensity(edge_image,r)-0.5);
338 if (j > (ssize_t) width)
342 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
343 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
344 if (p == (const Quantum *) NULL)
346 center=(ssize_t) GetPixelChannels(image)*(width-j)*((width-j)/2L)+
347 GetPixelChannels(image)*((width-j)/2L);
348 if (GetPixelMask(image,p) != 0)
350 q+=GetPixelChannels(blur_image);
351 r+=GetPixelChannels(edge_image);
354 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
368 register const double
371 register const Quantum
380 channel=GetPixelChannelMapChannel(image,i);
381 traits=GetPixelChannelMapTraits(image,channel);
382 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
383 if ((traits == UndefinedPixelTrait) ||
384 (blur_traits == UndefinedPixelTrait))
386 if ((blur_traits & CopyPixelTrait) != 0)
388 SetPixelChannel(blur_image,channel,p[center+i],q);
395 if ((blur_traits & BlendPixelTrait) == 0)
400 for (v=0; v < (ssize_t) (width-j); v++)
402 for (u=0; u < (ssize_t) (width-j); u++)
404 pixel+=(*k)*pixels[i];
407 pixels+=GetPixelChannels(image);
410 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
411 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
417 for (v=0; v < (ssize_t) (width-j); v++)
419 for (u=0; u < (ssize_t) (width-j); u++)
421 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
422 pixel+=(*k)*alpha*pixels[i];
425 pixels+=GetPixelChannels(image);
428 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
429 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
431 q+=GetPixelChannels(blur_image);
432 r+=GetPixelChannels(edge_image);
434 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
436 if (image->progress_monitor != (MagickProgressMonitor) NULL)
441 #if defined(MAGICKCORE_OPENMP_SUPPORT)
442 #pragma omp critical (MagickCore_AdaptiveBlurImage)
444 proceed=SetImageProgress(image,AdaptiveBlurImageTag,progress++,
446 if (proceed == MagickFalse)
450 blur_image->type=image->type;
451 blur_view=DestroyCacheView(blur_view);
452 edge_view=DestroyCacheView(edge_view);
453 image_view=DestroyCacheView(image_view);
454 edge_image=DestroyImage(edge_image);
455 for (i=0; i < (ssize_t) width; i+=2)
456 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
457 kernel=(double **) RelinquishAlignedMemory(kernel);
458 if (status == MagickFalse)
459 blur_image=DestroyImage(blur_image);
464 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
468 % A d a p t i v e S h a r p e n I m a g e %
472 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
474 % AdaptiveSharpenImage() adaptively sharpens the image by sharpening more
475 % intensely near image edges and less intensely far from edges. We sharpen the
476 % image with a Gaussian operator of the given radius and standard deviation
477 % (sigma). For reasonable results, radius should be larger than sigma. Use a
478 % radius of 0 and AdaptiveSharpenImage() selects a suitable radius for you.
480 % The format of the AdaptiveSharpenImage method is:
482 % Image *AdaptiveSharpenImage(const Image *image,const double radius,
483 % const double sigma,ExceptionInfo *exception)
485 % A description of each parameter follows:
487 % o image: the image.
489 % o radius: the radius of the Gaussian, in pixels, not counting the center
492 % o sigma: the standard deviation of the Laplacian, in pixels.
494 % o exception: return any errors or warnings in this structure.
497 MagickExport Image *AdaptiveSharpenImage(const Image *image,const double radius,
498 const double sigma,ExceptionInfo *exception)
500 #define AdaptiveSharpenImageTag "Convolve/Image"
501 #define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
536 assert(image != (const Image *) NULL);
537 assert(image->signature == MagickSignature);
538 if (image->debug != MagickFalse)
539 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
540 assert(exception != (ExceptionInfo *) NULL);
541 assert(exception->signature == MagickSignature);
542 sharp_image=CloneImage(image,0,0,MagickTrue,exception);
543 if (sharp_image == (Image *) NULL)
544 return((Image *) NULL);
545 if (fabs(sigma) <= MagickEpsilon)
547 if (SetImageStorageClass(sharp_image,DirectClass,exception) == MagickFalse)
549 sharp_image=DestroyImage(sharp_image);
550 return((Image *) NULL);
553 Edge detect the image brighness channel, level, sharp, and level again.
555 edge_image=EdgeImage(image,radius,sigma,exception);
556 if (edge_image == (Image *) NULL)
558 sharp_image=DestroyImage(sharp_image);
559 return((Image *) NULL);
561 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
562 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
563 if (gaussian_image != (Image *) NULL)
565 edge_image=DestroyImage(edge_image);
566 edge_image=gaussian_image;
568 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
570 Create a set of kernels from maximum (radius,sigma) to minimum.
572 width=GetOptimalKernelWidth2D(radius,sigma);
573 kernel=(double **) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
574 if (kernel == (double **) NULL)
576 edge_image=DestroyImage(edge_image);
577 sharp_image=DestroyImage(sharp_image);
578 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
580 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
581 for (i=0; i < (ssize_t) width; i+=2)
583 kernel[i]=(double *) AcquireAlignedMemory((size_t) (width-i),(width-i)*
585 if (kernel[i] == (double *) NULL)
588 j=(ssize_t) (width-i)/2;
590 for (v=(-j); v <= j; v++)
592 for (u=(-j); u <= j; u++)
594 kernel[i][k]=(double) (-exp(-((double) u*u+v*v)/(2.0*MagickSigma*
595 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
596 normalize+=kernel[i][k];
600 if (fabs(normalize) <= MagickEpsilon)
602 normalize=1.0/normalize;
603 for (k=0; k < (j*j); k++)
604 kernel[i][k]=normalize*kernel[i][k];
606 if (i < (ssize_t) width)
608 for (i-=2; i >= 0; i-=2)
609 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
610 kernel=(double **) RelinquishAlignedMemory(kernel);
611 edge_image=DestroyImage(edge_image);
612 sharp_image=DestroyImage(sharp_image);
613 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
616 Adaptively sharpen image.
620 image_view=AcquireVirtualCacheView(image,exception);
621 edge_view=AcquireVirtualCacheView(edge_image,exception);
622 sharp_view=AcquireAuthenticCacheView(sharp_image,exception);
623 #if defined(MAGICKCORE_OPENMP_SUPPORT)
624 #pragma omp parallel for schedule(static,4) shared(progress,status) \
625 dynamic_number_threads(image,image->columns,image->rows,1)
627 for (y=0; y < (ssize_t) sharp_image->rows; y++)
629 register const Quantum
638 if (status == MagickFalse)
640 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
641 q=QueueCacheViewAuthenticPixels(sharp_view,0,y,sharp_image->columns,1,
643 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
648 for (x=0; x < (ssize_t) sharp_image->columns; x++)
650 register const Quantum
660 j=(ssize_t) ceil((double) width*QuantumScale*
661 GetPixelIntensity(edge_image,r)-0.5);
665 if (j > (ssize_t) width)
669 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
670 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
671 if (p == (const Quantum *) NULL)
673 center=(ssize_t) GetPixelChannels(image)*(width-j)*((width-j)/2L)+
674 GetPixelChannels(image)*((width-j)/2);
675 if (GetPixelMask(image,p) != 0)
677 q+=GetPixelChannels(sharp_image);
678 r+=GetPixelChannels(edge_image);
681 for (i=0; i < (ssize_t) GetPixelChannels(sharp_image); i++)
695 register const double
698 register const Quantum
707 channel=GetPixelChannelMapChannel(image,i);
708 traits=GetPixelChannelMapTraits(image,channel);
709 sharp_traits=GetPixelChannelMapTraits(sharp_image,channel);
710 if ((traits == UndefinedPixelTrait) ||
711 (sharp_traits == UndefinedPixelTrait))
713 if ((sharp_traits & CopyPixelTrait) != 0)
715 SetPixelChannel(sharp_image,channel,p[center+i],q);
722 if ((sharp_traits & BlendPixelTrait) == 0)
727 for (v=0; v < (ssize_t) (width-j); v++)
729 for (u=0; u < (ssize_t) (width-j); u++)
731 pixel+=(*k)*pixels[i];
734 pixels+=GetPixelChannels(image);
737 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
738 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
744 for (v=0; v < (ssize_t) (width-j); v++)
746 for (u=0; u < (ssize_t) (width-j); u++)
748 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
749 pixel+=(*k)*alpha*pixels[i];
752 pixels+=GetPixelChannels(image);
755 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
756 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
758 q+=GetPixelChannels(sharp_image);
759 r+=GetPixelChannels(edge_image);
761 if (SyncCacheViewAuthenticPixels(sharp_view,exception) == MagickFalse)
763 if (image->progress_monitor != (MagickProgressMonitor) NULL)
768 #if defined(MAGICKCORE_OPENMP_SUPPORT)
769 #pragma omp critical (MagickCore_AdaptiveSharpenImage)
771 proceed=SetImageProgress(image,AdaptiveSharpenImageTag,progress++,
773 if (proceed == MagickFalse)
777 sharp_image->type=image->type;
778 sharp_view=DestroyCacheView(sharp_view);
779 edge_view=DestroyCacheView(edge_view);
780 image_view=DestroyCacheView(image_view);
781 edge_image=DestroyImage(edge_image);
782 for (i=0; i < (ssize_t) width; i+=2)
783 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
784 kernel=(double **) RelinquishAlignedMemory(kernel);
785 if (status == MagickFalse)
786 sharp_image=DestroyImage(sharp_image);
791 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
795 % B l u r I m a g e %
799 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
801 % BlurImage() blurs an image. We convolve the image with a Gaussian operator
802 % of the given radius and standard deviation (sigma). For reasonable results,
803 % the radius should be larger than sigma. Use a radius of 0 and BlurImage()
804 % selects a suitable radius for you.
806 % BlurImage() differs from GaussianBlurImage() in that it uses a separable
807 % kernel which is faster but mathematically equivalent to the non-separable
810 % The format of the BlurImage method is:
812 % Image *BlurImage(const Image *image,const double radius,
813 % const double sigma,ExceptionInfo *exception)
815 % A description of each parameter follows:
817 % o image: the image.
819 % o radius: the radius of the Gaussian, in pixels, not counting the center
822 % o sigma: the standard deviation of the Gaussian, in pixels.
824 % o exception: return any errors or warnings in this structure.
828 static double *GetBlurKernel(const size_t width,const double sigma)
842 Generate a 1-D convolution kernel.
844 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
845 kernel=(double *) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
846 if (kernel == (double *) NULL)
851 for (k=(-j); k <= j; k++)
853 kernel[i]=(double) (exp(-((double) k*k)/(2.0*MagickSigma*MagickSigma))/
854 (MagickSQ2PI*MagickSigma));
855 normalize+=kernel[i];
858 for (i=0; i < (ssize_t) width; i++)
859 kernel[i]/=normalize;
863 MagickExport Image *BlurImage(const Image *image,const double radius,
864 const double sigma,ExceptionInfo *exception)
866 #define BlurImageTag "Blur/Image"
896 Initialize blur image attributes.
898 assert(image != (Image *) NULL);
899 assert(image->signature == MagickSignature);
900 if (image->debug != MagickFalse)
901 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
902 assert(exception != (ExceptionInfo *) NULL);
903 assert(exception->signature == MagickSignature);
904 blur_image=CloneImage(image,0,0,MagickTrue,exception);
905 if (blur_image == (Image *) NULL)
906 return((Image *) NULL);
907 if (fabs(sigma) <= MagickEpsilon)
909 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
911 blur_image=DestroyImage(blur_image);
912 return((Image *) NULL);
914 width=GetOptimalKernelWidth1D(radius,sigma);
915 kernel=GetBlurKernel(width,sigma);
916 if (kernel == (double *) NULL)
918 blur_image=DestroyImage(blur_image);
919 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
921 if (image->debug != MagickFalse)
924 format[MaxTextExtent],
927 register const double
930 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
931 " blur image with kernel width %.20g:",(double) width);
932 message=AcquireString("");
934 for (i=0; i < (ssize_t) width; i++)
937 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) i);
938 (void) ConcatenateString(&message,format);
939 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
940 (void) ConcatenateString(&message,format);
941 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
943 message=DestroyString(message);
950 center=(ssize_t) GetPixelChannels(image)*(width/2L);
951 image_view=AcquireVirtualCacheView(image,exception);
952 blur_view=AcquireAuthenticCacheView(blur_image,exception);
953 #if defined(MAGICKCORE_OPENMP_SUPPORT)
954 #pragma omp parallel for schedule(static,4) shared(progress,status) \
955 dynamic_number_threads(image,image->columns,image->rows,1)
957 for (y=0; y < (ssize_t) image->rows; y++)
959 register const Quantum
968 if (status == MagickFalse)
970 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y,
971 image->columns+width,1,exception);
972 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
974 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
979 for (x=0; x < (ssize_t) image->columns; x++)
984 if (GetPixelMask(image,p) != 0)
986 p+=GetPixelChannels(image);
987 q+=GetPixelChannels(blur_image);
990 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1004 register const double
1007 register const Quantum
1013 channel=GetPixelChannelMapChannel(image,i);
1014 traits=GetPixelChannelMapTraits(image,channel);
1015 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
1016 if ((traits == UndefinedPixelTrait) ||
1017 (blur_traits == UndefinedPixelTrait))
1019 if ((blur_traits & CopyPixelTrait) != 0)
1021 SetPixelChannel(blur_image,channel,p[center+i],q);
1027 if ((blur_traits & BlendPixelTrait) == 0)
1032 for (u=0; u < (ssize_t) width; u++)
1034 pixel+=(*k)*pixels[i];
1036 pixels+=GetPixelChannels(image);
1038 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
1045 for (u=0; u < (ssize_t) width; u++)
1047 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
1048 pixel+=(*k)*alpha*pixels[i];
1051 pixels+=GetPixelChannels(image);
1053 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1054 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
1056 p+=GetPixelChannels(image);
1057 q+=GetPixelChannels(blur_image);
1059 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1061 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1066 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1067 #pragma omp critical (MagickCore_BlurImage)
1069 proceed=SetImageProgress(image,BlurImageTag,progress++,blur_image->rows+
1070 blur_image->columns);
1071 if (proceed == MagickFalse)
1075 blur_view=DestroyCacheView(blur_view);
1076 image_view=DestroyCacheView(image_view);
1080 center=(ssize_t) GetPixelChannels(blur_image)*(width/2L);
1081 image_view=AcquireVirtualCacheView(blur_image,exception);
1082 blur_view=AcquireAuthenticCacheView(blur_image,exception);
1083 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1084 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1085 dynamic_number_threads(image,image->columns,image->rows,1)
1087 for (x=0; x < (ssize_t) blur_image->columns; x++)
1089 register const Quantum
1098 if (status == MagickFalse)
1100 p=GetCacheViewVirtualPixels(image_view,x,-((ssize_t) width/2L),1,
1101 blur_image->rows+width,exception);
1102 q=GetCacheViewAuthenticPixels(blur_view,x,0,1,blur_image->rows,exception);
1103 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1108 for (y=0; y < (ssize_t) blur_image->rows; y++)
1113 if (GetPixelMask(image,p) != 0)
1115 p+=GetPixelChannels(blur_image);
1116 q+=GetPixelChannels(blur_image);
1119 for (i=0; i < (ssize_t) GetPixelChannels(blur_image); i++)
1133 register const double
1136 register const Quantum
1142 channel=GetPixelChannelMapChannel(blur_image,i);
1143 traits=GetPixelChannelMapTraits(blur_image,channel);
1144 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
1145 if ((traits == UndefinedPixelTrait) ||
1146 (blur_traits == UndefinedPixelTrait))
1148 if ((blur_traits & CopyPixelTrait) != 0)
1150 SetPixelChannel(blur_image,channel,p[center+i],q);
1156 if ((blur_traits & BlendPixelTrait) == 0)
1161 for (u=0; u < (ssize_t) width; u++)
1163 pixel+=(*k)*pixels[i];
1165 pixels+=GetPixelChannels(blur_image);
1167 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
1174 for (u=0; u < (ssize_t) width; u++)
1176 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(blur_image,
1178 pixel+=(*k)*alpha*pixels[i];
1181 pixels+=GetPixelChannels(blur_image);
1183 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1184 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
1186 p+=GetPixelChannels(blur_image);
1187 q+=GetPixelChannels(blur_image);
1189 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1191 if (blur_image->progress_monitor != (MagickProgressMonitor) NULL)
1196 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1197 #pragma omp critical (MagickCore_BlurImage)
1199 proceed=SetImageProgress(blur_image,BlurImageTag,progress++,
1200 blur_image->rows+blur_image->columns);
1201 if (proceed == MagickFalse)
1205 blur_view=DestroyCacheView(blur_view);
1206 image_view=DestroyCacheView(image_view);
1207 kernel=(double *) RelinquishAlignedMemory(kernel);
1208 blur_image->type=image->type;
1209 if (status == MagickFalse)
1210 blur_image=DestroyImage(blur_image);
1215 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1219 % C o n v o l v e I m a g e %
1223 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1225 % ConvolveImage() applies a custom convolution kernel to the image.
1227 % The format of the ConvolveImage method is:
1229 % Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
1230 % ExceptionInfo *exception)
1232 % A description of each parameter follows:
1234 % o image: the image.
1236 % o kernel: the filtering kernel.
1238 % o exception: return any errors or warnings in this structure.
1241 MagickExport Image *ConvolveImage(const Image *image,
1242 const KernelInfo *kernel_info,ExceptionInfo *exception)
1244 return(MorphologyImage(image,CorrelateMorphology,1,kernel_info,exception));
1248 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1252 % D e s p e c k l e I m a g e %
1256 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1258 % DespeckleImage() reduces the speckle noise in an image while perserving the
1259 % edges of the original image. A speckle removing filter uses a complementary % hulling technique (raising pixels that are darker than their surrounding
1260 % neighbors, then complementarily lowering pixels that are brighter than their
1261 % surrounding neighbors) to reduce the speckle index of that image (reference
1262 % Crimmins speckle removal).
1264 % The format of the DespeckleImage method is:
1266 % Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1268 % A description of each parameter follows:
1270 % o image: the image.
1272 % o exception: return any errors or warnings in this structure.
1276 static void Hull(const Image *image,const ssize_t x_offset,
1277 const ssize_t y_offset,const size_t columns,const size_t rows,
1278 const int polarity,Quantum *restrict f,Quantum *restrict g)
1289 assert(f != (Quantum *) NULL);
1290 assert(g != (Quantum *) NULL);
1293 r=p+(y_offset*(columns+2)+x_offset);
1294 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1295 #pragma omp parallel for schedule(static) \
1296 dynamic_number_threads(image,columns,rows,1)
1298 for (y=0; y < (ssize_t) rows; y++)
1307 i=(2*y+1)+y*columns;
1309 for (x=0; x < (ssize_t) columns; x++)
1311 v=(SignedQuantum) p[i];
1312 if ((SignedQuantum) r[i] >= (v+ScaleCharToQuantum(2)))
1313 v+=ScaleCharToQuantum(1);
1318 for (x=0; x < (ssize_t) columns; x++)
1320 v=(SignedQuantum) p[i];
1321 if ((SignedQuantum) r[i] <= (v-ScaleCharToQuantum(2)))
1322 v-=ScaleCharToQuantum(1);
1329 r=q+(y_offset*(columns+2)+x_offset);
1330 s=q-(y_offset*(columns+2)+x_offset);
1331 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1332 #pragma omp parallel for schedule(static) \
1333 dynamic_number_threads(image,columns,rows,1)
1335 for (y=0; y < (ssize_t) rows; y++)
1344 i=(2*y+1)+y*columns;
1346 for (x=0; x < (ssize_t) columns; x++)
1348 v=(SignedQuantum) q[i];
1349 if (((SignedQuantum) s[i] >= (v+ScaleCharToQuantum(2))) &&
1350 ((SignedQuantum) r[i] > v))
1351 v+=ScaleCharToQuantum(1);
1356 for (x=0; x < (ssize_t) columns; x++)
1358 v=(SignedQuantum) q[i];
1359 if (((SignedQuantum) s[i] <= (v-ScaleCharToQuantum(2))) &&
1360 ((SignedQuantum) r[i] < v))
1361 v-=ScaleCharToQuantum(1);
1368 MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1370 #define DespeckleImageTag "Despeckle/Image"
1392 static const ssize_t
1393 X[4] = {0, 1, 1,-1},
1394 Y[4] = {1, 0, 1, 1};
1397 Allocate despeckled image.
1399 assert(image != (const Image *) NULL);
1400 assert(image->signature == MagickSignature);
1401 if (image->debug != MagickFalse)
1402 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1403 assert(exception != (ExceptionInfo *) NULL);
1404 assert(exception->signature == MagickSignature);
1405 despeckle_image=CloneImage(image,0,0,MagickTrue,exception);
1406 if (despeckle_image == (Image *) NULL)
1407 return((Image *) NULL);
1408 status=SetImageStorageClass(despeckle_image,DirectClass,exception);
1409 if (status == MagickFalse)
1411 despeckle_image=DestroyImage(despeckle_image);
1412 return((Image *) NULL);
1415 Allocate image buffer.
1417 length=(size_t) ((image->columns+2)*(image->rows+2));
1418 pixels=(Quantum *) AcquireQuantumMemory(length,sizeof(*pixels));
1419 buffer=(Quantum *) AcquireQuantumMemory(length,sizeof(*buffer));
1420 if ((pixels == (Quantum *) NULL) || (buffer == (Quantum *) NULL))
1422 if (buffer != (Quantum *) NULL)
1423 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1424 if (pixels != (Quantum *) NULL)
1425 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1426 despeckle_image=DestroyImage(despeckle_image);
1427 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1430 Reduce speckle in the image.
1433 image_view=AcquireVirtualCacheView(image,exception);
1434 despeckle_view=AcquireAuthenticCacheView(despeckle_image,exception);
1435 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1452 if (status == MagickFalse)
1454 channel=GetPixelChannelMapChannel(image,i);
1455 traits=GetPixelChannelMapTraits(image,channel);
1456 despeckle_traits=GetPixelChannelMapTraits(despeckle_image,channel);
1457 if ((traits == UndefinedPixelTrait) ||
1458 (despeckle_traits == UndefinedPixelTrait))
1460 if ((despeckle_traits & CopyPixelTrait) != 0)
1462 (void) ResetMagickMemory(pixels,0,length*sizeof(*pixels));
1463 j=(ssize_t) image->columns+2;
1464 for (y=0; y < (ssize_t) image->rows; y++)
1466 register const Quantum
1469 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1470 if (p == (const Quantum *) NULL)
1476 for (x=0; x < (ssize_t) image->columns; x++)
1479 p+=GetPixelChannels(image);
1483 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
1484 for (k=0; k < 4; k++)
1486 Hull(image,X[k],Y[k],image->columns,image->rows,1,pixels,buffer);
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);
1491 j=(ssize_t) image->columns+2;
1492 for (y=0; y < (ssize_t) image->rows; y++)
1500 q=QueueCacheViewAuthenticPixels(despeckle_view,0,y,
1501 despeckle_image->columns,1,exception);
1502 if (q == (Quantum *) NULL)
1508 for (x=0; x < (ssize_t) image->columns; x++)
1510 SetPixelChannel(despeckle_image,channel,pixels[j++],q);
1511 q+=GetPixelChannels(despeckle_image);
1513 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1514 if (sync == MagickFalse)
1518 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1523 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1524 GetPixelChannels(image));
1525 if (proceed == MagickFalse)
1529 despeckle_view=DestroyCacheView(despeckle_view);
1530 image_view=DestroyCacheView(image_view);
1531 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1532 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1533 despeckle_image->type=image->type;
1534 if (status == MagickFalse)
1535 despeckle_image=DestroyImage(despeckle_image);
1536 return(despeckle_image);
1540 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1544 % E d g e I m a g e %
1548 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1550 % EdgeImage() finds edges in an image. Radius defines the radius of the
1551 % convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1554 % The format of the EdgeImage method is:
1556 % Image *EdgeImage(const Image *image,const double radius,
1557 % const double sigma,ExceptionInfo *exception)
1559 % A description of each parameter follows:
1561 % o image: the image.
1563 % o radius: the radius of the pixel neighborhood.
1565 % o sigma: the standard deviation of the Gaussian, in pixels.
1567 % o exception: return any errors or warnings in this structure.
1570 MagickExport Image *EdgeImage(const Image *image,const double radius,
1571 const double sigma,ExceptionInfo *exception)
1590 assert(image != (const Image *) NULL);
1591 assert(image->signature == MagickSignature);
1592 if (image->debug != MagickFalse)
1593 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1594 assert(exception != (ExceptionInfo *) NULL);
1595 assert(exception->signature == MagickSignature);
1596 width=GetOptimalKernelWidth1D(radius,sigma);
1597 kernel_info=AcquireKernelInfo((const char *) NULL);
1598 if (kernel_info == (KernelInfo *) NULL)
1599 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1600 kernel_info->width=width;
1601 kernel_info->height=width;
1602 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
1603 kernel_info->width*sizeof(*kernel_info->values));
1604 if (kernel_info->values == (double *) NULL)
1606 kernel_info=DestroyKernelInfo(kernel_info);
1607 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1609 j=(ssize_t) kernel_info->width/2;
1611 for (v=(-j); v <= j; v++)
1613 for (u=(-j); u <= j; u++)
1615 kernel_info->values[i]=(-1.0);
1619 kernel_info->values[i/2]=(double) (width*width-1.0);
1620 edge_image=ConvolveImage(image,kernel_info,exception);
1621 kernel_info=DestroyKernelInfo(kernel_info);
1626 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1630 % E m b o s s I m a g e %
1634 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1636 % EmbossImage() returns a grayscale image with a three-dimensional effect.
1637 % We convolve the image with a Gaussian operator of the given radius and
1638 % standard deviation (sigma). For reasonable results, radius should be
1639 % larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1642 % The format of the EmbossImage method is:
1644 % Image *EmbossImage(const Image *image,const double radius,
1645 % const double sigma,ExceptionInfo *exception)
1647 % A description of each parameter follows:
1649 % o image: the image.
1651 % o radius: the radius of the pixel neighborhood.
1653 % o sigma: the standard deviation of the Gaussian, in pixels.
1655 % o exception: return any errors or warnings in this structure.
1658 MagickExport Image *EmbossImage(const Image *image,const double radius,
1659 const double sigma,ExceptionInfo *exception)
1679 assert(image != (const Image *) NULL);
1680 assert(image->signature == MagickSignature);
1681 if (image->debug != MagickFalse)
1682 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1683 assert(exception != (ExceptionInfo *) NULL);
1684 assert(exception->signature == MagickSignature);
1685 width=GetOptimalKernelWidth1D(radius,sigma);
1686 kernel_info=AcquireKernelInfo((const char *) NULL);
1687 if (kernel_info == (KernelInfo *) NULL)
1688 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1689 kernel_info->width=width;
1690 kernel_info->height=width;
1691 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
1692 kernel_info->width*sizeof(*kernel_info->values));
1693 if (kernel_info->values == (double *) NULL)
1695 kernel_info=DestroyKernelInfo(kernel_info);
1696 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1698 j=(ssize_t) kernel_info->width/2;
1701 for (v=(-j); v <= j; v++)
1703 for (u=(-j); u <= j; u++)
1705 kernel_info->values[i]=(double) (((u < 0) || (v < 0) ? -8.0 : 8.0)*
1706 exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
1707 (2.0*MagickPI*MagickSigma*MagickSigma));
1709 kernel_info->values[i]=0.0;
1714 emboss_image=ConvolveImage(image,kernel_info,exception);
1715 kernel_info=DestroyKernelInfo(kernel_info);
1716 if (emboss_image != (Image *) NULL)
1717 (void) EqualizeImage(emboss_image,exception);
1718 return(emboss_image);
1722 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1726 % G a u s s i a n B l u r I m a g e %
1730 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1732 % GaussianBlurImage() blurs an image. We convolve the image with a
1733 % Gaussian operator of the given radius and standard deviation (sigma).
1734 % For reasonable results, the radius should be larger than sigma. Use a
1735 % radius of 0 and GaussianBlurImage() selects a suitable radius for you
1737 % The format of the GaussianBlurImage method is:
1739 % Image *GaussianBlurImage(const Image *image,onst double radius,
1740 % const double sigma,ExceptionInfo *exception)
1742 % A description of each parameter follows:
1744 % o image: the image.
1746 % o radius: the radius of the Gaussian, in pixels, not counting the center
1749 % o sigma: the standard deviation of the Gaussian, in pixels.
1751 % o exception: return any errors or warnings in this structure.
1754 MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
1755 const double sigma,ExceptionInfo *exception)
1774 assert(image != (const Image *) NULL);
1775 assert(image->signature == MagickSignature);
1776 if (image->debug != MagickFalse)
1777 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1778 assert(exception != (ExceptionInfo *) NULL);
1779 assert(exception->signature == MagickSignature);
1780 width=GetOptimalKernelWidth2D(radius,sigma);
1781 kernel_info=AcquireKernelInfo((const char *) NULL);
1782 if (kernel_info == (KernelInfo *) NULL)
1783 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1784 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1785 kernel_info->width=width;
1786 kernel_info->height=width;
1787 kernel_info->signature=MagickSignature;
1788 kernel_info->values=(double *) AcquireAlignedMemory(
1789 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1790 if (kernel_info->values == (double *) NULL)
1792 kernel_info=DestroyKernelInfo(kernel_info);
1793 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1795 j=(ssize_t) kernel_info->width/2;
1797 for (v=(-j); v <= j; v++)
1799 for (u=(-j); u <= j; u++)
1801 kernel_info->values[i]=(double) (exp(-((double) u*u+v*v)/(2.0*
1802 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
1806 blur_image=ConvolveImage(image,kernel_info,exception);
1807 kernel_info=DestroyKernelInfo(kernel_info);
1812 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1816 % M o t i o n B l u r I m a g e %
1820 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1822 % MotionBlurImage() simulates motion blur. We convolve the image with a
1823 % Gaussian operator of the given radius and standard deviation (sigma).
1824 % For reasonable results, radius should be larger than sigma. Use a
1825 % radius of 0 and MotionBlurImage() selects a suitable radius for you.
1826 % Angle gives the angle of the blurring motion.
1828 % Andrew Protano contributed this effect.
1830 % The format of the MotionBlurImage method is:
1832 % Image *MotionBlurImage(const Image *image,const double radius,
1833 % const double sigma,const double angle,ExceptionInfo *exception)
1835 % A description of each parameter follows:
1837 % o image: the image.
1839 % o radius: the radius of the Gaussian, in pixels, not counting
1842 % o sigma: the standard deviation of the Gaussian, in pixels.
1844 % o angle: Apply the effect along this angle.
1846 % o exception: return any errors or warnings in this structure.
1850 static double *GetMotionBlurKernel(const size_t width,const double sigma)
1860 Generate a 1-D convolution kernel.
1862 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1863 kernel=(double *) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
1864 if (kernel == (double *) NULL)
1867 for (i=0; i < (ssize_t) width; i++)
1869 kernel[i]=(double) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
1870 MagickSigma)))/(MagickSQ2PI*MagickSigma));
1871 normalize+=kernel[i];
1873 for (i=0; i < (ssize_t) width; i++)
1874 kernel[i]/=normalize;
1878 MagickExport Image *MotionBlurImage(const Image *image,const double radius,
1879 const double sigma,const double angle,ExceptionInfo *exception)
1913 assert(image != (Image *) NULL);
1914 assert(image->signature == MagickSignature);
1915 if (image->debug != MagickFalse)
1916 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1917 assert(exception != (ExceptionInfo *) NULL);
1918 width=GetOptimalKernelWidth1D(radius,sigma);
1919 kernel=GetMotionBlurKernel(width,sigma);
1920 if (kernel == (double *) NULL)
1921 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1922 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
1923 if (offset == (OffsetInfo *) NULL)
1925 kernel=(double *) RelinquishAlignedMemory(kernel);
1926 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1928 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1929 if (blur_image == (Image *) NULL)
1931 kernel=(double *) RelinquishAlignedMemory(kernel);
1932 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1933 return((Image *) NULL);
1935 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
1937 kernel=(double *) RelinquishAlignedMemory(kernel);
1938 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1939 blur_image=DestroyImage(blur_image);
1940 return((Image *) NULL);
1942 point.x=(double) width*sin(DegreesToRadians(angle));
1943 point.y=(double) width*cos(DegreesToRadians(angle));
1944 for (i=0; i < (ssize_t) width; i++)
1946 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
1947 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
1954 image_view=AcquireVirtualCacheView(image,exception);
1955 motion_view=AcquireVirtualCacheView(image,exception);
1956 blur_view=AcquireAuthenticCacheView(blur_image,exception);
1957 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1958 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1959 dynamic_number_threads(image,image->columns,image->rows,1)
1961 for (y=0; y < (ssize_t) image->rows; y++)
1963 register const Quantum
1972 if (status == MagickFalse)
1974 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1975 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
1977 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1982 for (x=0; x < (ssize_t) image->columns; x++)
1987 if (GetPixelMask(image,p) != 0)
1989 p+=GetPixelChannels(image);
1990 q+=GetPixelChannels(blur_image);
1993 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2007 register const Quantum
2016 channel=GetPixelChannelMapChannel(image,i);
2017 traits=GetPixelChannelMapTraits(image,channel);
2018 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
2019 if ((traits == UndefinedPixelTrait) ||
2020 (blur_traits == UndefinedPixelTrait))
2022 if ((blur_traits & CopyPixelTrait) != 0)
2024 SetPixelChannel(blur_image,channel,p[i],q);
2029 if ((blur_traits & BlendPixelTrait) == 0)
2031 for (j=0; j < (ssize_t) width; j++)
2033 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+
2034 offset[j].y,1,1,exception);
2035 if (r == (const Quantum *) NULL)
2043 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
2048 for (j=0; j < (ssize_t) width; j++)
2050 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+offset[j].y,1,
2052 if (r == (const Quantum *) NULL)
2057 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,r));
2058 pixel+=(*k)*alpha*r[i];
2062 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
2063 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2065 p+=GetPixelChannels(image);
2066 q+=GetPixelChannels(blur_image);
2068 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2070 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2075 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2076 #pragma omp critical (MagickCore_MotionBlurImage)
2078 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2079 if (proceed == MagickFalse)
2083 blur_view=DestroyCacheView(blur_view);
2084 motion_view=DestroyCacheView(motion_view);
2085 image_view=DestroyCacheView(image_view);
2086 kernel=(double *) RelinquishAlignedMemory(kernel);
2087 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2088 if (status == MagickFalse)
2089 blur_image=DestroyImage(blur_image);
2094 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2098 % P r e v i e w I m a g e %
2102 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2104 % PreviewImage() tiles 9 thumbnails of the specified image with an image
2105 % processing operation applied with varying parameters. This may be helpful
2106 % pin-pointing an appropriate parameter for a particular image processing
2109 % The format of the PreviewImages method is:
2111 % Image *PreviewImages(const Image *image,const PreviewType preview,
2112 % ExceptionInfo *exception)
2114 % A description of each parameter follows:
2116 % o image: the image.
2118 % o preview: the image processing operation.
2120 % o exception: return any errors or warnings in this structure.
2123 MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2124 ExceptionInfo *exception)
2126 #define NumberTiles 9
2127 #define PreviewImageTag "Preview/Image"
2128 #define DefaultPreviewGeometry "204x204+10+10"
2131 factor[MaxTextExtent],
2132 label[MaxTextExtent];
2174 Open output image file.
2176 assert(image != (Image *) NULL);
2177 assert(image->signature == MagickSignature);
2178 if (image->debug != MagickFalse)
2179 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2183 preview_info=AcquireImageInfo();
2184 SetGeometry(image,&geometry);
2185 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2186 &geometry.width,&geometry.height);
2187 images=NewImageList();
2189 GetQuantizeInfo(&quantize_info);
2195 for (i=0; i < NumberTiles; i++)
2197 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2198 if (thumbnail == (Image *) NULL)
2200 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2202 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel,exception);
2203 if (i == (NumberTiles/2))
2205 (void) QueryColorCompliance("#dfdfdf",AllCompliance,
2206 &thumbnail->matte_color,exception);
2207 AppendImageToList(&images,thumbnail);
2215 preview_image=RotateImage(thumbnail,degrees,exception);
2216 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
2222 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
2223 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",
2224 degrees,2.0*degrees);
2229 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2230 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
2231 preview_image=RollImage(thumbnail,x,y,exception);
2232 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
2233 (double) x,(double) y);
2238 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2239 if (preview_image == (Image *) NULL)
2241 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",
2243 (void) ModulateImage(preview_image,factor,exception);
2244 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2247 case SaturationPreview:
2249 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2250 if (preview_image == (Image *) NULL)
2252 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",
2254 (void) ModulateImage(preview_image,factor,exception);
2255 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2258 case BrightnessPreview:
2260 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2261 if (preview_image == (Image *) NULL)
2263 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
2264 (void) ModulateImage(preview_image,factor,exception);
2265 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2271 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2272 if (preview_image == (Image *) NULL)
2275 (void) GammaImage(preview_image,gamma,exception);
2276 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
2281 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2282 if (preview_image != (Image *) NULL)
2283 for (x=0; x < i; x++)
2284 (void) ContrastImage(preview_image,MagickTrue,exception);
2285 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
2291 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2292 if (preview_image == (Image *) NULL)
2294 for (x=0; x < i; x++)
2295 (void) ContrastImage(preview_image,MagickFalse,exception);
2296 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
2300 case GrayscalePreview:
2302 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2303 if (preview_image == (Image *) NULL)
2306 quantize_info.number_colors=colors;
2307 quantize_info.colorspace=GRAYColorspace;
2308 (void) QuantizeImage(&quantize_info,preview_image,exception);
2309 (void) FormatLocaleString(label,MaxTextExtent,
2310 "-colorspace gray -colors %.20g",(double) colors);
2313 case QuantizePreview:
2315 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2316 if (preview_image == (Image *) NULL)
2319 quantize_info.number_colors=colors;
2320 (void) QuantizeImage(&quantize_info,preview_image,exception);
2321 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
2325 case DespecklePreview:
2327 for (x=0; x < (i-1); x++)
2329 preview_image=DespeckleImage(thumbnail,exception);
2330 if (preview_image == (Image *) NULL)
2332 thumbnail=DestroyImage(thumbnail);
2333 thumbnail=preview_image;
2335 preview_image=DespeckleImage(thumbnail,exception);
2336 if (preview_image == (Image *) NULL)
2338 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
2342 case ReduceNoisePreview:
2344 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2345 (size_t) radius,exception);
2346 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
2349 case AddNoisePreview:
2355 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2360 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2365 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2370 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2375 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2380 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2385 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2389 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2390 (size_t) i,exception);
2391 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
2394 case SharpenPreview:
2396 preview_image=SharpenImage(thumbnail,radius,sigma,exception);
2397 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",
2403 preview_image=BlurImage(thumbnail,radius,sigma,exception);
2404 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
2408 case ThresholdPreview:
2410 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2411 if (preview_image == (Image *) NULL)
2413 (void) BilevelImage(thumbnail,(double) (percentage*((MagickRealType)
2414 QuantumRange+1.0))/100.0,exception);
2415 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",
2416 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
2419 case EdgeDetectPreview:
2421 preview_image=EdgeImage(thumbnail,radius,sigma,exception);
2422 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
2427 preview_image=SpreadImage(thumbnail,radius,thumbnail->interpolate,
2429 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",
2433 case SolarizePreview:
2435 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2436 if (preview_image == (Image *) NULL)
2438 (void) SolarizeImage(preview_image,(double) QuantumRange*
2439 percentage/100.0,exception);
2440 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
2441 (QuantumRange*percentage)/100.0);
2447 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2449 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",
2455 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2456 if (preview_image == (Image *) NULL)
2458 geometry.width=(size_t) (2*i+2);
2459 geometry.height=(size_t) (2*i+2);
2462 (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
2463 (void) FormatLocaleString(label,MaxTextExtent,
2464 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
2465 geometry.height,(double) geometry.x,(double) geometry.y);
2468 case SegmentPreview:
2470 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2471 if (preview_image == (Image *) NULL)
2474 (void) SegmentImage(preview_image,sRGBColorspace,MagickFalse,threshold,
2475 threshold,exception);
2476 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
2477 threshold,threshold);
2482 preview_image=SwirlImage(thumbnail,degrees,image->interpolate,
2484 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
2488 case ImplodePreview:
2491 preview_image=ImplodeImage(thumbnail,degrees,image->interpolate,
2493 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
2499 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,
2500 image->interpolate,exception);
2501 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",
2502 0.5*degrees,2.0*degrees);
2505 case OilPaintPreview:
2507 preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2509 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2513 case CharcoalDrawingPreview:
2515 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
2517 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2524 filename[MaxTextExtent];
2532 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2533 if (preview_image == (Image *) NULL)
2535 preview_info->quality=(size_t) percentage;
2536 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
2537 preview_info->quality);
2538 file=AcquireUniqueFileResource(filename);
2541 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
2542 "jpeg:%s",filename);
2543 status=WriteImage(preview_info,preview_image,exception);
2544 if (status != MagickFalse)
2549 (void) CopyMagickString(preview_info->filename,
2550 preview_image->filename,MaxTextExtent);
2551 quality_image=ReadImage(preview_info,exception);
2552 if (quality_image != (Image *) NULL)
2554 preview_image=DestroyImage(preview_image);
2555 preview_image=quality_image;
2558 (void) RelinquishUniqueFileResource(preview_image->filename);
2559 if ((GetBlobSize(preview_image)/1024) >= 1024)
2560 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
2561 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2564 if (GetBlobSize(preview_image) >= 1024)
2565 (void) FormatLocaleString(label,MaxTextExtent,
2566 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
2567 GetBlobSize(preview_image))/1024.0);
2569 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
2570 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
2574 thumbnail=DestroyImage(thumbnail);
2578 if (preview_image == (Image *) NULL)
2580 (void) DeleteImageProperty(preview_image,"label");
2581 (void) SetImageProperty(preview_image,"label",label,exception);
2582 AppendImageToList(&images,preview_image);
2583 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2585 if (proceed == MagickFalse)
2588 if (images == (Image *) NULL)
2590 preview_info=DestroyImageInfo(preview_info);
2591 return((Image *) NULL);
2596 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2597 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2598 montage_info->shadow=MagickTrue;
2599 (void) CloneString(&montage_info->tile,"3x3");
2600 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2601 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2602 montage_image=MontageImages(images,montage_info,exception);
2603 montage_info=DestroyMontageInfo(montage_info);
2604 images=DestroyImageList(images);
2605 if (montage_image == (Image *) NULL)
2606 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2607 if (montage_image->montage != (char *) NULL)
2610 Free image directory.
2612 montage_image->montage=(char *) RelinquishMagickMemory(
2613 montage_image->montage);
2614 if (image->directory != (char *) NULL)
2615 montage_image->directory=(char *) RelinquishMagickMemory(
2616 montage_image->directory);
2618 preview_info=DestroyImageInfo(preview_info);
2619 return(montage_image);
2623 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2627 % R a d i a l B l u r I m a g e %
2631 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2633 % RadialBlurImage() applies a radial blur to the image.
2635 % Andrew Protano contributed this effect.
2637 % The format of the RadialBlurImage method is:
2639 % Image *RadialBlurImage(const Image *image,const double angle,
2640 % ExceptionInfo *exception)
2642 % A description of each parameter follows:
2644 % o image: the image.
2646 % o angle: the angle of the radial blur.
2650 % o exception: return any errors or warnings in this structure.
2653 MagickExport Image *RadialBlurImage(const Image *image,const double angle,
2654 ExceptionInfo *exception)
2690 Allocate blur image.
2692 assert(image != (Image *) NULL);
2693 assert(image->signature == MagickSignature);
2694 if (image->debug != MagickFalse)
2695 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2696 assert(exception != (ExceptionInfo *) NULL);
2697 assert(exception->signature == MagickSignature);
2698 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2699 if (blur_image == (Image *) NULL)
2700 return((Image *) NULL);
2701 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2703 blur_image=DestroyImage(blur_image);
2704 return((Image *) NULL);
2706 blur_center.x=(double) image->columns/2.0;
2707 blur_center.y=(double) image->rows/2.0;
2708 blur_radius=hypot(blur_center.x,blur_center.y);
2709 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
2710 theta=DegreesToRadians(angle)/(MagickRealType) (n-1);
2711 cos_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2712 sizeof(*cos_theta));
2713 sin_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2714 sizeof(*sin_theta));
2715 if ((cos_theta == (MagickRealType *) NULL) ||
2716 (sin_theta == (MagickRealType *) NULL))
2718 blur_image=DestroyImage(blur_image);
2719 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2721 offset=theta*(MagickRealType) (n-1)/2.0;
2722 for (i=0; i < (ssize_t) n; i++)
2724 cos_theta[i]=cos((double) (theta*i-offset));
2725 sin_theta[i]=sin((double) (theta*i-offset));
2732 image_view=AcquireVirtualCacheView(image,exception);
2733 radial_view=AcquireVirtualCacheView(image,exception);
2734 blur_view=AcquireAuthenticCacheView(blur_image,exception);
2735 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2736 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2737 dynamic_number_threads(image,image->columns,image->rows,1)
2739 for (y=0; y < (ssize_t) image->rows; y++)
2741 register const Quantum
2750 if (status == MagickFalse)
2752 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2753 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2755 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2760 for (x=0; x < (ssize_t) image->columns; x++)
2774 center.x=(double) x-blur_center.x;
2775 center.y=(double) y-blur_center.y;
2776 radius=hypot((double) center.x,center.y);
2781 step=(size_t) (blur_radius/radius);
2788 if (GetPixelMask(image,p) != 0)
2790 p+=GetPixelChannels(image);
2791 q+=GetPixelChannels(blur_image);
2794 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2807 register const Quantum
2813 channel=GetPixelChannelMapChannel(image,i);
2814 traits=GetPixelChannelMapTraits(image,channel);
2815 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
2816 if ((traits == UndefinedPixelTrait) ||
2817 (blur_traits == UndefinedPixelTrait))
2819 if ((blur_traits & CopyPixelTrait) != 0)
2821 SetPixelChannel(blur_image,channel,p[i],q);
2826 if ((blur_traits & BlendPixelTrait) == 0)
2828 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2830 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2831 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2832 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2834 if (r == (const Quantum *) NULL)
2842 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
2843 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2846 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2848 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2849 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2850 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2852 if (r == (const Quantum *) NULL)
2857 pixel+=GetPixelAlpha(image,r)*r[i];
2858 gamma+=GetPixelAlpha(image,r);
2860 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
2861 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2863 p+=GetPixelChannels(image);
2864 q+=GetPixelChannels(blur_image);
2866 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2868 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2873 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2874 #pragma omp critical (MagickCore_RadialBlurImage)
2876 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2877 if (proceed == MagickFalse)
2881 blur_view=DestroyCacheView(blur_view);
2882 radial_view=DestroyCacheView(radial_view);
2883 image_view=DestroyCacheView(image_view);
2884 cos_theta=(MagickRealType *) RelinquishMagickMemory(cos_theta);
2885 sin_theta=(MagickRealType *) RelinquishMagickMemory(sin_theta);
2886 if (status == MagickFalse)
2887 blur_image=DestroyImage(blur_image);
2892 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2896 % S e l e c t i v e B l u r I m a g e %
2900 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2902 % SelectiveBlurImage() selectively blur pixels within a contrast threshold.
2903 % It is similar to the unsharpen mask that sharpens everything with contrast
2904 % above a certain threshold.
2906 % The format of the SelectiveBlurImage method is:
2908 % Image *SelectiveBlurImage(const Image *image,const double radius,
2909 % const double sigma,const double threshold,ExceptionInfo *exception)
2911 % A description of each parameter follows:
2913 % o image: the image.
2915 % o radius: the radius of the Gaussian, in pixels, not counting the center
2918 % o sigma: the standard deviation of the Gaussian, in pixels.
2920 % o threshold: only pixels within this contrast threshold are included
2921 % in the blur operation.
2923 % o exception: return any errors or warnings in this structure.
2926 MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
2927 const double sigma,const double threshold,ExceptionInfo *exception)
2929 #define SelectiveBlurImageTag "SelectiveBlur/Image"
2961 Initialize blur image attributes.
2963 assert(image != (Image *) NULL);
2964 assert(image->signature == MagickSignature);
2965 if (image->debug != MagickFalse)
2966 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2967 assert(exception != (ExceptionInfo *) NULL);
2968 assert(exception->signature == MagickSignature);
2969 width=GetOptimalKernelWidth1D(radius,sigma);
2970 kernel=(double *) AcquireAlignedMemory((size_t) width,width*sizeof(*kernel));
2971 if (kernel == (double *) NULL)
2972 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2973 j=(ssize_t) width/2;
2975 for (v=(-j); v <= j; v++)
2977 for (u=(-j); u <= j; u++)
2978 kernel[i++]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
2979 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
2981 if (image->debug != MagickFalse)
2984 format[MaxTextExtent],
2987 register const double
2994 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
2995 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
2997 message=AcquireString("");
2999 for (v=0; v < (ssize_t) width; v++)
3002 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
3003 (void) ConcatenateString(&message,format);
3004 for (u=0; u < (ssize_t) width; u++)
3006 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",*k++);
3007 (void) ConcatenateString(&message,format);
3009 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
3011 message=DestroyString(message);
3013 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3014 if (blur_image == (Image *) NULL)
3015 return((Image *) NULL);
3016 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
3018 blur_image=DestroyImage(blur_image);
3019 return((Image *) NULL);
3022 Threshold blur image.
3026 center=(ssize_t) (GetPixelChannels(image)*(image->columns+width)*(width/2L)+
3027 GetPixelChannels(image)*(width/2L));
3028 image_view=AcquireVirtualCacheView(image,exception);
3029 blur_view=AcquireAuthenticCacheView(blur_image,exception);
3030 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3031 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3032 dynamic_number_threads(image,image->columns,image->rows,1)
3034 for (y=0; y < (ssize_t) image->rows; y++)
3042 register const Quantum
3051 if (status == MagickFalse)
3053 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
3054 (width/2L),image->columns+width,width,exception);
3055 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3057 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3062 for (x=0; x < (ssize_t) image->columns; x++)
3067 if (GetPixelMask(image,p) != 0)
3069 p+=GetPixelChannels(image);
3070 q+=GetPixelChannels(blur_image);
3073 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3088 register const double
3091 register const Quantum
3100 channel=GetPixelChannelMapChannel(image,i);
3101 traits=GetPixelChannelMapTraits(image,channel);
3102 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
3103 if ((traits == UndefinedPixelTrait) ||
3104 (blur_traits == UndefinedPixelTrait))
3106 if ((blur_traits & CopyPixelTrait) != 0)
3108 SetPixelChannel(blur_image,channel,p[center+i],q);
3114 intensity=(MagickRealType) GetPixelIntensity(image,p+center);
3116 if ((blur_traits & BlendPixelTrait) == 0)
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 pixel+=(*k)*pixels[i];
3129 pixels+=GetPixelChannels(image);
3131 pixels+=image->columns*GetPixelChannels(image);
3133 if (fabs((double) gamma) < MagickEpsilon)
3135 SetPixelChannel(blur_image,channel,p[center+i],q);
3138 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3139 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3142 for (v=0; v < (ssize_t) width; v++)
3144 for (u=0; u < (ssize_t) width; u++)
3146 contrast=GetPixelIntensity(image,pixels)-intensity;
3147 if (fabs(contrast) < threshold)
3149 alpha=(MagickRealType) (QuantumScale*
3150 GetPixelAlpha(image,pixels));
3151 pixel+=(*k)*alpha*pixels[i];
3155 pixels+=GetPixelChannels(image);
3157 pixels+=image->columns*GetPixelChannels(image);
3159 if (fabs((double) gamma) < MagickEpsilon)
3161 SetPixelChannel(blur_image,channel,p[center+i],q);
3164 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3165 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3167 p+=GetPixelChannels(image);
3168 q+=GetPixelChannels(blur_image);
3170 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3171 if (sync == MagickFalse)
3173 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3178 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3179 #pragma omp critical (MagickCore_SelectiveBlurImage)
3181 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3183 if (proceed == MagickFalse)
3187 blur_image->type=image->type;
3188 blur_view=DestroyCacheView(blur_view);
3189 image_view=DestroyCacheView(image_view);
3190 kernel=(double *) RelinquishAlignedMemory(kernel);
3191 if (status == MagickFalse)
3192 blur_image=DestroyImage(blur_image);
3197 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3201 % S h a d e I m a g e %
3205 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3207 % ShadeImage() shines a distant light on an image to create a
3208 % three-dimensional effect. You control the positioning of the light with
3209 % azimuth and elevation; azimuth is measured in degrees off the x axis
3210 % and elevation is measured in pixels above the Z axis.
3212 % The format of the ShadeImage method is:
3214 % Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3215 % const double azimuth,const double elevation,ExceptionInfo *exception)
3217 % A description of each parameter follows:
3219 % o image: the image.
3221 % o gray: A value other than zero shades the intensity of each pixel.
3223 % o azimuth, elevation: Define the light source direction.
3225 % o exception: return any errors or warnings in this structure.
3228 MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3229 const double azimuth,const double elevation,ExceptionInfo *exception)
3231 #define ShadeImageTag "Shade/Image"
3253 Initialize shaded image attributes.
3255 assert(image != (const Image *) NULL);
3256 assert(image->signature == MagickSignature);
3257 if (image->debug != MagickFalse)
3258 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3259 assert(exception != (ExceptionInfo *) NULL);
3260 assert(exception->signature == MagickSignature);
3261 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3262 if (shade_image == (Image *) NULL)
3263 return((Image *) NULL);
3264 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
3266 shade_image=DestroyImage(shade_image);
3267 return((Image *) NULL);
3270 Compute the light vector.
3272 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3273 cos(DegreesToRadians(elevation));
3274 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3275 cos(DegreesToRadians(elevation));
3276 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3282 image_view=AcquireVirtualCacheView(image,exception);
3283 shade_view=AcquireAuthenticCacheView(shade_image,exception);
3284 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3285 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3286 dynamic_number_threads(image,image->columns,image->rows,1)
3288 for (y=0; y < (ssize_t) image->rows; y++)
3298 register const Quantum
3310 if (status == MagickFalse)
3312 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
3313 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3315 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3321 Shade this row of pixels.
3323 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
3324 pre=p+GetPixelChannels(image);
3325 center=pre+(image->columns+2)*GetPixelChannels(image);
3326 post=center+(image->columns+2)*GetPixelChannels(image);
3327 for (x=0; x < (ssize_t) image->columns; x++)
3333 Determine the surface normal and compute shading.
3335 normal.x=(double) (GetPixelIntensity(image,pre-GetPixelChannels(image))+
3336 GetPixelIntensity(image,center-GetPixelChannels(image))+
3337 GetPixelIntensity(image,post-GetPixelChannels(image))-
3338 GetPixelIntensity(image,pre+GetPixelChannels(image))-
3339 GetPixelIntensity(image,center+GetPixelChannels(image))-
3340 GetPixelIntensity(image,post+GetPixelChannels(image)));
3341 normal.y=(double) (GetPixelIntensity(image,post-GetPixelChannels(image))+
3342 GetPixelIntensity(image,post)+GetPixelIntensity(image,post+
3343 GetPixelChannels(image))-GetPixelIntensity(image,pre-
3344 GetPixelChannels(image))-GetPixelIntensity(image,pre)-
3345 GetPixelIntensity(image,pre+GetPixelChannels(image)));
3346 if ((normal.x == 0.0) && (normal.y == 0.0))
3351 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3352 if (distance > MagickEpsilon)
3355 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
3356 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3357 shade=distance/sqrt((double) normal_distance);
3360 if (GetPixelMask(image,p) != 0)
3362 pre+=GetPixelChannels(image);
3363 center+=GetPixelChannels(image);
3364 post+=GetPixelChannels(image);
3365 q+=GetPixelChannels(shade_image);
3368 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3377 channel=GetPixelChannelMapChannel(image,i);
3378 traits=GetPixelChannelMapTraits(image,channel);
3379 shade_traits=GetPixelChannelMapTraits(shade_image,channel);
3380 if ((traits == UndefinedPixelTrait) ||
3381 (shade_traits == UndefinedPixelTrait))
3383 if ((shade_traits & CopyPixelTrait) != 0)
3385 SetPixelChannel(shade_image,channel,center[i],q);
3388 if (gray != MagickFalse)
3390 SetPixelChannel(shade_image,channel,ClampToQuantum(shade),q);
3393 SetPixelChannel(shade_image,channel,ClampToQuantum(QuantumScale*shade*
3396 pre+=GetPixelChannels(image);
3397 center+=GetPixelChannels(image);
3398 post+=GetPixelChannels(image);
3399 q+=GetPixelChannels(shade_image);
3401 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3403 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3408 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3409 #pragma omp critical (MagickCore_ShadeImage)
3411 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3412 if (proceed == MagickFalse)
3416 shade_view=DestroyCacheView(shade_view);
3417 image_view=DestroyCacheView(image_view);
3418 if (status == MagickFalse)
3419 shade_image=DestroyImage(shade_image);
3420 return(shade_image);
3424 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3428 % S h a r p e n I m a g e %
3432 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3434 % SharpenImage() sharpens the image. We convolve the image with a Gaussian
3435 % operator of the given radius and standard deviation (sigma). For
3436 % reasonable results, radius should be larger than sigma. Use a radius of 0
3437 % and SharpenImage() selects a suitable radius for you.
3439 % Using a separable kernel would be faster, but the negative weights cancel
3440 % out on the corners of the kernel producing often undesirable ringing in the
3441 % filtered result; this can be avoided by using a 2D gaussian shaped image
3442 % sharpening kernel instead.
3444 % The format of the SharpenImage method is:
3446 % Image *SharpenImage(const Image *image,const double radius,
3447 % const double sigma,ExceptionInfo *exception)
3449 % A description of each parameter follows:
3451 % o image: the image.
3453 % o radius: the radius of the Gaussian, in pixels, not counting the center
3456 % o sigma: the standard deviation of the Laplacian, in pixels.
3458 % o exception: return any errors or warnings in this structure.
3461 MagickExport Image *SharpenImage(const Image *image,const double radius,
3462 const double sigma,ExceptionInfo *exception)
3484 assert(image != (const Image *) NULL);
3485 assert(image->signature == MagickSignature);
3486 if (image->debug != MagickFalse)
3487 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3488 assert(exception != (ExceptionInfo *) NULL);
3489 assert(exception->signature == MagickSignature);
3490 width=GetOptimalKernelWidth2D(radius,sigma);
3491 kernel_info=AcquireKernelInfo((const char *) NULL);
3492 if (kernel_info == (KernelInfo *) NULL)
3493 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3494 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3495 kernel_info->width=width;
3496 kernel_info->height=width;
3497 kernel_info->signature=MagickSignature;
3498 kernel_info->values=(double *) AcquireAlignedMemory(
3499 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
3500 if (kernel_info->values == (double *) NULL)
3502 kernel_info=DestroyKernelInfo(kernel_info);
3503 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3506 j=(ssize_t) kernel_info->width/2;
3508 for (v=(-j); v <= j; v++)
3510 for (u=(-j); u <= j; u++)
3512 kernel_info->values[i]=(double) (-exp(-((double) u*u+v*v)/(2.0*
3513 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3514 normalize+=kernel_info->values[i];
3518 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
3519 sharp_image=ConvolveImage(image,kernel_info,exception);
3520 kernel_info=DestroyKernelInfo(kernel_info);
3521 return(sharp_image);
3525 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3529 % S p r e a d I m a g e %
3533 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3535 % SpreadImage() is a special effects method that randomly displaces each
3536 % pixel in a block defined by the radius parameter.
3538 % The format of the SpreadImage method is:
3540 % Image *SpreadImage(const Image *image,const double radius,
3541 % const PixelInterpolateMethod method,ExceptionInfo *exception)
3543 % A description of each parameter follows:
3545 % o image: the image.
3547 % o radius: choose a random pixel in a neighborhood of this extent.
3549 % o method: the pixel interpolation method.
3551 % o exception: return any errors or warnings in this structure.
3554 MagickExport Image *SpreadImage(const Image *image,const double radius,
3555 const PixelInterpolateMethod method,ExceptionInfo *exception)
3557 #define SpreadImageTag "Spread/Image"
3573 **restrict random_info;
3585 Initialize spread image attributes.
3587 assert(image != (Image *) NULL);
3588 assert(image->signature == MagickSignature);
3589 if (image->debug != MagickFalse)
3590 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3591 assert(exception != (ExceptionInfo *) NULL);
3592 assert(exception->signature == MagickSignature);
3593 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3595 if (spread_image == (Image *) NULL)
3596 return((Image *) NULL);
3597 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
3599 spread_image=DestroyImage(spread_image);
3600 return((Image *) NULL);
3607 width=GetOptimalKernelWidth1D(radius,0.5);
3608 random_info=AcquireRandomInfoThreadSet();
3609 key=GetRandomSecretKey(random_info[0]);
3610 image_view=AcquireVirtualCacheView(image,exception);
3611 spread_view=AcquireAuthenticCacheView(spread_image,exception);
3612 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3613 #pragma omp parallel for schedule(static,8) shared(progress,status) \
3614 dynamic_number_threads(image,image->columns,image->rows,key == ~0UL)
3616 for (y=0; y < (ssize_t) image->rows; y++)
3619 id = GetOpenMPThreadId();
3621 register const Quantum
3630 if (status == MagickFalse)
3632 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3633 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
3635 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3640 for (x=0; x < (ssize_t) image->columns; x++)
3645 point.x=GetPseudoRandomValue(random_info[id]);
3646 point.y=GetPseudoRandomValue(random_info[id]);
3647 status=InterpolatePixelChannels(image,image_view,spread_image,method,
3648 (double) x+width*point.x-0.5,(double) y+width*point.y-0.5,q,exception);
3649 q+=GetPixelChannels(spread_image);
3651 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
3653 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3658 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3659 #pragma omp critical (MagickCore_SpreadImage)
3661 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3662 if (proceed == MagickFalse)
3666 spread_view=DestroyCacheView(spread_view);
3667 image_view=DestroyCacheView(image_view);
3668 random_info=DestroyRandomInfoThreadSet(random_info);
3669 return(spread_image);
3673 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3677 % U n s h a r p M a s k I m a g e %
3681 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3683 % UnsharpMaskImage() sharpens one or more image channels. We convolve the
3684 % image with a Gaussian operator of the given radius and standard deviation
3685 % (sigma). For reasonable results, radius should be larger than sigma. Use a
3686 % radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
3688 % The format of the UnsharpMaskImage method is:
3690 % Image *UnsharpMaskImage(const Image *image,const double radius,
3691 % const double sigma,const double amount,const double threshold,
3692 % ExceptionInfo *exception)
3694 % A description of each parameter follows:
3696 % o image: the image.
3698 % o radius: the radius of the Gaussian, in pixels, not counting the center
3701 % o sigma: the standard deviation of the Gaussian, in pixels.
3703 % o amount: the percentage of the difference between the original and the
3704 % blur image that is added back into the original.
3706 % o threshold: the threshold in pixels needed to apply the diffence amount.
3708 % o exception: return any errors or warnings in this structure.
3711 MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
3712 const double sigma,const double amount,const double threshold,
3713 ExceptionInfo *exception)
3715 #define SharpenImageTag "Sharpen/Image"
3736 assert(image != (const Image *) NULL);
3737 assert(image->signature == MagickSignature);
3738 if (image->debug != MagickFalse)
3739 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3740 assert(exception != (ExceptionInfo *) NULL);
3741 unsharp_image=BlurImage(image,radius,sigma,exception);
3742 if (unsharp_image == (Image *) NULL)
3743 return((Image *) NULL);
3744 quantum_threshold=(MagickRealType) QuantumRange*threshold;
3750 image_view=AcquireVirtualCacheView(image,exception);
3751 unsharp_view=AcquireAuthenticCacheView(unsharp_image,exception);
3752 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3753 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3754 dynamic_number_threads(image,image->columns,image->rows,1)
3756 for (y=0; y < (ssize_t) image->rows; y++)
3758 register const Quantum
3767 if (status == MagickFalse)
3769 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3770 q=QueueCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
3772 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3777 for (x=0; x < (ssize_t) image->columns; x++)
3782 if (GetPixelMask(image,p) != 0)
3784 p+=GetPixelChannels(image);
3785 q+=GetPixelChannels(unsharp_image);
3788 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3800 channel=GetPixelChannelMapChannel(image,i);
3801 traits=GetPixelChannelMapTraits(image,channel);
3802 unsharp_traits=GetPixelChannelMapTraits(unsharp_image,channel);
3803 if ((traits == UndefinedPixelTrait) ||
3804 (unsharp_traits == UndefinedPixelTrait))
3806 if ((unsharp_traits & CopyPixelTrait) != 0)
3808 SetPixelChannel(unsharp_image,channel,p[i],q);
3811 pixel=p[i]-(MagickRealType) GetPixelChannel(unsharp_image,channel,q);
3812 if (fabs(2.0*pixel) < quantum_threshold)
3813 pixel=(MagickRealType) p[i];
3815 pixel=(MagickRealType) p[i]+amount*pixel;
3816 SetPixelChannel(unsharp_image,channel,ClampToQuantum(pixel),q);
3818 p+=GetPixelChannels(image);
3819 q+=GetPixelChannels(unsharp_image);
3821 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
3823 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3828 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3829 #pragma omp critical (MagickCore_UnsharpMaskImage)
3831 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
3832 if (proceed == MagickFalse)
3836 unsharp_image->type=image->type;
3837 unsharp_view=DestroyCacheView(unsharp_view);
3838 image_view=DestroyCacheView(image_view);
3839 if (status == MagickFalse)
3840 unsharp_image=DestroyImage(unsharp_image);
3841 return(unsharp_image);