2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % EEEEE FFFFF FFFFF EEEEE CCCC TTTTT %
8 % EEE FFF FFF EEE C T %
10 % EEEEE F F EEEEE CCCC T %
13 % MagickCore Image Effects Methods %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/accelerate.h"
45 #include "MagickCore/blob.h"
46 #include "MagickCore/cache-view.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colorspace.h"
50 #include "MagickCore/constitute.h"
51 #include "MagickCore/decorate.h"
52 #include "MagickCore/distort.h"
53 #include "MagickCore/draw.h"
54 #include "MagickCore/enhance.h"
55 #include "MagickCore/exception.h"
56 #include "MagickCore/exception-private.h"
57 #include "MagickCore/effect.h"
58 #include "MagickCore/fx.h"
59 #include "MagickCore/gem.h"
60 #include "MagickCore/gem-private.h"
61 #include "MagickCore/geometry.h"
62 #include "MagickCore/image-private.h"
63 #include "MagickCore/list.h"
64 #include "MagickCore/log.h"
65 #include "MagickCore/memory_.h"
66 #include "MagickCore/monitor.h"
67 #include "MagickCore/monitor-private.h"
68 #include "MagickCore/montage.h"
69 #include "MagickCore/morphology.h"
70 #include "MagickCore/paint.h"
71 #include "MagickCore/pixel-accessor.h"
72 #include "MagickCore/pixel-private.h"
73 #include "MagickCore/property.h"
74 #include "MagickCore/quantize.h"
75 #include "MagickCore/quantum.h"
76 #include "MagickCore/quantum-private.h"
77 #include "MagickCore/random_.h"
78 #include "MagickCore/random-private.h"
79 #include "MagickCore/resample.h"
80 #include "MagickCore/resample-private.h"
81 #include "MagickCore/resize.h"
82 #include "MagickCore/resource_.h"
83 #include "MagickCore/segment.h"
84 #include "MagickCore/shear.h"
85 #include "MagickCore/signature-private.h"
86 #include "MagickCore/statistic.h"
87 #include "MagickCore/string_.h"
88 #include "MagickCore/thread-private.h"
89 #include "MagickCore/transform.h"
90 #include "MagickCore/threshold.h"
93 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97 % A d a p t i v e B l u r I m a g e %
101 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103 % AdaptiveBlurImage() adaptively blurs the image by blurring less
104 % intensely near image edges and more intensely far from edges. We blur the
105 % image with a Gaussian operator of the given radius and standard deviation
106 % (sigma). For reasonable results, radius should be larger than sigma. Use a
107 % radius of 0 and AdaptiveBlurImage() selects a suitable radius for you.
109 % The format of the AdaptiveBlurImage method is:
111 % Image *AdaptiveBlurImage(const Image *image,const double radius,
112 % const double sigma,ExceptionInfo *exception)
114 % A description of each parameter follows:
116 % o image: the image.
118 % o radius: the radius of the Gaussian, in pixels, not counting the center
121 % o sigma: the standard deviation of the Laplacian, in pixels.
123 % o exception: return any errors or warnings in this structure.
127 MagickExport MagickBooleanType AdaptiveLevelImage(Image *image,
128 const char *levels,ExceptionInfo *exception)
147 if (levels == (char *) NULL)
149 flags=ParseGeometry(levels,&geometry_info);
150 black_point=geometry_info.rho;
151 white_point=(double) QuantumRange;
152 if ((flags & SigmaValue) != 0)
153 white_point=geometry_info.sigma;
155 if ((flags & XiValue) != 0)
156 gamma=geometry_info.xi;
157 if ((flags & PercentValue) != 0)
159 black_point*=(double) image->columns*image->rows/100.0;
160 white_point*=(double) image->columns*image->rows/100.0;
162 if ((flags & SigmaValue) == 0)
163 white_point=(double) QuantumRange-black_point;
164 if ((flags & AspectValue ) == 0)
165 status=LevelImage(image,black_point,white_point,gamma,exception);
167 status=LevelizeImage(image,black_point,white_point,gamma,exception);
171 MagickExport Image *AdaptiveBlurImage(const Image *image,const double radius,
172 const double sigma,ExceptionInfo *exception)
174 #define AdaptiveBlurImageTag "Convolve/Image"
175 #define MagickSigma (fabs(sigma) < MagickEpsilon ? MagickEpsilon : sigma)
210 assert(image != (const Image *) NULL);
211 assert(image->signature == MagickSignature);
212 if (image->debug != MagickFalse)
213 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
214 assert(exception != (ExceptionInfo *) NULL);
215 assert(exception->signature == MagickSignature);
216 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
217 if (blur_image == (Image *) NULL)
218 return((Image *) NULL);
219 if (fabs(sigma) < MagickEpsilon)
221 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
223 blur_image=DestroyImage(blur_image);
224 return((Image *) NULL);
227 Edge detect the image brighness channel, level, blur, and level again.
229 edge_image=EdgeImage(image,radius,sigma,exception);
230 if (edge_image == (Image *) NULL)
232 blur_image=DestroyImage(blur_image);
233 return((Image *) NULL);
235 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
236 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
237 if (gaussian_image != (Image *) NULL)
239 edge_image=DestroyImage(edge_image);
240 edge_image=gaussian_image;
242 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
244 Create a set of kernels from maximum (radius,sigma) to minimum.
246 width=GetOptimalKernelWidth2D(radius,sigma);
247 kernel=(double **) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
248 if (kernel == (double **) NULL)
250 edge_image=DestroyImage(edge_image);
251 blur_image=DestroyImage(blur_image);
252 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
254 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
255 for (i=0; i < (ssize_t) width; i+=2)
257 kernel[i]=(double *) AcquireAlignedMemory((size_t) (width-i),(width-i)*
259 if (kernel[i] == (double *) NULL)
262 j=(ssize_t) (width-i)/2;
264 for (v=(-j); v <= j; v++)
266 for (u=(-j); u <= j; u++)
268 kernel[i][k]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
269 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
270 normalize+=kernel[i][k];
274 if (fabs(normalize) < MagickEpsilon)
275 normalize=MagickEpsilon;
276 normalize=MagickEpsilonReciprocal(normalize);
277 for (k=0; k < (j*j); k++)
278 kernel[i][k]=normalize*kernel[i][k];
280 if (i < (ssize_t) width)
282 for (i-=2; i >= 0; i-=2)
283 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
284 kernel=(double **) RelinquishAlignedMemory(kernel);
285 edge_image=DestroyImage(edge_image);
286 blur_image=DestroyImage(blur_image);
287 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
290 Adaptively blur image.
294 image_view=AcquireVirtualCacheView(image,exception);
295 edge_view=AcquireVirtualCacheView(edge_image,exception);
296 blur_view=AcquireAuthenticCacheView(blur_image,exception);
297 #if defined(MAGICKCORE_OPENMP_SUPPORT)
298 #pragma omp parallel for schedule(static,4) shared(progress,status) \
299 dynamic_number_threads(image,image->columns,image->rows,1)
301 for (y=0; y < (ssize_t) blur_image->rows; y++)
303 register const Quantum
312 if (status == MagickFalse)
314 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
315 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
317 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
322 for (x=0; x < (ssize_t) blur_image->columns; x++)
324 register const Quantum
334 j=(ssize_t) ceil((double) width*QuantumScale*
335 GetPixelIntensity(edge_image,r)-0.5);
339 if (j > (ssize_t) width)
343 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
344 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
345 if (p == (const Quantum *) NULL)
347 center=(ssize_t) GetPixelChannels(image)*(width-j)*((width-j)/2L)+
348 GetPixelChannels(image)*((width-j)/2L);
349 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
363 register const double
366 register const Quantum
375 channel=GetPixelChannelMapChannel(image,i);
376 traits=GetPixelChannelMapTraits(image,channel);
377 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
378 if ((traits == UndefinedPixelTrait) ||
379 (blur_traits == UndefinedPixelTrait))
381 if (((blur_traits & CopyPixelTrait) != 0) ||
382 (GetPixelMask(image,p) != 0))
384 SetPixelChannel(blur_image,channel,p[center+i],q);
391 if ((blur_traits & BlendPixelTrait) == 0)
396 for (v=0; v < (ssize_t) (width-j); v++)
398 for (u=0; u < (ssize_t) (width-j); u++)
400 pixel+=(*k)*pixels[i];
403 pixels+=GetPixelChannels(image);
406 gamma=MagickEpsilonReciprocal(gamma);
407 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
413 for (v=0; v < (ssize_t) (width-j); v++)
415 for (u=0; u < (ssize_t) (width-j); u++)
417 alpha=(double) (QuantumScale*GetPixelAlpha(image,pixels));
418 pixel+=(*k)*alpha*pixels[i];
421 pixels+=GetPixelChannels(image);
424 gamma=MagickEpsilonReciprocal(gamma);
425 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
427 q+=GetPixelChannels(blur_image);
428 r+=GetPixelChannels(edge_image);
430 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
432 if (image->progress_monitor != (MagickProgressMonitor) NULL)
437 #if defined(MAGICKCORE_OPENMP_SUPPORT)
438 #pragma omp critical (MagickCore_AdaptiveBlurImage)
440 proceed=SetImageProgress(image,AdaptiveBlurImageTag,progress++,
442 if (proceed == MagickFalse)
446 blur_image->type=image->type;
447 blur_view=DestroyCacheView(blur_view);
448 edge_view=DestroyCacheView(edge_view);
449 image_view=DestroyCacheView(image_view);
450 edge_image=DestroyImage(edge_image);
451 for (i=0; i < (ssize_t) width; i+=2)
452 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
453 kernel=(double **) RelinquishAlignedMemory(kernel);
454 if (status == MagickFalse)
455 blur_image=DestroyImage(blur_image);
460 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
464 % A d a p t i v e S h a r p e n I m a g e %
468 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
470 % AdaptiveSharpenImage() adaptively sharpens the image by sharpening more
471 % intensely near image edges and less intensely far from edges. We sharpen the
472 % image with a Gaussian operator of the given radius and standard deviation
473 % (sigma). For reasonable results, radius should be larger than sigma. Use a
474 % radius of 0 and AdaptiveSharpenImage() selects a suitable radius for you.
476 % The format of the AdaptiveSharpenImage method is:
478 % Image *AdaptiveSharpenImage(const Image *image,const double radius,
479 % const double sigma,ExceptionInfo *exception)
481 % A description of each parameter follows:
483 % o image: the image.
485 % o radius: the radius of the Gaussian, in pixels, not counting the center
488 % o sigma: the standard deviation of the Laplacian, in pixels.
490 % o exception: return any errors or warnings in this structure.
493 MagickExport Image *AdaptiveSharpenImage(const Image *image,const double radius,
494 const double sigma,ExceptionInfo *exception)
496 #define AdaptiveSharpenImageTag "Convolve/Image"
497 #define MagickSigma (fabs(sigma) < MagickEpsilon ? MagickEpsilon : sigma)
532 assert(image != (const Image *) NULL);
533 assert(image->signature == MagickSignature);
534 if (image->debug != MagickFalse)
535 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
536 assert(exception != (ExceptionInfo *) NULL);
537 assert(exception->signature == MagickSignature);
538 sharp_image=CloneImage(image,0,0,MagickTrue,exception);
539 if (sharp_image == (Image *) NULL)
540 return((Image *) NULL);
541 if (fabs(sigma) < MagickEpsilon)
543 if (SetImageStorageClass(sharp_image,DirectClass,exception) == MagickFalse)
545 sharp_image=DestroyImage(sharp_image);
546 return((Image *) NULL);
549 Edge detect the image brighness channel, level, sharp, and level again.
551 edge_image=EdgeImage(image,radius,sigma,exception);
552 if (edge_image == (Image *) NULL)
554 sharp_image=DestroyImage(sharp_image);
555 return((Image *) NULL);
557 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
558 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
559 if (gaussian_image != (Image *) NULL)
561 edge_image=DestroyImage(edge_image);
562 edge_image=gaussian_image;
564 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
566 Create a set of kernels from maximum (radius,sigma) to minimum.
568 width=GetOptimalKernelWidth2D(radius,sigma);
569 kernel=(double **) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
570 if (kernel == (double **) NULL)
572 edge_image=DestroyImage(edge_image);
573 sharp_image=DestroyImage(sharp_image);
574 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
576 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
577 for (i=0; i < (ssize_t) width; i+=2)
579 kernel[i]=(double *) AcquireAlignedMemory((size_t) (width-i),(width-i)*
581 if (kernel[i] == (double *) NULL)
584 j=(ssize_t) (width-i)/2;
586 for (v=(-j); v <= j; v++)
588 for (u=(-j); u <= j; u++)
590 kernel[i][k]=(double) (-exp(-((double) u*u+v*v)/(2.0*MagickSigma*
591 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
592 normalize+=kernel[i][k];
596 if (fabs(normalize) < MagickEpsilon)
597 normalize=MagickEpsilon;
598 normalize=MagickEpsilonReciprocal(normalize);
599 for (k=0; k < (j*j); k++)
600 kernel[i][k]=normalize*kernel[i][k];
602 if (i < (ssize_t) width)
604 for (i-=2; i >= 0; i-=2)
605 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
606 kernel=(double **) RelinquishAlignedMemory(kernel);
607 edge_image=DestroyImage(edge_image);
608 sharp_image=DestroyImage(sharp_image);
609 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
612 Adaptively sharpen image.
616 image_view=AcquireVirtualCacheView(image,exception);
617 edge_view=AcquireVirtualCacheView(edge_image,exception);
618 sharp_view=AcquireAuthenticCacheView(sharp_image,exception);
619 #if defined(MAGICKCORE_OPENMP_SUPPORT)
620 #pragma omp parallel for schedule(static,4) shared(progress,status) \
621 dynamic_number_threads(image,image->columns,image->rows,1)
623 for (y=0; y < (ssize_t) sharp_image->rows; y++)
625 register const Quantum
634 if (status == MagickFalse)
636 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
637 q=QueueCacheViewAuthenticPixels(sharp_view,0,y,sharp_image->columns,1,
639 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
644 for (x=0; x < (ssize_t) sharp_image->columns; x++)
646 register const Quantum
656 j=(ssize_t) ceil((double) width*QuantumScale*
657 GetPixelIntensity(edge_image,r)-0.5);
661 if (j > (ssize_t) width)
665 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
666 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
667 if (p == (const Quantum *) NULL)
669 center=(ssize_t) GetPixelChannels(image)*(width-j)*((width-j)/2L)+
670 GetPixelChannels(image)*((width-j)/2);
671 for (i=0; i < (ssize_t) GetPixelChannels(sharp_image); i++)
685 register const double
688 register const Quantum
697 channel=GetPixelChannelMapChannel(image,i);
698 traits=GetPixelChannelMapTraits(image,channel);
699 sharp_traits=GetPixelChannelMapTraits(sharp_image,channel);
700 if ((traits == UndefinedPixelTrait) ||
701 (sharp_traits == UndefinedPixelTrait))
703 if (((sharp_traits & CopyPixelTrait) != 0) ||
704 (GetPixelMask(image,p) != 0))
706 SetPixelChannel(sharp_image,channel,p[center+i],q);
713 if ((sharp_traits & BlendPixelTrait) == 0)
718 for (v=0; v < (ssize_t) (width-j); v++)
720 for (u=0; u < (ssize_t) (width-j); u++)
722 pixel+=(*k)*pixels[i];
725 pixels+=GetPixelChannels(image);
728 gamma=MagickEpsilonReciprocal(gamma);
729 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
735 for (v=0; v < (ssize_t) (width-j); v++)
737 for (u=0; u < (ssize_t) (width-j); u++)
739 alpha=(double) (QuantumScale*GetPixelAlpha(image,pixels));
740 pixel+=(*k)*alpha*pixels[i];
743 pixels+=GetPixelChannels(image);
746 gamma=MagickEpsilonReciprocal(gamma);
747 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
749 q+=GetPixelChannels(sharp_image);
750 r+=GetPixelChannels(edge_image);
752 if (SyncCacheViewAuthenticPixels(sharp_view,exception) == MagickFalse)
754 if (image->progress_monitor != (MagickProgressMonitor) NULL)
759 #if defined(MAGICKCORE_OPENMP_SUPPORT)
760 #pragma omp critical (MagickCore_AdaptiveSharpenImage)
762 proceed=SetImageProgress(image,AdaptiveSharpenImageTag,progress++,
764 if (proceed == MagickFalse)
768 sharp_image->type=image->type;
769 sharp_view=DestroyCacheView(sharp_view);
770 edge_view=DestroyCacheView(edge_view);
771 image_view=DestroyCacheView(image_view);
772 edge_image=DestroyImage(edge_image);
773 for (i=0; i < (ssize_t) width; i+=2)
774 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
775 kernel=(double **) RelinquishAlignedMemory(kernel);
776 if (status == MagickFalse)
777 sharp_image=DestroyImage(sharp_image);
782 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
786 % B l u r I m a g e %
790 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
792 % BlurImage() blurs an image. We convolve the image with a Gaussian operator
793 % of the given radius and standard deviation (sigma). For reasonable results,
794 % the radius should be larger than sigma. Use a radius of 0 and BlurImage()
795 % selects a suitable radius for you.
797 % BlurImage() differs from GaussianBlurImage() in that it uses a separable
798 % kernel which is faster but mathematically equivalent to the non-separable
801 % The format of the BlurImage method is:
803 % Image *BlurImage(const Image *image,const double radius,
804 % const double sigma,ExceptionInfo *exception)
806 % A description of each parameter follows:
808 % o image: the image.
810 % o radius: the radius of the Gaussian, in pixels, not counting the center
813 % o sigma: the standard deviation of the Gaussian, in pixels.
815 % o exception: return any errors or warnings in this structure.
819 static double *GetBlurKernel(const size_t width,const double sigma)
833 Generate a 1-D convolution kernel.
835 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
836 kernel=(double *) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
837 if (kernel == (double *) NULL)
842 for (k=(-j); k <= j; k++)
844 kernel[i]=(double) (exp(-((double) k*k)/(2.0*MagickSigma*MagickSigma))/
845 (MagickSQ2PI*MagickSigma));
846 normalize+=kernel[i];
849 for (i=0; i < (ssize_t) width; i++)
850 kernel[i]/=normalize;
854 MagickExport Image *BlurImage(const Image *image,const double radius,
855 const double sigma,ExceptionInfo *exception)
857 #define BlurImageTag "Blur/Image"
887 Initialize blur image attributes.
889 assert(image != (Image *) NULL);
890 assert(image->signature == MagickSignature);
891 if (image->debug != MagickFalse)
892 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
893 assert(exception != (ExceptionInfo *) NULL);
894 assert(exception->signature == MagickSignature);
895 blur_image=CloneImage(image,0,0,MagickTrue,exception);
896 if (blur_image == (Image *) NULL)
897 return((Image *) NULL);
898 if (fabs(sigma) < MagickEpsilon)
900 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
902 blur_image=DestroyImage(blur_image);
903 return((Image *) NULL);
905 width=GetOptimalKernelWidth1D(radius,sigma);
906 kernel=GetBlurKernel(width,sigma);
907 if (kernel == (double *) NULL)
909 blur_image=DestroyImage(blur_image);
910 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
912 if (image->debug != MagickFalse)
915 format[MaxTextExtent],
918 register const double
921 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
922 " blur image with kernel width %.20g:",(double) width);
923 message=AcquireString("");
925 for (i=0; i < (ssize_t) width; i++)
928 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) i);
929 (void) ConcatenateString(&message,format);
930 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
931 (void) ConcatenateString(&message,format);
932 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
934 message=DestroyString(message);
941 center=(ssize_t) GetPixelChannels(image)*(width/2L);
942 image_view=AcquireVirtualCacheView(image,exception);
943 blur_view=AcquireAuthenticCacheView(blur_image,exception);
944 #if defined(MAGICKCORE_OPENMP_SUPPORT)
945 #pragma omp parallel for schedule(static,4) shared(progress,status) \
946 dynamic_number_threads(image,image->columns,image->rows,1)
948 for (y=0; y < (ssize_t) image->rows; y++)
950 register const Quantum
959 if (status == MagickFalse)
961 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y,
962 image->columns+width,1,exception);
963 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
965 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
970 for (x=0; x < (ssize_t) image->columns; x++)
975 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
989 register const double
992 register const Quantum
998 channel=GetPixelChannelMapChannel(image,i);
999 traits=GetPixelChannelMapTraits(image,channel);
1000 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
1001 if ((traits == UndefinedPixelTrait) ||
1002 (blur_traits == UndefinedPixelTrait))
1004 if (((blur_traits & CopyPixelTrait) != 0) ||
1005 (GetPixelMask(image,p) != 0))
1007 SetPixelChannel(blur_image,channel,p[center+i],q);
1013 if ((blur_traits & BlendPixelTrait) == 0)
1018 for (u=0; u < (ssize_t) width; u++)
1020 pixel+=(*k)*pixels[i];
1022 pixels+=GetPixelChannels(image);
1024 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
1031 for (u=0; u < (ssize_t) width; u++)
1033 alpha=(double) (QuantumScale*GetPixelAlpha(image,pixels));
1034 pixel+=(*k)*alpha*pixels[i];
1037 pixels+=GetPixelChannels(image);
1039 gamma=MagickEpsilonReciprocal(gamma);
1040 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
1042 p+=GetPixelChannels(image);
1043 q+=GetPixelChannels(blur_image);
1045 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1047 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1052 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1053 #pragma omp critical (MagickCore_BlurImage)
1055 proceed=SetImageProgress(image,BlurImageTag,progress++,blur_image->rows+
1056 blur_image->columns);
1057 if (proceed == MagickFalse)
1061 blur_view=DestroyCacheView(blur_view);
1062 image_view=DestroyCacheView(image_view);
1066 center=(ssize_t) GetPixelChannels(blur_image)*(width/2L);
1067 image_view=AcquireVirtualCacheView(blur_image,exception);
1068 blur_view=AcquireAuthenticCacheView(blur_image,exception);
1069 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1070 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1071 dynamic_number_threads(image,image->columns,image->rows,1)
1073 for (x=0; x < (ssize_t) blur_image->columns; x++)
1075 register const Quantum
1084 if (status == MagickFalse)
1086 p=GetCacheViewVirtualPixels(image_view,x,-((ssize_t) width/2L),1,
1087 blur_image->rows+width,exception);
1088 q=GetCacheViewAuthenticPixels(blur_view,x,0,1,blur_image->rows,exception);
1089 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1094 for (y=0; y < (ssize_t) blur_image->rows; y++)
1099 for (i=0; i < (ssize_t) GetPixelChannels(blur_image); i++)
1113 register const double
1116 register const Quantum
1122 channel=GetPixelChannelMapChannel(blur_image,i);
1123 traits=GetPixelChannelMapTraits(blur_image,channel);
1124 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
1125 if ((traits == UndefinedPixelTrait) ||
1126 (blur_traits == UndefinedPixelTrait))
1128 if (((blur_traits & CopyPixelTrait) != 0) ||
1129 (GetPixelMask(image,p) != 0))
1131 SetPixelChannel(blur_image,channel,p[center+i],q);
1137 if ((blur_traits & BlendPixelTrait) == 0)
1142 for (u=0; u < (ssize_t) width; u++)
1144 pixel+=(*k)*pixels[i];
1146 pixels+=GetPixelChannels(blur_image);
1148 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
1155 for (u=0; u < (ssize_t) width; u++)
1157 alpha=(double) (QuantumScale*GetPixelAlpha(blur_image,
1159 pixel+=(*k)*alpha*pixels[i];
1162 pixels+=GetPixelChannels(blur_image);
1164 gamma=MagickEpsilonReciprocal(gamma);
1165 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
1167 p+=GetPixelChannels(blur_image);
1168 q+=GetPixelChannels(blur_image);
1170 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1172 if (blur_image->progress_monitor != (MagickProgressMonitor) NULL)
1177 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1178 #pragma omp critical (MagickCore_BlurImage)
1180 proceed=SetImageProgress(blur_image,BlurImageTag,progress++,
1181 blur_image->rows+blur_image->columns);
1182 if (proceed == MagickFalse)
1186 blur_view=DestroyCacheView(blur_view);
1187 image_view=DestroyCacheView(image_view);
1188 kernel=(double *) RelinquishAlignedMemory(kernel);
1189 blur_image->type=image->type;
1190 if (status == MagickFalse)
1191 blur_image=DestroyImage(blur_image);
1196 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1200 % C o n v o l v e I m a g e %
1204 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1206 % ConvolveImage() applies a custom convolution kernel to the image.
1208 % The format of the ConvolveImage method is:
1210 % Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
1211 % ExceptionInfo *exception)
1213 % A description of each parameter follows:
1215 % o image: the image.
1217 % o kernel: the filtering kernel.
1219 % o exception: return any errors or warnings in this structure.
1222 MagickExport Image *ConvolveImage(const Image *image,
1223 const KernelInfo *kernel_info,ExceptionInfo *exception)
1225 return(MorphologyImage(image,CorrelateMorphology,1,kernel_info,exception));
1229 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1233 % D e s p e c k l e I m a g e %
1237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1239 % DespeckleImage() reduces the speckle noise in an image while perserving the
1240 % edges of the original image. A speckle removing filter uses a complementary % hulling technique (raising pixels that are darker than their surrounding
1241 % neighbors, then complementarily lowering pixels that are brighter than their
1242 % surrounding neighbors) to reduce the speckle index of that image (reference
1243 % Crimmins speckle removal).
1245 % The format of the DespeckleImage method is:
1247 % Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1249 % A description of each parameter follows:
1251 % o image: the image.
1253 % o exception: return any errors or warnings in this structure.
1257 static void Hull(const Image *image,const ssize_t x_offset,
1258 const ssize_t y_offset,const size_t columns,const size_t rows,
1259 const int polarity,Quantum *restrict f,Quantum *restrict g)
1270 assert(f != (Quantum *) NULL);
1271 assert(g != (Quantum *) NULL);
1274 r=p+(y_offset*(columns+2)+x_offset);
1275 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1276 #pragma omp parallel for schedule(static) \
1277 dynamic_number_threads(image,columns,rows,1)
1279 for (y=0; y < (ssize_t) rows; y++)
1288 i=(2*y+1)+y*columns;
1290 for (x=0; x < (ssize_t) columns; x++)
1292 v=(SignedQuantum) p[i];
1293 if ((SignedQuantum) r[i] >= (v+ScaleCharToQuantum(2)))
1294 v+=ScaleCharToQuantum(1);
1299 for (x=0; x < (ssize_t) columns; x++)
1301 v=(SignedQuantum) p[i];
1302 if ((SignedQuantum) r[i] <= (v-ScaleCharToQuantum(2)))
1303 v-=ScaleCharToQuantum(1);
1310 r=q+(y_offset*(columns+2)+x_offset);
1311 s=q-(y_offset*(columns+2)+x_offset);
1312 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1313 #pragma omp parallel for schedule(static) \
1314 dynamic_number_threads(image,columns,rows,1)
1316 for (y=0; y < (ssize_t) rows; y++)
1325 i=(2*y+1)+y*columns;
1327 for (x=0; x < (ssize_t) columns; x++)
1329 v=(SignedQuantum) q[i];
1330 if (((SignedQuantum) s[i] >= (v+ScaleCharToQuantum(2))) &&
1331 ((SignedQuantum) r[i] > v))
1332 v+=ScaleCharToQuantum(1);
1337 for (x=0; x < (ssize_t) columns; x++)
1339 v=(SignedQuantum) q[i];
1340 if (((SignedQuantum) s[i] <= (v-ScaleCharToQuantum(2))) &&
1341 ((SignedQuantum) r[i] < v))
1342 v-=ScaleCharToQuantum(1);
1349 MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1351 #define DespeckleImageTag "Despeckle/Image"
1373 static const ssize_t
1374 X[4] = {0, 1, 1,-1},
1375 Y[4] = {1, 0, 1, 1};
1378 Allocate despeckled image.
1380 assert(image != (const Image *) NULL);
1381 assert(image->signature == MagickSignature);
1382 if (image->debug != MagickFalse)
1383 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1384 assert(exception != (ExceptionInfo *) NULL);
1385 assert(exception->signature == MagickSignature);
1386 despeckle_image=CloneImage(image,0,0,MagickTrue,exception);
1387 if (despeckle_image == (Image *) NULL)
1388 return((Image *) NULL);
1389 status=SetImageStorageClass(despeckle_image,DirectClass,exception);
1390 if (status == MagickFalse)
1392 despeckle_image=DestroyImage(despeckle_image);
1393 return((Image *) NULL);
1396 Allocate image buffer.
1398 length=(size_t) ((image->columns+2)*(image->rows+2));
1399 pixels=(Quantum *) AcquireQuantumMemory(length,sizeof(*pixels));
1400 buffer=(Quantum *) AcquireQuantumMemory(length,sizeof(*buffer));
1401 if ((pixels == (Quantum *) NULL) || (buffer == (Quantum *) NULL))
1403 if (buffer != (Quantum *) NULL)
1404 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1405 if (pixels != (Quantum *) NULL)
1406 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1407 despeckle_image=DestroyImage(despeckle_image);
1408 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1411 Reduce speckle in the image.
1414 image_view=AcquireVirtualCacheView(image,exception);
1415 despeckle_view=AcquireAuthenticCacheView(despeckle_image,exception);
1416 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1433 if (status == MagickFalse)
1435 channel=GetPixelChannelMapChannel(image,i);
1436 traits=GetPixelChannelMapTraits(image,channel);
1437 despeckle_traits=GetPixelChannelMapTraits(despeckle_image,channel);
1438 if ((traits == UndefinedPixelTrait) ||
1439 (despeckle_traits == UndefinedPixelTrait))
1441 if ((despeckle_traits & CopyPixelTrait) != 0)
1443 (void) ResetMagickMemory(pixels,0,length*sizeof(*pixels));
1444 j=(ssize_t) image->columns+2;
1445 for (y=0; y < (ssize_t) image->rows; y++)
1447 register const Quantum
1450 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1451 if (p == (const Quantum *) NULL)
1457 for (x=0; x < (ssize_t) image->columns; x++)
1460 p+=GetPixelChannels(image);
1464 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
1465 for (k=0; k < 4; k++)
1467 Hull(image,X[k],Y[k],image->columns,image->rows,1,pixels,buffer);
1468 Hull(image,-X[k],-Y[k],image->columns,image->rows,1,pixels,buffer);
1469 Hull(image,-X[k],-Y[k],image->columns,image->rows,-1,pixels,buffer);
1470 Hull(image,X[k],Y[k],image->columns,image->rows,-1,pixels,buffer);
1472 j=(ssize_t) image->columns+2;
1473 for (y=0; y < (ssize_t) image->rows; y++)
1481 q=QueueCacheViewAuthenticPixels(despeckle_view,0,y,
1482 despeckle_image->columns,1,exception);
1483 if (q == (Quantum *) NULL)
1489 for (x=0; x < (ssize_t) image->columns; x++)
1491 SetPixelChannel(despeckle_image,channel,pixels[j++],q);
1492 q+=GetPixelChannels(despeckle_image);
1494 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1495 if (sync == MagickFalse)
1499 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1504 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1505 GetPixelChannels(image));
1506 if (proceed == MagickFalse)
1510 despeckle_view=DestroyCacheView(despeckle_view);
1511 image_view=DestroyCacheView(image_view);
1512 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1513 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1514 despeckle_image->type=image->type;
1515 if (status == MagickFalse)
1516 despeckle_image=DestroyImage(despeckle_image);
1517 return(despeckle_image);
1521 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1525 % E d g e I m a g e %
1529 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1531 % EdgeImage() finds edges in an image. Radius defines the radius of the
1532 % convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1535 % The format of the EdgeImage method is:
1537 % Image *EdgeImage(const Image *image,const double radius,
1538 % const double sigma,ExceptionInfo *exception)
1540 % A description of each parameter follows:
1542 % o image: the image.
1544 % o radius: the radius of the pixel neighborhood.
1546 % o sigma: the standard deviation of the Gaussian, in pixels.
1548 % o exception: return any errors or warnings in this structure.
1551 MagickExport Image *EdgeImage(const Image *image,const double radius,
1552 const double sigma,ExceptionInfo *exception)
1571 assert(image != (const Image *) NULL);
1572 assert(image->signature == MagickSignature);
1573 if (image->debug != MagickFalse)
1574 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1575 assert(exception != (ExceptionInfo *) NULL);
1576 assert(exception->signature == MagickSignature);
1577 width=GetOptimalKernelWidth1D(radius,sigma);
1578 kernel_info=AcquireKernelInfo((const char *) NULL);
1579 if (kernel_info == (KernelInfo *) NULL)
1580 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1581 kernel_info->width=width;
1582 kernel_info->height=width;
1583 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1584 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1585 if (kernel_info->values == (MagickRealType *) NULL)
1587 kernel_info=DestroyKernelInfo(kernel_info);
1588 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1590 j=(ssize_t) kernel_info->width/2;
1592 for (v=(-j); v <= j; v++)
1594 for (u=(-j); u <= j; u++)
1596 kernel_info->values[i]=(MagickRealType) (-1.0);
1600 kernel_info->values[i/2]=(MagickRealType) (width*width-1.0);
1601 edge_image=ConvolveImage(image,kernel_info,exception);
1602 if (edge_image != (Image *) NULL)
1603 (void) ClampImage(edge_image,exception);
1604 kernel_info=DestroyKernelInfo(kernel_info);
1609 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1613 % E m b o s s I m a g e %
1617 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1619 % EmbossImage() returns a grayscale image with a three-dimensional effect.
1620 % We convolve the image with a Gaussian operator of the given radius and
1621 % standard deviation (sigma). For reasonable results, radius should be
1622 % larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1625 % The format of the EmbossImage method is:
1627 % Image *EmbossImage(const Image *image,const double radius,
1628 % const double sigma,ExceptionInfo *exception)
1630 % A description of each parameter follows:
1632 % o image: the image.
1634 % o radius: the radius of the pixel neighborhood.
1636 % o sigma: the standard deviation of the Gaussian, in pixels.
1638 % o exception: return any errors or warnings in this structure.
1641 MagickExport Image *EmbossImage(const Image *image,const double radius,
1642 const double sigma,ExceptionInfo *exception)
1662 assert(image != (const Image *) NULL);
1663 assert(image->signature == MagickSignature);
1664 if (image->debug != MagickFalse)
1665 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1666 assert(exception != (ExceptionInfo *) NULL);
1667 assert(exception->signature == MagickSignature);
1668 width=GetOptimalKernelWidth1D(radius,sigma);
1669 kernel_info=AcquireKernelInfo((const char *) NULL);
1670 if (kernel_info == (KernelInfo *) NULL)
1671 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1672 kernel_info->width=width;
1673 kernel_info->height=width;
1674 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1675 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1676 if (kernel_info->values == (MagickRealType *) NULL)
1678 kernel_info=DestroyKernelInfo(kernel_info);
1679 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1681 j=(ssize_t) kernel_info->width/2;
1684 for (v=(-j); v <= j; v++)
1686 for (u=(-j); u <= j; u++)
1688 kernel_info->values[i]=(MagickRealType) (((u < 0) || (v < 0) ? -8.0 :
1689 8.0)*exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
1690 (2.0*MagickPI*MagickSigma*MagickSigma));
1692 kernel_info->values[i]=0.0;
1697 emboss_image=ConvolveImage(image,kernel_info,exception);
1698 kernel_info=DestroyKernelInfo(kernel_info);
1699 if (emboss_image != (Image *) NULL)
1700 (void) EqualizeImage(emboss_image,exception);
1701 return(emboss_image);
1705 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1709 % G a u s s i a n B l u r I m a g e %
1713 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1715 % GaussianBlurImage() blurs an image. We convolve the image with a
1716 % Gaussian operator of the given radius and standard deviation (sigma).
1717 % For reasonable results, the radius should be larger than sigma. Use a
1718 % radius of 0 and GaussianBlurImage() selects a suitable radius for you
1720 % The format of the GaussianBlurImage method is:
1722 % Image *GaussianBlurImage(const Image *image,onst double radius,
1723 % const double sigma,ExceptionInfo *exception)
1725 % A description of each parameter follows:
1727 % o image: the image.
1729 % o radius: the radius of the Gaussian, in pixels, not counting the center
1732 % o sigma: the standard deviation of the Gaussian, in pixels.
1734 % o exception: return any errors or warnings in this structure.
1737 MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
1738 const double sigma,ExceptionInfo *exception)
1757 assert(image != (const Image *) NULL);
1758 assert(image->signature == MagickSignature);
1759 if (image->debug != MagickFalse)
1760 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1761 assert(exception != (ExceptionInfo *) NULL);
1762 assert(exception->signature == MagickSignature);
1763 width=GetOptimalKernelWidth2D(radius,sigma);
1764 kernel_info=AcquireKernelInfo((const char *) NULL);
1765 if (kernel_info == (KernelInfo *) NULL)
1766 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1767 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1768 kernel_info->width=width;
1769 kernel_info->height=width;
1770 kernel_info->signature=MagickSignature;
1771 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1772 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1773 if (kernel_info->values == (MagickRealType *) NULL)
1775 kernel_info=DestroyKernelInfo(kernel_info);
1776 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1778 j=(ssize_t) kernel_info->width/2;
1780 for (v=(-j); v <= j; v++)
1782 for (u=(-j); u <= j; u++)
1784 kernel_info->values[i]=(MagickRealType) (exp(-((double) u*u+v*v)/(2.0*
1785 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
1789 blur_image=ConvolveImage(image,kernel_info,exception);
1790 kernel_info=DestroyKernelInfo(kernel_info);
1795 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1799 % M o t i o n B l u r I m a g e %
1803 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1805 % MotionBlurImage() simulates motion blur. We convolve the image with a
1806 % Gaussian operator of the given radius and standard deviation (sigma).
1807 % For reasonable results, radius should be larger than sigma. Use a
1808 % radius of 0 and MotionBlurImage() selects a suitable radius for you.
1809 % Angle gives the angle of the blurring motion.
1811 % Andrew Protano contributed this effect.
1813 % The format of the MotionBlurImage method is:
1815 % Image *MotionBlurImage(const Image *image,const double radius,
1816 % const double sigma,const double angle,ExceptionInfo *exception)
1818 % A description of each parameter follows:
1820 % o image: the image.
1822 % o radius: the radius of the Gaussian, in pixels, not counting
1825 % o sigma: the standard deviation of the Gaussian, in pixels.
1827 % o angle: Apply the effect along this angle.
1829 % o exception: return any errors or warnings in this structure.
1833 static double *GetMotionBlurKernel(const size_t width,const double sigma)
1843 Generate a 1-D convolution kernel.
1845 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1846 kernel=(double *) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
1847 if (kernel == (double *) NULL)
1850 for (i=0; i < (ssize_t) width; i++)
1852 kernel[i]=(double) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
1853 MagickSigma)))/(MagickSQ2PI*MagickSigma));
1854 normalize+=kernel[i];
1856 for (i=0; i < (ssize_t) width; i++)
1857 kernel[i]/=normalize;
1861 MagickExport Image *MotionBlurImage(const Image *image,const double radius,
1862 const double sigma,const double angle,ExceptionInfo *exception)
1896 assert(image != (Image *) NULL);
1897 assert(image->signature == MagickSignature);
1898 if (image->debug != MagickFalse)
1899 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1900 assert(exception != (ExceptionInfo *) NULL);
1901 width=GetOptimalKernelWidth1D(radius,sigma);
1902 kernel=GetMotionBlurKernel(width,sigma);
1903 if (kernel == (double *) NULL)
1904 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1905 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
1906 if (offset == (OffsetInfo *) NULL)
1908 kernel=(double *) RelinquishAlignedMemory(kernel);
1909 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1911 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1912 if (blur_image == (Image *) NULL)
1914 kernel=(double *) RelinquishAlignedMemory(kernel);
1915 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1916 return((Image *) NULL);
1918 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
1920 kernel=(double *) RelinquishAlignedMemory(kernel);
1921 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1922 blur_image=DestroyImage(blur_image);
1923 return((Image *) NULL);
1925 point.x=(double) width*sin(DegreesToRadians(angle));
1926 point.y=(double) width*cos(DegreesToRadians(angle));
1927 for (i=0; i < (ssize_t) width; i++)
1929 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
1930 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
1937 image_view=AcquireVirtualCacheView(image,exception);
1938 motion_view=AcquireVirtualCacheView(image,exception);
1939 blur_view=AcquireAuthenticCacheView(blur_image,exception);
1940 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1941 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1942 dynamic_number_threads(image,image->columns,image->rows,1)
1944 for (y=0; y < (ssize_t) image->rows; y++)
1946 register const Quantum
1955 if (status == MagickFalse)
1957 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1958 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
1960 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1965 for (x=0; x < (ssize_t) image->columns; x++)
1970 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1984 register const Quantum
1993 channel=GetPixelChannelMapChannel(image,i);
1994 traits=GetPixelChannelMapTraits(image,channel);
1995 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
1996 if ((traits == UndefinedPixelTrait) ||
1997 (blur_traits == UndefinedPixelTrait))
1999 if (((blur_traits & CopyPixelTrait) != 0) ||
2000 (GetPixelMask(image,p) != 0))
2002 SetPixelChannel(blur_image,channel,p[i],q);
2007 if ((blur_traits & BlendPixelTrait) == 0)
2009 for (j=0; j < (ssize_t) width; j++)
2011 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+
2012 offset[j].y,1,1,exception);
2013 if (r == (const Quantum *) NULL)
2021 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
2026 for (j=0; j < (ssize_t) width; j++)
2028 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+offset[j].y,1,
2030 if (r == (const Quantum *) NULL)
2035 alpha=(double) (QuantumScale*GetPixelAlpha(image,r));
2036 pixel+=(*k)*alpha*r[i];
2040 gamma=MagickEpsilonReciprocal(gamma);
2041 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2043 p+=GetPixelChannels(image);
2044 q+=GetPixelChannels(blur_image);
2046 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2048 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2053 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2054 #pragma omp critical (MagickCore_MotionBlurImage)
2056 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2057 if (proceed == MagickFalse)
2061 blur_view=DestroyCacheView(blur_view);
2062 motion_view=DestroyCacheView(motion_view);
2063 image_view=DestroyCacheView(image_view);
2064 kernel=(double *) RelinquishAlignedMemory(kernel);
2065 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2066 if (status == MagickFalse)
2067 blur_image=DestroyImage(blur_image);
2072 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2076 % P r e v i e w I m a g e %
2080 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2082 % PreviewImage() tiles 9 thumbnails of the specified image with an image
2083 % processing operation applied with varying parameters. This may be helpful
2084 % pin-pointing an appropriate parameter for a particular image processing
2087 % The format of the PreviewImages method is:
2089 % Image *PreviewImages(const Image *image,const PreviewType preview,
2090 % ExceptionInfo *exception)
2092 % A description of each parameter follows:
2094 % o image: the image.
2096 % o preview: the image processing operation.
2098 % o exception: return any errors or warnings in this structure.
2101 MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2102 ExceptionInfo *exception)
2104 #define NumberTiles 9
2105 #define PreviewImageTag "Preview/Image"
2106 #define DefaultPreviewGeometry "204x204+10+10"
2109 factor[MaxTextExtent],
2110 label[MaxTextExtent];
2155 Open output image file.
2157 assert(image != (Image *) NULL);
2158 assert(image->signature == MagickSignature);
2159 if (image->debug != MagickFalse)
2160 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2164 preview_info=AcquireImageInfo();
2165 SetGeometry(image,&geometry);
2166 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2167 &geometry.width,&geometry.height);
2168 images=NewImageList();
2170 GetQuantizeInfo(&quantize_info);
2176 for (i=0; i < NumberTiles; i++)
2178 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2179 if (thumbnail == (Image *) NULL)
2181 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2183 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel,exception);
2184 if (i == (NumberTiles/2))
2186 (void) QueryColorCompliance("#dfdfdf",AllCompliance,
2187 &thumbnail->matte_color,exception);
2188 AppendImageToList(&images,thumbnail);
2196 preview_image=RotateImage(thumbnail,degrees,exception);
2197 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
2203 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
2204 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",
2205 degrees,2.0*degrees);
2210 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2211 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
2212 preview_image=RollImage(thumbnail,x,y,exception);
2213 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
2214 (double) x,(double) y);
2219 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2220 if (preview_image == (Image *) NULL)
2222 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",
2224 (void) ModulateImage(preview_image,factor,exception);
2225 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2228 case SaturationPreview:
2230 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2231 if (preview_image == (Image *) NULL)
2233 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",
2235 (void) ModulateImage(preview_image,factor,exception);
2236 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2239 case BrightnessPreview:
2241 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2242 if (preview_image == (Image *) NULL)
2244 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
2245 (void) ModulateImage(preview_image,factor,exception);
2246 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2252 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2253 if (preview_image == (Image *) NULL)
2256 (void) GammaImage(preview_image,gamma,exception);
2257 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
2262 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2263 if (preview_image != (Image *) NULL)
2264 for (x=0; x < i; x++)
2265 (void) ContrastImage(preview_image,MagickTrue,exception);
2266 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
2272 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2273 if (preview_image == (Image *) NULL)
2275 for (x=0; x < i; x++)
2276 (void) ContrastImage(preview_image,MagickFalse,exception);
2277 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
2281 case GrayscalePreview:
2283 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2284 if (preview_image == (Image *) NULL)
2287 quantize_info.number_colors=colors;
2288 quantize_info.colorspace=GRAYColorspace;
2289 (void) QuantizeImage(&quantize_info,preview_image,exception);
2290 (void) FormatLocaleString(label,MaxTextExtent,
2291 "-colorspace gray -colors %.20g",(double) colors);
2294 case QuantizePreview:
2296 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2297 if (preview_image == (Image *) NULL)
2300 quantize_info.number_colors=colors;
2301 (void) QuantizeImage(&quantize_info,preview_image,exception);
2302 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
2306 case DespecklePreview:
2308 for (x=0; x < (i-1); x++)
2310 preview_image=DespeckleImage(thumbnail,exception);
2311 if (preview_image == (Image *) NULL)
2313 thumbnail=DestroyImage(thumbnail);
2314 thumbnail=preview_image;
2316 preview_image=DespeckleImage(thumbnail,exception);
2317 if (preview_image == (Image *) NULL)
2319 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
2323 case ReduceNoisePreview:
2325 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2326 (size_t) radius,exception);
2327 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
2330 case AddNoisePreview:
2336 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2341 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2346 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2351 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2356 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2361 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2366 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2370 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2371 (size_t) i,exception);
2372 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
2375 case SharpenPreview:
2377 preview_image=SharpenImage(thumbnail,radius,sigma,exception);
2378 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",
2384 preview_image=BlurImage(thumbnail,radius,sigma,exception);
2385 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
2389 case ThresholdPreview:
2391 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2392 if (preview_image == (Image *) NULL)
2394 (void) BilevelImage(thumbnail,(double) (percentage*((double)
2395 QuantumRange+1.0))/100.0,exception);
2396 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",
2397 (double) (percentage*((double) QuantumRange+1.0))/100.0);
2400 case EdgeDetectPreview:
2402 preview_image=EdgeImage(thumbnail,radius,sigma,exception);
2403 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
2408 preview_image=SpreadImage(thumbnail,radius,thumbnail->interpolate,
2410 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",
2414 case SolarizePreview:
2416 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2417 if (preview_image == (Image *) NULL)
2419 (void) SolarizeImage(preview_image,(double) QuantumRange*
2420 percentage/100.0,exception);
2421 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
2422 (QuantumRange*percentage)/100.0);
2428 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2430 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",
2436 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2437 if (preview_image == (Image *) NULL)
2439 geometry.width=(size_t) (2*i+2);
2440 geometry.height=(size_t) (2*i+2);
2443 (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
2444 (void) FormatLocaleString(label,MaxTextExtent,
2445 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
2446 geometry.height,(double) geometry.x,(double) geometry.y);
2449 case SegmentPreview:
2451 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2452 if (preview_image == (Image *) NULL)
2455 (void) SegmentImage(preview_image,sRGBColorspace,MagickFalse,threshold,
2456 threshold,exception);
2457 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
2458 threshold,threshold);
2463 preview_image=SwirlImage(thumbnail,degrees,image->interpolate,
2465 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
2469 case ImplodePreview:
2472 preview_image=ImplodeImage(thumbnail,degrees,image->interpolate,
2474 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
2480 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,
2481 image->interpolate,exception);
2482 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",
2483 0.5*degrees,2.0*degrees);
2486 case OilPaintPreview:
2488 preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2490 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2494 case CharcoalDrawingPreview:
2496 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
2498 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2505 filename[MaxTextExtent];
2513 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2514 if (preview_image == (Image *) NULL)
2516 preview_info->quality=(size_t) percentage;
2517 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
2518 preview_info->quality);
2519 file=AcquireUniqueFileResource(filename);
2522 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
2523 "jpeg:%s",filename);
2524 status=WriteImage(preview_info,preview_image,exception);
2525 if (status != MagickFalse)
2530 (void) CopyMagickString(preview_info->filename,
2531 preview_image->filename,MaxTextExtent);
2532 quality_image=ReadImage(preview_info,exception);
2533 if (quality_image != (Image *) NULL)
2535 preview_image=DestroyImage(preview_image);
2536 preview_image=quality_image;
2539 (void) RelinquishUniqueFileResource(preview_image->filename);
2540 if ((GetBlobSize(preview_image)/1024) >= 1024)
2541 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
2542 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2545 if (GetBlobSize(preview_image) >= 1024)
2546 (void) FormatLocaleString(label,MaxTextExtent,
2547 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
2548 GetBlobSize(preview_image))/1024.0);
2550 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
2551 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
2555 thumbnail=DestroyImage(thumbnail);
2559 if (preview_image == (Image *) NULL)
2561 (void) DeleteImageProperty(preview_image,"label");
2562 (void) SetImageProperty(preview_image,"label",label,exception);
2563 AppendImageToList(&images,preview_image);
2564 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2566 if (proceed == MagickFalse)
2569 if (images == (Image *) NULL)
2571 preview_info=DestroyImageInfo(preview_info);
2572 return((Image *) NULL);
2577 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2578 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2579 montage_info->shadow=MagickTrue;
2580 (void) CloneString(&montage_info->tile,"3x3");
2581 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2582 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2583 montage_image=MontageImages(images,montage_info,exception);
2584 montage_info=DestroyMontageInfo(montage_info);
2585 images=DestroyImageList(images);
2586 if (montage_image == (Image *) NULL)
2587 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2588 if (montage_image->montage != (char *) NULL)
2591 Free image directory.
2593 montage_image->montage=(char *) RelinquishMagickMemory(
2594 montage_image->montage);
2595 if (image->directory != (char *) NULL)
2596 montage_image->directory=(char *) RelinquishMagickMemory(
2597 montage_image->directory);
2599 preview_info=DestroyImageInfo(preview_info);
2600 return(montage_image);
2604 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2608 % R a d i a l B l u r I m a g e %
2612 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2614 % RadialBlurImage() applies a radial blur to the image.
2616 % Andrew Protano contributed this effect.
2618 % The format of the RadialBlurImage method is:
2620 % Image *RadialBlurImage(const Image *image,const double angle,
2621 % ExceptionInfo *exception)
2623 % A description of each parameter follows:
2625 % o image: the image.
2627 % o angle: the angle of the radial blur.
2631 % o exception: return any errors or warnings in this structure.
2634 MagickExport Image *RadialBlurImage(const Image *image,const double angle,
2635 ExceptionInfo *exception)
2671 Allocate blur image.
2673 assert(image != (Image *) NULL);
2674 assert(image->signature == MagickSignature);
2675 if (image->debug != MagickFalse)
2676 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2677 assert(exception != (ExceptionInfo *) NULL);
2678 assert(exception->signature == MagickSignature);
2679 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2680 if (blur_image == (Image *) NULL)
2681 return((Image *) NULL);
2682 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2684 blur_image=DestroyImage(blur_image);
2685 return((Image *) NULL);
2687 blur_center.x=(double) image->columns/2.0;
2688 blur_center.y=(double) image->rows/2.0;
2689 blur_radius=hypot(blur_center.x,blur_center.y);
2690 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
2691 theta=DegreesToRadians(angle)/(double) (n-1);
2692 cos_theta=(double *) AcquireQuantumMemory((size_t) n,
2693 sizeof(*cos_theta));
2694 sin_theta=(double *) AcquireQuantumMemory((size_t) n,
2695 sizeof(*sin_theta));
2696 if ((cos_theta == (double *) NULL) ||
2697 (sin_theta == (double *) NULL))
2699 blur_image=DestroyImage(blur_image);
2700 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2702 offset=theta*(double) (n-1)/2.0;
2703 for (i=0; i < (ssize_t) n; i++)
2705 cos_theta[i]=cos((double) (theta*i-offset));
2706 sin_theta[i]=sin((double) (theta*i-offset));
2713 image_view=AcquireVirtualCacheView(image,exception);
2714 radial_view=AcquireVirtualCacheView(image,exception);
2715 blur_view=AcquireAuthenticCacheView(blur_image,exception);
2716 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2717 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2718 dynamic_number_threads(image,image->columns,image->rows,1)
2720 for (y=0; y < (ssize_t) image->rows; y++)
2722 register const Quantum
2731 if (status == MagickFalse)
2733 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2734 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2736 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2741 for (x=0; x < (ssize_t) image->columns; x++)
2755 center.x=(double) x-blur_center.x;
2756 center.y=(double) y-blur_center.y;
2757 radius=hypot((double) center.x,center.y);
2762 step=(size_t) (blur_radius/radius);
2769 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2782 register const Quantum
2788 channel=GetPixelChannelMapChannel(image,i);
2789 traits=GetPixelChannelMapTraits(image,channel);
2790 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
2791 if ((traits == UndefinedPixelTrait) ||
2792 (blur_traits == UndefinedPixelTrait))
2794 if (((blur_traits & CopyPixelTrait) != 0) ||
2795 (GetPixelMask(image,p) != 0))
2797 SetPixelChannel(blur_image,channel,p[i],q);
2802 if ((blur_traits & BlendPixelTrait) == 0)
2804 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2806 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2807 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2808 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2810 if (r == (const Quantum *) NULL)
2818 gamma=MagickEpsilonReciprocal(gamma);
2819 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2822 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2824 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2825 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2826 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2828 if (r == (const Quantum *) NULL)
2833 pixel+=GetPixelAlpha(image,r)*r[i];
2834 gamma+=GetPixelAlpha(image,r);
2836 gamma=MagickEpsilonReciprocal(gamma);
2837 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2839 p+=GetPixelChannels(image);
2840 q+=GetPixelChannels(blur_image);
2842 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2844 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2849 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2850 #pragma omp critical (MagickCore_RadialBlurImage)
2852 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2853 if (proceed == MagickFalse)
2857 blur_view=DestroyCacheView(blur_view);
2858 radial_view=DestroyCacheView(radial_view);
2859 image_view=DestroyCacheView(image_view);
2860 cos_theta=(double *) RelinquishMagickMemory(cos_theta);
2861 sin_theta=(double *) RelinquishMagickMemory(sin_theta);
2862 if (status == MagickFalse)
2863 blur_image=DestroyImage(blur_image);
2868 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2872 % S e l e c t i v e B l u r I m a g e %
2876 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2878 % SelectiveBlurImage() selectively blur pixels within a contrast threshold.
2879 % It is similar to the unsharpen mask that sharpens everything with contrast
2880 % above a certain threshold.
2882 % The format of the SelectiveBlurImage method is:
2884 % Image *SelectiveBlurImage(const Image *image,const double radius,
2885 % const double sigma,const double threshold,ExceptionInfo *exception)
2887 % A description of each parameter follows:
2889 % o image: the image.
2891 % o radius: the radius of the Gaussian, in pixels, not counting the center
2894 % o sigma: the standard deviation of the Gaussian, in pixels.
2896 % o threshold: only pixels within this contrast threshold are included
2897 % in the blur operation.
2899 % o exception: return any errors or warnings in this structure.
2902 MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
2903 const double sigma,const double threshold,ExceptionInfo *exception)
2905 #define SelectiveBlurImageTag "SelectiveBlur/Image"
2939 Initialize blur image attributes.
2941 assert(image != (Image *) NULL);
2942 assert(image->signature == MagickSignature);
2943 if (image->debug != MagickFalse)
2944 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2945 assert(exception != (ExceptionInfo *) NULL);
2946 assert(exception->signature == MagickSignature);
2947 width=GetOptimalKernelWidth1D(radius,sigma);
2948 kernel=(double *) AcquireAlignedMemory((size_t) width,width*sizeof(*kernel));
2949 if (kernel == (double *) NULL)
2950 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2951 j=(ssize_t) width/2;
2953 for (v=(-j); v <= j; v++)
2955 for (u=(-j); u <= j; u++)
2956 kernel[i++]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
2957 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
2959 if (image->debug != MagickFalse)
2962 format[MaxTextExtent],
2965 register const double
2972 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
2973 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
2975 message=AcquireString("");
2977 for (v=0; v < (ssize_t) width; v++)
2980 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
2981 (void) ConcatenateString(&message,format);
2982 for (u=0; u < (ssize_t) width; u++)
2984 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",*k++);
2985 (void) ConcatenateString(&message,format);
2987 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
2989 message=DestroyString(message);
2991 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2992 if (blur_image == (Image *) NULL)
2993 return((Image *) NULL);
2994 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2996 blur_image=DestroyImage(blur_image);
2997 kernel=(double *) RelinquishAlignedMemory(kernel);
2998 return((Image *) NULL);
3000 luminance_image=CloneImage(image,0,0,MagickTrue,exception);
3001 if (luminance_image == (Image *) NULL)
3003 blur_image=DestroyImage(blur_image);
3004 kernel=(double *) RelinquishAlignedMemory(kernel);
3005 return((Image *) NULL);
3007 status=TransformImageColorspace(luminance_image,GRAYColorspace,exception);
3008 if (status == MagickFalse)
3010 luminance_image=DestroyImage(luminance_image);
3011 blur_image=DestroyImage(blur_image);
3012 kernel=(double *) RelinquishAlignedMemory(kernel);
3013 return((Image *) NULL);
3016 Threshold blur image.
3020 center=(ssize_t) (GetPixelChannels(image)*(image->columns+width)*(width/2L)+
3021 GetPixelChannels(image)*(width/2L));
3022 image_view=AcquireVirtualCacheView(image,exception);
3023 luminance_view=AcquireVirtualCacheView(luminance_image,exception);
3024 blur_view=AcquireAuthenticCacheView(blur_image,exception);
3025 #if defined(MMAGICKCORE_OPENMP_SUPPORT)
3026 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3027 dynamic_number_threads(image,image->columns,image->rows,1)
3029 for (y=0; y < (ssize_t) image->rows; y++)
3037 register const Quantum
3047 if (status == MagickFalse)
3049 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
3050 (width/2L),image->columns+width,width,exception);
3051 l=GetCacheViewVirtualPixels(luminance_view,-((ssize_t) width/2L),y-(ssize_t)
3052 (width/2L),luminance_image->columns+width,width,exception);
3053 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3055 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3060 for (x=0; x < (ssize_t) image->columns; x++)
3068 intensity=GetPixelIntensity(image,p+center);
3069 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3083 register const double
3086 register const Quantum
3087 *restrict luminance_pixels,
3096 channel=GetPixelChannelMapChannel(image,i);
3097 traits=GetPixelChannelMapTraits(image,channel);
3098 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
3099 if ((traits == UndefinedPixelTrait) ||
3100 (blur_traits == UndefinedPixelTrait))
3102 if (((blur_traits & CopyPixelTrait) != 0) ||
3103 (GetPixelMask(image,p) != 0))
3105 SetPixelChannel(blur_image,channel,p[center+i],q);
3113 if ((blur_traits & BlendPixelTrait) == 0)
3115 for (v=0; v < (ssize_t) width; v++)
3117 for (u=0; u < (ssize_t) width; u++)
3119 contrast=GetPixelIntensity(luminance_image,luminance_pixels)-
3121 if (fabs(contrast) < threshold)
3123 pixel+=(*k)*pixels[i];
3127 pixels+=GetPixelChannels(image);
3128 luminance_pixels+=GetPixelChannels(luminance_image);
3130 pixels+=image->columns*GetPixelChannels(image);
3131 luminance_pixels+=luminance_image->columns*
3132 GetPixelChannels(luminance_image);
3134 if (fabs((double) gamma) < MagickEpsilon)
3136 SetPixelChannel(blur_image,channel,p[center+i],q);
3139 gamma=MagickEpsilonReciprocal(gamma);
3140 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3143 for (v=0; v < (ssize_t) width; v++)
3145 for (u=0; u < (ssize_t) width; u++)
3147 contrast=GetPixelIntensity(image,pixels)-intensity;
3148 if (fabs(contrast) < threshold)
3150 alpha=(double) (QuantumScale*
3151 GetPixelAlpha(image,pixels));
3152 pixel+=(*k)*alpha*pixels[i];
3156 pixels+=GetPixelChannels(image);
3157 luminance_pixels+=GetPixelChannels(luminance_image);
3159 pixels+=image->columns*GetPixelChannels(image);
3160 luminance_pixels+=luminance_image->columns*
3161 GetPixelChannels(luminance_image);
3163 if (fabs((double) gamma) < MagickEpsilon)
3165 SetPixelChannel(blur_image,channel,p[center+i],q);
3168 gamma=MagickEpsilonReciprocal(gamma);
3169 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3171 p+=GetPixelChannels(image);
3172 l+=GetPixelChannels(luminance_image);
3173 q+=GetPixelChannels(blur_image);
3175 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3176 if (sync == MagickFalse)
3178 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3183 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3184 #pragma omp critical (MagickCore_SelectiveBlurImage)
3186 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3188 if (proceed == MagickFalse)
3192 blur_image->type=image->type;
3193 blur_view=DestroyCacheView(blur_view);
3194 image_view=DestroyCacheView(image_view);
3195 luminance_image=DestroyImage(luminance_image);
3196 kernel=(double *) RelinquishAlignedMemory(kernel);
3197 if (status == MagickFalse)
3198 blur_image=DestroyImage(blur_image);
3203 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3207 % S h a d e I m a g e %
3211 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3213 % ShadeImage() shines a distant light on an image to create a
3214 % three-dimensional effect. You control the positioning of the light with
3215 % azimuth and elevation; azimuth is measured in degrees off the x axis
3216 % and elevation is measured in pixels above the Z axis.
3218 % The format of the ShadeImage method is:
3220 % Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3221 % const double azimuth,const double elevation,ExceptionInfo *exception)
3223 % A description of each parameter follows:
3225 % o image: the image.
3227 % o gray: A value other than zero shades the intensity of each pixel.
3229 % o azimuth, elevation: Define the light source direction.
3231 % o exception: return any errors or warnings in this structure.
3234 MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3235 const double azimuth,const double elevation,ExceptionInfo *exception)
3237 #define ShadeImageTag "Shade/Image"
3259 Initialize shaded image attributes.
3261 assert(image != (const Image *) NULL);
3262 assert(image->signature == MagickSignature);
3263 if (image->debug != MagickFalse)
3264 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3265 assert(exception != (ExceptionInfo *) NULL);
3266 assert(exception->signature == MagickSignature);
3267 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3268 if (shade_image == (Image *) NULL)
3269 return((Image *) NULL);
3270 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
3272 shade_image=DestroyImage(shade_image);
3273 return((Image *) NULL);
3276 Compute the light vector.
3278 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3279 cos(DegreesToRadians(elevation));
3280 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3281 cos(DegreesToRadians(elevation));
3282 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3288 image_view=AcquireVirtualCacheView(image,exception);
3289 shade_view=AcquireAuthenticCacheView(shade_image,exception);
3290 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3291 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3292 dynamic_number_threads(image,image->columns,image->rows,1)
3294 for (y=0; y < (ssize_t) image->rows; y++)
3304 register const Quantum
3316 if (status == MagickFalse)
3318 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
3319 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3321 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3327 Shade this row of pixels.
3329 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
3330 pre=p+GetPixelChannels(image);
3331 center=pre+(image->columns+2)*GetPixelChannels(image);
3332 post=center+(image->columns+2)*GetPixelChannels(image);
3333 for (x=0; x < (ssize_t) image->columns; x++)
3339 Determine the surface normal and compute shading.
3341 normal.x=(double) (GetPixelIntensity(image,pre-GetPixelChannels(image))+
3342 GetPixelIntensity(image,center-GetPixelChannels(image))+
3343 GetPixelIntensity(image,post-GetPixelChannels(image))-
3344 GetPixelIntensity(image,pre+GetPixelChannels(image))-
3345 GetPixelIntensity(image,center+GetPixelChannels(image))-
3346 GetPixelIntensity(image,post+GetPixelChannels(image)));
3347 normal.y=(double) (GetPixelIntensity(image,post-GetPixelChannels(image))+
3348 GetPixelIntensity(image,post)+GetPixelIntensity(image,post+
3349 GetPixelChannels(image))-GetPixelIntensity(image,pre-
3350 GetPixelChannels(image))-GetPixelIntensity(image,pre)-
3351 GetPixelIntensity(image,pre+GetPixelChannels(image)));
3352 if ((normal.x == 0.0) && (normal.y == 0.0))
3357 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3358 if (distance > MagickEpsilon)
3361 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
3362 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3363 shade=distance/sqrt((double) normal_distance);
3366 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3375 channel=GetPixelChannelMapChannel(image,i);
3376 traits=GetPixelChannelMapTraits(image,channel);
3377 shade_traits=GetPixelChannelMapTraits(shade_image,channel);
3378 if ((traits == UndefinedPixelTrait) ||
3379 (shade_traits == UndefinedPixelTrait))
3381 if (((shade_traits & CopyPixelTrait) != 0) ||
3382 (GetPixelMask(image,p) != 0))
3384 SetPixelChannel(shade_image,channel,center[i],q);
3387 if (gray != MagickFalse)
3389 SetPixelChannel(shade_image,channel,ClampToQuantum(shade),q);
3392 SetPixelChannel(shade_image,channel,ClampToQuantum(QuantumScale*shade*
3395 pre+=GetPixelChannels(image);
3396 center+=GetPixelChannels(image);
3397 post+=GetPixelChannels(image);
3398 q+=GetPixelChannels(shade_image);
3400 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3402 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3407 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3408 #pragma omp critical (MagickCore_ShadeImage)
3410 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3411 if (proceed == MagickFalse)
3415 shade_view=DestroyCacheView(shade_view);
3416 image_view=DestroyCacheView(image_view);
3417 if (status == MagickFalse)
3418 shade_image=DestroyImage(shade_image);
3419 return(shade_image);
3423 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3427 % S h a r p e n I m a g e %
3431 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3433 % SharpenImage() sharpens the image. We convolve the image with a Gaussian
3434 % operator of the given radius and standard deviation (sigma). For
3435 % reasonable results, radius should be larger than sigma. Use a radius of 0
3436 % and SharpenImage() selects a suitable radius for you.
3438 % Using a separable kernel would be faster, but the negative weights cancel
3439 % out on the corners of the kernel producing often undesirable ringing in the
3440 % filtered result; this can be avoided by using a 2D gaussian shaped image
3441 % sharpening kernel instead.
3443 % The format of the SharpenImage method is:
3445 % Image *SharpenImage(const Image *image,const double radius,
3446 % const double sigma,ExceptionInfo *exception)
3448 % A description of each parameter follows:
3450 % o image: the image.
3452 % o radius: the radius of the Gaussian, in pixels, not counting the center
3455 % o sigma: the standard deviation of the Laplacian, in pixels.
3457 % o exception: return any errors or warnings in this structure.
3460 MagickExport Image *SharpenImage(const Image *image,const double radius,
3461 const double sigma,ExceptionInfo *exception)
3483 assert(image != (const Image *) NULL);
3484 assert(image->signature == MagickSignature);
3485 if (image->debug != MagickFalse)
3486 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3487 assert(exception != (ExceptionInfo *) NULL);
3488 assert(exception->signature == MagickSignature);
3489 width=GetOptimalKernelWidth2D(radius,sigma);
3490 kernel_info=AcquireKernelInfo((const char *) NULL);
3491 if (kernel_info == (KernelInfo *) NULL)
3492 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3493 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3494 kernel_info->width=width;
3495 kernel_info->height=width;
3496 kernel_info->signature=MagickSignature;
3497 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
3498 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
3499 if (kernel_info->values == (MagickRealType *) NULL)
3501 kernel_info=DestroyKernelInfo(kernel_info);
3502 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3505 j=(ssize_t) kernel_info->width/2;
3507 for (v=(-j); v <= j; v++)
3509 for (u=(-j); u <= j; u++)
3511 kernel_info->values[i]=(MagickRealType) (-exp(-((double) u*u+v*v)/(2.0*
3512 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3513 normalize+=kernel_info->values[i];
3517 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
3518 sharp_image=ConvolveImage(image,kernel_info,exception);
3519 if (sharp_image != (Image *) NULL)
3520 (void) ClampImage(sharp_image,exception);
3521 kernel_info=DestroyKernelInfo(kernel_info);
3522 return(sharp_image);
3526 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3530 % S p r e a d I m a g e %
3534 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3536 % SpreadImage() is a special effects method that randomly displaces each
3537 % pixel in a block defined by the radius parameter.
3539 % The format of the SpreadImage method is:
3541 % Image *SpreadImage(const Image *image,const double radius,
3542 % const PixelInterpolateMethod method,ExceptionInfo *exception)
3544 % A description of each parameter follows:
3546 % o image: the image.
3548 % o radius: choose a random pixel in a neighborhood of this extent.
3550 % o method: the pixel interpolation method.
3552 % o exception: return any errors or warnings in this structure.
3555 MagickExport Image *SpreadImage(const Image *image,const double radius,
3556 const PixelInterpolateMethod method,ExceptionInfo *exception)
3558 #define SpreadImageTag "Spread/Image"
3574 **restrict random_info;
3586 Initialize spread image attributes.
3588 assert(image != (Image *) NULL);
3589 assert(image->signature == MagickSignature);
3590 if (image->debug != MagickFalse)
3591 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3592 assert(exception != (ExceptionInfo *) NULL);
3593 assert(exception->signature == MagickSignature);
3594 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3596 if (spread_image == (Image *) NULL)
3597 return((Image *) NULL);
3598 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
3600 spread_image=DestroyImage(spread_image);
3601 return((Image *) NULL);
3608 width=GetOptimalKernelWidth1D(radius,0.5);
3609 random_info=AcquireRandomInfoThreadSet();
3610 key=GetRandomSecretKey(random_info[0]);
3611 image_view=AcquireVirtualCacheView(image,exception);
3612 spread_view=AcquireAuthenticCacheView(spread_image,exception);
3613 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3614 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3615 dynamic_number_threads(image,image->columns,image->rows,key == ~0UL)
3617 for (y=0; y < (ssize_t) image->rows; y++)
3620 id = GetOpenMPThreadId();
3622 register const Quantum
3631 if (status == MagickFalse)
3633 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3634 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
3636 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3641 for (x=0; x < (ssize_t) image->columns; x++)
3646 point.x=GetPseudoRandomValue(random_info[id]);
3647 point.y=GetPseudoRandomValue(random_info[id]);
3648 status=InterpolatePixelChannels(image,image_view,spread_image,method,
3649 (double) x+width*point.x-0.5,(double) y+width*point.y-0.5,q,exception);
3650 q+=GetPixelChannels(spread_image);
3652 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
3654 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3659 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3660 #pragma omp critical (MagickCore_SpreadImage)
3662 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3663 if (proceed == MagickFalse)
3667 spread_view=DestroyCacheView(spread_view);
3668 image_view=DestroyCacheView(image_view);
3669 random_info=DestroyRandomInfoThreadSet(random_info);
3670 return(spread_image);
3674 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3678 % U n s h a r p M a s k I m a g e %
3682 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3684 % UnsharpMaskImage() sharpens one or more image channels. We convolve the
3685 % image with a Gaussian operator of the given radius and standard deviation
3686 % (sigma). For reasonable results, radius should be larger than sigma. Use a
3687 % radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
3689 % The format of the UnsharpMaskImage method is:
3691 % Image *UnsharpMaskImage(const Image *image,const double radius,
3692 % const double sigma,const double amount,const double threshold,
3693 % ExceptionInfo *exception)
3695 % A description of each parameter follows:
3697 % o image: the image.
3699 % o radius: the radius of the Gaussian, in pixels, not counting the center
3702 % o sigma: the standard deviation of the Gaussian, in pixels.
3704 % o amount: the percentage of the difference between the original and the
3705 % blur image that is added back into the original.
3707 % o threshold: the threshold in pixels needed to apply the diffence amount.
3709 % o exception: return any errors or warnings in this structure.
3712 MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
3713 const double sigma,const double amount,const double threshold,
3714 ExceptionInfo *exception)
3716 #define SharpenImageTag "Sharpen/Image"
3737 assert(image != (const Image *) NULL);
3738 assert(image->signature == MagickSignature);
3739 if (image->debug != MagickFalse)
3740 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3741 assert(exception != (ExceptionInfo *) NULL);
3742 unsharp_image=BlurImage(image,radius,sigma,exception);
3743 if (unsharp_image == (Image *) NULL)
3744 return((Image *) NULL);
3745 quantum_threshold=(double) QuantumRange*threshold;
3751 image_view=AcquireVirtualCacheView(image,exception);
3752 unsharp_view=AcquireAuthenticCacheView(unsharp_image,exception);
3753 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3754 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3755 dynamic_number_threads(image,image->columns,image->rows,1)
3757 for (y=0; y < (ssize_t) image->rows; y++)
3759 register const Quantum
3768 if (status == MagickFalse)
3770 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3771 q=QueueCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
3773 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3778 for (x=0; x < (ssize_t) image->columns; x++)
3783 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3795 channel=GetPixelChannelMapChannel(image,i);
3796 traits=GetPixelChannelMapTraits(image,channel);
3797 unsharp_traits=GetPixelChannelMapTraits(unsharp_image,channel);
3798 if ((traits == UndefinedPixelTrait) ||
3799 (unsharp_traits == UndefinedPixelTrait))
3801 if (((unsharp_traits & CopyPixelTrait) != 0) ||
3802 (GetPixelMask(image,p) != 0))
3804 SetPixelChannel(unsharp_image,channel,p[i],q);
3807 pixel=p[i]-(double) GetPixelChannel(unsharp_image,channel,q);
3808 if (fabs(2.0*pixel) < quantum_threshold)
3809 pixel=(double) p[i];
3811 pixel=(double) p[i]+amount*pixel;
3812 SetPixelChannel(unsharp_image,channel,ClampToQuantum(pixel),q);
3814 p+=GetPixelChannels(image);
3815 q+=GetPixelChannels(unsharp_image);
3817 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
3819 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3824 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3825 #pragma omp critical (MagickCore_UnsharpMaskImage)
3827 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
3828 if (proceed == MagickFalse)
3832 unsharp_image->type=image->type;
3833 unsharp_view=DestroyCacheView(unsharp_view);
3834 if (unsharp_image != (Image *) NULL)
3835 (void) ClampImage(unsharp_image,exception);
3836 image_view=DestroyCacheView(image_view);
3837 if (status == MagickFalse)
3838 unsharp_image=DestroyImage(unsharp_image);
3839 return(unsharp_image);