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,exception) == MagickFalse)
222 blur_image=DestroyImage(blur_image);
223 return((Image *) NULL);
226 Edge detect the image brighness channel, level, blur, and level again.
228 edge_image=EdgeImage(image,radius,exception);
229 if (edge_image == (Image *) NULL)
231 blur_image=DestroyImage(blur_image);
232 return((Image *) NULL);
234 (void) AdaptiveLevelImage(edge_image,"20%,95%");
235 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
236 if (gaussian_image != (Image *) NULL)
238 edge_image=DestroyImage(edge_image);
239 edge_image=gaussian_image;
241 (void) AdaptiveLevelImage(edge_image,"10%,95%");
243 Create a set of kernels from maximum (radius,sigma) to minimum.
245 width=GetOptimalKernelWidth2D(radius,sigma);
246 kernel=(double **) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
247 if (kernel == (double **) NULL)
249 edge_image=DestroyImage(edge_image);
250 blur_image=DestroyImage(blur_image);
251 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
253 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
254 for (i=0; i < (ssize_t) width; i+=2)
256 kernel[i]=(double *) AcquireQuantumMemory((size_t) (width-i),(width-i)*
258 if (kernel[i] == (double *) NULL)
261 j=(ssize_t) (width-i)/2;
263 for (v=(-j); v <= j; v++)
265 for (u=(-j); u <= j; u++)
267 kernel[i][k]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
268 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
269 normalize+=kernel[i][k];
273 if (fabs(normalize) <= MagickEpsilon)
275 normalize=1.0/normalize;
276 for (k=0; k < (j*j); k++)
277 kernel[i][k]=normalize*kernel[i][k];
279 if (i < (ssize_t) width)
281 for (i-=2; i >= 0; i-=2)
282 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
283 kernel=(double **) RelinquishMagickMemory(kernel);
284 edge_image=DestroyImage(edge_image);
285 blur_image=DestroyImage(blur_image);
286 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
289 Adaptively blur image.
293 GetPixelInfo(image,&bias);
294 SetPixelInfoBias(image,&bias);
295 image_view=AcquireCacheView(image);
296 edge_view=AcquireCacheView(edge_image);
297 blur_view=AcquireCacheView(blur_image);
298 #if defined(MAGICKCORE_OPENMP_SUPPORT)
299 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
301 for (y=0; y < (ssize_t) blur_image->rows; y++)
303 register const Quantum
313 if (status == MagickFalse)
315 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
316 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
318 if ((r == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
323 for (x=0; x < (ssize_t) blur_image->columns; x++)
332 register const double
341 i=(ssize_t) ceil((double) width*QuantumScale*
342 GetPixelIntensity(edge_image,r)-0.5);
346 if (i > (ssize_t) width)
350 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-i)/2L),y-
351 (ssize_t) ((width-i)/2L),width-i,width-i,exception);
352 if (p == (const Quantum *) NULL)
356 for (v=0; v < (ssize_t) (width-i); v++)
358 for (u=0; u < (ssize_t) (width-i); u++)
361 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
362 (image->matte != MagickFalse))
363 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,p));
364 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
365 pixel.red+=(*k)*alpha*GetPixelRed(image,p);
366 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
367 pixel.green+=(*k)*alpha*GetPixelGreen(image,p);
368 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
369 pixel.blue+=(*k)*alpha*GetPixelBlue(image,p);
370 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
371 (image->colorspace == CMYKColorspace))
372 pixel.black+=(*k)*alpha*GetPixelBlack(image,p);
373 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
374 pixel.alpha+=(*k)*GetPixelAlpha(image,p);
377 p+=GetPixelChannels(image);
380 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
381 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
382 SetPixelRed(blur_image,ClampToQuantum(gamma*pixel.red),q);
383 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
384 SetPixelGreen(blur_image,ClampToQuantum(gamma*pixel.green),q);
385 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
386 SetPixelBlue(blur_image,ClampToQuantum(gamma*pixel.blue),q);
387 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
388 (image->colorspace == CMYKColorspace))
389 SetPixelBlack(blur_image,ClampToQuantum(gamma*pixel.black),q);
390 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
391 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
392 q+=GetPixelChannels(blur_image);
393 r+=GetPixelChannels(edge_image);
395 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
397 if (image->progress_monitor != (MagickProgressMonitor) NULL)
402 #if defined(MAGICKCORE_OPENMP_SUPPORT)
403 #pragma omp critical (MagickCore_AdaptiveSharpenImage)
405 proceed=SetImageProgress(image,AdaptiveBlurImageTag,progress++,
407 if (proceed == MagickFalse)
411 blur_image->type=image->type;
412 blur_view=DestroyCacheView(blur_view);
413 edge_view=DestroyCacheView(edge_view);
414 image_view=DestroyCacheView(image_view);
415 edge_image=DestroyImage(edge_image);
416 for (i=0; i < (ssize_t) width; i+=2)
417 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
418 kernel=(double **) RelinquishMagickMemory(kernel);
419 if (status == MagickFalse)
420 blur_image=DestroyImage(blur_image);
425 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
429 % A d a p t i v e S h a r p e n I m a g e %
433 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
435 % AdaptiveSharpenImage() adaptively sharpens the image by sharpening more
436 % intensely near image edges and less intensely far from edges. We sharpen the
437 % image with a Gaussian operator of the given radius and standard deviation
438 % (sigma). For reasonable results, radius should be larger than sigma. Use a
439 % radius of 0 and AdaptiveSharpenImage() selects a suitable radius for you.
441 % The format of the AdaptiveSharpenImage method is:
443 % Image *AdaptiveSharpenImage(const Image *image,const double radius,
444 % const double sigma,ExceptionInfo *exception)
446 % A description of each parameter follows:
448 % o image: the image.
450 % o radius: the radius of the Gaussian, in pixels, not counting the center
453 % o sigma: the standard deviation of the Laplacian, in pixels.
455 % o exception: return any errors or warnings in this structure.
458 MagickExport Image *AdaptiveSharpenImage(const Image *image,const double radius,
459 const double sigma,ExceptionInfo *exception)
461 #define AdaptiveSharpenImageTag "Convolve/Image"
462 #define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
500 assert(image != (const Image *) NULL);
501 assert(image->signature == MagickSignature);
502 if (image->debug != MagickFalse)
503 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
504 assert(exception != (ExceptionInfo *) NULL);
505 assert(exception->signature == MagickSignature);
506 sharp_image=CloneImage(image,0,0,MagickTrue,exception);
507 if (sharp_image == (Image *) NULL)
508 return((Image *) NULL);
509 if (fabs(sigma) <= MagickEpsilon)
511 if (SetImageStorageClass(sharp_image,DirectClass,exception) == MagickFalse)
513 sharp_image=DestroyImage(sharp_image);
514 return((Image *) NULL);
517 Edge detect the image brighness channel, level, sharp, and level again.
519 edge_image=EdgeImage(image,radius,exception);
520 if (edge_image == (Image *) NULL)
522 sharp_image=DestroyImage(sharp_image);
523 return((Image *) NULL);
525 (void) AdaptiveLevelImage(edge_image,"20%,95%");
526 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
527 if (gaussian_image != (Image *) NULL)
529 edge_image=DestroyImage(edge_image);
530 edge_image=gaussian_image;
532 (void) AdaptiveLevelImage(edge_image,"10%,95%");
534 Create a set of kernels from maximum (radius,sigma) to minimum.
536 width=GetOptimalKernelWidth2D(radius,sigma);
537 kernel=(double **) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
538 if (kernel == (double **) NULL)
540 edge_image=DestroyImage(edge_image);
541 sharp_image=DestroyImage(sharp_image);
542 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
544 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
545 for (i=0; i < (ssize_t) width; i+=2)
547 kernel[i]=(double *) AcquireQuantumMemory((size_t) (width-i),(width-i)*
549 if (kernel[i] == (double *) NULL)
552 j=(ssize_t) (width-i)/2;
554 for (v=(-j); v <= j; v++)
556 for (u=(-j); u <= j; u++)
558 kernel[i][k]=(double) (-exp(-((double) u*u+v*v)/(2.0*MagickSigma*
559 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
560 normalize+=kernel[i][k];
564 if (fabs(normalize) <= MagickEpsilon)
566 normalize=1.0/normalize;
567 for (k=0; k < (j*j); k++)
568 kernel[i][k]=normalize*kernel[i][k];
570 if (i < (ssize_t) width)
572 for (i-=2; i >= 0; i-=2)
573 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
574 kernel=(double **) RelinquishMagickMemory(kernel);
575 edge_image=DestroyImage(edge_image);
576 sharp_image=DestroyImage(sharp_image);
577 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
580 Adaptively sharpen image.
584 GetPixelInfo(image,&bias);
585 SetPixelInfoBias(image,&bias);
586 image_view=AcquireCacheView(image);
587 edge_view=AcquireCacheView(edge_image);
588 sharp_view=AcquireCacheView(sharp_image);
589 #if defined(MAGICKCORE_OPENMP_SUPPORT)
590 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
592 for (y=0; y < (ssize_t) sharp_image->rows; y++)
594 register const Quantum
604 if (status == MagickFalse)
606 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
607 q=QueueCacheViewAuthenticPixels(sharp_view,0,y,sharp_image->columns,1,
609 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
614 for (x=0; x < (ssize_t) sharp_image->columns; x++)
623 register const double
632 i=(ssize_t) ceil((double) width*QuantumScale*
633 GetPixelIntensity(edge_image,r)-0.5);
637 if (i > (ssize_t) width)
641 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-i)/2L),y-
642 (ssize_t) ((width-i)/2L),width-i,width-i,exception);
643 if (p == (const Quantum *) NULL)
647 for (v=0; v < (ssize_t) (width-i); v++)
649 for (u=0; u < (ssize_t) (width-i); u++)
652 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
653 (image->matte != MagickFalse))
654 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,p));
655 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
656 pixel.red+=(*k)*alpha*GetPixelRed(image,p);
657 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
658 pixel.green+=(*k)*alpha*GetPixelGreen(image,p);
659 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
660 pixel.blue+=(*k)*alpha*GetPixelBlue(image,p);
661 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
662 (image->colorspace == CMYKColorspace))
663 pixel.black+=(*k)*alpha*GetPixelBlack(image,p);
664 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
665 pixel.alpha+=(*k)*GetPixelAlpha(image,p);
668 p+=GetPixelChannels(image);
671 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
672 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
673 SetPixelRed(sharp_image,ClampToQuantum(gamma*pixel.red),q);
674 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
675 SetPixelGreen(sharp_image,ClampToQuantum(gamma*pixel.green),q);
676 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
677 SetPixelBlue(sharp_image,ClampToQuantum(gamma*pixel.blue),q);
678 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
679 (image->colorspace == CMYKColorspace))
680 SetPixelBlack(sharp_image,ClampToQuantum(gamma*pixel.black),q);
681 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
682 SetPixelAlpha(sharp_image,ClampToQuantum(pixel.alpha),q);
683 q+=GetPixelChannels(sharp_image);
684 r+=GetPixelChannels(edge_image);
686 if (SyncCacheViewAuthenticPixels(sharp_view,exception) == MagickFalse)
688 if (image->progress_monitor != (MagickProgressMonitor) NULL)
693 #if defined(MAGICKCORE_OPENMP_SUPPORT)
694 #pragma omp critical (MagickCore_AdaptiveSharpenImage)
696 proceed=SetImageProgress(image,AdaptiveSharpenImageTag,progress++,
698 if (proceed == MagickFalse)
702 sharp_image->type=image->type;
703 sharp_view=DestroyCacheView(sharp_view);
704 edge_view=DestroyCacheView(edge_view);
705 image_view=DestroyCacheView(image_view);
706 edge_image=DestroyImage(edge_image);
707 for (i=0; i < (ssize_t) width; i+=2)
708 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
709 kernel=(double **) RelinquishMagickMemory(kernel);
710 if (status == MagickFalse)
711 sharp_image=DestroyImage(sharp_image);
716 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
720 % B l u r I m a g e %
724 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
726 % BlurImage() blurs an image. We convolve the image with a Gaussian operator
727 % of the given radius and standard deviation (sigma). For reasonable results,
728 % the radius should be larger than sigma. Use a radius of 0 and BlurImage()
729 % selects a suitable radius for you.
731 % BlurImage() differs from GaussianBlurImage() in that it uses a separable
732 % kernel which is faster but mathematically equivalent to the non-separable
735 % The format of the BlurImage method is:
737 % Image *BlurImage(const Image *image,const double radius,
738 % const double sigma,ExceptionInfo *exception)
740 % A description of each parameter follows:
742 % o image: the image.
744 % o radius: the radius of the Gaussian, in pixels, not counting the center
747 % o sigma: the standard deviation of the Gaussian, in pixels.
749 % o exception: return any errors or warnings in this structure.
753 static double *GetBlurKernel(const size_t width,const double sigma)
767 Generate a 1-D convolution kernel.
769 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
770 kernel=(double *) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
771 if (kernel == (double *) NULL)
776 for (k=(-j); k <= j; k++)
778 kernel[i]=(double) (exp(-((double) k*k)/(2.0*MagickSigma*MagickSigma))/
779 (MagickSQ2PI*MagickSigma));
780 normalize+=kernel[i];
783 for (i=0; i < (ssize_t) width; i++)
784 kernel[i]/=normalize;
788 MagickExport Image *BlurImage(const Image *image,const double radius,
789 const double sigma,ExceptionInfo *exception)
791 #define BlurImageTag "Blur/Image"
823 Initialize blur image attributes.
825 assert(image != (Image *) NULL);
826 assert(image->signature == MagickSignature);
827 if (image->debug != MagickFalse)
828 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
829 assert(exception != (ExceptionInfo *) NULL);
830 assert(exception->signature == MagickSignature);
831 blur_image=CloneImage(image,0,0,MagickTrue,exception);
832 if (blur_image == (Image *) NULL)
833 return((Image *) NULL);
834 if (fabs(sigma) <= MagickEpsilon)
836 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
838 blur_image=DestroyImage(blur_image);
839 return((Image *) NULL);
841 width=GetOptimalKernelWidth1D(radius,sigma);
842 kernel=GetBlurKernel(width,sigma);
843 if (kernel == (double *) NULL)
845 blur_image=DestroyImage(blur_image);
846 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
848 if (image->debug != MagickFalse)
851 format[MaxTextExtent],
854 register const double
857 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
858 " BlurImage with %.20g kernel:",(double) width);
859 message=AcquireString("");
861 for (i=0; i < (ssize_t) width; i++)
864 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) i);
865 (void) ConcatenateString(&message,format);
866 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
867 (void) ConcatenateString(&message,format);
868 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
870 message=DestroyString(message);
877 GetPixelInfo(image,&bias);
878 SetPixelInfoBias(image,&bias);
879 image_view=AcquireCacheView(image);
880 blur_view=AcquireCacheView(blur_image);
881 #if defined(MAGICKCORE_OPENMP_SUPPORT)
882 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
884 for (y=0; y < (ssize_t) blur_image->rows; y++)
886 register const Quantum
895 if (status == MagickFalse)
897 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y,
898 image->columns+width,1,exception);
899 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
901 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
906 for (x=0; x < (ssize_t) blur_image->columns; x++)
911 register const double
914 register const Quantum
915 *restrict kernel_pixels;
923 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) == 0) ||
924 (image->matte == MagickFalse))
926 for (i=0; i < (ssize_t) width; i++)
928 pixel.red+=(*k)*GetPixelRed(image,kernel_pixels);
929 pixel.green+=(*k)*GetPixelGreen(image,kernel_pixels);
930 pixel.blue+=(*k)*GetPixelBlue(image,kernel_pixels);
931 if (image->colorspace == CMYKColorspace)
932 pixel.black+=(*k)*GetPixelBlack(image,kernel_pixels);
934 kernel_pixels+=GetPixelChannels(image);
936 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
937 SetPixelRed(blur_image,ClampToQuantum(pixel.red),q);
938 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
939 SetPixelGreen(blur_image,ClampToQuantum(pixel.green),q);
940 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
941 SetPixelBlue(blur_image,ClampToQuantum(pixel.blue),q);
942 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
943 (blur_image->colorspace == CMYKColorspace))
944 SetPixelBlack(blur_image,ClampToQuantum(pixel.black),q);
945 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
949 for (i=0; i < (ssize_t) width; i++)
951 pixel.alpha+=(*k)*GetPixelAlpha(image,kernel_pixels);
953 kernel_pixels+=GetPixelChannels(image);
955 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
965 for (i=0; i < (ssize_t) width; i++)
967 alpha=(MagickRealType) (QuantumScale*
968 GetPixelAlpha(image,kernel_pixels));
969 pixel.red+=(*k)*alpha*GetPixelRed(image,kernel_pixels);
970 pixel.green+=(*k)*alpha*GetPixelGreen(image,kernel_pixels);
971 pixel.blue+=(*k)*alpha*GetPixelBlue(image,kernel_pixels);
972 if (image->colorspace == CMYKColorspace)
973 pixel.black+=(*k)*alpha*GetPixelBlack(image,kernel_pixels);
976 kernel_pixels+=GetPixelChannels(image);
978 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
979 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
980 SetPixelRed(blur_image,ClampToQuantum(gamma*pixel.red),q);
981 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
982 SetPixelGreen(blur_image,ClampToQuantum(gamma*pixel.green),q);
983 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
984 SetPixelBlue(blur_image,ClampToQuantum(gamma*pixel.blue),q);
985 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
986 (blur_image->colorspace == CMYKColorspace))
987 SetPixelBlack(blur_image,ClampToQuantum(gamma*pixel.black),q);
988 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
992 for (i=0; i < (ssize_t) width; i++)
994 pixel.alpha+=(*k)*GetPixelAlpha(image,kernel_pixels);
996 kernel_pixels+=GetPixelChannels(image);
998 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
1001 p+=GetPixelChannels(image);
1002 q+=GetPixelChannels(blur_image);
1004 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1006 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1011 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1012 #pragma omp critical (MagickCore_BlurImage)
1014 proceed=SetImageProgress(image,BlurImageTag,progress++,blur_image->rows+
1015 blur_image->columns);
1016 if (proceed == MagickFalse)
1020 blur_view=DestroyCacheView(blur_view);
1021 image_view=DestroyCacheView(image_view);
1025 image_view=AcquireCacheView(blur_image);
1026 blur_view=AcquireCacheView(blur_image);
1027 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1028 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1030 for (x=0; x < (ssize_t) blur_image->columns; x++)
1032 register const Quantum
1041 if (status == MagickFalse)
1043 p=GetCacheViewVirtualPixels(image_view,x,-((ssize_t) width/2L),1,
1044 image->rows+width,exception);
1045 q=GetCacheViewAuthenticPixels(blur_view,x,0,1,blur_image->rows,exception);
1046 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1051 for (y=0; y < (ssize_t) blur_image->rows; y++)
1056 register const double
1059 register const Quantum
1060 *restrict kernel_pixels;
1068 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) == 0) ||
1069 (blur_image->matte == MagickFalse))
1071 for (i=0; i < (ssize_t) width; i++)
1073 pixel.red+=(*k)*GetPixelRed(blur_image,kernel_pixels);
1074 pixel.green+=(*k)*GetPixelGreen(blur_image,kernel_pixels);
1075 pixel.blue+=(*k)*GetPixelBlue(blur_image,kernel_pixels);
1076 if (blur_image->colorspace == CMYKColorspace)
1077 pixel.black+=(*k)*GetPixelBlack(blur_image,kernel_pixels);
1079 kernel_pixels+=GetPixelChannels(blur_image);
1081 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1082 SetPixelRed(blur_image,ClampToQuantum(pixel.red),q);
1083 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1084 SetPixelGreen(blur_image,ClampToQuantum(pixel.green),q);
1085 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1086 SetPixelBlue(blur_image,ClampToQuantum(pixel.blue),q);
1087 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
1088 (blur_image->colorspace == CMYKColorspace))
1089 SetPixelBlack(blur_image,ClampToQuantum(pixel.black),q);
1090 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1094 for (i=0; i < (ssize_t) width; i++)
1096 pixel.alpha+=(*k)*GetPixelAlpha(blur_image,kernel_pixels);
1098 kernel_pixels+=GetPixelChannels(blur_image);
1100 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
1110 for (i=0; i < (ssize_t) width; i++)
1112 alpha=(MagickRealType) (QuantumScale*
1113 GetPixelAlpha(blur_image,kernel_pixels));
1114 pixel.red+=(*k)*alpha*GetPixelRed(blur_image,kernel_pixels);
1115 pixel.green+=(*k)*alpha*GetPixelGreen(blur_image,kernel_pixels);
1116 pixel.blue+=(*k)*alpha*GetPixelBlue(blur_image,kernel_pixels);
1117 if (blur_image->colorspace == CMYKColorspace)
1118 pixel.black+=(*k)*alpha*GetPixelBlack(blur_image,kernel_pixels);
1121 kernel_pixels+=GetPixelChannels(blur_image);
1123 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1124 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
1125 SetPixelRed(blur_image,ClampToQuantum(gamma*pixel.red),q);
1126 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
1127 SetPixelGreen(blur_image,ClampToQuantum(gamma*pixel.green),q);
1128 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
1129 SetPixelBlue(blur_image,ClampToQuantum(gamma*pixel.blue),q);
1130 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
1131 (blur_image->colorspace == CMYKColorspace))
1132 SetPixelBlack(blur_image,ClampToQuantum(gamma*pixel.black),q);
1133 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
1137 for (i=0; i < (ssize_t) width; i++)
1139 pixel.alpha+=(*k)*GetPixelAlpha(blur_image,kernel_pixels);
1141 kernel_pixels+=GetPixelChannels(blur_image);
1143 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
1146 p+=GetPixelChannels(blur_image);
1147 q+=GetPixelChannels(blur_image);
1149 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1151 if (blur_image->progress_monitor != (MagickProgressMonitor) NULL)
1156 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1157 #pragma omp critical (MagickCore_BlurImage)
1159 proceed=SetImageProgress(blur_image,BlurImageTag,progress++,
1160 blur_image->rows+blur_image->columns);
1161 if (proceed == MagickFalse)
1165 blur_view=DestroyCacheView(blur_view);
1166 image_view=DestroyCacheView(image_view);
1167 kernel=(double *) RelinquishMagickMemory(kernel);
1168 if (status == MagickFalse)
1169 blur_image=DestroyImage(blur_image);
1170 blur_image->type=image->type;
1175 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1179 % C o n v o l v e I m a g e %
1183 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1185 % ConvolveImage() applies a custom convolution kernel to the image.
1187 % The format of the ConvolveImage method is:
1189 % Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
1190 % ExceptionInfo *exception)
1192 % A description of each parameter follows:
1194 % o image: the image.
1196 % o kernel: the filtering kernel.
1198 % o exception: return any errors or warnings in this structure.
1201 MagickExport Image *ConvolveImage(const Image *image,
1202 const KernelInfo *kernel_info,ExceptionInfo *exception)
1204 #define ConvolveImageTag "Convolve/Image"
1224 Initialize convolve image attributes.
1226 assert(image != (Image *) NULL);
1227 assert(image->signature == MagickSignature);
1228 if (image->debug != MagickFalse)
1229 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1230 assert(exception != (ExceptionInfo *) NULL);
1231 assert(exception->signature == MagickSignature);
1232 if ((kernel_info->width % 2) == 0)
1233 ThrowImageException(OptionError,"KernelWidthMustBeAnOddNumber");
1234 convolve_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1236 if (convolve_image == (Image *) NULL)
1237 return((Image *) NULL);
1238 if (SetImageStorageClass(convolve_image,DirectClass,exception) == MagickFalse)
1240 convolve_image=DestroyImage(convolve_image);
1241 return((Image *) NULL);
1243 if (image->debug != MagickFalse)
1246 format[MaxTextExtent],
1249 register const double
1258 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1259 " ConvolveImage with %.20gx%.20g kernel:",(double) kernel_info->width,
1260 (double) kernel_info->height);
1261 message=AcquireString("");
1262 k=kernel_info->values;
1263 for (v=0; v < (ssize_t) kernel_info->width; v++)
1266 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
1267 (void) ConcatenateString(&message,format);
1268 for (u=0; u < (ssize_t) kernel_info->height; u++)
1270 (void) FormatLocaleString(format,MaxTextExtent,"%g ",*k++);
1271 (void) ConcatenateString(&message,format);
1273 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
1275 message=DestroyString(message);
1280 center=(ssize_t) GetPixelChannels(image)*(image->columns+kernel_info->width)*
1281 (kernel_info->height/2L)+GetPixelChannels(image)*(kernel_info->width/2);
1284 image_view=AcquireCacheView(image);
1285 convolve_view=AcquireCacheView(convolve_image);
1286 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1287 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1289 for (y=0; y < (ssize_t) image->rows; y++)
1291 register const Quantum
1300 if (status == MagickFalse)
1302 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) kernel_info->width/2L),y-
1303 (ssize_t) (kernel_info->height/2L),image->columns+kernel_info->width,
1304 kernel_info->height,exception);
1305 q=QueueCacheViewAuthenticPixels(convolve_view,0,y,convolve_image->columns,1,
1307 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1312 for (x=0; x < (ssize_t) image->columns; x++)
1317 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1331 register const double
1334 register const Quantum
1343 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1344 if (traits == UndefinedPixelTrait)
1346 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
1347 convolve_traits=GetPixelChannelMapTraits(convolve_image,channel);
1348 if (convolve_traits == UndefinedPixelTrait)
1350 if ((convolve_traits & CopyPixelTrait) != 0)
1352 q[channel]=p[center+i];
1355 k=kernel_info->values;
1357 pixel=kernel_info->bias;
1358 if ((convolve_traits & BlendPixelTrait) == 0)
1363 for (v=0; v < (ssize_t) kernel_info->height; v++)
1365 for (u=0; u < (ssize_t) kernel_info->width; u++)
1367 pixel+=(*k)*pixels[i];
1369 pixels+=GetPixelChannels(image);
1371 pixels+=image->columns*GetPixelChannels(image);
1373 q[channel]=ClampToQuantum(pixel);
1380 for (v=0; v < (ssize_t) kernel_info->height; v++)
1382 for (u=0; u < (ssize_t) kernel_info->width; u++)
1384 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixels));
1385 pixel+=(*k)*alpha*pixels[i];
1388 pixels+=GetPixelChannels(image);
1390 pixels+=image->columns*GetPixelChannels(image);
1392 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1393 q[channel]=ClampToQuantum(gamma*pixel);
1395 p+=GetPixelChannels(image);
1396 q+=GetPixelChannels(convolve_image);
1398 if (SyncCacheViewAuthenticPixels(convolve_view,exception) == MagickFalse)
1400 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1405 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1406 #pragma omp critical (MagickCore_ConvolveImage)
1408 proceed=SetImageProgress(image,ConvolveImageTag,progress++,image->rows);
1409 if (proceed == MagickFalse)
1413 convolve_image->type=image->type;
1414 convolve_view=DestroyCacheView(convolve_view);
1415 image_view=DestroyCacheView(image_view);
1416 if (status == MagickFalse)
1417 convolve_image=DestroyImage(convolve_image);
1418 return(convolve_image);
1422 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1426 % D e s p e c k l e I m a g e %
1430 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1432 % DespeckleImage() reduces the speckle noise in an image while perserving the
1433 % edges of the original image.
1435 % The format of the DespeckleImage method is:
1437 % Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1439 % A description of each parameter follows:
1441 % o image: the image.
1443 % o exception: return any errors or warnings in this structure.
1447 static void Hull(const ssize_t x_offset,const ssize_t y_offset,
1448 const size_t columns,const size_t rows,Quantum *f,Quantum *g,
1466 assert(f != (Quantum *) NULL);
1467 assert(g != (Quantum *) NULL);
1470 r=p+(y_offset*((ssize_t) columns+2)+x_offset);
1471 for (y=0; y < (ssize_t) rows; y++)
1477 for (x=(ssize_t) columns; x != 0; x--)
1479 v=(MagickRealType) (*p);
1480 if ((MagickRealType) *r >= (v+(MagickRealType) ScaleCharToQuantum(2)))
1481 v+=ScaleCharToQuantum(1);
1488 for (x=(ssize_t) columns; x != 0; x--)
1490 v=(MagickRealType) (*p);
1491 if ((MagickRealType) *r <= (v-(MagickRealType) ScaleCharToQuantum(2)))
1492 v-=(ssize_t) ScaleCharToQuantum(1);
1504 r=q+(y_offset*((ssize_t) columns+2)+x_offset);
1505 s=q-(y_offset*((ssize_t) columns+2)+x_offset);
1506 for (y=0; y < (ssize_t) rows; y++)
1513 for (x=(ssize_t) columns; x != 0; x--)
1515 v=(MagickRealType) (*q);
1516 if (((MagickRealType) *s >=
1517 (v+(MagickRealType) ScaleCharToQuantum(2))) &&
1518 ((MagickRealType) *r > v))
1519 v+=ScaleCharToQuantum(1);
1527 for (x=(ssize_t) columns; x != 0; x--)
1529 v=(MagickRealType) (*q);
1530 if (((MagickRealType) *s <=
1531 (v-(MagickRealType) ScaleCharToQuantum(2))) &&
1532 ((MagickRealType) *r < v))
1533 v-=(MagickRealType) ScaleCharToQuantum(1);
1547 MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1549 #define DespeckleImageTag "Despeckle/Image"
1572 static const ssize_t
1573 X[4] = {0, 1, 1,-1},
1574 Y[4] = {1, 0, 1, 1};
1577 Allocate despeckled image.
1579 assert(image != (const Image *) NULL);
1580 assert(image->signature == MagickSignature);
1581 if (image->debug != MagickFalse)
1582 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1583 assert(exception != (ExceptionInfo *) NULL);
1584 assert(exception->signature == MagickSignature);
1585 despeckle_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1587 if (despeckle_image == (Image *) NULL)
1588 return((Image *) NULL);
1589 if (SetImageStorageClass(despeckle_image,DirectClass,exception) == MagickFalse)
1591 despeckle_image=DestroyImage(despeckle_image);
1592 return((Image *) NULL);
1595 Allocate image buffers.
1597 length=(size_t) ((image->columns+2)*(image->rows+2));
1598 pixels=(Quantum *) AcquireQuantumMemory(length,2*sizeof(*pixels));
1599 buffers=(Quantum *) AcquireQuantumMemory(length,2*sizeof(*pixels));
1600 if ((pixels == (Quantum *) NULL) || (buffers == (Quantum *) NULL))
1602 if (buffers != (Quantum *) NULL)
1603 buffers=(Quantum *) RelinquishMagickMemory(buffers);
1604 if (pixels != (Quantum *) NULL)
1605 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1606 despeckle_image=DestroyImage(despeckle_image);
1607 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1610 Reduce speckle in the image.
1613 number_channels=(size_t) (image->colorspace == CMYKColorspace ? 5 : 4);
1614 image_view=AcquireCacheView(image);
1615 despeckle_view=AcquireCacheView(despeckle_image);
1616 for (i=0; i < (ssize_t) number_channels; i++)
1630 if (status == MagickFalse)
1633 (void) ResetMagickMemory(pixel,0,length*sizeof(*pixel));
1635 j=(ssize_t) image->columns+2;
1636 for (y=0; y < (ssize_t) image->rows; y++)
1638 register const Quantum
1641 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1642 if (p == (const Quantum *) NULL)
1645 for (x=0; x < (ssize_t) image->columns; x++)
1649 case 0: pixel[j]=GetPixelRed(image,p); break;
1650 case 1: pixel[j]=GetPixelGreen(image,p); break;
1651 case 2: pixel[j]=GetPixelBlue(image,p); break;
1652 case 3: pixel[j]=GetPixelAlpha(image,p); break;
1653 case 4: pixel[j]=GetPixelBlack(image,p); break;
1656 p+=GetPixelChannels(image);
1661 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
1662 for (k=0; k < 4; k++)
1664 Hull(X[k],Y[k],image->columns,image->rows,pixel,buffer,1);
1665 Hull(-X[k],-Y[k],image->columns,image->rows,pixel,buffer,1);
1666 Hull(-X[k],-Y[k],image->columns,image->rows,pixel,buffer,-1);
1667 Hull(X[k],Y[k],image->columns,image->rows,pixel,buffer,-1);
1669 j=(ssize_t) image->columns+2;
1670 for (y=0; y < (ssize_t) image->rows; y++)
1678 q=GetCacheViewAuthenticPixels(despeckle_view,0,y,despeckle_image->columns,
1680 if (q == (const Quantum *) NULL)
1683 for (x=0; x < (ssize_t) image->columns; x++)
1687 case 0: SetPixelRed(despeckle_image,pixel[j],q); break;
1688 case 1: SetPixelGreen(despeckle_image,pixel[j],q); break;
1689 case 2: SetPixelBlue(despeckle_image,pixel[j],q); break;
1690 case 3: SetPixelAlpha(despeckle_image,pixel[j],q); break;
1691 case 4: SetPixelBlack(despeckle_image,pixel[j],q); break;
1694 q+=GetPixelChannels(despeckle_image);
1697 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1698 if (sync == MagickFalse)
1705 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1710 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1712 if (proceed == MagickFalse)
1716 despeckle_view=DestroyCacheView(despeckle_view);
1717 image_view=DestroyCacheView(image_view);
1718 buffers=(Quantum *) RelinquishMagickMemory(buffers);
1719 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1720 despeckle_image->type=image->type;
1721 if (status == MagickFalse)
1722 despeckle_image=DestroyImage(despeckle_image);
1723 return(despeckle_image);
1727 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1731 % E d g e I m a g e %
1735 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1737 % EdgeImage() finds edges in an image. Radius defines the radius of the
1738 % convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1741 % The format of the EdgeImage method is:
1743 % Image *EdgeImage(const Image *image,const double radius,
1744 % ExceptionInfo *exception)
1746 % A description of each parameter follows:
1748 % o image: the image.
1750 % o radius: the radius of the pixel neighborhood.
1752 % o exception: return any errors or warnings in this structure.
1755 MagickExport Image *EdgeImage(const Image *image,const double radius,
1756 ExceptionInfo *exception)
1775 assert(image != (const Image *) NULL);
1776 assert(image->signature == MagickSignature);
1777 if (image->debug != MagickFalse)
1778 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1779 assert(exception != (ExceptionInfo *) NULL);
1780 assert(exception->signature == MagickSignature);
1781 width=GetOptimalKernelWidth1D(radius,0.5);
1782 kernel_info=AcquireKernelInfo((const char *) NULL);
1783 if (kernel_info == (KernelInfo *) NULL)
1784 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1785 kernel_info->width=width;
1786 kernel_info->height=width;
1787 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
1788 kernel_info->width*sizeof(*kernel_info->values));
1789 if (kernel_info->values == (double *) NULL)
1791 kernel_info=DestroyKernelInfo(kernel_info);
1792 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1794 j=(ssize_t) kernel_info->width/2;
1796 for (v=(-j); v <= j; v++)
1798 for (u=(-j); u <= j; u++)
1800 kernel_info->values[i]=(-1.0);
1804 kernel_info->values[i/2]=(double) (width*width-1.0);
1805 kernel_info->bias=image->bias;
1806 edge_image=ConvolveImage(image,kernel_info,exception);
1807 kernel_info=DestroyKernelInfo(kernel_info);
1812 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1816 % E m b o s s I m a g e %
1820 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1822 % EmbossImage() returns a grayscale image with a three-dimensional effect.
1823 % We convolve the image with a Gaussian operator of the given radius and
1824 % standard deviation (sigma). For reasonable results, radius should be
1825 % larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1828 % The format of the EmbossImage method is:
1830 % Image *EmbossImage(const Image *image,const double radius,
1831 % const double sigma,ExceptionInfo *exception)
1833 % A description of each parameter follows:
1835 % o image: the image.
1837 % o radius: the radius of the pixel neighborhood.
1839 % o sigma: the standard deviation of the Gaussian, in pixels.
1841 % o exception: return any errors or warnings in this structure.
1844 MagickExport Image *EmbossImage(const Image *image,const double radius,
1845 const double sigma,ExceptionInfo *exception)
1865 assert(image != (const Image *) NULL);
1866 assert(image->signature == MagickSignature);
1867 if (image->debug != MagickFalse)
1868 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1869 assert(exception != (ExceptionInfo *) NULL);
1870 assert(exception->signature == MagickSignature);
1871 width=GetOptimalKernelWidth2D(radius,sigma);
1872 kernel_info=AcquireKernelInfo((const char *) NULL);
1873 if (kernel_info == (KernelInfo *) NULL)
1874 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1875 kernel_info->width=width;
1876 kernel_info->height=width;
1877 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
1878 kernel_info->width*sizeof(*kernel_info->values));
1879 if (kernel_info->values == (double *) NULL)
1881 kernel_info=DestroyKernelInfo(kernel_info);
1882 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1884 j=(ssize_t) kernel_info->width/2;
1887 for (v=(-j); v <= j; v++)
1889 for (u=(-j); u <= j; u++)
1891 kernel_info->values[i]=(double) (((u < 0) || (v < 0) ? -8.0 : 8.0)*
1892 exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
1893 (2.0*MagickPI*MagickSigma*MagickSigma));
1895 kernel_info->values[i]=0.0;
1900 kernel_info->bias=image->bias;
1901 emboss_image=ConvolveImage(image,kernel_info,exception);
1902 kernel_info=DestroyKernelInfo(kernel_info);
1903 if (emboss_image != (Image *) NULL)
1904 (void) EqualizeImage(emboss_image);
1905 return(emboss_image);
1909 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1913 % G a u s s i a n B l u r I m a g e %
1917 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1919 % GaussianBlurImage() blurs an image. We convolve the image with a
1920 % Gaussian operator of the given radius and standard deviation (sigma).
1921 % For reasonable results, the radius should be larger than sigma. Use a
1922 % radius of 0 and GaussianBlurImage() selects a suitable radius for you
1924 % The format of the GaussianBlurImage method is:
1926 % Image *GaussianBlurImage(const Image *image,onst double radius,
1927 % const double sigma,ExceptionInfo *exception)
1929 % A description of each parameter follows:
1931 % o image: the image.
1933 % o radius: the radius of the Gaussian, in pixels, not counting the center
1936 % o sigma: the standard deviation of the Gaussian, in pixels.
1938 % o exception: return any errors or warnings in this structure.
1941 MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
1942 const double sigma,ExceptionInfo *exception)
1961 assert(image != (const Image *) NULL);
1962 assert(image->signature == MagickSignature);
1963 if (image->debug != MagickFalse)
1964 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1965 assert(exception != (ExceptionInfo *) NULL);
1966 assert(exception->signature == MagickSignature);
1967 width=GetOptimalKernelWidth2D(radius,sigma);
1968 kernel_info=AcquireKernelInfo((const char *) NULL);
1969 if (kernel_info == (KernelInfo *) NULL)
1970 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1971 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1972 kernel_info->width=width;
1973 kernel_info->height=width;
1974 kernel_info->signature=MagickSignature;
1975 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
1976 kernel_info->width*sizeof(*kernel_info->values));
1977 if (kernel_info->values == (double *) NULL)
1979 kernel_info=DestroyKernelInfo(kernel_info);
1980 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1982 j=(ssize_t) kernel_info->width/2;
1984 for (v=(-j); v <= j; v++)
1986 for (u=(-j); u <= j; u++)
1988 kernel_info->values[i]=(double) (exp(-((double) u*u+v*v)/(2.0*
1989 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
1993 kernel_info->bias=image->bias;
1994 blur_image=ConvolveImage(image,kernel_info,exception);
1995 kernel_info=DestroyKernelInfo(kernel_info);
2000 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2004 % M o t i o n B l u r I m a g e %
2008 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2010 % MotionBlurImage() simulates motion blur. We convolve the image with a
2011 % Gaussian operator of the given radius and standard deviation (sigma).
2012 % For reasonable results, radius should be larger than sigma. Use a
2013 % radius of 0 and MotionBlurImage() selects a suitable radius for you.
2014 % Angle gives the angle of the blurring motion.
2016 % Andrew Protano contributed this effect.
2018 % The format of the MotionBlurImage method is:
2020 % Image *MotionBlurImage(const Image *image,const double radius,
2021 % const double sigma,const double angle,ExceptionInfo *exception)
2023 % A description of each parameter follows:
2025 % o image: the image.
2027 % o radius: the radius of the Gaussian, in pixels, not counting
2030 % o sigma: the standard deviation of the Gaussian, in pixels.
2032 % o angle: Apply the effect along this angle.
2034 % o exception: return any errors or warnings in this structure.
2038 static double *GetMotionBlurKernel(const size_t width,const double sigma)
2048 Generate a 1-D convolution kernel.
2050 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2051 kernel=(double *) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
2052 if (kernel == (double *) NULL)
2055 for (i=0; i < (ssize_t) width; i++)
2057 kernel[i]=(double) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
2058 MagickSigma)))/(MagickSQ2PI*MagickSigma));
2059 normalize+=kernel[i];
2061 for (i=0; i < (ssize_t) width; i++)
2062 kernel[i]/=normalize;
2066 MagickExport Image *MotionBlurImage(const Image *image,
2067 const double radius,const double sigma,const double angle,
2068 ExceptionInfo *exception)
2104 assert(image != (Image *) NULL);
2105 assert(image->signature == MagickSignature);
2106 if (image->debug != MagickFalse)
2107 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2108 assert(exception != (ExceptionInfo *) NULL);
2109 width=GetOptimalKernelWidth1D(radius,sigma);
2110 kernel=GetMotionBlurKernel(width,sigma);
2111 if (kernel == (double *) NULL)
2112 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2113 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
2114 if (offset == (OffsetInfo *) NULL)
2116 kernel=(double *) RelinquishMagickMemory(kernel);
2117 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2119 blur_image=CloneImage(image,0,0,MagickTrue,exception);
2120 if (blur_image == (Image *) NULL)
2122 kernel=(double *) RelinquishMagickMemory(kernel);
2123 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2124 return((Image *) NULL);
2126 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2128 kernel=(double *) RelinquishMagickMemory(kernel);
2129 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2130 blur_image=DestroyImage(blur_image);
2131 return((Image *) NULL);
2133 point.x=(double) width*sin(DegreesToRadians(angle));
2134 point.y=(double) width*cos(DegreesToRadians(angle));
2135 for (i=0; i < (ssize_t) width; i++)
2137 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
2138 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
2145 GetPixelInfo(image,&bias);
2146 image_view=AcquireCacheView(image);
2147 blur_view=AcquireCacheView(blur_image);
2148 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2149 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
2151 for (y=0; y < (ssize_t) image->rows; y++)
2159 if (status == MagickFalse)
2161 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2163 if (q == (const Quantum *) NULL)
2168 for (x=0; x < (ssize_t) image->columns; x++)
2184 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) == 0) || (image->matte == MagickFalse))
2186 for (i=0; i < (ssize_t) width; i++)
2188 (void) GetOneCacheViewVirtualPixel(image_view,x+offset[i].x,y+
2189 offset[i].y,&pixel,exception);
2190 qixel.red+=(*k)*pixel.red;
2191 qixel.green+=(*k)*pixel.green;
2192 qixel.blue+=(*k)*pixel.blue;
2193 qixel.alpha+=(*k)*pixel.alpha;
2194 if (image->colorspace == CMYKColorspace)
2195 qixel.black+=(*k)*pixel.black;
2198 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2199 SetPixelRed(blur_image,
2200 ClampToQuantum(qixel.red),q);
2201 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2202 SetPixelGreen(blur_image,
2203 ClampToQuantum(qixel.green),q);
2204 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2205 SetPixelBlue(blur_image,
2206 ClampToQuantum(qixel.blue),q);
2207 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
2208 (image->colorspace == CMYKColorspace))
2209 SetPixelBlack(blur_image,
2210 ClampToQuantum(qixel.black),q);
2211 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
2212 SetPixelAlpha(blur_image,
2213 ClampToQuantum(qixel.alpha),q);
2223 for (i=0; i < (ssize_t) width; i++)
2225 (void) GetOneCacheViewVirtualPixel(image_view,x+offset[i].x,y+
2226 offset[i].y,&pixel,exception);
2227 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
2228 qixel.red+=(*k)*alpha*pixel.red;
2229 qixel.green+=(*k)*alpha*pixel.green;
2230 qixel.blue+=(*k)*alpha*pixel.blue;
2231 qixel.alpha+=(*k)*pixel.alpha;
2232 if (image->colorspace == CMYKColorspace)
2233 qixel.black+=(*k)*alpha*pixel.black;
2237 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
2238 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2239 SetPixelRed(blur_image,
2240 ClampToQuantum(gamma*qixel.red),q);
2241 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2242 SetPixelGreen(blur_image,
2243 ClampToQuantum(gamma*qixel.green),q);
2244 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2245 SetPixelBlue(blur_image,
2246 ClampToQuantum(gamma*qixel.blue),q);
2247 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
2248 (image->colorspace == CMYKColorspace))
2249 SetPixelBlack(blur_image,
2250 ClampToQuantum(gamma*qixel.black),q);
2251 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
2252 SetPixelAlpha(blur_image,
2253 ClampToQuantum(qixel.alpha),q);
2255 q+=GetPixelChannels(blur_image);
2257 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2259 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2264 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2265 #pragma omp critical (MagickCore_MotionBlurImage)
2267 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2268 if (proceed == MagickFalse)
2272 blur_view=DestroyCacheView(blur_view);
2273 image_view=DestroyCacheView(image_view);
2274 kernel=(double *) RelinquishMagickMemory(kernel);
2275 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2276 if (status == MagickFalse)
2277 blur_image=DestroyImage(blur_image);
2282 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2286 % P r e v i e w I m a g e %
2290 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2292 % PreviewImage() tiles 9 thumbnails of the specified image with an image
2293 % processing operation applied with varying parameters. This may be helpful
2294 % pin-pointing an appropriate parameter for a particular image processing
2297 % The format of the PreviewImages method is:
2299 % Image *PreviewImages(const Image *image,const PreviewType preview,
2300 % ExceptionInfo *exception)
2302 % A description of each parameter follows:
2304 % o image: the image.
2306 % o preview: the image processing operation.
2308 % o exception: return any errors or warnings in this structure.
2311 MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2312 ExceptionInfo *exception)
2314 #define NumberTiles 9
2315 #define PreviewImageTag "Preview/Image"
2316 #define DefaultPreviewGeometry "204x204+10+10"
2319 factor[MaxTextExtent],
2320 label[MaxTextExtent];
2362 Open output image file.
2364 assert(image != (Image *) NULL);
2365 assert(image->signature == MagickSignature);
2366 if (image->debug != MagickFalse)
2367 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2371 preview_info=AcquireImageInfo();
2372 SetGeometry(image,&geometry);
2373 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2374 &geometry.width,&geometry.height);
2375 images=NewImageList();
2377 GetQuantizeInfo(&quantize_info);
2383 for (i=0; i < NumberTiles; i++)
2385 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2386 if (thumbnail == (Image *) NULL)
2388 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2390 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel);
2391 if (i == (NumberTiles/2))
2393 (void) QueryColorDatabase("#dfdfdf",&thumbnail->matte_color,exception);
2394 AppendImageToList(&images,thumbnail);
2402 preview_image=RotateImage(thumbnail,degrees,exception);
2403 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
2409 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
2410 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",
2411 degrees,2.0*degrees);
2416 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2417 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
2418 preview_image=RollImage(thumbnail,x,y,exception);
2419 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
2420 (double) x,(double) y);
2425 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2426 if (preview_image == (Image *) NULL)
2428 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",
2430 (void) ModulateImage(preview_image,factor);
2431 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2434 case SaturationPreview:
2436 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2437 if (preview_image == (Image *) NULL)
2439 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",
2441 (void) ModulateImage(preview_image,factor);
2442 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2445 case BrightnessPreview:
2447 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2448 if (preview_image == (Image *) NULL)
2450 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
2451 (void) ModulateImage(preview_image,factor);
2452 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2458 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2459 if (preview_image == (Image *) NULL)
2462 (void) GammaImage(preview_image,gamma,exception);
2463 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
2468 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2469 if (preview_image != (Image *) NULL)
2470 for (x=0; x < i; x++)
2471 (void) ContrastImage(preview_image,MagickTrue);
2472 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
2478 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2479 if (preview_image == (Image *) NULL)
2481 for (x=0; x < i; x++)
2482 (void) ContrastImage(preview_image,MagickFalse);
2483 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
2487 case GrayscalePreview:
2489 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2490 if (preview_image == (Image *) NULL)
2493 quantize_info.number_colors=colors;
2494 quantize_info.colorspace=GRAYColorspace;
2495 (void) QuantizeImage(&quantize_info,preview_image);
2496 (void) FormatLocaleString(label,MaxTextExtent,
2497 "-colorspace gray -colors %.20g",(double) colors);
2500 case QuantizePreview:
2502 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2503 if (preview_image == (Image *) NULL)
2506 quantize_info.number_colors=colors;
2507 (void) QuantizeImage(&quantize_info,preview_image);
2508 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
2512 case DespecklePreview:
2514 for (x=0; x < (i-1); x++)
2516 preview_image=DespeckleImage(thumbnail,exception);
2517 if (preview_image == (Image *) NULL)
2519 thumbnail=DestroyImage(thumbnail);
2520 thumbnail=preview_image;
2522 preview_image=DespeckleImage(thumbnail,exception);
2523 if (preview_image == (Image *) NULL)
2525 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
2529 case ReduceNoisePreview:
2531 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2532 (size_t) radius,exception);
2533 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
2536 case AddNoisePreview:
2542 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2547 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2552 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2557 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2562 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2567 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2572 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2576 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2577 (size_t) i,exception);
2578 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
2581 case SharpenPreview:
2583 preview_image=SharpenImage(thumbnail,radius,sigma,exception);
2584 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",
2590 preview_image=BlurImage(thumbnail,radius,sigma,exception);
2591 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
2595 case ThresholdPreview:
2597 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2598 if (preview_image == (Image *) NULL)
2600 (void) BilevelImage(thumbnail,
2601 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
2602 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",
2603 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
2606 case EdgeDetectPreview:
2608 preview_image=EdgeImage(thumbnail,radius,exception);
2609 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
2614 preview_image=SpreadImage(thumbnail,radius,exception);
2615 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",
2619 case SolarizePreview:
2621 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2622 if (preview_image == (Image *) NULL)
2624 (void) SolarizeImage(preview_image,(double) QuantumRange*
2626 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
2627 (QuantumRange*percentage)/100.0);
2633 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2635 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",
2641 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2642 if (preview_image == (Image *) NULL)
2644 geometry.width=(size_t) (2*i+2);
2645 geometry.height=(size_t) (2*i+2);
2648 (void) RaiseImage(preview_image,&geometry,MagickTrue);
2649 (void) FormatLocaleString(label,MaxTextExtent,
2650 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
2651 geometry.height,(double) geometry.x,(double) geometry.y);
2654 case SegmentPreview:
2656 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2657 if (preview_image == (Image *) NULL)
2660 (void) SegmentImage(preview_image,RGBColorspace,MagickFalse,threshold,
2662 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
2663 threshold,threshold);
2668 preview_image=SwirlImage(thumbnail,degrees,exception);
2669 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
2673 case ImplodePreview:
2676 preview_image=ImplodeImage(thumbnail,degrees,exception);
2677 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
2683 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,exception);
2684 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",
2685 0.5*degrees,2.0*degrees);
2688 case OilPaintPreview:
2690 preview_image=OilPaintImage(thumbnail,(double) radius,exception);
2691 (void) FormatLocaleString(label,MaxTextExtent,"paint %g",radius);
2694 case CharcoalDrawingPreview:
2696 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
2698 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",
2705 filename[MaxTextExtent];
2713 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2714 if (preview_image == (Image *) NULL)
2716 preview_info->quality=(size_t) percentage;
2717 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
2718 preview_info->quality);
2719 file=AcquireUniqueFileResource(filename);
2722 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
2723 "jpeg:%s",filename);
2724 status=WriteImage(preview_info,preview_image);
2725 if (status != MagickFalse)
2730 (void) CopyMagickString(preview_info->filename,
2731 preview_image->filename,MaxTextExtent);
2732 quality_image=ReadImage(preview_info,exception);
2733 if (quality_image != (Image *) NULL)
2735 preview_image=DestroyImage(preview_image);
2736 preview_image=quality_image;
2739 (void) RelinquishUniqueFileResource(preview_image->filename);
2740 if ((GetBlobSize(preview_image)/1024) >= 1024)
2741 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
2742 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2745 if (GetBlobSize(preview_image) >= 1024)
2746 (void) FormatLocaleString(label,MaxTextExtent,
2747 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
2748 GetBlobSize(preview_image))/1024.0);
2750 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
2751 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
2755 thumbnail=DestroyImage(thumbnail);
2759 if (preview_image == (Image *) NULL)
2761 (void) DeleteImageProperty(preview_image,"label");
2762 (void) SetImageProperty(preview_image,"label",label);
2763 AppendImageToList(&images,preview_image);
2764 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2766 if (proceed == MagickFalse)
2769 if (images == (Image *) NULL)
2771 preview_info=DestroyImageInfo(preview_info);
2772 return((Image *) NULL);
2777 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2778 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2779 montage_info->shadow=MagickTrue;
2780 (void) CloneString(&montage_info->tile,"3x3");
2781 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2782 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2783 montage_image=MontageImages(images,montage_info,exception);
2784 montage_info=DestroyMontageInfo(montage_info);
2785 images=DestroyImageList(images);
2786 if (montage_image == (Image *) NULL)
2787 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2788 if (montage_image->montage != (char *) NULL)
2791 Free image directory.
2793 montage_image->montage=(char *) RelinquishMagickMemory(
2794 montage_image->montage);
2795 if (image->directory != (char *) NULL)
2796 montage_image->directory=(char *) RelinquishMagickMemory(
2797 montage_image->directory);
2799 preview_info=DestroyImageInfo(preview_info);
2800 return(montage_image);
2804 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2808 % R a d i a l B l u r I m a g e %
2812 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2814 % RadialBlurImage() applies a radial blur to the image.
2816 % Andrew Protano contributed this effect.
2818 % The format of the RadialBlurImage method is:
2820 % Image *RadialBlurImage(const Image *image,const double angle,
2821 % ExceptionInfo *exception)
2823 % A description of each parameter follows:
2825 % o image: the image.
2827 % o angle: the angle of the radial blur.
2829 % o exception: return any errors or warnings in this structure.
2832 MagickExport Image *RadialBlurImage(const Image *image,
2833 const double angle,ExceptionInfo *exception)
2871 Allocate blur image.
2873 assert(image != (Image *) NULL);
2874 assert(image->signature == MagickSignature);
2875 if (image->debug != MagickFalse)
2876 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2877 assert(exception != (ExceptionInfo *) NULL);
2878 assert(exception->signature == MagickSignature);
2879 blur_image=CloneImage(image,0,0,MagickTrue,exception);
2880 if (blur_image == (Image *) NULL)
2881 return((Image *) NULL);
2882 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2884 blur_image=DestroyImage(blur_image);
2885 return((Image *) NULL);
2887 blur_center.x=(double) image->columns/2.0;
2888 blur_center.y=(double) image->rows/2.0;
2889 blur_radius=hypot(blur_center.x,blur_center.y);
2890 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
2891 theta=DegreesToRadians(angle)/(MagickRealType) (n-1);
2892 cos_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2893 sizeof(*cos_theta));
2894 sin_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
2895 sizeof(*sin_theta));
2896 if ((cos_theta == (MagickRealType *) NULL) ||
2897 (sin_theta == (MagickRealType *) NULL))
2899 blur_image=DestroyImage(blur_image);
2900 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2902 offset=theta*(MagickRealType) (n-1)/2.0;
2903 for (i=0; i < (ssize_t) n; i++)
2905 cos_theta[i]=cos((double) (theta*i-offset));
2906 sin_theta[i]=sin((double) (theta*i-offset));
2913 GetPixelInfo(image,&bias);
2914 image_view=AcquireCacheView(image);
2915 blur_view=AcquireCacheView(blur_image);
2916 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2917 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2919 for (y=0; y < (ssize_t) blur_image->rows; y++)
2927 if (status == MagickFalse)
2929 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2931 if (q == (const Quantum *) NULL)
2936 for (x=0; x < (ssize_t) blur_image->columns; x++)
2957 center.x=(double) x-blur_center.x;
2958 center.y=(double) y-blur_center.y;
2959 radius=hypot((double) center.x,center.y);
2964 step=(size_t) (blur_radius/radius);
2973 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) == 0) || (image->matte == MagickFalse))
2975 for (i=0; i < (ssize_t) n; i+=(ssize_t) step)
2977 (void) GetOneCacheViewVirtualPixel(image_view,(ssize_t)
2978 (blur_center.x+center.x*cos_theta[i]-center.y*sin_theta[i]+0.5),
2979 (ssize_t) (blur_center.y+center.x*sin_theta[i]+center.y*
2980 cos_theta[i]+0.5),&pixel,exception);
2981 qixel.red+=pixel.red;
2982 qixel.green+=pixel.green;
2983 qixel.blue+=pixel.blue;
2984 if (image->colorspace == CMYKColorspace)
2985 qixel.black+=pixel.black;
2986 qixel.alpha+=pixel.alpha;
2989 normalize=1.0/(fabs((double) normalize) <= MagickEpsilon ? 1.0 :
2991 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
2992 SetPixelRed(blur_image,
2993 ClampToQuantum(normalize*qixel.red),q);
2994 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
2995 SetPixelGreen(blur_image,
2996 ClampToQuantum(normalize*qixel.green),q);
2997 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
2998 SetPixelBlue(blur_image,
2999 ClampToQuantum(normalize*qixel.blue),q);
3000 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
3001 (image->colorspace == CMYKColorspace))
3002 SetPixelBlack(blur_image,
3003 ClampToQuantum(normalize*qixel.black),q);
3004 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
3005 SetPixelAlpha(blur_image,
3006 ClampToQuantum(normalize*qixel.alpha),q);
3016 for (i=0; i < (ssize_t) n; i+=(ssize_t) step)
3018 (void) GetOneCacheViewVirtualPixel(image_view,(ssize_t)
3019 (blur_center.x+center.x*cos_theta[i]-center.y*sin_theta[i]+0.5),
3020 (ssize_t) (blur_center.y+center.x*sin_theta[i]+center.y*
3021 cos_theta[i]+0.5),&pixel,exception);
3022 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
3023 qixel.red+=alpha*pixel.red;
3024 qixel.green+=alpha*pixel.green;
3025 qixel.blue+=alpha*pixel.blue;
3026 qixel.alpha+=pixel.alpha;
3027 if (image->colorspace == CMYKColorspace)
3028 qixel.black+=alpha*pixel.black;
3032 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3033 normalize=1.0/(fabs((double) normalize) <= MagickEpsilon ? 1.0 :
3035 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3036 SetPixelRed(blur_image,
3037 ClampToQuantum(gamma*qixel.red),q);
3038 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3039 SetPixelGreen(blur_image,
3040 ClampToQuantum(gamma*qixel.green),q);
3041 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3042 SetPixelBlue(blur_image,
3043 ClampToQuantum(gamma*qixel.blue),q);
3044 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
3045 (image->colorspace == CMYKColorspace))
3046 SetPixelBlack(blur_image,
3047 ClampToQuantum(gamma*qixel.black),q);
3048 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
3049 SetPixelAlpha(blur_image,
3050 ClampToQuantum(normalize*qixel.alpha),q);
3052 q+=GetPixelChannels(blur_image);
3054 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
3056 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3061 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3062 #pragma omp critical (MagickCore_RadialBlurImage)
3064 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
3065 if (proceed == MagickFalse)
3069 blur_view=DestroyCacheView(blur_view);
3070 image_view=DestroyCacheView(image_view);
3071 cos_theta=(MagickRealType *) RelinquishMagickMemory(cos_theta);
3072 sin_theta=(MagickRealType *) RelinquishMagickMemory(sin_theta);
3073 if (status == MagickFalse)
3074 blur_image=DestroyImage(blur_image);
3079 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3083 % S e l e c t i v e B l u r I m a g e %
3087 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3089 % SelectiveBlurImage() selectively blur pixels within a contrast threshold.
3090 % It is similar to the unsharpen mask that sharpens everything with contrast
3091 % above a certain threshold.
3093 % The format of the SelectiveBlurImage method is:
3095 % Image *SelectiveBlurImage(const Image *image,const double radius,
3096 % const double sigma,const double threshold,ExceptionInfo *exception)
3098 % A description of each parameter follows:
3100 % o image: the image.
3102 % o radius: the radius of the Gaussian, in pixels, not counting the center
3105 % o sigma: the standard deviation of the Gaussian, in pixels.
3107 % o threshold: only pixels within this contrast threshold are included
3108 % in the blur operation.
3110 % o exception: return any errors or warnings in this structure.
3113 MagickExport Image *SelectiveBlurImage(const Image *image,
3114 const double radius,const double sigma,const double threshold,
3115 ExceptionInfo *exception)
3117 #define SelectiveBlurImageTag "SelectiveBlur/Image"
3151 Initialize blur image attributes.
3153 assert(image != (Image *) NULL);
3154 assert(image->signature == MagickSignature);
3155 if (image->debug != MagickFalse)
3156 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3157 assert(exception != (ExceptionInfo *) NULL);
3158 assert(exception->signature == MagickSignature);
3159 width=GetOptimalKernelWidth1D(radius,sigma);
3160 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
3161 if (kernel == (double *) NULL)
3162 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3163 j=(ssize_t) width/2;
3165 for (v=(-j); v <= j; v++)
3167 for (u=(-j); u <= j; u++)
3168 kernel[i++]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
3169 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3171 if (image->debug != MagickFalse)
3174 format[MaxTextExtent],
3177 register const double
3184 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
3185 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
3187 message=AcquireString("");
3189 for (v=0; v < (ssize_t) width; v++)
3192 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
3193 (void) ConcatenateString(&message,format);
3194 for (u=0; u < (ssize_t) width; u++)
3196 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",*k++);
3197 (void) ConcatenateString(&message,format);
3199 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
3201 message=DestroyString(message);
3203 blur_image=CloneImage(image,0,0,MagickTrue,exception);
3204 if (blur_image == (Image *) NULL)
3205 return((Image *) NULL);
3206 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
3208 blur_image=DestroyImage(blur_image);
3209 return((Image *) NULL);
3212 Threshold blur image.
3216 GetPixelInfo(image,&bias);
3217 SetPixelInfoBias(image,&bias);
3218 image_view=AcquireCacheView(image);
3219 blur_view=AcquireCacheView(blur_image);
3220 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3221 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3223 for (y=0; y < (ssize_t) image->rows; y++)
3234 register const Quantum
3243 if (status == MagickFalse)
3245 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
3246 (width/2L),image->columns+width,width,exception);
3247 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3249 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3254 for (x=0; x < (ssize_t) image->columns; x++)
3259 register const double
3273 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) == 0) || (image->matte == MagickFalse))
3275 for (v=0; v < (ssize_t) width; v++)
3277 for (u=0; u < (ssize_t) width; u++)
3279 contrast=GetPixelIntensity(image,p+(u+j)*GetPixelChannels(image))-
3280 (double) GetPixelIntensity(blur_image,q);
3281 if (fabs(contrast) < threshold)
3284 GetPixelRed(image,p+(u+j)*GetPixelChannels(image));
3286 GetPixelGreen(image,p+(u+j)*GetPixelChannels(image));
3288 GetPixelBlue(image,p+(u+j)*GetPixelChannels(image));
3289 if (image->colorspace == CMYKColorspace)
3291 GetPixelBlack(image,p+(u+j)*GetPixelChannels(image));
3296 j+=(ssize_t) (image->columns+width);
3300 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3301 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3302 SetPixelRed(blur_image,ClampToQuantum(gamma*pixel.red),q);
3303 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3304 SetPixelGreen(blur_image,ClampToQuantum(gamma*pixel.green),q);
3305 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3306 SetPixelBlue(blur_image,ClampToQuantum(gamma*pixel.blue),q);
3307 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
3308 (image->colorspace == CMYKColorspace))
3309 SetPixelBlack(blur_image,ClampToQuantum(gamma*pixel.black),q);
3311 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
3315 for (v=0; v < (ssize_t) width; v++)
3317 for (u=0; u < (ssize_t) width; u++)
3319 contrast=GetPixelIntensity(image,p+(u+j)*
3320 GetPixelChannels(image))-(double)
3321 GetPixelIntensity(blur_image,q);
3322 if (fabs(contrast) < threshold)
3325 GetPixelAlpha(image,p+(u+j)*GetPixelChannels(image));
3330 j+=(ssize_t) (image->columns+width);
3334 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
3336 SetPixelAlpha(blur_image,ClampToQuantum(gamma*pixel.alpha),q);
3345 for (v=0; v < (ssize_t) width; v++)
3347 for (u=0; u < (ssize_t) width; u++)
3349 contrast=GetPixelIntensity(image,p+(u+j)*
3350 GetPixelChannels(image))-(double)
3351 GetPixelIntensity(blur_image,q);
3352 if (fabs(contrast) < threshold)
3354 alpha=(MagickRealType) (QuantumScale*
3355 GetPixelAlpha(image,p+(u+j)*GetPixelChannels(image)));
3356 pixel.red+=(*k)*alpha*
3357 GetPixelRed(image,p+(u+j)*GetPixelChannels(image));
3358 pixel.green+=(*k)*alpha*GetPixelGreen(image,p+(u+j)*
3359 GetPixelChannels(image));
3360 pixel.blue+=(*k)*alpha*GetPixelBlue(image,p+(u+j)*
3361 GetPixelChannels(image));
3362 pixel.alpha+=(*k)*GetPixelAlpha(image,p+(u+j)*
3363 GetPixelChannels(image));
3364 if (image->colorspace == CMYKColorspace)
3365 pixel.black+=(*k)*GetPixelBlack(image,p+(u+j)*
3366 GetPixelChannels(image));
3371 j+=(ssize_t) (image->columns+width);
3375 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3376 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3377 SetPixelRed(blur_image,ClampToQuantum(gamma*pixel.red),q);
3378 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3379 SetPixelGreen(blur_image,ClampToQuantum(gamma*pixel.green),q);
3380 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3381 SetPixelBlue(blur_image,ClampToQuantum(gamma*pixel.blue),q);
3382 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
3383 (image->colorspace == CMYKColorspace))
3384 SetPixelBlack(blur_image,ClampToQuantum(gamma*pixel.black),q);
3386 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
3390 for (v=0; v < (ssize_t) width; v++)
3392 for (u=0; u < (ssize_t) width; u++)
3394 contrast=GetPixelIntensity(image,p+(u+j)*
3395 GetPixelChannels(image))-(double)
3396 GetPixelIntensity(blur_image,q);
3397 if (fabs(contrast) < threshold)
3400 GetPixelAlpha(image,p+(u+j)*GetPixelChannels(image));
3405 j+=(ssize_t) (image->columns+width);
3409 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
3411 SetPixelAlpha(blur_image,ClampToQuantum(pixel.alpha),q);
3415 p+=GetPixelChannels(image);
3416 q+=GetPixelChannels(blur_image);
3418 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3419 if (sync == MagickFalse)
3421 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3426 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3427 #pragma omp critical (MagickCore_SelectiveBlurImage)
3429 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3431 if (proceed == MagickFalse)
3435 blur_image->type=image->type;
3436 blur_view=DestroyCacheView(blur_view);
3437 image_view=DestroyCacheView(image_view);
3438 kernel=(double *) RelinquishMagickMemory(kernel);
3439 if (status == MagickFalse)
3440 blur_image=DestroyImage(blur_image);
3445 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3449 % S h a d e I m a g e %
3453 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3455 % ShadeImage() shines a distant light on an image to create a
3456 % three-dimensional effect. You control the positioning of the light with
3457 % azimuth and elevation; azimuth is measured in degrees off the x axis
3458 % and elevation is measured in pixels above the Z axis.
3460 % The format of the ShadeImage method is:
3462 % Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3463 % const double azimuth,const double elevation,ExceptionInfo *exception)
3465 % A description of each parameter follows:
3467 % o image: the image.
3469 % o gray: A value other than zero shades the intensity of each pixel.
3471 % o azimuth, elevation: Define the light source direction.
3473 % o exception: return any errors or warnings in this structure.
3476 MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3477 const double azimuth,const double elevation,ExceptionInfo *exception)
3479 #define ShadeImageTag "Shade/Image"
3501 Initialize shaded image attributes.
3503 assert(image != (const Image *) NULL);
3504 assert(image->signature == MagickSignature);
3505 if (image->debug != MagickFalse)
3506 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3507 assert(exception != (ExceptionInfo *) NULL);
3508 assert(exception->signature == MagickSignature);
3509 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3510 if (shade_image == (Image *) NULL)
3511 return((Image *) NULL);
3512 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
3514 shade_image=DestroyImage(shade_image);
3515 return((Image *) NULL);
3518 Compute the light vector.
3520 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3521 cos(DegreesToRadians(elevation));
3522 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3523 cos(DegreesToRadians(elevation));
3524 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3530 image_view=AcquireCacheView(image);
3531 shade_view=AcquireCacheView(shade_image);
3532 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3533 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3535 for (y=0; y < (ssize_t) image->rows; y++)
3545 register const Quantum
3557 if (status == MagickFalse)
3559 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
3560 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3562 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3568 Shade this row of pixels.
3570 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
3571 s0=p+GetPixelChannels(image);
3572 s1=s0+(image->columns+2)*GetPixelChannels(image);
3573 s2=s1+(image->columns+2)*GetPixelChannels(image);
3574 for (x=0; x < (ssize_t) image->columns; x++)
3577 Determine the surface normal and compute shading.
3579 normal.x=(double) (GetPixelIntensity(image,s0-GetPixelChannels(image))+
3580 GetPixelIntensity(image,s1-GetPixelChannels(image))+
3581 GetPixelIntensity(image,s2-GetPixelChannels(image))-
3582 GetPixelIntensity(image,s0+GetPixelChannels(image))-
3583 GetPixelIntensity(image,s1+GetPixelChannels(image))-
3584 GetPixelIntensity(image,s2+GetPixelChannels(image)));
3585 normal.y=(double) (GetPixelIntensity(image,s2-GetPixelChannels(image))+
3586 GetPixelIntensity(image,s2)+
3587 GetPixelIntensity(image,s2+GetPixelChannels(image))-
3588 GetPixelIntensity(image,s0-GetPixelChannels(image))-
3589 GetPixelIntensity(image,s0)-
3590 GetPixelIntensity(image,s0+GetPixelChannels(image)));
3591 if ((normal.x == 0.0) && (normal.y == 0.0))
3596 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3597 if (distance > MagickEpsilon)
3600 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
3601 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3602 shade=distance/sqrt((double) normal_distance);
3605 if (gray != MagickFalse)
3607 SetPixelRed(shade_image,ClampToQuantum(shade),q);
3608 SetPixelGreen(shade_image,ClampToQuantum(shade),q);
3609 SetPixelBlue(shade_image,ClampToQuantum(shade),q);
3613 SetPixelRed(shade_image,ClampToQuantum(QuantumScale*shade*
3614 GetPixelRed(image,s1)),q);
3615 SetPixelGreen(shade_image,ClampToQuantum(QuantumScale*shade*
3616 GetPixelGreen(image,s1)),q);
3617 SetPixelBlue(shade_image,ClampToQuantum(QuantumScale*shade*
3618 GetPixelBlue(image,s1)),q);
3620 SetPixelAlpha(shade_image,GetPixelAlpha(image,s1),q);
3621 s0+=GetPixelChannels(image);
3622 s1+=GetPixelChannels(image);
3623 s2+=GetPixelChannels(image);
3624 q+=GetPixelChannels(shade_image);
3626 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3628 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3633 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3634 #pragma omp critical (MagickCore_ShadeImage)
3636 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3637 if (proceed == MagickFalse)
3641 shade_view=DestroyCacheView(shade_view);
3642 image_view=DestroyCacheView(image_view);
3643 if (status == MagickFalse)
3644 shade_image=DestroyImage(shade_image);
3645 return(shade_image);
3649 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3653 % S h a r p e n I m a g e %
3657 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3659 % SharpenImage() sharpens the image. We convolve the image with a Gaussian
3660 % operator of the given radius and standard deviation (sigma). For
3661 % reasonable results, radius should be larger than sigma. Use a radius of 0
3662 % and SharpenImage() selects a suitable radius for you.
3664 % Using a separable kernel would be faster, but the negative weights cancel
3665 % out on the corners of the kernel producing often undesirable ringing in the
3666 % filtered result; this can be avoided by using a 2D gaussian shaped image
3667 % sharpening kernel instead.
3669 % The format of the SharpenImage method is:
3671 % Image *SharpenImage(const Image *image,const double radius,
3672 % const double sigma,ExceptionInfo *exception)
3674 % A description of each parameter follows:
3676 % o image: the image.
3678 % o radius: the radius of the Gaussian, in pixels, not counting the center
3681 % o sigma: the standard deviation of the Laplacian, in pixels.
3683 % o exception: return any errors or warnings in this structure.
3686 MagickExport Image *SharpenImage(const Image *image,const double radius,
3687 const double sigma,ExceptionInfo *exception)
3709 assert(image != (const Image *) NULL);
3710 assert(image->signature == MagickSignature);
3711 if (image->debug != MagickFalse)
3712 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3713 assert(exception != (ExceptionInfo *) NULL);
3714 assert(exception->signature == MagickSignature);
3715 width=GetOptimalKernelWidth2D(radius,sigma);
3716 kernel_info=AcquireKernelInfo((const char *) NULL);
3717 if (kernel_info == (KernelInfo *) NULL)
3718 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3719 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3720 kernel_info->width=width;
3721 kernel_info->height=width;
3722 kernel_info->signature=MagickSignature;
3723 kernel_info->values=(double *) AcquireAlignedMemory(kernel_info->width,
3724 kernel_info->width*sizeof(*kernel_info->values));
3725 if (kernel_info->values == (double *) NULL)
3727 kernel_info=DestroyKernelInfo(kernel_info);
3728 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3731 j=(ssize_t) kernel_info->width/2;
3733 for (v=(-j); v <= j; v++)
3735 for (u=(-j); u <= j; u++)
3737 kernel_info->values[i]=(double) (-exp(-((double) u*u+v*v)/(2.0*
3738 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3739 normalize+=kernel_info->values[i];
3743 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
3744 kernel_info->bias=image->bias;
3745 sharp_image=ConvolveImage(image,kernel_info,exception);
3746 kernel_info=DestroyKernelInfo(kernel_info);
3747 return(sharp_image);
3751 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3755 % S p r e a d I m a g e %
3759 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3761 % SpreadImage() is a special effects method that randomly displaces each
3762 % pixel in a block defined by the radius parameter.
3764 % The format of the SpreadImage method is:
3766 % Image *SpreadImage(const Image *image,const double radius,
3767 % ExceptionInfo *exception)
3769 % A description of each parameter follows:
3771 % o image: the image.
3773 % o radius: Choose a random pixel in a neighborhood of this extent.
3775 % o exception: return any errors or warnings in this structure.
3778 MagickExport Image *SpreadImage(const Image *image,const double radius,
3779 ExceptionInfo *exception)
3781 #define SpreadImageTag "Spread/Image"
3800 **restrict random_info;
3809 Initialize spread image attributes.
3811 assert(image != (Image *) NULL);
3812 assert(image->signature == MagickSignature);
3813 if (image->debug != MagickFalse)
3814 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3815 assert(exception != (ExceptionInfo *) NULL);
3816 assert(exception->signature == MagickSignature);
3817 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3819 if (spread_image == (Image *) NULL)
3820 return((Image *) NULL);
3821 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
3823 spread_image=DestroyImage(spread_image);
3824 return((Image *) NULL);
3831 GetPixelInfo(spread_image,&bias);
3832 width=GetOptimalKernelWidth1D(radius,0.5);
3833 random_info=AcquireRandomInfoThreadSet();
3834 image_view=AcquireCacheView(image);
3835 spread_view=AcquireCacheView(spread_image);
3836 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3837 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
3839 for (y=0; y < (ssize_t) spread_image->rows; y++)
3842 id = GetOpenMPThreadId();
3853 if (status == MagickFalse)
3855 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
3857 if (q == (const Quantum *) NULL)
3863 for (x=0; x < (ssize_t) spread_image->columns; x++)
3865 (void) InterpolatePixelInfo(image,image_view,
3866 UndefinedInterpolatePixel,(double) x+width*(GetPseudoRandomValue(
3867 random_info[id])-0.5),(double) y+width*(GetPseudoRandomValue(
3868 random_info[id])-0.5),&pixel,exception);
3869 SetPixelPixelInfo(spread_image,&pixel,q);
3870 q+=GetPixelChannels(spread_image);
3872 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
3874 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3879 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3880 #pragma omp critical (MagickCore_SpreadImage)
3882 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3883 if (proceed == MagickFalse)
3887 spread_view=DestroyCacheView(spread_view);
3888 image_view=DestroyCacheView(image_view);
3889 random_info=DestroyRandomInfoThreadSet(random_info);
3890 return(spread_image);
3894 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3898 % S t a t i s t i c I m a g e %
3902 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3904 % StatisticImage() makes each pixel the min / max / median / mode / etc. of
3905 % the neighborhood of the specified width and height.
3907 % The format of the StatisticImage method is:
3909 % Image *StatisticImage(const Image *image,const StatisticType type,
3910 % const size_t width,const size_t height,ExceptionInfo *exception)
3912 % A description of each parameter follows:
3914 % o image: the image.
3916 % o type: the statistic type (median, mode, etc.).
3918 % o width: the width of the pixel neighborhood.
3920 % o height: the height of the pixel neighborhood.
3922 % o exception: return any errors or warnings in this structure.
3926 #define ListChannels 5
3928 typedef struct _ListNode
3936 typedef struct _SkipList
3945 typedef struct _PixelList
3953 lists[ListChannels];
3956 static PixelList *DestroyPixelList(PixelList *pixel_list)
3961 if (pixel_list == (PixelList *) NULL)
3962 return((PixelList *) NULL);
3963 for (i=0; i < ListChannels; i++)
3964 if (pixel_list->lists[i].nodes != (ListNode *) NULL)
3965 pixel_list->lists[i].nodes=(ListNode *) RelinquishMagickMemory(
3966 pixel_list->lists[i].nodes);
3967 pixel_list=(PixelList *) RelinquishMagickMemory(pixel_list);
3971 static PixelList **DestroyPixelListThreadSet(PixelList **pixel_list)
3976 assert(pixel_list != (PixelList **) NULL);
3977 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
3978 if (pixel_list[i] != (PixelList *) NULL)
3979 pixel_list[i]=DestroyPixelList(pixel_list[i]);
3980 pixel_list=(PixelList **) RelinquishMagickMemory(pixel_list);
3984 static PixelList *AcquirePixelList(const size_t width,const size_t height)
3992 pixel_list=(PixelList *) AcquireMagickMemory(sizeof(*pixel_list));
3993 if (pixel_list == (PixelList *) NULL)
3995 (void) ResetMagickMemory((void *) pixel_list,0,sizeof(*pixel_list));
3996 pixel_list->length=width*height;
3997 for (i=0; i < ListChannels; i++)
3999 pixel_list->lists[i].nodes=(ListNode *) AcquireQuantumMemory(65537UL,
4000 sizeof(*pixel_list->lists[i].nodes));
4001 if (pixel_list->lists[i].nodes == (ListNode *) NULL)
4002 return(DestroyPixelList(pixel_list));
4003 (void) ResetMagickMemory(pixel_list->lists[i].nodes,0,65537UL*
4004 sizeof(*pixel_list->lists[i].nodes));
4006 pixel_list->signature=MagickSignature;
4010 static PixelList **AcquirePixelListThreadSet(const size_t width,
4011 const size_t height)
4022 number_threads=GetOpenMPMaximumThreads();
4023 pixel_list=(PixelList **) AcquireQuantumMemory(number_threads,
4024 sizeof(*pixel_list));
4025 if (pixel_list == (PixelList **) NULL)
4026 return((PixelList **) NULL);
4027 (void) ResetMagickMemory(pixel_list,0,number_threads*sizeof(*pixel_list));
4028 for (i=0; i < (ssize_t) number_threads; i++)
4030 pixel_list[i]=AcquirePixelList(width,height);
4031 if (pixel_list[i] == (PixelList *) NULL)
4032 return(DestroyPixelListThreadSet(pixel_list));
4037 static void AddNodePixelList(PixelList *pixel_list,const ssize_t channel,
4051 Initialize the node.
4053 list=pixel_list->lists+channel;
4054 list->nodes[color].signature=pixel_list->signature;
4055 list->nodes[color].count=1;
4057 Determine where it belongs in the list.
4060 for (level=list->level; level >= 0; level--)
4062 while (list->nodes[search].next[level] < color)
4063 search=list->nodes[search].next[level];
4064 update[level]=search;
4067 Generate a pseudo-random level for this node.
4069 for (level=0; ; level++)
4071 pixel_list->seed=(pixel_list->seed*42893621L)+1L;
4072 if ((pixel_list->seed & 0x300) != 0x300)
4077 if (level > (list->level+2))
4078 level=list->level+2;
4080 If we're raising the list's level, link back to the root node.
4082 while (level > list->level)
4085 update[list->level]=65536UL;
4088 Link the node into the skip-list.
4092 list->nodes[color].next[level]=list->nodes[update[level]].next[level];
4093 list->nodes[update[level]].next[level]=color;
4094 } while (level-- > 0);
4097 static PixelInfo GetMaximumPixelList(PixelList *pixel_list)
4116 channels[ListChannels];
4119 Find the maximum value for each of the color.
4121 for (channel=0; channel < 5; channel++)
4123 list=pixel_list->lists+channel;
4126 maximum=list->nodes[color].next[0];
4129 color=list->nodes[color].next[0];
4130 if (color > maximum)
4132 count+=list->nodes[color].count;
4133 } while (count < (ssize_t) pixel_list->length);
4134 channels[channel]=(unsigned short) maximum;
4136 GetPixelInfo((const Image *) NULL,&pixel);
4137 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4138 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4139 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
4140 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
4141 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4145 static PixelInfo GetMeanPixelList(PixelList *pixel_list)
4166 channels[ListChannels];
4169 Find the mean value for each of the color.
4171 for (channel=0; channel < 5; channel++)
4173 list=pixel_list->lists+channel;
4179 color=list->nodes[color].next[0];
4180 sum+=(MagickRealType) list->nodes[color].count*color;
4181 count+=list->nodes[color].count;
4182 } while (count < (ssize_t) pixel_list->length);
4183 sum/=pixel_list->length;
4184 channels[channel]=(unsigned short) sum;
4186 GetPixelInfo((const Image *) NULL,&pixel);
4187 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4188 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4189 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
4190 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4191 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
4195 static PixelInfo GetMedianPixelList(PixelList *pixel_list)
4213 channels[ListChannels];
4216 Find the median value for each of the color.
4218 for (channel=0; channel < 5; channel++)
4220 list=pixel_list->lists+channel;
4225 color=list->nodes[color].next[0];
4226 count+=list->nodes[color].count;
4227 } while (count <= (ssize_t) (pixel_list->length >> 1));
4228 channels[channel]=(unsigned short) color;
4230 GetPixelInfo((const Image *) NULL,&pixel);
4231 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4232 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4233 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
4234 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4235 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
4239 static PixelInfo GetMinimumPixelList(PixelList *pixel_list)
4258 channels[ListChannels];
4261 Find the minimum value for each of the color.
4263 for (channel=0; channel < 5; channel++)
4265 list=pixel_list->lists+channel;
4268 minimum=list->nodes[color].next[0];
4271 color=list->nodes[color].next[0];
4272 if (color < minimum)
4274 count+=list->nodes[color].count;
4275 } while (count < (ssize_t) pixel_list->length);
4276 channels[channel]=(unsigned short) minimum;
4278 GetPixelInfo((const Image *) NULL,&pixel);
4279 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4280 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4281 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
4282 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4283 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
4287 static PixelInfo GetModePixelList(PixelList *pixel_list)
4310 Make each pixel the 'predominant color' of the specified neighborhood.
4312 for (channel=0; channel < 5; channel++)
4314 list=pixel_list->lists+channel;
4317 max_count=list->nodes[mode].count;
4321 color=list->nodes[color].next[0];
4322 if (list->nodes[color].count > max_count)
4325 max_count=list->nodes[mode].count;
4327 count+=list->nodes[color].count;
4328 } while (count < (ssize_t) pixel_list->length);
4329 channels[channel]=(unsigned short) mode;
4331 GetPixelInfo((const Image *) NULL,&pixel);
4332 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4333 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4334 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
4335 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4336 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
4340 static PixelInfo GetNonpeakPixelList(PixelList *pixel_list)
4363 Finds the non peak value for each of the colors.
4365 for (channel=0; channel < 5; channel++)
4367 list=pixel_list->lists+channel;
4369 next=list->nodes[color].next[0];
4375 next=list->nodes[color].next[0];
4376 count+=list->nodes[color].count;
4377 } while (count <= (ssize_t) (pixel_list->length >> 1));
4378 if ((previous == 65536UL) && (next != 65536UL))
4381 if ((previous != 65536UL) && (next == 65536UL))
4383 channels[channel]=(unsigned short) color;
4385 GetPixelInfo((const Image *) NULL,&pixel);
4386 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4387 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4388 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
4389 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
4390 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4394 static PixelInfo GetStandardDeviationPixelList(PixelList *pixel_list)
4416 channels[ListChannels];
4419 Find the standard-deviation value for each of the color.
4421 for (channel=0; channel < 5; channel++)
4423 list=pixel_list->lists+channel;
4433 color=list->nodes[color].next[0];
4434 sum+=(MagickRealType) list->nodes[color].count*color;
4435 for (i=0; i < (ssize_t) list->nodes[color].count; i++)
4436 sum_squared+=((MagickRealType) color)*((MagickRealType) color);
4437 count+=list->nodes[color].count;
4438 } while (count < (ssize_t) pixel_list->length);
4439 sum/=pixel_list->length;
4440 sum_squared/=pixel_list->length;
4441 channels[channel]=(unsigned short) sqrt(sum_squared-(sum*sum));
4443 GetPixelInfo((const Image *) NULL,&pixel);
4444 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4445 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4446 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
4447 pixel.alpha=(MagickRealType) ScaleShortToQuantum(channels[3]);
4448 pixel.black=(MagickRealType) ScaleShortToQuantum(channels[4]);
4452 static inline void InsertPixelList(const Image *image,const Quantum *pixel,
4453 PixelList *pixel_list)
4461 index=ScaleQuantumToShort(GetPixelRed(image,pixel));
4462 signature=pixel_list->lists[0].nodes[index].signature;
4463 if (signature == pixel_list->signature)
4464 pixel_list->lists[0].nodes[index].count++;
4466 AddNodePixelList(pixel_list,0,index);
4467 index=ScaleQuantumToShort(GetPixelGreen(image,pixel));
4468 signature=pixel_list->lists[1].nodes[index].signature;
4469 if (signature == pixel_list->signature)
4470 pixel_list->lists[1].nodes[index].count++;
4472 AddNodePixelList(pixel_list,1,index);
4473 index=ScaleQuantumToShort(GetPixelBlue(image,pixel));
4474 signature=pixel_list->lists[2].nodes[index].signature;
4475 if (signature == pixel_list->signature)
4476 pixel_list->lists[2].nodes[index].count++;
4478 AddNodePixelList(pixel_list,2,index);
4479 index=ScaleQuantumToShort(GetPixelAlpha(image,pixel));
4480 signature=pixel_list->lists[3].nodes[index].signature;
4481 if (signature == pixel_list->signature)
4482 pixel_list->lists[3].nodes[index].count++;
4484 AddNodePixelList(pixel_list,3,index);
4485 if (image->colorspace == CMYKColorspace)
4486 index=ScaleQuantumToShort(GetPixelBlack(image,pixel));
4487 signature=pixel_list->lists[4].nodes[index].signature;
4488 if (signature == pixel_list->signature)
4489 pixel_list->lists[4].nodes[index].count++;
4491 AddNodePixelList(pixel_list,4,index);
4494 static inline MagickRealType MagickAbsoluteValue(const MagickRealType x)
4501 static void ResetPixelList(PixelList *pixel_list)
4516 Reset the skip-list.
4518 for (channel=0; channel < 5; channel++)
4520 list=pixel_list->lists+channel;
4521 root=list->nodes+65536UL;
4523 for (level=0; level < 9; level++)
4524 root->next[level]=65536UL;
4526 pixel_list->seed=pixel_list->signature++;
4529 MagickExport Image *StatisticImage(const Image *image,const StatisticType type,
4530 const size_t width,const size_t height,ExceptionInfo *exception)
4532 #define StatisticWidth \
4533 (width == 0 ? GetOptimalKernelWidth2D((double) width,0.5) : width)
4534 #define StatisticHeight \
4535 (height == 0 ? GetOptimalKernelWidth2D((double) height,0.5) : height)
4536 #define StatisticImageTag "Statistic/Image"
4552 **restrict pixel_list;
4558 Initialize statistics image attributes.
4560 assert(image != (Image *) NULL);
4561 assert(image->signature == MagickSignature);
4562 if (image->debug != MagickFalse)
4563 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4564 assert(exception != (ExceptionInfo *) NULL);
4565 assert(exception->signature == MagickSignature);
4566 statistic_image=CloneImage(image,image->columns,image->rows,MagickTrue,
4568 if (statistic_image == (Image *) NULL)
4569 return((Image *) NULL);
4570 if (SetImageStorageClass(statistic_image,DirectClass,exception) == MagickFalse)
4572 statistic_image=DestroyImage(statistic_image);
4573 return((Image *) NULL);
4575 pixel_list=AcquirePixelListThreadSet(StatisticWidth,StatisticHeight);
4576 if (pixel_list == (PixelList **) NULL)
4578 statistic_image=DestroyImage(statistic_image);
4579 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
4582 Make each pixel the min / max / median / mode / etc. of the neighborhood.
4586 image_view=AcquireCacheView(image);
4587 statistic_view=AcquireCacheView(statistic_image);
4588 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4589 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
4591 for (y=0; y < (ssize_t) statistic_image->rows; y++)
4594 id = GetOpenMPThreadId();
4596 register const Quantum
4605 if (status == MagickFalse)
4607 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) StatisticWidth/2L),y-
4608 (ssize_t) (StatisticHeight/2L),image->columns+StatisticWidth,
4609 StatisticHeight,exception);
4610 q=QueueCacheViewAuthenticPixels(statistic_view,0,y,statistic_image->columns, 1,exception);
4611 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
4616 for (x=0; x < (ssize_t) statistic_image->columns; x++)
4621 register const Quantum
4629 ResetPixelList(pixel_list[id]);
4630 for (v=0; v < (ssize_t) StatisticHeight; v++)
4632 for (u=0; u < (ssize_t) StatisticWidth; u++)
4633 InsertPixelList(image,r+u*GetPixelChannels(image),pixel_list[id]);
4634 r+=(image->columns+StatisticWidth)*GetPixelChannels(image);
4636 GetPixelInfo(image,&pixel);
4637 SetPixelInfo(image,p+(StatisticWidth*StatisticHeight/2)*
4638 GetPixelChannels(image),&pixel);
4641 case GradientStatistic:
4647 minimum=GetMinimumPixelList(pixel_list[id]);
4648 maximum=GetMaximumPixelList(pixel_list[id]);
4649 pixel.red=MagickAbsoluteValue(maximum.red-minimum.red);
4650 pixel.green=MagickAbsoluteValue(maximum.green-minimum.green);
4651 pixel.blue=MagickAbsoluteValue(maximum.blue-minimum.blue);
4652 pixel.alpha=MagickAbsoluteValue(maximum.alpha-minimum.alpha);
4653 if (image->colorspace == CMYKColorspace)
4654 pixel.black=MagickAbsoluteValue(maximum.black-minimum.black);
4657 case MaximumStatistic:
4659 pixel=GetMaximumPixelList(pixel_list[id]);
4664 pixel=GetMeanPixelList(pixel_list[id]);
4667 case MedianStatistic:
4670 pixel=GetMedianPixelList(pixel_list[id]);
4673 case MinimumStatistic:
4675 pixel=GetMinimumPixelList(pixel_list[id]);
4680 pixel=GetModePixelList(pixel_list[id]);
4683 case NonpeakStatistic:
4685 pixel=GetNonpeakPixelList(pixel_list[id]);
4688 case StandardDeviationStatistic:
4690 pixel=GetStandardDeviationPixelList(pixel_list[id]);
4694 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
4695 SetPixelRed(statistic_image,ClampToQuantum(pixel.red),q);
4696 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
4697 SetPixelGreen(statistic_image,ClampToQuantum(pixel.green),q);
4698 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
4699 SetPixelBlue(statistic_image,ClampToQuantum(pixel.blue),q);
4700 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
4701 (image->colorspace == CMYKColorspace))
4702 SetPixelBlack(statistic_image,ClampToQuantum(pixel.black),q);
4703 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
4704 (image->matte != MagickFalse))
4705 SetPixelAlpha(statistic_image,ClampToQuantum(pixel.alpha),q);
4706 p+=GetPixelChannels(image);
4707 q+=GetPixelChannels(statistic_image);
4709 if (SyncCacheViewAuthenticPixels(statistic_view,exception) == MagickFalse)
4711 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4716 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4717 #pragma omp critical (MagickCore_StatisticImage)
4719 proceed=SetImageProgress(image,StatisticImageTag,progress++,
4721 if (proceed == MagickFalse)
4725 statistic_view=DestroyCacheView(statistic_view);
4726 image_view=DestroyCacheView(image_view);
4727 pixel_list=DestroyPixelListThreadSet(pixel_list);
4728 return(statistic_image);
4732 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4736 % U n s h a r p M a s k I m a g e %
4740 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4742 % UnsharpMaskImage() sharpens one or more image channels. We convolve the
4743 % image with a Gaussian operator of the given radius and standard deviation
4744 % (sigma). For reasonable results, radius should be larger than sigma. Use a
4745 % radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
4747 % The format of the UnsharpMaskImage method is:
4749 % Image *UnsharpMaskImage(const Image *image,const double radius,
4750 % const double sigma,const double amount,const double threshold,
4751 % ExceptionInfo *exception)
4753 % A description of each parameter follows:
4755 % o image: the image.
4757 % o radius: the radius of the Gaussian, in pixels, not counting the center
4760 % o sigma: the standard deviation of the Gaussian, in pixels.
4762 % o amount: the percentage of the difference between the original and the
4763 % blur image that is added back into the original.
4765 % o threshold: the threshold in pixels needed to apply the diffence amount.
4767 % o exception: return any errors or warnings in this structure.
4770 MagickExport Image *UnsharpMaskImage(const Image *image,
4771 const double radius,const double sigma,const double amount,
4772 const double threshold,ExceptionInfo *exception)
4774 #define SharpenImageTag "Sharpen/Image"
4798 assert(image != (const Image *) NULL);
4799 assert(image->signature == MagickSignature);
4800 if (image->debug != MagickFalse)
4801 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4802 assert(exception != (ExceptionInfo *) NULL);
4803 unsharp_image=BlurImage(image,radius,sigma,exception);
4804 if (unsharp_image == (Image *) NULL)
4805 return((Image *) NULL);
4806 quantum_threshold=(MagickRealType) QuantumRange*threshold;
4812 GetPixelInfo(image,&bias);
4813 image_view=AcquireCacheView(image);
4814 unsharp_view=AcquireCacheView(unsharp_image);
4815 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4816 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
4818 for (y=0; y < (ssize_t) image->rows; y++)
4823 register const Quantum
4832 if (status == MagickFalse)
4834 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
4835 q=GetCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
4837 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
4843 for (x=0; x < (ssize_t) image->columns; x++)
4845 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
4847 pixel.red=GetPixelRed(image,p)-(MagickRealType) GetPixelRed(image,q);
4848 if (fabs(2.0*pixel.red) < quantum_threshold)
4849 pixel.red=(MagickRealType) GetPixelRed(image,p);
4851 pixel.red=(MagickRealType) GetPixelRed(image,p)+(pixel.red*amount);
4852 SetPixelRed(unsharp_image,ClampToQuantum(pixel.red),q);
4854 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
4856 pixel.green=GetPixelGreen(image,p)-
4857 (MagickRealType) GetPixelGreen(image,q);
4858 if (fabs(2.0*pixel.green) < quantum_threshold)
4859 pixel.green=(MagickRealType)
4860 GetPixelGreen(image,p);
4862 pixel.green=(MagickRealType)
4863 GetPixelGreen(image,p)+
4864 (pixel.green*amount);
4865 SetPixelGreen(unsharp_image,
4866 ClampToQuantum(pixel.green),q);
4868 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
4870 pixel.blue=GetPixelBlue(image,p)-
4871 (MagickRealType) GetPixelBlue(image,q);
4872 if (fabs(2.0*pixel.blue) < quantum_threshold)
4873 pixel.blue=(MagickRealType)
4874 GetPixelBlue(image,p);
4876 pixel.blue=(MagickRealType)
4877 GetPixelBlue(image,p)+(pixel.blue*amount);
4878 SetPixelBlue(unsharp_image,
4879 ClampToQuantum(pixel.blue),q);
4881 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
4882 (image->colorspace == CMYKColorspace))
4884 pixel.black=GetPixelBlack(image,p)-
4885 (MagickRealType) GetPixelBlack(image,q);
4886 if (fabs(2.0*pixel.black) < quantum_threshold)
4887 pixel.black=(MagickRealType)
4888 GetPixelBlack(image,p);
4890 pixel.black=(MagickRealType)
4891 GetPixelBlack(image,p)+(pixel.black*
4893 SetPixelBlack(unsharp_image,
4894 ClampToQuantum(pixel.black),q);
4896 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
4898 pixel.alpha=GetPixelAlpha(image,p)-
4899 (MagickRealType) GetPixelAlpha(image,q);
4900 if (fabs(2.0*pixel.alpha) < quantum_threshold)
4901 pixel.alpha=(MagickRealType)
4902 GetPixelAlpha(image,p);
4904 pixel.alpha=GetPixelAlpha(image,p)+
4905 (pixel.alpha*amount);
4906 SetPixelAlpha(unsharp_image,
4907 ClampToQuantum(pixel.alpha),q);
4909 p+=GetPixelChannels(image);
4910 q+=GetPixelChannels(unsharp_image);
4912 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
4914 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4919 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4920 #pragma omp critical (MagickCore_UnsharpMaskImage)
4922 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
4923 if (proceed == MagickFalse)
4927 unsharp_image->type=image->type;
4928 unsharp_view=DestroyCacheView(unsharp_view);
4929 image_view=DestroyCacheView(image_view);
4930 if (status == MagickFalse)
4931 unsharp_image=DestroyImage(unsharp_image);
4932 return(unsharp_image);