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-2013 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/memory-private.h"
67 #include "MagickCore/monitor.h"
68 #include "MagickCore/monitor-private.h"
69 #include "MagickCore/montage.h"
70 #include "MagickCore/morphology.h"
71 #include "MagickCore/paint.h"
72 #include "MagickCore/pixel-accessor.h"
73 #include "MagickCore/pixel-private.h"
74 #include "MagickCore/property.h"
75 #include "MagickCore/quantize.h"
76 #include "MagickCore/quantum.h"
77 #include "MagickCore/quantum-private.h"
78 #include "MagickCore/random_.h"
79 #include "MagickCore/random-private.h"
80 #include "MagickCore/resample.h"
81 #include "MagickCore/resample-private.h"
82 #include "MagickCore/resize.h"
83 #include "MagickCore/resource_.h"
84 #include "MagickCore/segment.h"
85 #include "MagickCore/shear.h"
86 #include "MagickCore/signature-private.h"
87 #include "MagickCore/statistic.h"
88 #include "MagickCore/string_.h"
89 #include "MagickCore/thread-private.h"
90 #include "MagickCore/transform.h"
91 #include "MagickCore/threshold.h"
94 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98 % A d a p t i v e B l u r I m a g e %
102 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104 % AdaptiveBlurImage() adaptively blurs the image by blurring less
105 % intensely near image edges and more intensely far from edges. We blur the
106 % image with a Gaussian operator of the given radius and standard deviation
107 % (sigma). For reasonable results, radius should be larger than sigma. Use a
108 % radius of 0 and AdaptiveBlurImage() selects a suitable radius for you.
110 % The format of the AdaptiveBlurImage method is:
112 % Image *AdaptiveBlurImage(const Image *image,const double radius,
113 % const double sigma,ExceptionInfo *exception)
115 % A description of each parameter follows:
117 % o image: the image.
119 % o radius: the radius of the Gaussian, in pixels, not counting the center
122 % o sigma: the standard deviation of the Laplacian, in pixels.
124 % o exception: return any errors or warnings in this structure.
128 MagickExport MagickBooleanType AdaptiveLevelImage(Image *image,
129 const char *levels,ExceptionInfo *exception)
148 if (levels == (char *) NULL)
150 flags=ParseGeometry(levels,&geometry_info);
151 black_point=geometry_info.rho;
152 white_point=(double) QuantumRange;
153 if ((flags & SigmaValue) != 0)
154 white_point=geometry_info.sigma;
156 if ((flags & XiValue) != 0)
157 gamma=geometry_info.xi;
158 if ((flags & PercentValue) != 0)
160 black_point*=(double) image->columns*image->rows/100.0;
161 white_point*=(double) image->columns*image->rows/100.0;
163 if ((flags & SigmaValue) == 0)
164 white_point=(double) QuantumRange-black_point;
165 if ((flags & AspectValue ) == 0)
166 status=LevelImage(image,black_point,white_point,gamma,exception);
168 status=LevelizeImage(image,black_point,white_point,gamma,exception);
172 MagickExport Image *AdaptiveBlurImage(const Image *image,const double radius,
173 const double sigma,ExceptionInfo *exception)
175 #define AdaptiveBlurImageTag "Convolve/Image"
176 #define MagickSigma (fabs(sigma) < MagickEpsilon ? MagickEpsilon : sigma)
213 assert(image != (const Image *) NULL);
214 assert(image->signature == MagickSignature);
215 if (image->debug != MagickFalse)
216 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
217 assert(exception != (ExceptionInfo *) NULL);
218 assert(exception->signature == MagickSignature);
219 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
220 if (blur_image == (Image *) NULL)
221 return((Image *) NULL);
222 if (fabs(sigma) < MagickEpsilon)
224 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
226 blur_image=DestroyImage(blur_image);
227 return((Image *) NULL);
230 Edge detect the image brighness channel, level, blur, and level again.
232 edge_image=EdgeImage(image,radius,sigma,exception);
233 if (edge_image == (Image *) NULL)
235 blur_image=DestroyImage(blur_image);
236 return((Image *) NULL);
238 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
239 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
240 if (gaussian_image != (Image *) NULL)
242 edge_image=DestroyImage(edge_image);
243 edge_image=gaussian_image;
245 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
247 Create a set of kernels from maximum (radius,sigma) to minimum.
249 width=GetOptimalKernelWidth2D(radius,sigma);
250 kernel=(MagickRealType **) MagickAssumeAligned(AcquireAlignedMemory((size_t)
251 width,sizeof(*kernel)));
252 if (kernel == (MagickRealType **) NULL)
254 edge_image=DestroyImage(edge_image);
255 blur_image=DestroyImage(blur_image);
256 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
258 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
259 for (i=0; i < (ssize_t) width; i+=2)
261 kernel[i]=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory(
262 (size_t) (width-i),(width-i)*sizeof(**kernel)));
263 if (kernel[i] == (MagickRealType *) NULL)
266 j=(ssize_t) (width-i)/2;
268 for (v=(-j); v <= j; v++)
270 for (u=(-j); u <= j; u++)
272 kernel[i][k]=(MagickRealType) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
273 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
274 normalize+=kernel[i][k];
278 if (fabs(normalize) < MagickEpsilon)
279 normalize=MagickEpsilon;
280 normalize=PerceptibleReciprocal(normalize);
281 for (k=0; k < (j*j); k++)
282 kernel[i][k]=normalize*kernel[i][k];
284 if (i < (ssize_t) width)
286 for (i-=2; i >= 0; i-=2)
287 kernel[i]=(MagickRealType *) RelinquishAlignedMemory(kernel[i]);
288 kernel=(MagickRealType **) RelinquishAlignedMemory(kernel);
289 edge_image=DestroyImage(edge_image);
290 blur_image=DestroyImage(blur_image);
291 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
294 Adaptively blur image.
298 image_view=AcquireVirtualCacheView(image);
299 edge_view=AcquireVirtualCacheView(edge_image);
300 blur_view=AcquireAuthenticCacheView(blur_image);
301 #if defined(MAGICKCORE_OPENMP_SUPPORT)
302 #pragma omp parallel for schedule(static,4) shared(progress,status) \
303 dynamic_number_threads(image,image->columns,image->rows,1)
305 for (y=0; y < (ssize_t) blur_image->rows; y++)
307 register const Quantum
316 if (status == MagickFalse)
318 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
319 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
321 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
326 for (x=0; x < (ssize_t) blur_image->columns; x++)
328 register const Quantum
338 j=(ssize_t) ceil((double) width*QuantumScale*
339 GetPixelIntensity(edge_image,r)-0.5);
343 if (j > (ssize_t) width)
347 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
348 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
349 if (p == (const Quantum *) NULL)
351 center=(ssize_t) GetPixelChannels(image)*(width-j)*((width-j)/2L)+
352 GetPixelChannels(image)*((width-j)/2L);
353 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
367 register const MagickRealType
370 register const Quantum
379 channel=GetPixelChannelChannel(image,i);
380 traits=GetPixelChannelTraits(image,channel);
381 blur_traits=GetPixelChannelTraits(blur_image,channel);
382 if ((traits == UndefinedPixelTrait) ||
383 (blur_traits == UndefinedPixelTrait))
385 if (((blur_traits & CopyPixelTrait) != 0) ||
386 (GetPixelMask(image,p) != 0))
388 SetPixelChannel(blur_image,channel,p[center+i],q);
395 if ((blur_traits & BlendPixelTrait) == 0)
400 for (v=0; v < (ssize_t) (width-j); v++)
402 for (u=0; u < (ssize_t) (width-j); u++)
404 pixel+=(*k)*pixels[i];
407 pixels+=GetPixelChannels(image);
410 gamma=PerceptibleReciprocal(gamma);
411 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
417 for (v=0; v < (ssize_t) (width-j); v++)
419 for (u=0; u < (ssize_t) (width-j); u++)
421 alpha=(double) (QuantumScale*GetPixelAlpha(image,pixels));
422 pixel+=(*k)*alpha*pixels[i];
425 pixels+=GetPixelChannels(image);
428 gamma=PerceptibleReciprocal(gamma);
429 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
431 q+=GetPixelChannels(blur_image);
432 r+=GetPixelChannels(edge_image);
434 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
436 if (image->progress_monitor != (MagickProgressMonitor) NULL)
441 #if defined(MAGICKCORE_OPENMP_SUPPORT)
442 #pragma omp critical (MagickCore_AdaptiveBlurImage)
444 proceed=SetImageProgress(image,AdaptiveBlurImageTag,progress++,
446 if (proceed == MagickFalse)
450 blur_image->type=image->type;
451 blur_view=DestroyCacheView(blur_view);
452 edge_view=DestroyCacheView(edge_view);
453 image_view=DestroyCacheView(image_view);
454 edge_image=DestroyImage(edge_image);
455 for (i=0; i < (ssize_t) width; i+=2)
456 kernel[i]=(MagickRealType *) RelinquishAlignedMemory(kernel[i]);
457 kernel=(MagickRealType **) RelinquishAlignedMemory(kernel);
458 if (status == MagickFalse)
459 blur_image=DestroyImage(blur_image);
464 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
468 % A d a p t i v e S h a r p e n I m a g e %
472 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
474 % AdaptiveSharpenImage() adaptively sharpens the image by sharpening more
475 % intensely near image edges and less intensely far from edges. We sharpen the
476 % image with a Gaussian operator of the given radius and standard deviation
477 % (sigma). For reasonable results, radius should be larger than sigma. Use a
478 % radius of 0 and AdaptiveSharpenImage() selects a suitable radius for you.
480 % The format of the AdaptiveSharpenImage method is:
482 % Image *AdaptiveSharpenImage(const Image *image,const double radius,
483 % const double sigma,ExceptionInfo *exception)
485 % A description of each parameter follows:
487 % o image: the image.
489 % o radius: the radius of the Gaussian, in pixels, not counting the center
492 % o sigma: the standard deviation of the Laplacian, in pixels.
494 % o exception: return any errors or warnings in this structure.
497 MagickExport Image *AdaptiveSharpenImage(const Image *image,const double radius,
498 const double sigma,ExceptionInfo *exception)
500 #define AdaptiveSharpenImageTag "Convolve/Image"
501 #define MagickSigma (fabs(sigma) < MagickEpsilon ? MagickEpsilon : sigma)
538 assert(image != (const Image *) NULL);
539 assert(image->signature == MagickSignature);
540 if (image->debug != MagickFalse)
541 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
542 assert(exception != (ExceptionInfo *) NULL);
543 assert(exception->signature == MagickSignature);
544 sharp_image=CloneImage(image,0,0,MagickTrue,exception);
545 if (sharp_image == (Image *) NULL)
546 return((Image *) NULL);
547 if (fabs(sigma) < MagickEpsilon)
549 if (SetImageStorageClass(sharp_image,DirectClass,exception) == MagickFalse)
551 sharp_image=DestroyImage(sharp_image);
552 return((Image *) NULL);
555 Edge detect the image brighness channel, level, sharp, and level again.
557 edge_image=EdgeImage(image,radius,sigma,exception);
558 if (edge_image == (Image *) NULL)
560 sharp_image=DestroyImage(sharp_image);
561 return((Image *) NULL);
563 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
564 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
565 if (gaussian_image != (Image *) NULL)
567 edge_image=DestroyImage(edge_image);
568 edge_image=gaussian_image;
570 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
572 Create a set of kernels from maximum (radius,sigma) to minimum.
574 width=GetOptimalKernelWidth2D(radius,sigma);
575 kernel=(MagickRealType **) MagickAssumeAligned(AcquireAlignedMemory((size_t)
576 width,sizeof(*kernel)));
577 if (kernel == (MagickRealType **) NULL)
579 edge_image=DestroyImage(edge_image);
580 sharp_image=DestroyImage(sharp_image);
581 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
583 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
584 for (i=0; i < (ssize_t) width; i+=2)
586 kernel[i]=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory(
587 (size_t) (width-i),(width-i)*sizeof(**kernel)));
588 if (kernel[i] == (MagickRealType *) NULL)
591 j=(ssize_t) (width-i)/2;
593 for (v=(-j); v <= j; v++)
595 for (u=(-j); u <= j; u++)
597 kernel[i][k]=(MagickRealType) (-exp(-((double) u*u+v*v)/(2.0*
598 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
599 normalize+=kernel[i][k];
603 if (fabs(normalize) < MagickEpsilon)
604 normalize=MagickEpsilon;
605 normalize=PerceptibleReciprocal(normalize);
606 for (k=0; k < (j*j); k++)
607 kernel[i][k]=normalize*kernel[i][k];
609 if (i < (ssize_t) width)
611 for (i-=2; i >= 0; i-=2)
612 kernel[i]=(MagickRealType *) RelinquishAlignedMemory(kernel[i]);
613 kernel=(MagickRealType **) RelinquishAlignedMemory(kernel);
614 edge_image=DestroyImage(edge_image);
615 sharp_image=DestroyImage(sharp_image);
616 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
619 Adaptively sharpen image.
623 image_view=AcquireVirtualCacheView(image);
624 edge_view=AcquireVirtualCacheView(edge_image);
625 sharp_view=AcquireAuthenticCacheView(sharp_image);
626 #if defined(MAGICKCORE_OPENMP_SUPPORT)
627 #pragma omp parallel for schedule(static,4) shared(progress,status) \
628 dynamic_number_threads(image,image->columns,image->rows,1)
630 for (y=0; y < (ssize_t) sharp_image->rows; y++)
632 register const Quantum
641 if (status == MagickFalse)
643 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
644 q=QueueCacheViewAuthenticPixels(sharp_view,0,y,sharp_image->columns,1,
646 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
651 for (x=0; x < (ssize_t) sharp_image->columns; x++)
653 register const Quantum
663 j=(ssize_t) ceil((double) width*QuantumScale*
664 GetPixelIntensity(edge_image,r)-0.5);
668 if (j > (ssize_t) width)
672 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
673 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
674 if (p == (const Quantum *) NULL)
676 center=(ssize_t) GetPixelChannels(image)*(width-j)*((width-j)/2L)+
677 GetPixelChannels(image)*((width-j)/2);
678 for (i=0; i < (ssize_t) GetPixelChannels(sharp_image); i++)
692 register const MagickRealType
695 register const Quantum
704 channel=GetPixelChannelChannel(image,i);
705 traits=GetPixelChannelTraits(image,channel);
706 sharp_traits=GetPixelChannelTraits(sharp_image,channel);
707 if ((traits == UndefinedPixelTrait) ||
708 (sharp_traits == UndefinedPixelTrait))
710 if (((sharp_traits & CopyPixelTrait) != 0) ||
711 (GetPixelMask(image,p) != 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=PerceptibleReciprocal(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=(double) (QuantumScale*GetPixelAlpha(image,pixels));
747 pixel+=(*k)*alpha*pixels[i];
750 pixels+=GetPixelChannels(image);
753 gamma=PerceptibleReciprocal(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]=(MagickRealType *) RelinquishAlignedMemory(kernel[i]);
782 kernel=(MagickRealType **) 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 MagickRealType *GetBlurKernel(const size_t width,const double sigma)
842 Generate a 1-D convolution kernel.
844 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
845 kernel=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory((size_t)
846 width,sizeof(*kernel)));
847 if (kernel == (MagickRealType *) NULL)
852 for (k=(-j); k <= j; k++)
854 kernel[i]=(MagickRealType) (exp(-((double) k*k)/(2.0*MagickSigma*
855 MagickSigma))/(MagickSQ2PI*MagickSigma));
856 normalize+=kernel[i];
859 for (i=0; i < (ssize_t) width; i++)
860 kernel[i]/=normalize;
864 MagickExport Image *BlurImage(const Image *image,const double radius,
865 const double sigma,ExceptionInfo *exception)
867 #define BlurImageTag "Blur/Image"
897 Initialize blur image attributes.
899 assert(image != (Image *) NULL);
900 assert(image->signature == MagickSignature);
901 if (image->debug != MagickFalse)
902 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
903 assert(exception != (ExceptionInfo *) NULL);
904 assert(exception->signature == MagickSignature);
905 blur_image=CloneImage(image,0,0,MagickTrue,exception);
906 if (blur_image == (Image *) NULL)
907 return((Image *) NULL);
908 if (fabs(sigma) < MagickEpsilon)
910 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
912 blur_image=DestroyImage(blur_image);
913 return((Image *) NULL);
915 width=GetOptimalKernelWidth1D(radius,sigma);
916 kernel=GetBlurKernel(width,sigma);
917 if (kernel == (MagickRealType *) NULL)
919 blur_image=DestroyImage(blur_image);
920 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
922 if (image->debug != MagickFalse)
925 format[MaxTextExtent],
928 register const MagickRealType
931 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
932 " blur image with kernel width %.20g:",(double) width);
933 message=AcquireString("");
935 for (i=0; i < (ssize_t) width; i++)
938 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) i);
939 (void) ConcatenateString(&message,format);
940 (void) FormatLocaleString(format,MaxTextExtent,"%g ",(double) *k++);
941 (void) ConcatenateString(&message,format);
942 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
944 message=DestroyString(message);
951 center=(ssize_t) GetPixelChannels(image)*(width/2L);
952 image_view=AcquireVirtualCacheView(image);
953 blur_view=AcquireAuthenticCacheView(blur_image);
954 #if defined(MAGICKCORE_OPENMP_SUPPORT)
955 #pragma omp parallel for schedule(static,4) shared(progress,status) \
956 dynamic_number_threads(image,image->columns,image->rows,1)
958 for (y=0; y < (ssize_t) image->rows; y++)
960 register const Quantum
969 if (status == MagickFalse)
971 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y,
972 image->columns+width,1,exception);
973 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
975 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
980 for (x=0; x < (ssize_t) image->columns; x++)
985 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
999 register const MagickRealType
1002 register const Quantum
1008 channel=GetPixelChannelChannel(image,i);
1009 traits=GetPixelChannelTraits(image,channel);
1010 blur_traits=GetPixelChannelTraits(blur_image,channel);
1011 if ((traits == UndefinedPixelTrait) ||
1012 (blur_traits == UndefinedPixelTrait))
1014 if (((blur_traits & CopyPixelTrait) != 0) ||
1015 (GetPixelMask(image,p) != 0))
1017 SetPixelChannel(blur_image,channel,p[center+i],q);
1023 if ((blur_traits & BlendPixelTrait) == 0)
1028 for (u=0; u < (ssize_t) width; u++)
1030 pixel+=(*k)*pixels[i];
1032 pixels+=GetPixelChannels(image);
1034 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
1041 for (u=0; u < (ssize_t) width; u++)
1043 alpha=(double) (QuantumScale*GetPixelAlpha(image,pixels));
1044 pixel+=(*k)*alpha*pixels[i];
1047 pixels+=GetPixelChannels(image);
1049 gamma=PerceptibleReciprocal(gamma);
1050 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
1052 p+=GetPixelChannels(image);
1053 q+=GetPixelChannels(blur_image);
1055 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1057 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1062 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1063 #pragma omp critical (MagickCore_BlurImage)
1065 proceed=SetImageProgress(image,BlurImageTag,progress++,blur_image->rows+
1066 blur_image->columns);
1067 if (proceed == MagickFalse)
1071 blur_view=DestroyCacheView(blur_view);
1072 image_view=DestroyCacheView(image_view);
1076 center=(ssize_t) GetPixelChannels(blur_image)*(width/2L);
1077 image_view=AcquireVirtualCacheView(blur_image);
1078 blur_view=AcquireAuthenticCacheView(blur_image);
1079 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1080 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1081 dynamic_number_threads(image,image->columns,image->rows,1)
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 for (i=0; i < (ssize_t) GetPixelChannels(blur_image); i++)
1123 register const MagickRealType
1126 register const Quantum
1132 channel=GetPixelChannelChannel(blur_image,i);
1133 traits=GetPixelChannelTraits(blur_image,channel);
1134 blur_traits=GetPixelChannelTraits(blur_image,channel);
1135 if ((traits == UndefinedPixelTrait) ||
1136 (blur_traits == UndefinedPixelTrait))
1138 if (((blur_traits & CopyPixelTrait) != 0) ||
1139 (GetPixelMask(image,p) != 0))
1141 SetPixelChannel(blur_image,channel,p[center+i],q);
1147 if ((blur_traits & BlendPixelTrait) == 0)
1152 for (u=0; u < (ssize_t) width; u++)
1154 pixel+=(*k)*pixels[i];
1156 pixels+=GetPixelChannels(blur_image);
1158 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
1165 for (u=0; u < (ssize_t) width; u++)
1167 alpha=(double) (QuantumScale*GetPixelAlpha(blur_image,
1169 pixel+=(*k)*alpha*pixels[i];
1172 pixels+=GetPixelChannels(blur_image);
1174 gamma=PerceptibleReciprocal(gamma);
1175 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
1177 p+=GetPixelChannels(blur_image);
1178 q+=GetPixelChannels(blur_image);
1180 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1182 if (blur_image->progress_monitor != (MagickProgressMonitor) NULL)
1187 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1188 #pragma omp critical (MagickCore_BlurImage)
1190 proceed=SetImageProgress(blur_image,BlurImageTag,progress++,
1191 blur_image->rows+blur_image->columns);
1192 if (proceed == MagickFalse)
1196 blur_view=DestroyCacheView(blur_view);
1197 image_view=DestroyCacheView(image_view);
1198 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
1199 blur_image->type=image->type;
1200 if (status == MagickFalse)
1201 blur_image=DestroyImage(blur_image);
1206 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1210 % C o n v o l v e I m a g e %
1214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1216 % ConvolveImage() applies a custom convolution kernel to the image.
1218 % The format of the ConvolveImage method is:
1220 % Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
1221 % ExceptionInfo *exception)
1223 % A description of each parameter follows:
1225 % o image: the image.
1227 % o kernel: the filtering kernel.
1229 % o exception: return any errors or warnings in this structure.
1232 MagickExport Image *ConvolveImage(const Image *image,
1233 const KernelInfo *kernel_info,ExceptionInfo *exception)
1235 return(MorphologyImage(image,CorrelateMorphology,1,kernel_info,exception));
1239 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1243 % D e s p e c k l e I m a g e %
1247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1249 % DespeckleImage() reduces the speckle noise in an image while perserving the
1250 % edges of the original image. A speckle removing filter uses a complementary % hulling technique (raising pixels that are darker than their surrounding
1251 % neighbors, then complementarily lowering pixels that are brighter than their
1252 % surrounding neighbors) to reduce the speckle index of that image (reference
1253 % Crimmins speckle removal).
1255 % The format of the DespeckleImage method is:
1257 % Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1259 % A description of each parameter follows:
1261 % o image: the image.
1263 % o exception: return any errors or warnings in this structure.
1267 static void Hull(const Image *image,const ssize_t x_offset,
1268 const ssize_t y_offset,const size_t columns,const size_t rows,
1269 const int polarity,Quantum *restrict f,Quantum *restrict g)
1280 assert(f != (Quantum *) NULL);
1281 assert(g != (Quantum *) NULL);
1284 r=p+(y_offset*(columns+2)+x_offset);
1285 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1286 #pragma omp parallel for schedule(static) \
1287 dynamic_number_threads(image,columns,rows,1)
1289 for (y=0; y < (ssize_t) rows; y++)
1298 i=(2*y+1)+y*columns;
1300 for (x=0; x < (ssize_t) columns; x++)
1302 v=(MagickRealType) p[i];
1303 if ((MagickRealType) r[i] >= (v+ScaleCharToQuantum(2)))
1304 v+=ScaleCharToQuantum(1);
1309 for (x=0; x < (ssize_t) columns; x++)
1311 v=(MagickRealType) p[i];
1312 if ((MagickRealType) r[i] <= (v-ScaleCharToQuantum(2)))
1313 v-=ScaleCharToQuantum(1);
1320 r=q+(y_offset*(columns+2)+x_offset);
1321 s=q-(y_offset*(columns+2)+x_offset);
1322 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1323 #pragma omp parallel for schedule(static) \
1324 dynamic_number_threads(image,columns,rows,1)
1326 for (y=0; y < (ssize_t) rows; y++)
1335 i=(2*y+1)+y*columns;
1337 for (x=0; x < (ssize_t) columns; x++)
1339 v=(MagickRealType) q[i];
1340 if (((MagickRealType) s[i] >= (v+ScaleCharToQuantum(2))) &&
1341 ((MagickRealType) r[i] > v))
1342 v+=ScaleCharToQuantum(1);
1347 for (x=0; x < (ssize_t) columns; x++)
1349 v=(MagickRealType) q[i];
1350 if (((MagickRealType) s[i] <= (v-ScaleCharToQuantum(2))) &&
1351 ((MagickRealType) r[i] < v))
1352 v-=ScaleCharToQuantum(1);
1359 MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1361 #define DespeckleImageTag "Despeckle/Image"
1383 static const ssize_t
1384 X[4] = {0, 1, 1,-1},
1385 Y[4] = {1, 0, 1, 1};
1388 Allocate despeckled image.
1390 assert(image != (const Image *) NULL);
1391 assert(image->signature == MagickSignature);
1392 if (image->debug != MagickFalse)
1393 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1394 assert(exception != (ExceptionInfo *) NULL);
1395 assert(exception->signature == MagickSignature);
1396 despeckle_image=CloneImage(image,0,0,MagickTrue,exception);
1397 if (despeckle_image == (Image *) NULL)
1398 return((Image *) NULL);
1399 status=SetImageStorageClass(despeckle_image,DirectClass,exception);
1400 if (status == MagickFalse)
1402 despeckle_image=DestroyImage(despeckle_image);
1403 return((Image *) NULL);
1406 Allocate image buffer.
1408 length=(size_t) ((image->columns+2)*(image->rows+2));
1409 pixels=(Quantum *) AcquireQuantumMemory(length,sizeof(*pixels));
1410 buffer=(Quantum *) AcquireQuantumMemory(length,sizeof(*buffer));
1411 if ((pixels == (Quantum *) NULL) || (buffer == (Quantum *) NULL))
1413 if (buffer != (Quantum *) NULL)
1414 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1415 if (pixels != (Quantum *) NULL)
1416 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1417 despeckle_image=DestroyImage(despeckle_image);
1418 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1421 Reduce speckle in the image.
1424 image_view=AcquireVirtualCacheView(image);
1425 despeckle_view=AcquireAuthenticCacheView(despeckle_image);
1426 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1443 if (status == MagickFalse)
1445 channel=GetPixelChannelChannel(image,i);
1446 traits=GetPixelChannelTraits(image,channel);
1447 despeckle_traits=GetPixelChannelTraits(despeckle_image,channel);
1448 if ((traits == UndefinedPixelTrait) ||
1449 (despeckle_traits == UndefinedPixelTrait))
1451 if ((despeckle_traits & CopyPixelTrait) != 0)
1453 (void) ResetMagickMemory(pixels,0,length*sizeof(*pixels));
1454 j=(ssize_t) image->columns+2;
1455 for (y=0; y < (ssize_t) image->rows; y++)
1457 register const Quantum
1460 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1461 if (p == (const Quantum *) NULL)
1467 for (x=0; x < (ssize_t) image->columns; x++)
1470 p+=GetPixelChannels(image);
1474 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
1475 for (k=0; k < 4; k++)
1477 Hull(image,X[k],Y[k],image->columns,image->rows,1,pixels,buffer);
1478 Hull(image,-X[k],-Y[k],image->columns,image->rows,1,pixels,buffer);
1479 Hull(image,-X[k],-Y[k],image->columns,image->rows,-1,pixels,buffer);
1480 Hull(image,X[k],Y[k],image->columns,image->rows,-1,pixels,buffer);
1482 j=(ssize_t) image->columns+2;
1483 for (y=0; y < (ssize_t) image->rows; y++)
1491 q=QueueCacheViewAuthenticPixels(despeckle_view,0,y,
1492 despeckle_image->columns,1,exception);
1493 if (q == (Quantum *) NULL)
1499 for (x=0; x < (ssize_t) image->columns; x++)
1501 SetPixelChannel(despeckle_image,channel,pixels[j++],q);
1502 q+=GetPixelChannels(despeckle_image);
1504 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1505 if (sync == MagickFalse)
1509 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1514 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1515 GetPixelChannels(image));
1516 if (proceed == MagickFalse)
1520 despeckle_view=DestroyCacheView(despeckle_view);
1521 image_view=DestroyCacheView(image_view);
1522 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1523 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1524 despeckle_image->type=image->type;
1525 if (status == MagickFalse)
1526 despeckle_image=DestroyImage(despeckle_image);
1527 return(despeckle_image);
1531 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1535 % E d g e I m a g e %
1539 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1541 % EdgeImage() finds edges in an image. Radius defines the radius of the
1542 % convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1545 % The format of the EdgeImage method is:
1547 % Image *EdgeImage(const Image *image,const double radius,
1548 % const double sigma,ExceptionInfo *exception)
1550 % A description of each parameter follows:
1552 % o image: the image.
1554 % o radius: the radius of the pixel neighborhood.
1556 % o sigma: the standard deviation of the Gaussian, in pixels.
1558 % o exception: return any errors or warnings in this structure.
1561 MagickExport Image *EdgeImage(const Image *image,const double radius,
1562 const double sigma,ExceptionInfo *exception)
1581 assert(image != (const Image *) NULL);
1582 assert(image->signature == MagickSignature);
1583 if (image->debug != MagickFalse)
1584 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1585 assert(exception != (ExceptionInfo *) NULL);
1586 assert(exception->signature == MagickSignature);
1587 width=GetOptimalKernelWidth1D(radius,sigma);
1588 kernel_info=AcquireKernelInfo((const char *) NULL);
1589 if (kernel_info == (KernelInfo *) NULL)
1590 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1591 kernel_info->width=width;
1592 kernel_info->height=width;
1593 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
1594 AcquireAlignedMemory(kernel_info->width,kernel_info->width*
1595 sizeof(*kernel_info->values)));
1596 if (kernel_info->values == (MagickRealType *) NULL)
1598 kernel_info=DestroyKernelInfo(kernel_info);
1599 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1601 j=(ssize_t) kernel_info->width/2;
1603 for (v=(-j); v <= j; v++)
1605 for (u=(-j); u <= j; u++)
1607 kernel_info->values[i]=(MagickRealType) (-1.0);
1611 kernel_info->values[i/2]=(MagickRealType) (width*width-1.0);
1612 edge_image=ConvolveImage(image,kernel_info,exception);
1613 kernel_info=DestroyKernelInfo(kernel_info);
1618 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1622 % E m b o s s I m a g e %
1626 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1628 % EmbossImage() returns a grayscale image with a three-dimensional effect.
1629 % We convolve the image with a Gaussian operator of the given radius and
1630 % standard deviation (sigma). For reasonable results, radius should be
1631 % larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1634 % The format of the EmbossImage method is:
1636 % Image *EmbossImage(const Image *image,const double radius,
1637 % const double sigma,ExceptionInfo *exception)
1639 % A description of each parameter follows:
1641 % o image: the image.
1643 % o radius: the radius of the pixel neighborhood.
1645 % o sigma: the standard deviation of the Gaussian, in pixels.
1647 % o exception: return any errors or warnings in this structure.
1650 MagickExport Image *EmbossImage(const Image *image,const double radius,
1651 const double sigma,ExceptionInfo *exception)
1671 assert(image != (const Image *) NULL);
1672 assert(image->signature == MagickSignature);
1673 if (image->debug != MagickFalse)
1674 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1675 assert(exception != (ExceptionInfo *) NULL);
1676 assert(exception->signature == MagickSignature);
1677 width=GetOptimalKernelWidth1D(radius,sigma);
1678 kernel_info=AcquireKernelInfo((const char *) NULL);
1679 if (kernel_info == (KernelInfo *) NULL)
1680 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1681 kernel_info->width=width;
1682 kernel_info->height=width;
1683 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
1684 AcquireAlignedMemory(kernel_info->width,kernel_info->width*
1685 sizeof(*kernel_info->values)));
1686 if (kernel_info->values == (MagickRealType *) NULL)
1688 kernel_info=DestroyKernelInfo(kernel_info);
1689 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1691 j=(ssize_t) kernel_info->width/2;
1694 for (v=(-j); v <= j; v++)
1696 for (u=(-j); u <= j; u++)
1698 kernel_info->values[i]=(MagickRealType) (((u < 0) || (v < 0) ? -8.0 :
1699 8.0)*exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
1700 (2.0*MagickPI*MagickSigma*MagickSigma));
1702 kernel_info->values[i]=0.0;
1707 emboss_image=ConvolveImage(image,kernel_info,exception);
1708 kernel_info=DestroyKernelInfo(kernel_info);
1709 if (emboss_image != (Image *) NULL)
1710 (void) EqualizeImage(emboss_image,exception);
1711 return(emboss_image);
1715 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1719 % G a u s s i a n B l u r I m a g e %
1723 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1725 % GaussianBlurImage() blurs an image. We convolve the image with a
1726 % Gaussian operator of the given radius and standard deviation (sigma).
1727 % For reasonable results, the radius should be larger than sigma. Use a
1728 % radius of 0 and GaussianBlurImage() selects a suitable radius for you
1730 % The format of the GaussianBlurImage method is:
1732 % Image *GaussianBlurImage(const Image *image,onst double radius,
1733 % const double sigma,ExceptionInfo *exception)
1735 % A description of each parameter follows:
1737 % o image: the image.
1739 % o radius: the radius of the Gaussian, in pixels, not counting the center
1742 % o sigma: the standard deviation of the Gaussian, in pixels.
1744 % o exception: return any errors or warnings in this structure.
1747 MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
1748 const double sigma,ExceptionInfo *exception)
1767 assert(image != (const Image *) NULL);
1768 assert(image->signature == MagickSignature);
1769 if (image->debug != MagickFalse)
1770 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1771 assert(exception != (ExceptionInfo *) NULL);
1772 assert(exception->signature == MagickSignature);
1773 width=GetOptimalKernelWidth2D(radius,sigma);
1774 kernel_info=AcquireKernelInfo((const char *) NULL);
1775 if (kernel_info == (KernelInfo *) NULL)
1776 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1777 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1778 kernel_info->width=width;
1779 kernel_info->height=width;
1780 kernel_info->signature=MagickSignature;
1781 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
1782 AcquireAlignedMemory(kernel_info->width,kernel_info->width*
1783 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]=(MagickRealType) (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 MagickRealType *GetMotionBlurKernel(const size_t width,
1855 Generate a 1-D convolution kernel.
1857 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1858 kernel=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory((size_t)
1859 width,sizeof(*kernel)));
1860 if (kernel == (MagickRealType *) NULL)
1863 for (i=0; i < (ssize_t) width; i++)
1865 kernel[i]=(MagickRealType) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
1866 MagickSigma)))/(MagickSQ2PI*MagickSigma));
1867 normalize+=kernel[i];
1869 for (i=0; i < (ssize_t) width; i++)
1870 kernel[i]/=normalize;
1874 MagickExport Image *MotionBlurImage(const Image *image,const double radius,
1875 const double sigma,const double angle,ExceptionInfo *exception)
1909 assert(image != (Image *) NULL);
1910 assert(image->signature == MagickSignature);
1911 if (image->debug != MagickFalse)
1912 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1913 assert(exception != (ExceptionInfo *) NULL);
1914 width=GetOptimalKernelWidth1D(radius,sigma);
1915 kernel=GetMotionBlurKernel(width,sigma);
1916 if (kernel == (MagickRealType *) NULL)
1917 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1918 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
1919 if (offset == (OffsetInfo *) NULL)
1921 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
1922 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1924 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1925 if (blur_image == (Image *) NULL)
1927 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
1928 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1929 return((Image *) NULL);
1931 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
1933 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
1934 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1935 blur_image=DestroyImage(blur_image);
1936 return((Image *) NULL);
1938 point.x=(double) width*sin(DegreesToRadians(angle));
1939 point.y=(double) width*cos(DegreesToRadians(angle));
1940 for (i=0; i < (ssize_t) width; i++)
1942 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
1943 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
1950 image_view=AcquireVirtualCacheView(image);
1951 motion_view=AcquireVirtualCacheView(image);
1952 blur_view=AcquireAuthenticCacheView(blur_image);
1953 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1954 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1955 dynamic_number_threads(image,image->columns,image->rows,1)
1957 for (y=0; y < (ssize_t) image->rows; y++)
1959 register const Quantum
1968 if (status == MagickFalse)
1970 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1971 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
1973 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1978 for (x=0; x < (ssize_t) image->columns; x++)
1983 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1997 register const Quantum
2000 register MagickRealType
2006 channel=GetPixelChannelChannel(image,i);
2007 traits=GetPixelChannelTraits(image,channel);
2008 blur_traits=GetPixelChannelTraits(blur_image,channel);
2009 if ((traits == UndefinedPixelTrait) ||
2010 (blur_traits == UndefinedPixelTrait))
2012 if (((blur_traits & CopyPixelTrait) != 0) ||
2013 (GetPixelMask(image,p) != 0))
2015 SetPixelChannel(blur_image,channel,p[i],q);
2020 if ((blur_traits & BlendPixelTrait) == 0)
2022 for (j=0; j < (ssize_t) width; j++)
2024 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+
2025 offset[j].y,1,1,exception);
2026 if (r == (const Quantum *) NULL)
2034 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
2039 for (j=0; j < (ssize_t) width; j++)
2041 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+offset[j].y,1,
2043 if (r == (const Quantum *) NULL)
2048 alpha=(double) (QuantumScale*GetPixelAlpha(image,r));
2049 pixel+=(*k)*alpha*r[i];
2053 gamma=PerceptibleReciprocal(gamma);
2054 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2056 p+=GetPixelChannels(image);
2057 q+=GetPixelChannels(blur_image);
2059 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2061 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2066 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2067 #pragma omp critical (MagickCore_MotionBlurImage)
2069 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2070 if (proceed == MagickFalse)
2074 blur_view=DestroyCacheView(blur_view);
2075 motion_view=DestroyCacheView(motion_view);
2076 image_view=DestroyCacheView(image_view);
2077 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2078 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2079 if (status == MagickFalse)
2080 blur_image=DestroyImage(blur_image);
2085 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2089 % P r e v i e w I m a g e %
2093 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2095 % PreviewImage() tiles 9 thumbnails of the specified image with an image
2096 % processing operation applied with varying parameters. This may be helpful
2097 % pin-pointing an appropriate parameter for a particular image processing
2100 % The format of the PreviewImages method is:
2102 % Image *PreviewImages(const Image *image,const PreviewType preview,
2103 % ExceptionInfo *exception)
2105 % A description of each parameter follows:
2107 % o image: the image.
2109 % o preview: the image processing operation.
2111 % o exception: return any errors or warnings in this structure.
2114 MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2115 ExceptionInfo *exception)
2117 #define NumberTiles 9
2118 #define PreviewImageTag "Preview/Image"
2119 #define DefaultPreviewGeometry "204x204+10+10"
2122 factor[MaxTextExtent],
2123 label[MaxTextExtent];
2168 Open output image file.
2170 assert(image != (Image *) NULL);
2171 assert(image->signature == MagickSignature);
2172 if (image->debug != MagickFalse)
2173 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2177 preview_info=AcquireImageInfo();
2178 SetGeometry(image,&geometry);
2179 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2180 &geometry.width,&geometry.height);
2181 images=NewImageList();
2183 GetQuantizeInfo(&quantize_info);
2189 for (i=0; i < NumberTiles; i++)
2191 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2192 if (thumbnail == (Image *) NULL)
2194 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2196 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel,exception);
2197 if (i == (NumberTiles/2))
2199 (void) QueryColorCompliance("#dfdfdf",AllCompliance,
2200 &thumbnail->matte_color,exception);
2201 AppendImageToList(&images,thumbnail);
2209 preview_image=RotateImage(thumbnail,degrees,exception);
2210 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
2216 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
2217 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",
2218 degrees,2.0*degrees);
2223 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2224 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
2225 preview_image=RollImage(thumbnail,x,y,exception);
2226 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
2227 (double) x,(double) y);
2232 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2233 if (preview_image == (Image *) NULL)
2235 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",
2237 (void) ModulateImage(preview_image,factor,exception);
2238 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2241 case SaturationPreview:
2243 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2244 if (preview_image == (Image *) NULL)
2246 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",
2248 (void) ModulateImage(preview_image,factor,exception);
2249 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2252 case BrightnessPreview:
2254 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2255 if (preview_image == (Image *) NULL)
2257 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
2258 (void) ModulateImage(preview_image,factor,exception);
2259 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2265 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2266 if (preview_image == (Image *) NULL)
2269 (void) GammaImage(preview_image,gamma,exception);
2270 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
2275 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2276 if (preview_image != (Image *) NULL)
2277 for (x=0; x < i; x++)
2278 (void) ContrastImage(preview_image,MagickTrue,exception);
2279 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
2285 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2286 if (preview_image == (Image *) NULL)
2288 for (x=0; x < i; x++)
2289 (void) ContrastImage(preview_image,MagickFalse,exception);
2290 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
2294 case GrayscalePreview:
2296 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2297 if (preview_image == (Image *) NULL)
2300 quantize_info.number_colors=colors;
2301 quantize_info.colorspace=GRAYColorspace;
2302 (void) QuantizeImage(&quantize_info,preview_image,exception);
2303 (void) FormatLocaleString(label,MaxTextExtent,
2304 "-colorspace gray -colors %.20g",(double) colors);
2307 case QuantizePreview:
2309 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2310 if (preview_image == (Image *) NULL)
2313 quantize_info.number_colors=colors;
2314 (void) QuantizeImage(&quantize_info,preview_image,exception);
2315 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
2319 case DespecklePreview:
2321 for (x=0; x < (i-1); x++)
2323 preview_image=DespeckleImage(thumbnail,exception);
2324 if (preview_image == (Image *) NULL)
2326 thumbnail=DestroyImage(thumbnail);
2327 thumbnail=preview_image;
2329 preview_image=DespeckleImage(thumbnail,exception);
2330 if (preview_image == (Image *) NULL)
2332 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
2336 case ReduceNoisePreview:
2338 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2339 (size_t) radius,exception);
2340 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
2343 case AddNoisePreview:
2349 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2354 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2359 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2364 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2369 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2374 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2379 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2383 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2384 (size_t) i,exception);
2385 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
2388 case SharpenPreview:
2390 preview_image=SharpenImage(thumbnail,radius,sigma,exception);
2391 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",
2397 preview_image=BlurImage(thumbnail,radius,sigma,exception);
2398 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
2402 case ThresholdPreview:
2404 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2405 if (preview_image == (Image *) NULL)
2407 (void) BilevelImage(thumbnail,(double) (percentage*((double)
2408 QuantumRange+1.0))/100.0,exception);
2409 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",
2410 (double) (percentage*((double) QuantumRange+1.0))/100.0);
2413 case EdgeDetectPreview:
2415 preview_image=EdgeImage(thumbnail,radius,sigma,exception);
2416 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
2421 preview_image=SpreadImage(thumbnail,radius,thumbnail->interpolate,
2423 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",
2427 case SolarizePreview:
2429 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2430 if (preview_image == (Image *) NULL)
2432 (void) SolarizeImage(preview_image,(double) QuantumRange*
2433 percentage/100.0,exception);
2434 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
2435 (QuantumRange*percentage)/100.0);
2441 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2443 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",
2449 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2450 if (preview_image == (Image *) NULL)
2452 geometry.width=(size_t) (2*i+2);
2453 geometry.height=(size_t) (2*i+2);
2456 (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
2457 (void) FormatLocaleString(label,MaxTextExtent,
2458 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
2459 geometry.height,(double) geometry.x,(double) geometry.y);
2462 case SegmentPreview:
2464 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2465 if (preview_image == (Image *) NULL)
2468 (void) SegmentImage(preview_image,sRGBColorspace,MagickFalse,threshold,
2469 threshold,exception);
2470 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
2471 threshold,threshold);
2476 preview_image=SwirlImage(thumbnail,degrees,image->interpolate,
2478 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
2482 case ImplodePreview:
2485 preview_image=ImplodeImage(thumbnail,degrees,image->interpolate,
2487 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
2493 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,
2494 image->interpolate,exception);
2495 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",
2496 0.5*degrees,2.0*degrees);
2499 case OilPaintPreview:
2501 preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2503 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2507 case CharcoalDrawingPreview:
2509 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
2511 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2518 filename[MaxTextExtent];
2526 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2527 if (preview_image == (Image *) NULL)
2529 preview_info->quality=(size_t) percentage;
2530 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
2531 preview_info->quality);
2532 file=AcquireUniqueFileResource(filename);
2535 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
2536 "jpeg:%s",filename);
2537 status=WriteImage(preview_info,preview_image,exception);
2538 if (status != MagickFalse)
2543 (void) CopyMagickString(preview_info->filename,
2544 preview_image->filename,MaxTextExtent);
2545 quality_image=ReadImage(preview_info,exception);
2546 if (quality_image != (Image *) NULL)
2548 preview_image=DestroyImage(preview_image);
2549 preview_image=quality_image;
2552 (void) RelinquishUniqueFileResource(preview_image->filename);
2553 if ((GetBlobSize(preview_image)/1024) >= 1024)
2554 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
2555 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2558 if (GetBlobSize(preview_image) >= 1024)
2559 (void) FormatLocaleString(label,MaxTextExtent,
2560 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
2561 GetBlobSize(preview_image))/1024.0);
2563 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
2564 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
2568 thumbnail=DestroyImage(thumbnail);
2572 if (preview_image == (Image *) NULL)
2574 (void) DeleteImageProperty(preview_image,"label");
2575 (void) SetImageProperty(preview_image,"label",label,exception);
2576 AppendImageToList(&images,preview_image);
2577 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2579 if (proceed == MagickFalse)
2582 if (images == (Image *) NULL)
2584 preview_info=DestroyImageInfo(preview_info);
2585 return((Image *) NULL);
2590 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2591 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2592 montage_info->shadow=MagickTrue;
2593 (void) CloneString(&montage_info->tile,"3x3");
2594 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2595 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2596 montage_image=MontageImages(images,montage_info,exception);
2597 montage_info=DestroyMontageInfo(montage_info);
2598 images=DestroyImageList(images);
2599 if (montage_image == (Image *) NULL)
2600 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2601 if (montage_image->montage != (char *) NULL)
2604 Free image directory.
2606 montage_image->montage=(char *) RelinquishMagickMemory(
2607 montage_image->montage);
2608 if (image->directory != (char *) NULL)
2609 montage_image->directory=(char *) RelinquishMagickMemory(
2610 montage_image->directory);
2612 preview_info=DestroyImageInfo(preview_info);
2613 return(montage_image);
2617 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2621 % R a d i a l B l u r I m a g e %
2625 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2627 % RadialBlurImage() applies a radial blur to the image.
2629 % Andrew Protano contributed this effect.
2631 % The format of the RadialBlurImage method is:
2633 % Image *RadialBlurImage(const Image *image,const double angle,
2634 % ExceptionInfo *exception)
2636 % A description of each parameter follows:
2638 % o image: the image.
2640 % o angle: the angle of the radial blur.
2644 % o exception: return any errors or warnings in this structure.
2647 MagickExport Image *RadialBlurImage(const Image *image,const double angle,
2648 ExceptionInfo *exception)
2684 Allocate blur image.
2686 assert(image != (Image *) NULL);
2687 assert(image->signature == MagickSignature);
2688 if (image->debug != MagickFalse)
2689 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2690 assert(exception != (ExceptionInfo *) NULL);
2691 assert(exception->signature == MagickSignature);
2692 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2693 if (blur_image == (Image *) NULL)
2694 return((Image *) NULL);
2695 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2697 blur_image=DestroyImage(blur_image);
2698 return((Image *) NULL);
2700 blur_center.x=(double) image->columns/2.0;
2701 blur_center.y=(double) image->rows/2.0;
2702 blur_radius=hypot(blur_center.x,blur_center.y);
2703 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
2704 theta=DegreesToRadians(angle)/(double) (n-1);
2705 cos_theta=(double *) AcquireQuantumMemory((size_t) n,
2706 sizeof(*cos_theta));
2707 sin_theta=(double *) AcquireQuantumMemory((size_t) n,
2708 sizeof(*sin_theta));
2709 if ((cos_theta == (double *) NULL) ||
2710 (sin_theta == (double *) NULL))
2712 blur_image=DestroyImage(blur_image);
2713 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2715 offset=theta*(double) (n-1)/2.0;
2716 for (i=0; i < (ssize_t) n; i++)
2718 cos_theta[i]=cos((double) (theta*i-offset));
2719 sin_theta[i]=sin((double) (theta*i-offset));
2726 image_view=AcquireVirtualCacheView(image);
2727 radial_view=AcquireVirtualCacheView(image);
2728 blur_view=AcquireAuthenticCacheView(blur_image);
2729 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2730 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2731 dynamic_number_threads(image,image->columns,image->rows,1)
2733 for (y=0; y < (ssize_t) image->rows; y++)
2735 register const Quantum
2744 if (status == MagickFalse)
2746 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2747 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2749 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2754 for (x=0; x < (ssize_t) image->columns; x++)
2768 center.x=(double) x-blur_center.x;
2769 center.y=(double) y-blur_center.y;
2770 radius=hypot((double) center.x,center.y);
2775 step=(size_t) (blur_radius/radius);
2782 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2795 register const Quantum
2801 channel=GetPixelChannelChannel(image,i);
2802 traits=GetPixelChannelTraits(image,channel);
2803 blur_traits=GetPixelChannelTraits(blur_image,channel);
2804 if ((traits == UndefinedPixelTrait) ||
2805 (blur_traits == UndefinedPixelTrait))
2807 if (((blur_traits & CopyPixelTrait) != 0) ||
2808 (GetPixelMask(image,p) != 0))
2810 SetPixelChannel(blur_image,channel,p[i],q);
2815 if ((blur_traits & BlendPixelTrait) == 0)
2817 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2819 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2820 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2821 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2823 if (r == (const Quantum *) NULL)
2831 gamma=PerceptibleReciprocal(gamma);
2832 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2835 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2837 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2838 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2839 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2841 if (r == (const Quantum *) NULL)
2846 pixel+=GetPixelAlpha(image,r)*r[i];
2847 gamma+=GetPixelAlpha(image,r);
2849 gamma=PerceptibleReciprocal(gamma);
2850 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2852 p+=GetPixelChannels(image);
2853 q+=GetPixelChannels(blur_image);
2855 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2857 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2862 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2863 #pragma omp critical (MagickCore_RadialBlurImage)
2865 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2866 if (proceed == MagickFalse)
2870 blur_view=DestroyCacheView(blur_view);
2871 radial_view=DestroyCacheView(radial_view);
2872 image_view=DestroyCacheView(image_view);
2873 cos_theta=(double *) RelinquishMagickMemory(cos_theta);
2874 sin_theta=(double *) RelinquishMagickMemory(sin_theta);
2875 if (status == MagickFalse)
2876 blur_image=DestroyImage(blur_image);
2881 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2885 % S e l e c t i v e B l u r I m a g e %
2889 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2891 % SelectiveBlurImage() selectively blur pixels within a contrast threshold.
2892 % It is similar to the unsharpen mask that sharpens everything with contrast
2893 % above a certain threshold.
2895 % The format of the SelectiveBlurImage method is:
2897 % Image *SelectiveBlurImage(const Image *image,const double radius,
2898 % const double sigma,const double threshold,ExceptionInfo *exception)
2900 % A description of each parameter follows:
2902 % o image: the image.
2904 % o radius: the radius of the Gaussian, in pixels, not counting the center
2907 % o sigma: the standard deviation of the Gaussian, in pixels.
2909 % o threshold: only pixels within this contrast threshold are included
2910 % in the blur operation.
2912 % o exception: return any errors or warnings in this structure.
2915 MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
2916 const double sigma,const double threshold,ExceptionInfo *exception)
2918 #define SelectiveBlurImageTag "SelectiveBlur/Image"
2952 Initialize blur image attributes.
2954 assert(image != (Image *) NULL);
2955 assert(image->signature == MagickSignature);
2956 if (image->debug != MagickFalse)
2957 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2958 assert(exception != (ExceptionInfo *) NULL);
2959 assert(exception->signature == MagickSignature);
2960 width=GetOptimalKernelWidth1D(radius,sigma);
2961 kernel=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory((size_t)
2962 width,width*sizeof(*kernel)));
2963 if (kernel == (MagickRealType *) 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++]=(MagickRealType) (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 MagickRealType
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 ",(double) *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 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3012 return((Image *) NULL);
3014 luminance_image=CloneImage(image,0,0,MagickTrue,exception);
3015 if (luminance_image == (Image *) NULL)
3017 blur_image=DestroyImage(blur_image);
3018 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3019 return((Image *) NULL);
3021 status=TransformImageColorspace(luminance_image,GRAYColorspace,exception);
3022 if (status == MagickFalse)
3024 luminance_image=DestroyImage(luminance_image);
3025 blur_image=DestroyImage(blur_image);
3026 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3027 return((Image *) NULL);
3030 Threshold blur image.
3034 center=(ssize_t) (GetPixelChannels(image)*(image->columns+width)*(width/2L)+
3035 GetPixelChannels(image)*(width/2L));
3036 image_view=AcquireVirtualCacheView(image);
3037 luminance_view=AcquireVirtualCacheView(luminance_image);
3038 blur_view=AcquireAuthenticCacheView(blur_image);
3039 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3040 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3041 dynamic_number_threads(image,image->columns,image->rows,1)
3043 for (y=0; y < (ssize_t) image->rows; y++)
3051 register const Quantum
3061 if (status == MagickFalse)
3063 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
3064 (width/2L),image->columns+width,width,exception);
3065 l=GetCacheViewVirtualPixels(luminance_view,-((ssize_t) width/2L),y-(ssize_t)
3066 (width/2L),luminance_image->columns+width,width,exception);
3067 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3069 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3074 for (x=0; x < (ssize_t) image->columns; x++)
3082 intensity=GetPixelIntensity(image,p+center);
3083 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3097 register const MagickRealType
3100 register const Quantum
3101 *restrict luminance_pixels,
3110 channel=GetPixelChannelChannel(image,i);
3111 traits=GetPixelChannelTraits(image,channel);
3112 blur_traits=GetPixelChannelTraits(blur_image,channel);
3113 if ((traits == UndefinedPixelTrait) ||
3114 (blur_traits == UndefinedPixelTrait))
3116 if (((blur_traits & CopyPixelTrait) != 0) ||
3117 (GetPixelMask(image,p) != 0))
3119 SetPixelChannel(blur_image,channel,p[center+i],q);
3127 if ((blur_traits & BlendPixelTrait) == 0)
3129 for (v=0; v < (ssize_t) width; v++)
3131 for (u=0; u < (ssize_t) width; u++)
3133 contrast=GetPixelIntensity(luminance_image,luminance_pixels)-
3135 if (fabs(contrast) < threshold)
3137 pixel+=(*k)*pixels[i];
3141 pixels+=GetPixelChannels(image);
3142 luminance_pixels+=GetPixelChannels(luminance_image);
3144 pixels+=image->columns*GetPixelChannels(image);
3145 luminance_pixels+=luminance_image->columns*
3146 GetPixelChannels(luminance_image);
3148 if (fabs((double) gamma) < MagickEpsilon)
3150 SetPixelChannel(blur_image,channel,p[center+i],q);
3153 gamma=PerceptibleReciprocal(gamma);
3154 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3157 for (v=0; v < (ssize_t) width; v++)
3159 for (u=0; u < (ssize_t) width; u++)
3161 contrast=GetPixelIntensity(image,pixels)-intensity;
3162 if (fabs(contrast) < threshold)
3164 alpha=(double) (QuantumScale*
3165 GetPixelAlpha(image,pixels));
3166 pixel+=(*k)*alpha*pixels[i];
3170 pixels+=GetPixelChannels(image);
3171 luminance_pixels+=GetPixelChannels(luminance_image);
3173 pixels+=image->columns*GetPixelChannels(image);
3174 luminance_pixels+=luminance_image->columns*
3175 GetPixelChannels(luminance_image);
3177 if (fabs((double) gamma) < MagickEpsilon)
3179 SetPixelChannel(blur_image,channel,p[center+i],q);
3182 gamma=PerceptibleReciprocal(gamma);
3183 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3185 p+=GetPixelChannels(image);
3186 l+=GetPixelChannels(luminance_image);
3187 q+=GetPixelChannels(blur_image);
3189 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3190 if (sync == MagickFalse)
3192 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3197 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3198 #pragma omp critical (MagickCore_SelectiveBlurImage)
3200 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3202 if (proceed == MagickFalse)
3206 blur_image->type=image->type;
3207 blur_view=DestroyCacheView(blur_view);
3208 image_view=DestroyCacheView(image_view);
3209 luminance_image=DestroyImage(luminance_image);
3210 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3211 if (status == MagickFalse)
3212 blur_image=DestroyImage(blur_image);
3217 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3221 % S h a d e I m a g e %
3225 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3227 % ShadeImage() shines a distant light on an image to create a
3228 % three-dimensional effect. You control the positioning of the light with
3229 % azimuth and elevation; azimuth is measured in degrees off the x axis
3230 % and elevation is measured in pixels above the Z axis.
3232 % The format of the ShadeImage method is:
3234 % Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3235 % const double azimuth,const double elevation,ExceptionInfo *exception)
3237 % A description of each parameter follows:
3239 % o image: the image.
3241 % o gray: A value other than zero shades the intensity of each pixel.
3243 % o azimuth, elevation: Define the light source direction.
3245 % o exception: return any errors or warnings in this structure.
3248 MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3249 const double azimuth,const double elevation,ExceptionInfo *exception)
3251 #define ShadeImageTag "Shade/Image"
3273 Initialize shaded image attributes.
3275 assert(image != (const Image *) NULL);
3276 assert(image->signature == MagickSignature);
3277 if (image->debug != MagickFalse)
3278 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3279 assert(exception != (ExceptionInfo *) NULL);
3280 assert(exception->signature == MagickSignature);
3281 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3282 if (shade_image == (Image *) NULL)
3283 return((Image *) NULL);
3284 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
3286 shade_image=DestroyImage(shade_image);
3287 return((Image *) NULL);
3290 Compute the light vector.
3292 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3293 cos(DegreesToRadians(elevation));
3294 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3295 cos(DegreesToRadians(elevation));
3296 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3302 image_view=AcquireVirtualCacheView(image);
3303 shade_view=AcquireAuthenticCacheView(shade_image);
3304 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3305 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3306 dynamic_number_threads(image,image->columns,image->rows,1)
3308 for (y=0; y < (ssize_t) image->rows; y++)
3318 register const Quantum
3330 if (status == MagickFalse)
3332 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
3333 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3335 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3341 Shade this row of pixels.
3343 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
3344 pre=p+GetPixelChannels(image);
3345 center=pre+(image->columns+2)*GetPixelChannels(image);
3346 post=center+(image->columns+2)*GetPixelChannels(image);
3347 for (x=0; x < (ssize_t) image->columns; x++)
3353 Determine the surface normal and compute shading.
3355 normal.x=(double) (GetPixelIntensity(image,pre-GetPixelChannels(image))+
3356 GetPixelIntensity(image,center-GetPixelChannels(image))+
3357 GetPixelIntensity(image,post-GetPixelChannels(image))-
3358 GetPixelIntensity(image,pre+GetPixelChannels(image))-
3359 GetPixelIntensity(image,center+GetPixelChannels(image))-
3360 GetPixelIntensity(image,post+GetPixelChannels(image)));
3361 normal.y=(double) (GetPixelIntensity(image,post-GetPixelChannels(image))+
3362 GetPixelIntensity(image,post)+GetPixelIntensity(image,post+
3363 GetPixelChannels(image))-GetPixelIntensity(image,pre-
3364 GetPixelChannels(image))-GetPixelIntensity(image,pre)-
3365 GetPixelIntensity(image,pre+GetPixelChannels(image)));
3366 if ((normal.x == 0.0) && (normal.y == 0.0))
3371 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3372 if (distance > MagickEpsilon)
3375 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
3376 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3377 shade=distance/sqrt((double) normal_distance);
3380 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3389 channel=GetPixelChannelChannel(image,i);
3390 traits=GetPixelChannelTraits(image,channel);
3391 shade_traits=GetPixelChannelTraits(shade_image,channel);
3392 if ((traits == UndefinedPixelTrait) ||
3393 (shade_traits == UndefinedPixelTrait))
3395 if (((shade_traits & CopyPixelTrait) != 0) ||
3396 (GetPixelMask(image,p) != 0))
3398 SetPixelChannel(shade_image,channel,center[i],q);
3401 if (gray != MagickFalse)
3403 SetPixelChannel(shade_image,channel,ClampToQuantum(shade),q);
3406 SetPixelChannel(shade_image,channel,ClampToQuantum(QuantumScale*shade*
3409 pre+=GetPixelChannels(image);
3410 center+=GetPixelChannels(image);
3411 post+=GetPixelChannels(image);
3412 q+=GetPixelChannels(shade_image);
3414 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3416 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3421 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3422 #pragma omp critical (MagickCore_ShadeImage)
3424 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3425 if (proceed == MagickFalse)
3429 shade_view=DestroyCacheView(shade_view);
3430 image_view=DestroyCacheView(image_view);
3431 if (status == MagickFalse)
3432 shade_image=DestroyImage(shade_image);
3433 return(shade_image);
3437 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3441 % S h a r p e n I m a g e %
3445 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3447 % SharpenImage() sharpens the image. We convolve the image with a Gaussian
3448 % operator of the given radius and standard deviation (sigma). For
3449 % reasonable results, radius should be larger than sigma. Use a radius of 0
3450 % and SharpenImage() selects a suitable radius for you.
3452 % Using a separable kernel would be faster, but the negative weights cancel
3453 % out on the corners of the kernel producing often undesirable ringing in the
3454 % filtered result; this can be avoided by using a 2D gaussian shaped image
3455 % sharpening kernel instead.
3457 % The format of the SharpenImage method is:
3459 % Image *SharpenImage(const Image *image,const double radius,
3460 % const double sigma,ExceptionInfo *exception)
3462 % A description of each parameter follows:
3464 % o image: the image.
3466 % o radius: the radius of the Gaussian, in pixels, not counting the center
3469 % o sigma: the standard deviation of the Laplacian, in pixels.
3471 % o exception: return any errors or warnings in this structure.
3474 MagickExport Image *SharpenImage(const Image *image,const double radius,
3475 const double sigma,ExceptionInfo *exception)
3497 assert(image != (const Image *) NULL);
3498 assert(image->signature == MagickSignature);
3499 if (image->debug != MagickFalse)
3500 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3501 assert(exception != (ExceptionInfo *) NULL);
3502 assert(exception->signature == MagickSignature);
3503 width=GetOptimalKernelWidth2D(radius,sigma);
3504 kernel_info=AcquireKernelInfo((const char *) NULL);
3505 if (kernel_info == (KernelInfo *) NULL)
3506 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3507 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3508 kernel_info->width=width;
3509 kernel_info->height=width;
3510 kernel_info->signature=MagickSignature;
3511 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
3512 AcquireAlignedMemory(kernel_info->width,kernel_info->width*
3513 sizeof(*kernel_info->values)));
3514 if (kernel_info->values == (MagickRealType *) NULL)
3516 kernel_info=DestroyKernelInfo(kernel_info);
3517 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3520 j=(ssize_t) kernel_info->width/2;
3522 for (v=(-j); v <= j; v++)
3524 for (u=(-j); u <= j; u++)
3526 kernel_info->values[i]=(MagickRealType) (-exp(-((double) u*u+v*v)/(2.0*
3527 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3528 normalize+=kernel_info->values[i];
3532 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
3533 sharp_image=ConvolveImage(image,kernel_info,exception);
3534 kernel_info=DestroyKernelInfo(kernel_info);
3535 return(sharp_image);
3539 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3543 % S p r e a d I m a g e %
3547 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3549 % SpreadImage() is a special effects method that randomly displaces each
3550 % pixel in a block defined by the radius parameter.
3552 % The format of the SpreadImage method is:
3554 % Image *SpreadImage(const Image *image,const double radius,
3555 % const PixelInterpolateMethod method,ExceptionInfo *exception)
3557 % A description of each parameter follows:
3559 % o image: the image.
3561 % o radius: choose a random pixel in a neighborhood of this extent.
3563 % o method: the pixel interpolation method.
3565 % o exception: return any errors or warnings in this structure.
3568 MagickExport Image *SpreadImage(const Image *image,const double radius,
3569 const PixelInterpolateMethod method,ExceptionInfo *exception)
3571 #define SpreadImageTag "Spread/Image"
3587 **restrict random_info;
3595 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3601 Initialize spread image attributes.
3603 assert(image != (Image *) NULL);
3604 assert(image->signature == MagickSignature);
3605 if (image->debug != MagickFalse)
3606 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3607 assert(exception != (ExceptionInfo *) NULL);
3608 assert(exception->signature == MagickSignature);
3609 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3611 if (spread_image == (Image *) NULL)
3612 return((Image *) NULL);
3613 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
3615 spread_image=DestroyImage(spread_image);
3616 return((Image *) NULL);
3623 width=GetOptimalKernelWidth1D(radius,0.5);
3624 random_info=AcquireRandomInfoThreadSet();
3625 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3626 key=GetRandomSecretKey(random_info[0]);
3628 image_view=AcquireVirtualCacheView(image);
3629 spread_view=AcquireAuthenticCacheView(spread_image);
3630 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3631 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3632 dynamic_number_threads(image,image->columns,image->rows,key == ~0UL)
3634 for (y=0; y < (ssize_t) image->rows; y++)
3637 id = GetOpenMPThreadId();
3639 register const Quantum
3648 if (status == MagickFalse)
3650 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3651 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
3653 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3658 for (x=0; x < (ssize_t) image->columns; x++)
3663 point.x=GetPseudoRandomValue(random_info[id]);
3664 point.y=GetPseudoRandomValue(random_info[id]);
3665 status=InterpolatePixelChannels(image,image_view,spread_image,method,
3666 (double) x+width*point.x-0.5,(double) y+width*point.y-0.5,q,exception);
3667 q+=GetPixelChannels(spread_image);
3669 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
3671 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3676 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3677 #pragma omp critical (MagickCore_SpreadImage)
3679 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3680 if (proceed == MagickFalse)
3684 spread_view=DestroyCacheView(spread_view);
3685 image_view=DestroyCacheView(image_view);
3686 random_info=DestroyRandomInfoThreadSet(random_info);
3687 if (status == MagickFalse)
3688 spread_image=DestroyImage(spread_image);
3689 return(spread_image);
3693 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3697 % U n s h a r p M a s k I m a g e %
3701 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3703 % UnsharpMaskImage() sharpens one or more image channels. We convolve the
3704 % image with a Gaussian operator of the given radius and standard deviation
3705 % (sigma). For reasonable results, radius should be larger than sigma. Use a
3706 % radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
3708 % The format of the UnsharpMaskImage method is:
3710 % Image *UnsharpMaskImage(const Image *image,const double radius,
3711 % const double sigma,const double amount,const double threshold,
3712 % ExceptionInfo *exception)
3714 % A description of each parameter follows:
3716 % o image: the image.
3718 % o radius: the radius of the Gaussian, in pixels, not counting the center
3721 % o sigma: the standard deviation of the Gaussian, in pixels.
3723 % o amount: the percentage of the difference between the original and the
3724 % blur image that is added back into the original.
3726 % o threshold: the threshold in pixels needed to apply the diffence amount.
3728 % o exception: return any errors or warnings in this structure.
3731 MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
3732 const double sigma,const double amount,const double threshold,
3733 ExceptionInfo *exception)
3735 #define SharpenImageTag "Sharpen/Image"
3756 assert(image != (const Image *) NULL);
3757 assert(image->signature == MagickSignature);
3758 if (image->debug != MagickFalse)
3759 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3760 assert(exception != (ExceptionInfo *) NULL);
3761 unsharp_image=BlurImage(image,radius,sigma,exception);
3762 if (unsharp_image == (Image *) NULL)
3763 return((Image *) NULL);
3764 quantum_threshold=(double) QuantumRange*threshold;
3770 image_view=AcquireVirtualCacheView(image);
3771 unsharp_view=AcquireAuthenticCacheView(unsharp_image);
3772 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3773 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3774 dynamic_number_threads(image,image->columns,image->rows,1)
3776 for (y=0; y < (ssize_t) image->rows; y++)
3778 register const Quantum
3787 if (status == MagickFalse)
3789 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3790 q=QueueCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
3792 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3797 for (x=0; x < (ssize_t) image->columns; x++)
3802 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3814 channel=GetPixelChannelChannel(image,i);
3815 traits=GetPixelChannelTraits(image,channel);
3816 unsharp_traits=GetPixelChannelTraits(unsharp_image,channel);
3817 if ((traits == UndefinedPixelTrait) ||
3818 (unsharp_traits == UndefinedPixelTrait))
3820 if (((unsharp_traits & CopyPixelTrait) != 0) ||
3821 (GetPixelMask(image,p) != 0))
3823 SetPixelChannel(unsharp_image,channel,p[i],q);
3826 pixel=p[i]-(double) GetPixelChannel(unsharp_image,channel,q);
3827 if (fabs(2.0*pixel) < quantum_threshold)
3828 pixel=(double) p[i];
3830 pixel=(double) p[i]+amount*pixel;
3831 SetPixelChannel(unsharp_image,channel,ClampToQuantum(pixel),q);
3833 p+=GetPixelChannels(image);
3834 q+=GetPixelChannels(unsharp_image);
3836 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
3838 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3843 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3844 #pragma omp critical (MagickCore_UnsharpMaskImage)
3846 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
3847 if (proceed == MagickFalse)
3851 unsharp_image->type=image->type;
3852 unsharp_view=DestroyCacheView(unsharp_view);
3853 image_view=DestroyCacheView(image_view);
3854 if (status == MagickFalse)
3855 unsharp_image=DestroyImage(unsharp_image);
3856 return(unsharp_image);