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-2011 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/draw.h"
53 #include "MagickCore/enhance.h"
54 #include "MagickCore/exception.h"
55 #include "MagickCore/exception-private.h"
56 #include "MagickCore/effect.h"
57 #include "MagickCore/fx.h"
58 #include "MagickCore/gem.h"
59 #include "MagickCore/gem-private.h"
60 #include "MagickCore/geometry.h"
61 #include "MagickCore/image-private.h"
62 #include "MagickCore/list.h"
63 #include "MagickCore/log.h"
64 #include "MagickCore/memory_.h"
65 #include "MagickCore/monitor.h"
66 #include "MagickCore/monitor-private.h"
67 #include "MagickCore/montage.h"
68 #include "MagickCore/morphology.h"
69 #include "MagickCore/paint.h"
70 #include "MagickCore/pixel-accessor.h"
71 #include "MagickCore/property.h"
72 #include "MagickCore/quantize.h"
73 #include "MagickCore/quantum.h"
74 #include "MagickCore/quantum-private.h"
75 #include "MagickCore/random_.h"
76 #include "MagickCore/random-private.h"
77 #include "MagickCore/resample.h"
78 #include "MagickCore/resample-private.h"
79 #include "MagickCore/resize.h"
80 #include "MagickCore/resource_.h"
81 #include "MagickCore/segment.h"
82 #include "MagickCore/shear.h"
83 #include "MagickCore/signature-private.h"
84 #include "MagickCore/string_.h"
85 #include "MagickCore/thread-private.h"
86 #include "MagickCore/transform.h"
87 #include "MagickCore/threshold.h"
90 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
94 % A d a p t i v e B l u r I m a g e %
98 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
100 % AdaptiveBlurImage() adaptively blurs the image by blurring less
101 % intensely near image edges and more intensely far from edges. We blur the
102 % image with a Gaussian operator of the given radius and standard deviation
103 % (sigma). For reasonable results, radius should be larger than sigma. Use a
104 % radius of 0 and AdaptiveBlurImage() selects a suitable radius for you.
106 % The format of the AdaptiveBlurImage method is:
108 % Image *AdaptiveBlurImage(const Image *image,const double radius,
109 % const double sigma,const double bias,ExceptionInfo *exception)
111 % A description of each parameter follows:
113 % o image: the image.
115 % o radius: the radius of the Gaussian, in pixels, not counting the center
118 % o sigma: the standard deviation of the Laplacian, in pixels.
122 % o exception: return any errors or warnings in this structure.
126 MagickExport MagickBooleanType AdaptiveLevelImage(Image *image,
127 const char *levels,ExceptionInfo *exception)
146 if (levels == (char *) NULL)
148 flags=ParseGeometry(levels,&geometry_info);
149 black_point=geometry_info.rho;
150 white_point=(double) QuantumRange;
151 if ((flags & SigmaValue) != 0)
152 white_point=geometry_info.sigma;
154 if ((flags & XiValue) != 0)
155 gamma=geometry_info.xi;
156 if ((flags & PercentValue) != 0)
158 black_point*=(double) image->columns*image->rows/100.0;
159 white_point*=(double) image->columns*image->rows/100.0;
161 if ((flags & SigmaValue) == 0)
162 white_point=(double) QuantumRange-black_point;
163 if ((flags & AspectValue ) == 0)
164 status=LevelImage(image,black_point,white_point,gamma,exception);
166 status=LevelizeImage(image,black_point,white_point,gamma,exception);
170 MagickExport Image *AdaptiveBlurImage(const Image *image,
171 const double radius,const double sigma,const double bias,
172 ExceptionInfo *exception)
174 #define AdaptiveBlurImageTag "Convolve/Image"
175 #define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
210 assert(image != (const Image *) NULL);
211 assert(image->signature == MagickSignature);
212 if (image->debug != MagickFalse)
213 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
214 assert(exception != (ExceptionInfo *) NULL);
215 assert(exception->signature == MagickSignature);
216 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
217 if (blur_image == (Image *) NULL)
218 return((Image *) NULL);
219 if (fabs(sigma) <= MagickEpsilon)
221 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
223 blur_image=DestroyImage(blur_image);
224 return((Image *) NULL);
227 Edge detect the image brighness channel, level, blur, and level again.
229 edge_image=EdgeImage(image,radius,sigma,exception);
230 if (edge_image == (Image *) NULL)
232 blur_image=DestroyImage(blur_image);
233 return((Image *) NULL);
235 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
236 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,bias,exception);
237 if (gaussian_image != (Image *) NULL)
239 edge_image=DestroyImage(edge_image);
240 edge_image=gaussian_image;
242 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
244 Create a set of kernels from maximum (radius,sigma) to minimum.
246 width=GetOptimalKernelWidth2D(radius,sigma);
247 kernel=(double **) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
248 if (kernel == (double **) NULL)
250 edge_image=DestroyImage(edge_image);
251 blur_image=DestroyImage(blur_image);
252 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
254 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
255 for (i=0; i < (ssize_t) width; i+=2)
257 kernel[i]=(double *) AcquireQuantumMemory((size_t) (width-i),(width-i)*
259 if (kernel[i] == (double *) NULL)
262 j=(ssize_t) (width-i)/2;
264 for (v=(-j); v <= j; v++)
266 for (u=(-j); u <= j; u++)
268 kernel[i][k]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
269 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
270 normalize+=kernel[i][k];
274 if (fabs(normalize) <= MagickEpsilon)
276 normalize=1.0/normalize;
277 for (k=0; k < (j*j); k++)
278 kernel[i][k]=normalize*kernel[i][k];
280 if (i < (ssize_t) width)
282 for (i-=2; i >= 0; i-=2)
283 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
284 kernel=(double **) RelinquishMagickMemory(kernel);
285 edge_image=DestroyImage(edge_image);
286 blur_image=DestroyImage(blur_image);
287 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
290 Adaptively blur image.
294 image_view=AcquireCacheView(image);
295 edge_view=AcquireCacheView(edge_image);
296 blur_view=AcquireCacheView(blur_image);
297 #if defined(MAGICKCORE_OPENMP_SUPPORT)
298 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
300 for (y=0; y < (ssize_t) blur_image->rows; y++)
302 register const Quantum
311 if (status == MagickFalse)
313 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
314 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
316 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
321 for (x=0; x < (ssize_t) blur_image->columns; x++)
323 register const Quantum
333 j=(ssize_t) ceil((double) width*QuantumScale*
334 GetPixelIntensity(edge_image,r)-0.5);
338 if (j > (ssize_t) width)
342 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
343 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
344 if (p == (const Quantum *) NULL)
346 center=(ssize_t) GetPixelChannels(image)*(width-j)*
347 ((width-j)/2L)+GetPixelChannels(image)*((width-j)/2L);
348 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
362 register const double
365 register const Quantum
374 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
375 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
376 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
377 if ((traits == UndefinedPixelTrait) ||
378 (blur_traits == UndefinedPixelTrait))
380 if ((blur_traits & CopyPixelTrait) != 0)
382 q[channel]=p[center+i];
389 if ((blur_traits & BlendPixelTrait) == 0)
394 for (v=0; v < (ssize_t) (width-j); v++)
396 for (u=0; u < (ssize_t) (width-j); u++)
398 pixel+=(*k)*pixels[i];
401 pixels+=GetPixelChannels(image);
404 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
405 q[channel]=ClampToQuantum(gamma*pixel);
411 for (v=0; v < (ssize_t) (width-j); v++)
413 for (u=0; u < (ssize_t) (width-j); u++)
415 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
416 pixel+=(*k)*alpha*pixels[i];
419 pixels+=GetPixelChannels(image);
422 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
423 q[channel]=ClampToQuantum(gamma*pixel);
425 q+=GetPixelChannels(blur_image);
426 r+=GetPixelChannels(edge_image);
428 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
430 if (image->progress_monitor != (MagickProgressMonitor) NULL)
435 #if defined(MAGICKCORE_OPENMP_SUPPORT)
436 #pragma omp critical (MagickCore_AdaptiveBlurImage)
438 proceed=SetImageProgress(image,AdaptiveBlurImageTag,progress++,
440 if (proceed == MagickFalse)
444 blur_image->type=image->type;
445 blur_view=DestroyCacheView(blur_view);
446 edge_view=DestroyCacheView(edge_view);
447 image_view=DestroyCacheView(image_view);
448 edge_image=DestroyImage(edge_image);
449 for (i=0; i < (ssize_t) width; i+=2)
450 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
451 kernel=(double **) RelinquishMagickMemory(kernel);
452 if (status == MagickFalse)
453 blur_image=DestroyImage(blur_image);
458 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
462 % A d a p t i v e S h a r p e n I m a g e %
466 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
468 % AdaptiveSharpenImage() adaptively sharpens the image by sharpening more
469 % intensely near image edges and less intensely far from edges. We sharpen the
470 % image with a Gaussian operator of the given radius and standard deviation
471 % (sigma). For reasonable results, radius should be larger than sigma. Use a
472 % radius of 0 and AdaptiveSharpenImage() selects a suitable radius for you.
474 % The format of the AdaptiveSharpenImage method is:
476 % Image *AdaptiveSharpenImage(const Image *image,const double radius,
477 % const double sigma,const double bias,ExceptionInfo *exception)
479 % A description of each parameter follows:
481 % o image: the image.
483 % o radius: the radius of the Gaussian, in pixels, not counting the center
486 % o sigma: the standard deviation of the Laplacian, in pixels.
490 % o exception: return any errors or warnings in this structure.
493 MagickExport Image *AdaptiveSharpenImage(const Image *image,const double radius,
494 const double sigma,const double bias,ExceptionInfo *exception)
496 #define AdaptiveSharpenImageTag "Convolve/Image"
497 #define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
532 assert(image != (const Image *) NULL);
533 assert(image->signature == MagickSignature);
534 if (image->debug != MagickFalse)
535 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
536 assert(exception != (ExceptionInfo *) NULL);
537 assert(exception->signature == MagickSignature);
538 sharp_image=CloneImage(image,0,0,MagickTrue,exception);
539 if (sharp_image == (Image *) NULL)
540 return((Image *) NULL);
541 if (fabs(sigma) <= MagickEpsilon)
543 if (SetImageStorageClass(sharp_image,DirectClass,exception) == MagickFalse)
545 sharp_image=DestroyImage(sharp_image);
546 return((Image *) NULL);
549 Edge detect the image brighness channel, level, sharp, and level again.
551 edge_image=EdgeImage(image,radius,sigma,exception);
552 if (edge_image == (Image *) NULL)
554 sharp_image=DestroyImage(sharp_image);
555 return((Image *) NULL);
557 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
558 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,bias,exception);
559 if (gaussian_image != (Image *) NULL)
561 edge_image=DestroyImage(edge_image);
562 edge_image=gaussian_image;
564 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
566 Create a set of kernels from maximum (radius,sigma) to minimum.
568 width=GetOptimalKernelWidth2D(radius,sigma);
569 kernel=(double **) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
570 if (kernel == (double **) NULL)
572 edge_image=DestroyImage(edge_image);
573 sharp_image=DestroyImage(sharp_image);
574 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
576 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
577 for (i=0; i < (ssize_t) width; i+=2)
579 kernel[i]=(double *) AcquireQuantumMemory((size_t) (width-i),(width-i)*
581 if (kernel[i] == (double *) NULL)
584 j=(ssize_t) (width-i)/2;
586 for (v=(-j); v <= j; v++)
588 for (u=(-j); u <= j; u++)
590 kernel[i][k]=(double) (-exp(-((double) u*u+v*v)/(2.0*MagickSigma*
591 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
592 normalize+=kernel[i][k];
596 if (fabs(normalize) <= MagickEpsilon)
598 normalize=1.0/normalize;
599 for (k=0; k < (j*j); k++)
600 kernel[i][k]=normalize*kernel[i][k];
602 if (i < (ssize_t) width)
604 for (i-=2; i >= 0; i-=2)
605 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
606 kernel=(double **) RelinquishMagickMemory(kernel);
607 edge_image=DestroyImage(edge_image);
608 sharp_image=DestroyImage(sharp_image);
609 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
612 Adaptively sharpen image.
616 image_view=AcquireCacheView(image);
617 edge_view=AcquireCacheView(edge_image);
618 sharp_view=AcquireCacheView(sharp_image);
619 #if defined(MAGICKCORE_OPENMP_SUPPORT)
620 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
622 for (y=0; y < (ssize_t) sharp_image->rows; y++)
624 register const Quantum
633 if (status == MagickFalse)
635 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
636 q=QueueCacheViewAuthenticPixels(sharp_view,0,y,sharp_image->columns,1,
638 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
643 for (x=0; x < (ssize_t) sharp_image->columns; x++)
645 register const Quantum
655 j=(ssize_t) ceil((double) width*QuantumScale*
656 GetPixelIntensity(edge_image,r)-0.5);
660 if (j > (ssize_t) width)
664 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
665 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
666 if (p == (const Quantum *) NULL)
668 center=(ssize_t) GetPixelChannels(image)*(width-j)*
669 ((width-j)/2L)+GetPixelChannels(image)*((width-j)/2);
670 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
684 register const double
687 register const Quantum
696 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
697 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
698 sharp_traits=GetPixelChannelMapTraits(sharp_image,channel);
699 if ((traits == UndefinedPixelTrait) ||
700 (sharp_traits == UndefinedPixelTrait))
702 if ((sharp_traits & CopyPixelTrait) != 0)
704 q[channel]=p[center+i];
711 if ((sharp_traits & BlendPixelTrait) == 0)
716 for (v=0; v < (ssize_t) (width-j); v++)
718 for (u=0; u < (ssize_t) (width-j); u++)
720 pixel+=(*k)*pixels[i];
723 pixels+=GetPixelChannels(image);
726 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
727 q[channel]=ClampToQuantum(gamma*pixel);
733 for (v=0; v < (ssize_t) (width-j); v++)
735 for (u=0; u < (ssize_t) (width-j); u++)
737 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
738 pixel+=(*k)*alpha*pixels[i];
741 pixels+=GetPixelChannels(image);
744 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
745 q[channel]=ClampToQuantum(gamma*pixel);
747 q+=GetPixelChannels(sharp_image);
748 r+=GetPixelChannels(edge_image);
750 if (SyncCacheViewAuthenticPixels(sharp_view,exception) == MagickFalse)
752 if (image->progress_monitor != (MagickProgressMonitor) NULL)
757 #if defined(MAGICKCORE_OPENMP_SUPPORT)
758 #pragma omp critical (MagickCore_AdaptiveSharpenImage)
760 proceed=SetImageProgress(image,AdaptiveSharpenImageTag,progress++,
762 if (proceed == MagickFalse)
766 sharp_image->type=image->type;
767 sharp_view=DestroyCacheView(sharp_view);
768 edge_view=DestroyCacheView(edge_view);
769 image_view=DestroyCacheView(image_view);
770 edge_image=DestroyImage(edge_image);
771 for (i=0; i < (ssize_t) width; i+=2)
772 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
773 kernel=(double **) RelinquishMagickMemory(kernel);
774 if (status == MagickFalse)
775 sharp_image=DestroyImage(sharp_image);
780 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
784 % B l u r I m a g e %
788 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
790 % BlurImage() blurs an image. We convolve the image with a Gaussian operator
791 % of the given radius and standard deviation (sigma). For reasonable results,
792 % the radius should be larger than sigma. Use a radius of 0 and BlurImage()
793 % selects a suitable radius for you.
795 % BlurImage() differs from GaussianBlurImage() in that it uses a separable
796 % kernel which is faster but mathematically equivalent to the non-separable
799 % The format of the BlurImage method is:
801 % Image *BlurImage(const Image *image,const double radius,
802 % const double sigma,const double bias,ExceptionInfo *exception)
804 % A description of each parameter follows:
806 % o image: the image.
808 % o radius: the radius of the Gaussian, in pixels, not counting the center
811 % o sigma: the standard deviation of the Gaussian, in pixels.
815 % o exception: return any errors or warnings in this structure.
819 static double *GetBlurKernel(const size_t width,const double sigma)
833 Generate a 1-D convolution kernel.
835 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
836 kernel=(double *) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
837 if (kernel == (double *) NULL)
842 for (k=(-j); k <= j; k++)
844 kernel[i]=(double) (exp(-((double) k*k)/(2.0*MagickSigma*MagickSigma))/
845 (MagickSQ2PI*MagickSigma));
846 normalize+=kernel[i];
849 for (i=0; i < (ssize_t) width; i++)
850 kernel[i]/=normalize;
854 MagickExport Image *BlurImage(const Image *image,const double radius,
855 const double sigma,const double bias,ExceptionInfo *exception)
857 #define BlurImageTag "Blur/Image"
887 Initialize blur image attributes.
889 assert(image != (Image *) NULL);
890 assert(image->signature == MagickSignature);
891 if (image->debug != MagickFalse)
892 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
893 assert(exception != (ExceptionInfo *) NULL);
894 assert(exception->signature == MagickSignature);
895 blur_image=CloneImage(image,0,0,MagickTrue,exception);
896 if (blur_image == (Image *) NULL)
897 return((Image *) NULL);
898 if (fabs(sigma) <= MagickEpsilon)
900 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
902 blur_image=DestroyImage(blur_image);
903 return((Image *) NULL);
905 width=GetOptimalKernelWidth1D(radius,sigma);
906 kernel=GetBlurKernel(width,sigma);
907 if (kernel == (double *) NULL)
909 blur_image=DestroyImage(blur_image);
910 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
912 if (image->debug != MagickFalse)
915 format[MaxTextExtent],
918 register const double
921 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
922 " BlurImage with %.20g kernel:",(double) width);
923 message=AcquireString("");
925 for (i=0; i < (ssize_t) width; i++)
928 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) i);
929 (void) ConcatenateString(&message,format);
930 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
931 (void) ConcatenateString(&message,format);
932 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
934 message=DestroyString(message);
941 center=(ssize_t) GetPixelChannels(image)*(width/2L);
942 image_view=AcquireCacheView(image);
943 blur_view=AcquireCacheView(blur_image);
944 #if defined(MAGICKCORE_OPENMP_SUPPORT)
945 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
947 for (y=0; y < (ssize_t) image->rows; y++)
949 register const Quantum
958 if (status == MagickFalse)
960 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y,
961 image->columns+width,1,exception);
962 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
964 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
969 for (x=0; x < (ssize_t) image->columns; x++)
974 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
988 register const double
991 register const Quantum
997 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
998 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
999 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
1000 if ((traits == UndefinedPixelTrait) ||
1001 (blur_traits == UndefinedPixelTrait))
1003 if ((blur_traits & CopyPixelTrait) != 0)
1005 q[channel]=p[center+i];
1011 if ((blur_traits & BlendPixelTrait) == 0)
1016 for (u=0; u < (ssize_t) width; u++)
1018 pixel+=(*k)*pixels[i];
1020 pixels+=GetPixelChannels(image);
1022 q[channel]=ClampToQuantum(pixel);
1029 for (u=0; u < (ssize_t) width; u++)
1031 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
1032 pixel+=(*k)*alpha*pixels[i];
1035 pixels+=GetPixelChannels(image);
1037 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1038 q[channel]=ClampToQuantum(gamma*pixel);
1040 p+=GetPixelChannels(image);
1041 q+=GetPixelChannels(blur_image);
1043 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1045 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1050 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1051 #pragma omp critical (MagickCore_BlurImage)
1053 proceed=SetImageProgress(image,BlurImageTag,progress++,blur_image->rows+
1054 blur_image->columns);
1055 if (proceed == MagickFalse)
1059 blur_view=DestroyCacheView(blur_view);
1060 image_view=DestroyCacheView(image_view);
1064 image_view=AcquireCacheView(blur_image);
1065 blur_view=AcquireCacheView(blur_image);
1066 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1067 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1069 for (x=0; x < (ssize_t) image->columns; x++)
1071 register const Quantum
1080 if (status == MagickFalse)
1082 p=GetCacheViewVirtualPixels(image_view,x,-((ssize_t) width/2L),1,
1083 image->rows+width,exception);
1084 q=GetCacheViewAuthenticPixels(blur_view,x,0,1,blur_image->rows,exception);
1085 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1090 for (y=0; y < (ssize_t) image->rows; y++)
1095 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1109 register const double
1112 register const Quantum
1118 traits=GetPixelChannelMapTraits(blur_image,(PixelChannel) i);
1119 channel=GetPixelChannelMapChannel(blur_image,(PixelChannel) i);
1120 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
1121 if ((traits == UndefinedPixelTrait) ||
1122 (blur_traits == UndefinedPixelTrait))
1124 if ((blur_traits & CopyPixelTrait) != 0)
1126 q[channel]=p[center+i];
1132 if ((blur_traits & BlendPixelTrait) == 0)
1137 for (u=0; u < (ssize_t) width; u++)
1139 pixel+=(*k)*pixels[i];
1141 pixels+=GetPixelChannels(blur_image);
1143 q[channel]=ClampToQuantum(pixel);
1150 for (u=0; u < (ssize_t) width; u++)
1152 alpha=(MagickRealType) (QuantumScale*
1153 GetPixelAlpha(blur_image,pixels));
1154 pixel+=(*k)*alpha*pixels[i];
1157 pixels+=GetPixelChannels(blur_image);
1159 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1160 q[channel]=ClampToQuantum(gamma*pixel);
1162 p+=GetPixelChannels(blur_image);
1163 q+=GetPixelChannels(blur_image);
1165 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1167 if (blur_image->progress_monitor != (MagickProgressMonitor) NULL)
1172 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1173 #pragma omp critical (MagickCore_BlurImage)
1175 proceed=SetImageProgress(blur_image,BlurImageTag,progress++,
1176 blur_image->rows+blur_image->columns);
1177 if (proceed == MagickFalse)
1181 blur_view=DestroyCacheView(blur_view);
1182 image_view=DestroyCacheView(image_view);
1183 kernel=(double *) RelinquishMagickMemory(kernel);
1184 if (status == MagickFalse)
1185 blur_image=DestroyImage(blur_image);
1186 blur_image->type=image->type;
1191 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1195 % C o n v o l v e I m a g e %
1199 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1201 % ConvolveImage() applies a custom convolution kernel to the image.
1203 % The format of the ConvolveImage method is:
1205 % Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
1206 % ExceptionInfo *exception)
1208 % A description of each parameter follows:
1210 % o image: the image.
1212 % o kernel: the filtering kernel.
1214 % o exception: return any errors or warnings in this structure.
1217 MagickExport Image *ConvolveImage(const Image *image,
1218 const KernelInfo *kernel_info,ExceptionInfo *exception)
1220 #define ConvolveImageTag "Convolve/Image"
1240 Initialize convolve image attributes.
1242 assert(image != (Image *) NULL);
1243 assert(image->signature == MagickSignature);
1244 if (image->debug != MagickFalse)
1245 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1246 assert(exception != (ExceptionInfo *) NULL);
1247 assert(exception->signature == MagickSignature);
1248 if ((kernel_info->width % 2) == 0)
1249 ThrowImageException(OptionError,"KernelWidthMustBeAnOddNumber");
1250 convolve_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1252 if (convolve_image == (Image *) NULL)
1253 return((Image *) NULL);
1254 if (SetImageStorageClass(convolve_image,DirectClass,exception) == MagickFalse)
1256 convolve_image=DestroyImage(convolve_image);
1257 return((Image *) NULL);
1259 if (image->debug != MagickFalse)
1262 format[MaxTextExtent],
1265 register const double
1274 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1275 " ConvolveImage with %.20gx%.20g kernel:",(double) kernel_info->width,
1276 (double) kernel_info->height);
1277 message=AcquireString("");
1278 k=kernel_info->values;
1279 for (v=0; v < (ssize_t) kernel_info->width; v++)
1282 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
1283 (void) ConcatenateString(&message,format);
1284 for (u=0; u < (ssize_t) kernel_info->height; u++)
1286 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
1287 (void) ConcatenateString(&message,format);
1289 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
1291 message=DestroyString(message);
1296 center=(ssize_t) GetPixelChannels(image)*(image->columns+kernel_info->width)*
1297 (kernel_info->height/2L)+GetPixelChannels(image)*(kernel_info->width/2L);
1300 image_view=AcquireCacheView(image);
1301 convolve_view=AcquireCacheView(convolve_image);
1302 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1303 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1305 for (y=0; y < (ssize_t) image->rows; y++)
1307 register const Quantum
1316 if (status == MagickFalse)
1318 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) kernel_info->width/2L),y-
1319 (ssize_t) (kernel_info->height/2L),image->columns+kernel_info->width,
1320 kernel_info->height,exception);
1321 q=QueueCacheViewAuthenticPixels(convolve_view,0,y,convolve_image->columns,1,
1323 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1328 for (x=0; x < (ssize_t) image->columns; x++)
1333 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1347 register const double
1350 register const Quantum
1359 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1360 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
1361 convolve_traits=GetPixelChannelMapTraits(convolve_image,channel);
1362 if ((traits == UndefinedPixelTrait) ||
1363 (convolve_traits == UndefinedPixelTrait))
1365 if ((convolve_traits & CopyPixelTrait) != 0)
1367 q[channel]=p[center+i];
1370 k=kernel_info->values;
1372 pixel=kernel_info->bias;
1373 if ((convolve_traits & BlendPixelTrait) == 0)
1378 for (v=0; v < (ssize_t) kernel_info->height; v++)
1380 for (u=0; u < (ssize_t) kernel_info->width; u++)
1382 pixel+=(*k)*pixels[i];
1384 pixels+=GetPixelChannels(image);
1386 pixels+=image->columns*GetPixelChannels(image);
1388 q[channel]=ClampToQuantum(pixel);
1395 for (v=0; v < (ssize_t) kernel_info->height; v++)
1397 for (u=0; u < (ssize_t) kernel_info->width; u++)
1399 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
1400 pixel+=(*k)*alpha*pixels[i];
1403 pixels+=GetPixelChannels(image);
1405 pixels+=image->columns*GetPixelChannels(image);
1407 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1408 q[channel]=ClampToQuantum(gamma*pixel);
1410 p+=GetPixelChannels(image);
1411 q+=GetPixelChannels(convolve_image);
1413 if (SyncCacheViewAuthenticPixels(convolve_view,exception) == MagickFalse)
1415 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1420 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1421 #pragma omp critical (MagickCore_ConvolveImage)
1423 proceed=SetImageProgress(image,ConvolveImageTag,progress++,image->rows);
1424 if (proceed == MagickFalse)
1428 convolve_image->type=image->type;
1429 convolve_view=DestroyCacheView(convolve_view);
1430 image_view=DestroyCacheView(image_view);
1431 if (status == MagickFalse)
1432 convolve_image=DestroyImage(convolve_image);
1433 return(convolve_image);
1437 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1441 % D e s p e c k l e I m a g e %
1445 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1447 % DespeckleImage() reduces the speckle noise in an image while perserving the
1448 % edges of the original image.
1450 % The format of the DespeckleImage method is:
1452 % Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1454 % A description of each parameter follows:
1456 % o image: the image.
1458 % o exception: return any errors or warnings in this structure.
1462 static void Hull(const ssize_t x_offset,const ssize_t y_offset,
1463 const size_t columns,const size_t rows,Quantum *f,Quantum *g,
1481 assert(f != (Quantum *) NULL);
1482 assert(g != (Quantum *) NULL);
1485 r=p+(y_offset*((ssize_t) columns+2)+x_offset);
1486 for (y=0; y < (ssize_t) rows; y++)
1492 for (x=(ssize_t) columns; x != 0; x--)
1494 v=(MagickRealType) (*p);
1495 if ((MagickRealType) *r >= (v+(MagickRealType) ScaleCharToQuantum(2)))
1496 v+=ScaleCharToQuantum(1);
1503 for (x=(ssize_t) columns; x != 0; x--)
1505 v=(MagickRealType) (*p);
1506 if ((MagickRealType) *r <= (v-(MagickRealType) ScaleCharToQuantum(2)))
1507 v-=(ssize_t) ScaleCharToQuantum(1);
1519 r=q+(y_offset*((ssize_t) columns+2)+x_offset);
1520 s=q-(y_offset*((ssize_t) columns+2)+x_offset);
1521 for (y=0; y < (ssize_t) rows; y++)
1528 for (x=(ssize_t) columns; x != 0; x--)
1530 v=(MagickRealType) (*q);
1531 if (((MagickRealType) *s >=
1532 (v+(MagickRealType) ScaleCharToQuantum(2))) &&
1533 ((MagickRealType) *r > v))
1534 v+=ScaleCharToQuantum(1);
1542 for (x=(ssize_t) columns; x != 0; x--)
1544 v=(MagickRealType) (*q);
1545 if (((MagickRealType) *s <=
1546 (v-(MagickRealType) ScaleCharToQuantum(2))) &&
1547 ((MagickRealType) *r < v))
1548 v-=(MagickRealType) ScaleCharToQuantum(1);
1562 MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1564 #define DespeckleImageTag "Despeckle/Image"
1586 static const ssize_t
1587 X[4] = {0, 1, 1,-1},
1588 Y[4] = {1, 0, 1, 1};
1591 Allocate despeckled image.
1593 assert(image != (const Image *) NULL);
1594 assert(image->signature == MagickSignature);
1595 if (image->debug != MagickFalse)
1596 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1597 assert(exception != (ExceptionInfo *) NULL);
1598 assert(exception->signature == MagickSignature);
1599 despeckle_image=CloneImage(image,0,0,MagickTrue,exception);
1600 if (despeckle_image == (Image *) NULL)
1601 return((Image *) NULL);
1602 status=SetImageStorageClass(despeckle_image,DirectClass,exception);
1603 if (status == MagickFalse)
1605 despeckle_image=DestroyImage(despeckle_image);
1606 return((Image *) NULL);
1609 Allocate image buffers.
1611 length=(size_t) ((image->columns+2)*(image->rows+2));
1612 pixels=(Quantum *) AcquireQuantumMemory(length,2*sizeof(*pixels));
1613 buffers=(Quantum *) AcquireQuantumMemory(length,2*sizeof(*pixels));
1614 if ((pixels == (Quantum *) NULL) || (buffers == (Quantum *) NULL))
1616 if (buffers != (Quantum *) NULL)
1617 buffers=(Quantum *) RelinquishMagickMemory(buffers);
1618 if (pixels != (Quantum *) NULL)
1619 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1620 despeckle_image=DestroyImage(despeckle_image);
1621 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1624 Reduce speckle in the image.
1627 image_view=AcquireCacheView(image);
1628 despeckle_view=AcquireCacheView(despeckle_image);
1629 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1650 if (status == MagickFalse)
1652 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1653 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
1654 despeckle_traits=GetPixelChannelMapTraits(despeckle_image,channel);
1655 if ((traits == UndefinedPixelTrait) ||
1656 (despeckle_traits == UndefinedPixelTrait))
1658 if ((despeckle_traits & CopyPixelTrait) != 0)
1661 (void) ResetMagickMemory(pixel,0,length*sizeof(*pixel));
1663 j=(ssize_t) image->columns+2;
1664 for (y=0; y < (ssize_t) image->rows; y++)
1666 register const Quantum
1669 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1670 if (p == (const Quantum *) NULL)
1676 for (x=0; x < (ssize_t) image->columns; x++)
1679 p+=GetPixelChannels(image);
1683 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
1684 for (k=0; k < 4; k++)
1686 Hull(X[k],Y[k],image->columns,image->rows,pixel,buffer,1);
1687 Hull(-X[k],-Y[k],image->columns,image->rows,pixel,buffer,1);
1688 Hull(-X[k],-Y[k],image->columns,image->rows,pixel,buffer,-1);
1689 Hull(X[k],Y[k],image->columns,image->rows,pixel,buffer,-1);
1691 j=(ssize_t) image->columns+2;
1692 for (y=0; y < (ssize_t) image->rows; y++)
1700 q=GetCacheViewAuthenticPixels(despeckle_view,0,y,despeckle_image->columns,
1702 if (q == (Quantum *) NULL)
1708 for (x=0; x < (ssize_t) image->columns; x++)
1710 q[channel]=pixel[j++];
1711 q+=GetPixelChannels(despeckle_image);
1713 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1714 if (sync == MagickFalse)
1718 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1723 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1724 GetPixelChannels(image));
1725 if (proceed == MagickFalse)
1729 despeckle_view=DestroyCacheView(despeckle_view);
1730 image_view=DestroyCacheView(image_view);
1731 buffers=(Quantum *) RelinquishMagickMemory(buffers);
1732 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1733 despeckle_image->type=image->type;
1734 if (status == MagickFalse)
1735 despeckle_image=DestroyImage(despeckle_image);
1736 return(despeckle_image);
1740 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1744 % E d g e I m a g e %
1748 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1750 % EdgeImage() finds edges in an image. Radius defines the radius of the
1751 % convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1754 % The format of the EdgeImage method is:
1756 % Image *EdgeImage(const Image *image,const double radius,
1757 % const double sigma,ExceptionInfo *exception)
1759 % A description of each parameter follows:
1761 % o image: the image.
1763 % o radius: the radius of the pixel neighborhood.
1765 % o sigma: the standard deviation of the Gaussian, in pixels.
1767 % o exception: return any errors or warnings in this structure.
1770 MagickExport Image *EdgeImage(const Image *image,const double radius,
1771 const double sigma,ExceptionInfo *exception)
1790 assert(image != (const Image *) NULL);
1791 assert(image->signature == MagickSignature);
1792 if (image->debug != MagickFalse)
1793 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1794 assert(exception != (ExceptionInfo *) NULL);
1795 assert(exception->signature == MagickSignature);
1796 width=GetOptimalKernelWidth2D(radius,sigma);
1797 kernel_info=AcquireKernelInfo((const char *) NULL);
1798 if (kernel_info == (KernelInfo *) NULL)
1799 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1800 kernel_info->width=width;
1801 kernel_info->height=width;
1802 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
1803 kernel_info->width*sizeof(*kernel_info->values));
1804 if (kernel_info->values == (double *) NULL)
1806 kernel_info=DestroyKernelInfo(kernel_info);
1807 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1809 j=(ssize_t) kernel_info->width/2;
1811 for (v=(-j); v <= j; v++)
1813 for (u=(-j); u <= j; u++)
1815 kernel_info->values[i]=(-1.0);
1819 kernel_info->values[i/2]=(double) (width*width-1.0);
1820 kernel_info->bias=image->bias;
1821 edge_image=ConvolveImage(image,kernel_info,exception);
1822 kernel_info=DestroyKernelInfo(kernel_info);
1827 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1831 % E m b o s s I m a g e %
1835 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1837 % EmbossImage() returns a grayscale image with a three-dimensional effect.
1838 % We convolve the image with a Gaussian operator of the given radius and
1839 % standard deviation (sigma). For reasonable results, radius should be
1840 % larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1843 % The format of the EmbossImage method is:
1845 % Image *EmbossImage(const Image *image,const double radius,
1846 % const double sigma,ExceptionInfo *exception)
1848 % A description of each parameter follows:
1850 % o image: the image.
1852 % o radius: the radius of the pixel neighborhood.
1854 % o sigma: the standard deviation of the Gaussian, in pixels.
1856 % o exception: return any errors or warnings in this structure.
1859 MagickExport Image *EmbossImage(const Image *image,const double radius,
1860 const double sigma,ExceptionInfo *exception)
1880 assert(image != (const Image *) NULL);
1881 assert(image->signature == MagickSignature);
1882 if (image->debug != MagickFalse)
1883 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1884 assert(exception != (ExceptionInfo *) NULL);
1885 assert(exception->signature == MagickSignature);
1886 width=GetOptimalKernelWidth2D(radius,sigma);
1887 kernel_info=AcquireKernelInfo((const char *) NULL);
1888 if (kernel_info == (KernelInfo *) NULL)
1889 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1890 kernel_info->width=width;
1891 kernel_info->height=width;
1892 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
1893 kernel_info->width*sizeof(*kernel_info->values));
1894 if (kernel_info->values == (double *) NULL)
1896 kernel_info=DestroyKernelInfo(kernel_info);
1897 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1899 j=(ssize_t) kernel_info->width/2;
1902 for (v=(-j); v <= j; v++)
1904 for (u=(-j); u <= j; u++)
1906 kernel_info->values[i]=(double) (((u < 0) || (v < 0) ? -8.0 : 8.0)*
1907 exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
1908 (2.0*MagickPI*MagickSigma*MagickSigma));
1910 kernel_info->values[i]=0.0;
1915 kernel_info->bias=image->bias;
1916 emboss_image=ConvolveImage(image,kernel_info,exception);
1917 kernel_info=DestroyKernelInfo(kernel_info);
1918 if (emboss_image != (Image *) NULL)
1919 (void) EqualizeImage(emboss_image,exception);
1920 return(emboss_image);
1924 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1928 % G a u s s i a n B l u r I m a g e %
1932 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1934 % GaussianBlurImage() blurs an image. We convolve the image with a
1935 % Gaussian operator of the given radius and standard deviation (sigma).
1936 % For reasonable results, the radius should be larger than sigma. Use a
1937 % radius of 0 and GaussianBlurImage() selects a suitable radius for you
1939 % The format of the GaussianBlurImage method is:
1941 % Image *GaussianBlurImage(const Image *image,onst double radius,
1942 % const double sigma,const double bias,ExceptionInfo *exception)
1944 % A description of each parameter follows:
1946 % o image: the image.
1948 % o radius: the radius of the Gaussian, in pixels, not counting the center
1951 % o sigma: the standard deviation of the Gaussian, in pixels.
1955 % o exception: return any errors or warnings in this structure.
1958 MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
1959 const double sigma,const double bias,ExceptionInfo *exception)
1978 assert(image != (const Image *) NULL);
1979 assert(image->signature == MagickSignature);
1980 if (image->debug != MagickFalse)
1981 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1982 assert(exception != (ExceptionInfo *) NULL);
1983 assert(exception->signature == MagickSignature);
1984 width=GetOptimalKernelWidth2D(radius,sigma);
1985 kernel_info=AcquireKernelInfo((const char *) NULL);
1986 if (kernel_info == (KernelInfo *) NULL)
1987 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1988 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1989 kernel_info->width=width;
1990 kernel_info->height=width;
1991 kernel_info->bias=bias;
1992 kernel_info->signature=MagickSignature;
1993 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
1994 kernel_info->width*sizeof(*kernel_info->values));
1995 if (kernel_info->values == (double *) NULL)
1997 kernel_info=DestroyKernelInfo(kernel_info);
1998 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2000 j=(ssize_t) kernel_info->width/2;
2002 for (v=(-j); v <= j; v++)
2004 for (u=(-j); u <= j; u++)
2006 kernel_info->values[i]=(double) (exp(-((double) u*u+v*v)/(2.0*
2007 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
2011 blur_image=ConvolveImage(image,kernel_info,exception);
2012 kernel_info=DestroyKernelInfo(kernel_info);
2017 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2021 % M o t i o n B l u r I m a g e %
2025 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2027 % MotionBlurImage() simulates motion blur. We convolve the image with a
2028 % Gaussian operator of the given radius and standard deviation (sigma).
2029 % For reasonable results, radius should be larger than sigma. Use a
2030 % radius of 0 and MotionBlurImage() selects a suitable radius for you.
2031 % Angle gives the angle of the blurring motion.
2033 % Andrew Protano contributed this effect.
2035 % The format of the MotionBlurImage method is:
2037 % Image *MotionBlurImage(const Image *image,const double radius,
2038 % const double sigma,const double angle,const double bias,
2039 % ExceptionInfo *exception)
2041 % A description of each parameter follows:
2043 % o image: the image.
2045 % o radius: the radius of the Gaussian, in pixels, not counting
2048 % o sigma: the standard deviation of the Gaussian, in pixels.
2050 % o angle: Apply the effect along this angle.
2054 % o exception: return any errors or warnings in this structure.
2058 static double *GetMotionBlurKernel(const size_t width,const double sigma)
2068 Generate a 1-D convolution kernel.
2070 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2071 kernel=(double *) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
2072 if (kernel == (double *) NULL)
2075 for (i=0; i < (ssize_t) width; i++)
2077 kernel[i]=(double) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
2078 MagickSigma)))/(MagickSQ2PI*MagickSigma));
2079 normalize+=kernel[i];
2081 for (i=0; i < (ssize_t) width; i++)
2082 kernel[i]/=normalize;
2086 MagickExport Image *MotionBlurImage(const Image *image,const double radius,
2087 const double sigma,const double angle,const double bias,
2088 ExceptionInfo *exception)
2121 assert(image != (Image *) NULL);
2122 assert(image->signature == MagickSignature);
2123 if (image->debug != MagickFalse)
2124 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2125 assert(exception != (ExceptionInfo *) NULL);
2126 width=GetOptimalKernelWidth1D(radius,sigma);
2127 kernel=GetMotionBlurKernel(width,sigma);
2128 if (kernel == (double *) NULL)
2129 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2130 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
2131 if (offset == (OffsetInfo *) NULL)
2133 kernel=(double *) RelinquishMagickMemory(kernel);
2134 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2136 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2137 if (blur_image == (Image *) NULL)
2139 kernel=(double *) RelinquishMagickMemory(kernel);
2140 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2141 return((Image *) NULL);
2143 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2145 kernel=(double *) RelinquishMagickMemory(kernel);
2146 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2147 blur_image=DestroyImage(blur_image);
2148 return((Image *) NULL);
2150 point.x=(double) width*sin(DegreesToRadians(angle));
2151 point.y=(double) width*cos(DegreesToRadians(angle));
2152 for (i=0; i < (ssize_t) width; i++)
2154 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
2155 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
2162 image_view=AcquireCacheView(image);
2163 blur_view=AcquireCacheView(blur_image);
2164 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2165 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
2167 for (y=0; y < (ssize_t) image->rows; y++)
2169 register const Quantum
2178 if (status == MagickFalse)
2180 p=GetCacheViewVirtualPixels(blur_view,0,y,image->columns,1,exception);
2181 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2183 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2188 for (x=0; x < (ssize_t) image->columns; x++)
2193 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2207 register const Quantum
2216 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
2217 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
2218 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
2219 if ((traits == UndefinedPixelTrait) ||
2220 (blur_traits == UndefinedPixelTrait))
2222 if ((blur_traits & CopyPixelTrait) != 0)
2229 if ((blur_traits & BlendPixelTrait) == 0)
2231 for (j=0; j < (ssize_t) width; j++)
2233 r=GetCacheViewVirtualPixels(image_view,x+offset[j].x,y+
2234 offset[j].y,1,1,exception);
2235 if (r == (const Quantum *) NULL)
2243 q[channel]=ClampToQuantum(pixel);
2248 for (j=0; j < (ssize_t) width; j++)
2250 r=GetCacheViewVirtualPixels(image_view,x+offset[j].x,y+offset[j].y,1,
2252 if (r == (const Quantum *) NULL)
2257 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,r));
2258 pixel+=(*k)*alpha*r[i];
2262 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
2263 q[channel]=ClampToQuantum(gamma*pixel);
2265 p+=GetPixelChannels(image);
2266 q+=GetPixelChannels(blur_image);
2268 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2270 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2275 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2276 #pragma omp critical (MagickCore_MotionBlurImage)
2278 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2279 if (proceed == MagickFalse)
2283 blur_view=DestroyCacheView(blur_view);
2284 image_view=DestroyCacheView(image_view);
2285 kernel=(double *) RelinquishMagickMemory(kernel);
2286 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2287 if (status == MagickFalse)
2288 blur_image=DestroyImage(blur_image);
2293 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2297 % P r e v i e w I m a g e %
2301 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2303 % PreviewImage() tiles 9 thumbnails of the specified image with an image
2304 % processing operation applied with varying parameters. This may be helpful
2305 % pin-pointing an appropriate parameter for a particular image processing
2308 % The format of the PreviewImages method is:
2310 % Image *PreviewImages(const Image *image,const PreviewType preview,
2311 % ExceptionInfo *exception)
2313 % A description of each parameter follows:
2315 % o image: the image.
2317 % o preview: the image processing operation.
2319 % o exception: return any errors or warnings in this structure.
2322 MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2323 ExceptionInfo *exception)
2325 #define NumberTiles 9
2326 #define PreviewImageTag "Preview/Image"
2327 #define DefaultPreviewGeometry "204x204+10+10"
2330 factor[MaxTextExtent],
2331 label[MaxTextExtent];
2373 Open output image file.
2375 assert(image != (Image *) NULL);
2376 assert(image->signature == MagickSignature);
2377 if (image->debug != MagickFalse)
2378 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2382 preview_info=AcquireImageInfo();
2383 SetGeometry(image,&geometry);
2384 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2385 &geometry.width,&geometry.height);
2386 images=NewImageList();
2388 GetQuantizeInfo(&quantize_info);
2394 for (i=0; i < NumberTiles; i++)
2396 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2397 if (thumbnail == (Image *) NULL)
2399 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2401 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel);
2402 if (i == (NumberTiles/2))
2404 (void) QueryColorDatabase("#dfdfdf",&thumbnail->matte_color,exception);
2405 AppendImageToList(&images,thumbnail);
2413 preview_image=RotateImage(thumbnail,degrees,exception);
2414 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
2420 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
2421 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",
2422 degrees,2.0*degrees);
2427 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2428 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
2429 preview_image=RollImage(thumbnail,x,y,exception);
2430 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
2431 (double) x,(double) y);
2436 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2437 if (preview_image == (Image *) NULL)
2439 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",
2441 (void) ModulateImage(preview_image,factor,exception);
2442 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2445 case SaturationPreview:
2447 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2448 if (preview_image == (Image *) NULL)
2450 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",
2452 (void) ModulateImage(preview_image,factor,exception);
2453 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2456 case BrightnessPreview:
2458 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2459 if (preview_image == (Image *) NULL)
2461 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
2462 (void) ModulateImage(preview_image,factor,exception);
2463 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2469 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2470 if (preview_image == (Image *) NULL)
2473 (void) GammaImage(preview_image,gamma,exception);
2474 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
2479 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2480 if (preview_image != (Image *) NULL)
2481 for (x=0; x < i; x++)
2482 (void) ContrastImage(preview_image,MagickTrue,exception);
2483 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
2489 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2490 if (preview_image == (Image *) NULL)
2492 for (x=0; x < i; x++)
2493 (void) ContrastImage(preview_image,MagickFalse,exception);
2494 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
2498 case GrayscalePreview:
2500 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2501 if (preview_image == (Image *) NULL)
2504 quantize_info.number_colors=colors;
2505 quantize_info.colorspace=GRAYColorspace;
2506 (void) QuantizeImage(&quantize_info,preview_image,exception);
2507 (void) FormatLocaleString(label,MaxTextExtent,
2508 "-colorspace gray -colors %.20g",(double) colors);
2511 case QuantizePreview:
2513 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2514 if (preview_image == (Image *) NULL)
2517 quantize_info.number_colors=colors;
2518 (void) QuantizeImage(&quantize_info,preview_image,exception);
2519 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
2523 case DespecklePreview:
2525 for (x=0; x < (i-1); x++)
2527 preview_image=DespeckleImage(thumbnail,exception);
2528 if (preview_image == (Image *) NULL)
2530 thumbnail=DestroyImage(thumbnail);
2531 thumbnail=preview_image;
2533 preview_image=DespeckleImage(thumbnail,exception);
2534 if (preview_image == (Image *) NULL)
2536 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
2540 case ReduceNoisePreview:
2542 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2543 (size_t) radius,exception);
2544 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
2547 case AddNoisePreview:
2553 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2558 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2563 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2568 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2573 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2578 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2583 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2587 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2588 (size_t) i,exception);
2589 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
2592 case SharpenPreview:
2594 preview_image=SharpenImage(thumbnail,radius,sigma,image->bias,
2596 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",
2602 preview_image=BlurImage(thumbnail,radius,sigma,image->bias,exception);
2603 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
2607 case ThresholdPreview:
2609 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2610 if (preview_image == (Image *) NULL)
2612 (void) BilevelImage(thumbnail,
2613 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
2614 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",
2615 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
2618 case EdgeDetectPreview:
2620 preview_image=EdgeImage(thumbnail,radius,sigma,exception);
2621 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
2626 preview_image=SpreadImage(thumbnail,radius,exception);
2627 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",
2631 case SolarizePreview:
2633 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2634 if (preview_image == (Image *) NULL)
2636 (void) SolarizeImage(preview_image,(double) QuantumRange*
2637 percentage/100.0,exception);
2638 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
2639 (QuantumRange*percentage)/100.0);
2645 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2647 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",
2653 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2654 if (preview_image == (Image *) NULL)
2656 geometry.width=(size_t) (2*i+2);
2657 geometry.height=(size_t) (2*i+2);
2660 (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
2661 (void) FormatLocaleString(label,MaxTextExtent,
2662 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
2663 geometry.height,(double) geometry.x,(double) geometry.y);
2666 case SegmentPreview:
2668 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2669 if (preview_image == (Image *) NULL)
2672 (void) SegmentImage(preview_image,RGBColorspace,MagickFalse,threshold,
2673 threshold,exception);
2674 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
2675 threshold,threshold);
2680 preview_image=SwirlImage(thumbnail,degrees,exception);
2681 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
2685 case ImplodePreview:
2688 preview_image=ImplodeImage(thumbnail,degrees,exception);
2689 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
2695 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,exception);
2696 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",
2697 0.5*degrees,2.0*degrees);
2700 case OilPaintPreview:
2702 preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2704 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2708 case CharcoalDrawingPreview:
2710 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
2711 image->bias,exception);
2712 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2719 filename[MaxTextExtent];
2727 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2728 if (preview_image == (Image *) NULL)
2730 preview_info->quality=(size_t) percentage;
2731 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
2732 preview_info->quality);
2733 file=AcquireUniqueFileResource(filename);
2736 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
2737 "jpeg:%s",filename);
2738 status=WriteImage(preview_info,preview_image,exception);
2739 if (status != MagickFalse)
2744 (void) CopyMagickString(preview_info->filename,
2745 preview_image->filename,MaxTextExtent);
2746 quality_image=ReadImage(preview_info,exception);
2747 if (quality_image != (Image *) NULL)
2749 preview_image=DestroyImage(preview_image);
2750 preview_image=quality_image;
2753 (void) RelinquishUniqueFileResource(preview_image->filename);
2754 if ((GetBlobSize(preview_image)/1024) >= 1024)
2755 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
2756 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2759 if (GetBlobSize(preview_image) >= 1024)
2760 (void) FormatLocaleString(label,MaxTextExtent,
2761 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
2762 GetBlobSize(preview_image))/1024.0);
2764 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
2765 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
2769 thumbnail=DestroyImage(thumbnail);
2773 if (preview_image == (Image *) NULL)
2775 (void) DeleteImageProperty(preview_image,"label");
2776 (void) SetImageProperty(preview_image,"label",label);
2777 AppendImageToList(&images,preview_image);
2778 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2780 if (proceed == MagickFalse)
2783 if (images == (Image *) NULL)
2785 preview_info=DestroyImageInfo(preview_info);
2786 return((Image *) NULL);
2791 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2792 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2793 montage_info->shadow=MagickTrue;
2794 (void) CloneString(&montage_info->tile,"3x3");
2795 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2796 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2797 montage_image=MontageImages(images,montage_info,exception);
2798 montage_info=DestroyMontageInfo(montage_info);
2799 images=DestroyImageList(images);
2800 if (montage_image == (Image *) NULL)
2801 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2802 if (montage_image->montage != (char *) NULL)
2805 Free image directory.
2807 montage_image->montage=(char *) RelinquishMagickMemory(
2808 montage_image->montage);
2809 if (image->directory != (char *) NULL)
2810 montage_image->directory=(char *) RelinquishMagickMemory(
2811 montage_image->directory);
2813 preview_info=DestroyImageInfo(preview_info);
2814 return(montage_image);
2818 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2822 % R a d i a l B l u r I m a g e %
2826 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2828 % RadialBlurImage() applies a radial blur to the image.
2830 % Andrew Protano contributed this effect.
2832 % The format of the RadialBlurImage method is:
2834 % Image *RadialBlurImage(const Image *image,const double angle,
2835 % const double blur,ExceptionInfo *exception)
2837 % A description of each parameter follows:
2839 % o image: the image.
2841 % o angle: the angle of the radial blur.
2845 % o exception: return any errors or warnings in this structure.
2848 MagickExport Image *RadialBlurImage(const Image *image,
2849 const double angle,const double bias,ExceptionInfo *exception)
2884 Allocate blur image.
2886 assert(image != (Image *) NULL);
2887 assert(image->signature == MagickSignature);
2888 if (image->debug != MagickFalse)
2889 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2890 assert(exception != (ExceptionInfo *) NULL);
2891 assert(exception->signature == MagickSignature);
2892 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2893 if (blur_image == (Image *) NULL)
2894 return((Image *) NULL);
2895 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2897 blur_image=DestroyImage(blur_image);
2898 return((Image *) NULL);
2900 blur_center.x=(double) image->columns/2.0;
2901 blur_center.y=(double) image->rows/2.0;
2902 blur_radius=hypot(blur_center.x,blur_center.y);
2903 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
2904 theta=DegreesToRadians(angle)/(MagickRealType) (n-1);
2905 cos_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2906 sizeof(*cos_theta));
2907 sin_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2908 sizeof(*sin_theta));
2909 if ((cos_theta == (MagickRealType *) NULL) ||
2910 (sin_theta == (MagickRealType *) NULL))
2912 blur_image=DestroyImage(blur_image);
2913 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2915 offset=theta*(MagickRealType) (n-1)/2.0;
2916 for (i=0; i < (ssize_t) n; i++)
2918 cos_theta[i]=cos((double) (theta*i-offset));
2919 sin_theta[i]=sin((double) (theta*i-offset));
2926 image_view=AcquireCacheView(image);
2927 blur_view=AcquireCacheView(blur_image);
2928 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2929 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2931 for (y=0; y < (ssize_t) image->rows; y++)
2933 register const Quantum
2942 if (status == MagickFalse)
2944 p=GetCacheViewVirtualPixels(blur_view,0,y,image->columns,1,exception);
2945 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2947 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2952 for (x=0; x < (ssize_t) image->columns; x++)
2966 center.x=(double) x-blur_center.x;
2967 center.y=(double) y-blur_center.y;
2968 radius=hypot((double) center.x,center.y);
2973 step=(size_t) (blur_radius/radius);
2980 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2993 register const Quantum
2999 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3000 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3001 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
3002 if ((traits == UndefinedPixelTrait) ||
3003 (blur_traits == UndefinedPixelTrait))
3005 if ((blur_traits & CopyPixelTrait) != 0)
3012 if ((blur_traits & BlendPixelTrait) == 0)
3014 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
3016 r=GetCacheViewVirtualPixels(image_view, (ssize_t) (blur_center.x+
3017 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
3018 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
3020 if (r == (const Quantum *) NULL)
3028 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3029 q[channel]=ClampToQuantum(gamma*pixel);
3032 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
3034 r=GetCacheViewVirtualPixels(image_view, (ssize_t) (blur_center.x+
3035 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
3036 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
3038 if (r == (const Quantum *) NULL)
3043 pixel+=GetPixelAlpha(image,r)*r[i];
3044 gamma+=GetPixelAlpha(image,r);
3046 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3047 q[channel]=ClampToQuantum(gamma*pixel);
3049 p+=GetPixelChannels(image);
3050 q+=GetPixelChannels(blur_image);
3052 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
3054 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3059 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3060 #pragma omp critical (MagickCore_RadialBlurImage)
3062 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
3063 if (proceed == MagickFalse)
3067 blur_view=DestroyCacheView(blur_view);
3068 image_view=DestroyCacheView(image_view);
3069 cos_theta=(MagickRealType *) RelinquishMagickMemory(cos_theta);
3070 sin_theta=(MagickRealType *) RelinquishMagickMemory(sin_theta);
3071 if (status == MagickFalse)
3072 blur_image=DestroyImage(blur_image);
3077 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3081 % S e l e c t i v e B l u r I m a g e %
3085 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3087 % SelectiveBlurImage() selectively blur pixels within a contrast threshold.
3088 % It is similar to the unsharpen mask that sharpens everything with contrast
3089 % above a certain threshold.
3091 % The format of the SelectiveBlurImage method is:
3093 % Image *SelectiveBlurImage(const Image *image,const double radius,
3094 % const double sigma,const double threshold,const double bias,
3095 % ExceptionInfo *exception)
3097 % A description of each parameter follows:
3099 % o image: the image.
3101 % o radius: the radius of the Gaussian, in pixels, not counting the center
3104 % o sigma: the standard deviation of the Gaussian, in pixels.
3106 % o threshold: only pixels within this contrast threshold are included
3107 % in the blur operation.
3111 % o exception: return any errors or warnings in this structure.
3114 MagickExport Image *SelectiveBlurImage(const Image *image,
3115 const double radius,const double sigma,const double threshold,
3116 const double bias,ExceptionInfo *exception)
3118 #define SelectiveBlurImageTag "SelectiveBlur/Image"
3150 Initialize blur image attributes.
3152 assert(image != (Image *) NULL);
3153 assert(image->signature == MagickSignature);
3154 if (image->debug != MagickFalse)
3155 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3156 assert(exception != (ExceptionInfo *) NULL);
3157 assert(exception->signature == MagickSignature);
3158 width=GetOptimalKernelWidth1D(radius,sigma);
3159 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
3160 if (kernel == (double *) NULL)
3161 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3162 j=(ssize_t) width/2;
3164 for (v=(-j); v <= j; v++)
3166 for (u=(-j); u <= j; u++)
3167 kernel[i++]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
3168 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3170 if (image->debug != MagickFalse)
3173 format[MaxTextExtent],
3176 register const double
3183 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
3184 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
3186 message=AcquireString("");
3188 for (v=0; v < (ssize_t) width; v++)
3191 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
3192 (void) ConcatenateString(&message,format);
3193 for (u=0; u < (ssize_t) width; u++)
3195 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",*k++);
3196 (void) ConcatenateString(&message,format);
3198 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
3200 message=DestroyString(message);
3202 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3203 if (blur_image == (Image *) NULL)
3204 return((Image *) NULL);
3205 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
3207 blur_image=DestroyImage(blur_image);
3208 return((Image *) NULL);
3211 Threshold blur image.
3215 center=(ssize_t) GetPixelChannels(image)*(image->columns+width)*(width/2L)+
3216 GetPixelChannels(image)*(width/2L);
3217 image_view=AcquireCacheView(image);
3218 blur_view=AcquireCacheView(blur_image);
3219 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3220 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3222 for (y=0; y < (ssize_t) image->rows; y++)
3230 register const Quantum
3239 if (status == MagickFalse)
3241 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
3242 (width/2L),image->columns+width,width,exception);
3243 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3245 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3250 for (x=0; x < (ssize_t) image->columns; x++)
3255 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3270 register const double
3273 register const Quantum
3282 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3283 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3284 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
3285 if ((traits == UndefinedPixelTrait) ||
3286 (blur_traits == UndefinedPixelTrait))
3288 if ((blur_traits & CopyPixelTrait) != 0)
3290 q[channel]=p[center+i];
3296 intensity=GetPixelIntensity(image,p+center);
3298 if ((blur_traits & BlendPixelTrait) == 0)
3300 for (v=0; v < (ssize_t) width; v++)
3302 for (u=0; u < (ssize_t) width; u++)
3304 contrast=GetPixelIntensity(image,pixels)-intensity;
3305 if (fabs(contrast) < threshold)
3307 pixel+=(*k)*pixels[i];
3311 pixels+=GetPixelChannels(image);
3313 pixels+=image->columns*GetPixelChannels(image);
3315 if (fabs((double) gamma) < MagickEpsilon)
3317 q[channel]=p[center+i];
3320 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3321 q[channel]=ClampToQuantum(gamma*pixel);
3324 for (v=0; v < (ssize_t) width; v++)
3326 for (u=0; u < (ssize_t) width; u++)
3328 contrast=GetPixelIntensity(image,pixels)-intensity;
3329 if (fabs(contrast) < threshold)
3331 alpha=(MagickRealType) (QuantumScale*
3332 GetPixelAlpha(image,pixels));
3333 pixel+=(*k)*alpha*pixels[i];
3337 pixels+=GetPixelChannels(image);
3339 pixels+=image->columns*GetPixelChannels(image);
3341 if (fabs((double) gamma) < MagickEpsilon)
3343 q[channel]=p[center+i];
3346 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3347 q[channel]=ClampToQuantum(gamma*pixel);
3349 p+=GetPixelChannels(image);
3350 q+=GetPixelChannels(blur_image);
3352 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3353 if (sync == MagickFalse)
3355 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3360 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3361 #pragma omp critical (MagickCore_SelectiveBlurImage)
3363 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3365 if (proceed == MagickFalse)
3369 blur_image->type=image->type;
3370 blur_view=DestroyCacheView(blur_view);
3371 image_view=DestroyCacheView(image_view);
3372 kernel=(double *) RelinquishMagickMemory(kernel);
3373 if (status == MagickFalse)
3374 blur_image=DestroyImage(blur_image);
3379 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3383 % S h a d e I m a g e %
3387 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3389 % ShadeImage() shines a distant light on an image to create a
3390 % three-dimensional effect. You control the positioning of the light with
3391 % azimuth and elevation; azimuth is measured in degrees off the x axis
3392 % and elevation is measured in pixels above the Z axis.
3394 % The format of the ShadeImage method is:
3396 % Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3397 % const double azimuth,const double elevation,ExceptionInfo *exception)
3399 % A description of each parameter follows:
3401 % o image: the image.
3403 % o gray: A value other than zero shades the intensity of each pixel.
3405 % o azimuth, elevation: Define the light source direction.
3407 % o exception: return any errors or warnings in this structure.
3410 MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3411 const double azimuth,const double elevation,ExceptionInfo *exception)
3413 #define ShadeImageTag "Shade/Image"
3435 Initialize shaded image attributes.
3437 assert(image != (const Image *) NULL);
3438 assert(image->signature == MagickSignature);
3439 if (image->debug != MagickFalse)
3440 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3441 assert(exception != (ExceptionInfo *) NULL);
3442 assert(exception->signature == MagickSignature);
3443 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3444 if (shade_image == (Image *) NULL)
3445 return((Image *) NULL);
3446 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
3448 shade_image=DestroyImage(shade_image);
3449 return((Image *) NULL);
3452 Compute the light vector.
3454 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3455 cos(DegreesToRadians(elevation));
3456 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3457 cos(DegreesToRadians(elevation));
3458 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3464 image_view=AcquireCacheView(image);
3465 shade_view=AcquireCacheView(shade_image);
3466 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3467 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3469 for (y=0; y < (ssize_t) image->rows; y++)
3479 register const Quantum
3491 if (status == MagickFalse)
3493 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
3494 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3496 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3502 Shade this row of pixels.
3504 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
3505 pre=p+GetPixelChannels(image);
3506 center=pre+(image->columns+2)*GetPixelChannels(image);
3507 post=center+(image->columns+2)*GetPixelChannels(image);
3508 for (x=0; x < (ssize_t) image->columns; x++)
3514 Determine the surface normal and compute shading.
3516 normal.x=(double) (GetPixelIntensity(image,pre-GetPixelChannels(image))+
3517 GetPixelIntensity(image,center-GetPixelChannels(image))+
3518 GetPixelIntensity(image,post-GetPixelChannels(image))-
3519 GetPixelIntensity(image,pre+GetPixelChannels(image))-
3520 GetPixelIntensity(image,center+GetPixelChannels(image))-
3521 GetPixelIntensity(image,post+GetPixelChannels(image)));
3522 normal.y=(double) (GetPixelIntensity(image,post-GetPixelChannels(image))+
3523 GetPixelIntensity(image,post)+GetPixelIntensity(image,post+
3524 GetPixelChannels(image))-GetPixelIntensity(image,pre-
3525 GetPixelChannels(image))-GetPixelIntensity(image,pre)-
3526 GetPixelIntensity(image,pre+GetPixelChannels(image)));
3527 if ((normal.x == 0.0) && (normal.y == 0.0))
3532 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3533 if (distance > MagickEpsilon)
3536 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
3537 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3538 shade=distance/sqrt((double) normal_distance);
3541 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3550 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3551 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3552 shade_traits=GetPixelChannelMapTraits(shade_image,channel);
3553 if ((traits == UndefinedPixelTrait) ||
3554 (shade_traits == UndefinedPixelTrait))
3556 if ((shade_traits & CopyPixelTrait) != 0)
3558 q[channel]=center[i];
3561 if (gray != MagickFalse)
3563 q[channel]=ClampToQuantum(shade);
3566 q[channel]=ClampToQuantum(QuantumScale*shade*center[i]);
3568 pre+=GetPixelChannels(image);
3569 center+=GetPixelChannels(image);
3570 post+=GetPixelChannels(image);
3571 q+=GetPixelChannels(shade_image);
3573 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3575 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3580 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3581 #pragma omp critical (MagickCore_ShadeImage)
3583 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3584 if (proceed == MagickFalse)
3588 shade_view=DestroyCacheView(shade_view);
3589 image_view=DestroyCacheView(image_view);
3590 if (status == MagickFalse)
3591 shade_image=DestroyImage(shade_image);
3592 return(shade_image);
3596 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3600 % S h a r p e n I m a g e %
3604 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3606 % SharpenImage() sharpens the image. We convolve the image with a Gaussian
3607 % operator of the given radius and standard deviation (sigma). For
3608 % reasonable results, radius should be larger than sigma. Use a radius of 0
3609 % and SharpenImage() selects a suitable radius for you.
3611 % Using a separable kernel would be faster, but the negative weights cancel
3612 % out on the corners of the kernel producing often undesirable ringing in the
3613 % filtered result; this can be avoided by using a 2D gaussian shaped image
3614 % sharpening kernel instead.
3616 % The format of the SharpenImage method is:
3618 % Image *SharpenImage(const Image *image,const double radius,
3619 % const double sigma,const double bias,ExceptionInfo *exception)
3621 % A description of each parameter follows:
3623 % o image: the image.
3625 % o radius: the radius of the Gaussian, in pixels, not counting the center
3628 % o sigma: the standard deviation of the Laplacian, in pixels.
3632 % o exception: return any errors or warnings in this structure.
3635 MagickExport Image *SharpenImage(const Image *image,const double radius,
3636 const double sigma,const double bias,ExceptionInfo *exception)
3658 assert(image != (const Image *) NULL);
3659 assert(image->signature == MagickSignature);
3660 if (image->debug != MagickFalse)
3661 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3662 assert(exception != (ExceptionInfo *) NULL);
3663 assert(exception->signature == MagickSignature);
3664 width=GetOptimalKernelWidth2D(radius,sigma);
3665 kernel_info=AcquireKernelInfo((const char *) NULL);
3666 if (kernel_info == (KernelInfo *) NULL)
3667 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3668 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3669 kernel_info->width=width;
3670 kernel_info->height=width;
3671 kernel_info->bias=bias;
3672 kernel_info->signature=MagickSignature;
3673 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
3674 kernel_info->width*sizeof(*kernel_info->values));
3675 if (kernel_info->values == (double *) NULL)
3677 kernel_info=DestroyKernelInfo(kernel_info);
3678 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3681 j=(ssize_t) kernel_info->width/2;
3683 for (v=(-j); v <= j; v++)
3685 for (u=(-j); u <= j; u++)
3687 kernel_info->values[i]=(double) (-exp(-((double) u*u+v*v)/(2.0*
3688 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3689 normalize+=kernel_info->values[i];
3693 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
3694 sharp_image=ConvolveImage(image,kernel_info,exception);
3695 kernel_info=DestroyKernelInfo(kernel_info);
3696 return(sharp_image);
3700 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3704 % S p r e a d I m a g e %
3708 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3710 % SpreadImage() is a special effects method that randomly displaces each
3711 % pixel in a block defined by the radius parameter.
3713 % The format of the SpreadImage method is:
3715 % Image *SpreadImage(const Image *image,const double radius,
3716 % ExceptionInfo *exception)
3718 % A description of each parameter follows:
3720 % o image: the image.
3722 % o radius: Choose a random pixel in a neighborhood of this extent.
3724 % o exception: return any errors or warnings in this structure.
3727 MagickExport Image *SpreadImage(const Image *image,const double radius,
3728 ExceptionInfo *exception)
3730 #define SpreadImageTag "Spread/Image"
3746 **restrict random_info;
3755 Initialize spread image attributes.
3757 assert(image != (Image *) NULL);
3758 assert(image->signature == MagickSignature);
3759 if (image->debug != MagickFalse)
3760 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3761 assert(exception != (ExceptionInfo *) NULL);
3762 assert(exception->signature == MagickSignature);
3763 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3765 if (spread_image == (Image *) NULL)
3766 return((Image *) NULL);
3767 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
3769 spread_image=DestroyImage(spread_image);
3770 return((Image *) NULL);
3777 width=GetOptimalKernelWidth1D(radius,0.5);
3778 random_info=AcquireRandomInfoThreadSet();
3779 image_view=AcquireCacheView(image);
3780 spread_view=AcquireCacheView(spread_image);
3781 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3782 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
3784 for (y=0; y < (ssize_t) image->rows; y++)
3787 id = GetOpenMPThreadId();
3789 register const Quantum
3798 if (status == MagickFalse)
3800 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3801 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
3803 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3808 for (x=0; x < (ssize_t) image->columns; x++)
3816 point.x=GetPseudoRandomValue(random_info[id]);
3817 point.y=GetPseudoRandomValue(random_info[id]);
3818 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3830 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3831 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3832 spread_traits=GetPixelChannelMapTraits(spread_image,channel);
3833 if ((traits == UndefinedPixelTrait) ||
3834 (spread_traits == UndefinedPixelTrait))
3836 if ((spread_traits & CopyPixelTrait) != 0)
3841 status=InterpolatePixelChannel(image,image_view,(PixelChannel) i,
3842 MeshInterpolatePixel,(double) x+width*point.x-0.5,(double) y+width*
3843 point.y-0.5,&pixel,exception);
3844 q[channel]=ClampToQuantum(pixel);
3846 q+=GetPixelChannels(spread_image);
3848 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
3850 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3855 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3856 #pragma omp critical (MagickCore_SpreadImage)
3858 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3859 if (proceed == MagickFalse)
3863 spread_view=DestroyCacheView(spread_view);
3864 image_view=DestroyCacheView(image_view);
3865 random_info=DestroyRandomInfoThreadSet(random_info);
3866 return(spread_image);
3870 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3874 % S t a t i s t i c I m a g e %
3878 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3880 % StatisticImage() makes each pixel the min / max / median / mode / etc. of
3881 % the neighborhood of the specified width and height.
3883 % The format of the StatisticImage method is:
3885 % Image *StatisticImage(const Image *image,const StatisticType type,
3886 % const size_t width,const size_t height,ExceptionInfo *exception)
3888 % A description of each parameter follows:
3890 % o image: the image.
3892 % o type: the statistic type (median, mode, etc.).
3894 % o width: the width of the pixel neighborhood.
3896 % o height: the height of the pixel neighborhood.
3898 % o exception: return any errors or warnings in this structure.
3902 typedef struct _SkipNode
3910 typedef struct _SkipList
3919 typedef struct _PixelList
3932 static PixelList *DestroyPixelList(PixelList *pixel_list)
3934 if (pixel_list == (PixelList *) NULL)
3935 return((PixelList *) NULL);
3936 if (pixel_list->skip_list.nodes != (SkipNode *) NULL)
3937 pixel_list->skip_list.nodes=(SkipNode *) RelinquishMagickMemory(
3938 pixel_list->skip_list.nodes);
3939 pixel_list=(PixelList *) RelinquishMagickMemory(pixel_list);
3943 static PixelList **DestroyPixelListThreadSet(PixelList **pixel_list)
3948 assert(pixel_list != (PixelList **) NULL);
3949 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
3950 if (pixel_list[i] != (PixelList *) NULL)
3951 pixel_list[i]=DestroyPixelList(pixel_list[i]);
3952 pixel_list=(PixelList **) RelinquishMagickMemory(pixel_list);
3956 static PixelList *AcquirePixelList(const size_t width,const size_t height)
3961 pixel_list=(PixelList *) AcquireMagickMemory(sizeof(*pixel_list));
3962 if (pixel_list == (PixelList *) NULL)
3964 (void) ResetMagickMemory((void *) pixel_list,0,sizeof(*pixel_list));
3965 pixel_list->length=width*height;
3966 pixel_list->skip_list.nodes=(SkipNode *) AcquireQuantumMemory(65537UL,
3967 sizeof(*pixel_list->skip_list.nodes));
3968 if (pixel_list->skip_list.nodes == (SkipNode *) NULL)
3969 return(DestroyPixelList(pixel_list));
3970 (void) ResetMagickMemory(pixel_list->skip_list.nodes,0,65537UL*
3971 sizeof(*pixel_list->skip_list.nodes));
3972 pixel_list->signature=MagickSignature;
3976 static PixelList **AcquirePixelListThreadSet(const size_t width,
3977 const size_t height)
3988 number_threads=GetOpenMPMaximumThreads();
3989 pixel_list=(PixelList **) AcquireQuantumMemory(number_threads,
3990 sizeof(*pixel_list));
3991 if (pixel_list == (PixelList **) NULL)
3992 return((PixelList **) NULL);
3993 (void) ResetMagickMemory(pixel_list,0,number_threads*sizeof(*pixel_list));
3994 for (i=0; i < (ssize_t) number_threads; i++)
3996 pixel_list[i]=AcquirePixelList(width,height);
3997 if (pixel_list[i] == (PixelList *) NULL)
3998 return(DestroyPixelListThreadSet(pixel_list));
4003 static void AddNodePixelList(PixelList *pixel_list,const size_t color)
4016 Initialize the node.
4018 p=(&pixel_list->skip_list);
4019 p->nodes[color].signature=pixel_list->signature;
4020 p->nodes[color].count=1;
4022 Determine where it belongs in the list.
4025 for (level=p->level; level >= 0; level--)
4027 while (p->nodes[search].next[level] < color)
4028 search=p->nodes[search].next[level];
4029 update[level]=search;
4032 Generate a pseudo-random level for this node.
4034 for (level=0; ; level++)
4036 pixel_list->seed=(pixel_list->seed*42893621L)+1L;
4037 if ((pixel_list->seed & 0x300) != 0x300)
4042 if (level > (p->level+2))
4045 If we're raising the list's level, link back to the root node.
4047 while (level > p->level)
4050 update[p->level]=65536UL;
4053 Link the node into the skip-list.
4057 p->nodes[color].next[level]=p->nodes[update[level]].next[level];
4058 p->nodes[update[level]].next[level]=color;
4059 } while (level-- > 0);
4062 static Quantum GetMaximumPixelList(PixelList *pixel_list)
4075 Find the maximum value for each of the color.
4077 p=(&pixel_list->skip_list);
4080 maximum=p->nodes[color].next[0];
4083 color=p->nodes[color].next[0];
4084 if (color > maximum)
4086 count+=p->nodes[color].count;
4087 } while (count < (ssize_t) pixel_list->length);
4088 return(ScaleShortToQuantum((unsigned short) maximum));
4091 static Quantum GetMeanPixelList(PixelList *pixel_list)
4106 Find the mean value for each of the color.
4108 p=(&pixel_list->skip_list);
4114 color=p->nodes[color].next[0];
4115 sum+=(MagickRealType) p->nodes[color].count*color;
4116 count+=p->nodes[color].count;
4117 } while (count < (ssize_t) pixel_list->length);
4118 sum/=pixel_list->length;
4119 return(ScaleShortToQuantum((unsigned short) sum));
4122 static Quantum GetMedianPixelList(PixelList *pixel_list)
4134 Find the median value for each of the color.
4136 p=(&pixel_list->skip_list);
4141 color=p->nodes[color].next[0];
4142 count+=p->nodes[color].count;
4143 } while (count <= (ssize_t) (pixel_list->length >> 1));
4144 return(ScaleShortToQuantum((unsigned short) color));
4147 static Quantum GetMinimumPixelList(PixelList *pixel_list)
4160 Find the minimum value for each of the color.
4162 p=(&pixel_list->skip_list);
4165 minimum=p->nodes[color].next[0];
4168 color=p->nodes[color].next[0];
4169 if (color < minimum)
4171 count+=p->nodes[color].count;
4172 } while (count < (ssize_t) pixel_list->length);
4173 return(ScaleShortToQuantum((unsigned short) minimum));
4176 static Quantum GetModePixelList(PixelList *pixel_list)
4190 Make each pixel the 'predominant color' of the specified neighborhood.
4192 p=(&pixel_list->skip_list);
4195 max_count=p->nodes[mode].count;
4199 color=p->nodes[color].next[0];
4200 if (p->nodes[color].count > max_count)
4203 max_count=p->nodes[mode].count;
4205 count+=p->nodes[color].count;
4206 } while (count < (ssize_t) pixel_list->length);
4207 return(ScaleShortToQuantum((unsigned short) mode));
4210 static Quantum GetNonpeakPixelList(PixelList *pixel_list)
4224 Finds the non peak value for each of the colors.
4226 p=(&pixel_list->skip_list);
4228 next=p->nodes[color].next[0];
4234 next=p->nodes[color].next[0];
4235 count+=p->nodes[color].count;
4236 } while (count <= (ssize_t) (pixel_list->length >> 1));
4237 if ((previous == 65536UL) && (next != 65536UL))
4240 if ((previous != 65536UL) && (next == 65536UL))
4242 return(ScaleShortToQuantum((unsigned short) color));
4245 static Quantum GetStandardDeviationPixelList(PixelList *pixel_list)
4261 Find the standard-deviation value for each of the color.
4263 p=(&pixel_list->skip_list);
4273 color=p->nodes[color].next[0];
4274 sum+=(MagickRealType) p->nodes[color].count*color;
4275 for (i=0; i < (ssize_t) p->nodes[color].count; i++)
4276 sum_squared+=((MagickRealType) color)*((MagickRealType) color);
4277 count+=p->nodes[color].count;
4278 } while (count < (ssize_t) pixel_list->length);
4279 sum/=pixel_list->length;
4280 sum_squared/=pixel_list->length;
4281 return(ScaleShortToQuantum((unsigned short) sqrt(sum_squared-(sum*sum))));
4284 static inline void InsertPixelList(const Image *image,const Quantum pixel,
4285 PixelList *pixel_list)
4293 index=ScaleQuantumToShort(pixel);
4294 signature=pixel_list->skip_list.nodes[index].signature;
4295 if (signature == pixel_list->signature)
4297 pixel_list->skip_list.nodes[index].count++;
4300 AddNodePixelList(pixel_list,index);
4303 static inline MagickRealType MagickAbsoluteValue(const MagickRealType x)
4310 static inline size_t MagickMax(const size_t x,const size_t y)
4317 static void ResetPixelList(PixelList *pixel_list)
4329 Reset the skip-list.
4331 p=(&pixel_list->skip_list);
4332 root=p->nodes+65536UL;
4334 for (level=0; level < 9; level++)
4335 root->next[level]=65536UL;
4336 pixel_list->seed=pixel_list->signature++;
4339 MagickExport Image *StatisticImage(const Image *image,const StatisticType type,
4340 const size_t width,const size_t height,ExceptionInfo *exception)
4342 #define StatisticImageTag "Statistic/Image"
4358 **restrict pixel_list;
4365 Initialize statistics image attributes.
4367 assert(image != (Image *) NULL);
4368 assert(image->signature == MagickSignature);
4369 if (image->debug != MagickFalse)
4370 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4371 assert(exception != (ExceptionInfo *) NULL);
4372 assert(exception->signature == MagickSignature);
4373 statistic_image=CloneImage(image,image->columns,image->rows,MagickTrue,
4375 if (statistic_image == (Image *) NULL)
4376 return((Image *) NULL);
4377 status=SetImageStorageClass(statistic_image,DirectClass,exception);
4378 if (status == MagickFalse)
4380 statistic_image=DestroyImage(statistic_image);
4381 return((Image *) NULL);
4383 pixel_list=AcquirePixelListThreadSet(MagickMax(width,1),MagickMax(height,1));
4384 if (pixel_list == (PixelList **) NULL)
4386 statistic_image=DestroyImage(statistic_image);
4387 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
4390 Make each pixel the min / max / median / mode / etc. of the neighborhood.
4392 center=(ssize_t) GetPixelChannels(image)*(image->columns+MagickMax(width,1))*
4393 (MagickMax(height,1)/2L)+GetPixelChannels(image)*(MagickMax(width,1)/2L);
4396 image_view=AcquireCacheView(image);
4397 statistic_view=AcquireCacheView(statistic_image);
4398 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4399 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
4401 for (y=0; y < (ssize_t) statistic_image->rows; y++)
4404 id = GetOpenMPThreadId();
4406 register const Quantum
4415 if (status == MagickFalse)
4417 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) MagickMax(width,1)/2L),y-
4418 (ssize_t) (MagickMax(height,1)/2L),image->columns+MagickMax(width,1),
4419 MagickMax(height,1),exception);
4420 q=QueueCacheViewAuthenticPixels(statistic_view,0,y,statistic_image->columns, 1,exception);
4421 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
4426 for (x=0; x < (ssize_t) statistic_image->columns; x++)
4431 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4443 register const Quantum
4452 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
4453 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
4454 statistic_traits=GetPixelChannelMapTraits(statistic_image,channel);
4455 if ((traits == UndefinedPixelTrait) ||
4456 (statistic_traits == UndefinedPixelTrait))
4458 if ((statistic_traits & CopyPixelTrait) != 0)
4460 q[channel]=p[center+i];
4464 ResetPixelList(pixel_list[id]);
4465 for (v=0; v < (ssize_t) MagickMax(height,1); v++)
4467 for (u=0; u < (ssize_t) MagickMax(width,1); u++)
4469 InsertPixelList(image,pixels[i],pixel_list[id]);
4470 pixels+=GetPixelChannels(image);
4472 pixels+=image->columns*GetPixelChannels(image);
4476 case GradientStatistic:
4482 minimum=(MagickRealType) GetMinimumPixelList(pixel_list[id]);
4483 maximum=(MagickRealType) GetMaximumPixelList(pixel_list[id]);
4484 pixel=ClampToQuantum(MagickAbsoluteValue(maximum-minimum));
4487 case MaximumStatistic:
4489 pixel=GetMaximumPixelList(pixel_list[id]);
4494 pixel=GetMeanPixelList(pixel_list[id]);
4497 case MedianStatistic:
4500 pixel=GetMedianPixelList(pixel_list[id]);
4503 case MinimumStatistic:
4505 pixel=GetMinimumPixelList(pixel_list[id]);
4510 pixel=GetModePixelList(pixel_list[id]);
4513 case NonpeakStatistic:
4515 pixel=GetNonpeakPixelList(pixel_list[id]);
4518 case StandardDeviationStatistic:
4520 pixel=GetStandardDeviationPixelList(pixel_list[id]);
4526 p+=GetPixelChannels(image);
4527 q+=GetPixelChannels(statistic_image);
4529 if (SyncCacheViewAuthenticPixels(statistic_view,exception) == MagickFalse)
4531 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4536 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4537 #pragma omp critical (MagickCore_StatisticImage)
4539 proceed=SetImageProgress(image,StatisticImageTag,progress++,
4541 if (proceed == MagickFalse)
4545 statistic_view=DestroyCacheView(statistic_view);
4546 image_view=DestroyCacheView(image_view);
4547 pixel_list=DestroyPixelListThreadSet(pixel_list);
4548 return(statistic_image);
4552 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4556 % U n s h a r p M a s k I m a g e %
4560 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4562 % UnsharpMaskImage() sharpens one or more image channels. We convolve the
4563 % image with a Gaussian operator of the given radius and standard deviation
4564 % (sigma). For reasonable results, radius should be larger than sigma. Use a
4565 % radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
4567 % The format of the UnsharpMaskImage method is:
4569 % Image *UnsharpMaskImage(const Image *image,const double radius,
4570 % const double sigma,const double amount,const double threshold,
4571 % ExceptionInfo *exception)
4573 % A description of each parameter follows:
4575 % o image: the image.
4577 % o radius: the radius of the Gaussian, in pixels, not counting the center
4580 % o sigma: the standard deviation of the Gaussian, in pixels.
4582 % o amount: the percentage of the difference between the original and the
4583 % blur image that is added back into the original.
4585 % o threshold: the threshold in pixels needed to apply the diffence amount.
4587 % o exception: return any errors or warnings in this structure.
4590 MagickExport Image *UnsharpMaskImage(const Image *image,
4591 const double radius,const double sigma,const double amount,
4592 const double threshold,ExceptionInfo *exception)
4594 #define SharpenImageTag "Sharpen/Image"
4615 assert(image != (const Image *) NULL);
4616 assert(image->signature == MagickSignature);
4617 if (image->debug != MagickFalse)
4618 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4619 assert(exception != (ExceptionInfo *) NULL);
4620 unsharp_image=BlurImage(image,radius,sigma,image->bias,exception);
4621 if (unsharp_image == (Image *) NULL)
4622 return((Image *) NULL);
4623 quantum_threshold=(MagickRealType) QuantumRange*threshold;
4629 image_view=AcquireCacheView(image);
4630 unsharp_view=AcquireCacheView(unsharp_image);
4631 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4632 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
4634 for (y=0; y < (ssize_t) image->rows; y++)
4636 register const Quantum
4645 if (status == MagickFalse)
4647 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
4648 q=GetCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
4650 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
4655 for (x=0; x < (ssize_t) image->columns; x++)
4660 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4672 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
4673 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
4674 unsharp_traits=GetPixelChannelMapTraits(unsharp_image,channel);
4675 if ((traits == UndefinedPixelTrait) ||
4676 (unsharp_traits == UndefinedPixelTrait))
4678 if ((unsharp_traits & CopyPixelTrait) != 0)
4683 pixel=p[i]-(MagickRealType) q[channel];
4684 if (fabs(2.0*pixel) < quantum_threshold)
4685 pixel=(MagickRealType) p[i];
4687 pixel=(MagickRealType) p[i]+amount*pixel;
4688 q[channel]=ClampToQuantum(pixel);
4690 p+=GetPixelChannels(image);
4691 q+=GetPixelChannels(unsharp_image);
4693 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
4695 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4700 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4701 #pragma omp critical (MagickCore_UnsharpMaskImage)
4703 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
4704 if (proceed == MagickFalse)
4708 unsharp_image->type=image->type;
4709 unsharp_view=DestroyCacheView(unsharp_view);
4710 image_view=DestroyCacheView(image_view);
4711 if (status == MagickFalse)
4712 unsharp_image=DestroyImage(unsharp_image);
4713 return(unsharp_image);