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)
299 for (y=0; y < (ssize_t) blur_image->rows; y++)
301 register const Quantum
310 if (status == MagickFalse)
312 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
313 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
315 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
320 for (x=0; x < (ssize_t) blur_image->columns; x++)
322 register const Quantum
332 j=(ssize_t) ceil((double) width*QuantumScale*
333 GetPixelIntensity(edge_image,r)-0.5);
337 if (j > (ssize_t) width)
341 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
342 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
343 if (p == (const Quantum *) NULL)
345 center=(ssize_t) GetPixelChannels(image)*(width-j)*((width-j)/2L)+
346 GetPixelChannels(image)*((width-j)/2L);
347 if (GetPixelMask(image,p) != 0)
349 q+=GetPixelChannels(blur_image);
350 r+=GetPixelChannels(edge_image);
353 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
367 register const double
370 register const Quantum
379 channel=GetPixelChannelMapChannel(image,i);
380 traits=GetPixelChannelMapTraits(image,channel);
381 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
382 if ((traits == UndefinedPixelTrait) ||
383 (blur_traits == UndefinedPixelTrait))
385 if ((blur_traits & CopyPixelTrait) != 0)
387 SetPixelChannel(blur_image,channel,p[center+i],q);
394 if ((blur_traits & BlendPixelTrait) == 0)
399 for (v=0; v < (ssize_t) (width-j); v++)
401 for (u=0; u < (ssize_t) (width-j); u++)
403 pixel+=(*k)*pixels[i];
406 pixels+=GetPixelChannels(image);
409 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
410 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
416 for (v=0; v < (ssize_t) (width-j); v++)
418 for (u=0; u < (ssize_t) (width-j); u++)
420 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
421 pixel+=(*k)*alpha*pixels[i];
424 pixels+=GetPixelChannels(image);
427 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
428 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
430 q+=GetPixelChannels(blur_image);
431 r+=GetPixelChannels(edge_image);
433 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
435 if (image->progress_monitor != (MagickProgressMonitor) NULL)
440 #if defined(MAGICKCORE_OPENMP_SUPPORT)
441 #pragma omp critical (MagickCore_AdaptiveBlurImage)
443 proceed=SetImageProgress(image,AdaptiveBlurImageTag,progress++,
445 if (proceed == MagickFalse)
449 blur_image->type=image->type;
450 blur_view=DestroyCacheView(blur_view);
451 edge_view=DestroyCacheView(edge_view);
452 image_view=DestroyCacheView(image_view);
453 edge_image=DestroyImage(edge_image);
454 for (i=0; i < (ssize_t) width; i+=2)
455 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
456 kernel=(double **) RelinquishAlignedMemory(kernel);
457 if (status == MagickFalse)
458 blur_image=DestroyImage(blur_image);
463 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
467 % A d a p t i v e S h a r p e n I m a g e %
471 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
473 % AdaptiveSharpenImage() adaptively sharpens the image by sharpening more
474 % intensely near image edges and less intensely far from edges. We sharpen the
475 % image with a Gaussian operator of the given radius and standard deviation
476 % (sigma). For reasonable results, radius should be larger than sigma. Use a
477 % radius of 0 and AdaptiveSharpenImage() selects a suitable radius for you.
479 % The format of the AdaptiveSharpenImage method is:
481 % Image *AdaptiveSharpenImage(const Image *image,const double radius,
482 % const double sigma,ExceptionInfo *exception)
484 % A description of each parameter follows:
486 % o image: the image.
488 % o radius: the radius of the Gaussian, in pixels, not counting the center
491 % o sigma: the standard deviation of the Laplacian, in pixels.
493 % o exception: return any errors or warnings in this structure.
496 MagickExport Image *AdaptiveSharpenImage(const Image *image,const double radius,
497 const double sigma,ExceptionInfo *exception)
499 #define AdaptiveSharpenImageTag "Convolve/Image"
500 #define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
535 assert(image != (const Image *) NULL);
536 assert(image->signature == MagickSignature);
537 if (image->debug != MagickFalse)
538 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
539 assert(exception != (ExceptionInfo *) NULL);
540 assert(exception->signature == MagickSignature);
541 sharp_image=CloneImage(image,0,0,MagickTrue,exception);
542 if (sharp_image == (Image *) NULL)
543 return((Image *) NULL);
544 if (fabs(sigma) <= MagickEpsilon)
546 if (SetImageStorageClass(sharp_image,DirectClass,exception) == MagickFalse)
548 sharp_image=DestroyImage(sharp_image);
549 return((Image *) NULL);
552 Edge detect the image brighness channel, level, sharp, and level again.
554 edge_image=EdgeImage(image,radius,sigma,exception);
555 if (edge_image == (Image *) NULL)
557 sharp_image=DestroyImage(sharp_image);
558 return((Image *) NULL);
560 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
561 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
562 if (gaussian_image != (Image *) NULL)
564 edge_image=DestroyImage(edge_image);
565 edge_image=gaussian_image;
567 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
569 Create a set of kernels from maximum (radius,sigma) to minimum.
571 width=GetOptimalKernelWidth2D(radius,sigma);
572 kernel=(double **) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
573 if (kernel == (double **) NULL)
575 edge_image=DestroyImage(edge_image);
576 sharp_image=DestroyImage(sharp_image);
577 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
579 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
580 for (i=0; i < (ssize_t) width; i+=2)
582 kernel[i]=(double *) AcquireAlignedMemory((size_t) (width-i),(width-i)*
584 if (kernel[i] == (double *) NULL)
587 j=(ssize_t) (width-i)/2;
589 for (v=(-j); v <= j; v++)
591 for (u=(-j); u <= j; u++)
593 kernel[i][k]=(double) (-exp(-((double) u*u+v*v)/(2.0*MagickSigma*
594 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
595 normalize+=kernel[i][k];
599 if (fabs(normalize) <= MagickEpsilon)
601 normalize=1.0/normalize;
602 for (k=0; k < (j*j); k++)
603 kernel[i][k]=normalize*kernel[i][k];
605 if (i < (ssize_t) width)
607 for (i-=2; i >= 0; i-=2)
608 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
609 kernel=(double **) RelinquishAlignedMemory(kernel);
610 edge_image=DestroyImage(edge_image);
611 sharp_image=DestroyImage(sharp_image);
612 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
615 Adaptively sharpen image.
619 image_view=AcquireVirtualCacheView(image,exception);
620 edge_view=AcquireVirtualCacheView(edge_image,exception);
621 sharp_view=AcquireAuthenticCacheView(sharp_image,exception);
622 #if defined(MAGICKCORE_OPENMP_SUPPORT)
623 #pragma omp parallel for schedule(static,4) shared(progress,status)
625 for (y=0; y < (ssize_t) sharp_image->rows; y++)
627 register const Quantum
636 if (status == MagickFalse)
638 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
639 q=QueueCacheViewAuthenticPixels(sharp_view,0,y,sharp_image->columns,1,
641 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
646 for (x=0; x < (ssize_t) sharp_image->columns; x++)
648 register const Quantum
658 j=(ssize_t) ceil((double) width*QuantumScale*
659 GetPixelIntensity(edge_image,r)-0.5);
663 if (j > (ssize_t) width)
667 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
668 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
669 if (p == (const Quantum *) NULL)
671 center=(ssize_t) GetPixelChannels(image)*(width-j)*((width-j)/2L)+
672 GetPixelChannels(image)*((width-j)/2);
673 if (GetPixelMask(image,p) != 0)
675 q+=GetPixelChannels(sharp_image);
676 r+=GetPixelChannels(edge_image);
679 for (i=0; i < (ssize_t) GetPixelChannels(sharp_image); i++)
693 register const double
696 register const Quantum
705 channel=GetPixelChannelMapChannel(image,i);
706 traits=GetPixelChannelMapTraits(image,channel);
707 sharp_traits=GetPixelChannelMapTraits(sharp_image,channel);
708 if ((traits == UndefinedPixelTrait) ||
709 (sharp_traits == UndefinedPixelTrait))
711 if ((sharp_traits & CopyPixelTrait) != 0)
713 SetPixelChannel(sharp_image,channel,p[center+i],q);
720 if ((sharp_traits & BlendPixelTrait) == 0)
725 for (v=0; v < (ssize_t) (width-j); v++)
727 for (u=0; u < (ssize_t) (width-j); u++)
729 pixel+=(*k)*pixels[i];
732 pixels+=GetPixelChannels(image);
735 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
736 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
742 for (v=0; v < (ssize_t) (width-j); v++)
744 for (u=0; u < (ssize_t) (width-j); u++)
746 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
747 pixel+=(*k)*alpha*pixels[i];
750 pixels+=GetPixelChannels(image);
753 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
754 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
756 q+=GetPixelChannels(sharp_image);
757 r+=GetPixelChannels(edge_image);
759 if (SyncCacheViewAuthenticPixels(sharp_view,exception) == MagickFalse)
761 if (image->progress_monitor != (MagickProgressMonitor) NULL)
766 #if defined(MAGICKCORE_OPENMP_SUPPORT)
767 #pragma omp critical (MagickCore_AdaptiveSharpenImage)
769 proceed=SetImageProgress(image,AdaptiveSharpenImageTag,progress++,
771 if (proceed == MagickFalse)
775 sharp_image->type=image->type;
776 sharp_view=DestroyCacheView(sharp_view);
777 edge_view=DestroyCacheView(edge_view);
778 image_view=DestroyCacheView(image_view);
779 edge_image=DestroyImage(edge_image);
780 for (i=0; i < (ssize_t) width; i+=2)
781 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
782 kernel=(double **) RelinquishAlignedMemory(kernel);
783 if (status == MagickFalse)
784 sharp_image=DestroyImage(sharp_image);
789 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
793 % B l u r I m a g e %
797 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
799 % BlurImage() blurs an image. We convolve the image with a Gaussian operator
800 % of the given radius and standard deviation (sigma). For reasonable results,
801 % the radius should be larger than sigma. Use a radius of 0 and BlurImage()
802 % selects a suitable radius for you.
804 % BlurImage() differs from GaussianBlurImage() in that it uses a separable
805 % kernel which is faster but mathematically equivalent to the non-separable
808 % The format of the BlurImage method is:
810 % Image *BlurImage(const Image *image,const double radius,
811 % const double sigma,ExceptionInfo *exception)
813 % A description of each parameter follows:
815 % o image: the image.
817 % o radius: the radius of the Gaussian, in pixels, not counting the center
820 % o sigma: the standard deviation of the Gaussian, in pixels.
822 % o exception: return any errors or warnings in this structure.
826 static double *GetBlurKernel(const size_t width,const double sigma)
840 Generate a 1-D convolution kernel.
842 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
843 kernel=(double *) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
844 if (kernel == (double *) NULL)
849 for (k=(-j); k <= j; k++)
851 kernel[i]=(double) (exp(-((double) k*k)/(2.0*MagickSigma*MagickSigma))/
852 (MagickSQ2PI*MagickSigma));
853 normalize+=kernel[i];
856 for (i=0; i < (ssize_t) width; i++)
857 kernel[i]/=normalize;
861 MagickExport Image *BlurImage(const Image *image,const double radius,
862 const double sigma,ExceptionInfo *exception)
864 #define BlurImageTag "Blur/Image"
894 Initialize blur image attributes.
896 assert(image != (Image *) NULL);
897 assert(image->signature == MagickSignature);
898 if (image->debug != MagickFalse)
899 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
900 assert(exception != (ExceptionInfo *) NULL);
901 assert(exception->signature == MagickSignature);
902 blur_image=CloneImage(image,0,0,MagickTrue,exception);
903 if (blur_image == (Image *) NULL)
904 return((Image *) NULL);
905 if (fabs(sigma) <= MagickEpsilon)
907 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
909 blur_image=DestroyImage(blur_image);
910 return((Image *) NULL);
912 width=GetOptimalKernelWidth1D(radius,sigma);
913 kernel=GetBlurKernel(width,sigma);
914 if (kernel == (double *) NULL)
916 blur_image=DestroyImage(blur_image);
917 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
919 if (image->debug != MagickFalse)
922 format[MaxTextExtent],
925 register const double
928 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
929 " blur image with kernel width %.20g:",(double) width);
930 message=AcquireString("");
932 for (i=0; i < (ssize_t) width; i++)
935 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) i);
936 (void) ConcatenateString(&message,format);
937 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
938 (void) ConcatenateString(&message,format);
939 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
941 message=DestroyString(message);
948 center=(ssize_t) GetPixelChannels(image)*(width/2L);
949 image_view=AcquireVirtualCacheView(image,exception);
950 blur_view=AcquireAuthenticCacheView(blur_image,exception);
951 #if defined(MAGICKCORE_OPENMP_SUPPORT)
952 #pragma omp parallel for schedule(static,4) shared(progress,status)
954 for (y=0; y < (ssize_t) image->rows; y++)
956 register const Quantum
965 if (status == MagickFalse)
967 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y,
968 image->columns+width,1,exception);
969 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
971 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
976 for (x=0; x < (ssize_t) image->columns; x++)
981 if (GetPixelMask(image,p) != 0)
983 p+=GetPixelChannels(image);
984 q+=GetPixelChannels(blur_image);
987 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1001 register const double
1004 register const Quantum
1010 channel=GetPixelChannelMapChannel(image,i);
1011 traits=GetPixelChannelMapTraits(image,channel);
1012 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
1013 if ((traits == UndefinedPixelTrait) ||
1014 (blur_traits == UndefinedPixelTrait))
1016 if ((blur_traits & CopyPixelTrait) != 0)
1018 SetPixelChannel(blur_image,channel,p[center+i],q);
1024 if ((blur_traits & BlendPixelTrait) == 0)
1029 for (u=0; u < (ssize_t) width; u++)
1031 pixel+=(*k)*pixels[i];
1033 pixels+=GetPixelChannels(image);
1035 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
1042 for (u=0; u < (ssize_t) width; u++)
1044 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
1045 pixel+=(*k)*alpha*pixels[i];
1048 pixels+=GetPixelChannels(image);
1050 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1051 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
1053 p+=GetPixelChannels(image);
1054 q+=GetPixelChannels(blur_image);
1056 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1058 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1063 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1064 #pragma omp critical (MagickCore_BlurImage)
1066 proceed=SetImageProgress(image,BlurImageTag,progress++,blur_image->rows+
1067 blur_image->columns);
1068 if (proceed == MagickFalse)
1072 blur_view=DestroyCacheView(blur_view);
1073 image_view=DestroyCacheView(image_view);
1077 center=(ssize_t) GetPixelChannels(blur_image)*(width/2L);
1078 image_view=AcquireVirtualCacheView(blur_image,exception);
1079 blur_view=AcquireAuthenticCacheView(blur_image,exception);
1080 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1081 #pragma omp parallel for schedule(static,4) shared(progress,status)
1083 for (x=0; x < (ssize_t) blur_image->columns; x++)
1085 register const Quantum
1094 if (status == MagickFalse)
1096 p=GetCacheViewVirtualPixels(image_view,x,-((ssize_t) width/2L),1,
1097 blur_image->rows+width,exception);
1098 q=GetCacheViewAuthenticPixels(blur_view,x,0,1,blur_image->rows,exception);
1099 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1104 for (y=0; y < (ssize_t) blur_image->rows; y++)
1109 if (GetPixelMask(image,p) != 0)
1111 p+=GetPixelChannels(blur_image);
1112 q+=GetPixelChannels(blur_image);
1115 for (i=0; i < (ssize_t) GetPixelChannels(blur_image); i++)
1129 register const double
1132 register const Quantum
1138 channel=GetPixelChannelMapChannel(blur_image,i);
1139 traits=GetPixelChannelMapTraits(blur_image,channel);
1140 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
1141 if ((traits == UndefinedPixelTrait) ||
1142 (blur_traits == UndefinedPixelTrait))
1144 if ((blur_traits & CopyPixelTrait) != 0)
1146 SetPixelChannel(blur_image,channel,p[center+i],q);
1152 if ((blur_traits & BlendPixelTrait) == 0)
1157 for (u=0; u < (ssize_t) width; u++)
1159 pixel+=(*k)*pixels[i];
1161 pixels+=GetPixelChannels(blur_image);
1163 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
1170 for (u=0; u < (ssize_t) width; u++)
1172 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(blur_image,
1174 pixel+=(*k)*alpha*pixels[i];
1177 pixels+=GetPixelChannels(blur_image);
1179 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1180 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
1182 p+=GetPixelChannels(blur_image);
1183 q+=GetPixelChannels(blur_image);
1185 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1187 if (blur_image->progress_monitor != (MagickProgressMonitor) NULL)
1192 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1193 #pragma omp critical (MagickCore_BlurImage)
1195 proceed=SetImageProgress(blur_image,BlurImageTag,progress++,
1196 blur_image->rows+blur_image->columns);
1197 if (proceed == MagickFalse)
1201 blur_view=DestroyCacheView(blur_view);
1202 image_view=DestroyCacheView(image_view);
1203 kernel=(double *) RelinquishAlignedMemory(kernel);
1204 blur_image->type=image->type;
1205 if (status == MagickFalse)
1206 blur_image=DestroyImage(blur_image);
1211 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1215 % C o n v o l v e I m a g e %
1219 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1221 % ConvolveImage() applies a custom convolution kernel to the image.
1223 % The format of the ConvolveImage method is:
1225 % Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
1226 % ExceptionInfo *exception)
1228 % A description of each parameter follows:
1230 % o image: the image.
1232 % o kernel: the filtering kernel.
1234 % o exception: return any errors or warnings in this structure.
1237 MagickExport Image *ConvolveImage(const Image *image,
1238 const KernelInfo *kernel_info,ExceptionInfo *exception)
1240 return(MorphologyImage(image,ConvolveMorphology,1,kernel_info,exception));
1244 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1248 % D e s p e c k l e I m a g e %
1252 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1254 % DespeckleImage() reduces the speckle noise in an image while perserving the
1255 % edges of the original image. A speckle removing filter uses a complementary % hulling technique (raising pixels that are darker than their surrounding
1256 % neighbors, then complementarily lowering pixels that are brighter than their
1257 % surrounding neighbors) to reduce the speckle index of that image (reference
1258 % Crimmins speckle removal).
1260 % The format of the DespeckleImage method is:
1262 % Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1264 % A description of each parameter follows:
1266 % o image: the image.
1268 % o exception: return any errors or warnings in this structure.
1272 static void Hull(const ssize_t x_offset,const ssize_t y_offset,
1273 const size_t columns,const size_t rows,const int polarity,Quantum *restrict f,
1274 Quantum *restrict g)
1285 assert(f != (Quantum *) NULL);
1286 assert(g != (Quantum *) NULL);
1289 r=p+(y_offset*(columns+2)+x_offset);
1290 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1291 #pragma omp parallel for schedule(static)
1293 for (y=0; y < (ssize_t) rows; y++)
1302 i=(2*y+1)+y*columns;
1304 for (x=0; x < (ssize_t) columns; x++)
1306 v=(SignedQuantum) p[i];
1307 if ((SignedQuantum) r[i] >= (v+ScaleCharToQuantum(2)))
1308 v+=ScaleCharToQuantum(1);
1313 for (x=0; x < (ssize_t) columns; x++)
1315 v=(SignedQuantum) p[i];
1316 if ((SignedQuantum) r[i] <= (v-ScaleCharToQuantum(2)))
1317 v-=ScaleCharToQuantum(1);
1324 r=q+(y_offset*(columns+2)+x_offset);
1325 s=q-(y_offset*(columns+2)+x_offset);
1326 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1327 #pragma omp parallel for schedule(static)
1329 for (y=0; y < (ssize_t) rows; y++)
1338 i=(2*y+1)+y*columns;
1340 for (x=0; x < (ssize_t) columns; x++)
1342 v=(SignedQuantum) q[i];
1343 if (((SignedQuantum) s[i] >= (v+ScaleCharToQuantum(2))) &&
1344 ((SignedQuantum) r[i] > v))
1345 v+=ScaleCharToQuantum(1);
1350 for (x=0; x < (ssize_t) columns; x++)
1352 v=(SignedQuantum) q[i];
1353 if (((SignedQuantum) s[i] <= (v-ScaleCharToQuantum(2))) &&
1354 ((SignedQuantum) r[i] < v))
1355 v-=ScaleCharToQuantum(1);
1362 MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1364 #define DespeckleImageTag "Despeckle/Image"
1386 static const ssize_t
1387 X[4] = {0, 1, 1,-1},
1388 Y[4] = {1, 0, 1, 1};
1391 Allocate despeckled image.
1393 assert(image != (const Image *) NULL);
1394 assert(image->signature == MagickSignature);
1395 if (image->debug != MagickFalse)
1396 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1397 assert(exception != (ExceptionInfo *) NULL);
1398 assert(exception->signature == MagickSignature);
1399 despeckle_image=CloneImage(image,0,0,MagickTrue,exception);
1400 if (despeckle_image == (Image *) NULL)
1401 return((Image *) NULL);
1402 status=SetImageStorageClass(despeckle_image,DirectClass,exception);
1403 if (status == MagickFalse)
1405 despeckle_image=DestroyImage(despeckle_image);
1406 return((Image *) NULL);
1409 Allocate image buffer.
1411 length=(size_t) ((image->columns+2)*(image->rows+2));
1412 pixels=(Quantum *) AcquireQuantumMemory(length,sizeof(*pixels));
1413 buffer=(Quantum *) AcquireQuantumMemory(length,sizeof(*buffer));
1414 if ((pixels == (Quantum *) NULL) || (buffer == (Quantum *) NULL))
1416 if (buffer != (Quantum *) NULL)
1417 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1418 if (pixels != (Quantum *) NULL)
1419 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1420 despeckle_image=DestroyImage(despeckle_image);
1421 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1424 Reduce speckle in the image.
1427 image_view=AcquireVirtualCacheView(image,exception);
1428 despeckle_view=AcquireAuthenticCacheView(despeckle_image,exception);
1429 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1446 if (status == MagickFalse)
1448 channel=GetPixelChannelMapChannel(image,i);
1449 traits=GetPixelChannelMapTraits(image,channel);
1450 despeckle_traits=GetPixelChannelMapTraits(despeckle_image,channel);
1451 if ((traits == UndefinedPixelTrait) ||
1452 (despeckle_traits == UndefinedPixelTrait))
1454 if ((despeckle_traits & CopyPixelTrait) != 0)
1456 (void) ResetMagickMemory(pixels,0,length*sizeof(*pixels));
1457 j=(ssize_t) image->columns+2;
1458 for (y=0; y < (ssize_t) image->rows; y++)
1460 register const Quantum
1463 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1464 if (p == (const Quantum *) NULL)
1470 for (x=0; x < (ssize_t) image->columns; x++)
1473 p+=GetPixelChannels(image);
1477 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
1478 for (k=0; k < 4; k++)
1480 Hull(X[k],Y[k],image->columns,image->rows,1,pixels,buffer);
1481 Hull(-X[k],-Y[k],image->columns,image->rows,1,pixels,buffer);
1482 Hull(-X[k],-Y[k],image->columns,image->rows,-1,pixels,buffer);
1483 Hull(X[k],Y[k],image->columns,image->rows,-1,pixels,buffer);
1485 j=(ssize_t) image->columns+2;
1486 for (y=0; y < (ssize_t) image->rows; y++)
1494 q=QueueCacheViewAuthenticPixels(despeckle_view,0,y,
1495 despeckle_image->columns,1,exception);
1496 if (q == (Quantum *) NULL)
1502 for (x=0; x < (ssize_t) image->columns; x++)
1504 SetPixelChannel(despeckle_image,channel,pixels[j++],q);
1505 q+=GetPixelChannels(despeckle_image);
1507 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1508 if (sync == MagickFalse)
1512 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1517 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1518 GetPixelChannels(image));
1519 if (proceed == MagickFalse)
1523 despeckle_view=DestroyCacheView(despeckle_view);
1524 image_view=DestroyCacheView(image_view);
1525 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1526 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1527 despeckle_image->type=image->type;
1528 if (status == MagickFalse)
1529 despeckle_image=DestroyImage(despeckle_image);
1530 return(despeckle_image);
1534 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1538 % E d g e I m a g e %
1542 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1544 % EdgeImage() finds edges in an image. Radius defines the radius of the
1545 % convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1548 % The format of the EdgeImage method is:
1550 % Image *EdgeImage(const Image *image,const double radius,
1551 % const double sigma,ExceptionInfo *exception)
1553 % A description of each parameter follows:
1555 % o image: the image.
1557 % o radius: the radius of the pixel neighborhood.
1559 % o sigma: the standard deviation of the Gaussian, in pixels.
1561 % o exception: return any errors or warnings in this structure.
1564 MagickExport Image *EdgeImage(const Image *image,const double radius,
1565 const double sigma,ExceptionInfo *exception)
1584 assert(image != (const Image *) NULL);
1585 assert(image->signature == MagickSignature);
1586 if (image->debug != MagickFalse)
1587 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1588 assert(exception != (ExceptionInfo *) NULL);
1589 assert(exception->signature == MagickSignature);
1590 width=GetOptimalKernelWidth1D(radius,sigma);
1591 kernel_info=AcquireKernelInfo((const char *) NULL);
1592 if (kernel_info == (KernelInfo *) NULL)
1593 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1594 kernel_info->width=width;
1595 kernel_info->height=width;
1596 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1597 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1598 if (kernel_info->values == (MagickRealType *) NULL)
1600 kernel_info=DestroyKernelInfo(kernel_info);
1601 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1603 j=(ssize_t) kernel_info->width/2;
1605 for (v=(-j); v <= j; v++)
1607 for (u=(-j); u <= j; u++)
1609 kernel_info->values[i]=(-1.0);
1613 kernel_info->values[i/2]=(double) (width*width-1.0);
1614 edge_image=ConvolveImage(image,kernel_info,exception);
1615 kernel_info=DestroyKernelInfo(kernel_info);
1620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1624 % E m b o s s I m a g e %
1628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1630 % EmbossImage() returns a grayscale image with a three-dimensional effect.
1631 % We convolve the image with a Gaussian operator of the given radius and
1632 % standard deviation (sigma). For reasonable results, radius should be
1633 % larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1636 % The format of the EmbossImage method is:
1638 % Image *EmbossImage(const Image *image,const double radius,
1639 % const double sigma,ExceptionInfo *exception)
1641 % A description of each parameter follows:
1643 % o image: the image.
1645 % o radius: the radius of the pixel neighborhood.
1647 % o sigma: the standard deviation of the Gaussian, in pixels.
1649 % o exception: return any errors or warnings in this structure.
1652 MagickExport Image *EmbossImage(const Image *image,const double radius,
1653 const double sigma,ExceptionInfo *exception)
1673 assert(image != (const Image *) NULL);
1674 assert(image->signature == MagickSignature);
1675 if (image->debug != MagickFalse)
1676 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1677 assert(exception != (ExceptionInfo *) NULL);
1678 assert(exception->signature == MagickSignature);
1679 width=GetOptimalKernelWidth1D(radius,sigma);
1680 kernel_info=AcquireKernelInfo((const char *) NULL);
1681 if (kernel_info == (KernelInfo *) NULL)
1682 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1683 kernel_info->width=width;
1684 kernel_info->height=width;
1685 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1686 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1687 if (kernel_info->values == (MagickRealType *) NULL)
1689 kernel_info=DestroyKernelInfo(kernel_info);
1690 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1692 j=(ssize_t) kernel_info->width/2;
1695 for (v=(-j); v <= j; v++)
1697 for (u=(-j); u <= j; u++)
1699 kernel_info->values[i]=(double) (((u < 0) || (v < 0) ? -8.0 : 8.0)*
1700 exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
1701 (2.0*MagickPI*MagickSigma*MagickSigma));
1703 kernel_info->values[i]=0.0;
1708 emboss_image=ConvolveImage(image,kernel_info,exception);
1709 kernel_info=DestroyKernelInfo(kernel_info);
1710 if (emboss_image != (Image *) NULL)
1711 (void) EqualizeImage(emboss_image,exception);
1712 return(emboss_image);
1716 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1720 % G a u s s i a n B l u r I m a g e %
1724 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1726 % GaussianBlurImage() blurs an image. We convolve the image with a
1727 % Gaussian operator of the given radius and standard deviation (sigma).
1728 % For reasonable results, the radius should be larger than sigma. Use a
1729 % radius of 0 and GaussianBlurImage() selects a suitable radius for you
1731 % The format of the GaussianBlurImage method is:
1733 % Image *GaussianBlurImage(const Image *image,onst double radius,
1734 % const double sigma,ExceptionInfo *exception)
1736 % A description of each parameter follows:
1738 % o image: the image.
1740 % o radius: the radius of the Gaussian, in pixels, not counting the center
1743 % o sigma: the standard deviation of the Gaussian, in pixels.
1745 % o exception: return any errors or warnings in this structure.
1748 MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
1749 const double sigma,ExceptionInfo *exception)
1768 assert(image != (const Image *) NULL);
1769 assert(image->signature == MagickSignature);
1770 if (image->debug != MagickFalse)
1771 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1772 assert(exception != (ExceptionInfo *) NULL);
1773 assert(exception->signature == MagickSignature);
1774 width=GetOptimalKernelWidth2D(radius,sigma);
1775 kernel_info=AcquireKernelInfo((const char *) NULL);
1776 if (kernel_info == (KernelInfo *) NULL)
1777 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1778 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1779 kernel_info->width=width;
1780 kernel_info->height=width;
1781 kernel_info->signature=MagickSignature;
1782 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1783 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1784 if (kernel_info->values == (MagickRealType *) NULL)
1786 kernel_info=DestroyKernelInfo(kernel_info);
1787 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1789 j=(ssize_t) kernel_info->width/2;
1791 for (v=(-j); v <= j; v++)
1793 for (u=(-j); u <= j; u++)
1795 kernel_info->values[i]=(double) (exp(-((double) u*u+v*v)/(2.0*
1796 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
1800 blur_image=ConvolveImage(image,kernel_info,exception);
1801 kernel_info=DestroyKernelInfo(kernel_info);
1806 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1810 % M o t i o n B l u r I m a g e %
1814 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1816 % MotionBlurImage() simulates motion blur. We convolve the image with a
1817 % Gaussian operator of the given radius and standard deviation (sigma).
1818 % For reasonable results, radius should be larger than sigma. Use a
1819 % radius of 0 and MotionBlurImage() selects a suitable radius for you.
1820 % Angle gives the angle of the blurring motion.
1822 % Andrew Protano contributed this effect.
1824 % The format of the MotionBlurImage method is:
1826 % Image *MotionBlurImage(const Image *image,const double radius,
1827 % const double sigma,const double angle,ExceptionInfo *exception)
1829 % A description of each parameter follows:
1831 % o image: the image.
1833 % o radius: the radius of the Gaussian, in pixels, not counting
1836 % o sigma: the standard deviation of the Gaussian, in pixels.
1838 % o angle: Apply the effect along this angle.
1840 % o exception: return any errors or warnings in this structure.
1844 static double *GetMotionBlurKernel(const size_t width,const double sigma)
1854 Generate a 1-D convolution kernel.
1856 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1857 kernel=(double *) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
1858 if (kernel == (double *) NULL)
1861 for (i=0; i < (ssize_t) width; i++)
1863 kernel[i]=(double) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
1864 MagickSigma)))/(MagickSQ2PI*MagickSigma));
1865 normalize+=kernel[i];
1867 for (i=0; i < (ssize_t) width; i++)
1868 kernel[i]/=normalize;
1872 MagickExport Image *MotionBlurImage(const Image *image,const double radius,
1873 const double sigma,const double angle,ExceptionInfo *exception)
1907 assert(image != (Image *) NULL);
1908 assert(image->signature == MagickSignature);
1909 if (image->debug != MagickFalse)
1910 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1911 assert(exception != (ExceptionInfo *) NULL);
1912 width=GetOptimalKernelWidth1D(radius,sigma);
1913 kernel=GetMotionBlurKernel(width,sigma);
1914 if (kernel == (double *) NULL)
1915 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1916 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
1917 if (offset == (OffsetInfo *) NULL)
1919 kernel=(double *) RelinquishAlignedMemory(kernel);
1920 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1922 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1923 if (blur_image == (Image *) NULL)
1925 kernel=(double *) RelinquishAlignedMemory(kernel);
1926 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1927 return((Image *) NULL);
1929 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
1931 kernel=(double *) RelinquishAlignedMemory(kernel);
1932 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1933 blur_image=DestroyImage(blur_image);
1934 return((Image *) NULL);
1936 point.x=(double) width*sin(DegreesToRadians(angle));
1937 point.y=(double) width*cos(DegreesToRadians(angle));
1938 for (i=0; i < (ssize_t) width; i++)
1940 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
1941 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
1948 image_view=AcquireVirtualCacheView(image,exception);
1949 motion_view=AcquireVirtualCacheView(image,exception);
1950 blur_view=AcquireAuthenticCacheView(blur_image,exception);
1951 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1952 #pragma omp parallel for schedule(static,4) shared(progress,status)
1954 for (y=0; y < (ssize_t) image->rows; y++)
1956 register const Quantum
1965 if (status == MagickFalse)
1967 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1968 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
1970 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1975 for (x=0; x < (ssize_t) image->columns; x++)
1980 if (GetPixelMask(image,p) != 0)
1982 p+=GetPixelChannels(image);
1983 q+=GetPixelChannels(blur_image);
1986 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2000 register const Quantum
2009 channel=GetPixelChannelMapChannel(image,i);
2010 traits=GetPixelChannelMapTraits(image,channel);
2011 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
2012 if ((traits == UndefinedPixelTrait) ||
2013 (blur_traits == UndefinedPixelTrait))
2015 if ((blur_traits & CopyPixelTrait) != 0)
2017 SetPixelChannel(blur_image,channel,p[i],q);
2022 if ((blur_traits & BlendPixelTrait) == 0)
2024 for (j=0; j < (ssize_t) width; j++)
2026 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+
2027 offset[j].y,1,1,exception);
2028 if (r == (const Quantum *) NULL)
2036 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
2041 for (j=0; j < (ssize_t) width; j++)
2043 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+offset[j].y,1,
2045 if (r == (const Quantum *) NULL)
2050 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,r));
2051 pixel+=(*k)*alpha*r[i];
2055 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
2056 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2058 p+=GetPixelChannels(image);
2059 q+=GetPixelChannels(blur_image);
2061 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2063 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2068 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2069 #pragma omp critical (MagickCore_MotionBlurImage)
2071 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2072 if (proceed == MagickFalse)
2076 blur_view=DestroyCacheView(blur_view);
2077 motion_view=DestroyCacheView(motion_view);
2078 image_view=DestroyCacheView(image_view);
2079 kernel=(double *) RelinquishAlignedMemory(kernel);
2080 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2081 if (status == MagickFalse)
2082 blur_image=DestroyImage(blur_image);
2087 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2091 % P r e v i e w I m a g e %
2095 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2097 % PreviewImage() tiles 9 thumbnails of the specified image with an image
2098 % processing operation applied with varying parameters. This may be helpful
2099 % pin-pointing an appropriate parameter for a particular image processing
2102 % The format of the PreviewImages method is:
2104 % Image *PreviewImages(const Image *image,const PreviewType preview,
2105 % ExceptionInfo *exception)
2107 % A description of each parameter follows:
2109 % o image: the image.
2111 % o preview: the image processing operation.
2113 % o exception: return any errors or warnings in this structure.
2116 MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2117 ExceptionInfo *exception)
2119 #define NumberTiles 9
2120 #define PreviewImageTag "Preview/Image"
2121 #define DefaultPreviewGeometry "204x204+10+10"
2124 factor[MaxTextExtent],
2125 label[MaxTextExtent];
2167 Open output image file.
2169 assert(image != (Image *) NULL);
2170 assert(image->signature == MagickSignature);
2171 if (image->debug != MagickFalse)
2172 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2176 preview_info=AcquireImageInfo();
2177 SetGeometry(image,&geometry);
2178 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2179 &geometry.width,&geometry.height);
2180 images=NewImageList();
2182 GetQuantizeInfo(&quantize_info);
2188 for (i=0; i < NumberTiles; i++)
2190 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2191 if (thumbnail == (Image *) NULL)
2193 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2195 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel,exception);
2196 if (i == (NumberTiles/2))
2198 (void) QueryColorCompliance("#dfdfdf",AllCompliance,
2199 &thumbnail->matte_color,exception);
2200 AppendImageToList(&images,thumbnail);
2208 preview_image=RotateImage(thumbnail,degrees,exception);
2209 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
2215 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
2216 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",
2217 degrees,2.0*degrees);
2222 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2223 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
2224 preview_image=RollImage(thumbnail,x,y,exception);
2225 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
2226 (double) x,(double) y);
2231 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2232 if (preview_image == (Image *) NULL)
2234 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",
2236 (void) ModulateImage(preview_image,factor,exception);
2237 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2240 case SaturationPreview:
2242 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2243 if (preview_image == (Image *) NULL)
2245 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",
2247 (void) ModulateImage(preview_image,factor,exception);
2248 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2251 case BrightnessPreview:
2253 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2254 if (preview_image == (Image *) NULL)
2256 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
2257 (void) ModulateImage(preview_image,factor,exception);
2258 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2264 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2265 if (preview_image == (Image *) NULL)
2268 (void) GammaImage(preview_image,gamma,exception);
2269 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
2274 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2275 if (preview_image != (Image *) NULL)
2276 for (x=0; x < i; x++)
2277 (void) ContrastImage(preview_image,MagickTrue,exception);
2278 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
2284 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2285 if (preview_image == (Image *) NULL)
2287 for (x=0; x < i; x++)
2288 (void) ContrastImage(preview_image,MagickFalse,exception);
2289 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
2293 case GrayscalePreview:
2295 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2296 if (preview_image == (Image *) NULL)
2299 quantize_info.number_colors=colors;
2300 quantize_info.colorspace=GRAYColorspace;
2301 (void) QuantizeImage(&quantize_info,preview_image,exception);
2302 (void) FormatLocaleString(label,MaxTextExtent,
2303 "-colorspace gray -colors %.20g",(double) colors);
2306 case QuantizePreview:
2308 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2309 if (preview_image == (Image *) NULL)
2312 quantize_info.number_colors=colors;
2313 (void) QuantizeImage(&quantize_info,preview_image,exception);
2314 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
2318 case DespecklePreview:
2320 for (x=0; x < (i-1); x++)
2322 preview_image=DespeckleImage(thumbnail,exception);
2323 if (preview_image == (Image *) NULL)
2325 thumbnail=DestroyImage(thumbnail);
2326 thumbnail=preview_image;
2328 preview_image=DespeckleImage(thumbnail,exception);
2329 if (preview_image == (Image *) NULL)
2331 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
2335 case ReduceNoisePreview:
2337 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2338 (size_t) radius,exception);
2339 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
2342 case AddNoisePreview:
2348 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2353 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2358 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2363 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2368 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2373 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2378 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2382 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2383 (size_t) i,exception);
2384 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
2387 case SharpenPreview:
2389 preview_image=SharpenImage(thumbnail,radius,sigma,exception);
2390 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",
2396 preview_image=BlurImage(thumbnail,radius,sigma,exception);
2397 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
2401 case ThresholdPreview:
2403 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2404 if (preview_image == (Image *) NULL)
2406 (void) BilevelImage(thumbnail,(double) (percentage*((MagickRealType)
2407 QuantumRange+1.0))/100.0,exception);
2408 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",
2409 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
2412 case EdgeDetectPreview:
2414 preview_image=EdgeImage(thumbnail,radius,sigma,exception);
2415 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
2420 preview_image=SpreadImage(thumbnail,radius,thumbnail->interpolate,
2422 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",
2426 case SolarizePreview:
2428 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2429 if (preview_image == (Image *) NULL)
2431 (void) SolarizeImage(preview_image,(double) QuantumRange*
2432 percentage/100.0,exception);
2433 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
2434 (QuantumRange*percentage)/100.0);
2440 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2442 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",
2448 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2449 if (preview_image == (Image *) NULL)
2451 geometry.width=(size_t) (2*i+2);
2452 geometry.height=(size_t) (2*i+2);
2455 (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
2456 (void) FormatLocaleString(label,MaxTextExtent,
2457 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
2458 geometry.height,(double) geometry.x,(double) geometry.y);
2461 case SegmentPreview:
2463 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2464 if (preview_image == (Image *) NULL)
2467 (void) SegmentImage(preview_image,sRGBColorspace,MagickFalse,threshold,
2468 threshold,exception);
2469 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
2470 threshold,threshold);
2475 preview_image=SwirlImage(thumbnail,degrees,image->interpolate,
2477 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
2481 case ImplodePreview:
2484 preview_image=ImplodeImage(thumbnail,degrees,image->interpolate,
2486 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
2492 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,
2493 image->interpolate,exception);
2494 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",
2495 0.5*degrees,2.0*degrees);
2498 case OilPaintPreview:
2500 preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2502 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2506 case CharcoalDrawingPreview:
2508 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
2510 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2517 filename[MaxTextExtent];
2525 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2526 if (preview_image == (Image *) NULL)
2528 preview_info->quality=(size_t) percentage;
2529 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
2530 preview_info->quality);
2531 file=AcquireUniqueFileResource(filename);
2534 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
2535 "jpeg:%s",filename);
2536 status=WriteImage(preview_info,preview_image,exception);
2537 if (status != MagickFalse)
2542 (void) CopyMagickString(preview_info->filename,
2543 preview_image->filename,MaxTextExtent);
2544 quality_image=ReadImage(preview_info,exception);
2545 if (quality_image != (Image *) NULL)
2547 preview_image=DestroyImage(preview_image);
2548 preview_image=quality_image;
2551 (void) RelinquishUniqueFileResource(preview_image->filename);
2552 if ((GetBlobSize(preview_image)/1024) >= 1024)
2553 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
2554 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2557 if (GetBlobSize(preview_image) >= 1024)
2558 (void) FormatLocaleString(label,MaxTextExtent,
2559 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
2560 GetBlobSize(preview_image))/1024.0);
2562 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
2563 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
2567 thumbnail=DestroyImage(thumbnail);
2571 if (preview_image == (Image *) NULL)
2573 (void) DeleteImageProperty(preview_image,"label");
2574 (void) SetImageProperty(preview_image,"label",label,exception);
2575 AppendImageToList(&images,preview_image);
2576 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2578 if (proceed == MagickFalse)
2581 if (images == (Image *) NULL)
2583 preview_info=DestroyImageInfo(preview_info);
2584 return((Image *) NULL);
2589 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2590 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2591 montage_info->shadow=MagickTrue;
2592 (void) CloneString(&montage_info->tile,"3x3");
2593 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2594 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2595 montage_image=MontageImages(images,montage_info,exception);
2596 montage_info=DestroyMontageInfo(montage_info);
2597 images=DestroyImageList(images);
2598 if (montage_image == (Image *) NULL)
2599 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2600 if (montage_image->montage != (char *) NULL)
2603 Free image directory.
2605 montage_image->montage=(char *) RelinquishMagickMemory(
2606 montage_image->montage);
2607 if (image->directory != (char *) NULL)
2608 montage_image->directory=(char *) RelinquishMagickMemory(
2609 montage_image->directory);
2611 preview_info=DestroyImageInfo(preview_info);
2612 return(montage_image);
2616 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2620 % R a d i a l B l u r I m a g e %
2624 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2626 % RadialBlurImage() applies a radial blur to the image.
2628 % Andrew Protano contributed this effect.
2630 % The format of the RadialBlurImage method is:
2632 % Image *RadialBlurImage(const Image *image,const double angle,
2633 % ExceptionInfo *exception)
2635 % A description of each parameter follows:
2637 % o image: the image.
2639 % o angle: the angle of the radial blur.
2643 % o exception: return any errors or warnings in this structure.
2646 MagickExport Image *RadialBlurImage(const Image *image,const double angle,
2647 ExceptionInfo *exception)
2683 Allocate blur image.
2685 assert(image != (Image *) NULL);
2686 assert(image->signature == MagickSignature);
2687 if (image->debug != MagickFalse)
2688 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2689 assert(exception != (ExceptionInfo *) NULL);
2690 assert(exception->signature == MagickSignature);
2691 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2692 if (blur_image == (Image *) NULL)
2693 return((Image *) NULL);
2694 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2696 blur_image=DestroyImage(blur_image);
2697 return((Image *) NULL);
2699 blur_center.x=(double) image->columns/2.0;
2700 blur_center.y=(double) image->rows/2.0;
2701 blur_radius=hypot(blur_center.x,blur_center.y);
2702 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
2703 theta=DegreesToRadians(angle)/(MagickRealType) (n-1);
2704 cos_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2705 sizeof(*cos_theta));
2706 sin_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2707 sizeof(*sin_theta));
2708 if ((cos_theta == (MagickRealType *) NULL) ||
2709 (sin_theta == (MagickRealType *) NULL))
2711 blur_image=DestroyImage(blur_image);
2712 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2714 offset=theta*(MagickRealType) (n-1)/2.0;
2715 for (i=0; i < (ssize_t) n; i++)
2717 cos_theta[i]=cos((double) (theta*i-offset));
2718 sin_theta[i]=sin((double) (theta*i-offset));
2725 image_view=AcquireVirtualCacheView(image,exception);
2726 radial_view=AcquireVirtualCacheView(image,exception);
2727 blur_view=AcquireAuthenticCacheView(blur_image,exception);
2728 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2729 #pragma omp parallel for schedule(static,4) shared(progress,status)
2731 for (y=0; y < (ssize_t) image->rows; y++)
2733 register const Quantum
2742 if (status == MagickFalse)
2744 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2745 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2747 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2752 for (x=0; x < (ssize_t) image->columns; x++)
2766 center.x=(double) x-blur_center.x;
2767 center.y=(double) y-blur_center.y;
2768 radius=hypot((double) center.x,center.y);
2773 step=(size_t) (blur_radius/radius);
2780 if (GetPixelMask(image,p) != 0)
2782 p+=GetPixelChannels(image);
2783 q+=GetPixelChannels(blur_image);
2786 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2799 register const Quantum
2805 channel=GetPixelChannelMapChannel(image,i);
2806 traits=GetPixelChannelMapTraits(image,channel);
2807 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
2808 if ((traits == UndefinedPixelTrait) ||
2809 (blur_traits == UndefinedPixelTrait))
2811 if ((blur_traits & CopyPixelTrait) != 0)
2813 SetPixelChannel(blur_image,channel,p[i],q);
2818 if ((blur_traits & BlendPixelTrait) == 0)
2820 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2822 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2823 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2824 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2826 if (r == (const Quantum *) NULL)
2834 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
2835 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2838 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2840 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2841 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2842 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2844 if (r == (const Quantum *) NULL)
2849 pixel+=GetPixelAlpha(image,r)*r[i];
2850 gamma+=GetPixelAlpha(image,r);
2852 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
2853 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2855 p+=GetPixelChannels(image);
2856 q+=GetPixelChannels(blur_image);
2858 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2860 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2865 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2866 #pragma omp critical (MagickCore_RadialBlurImage)
2868 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2869 if (proceed == MagickFalse)
2873 blur_view=DestroyCacheView(blur_view);
2874 radial_view=DestroyCacheView(radial_view);
2875 image_view=DestroyCacheView(image_view);
2876 cos_theta=(MagickRealType *) RelinquishMagickMemory(cos_theta);
2877 sin_theta=(MagickRealType *) RelinquishMagickMemory(sin_theta);
2878 if (status == MagickFalse)
2879 blur_image=DestroyImage(blur_image);
2884 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2888 % S e l e c t i v e B l u r I m a g e %
2892 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2894 % SelectiveBlurImage() selectively blur pixels within a contrast threshold.
2895 % It is similar to the unsharpen mask that sharpens everything with contrast
2896 % above a certain threshold.
2898 % The format of the SelectiveBlurImage method is:
2900 % Image *SelectiveBlurImage(const Image *image,const double radius,
2901 % const double sigma,const double threshold,ExceptionInfo *exception)
2903 % A description of each parameter follows:
2905 % o image: the image.
2907 % o radius: the radius of the Gaussian, in pixels, not counting the center
2910 % o sigma: the standard deviation of the Gaussian, in pixels.
2912 % o threshold: only pixels within this contrast threshold are included
2913 % in the blur operation.
2915 % o exception: return any errors or warnings in this structure.
2918 MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
2919 const double sigma,const double threshold,ExceptionInfo *exception)
2921 #define SelectiveBlurImageTag "SelectiveBlur/Image"
2953 Initialize blur image attributes.
2955 assert(image != (Image *) NULL);
2956 assert(image->signature == MagickSignature);
2957 if (image->debug != MagickFalse)
2958 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2959 assert(exception != (ExceptionInfo *) NULL);
2960 assert(exception->signature == MagickSignature);
2961 width=GetOptimalKernelWidth1D(radius,sigma);
2962 kernel=(double *) AcquireAlignedMemory((size_t) width,width*sizeof(*kernel));
2963 if (kernel == (double *) NULL)
2964 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2965 j=(ssize_t) width/2;
2967 for (v=(-j); v <= j; v++)
2969 for (u=(-j); u <= j; u++)
2970 kernel[i++]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
2971 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
2973 if (image->debug != MagickFalse)
2976 format[MaxTextExtent],
2979 register const double
2986 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
2987 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
2989 message=AcquireString("");
2991 for (v=0; v < (ssize_t) width; v++)
2994 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
2995 (void) ConcatenateString(&message,format);
2996 for (u=0; u < (ssize_t) width; u++)
2998 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",*k++);
2999 (void) ConcatenateString(&message,format);
3001 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
3003 message=DestroyString(message);
3005 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3006 if (blur_image == (Image *) NULL)
3007 return((Image *) NULL);
3008 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
3010 blur_image=DestroyImage(blur_image);
3011 return((Image *) NULL);
3014 Threshold blur image.
3018 center=(ssize_t) (GetPixelChannels(image)*(image->columns+width)*(width/2L)+
3019 GetPixelChannels(image)*(width/2L));
3020 image_view=AcquireVirtualCacheView(image,exception);
3021 blur_view=AcquireAuthenticCacheView(blur_image,exception);
3022 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3023 #pragma omp parallel for schedule(static,4) shared(progress,status)
3025 for (y=0; y < (ssize_t) image->rows; y++)
3033 register const Quantum
3042 if (status == MagickFalse)
3044 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
3045 (width/2L),image->columns+width,width,exception);
3046 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3048 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3053 for (x=0; x < (ssize_t) image->columns; x++)
3058 if (GetPixelMask(image,p) != 0)
3060 p+=GetPixelChannels(image);
3061 q+=GetPixelChannels(blur_image);
3064 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3079 register const double
3082 register const Quantum
3091 channel=GetPixelChannelMapChannel(image,i);
3092 traits=GetPixelChannelMapTraits(image,channel);
3093 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
3094 if ((traits == UndefinedPixelTrait) ||
3095 (blur_traits == UndefinedPixelTrait))
3097 if ((blur_traits & CopyPixelTrait) != 0)
3099 SetPixelChannel(blur_image,channel,p[center+i],q);
3105 intensity=(MagickRealType) GetPixelIntensity(image,p+center);
3107 if ((blur_traits & BlendPixelTrait) == 0)
3109 for (v=0; v < (ssize_t) width; v++)
3111 for (u=0; u < (ssize_t) width; u++)
3113 contrast=GetPixelIntensity(image,pixels)-intensity;
3114 if (fabs(contrast) < threshold)
3116 pixel+=(*k)*pixels[i];
3120 pixels+=GetPixelChannels(image);
3122 pixels+=image->columns*GetPixelChannels(image);
3124 if (fabs((double) gamma) < MagickEpsilon)
3126 SetPixelChannel(blur_image,channel,p[center+i],q);
3129 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3130 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3133 for (v=0; v < (ssize_t) width; v++)
3135 for (u=0; u < (ssize_t) width; u++)
3137 contrast=GetPixelIntensity(image,pixels)-intensity;
3138 if (fabs(contrast) < threshold)
3140 alpha=(MagickRealType) (QuantumScale*
3141 GetPixelAlpha(image,pixels));
3142 pixel+=(*k)*alpha*pixels[i];
3146 pixels+=GetPixelChannels(image);
3148 pixels+=image->columns*GetPixelChannels(image);
3150 if (fabs((double) gamma) < MagickEpsilon)
3152 SetPixelChannel(blur_image,channel,p[center+i],q);
3155 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3156 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3158 p+=GetPixelChannels(image);
3159 q+=GetPixelChannels(blur_image);
3161 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3162 if (sync == MagickFalse)
3164 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3169 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3170 #pragma omp critical (MagickCore_SelectiveBlurImage)
3172 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3174 if (proceed == MagickFalse)
3178 blur_image->type=image->type;
3179 blur_view=DestroyCacheView(blur_view);
3180 image_view=DestroyCacheView(image_view);
3181 kernel=(double *) RelinquishAlignedMemory(kernel);
3182 if (status == MagickFalse)
3183 blur_image=DestroyImage(blur_image);
3188 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3192 % S h a d e I m a g e %
3196 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3198 % ShadeImage() shines a distant light on an image to create a
3199 % three-dimensional effect. You control the positioning of the light with
3200 % azimuth and elevation; azimuth is measured in degrees off the x axis
3201 % and elevation is measured in pixels above the Z axis.
3203 % The format of the ShadeImage method is:
3205 % Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3206 % const double azimuth,const double elevation,ExceptionInfo *exception)
3208 % A description of each parameter follows:
3210 % o image: the image.
3212 % o gray: A value other than zero shades the intensity of each pixel.
3214 % o azimuth, elevation: Define the light source direction.
3216 % o exception: return any errors or warnings in this structure.
3219 MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3220 const double azimuth,const double elevation,ExceptionInfo *exception)
3222 #define ShadeImageTag "Shade/Image"
3244 Initialize shaded image attributes.
3246 assert(image != (const Image *) NULL);
3247 assert(image->signature == MagickSignature);
3248 if (image->debug != MagickFalse)
3249 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3250 assert(exception != (ExceptionInfo *) NULL);
3251 assert(exception->signature == MagickSignature);
3252 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3253 if (shade_image == (Image *) NULL)
3254 return((Image *) NULL);
3255 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
3257 shade_image=DestroyImage(shade_image);
3258 return((Image *) NULL);
3261 Compute the light vector.
3263 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3264 cos(DegreesToRadians(elevation));
3265 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3266 cos(DegreesToRadians(elevation));
3267 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3273 image_view=AcquireVirtualCacheView(image,exception);
3274 shade_view=AcquireAuthenticCacheView(shade_image,exception);
3275 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3276 #pragma omp parallel for schedule(static,4) shared(progress,status)
3278 for (y=0; y < (ssize_t) image->rows; y++)
3288 register const Quantum
3300 if (status == MagickFalse)
3302 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
3303 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3305 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3311 Shade this row of pixels.
3313 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
3314 pre=p+GetPixelChannels(image);
3315 center=pre+(image->columns+2)*GetPixelChannels(image);
3316 post=center+(image->columns+2)*GetPixelChannels(image);
3317 for (x=0; x < (ssize_t) image->columns; x++)
3323 Determine the surface normal and compute shading.
3325 normal.x=(double) (GetPixelIntensity(image,pre-GetPixelChannels(image))+
3326 GetPixelIntensity(image,center-GetPixelChannels(image))+
3327 GetPixelIntensity(image,post-GetPixelChannels(image))-
3328 GetPixelIntensity(image,pre+GetPixelChannels(image))-
3329 GetPixelIntensity(image,center+GetPixelChannels(image))-
3330 GetPixelIntensity(image,post+GetPixelChannels(image)));
3331 normal.y=(double) (GetPixelIntensity(image,post-GetPixelChannels(image))+
3332 GetPixelIntensity(image,post)+GetPixelIntensity(image,post+
3333 GetPixelChannels(image))-GetPixelIntensity(image,pre-
3334 GetPixelChannels(image))-GetPixelIntensity(image,pre)-
3335 GetPixelIntensity(image,pre+GetPixelChannels(image)));
3336 if ((normal.x == 0.0) && (normal.y == 0.0))
3341 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3342 if (distance > MagickEpsilon)
3345 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
3346 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3347 shade=distance/sqrt((double) normal_distance);
3350 if (GetPixelMask(image,p) != 0)
3352 pre+=GetPixelChannels(image);
3353 center+=GetPixelChannels(image);
3354 post+=GetPixelChannels(image);
3355 q+=GetPixelChannels(shade_image);
3358 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3367 channel=GetPixelChannelMapChannel(image,i);
3368 traits=GetPixelChannelMapTraits(image,channel);
3369 shade_traits=GetPixelChannelMapTraits(shade_image,channel);
3370 if ((traits == UndefinedPixelTrait) ||
3371 (shade_traits == UndefinedPixelTrait))
3373 if ((shade_traits & CopyPixelTrait) != 0)
3375 SetPixelChannel(shade_image,channel,center[i],q);
3378 if (gray != MagickFalse)
3380 SetPixelChannel(shade_image,channel,ClampToQuantum(shade),q);
3383 SetPixelChannel(shade_image,channel,ClampToQuantum(QuantumScale*shade*
3386 pre+=GetPixelChannels(image);
3387 center+=GetPixelChannels(image);
3388 post+=GetPixelChannels(image);
3389 q+=GetPixelChannels(shade_image);
3391 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3393 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3398 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3399 #pragma omp critical (MagickCore_ShadeImage)
3401 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3402 if (proceed == MagickFalse)
3406 shade_view=DestroyCacheView(shade_view);
3407 image_view=DestroyCacheView(image_view);
3408 if (status == MagickFalse)
3409 shade_image=DestroyImage(shade_image);
3410 return(shade_image);
3414 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3418 % S h a r p e n I m a g e %
3422 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3424 % SharpenImage() sharpens the image. We convolve the image with a Gaussian
3425 % operator of the given radius and standard deviation (sigma). For
3426 % reasonable results, radius should be larger than sigma. Use a radius of 0
3427 % and SharpenImage() selects a suitable radius for you.
3429 % Using a separable kernel would be faster, but the negative weights cancel
3430 % out on the corners of the kernel producing often undesirable ringing in the
3431 % filtered result; this can be avoided by using a 2D gaussian shaped image
3432 % sharpening kernel instead.
3434 % The format of the SharpenImage method is:
3436 % Image *SharpenImage(const Image *image,const double radius,
3437 % const double sigma,ExceptionInfo *exception)
3439 % A description of each parameter follows:
3441 % o image: the image.
3443 % o radius: the radius of the Gaussian, in pixels, not counting the center
3446 % o sigma: the standard deviation of the Laplacian, in pixels.
3448 % o exception: return any errors or warnings in this structure.
3451 MagickExport Image *SharpenImage(const Image *image,const double radius,
3452 const double sigma,ExceptionInfo *exception)
3474 assert(image != (const Image *) NULL);
3475 assert(image->signature == MagickSignature);
3476 if (image->debug != MagickFalse)
3477 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3478 assert(exception != (ExceptionInfo *) NULL);
3479 assert(exception->signature == MagickSignature);
3480 width=GetOptimalKernelWidth2D(radius,sigma);
3481 kernel_info=AcquireKernelInfo((const char *) NULL);
3482 if (kernel_info == (KernelInfo *) NULL)
3483 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3484 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3485 kernel_info->width=width;
3486 kernel_info->height=width;
3487 kernel_info->signature=MagickSignature;
3488 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
3489 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
3490 if (kernel_info->values == (MagickRealType *) NULL)
3492 kernel_info=DestroyKernelInfo(kernel_info);
3493 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3496 j=(ssize_t) kernel_info->width/2;
3498 for (v=(-j); v <= j; v++)
3500 for (u=(-j); u <= j; u++)
3502 kernel_info->values[i]=(double) (-exp(-((double) u*u+v*v)/(2.0*
3503 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3504 normalize+=kernel_info->values[i];
3508 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
3509 sharp_image=ConvolveImage(image,kernel_info,exception);
3510 kernel_info=DestroyKernelInfo(kernel_info);
3511 return(sharp_image);
3515 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3519 % S p r e a d I m a g e %
3523 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3525 % SpreadImage() is a special effects method that randomly displaces each
3526 % pixel in a block defined by the radius parameter.
3528 % The format of the SpreadImage method is:
3530 % Image *SpreadImage(const Image *image,const double radius,
3531 % const PixelInterpolateMethod method,ExceptionInfo *exception)
3533 % A description of each parameter follows:
3535 % o image: the image.
3537 % o radius: choose a random pixel in a neighborhood of this extent.
3539 % o method: the pixel interpolation method.
3541 % o exception: return any errors or warnings in this structure.
3544 MagickExport Image *SpreadImage(const Image *image,const double radius,
3545 const PixelInterpolateMethod method,ExceptionInfo *exception)
3547 #define SpreadImageTag "Spread/Image"
3563 **restrict random_info;
3575 Initialize spread image attributes.
3577 assert(image != (Image *) NULL);
3578 assert(image->signature == MagickSignature);
3579 if (image->debug != MagickFalse)
3580 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3581 assert(exception != (ExceptionInfo *) NULL);
3582 assert(exception->signature == MagickSignature);
3583 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3585 if (spread_image == (Image *) NULL)
3586 return((Image *) NULL);
3587 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
3589 spread_image=DestroyImage(spread_image);
3590 return((Image *) NULL);
3597 width=GetOptimalKernelWidth1D(radius,0.5);
3598 random_info=AcquireRandomInfoThreadSet();
3599 key=GetRandomSecretKey(random_info[0]);
3600 image_view=AcquireVirtualCacheView(image,exception);
3601 spread_view=AcquireAuthenticCacheView(spread_image,exception);
3602 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3603 #pragma omp parallel for schedule(static,8) shared(progress,status) \
3604 if (key == ~0UL) num_threads(GetMagickResourceLimit(ThreadResource))
3606 for (y=0; y < (ssize_t) image->rows; y++)
3609 id = GetOpenMPThreadId();
3611 register const Quantum
3620 if (status == MagickFalse)
3622 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3623 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
3625 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3630 for (x=0; x < (ssize_t) image->columns; x++)
3635 point.x=GetPseudoRandomValue(random_info[id]);
3636 point.y=GetPseudoRandomValue(random_info[id]);
3637 status=InterpolatePixelChannels(image,image_view,spread_image,method,
3638 (double) x+width*point.x-0.5,(double) y+width*point.y-0.5,q,exception);
3639 q+=GetPixelChannels(spread_image);
3641 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
3643 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3648 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3649 #pragma omp critical (MagickCore_SpreadImage)
3651 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3652 if (proceed == MagickFalse)
3656 spread_view=DestroyCacheView(spread_view);
3657 image_view=DestroyCacheView(image_view);
3658 random_info=DestroyRandomInfoThreadSet(random_info);
3659 return(spread_image);
3663 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3667 % U n s h a r p M a s k I m a g e %
3671 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3673 % UnsharpMaskImage() sharpens one or more image channels. We convolve the
3674 % image with a Gaussian operator of the given radius and standard deviation
3675 % (sigma). For reasonable results, radius should be larger than sigma. Use a
3676 % radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
3678 % The format of the UnsharpMaskImage method is:
3680 % Image *UnsharpMaskImage(const Image *image,const double radius,
3681 % const double sigma,const double amount,const double threshold,
3682 % ExceptionInfo *exception)
3684 % A description of each parameter follows:
3686 % o image: the image.
3688 % o radius: the radius of the Gaussian, in pixels, not counting the center
3691 % o sigma: the standard deviation of the Gaussian, in pixels.
3693 % o amount: the percentage of the difference between the original and the
3694 % blur image that is added back into the original.
3696 % o threshold: the threshold in pixels needed to apply the diffence amount.
3698 % o exception: return any errors or warnings in this structure.
3701 MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
3702 const double sigma,const double amount,const double threshold,
3703 ExceptionInfo *exception)
3705 #define SharpenImageTag "Sharpen/Image"
3726 assert(image != (const Image *) NULL);
3727 assert(image->signature == MagickSignature);
3728 if (image->debug != MagickFalse)
3729 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3730 assert(exception != (ExceptionInfo *) NULL);
3731 unsharp_image=BlurImage(image,radius,sigma,exception);
3732 if (unsharp_image == (Image *) NULL)
3733 return((Image *) NULL);
3734 quantum_threshold=(MagickRealType) QuantumRange*threshold;
3740 image_view=AcquireVirtualCacheView(image,exception);
3741 unsharp_view=AcquireAuthenticCacheView(unsharp_image,exception);
3742 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3743 #pragma omp parallel for schedule(static,4) shared(progress,status)
3745 for (y=0; y < (ssize_t) image->rows; y++)
3747 register const Quantum
3756 if (status == MagickFalse)
3758 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3759 q=QueueCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
3761 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3766 for (x=0; x < (ssize_t) image->columns; x++)
3771 if (GetPixelMask(image,p) != 0)
3773 p+=GetPixelChannels(image);
3774 q+=GetPixelChannels(unsharp_image);
3777 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3789 channel=GetPixelChannelMapChannel(image,i);
3790 traits=GetPixelChannelMapTraits(image,channel);
3791 unsharp_traits=GetPixelChannelMapTraits(unsharp_image,channel);
3792 if ((traits == UndefinedPixelTrait) ||
3793 (unsharp_traits == UndefinedPixelTrait))
3795 if ((unsharp_traits & CopyPixelTrait) != 0)
3797 SetPixelChannel(unsharp_image,channel,p[i],q);
3800 pixel=p[i]-(MagickRealType) GetPixelChannel(unsharp_image,channel,q);
3801 if (fabs(2.0*pixel) < quantum_threshold)
3802 pixel=(MagickRealType) p[i];
3804 pixel=(MagickRealType) p[i]+amount*pixel;
3805 SetPixelChannel(unsharp_image,channel,ClampToQuantum(pixel),q);
3807 p+=GetPixelChannels(image);
3808 q+=GetPixelChannels(unsharp_image);
3810 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
3812 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3817 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3818 #pragma omp critical (MagickCore_UnsharpMaskImage)
3820 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
3821 if (proceed == MagickFalse)
3825 unsharp_image->type=image->type;
3826 unsharp_view=DestroyCacheView(unsharp_view);
3827 image_view=DestroyCacheView(image_view);
3828 if (status == MagickFalse)
3829 unsharp_image=DestroyImage(unsharp_image);
3830 return(unsharp_image);