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/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=MagickEpsilonReciprocal(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,exception);
299 edge_view=AcquireVirtualCacheView(edge_image,exception);
300 blur_view=AcquireAuthenticCacheView(blur_image,exception);
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=MagickEpsilonReciprocal(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=MagickEpsilonReciprocal(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=MagickEpsilonReciprocal(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,exception);
624 edge_view=AcquireVirtualCacheView(edge_image,exception);
625 sharp_view=AcquireAuthenticCacheView(sharp_image,exception);
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=MagickEpsilonReciprocal(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=MagickEpsilonReciprocal(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,exception);
953 blur_view=AcquireAuthenticCacheView(blur_image,exception);
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=MagickEpsilonReciprocal(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,exception);
1078 blur_view=AcquireAuthenticCacheView(blur_image,exception);
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=MagickEpsilonReciprocal(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,exception);
1425 despeckle_view=AcquireAuthenticCacheView(despeckle_image,exception);
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 if (edge_image != (Image *) NULL)
1614 (void) ClampImage(edge_image,exception);
1615 kernel_info=DestroyKernelInfo(kernel_info);
1620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1624 % E m b o s s I m a g e %
1628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1630 % EmbossImage() returns a grayscale image with a three-dimensional effect.
1631 % We convolve the image with a Gaussian operator of the given radius and
1632 % standard deviation (sigma). For reasonable results, radius should be
1633 % larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1636 % The format of the EmbossImage method is:
1638 % Image *EmbossImage(const Image *image,const double radius,
1639 % const double sigma,ExceptionInfo *exception)
1641 % A description of each parameter follows:
1643 % o image: the image.
1645 % o radius: the radius of the pixel neighborhood.
1647 % o sigma: the standard deviation of the Gaussian, in pixels.
1649 % o exception: return any errors or warnings in this structure.
1652 MagickExport Image *EmbossImage(const Image *image,const double radius,
1653 const double sigma,ExceptionInfo *exception)
1673 assert(image != (const Image *) NULL);
1674 assert(image->signature == MagickSignature);
1675 if (image->debug != MagickFalse)
1676 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1677 assert(exception != (ExceptionInfo *) NULL);
1678 assert(exception->signature == MagickSignature);
1679 width=GetOptimalKernelWidth1D(radius,sigma);
1680 kernel_info=AcquireKernelInfo((const char *) NULL);
1681 if (kernel_info == (KernelInfo *) NULL)
1682 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1683 kernel_info->width=width;
1684 kernel_info->height=width;
1685 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
1686 AcquireAlignedMemory(kernel_info->width,kernel_info->width*
1687 sizeof(*kernel_info->values)));
1688 if (kernel_info->values == (MagickRealType *) NULL)
1690 kernel_info=DestroyKernelInfo(kernel_info);
1691 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1693 j=(ssize_t) kernel_info->width/2;
1696 for (v=(-j); v <= j; v++)
1698 for (u=(-j); u <= j; u++)
1700 kernel_info->values[i]=(MagickRealType) (((u < 0) || (v < 0) ? -8.0 :
1701 8.0)*exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
1702 (2.0*MagickPI*MagickSigma*MagickSigma));
1704 kernel_info->values[i]=0.0;
1709 emboss_image=ConvolveImage(image,kernel_info,exception);
1710 kernel_info=DestroyKernelInfo(kernel_info);
1711 if (emboss_image != (Image *) NULL)
1712 (void) EqualizeImage(emboss_image,exception);
1713 return(emboss_image);
1717 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1721 % G a u s s i a n B l u r I m a g e %
1725 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1727 % GaussianBlurImage() blurs an image. We convolve the image with a
1728 % Gaussian operator of the given radius and standard deviation (sigma).
1729 % For reasonable results, the radius should be larger than sigma. Use a
1730 % radius of 0 and GaussianBlurImage() selects a suitable radius for you
1732 % The format of the GaussianBlurImage method is:
1734 % Image *GaussianBlurImage(const Image *image,onst double radius,
1735 % const double sigma,ExceptionInfo *exception)
1737 % A description of each parameter follows:
1739 % o image: the image.
1741 % o radius: the radius of the Gaussian, in pixels, not counting the center
1744 % o sigma: the standard deviation of the Gaussian, in pixels.
1746 % o exception: return any errors or warnings in this structure.
1749 MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
1750 const double sigma,ExceptionInfo *exception)
1769 assert(image != (const Image *) NULL);
1770 assert(image->signature == MagickSignature);
1771 if (image->debug != MagickFalse)
1772 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1773 assert(exception != (ExceptionInfo *) NULL);
1774 assert(exception->signature == MagickSignature);
1775 width=GetOptimalKernelWidth2D(radius,sigma);
1776 kernel_info=AcquireKernelInfo((const char *) NULL);
1777 if (kernel_info == (KernelInfo *) NULL)
1778 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1779 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1780 kernel_info->width=width;
1781 kernel_info->height=width;
1782 kernel_info->signature=MagickSignature;
1783 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
1784 AcquireAlignedMemory(kernel_info->width,kernel_info->width*
1785 sizeof(*kernel_info->values)));
1786 if (kernel_info->values == (MagickRealType *) NULL)
1788 kernel_info=DestroyKernelInfo(kernel_info);
1789 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1791 j=(ssize_t) kernel_info->width/2;
1793 for (v=(-j); v <= j; v++)
1795 for (u=(-j); u <= j; u++)
1797 kernel_info->values[i]=(MagickRealType) (exp(-((double) u*u+v*v)/(2.0*
1798 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
1802 blur_image=ConvolveImage(image,kernel_info,exception);
1803 kernel_info=DestroyKernelInfo(kernel_info);
1808 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1812 % M o t i o n B l u r I m a g e %
1816 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1818 % MotionBlurImage() simulates motion blur. We convolve the image with a
1819 % Gaussian operator of the given radius and standard deviation (sigma).
1820 % For reasonable results, radius should be larger than sigma. Use a
1821 % radius of 0 and MotionBlurImage() selects a suitable radius for you.
1822 % Angle gives the angle of the blurring motion.
1824 % Andrew Protano contributed this effect.
1826 % The format of the MotionBlurImage method is:
1828 % Image *MotionBlurImage(const Image *image,const double radius,
1829 % const double sigma,const double angle,ExceptionInfo *exception)
1831 % A description of each parameter follows:
1833 % o image: the image.
1835 % o radius: the radius of the Gaussian, in pixels, not counting
1838 % o sigma: the standard deviation of the Gaussian, in pixels.
1840 % o angle: Apply the effect along this angle.
1842 % o exception: return any errors or warnings in this structure.
1846 static MagickRealType *GetMotionBlurKernel(const size_t width,
1857 Generate a 1-D convolution kernel.
1859 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1860 kernel=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory((size_t)
1861 width,sizeof(*kernel)));
1862 if (kernel == (MagickRealType *) NULL)
1865 for (i=0; i < (ssize_t) width; i++)
1867 kernel[i]=(MagickRealType) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
1868 MagickSigma)))/(MagickSQ2PI*MagickSigma));
1869 normalize+=kernel[i];
1871 for (i=0; i < (ssize_t) width; i++)
1872 kernel[i]/=normalize;
1876 MagickExport Image *MotionBlurImage(const Image *image,const double radius,
1877 const double sigma,const double angle,ExceptionInfo *exception)
1911 assert(image != (Image *) NULL);
1912 assert(image->signature == MagickSignature);
1913 if (image->debug != MagickFalse)
1914 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1915 assert(exception != (ExceptionInfo *) NULL);
1916 width=GetOptimalKernelWidth1D(radius,sigma);
1917 kernel=GetMotionBlurKernel(width,sigma);
1918 if (kernel == (MagickRealType *) NULL)
1919 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1920 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
1921 if (offset == (OffsetInfo *) NULL)
1923 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
1924 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1926 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1927 if (blur_image == (Image *) NULL)
1929 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
1930 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1931 return((Image *) NULL);
1933 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
1935 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
1936 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1937 blur_image=DestroyImage(blur_image);
1938 return((Image *) NULL);
1940 point.x=(double) width*sin(DegreesToRadians(angle));
1941 point.y=(double) width*cos(DegreesToRadians(angle));
1942 for (i=0; i < (ssize_t) width; i++)
1944 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
1945 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
1952 image_view=AcquireVirtualCacheView(image,exception);
1953 motion_view=AcquireVirtualCacheView(image,exception);
1954 blur_view=AcquireAuthenticCacheView(blur_image,exception);
1955 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1956 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1957 dynamic_number_threads(image,image->columns,image->rows,1)
1959 for (y=0; y < (ssize_t) image->rows; y++)
1961 register const Quantum
1970 if (status == MagickFalse)
1972 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1973 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
1975 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1980 for (x=0; x < (ssize_t) image->columns; x++)
1985 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1999 register const Quantum
2002 register MagickRealType
2008 channel=GetPixelChannelChannel(image,i);
2009 traits=GetPixelChannelTraits(image,channel);
2010 blur_traits=GetPixelChannelTraits(blur_image,channel);
2011 if ((traits == UndefinedPixelTrait) ||
2012 (blur_traits == UndefinedPixelTrait))
2014 if (((blur_traits & CopyPixelTrait) != 0) ||
2015 (GetPixelMask(image,p) != 0))
2017 SetPixelChannel(blur_image,channel,p[i],q);
2022 if ((blur_traits & BlendPixelTrait) == 0)
2024 for (j=0; j < (ssize_t) width; j++)
2026 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+
2027 offset[j].y,1,1,exception);
2028 if (r == (const Quantum *) NULL)
2036 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
2041 for (j=0; j < (ssize_t) width; j++)
2043 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+offset[j].y,1,
2045 if (r == (const Quantum *) NULL)
2050 alpha=(double) (QuantumScale*GetPixelAlpha(image,r));
2051 pixel+=(*k)*alpha*r[i];
2055 gamma=MagickEpsilonReciprocal(gamma);
2056 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2058 p+=GetPixelChannels(image);
2059 q+=GetPixelChannels(blur_image);
2061 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2063 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2068 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2069 #pragma omp critical (MagickCore_MotionBlurImage)
2071 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2072 if (proceed == MagickFalse)
2076 blur_view=DestroyCacheView(blur_view);
2077 motion_view=DestroyCacheView(motion_view);
2078 image_view=DestroyCacheView(image_view);
2079 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2080 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2081 if (status == MagickFalse)
2082 blur_image=DestroyImage(blur_image);
2087 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2091 % P r e v i e w I m a g e %
2095 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2097 % PreviewImage() tiles 9 thumbnails of the specified image with an image
2098 % processing operation applied with varying parameters. This may be helpful
2099 % pin-pointing an appropriate parameter for a particular image processing
2102 % The format of the PreviewImages method is:
2104 % Image *PreviewImages(const Image *image,const PreviewType preview,
2105 % ExceptionInfo *exception)
2107 % A description of each parameter follows:
2109 % o image: the image.
2111 % o preview: the image processing operation.
2113 % o exception: return any errors or warnings in this structure.
2116 MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2117 ExceptionInfo *exception)
2119 #define NumberTiles 9
2120 #define PreviewImageTag "Preview/Image"
2121 #define DefaultPreviewGeometry "204x204+10+10"
2124 factor[MaxTextExtent],
2125 label[MaxTextExtent];
2170 Open output image file.
2172 assert(image != (Image *) NULL);
2173 assert(image->signature == MagickSignature);
2174 if (image->debug != MagickFalse)
2175 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2179 preview_info=AcquireImageInfo();
2180 SetGeometry(image,&geometry);
2181 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2182 &geometry.width,&geometry.height);
2183 images=NewImageList();
2185 GetQuantizeInfo(&quantize_info);
2191 for (i=0; i < NumberTiles; i++)
2193 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2194 if (thumbnail == (Image *) NULL)
2196 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2198 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel,exception);
2199 if (i == (NumberTiles/2))
2201 (void) QueryColorCompliance("#dfdfdf",AllCompliance,
2202 &thumbnail->matte_color,exception);
2203 AppendImageToList(&images,thumbnail);
2211 preview_image=RotateImage(thumbnail,degrees,exception);
2212 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
2218 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
2219 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",
2220 degrees,2.0*degrees);
2225 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2226 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
2227 preview_image=RollImage(thumbnail,x,y,exception);
2228 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
2229 (double) x,(double) y);
2234 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2235 if (preview_image == (Image *) NULL)
2237 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",
2239 (void) ModulateImage(preview_image,factor,exception);
2240 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2243 case SaturationPreview:
2245 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2246 if (preview_image == (Image *) NULL)
2248 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",
2250 (void) ModulateImage(preview_image,factor,exception);
2251 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2254 case BrightnessPreview:
2256 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2257 if (preview_image == (Image *) NULL)
2259 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
2260 (void) ModulateImage(preview_image,factor,exception);
2261 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2267 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2268 if (preview_image == (Image *) NULL)
2271 (void) GammaImage(preview_image,gamma,exception);
2272 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
2277 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2278 if (preview_image != (Image *) NULL)
2279 for (x=0; x < i; x++)
2280 (void) ContrastImage(preview_image,MagickTrue,exception);
2281 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
2287 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2288 if (preview_image == (Image *) NULL)
2290 for (x=0; x < i; x++)
2291 (void) ContrastImage(preview_image,MagickFalse,exception);
2292 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
2296 case GrayscalePreview:
2298 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2299 if (preview_image == (Image *) NULL)
2302 quantize_info.number_colors=colors;
2303 quantize_info.colorspace=GRAYColorspace;
2304 (void) QuantizeImage(&quantize_info,preview_image,exception);
2305 (void) FormatLocaleString(label,MaxTextExtent,
2306 "-colorspace gray -colors %.20g",(double) colors);
2309 case QuantizePreview:
2311 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2312 if (preview_image == (Image *) NULL)
2315 quantize_info.number_colors=colors;
2316 (void) QuantizeImage(&quantize_info,preview_image,exception);
2317 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
2321 case DespecklePreview:
2323 for (x=0; x < (i-1); x++)
2325 preview_image=DespeckleImage(thumbnail,exception);
2326 if (preview_image == (Image *) NULL)
2328 thumbnail=DestroyImage(thumbnail);
2329 thumbnail=preview_image;
2331 preview_image=DespeckleImage(thumbnail,exception);
2332 if (preview_image == (Image *) NULL)
2334 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
2338 case ReduceNoisePreview:
2340 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2341 (size_t) radius,exception);
2342 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
2345 case AddNoisePreview:
2351 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2356 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2361 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2366 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2371 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2376 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2381 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2385 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2386 (size_t) i,exception);
2387 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
2390 case SharpenPreview:
2392 preview_image=SharpenImage(thumbnail,radius,sigma,exception);
2393 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",
2399 preview_image=BlurImage(thumbnail,radius,sigma,exception);
2400 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
2404 case ThresholdPreview:
2406 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2407 if (preview_image == (Image *) NULL)
2409 (void) BilevelImage(thumbnail,(double) (percentage*((double)
2410 QuantumRange+1.0))/100.0,exception);
2411 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",
2412 (double) (percentage*((double) QuantumRange+1.0))/100.0);
2415 case EdgeDetectPreview:
2417 preview_image=EdgeImage(thumbnail,radius,sigma,exception);
2418 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
2423 preview_image=SpreadImage(thumbnail,radius,thumbnail->interpolate,
2425 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",
2429 case SolarizePreview:
2431 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2432 if (preview_image == (Image *) NULL)
2434 (void) SolarizeImage(preview_image,(double) QuantumRange*
2435 percentage/100.0,exception);
2436 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
2437 (QuantumRange*percentage)/100.0);
2443 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2445 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",
2451 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2452 if (preview_image == (Image *) NULL)
2454 geometry.width=(size_t) (2*i+2);
2455 geometry.height=(size_t) (2*i+2);
2458 (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
2459 (void) FormatLocaleString(label,MaxTextExtent,
2460 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
2461 geometry.height,(double) geometry.x,(double) geometry.y);
2464 case SegmentPreview:
2466 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2467 if (preview_image == (Image *) NULL)
2470 (void) SegmentImage(preview_image,sRGBColorspace,MagickFalse,threshold,
2471 threshold,exception);
2472 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
2473 threshold,threshold);
2478 preview_image=SwirlImage(thumbnail,degrees,image->interpolate,
2480 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
2484 case ImplodePreview:
2487 preview_image=ImplodeImage(thumbnail,degrees,image->interpolate,
2489 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
2495 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,
2496 image->interpolate,exception);
2497 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",
2498 0.5*degrees,2.0*degrees);
2501 case OilPaintPreview:
2503 preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2505 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2509 case CharcoalDrawingPreview:
2511 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
2513 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2520 filename[MaxTextExtent];
2528 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2529 if (preview_image == (Image *) NULL)
2531 preview_info->quality=(size_t) percentage;
2532 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
2533 preview_info->quality);
2534 file=AcquireUniqueFileResource(filename);
2537 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
2538 "jpeg:%s",filename);
2539 status=WriteImage(preview_info,preview_image,exception);
2540 if (status != MagickFalse)
2545 (void) CopyMagickString(preview_info->filename,
2546 preview_image->filename,MaxTextExtent);
2547 quality_image=ReadImage(preview_info,exception);
2548 if (quality_image != (Image *) NULL)
2550 preview_image=DestroyImage(preview_image);
2551 preview_image=quality_image;
2554 (void) RelinquishUniqueFileResource(preview_image->filename);
2555 if ((GetBlobSize(preview_image)/1024) >= 1024)
2556 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
2557 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2560 if (GetBlobSize(preview_image) >= 1024)
2561 (void) FormatLocaleString(label,MaxTextExtent,
2562 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
2563 GetBlobSize(preview_image))/1024.0);
2565 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
2566 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
2570 thumbnail=DestroyImage(thumbnail);
2574 if (preview_image == (Image *) NULL)
2576 (void) DeleteImageProperty(preview_image,"label");
2577 (void) SetImageProperty(preview_image,"label",label,exception);
2578 AppendImageToList(&images,preview_image);
2579 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2581 if (proceed == MagickFalse)
2584 if (images == (Image *) NULL)
2586 preview_info=DestroyImageInfo(preview_info);
2587 return((Image *) NULL);
2592 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2593 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2594 montage_info->shadow=MagickTrue;
2595 (void) CloneString(&montage_info->tile,"3x3");
2596 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2597 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2598 montage_image=MontageImages(images,montage_info,exception);
2599 montage_info=DestroyMontageInfo(montage_info);
2600 images=DestroyImageList(images);
2601 if (montage_image == (Image *) NULL)
2602 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2603 if (montage_image->montage != (char *) NULL)
2606 Free image directory.
2608 montage_image->montage=(char *) RelinquishMagickMemory(
2609 montage_image->montage);
2610 if (image->directory != (char *) NULL)
2611 montage_image->directory=(char *) RelinquishMagickMemory(
2612 montage_image->directory);
2614 preview_info=DestroyImageInfo(preview_info);
2615 return(montage_image);
2619 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2623 % R a d i a l B l u r I m a g e %
2627 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2629 % RadialBlurImage() applies a radial blur to the image.
2631 % Andrew Protano contributed this effect.
2633 % The format of the RadialBlurImage method is:
2635 % Image *RadialBlurImage(const Image *image,const double angle,
2636 % ExceptionInfo *exception)
2638 % A description of each parameter follows:
2640 % o image: the image.
2642 % o angle: the angle of the radial blur.
2646 % o exception: return any errors or warnings in this structure.
2649 MagickExport Image *RadialBlurImage(const Image *image,const double angle,
2650 ExceptionInfo *exception)
2686 Allocate blur image.
2688 assert(image != (Image *) NULL);
2689 assert(image->signature == MagickSignature);
2690 if (image->debug != MagickFalse)
2691 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2692 assert(exception != (ExceptionInfo *) NULL);
2693 assert(exception->signature == MagickSignature);
2694 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2695 if (blur_image == (Image *) NULL)
2696 return((Image *) NULL);
2697 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2699 blur_image=DestroyImage(blur_image);
2700 return((Image *) NULL);
2702 blur_center.x=(double) image->columns/2.0;
2703 blur_center.y=(double) image->rows/2.0;
2704 blur_radius=hypot(blur_center.x,blur_center.y);
2705 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
2706 theta=DegreesToRadians(angle)/(double) (n-1);
2707 cos_theta=(double *) AcquireQuantumMemory((size_t) n,
2708 sizeof(*cos_theta));
2709 sin_theta=(double *) AcquireQuantumMemory((size_t) n,
2710 sizeof(*sin_theta));
2711 if ((cos_theta == (double *) NULL) ||
2712 (sin_theta == (double *) NULL))
2714 blur_image=DestroyImage(blur_image);
2715 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2717 offset=theta*(double) (n-1)/2.0;
2718 for (i=0; i < (ssize_t) n; i++)
2720 cos_theta[i]=cos((double) (theta*i-offset));
2721 sin_theta[i]=sin((double) (theta*i-offset));
2728 image_view=AcquireVirtualCacheView(image,exception);
2729 radial_view=AcquireVirtualCacheView(image,exception);
2730 blur_view=AcquireAuthenticCacheView(blur_image,exception);
2731 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2732 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2733 dynamic_number_threads(image,image->columns,image->rows,1)
2735 for (y=0; y < (ssize_t) image->rows; y++)
2737 register const Quantum
2746 if (status == MagickFalse)
2748 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2749 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2751 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2756 for (x=0; x < (ssize_t) image->columns; x++)
2770 center.x=(double) x-blur_center.x;
2771 center.y=(double) y-blur_center.y;
2772 radius=hypot((double) center.x,center.y);
2777 step=(size_t) (blur_radius/radius);
2784 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2797 register const Quantum
2803 channel=GetPixelChannelChannel(image,i);
2804 traits=GetPixelChannelTraits(image,channel);
2805 blur_traits=GetPixelChannelTraits(blur_image,channel);
2806 if ((traits == UndefinedPixelTrait) ||
2807 (blur_traits == UndefinedPixelTrait))
2809 if (((blur_traits & CopyPixelTrait) != 0) ||
2810 (GetPixelMask(image,p) != 0))
2812 SetPixelChannel(blur_image,channel,p[i],q);
2817 if ((blur_traits & BlendPixelTrait) == 0)
2819 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2821 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2822 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2823 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2825 if (r == (const Quantum *) NULL)
2833 gamma=MagickEpsilonReciprocal(gamma);
2834 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2837 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2839 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2840 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2841 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2843 if (r == (const Quantum *) NULL)
2848 pixel+=GetPixelAlpha(image,r)*r[i];
2849 gamma+=GetPixelAlpha(image,r);
2851 gamma=MagickEpsilonReciprocal(gamma);
2852 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2854 p+=GetPixelChannels(image);
2855 q+=GetPixelChannels(blur_image);
2857 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2859 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2864 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2865 #pragma omp critical (MagickCore_RadialBlurImage)
2867 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2868 if (proceed == MagickFalse)
2872 blur_view=DestroyCacheView(blur_view);
2873 radial_view=DestroyCacheView(radial_view);
2874 image_view=DestroyCacheView(image_view);
2875 cos_theta=(double *) RelinquishMagickMemory(cos_theta);
2876 sin_theta=(double *) RelinquishMagickMemory(sin_theta);
2877 if (status == MagickFalse)
2878 blur_image=DestroyImage(blur_image);
2883 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2887 % S e l e c t i v e B l u r I m a g e %
2891 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2893 % SelectiveBlurImage() selectively blur pixels within a contrast threshold.
2894 % It is similar to the unsharpen mask that sharpens everything with contrast
2895 % above a certain threshold.
2897 % The format of the SelectiveBlurImage method is:
2899 % Image *SelectiveBlurImage(const Image *image,const double radius,
2900 % const double sigma,const double threshold,ExceptionInfo *exception)
2902 % A description of each parameter follows:
2904 % o image: the image.
2906 % o radius: the radius of the Gaussian, in pixels, not counting the center
2909 % o sigma: the standard deviation of the Gaussian, in pixels.
2911 % o threshold: only pixels within this contrast threshold are included
2912 % in the blur operation.
2914 % o exception: return any errors or warnings in this structure.
2917 MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
2918 const double sigma,const double threshold,ExceptionInfo *exception)
2920 #define SelectiveBlurImageTag "SelectiveBlur/Image"
2954 Initialize blur image attributes.
2956 assert(image != (Image *) NULL);
2957 assert(image->signature == MagickSignature);
2958 if (image->debug != MagickFalse)
2959 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2960 assert(exception != (ExceptionInfo *) NULL);
2961 assert(exception->signature == MagickSignature);
2962 width=GetOptimalKernelWidth1D(radius,sigma);
2963 kernel=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory((size_t)
2964 width,width*sizeof(*kernel)));
2965 if (kernel == (MagickRealType *) NULL)
2966 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2967 j=(ssize_t) width/2;
2969 for (v=(-j); v <= j; v++)
2971 for (u=(-j); u <= j; u++)
2972 kernel[i++]=(MagickRealType) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
2973 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
2975 if (image->debug != MagickFalse)
2978 format[MaxTextExtent],
2981 register const MagickRealType
2988 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
2989 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
2991 message=AcquireString("");
2993 for (v=0; v < (ssize_t) width; v++)
2996 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
2997 (void) ConcatenateString(&message,format);
2998 for (u=0; u < (ssize_t) width; u++)
3000 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",(double) *k++);
3001 (void) ConcatenateString(&message,format);
3003 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
3005 message=DestroyString(message);
3007 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3008 if (blur_image == (Image *) NULL)
3009 return((Image *) NULL);
3010 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
3012 blur_image=DestroyImage(blur_image);
3013 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3014 return((Image *) NULL);
3016 luminance_image=CloneImage(image,0,0,MagickTrue,exception);
3017 if (luminance_image == (Image *) NULL)
3019 blur_image=DestroyImage(blur_image);
3020 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3021 return((Image *) NULL);
3023 status=TransformImageColorspace(luminance_image,GRAYColorspace,exception);
3024 if (status == MagickFalse)
3026 luminance_image=DestroyImage(luminance_image);
3027 blur_image=DestroyImage(blur_image);
3028 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3029 return((Image *) NULL);
3032 Threshold blur image.
3036 center=(ssize_t) (GetPixelChannels(image)*(image->columns+width)*(width/2L)+
3037 GetPixelChannels(image)*(width/2L));
3038 image_view=AcquireVirtualCacheView(image,exception);
3039 luminance_view=AcquireVirtualCacheView(luminance_image,exception);
3040 blur_view=AcquireAuthenticCacheView(blur_image,exception);
3041 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3042 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3043 dynamic_number_threads(image,image->columns,image->rows,1)
3045 for (y=0; y < (ssize_t) image->rows; y++)
3053 register const Quantum
3063 if (status == MagickFalse)
3065 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
3066 (width/2L),image->columns+width,width,exception);
3067 l=GetCacheViewVirtualPixels(luminance_view,-((ssize_t) width/2L),y-(ssize_t)
3068 (width/2L),luminance_image->columns+width,width,exception);
3069 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3071 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3076 for (x=0; x < (ssize_t) image->columns; x++)
3084 intensity=GetPixelIntensity(image,p+center);
3085 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3099 register const MagickRealType
3102 register const Quantum
3103 *restrict luminance_pixels,
3112 channel=GetPixelChannelChannel(image,i);
3113 traits=GetPixelChannelTraits(image,channel);
3114 blur_traits=GetPixelChannelTraits(blur_image,channel);
3115 if ((traits == UndefinedPixelTrait) ||
3116 (blur_traits == UndefinedPixelTrait))
3118 if (((blur_traits & CopyPixelTrait) != 0) ||
3119 (GetPixelMask(image,p) != 0))
3121 SetPixelChannel(blur_image,channel,p[center+i],q);
3129 if ((blur_traits & BlendPixelTrait) == 0)
3131 for (v=0; v < (ssize_t) width; v++)
3133 for (u=0; u < (ssize_t) width; u++)
3135 contrast=GetPixelIntensity(luminance_image,luminance_pixels)-
3137 if (fabs(contrast) < threshold)
3139 pixel+=(*k)*pixels[i];
3143 pixels+=GetPixelChannels(image);
3144 luminance_pixels+=GetPixelChannels(luminance_image);
3146 pixels+=image->columns*GetPixelChannels(image);
3147 luminance_pixels+=luminance_image->columns*
3148 GetPixelChannels(luminance_image);
3150 if (fabs((double) gamma) < MagickEpsilon)
3152 SetPixelChannel(blur_image,channel,p[center+i],q);
3155 gamma=MagickEpsilonReciprocal(gamma);
3156 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3159 for (v=0; v < (ssize_t) width; v++)
3161 for (u=0; u < (ssize_t) width; u++)
3163 contrast=GetPixelIntensity(image,pixels)-intensity;
3164 if (fabs(contrast) < threshold)
3166 alpha=(double) (QuantumScale*
3167 GetPixelAlpha(image,pixels));
3168 pixel+=(*k)*alpha*pixels[i];
3172 pixels+=GetPixelChannels(image);
3173 luminance_pixels+=GetPixelChannels(luminance_image);
3175 pixels+=image->columns*GetPixelChannels(image);
3176 luminance_pixels+=luminance_image->columns*
3177 GetPixelChannels(luminance_image);
3179 if (fabs((double) gamma) < MagickEpsilon)
3181 SetPixelChannel(blur_image,channel,p[center+i],q);
3184 gamma=MagickEpsilonReciprocal(gamma);
3185 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3187 p+=GetPixelChannels(image);
3188 l+=GetPixelChannels(luminance_image);
3189 q+=GetPixelChannels(blur_image);
3191 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3192 if (sync == MagickFalse)
3194 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3199 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3200 #pragma omp critical (MagickCore_SelectiveBlurImage)
3202 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3204 if (proceed == MagickFalse)
3208 blur_image->type=image->type;
3209 blur_view=DestroyCacheView(blur_view);
3210 image_view=DestroyCacheView(image_view);
3211 luminance_image=DestroyImage(luminance_image);
3212 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3213 if (status == MagickFalse)
3214 blur_image=DestroyImage(blur_image);
3219 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3223 % S h a d e I m a g e %
3227 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3229 % ShadeImage() shines a distant light on an image to create a
3230 % three-dimensional effect. You control the positioning of the light with
3231 % azimuth and elevation; azimuth is measured in degrees off the x axis
3232 % and elevation is measured in pixels above the Z axis.
3234 % The format of the ShadeImage method is:
3236 % Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3237 % const double azimuth,const double elevation,ExceptionInfo *exception)
3239 % A description of each parameter follows:
3241 % o image: the image.
3243 % o gray: A value other than zero shades the intensity of each pixel.
3245 % o azimuth, elevation: Define the light source direction.
3247 % o exception: return any errors or warnings in this structure.
3250 MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3251 const double azimuth,const double elevation,ExceptionInfo *exception)
3253 #define ShadeImageTag "Shade/Image"
3275 Initialize shaded image attributes.
3277 assert(image != (const Image *) NULL);
3278 assert(image->signature == MagickSignature);
3279 if (image->debug != MagickFalse)
3280 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3281 assert(exception != (ExceptionInfo *) NULL);
3282 assert(exception->signature == MagickSignature);
3283 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3284 if (shade_image == (Image *) NULL)
3285 return((Image *) NULL);
3286 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
3288 shade_image=DestroyImage(shade_image);
3289 return((Image *) NULL);
3292 Compute the light vector.
3294 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3295 cos(DegreesToRadians(elevation));
3296 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3297 cos(DegreesToRadians(elevation));
3298 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3304 image_view=AcquireVirtualCacheView(image,exception);
3305 shade_view=AcquireAuthenticCacheView(shade_image,exception);
3306 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3307 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3308 dynamic_number_threads(image,image->columns,image->rows,1)
3310 for (y=0; y < (ssize_t) image->rows; y++)
3320 register const Quantum
3332 if (status == MagickFalse)
3334 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
3335 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3337 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3343 Shade this row of pixels.
3345 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
3346 pre=p+GetPixelChannels(image);
3347 center=pre+(image->columns+2)*GetPixelChannels(image);
3348 post=center+(image->columns+2)*GetPixelChannels(image);
3349 for (x=0; x < (ssize_t) image->columns; x++)
3355 Determine the surface normal and compute shading.
3357 normal.x=(double) (GetPixelIntensity(image,pre-GetPixelChannels(image))+
3358 GetPixelIntensity(image,center-GetPixelChannels(image))+
3359 GetPixelIntensity(image,post-GetPixelChannels(image))-
3360 GetPixelIntensity(image,pre+GetPixelChannels(image))-
3361 GetPixelIntensity(image,center+GetPixelChannels(image))-
3362 GetPixelIntensity(image,post+GetPixelChannels(image)));
3363 normal.y=(double) (GetPixelIntensity(image,post-GetPixelChannels(image))+
3364 GetPixelIntensity(image,post)+GetPixelIntensity(image,post+
3365 GetPixelChannels(image))-GetPixelIntensity(image,pre-
3366 GetPixelChannels(image))-GetPixelIntensity(image,pre)-
3367 GetPixelIntensity(image,pre+GetPixelChannels(image)));
3368 if ((normal.x == 0.0) && (normal.y == 0.0))
3373 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3374 if (distance > MagickEpsilon)
3377 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
3378 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3379 shade=distance/sqrt((double) normal_distance);
3382 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3391 channel=GetPixelChannelChannel(image,i);
3392 traits=GetPixelChannelTraits(image,channel);
3393 shade_traits=GetPixelChannelTraits(shade_image,channel);
3394 if ((traits == UndefinedPixelTrait) ||
3395 (shade_traits == UndefinedPixelTrait))
3397 if (((shade_traits & CopyPixelTrait) != 0) ||
3398 (GetPixelMask(image,p) != 0))
3400 SetPixelChannel(shade_image,channel,center[i],q);
3403 if (gray != MagickFalse)
3405 SetPixelChannel(shade_image,channel,ClampToQuantum(shade),q);
3408 SetPixelChannel(shade_image,channel,ClampToQuantum(QuantumScale*shade*
3411 pre+=GetPixelChannels(image);
3412 center+=GetPixelChannels(image);
3413 post+=GetPixelChannels(image);
3414 q+=GetPixelChannels(shade_image);
3416 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3418 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3423 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3424 #pragma omp critical (MagickCore_ShadeImage)
3426 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3427 if (proceed == MagickFalse)
3431 shade_view=DestroyCacheView(shade_view);
3432 image_view=DestroyCacheView(image_view);
3433 if (status == MagickFalse)
3434 shade_image=DestroyImage(shade_image);
3435 return(shade_image);
3439 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3443 % S h a r p e n I m a g e %
3447 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3449 % SharpenImage() sharpens the image. We convolve the image with a Gaussian
3450 % operator of the given radius and standard deviation (sigma). For
3451 % reasonable results, radius should be larger than sigma. Use a radius of 0
3452 % and SharpenImage() selects a suitable radius for you.
3454 % Using a separable kernel would be faster, but the negative weights cancel
3455 % out on the corners of the kernel producing often undesirable ringing in the
3456 % filtered result; this can be avoided by using a 2D gaussian shaped image
3457 % sharpening kernel instead.
3459 % The format of the SharpenImage method is:
3461 % Image *SharpenImage(const Image *image,const double radius,
3462 % const double sigma,ExceptionInfo *exception)
3464 % A description of each parameter follows:
3466 % o image: the image.
3468 % o radius: the radius of the Gaussian, in pixels, not counting the center
3471 % o sigma: the standard deviation of the Laplacian, in pixels.
3473 % o exception: return any errors or warnings in this structure.
3476 MagickExport Image *SharpenImage(const Image *image,const double radius,
3477 const double sigma,ExceptionInfo *exception)
3499 assert(image != (const Image *) NULL);
3500 assert(image->signature == MagickSignature);
3501 if (image->debug != MagickFalse)
3502 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3503 assert(exception != (ExceptionInfo *) NULL);
3504 assert(exception->signature == MagickSignature);
3505 width=GetOptimalKernelWidth2D(radius,sigma);
3506 kernel_info=AcquireKernelInfo((const char *) NULL);
3507 if (kernel_info == (KernelInfo *) NULL)
3508 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3509 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3510 kernel_info->width=width;
3511 kernel_info->height=width;
3512 kernel_info->signature=MagickSignature;
3513 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
3514 AcquireAlignedMemory(kernel_info->width,kernel_info->width*
3515 sizeof(*kernel_info->values)));
3516 if (kernel_info->values == (MagickRealType *) NULL)
3518 kernel_info=DestroyKernelInfo(kernel_info);
3519 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3522 j=(ssize_t) kernel_info->width/2;
3524 for (v=(-j); v <= j; v++)
3526 for (u=(-j); u <= j; u++)
3528 kernel_info->values[i]=(MagickRealType) (-exp(-((double) u*u+v*v)/(2.0*
3529 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3530 normalize+=kernel_info->values[i];
3534 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
3535 sharp_image=ConvolveImage(image,kernel_info,exception);
3536 if (sharp_image != (Image *) NULL)
3537 (void) ClampImage(sharp_image,exception);
3538 kernel_info=DestroyKernelInfo(kernel_info);
3539 return(sharp_image);
3543 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3547 % S p r e a d I m a g e %
3551 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3553 % SpreadImage() is a special effects method that randomly displaces each
3554 % pixel in a block defined by the radius parameter.
3556 % The format of the SpreadImage method is:
3558 % Image *SpreadImage(const Image *image,const double radius,
3559 % const PixelInterpolateMethod method,ExceptionInfo *exception)
3561 % A description of each parameter follows:
3563 % o image: the image.
3565 % o radius: choose a random pixel in a neighborhood of this extent.
3567 % o method: the pixel interpolation method.
3569 % o exception: return any errors or warnings in this structure.
3572 MagickExport Image *SpreadImage(const Image *image,const double radius,
3573 const PixelInterpolateMethod method,ExceptionInfo *exception)
3575 #define SpreadImageTag "Spread/Image"
3591 **restrict random_info;
3599 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3605 Initialize spread image attributes.
3607 assert(image != (Image *) NULL);
3608 assert(image->signature == MagickSignature);
3609 if (image->debug != MagickFalse)
3610 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3611 assert(exception != (ExceptionInfo *) NULL);
3612 assert(exception->signature == MagickSignature);
3613 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3615 if (spread_image == (Image *) NULL)
3616 return((Image *) NULL);
3617 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
3619 spread_image=DestroyImage(spread_image);
3620 return((Image *) NULL);
3627 width=GetOptimalKernelWidth1D(radius,0.5);
3628 random_info=AcquireRandomInfoThreadSet();
3629 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3630 key=GetRandomSecretKey(random_info[0]);
3632 image_view=AcquireVirtualCacheView(image,exception);
3633 spread_view=AcquireAuthenticCacheView(spread_image,exception);
3634 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3635 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3636 dynamic_number_threads(image,image->columns,image->rows,key == ~0UL)
3638 for (y=0; y < (ssize_t) image->rows; y++)
3641 id = GetOpenMPThreadId();
3643 register const Quantum
3652 if (status == MagickFalse)
3654 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3655 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
3657 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3662 for (x=0; x < (ssize_t) image->columns; x++)
3667 point.x=GetPseudoRandomValue(random_info[id]);
3668 point.y=GetPseudoRandomValue(random_info[id]);
3669 status=InterpolatePixelChannels(image,image_view,spread_image,method,
3670 (double) x+width*point.x-0.5,(double) y+width*point.y-0.5,q,exception);
3671 q+=GetPixelChannels(spread_image);
3673 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
3675 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3680 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3681 #pragma omp critical (MagickCore_SpreadImage)
3683 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3684 if (proceed == MagickFalse)
3688 spread_view=DestroyCacheView(spread_view);
3689 image_view=DestroyCacheView(image_view);
3690 random_info=DestroyRandomInfoThreadSet(random_info);
3691 return(spread_image);
3695 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3699 % U n s h a r p M a s k I m a g e %
3703 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3705 % UnsharpMaskImage() sharpens one or more image channels. We convolve the
3706 % image with a Gaussian operator of the given radius and standard deviation
3707 % (sigma). For reasonable results, radius should be larger than sigma. Use a
3708 % radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
3710 % The format of the UnsharpMaskImage method is:
3712 % Image *UnsharpMaskImage(const Image *image,const double radius,
3713 % const double sigma,const double amount,const double threshold,
3714 % ExceptionInfo *exception)
3716 % A description of each parameter follows:
3718 % o image: the image.
3720 % o radius: the radius of the Gaussian, in pixels, not counting the center
3723 % o sigma: the standard deviation of the Gaussian, in pixels.
3725 % o amount: the percentage of the difference between the original and the
3726 % blur image that is added back into the original.
3728 % o threshold: the threshold in pixels needed to apply the diffence amount.
3730 % o exception: return any errors or warnings in this structure.
3733 MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
3734 const double sigma,const double amount,const double threshold,
3735 ExceptionInfo *exception)
3737 #define SharpenImageTag "Sharpen/Image"
3758 assert(image != (const Image *) NULL);
3759 assert(image->signature == MagickSignature);
3760 if (image->debug != MagickFalse)
3761 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3762 assert(exception != (ExceptionInfo *) NULL);
3763 unsharp_image=BlurImage(image,radius,sigma,exception);
3764 if (unsharp_image == (Image *) NULL)
3765 return((Image *) NULL);
3766 quantum_threshold=(double) QuantumRange*threshold;
3772 image_view=AcquireVirtualCacheView(image,exception);
3773 unsharp_view=AcquireAuthenticCacheView(unsharp_image,exception);
3774 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3775 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3776 dynamic_number_threads(image,image->columns,image->rows,1)
3778 for (y=0; y < (ssize_t) image->rows; y++)
3780 register const Quantum
3789 if (status == MagickFalse)
3791 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3792 q=QueueCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
3794 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3799 for (x=0; x < (ssize_t) image->columns; x++)
3804 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3816 channel=GetPixelChannelChannel(image,i);
3817 traits=GetPixelChannelTraits(image,channel);
3818 unsharp_traits=GetPixelChannelTraits(unsharp_image,channel);
3819 if ((traits == UndefinedPixelTrait) ||
3820 (unsharp_traits == UndefinedPixelTrait))
3822 if (((unsharp_traits & CopyPixelTrait) != 0) ||
3823 (GetPixelMask(image,p) != 0))
3825 SetPixelChannel(unsharp_image,channel,p[i],q);
3828 pixel=p[i]-(double) GetPixelChannel(unsharp_image,channel,q);
3829 if (fabs(2.0*pixel) < quantum_threshold)
3830 pixel=(double) p[i];
3832 pixel=(double) p[i]+amount*pixel;
3833 SetPixelChannel(unsharp_image,channel,ClampToQuantum(pixel),q);
3835 p+=GetPixelChannels(image);
3836 q+=GetPixelChannels(unsharp_image);
3838 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
3840 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3845 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3846 #pragma omp critical (MagickCore_UnsharpMaskImage)
3848 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
3849 if (proceed == MagickFalse)
3853 unsharp_image->type=image->type;
3854 unsharp_view=DestroyCacheView(unsharp_view);
3855 if (unsharp_image != (Image *) NULL)
3856 (void) ClampImage(unsharp_image,exception);
3857 image_view=DestroyCacheView(image_view);
3858 if (status == MagickFalse)
3859 unsharp_image=DestroyImage(unsharp_image);
3860 return(unsharp_image);