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 ? MagickEpsilon : 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 ? MagickEpsilon : 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 ? MagickEpsilon : 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 ? MagickEpsilon : 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 ? MagickEpsilon : 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 ? MagickEpsilon : 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 ? MagickEpsilon : 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 ? MagickEpsilon : 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 ? MagickEpsilon : 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];
2177 Open output image file.
2179 assert(image != (Image *) NULL);
2180 assert(image->signature == MagickSignature);
2181 if (image->debug != MagickFalse)
2182 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2186 preview_info=AcquireImageInfo();
2187 SetGeometry(image,&geometry);
2188 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2189 &geometry.width,&geometry.height);
2190 images=NewImageList();
2192 GetQuantizeInfo(&quantize_info);
2198 for (i=0; i < NumberTiles; i++)
2200 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2201 if (thumbnail == (Image *) NULL)
2203 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2205 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel,exception);
2206 if (i == (NumberTiles/2))
2208 (void) QueryColorCompliance("#dfdfdf",AllCompliance,
2209 &thumbnail->matte_color,exception);
2210 AppendImageToList(&images,thumbnail);
2218 preview_image=RotateImage(thumbnail,degrees,exception);
2219 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
2225 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
2226 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",
2227 degrees,2.0*degrees);
2232 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2233 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
2234 preview_image=RollImage(thumbnail,x,y,exception);
2235 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
2236 (double) x,(double) y);
2241 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2242 if (preview_image == (Image *) NULL)
2244 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",
2246 (void) ModulateImage(preview_image,factor,exception);
2247 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2250 case SaturationPreview:
2252 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2253 if (preview_image == (Image *) NULL)
2255 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",
2257 (void) ModulateImage(preview_image,factor,exception);
2258 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2261 case BrightnessPreview:
2263 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2264 if (preview_image == (Image *) NULL)
2266 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
2267 (void) ModulateImage(preview_image,factor,exception);
2268 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2274 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2275 if (preview_image == (Image *) NULL)
2278 (void) GammaImage(preview_image,gamma,exception);
2279 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
2284 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2285 if (preview_image != (Image *) NULL)
2286 for (x=0; x < i; x++)
2287 (void) ContrastImage(preview_image,MagickTrue,exception);
2288 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
2294 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2295 if (preview_image == (Image *) NULL)
2297 for (x=0; x < i; x++)
2298 (void) ContrastImage(preview_image,MagickFalse,exception);
2299 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
2303 case GrayscalePreview:
2305 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2306 if (preview_image == (Image *) NULL)
2309 quantize_info.number_colors=colors;
2310 quantize_info.colorspace=GRAYColorspace;
2311 (void) QuantizeImage(&quantize_info,preview_image,exception);
2312 (void) FormatLocaleString(label,MaxTextExtent,
2313 "-colorspace gray -colors %.20g",(double) colors);
2316 case QuantizePreview:
2318 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2319 if (preview_image == (Image *) NULL)
2322 quantize_info.number_colors=colors;
2323 (void) QuantizeImage(&quantize_info,preview_image,exception);
2324 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
2328 case DespecklePreview:
2330 for (x=0; x < (i-1); x++)
2332 preview_image=DespeckleImage(thumbnail,exception);
2333 if (preview_image == (Image *) NULL)
2335 thumbnail=DestroyImage(thumbnail);
2336 thumbnail=preview_image;
2338 preview_image=DespeckleImage(thumbnail,exception);
2339 if (preview_image == (Image *) NULL)
2341 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
2345 case ReduceNoisePreview:
2347 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2348 (size_t) radius,exception);
2349 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
2352 case AddNoisePreview:
2358 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2363 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2368 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2373 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2378 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2383 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2388 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2392 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2393 (size_t) i,exception);
2394 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
2397 case SharpenPreview:
2399 preview_image=SharpenImage(thumbnail,radius,sigma,exception);
2400 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",
2406 preview_image=BlurImage(thumbnail,radius,sigma,exception);
2407 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
2411 case ThresholdPreview:
2413 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2414 if (preview_image == (Image *) NULL)
2416 (void) BilevelImage(thumbnail,(double) (percentage*((MagickRealType)
2417 QuantumRange+1.0))/100.0,exception);
2418 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",
2419 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
2422 case EdgeDetectPreview:
2424 preview_image=EdgeImage(thumbnail,radius,sigma,exception);
2425 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
2430 preview_image=SpreadImage(thumbnail,radius,thumbnail->interpolate,
2432 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",
2436 case SolarizePreview:
2438 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2439 if (preview_image == (Image *) NULL)
2441 (void) SolarizeImage(preview_image,(double) QuantumRange*
2442 percentage/100.0,exception);
2443 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
2444 (QuantumRange*percentage)/100.0);
2450 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2452 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",
2458 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2459 if (preview_image == (Image *) NULL)
2461 geometry.width=(size_t) (2*i+2);
2462 geometry.height=(size_t) (2*i+2);
2465 (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
2466 (void) FormatLocaleString(label,MaxTextExtent,
2467 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
2468 geometry.height,(double) geometry.x,(double) geometry.y);
2471 case SegmentPreview:
2473 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2474 if (preview_image == (Image *) NULL)
2477 (void) SegmentImage(preview_image,sRGBColorspace,MagickFalse,threshold,
2478 threshold,exception);
2479 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
2480 threshold,threshold);
2485 preview_image=SwirlImage(thumbnail,degrees,image->interpolate,
2487 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
2491 case ImplodePreview:
2494 preview_image=ImplodeImage(thumbnail,degrees,image->interpolate,
2496 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
2502 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,
2503 image->interpolate,exception);
2504 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",
2505 0.5*degrees,2.0*degrees);
2508 case OilPaintPreview:
2510 preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2512 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2516 case CharcoalDrawingPreview:
2518 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
2520 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2527 filename[MaxTextExtent];
2535 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2536 if (preview_image == (Image *) NULL)
2538 preview_info->quality=(size_t) percentage;
2539 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
2540 preview_info->quality);
2541 file=AcquireUniqueFileResource(filename);
2544 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
2545 "jpeg:%s",filename);
2546 status=WriteImage(preview_info,preview_image,exception);
2547 if (status != MagickFalse)
2552 (void) CopyMagickString(preview_info->filename,
2553 preview_image->filename,MaxTextExtent);
2554 quality_image=ReadImage(preview_info,exception);
2555 if (quality_image != (Image *) NULL)
2557 preview_image=DestroyImage(preview_image);
2558 preview_image=quality_image;
2561 (void) RelinquishUniqueFileResource(preview_image->filename);
2562 if ((GetBlobSize(preview_image)/1024) >= 1024)
2563 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
2564 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2567 if (GetBlobSize(preview_image) >= 1024)
2568 (void) FormatLocaleString(label,MaxTextExtent,
2569 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
2570 GetBlobSize(preview_image))/1024.0);
2572 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
2573 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
2577 thumbnail=DestroyImage(thumbnail);
2581 if (preview_image == (Image *) NULL)
2583 (void) DeleteImageProperty(preview_image,"label");
2584 (void) SetImageProperty(preview_image,"label",label,exception);
2585 AppendImageToList(&images,preview_image);
2586 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2588 if (proceed == MagickFalse)
2591 if (images == (Image *) NULL)
2593 preview_info=DestroyImageInfo(preview_info);
2594 return((Image *) NULL);
2599 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2600 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2601 montage_info->shadow=MagickTrue;
2602 (void) CloneString(&montage_info->tile,"3x3");
2603 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2604 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2605 montage_image=MontageImages(images,montage_info,exception);
2606 montage_info=DestroyMontageInfo(montage_info);
2607 images=DestroyImageList(images);
2608 if (montage_image == (Image *) NULL)
2609 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2610 if (montage_image->montage != (char *) NULL)
2613 Free image directory.
2615 montage_image->montage=(char *) RelinquishMagickMemory(
2616 montage_image->montage);
2617 if (image->directory != (char *) NULL)
2618 montage_image->directory=(char *) RelinquishMagickMemory(
2619 montage_image->directory);
2621 preview_info=DestroyImageInfo(preview_info);
2622 return(montage_image);
2626 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2630 % R a d i a l B l u r I m a g e %
2634 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2636 % RadialBlurImage() applies a radial blur to the image.
2638 % Andrew Protano contributed this effect.
2640 % The format of the RadialBlurImage method is:
2642 % Image *RadialBlurImage(const Image *image,const double angle,
2643 % ExceptionInfo *exception)
2645 % A description of each parameter follows:
2647 % o image: the image.
2649 % o angle: the angle of the radial blur.
2653 % o exception: return any errors or warnings in this structure.
2656 MagickExport Image *RadialBlurImage(const Image *image,const double angle,
2657 ExceptionInfo *exception)
2693 Allocate blur image.
2695 assert(image != (Image *) NULL);
2696 assert(image->signature == MagickSignature);
2697 if (image->debug != MagickFalse)
2698 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2699 assert(exception != (ExceptionInfo *) NULL);
2700 assert(exception->signature == MagickSignature);
2701 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2702 if (blur_image == (Image *) NULL)
2703 return((Image *) NULL);
2704 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2706 blur_image=DestroyImage(blur_image);
2707 return((Image *) NULL);
2709 blur_center.x=(double) image->columns/2.0;
2710 blur_center.y=(double) image->rows/2.0;
2711 blur_radius=hypot(blur_center.x,blur_center.y);
2712 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
2713 theta=DegreesToRadians(angle)/(MagickRealType) (n-1);
2714 cos_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2715 sizeof(*cos_theta));
2716 sin_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2717 sizeof(*sin_theta));
2718 if ((cos_theta == (MagickRealType *) NULL) ||
2719 (sin_theta == (MagickRealType *) NULL))
2721 blur_image=DestroyImage(blur_image);
2722 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2724 offset=theta*(MagickRealType) (n-1)/2.0;
2725 for (i=0; i < (ssize_t) n; i++)
2727 cos_theta[i]=cos((double) (theta*i-offset));
2728 sin_theta[i]=sin((double) (theta*i-offset));
2735 image_view=AcquireVirtualCacheView(image,exception);
2736 radial_view=AcquireVirtualCacheView(image,exception);
2737 blur_view=AcquireAuthenticCacheView(blur_image,exception);
2738 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2739 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2740 dynamic_number_threads(image,image->columns,image->rows,1)
2742 for (y=0; y < (ssize_t) image->rows; y++)
2744 register const Quantum
2753 if (status == MagickFalse)
2755 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2756 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2758 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2763 for (x=0; x < (ssize_t) image->columns; x++)
2777 center.x=(double) x-blur_center.x;
2778 center.y=(double) y-blur_center.y;
2779 radius=hypot((double) center.x,center.y);
2784 step=(size_t) (blur_radius/radius);
2791 if (GetPixelMask(image,p) != 0)
2793 p+=GetPixelChannels(image);
2794 q+=GetPixelChannels(blur_image);
2797 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2810 register const Quantum
2816 channel=GetPixelChannelMapChannel(image,i);
2817 traits=GetPixelChannelMapTraits(image,channel);
2818 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
2819 if ((traits == UndefinedPixelTrait) ||
2820 (blur_traits == UndefinedPixelTrait))
2822 if ((blur_traits & CopyPixelTrait) != 0)
2824 SetPixelChannel(blur_image,channel,p[i],q);
2829 if ((blur_traits & BlendPixelTrait) == 0)
2831 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2833 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2834 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2835 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2837 if (r == (const Quantum *) NULL)
2845 gamma=1.0/(fabs((double) gamma) < MagickEpsilon ? MagickEpsilon : gamma);
2846 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2849 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2851 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2852 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2853 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2855 if (r == (const Quantum *) NULL)
2860 pixel+=GetPixelAlpha(image,r)*r[i];
2861 gamma+=GetPixelAlpha(image,r);
2863 gamma=1.0/(fabs((double) gamma) < MagickEpsilon ? MagickEpsilon : gamma);
2864 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2866 p+=GetPixelChannels(image);
2867 q+=GetPixelChannels(blur_image);
2869 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2871 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2876 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2877 #pragma omp critical (MagickCore_RadialBlurImage)
2879 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2880 if (proceed == MagickFalse)
2884 blur_view=DestroyCacheView(blur_view);
2885 radial_view=DestroyCacheView(radial_view);
2886 image_view=DestroyCacheView(image_view);
2887 cos_theta=(MagickRealType *) RelinquishMagickMemory(cos_theta);
2888 sin_theta=(MagickRealType *) RelinquishMagickMemory(sin_theta);
2889 if (status == MagickFalse)
2890 blur_image=DestroyImage(blur_image);
2895 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2899 % S e l e c t i v e B l u r I m a g e %
2903 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2905 % SelectiveBlurImage() selectively blur pixels within a contrast threshold.
2906 % It is similar to the unsharpen mask that sharpens everything with contrast
2907 % above a certain threshold.
2909 % The format of the SelectiveBlurImage method is:
2911 % Image *SelectiveBlurImage(const Image *image,const double radius,
2912 % const double sigma,const double threshold,ExceptionInfo *exception)
2914 % A description of each parameter follows:
2916 % o image: the image.
2918 % o radius: the radius of the Gaussian, in pixels, not counting the center
2921 % o sigma: the standard deviation of the Gaussian, in pixels.
2923 % o threshold: only pixels within this contrast threshold are included
2924 % in the blur operation.
2926 % o exception: return any errors or warnings in this structure.
2929 MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
2930 const double sigma,const double threshold,ExceptionInfo *exception)
2932 #define SelectiveBlurImageTag "SelectiveBlur/Image"
2964 Initialize blur image attributes.
2966 assert(image != (Image *) NULL);
2967 assert(image->signature == MagickSignature);
2968 if (image->debug != MagickFalse)
2969 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2970 assert(exception != (ExceptionInfo *) NULL);
2971 assert(exception->signature == MagickSignature);
2972 width=GetOptimalKernelWidth1D(radius,sigma);
2973 kernel=(double *) AcquireAlignedMemory((size_t) width,width*sizeof(*kernel));
2974 if (kernel == (double *) NULL)
2975 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2976 j=(ssize_t) width/2;
2978 for (v=(-j); v <= j; v++)
2980 for (u=(-j); u <= j; u++)
2981 kernel[i++]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
2982 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
2984 if (image->debug != MagickFalse)
2987 format[MaxTextExtent],
2990 register const double
2997 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
2998 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
3000 message=AcquireString("");
3002 for (v=0; v < (ssize_t) width; v++)
3005 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
3006 (void) ConcatenateString(&message,format);
3007 for (u=0; u < (ssize_t) width; u++)
3009 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",*k++);
3010 (void) ConcatenateString(&message,format);
3012 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
3014 message=DestroyString(message);
3016 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3017 if (blur_image == (Image *) NULL)
3018 return((Image *) NULL);
3019 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
3021 blur_image=DestroyImage(blur_image);
3022 return((Image *) NULL);
3025 Threshold blur image.
3029 center=(ssize_t) (GetPixelChannels(image)*(image->columns+width)*(width/2L)+
3030 GetPixelChannels(image)*(width/2L));
3031 image_view=AcquireVirtualCacheView(image,exception);
3032 blur_view=AcquireAuthenticCacheView(blur_image,exception);
3033 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3034 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3035 dynamic_number_threads(image,image->columns,image->rows,1)
3037 for (y=0; y < (ssize_t) image->rows; y++)
3045 register const Quantum
3054 if (status == MagickFalse)
3056 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
3057 (width/2L),image->columns+width,width,exception);
3058 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3060 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3065 for (x=0; x < (ssize_t) image->columns; x++)
3070 if (GetPixelMask(image,p) != 0)
3072 p+=GetPixelChannels(image);
3073 q+=GetPixelChannels(blur_image);
3076 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3091 register const double
3094 register const Quantum
3103 channel=GetPixelChannelMapChannel(image,i);
3104 traits=GetPixelChannelMapTraits(image,channel);
3105 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
3106 if ((traits == UndefinedPixelTrait) ||
3107 (blur_traits == UndefinedPixelTrait))
3109 if ((blur_traits & CopyPixelTrait) != 0)
3111 SetPixelChannel(blur_image,channel,p[center+i],q);
3117 intensity=(MagickRealType) GetPixelIntensity(image,p+center);
3119 if ((blur_traits & BlendPixelTrait) == 0)
3121 for (v=0; v < (ssize_t) width; v++)
3123 for (u=0; u < (ssize_t) width; u++)
3125 contrast=GetPixelIntensity(image,pixels)-intensity;
3126 if (fabs(contrast) < threshold)
3128 pixel+=(*k)*pixels[i];
3132 pixels+=GetPixelChannels(image);
3134 pixels+=image->columns*GetPixelChannels(image);
3136 if (fabs((double) gamma) < MagickEpsilon)
3138 SetPixelChannel(blur_image,channel,p[center+i],q);
3141 gamma=1.0/(fabs((double) gamma) < MagickEpsilon ? MagickEpsilon : gamma);
3142 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3145 for (v=0; v < (ssize_t) width; v++)
3147 for (u=0; u < (ssize_t) width; u++)
3149 contrast=GetPixelIntensity(image,pixels)-intensity;
3150 if (fabs(contrast) < threshold)
3152 alpha=(MagickRealType) (QuantumScale*
3153 GetPixelAlpha(image,pixels));
3154 pixel+=(*k)*alpha*pixels[i];
3158 pixels+=GetPixelChannels(image);
3160 pixels+=image->columns*GetPixelChannels(image);
3162 if (fabs((double) gamma) < MagickEpsilon)
3164 SetPixelChannel(blur_image,channel,p[center+i],q);
3167 gamma=1.0/(fabs((double) gamma) < MagickEpsilon ? MagickEpsilon : gamma);
3168 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3170 p+=GetPixelChannels(image);
3171 q+=GetPixelChannels(blur_image);
3173 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3174 if (sync == MagickFalse)
3176 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3181 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3182 #pragma omp critical (MagickCore_SelectiveBlurImage)
3184 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3186 if (proceed == MagickFalse)
3190 blur_image->type=image->type;
3191 blur_view=DestroyCacheView(blur_view);
3192 image_view=DestroyCacheView(image_view);
3193 kernel=(double *) RelinquishAlignedMemory(kernel);
3194 if (status == MagickFalse)
3195 blur_image=DestroyImage(blur_image);
3200 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3204 % S h a d e I m a g e %
3208 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3210 % ShadeImage() shines a distant light on an image to create a
3211 % three-dimensional effect. You control the positioning of the light with
3212 % azimuth and elevation; azimuth is measured in degrees off the x axis
3213 % and elevation is measured in pixels above the Z axis.
3215 % The format of the ShadeImage method is:
3217 % Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3218 % const double azimuth,const double elevation,ExceptionInfo *exception)
3220 % A description of each parameter follows:
3222 % o image: the image.
3224 % o gray: A value other than zero shades the intensity of each pixel.
3226 % o azimuth, elevation: Define the light source direction.
3228 % o exception: return any errors or warnings in this structure.
3231 MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3232 const double azimuth,const double elevation,ExceptionInfo *exception)
3234 #define ShadeImageTag "Shade/Image"
3256 Initialize shaded image attributes.
3258 assert(image != (const Image *) NULL);
3259 assert(image->signature == MagickSignature);
3260 if (image->debug != MagickFalse)
3261 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3262 assert(exception != (ExceptionInfo *) NULL);
3263 assert(exception->signature == MagickSignature);
3264 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3265 if (shade_image == (Image *) NULL)
3266 return((Image *) NULL);
3267 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
3269 shade_image=DestroyImage(shade_image);
3270 return((Image *) NULL);
3273 Compute the light vector.
3275 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3276 cos(DegreesToRadians(elevation));
3277 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3278 cos(DegreesToRadians(elevation));
3279 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3285 image_view=AcquireVirtualCacheView(image,exception);
3286 shade_view=AcquireAuthenticCacheView(shade_image,exception);
3287 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3288 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3289 dynamic_number_threads(image,image->columns,image->rows,1)
3291 for (y=0; y < (ssize_t) image->rows; y++)
3301 register const Quantum
3313 if (status == MagickFalse)
3315 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
3316 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3318 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3324 Shade this row of pixels.
3326 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
3327 pre=p+GetPixelChannels(image);
3328 center=pre+(image->columns+2)*GetPixelChannels(image);
3329 post=center+(image->columns+2)*GetPixelChannels(image);
3330 for (x=0; x < (ssize_t) image->columns; x++)
3336 Determine the surface normal and compute shading.
3338 normal.x=(double) (GetPixelIntensity(image,pre-GetPixelChannels(image))+
3339 GetPixelIntensity(image,center-GetPixelChannels(image))+
3340 GetPixelIntensity(image,post-GetPixelChannels(image))-
3341 GetPixelIntensity(image,pre+GetPixelChannels(image))-
3342 GetPixelIntensity(image,center+GetPixelChannels(image))-
3343 GetPixelIntensity(image,post+GetPixelChannels(image)));
3344 normal.y=(double) (GetPixelIntensity(image,post-GetPixelChannels(image))+
3345 GetPixelIntensity(image,post)+GetPixelIntensity(image,post+
3346 GetPixelChannels(image))-GetPixelIntensity(image,pre-
3347 GetPixelChannels(image))-GetPixelIntensity(image,pre)-
3348 GetPixelIntensity(image,pre+GetPixelChannels(image)));
3349 if ((normal.x == 0.0) && (normal.y == 0.0))
3354 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3355 if (distance > MagickEpsilon)
3358 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
3359 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3360 shade=distance/sqrt((double) normal_distance);
3363 if (GetPixelMask(image,p) != 0)
3365 pre+=GetPixelChannels(image);
3366 center+=GetPixelChannels(image);
3367 post+=GetPixelChannels(image);
3368 q+=GetPixelChannels(shade_image);
3371 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3380 channel=GetPixelChannelMapChannel(image,i);
3381 traits=GetPixelChannelMapTraits(image,channel);
3382 shade_traits=GetPixelChannelMapTraits(shade_image,channel);
3383 if ((traits == UndefinedPixelTrait) ||
3384 (shade_traits == UndefinedPixelTrait))
3386 if ((shade_traits & CopyPixelTrait) != 0)
3388 SetPixelChannel(shade_image,channel,center[i],q);
3391 if (gray != MagickFalse)
3393 SetPixelChannel(shade_image,channel,ClampToQuantum(shade),q);
3396 SetPixelChannel(shade_image,channel,ClampToQuantum(QuantumScale*shade*
3399 pre+=GetPixelChannels(image);
3400 center+=GetPixelChannels(image);
3401 post+=GetPixelChannels(image);
3402 q+=GetPixelChannels(shade_image);
3404 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3406 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3411 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3412 #pragma omp critical (MagickCore_ShadeImage)
3414 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3415 if (proceed == MagickFalse)
3419 shade_view=DestroyCacheView(shade_view);
3420 image_view=DestroyCacheView(image_view);
3421 if (status == MagickFalse)
3422 shade_image=DestroyImage(shade_image);
3423 return(shade_image);
3427 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3431 % S h a r p e n I m a g e %
3435 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3437 % SharpenImage() sharpens the image. We convolve the image with a Gaussian
3438 % operator of the given radius and standard deviation (sigma). For
3439 % reasonable results, radius should be larger than sigma. Use a radius of 0
3440 % and SharpenImage() selects a suitable radius for you.
3442 % Using a separable kernel would be faster, but the negative weights cancel
3443 % out on the corners of the kernel producing often undesirable ringing in the
3444 % filtered result; this can be avoided by using a 2D gaussian shaped image
3445 % sharpening kernel instead.
3447 % The format of the SharpenImage method is:
3449 % Image *SharpenImage(const Image *image,const double radius,
3450 % const double sigma,ExceptionInfo *exception)
3452 % A description of each parameter follows:
3454 % o image: the image.
3456 % o radius: the radius of the Gaussian, in pixels, not counting the center
3459 % o sigma: the standard deviation of the Laplacian, in pixels.
3461 % o exception: return any errors or warnings in this structure.
3464 MagickExport Image *SharpenImage(const Image *image,const double radius,
3465 const double sigma,ExceptionInfo *exception)
3487 assert(image != (const Image *) NULL);
3488 assert(image->signature == MagickSignature);
3489 if (image->debug != MagickFalse)
3490 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3491 assert(exception != (ExceptionInfo *) NULL);
3492 assert(exception->signature == MagickSignature);
3493 width=GetOptimalKernelWidth2D(radius,sigma);
3494 kernel_info=AcquireKernelInfo((const char *) NULL);
3495 if (kernel_info == (KernelInfo *) NULL)
3496 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3497 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3498 kernel_info->width=width;
3499 kernel_info->height=width;
3500 kernel_info->signature=MagickSignature;
3501 kernel_info->values=(double *) AcquireAlignedMemory(
3502 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
3503 if (kernel_info->values == (double *) NULL)
3505 kernel_info=DestroyKernelInfo(kernel_info);
3506 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3509 j=(ssize_t) kernel_info->width/2;
3511 for (v=(-j); v <= j; v++)
3513 for (u=(-j); u <= j; u++)
3515 kernel_info->values[i]=(double) (-exp(-((double) u*u+v*v)/(2.0*
3516 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3517 normalize+=kernel_info->values[i];
3521 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
3522 sharp_image=ConvolveImage(image,kernel_info,exception);
3523 kernel_info=DestroyKernelInfo(kernel_info);
3524 return(sharp_image);
3528 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3532 % S p r e a d I m a g e %
3536 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3538 % SpreadImage() is a special effects method that randomly displaces each
3539 % pixel in a block defined by the radius parameter.
3541 % The format of the SpreadImage method is:
3543 % Image *SpreadImage(const Image *image,const double radius,
3544 % const PixelInterpolateMethod method,ExceptionInfo *exception)
3546 % A description of each parameter follows:
3548 % o image: the image.
3550 % o radius: choose a random pixel in a neighborhood of this extent.
3552 % o method: the pixel interpolation method.
3554 % o exception: return any errors or warnings in this structure.
3557 MagickExport Image *SpreadImage(const Image *image,const double radius,
3558 const PixelInterpolateMethod method,ExceptionInfo *exception)
3560 #define SpreadImageTag "Spread/Image"
3576 **restrict random_info;
3588 Initialize spread image attributes.
3590 assert(image != (Image *) NULL);
3591 assert(image->signature == MagickSignature);
3592 if (image->debug != MagickFalse)
3593 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3594 assert(exception != (ExceptionInfo *) NULL);
3595 assert(exception->signature == MagickSignature);
3596 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3598 if (spread_image == (Image *) NULL)
3599 return((Image *) NULL);
3600 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
3602 spread_image=DestroyImage(spread_image);
3603 return((Image *) NULL);
3610 width=GetOptimalKernelWidth1D(radius,0.5);
3611 random_info=AcquireRandomInfoThreadSet();
3612 key=GetRandomSecretKey(random_info[0]);
3613 image_view=AcquireVirtualCacheView(image,exception);
3614 spread_view=AcquireAuthenticCacheView(spread_image,exception);
3615 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3616 #pragma omp parallel for schedule(static,8) shared(progress,status) \
3617 dynamic_number_threads(image,image->columns,image->rows,key == ~0UL)
3619 for (y=0; y < (ssize_t) image->rows; y++)
3622 id = GetOpenMPThreadId();
3624 register const Quantum
3633 if (status == MagickFalse)
3635 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3636 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
3638 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3643 for (x=0; x < (ssize_t) image->columns; x++)
3648 point.x=GetPseudoRandomValue(random_info[id]);
3649 point.y=GetPseudoRandomValue(random_info[id]);
3650 status=InterpolatePixelChannels(image,image_view,spread_image,method,
3651 (double) x+width*point.x-0.5,(double) y+width*point.y-0.5,q,exception);
3652 q+=GetPixelChannels(spread_image);
3654 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
3656 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3661 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3662 #pragma omp critical (MagickCore_SpreadImage)
3664 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3665 if (proceed == MagickFalse)
3669 spread_view=DestroyCacheView(spread_view);
3670 image_view=DestroyCacheView(image_view);
3671 random_info=DestroyRandomInfoThreadSet(random_info);
3672 return(spread_image);
3676 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3680 % U n s h a r p M a s k I m a g e %
3684 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3686 % UnsharpMaskImage() sharpens one or more image channels. We convolve the
3687 % image with a Gaussian operator of the given radius and standard deviation
3688 % (sigma). For reasonable results, radius should be larger than sigma. Use a
3689 % radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
3691 % The format of the UnsharpMaskImage method is:
3693 % Image *UnsharpMaskImage(const Image *image,const double radius,
3694 % const double sigma,const double amount,const double threshold,
3695 % ExceptionInfo *exception)
3697 % A description of each parameter follows:
3699 % o image: the image.
3701 % o radius: the radius of the Gaussian, in pixels, not counting the center
3704 % o sigma: the standard deviation of the Gaussian, in pixels.
3706 % o amount: the percentage of the difference between the original and the
3707 % blur image that is added back into the original.
3709 % o threshold: the threshold in pixels needed to apply the diffence amount.
3711 % o exception: return any errors or warnings in this structure.
3714 MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
3715 const double sigma,const double amount,const double threshold,
3716 ExceptionInfo *exception)
3718 #define SharpenImageTag "Sharpen/Image"
3739 assert(image != (const Image *) NULL);
3740 assert(image->signature == MagickSignature);
3741 if (image->debug != MagickFalse)
3742 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3743 assert(exception != (ExceptionInfo *) NULL);
3744 unsharp_image=BlurImage(image,radius,sigma,exception);
3745 if (unsharp_image == (Image *) NULL)
3746 return((Image *) NULL);
3747 quantum_threshold=(MagickRealType) QuantumRange*threshold;
3753 image_view=AcquireVirtualCacheView(image,exception);
3754 unsharp_view=AcquireAuthenticCacheView(unsharp_image,exception);
3755 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3756 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3757 dynamic_number_threads(image,image->columns,image->rows,1)
3759 for (y=0; y < (ssize_t) image->rows; y++)
3761 register const Quantum
3770 if (status == MagickFalse)
3772 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3773 q=QueueCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
3775 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3780 for (x=0; x < (ssize_t) image->columns; x++)
3785 if (GetPixelMask(image,p) != 0)
3787 p+=GetPixelChannels(image);
3788 q+=GetPixelChannels(unsharp_image);
3791 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3803 channel=GetPixelChannelMapChannel(image,i);
3804 traits=GetPixelChannelMapTraits(image,channel);
3805 unsharp_traits=GetPixelChannelMapTraits(unsharp_image,channel);
3806 if ((traits == UndefinedPixelTrait) ||
3807 (unsharp_traits == UndefinedPixelTrait))
3809 if ((unsharp_traits & CopyPixelTrait) != 0)
3811 SetPixelChannel(unsharp_image,channel,p[i],q);
3814 pixel=p[i]-(MagickRealType) GetPixelChannel(unsharp_image,channel,q);
3815 if (fabs(2.0*pixel) < quantum_threshold)
3816 pixel=(MagickRealType) p[i];
3818 pixel=(MagickRealType) p[i]+amount*pixel;
3819 SetPixelChannel(unsharp_image,channel,ClampToQuantum(pixel),q);
3821 p+=GetPixelChannels(image);
3822 q+=GetPixelChannels(unsharp_image);
3824 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
3826 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3831 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3832 #pragma omp critical (MagickCore_UnsharpMaskImage)
3834 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
3835 if (proceed == MagickFalse)
3839 unsharp_image->type=image->type;
3840 unsharp_view=DestroyCacheView(unsharp_view);
3841 image_view=DestroyCacheView(image_view);
3842 if (status == MagickFalse)
3843 unsharp_image=DestroyImage(unsharp_image);
3844 return(unsharp_image);