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/geometry.h"
60 #include "MagickCore/image-private.h"
61 #include "MagickCore/list.h"
62 #include "MagickCore/log.h"
63 #include "MagickCore/memory_.h"
64 #include "MagickCore/monitor.h"
65 #include "MagickCore/monitor-private.h"
66 #include "MagickCore/montage.h"
67 #include "MagickCore/morphology.h"
68 #include "MagickCore/paint.h"
69 #include "MagickCore/pixel-accessor.h"
70 #include "MagickCore/property.h"
71 #include "MagickCore/quantize.h"
72 #include "MagickCore/quantum.h"
73 #include "MagickCore/quantum-private.h"
74 #include "MagickCore/random_.h"
75 #include "MagickCore/random-private.h"
76 #include "MagickCore/resample.h"
77 #include "MagickCore/resample-private.h"
78 #include "MagickCore/resize.h"
79 #include "MagickCore/resource_.h"
80 #include "MagickCore/segment.h"
81 #include "MagickCore/shear.h"
82 #include "MagickCore/signature-private.h"
83 #include "MagickCore/string_.h"
84 #include "MagickCore/thread-private.h"
85 #include "MagickCore/transform.h"
86 #include "MagickCore/threshold.h"
89 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93 % A d a p t i v e B l u r I m a g e %
97 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99 % AdaptiveBlurImage() adaptively blurs the image by blurring less
100 % intensely near image edges and more intensely far from edges. We blur the
101 % image with a Gaussian operator of the given radius and standard deviation
102 % (sigma). For reasonable results, radius should be larger than sigma. Use a
103 % radius of 0 and AdaptiveBlurImage() selects a suitable radius for you.
105 % The format of the AdaptiveBlurImage method is:
107 % Image *AdaptiveBlurImage(const Image *image,const double radius,
108 % const double sigma,ExceptionInfo *exception)
110 % A description of each parameter follows:
112 % o image: the image.
114 % o radius: the radius of the Gaussian, in pixels, not counting the center
117 % o sigma: the standard deviation of the Laplacian, in pixels.
119 % o exception: return any errors or warnings in this structure.
123 MagickExport MagickBooleanType AdaptiveLevelImage(Image *image,
143 if (levels == (char *) NULL)
145 flags=ParseGeometry(levels,&geometry_info);
146 black_point=geometry_info.rho;
147 white_point=(double) QuantumRange;
148 if ((flags & SigmaValue) != 0)
149 white_point=geometry_info.sigma;
151 if ((flags & XiValue) != 0)
152 gamma=geometry_info.xi;
153 if ((flags & PercentValue) != 0)
155 black_point*=(double) image->columns*image->rows/100.0;
156 white_point*=(double) image->columns*image->rows/100.0;
158 if ((flags & SigmaValue) == 0)
159 white_point=(double) QuantumRange-black_point;
160 if ((flags & AspectValue ) == 0)
161 status=LevelImage(image,black_point,white_point,gamma);
163 status=LevelizeImage(image,black_point,white_point,gamma);
167 MagickExport Image *AdaptiveBlurImage(const Image *image,
168 const double radius,const double sigma,ExceptionInfo *exception)
170 #define AdaptiveBlurImageTag "Convolve/Image"
171 #define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
209 assert(image != (const Image *) NULL);
210 assert(image->signature == MagickSignature);
211 if (image->debug != MagickFalse)
212 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
213 assert(exception != (ExceptionInfo *) NULL);
214 assert(exception->signature == MagickSignature);
215 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
216 if (blur_image == (Image *) NULL)
217 return((Image *) NULL);
218 if (fabs(sigma) <= MagickEpsilon)
220 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
222 InheritException(exception,&blur_image->exception);
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,exception);
230 if (edge_image == (Image *) NULL)
232 blur_image=DestroyImage(blur_image);
233 return((Image *) NULL);
235 (void) AdaptiveLevelImage(edge_image,"20%,95%");
236 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
237 if (gaussian_image != (Image *) NULL)
239 edge_image=DestroyImage(edge_image);
240 edge_image=gaussian_image;
242 (void) AdaptiveLevelImage(edge_image,"10%,95%");
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 GetPixelInfo(image,&bias);
295 SetPixelInfoBias(image,&bias);
296 image_view=AcquireCacheView(image);
297 edge_view=AcquireCacheView(edge_image);
298 blur_view=AcquireCacheView(blur_image);
299 #if defined(MAGICKCORE_OPENMP_SUPPORT)
300 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
302 for (y=0; y < (ssize_t) blur_image->rows; y++)
304 register const Quantum
314 if (status == MagickFalse)
316 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
317 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
319 if ((r == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
324 for (x=0; x < (ssize_t) blur_image->columns; x++)
333 register const double
342 i=(ssize_t) ceil((double) width*QuantumScale*
343 GetPixelIntensity(edge_image,r)-0.5);
347 if (i > (ssize_t) width)
351 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-i)/2L),y-
352 (ssize_t) ((width-i)/2L),width-i,width-i,exception);
353 if (p == (const Quantum *) NULL)
357 for (v=0; v < (ssize_t) (width-i); v++)
359 for (u=0; u < (ssize_t) (width-i); u++)
362 if (((GetPixelAlphaTraits(image) & ActivePixelTrait) != 0) &&
363 (image->matte != MagickFalse))
364 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,p));
365 if ((GetPixelRedTraits(image) & ActivePixelTrait) != 0)
366 pixel.red+=(*k)*alpha*GetPixelRed(image,p);
367 if ((GetPixelGreenTraits(image) & ActivePixelTrait) != 0)
368 pixel.green+=(*k)*alpha*GetPixelGreen(image,p);
369 if ((GetPixelBlueTraits(image) & ActivePixelTrait) != 0)
370 pixel.blue+=(*k)*alpha*GetPixelBlue(image,p);
371 if (((GetPixelBlackTraits(image) & ActivePixelTrait) != 0) &&
372 (image->colorspace == CMYKColorspace))
373 pixel.black+=(*k)*alpha*GetPixelBlack(image,p);
374 if ((GetPixelAlphaTraits(image) & ActivePixelTrait) != 0)
375 pixel.alpha+=(*k)*GetPixelAlpha(image,p);
378 p+=GetPixelComponents(image);
381 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
382 if ((GetPixelRedTraits(image) & ActivePixelTrait) != 0)
383 SetPixelRed(blur_image,ClampToQuantum(gamma*pixel.red),q);
384 if ((GetPixelGreenTraits(image) & ActivePixelTrait) != 0)
385 SetPixelGreen(blur_image,ClampToQuantum(gamma*pixel.green),q);
386 if ((GetPixelBlueTraits(image) & ActivePixelTrait) != 0)
387 SetPixelBlue(blur_image,ClampToQuantum(gamma*pixel.blue),q);
388 if (((GetPixelBlackTraits(image) & ActivePixelTrait) != 0) &&
389 (image->colorspace == CMYKColorspace))
390 SetPixelBlack(blur_image,ClampToQuantum(gamma*pixel.black),q);
391 if ((GetPixelAlphaTraits(image) & ActivePixelTrait) != 0)
392 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
393 q+=GetPixelComponents(blur_image);
394 r+=GetPixelComponents(edge_image);
396 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
398 if (image->progress_monitor != (MagickProgressMonitor) NULL)
403 #if defined(MAGICKCORE_OPENMP_SUPPORT)
404 #pragma omp critical (MagickCore_AdaptiveBlurImage)
406 proceed=SetImageProgress(image,AdaptiveBlurImageTag,progress++,
408 if (proceed == MagickFalse)
412 blur_image->type=image->type;
413 blur_view=DestroyCacheView(blur_view);
414 edge_view=DestroyCacheView(edge_view);
415 image_view=DestroyCacheView(image_view);
416 edge_image=DestroyImage(edge_image);
417 for (i=0; i < (ssize_t) width; i+=2)
418 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
419 kernel=(double **) RelinquishMagickMemory(kernel);
420 if (status == MagickFalse)
421 blur_image=DestroyImage(blur_image);
426 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
430 % A d a p t i v e S h a r p e n I m a g e %
434 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
436 % AdaptiveSharpenImage() adaptively sharpens the image by sharpening more
437 % intensely near image edges and less intensely far from edges. We sharpen the
438 % image with a Gaussian operator of the given radius and standard deviation
439 % (sigma). For reasonable results, radius should be larger than sigma. Use a
440 % radius of 0 and AdaptiveSharpenImage() selects a suitable radius for you.
442 % The format of the AdaptiveSharpenImage method is:
444 % Image *AdaptiveSharpenImage(const Image *image,const double radius,
445 % const double sigma,ExceptionInfo *exception)
447 % A description of each parameter follows:
449 % o image: the image.
451 % o radius: the radius of the Gaussian, in pixels, not counting the center
454 % o sigma: the standard deviation of the Laplacian, in pixels.
456 % o exception: return any errors or warnings in this structure.
459 MagickExport Image *AdaptiveSharpenImage(const Image *image,const double radius,
460 const double sigma,ExceptionInfo *exception)
462 #define AdaptiveSharpenImageTag "Convolve/Image"
463 #define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
501 assert(image != (const Image *) NULL);
502 assert(image->signature == MagickSignature);
503 if (image->debug != MagickFalse)
504 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
505 assert(exception != (ExceptionInfo *) NULL);
506 assert(exception->signature == MagickSignature);
507 sharp_image=CloneImage(image,0,0,MagickTrue,exception);
508 if (sharp_image == (Image *) NULL)
509 return((Image *) NULL);
510 if (fabs(sigma) <= MagickEpsilon)
512 if (SetImageStorageClass(sharp_image,DirectClass) == MagickFalse)
514 InheritException(exception,&sharp_image->exception);
515 sharp_image=DestroyImage(sharp_image);
516 return((Image *) NULL);
519 Edge detect the image brighness channel, level, sharp, and level again.
521 edge_image=EdgeImage(image,radius,exception);
522 if (edge_image == (Image *) NULL)
524 sharp_image=DestroyImage(sharp_image);
525 return((Image *) NULL);
527 (void) AdaptiveLevelImage(edge_image,"20%,95%");
528 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
529 if (gaussian_image != (Image *) NULL)
531 edge_image=DestroyImage(edge_image);
532 edge_image=gaussian_image;
534 (void) AdaptiveLevelImage(edge_image,"10%,95%");
536 Create a set of kernels from maximum (radius,sigma) to minimum.
538 width=GetOptimalKernelWidth2D(radius,sigma);
539 kernel=(double **) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
540 if (kernel == (double **) NULL)
542 edge_image=DestroyImage(edge_image);
543 sharp_image=DestroyImage(sharp_image);
544 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
546 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
547 for (i=0; i < (ssize_t) width; i+=2)
549 kernel[i]=(double *) AcquireQuantumMemory((size_t) (width-i),(width-i)*
551 if (kernel[i] == (double *) NULL)
554 j=(ssize_t) (width-i)/2;
556 for (v=(-j); v <= j; v++)
558 for (u=(-j); u <= j; u++)
560 kernel[i][k]=(double) (-exp(-((double) u*u+v*v)/(2.0*MagickSigma*
561 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
562 normalize+=kernel[i][k];
566 if (fabs(normalize) <= MagickEpsilon)
568 normalize=1.0/normalize;
569 for (k=0; k < (j*j); k++)
570 kernel[i][k]=normalize*kernel[i][k];
572 if (i < (ssize_t) width)
574 for (i-=2; i >= 0; i-=2)
575 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
576 kernel=(double **) RelinquishMagickMemory(kernel);
577 edge_image=DestroyImage(edge_image);
578 sharp_image=DestroyImage(sharp_image);
579 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
582 Adaptively sharpen image.
586 GetPixelInfo(image,&bias);
587 SetPixelInfoBias(image,&bias);
588 image_view=AcquireCacheView(image);
589 edge_view=AcquireCacheView(edge_image);
590 sharp_view=AcquireCacheView(sharp_image);
591 #if defined(MAGICKCORE_OPENMP_SUPPORT)
592 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
594 for (y=0; y < (ssize_t) sharp_image->rows; y++)
596 register const Quantum
606 if (status == MagickFalse)
608 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
609 q=QueueCacheViewAuthenticPixels(sharp_view,0,y,sharp_image->columns,1,
611 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
616 for (x=0; x < (ssize_t) sharp_image->columns; x++)
625 register const double
634 i=(ssize_t) ceil((double) width*QuantumScale*
635 GetPixelIntensity(edge_image,r)-0.5);
639 if (i > (ssize_t) width)
643 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-i)/2L),y-
644 (ssize_t) ((width-i)/2L),width-i,width-i,exception);
645 if (p == (const Quantum *) NULL)
649 for (v=0; v < (ssize_t) (width-i); v++)
651 for (u=0; u < (ssize_t) (width-i); u++)
654 if (((GetPixelAlphaTraits(image) & ActivePixelTrait) != 0) &&
655 (image->matte != MagickFalse))
656 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,p));
657 if ((GetPixelRedTraits(image) & ActivePixelTrait) != 0)
658 pixel.red+=(*k)*alpha*GetPixelRed(image,p);
659 if ((GetPixelGreenTraits(image) & ActivePixelTrait) != 0)
660 pixel.green+=(*k)*alpha*GetPixelGreen(image,p);
661 if ((GetPixelBlueTraits(image) & ActivePixelTrait) != 0)
662 pixel.blue+=(*k)*alpha*GetPixelBlue(image,p);
663 if (((GetPixelBlackTraits(image) & ActivePixelTrait) != 0) &&
664 (image->colorspace == CMYKColorspace))
665 pixel.black+=(*k)*alpha*GetPixelBlack(image,p);
666 if ((GetPixelAlphaTraits(image) & ActivePixelTrait) != 0)
667 pixel.alpha+=(*k)*GetPixelAlpha(image,p);
670 p+=GetPixelComponents(image);
673 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
674 if ((GetPixelRedTraits(image) & ActivePixelTrait) != 0)
675 SetPixelRed(sharp_image,ClampToQuantum(gamma*pixel.red),q);
676 if ((GetPixelGreenTraits(image) & ActivePixelTrait) != 0)
677 SetPixelGreen(sharp_image,ClampToQuantum(gamma*pixel.green),q);
678 if ((GetPixelBlueTraits(image) & ActivePixelTrait) != 0)
679 SetPixelBlue(sharp_image,ClampToQuantum(gamma*pixel.blue),q);
680 if (((GetPixelBlackTraits(image) & ActivePixelTrait) != 0) &&
681 (image->colorspace == CMYKColorspace))
682 SetPixelBlack(sharp_image,ClampToQuantum(gamma*pixel.black),q);
683 if ((GetPixelAlphaTraits(image) & ActivePixelTrait) != 0)
684 SetPixelAlpha(sharp_image,ClampToQuantum(pixel.alpha),q);
685 q+=GetPixelComponents(sharp_image);
686 r+=GetPixelComponents(edge_image);
688 if (SyncCacheViewAuthenticPixels(sharp_view,exception) == MagickFalse)
690 if (image->progress_monitor != (MagickProgressMonitor) NULL)
695 #if defined(MAGICKCORE_OPENMP_SUPPORT)
696 #pragma omp critical (MagickCore_AdaptiveSharpenImage)
698 proceed=SetImageProgress(image,AdaptiveSharpenImageTag,progress++,
700 if (proceed == MagickFalse)
704 sharp_image->type=image->type;
705 sharp_view=DestroyCacheView(sharp_view);
706 edge_view=DestroyCacheView(edge_view);
707 image_view=DestroyCacheView(image_view);
708 edge_image=DestroyImage(edge_image);
709 for (i=0; i < (ssize_t) width; i+=2)
710 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
711 kernel=(double **) RelinquishMagickMemory(kernel);
712 if (status == MagickFalse)
713 sharp_image=DestroyImage(sharp_image);
718 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
722 % B l u r I m a g e %
726 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
728 % BlurImage() blurs an image. We convolve the image with a Gaussian operator
729 % of the given radius and standard deviation (sigma). For reasonable results,
730 % the radius should be larger than sigma. Use a radius of 0 and BlurImage()
731 % selects a suitable radius for you.
733 % BlurImage() differs from GaussianBlurImage() in that it uses a separable
734 % kernel which is faster but mathematically equivalent to the non-separable
737 % The format of the BlurImage method is:
739 % Image *BlurImage(const Image *image,const double radius,
740 % const double sigma,ExceptionInfo *exception)
742 % A description of each parameter follows:
744 % o image: the image.
746 % o radius: the radius of the Gaussian, in pixels, not counting the center
749 % o sigma: the standard deviation of the Gaussian, in pixels.
751 % o exception: return any errors or warnings in this structure.
755 static double *GetBlurKernel(const size_t width,const double sigma)
769 Generate a 1-D convolution kernel.
771 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
772 kernel=(double *) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
773 if (kernel == (double *) NULL)
778 for (k=(-j); k <= j; k++)
780 kernel[i]=(double) (exp(-((double) k*k)/(2.0*MagickSigma*MagickSigma))/
781 (MagickSQ2PI*MagickSigma));
782 normalize+=kernel[i];
785 for (i=0; i < (ssize_t) width; i++)
786 kernel[i]/=normalize;
790 MagickExport Image *BlurImage(const Image *image,const double radius,
791 const double sigma,ExceptionInfo *exception)
793 #define BlurImageTag "Blur/Image"
825 Initialize blur image attributes.
827 assert(image != (Image *) NULL);
828 assert(image->signature == MagickSignature);
829 if (image->debug != MagickFalse)
830 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
831 assert(exception != (ExceptionInfo *) NULL);
832 assert(exception->signature == MagickSignature);
833 blur_image=CloneImage(image,0,0,MagickTrue,exception);
834 if (blur_image == (Image *) NULL)
835 return((Image *) NULL);
836 if (fabs(sigma) <= MagickEpsilon)
838 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
840 InheritException(exception,&blur_image->exception);
841 blur_image=DestroyImage(blur_image);
842 return((Image *) NULL);
844 width=GetOptimalKernelWidth1D(radius,sigma);
845 kernel=GetBlurKernel(width,sigma);
846 if (kernel == (double *) NULL)
848 blur_image=DestroyImage(blur_image);
849 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
851 if (image->debug != MagickFalse)
854 format[MaxTextExtent],
857 register const double
860 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
861 " BlurImage with %.20g kernel:",(double) width);
862 message=AcquireString("");
864 for (i=0; i < (ssize_t) width; i++)
867 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) i);
868 (void) ConcatenateString(&message,format);
869 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
870 (void) ConcatenateString(&message,format);
871 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
873 message=DestroyString(message);
880 GetPixelInfo(image,&bias);
881 SetPixelInfoBias(image,&bias);
882 image_view=AcquireCacheView(image);
883 blur_view=AcquireCacheView(blur_image);
884 #if defined(MAGICKCORE_OPENMP_SUPPORT)
885 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
887 for (y=0; y < (ssize_t) blur_image->rows; y++)
889 register const Quantum
898 if (status == MagickFalse)
900 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y,
901 image->columns+width,1,exception);
902 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
904 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
909 for (x=0; x < (ssize_t) blur_image->columns; x++)
914 register const double
917 register const Quantum
918 *restrict kernel_pixels;
926 if (((GetPixelAlphaTraits(image) & ActivePixelTrait) == 0) ||
927 (image->matte == MagickFalse))
929 for (i=0; i < (ssize_t) width; i++)
931 pixel.red+=(*k)*GetPixelRed(image,kernel_pixels);
932 pixel.green+=(*k)*GetPixelGreen(image,kernel_pixels);
933 pixel.blue+=(*k)*GetPixelBlue(image,kernel_pixels);
934 if (image->colorspace == CMYKColorspace)
935 pixel.black+=(*k)*GetPixelBlack(image,kernel_pixels);
937 kernel_pixels+=GetPixelComponents(image);
939 if ((GetPixelRedTraits(image) & ActivePixelTrait) != 0)
940 SetPixelRed(blur_image,ClampToQuantum(pixel.red),q);
941 if ((GetPixelGreenTraits(image) & ActivePixelTrait) != 0)
942 SetPixelGreen(blur_image,ClampToQuantum(pixel.green),q);
943 if ((GetPixelBlueTraits(image) & ActivePixelTrait) != 0)
944 SetPixelBlue(blur_image,ClampToQuantum(pixel.blue),q);
945 if (((GetPixelBlackTraits(image) & ActivePixelTrait) != 0) &&
946 (blur_image->colorspace == CMYKColorspace))
947 SetPixelBlack(blur_image,ClampToQuantum(pixel.black),q);
948 if ((GetPixelAlphaTraits(image) & ActivePixelTrait) != 0)
952 for (i=0; i < (ssize_t) width; i++)
954 pixel.alpha+=(*k)*GetPixelAlpha(image,kernel_pixels);
956 kernel_pixels+=GetPixelComponents(image);
958 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
968 for (i=0; i < (ssize_t) width; i++)
970 alpha=(MagickRealType) (QuantumScale*
971 GetPixelAlpha(image,kernel_pixels));
972 pixel.red+=(*k)*alpha*GetPixelRed(image,kernel_pixels);
973 pixel.green+=(*k)*alpha*GetPixelGreen(image,kernel_pixels);
974 pixel.blue+=(*k)*alpha*GetPixelBlue(image,kernel_pixels);
975 if (image->colorspace == CMYKColorspace)
976 pixel.black+=(*k)*alpha*GetPixelBlack(image,kernel_pixels);
979 kernel_pixels+=GetPixelComponents(image);
981 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
982 if ((GetPixelRedTraits(image) & ActivePixelTrait) != 0)
983 SetPixelRed(blur_image,ClampToQuantum(gamma*pixel.red),q);
984 if ((GetPixelGreenTraits(image) & ActivePixelTrait) != 0)
985 SetPixelGreen(blur_image,ClampToQuantum(gamma*pixel.green),q);
986 if ((GetPixelBlueTraits(image) & ActivePixelTrait) != 0)
987 SetPixelBlue(blur_image,ClampToQuantum(gamma*pixel.blue),q);
988 if (((GetPixelBlackTraits(image) & ActivePixelTrait) != 0) &&
989 (blur_image->colorspace == CMYKColorspace))
990 SetPixelBlack(blur_image,ClampToQuantum(gamma*pixel.black),q);
991 if ((GetPixelAlphaTraits(image) & ActivePixelTrait) != 0)
995 for (i=0; i < (ssize_t) width; i++)
997 pixel.alpha+=(*k)*GetPixelAlpha(image,kernel_pixels);
999 kernel_pixels+=GetPixelComponents(image);
1001 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
1004 p+=GetPixelComponents(image);
1005 q+=GetPixelComponents(blur_image);
1007 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1009 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1014 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1015 #pragma omp critical (MagickCore_BlurImage)
1017 proceed=SetImageProgress(image,BlurImageTag,progress++,blur_image->rows+
1018 blur_image->columns);
1019 if (proceed == MagickFalse)
1023 blur_view=DestroyCacheView(blur_view);
1024 image_view=DestroyCacheView(image_view);
1028 image_view=AcquireCacheView(blur_image);
1029 blur_view=AcquireCacheView(blur_image);
1030 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1031 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1033 for (x=0; x < (ssize_t) blur_image->columns; x++)
1035 register const Quantum
1044 if (status == MagickFalse)
1046 p=GetCacheViewVirtualPixels(image_view,x,-((ssize_t) width/2L),1,
1047 image->rows+width,exception);
1048 q=GetCacheViewAuthenticPixels(blur_view,x,0,1,blur_image->rows,exception);
1049 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1054 for (y=0; y < (ssize_t) blur_image->rows; y++)
1059 register const double
1062 register const Quantum
1063 *restrict kernel_pixels;
1071 if (((GetPixelAlphaTraits(image) & ActivePixelTrait) == 0) ||
1072 (blur_image->matte == MagickFalse))
1074 for (i=0; i < (ssize_t) width; i++)
1076 pixel.red+=(*k)*GetPixelRed(blur_image,kernel_pixels);
1077 pixel.green+=(*k)*GetPixelGreen(blur_image,kernel_pixels);
1078 pixel.blue+=(*k)*GetPixelBlue(blur_image,kernel_pixels);
1079 if (blur_image->colorspace == CMYKColorspace)
1080 pixel.black+=(*k)*GetPixelBlack(blur_image,kernel_pixels);
1082 kernel_pixels+=GetPixelComponents(blur_image);
1084 if ((GetPixelRedTraits(image) & ActivePixelTrait) != 0)
1085 SetPixelRed(blur_image,ClampToQuantum(pixel.red),q);
1086 if ((GetPixelGreenTraits(image) & ActivePixelTrait) != 0)
1087 SetPixelGreen(blur_image,ClampToQuantum(pixel.green),q);
1088 if ((GetPixelBlueTraits(image) & ActivePixelTrait) != 0)
1089 SetPixelBlue(blur_image,ClampToQuantum(pixel.blue),q);
1090 if (((GetPixelBlackTraits(image) & ActivePixelTrait) != 0) &&
1091 (blur_image->colorspace == CMYKColorspace))
1092 SetPixelBlack(blur_image,ClampToQuantum(pixel.black),q);
1093 if ((GetPixelAlphaTraits(image) & ActivePixelTrait) != 0)
1097 for (i=0; i < (ssize_t) width; i++)
1099 pixel.alpha+=(*k)*GetPixelAlpha(blur_image,kernel_pixels);
1101 kernel_pixels+=GetPixelComponents(blur_image);
1103 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
1113 for (i=0; i < (ssize_t) width; i++)
1115 alpha=(MagickRealType) (QuantumScale*
1116 GetPixelAlpha(blur_image,kernel_pixels));
1117 pixel.red+=(*k)*alpha*GetPixelRed(blur_image,kernel_pixels);
1118 pixel.green+=(*k)*alpha*GetPixelGreen(blur_image,kernel_pixels);
1119 pixel.blue+=(*k)*alpha*GetPixelBlue(blur_image,kernel_pixels);
1120 if (blur_image->colorspace == CMYKColorspace)
1121 pixel.black+=(*k)*alpha*GetPixelBlack(blur_image,kernel_pixels);
1124 kernel_pixels+=GetPixelComponents(blur_image);
1126 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1127 if ((GetPixelRedTraits(image) & ActivePixelTrait) != 0)
1128 SetPixelRed(blur_image,ClampToQuantum(gamma*pixel.red),q);
1129 if ((GetPixelGreenTraits(image) & ActivePixelTrait) != 0)
1130 SetPixelGreen(blur_image,ClampToQuantum(gamma*pixel.green),q);
1131 if ((GetPixelBlueTraits(image) & ActivePixelTrait) != 0)
1132 SetPixelBlue(blur_image,ClampToQuantum(gamma*pixel.blue),q);
1133 if (((GetPixelBlackTraits(image) & ActivePixelTrait) != 0) &&
1134 (blur_image->colorspace == CMYKColorspace))
1135 SetPixelBlack(blur_image,ClampToQuantum(gamma*pixel.black),q);
1136 if ((GetPixelAlphaTraits(image) & ActivePixelTrait) != 0)
1140 for (i=0; i < (ssize_t) width; i++)
1142 pixel.alpha+=(*k)*GetPixelAlpha(blur_image,kernel_pixels);
1144 kernel_pixels+=GetPixelComponents(blur_image);
1146 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
1149 p+=GetPixelComponents(blur_image);
1150 q+=GetPixelComponents(blur_image);
1152 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1154 if (blur_image->progress_monitor != (MagickProgressMonitor) NULL)
1159 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1160 #pragma omp critical (MagickCore_BlurImage)
1162 proceed=SetImageProgress(blur_image,BlurImageTag,progress++,
1163 blur_image->rows+blur_image->columns);
1164 if (proceed == MagickFalse)
1168 blur_view=DestroyCacheView(blur_view);
1169 image_view=DestroyCacheView(image_view);
1170 kernel=(double *) RelinquishMagickMemory(kernel);
1171 if (status == MagickFalse)
1172 blur_image=DestroyImage(blur_image);
1173 blur_image->type=image->type;
1178 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1182 % C o n v o l v e I m a g e %
1186 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1188 % ConvolveImage() applies a custom convolution kernel to the image.
1190 % The format of the ConvolveImage method is:
1192 % Image *ConvolveImage(const Image *image,const size_t order,
1193 % const double *kernel,ExceptionInfo *exception)
1194 % A description of each parameter follows:
1196 % o image: the image.
1198 % o order: the number of columns and rows in the filter kernel.
1200 % o kernel: An array of double representing the convolution kernel.
1202 % o exception: return any errors or warnings in this structure.
1205 MagickExport Image *ConvolveImage(const Image *image,const size_t order,
1206 const double *kernel,ExceptionInfo *exception)
1208 #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);
1251 if ((width % 2) == 0)
1252 ThrowImageException(OptionError,"KernelWidthMustBeAnOddNumber");
1253 convolve_image=CloneImage(image,0,0,MagickTrue,exception);
1254 if (convolve_image == (Image *) NULL)
1255 return((Image *) NULL);
1256 if (SetImageStorageClass(convolve_image,DirectClass) == MagickFalse)
1258 InheritException(exception,&convolve_image->exception);
1259 convolve_image=DestroyImage(convolve_image);
1260 return((Image *) NULL);
1262 if (image->debug != MagickFalse)
1265 format[MaxTextExtent],
1268 register const double
1275 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1276 " ConvolveImage with %.20gx%.20g kernel:",(double) width,(double)
1278 message=AcquireString("");
1280 for (v=0; v < (ssize_t) width; v++)
1283 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
1284 (void) ConcatenateString(&message,format);
1285 for (u=0; u < (ssize_t) width; u++)
1287 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
1288 (void) ConcatenateString(&message,format);
1290 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
1292 message=DestroyString(message);
1297 normal_kernel=(double *) AcquireQuantumMemory(width*width,
1298 sizeof(*normal_kernel));
1299 if (normal_kernel == (double *) NULL)
1301 convolve_image=DestroyImage(convolve_image);
1302 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1305 for (i=0; i < (ssize_t) (width*width); i++)
1307 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1308 for (i=0; i < (ssize_t) (width*width); i++)
1309 normal_kernel[i]=gamma*kernel[i];
1315 GetPixelInfo(image,&bias);
1316 SetPixelInfoBias(image,&bias);
1317 image_view=AcquireCacheView(image);
1318 convolve_view=AcquireCacheView(convolve_image);
1319 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1320 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1322 for (y=0; y < (ssize_t) image->rows; y++)
1327 register const Quantum
1336 if (status == MagickFalse)
1338 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
1339 (width/2L),image->columns+width,width,exception);
1340 q=GetCacheViewAuthenticPixels(convolve_view,0,y,convolve_image->columns,1,
1342 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1347 for (x=0; x < (ssize_t) image->columns; x++)
1352 register const double
1355 register const Quantum
1356 *restrict kernel_pixels;
1367 if (((GetPixelAlphaTraits(image) & ActivePixelTrait) == 0) ||
1368 (image->matte == MagickFalse))
1370 for (v=0; v < (ssize_t) width; v++)
1372 for (u=0; u < (ssize_t) width; u++)
1374 pixel.red+=(*k)*GetPixelRed(image,kernel_pixels+u*
1375 GetPixelComponents(image));
1376 pixel.green+=(*k)*GetPixelGreen(image,kernel_pixels+u*
1377 GetPixelComponents(image));
1378 pixel.blue+=(*k)*GetPixelBlue(image,kernel_pixels+u*
1379 GetPixelComponents(image));
1380 if (image->colorspace == CMYKColorspace)
1381 pixel.black+=(*k)*GetPixelBlack(image,kernel_pixels+u*
1382 GetPixelComponents(image));
1385 kernel_pixels+=(image->columns+width)*GetPixelComponents(image);
1387 if ((GetPixelRedTraits(image) & ActivePixelTrait) != 0)
1388 SetPixelRed(convolve_image,ClampToQuantum(pixel.red),q);
1389 if ((GetPixelGreenTraits(image) & ActivePixelTrait) != 0)
1390 SetPixelGreen(convolve_image,ClampToQuantum(pixel.green),q);
1391 if ((GetPixelBlueTraits(image) & ActivePixelTrait) != 0)
1392 SetPixelBlue(convolve_image,ClampToQuantum(pixel.blue),q);
1393 if (((GetPixelBlackTraits(image) & ActivePixelTrait) != 0) &&
1394 (image->colorspace == CMYKColorspace))
1395 SetPixelBlack(convolve_image,ClampToQuantum(pixel.black),q);
1396 if ((GetPixelAlphaTraits(image) & ActivePixelTrait) != 0)
1400 for (v=0; v < (ssize_t) width; v++)
1402 for (u=0; u < (ssize_t) width; u++)
1404 pixel.alpha+=(*k)*GetPixelAlpha(image,kernel_pixels+u*
1405 GetPixelComponents(image));
1408 kernel_pixels+=(image->columns+width)*GetPixelComponents(image);
1410 SetPixelAlpha(convolve_image,ClampToQuantum(pixel.alpha),q);
1420 for (v=0; v < (ssize_t) width; v++)
1422 for (u=0; u < (ssize_t) width; u++)
1424 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,
1425 kernel_pixels+u*GetPixelComponents(image)));
1426 pixel.red+=(*k)*alpha*GetPixelRed(image,kernel_pixels+u*
1427 GetPixelComponents(image));
1428 pixel.green+=(*k)*alpha*GetPixelGreen(image,kernel_pixels+u*
1429 GetPixelComponents(image));
1430 pixel.blue+=(*k)*alpha*GetPixelBlue(image,kernel_pixels+u*
1431 GetPixelComponents(image));
1432 if (image->colorspace == CMYKColorspace)
1433 pixel.black+=(*k)*alpha*GetPixelBlack(image,kernel_pixels+u*
1434 GetPixelComponents(image));
1438 kernel_pixels+=(image->columns+width)*GetPixelComponents(image);
1440 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1441 if ((GetPixelRedTraits(image) & ActivePixelTrait) != 0)
1442 SetPixelRed(convolve_image,ClampToQuantum(gamma*pixel.red),q);
1443 if ((GetPixelGreenTraits(image) & ActivePixelTrait) != 0)
1444 SetPixelGreen(convolve_image,ClampToQuantum(gamma*pixel.green),q);
1445 if ((GetPixelBlueTraits(image) & ActivePixelTrait) != 0)
1446 SetPixelBlue(convolve_image,ClampToQuantum(gamma*pixel.blue),q);
1447 if (((GetPixelBlackTraits(image) & ActivePixelTrait) != 0) &&
1448 (image->colorspace == CMYKColorspace))
1449 SetPixelBlack(convolve_image,ClampToQuantum(gamma*pixel.black),q);
1450 if ((GetPixelAlphaTraits(image) & ActivePixelTrait) != 0)
1454 for (v=0; v < (ssize_t) width; v++)
1456 for (u=0; u < (ssize_t) width; u++)
1458 pixel.alpha+=(*k)*GetPixelAlpha(image,kernel_pixels+u);
1461 kernel_pixels+=(image->columns+width)*GetPixelComponents(image);
1463 SetPixelAlpha(convolve_image,ClampToQuantum(pixel.alpha),q);
1466 p+=GetPixelComponents(image);
1467 q+=GetPixelComponents(convolve_image);
1469 sync=SyncCacheViewAuthenticPixels(convolve_view,exception);
1470 if (sync == MagickFalse)
1472 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1477 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1478 #pragma omp critical (MagickCore_ConvolveImage)
1480 proceed=SetImageProgress(image,ConvolveImageTag,progress++,image->rows);
1481 if (proceed == MagickFalse)
1485 convolve_image->type=image->type;
1486 convolve_view=DestroyCacheView(convolve_view);
1487 image_view=DestroyCacheView(image_view);
1488 normal_kernel=(double *) RelinquishMagickMemory(normal_kernel);
1489 if (status == MagickFalse)
1490 convolve_image=DestroyImage(convolve_image);
1491 return(convolve_image);
1495 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1499 % D e s p e c k l e I m a g e %
1503 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1505 % DespeckleImage() reduces the speckle noise in an image while perserving the
1506 % edges of the original image.
1508 % The format of the DespeckleImage method is:
1510 % Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1512 % A description of each parameter follows:
1514 % o image: the image.
1516 % o exception: return any errors or warnings in this structure.
1520 static void Hull(const ssize_t x_offset,const ssize_t y_offset,
1521 const size_t columns,const size_t rows,Quantum *f,Quantum *g,
1539 assert(f != (Quantum *) NULL);
1540 assert(g != (Quantum *) NULL);
1543 r=p+(y_offset*((ssize_t) columns+2)+x_offset);
1544 for (y=0; y < (ssize_t) rows; y++)
1550 for (x=(ssize_t) columns; x != 0; x--)
1552 v=(MagickRealType) (*p);
1553 if ((MagickRealType) *r >= (v+(MagickRealType) ScaleCharToQuantum(2)))
1554 v+=ScaleCharToQuantum(1);
1561 for (x=(ssize_t) columns; x != 0; x--)
1563 v=(MagickRealType) (*p);
1564 if ((MagickRealType) *r <= (v-(MagickRealType) ScaleCharToQuantum(2)))
1565 v-=(ssize_t) ScaleCharToQuantum(1);
1577 r=q+(y_offset*((ssize_t) columns+2)+x_offset);
1578 s=q-(y_offset*((ssize_t) columns+2)+x_offset);
1579 for (y=0; y < (ssize_t) rows; y++)
1586 for (x=(ssize_t) columns; x != 0; x--)
1588 v=(MagickRealType) (*q);
1589 if (((MagickRealType) *s >=
1590 (v+(MagickRealType) ScaleCharToQuantum(2))) &&
1591 ((MagickRealType) *r > v))
1592 v+=ScaleCharToQuantum(1);
1600 for (x=(ssize_t) columns; x != 0; x--)
1602 v=(MagickRealType) (*q);
1603 if (((MagickRealType) *s <=
1604 (v-(MagickRealType) ScaleCharToQuantum(2))) &&
1605 ((MagickRealType) *r < v))
1606 v-=(MagickRealType) ScaleCharToQuantum(1);
1620 MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1622 #define DespeckleImageTag "Despeckle/Image"
1645 static const ssize_t
1646 X[4] = {0, 1, 1,-1},
1647 Y[4] = {1, 0, 1, 1};
1650 Allocate despeckled image.
1652 assert(image != (const Image *) NULL);
1653 assert(image->signature == MagickSignature);
1654 if (image->debug != MagickFalse)
1655 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1656 assert(exception != (ExceptionInfo *) NULL);
1657 assert(exception->signature == MagickSignature);
1658 despeckle_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1660 if (despeckle_image == (Image *) NULL)
1661 return((Image *) NULL);
1662 if (SetImageStorageClass(despeckle_image,DirectClass) == MagickFalse)
1664 InheritException(exception,&despeckle_image->exception);
1665 despeckle_image=DestroyImage(despeckle_image);
1666 return((Image *) NULL);
1669 Allocate image buffers.
1671 length=(size_t) ((image->columns+2)*(image->rows+2));
1672 pixels=(Quantum *) AcquireQuantumMemory(length,2*sizeof(*pixels));
1673 buffers=(Quantum *) AcquireQuantumMemory(length,2*sizeof(*pixels));
1674 if ((pixels == (Quantum *) NULL) || (buffers == (Quantum *) NULL))
1676 if (buffers != (Quantum *) NULL)
1677 buffers=(Quantum *) RelinquishMagickMemory(buffers);
1678 if (pixels != (Quantum *) NULL)
1679 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1680 despeckle_image=DestroyImage(despeckle_image);
1681 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1684 Reduce speckle in the image.
1687 number_channels=(size_t) (image->colorspace == CMYKColorspace ? 5 : 4);
1688 image_view=AcquireCacheView(image);
1689 despeckle_view=AcquireCacheView(despeckle_image);
1690 for (i=0; i < (ssize_t) number_channels; i++)
1704 if (status == MagickFalse)
1707 (void) ResetMagickMemory(pixel,0,length*sizeof(*pixel));
1709 j=(ssize_t) image->columns+2;
1710 for (y=0; y < (ssize_t) image->rows; y++)
1712 register const Quantum
1715 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1716 if (p == (const Quantum *) NULL)
1719 for (x=0; x < (ssize_t) image->columns; x++)
1723 case 0: pixel[j]=GetPixelRed(image,p); break;
1724 case 1: pixel[j]=GetPixelGreen(image,p); break;
1725 case 2: pixel[j]=GetPixelBlue(image,p); break;
1726 case 3: pixel[j]=GetPixelAlpha(image,p); break;
1727 case 4: pixel[j]=GetPixelBlack(image,p); break;
1730 p+=GetPixelComponents(image);
1735 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
1736 for (k=0; k < 4; k++)
1738 Hull(X[k],Y[k],image->columns,image->rows,pixel,buffer,1);
1739 Hull(-X[k],-Y[k],image->columns,image->rows,pixel,buffer,1);
1740 Hull(-X[k],-Y[k],image->columns,image->rows,pixel,buffer,-1);
1741 Hull(X[k],Y[k],image->columns,image->rows,pixel,buffer,-1);
1743 j=(ssize_t) image->columns+2;
1744 for (y=0; y < (ssize_t) image->rows; y++)
1752 q=GetCacheViewAuthenticPixels(despeckle_view,0,y,despeckle_image->columns,
1754 if (q == (const Quantum *) NULL)
1757 for (x=0; x < (ssize_t) image->columns; x++)
1761 case 0: SetPixelRed(despeckle_image,pixel[j],q); break;
1762 case 1: SetPixelGreen(despeckle_image,pixel[j],q); break;
1763 case 2: SetPixelBlue(despeckle_image,pixel[j],q); break;
1764 case 3: SetPixelAlpha(despeckle_image,pixel[j],q); break;
1765 case 4: SetPixelBlack(despeckle_image,pixel[j],q); break;
1768 q+=GetPixelComponents(despeckle_image);
1771 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1772 if (sync == MagickFalse)
1779 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1784 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1786 if (proceed == MagickFalse)
1790 despeckle_view=DestroyCacheView(despeckle_view);
1791 image_view=DestroyCacheView(image_view);
1792 buffers=(Quantum *) RelinquishMagickMemory(buffers);
1793 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1794 despeckle_image->type=image->type;
1795 if (status == MagickFalse)
1796 despeckle_image=DestroyImage(despeckle_image);
1797 return(despeckle_image);
1801 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1805 % E d g e I m a g e %
1809 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1811 % EdgeImage() finds edges in an image. Radius defines the radius of the
1812 % convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1815 % The format of the EdgeImage method is:
1817 % Image *EdgeImage(const Image *image,const double radius,
1818 % ExceptionInfo *exception)
1820 % A description of each parameter follows:
1822 % o image: the image.
1824 % o radius: the radius of the pixel neighborhood.
1826 % o exception: return any errors or warnings in this structure.
1829 MagickExport Image *EdgeImage(const Image *image,const double radius,
1830 ExceptionInfo *exception)
1844 assert(image != (const Image *) NULL);
1845 assert(image->signature == MagickSignature);
1846 if (image->debug != MagickFalse)
1847 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1848 assert(exception != (ExceptionInfo *) NULL);
1849 assert(exception->signature == MagickSignature);
1850 width=GetOptimalKernelWidth1D(radius,0.5);
1851 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
1852 if (kernel == (double *) NULL)
1853 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1854 for (i=0; i < (ssize_t) (width*width); i++)
1856 kernel[i/2]=(double) (width*width-1.0);
1857 edge_image=ConvolveImage(image,width,kernel,exception);
1858 kernel=(double *) RelinquishMagickMemory(kernel);
1863 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1867 % E m b o s s I m a g e %
1871 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1873 % EmbossImage() returns a grayscale image with a three-dimensional effect.
1874 % We convolve the image with a Gaussian operator of the given radius and
1875 % standard deviation (sigma). For reasonable results, radius should be
1876 % larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1879 % The format of the EmbossImage method is:
1881 % Image *EmbossImage(const Image *image,const double radius,
1882 % const double sigma,ExceptionInfo *exception)
1884 % A description of each parameter follows:
1886 % o image: the image.
1888 % o radius: the radius of the pixel neighborhood.
1890 % o sigma: the standard deviation of the Gaussian, in pixels.
1892 % o exception: return any errors or warnings in this structure.
1895 MagickExport Image *EmbossImage(const Image *image,const double radius,
1896 const double sigma,ExceptionInfo *exception)
1916 assert(image != (Image *) NULL);
1917 assert(image->signature == MagickSignature);
1918 if (image->debug != MagickFalse)
1919 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1920 assert(exception != (ExceptionInfo *) NULL);
1921 assert(exception->signature == MagickSignature);
1922 width=GetOptimalKernelWidth2D(radius,sigma);
1923 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
1924 if (kernel == (double *) NULL)
1925 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1926 j=(ssize_t) width/2;
1929 for (v=(-j); v <= j; v++)
1931 for (u=(-j); u <= j; u++)
1933 kernel[i]=(double) (((u < 0) || (v < 0) ? -8.0 : 8.0)*
1934 exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
1935 (2.0*MagickPI*MagickSigma*MagickSigma));
1942 emboss_image=ConvolveImage(image,width,kernel,exception);
1943 if (emboss_image != (Image *) NULL)
1944 (void) EqualizeImage(emboss_image);
1945 kernel=(double *) RelinquishMagickMemory(kernel);
1946 return(emboss_image);
1950 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1954 % F i l t e r I m a g e %
1958 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1960 % FilterImage() applies a custom convolution kernel to the image.
1962 % The format of the FilterImage method is:
1964 % Image *FilterImage(const Image *image,const KernelInfo *kernel,
1965 % ExceptionInfo *exception)
1967 % A description of each parameter follows:
1969 % o image: the image.
1971 % o kernel: the filtering kernel.
1973 % o exception: return any errors or warnings in this structure.
1976 MagickExport Image *FilterImage(const Image *image,
1977 const KernelInfo *kernel,ExceptionInfo *exception)
1979 #define FilterImageTag "Filter/Image"
2001 Initialize filter image attributes.
2003 assert(image != (Image *) NULL);
2004 assert(image->signature == MagickSignature);
2005 if (image->debug != MagickFalse)
2006 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2007 assert(exception != (ExceptionInfo *) NULL);
2008 assert(exception->signature == MagickSignature);
2009 if ((kernel->width % 2) == 0)
2010 ThrowImageException(OptionError,"KernelWidthMustBeAnOddNumber");
2011 filter_image=CloneImage(image,0,0,MagickTrue,exception);
2012 if (filter_image == (Image *) NULL)
2013 return((Image *) NULL);
2014 if (SetImageStorageClass(filter_image,DirectClass) == MagickFalse)
2016 InheritException(exception,&filter_image->exception);
2017 filter_image=DestroyImage(filter_image);
2018 return((Image *) NULL);
2020 if (image->debug != MagickFalse)
2023 format[MaxTextExtent],
2026 register const double
2033 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
2034 " FilterImage with %.20gx%.20g kernel:",(double) kernel->width,(double)
2036 message=AcquireString("");
2038 for (v=0; v < (ssize_t) kernel->height; v++)
2041 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
2042 (void) ConcatenateString(&message,format);
2043 for (u=0; u < (ssize_t) kernel->width; u++)
2045 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
2046 (void) ConcatenateString(&message,format);
2048 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
2050 message=DestroyString(message);
2052 status=AccelerateConvolveImage(image,kernel,filter_image,exception);
2053 if (status == MagickTrue)
2054 return(filter_image);
2060 GetPixelInfo(image,&bias);
2061 SetPixelInfoBias(image,&bias);
2062 image_view=AcquireCacheView(image);
2063 filter_view=AcquireCacheView(filter_image);
2064 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2065 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2067 for (y=0; y < (ssize_t) image->rows; y++)
2072 register const Quantum
2081 if (status == MagickFalse)
2083 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) kernel->width/2L),
2084 y-(ssize_t) (kernel->height/2L),image->columns+kernel->width,
2085 kernel->height,exception);
2086 q=GetCacheViewAuthenticPixels(filter_view,0,y,filter_image->columns,1,
2088 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2093 for (x=0; x < (ssize_t) image->columns; x++)
2098 register const double
2101 register const Quantum
2102 *restrict kernel_pixels;
2113 if (((GetPixelAlphaTraits(image) & ActivePixelTrait) == 0) ||
2114 (image->matte == MagickFalse))
2116 for (v=0; v < (ssize_t) kernel->width; v++)
2118 for (u=0; u < (ssize_t) kernel->height; u++)
2120 pixel.red+=(*k)*GetPixelRed(image,kernel_pixels+u*
2121 GetPixelComponents(image));
2122 pixel.green+=(*k)*GetPixelGreen(image,kernel_pixels+u*
2123 GetPixelComponents(image));
2124 pixel.blue+=(*k)*GetPixelBlue(image,kernel_pixels+u*
2125 GetPixelComponents(image));
2126 if (image->colorspace == CMYKColorspace)
2127 pixel.black+=(*k)*GetPixelBlack(image,kernel_pixels+u*
2128 GetPixelComponents(image));
2131 kernel_pixels+=(image->columns+kernel->width)*
2132 GetPixelComponents(image);
2134 if ((GetPixelRedTraits(image) & ActivePixelTrait) != 0)
2135 SetPixelRed(filter_image,ClampToQuantum(pixel.red),q);
2136 if ((GetPixelGreenTraits(image) & ActivePixelTrait) != 0)
2137 SetPixelGreen(filter_image,ClampToQuantum(pixel.green),q);
2138 if ((GetPixelBlueTraits(image) & ActivePixelTrait) != 0)
2139 SetPixelBlue(filter_image,ClampToQuantum(pixel.blue),q);
2140 if (((GetPixelBlackTraits(image) & ActivePixelTrait) != 0) &&
2141 (image->colorspace == CMYKColorspace))
2142 SetPixelBlack(filter_image,ClampToQuantum(pixel.black),q);
2143 if ((GetPixelAlphaTraits(image) & ActivePixelTrait) != 0)
2147 for (v=0; v < (ssize_t) kernel->width; v++)
2149 for (u=0; u < (ssize_t) kernel->height; u++)
2151 pixel.alpha+=(*k)*GetPixelRed(image,kernel_pixels+u*
2152 GetPixelComponents(image));
2155 kernel_pixels+=(image->columns+kernel->width)*
2156 GetPixelComponents(image);
2158 SetPixelAlpha(filter_image,ClampToQuantum(pixel.alpha),q);
2168 for (v=0; v < (ssize_t) kernel->width; v++)
2170 for (u=0; u < (ssize_t) kernel->height; u++)
2172 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,
2173 kernel_pixels+u*GetPixelComponents(image)));
2174 pixel.red+=(*k)*alpha*GetPixelRed(image,kernel_pixels+u*
2175 GetPixelComponents(image));
2176 pixel.green+=(*k)*alpha*GetPixelGreen(image,kernel_pixels+u*
2177 GetPixelComponents(image));
2178 pixel.blue+=(*k)*alpha*GetPixelBlue(image,kernel_pixels+u*
2179 GetPixelComponents(image));
2180 if (image->colorspace == CMYKColorspace)
2181 pixel.black+=(*k)*alpha*GetPixelBlack(image,kernel_pixels+u*
2182 GetPixelComponents(image));
2186 kernel_pixels+=(image->columns+kernel->width)*
2187 GetPixelComponents(image);
2189 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
2190 if ((GetPixelRedTraits(image) & ActivePixelTrait) != 0)
2191 SetPixelRed(filter_image,ClampToQuantum(gamma*pixel.red),q);
2192 if ((GetPixelGreenTraits(image) & ActivePixelTrait) != 0)
2193 SetPixelGreen(filter_image,ClampToQuantum(gamma*pixel.green),q);
2194 if ((GetPixelBlueTraits(image) & ActivePixelTrait) != 0)
2195 SetPixelBlue(filter_image,ClampToQuantum(gamma*pixel.blue),q);
2196 if (((GetPixelBlackTraits(image) & ActivePixelTrait) != 0) &&
2197 (image->colorspace == CMYKColorspace))
2198 SetPixelBlack(filter_image,ClampToQuantum(gamma*pixel.black),q);
2199 if ((GetPixelAlphaTraits(image) & ActivePixelTrait) != 0)
2203 for (v=0; v < (ssize_t) kernel->width; v++)
2205 for (u=0; u < (ssize_t) kernel->height; u++)
2207 pixel.alpha+=(*k)*GetPixelAlpha(image,kernel_pixels+u*
2208 GetPixelComponents(image));
2211 kernel_pixels+=(image->columns+kernel->width)*
2212 GetPixelComponents(image);
2214 SetPixelAlpha(filter_image,ClampToQuantum(pixel.alpha),q);
2217 p+=GetPixelComponents(image);
2218 q+=GetPixelComponents(filter_image);
2220 sync=SyncCacheViewAuthenticPixels(filter_view,exception);
2221 if (sync == MagickFalse)
2223 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2228 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2229 #pragma omp critical (MagickCore_FilterImage)
2231 proceed=SetImageProgress(image,FilterImageTag,progress++,image->rows);
2232 if (proceed == MagickFalse)
2236 filter_image->type=image->type;
2237 filter_view=DestroyCacheView(filter_view);
2238 image_view=DestroyCacheView(image_view);
2239 if (status == MagickFalse)
2240 filter_image=DestroyImage(filter_image);
2241 return(filter_image);
2245 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2249 % G a u s s i a n B l u r I m a g e %
2253 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2255 % GaussianBlurImage() blurs an image. We convolve the image with a
2256 % Gaussian operator of the given radius and standard deviation (sigma).
2257 % For reasonable results, the radius should be larger than sigma. Use a
2258 % radius of 0 and GaussianBlurImage() selects a suitable radius for you
2260 % The format of the GaussianBlurImage method is:
2262 % Image *GaussianBlurImage(const Image *image,onst double radius,
2263 % const double sigma,ExceptionInfo *exception)
2265 % A description of each parameter follows:
2267 % o image: the image.
2269 % o radius: the radius of the Gaussian, in pixels, not counting the center
2272 % o sigma: the standard deviation of the Gaussian, in pixels.
2274 % o exception: return any errors or warnings in this structure.
2277 MagickExport Image *GaussianBlurImage(const Image *image,
2278 const double radius,const double sigma,ExceptionInfo *exception)
2297 assert(image != (const Image *) NULL);
2298 assert(image->signature == MagickSignature);
2299 if (image->debug != MagickFalse)
2300 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2301 assert(exception != (ExceptionInfo *) NULL);
2302 assert(exception->signature == MagickSignature);
2303 width=GetOptimalKernelWidth2D(radius,sigma);
2304 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
2305 if (kernel == (double *) NULL)
2306 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2307 j=(ssize_t) width/2;
2309 for (v=(-j); v <= j; v++)
2311 for (u=(-j); u <= j; u++)
2312 kernel[i++]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
2313 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
2315 blur_image=ConvolveImage(image,width,kernel,exception);
2316 kernel=(double *) RelinquishMagickMemory(kernel);
2321 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2325 % M o t i o n B l u r I m a g e %
2329 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2331 % MotionBlurImage() simulates motion blur. We convolve the image with a
2332 % Gaussian operator of the given radius and standard deviation (sigma).
2333 % For reasonable results, radius should be larger than sigma. Use a
2334 % radius of 0 and MotionBlurImage() selects a suitable radius for you.
2335 % Angle gives the angle of the blurring motion.
2337 % Andrew Protano contributed this effect.
2339 % The format of the MotionBlurImage method is:
2341 % Image *MotionBlurImage(const Image *image,const double radius,
2342 % const double sigma,const double angle,ExceptionInfo *exception)
2344 % A description of each parameter follows:
2346 % o image: the image.
2348 % o radius: the radius of the Gaussian, in pixels, not counting
2351 % o sigma: the standard deviation of the Gaussian, in pixels.
2353 % o angle: Apply the effect along this angle.
2355 % o exception: return any errors or warnings in this structure.
2359 static double *GetMotionBlurKernel(const size_t width,const double sigma)
2369 Generate a 1-D convolution kernel.
2371 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2372 kernel=(double *) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
2373 if (kernel == (double *) NULL)
2376 for (i=0; i < (ssize_t) width; i++)
2378 kernel[i]=(double) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
2379 MagickSigma)))/(MagickSQ2PI*MagickSigma));
2380 normalize+=kernel[i];
2382 for (i=0; i < (ssize_t) width; i++)
2383 kernel[i]/=normalize;
2387 MagickExport Image *MotionBlurImage(const Image *image,
2388 const double radius,const double sigma,const double angle,
2389 ExceptionInfo *exception)
2425 assert(image != (Image *) NULL);
2426 assert(image->signature == MagickSignature);
2427 if (image->debug != MagickFalse)
2428 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2429 assert(exception != (ExceptionInfo *) NULL);
2430 width=GetOptimalKernelWidth1D(radius,sigma);
2431 kernel=GetMotionBlurKernel(width,sigma);
2432 if (kernel == (double *) NULL)
2433 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2434 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
2435 if (offset == (OffsetInfo *) NULL)
2437 kernel=(double *) RelinquishMagickMemory(kernel);
2438 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2440 blur_image=CloneImage(image,0,0,MagickTrue,exception);
2441 if (blur_image == (Image *) NULL)
2443 kernel=(double *) RelinquishMagickMemory(kernel);
2444 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2445 return((Image *) NULL);
2447 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
2449 kernel=(double *) RelinquishMagickMemory(kernel);
2450 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2451 InheritException(exception,&blur_image->exception);
2452 blur_image=DestroyImage(blur_image);
2453 return((Image *) NULL);
2455 point.x=(double) width*sin(DegreesToRadians(angle));
2456 point.y=(double) width*cos(DegreesToRadians(angle));
2457 for (i=0; i < (ssize_t) width; i++)
2459 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
2460 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
2467 GetPixelInfo(image,&bias);
2468 image_view=AcquireCacheView(image);
2469 blur_view=AcquireCacheView(blur_image);
2470 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2471 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
2473 for (y=0; y < (ssize_t) image->rows; y++)
2481 if (status == MagickFalse)
2483 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2485 if (q == (const Quantum *) NULL)
2490 for (x=0; x < (ssize_t) image->columns; x++)
2506 if (((GetPixelAlphaTraits(image) & ActivePixelTrait) == 0) || (image->matte == MagickFalse))
2508 for (i=0; i < (ssize_t) width; i++)
2510 (void) GetOneCacheViewVirtualPixel(image_view,x+offset[i].x,y+
2511 offset[i].y,&pixel,exception);
2512 qixel.red+=(*k)*pixel.red;
2513 qixel.green+=(*k)*pixel.green;
2514 qixel.blue+=(*k)*pixel.blue;
2515 qixel.alpha+=(*k)*pixel.alpha;
2516 if (image->colorspace == CMYKColorspace)
2517 qixel.black+=(*k)*pixel.black;
2520 if ((GetPixelRedTraits(image) & ActivePixelTrait) != 0)
2521 SetPixelRed(blur_image,
2522 ClampToQuantum(qixel.red),q);
2523 if ((GetPixelGreenTraits(image) & ActivePixelTrait) != 0)
2524 SetPixelGreen(blur_image,
2525 ClampToQuantum(qixel.green),q);
2526 if ((GetPixelBlueTraits(image) & ActivePixelTrait) != 0)
2527 SetPixelBlue(blur_image,
2528 ClampToQuantum(qixel.blue),q);
2529 if (((GetPixelBlackTraits(image) & ActivePixelTrait) != 0) &&
2530 (image->colorspace == CMYKColorspace))
2531 SetPixelBlack(blur_image,
2532 ClampToQuantum(qixel.black),q);
2533 if ((GetPixelAlphaTraits(image) & ActivePixelTrait) != 0)
2534 SetPixelAlpha(blur_image,
2535 ClampToQuantum(qixel.alpha),q);
2545 for (i=0; i < (ssize_t) width; i++)
2547 (void) GetOneCacheViewVirtualPixel(image_view,x+offset[i].x,y+
2548 offset[i].y,&pixel,exception);
2549 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
2550 qixel.red+=(*k)*alpha*pixel.red;
2551 qixel.green+=(*k)*alpha*pixel.green;
2552 qixel.blue+=(*k)*alpha*pixel.blue;
2553 qixel.alpha+=(*k)*pixel.alpha;
2554 if (image->colorspace == CMYKColorspace)
2555 qixel.black+=(*k)*alpha*pixel.black;
2559 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
2560 if ((GetPixelRedTraits(image) & ActivePixelTrait) != 0)
2561 SetPixelRed(blur_image,
2562 ClampToQuantum(gamma*qixel.red),q);
2563 if ((GetPixelGreenTraits(image) & ActivePixelTrait) != 0)
2564 SetPixelGreen(blur_image,
2565 ClampToQuantum(gamma*qixel.green),q);
2566 if ((GetPixelBlueTraits(image) & ActivePixelTrait) != 0)
2567 SetPixelBlue(blur_image,
2568 ClampToQuantum(gamma*qixel.blue),q);
2569 if (((GetPixelBlackTraits(image) & ActivePixelTrait) != 0) &&
2570 (image->colorspace == CMYKColorspace))
2571 SetPixelBlack(blur_image,
2572 ClampToQuantum(gamma*qixel.black),q);
2573 if ((GetPixelAlphaTraits(image) & ActivePixelTrait) != 0)
2574 SetPixelAlpha(blur_image,
2575 ClampToQuantum(qixel.alpha),q);
2577 q+=GetPixelComponents(blur_image);
2579 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2581 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2586 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2587 #pragma omp critical (MagickCore_MotionBlurImage)
2589 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2590 if (proceed == MagickFalse)
2594 blur_view=DestroyCacheView(blur_view);
2595 image_view=DestroyCacheView(image_view);
2596 kernel=(double *) RelinquishMagickMemory(kernel);
2597 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2598 if (status == MagickFalse)
2599 blur_image=DestroyImage(blur_image);
2604 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2608 % P r e v i e w I m a g e %
2612 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2614 % PreviewImage() tiles 9 thumbnails of the specified image with an image
2615 % processing operation applied with varying parameters. This may be helpful
2616 % pin-pointing an appropriate parameter for a particular image processing
2619 % The format of the PreviewImages method is:
2621 % Image *PreviewImages(const Image *image,const PreviewType preview,
2622 % ExceptionInfo *exception)
2624 % A description of each parameter follows:
2626 % o image: the image.
2628 % o preview: the image processing operation.
2630 % o exception: return any errors or warnings in this structure.
2633 MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2634 ExceptionInfo *exception)
2636 #define NumberTiles 9
2637 #define PreviewImageTag "Preview/Image"
2638 #define DefaultPreviewGeometry "204x204+10+10"
2641 factor[MaxTextExtent],
2642 label[MaxTextExtent];
2684 Open output image file.
2686 assert(image != (Image *) NULL);
2687 assert(image->signature == MagickSignature);
2688 if (image->debug != MagickFalse)
2689 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2693 preview_info=AcquireImageInfo();
2694 SetGeometry(image,&geometry);
2695 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2696 &geometry.width,&geometry.height);
2697 images=NewImageList();
2699 GetQuantizeInfo(&quantize_info);
2705 for (i=0; i < NumberTiles; i++)
2707 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2708 if (thumbnail == (Image *) NULL)
2710 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2712 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel);
2713 if (i == (NumberTiles/2))
2715 (void) QueryColorDatabase("#dfdfdf",&thumbnail->matte_color,exception);
2716 AppendImageToList(&images,thumbnail);
2724 preview_image=RotateImage(thumbnail,degrees,exception);
2725 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
2731 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
2732 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",
2733 degrees,2.0*degrees);
2738 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2739 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
2740 preview_image=RollImage(thumbnail,x,y,exception);
2741 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
2742 (double) x,(double) y);
2747 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2748 if (preview_image == (Image *) NULL)
2750 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",
2752 (void) ModulateImage(preview_image,factor);
2753 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2756 case SaturationPreview:
2758 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2759 if (preview_image == (Image *) NULL)
2761 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",
2763 (void) ModulateImage(preview_image,factor);
2764 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2767 case BrightnessPreview:
2769 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2770 if (preview_image == (Image *) NULL)
2772 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
2773 (void) ModulateImage(preview_image,factor);
2774 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2780 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2781 if (preview_image == (Image *) NULL)
2784 (void) GammaImage(preview_image,gamma);
2785 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
2790 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2791 if (preview_image != (Image *) NULL)
2792 for (x=0; x < i; x++)
2793 (void) ContrastImage(preview_image,MagickTrue);
2794 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
2800 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2801 if (preview_image == (Image *) NULL)
2803 for (x=0; x < i; x++)
2804 (void) ContrastImage(preview_image,MagickFalse);
2805 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
2809 case GrayscalePreview:
2811 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2812 if (preview_image == (Image *) NULL)
2815 quantize_info.number_colors=colors;
2816 quantize_info.colorspace=GRAYColorspace;
2817 (void) QuantizeImage(&quantize_info,preview_image);
2818 (void) FormatLocaleString(label,MaxTextExtent,
2819 "-colorspace gray -colors %.20g",(double) colors);
2822 case QuantizePreview:
2824 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2825 if (preview_image == (Image *) NULL)
2828 quantize_info.number_colors=colors;
2829 (void) QuantizeImage(&quantize_info,preview_image);
2830 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
2834 case DespecklePreview:
2836 for (x=0; x < (i-1); x++)
2838 preview_image=DespeckleImage(thumbnail,exception);
2839 if (preview_image == (Image *) NULL)
2841 thumbnail=DestroyImage(thumbnail);
2842 thumbnail=preview_image;
2844 preview_image=DespeckleImage(thumbnail,exception);
2845 if (preview_image == (Image *) NULL)
2847 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
2851 case ReduceNoisePreview:
2853 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2854 (size_t) radius,exception);
2855 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
2858 case AddNoisePreview:
2864 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2869 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2874 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2879 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2884 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2889 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2894 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2898 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2899 (size_t) i,exception);
2900 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
2903 case SharpenPreview:
2905 preview_image=SharpenImage(thumbnail,radius,sigma,exception);
2906 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",
2912 preview_image=BlurImage(thumbnail,radius,sigma,exception);
2913 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
2917 case ThresholdPreview:
2919 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2920 if (preview_image == (Image *) NULL)
2922 (void) BilevelImage(thumbnail,
2923 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
2924 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",
2925 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
2928 case EdgeDetectPreview:
2930 preview_image=EdgeImage(thumbnail,radius,exception);
2931 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
2936 preview_image=SpreadImage(thumbnail,radius,exception);
2937 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",
2941 case SolarizePreview:
2943 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2944 if (preview_image == (Image *) NULL)
2946 (void) SolarizeImage(preview_image,(double) QuantumRange*
2948 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
2949 (QuantumRange*percentage)/100.0);
2955 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2957 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",
2963 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2964 if (preview_image == (Image *) NULL)
2966 geometry.width=(size_t) (2*i+2);
2967 geometry.height=(size_t) (2*i+2);
2970 (void) RaiseImage(preview_image,&geometry,MagickTrue);
2971 (void) FormatLocaleString(label,MaxTextExtent,
2972 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
2973 geometry.height,(double) geometry.x,(double) geometry.y);
2976 case SegmentPreview:
2978 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2979 if (preview_image == (Image *) NULL)
2982 (void) SegmentImage(preview_image,RGBColorspace,MagickFalse,threshold,
2984 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
2985 threshold,threshold);
2990 preview_image=SwirlImage(thumbnail,degrees,exception);
2991 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
2995 case ImplodePreview:
2998 preview_image=ImplodeImage(thumbnail,degrees,exception);
2999 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
3005 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,exception);
3006 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",
3007 0.5*degrees,2.0*degrees);
3010 case OilPaintPreview:
3012 preview_image=OilPaintImage(thumbnail,(double) radius,exception);
3013 (void) FormatLocaleString(label,MaxTextExtent,"paint %g",radius);
3016 case CharcoalDrawingPreview:
3018 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
3020 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
3027 filename[MaxTextExtent];
3035 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3036 if (preview_image == (Image *) NULL)
3038 preview_info->quality=(size_t) percentage;
3039 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
3040 preview_info->quality);
3041 file=AcquireUniqueFileResource(filename);
3044 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
3045 "jpeg:%s",filename);
3046 status=WriteImage(preview_info,preview_image);
3047 if (status != MagickFalse)
3052 (void) CopyMagickString(preview_info->filename,
3053 preview_image->filename,MaxTextExtent);
3054 quality_image=ReadImage(preview_info,exception);
3055 if (quality_image != (Image *) NULL)
3057 preview_image=DestroyImage(preview_image);
3058 preview_image=quality_image;
3061 (void) RelinquishUniqueFileResource(preview_image->filename);
3062 if ((GetBlobSize(preview_image)/1024) >= 1024)
3063 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
3064 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
3067 if (GetBlobSize(preview_image) >= 1024)
3068 (void) FormatLocaleString(label,MaxTextExtent,
3069 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
3070 GetBlobSize(preview_image))/1024.0);
3072 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
3073 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
3077 thumbnail=DestroyImage(thumbnail);
3081 if (preview_image == (Image *) NULL)
3083 (void) DeleteImageProperty(preview_image,"label");
3084 (void) SetImageProperty(preview_image,"label",label);
3085 AppendImageToList(&images,preview_image);
3086 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
3088 if (proceed == MagickFalse)
3091 if (images == (Image *) NULL)
3093 preview_info=DestroyImageInfo(preview_info);
3094 return((Image *) NULL);
3099 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
3100 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
3101 montage_info->shadow=MagickTrue;
3102 (void) CloneString(&montage_info->tile,"3x3");
3103 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
3104 (void) CloneString(&montage_info->frame,DefaultTileFrame);
3105 montage_image=MontageImages(images,montage_info,exception);
3106 montage_info=DestroyMontageInfo(montage_info);
3107 images=DestroyImageList(images);
3108 if (montage_image == (Image *) NULL)
3109 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3110 if (montage_image->montage != (char *) NULL)
3113 Free image directory.
3115 montage_image->montage=(char *) RelinquishMagickMemory(
3116 montage_image->montage);
3117 if (image->directory != (char *) NULL)
3118 montage_image->directory=(char *) RelinquishMagickMemory(
3119 montage_image->directory);
3121 preview_info=DestroyImageInfo(preview_info);
3122 return(montage_image);
3126 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3130 % R a d i a l B l u r I m a g e %
3134 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3136 % RadialBlurImage() applies a radial blur to the image.
3138 % Andrew Protano contributed this effect.
3140 % The format of the RadialBlurImage method is:
3142 % Image *RadialBlurImage(const Image *image,const double angle,
3143 % ExceptionInfo *exception)
3145 % A description of each parameter follows:
3147 % o image: the image.
3149 % o angle: the angle of the radial blur.
3151 % o exception: return any errors or warnings in this structure.
3154 MagickExport Image *RadialBlurImage(const Image *image,
3155 const double angle,ExceptionInfo *exception)
3193 Allocate blur image.
3195 assert(image != (Image *) NULL);
3196 assert(image->signature == MagickSignature);
3197 if (image->debug != MagickFalse)
3198 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3199 assert(exception != (ExceptionInfo *) NULL);
3200 assert(exception->signature == MagickSignature);
3201 blur_image=CloneImage(image,0,0,MagickTrue,exception);
3202 if (blur_image == (Image *) NULL)
3203 return((Image *) NULL);
3204 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
3206 InheritException(exception,&blur_image->exception);
3207 blur_image=DestroyImage(blur_image);
3208 return((Image *) NULL);
3210 blur_center.x=(double) image->columns/2.0;
3211 blur_center.y=(double) image->rows/2.0;
3212 blur_radius=hypot(blur_center.x,blur_center.y);
3213 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
3214 theta=DegreesToRadians(angle)/(MagickRealType) (n-1);
3215 cos_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
3216 sizeof(*cos_theta));
3217 sin_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
3218 sizeof(*sin_theta));
3219 if ((cos_theta == (MagickRealType *) NULL) ||
3220 (sin_theta == (MagickRealType *) NULL))
3222 blur_image=DestroyImage(blur_image);
3223 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3225 offset=theta*(MagickRealType) (n-1)/2.0;
3226 for (i=0; i < (ssize_t) n; i++)
3228 cos_theta[i]=cos((double) (theta*i-offset));
3229 sin_theta[i]=sin((double) (theta*i-offset));
3236 GetPixelInfo(image,&bias);
3237 image_view=AcquireCacheView(image);
3238 blur_view=AcquireCacheView(blur_image);
3239 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3240 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3242 for (y=0; y < (ssize_t) blur_image->rows; y++)
3250 if (status == MagickFalse)
3252 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3254 if (q == (const Quantum *) NULL)
3259 for (x=0; x < (ssize_t) blur_image->columns; x++)
3280 center.x=(double) x-blur_center.x;
3281 center.y=(double) y-blur_center.y;
3282 radius=hypot((double) center.x,center.y);
3287 step=(size_t) (blur_radius/radius);
3296 if (((GetPixelAlphaTraits(image) & ActivePixelTrait) == 0) || (image->matte == MagickFalse))
3298 for (i=0; i < (ssize_t) n; i+=(ssize_t) step)
3300 (void) GetOneCacheViewVirtualPixel(image_view,(ssize_t)
3301 (blur_center.x+center.x*cos_theta[i]-center.y*sin_theta[i]+0.5),
3302 (ssize_t) (blur_center.y+center.x*sin_theta[i]+center.y*
3303 cos_theta[i]+0.5),&pixel,exception);
3304 qixel.red+=pixel.red;
3305 qixel.green+=pixel.green;
3306 qixel.blue+=pixel.blue;
3307 if (image->colorspace == CMYKColorspace)
3308 qixel.black+=pixel.black;
3309 qixel.alpha+=pixel.alpha;
3312 normalize=1.0/(fabs((double) normalize) <= MagickEpsilon ? 1.0 :
3314 if ((GetPixelRedTraits(image) & ActivePixelTrait) != 0)
3315 SetPixelRed(blur_image,
3316 ClampToQuantum(normalize*qixel.red),q);
3317 if ((GetPixelGreenTraits(image) & ActivePixelTrait) != 0)
3318 SetPixelGreen(blur_image,
3319 ClampToQuantum(normalize*qixel.green),q);
3320 if ((GetPixelBlueTraits(image) & ActivePixelTrait) != 0)
3321 SetPixelBlue(blur_image,
3322 ClampToQuantum(normalize*qixel.blue),q);
3323 if (((GetPixelBlackTraits(image) & ActivePixelTrait) != 0) &&
3324 (image->colorspace == CMYKColorspace))
3325 SetPixelBlack(blur_image,
3326 ClampToQuantum(normalize*qixel.black),q);
3327 if ((GetPixelAlphaTraits(image) & ActivePixelTrait) != 0)
3328 SetPixelAlpha(blur_image,
3329 ClampToQuantum(normalize*qixel.alpha),q);
3339 for (i=0; i < (ssize_t) n; i+=(ssize_t) step)
3341 (void) GetOneCacheViewVirtualPixel(image_view,(ssize_t)
3342 (blur_center.x+center.x*cos_theta[i]-center.y*sin_theta[i]+0.5),
3343 (ssize_t) (blur_center.y+center.x*sin_theta[i]+center.y*
3344 cos_theta[i]+0.5),&pixel,exception);
3345 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
3346 qixel.red+=alpha*pixel.red;
3347 qixel.green+=alpha*pixel.green;
3348 qixel.blue+=alpha*pixel.blue;
3349 qixel.alpha+=pixel.alpha;
3350 if (image->colorspace == CMYKColorspace)
3351 qixel.black+=alpha*pixel.black;
3355 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3356 normalize=1.0/(fabs((double) normalize) <= MagickEpsilon ? 1.0 :
3358 if ((GetPixelRedTraits(image) & ActivePixelTrait) != 0)
3359 SetPixelRed(blur_image,
3360 ClampToQuantum(gamma*qixel.red),q);
3361 if ((GetPixelGreenTraits(image) & ActivePixelTrait) != 0)
3362 SetPixelGreen(blur_image,
3363 ClampToQuantum(gamma*qixel.green),q);
3364 if ((GetPixelBlueTraits(image) & ActivePixelTrait) != 0)
3365 SetPixelBlue(blur_image,
3366 ClampToQuantum(gamma*qixel.blue),q);
3367 if (((GetPixelBlackTraits(image) & ActivePixelTrait) != 0) &&
3368 (image->colorspace == CMYKColorspace))
3369 SetPixelBlack(blur_image,
3370 ClampToQuantum(gamma*qixel.black),q);
3371 if ((GetPixelAlphaTraits(image) & ActivePixelTrait) != 0)
3372 SetPixelAlpha(blur_image,
3373 ClampToQuantum(normalize*qixel.alpha),q);
3375 q+=GetPixelComponents(blur_image);
3377 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
3379 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3384 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3385 #pragma omp critical (MagickCore_RadialBlurImage)
3387 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
3388 if (proceed == MagickFalse)
3392 blur_view=DestroyCacheView(blur_view);
3393 image_view=DestroyCacheView(image_view);
3394 cos_theta=(MagickRealType *) RelinquishMagickMemory(cos_theta);
3395 sin_theta=(MagickRealType *) RelinquishMagickMemory(sin_theta);
3396 if (status == MagickFalse)
3397 blur_image=DestroyImage(blur_image);
3402 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3406 % S e l e c t i v e B l u r I m a g e %
3410 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3412 % SelectiveBlurImage() selectively blur pixels within a contrast threshold.
3413 % It is similar to the unsharpen mask that sharpens everything with contrast
3414 % above a certain threshold.
3416 % The format of the SelectiveBlurImage method is:
3418 % Image *SelectiveBlurImage(const Image *image,const double radius,
3419 % const double sigma,const double threshold,ExceptionInfo *exception)
3421 % A description of each parameter follows:
3423 % o image: the image.
3425 % o radius: the radius of the Gaussian, in pixels, not counting the center
3428 % o sigma: the standard deviation of the Gaussian, in pixels.
3430 % o threshold: only pixels within this contrast threshold are included
3431 % in the blur operation.
3433 % o exception: return any errors or warnings in this structure.
3436 MagickExport Image *SelectiveBlurImage(const Image *image,
3437 const double radius,const double sigma,const double threshold,
3438 ExceptionInfo *exception)
3440 #define SelectiveBlurImageTag "SelectiveBlur/Image"
3474 Initialize blur image attributes.
3476 assert(image != (Image *) NULL);
3477 assert(image->signature == MagickSignature);
3478 if (image->debug != MagickFalse)
3479 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3480 assert(exception != (ExceptionInfo *) NULL);
3481 assert(exception->signature == MagickSignature);
3482 width=GetOptimalKernelWidth1D(radius,sigma);
3483 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
3484 if (kernel == (double *) NULL)
3485 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3486 j=(ssize_t) width/2;
3488 for (v=(-j); v <= j; v++)
3490 for (u=(-j); u <= j; u++)
3491 kernel[i++]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
3492 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3494 if (image->debug != MagickFalse)
3497 format[MaxTextExtent],
3500 register const double
3507 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
3508 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
3510 message=AcquireString("");
3512 for (v=0; v < (ssize_t) width; v++)
3515 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
3516 (void) ConcatenateString(&message,format);
3517 for (u=0; u < (ssize_t) width; u++)
3519 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",*k++);
3520 (void) ConcatenateString(&message,format);
3522 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
3524 message=DestroyString(message);
3526 blur_image=CloneImage(image,0,0,MagickTrue,exception);
3527 if (blur_image == (Image *) NULL)
3528 return((Image *) NULL);
3529 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
3531 InheritException(exception,&blur_image->exception);
3532 blur_image=DestroyImage(blur_image);
3533 return((Image *) NULL);
3536 Threshold blur image.
3540 GetPixelInfo(image,&bias);
3541 SetPixelInfoBias(image,&bias);
3542 image_view=AcquireCacheView(image);
3543 blur_view=AcquireCacheView(blur_image);
3544 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3545 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3547 for (y=0; y < (ssize_t) image->rows; y++)
3558 register const Quantum
3567 if (status == MagickFalse)
3569 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
3570 (width/2L),image->columns+width,width,exception);
3571 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3573 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3578 for (x=0; x < (ssize_t) image->columns; x++)
3583 register const double
3597 if (((GetPixelAlphaTraits(image) & ActivePixelTrait) == 0) || (image->matte == MagickFalse))
3599 for (v=0; v < (ssize_t) width; v++)
3601 for (u=0; u < (ssize_t) width; u++)
3603 contrast=GetPixelIntensity(image,p+(u+j)*GetPixelComponents(image))-
3604 (double) GetPixelIntensity(blur_image,q);
3605 if (fabs(contrast) < threshold)
3608 GetPixelRed(image,p+(u+j)*GetPixelComponents(image));
3610 GetPixelGreen(image,p+(u+j)*GetPixelComponents(image));
3612 GetPixelBlue(image,p+(u+j)*GetPixelComponents(image));
3613 if (image->colorspace == CMYKColorspace)
3615 GetPixelBlack(image,p+(u+j)*GetPixelComponents(image));
3620 j+=(ssize_t) (image->columns+width);
3624 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3625 if ((GetPixelRedTraits(image) & ActivePixelTrait) != 0)
3626 SetPixelRed(blur_image,ClampToQuantum(gamma*pixel.red),q);
3627 if ((GetPixelGreenTraits(image) & ActivePixelTrait) != 0)
3628 SetPixelGreen(blur_image,ClampToQuantum(gamma*pixel.green),q);
3629 if ((GetPixelBlueTraits(image) & ActivePixelTrait) != 0)
3630 SetPixelBlue(blur_image,ClampToQuantum(gamma*pixel.blue),q);
3631 if (((GetPixelBlackTraits(image) & ActivePixelTrait) != 0) &&
3632 (image->colorspace == CMYKColorspace))
3633 SetPixelBlack(blur_image,ClampToQuantum(gamma*pixel.black),q);
3635 if ((GetPixelAlphaTraits(image) & ActivePixelTrait) != 0)
3639 for (v=0; v < (ssize_t) width; v++)
3641 for (u=0; u < (ssize_t) width; u++)
3643 contrast=GetPixelIntensity(image,p+(u+j)*
3644 GetPixelComponents(image))-(double)
3645 GetPixelIntensity(blur_image,q);
3646 if (fabs(contrast) < threshold)
3649 GetPixelAlpha(image,p+(u+j)*GetPixelComponents(image));
3654 j+=(ssize_t) (image->columns+width);
3658 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
3660 SetPixelAlpha(blur_image,ClampToQuantum(gamma*pixel.alpha),q);
3669 for (v=0; v < (ssize_t) width; v++)
3671 for (u=0; u < (ssize_t) width; u++)
3673 contrast=GetPixelIntensity(image,p+(u+j)*
3674 GetPixelComponents(image))-(double)
3675 GetPixelIntensity(blur_image,q);
3676 if (fabs(contrast) < threshold)
3678 alpha=(MagickRealType) (QuantumScale*
3679 GetPixelAlpha(image,p+(u+j)*GetPixelComponents(image)));
3680 pixel.red+=(*k)*alpha*
3681 GetPixelRed(image,p+(u+j)*GetPixelComponents(image));
3682 pixel.green+=(*k)*alpha*GetPixelGreen(image,p+(u+j)*
3683 GetPixelComponents(image));
3684 pixel.blue+=(*k)*alpha*GetPixelBlue(image,p+(u+j)*
3685 GetPixelComponents(image));
3686 pixel.alpha+=(*k)*GetPixelAlpha(image,p+(u+j)*
3687 GetPixelComponents(image));
3688 if (image->colorspace == CMYKColorspace)
3689 pixel.black+=(*k)*GetPixelBlack(image,p+(u+j)*
3690 GetPixelComponents(image));
3695 j+=(ssize_t) (image->columns+width);
3699 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3700 if ((GetPixelRedTraits(image) & ActivePixelTrait) != 0)
3701 SetPixelRed(blur_image,ClampToQuantum(gamma*pixel.red),q);
3702 if ((GetPixelGreenTraits(image) & ActivePixelTrait) != 0)
3703 SetPixelGreen(blur_image,ClampToQuantum(gamma*pixel.green),q);
3704 if ((GetPixelBlueTraits(image) & ActivePixelTrait) != 0)
3705 SetPixelBlue(blur_image,ClampToQuantum(gamma*pixel.blue),q);
3706 if (((GetPixelBlackTraits(image) & ActivePixelTrait) != 0) &&
3707 (image->colorspace == CMYKColorspace))
3708 SetPixelBlack(blur_image,ClampToQuantum(gamma*pixel.black),q);
3710 if ((GetPixelAlphaTraits(image) & ActivePixelTrait) != 0)
3714 for (v=0; v < (ssize_t) width; v++)
3716 for (u=0; u < (ssize_t) width; u++)
3718 contrast=GetPixelIntensity(image,p+(u+j)*
3719 GetPixelComponents(image))-(double)
3720 GetPixelIntensity(blur_image,q);
3721 if (fabs(contrast) < threshold)
3724 GetPixelAlpha(image,p+(u+j)*GetPixelComponents(image));
3729 j+=(ssize_t) (image->columns+width);
3733 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
3735 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
3739 p+=GetPixelComponents(image);
3740 q+=GetPixelComponents(blur_image);
3742 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3743 if (sync == MagickFalse)
3745 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3750 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3751 #pragma omp critical (MagickCore_SelectiveBlurImage)
3753 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3755 if (proceed == MagickFalse)
3759 blur_image->type=image->type;
3760 blur_view=DestroyCacheView(blur_view);
3761 image_view=DestroyCacheView(image_view);
3762 kernel=(double *) RelinquishMagickMemory(kernel);
3763 if (status == MagickFalse)
3764 blur_image=DestroyImage(blur_image);
3769 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3773 % S h a d e I m a g e %
3777 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3779 % ShadeImage() shines a distant light on an image to create a
3780 % three-dimensional effect. You control the positioning of the light with
3781 % azimuth and elevation; azimuth is measured in degrees off the x axis
3782 % and elevation is measured in pixels above the Z axis.
3784 % The format of the ShadeImage method is:
3786 % Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3787 % const double azimuth,const double elevation,ExceptionInfo *exception)
3789 % A description of each parameter follows:
3791 % o image: the image.
3793 % o gray: A value other than zero shades the intensity of each pixel.
3795 % o azimuth, elevation: Define the light source direction.
3797 % o exception: return any errors or warnings in this structure.
3800 MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3801 const double azimuth,const double elevation,ExceptionInfo *exception)
3803 #define ShadeImageTag "Shade/Image"
3825 Initialize shaded image attributes.
3827 assert(image != (const Image *) NULL);
3828 assert(image->signature == MagickSignature);
3829 if (image->debug != MagickFalse)
3830 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3831 assert(exception != (ExceptionInfo *) NULL);
3832 assert(exception->signature == MagickSignature);
3833 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3834 if (shade_image == (Image *) NULL)
3835 return((Image *) NULL);
3836 if (SetImageStorageClass(shade_image,DirectClass) == MagickFalse)
3838 InheritException(exception,&shade_image->exception);
3839 shade_image=DestroyImage(shade_image);
3840 return((Image *) NULL);
3843 Compute the light vector.
3845 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3846 cos(DegreesToRadians(elevation));
3847 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3848 cos(DegreesToRadians(elevation));
3849 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3855 image_view=AcquireCacheView(image);
3856 shade_view=AcquireCacheView(shade_image);
3857 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3858 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3860 for (y=0; y < (ssize_t) image->rows; y++)
3870 register const Quantum
3882 if (status == MagickFalse)
3884 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
3885 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3887 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3893 Shade this row of pixels.
3895 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
3896 s0=p+GetPixelComponents(image);
3897 s1=s0+(image->columns+2)*GetPixelComponents(image);
3898 s2=s1+(image->columns+2)*GetPixelComponents(image);
3899 for (x=0; x < (ssize_t) image->columns; x++)
3902 Determine the surface normal and compute shading.
3904 normal.x=(double) (GetPixelIntensity(image,s0-GetPixelComponents(image))+
3905 GetPixelIntensity(image,s1-GetPixelComponents(image))+
3906 GetPixelIntensity(image,s2-GetPixelComponents(image))-
3907 GetPixelIntensity(image,s0+GetPixelComponents(image))-
3908 GetPixelIntensity(image,s1+GetPixelComponents(image))-
3909 GetPixelIntensity(image,s2+GetPixelComponents(image)));
3910 normal.y=(double) (GetPixelIntensity(image,s2-GetPixelComponents(image))+
3911 GetPixelIntensity(image,s2)+
3912 GetPixelIntensity(image,s2+GetPixelComponents(image))-
3913 GetPixelIntensity(image,s0-GetPixelComponents(image))-
3914 GetPixelIntensity(image,s0)-
3915 GetPixelIntensity(image,s0+GetPixelComponents(image)));
3916 if ((normal.x == 0.0) && (normal.y == 0.0))
3921 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3922 if (distance > MagickEpsilon)
3925 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
3926 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3927 shade=distance/sqrt((double) normal_distance);
3930 if (gray != MagickFalse)
3932 SetPixelRed(shade_image,ClampToQuantum(shade),q);
3933 SetPixelGreen(shade_image,ClampToQuantum(shade),q);
3934 SetPixelBlue(shade_image,ClampToQuantum(shade),q);
3938 SetPixelRed(shade_image,ClampToQuantum(QuantumScale*shade*
3939 GetPixelRed(image,s1)),q);
3940 SetPixelGreen(shade_image,ClampToQuantum(QuantumScale*shade*
3941 GetPixelGreen(image,s1)),q);
3942 SetPixelBlue(shade_image,ClampToQuantum(QuantumScale*shade*
3943 GetPixelBlue(image,s1)),q);
3945 SetPixelAlpha(shade_image,GetPixelAlpha(image,s1),q);
3946 s0+=GetPixelComponents(image);
3947 s1+=GetPixelComponents(image);
3948 s2+=GetPixelComponents(image);
3949 q+=GetPixelComponents(shade_image);
3951 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3953 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3958 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3959 #pragma omp critical (MagickCore_ShadeImage)
3961 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3962 if (proceed == MagickFalse)
3966 shade_view=DestroyCacheView(shade_view);
3967 image_view=DestroyCacheView(image_view);
3968 if (status == MagickFalse)
3969 shade_image=DestroyImage(shade_image);
3970 return(shade_image);
3974 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3978 % S h a r p e n I m a g e %
3982 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3984 % SharpenImage() sharpens the image. We convolve the image with a Gaussian
3985 % operator of the given radius and standard deviation (sigma). For
3986 % reasonable results, radius should be larger than sigma. Use a radius of 0
3987 % and SharpenImage() selects a suitable radius for you.
3989 % Using a separable kernel would be faster, but the negative weights cancel
3990 % out on the corners of the kernel producing often undesirable ringing in the
3991 % filtered result; this can be avoided by using a 2D gaussian shaped image
3992 % sharpening kernel instead.
3994 % The format of the SharpenImage method is:
3996 % Image *SharpenImage(const Image *image,const double radius,
3997 % const double sigma,ExceptionInfo *exception)
3999 % A description of each parameter follows:
4001 % o image: the image.
4003 % o radius: the radius of the Gaussian, in pixels, not counting the center
4006 % o sigma: the standard deviation of the Laplacian, in pixels.
4008 % o exception: return any errors or warnings in this structure.
4011 MagickExport Image *SharpenImage(const Image *image,const double radius,
4012 const double sigma,ExceptionInfo *exception)
4032 assert(image != (const Image *) NULL);
4033 assert(image->signature == MagickSignature);
4034 if (image->debug != MagickFalse)
4035 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4036 assert(exception != (ExceptionInfo *) NULL);
4037 assert(exception->signature == MagickSignature);
4038 width=GetOptimalKernelWidth2D(radius,sigma);
4039 kernel=(double *) AcquireQuantumMemory((size_t) width*width,sizeof(*kernel));
4040 if (kernel == (double *) NULL)
4041 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
4043 j=(ssize_t) width/2;
4045 for (v=(-j); v <= j; v++)
4047 for (u=(-j); u <= j; u++)
4049 kernel[i]=(double) (-exp(-((double) u*u+v*v)/(2.0*MagickSigma*
4050 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
4051 normalize+=kernel[i];
4055 kernel[i/2]=(double) ((-2.0)*normalize);
4056 sharp_image=ConvolveImage(image,width,kernel,exception);
4057 kernel=(double *) RelinquishMagickMemory(kernel);
4058 return(sharp_image);
4062 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4066 % S p r e a d I m a g e %
4070 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4072 % SpreadImage() is a special effects method that randomly displaces each
4073 % pixel in a block defined by the radius parameter.
4075 % The format of the SpreadImage method is:
4077 % Image *SpreadImage(const Image *image,const double radius,
4078 % ExceptionInfo *exception)
4080 % A description of each parameter follows:
4082 % o image: the image.
4084 % o radius: Choose a random pixel in a neighborhood of this extent.
4086 % o exception: return any errors or warnings in this structure.
4089 MagickExport Image *SpreadImage(const Image *image,const double radius,
4090 ExceptionInfo *exception)
4092 #define SpreadImageTag "Spread/Image"
4111 **restrict random_info;
4120 Initialize spread image attributes.
4122 assert(image != (Image *) NULL);
4123 assert(image->signature == MagickSignature);
4124 if (image->debug != MagickFalse)
4125 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4126 assert(exception != (ExceptionInfo *) NULL);
4127 assert(exception->signature == MagickSignature);
4128 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
4130 if (spread_image == (Image *) NULL)
4131 return((Image *) NULL);
4132 if (SetImageStorageClass(spread_image,DirectClass) == MagickFalse)
4134 InheritException(exception,&spread_image->exception);
4135 spread_image=DestroyImage(spread_image);
4136 return((Image *) NULL);
4143 GetPixelInfo(spread_image,&bias);
4144 width=GetOptimalKernelWidth1D(radius,0.5);
4145 random_info=AcquireRandomInfoThreadSet();
4146 image_view=AcquireCacheView(image);
4147 spread_view=AcquireCacheView(spread_image);
4148 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4149 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
4151 for (y=0; y < (ssize_t) spread_image->rows; y++)
4154 id = GetOpenMPThreadId();
4165 if (status == MagickFalse)
4167 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
4169 if (q == (const Quantum *) NULL)
4175 for (x=0; x < (ssize_t) spread_image->columns; x++)
4177 (void) InterpolatePixelInfo(image,image_view,
4178 UndefinedInterpolatePixel,(double) x+width*(GetPseudoRandomValue(
4179 random_info[id])-0.5),(double) y+width*(GetPseudoRandomValue(
4180 random_info[id])-0.5),&pixel,exception);
4181 SetPixelPixelInfo(spread_image,&pixel,q);
4182 q+=GetPixelComponents(spread_image);
4184 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
4186 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4191 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4192 #pragma omp critical (MagickCore_SpreadImage)
4194 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
4195 if (proceed == MagickFalse)
4199 spread_view=DestroyCacheView(spread_view);
4200 image_view=DestroyCacheView(image_view);
4201 random_info=DestroyRandomInfoThreadSet(random_info);
4202 return(spread_image);
4206 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4210 % S t a t i s t i c I m a g e %
4214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4216 % StatisticImage() makes each pixel the min / max / median / mode / etc. of
4217 % the neighborhood of the specified width and height.
4219 % The format of the StatisticImage method is:
4221 % Image *StatisticImage(const Image *image,const StatisticType type,
4222 % const size_t width,const size_t height,ExceptionInfo *exception)
4224 % A description of each parameter follows:
4226 % o image: the image.
4228 % o type: the statistic type (median, mode, etc.).
4230 % o width: the width of the pixel neighborhood.
4232 % o height: the height of the pixel neighborhood.
4234 % o exception: return any errors or warnings in this structure.
4238 #define ListChannels 5
4240 typedef struct _ListNode
4248 typedef struct _SkipList
4257 typedef struct _PixelList
4265 lists[ListChannels];
4268 static PixelList *DestroyPixelList(PixelList *pixel_list)
4273 if (pixel_list == (PixelList *) NULL)
4274 return((PixelList *) NULL);
4275 for (i=0; i < ListChannels; i++)
4276 if (pixel_list->lists[i].nodes != (ListNode *) NULL)
4277 pixel_list->lists[i].nodes=(ListNode *) RelinquishMagickMemory(
4278 pixel_list->lists[i].nodes);
4279 pixel_list=(PixelList *) RelinquishMagickMemory(pixel_list);
4283 static PixelList **DestroyPixelListThreadSet(PixelList **pixel_list)
4288 assert(pixel_list != (PixelList **) NULL);
4289 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
4290 if (pixel_list[i] != (PixelList *) NULL)
4291 pixel_list[i]=DestroyPixelList(pixel_list[i]);
4292 pixel_list=(PixelList **) RelinquishMagickMemory(pixel_list);
4296 static PixelList *AcquirePixelList(const size_t width,const size_t height)
4304 pixel_list=(PixelList *) AcquireMagickMemory(sizeof(*pixel_list));
4305 if (pixel_list == (PixelList *) NULL)
4307 (void) ResetMagickMemory((void *) pixel_list,0,sizeof(*pixel_list));
4308 pixel_list->length=width*height;
4309 for (i=0; i < ListChannels; i++)
4311 pixel_list->lists[i].nodes=(ListNode *) AcquireQuantumMemory(65537UL,
4312 sizeof(*pixel_list->lists[i].nodes));
4313 if (pixel_list->lists[i].nodes == (ListNode *) NULL)
4314 return(DestroyPixelList(pixel_list));
4315 (void) ResetMagickMemory(pixel_list->lists[i].nodes,0,65537UL*
4316 sizeof(*pixel_list->lists[i].nodes));
4318 pixel_list->signature=MagickSignature;
4322 static PixelList **AcquirePixelListThreadSet(const size_t width,
4323 const size_t height)
4334 number_threads=GetOpenMPMaximumThreads();
4335 pixel_list=(PixelList **) AcquireQuantumMemory(number_threads,
4336 sizeof(*pixel_list));
4337 if (pixel_list == (PixelList **) NULL)
4338 return((PixelList **) NULL);
4339 (void) ResetMagickMemory(pixel_list,0,number_threads*sizeof(*pixel_list));
4340 for (i=0; i < (ssize_t) number_threads; i++)
4342 pixel_list[i]=AcquirePixelList(width,height);
4343 if (pixel_list[i] == (PixelList *) NULL)
4344 return(DestroyPixelListThreadSet(pixel_list));
4349 static void AddNodePixelList(PixelList *pixel_list,const ssize_t channel,
4363 Initialize the node.
4365 list=pixel_list->lists+channel;
4366 list->nodes[color].signature=pixel_list->signature;
4367 list->nodes[color].count=1;
4369 Determine where it belongs in the list.
4372 for (level=list->level; level >= 0; level--)
4374 while (list->nodes[search].next[level] < color)
4375 search=list->nodes[search].next[level];
4376 update[level]=search;
4379 Generate a pseudo-random level for this node.
4381 for (level=0; ; level++)
4383 pixel_list->seed=(pixel_list->seed*42893621L)+1L;
4384 if ((pixel_list->seed & 0x300) != 0x300)
4389 if (level > (list->level+2))
4390 level=list->level+2;
4392 If we're raising the list's level, link back to the root node.
4394 while (level > list->level)
4397 update[list->level]=65536UL;
4400 Link the node into the skip-list.
4404 list->nodes[color].next[level]=list->nodes[update[level]].next[level];
4405 list->nodes[update[level]].next[level]=color;
4406 } while (level-- > 0);
4409 static PixelInfo GetMaximumPixelList(PixelList *pixel_list)
4428 channels[ListChannels];
4431 Find the maximum value for each of the color.
4433 for (channel=0; channel < 5; channel++)
4435 list=pixel_list->lists+channel;
4438 maximum=list->nodes[color].next[0];
4441 color=list->nodes[color].next[0];
4442 if (color > maximum)
4444 count+=list->nodes[color].count;
4445 } while (count < (ssize_t) pixel_list->length);
4446 channels[channel]=(unsigned short) maximum;
4448 GetPixelInfo((const Image *) NULL,&pixel);
4449 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4450 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4451 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
4452 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
4453 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4457 static PixelInfo GetMeanPixelList(PixelList *pixel_list)
4478 channels[ListChannels];
4481 Find the mean value for each of the color.
4483 for (channel=0; channel < 5; channel++)
4485 list=pixel_list->lists+channel;
4491 color=list->nodes[color].next[0];
4492 sum+=(MagickRealType) list->nodes[color].count*color;
4493 count+=list->nodes[color].count;
4494 } while (count < (ssize_t) pixel_list->length);
4495 sum/=pixel_list->length;
4496 channels[channel]=(unsigned short) sum;
4498 GetPixelInfo((const Image *) NULL,&pixel);
4499 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4500 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4501 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
4502 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4503 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
4507 static PixelInfo GetMedianPixelList(PixelList *pixel_list)
4525 channels[ListChannels];
4528 Find the median value for each of the color.
4530 for (channel=0; channel < 5; channel++)
4532 list=pixel_list->lists+channel;
4537 color=list->nodes[color].next[0];
4538 count+=list->nodes[color].count;
4539 } while (count <= (ssize_t) (pixel_list->length >> 1));
4540 channels[channel]=(unsigned short) color;
4542 GetPixelInfo((const Image *) NULL,&pixel);
4543 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4544 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4545 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
4546 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4547 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
4551 static PixelInfo GetMinimumPixelList(PixelList *pixel_list)
4570 channels[ListChannels];
4573 Find the minimum value for each of the color.
4575 for (channel=0; channel < 5; channel++)
4577 list=pixel_list->lists+channel;
4580 minimum=list->nodes[color].next[0];
4583 color=list->nodes[color].next[0];
4584 if (color < minimum)
4586 count+=list->nodes[color].count;
4587 } while (count < (ssize_t) pixel_list->length);
4588 channels[channel]=(unsigned short) minimum;
4590 GetPixelInfo((const Image *) NULL,&pixel);
4591 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4592 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4593 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
4594 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4595 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
4599 static PixelInfo GetModePixelList(PixelList *pixel_list)
4622 Make each pixel the 'predominant color' of the specified neighborhood.
4624 for (channel=0; channel < 5; channel++)
4626 list=pixel_list->lists+channel;
4629 max_count=list->nodes[mode].count;
4633 color=list->nodes[color].next[0];
4634 if (list->nodes[color].count > max_count)
4637 max_count=list->nodes[mode].count;
4639 count+=list->nodes[color].count;
4640 } while (count < (ssize_t) pixel_list->length);
4641 channels[channel]=(unsigned short) mode;
4643 GetPixelInfo((const Image *) NULL,&pixel);
4644 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4645 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4646 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
4647 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4648 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
4652 static PixelInfo GetNonpeakPixelList(PixelList *pixel_list)
4675 Finds the non peak value for each of the colors.
4677 for (channel=0; channel < 5; channel++)
4679 list=pixel_list->lists+channel;
4681 next=list->nodes[color].next[0];
4687 next=list->nodes[color].next[0];
4688 count+=list->nodes[color].count;
4689 } while (count <= (ssize_t) (pixel_list->length >> 1));
4690 if ((previous == 65536UL) && (next != 65536UL))
4693 if ((previous != 65536UL) && (next == 65536UL))
4695 channels[channel]=(unsigned short) color;
4697 GetPixelInfo((const Image *) NULL,&pixel);
4698 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4699 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4700 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
4701 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
4702 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4706 static PixelInfo GetStandardDeviationPixelList(PixelList *pixel_list)
4728 channels[ListChannels];
4731 Find the standard-deviation value for each of the color.
4733 for (channel=0; channel < 5; channel++)
4735 list=pixel_list->lists+channel;
4745 color=list->nodes[color].next[0];
4746 sum+=(MagickRealType) list->nodes[color].count*color;
4747 for (i=0; i < (ssize_t) list->nodes[color].count; i++)
4748 sum_squared+=((MagickRealType) color)*((MagickRealType) color);
4749 count+=list->nodes[color].count;
4750 } while (count < (ssize_t) pixel_list->length);
4751 sum/=pixel_list->length;
4752 sum_squared/=pixel_list->length;
4753 channels[channel]=(unsigned short) sqrt(sum_squared-(sum*sum));
4755 GetPixelInfo((const Image *) NULL,&pixel);
4756 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4757 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4758 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
4759 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
4760 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4764 static inline void InsertPixelList(const Image *image,const Quantum *pixel,
4765 PixelList *pixel_list)
4773 index=ScaleQuantumToShort(GetPixelRed(image,pixel));
4774 signature=pixel_list->lists[0].nodes[index].signature;
4775 if (signature == pixel_list->signature)
4776 pixel_list->lists[0].nodes[index].count++;
4778 AddNodePixelList(pixel_list,0,index);
4779 index=ScaleQuantumToShort(GetPixelGreen(image,pixel));
4780 signature=pixel_list->lists[1].nodes[index].signature;
4781 if (signature == pixel_list->signature)
4782 pixel_list->lists[1].nodes[index].count++;
4784 AddNodePixelList(pixel_list,1,index);
4785 index=ScaleQuantumToShort(GetPixelBlue(image,pixel));
4786 signature=pixel_list->lists[2].nodes[index].signature;
4787 if (signature == pixel_list->signature)
4788 pixel_list->lists[2].nodes[index].count++;
4790 AddNodePixelList(pixel_list,2,index);
4791 index=ScaleQuantumToShort(GetPixelAlpha(image,pixel));
4792 signature=pixel_list->lists[3].nodes[index].signature;
4793 if (signature == pixel_list->signature)
4794 pixel_list->lists[3].nodes[index].count++;
4796 AddNodePixelList(pixel_list,3,index);
4797 if (image->colorspace == CMYKColorspace)
4798 index=ScaleQuantumToShort(GetPixelBlack(image,pixel));
4799 signature=pixel_list->lists[4].nodes[index].signature;
4800 if (signature == pixel_list->signature)
4801 pixel_list->lists[4].nodes[index].count++;
4803 AddNodePixelList(pixel_list,4,index);
4806 static inline MagickRealType MagickAbsoluteValue(const MagickRealType x)
4813 static void ResetPixelList(PixelList *pixel_list)
4828 Reset the skip-list.
4830 for (channel=0; channel < 5; channel++)
4832 list=pixel_list->lists+channel;
4833 root=list->nodes+65536UL;
4835 for (level=0; level < 9; level++)
4836 root->next[level]=65536UL;
4838 pixel_list->seed=pixel_list->signature++;
4841 MagickExport Image *StatisticImage(const Image *image,const StatisticType type,
4842 const size_t width,const size_t height,ExceptionInfo *exception)
4844 #define StatisticWidth \
4845 (width == 0 ? GetOptimalKernelWidth2D((double) width,0.5) : width)
4846 #define StatisticHeight \
4847 (height == 0 ? GetOptimalKernelWidth2D((double) height,0.5) : height)
4848 #define StatisticImageTag "Statistic/Image"
4864 **restrict pixel_list;
4870 Initialize statistics image attributes.
4872 assert(image != (Image *) NULL);
4873 assert(image->signature == MagickSignature);
4874 if (image->debug != MagickFalse)
4875 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4876 assert(exception != (ExceptionInfo *) NULL);
4877 assert(exception->signature == MagickSignature);
4878 statistic_image=CloneImage(image,image->columns,image->rows,MagickTrue,
4880 if (statistic_image == (Image *) NULL)
4881 return((Image *) NULL);
4882 if (SetImageStorageClass(statistic_image,DirectClass) == MagickFalse)
4884 InheritException(exception,&statistic_image->exception);
4885 statistic_image=DestroyImage(statistic_image);
4886 return((Image *) NULL);
4888 pixel_list=AcquirePixelListThreadSet(StatisticWidth,StatisticHeight);
4889 if (pixel_list == (PixelList **) NULL)
4891 statistic_image=DestroyImage(statistic_image);
4892 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
4895 Make each pixel the min / max / median / mode / etc. of the neighborhood.
4899 image_view=AcquireCacheView(image);
4900 statistic_view=AcquireCacheView(statistic_image);
4901 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4902 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
4904 for (y=0; y < (ssize_t) statistic_image->rows; y++)
4907 id = GetOpenMPThreadId();
4909 register const Quantum
4918 if (status == MagickFalse)
4920 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) StatisticWidth/2L),y-
4921 (ssize_t) (StatisticHeight/2L),image->columns+StatisticWidth,
4922 StatisticHeight,exception);
4923 q=QueueCacheViewAuthenticPixels(statistic_view,0,y,statistic_image->columns, 1,exception);
4924 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
4929 for (x=0; x < (ssize_t) statistic_image->columns; x++)
4934 register const Quantum
4942 ResetPixelList(pixel_list[id]);
4943 for (v=0; v < (ssize_t) StatisticHeight; v++)
4945 for (u=0; u < (ssize_t) StatisticWidth; u++)
4946 InsertPixelList(image,r+u*GetPixelComponents(image),pixel_list[id]);
4947 r+=(image->columns+StatisticWidth)*GetPixelComponents(image);
4949 GetPixelInfo(image,&pixel);
4950 SetPixelInfo(image,p+(StatisticWidth*StatisticHeight/2)*
4951 GetPixelComponents(image),&pixel);
4954 case GradientStatistic:
4960 minimum=GetMinimumPixelList(pixel_list[id]);
4961 maximum=GetMaximumPixelList(pixel_list[id]);
4962 pixel.red=MagickAbsoluteValue(maximum.red-minimum.red);
4963 pixel.green=MagickAbsoluteValue(maximum.green-minimum.green);
4964 pixel.blue=MagickAbsoluteValue(maximum.blue-minimum.blue);
4965 pixel.alpha=MagickAbsoluteValue(maximum.alpha-minimum.alpha);
4966 if (image->colorspace == CMYKColorspace)
4967 pixel.black=MagickAbsoluteValue(maximum.black-minimum.black);
4970 case MaximumStatistic:
4972 pixel=GetMaximumPixelList(pixel_list[id]);
4977 pixel=GetMeanPixelList(pixel_list[id]);
4980 case MedianStatistic:
4983 pixel=GetMedianPixelList(pixel_list[id]);
4986 case MinimumStatistic:
4988 pixel=GetMinimumPixelList(pixel_list[id]);
4993 pixel=GetModePixelList(pixel_list[id]);
4996 case NonpeakStatistic:
4998 pixel=GetNonpeakPixelList(pixel_list[id]);
5001 case StandardDeviationStatistic:
5003 pixel=GetStandardDeviationPixelList(pixel_list[id]);
5007 if ((GetPixelRedTraits(image) & ActivePixelTrait) != 0)
5008 SetPixelRed(statistic_image,ClampToQuantum(pixel.red),q);
5009 if ((GetPixelGreenTraits(image) & ActivePixelTrait) != 0)
5010 SetPixelGreen(statistic_image,ClampToQuantum(pixel.green),q);
5011 if ((GetPixelBlueTraits(image) & ActivePixelTrait) != 0)
5012 SetPixelBlue(statistic_image,ClampToQuantum(pixel.blue),q);
5013 if (((GetPixelBlackTraits(image) & ActivePixelTrait) != 0) &&
5014 (image->colorspace == CMYKColorspace))
5015 SetPixelBlack(statistic_image,ClampToQuantum(pixel.black),q);
5016 if (((GetPixelAlphaTraits(image) & ActivePixelTrait) != 0) &&
5017 (image->matte != MagickFalse))
5018 SetPixelAlpha(statistic_image,ClampToQuantum(pixel.alpha),q);
5019 p+=GetPixelComponents(image);
5020 q+=GetPixelComponents(statistic_image);
5022 if (SyncCacheViewAuthenticPixels(statistic_view,exception) == MagickFalse)
5024 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5029 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5030 #pragma omp critical (MagickCore_StatisticImage)
5032 proceed=SetImageProgress(image,StatisticImageTag,progress++,
5034 if (proceed == MagickFalse)
5038 statistic_view=DestroyCacheView(statistic_view);
5039 image_view=DestroyCacheView(image_view);
5040 pixel_list=DestroyPixelListThreadSet(pixel_list);
5041 return(statistic_image);
5045 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5049 % U n s h a r p M a s k I m a g e %
5053 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5055 % UnsharpMaskImage() sharpens one or more image channels. We convolve the
5056 % image with a Gaussian operator of the given radius and standard deviation
5057 % (sigma). For reasonable results, radius should be larger than sigma. Use a
5058 % radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
5060 % The format of the UnsharpMaskImage method is:
5062 % Image *UnsharpMaskImage(const Image *image,const double radius,
5063 % const double sigma,const double amount,const double threshold,
5064 % ExceptionInfo *exception)
5066 % A description of each parameter follows:
5068 % o image: the image.
5070 % o radius: the radius of the Gaussian, in pixels, not counting the center
5073 % o sigma: the standard deviation of the Gaussian, in pixels.
5075 % o amount: the percentage of the difference between the original and the
5076 % blur image that is added back into the original.
5078 % o threshold: the threshold in pixels needed to apply the diffence amount.
5080 % o exception: return any errors or warnings in this structure.
5083 MagickExport Image *UnsharpMaskImage(const Image *image,
5084 const double radius,const double sigma,const double amount,
5085 const double threshold,ExceptionInfo *exception)
5087 #define SharpenImageTag "Sharpen/Image"
5111 assert(image != (const Image *) NULL);
5112 assert(image->signature == MagickSignature);
5113 if (image->debug != MagickFalse)
5114 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5115 assert(exception != (ExceptionInfo *) NULL);
5116 unsharp_image=BlurImage(image,radius,sigma,exception);
5117 if (unsharp_image == (Image *) NULL)
5118 return((Image *) NULL);
5119 quantum_threshold=(MagickRealType) QuantumRange*threshold;
5125 GetPixelInfo(image,&bias);
5126 image_view=AcquireCacheView(image);
5127 unsharp_view=AcquireCacheView(unsharp_image);
5128 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5129 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
5131 for (y=0; y < (ssize_t) image->rows; y++)
5136 register const Quantum
5145 if (status == MagickFalse)
5147 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5148 q=GetCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
5150 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
5156 for (x=0; x < (ssize_t) image->columns; x++)
5158 if ((GetPixelRedTraits(image) & ActivePixelTrait) != 0)
5160 pixel.red=GetPixelRed(image,p)-(MagickRealType) GetPixelRed(image,q);
5161 if (fabs(2.0*pixel.red) < quantum_threshold)
5162 pixel.red=(MagickRealType) GetPixelRed(image,p);
5164 pixel.red=(MagickRealType) GetPixelRed(image,p)+(pixel.red*amount);
5165 SetPixelRed(unsharp_image,ClampToQuantum(pixel.red),q);
5167 if ((GetPixelGreenTraits(image) & ActivePixelTrait) != 0)
5169 pixel.green=GetPixelGreen(image,p)-
5170 (MagickRealType) GetPixelGreen(image,q);
5171 if (fabs(2.0*pixel.green) < quantum_threshold)
5172 pixel.green=(MagickRealType)
5173 GetPixelGreen(image,p);
5175 pixel.green=(MagickRealType)
5176 GetPixelGreen(image,p)+
5177 (pixel.green*amount);
5178 SetPixelGreen(unsharp_image,
5179 ClampToQuantum(pixel.green),q);
5181 if ((GetPixelBlueTraits(image) & ActivePixelTrait) != 0)
5183 pixel.blue=GetPixelBlue(image,p)-
5184 (MagickRealType) GetPixelBlue(image,q);
5185 if (fabs(2.0*pixel.blue) < quantum_threshold)
5186 pixel.blue=(MagickRealType)
5187 GetPixelBlue(image,p);
5189 pixel.blue=(MagickRealType)
5190 GetPixelBlue(image,p)+(pixel.blue*amount);
5191 SetPixelBlue(unsharp_image,
5192 ClampToQuantum(pixel.blue),q);
5194 if (((GetPixelBlackTraits(image) & ActivePixelTrait) != 0) &&
5195 (image->colorspace == CMYKColorspace))
5197 pixel.black=GetPixelBlack(image,p)-
5198 (MagickRealType) GetPixelBlack(image,q);
5199 if (fabs(2.0*pixel.black) < quantum_threshold)
5200 pixel.black=(MagickRealType)
5201 GetPixelBlack(image,p);
5203 pixel.black=(MagickRealType)
5204 GetPixelBlack(image,p)+(pixel.black*
5206 SetPixelBlack(unsharp_image,
5207 ClampToQuantum(pixel.black),q);
5209 if ((GetPixelAlphaTraits(image) & ActivePixelTrait) != 0)
5211 pixel.alpha=GetPixelAlpha(image,p)-
5212 (MagickRealType) GetPixelAlpha(image,q);
5213 if (fabs(2.0*pixel.alpha) < quantum_threshold)
5214 pixel.alpha=(MagickRealType)
5215 GetPixelAlpha(image,p);
5217 pixel.alpha=GetPixelAlpha(image,p)+
5218 (pixel.alpha*amount);
5219 SetPixelAlpha(unsharp_image,
5220 ClampToQuantum(pixel.alpha),q);
5222 p+=GetPixelComponents(image);
5223 q+=GetPixelComponents(unsharp_image);
5225 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
5227 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5232 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5233 #pragma omp critical (MagickCore_UnsharpMaskImage)
5235 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
5236 if (proceed == MagickFalse)
5240 unsharp_image->type=image->type;
5241 unsharp_view=DestroyCacheView(unsharp_view);
5242 image_view=DestroyCacheView(image_view);
5243 if (status == MagickFalse)
5244 unsharp_image=DestroyImage(unsharp_image);
5245 return(unsharp_image);