2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % EEEEE FFFFF FFFFF EEEEE CCCC TTTTT %
8 % EEE FFF FFF EEE C T %
10 % EEEEE F F EEEEE CCCC T %
13 % MagickCore Image Effects Methods %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/accelerate.h"
45 #include "MagickCore/blob.h"
46 #include "MagickCore/cache-view.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colorspace.h"
50 #include "MagickCore/constitute.h"
51 #include "MagickCore/decorate.h"
52 #include "MagickCore/distort.h"
53 #include "MagickCore/draw.h"
54 #include "MagickCore/enhance.h"
55 #include "MagickCore/exception.h"
56 #include "MagickCore/exception-private.h"
57 #include "MagickCore/effect.h"
58 #include "MagickCore/fx.h"
59 #include "MagickCore/gem.h"
60 #include "MagickCore/gem-private.h"
61 #include "MagickCore/geometry.h"
62 #include "MagickCore/image-private.h"
63 #include "MagickCore/list.h"
64 #include "MagickCore/log.h"
65 #include "MagickCore/memory_.h"
66 #include "MagickCore/monitor.h"
67 #include "MagickCore/monitor-private.h"
68 #include "MagickCore/montage.h"
69 #include "MagickCore/morphology.h"
70 #include "MagickCore/paint.h"
71 #include "MagickCore/pixel-accessor.h"
72 #include "MagickCore/property.h"
73 #include "MagickCore/quantize.h"
74 #include "MagickCore/quantum.h"
75 #include "MagickCore/quantum-private.h"
76 #include "MagickCore/random_.h"
77 #include "MagickCore/random-private.h"
78 #include "MagickCore/resample.h"
79 #include "MagickCore/resample-private.h"
80 #include "MagickCore/resize.h"
81 #include "MagickCore/resource_.h"
82 #include "MagickCore/segment.h"
83 #include "MagickCore/shear.h"
84 #include "MagickCore/signature-private.h"
85 #include "MagickCore/statistic.h"
86 #include "MagickCore/string_.h"
87 #include "MagickCore/thread-private.h"
88 #include "MagickCore/transform.h"
89 #include "MagickCore/threshold.h"
92 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96 % A d a p t i v e B l u r I m a g e %
100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102 % AdaptiveBlurImage() adaptively blurs the image by blurring less
103 % intensely near image edges and more intensely far from edges. We blur the
104 % image with a Gaussian operator of the given radius and standard deviation
105 % (sigma). For reasonable results, radius should be larger than sigma. Use a
106 % radius of 0 and AdaptiveBlurImage() selects a suitable radius for you.
108 % The format of the AdaptiveBlurImage method is:
110 % Image *AdaptiveBlurImage(const Image *image,const double radius,
111 % const double sigma,const double bias,ExceptionInfo *exception)
113 % A description of each parameter follows:
115 % o image: the image.
117 % o radius: the radius of the Gaussian, in pixels, not counting the center
120 % o sigma: the standard deviation of the Laplacian, in pixels.
124 % o exception: return any errors or warnings in this structure.
128 MagickExport MagickBooleanType AdaptiveLevelImage(Image *image,
129 const char *levels,ExceptionInfo *exception)
148 if (levels == (char *) NULL)
150 flags=ParseGeometry(levels,&geometry_info);
151 black_point=geometry_info.rho;
152 white_point=(double) QuantumRange;
153 if ((flags & SigmaValue) != 0)
154 white_point=geometry_info.sigma;
156 if ((flags & XiValue) != 0)
157 gamma=geometry_info.xi;
158 if ((flags & PercentValue) != 0)
160 black_point*=(double) image->columns*image->rows/100.0;
161 white_point*=(double) image->columns*image->rows/100.0;
163 if ((flags & SigmaValue) == 0)
164 white_point=(double) QuantumRange-black_point;
165 if ((flags & AspectValue ) == 0)
166 status=LevelImage(image,black_point,white_point,gamma,exception);
168 status=LevelizeImage(image,black_point,white_point,gamma,exception);
172 MagickExport Image *AdaptiveBlurImage(const Image *image,const double radius,
173 const double sigma,const double bias,ExceptionInfo *exception)
175 #define AdaptiveBlurImageTag "Convolve/Image"
176 #define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
211 assert(image != (const Image *) NULL);
212 assert(image->signature == MagickSignature);
213 if (image->debug != MagickFalse)
214 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
215 assert(exception != (ExceptionInfo *) NULL);
216 assert(exception->signature == MagickSignature);
217 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
218 if (blur_image == (Image *) NULL)
219 return((Image *) NULL);
220 if (fabs(sigma) <= MagickEpsilon)
222 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
224 blur_image=DestroyImage(blur_image);
225 return((Image *) NULL);
228 Edge detect the image brighness channel, level, blur, and level again.
230 edge_image=EdgeImage(image,radius,sigma,exception);
231 if (edge_image == (Image *) NULL)
233 blur_image=DestroyImage(blur_image);
234 return((Image *) NULL);
236 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
237 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
238 if (gaussian_image != (Image *) NULL)
240 edge_image=DestroyImage(edge_image);
241 edge_image=gaussian_image;
243 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
245 Create a set of kernels from maximum (radius,sigma) to minimum.
247 width=GetOptimalKernelWidth2D(radius,sigma);
248 kernel=(double **) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
249 if (kernel == (double **) NULL)
251 edge_image=DestroyImage(edge_image);
252 blur_image=DestroyImage(blur_image);
253 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
255 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
256 for (i=0; i < (ssize_t) width; i+=2)
258 kernel[i]=(double *) AcquireAlignedMemory((size_t) (width-i),(width-i)*
260 if (kernel[i] == (double *) NULL)
263 j=(ssize_t) (width-i)/2;
265 for (v=(-j); v <= j; v++)
267 for (u=(-j); u <= j; u++)
269 kernel[i][k]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
270 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
271 normalize+=kernel[i][k];
275 if (fabs(normalize) <= MagickEpsilon)
277 normalize=1.0/normalize;
278 for (k=0; k < (j*j); k++)
279 kernel[i][k]=normalize*kernel[i][k];
281 if (i < (ssize_t) width)
283 for (i-=2; i >= 0; i-=2)
284 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
285 kernel=(double **) RelinquishAlignedMemory(kernel);
286 edge_image=DestroyImage(edge_image);
287 blur_image=DestroyImage(blur_image);
288 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
291 Adaptively blur image.
295 image_view=AcquireCacheView(image);
296 edge_view=AcquireCacheView(edge_image);
297 blur_view=AcquireCacheView(blur_image);
298 #if defined(MAGICKCORE_OPENMP_SUPPORT)
299 #pragma omp parallel for schedule(static,4) shared(progress,status)
301 for (y=0; y < (ssize_t) blur_image->rows; y++)
303 register const Quantum
312 if (status == MagickFalse)
314 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
315 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
317 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
322 for (x=0; x < (ssize_t) blur_image->columns; x++)
324 register const Quantum
334 j=(ssize_t) ceil((double) width*QuantumScale*
335 GetPixelIntensity(edge_image,r)-0.5);
339 if (j > (ssize_t) width)
343 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
344 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
345 if (p == (const Quantum *) NULL)
347 center=(ssize_t) GetPixelChannels(image)*(width-j)*
348 ((width-j)/2L)+GetPixelChannels(image)*((width-j)/2L);
349 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
363 register const double
366 register const Quantum
375 channel=GetPixelChannelMapChannel(image,i);
376 traits=GetPixelChannelMapTraits(image,channel);
377 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
378 if ((traits == UndefinedPixelTrait) ||
379 (blur_traits == UndefinedPixelTrait))
381 if ((blur_traits & CopyPixelTrait) != 0)
383 SetPixelChannel(blur_image,channel,p[center+i],q);
390 if ((blur_traits & BlendPixelTrait) == 0)
395 for (v=0; v < (ssize_t) (width-j); v++)
397 for (u=0; u < (ssize_t) (width-j); u++)
399 pixel+=(*k)*pixels[i];
402 pixels+=GetPixelChannels(image);
405 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
406 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
412 for (v=0; v < (ssize_t) (width-j); v++)
414 for (u=0; u < (ssize_t) (width-j); u++)
416 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
417 pixel+=(*k)*alpha*pixels[i];
420 pixels+=GetPixelChannels(image);
423 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
424 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
426 q+=GetPixelChannels(blur_image);
427 r+=GetPixelChannels(edge_image);
429 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
431 if (image->progress_monitor != (MagickProgressMonitor) NULL)
436 #if defined(MAGICKCORE_OPENMP_SUPPORT)
437 #pragma omp critical (MagickCore_AdaptiveBlurImage)
439 proceed=SetImageProgress(image,AdaptiveBlurImageTag,progress++,
441 if (proceed == MagickFalse)
445 blur_image->type=image->type;
446 blur_view=DestroyCacheView(blur_view);
447 edge_view=DestroyCacheView(edge_view);
448 image_view=DestroyCacheView(image_view);
449 edge_image=DestroyImage(edge_image);
450 for (i=0; i < (ssize_t) width; i+=2)
451 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
452 kernel=(double **) RelinquishAlignedMemory(kernel);
453 if (status == MagickFalse)
454 blur_image=DestroyImage(blur_image);
459 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
463 % A d a p t i v e S h a r p e n I m a g e %
467 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
469 % AdaptiveSharpenImage() adaptively sharpens the image by sharpening more
470 % intensely near image edges and less intensely far from edges. We sharpen the
471 % image with a Gaussian operator of the given radius and standard deviation
472 % (sigma). For reasonable results, radius should be larger than sigma. Use a
473 % radius of 0 and AdaptiveSharpenImage() selects a suitable radius for you.
475 % The format of the AdaptiveSharpenImage method is:
477 % Image *AdaptiveSharpenImage(const Image *image,const double radius,
478 % const double sigma,const double bias,ExceptionInfo *exception)
480 % A description of each parameter follows:
482 % o image: the image.
484 % o radius: the radius of the Gaussian, in pixels, not counting the center
487 % o sigma: the standard deviation of the Laplacian, in pixels.
491 % o exception: return any errors or warnings in this structure.
494 MagickExport Image *AdaptiveSharpenImage(const Image *image,const double radius,
495 const double sigma,const double bias,ExceptionInfo *exception)
497 #define AdaptiveSharpenImageTag "Convolve/Image"
498 #define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
533 assert(image != (const Image *) NULL);
534 assert(image->signature == MagickSignature);
535 if (image->debug != MagickFalse)
536 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
537 assert(exception != (ExceptionInfo *) NULL);
538 assert(exception->signature == MagickSignature);
539 sharp_image=CloneImage(image,0,0,MagickTrue,exception);
540 if (sharp_image == (Image *) NULL)
541 return((Image *) NULL);
542 if (fabs(sigma) <= MagickEpsilon)
544 if (SetImageStorageClass(sharp_image,DirectClass,exception) == MagickFalse)
546 sharp_image=DestroyImage(sharp_image);
547 return((Image *) NULL);
550 Edge detect the image brighness channel, level, sharp, and level again.
552 edge_image=EdgeImage(image,radius,sigma,exception);
553 if (edge_image == (Image *) NULL)
555 sharp_image=DestroyImage(sharp_image);
556 return((Image *) NULL);
558 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
559 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
560 if (gaussian_image != (Image *) NULL)
562 edge_image=DestroyImage(edge_image);
563 edge_image=gaussian_image;
565 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
567 Create a set of kernels from maximum (radius,sigma) to minimum.
569 width=GetOptimalKernelWidth2D(radius,sigma);
570 kernel=(double **) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
571 if (kernel == (double **) NULL)
573 edge_image=DestroyImage(edge_image);
574 sharp_image=DestroyImage(sharp_image);
575 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
577 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
578 for (i=0; i < (ssize_t) width; i+=2)
580 kernel[i]=(double *) AcquireAlignedMemory((size_t) (width-i),(width-i)*
582 if (kernel[i] == (double *) NULL)
585 j=(ssize_t) (width-i)/2;
587 for (v=(-j); v <= j; v++)
589 for (u=(-j); u <= j; u++)
591 kernel[i][k]=(double) (-exp(-((double) u*u+v*v)/(2.0*MagickSigma*
592 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
593 normalize+=kernel[i][k];
597 if (fabs(normalize) <= MagickEpsilon)
599 normalize=1.0/normalize;
600 for (k=0; k < (j*j); k++)
601 kernel[i][k]=normalize*kernel[i][k];
603 if (i < (ssize_t) width)
605 for (i-=2; i >= 0; i-=2)
606 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
607 kernel=(double **) RelinquishAlignedMemory(kernel);
608 edge_image=DestroyImage(edge_image);
609 sharp_image=DestroyImage(sharp_image);
610 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
613 Adaptively sharpen image.
617 image_view=AcquireCacheView(image);
618 edge_view=AcquireCacheView(edge_image);
619 sharp_view=AcquireCacheView(sharp_image);
620 #if defined(MAGICKCORE_OPENMP_SUPPORT)
621 #pragma omp parallel for schedule(static,4) shared(progress,status)
623 for (y=0; y < (ssize_t) sharp_image->rows; y++)
625 register const Quantum
634 if (status == MagickFalse)
636 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
637 q=QueueCacheViewAuthenticPixels(sharp_view,0,y,sharp_image->columns,1,
639 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
644 for (x=0; x < (ssize_t) sharp_image->columns; x++)
646 register const Quantum
656 j=(ssize_t) ceil((double) width*QuantumScale*
657 GetPixelIntensity(edge_image,r)-0.5);
661 if (j > (ssize_t) width)
665 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
666 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
667 if (p == (const Quantum *) NULL)
669 center=(ssize_t) GetPixelChannels(image)*(width-j)*
670 ((width-j)/2L)+GetPixelChannels(image)*((width-j)/2);
671 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
685 register const double
688 register const Quantum
697 channel=GetPixelChannelMapChannel(image,i);
698 traits=GetPixelChannelMapTraits(image,channel);
699 sharp_traits=GetPixelChannelMapTraits(sharp_image,channel);
700 if ((traits == UndefinedPixelTrait) ||
701 (sharp_traits == UndefinedPixelTrait))
703 if ((sharp_traits & CopyPixelTrait) != 0)
705 SetPixelChannel(sharp_image,channel,p[center+i],q);
712 if ((sharp_traits & BlendPixelTrait) == 0)
717 for (v=0; v < (ssize_t) (width-j); v++)
719 for (u=0; u < (ssize_t) (width-j); u++)
721 pixel+=(*k)*pixels[i];
724 pixels+=GetPixelChannels(image);
727 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
728 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
734 for (v=0; v < (ssize_t) (width-j); v++)
736 for (u=0; u < (ssize_t) (width-j); u++)
738 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
739 pixel+=(*k)*alpha*pixels[i];
742 pixels+=GetPixelChannels(image);
745 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
746 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
748 q+=GetPixelChannels(sharp_image);
749 r+=GetPixelChannels(edge_image);
751 if (SyncCacheViewAuthenticPixels(sharp_view,exception) == MagickFalse)
753 if (image->progress_monitor != (MagickProgressMonitor) NULL)
758 #if defined(MAGICKCORE_OPENMP_SUPPORT)
759 #pragma omp critical (MagickCore_AdaptiveSharpenImage)
761 proceed=SetImageProgress(image,AdaptiveSharpenImageTag,progress++,
763 if (proceed == MagickFalse)
767 sharp_image->type=image->type;
768 sharp_view=DestroyCacheView(sharp_view);
769 edge_view=DestroyCacheView(edge_view);
770 image_view=DestroyCacheView(image_view);
771 edge_image=DestroyImage(edge_image);
772 for (i=0; i < (ssize_t) width; i+=2)
773 kernel[i]=(double *) RelinquishAlignedMemory(kernel[i]);
774 kernel=(double **) RelinquishAlignedMemory(kernel);
775 if (status == MagickFalse)
776 sharp_image=DestroyImage(sharp_image);
781 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
785 % B l u r I m a g e %
789 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
791 % BlurImage() blurs an image. We convolve the image with a Gaussian operator
792 % of the given radius and standard deviation (sigma). For reasonable results,
793 % the radius should be larger than sigma. Use a radius of 0 and BlurImage()
794 % selects a suitable radius for you.
796 % BlurImage() differs from GaussianBlurImage() in that it uses a separable
797 % kernel which is faster but mathematically equivalent to the non-separable
800 % The format of the BlurImage method is:
802 % Image *BlurImage(const Image *image,const double radius,
803 % const double sigma,const double bias,ExceptionInfo *exception)
805 % A description of each parameter follows:
807 % o image: the image.
809 % o radius: the radius of the Gaussian, in pixels, not counting the center
812 % o sigma: the standard deviation of the Gaussian, in pixels.
816 % o exception: return any errors or warnings in this structure.
820 static double *GetBlurKernel(const size_t width,const double sigma)
834 Generate a 1-D convolution kernel.
836 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
837 kernel=(double *) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
838 if (kernel == (double *) NULL)
843 for (k=(-j); k <= j; k++)
845 kernel[i]=(double) (exp(-((double) k*k)/(2.0*MagickSigma*MagickSigma))/
846 (MagickSQ2PI*MagickSigma));
847 normalize+=kernel[i];
850 for (i=0; i < (ssize_t) width; i++)
851 kernel[i]/=normalize;
855 MagickExport Image *BlurImage(const Image *image,const double radius,
856 const double sigma,const double bias,ExceptionInfo *exception)
858 #define BlurImageTag "Blur/Image"
888 Initialize blur image attributes.
890 assert(image != (Image *) NULL);
891 assert(image->signature == MagickSignature);
892 if (image->debug != MagickFalse)
893 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
894 assert(exception != (ExceptionInfo *) NULL);
895 assert(exception->signature == MagickSignature);
896 blur_image=CloneImage(image,0,0,MagickTrue,exception);
897 if (blur_image == (Image *) NULL)
898 return((Image *) NULL);
899 if (fabs(sigma) <= MagickEpsilon)
901 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
903 blur_image=DestroyImage(blur_image);
904 return((Image *) NULL);
906 width=GetOptimalKernelWidth1D(radius,sigma);
907 kernel=GetBlurKernel(width,sigma);
908 if (kernel == (double *) NULL)
910 blur_image=DestroyImage(blur_image);
911 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
913 if (image->debug != MagickFalse)
916 format[MaxTextExtent],
919 register const double
922 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
923 " blur image with kernel width %.20g:",(double) width);
924 message=AcquireString("");
926 for (i=0; i < (ssize_t) width; i++)
929 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) i);
930 (void) ConcatenateString(&message,format);
931 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
932 (void) ConcatenateString(&message,format);
933 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
935 message=DestroyString(message);
942 center=(ssize_t) GetPixelChannels(image)*(width/2L);
943 image_view=AcquireCacheView(image);
944 blur_view=AcquireCacheView(blur_image);
945 #if defined(MAGICKCORE_OPENMP_SUPPORT)
946 #pragma omp parallel for schedule(static,4) shared(progress,status)
948 for (y=0; y < (ssize_t) image->rows; y++)
950 register const Quantum
959 if (status == MagickFalse)
961 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y,
962 image->columns+width,1,exception);
963 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
965 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
970 for (x=0; x < (ssize_t) image->columns; x++)
975 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
989 register const double
992 register const Quantum
998 channel=GetPixelChannelMapChannel(image,i);
999 traits=GetPixelChannelMapTraits(image,channel);
1000 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
1001 if ((traits == UndefinedPixelTrait) ||
1002 (blur_traits == UndefinedPixelTrait))
1004 if ((blur_traits & CopyPixelTrait) != 0)
1006 SetPixelChannel(blur_image,channel,p[center+i],q);
1012 if ((blur_traits & BlendPixelTrait) == 0)
1017 for (u=0; u < (ssize_t) width; u++)
1019 pixel+=(*k)*pixels[i];
1021 pixels+=GetPixelChannels(image);
1023 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
1030 for (u=0; u < (ssize_t) width; u++)
1032 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
1033 pixel+=(*k)*alpha*pixels[i];
1036 pixels+=GetPixelChannels(image);
1038 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1039 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
1041 p+=GetPixelChannels(image);
1042 q+=GetPixelChannels(blur_image);
1044 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1046 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1051 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1052 #pragma omp critical (MagickCore_BlurImage)
1054 proceed=SetImageProgress(image,BlurImageTag,progress++,blur_image->rows+
1055 blur_image->columns);
1056 if (proceed == MagickFalse)
1060 blur_view=DestroyCacheView(blur_view);
1061 image_view=DestroyCacheView(image_view);
1065 center=(ssize_t) GetPixelChannels(blur_image)*(width/2L);
1066 image_view=AcquireCacheView(blur_image);
1067 blur_view=AcquireCacheView(blur_image);
1068 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1069 #pragma omp parallel for schedule(static,4) shared(progress,status)
1071 for (x=0; x < (ssize_t) blur_image->columns; x++)
1073 register const Quantum
1082 if (status == MagickFalse)
1084 p=GetCacheViewVirtualPixels(image_view,x,-((ssize_t) width/2L),1,
1085 blur_image->rows+width,exception);
1086 q=GetCacheViewAuthenticPixels(blur_view,x,0,1,blur_image->rows,exception);
1087 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1092 for (y=0; y < (ssize_t) blur_image->rows; y++)
1097 for (i=0; i < (ssize_t) GetPixelChannels(blur_image); i++)
1111 register const double
1114 register const Quantum
1120 channel=GetPixelChannelMapChannel(blur_image,i);
1121 traits=GetPixelChannelMapTraits(blur_image,channel);
1122 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
1123 if ((traits == UndefinedPixelTrait) ||
1124 (blur_traits == UndefinedPixelTrait))
1126 if ((blur_traits & CopyPixelTrait) != 0)
1128 SetPixelChannel(blur_image,channel,p[center+i],q);
1134 if ((blur_traits & BlendPixelTrait) == 0)
1139 for (u=0; u < (ssize_t) width; u++)
1141 pixel+=(*k)*pixels[i];
1143 pixels+=GetPixelChannels(blur_image);
1145 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
1152 for (u=0; u < (ssize_t) width; u++)
1154 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(blur_image,
1156 pixel+=(*k)*alpha*pixels[i];
1159 pixels+=GetPixelChannels(blur_image);
1161 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1162 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
1164 p+=GetPixelChannels(blur_image);
1165 q+=GetPixelChannels(blur_image);
1167 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1169 if (blur_image->progress_monitor != (MagickProgressMonitor) NULL)
1174 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1175 #pragma omp critical (MagickCore_BlurImage)
1177 proceed=SetImageProgress(blur_image,BlurImageTag,progress++,
1178 blur_image->rows+blur_image->columns);
1179 if (proceed == MagickFalse)
1183 blur_view=DestroyCacheView(blur_view);
1184 image_view=DestroyCacheView(image_view);
1185 kernel=(double *) RelinquishAlignedMemory(kernel);
1186 blur_image->type=image->type;
1187 if (status == MagickFalse)
1188 blur_image=DestroyImage(blur_image);
1193 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1197 % C o n v o l v e I m a g e %
1201 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1203 % ConvolveImage() applies a custom convolution kernel to the image.
1205 % The format of the ConvolveImage method is:
1207 % Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
1208 % ExceptionInfo *exception)
1210 % A description of each parameter follows:
1212 % o image: the image.
1214 % o kernel: the filtering kernel.
1216 % o exception: return any errors or warnings in this structure.
1219 MagickExport Image *ConvolveImage(const Image *image,
1220 const KernelInfo *kernel_info,ExceptionInfo *exception)
1222 #define ConvolveImageTag "Convolve/Image"
1242 Initialize convolve image attributes.
1244 assert(image != (Image *) NULL);
1245 assert(image->signature == MagickSignature);
1246 if (image->debug != MagickFalse)
1247 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1248 assert(exception != (ExceptionInfo *) NULL);
1249 assert(exception->signature == MagickSignature);
1250 if ((kernel_info->width % 2) == 0)
1251 ThrowImageException(OptionError,"KernelWidthMustBeAnOddNumber");
1252 convolve_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1254 if (convolve_image == (Image *) NULL)
1255 return((Image *) NULL);
1256 if (SetImageStorageClass(convolve_image,DirectClass,exception) == MagickFalse)
1258 convolve_image=DestroyImage(convolve_image);
1259 return((Image *) NULL);
1261 if (image->debug != MagickFalse)
1264 format[MaxTextExtent],
1267 register const MagickRealType
1276 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1277 " ConvolveImage with %.20gx%.20g kernel:",(double) kernel_info->width,
1278 (double) kernel_info->height);
1279 message=AcquireString("");
1280 k=kernel_info->values;
1281 for (v=0; v < (ssize_t) kernel_info->width; v++)
1284 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
1285 (void) ConcatenateString(&message,format);
1286 for (u=0; u < (ssize_t) kernel_info->height; u++)
1288 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
1289 (void) ConcatenateString(&message,format);
1291 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
1293 message=DestroyString(message);
1295 status=AccelerateConvolveImage(image,kernel_info,convolve_image,exception);
1296 if (status == MagickTrue)
1297 return(convolve_image);
1300 FUTURE: Use Morphology Convolve instead.
1302 center=(ssize_t) GetPixelChannels(image)*(image->columns+kernel_info->width)*
1303 (kernel_info->height/2L)+GetPixelChannels(image)*(kernel_info->width/2L);
1306 image_view=AcquireCacheView(image);
1307 convolve_view=AcquireCacheView(convolve_image);
1308 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1309 #pragma omp parallel for schedule(static,4) shared(progress,status)
1311 for (y=0; y < (ssize_t) image->rows; y++)
1313 register const Quantum
1322 if (status == MagickFalse)
1324 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) kernel_info->width/2L),y-
1325 (ssize_t) (kernel_info->height/2L),image->columns+kernel_info->width,
1326 kernel_info->height,exception);
1327 q=QueueCacheViewAuthenticPixels(convolve_view,0,y,convolve_image->columns,1,
1329 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1334 for (x=0; x < (ssize_t) image->columns; x++)
1339 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1353 register const MagickRealType
1356 register const Quantum
1365 channel=GetPixelChannelMapChannel(image,i);
1366 traits=GetPixelChannelMapTraits(image,channel);
1367 convolve_traits=GetPixelChannelMapTraits(convolve_image,channel);
1368 if ((traits == UndefinedPixelTrait) ||
1369 (convolve_traits == UndefinedPixelTrait))
1371 if ((convolve_traits & CopyPixelTrait) != 0)
1373 SetPixelChannel(convolve_image,channel,p[center+i],q);
1376 k=kernel_info->values;
1378 pixel=kernel_info->bias;
1379 if ((convolve_traits & BlendPixelTrait) == 0)
1384 for (v=0; v < (ssize_t) kernel_info->height; v++)
1386 for (u=0; u < (ssize_t) kernel_info->width; u++)
1388 pixel+=(*k)*pixels[i];
1390 pixels+=GetPixelChannels(image);
1392 pixels+=image->columns*GetPixelChannels(image);
1394 SetPixelChannel(convolve_image,channel,ClampToQuantum(pixel),q);
1401 for (v=0; v < (ssize_t) kernel_info->height; v++)
1403 for (u=0; u < (ssize_t) kernel_info->width; u++)
1405 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
1406 pixel+=(*k)*alpha*pixels[i];
1409 pixels+=GetPixelChannels(image);
1411 pixels+=image->columns*GetPixelChannels(image);
1413 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1414 SetPixelChannel(convolve_image,channel,ClampToQuantum(gamma*pixel),q);
1416 p+=GetPixelChannels(image);
1417 q+=GetPixelChannels(convolve_image);
1419 if (SyncCacheViewAuthenticPixels(convolve_view,exception) == MagickFalse)
1421 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1426 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1427 #pragma omp critical (MagickCore_ConvolveImage)
1429 proceed=SetImageProgress(image,ConvolveImageTag,progress++,image->rows);
1430 if (proceed == MagickFalse)
1434 convolve_image->type=image->type;
1435 convolve_view=DestroyCacheView(convolve_view);
1436 image_view=DestroyCacheView(image_view);
1437 if (status == MagickFalse)
1438 convolve_image=DestroyImage(convolve_image);
1439 return(convolve_image);
1443 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1447 % D e s p e c k l e I m a g e %
1451 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1453 % DespeckleImage() reduces the speckle noise in an image while perserving the
1454 % edges of the original image. A speckle removing filter uses a complementary % hulling technique (raising pixels that are darker than their surrounding
1455 % neighbors, then complementarily lowering pixels that are brighter than their
1456 % surrounding neighbors) to reduce the speckle index of that image (reference
1457 % Crimmins speckle removal).
1459 % The format of the DespeckleImage method is:
1461 % Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1463 % A description of each parameter follows:
1465 % o image: the image.
1467 % o exception: return any errors or warnings in this structure.
1471 static void Hull(const ssize_t x_offset,const ssize_t y_offset,
1472 const size_t columns,const size_t rows,const int polarity,Quantum *restrict f,
1473 Quantum *restrict g)
1484 assert(f != (Quantum *) NULL);
1485 assert(g != (Quantum *) NULL);
1488 r=p+(y_offset*(columns+2)+x_offset);
1489 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1490 #pragma omp parallel for schedule(static)
1492 for (y=0; y < (ssize_t) rows; y++)
1501 i=(2*y+1)+y*columns;
1503 for (x=0; x < (ssize_t) columns; x++)
1505 v=(SignedQuantum) p[i];
1506 if ((SignedQuantum) r[i] >= (v+ScaleCharToQuantum(2)))
1507 v+=ScaleCharToQuantum(1);
1512 for (x=0; x < (ssize_t) columns; x++)
1514 v=(SignedQuantum) p[i];
1515 if ((SignedQuantum) r[i] <= (v-ScaleCharToQuantum(2)))
1516 v-=ScaleCharToQuantum(1);
1523 r=q+(y_offset*(columns+2)+x_offset);
1524 s=q-(y_offset*(columns+2)+x_offset);
1525 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1526 #pragma omp parallel for schedule(static)
1528 for (y=0; y < (ssize_t) rows; y++)
1537 i=(2*y+1)+y*columns;
1539 for (x=0; x < (ssize_t) columns; x++)
1541 v=(SignedQuantum) q[i];
1542 if (((SignedQuantum) s[i] >= (v+ScaleCharToQuantum(2))) &&
1543 ((SignedQuantum) r[i] > v))
1544 v+=ScaleCharToQuantum(1);
1549 for (x=0; x < (ssize_t) columns; x++)
1551 v=(SignedQuantum) q[i];
1552 if (((SignedQuantum) s[i] <= (v-ScaleCharToQuantum(2))) &&
1553 ((SignedQuantum) r[i] < v))
1554 v-=ScaleCharToQuantum(1);
1561 MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1563 #define DespeckleImageTag "Despeckle/Image"
1585 static const ssize_t
1586 X[4] = {0, 1, 1,-1},
1587 Y[4] = {1, 0, 1, 1};
1590 Allocate despeckled image.
1592 assert(image != (const Image *) NULL);
1593 assert(image->signature == MagickSignature);
1594 if (image->debug != MagickFalse)
1595 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1596 assert(exception != (ExceptionInfo *) NULL);
1597 assert(exception->signature == MagickSignature);
1598 despeckle_image=CloneImage(image,0,0,MagickTrue,exception);
1599 if (despeckle_image == (Image *) NULL)
1600 return((Image *) NULL);
1601 status=SetImageStorageClass(despeckle_image,DirectClass,exception);
1602 if (status == MagickFalse)
1604 despeckle_image=DestroyImage(despeckle_image);
1605 return((Image *) NULL);
1608 Allocate image buffer.
1610 length=(size_t) ((image->columns+2)*(image->rows+2));
1611 pixels=(Quantum *) AcquireQuantumMemory(length,sizeof(*pixels));
1612 buffer=(Quantum *) AcquireQuantumMemory(length,sizeof(*buffer));
1613 if ((pixels == (Quantum *) NULL) || (buffer == (Quantum *) NULL))
1615 if (buffer != (Quantum *) NULL)
1616 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1617 if (pixels != (Quantum *) NULL)
1618 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1619 despeckle_image=DestroyImage(despeckle_image);
1620 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1623 Reduce speckle in the image.
1626 image_view=AcquireCacheView(image);
1627 despeckle_view=AcquireCacheView(despeckle_image);
1628 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1645 if (status == MagickFalse)
1647 channel=GetPixelChannelMapChannel(image,i);
1648 traits=GetPixelChannelMapTraits(image,channel);
1649 despeckle_traits=GetPixelChannelMapTraits(despeckle_image,channel);
1650 if ((traits == UndefinedPixelTrait) ||
1651 (despeckle_traits == UndefinedPixelTrait))
1653 if ((despeckle_traits & CopyPixelTrait) != 0)
1655 (void) ResetMagickMemory(pixels,0,length*sizeof(*pixels));
1656 j=(ssize_t) image->columns+2;
1657 for (y=0; y < (ssize_t) image->rows; y++)
1659 register const Quantum
1662 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1663 if (p == (const Quantum *) NULL)
1669 for (x=0; x < (ssize_t) image->columns; x++)
1672 p+=GetPixelChannels(image);
1676 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
1677 for (k=0; k < 4; k++)
1679 Hull(X[k],Y[k],image->columns,image->rows,1,pixels,buffer);
1680 Hull(-X[k],-Y[k],image->columns,image->rows,1,pixels,buffer);
1681 Hull(-X[k],-Y[k],image->columns,image->rows,-1,pixels,buffer);
1682 Hull(X[k],Y[k],image->columns,image->rows,-1,pixels,buffer);
1684 j=(ssize_t) image->columns+2;
1685 for (y=0; y < (ssize_t) image->rows; y++)
1693 q=QueueCacheViewAuthenticPixels(despeckle_view,0,y,
1694 despeckle_image->columns,1,exception);
1695 if (q == (Quantum *) NULL)
1701 for (x=0; x < (ssize_t) image->columns; x++)
1703 SetPixelChannel(despeckle_image,channel,pixels[j++],q);
1704 q+=GetPixelChannels(despeckle_image);
1706 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1707 if (sync == MagickFalse)
1711 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1716 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1717 GetPixelChannels(image));
1718 if (proceed == MagickFalse)
1722 despeckle_view=DestroyCacheView(despeckle_view);
1723 image_view=DestroyCacheView(image_view);
1724 buffer=(Quantum *) RelinquishMagickMemory(buffer);
1725 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1726 despeckle_image->type=image->type;
1727 if (status == MagickFalse)
1728 despeckle_image=DestroyImage(despeckle_image);
1729 return(despeckle_image);
1733 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1737 % E d g e I m a g e %
1741 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1743 % EdgeImage() finds edges in an image. Radius defines the radius of the
1744 % convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1747 % The format of the EdgeImage method is:
1749 % Image *EdgeImage(const Image *image,const double radius,
1750 % const double sigma,ExceptionInfo *exception)
1752 % A description of each parameter follows:
1754 % o image: the image.
1756 % o radius: the radius of the pixel neighborhood.
1758 % o sigma: the standard deviation of the Gaussian, in pixels.
1760 % o exception: return any errors or warnings in this structure.
1763 MagickExport Image *EdgeImage(const Image *image,const double radius,
1764 const double sigma,ExceptionInfo *exception)
1783 assert(image != (const Image *) NULL);
1784 assert(image->signature == MagickSignature);
1785 if (image->debug != MagickFalse)
1786 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1787 assert(exception != (ExceptionInfo *) NULL);
1788 assert(exception->signature == MagickSignature);
1789 width=GetOptimalKernelWidth1D(radius,sigma);
1790 kernel_info=AcquireKernelInfo((const char *) NULL);
1791 if (kernel_info == (KernelInfo *) NULL)
1792 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1793 kernel_info->width=width;
1794 kernel_info->height=width;
1795 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1796 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1797 if (kernel_info->values == (MagickRealType *) NULL)
1799 kernel_info=DestroyKernelInfo(kernel_info);
1800 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1802 j=(ssize_t) kernel_info->width/2;
1804 for (v=(-j); v <= j; v++)
1806 for (u=(-j); u <= j; u++)
1808 kernel_info->values[i]=(-1.0);
1812 kernel_info->values[i/2]=(double) (width*width-1.0);
1813 kernel_info->bias=image->bias; /* FUTURE: User bias on a edge image? */
1814 edge_image=ConvolveImage(image,kernel_info,exception);
1815 kernel_info=DestroyKernelInfo(kernel_info);
1820 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1824 % E m b o s s I m a g e %
1828 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1830 % EmbossImage() returns a grayscale image with a three-dimensional effect.
1831 % We convolve the image with a Gaussian operator of the given radius and
1832 % standard deviation (sigma). For reasonable results, radius should be
1833 % larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1836 % The format of the EmbossImage method is:
1838 % Image *EmbossImage(const Image *image,const double radius,
1839 % const double sigma,ExceptionInfo *exception)
1841 % A description of each parameter follows:
1843 % o image: the image.
1845 % o radius: the radius of the pixel neighborhood.
1847 % o sigma: the standard deviation of the Gaussian, in pixels.
1849 % o exception: return any errors or warnings in this structure.
1852 MagickExport Image *EmbossImage(const Image *image,const double radius,
1853 const double sigma,ExceptionInfo *exception)
1873 assert(image != (const Image *) NULL);
1874 assert(image->signature == MagickSignature);
1875 if (image->debug != MagickFalse)
1876 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1877 assert(exception != (ExceptionInfo *) NULL);
1878 assert(exception->signature == MagickSignature);
1879 width=GetOptimalKernelWidth1D(radius,sigma);
1880 kernel_info=AcquireKernelInfo((const char *) NULL);
1881 if (kernel_info == (KernelInfo *) NULL)
1882 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1883 kernel_info->width=width;
1884 kernel_info->height=width;
1885 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1886 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1887 if (kernel_info->values == (MagickRealType *) NULL)
1889 kernel_info=DestroyKernelInfo(kernel_info);
1890 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1892 j=(ssize_t) kernel_info->width/2;
1895 for (v=(-j); v <= j; v++)
1897 for (u=(-j); u <= j; u++)
1899 kernel_info->values[i]=(double) (((u < 0) || (v < 0) ? -8.0 : 8.0)*
1900 exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
1901 (2.0*MagickPI*MagickSigma*MagickSigma));
1903 kernel_info->values[i]=0.0;
1908 kernel_info->bias=image->bias; /* FUTURE: user bias on an edge image */
1909 emboss_image=ConvolveImage(image,kernel_info,exception);
1910 kernel_info=DestroyKernelInfo(kernel_info);
1911 if (emboss_image != (Image *) NULL)
1912 (void) EqualizeImage(emboss_image,exception);
1913 return(emboss_image);
1917 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1921 % G a u s s i a n B l u r I m a g e %
1925 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1927 % GaussianBlurImage() blurs an image. We convolve the image with a
1928 % Gaussian operator of the given radius and standard deviation (sigma).
1929 % For reasonable results, the radius should be larger than sigma. Use a
1930 % radius of 0 and GaussianBlurImage() selects a suitable radius for you
1932 % The format of the GaussianBlurImage method is:
1934 % Image *GaussianBlurImage(const Image *image,onst double radius,
1935 % const double sigma,ExceptionInfo *exception)
1937 % A description of each parameter follows:
1939 % o image: the image.
1941 % o radius: the radius of the Gaussian, in pixels, not counting the center
1944 % o sigma: the standard deviation of the Gaussian, in pixels.
1946 % o exception: return any errors or warnings in this structure.
1949 MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
1950 const double sigma,ExceptionInfo *exception)
1969 assert(image != (const Image *) NULL);
1970 assert(image->signature == MagickSignature);
1971 if (image->debug != MagickFalse)
1972 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1973 assert(exception != (ExceptionInfo *) NULL);
1974 assert(exception->signature == MagickSignature);
1975 width=GetOptimalKernelWidth2D(radius,sigma);
1976 kernel_info=AcquireKernelInfo((const char *) NULL);
1977 if (kernel_info == (KernelInfo *) NULL)
1978 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1979 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1980 kernel_info->width=width;
1981 kernel_info->height=width;
1982 kernel_info->signature=MagickSignature;
1983 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
1984 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
1985 if (kernel_info->values == (MagickRealType *) NULL)
1987 kernel_info=DestroyKernelInfo(kernel_info);
1988 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1990 j=(ssize_t) kernel_info->width/2;
1992 for (v=(-j); v <= j; v++)
1994 for (u=(-j); u <= j; u++)
1996 kernel_info->values[i]=(double) (exp(-((double) u*u+v*v)/(2.0*
1997 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
2001 blur_image=ConvolveImage(image,kernel_info,exception);
2002 kernel_info=DestroyKernelInfo(kernel_info);
2007 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2011 % M o t i o n B l u r I m a g e %
2015 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2017 % MotionBlurImage() simulates motion blur. We convolve the image with a
2018 % Gaussian operator of the given radius and standard deviation (sigma).
2019 % For reasonable results, radius should be larger than sigma. Use a
2020 % radius of 0 and MotionBlurImage() selects a suitable radius for you.
2021 % Angle gives the angle of the blurring motion.
2023 % Andrew Protano contributed this effect.
2025 % The format of the MotionBlurImage method is:
2027 % Image *MotionBlurImage(const Image *image,const double radius,
2028 % const double sigma,const double angle,const double bias,
2029 % ExceptionInfo *exception)
2031 % A description of each parameter follows:
2033 % o image: the image.
2035 % o radius: the radius of the Gaussian, in pixels, not counting
2038 % o sigma: the standard deviation of the Gaussian, in pixels.
2040 % o angle: Apply the effect along this angle.
2044 % o exception: return any errors or warnings in this structure.
2048 static double *GetMotionBlurKernel(const size_t width,const double sigma)
2058 Generate a 1-D convolution kernel.
2060 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2061 kernel=(double *) AcquireAlignedMemory((size_t) width,sizeof(*kernel));
2062 if (kernel == (double *) NULL)
2065 for (i=0; i < (ssize_t) width; i++)
2067 kernel[i]=(double) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
2068 MagickSigma)))/(MagickSQ2PI*MagickSigma));
2069 normalize+=kernel[i];
2071 for (i=0; i < (ssize_t) width; i++)
2072 kernel[i]/=normalize;
2076 MagickExport Image *MotionBlurImage(const Image *image,const double radius,
2077 const double sigma,const double angle,const double bias,
2078 ExceptionInfo *exception)
2112 assert(image != (Image *) NULL);
2113 assert(image->signature == MagickSignature);
2114 if (image->debug != MagickFalse)
2115 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2116 assert(exception != (ExceptionInfo *) NULL);
2117 width=GetOptimalKernelWidth1D(radius,sigma);
2118 kernel=GetMotionBlurKernel(width,sigma);
2119 if (kernel == (double *) NULL)
2120 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2121 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
2122 if (offset == (OffsetInfo *) NULL)
2124 kernel=(double *) RelinquishAlignedMemory(kernel);
2125 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2127 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2128 if (blur_image == (Image *) NULL)
2130 kernel=(double *) RelinquishAlignedMemory(kernel);
2131 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2132 return((Image *) NULL);
2134 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2136 kernel=(double *) RelinquishAlignedMemory(kernel);
2137 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2138 blur_image=DestroyImage(blur_image);
2139 return((Image *) NULL);
2141 point.x=(double) width*sin(DegreesToRadians(angle));
2142 point.y=(double) width*cos(DegreesToRadians(angle));
2143 for (i=0; i < (ssize_t) width; i++)
2145 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
2146 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
2153 image_view=AcquireCacheView(image);
2154 motion_view=AcquireCacheView(image);
2155 blur_view=AcquireCacheView(blur_image);
2156 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2157 #pragma omp parallel for schedule(static,4) shared(progress,status)
2159 for (y=0; y < (ssize_t) image->rows; y++)
2161 register const Quantum
2170 if (status == MagickFalse)
2172 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2173 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2175 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2180 for (x=0; x < (ssize_t) image->columns; x++)
2185 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2199 register const Quantum
2208 channel=GetPixelChannelMapChannel(image,i);
2209 traits=GetPixelChannelMapTraits(image,channel);
2210 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
2211 if ((traits == UndefinedPixelTrait) ||
2212 (blur_traits == UndefinedPixelTrait))
2214 if ((blur_traits & CopyPixelTrait) != 0)
2216 SetPixelChannel(blur_image,channel,p[i],q);
2221 if ((blur_traits & BlendPixelTrait) == 0)
2223 for (j=0; j < (ssize_t) width; j++)
2225 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+
2226 offset[j].y,1,1,exception);
2227 if (r == (const Quantum *) NULL)
2235 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
2240 for (j=0; j < (ssize_t) width; j++)
2242 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+offset[j].y,1,
2244 if (r == (const Quantum *) NULL)
2249 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,r));
2250 pixel+=(*k)*alpha*r[i];
2254 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
2255 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2257 p+=GetPixelChannels(image);
2258 q+=GetPixelChannels(blur_image);
2260 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2262 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2267 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2268 #pragma omp critical (MagickCore_MotionBlurImage)
2270 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2271 if (proceed == MagickFalse)
2275 blur_view=DestroyCacheView(blur_view);
2276 motion_view=DestroyCacheView(motion_view);
2277 image_view=DestroyCacheView(image_view);
2278 kernel=(double *) RelinquishAlignedMemory(kernel);
2279 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2280 if (status == MagickFalse)
2281 blur_image=DestroyImage(blur_image);
2286 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2290 % P r e v i e w I m a g e %
2294 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2296 % PreviewImage() tiles 9 thumbnails of the specified image with an image
2297 % processing operation applied with varying parameters. This may be helpful
2298 % pin-pointing an appropriate parameter for a particular image processing
2301 % The format of the PreviewImages method is:
2303 % Image *PreviewImages(const Image *image,const PreviewType preview,
2304 % ExceptionInfo *exception)
2306 % A description of each parameter follows:
2308 % o image: the image.
2310 % o preview: the image processing operation.
2312 % o exception: return any errors or warnings in this structure.
2315 MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2316 ExceptionInfo *exception)
2318 #define NumberTiles 9
2319 #define PreviewImageTag "Preview/Image"
2320 #define DefaultPreviewGeometry "204x204+10+10"
2323 factor[MaxTextExtent],
2324 label[MaxTextExtent];
2366 Open output image file.
2368 assert(image != (Image *) NULL);
2369 assert(image->signature == MagickSignature);
2370 if (image->debug != MagickFalse)
2371 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2375 preview_info=AcquireImageInfo();
2376 SetGeometry(image,&geometry);
2377 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2378 &geometry.width,&geometry.height);
2379 images=NewImageList();
2381 GetQuantizeInfo(&quantize_info);
2387 for (i=0; i < NumberTiles; i++)
2389 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2390 if (thumbnail == (Image *) NULL)
2392 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2394 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel,exception);
2395 if (i == (NumberTiles/2))
2397 (void) QueryColorCompliance("#dfdfdf",AllCompliance,
2398 &thumbnail->matte_color,exception);
2399 AppendImageToList(&images,thumbnail);
2407 preview_image=RotateImage(thumbnail,degrees,exception);
2408 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
2414 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
2415 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",
2416 degrees,2.0*degrees);
2421 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2422 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
2423 preview_image=RollImage(thumbnail,x,y,exception);
2424 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
2425 (double) x,(double) y);
2430 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2431 if (preview_image == (Image *) NULL)
2433 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",
2435 (void) ModulateImage(preview_image,factor,exception);
2436 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2439 case SaturationPreview:
2441 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2442 if (preview_image == (Image *) NULL)
2444 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",
2446 (void) ModulateImage(preview_image,factor,exception);
2447 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2450 case BrightnessPreview:
2452 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2453 if (preview_image == (Image *) NULL)
2455 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
2456 (void) ModulateImage(preview_image,factor,exception);
2457 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2463 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2464 if (preview_image == (Image *) NULL)
2467 (void) GammaImage(preview_image,gamma,exception);
2468 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
2473 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2474 if (preview_image != (Image *) NULL)
2475 for (x=0; x < i; x++)
2476 (void) ContrastImage(preview_image,MagickTrue,exception);
2477 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
2483 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2484 if (preview_image == (Image *) NULL)
2486 for (x=0; x < i; x++)
2487 (void) ContrastImage(preview_image,MagickFalse,exception);
2488 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
2492 case GrayscalePreview:
2494 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2495 if (preview_image == (Image *) NULL)
2498 quantize_info.number_colors=colors;
2499 quantize_info.colorspace=GRAYColorspace;
2500 (void) QuantizeImage(&quantize_info,preview_image,exception);
2501 (void) FormatLocaleString(label,MaxTextExtent,
2502 "-colorspace gray -colors %.20g",(double) colors);
2505 case QuantizePreview:
2507 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2508 if (preview_image == (Image *) NULL)
2511 quantize_info.number_colors=colors;
2512 (void) QuantizeImage(&quantize_info,preview_image,exception);
2513 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
2517 case DespecklePreview:
2519 for (x=0; x < (i-1); x++)
2521 preview_image=DespeckleImage(thumbnail,exception);
2522 if (preview_image == (Image *) NULL)
2524 thumbnail=DestroyImage(thumbnail);
2525 thumbnail=preview_image;
2527 preview_image=DespeckleImage(thumbnail,exception);
2528 if (preview_image == (Image *) NULL)
2530 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
2534 case ReduceNoisePreview:
2536 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2537 (size_t) radius,exception);
2538 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
2541 case AddNoisePreview:
2547 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2552 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2557 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2562 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2567 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2572 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2577 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2581 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2582 (size_t) i,exception);
2583 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
2586 case SharpenPreview:
2588 /* FUTURE: user bias on sharpen! This is non-sensical! */
2589 preview_image=SharpenImage(thumbnail,radius,sigma,image->bias,
2591 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",
2597 /* FUTURE: user bias on blur! This is non-sensical! */
2598 preview_image=BlurImage(thumbnail,radius,sigma,image->bias,exception);
2599 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
2603 case ThresholdPreview:
2605 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2606 if (preview_image == (Image *) NULL)
2608 (void) BilevelImage(thumbnail,(double) (percentage*((MagickRealType)
2609 QuantumRange+1.0))/100.0,exception);
2610 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",
2611 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
2614 case EdgeDetectPreview:
2616 preview_image=EdgeImage(thumbnail,radius,sigma,exception);
2617 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
2622 preview_image=SpreadImage(thumbnail,radius,thumbnail->interpolate,
2624 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",
2628 case SolarizePreview:
2630 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2631 if (preview_image == (Image *) NULL)
2633 (void) SolarizeImage(preview_image,(double) QuantumRange*
2634 percentage/100.0,exception);
2635 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
2636 (QuantumRange*percentage)/100.0);
2642 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2644 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",
2650 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2651 if (preview_image == (Image *) NULL)
2653 geometry.width=(size_t) (2*i+2);
2654 geometry.height=(size_t) (2*i+2);
2657 (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
2658 (void) FormatLocaleString(label,MaxTextExtent,
2659 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
2660 geometry.height,(double) geometry.x,(double) geometry.y);
2663 case SegmentPreview:
2665 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2666 if (preview_image == (Image *) NULL)
2669 (void) SegmentImage(preview_image,RGBColorspace,MagickFalse,threshold,
2670 threshold,exception);
2671 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
2672 threshold,threshold);
2677 preview_image=SwirlImage(thumbnail,degrees,image->interpolate,
2679 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
2683 case ImplodePreview:
2686 preview_image=ImplodeImage(thumbnail,degrees,image->interpolate,
2688 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
2694 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,
2695 image->interpolate,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 /* FUTURE: user bias on charcoal! This is non-sensical! */
2711 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
2712 image->bias,exception);
2713 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2720 filename[MaxTextExtent];
2728 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2729 if (preview_image == (Image *) NULL)
2731 preview_info->quality=(size_t) percentage;
2732 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
2733 preview_info->quality);
2734 file=AcquireUniqueFileResource(filename);
2737 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
2738 "jpeg:%s",filename);
2739 status=WriteImage(preview_info,preview_image,exception);
2740 if (status != MagickFalse)
2745 (void) CopyMagickString(preview_info->filename,
2746 preview_image->filename,MaxTextExtent);
2747 quality_image=ReadImage(preview_info,exception);
2748 if (quality_image != (Image *) NULL)
2750 preview_image=DestroyImage(preview_image);
2751 preview_image=quality_image;
2754 (void) RelinquishUniqueFileResource(preview_image->filename);
2755 if ((GetBlobSize(preview_image)/1024) >= 1024)
2756 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
2757 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2760 if (GetBlobSize(preview_image) >= 1024)
2761 (void) FormatLocaleString(label,MaxTextExtent,
2762 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
2763 GetBlobSize(preview_image))/1024.0);
2765 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
2766 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
2770 thumbnail=DestroyImage(thumbnail);
2774 if (preview_image == (Image *) NULL)
2776 (void) DeleteImageProperty(preview_image,"label");
2777 (void) SetImageProperty(preview_image,"label",label,exception);
2778 AppendImageToList(&images,preview_image);
2779 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2781 if (proceed == MagickFalse)
2784 if (images == (Image *) NULL)
2786 preview_info=DestroyImageInfo(preview_info);
2787 return((Image *) NULL);
2792 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2793 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2794 montage_info->shadow=MagickTrue;
2795 (void) CloneString(&montage_info->tile,"3x3");
2796 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2797 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2798 montage_image=MontageImages(images,montage_info,exception);
2799 montage_info=DestroyMontageInfo(montage_info);
2800 images=DestroyImageList(images);
2801 if (montage_image == (Image *) NULL)
2802 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2803 if (montage_image->montage != (char *) NULL)
2806 Free image directory.
2808 montage_image->montage=(char *) RelinquishMagickMemory(
2809 montage_image->montage);
2810 if (image->directory != (char *) NULL)
2811 montage_image->directory=(char *) RelinquishMagickMemory(
2812 montage_image->directory);
2814 preview_info=DestroyImageInfo(preview_info);
2815 return(montage_image);
2819 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2823 % R a d i a l B l u r I m a g e %
2827 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2829 % RadialBlurImage() applies a radial blur to the image.
2831 % Andrew Protano contributed this effect.
2833 % The format of the RadialBlurImage method is:
2835 % Image *RadialBlurImage(const Image *image,const double angle,
2836 % const double blur,ExceptionInfo *exception)
2838 % A description of each parameter follows:
2840 % o image: the image.
2842 % o angle: the angle of the radial blur.
2846 % o exception: return any errors or warnings in this structure.
2849 MagickExport Image *RadialBlurImage(const Image *image,const double angle,
2850 const double bias,ExceptionInfo *exception)
2886 Allocate blur image.
2888 assert(image != (Image *) NULL);
2889 assert(image->signature == MagickSignature);
2890 if (image->debug != MagickFalse)
2891 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2892 assert(exception != (ExceptionInfo *) NULL);
2893 assert(exception->signature == MagickSignature);
2894 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2895 if (blur_image == (Image *) NULL)
2896 return((Image *) NULL);
2897 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2899 blur_image=DestroyImage(blur_image);
2900 return((Image *) NULL);
2902 blur_center.x=(double) image->columns/2.0;
2903 blur_center.y=(double) image->rows/2.0;
2904 blur_radius=hypot(blur_center.x,blur_center.y);
2905 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
2906 theta=DegreesToRadians(angle)/(MagickRealType) (n-1);
2907 cos_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2908 sizeof(*cos_theta));
2909 sin_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2910 sizeof(*sin_theta));
2911 if ((cos_theta == (MagickRealType *) NULL) ||
2912 (sin_theta == (MagickRealType *) NULL))
2914 blur_image=DestroyImage(blur_image);
2915 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2917 offset=theta*(MagickRealType) (n-1)/2.0;
2918 for (i=0; i < (ssize_t) n; i++)
2920 cos_theta[i]=cos((double) (theta*i-offset));
2921 sin_theta[i]=sin((double) (theta*i-offset));
2928 image_view=AcquireCacheView(image);
2929 radial_view=AcquireCacheView(image);
2930 blur_view=AcquireCacheView(blur_image);
2931 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2932 #pragma omp parallel for schedule(static,4) shared(progress,status)
2934 for (y=0; y < (ssize_t) image->rows; y++)
2936 register const Quantum
2945 if (status == MagickFalse)
2947 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2948 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2950 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2955 for (x=0; x < (ssize_t) image->columns; x++)
2969 center.x=(double) x-blur_center.x;
2970 center.y=(double) y-blur_center.y;
2971 radius=hypot((double) center.x,center.y);
2976 step=(size_t) (blur_radius/radius);
2983 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2996 register const Quantum
3002 channel=GetPixelChannelMapChannel(image,i);
3003 traits=GetPixelChannelMapTraits(image,channel);
3004 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
3005 if ((traits == UndefinedPixelTrait) ||
3006 (blur_traits == UndefinedPixelTrait))
3008 if ((blur_traits & CopyPixelTrait) != 0)
3010 SetPixelChannel(blur_image,channel,p[i],q);
3015 if ((blur_traits & BlendPixelTrait) == 0)
3017 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
3019 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
3020 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
3021 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
3023 if (r == (const Quantum *) NULL)
3031 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3032 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3035 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
3037 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
3038 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
3039 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
3041 if (r == (const Quantum *) NULL)
3046 pixel+=GetPixelAlpha(image,r)*r[i];
3047 gamma+=GetPixelAlpha(image,r);
3049 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3050 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3052 p+=GetPixelChannels(image);
3053 q+=GetPixelChannels(blur_image);
3055 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
3057 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3062 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3063 #pragma omp critical (MagickCore_RadialBlurImage)
3065 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
3066 if (proceed == MagickFalse)
3070 blur_view=DestroyCacheView(blur_view);
3071 radial_view=DestroyCacheView(radial_view);
3072 image_view=DestroyCacheView(image_view);
3073 cos_theta=(MagickRealType *) RelinquishMagickMemory(cos_theta);
3074 sin_theta=(MagickRealType *) RelinquishMagickMemory(sin_theta);
3075 if (status == MagickFalse)
3076 blur_image=DestroyImage(blur_image);
3081 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3085 % S e l e c t i v e B l u r I m a g e %
3089 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3091 % SelectiveBlurImage() selectively blur pixels within a contrast threshold.
3092 % It is similar to the unsharpen mask that sharpens everything with contrast
3093 % above a certain threshold.
3095 % The format of the SelectiveBlurImage method is:
3097 % Image *SelectiveBlurImage(const Image *image,const double radius,
3098 % const double sigma,const double threshold,const double bias,
3099 % ExceptionInfo *exception)
3101 % A description of each parameter follows:
3103 % o image: the image.
3105 % o radius: the radius of the Gaussian, in pixels, not counting the center
3108 % o sigma: the standard deviation of the Gaussian, in pixels.
3110 % o threshold: only pixels within this contrast threshold are included
3111 % in the blur operation.
3115 % o exception: return any errors or warnings in this structure.
3118 MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
3119 const double sigma,const double threshold,const double bias,
3120 ExceptionInfo *exception)
3122 #define SelectiveBlurImageTag "SelectiveBlur/Image"
3154 Initialize blur image attributes.
3156 assert(image != (Image *) NULL);
3157 assert(image->signature == MagickSignature);
3158 if (image->debug != MagickFalse)
3159 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3160 assert(exception != (ExceptionInfo *) NULL);
3161 assert(exception->signature == MagickSignature);
3162 width=GetOptimalKernelWidth1D(radius,sigma);
3163 kernel=(double *) AcquireAlignedMemory((size_t) width,width*sizeof(*kernel));
3164 if (kernel == (double *) NULL)
3165 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3166 j=(ssize_t) width/2;
3168 for (v=(-j); v <= j; v++)
3170 for (u=(-j); u <= j; u++)
3171 kernel[i++]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
3172 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3174 if (image->debug != MagickFalse)
3177 format[MaxTextExtent],
3180 register const double
3187 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
3188 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
3190 message=AcquireString("");
3192 for (v=0; v < (ssize_t) width; v++)
3195 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
3196 (void) ConcatenateString(&message,format);
3197 for (u=0; u < (ssize_t) width; u++)
3199 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",*k++);
3200 (void) ConcatenateString(&message,format);
3202 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
3204 message=DestroyString(message);
3206 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3207 if (blur_image == (Image *) NULL)
3208 return((Image *) NULL);
3209 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
3211 blur_image=DestroyImage(blur_image);
3212 return((Image *) NULL);
3215 Threshold blur image.
3219 center=(ssize_t) (GetPixelChannels(image)*(image->columns+width)*(width/2L)+
3220 GetPixelChannels(image)*(width/2L));
3221 image_view=AcquireCacheView(image);
3222 blur_view=AcquireCacheView(blur_image);
3223 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3224 #pragma omp parallel for schedule(static,4) shared(progress,status)
3226 for (y=0; y < (ssize_t) image->rows; y++)
3234 register const Quantum
3243 if (status == MagickFalse)
3245 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
3246 (width/2L),image->columns+width,width,exception);
3247 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3249 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3254 for (x=0; x < (ssize_t) image->columns; x++)
3259 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3274 register const double
3277 register const Quantum
3286 channel=GetPixelChannelMapChannel(image,i);
3287 traits=GetPixelChannelMapTraits(image,channel);
3288 blur_traits=GetPixelChannelMapTraits(blur_image,channel);
3289 if ((traits == UndefinedPixelTrait) ||
3290 (blur_traits == UndefinedPixelTrait))
3292 if ((blur_traits & CopyPixelTrait) != 0)
3294 SetPixelChannel(blur_image,channel,p[center+i],q);
3300 intensity=(MagickRealType) GetPixelIntensity(image,p+center);
3302 if ((blur_traits & BlendPixelTrait) == 0)
3304 for (v=0; v < (ssize_t) width; v++)
3306 for (u=0; u < (ssize_t) width; u++)
3308 contrast=GetPixelIntensity(image,pixels)-intensity;
3309 if (fabs(contrast) < threshold)
3311 pixel+=(*k)*pixels[i];
3315 pixels+=GetPixelChannels(image);
3317 pixels+=image->columns*GetPixelChannels(image);
3319 if (fabs((double) gamma) < MagickEpsilon)
3321 SetPixelChannel(blur_image,channel,p[center+i],q);
3324 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3325 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3328 for (v=0; v < (ssize_t) width; v++)
3330 for (u=0; u < (ssize_t) width; u++)
3332 contrast=GetPixelIntensity(image,pixels)-intensity;
3333 if (fabs(contrast) < threshold)
3335 alpha=(MagickRealType) (QuantumScale*
3336 GetPixelAlpha(image,pixels));
3337 pixel+=(*k)*alpha*pixels[i];
3341 pixels+=GetPixelChannels(image);
3343 pixels+=image->columns*GetPixelChannels(image);
3345 if (fabs((double) gamma) < MagickEpsilon)
3347 SetPixelChannel(blur_image,channel,p[center+i],q);
3350 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3351 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3353 p+=GetPixelChannels(image);
3354 q+=GetPixelChannels(blur_image);
3356 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3357 if (sync == MagickFalse)
3359 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3364 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3365 #pragma omp critical (MagickCore_SelectiveBlurImage)
3367 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3369 if (proceed == MagickFalse)
3373 blur_image->type=image->type;
3374 blur_view=DestroyCacheView(blur_view);
3375 image_view=DestroyCacheView(image_view);
3376 kernel=(double *) RelinquishAlignedMemory(kernel);
3377 if (status == MagickFalse)
3378 blur_image=DestroyImage(blur_image);
3383 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3387 % S h a d e I m a g e %
3391 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3393 % ShadeImage() shines a distant light on an image to create a
3394 % three-dimensional effect. You control the positioning of the light with
3395 % azimuth and elevation; azimuth is measured in degrees off the x axis
3396 % and elevation is measured in pixels above the Z axis.
3398 % The format of the ShadeImage method is:
3400 % Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3401 % const double azimuth,const double elevation,ExceptionInfo *exception)
3403 % A description of each parameter follows:
3405 % o image: the image.
3407 % o gray: A value other than zero shades the intensity of each pixel.
3409 % o azimuth, elevation: Define the light source direction.
3411 % o exception: return any errors or warnings in this structure.
3414 MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3415 const double azimuth,const double elevation,ExceptionInfo *exception)
3417 #define ShadeImageTag "Shade/Image"
3439 Initialize shaded image attributes.
3441 assert(image != (const Image *) NULL);
3442 assert(image->signature == MagickSignature);
3443 if (image->debug != MagickFalse)
3444 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3445 assert(exception != (ExceptionInfo *) NULL);
3446 assert(exception->signature == MagickSignature);
3447 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3448 if (shade_image == (Image *) NULL)
3449 return((Image *) NULL);
3450 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
3452 shade_image=DestroyImage(shade_image);
3453 return((Image *) NULL);
3456 Compute the light vector.
3458 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3459 cos(DegreesToRadians(elevation));
3460 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3461 cos(DegreesToRadians(elevation));
3462 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3468 image_view=AcquireCacheView(image);
3469 shade_view=AcquireCacheView(shade_image);
3470 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3471 #pragma omp parallel for schedule(static,4) shared(progress,status)
3473 for (y=0; y < (ssize_t) image->rows; y++)
3483 register const Quantum
3495 if (status == MagickFalse)
3497 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
3498 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3500 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3506 Shade this row of pixels.
3508 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
3509 pre=p+GetPixelChannels(image);
3510 center=pre+(image->columns+2)*GetPixelChannels(image);
3511 post=center+(image->columns+2)*GetPixelChannels(image);
3512 for (x=0; x < (ssize_t) image->columns; x++)
3518 Determine the surface normal and compute shading.
3520 normal.x=(double) (GetPixelIntensity(image,pre-GetPixelChannels(image))+
3521 GetPixelIntensity(image,center-GetPixelChannels(image))+
3522 GetPixelIntensity(image,post-GetPixelChannels(image))-
3523 GetPixelIntensity(image,pre+GetPixelChannels(image))-
3524 GetPixelIntensity(image,center+GetPixelChannels(image))-
3525 GetPixelIntensity(image,post+GetPixelChannels(image)));
3526 normal.y=(double) (GetPixelIntensity(image,post-GetPixelChannels(image))+
3527 GetPixelIntensity(image,post)+GetPixelIntensity(image,post+
3528 GetPixelChannels(image))-GetPixelIntensity(image,pre-
3529 GetPixelChannels(image))-GetPixelIntensity(image,pre)-
3530 GetPixelIntensity(image,pre+GetPixelChannels(image)));
3531 if ((normal.x == 0.0) && (normal.y == 0.0))
3536 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3537 if (distance > MagickEpsilon)
3540 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
3541 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3542 shade=distance/sqrt((double) normal_distance);
3545 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3554 channel=GetPixelChannelMapChannel(image,i);
3555 traits=GetPixelChannelMapTraits(image,channel);
3556 shade_traits=GetPixelChannelMapTraits(shade_image,channel);
3557 if ((traits == UndefinedPixelTrait) ||
3558 (shade_traits == UndefinedPixelTrait))
3560 if ((shade_traits & CopyPixelTrait) != 0)
3562 SetPixelChannel(shade_image,channel,center[i],q);
3565 if (gray != MagickFalse)
3567 SetPixelChannel(shade_image,channel,ClampToQuantum(shade),q);
3570 SetPixelChannel(shade_image,channel,ClampToQuantum(QuantumScale*shade*
3573 pre+=GetPixelChannels(image);
3574 center+=GetPixelChannels(image);
3575 post+=GetPixelChannels(image);
3576 q+=GetPixelChannels(shade_image);
3578 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3580 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3585 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3586 #pragma omp critical (MagickCore_ShadeImage)
3588 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3589 if (proceed == MagickFalse)
3593 shade_view=DestroyCacheView(shade_view);
3594 image_view=DestroyCacheView(image_view);
3595 if (status == MagickFalse)
3596 shade_image=DestroyImage(shade_image);
3597 return(shade_image);
3601 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3605 % S h a r p e n I m a g e %
3609 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3611 % SharpenImage() sharpens the image. We convolve the image with a Gaussian
3612 % operator of the given radius and standard deviation (sigma). For
3613 % reasonable results, radius should be larger than sigma. Use a radius of 0
3614 % and SharpenImage() selects a suitable radius for you.
3616 % Using a separable kernel would be faster, but the negative weights cancel
3617 % out on the corners of the kernel producing often undesirable ringing in the
3618 % filtered result; this can be avoided by using a 2D gaussian shaped image
3619 % sharpening kernel instead.
3621 % The format of the SharpenImage method is:
3623 % Image *SharpenImage(const Image *image,const double radius,
3624 % const double sigma,const double bias,ExceptionInfo *exception)
3626 % A description of each parameter follows:
3628 % o image: the image.
3630 % o radius: the radius of the Gaussian, in pixels, not counting the center
3633 % o sigma: the standard deviation of the Laplacian, in pixels.
3637 % o exception: return any errors or warnings in this structure.
3640 MagickExport Image *SharpenImage(const Image *image,const double radius,
3641 const double sigma,const double bias,ExceptionInfo *exception)
3663 assert(image != (const Image *) NULL);
3664 assert(image->signature == MagickSignature);
3665 if (image->debug != MagickFalse)
3666 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3667 assert(exception != (ExceptionInfo *) NULL);
3668 assert(exception->signature == MagickSignature);
3669 width=GetOptimalKernelWidth2D(radius,sigma);
3670 kernel_info=AcquireKernelInfo((const char *) NULL);
3671 if (kernel_info == (KernelInfo *) NULL)
3672 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3673 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3674 kernel_info->width=width;
3675 kernel_info->height=width;
3676 kernel_info->bias=bias; /* FUTURE: user bias - non-sensical! */
3677 kernel_info->signature=MagickSignature;
3678 kernel_info->values=(MagickRealType *) AcquireAlignedMemory(
3679 kernel_info->width,kernel_info->width*sizeof(*kernel_info->values));
3680 if (kernel_info->values == (MagickRealType *) NULL)
3682 kernel_info=DestroyKernelInfo(kernel_info);
3683 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3686 j=(ssize_t) kernel_info->width/2;
3688 for (v=(-j); v <= j; v++)
3690 for (u=(-j); u <= j; u++)
3692 kernel_info->values[i]=(double) (-exp(-((double) u*u+v*v)/(2.0*
3693 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3694 normalize+=kernel_info->values[i];
3698 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
3699 sharp_image=ConvolveImage(image,kernel_info,exception);
3700 kernel_info=DestroyKernelInfo(kernel_info);
3701 return(sharp_image);
3705 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3709 % S p r e a d I m a g e %
3713 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3715 % SpreadImage() is a special effects method that randomly displaces each
3716 % pixel in a block defined by the radius parameter.
3718 % The format of the SpreadImage method is:
3720 % Image *SpreadImage(const Image *image,const double radius,
3721 % const PixelInterpolateMethod method,ExceptionInfo *exception)
3723 % A description of each parameter follows:
3725 % o image: the image.
3727 % o radius: choose a random pixel in a neighborhood of this extent.
3729 % o method: the pixel interpolation method.
3731 % o exception: return any errors or warnings in this structure.
3734 MagickExport Image *SpreadImage(const Image *image,const double radius,
3735 const PixelInterpolateMethod method,ExceptionInfo *exception)
3737 #define SpreadImageTag "Spread/Image"
3753 **restrict random_info;
3762 Initialize spread image attributes.
3764 assert(image != (Image *) NULL);
3765 assert(image->signature == MagickSignature);
3766 if (image->debug != MagickFalse)
3767 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3768 assert(exception != (ExceptionInfo *) NULL);
3769 assert(exception->signature == MagickSignature);
3770 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3772 if (spread_image == (Image *) NULL)
3773 return((Image *) NULL);
3774 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
3776 spread_image=DestroyImage(spread_image);
3777 return((Image *) NULL);
3784 width=GetOptimalKernelWidth1D(radius,0.5);
3785 random_info=AcquireRandomInfoThreadSet();
3786 image_view=AcquireCacheView(image);
3787 spread_view=AcquireCacheView(spread_image);
3788 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3789 #pragma omp parallel for schedule(static,8) shared(progress,status)
3791 for (y=0; y < (ssize_t) image->rows; y++)
3794 id = GetOpenMPThreadId();
3796 register const Quantum
3805 if (status == MagickFalse)
3807 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3808 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
3810 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3815 for (x=0; x < (ssize_t) image->columns; x++)
3820 point.x=GetPseudoRandomValue(random_info[id]);
3821 point.y=GetPseudoRandomValue(random_info[id]);
3822 status=InterpolatePixelChannels(image,image_view,spread_image,method,
3823 (double) x+width*point.x-0.5,(double) y+width*point.y-0.5,q,exception);
3824 q+=GetPixelChannels(spread_image);
3826 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
3828 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3833 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3834 #pragma omp critical (MagickCore_SpreadImage)
3836 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3837 if (proceed == MagickFalse)
3841 spread_view=DestroyCacheView(spread_view);
3842 image_view=DestroyCacheView(image_view);
3843 random_info=DestroyRandomInfoThreadSet(random_info);
3844 return(spread_image);
3848 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3852 % U n s h a r p M a s k I m a g e %
3856 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3858 % UnsharpMaskImage() sharpens one or more image channels. We convolve the
3859 % image with a Gaussian operator of the given radius and standard deviation
3860 % (sigma). For reasonable results, radius should be larger than sigma. Use a
3861 % radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
3863 % The format of the UnsharpMaskImage method is:
3865 % Image *UnsharpMaskImage(const Image *image,const double radius,
3866 % const double sigma,const double amount,const double threshold,
3867 % ExceptionInfo *exception)
3869 % A description of each parameter follows:
3871 % o image: the image.
3873 % o radius: the radius of the Gaussian, in pixels, not counting the center
3876 % o sigma: the standard deviation of the Gaussian, in pixels.
3878 % o amount: the percentage of the difference between the original and the
3879 % blur image that is added back into the original.
3881 % o threshold: the threshold in pixels needed to apply the diffence amount.
3883 % o exception: return any errors or warnings in this structure.
3886 MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
3887 const double sigma,const double amount,const double threshold,
3888 ExceptionInfo *exception)
3890 #define SharpenImageTag "Sharpen/Image"
3911 assert(image != (const Image *) NULL);
3912 assert(image->signature == MagickSignature);
3913 if (image->debug != MagickFalse)
3914 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3915 assert(exception != (ExceptionInfo *) NULL);
3916 unsharp_image=BlurImage(image,radius,sigma,image->bias,exception);
3917 if (unsharp_image == (Image *) NULL)
3918 return((Image *) NULL);
3919 quantum_threshold=(MagickRealType) QuantumRange*threshold;
3925 image_view=AcquireCacheView(image);
3926 unsharp_view=AcquireCacheView(unsharp_image);
3927 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3928 #pragma omp parallel for schedule(static,4) shared(progress,status)
3930 for (y=0; y < (ssize_t) image->rows; y++)
3932 register const Quantum
3941 if (status == MagickFalse)
3943 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3944 q=QueueCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
3946 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3951 for (x=0; x < (ssize_t) image->columns; x++)
3956 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3968 channel=GetPixelChannelMapChannel(image,i);
3969 traits=GetPixelChannelMapTraits(image,channel);
3970 unsharp_traits=GetPixelChannelMapTraits(unsharp_image,channel);
3971 if ((traits == UndefinedPixelTrait) ||
3972 (unsharp_traits == UndefinedPixelTrait))
3974 if ((unsharp_traits & CopyPixelTrait) != 0)
3976 SetPixelChannel(unsharp_image,channel,p[i],q);
3979 pixel=p[i]-(MagickRealType) GetPixelChannel(unsharp_image,channel,q);
3980 if (fabs(2.0*pixel) < quantum_threshold)
3981 pixel=(MagickRealType) p[i];
3983 pixel=(MagickRealType) p[i]+amount*pixel;
3984 SetPixelChannel(unsharp_image,channel,ClampToQuantum(pixel),q);
3986 p+=GetPixelChannels(image);
3987 q+=GetPixelChannels(unsharp_image);
3989 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
3991 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3996 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3997 #pragma omp critical (MagickCore_UnsharpMaskImage)
3999 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
4000 if (proceed == MagickFalse)
4004 unsharp_image->type=image->type;
4005 unsharp_view=DestroyCacheView(unsharp_view);
4006 image_view=DestroyCacheView(image_view);
4007 if (status == MagickFalse)
4008 unsharp_image=DestroyImage(unsharp_image);
4009 return(unsharp_image);