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-2014 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/accelerate.h"
45 #include "MagickCore/blob.h"
46 #include "MagickCore/cache-view.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colorspace.h"
50 #include "MagickCore/constitute.h"
51 #include "MagickCore/decorate.h"
52 #include "MagickCore/distort.h"
53 #include "MagickCore/draw.h"
54 #include "MagickCore/enhance.h"
55 #include "MagickCore/exception.h"
56 #include "MagickCore/exception-private.h"
57 #include "MagickCore/effect.h"
58 #include "MagickCore/fx.h"
59 #include "MagickCore/gem.h"
60 #include "MagickCore/gem-private.h"
61 #include "MagickCore/geometry.h"
62 #include "MagickCore/image-private.h"
63 #include "MagickCore/list.h"
64 #include "MagickCore/log.h"
65 #include "MagickCore/memory_.h"
66 #include "MagickCore/memory-private.h"
67 #include "MagickCore/monitor.h"
68 #include "MagickCore/monitor-private.h"
69 #include "MagickCore/montage.h"
70 #include "MagickCore/morphology.h"
71 #include "MagickCore/morphology-private.h"
72 #include "MagickCore/paint.h"
73 #include "MagickCore/pixel-accessor.h"
74 #include "MagickCore/pixel-private.h"
75 #include "MagickCore/property.h"
76 #include "MagickCore/quantize.h"
77 #include "MagickCore/quantum.h"
78 #include "MagickCore/quantum-private.h"
79 #include "MagickCore/random_.h"
80 #include "MagickCore/random-private.h"
81 #include "MagickCore/resample.h"
82 #include "MagickCore/resample-private.h"
83 #include "MagickCore/resize.h"
84 #include "MagickCore/resource_.h"
85 #include "MagickCore/segment.h"
86 #include "MagickCore/shear.h"
87 #include "MagickCore/signature-private.h"
88 #include "MagickCore/statistic.h"
89 #include "MagickCore/string_.h"
90 #include "MagickCore/thread-private.h"
91 #include "MagickCore/transform.h"
92 #include "MagickCore/threshold.h"
95 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99 % A d a p t i v e B l u r I m a g e %
103 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105 % AdaptiveBlurImage() adaptively blurs the image by blurring less
106 % intensely near image edges and more intensely far from edges. We blur the
107 % image with a Gaussian operator of the given radius and standard deviation
108 % (sigma). For reasonable results, radius should be larger than sigma. Use a
109 % radius of 0 and AdaptiveBlurImage() selects a suitable radius for you.
111 % The format of the AdaptiveBlurImage method is:
113 % Image *AdaptiveBlurImage(const Image *image,const double radius,
114 % const double sigma,ExceptionInfo *exception)
116 % A description of each parameter follows:
118 % o image: the image.
120 % o radius: the radius of the Gaussian, in pixels, not counting the center
123 % o sigma: the standard deviation of the Laplacian, in pixels.
125 % o exception: return any errors or warnings in this structure.
129 MagickExport MagickBooleanType AdaptiveLevelImage(Image *image,
130 const char *levels,ExceptionInfo *exception)
149 if (levels == (char *) NULL)
151 flags=ParseGeometry(levels,&geometry_info);
152 black_point=geometry_info.rho;
153 white_point=(double) QuantumRange;
154 if ((flags & SigmaValue) != 0)
155 white_point=geometry_info.sigma;
157 if ((flags & XiValue) != 0)
158 gamma=geometry_info.xi;
159 if ((flags & PercentValue) != 0)
161 black_point*=(double) image->columns*image->rows/100.0;
162 white_point*=(double) image->columns*image->rows/100.0;
164 if ((flags & SigmaValue) == 0)
165 white_point=(double) QuantumRange-black_point;
166 if ((flags & AspectValue ) == 0)
167 status=LevelImage(image,black_point,white_point,gamma,exception);
169 status=LevelizeImage(image,black_point,white_point,gamma,exception);
173 MagickExport Image *AdaptiveBlurImage(const Image *image,const double radius,
174 const double sigma,ExceptionInfo *exception)
176 #define AdaptiveBlurImageTag "Convolve/Image"
177 #define MagickSigma (fabs(sigma) < MagickEpsilon ? MagickEpsilon : sigma)
214 assert(image != (const Image *) NULL);
215 assert(image->signature == MagickSignature);
216 if (image->debug != MagickFalse)
217 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
218 assert(exception != (ExceptionInfo *) NULL);
219 assert(exception->signature == MagickSignature);
220 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
221 if (blur_image == (Image *) NULL)
222 return((Image *) NULL);
223 if (fabs(sigma) < MagickEpsilon)
225 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
227 blur_image=DestroyImage(blur_image);
228 return((Image *) NULL);
231 Edge detect the image brighness channel, level, blur, and level again.
233 edge_image=EdgeImage(image,radius,exception);
234 if (edge_image == (Image *) NULL)
236 blur_image=DestroyImage(blur_image);
237 return((Image *) NULL);
239 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
240 gaussian_image=BlurImage(edge_image,radius,sigma,exception);
241 if (gaussian_image != (Image *) NULL)
243 edge_image=DestroyImage(edge_image);
244 edge_image=gaussian_image;
246 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
248 Create a set of kernels from maximum (radius,sigma) to minimum.
250 width=GetOptimalKernelWidth2D(radius,sigma);
251 kernel=(MagickRealType **) MagickAssumeAligned(AcquireAlignedMemory((size_t)
252 width,sizeof(*kernel)));
253 if (kernel == (MagickRealType **) NULL)
255 edge_image=DestroyImage(edge_image);
256 blur_image=DestroyImage(blur_image);
257 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
259 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
260 for (i=0; i < (ssize_t) width; i+=2)
262 kernel[i]=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory(
263 (size_t) (width-i),(width-i)*sizeof(**kernel)));
264 if (kernel[i] == (MagickRealType *) NULL)
267 j=(ssize_t) (width-i-1)/2;
269 for (v=(-j); v <= j; v++)
271 for (u=(-j); u <= j; u++)
273 kernel[i][k]=(MagickRealType) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
274 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
275 normalize+=kernel[i][k];
279 kernel[i][(j-1)/2]+=(1.0-normalize);
280 if (sigma < MagickEpsilon)
281 kernel[i][(j-1)/2]=1.0;
283 if (i < (ssize_t) width)
285 for (i-=2; i >= 0; i-=2)
286 kernel[i]=(MagickRealType *) RelinquishAlignedMemory(kernel[i]);
287 kernel=(MagickRealType **) RelinquishAlignedMemory(kernel);
288 edge_image=DestroyImage(edge_image);
289 blur_image=DestroyImage(blur_image);
290 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
293 Adaptively blur image.
297 image_view=AcquireVirtualCacheView(image,exception);
298 edge_view=AcquireVirtualCacheView(edge_image,exception);
299 blur_view=AcquireAuthenticCacheView(blur_image,exception);
300 #if defined(MAGICKCORE_OPENMP_SUPPORT)
301 #pragma omp parallel for schedule(static,4) shared(progress,status) \
302 magick_threads(image,blur_image,blur_image->rows,1)
304 for (y=0; y < (ssize_t) blur_image->rows; y++)
306 register const Quantum
315 if (status == MagickFalse)
317 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
318 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
320 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
325 for (x=0; x < (ssize_t) blur_image->columns; x++)
327 register const Quantum
337 j=(ssize_t) ceil((double) width*QuantumScale*
338 GetPixelIntensity(edge_image,r)-0.5);
342 if (j > (ssize_t) width)
346 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
347 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
348 if (p == (const Quantum *) NULL)
350 center=(ssize_t) GetPixelChannels(image)*(width-j)*((width-j)/2L)+
351 GetPixelChannels(image)*((width-j)/2L);
352 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
366 register const MagickRealType
369 register const Quantum
378 channel=GetPixelChannelChannel(image,i);
379 traits=GetPixelChannelTraits(image,channel);
380 blur_traits=GetPixelChannelTraits(blur_image,channel);
381 if ((traits == UndefinedPixelTrait) ||
382 (blur_traits == UndefinedPixelTrait))
384 if (((blur_traits & CopyPixelTrait) != 0) ||
385 (GetPixelReadMask(image,p+center) == 0))
387 SetPixelChannel(blur_image,channel,p[center+i],q);
394 if ((blur_traits & BlendPixelTrait) == 0)
399 for (v=0; v < (ssize_t) (width-j); v++)
401 for (u=0; u < (ssize_t) (width-j); u++)
403 pixel+=(*k)*pixels[i];
406 pixels+=GetPixelChannels(image);
409 gamma=PerceptibleReciprocal(gamma);
410 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
416 for (v=0; v < (ssize_t) (width-j); v++)
418 for (u=0; u < (ssize_t) (width-j); u++)
420 alpha=(double) (QuantumScale*GetPixelAlpha(image,pixels));
421 pixel+=(*k)*alpha*pixels[i];
424 pixels+=GetPixelChannels(image);
427 gamma=PerceptibleReciprocal(gamma);
428 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
430 q+=GetPixelChannels(blur_image);
431 r+=GetPixelChannels(edge_image);
433 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
435 if (image->progress_monitor != (MagickProgressMonitor) NULL)
440 #if defined(MAGICKCORE_OPENMP_SUPPORT)
441 #pragma omp critical (MagickCore_AdaptiveBlurImage)
443 proceed=SetImageProgress(image,AdaptiveBlurImageTag,progress++,
445 if (proceed == MagickFalse)
449 blur_image->type=image->type;
450 blur_view=DestroyCacheView(blur_view);
451 edge_view=DestroyCacheView(edge_view);
452 image_view=DestroyCacheView(image_view);
453 edge_image=DestroyImage(edge_image);
454 for (i=0; i < (ssize_t) width; i+=2)
455 kernel[i]=(MagickRealType *) RelinquishAlignedMemory(kernel[i]);
456 kernel=(MagickRealType **) RelinquishAlignedMemory(kernel);
457 if (status == MagickFalse)
458 blur_image=DestroyImage(blur_image);
463 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
467 % A d a p t i v e S h a r p e n I m a g e %
471 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
473 % AdaptiveSharpenImage() adaptively sharpens the image by sharpening more
474 % intensely near image edges and less intensely far from edges. We sharpen the
475 % image with a Gaussian operator of the given radius and standard deviation
476 % (sigma). For reasonable results, radius should be larger than sigma. Use a
477 % radius of 0 and AdaptiveSharpenImage() selects a suitable radius for you.
479 % The format of the AdaptiveSharpenImage method is:
481 % Image *AdaptiveSharpenImage(const Image *image,const double radius,
482 % const double sigma,ExceptionInfo *exception)
484 % A description of each parameter follows:
486 % o image: the image.
488 % o radius: the radius of the Gaussian, in pixels, not counting the center
491 % o sigma: the standard deviation of the Laplacian, in pixels.
493 % o exception: return any errors or warnings in this structure.
496 MagickExport Image *AdaptiveSharpenImage(const Image *image,const double radius,
497 const double sigma,ExceptionInfo *exception)
499 #define AdaptiveSharpenImageTag "Convolve/Image"
500 #define MagickSigma (fabs(sigma) < MagickEpsilon ? MagickEpsilon : sigma)
537 assert(image != (const Image *) NULL);
538 assert(image->signature == MagickSignature);
539 if (image->debug != MagickFalse)
540 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
541 assert(exception != (ExceptionInfo *) NULL);
542 assert(exception->signature == MagickSignature);
543 sharp_image=CloneImage(image,0,0,MagickTrue,exception);
544 if (sharp_image == (Image *) NULL)
545 return((Image *) NULL);
546 if (fabs(sigma) < MagickEpsilon)
548 if (SetImageStorageClass(sharp_image,DirectClass,exception) == MagickFalse)
550 sharp_image=DestroyImage(sharp_image);
551 return((Image *) NULL);
554 Edge detect the image brighness channel, level, sharp, and level again.
556 edge_image=EdgeImage(image,radius,exception);
557 if (edge_image == (Image *) NULL)
559 sharp_image=DestroyImage(sharp_image);
560 return((Image *) NULL);
562 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
563 gaussian_image=BlurImage(edge_image,radius,sigma,exception);
564 if (gaussian_image != (Image *) NULL)
566 edge_image=DestroyImage(edge_image);
567 edge_image=gaussian_image;
569 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
571 Create a set of kernels from maximum (radius,sigma) to minimum.
573 width=GetOptimalKernelWidth2D(radius,sigma);
574 kernel=(MagickRealType **) MagickAssumeAligned(AcquireAlignedMemory((size_t)
575 width,sizeof(*kernel)));
576 if (kernel == (MagickRealType **) NULL)
578 edge_image=DestroyImage(edge_image);
579 sharp_image=DestroyImage(sharp_image);
580 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
582 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
583 for (i=0; i < (ssize_t) width; i+=2)
585 kernel[i]=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory(
586 (size_t) (width-i),(width-i)*sizeof(**kernel)));
587 if (kernel[i] == (MagickRealType *) NULL)
590 j=(ssize_t) (width-i)/2;
592 for (v=(-j); v <= j; v++)
594 for (u=(-j); u <= j; u++)
596 kernel[i][k]=(MagickRealType) (-exp(-((double) u*u+v*v)/(2.0*
597 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
598 normalize+=kernel[i][k];
602 kernel[i][(k-1)/2]=(double) ((-2.0)*normalize);
603 if (sigma < MagickEpsilon)
604 kernel[i][(k-1)/2]=1.0;
606 if (i < (ssize_t) width)
608 for (i-=2; i >= 0; i-=2)
609 kernel[i]=(MagickRealType *) RelinquishAlignedMemory(kernel[i]);
610 kernel=(MagickRealType **) RelinquishAlignedMemory(kernel);
611 edge_image=DestroyImage(edge_image);
612 sharp_image=DestroyImage(sharp_image);
613 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
616 Adaptively sharpen image.
620 image_view=AcquireVirtualCacheView(image,exception);
621 edge_view=AcquireVirtualCacheView(edge_image,exception);
622 sharp_view=AcquireAuthenticCacheView(sharp_image,exception);
623 #if defined(MAGICKCORE_OPENMP_SUPPORT)
624 #pragma omp parallel for schedule(static,4) shared(progress,status) \
625 magick_threads(image,sharp_image,sharp_image->rows,1)
627 for (y=0; y < (ssize_t) sharp_image->rows; y++)
629 register const Quantum
638 if (status == MagickFalse)
640 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
641 q=QueueCacheViewAuthenticPixels(sharp_view,0,y,sharp_image->columns,1,
643 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
648 for (x=0; x < (ssize_t) sharp_image->columns; x++)
650 register const Quantum
660 j=(ssize_t) ceil((double) width*(1.0-QuantumScale*
661 GetPixelIntensity(edge_image,r))-0.5);
665 if (j > (ssize_t) width)
669 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
670 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
671 if (p == (const Quantum *) NULL)
673 center=(ssize_t) GetPixelChannels(image)*(width-j)*((width-j)/2L)+
674 GetPixelChannels(image)*((width-j)/2);
675 for (i=0; i < (ssize_t) GetPixelChannels(sharp_image); i++)
689 register const MagickRealType
692 register const Quantum
701 channel=GetPixelChannelChannel(image,i);
702 traits=GetPixelChannelTraits(image,channel);
703 sharp_traits=GetPixelChannelTraits(sharp_image,channel);
704 if ((traits == UndefinedPixelTrait) ||
705 (sharp_traits == UndefinedPixelTrait))
707 if (((sharp_traits & CopyPixelTrait) != 0) ||
708 (GetPixelReadMask(image,p+center) == 0))
710 SetPixelChannel(sharp_image,channel,p[center+i],q);
717 if ((sharp_traits & BlendPixelTrait) == 0)
722 for (v=0; v < (ssize_t) (width-j); v++)
724 for (u=0; u < (ssize_t) (width-j); u++)
726 pixel+=(*k)*pixels[i];
729 pixels+=GetPixelChannels(image);
732 gamma=PerceptibleReciprocal(gamma);
733 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
739 for (v=0; v < (ssize_t) (width-j); v++)
741 for (u=0; u < (ssize_t) (width-j); u++)
743 alpha=(double) (QuantumScale*GetPixelAlpha(image,pixels));
744 pixel+=(*k)*alpha*pixels[i];
747 pixels+=GetPixelChannels(image);
750 gamma=PerceptibleReciprocal(gamma);
751 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
753 q+=GetPixelChannels(sharp_image);
754 r+=GetPixelChannels(edge_image);
756 if (SyncCacheViewAuthenticPixels(sharp_view,exception) == MagickFalse)
758 if (image->progress_monitor != (MagickProgressMonitor) NULL)
763 #if defined(MAGICKCORE_OPENMP_SUPPORT)
764 #pragma omp critical (MagickCore_AdaptiveSharpenImage)
766 proceed=SetImageProgress(image,AdaptiveSharpenImageTag,progress++,
768 if (proceed == MagickFalse)
772 sharp_image->type=image->type;
773 sharp_view=DestroyCacheView(sharp_view);
774 edge_view=DestroyCacheView(edge_view);
775 image_view=DestroyCacheView(image_view);
776 edge_image=DestroyImage(edge_image);
777 for (i=0; i < (ssize_t) width; i+=2)
778 kernel[i]=(MagickRealType *) RelinquishAlignedMemory(kernel[i]);
779 kernel=(MagickRealType **) RelinquishAlignedMemory(kernel);
780 if (status == MagickFalse)
781 sharp_image=DestroyImage(sharp_image);
786 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
790 % B l u r I m a g e %
794 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
796 % BlurImage() blurs an image. We convolve the image with a Gaussian operator
797 % of the given radius and standard deviation (sigma). For reasonable results,
798 % the radius should be larger than sigma. Use a radius of 0 and BlurImage()
799 % selects a suitable radius for you.
801 % The format of the BlurImage method is:
803 % Image *BlurImage(const Image *image,const double radius,
804 % const double sigma,ExceptionInfo *exception)
806 % A description of each parameter follows:
808 % o image: the image.
810 % o radius: the radius of the Gaussian, in pixels, not counting the center
813 % o sigma: the standard deviation of the Gaussian, in pixels.
815 % o exception: return any errors or warnings in this structure.
818 MagickExport Image *BlurImage(const Image *image,const double radius,
819 const double sigma,ExceptionInfo *exception)
822 geometry[MaxTextExtent];
830 assert(image != (const Image *) NULL);
831 assert(image->signature == MagickSignature);
832 if (image->debug != MagickFalse)
833 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
834 assert(exception != (ExceptionInfo *) NULL);
835 assert(exception->signature == MagickSignature);
836 (void) FormatLocaleString(geometry,MaxTextExtent,
837 "blur:%.20gx%.20g;blur:%.20gx%.20g+90",radius,sigma,radius,sigma);
838 kernel_info=AcquireKernelInfo(geometry);
839 if (kernel_info == (KernelInfo *) NULL)
840 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
841 blur_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
842 UndefinedCompositeOp,0.0,exception);
843 kernel_info=DestroyKernelInfo(kernel_info);
848 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
852 % C o n v o l v e I m a g e %
856 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
858 % ConvolveImage() applies a custom convolution kernel to the image.
860 % The format of the ConvolveImage method is:
862 % Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
863 % ExceptionInfo *exception)
865 % A description of each parameter follows:
867 % o image: the image.
869 % o kernel: the filtering kernel.
871 % o exception: return any errors or warnings in this structure.
874 MagickExport Image *ConvolveImage(const Image *image,
875 const KernelInfo *kernel_info,ExceptionInfo *exception)
880 convolve_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
881 UndefinedCompositeOp,0.0,exception);
882 return(convolve_image);
886 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
890 % D e s p e c k l e I m a g e %
894 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
896 % DespeckleImage() reduces the speckle noise in an image while perserving the
897 % edges of the original image. A speckle removing filter uses a complementary % hulling technique (raising pixels that are darker than their surrounding
898 % neighbors, then complementarily lowering pixels that are brighter than their
899 % surrounding neighbors) to reduce the speckle index of that image (reference
900 % Crimmins speckle removal).
902 % The format of the DespeckleImage method is:
904 % Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
906 % A description of each parameter follows:
908 % o image: the image.
910 % o exception: return any errors or warnings in this structure.
914 static void Hull(const Image *image,const ssize_t x_offset,
915 const ssize_t y_offset,const size_t columns,const size_t rows,
916 const int polarity,Quantum *restrict f,Quantum *restrict g)
927 assert(image != (const Image *) NULL);
928 assert(image->signature == MagickSignature);
929 if (image->debug != MagickFalse)
930 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
931 assert(f != (Quantum *) NULL);
932 assert(g != (Quantum *) NULL);
935 r=p+(y_offset*(columns+2)+x_offset);
936 #if defined(MAGICKCORE_OPENMP_SUPPORT)
937 #pragma omp parallel for schedule(static,4) \
938 magick_threads(image,image,1,1)
940 for (y=0; y < (ssize_t) rows; y++)
951 for (x=0; x < (ssize_t) columns; x++)
953 v=(MagickRealType) p[i];
954 if ((MagickRealType) r[i] >= (v+ScaleCharToQuantum(2)))
955 v+=ScaleCharToQuantum(1);
960 for (x=0; x < (ssize_t) columns; x++)
962 v=(MagickRealType) p[i];
963 if ((MagickRealType) r[i] <= (v-ScaleCharToQuantum(2)))
964 v-=ScaleCharToQuantum(1);
971 r=q+(y_offset*(columns+2)+x_offset);
972 s=q-(y_offset*(columns+2)+x_offset);
973 #if defined(MAGICKCORE_OPENMP_SUPPORT)
974 #pragma omp parallel for schedule(static,4) \
975 magick_threads(image,image,1,1)
977 for (y=0; y < (ssize_t) rows; y++)
988 for (x=0; x < (ssize_t) columns; x++)
990 v=(MagickRealType) q[i];
991 if (((MagickRealType) s[i] >= (v+ScaleCharToQuantum(2))) &&
992 ((MagickRealType) r[i] > v))
993 v+=ScaleCharToQuantum(1);
998 for (x=0; x < (ssize_t) columns; x++)
1000 v=(MagickRealType) q[i];
1001 if (((MagickRealType) s[i] <= (v-ScaleCharToQuantum(2))) &&
1002 ((MagickRealType) r[i] < v))
1003 v-=ScaleCharToQuantum(1);
1010 MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1012 #define DespeckleImageTag "Despeckle/Image"
1038 static const ssize_t
1039 X[4] = {0, 1, 1,-1},
1040 Y[4] = {1, 0, 1, 1};
1043 Allocate despeckled image.
1045 assert(image != (const Image *) NULL);
1046 assert(image->signature == MagickSignature);
1047 if (image->debug != MagickFalse)
1048 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1049 assert(exception != (ExceptionInfo *) NULL);
1050 assert(exception->signature == MagickSignature);
1051 despeckle_image=CloneImage(image,0,0,MagickTrue,exception);
1052 if (despeckle_image == (Image *) NULL)
1053 return((Image *) NULL);
1054 status=SetImageStorageClass(despeckle_image,DirectClass,exception);
1055 if (status == MagickFalse)
1057 despeckle_image=DestroyImage(despeckle_image);
1058 return((Image *) NULL);
1061 Allocate image buffer.
1063 length=(size_t) ((image->columns+2)*(image->rows+2));
1064 pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
1065 buffer_info=AcquireVirtualMemory(length,sizeof(*buffer));
1066 if ((pixel_info == (MemoryInfo *) NULL) ||
1067 (buffer_info == (MemoryInfo *) NULL))
1069 if (buffer_info != (MemoryInfo *) NULL)
1070 buffer_info=RelinquishVirtualMemory(buffer_info);
1071 if (pixel_info != (MemoryInfo *) NULL)
1072 pixel_info=RelinquishVirtualMemory(pixel_info);
1073 despeckle_image=DestroyImage(despeckle_image);
1074 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1076 pixels=(Quantum *) GetVirtualMemoryBlob(pixel_info);
1077 buffer=(Quantum *) GetVirtualMemoryBlob(buffer_info);
1079 Reduce speckle in the image.
1082 image_view=AcquireVirtualCacheView(image,exception);
1083 despeckle_view=AcquireAuthenticCacheView(despeckle_image,exception);
1084 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1101 if (status == MagickFalse)
1103 channel=GetPixelChannelChannel(image,i);
1104 traits=GetPixelChannelTraits(image,channel);
1105 despeckle_traits=GetPixelChannelTraits(despeckle_image,channel);
1106 if ((traits == UndefinedPixelTrait) ||
1107 (despeckle_traits == UndefinedPixelTrait))
1109 if ((despeckle_traits & CopyPixelTrait) != 0)
1111 (void) ResetMagickMemory(pixels,0,length*sizeof(*pixels));
1112 j=(ssize_t) image->columns+2;
1113 for (y=0; y < (ssize_t) image->rows; y++)
1115 register const Quantum
1118 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1119 if (p == (const Quantum *) NULL)
1125 for (x=0; x < (ssize_t) image->columns; x++)
1128 p+=GetPixelChannels(image);
1132 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
1133 for (k=0; k < 4; k++)
1135 Hull(image,X[k],Y[k],image->columns,image->rows,1,pixels,buffer);
1136 Hull(image,-X[k],-Y[k],image->columns,image->rows,1,pixels,buffer);
1137 Hull(image,-X[k],-Y[k],image->columns,image->rows,-1,pixels,buffer);
1138 Hull(image,X[k],Y[k],image->columns,image->rows,-1,pixels,buffer);
1140 j=(ssize_t) image->columns+2;
1141 for (y=0; y < (ssize_t) image->rows; y++)
1149 q=GetCacheViewAuthenticPixels(despeckle_view,0,y,despeckle_image->columns,
1151 if (q == (Quantum *) NULL)
1157 for (x=0; x < (ssize_t) image->columns; x++)
1159 SetPixelChannel(despeckle_image,channel,pixels[j++],q);
1160 q+=GetPixelChannels(despeckle_image);
1162 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1163 if (sync == MagickFalse)
1167 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1172 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1173 GetPixelChannels(image));
1174 if (proceed == MagickFalse)
1178 despeckle_view=DestroyCacheView(despeckle_view);
1179 image_view=DestroyCacheView(image_view);
1180 buffer_info=RelinquishVirtualMemory(buffer_info);
1181 pixel_info=RelinquishVirtualMemory(pixel_info);
1182 despeckle_image->type=image->type;
1183 if (status == MagickFalse)
1184 despeckle_image=DestroyImage(despeckle_image);
1185 return(despeckle_image);
1189 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1193 % E d g e I m a g e %
1197 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1199 % EdgeImage() finds edges in an image. Radius defines the radius of the
1200 % convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1203 % The format of the EdgeImage method is:
1205 % Image *EdgeImage(const Image *image,const double radius,
1206 % ExceptionInfo *exception)
1208 % A description of each parameter follows:
1210 % o image: the image.
1212 % o radius: the radius of the pixel neighborhood.
1214 % o exception: return any errors or warnings in this structure.
1217 MagickExport Image *EdgeImage(const Image *image,const double radius,
1218 ExceptionInfo *exception)
1232 assert(image != (const Image *) NULL);
1233 assert(image->signature == MagickSignature);
1234 if (image->debug != MagickFalse)
1235 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1236 assert(exception != (ExceptionInfo *) NULL);
1237 assert(exception->signature == MagickSignature);
1238 width=GetOptimalKernelWidth1D(radius,0.5);
1239 kernel_info=AcquireKernelInfo((const char *) NULL);
1240 if (kernel_info == (KernelInfo *) NULL)
1241 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1242 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1243 kernel_info->width=width;
1244 kernel_info->height=width;
1245 kernel_info->x=(ssize_t) (kernel_info->width-1)/2;
1246 kernel_info->y=(ssize_t) (kernel_info->height-1)/2;
1247 kernel_info->signature=MagickSignature;
1248 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
1249 AcquireAlignedMemory(kernel_info->width,kernel_info->height*
1250 sizeof(*kernel_info->values)));
1251 if (kernel_info->values == (MagickRealType *) NULL)
1253 kernel_info=DestroyKernelInfo(kernel_info);
1254 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1256 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
1257 kernel_info->values[i]=(-1.0);
1258 kernel_info->values[i/2]=(double) kernel_info->width*kernel_info->height-1.0;
1259 edge_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1260 UndefinedCompositeOp,0.0,exception);
1261 kernel_info=DestroyKernelInfo(kernel_info);
1266 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1270 % E m b o s s I m a g e %
1274 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1276 % EmbossImage() returns a grayscale image with a three-dimensional effect.
1277 % We convolve the image with a Gaussian operator of the given radius and
1278 % standard deviation (sigma). For reasonable results, radius should be
1279 % larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1282 % The format of the EmbossImage method is:
1284 % Image *EmbossImage(const Image *image,const double radius,
1285 % const double sigma,ExceptionInfo *exception)
1287 % A description of each parameter follows:
1289 % o image: the image.
1291 % o radius: the radius of the pixel neighborhood.
1293 % o sigma: the standard deviation of the Gaussian, in pixels.
1295 % o exception: return any errors or warnings in this structure.
1298 MagickExport Image *EmbossImage(const Image *image,const double radius,
1299 const double sigma,ExceptionInfo *exception)
1323 assert(image != (const Image *) NULL);
1324 assert(image->signature == MagickSignature);
1325 if (image->debug != MagickFalse)
1326 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1327 assert(exception != (ExceptionInfo *) NULL);
1328 assert(exception->signature == MagickSignature);
1329 width=GetOptimalKernelWidth1D(radius,sigma);
1330 kernel_info=AcquireKernelInfo((const char *) NULL);
1331 if (kernel_info == (KernelInfo *) NULL)
1332 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1333 kernel_info->width=width;
1334 kernel_info->height=width;
1335 kernel_info->x=(ssize_t) (width-1)/2;
1336 kernel_info->y=(ssize_t) (width-1)/2;
1337 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
1338 AcquireAlignedMemory(kernel_info->width,kernel_info->width*
1339 sizeof(*kernel_info->values)));
1340 if (kernel_info->values == (MagickRealType *) NULL)
1342 kernel_info=DestroyKernelInfo(kernel_info);
1343 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1345 j=(ssize_t) (kernel_info->width-1)/2;
1348 for (v=(-j); v <= j; v++)
1350 for (u=(-j); u <= j; u++)
1352 kernel_info->values[i]=(MagickRealType) (((u < 0) || (v < 0) ? -8.0 :
1353 8.0)*exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
1354 (2.0*MagickPI*MagickSigma*MagickSigma));
1356 kernel_info->values[i]=0.0;
1362 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
1363 normalize+=kernel_info->values[i];
1364 gamma=PerceptibleReciprocal(normalize);
1365 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
1366 kernel_info->values[i]*=gamma;
1367 emboss_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1368 UndefinedCompositeOp,0.0,exception);
1369 kernel_info=DestroyKernelInfo(kernel_info);
1370 if (emboss_image != (Image *) NULL)
1371 (void) EqualizeImage(emboss_image,exception);
1372 return(emboss_image);
1376 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1380 % G a u s s i a n B l u r I m a g e %
1384 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1386 % GaussianBlurImage() blurs an image. We convolve the image with a
1387 % Gaussian operator of the given radius and standard deviation (sigma).
1388 % For reasonable results, the radius should be larger than sigma. Use a
1389 % radius of 0 and GaussianBlurImage() selects a suitable radius for you
1391 % The format of the GaussianBlurImage method is:
1393 % Image *GaussianBlurImage(const Image *image,onst double radius,
1394 % const double sigma,ExceptionInfo *exception)
1396 % A description of each parameter follows:
1398 % o image: the image.
1400 % o radius: the radius of the Gaussian, in pixels, not counting the center
1403 % o sigma: the standard deviation of the Gaussian, in pixels.
1405 % o exception: return any errors or warnings in this structure.
1408 MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
1409 const double sigma,ExceptionInfo *exception)
1412 geometry[MaxTextExtent];
1420 assert(image != (const Image *) NULL);
1421 assert(image->signature == MagickSignature);
1422 if (image->debug != MagickFalse)
1423 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1424 assert(exception != (ExceptionInfo *) NULL);
1425 assert(exception->signature == MagickSignature);
1426 (void) FormatLocaleString(geometry,MaxTextExtent,"gaussian:%.20gx%.20g",
1428 kernel_info=AcquireKernelInfo(geometry);
1429 if (kernel_info == (KernelInfo *) NULL)
1430 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1431 blur_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1432 UndefinedCompositeOp,0.0,exception);
1433 kernel_info=DestroyKernelInfo(kernel_info);
1438 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1442 % M o t i o n B l u r I m a g e %
1446 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1448 % MotionBlurImage() simulates motion blur. We convolve the image with a
1449 % Gaussian operator of the given radius and standard deviation (sigma).
1450 % For reasonable results, radius should be larger than sigma. Use a
1451 % radius of 0 and MotionBlurImage() selects a suitable radius for you.
1452 % Angle gives the angle of the blurring motion.
1454 % Andrew Protano contributed this effect.
1456 % The format of the MotionBlurImage method is:
1458 % Image *MotionBlurImage(const Image *image,const double radius,
1459 % const double sigma,const double angle,ExceptionInfo *exception)
1461 % A description of each parameter follows:
1463 % o image: the image.
1465 % o radius: the radius of the Gaussian, in pixels, not counting
1468 % o sigma: the standard deviation of the Gaussian, in pixels.
1470 % o angle: Apply the effect along this angle.
1472 % o exception: return any errors or warnings in this structure.
1476 static MagickRealType *GetMotionBlurKernel(const size_t width,
1487 Generate a 1-D convolution kernel.
1489 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1490 kernel=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory((size_t)
1491 width,sizeof(*kernel)));
1492 if (kernel == (MagickRealType *) NULL)
1495 for (i=0; i < (ssize_t) width; i++)
1497 kernel[i]=(MagickRealType) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
1498 MagickSigma)))/(MagickSQ2PI*MagickSigma));
1499 normalize+=kernel[i];
1501 for (i=0; i < (ssize_t) width; i++)
1502 kernel[i]/=normalize;
1506 MagickExport Image *MotionBlurImage(const Image *image,const double radius,
1507 const double sigma,const double angle,ExceptionInfo *exception)
1509 #define BlurImageTag "Blur/Image"
1543 assert(image != (Image *) NULL);
1544 assert(image->signature == MagickSignature);
1545 if (image->debug != MagickFalse)
1546 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1547 assert(exception != (ExceptionInfo *) NULL);
1548 width=GetOptimalKernelWidth1D(radius,sigma);
1549 kernel=GetMotionBlurKernel(width,sigma);
1550 if (kernel == (MagickRealType *) NULL)
1551 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1552 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
1553 if (offset == (OffsetInfo *) NULL)
1555 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
1556 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1558 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1559 if (blur_image == (Image *) NULL)
1561 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
1562 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1563 return((Image *) NULL);
1565 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
1567 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
1568 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1569 blur_image=DestroyImage(blur_image);
1570 return((Image *) NULL);
1572 point.x=(double) width*sin(DegreesToRadians(angle));
1573 point.y=(double) width*cos(DegreesToRadians(angle));
1574 for (i=0; i < (ssize_t) width; i++)
1576 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
1577 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
1584 image_view=AcquireVirtualCacheView(image,exception);
1585 motion_view=AcquireVirtualCacheView(image,exception);
1586 blur_view=AcquireAuthenticCacheView(blur_image,exception);
1587 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1588 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1589 magick_threads(image,blur_image,image->rows,1)
1591 for (y=0; y < (ssize_t) image->rows; y++)
1593 register const Quantum
1602 if (status == MagickFalse)
1604 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1605 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
1607 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1612 for (x=0; x < (ssize_t) image->columns; x++)
1617 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1631 register const Quantum
1634 register MagickRealType
1640 channel=GetPixelChannelChannel(image,i);
1641 traits=GetPixelChannelTraits(image,channel);
1642 blur_traits=GetPixelChannelTraits(blur_image,channel);
1643 if ((traits == UndefinedPixelTrait) ||
1644 (blur_traits == UndefinedPixelTrait))
1646 if (((blur_traits & CopyPixelTrait) != 0) ||
1647 (GetPixelReadMask(image,p) == 0))
1649 SetPixelChannel(blur_image,channel,p[i],q);
1654 if ((blur_traits & BlendPixelTrait) == 0)
1656 for (j=0; j < (ssize_t) width; j++)
1658 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+
1659 offset[j].y,1,1,exception);
1660 if (r == (const Quantum *) NULL)
1668 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
1673 for (j=0; j < (ssize_t) width; j++)
1675 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+offset[j].y,1,
1677 if (r == (const Quantum *) NULL)
1682 alpha=(double) (QuantumScale*GetPixelAlpha(image,r));
1683 pixel+=(*k)*alpha*r[i];
1687 gamma=PerceptibleReciprocal(gamma);
1688 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
1690 p+=GetPixelChannels(image);
1691 q+=GetPixelChannels(blur_image);
1693 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1695 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1700 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1701 #pragma omp critical (MagickCore_MotionBlurImage)
1703 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
1704 if (proceed == MagickFalse)
1708 blur_view=DestroyCacheView(blur_view);
1709 motion_view=DestroyCacheView(motion_view);
1710 image_view=DestroyCacheView(image_view);
1711 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
1712 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1713 if (status == MagickFalse)
1714 blur_image=DestroyImage(blur_image);
1719 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1723 % P r e v i e w I m a g e %
1727 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1729 % PreviewImage() tiles 9 thumbnails of the specified image with an image
1730 % processing operation applied with varying parameters. This may be helpful
1731 % pin-pointing an appropriate parameter for a particular image processing
1734 % The format of the PreviewImages method is:
1736 % Image *PreviewImages(const Image *image,const PreviewType preview,
1737 % ExceptionInfo *exception)
1739 % A description of each parameter follows:
1741 % o image: the image.
1743 % o preview: the image processing operation.
1745 % o exception: return any errors or warnings in this structure.
1748 MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
1749 ExceptionInfo *exception)
1751 #define NumberTiles 9
1752 #define PreviewImageTag "Preview/Image"
1753 #define DefaultPreviewGeometry "204x204+10+10"
1756 factor[MaxTextExtent],
1757 label[MaxTextExtent];
1802 Open output image file.
1804 assert(image != (Image *) NULL);
1805 assert(image->signature == MagickSignature);
1806 if (image->debug != MagickFalse)
1807 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1811 preview_info=AcquireImageInfo();
1812 SetGeometry(image,&geometry);
1813 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
1814 &geometry.width,&geometry.height);
1815 images=NewImageList();
1817 GetQuantizeInfo(&quantize_info);
1823 for (i=0; i < NumberTiles; i++)
1825 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
1826 if (thumbnail == (Image *) NULL)
1828 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
1830 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel,exception);
1831 if (i == (NumberTiles/2))
1833 (void) QueryColorCompliance("#dfdfdf",AllCompliance,
1834 &thumbnail->matte_color,exception);
1835 AppendImageToList(&images,thumbnail);
1843 preview_image=RotateImage(thumbnail,degrees,exception);
1844 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
1850 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
1851 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",degrees,
1857 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
1858 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
1859 preview_image=RollImage(thumbnail,x,y,exception);
1860 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
1861 (double) x,(double) y);
1866 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1867 if (preview_image == (Image *) NULL)
1869 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",2.0*
1871 (void) ModulateImage(preview_image,factor,exception);
1872 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
1875 case SaturationPreview:
1877 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1878 if (preview_image == (Image *) NULL)
1880 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",2.0*percentage);
1881 (void) ModulateImage(preview_image,factor,exception);
1882 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
1885 case BrightnessPreview:
1887 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1888 if (preview_image == (Image *) NULL)
1890 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
1891 (void) ModulateImage(preview_image,factor,exception);
1892 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
1898 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1899 if (preview_image == (Image *) NULL)
1902 (void) GammaImage(preview_image,gamma,exception);
1903 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
1908 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1909 if (preview_image != (Image *) NULL)
1910 for (x=0; x < i; x++)
1911 (void) ContrastImage(preview_image,MagickTrue,exception);
1912 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
1918 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1919 if (preview_image == (Image *) NULL)
1921 for (x=0; x < i; x++)
1922 (void) ContrastImage(preview_image,MagickFalse,exception);
1923 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
1927 case GrayscalePreview:
1929 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1930 if (preview_image == (Image *) NULL)
1933 quantize_info.number_colors=colors;
1934 quantize_info.colorspace=GRAYColorspace;
1935 (void) QuantizeImage(&quantize_info,preview_image,exception);
1936 (void) FormatLocaleString(label,MaxTextExtent,
1937 "-colorspace gray -colors %.20g",(double) colors);
1940 case QuantizePreview:
1942 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1943 if (preview_image == (Image *) NULL)
1946 quantize_info.number_colors=colors;
1947 (void) QuantizeImage(&quantize_info,preview_image,exception);
1948 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
1952 case DespecklePreview:
1954 for (x=0; x < (i-1); x++)
1956 preview_image=DespeckleImage(thumbnail,exception);
1957 if (preview_image == (Image *) NULL)
1959 thumbnail=DestroyImage(thumbnail);
1960 thumbnail=preview_image;
1962 preview_image=DespeckleImage(thumbnail,exception);
1963 if (preview_image == (Image *) NULL)
1965 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
1969 case ReduceNoisePreview:
1971 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
1972 (size_t) radius,exception);
1973 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
1976 case AddNoisePreview:
1982 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
1987 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
1992 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
1997 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2002 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2007 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2012 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2016 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2017 (size_t) i,exception);
2018 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
2021 case SharpenPreview:
2023 preview_image=SharpenImage(thumbnail,radius,sigma,exception);
2024 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",radius,
2030 preview_image=BlurImage(thumbnail,radius,sigma,exception);
2031 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
2035 case ThresholdPreview:
2037 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2038 if (preview_image == (Image *) NULL)
2040 (void) BilevelImage(thumbnail,(double) (percentage*((double)
2041 QuantumRange+1.0))/100.0,exception);
2042 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",(double)
2043 (percentage*((double) QuantumRange+1.0))/100.0);
2046 case EdgeDetectPreview:
2048 preview_image=EdgeImage(thumbnail,radius,exception);
2049 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
2054 preview_image=SpreadImage(thumbnail,radius,thumbnail->interpolate,
2056 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",radius+0.5);
2059 case SolarizePreview:
2061 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2062 if (preview_image == (Image *) NULL)
2064 (void) SolarizeImage(preview_image,(double) QuantumRange*percentage/
2066 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
2067 (QuantumRange*percentage)/100.0);
2073 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2075 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",degrees,
2081 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2082 if (preview_image == (Image *) NULL)
2084 geometry.width=(size_t) (2*i+2);
2085 geometry.height=(size_t) (2*i+2);
2088 (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
2089 (void) FormatLocaleString(label,MaxTextExtent,
2090 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
2091 geometry.height,(double) geometry.x,(double) geometry.y);
2094 case SegmentPreview:
2096 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2097 if (preview_image == (Image *) NULL)
2100 (void) SegmentImage(preview_image,sRGBColorspace,MagickFalse,threshold,
2101 threshold,exception);
2102 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
2103 threshold,threshold);
2108 preview_image=SwirlImage(thumbnail,degrees,image->interpolate,
2110 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
2114 case ImplodePreview:
2117 preview_image=ImplodeImage(thumbnail,degrees,image->interpolate,
2119 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
2125 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,
2126 image->interpolate,exception);
2127 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",0.5*degrees,
2131 case OilPaintPreview:
2133 preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2135 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",radius,
2139 case CharcoalDrawingPreview:
2141 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
2143 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",radius,
2150 filename[MaxTextExtent];
2158 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2159 if (preview_image == (Image *) NULL)
2161 preview_info->quality=(size_t) percentage;
2162 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
2163 preview_info->quality);
2164 file=AcquireUniqueFileResource(filename);
2167 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
2168 "jpeg:%s",filename);
2169 status=WriteImage(preview_info,preview_image,exception);
2170 if (status != MagickFalse)
2175 (void) CopyMagickString(preview_info->filename,
2176 preview_image->filename,MaxTextExtent);
2177 quality_image=ReadImage(preview_info,exception);
2178 if (quality_image != (Image *) NULL)
2180 preview_image=DestroyImage(preview_image);
2181 preview_image=quality_image;
2184 (void) RelinquishUniqueFileResource(preview_image->filename);
2185 if ((GetBlobSize(preview_image)/1024) >= 1024)
2186 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
2187 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2190 if (GetBlobSize(preview_image) >= 1024)
2191 (void) FormatLocaleString(label,MaxTextExtent,
2192 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
2193 GetBlobSize(preview_image))/1024.0);
2195 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
2196 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
2200 thumbnail=DestroyImage(thumbnail);
2204 if (preview_image == (Image *) NULL)
2206 (void) DeleteImageProperty(preview_image,"label");
2207 (void) SetImageProperty(preview_image,"label",label,exception);
2208 AppendImageToList(&images,preview_image);
2209 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2211 if (proceed == MagickFalse)
2214 if (images == (Image *) NULL)
2216 preview_info=DestroyImageInfo(preview_info);
2217 return((Image *) NULL);
2222 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2223 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2224 montage_info->shadow=MagickTrue;
2225 (void) CloneString(&montage_info->tile,"3x3");
2226 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2227 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2228 montage_image=MontageImages(images,montage_info,exception);
2229 montage_info=DestroyMontageInfo(montage_info);
2230 images=DestroyImageList(images);
2231 if (montage_image == (Image *) NULL)
2232 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2233 if (montage_image->montage != (char *) NULL)
2236 Free image directory.
2238 montage_image->montage=(char *) RelinquishMagickMemory(
2239 montage_image->montage);
2240 if (image->directory != (char *) NULL)
2241 montage_image->directory=(char *) RelinquishMagickMemory(
2242 montage_image->directory);
2244 preview_info=DestroyImageInfo(preview_info);
2245 return(montage_image);
2249 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2253 % R a d i a l B l u r I m a g e %
2257 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2259 % RadialBlurImage() applies a radial blur to the image.
2261 % Andrew Protano contributed this effect.
2263 % The format of the RadialBlurImage method is:
2265 % Image *RadialBlurImage(const Image *image,const double angle,
2266 % ExceptionInfo *exception)
2268 % A description of each parameter follows:
2270 % o image: the image.
2272 % o angle: the angle of the radial blur.
2276 % o exception: return any errors or warnings in this structure.
2279 MagickExport Image *RadialBlurImage(const Image *image,const double angle,
2280 ExceptionInfo *exception)
2316 Allocate blur image.
2318 assert(image != (Image *) NULL);
2319 assert(image->signature == MagickSignature);
2320 if (image->debug != MagickFalse)
2321 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2322 assert(exception != (ExceptionInfo *) NULL);
2323 assert(exception->signature == MagickSignature);
2324 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2325 if (blur_image == (Image *) NULL)
2326 return((Image *) NULL);
2327 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2329 blur_image=DestroyImage(blur_image);
2330 return((Image *) NULL);
2332 blur_center.x=(double) (image->columns-1)/2.0;
2333 blur_center.y=(double) (image->rows-1)/2.0;
2334 blur_radius=hypot(blur_center.x,blur_center.y);
2335 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
2336 theta=DegreesToRadians(angle)/(double) (n-1);
2337 cos_theta=(double *) AcquireQuantumMemory((size_t) n,
2338 sizeof(*cos_theta));
2339 sin_theta=(double *) AcquireQuantumMemory((size_t) n,
2340 sizeof(*sin_theta));
2341 if ((cos_theta == (double *) NULL) ||
2342 (sin_theta == (double *) NULL))
2344 blur_image=DestroyImage(blur_image);
2345 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2347 offset=theta*(double) (n-1)/2.0;
2348 for (i=0; i < (ssize_t) n; i++)
2350 cos_theta[i]=cos((double) (theta*i-offset));
2351 sin_theta[i]=sin((double) (theta*i-offset));
2358 image_view=AcquireVirtualCacheView(image,exception);
2359 radial_view=AcquireVirtualCacheView(image,exception);
2360 blur_view=AcquireAuthenticCacheView(blur_image,exception);
2361 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2362 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2363 magick_threads(image,blur_image,image->rows,1)
2365 for (y=0; y < (ssize_t) image->rows; y++)
2367 register const Quantum
2376 if (status == MagickFalse)
2378 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2379 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2381 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2386 for (x=0; x < (ssize_t) image->columns; x++)
2400 center.x=(double) x-blur_center.x;
2401 center.y=(double) y-blur_center.y;
2402 radius=hypot((double) center.x,center.y);
2407 step=(size_t) (blur_radius/radius);
2414 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2427 register const Quantum
2433 channel=GetPixelChannelChannel(image,i);
2434 traits=GetPixelChannelTraits(image,channel);
2435 blur_traits=GetPixelChannelTraits(blur_image,channel);
2436 if ((traits == UndefinedPixelTrait) ||
2437 (blur_traits == UndefinedPixelTrait))
2439 if (((blur_traits & CopyPixelTrait) != 0) ||
2440 (GetPixelReadMask(image,p) == 0))
2442 SetPixelChannel(blur_image,channel,p[i],q);
2447 if ((blur_traits & BlendPixelTrait) == 0)
2449 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2451 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2452 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2453 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2455 if (r == (const Quantum *) NULL)
2463 gamma=PerceptibleReciprocal(gamma);
2464 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2467 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2469 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2470 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2471 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2473 if (r == (const Quantum *) NULL)
2478 pixel+=GetPixelAlpha(image,r)*r[i];
2479 gamma+=GetPixelAlpha(image,r);
2481 gamma=PerceptibleReciprocal(gamma);
2482 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2484 p+=GetPixelChannels(image);
2485 q+=GetPixelChannels(blur_image);
2487 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2489 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2494 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2495 #pragma omp critical (MagickCore_RadialBlurImage)
2497 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2498 if (proceed == MagickFalse)
2502 blur_view=DestroyCacheView(blur_view);
2503 radial_view=DestroyCacheView(radial_view);
2504 image_view=DestroyCacheView(image_view);
2505 cos_theta=(double *) RelinquishMagickMemory(cos_theta);
2506 sin_theta=(double *) RelinquishMagickMemory(sin_theta);
2507 if (status == MagickFalse)
2508 blur_image=DestroyImage(blur_image);
2513 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2517 % S e l e c t i v e B l u r I m a g e %
2521 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2523 % SelectiveBlurImage() selectively blur pixels within a contrast threshold.
2524 % It is similar to the unsharpen mask that sharpens everything with contrast
2525 % above a certain threshold.
2527 % The format of the SelectiveBlurImage method is:
2529 % Image *SelectiveBlurImage(const Image *image,const double radius,
2530 % const double sigma,const double threshold,ExceptionInfo *exception)
2532 % A description of each parameter follows:
2534 % o image: the image.
2536 % o radius: the radius of the Gaussian, in pixels, not counting the center
2539 % o sigma: the standard deviation of the Gaussian, in pixels.
2541 % o threshold: only pixels within this contrast threshold are included
2542 % in the blur operation.
2544 % o exception: return any errors or warnings in this structure.
2547 MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
2548 const double sigma,const double threshold,ExceptionInfo *exception)
2550 #define SelectiveBlurImageTag "SelectiveBlur/Image"
2584 Initialize blur image attributes.
2586 assert(image != (Image *) NULL);
2587 assert(image->signature == MagickSignature);
2588 if (image->debug != MagickFalse)
2589 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2590 assert(exception != (ExceptionInfo *) NULL);
2591 assert(exception->signature == MagickSignature);
2592 width=GetOptimalKernelWidth1D(radius,sigma);
2593 kernel=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory((size_t)
2594 width,width*sizeof(*kernel)));
2595 if (kernel == (MagickRealType *) NULL)
2596 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2597 j=(ssize_t) (width-1)/2;
2599 for (v=(-j); v <= j; v++)
2601 for (u=(-j); u <= j; u++)
2602 kernel[i++]=(MagickRealType) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
2603 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
2605 if (image->debug != MagickFalse)
2608 format[MaxTextExtent],
2611 register const MagickRealType
2618 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
2619 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
2621 message=AcquireString("");
2623 for (v=0; v < (ssize_t) width; v++)
2626 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
2627 (void) ConcatenateString(&message,format);
2628 for (u=0; u < (ssize_t) width; u++)
2630 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",(double) *k++);
2631 (void) ConcatenateString(&message,format);
2633 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
2635 message=DestroyString(message);
2637 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2638 if (blur_image == (Image *) NULL)
2639 return((Image *) NULL);
2640 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2642 blur_image=DestroyImage(blur_image);
2643 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2644 return((Image *) NULL);
2646 luminance_image=CloneImage(image,0,0,MagickTrue,exception);
2647 if (luminance_image == (Image *) NULL)
2649 blur_image=DestroyImage(blur_image);
2650 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2651 return((Image *) NULL);
2653 status=TransformImageColorspace(luminance_image,GRAYColorspace,exception);
2654 if (status == MagickFalse)
2656 luminance_image=DestroyImage(luminance_image);
2657 blur_image=DestroyImage(blur_image);
2658 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2659 return((Image *) NULL);
2662 Threshold blur image.
2666 center=(ssize_t) (GetPixelChannels(image)*(image->columns+width)*
2667 ((width-1)/2L)+GetPixelChannels(image)*((width-1)/2L));
2668 image_view=AcquireVirtualCacheView(image,exception);
2669 luminance_view=AcquireVirtualCacheView(luminance_image,exception);
2670 blur_view=AcquireAuthenticCacheView(blur_image,exception);
2671 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2672 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2673 magick_threads(image,blur_image,image->rows,1)
2675 for (y=0; y < (ssize_t) image->rows; y++)
2683 register const Quantum
2693 if (status == MagickFalse)
2695 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) (width-1)/2L),y-(ssize_t)
2696 ((width-1)/2L),image->columns+width,width,exception);
2697 l=GetCacheViewVirtualPixels(luminance_view,-((ssize_t) (width-1)/2L),y-
2698 (ssize_t) ((width-1)/2L),luminance_image->columns+width,width,exception);
2699 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2701 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2706 for (x=0; x < (ssize_t) image->columns; x++)
2714 intensity=GetPixelIntensity(image,p+center);
2715 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2729 register const MagickRealType
2732 register const Quantum
2733 *restrict luminance_pixels,
2742 channel=GetPixelChannelChannel(image,i);
2743 traits=GetPixelChannelTraits(image,channel);
2744 blur_traits=GetPixelChannelTraits(blur_image,channel);
2745 if ((traits == UndefinedPixelTrait) ||
2746 (blur_traits == UndefinedPixelTrait))
2748 if (((blur_traits & CopyPixelTrait) != 0) ||
2749 (GetPixelReadMask(image,p+center) == 0))
2751 SetPixelChannel(blur_image,channel,p[center+i],q);
2759 if ((blur_traits & BlendPixelTrait) == 0)
2761 for (v=0; v < (ssize_t) width; v++)
2763 for (u=0; u < (ssize_t) width; u++)
2765 contrast=GetPixelIntensity(luminance_image,luminance_pixels)-
2767 if (fabs(contrast) < threshold)
2769 pixel+=(*k)*pixels[i];
2773 pixels+=GetPixelChannels(image);
2774 luminance_pixels+=GetPixelChannels(luminance_image);
2776 pixels+=(image->columns-1)*GetPixelChannels(image);
2777 luminance_pixels+=luminance_image->columns*
2778 GetPixelChannels(luminance_image);
2780 if (fabs((double) gamma) < MagickEpsilon)
2782 SetPixelChannel(blur_image,channel,p[center+i],q);
2785 gamma=PerceptibleReciprocal(gamma);
2786 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2789 for (v=0; v < (ssize_t) width; v++)
2791 for (u=0; u < (ssize_t) width; u++)
2793 contrast=GetPixelIntensity(image,pixels)-intensity;
2794 if (fabs(contrast) < threshold)
2796 alpha=(double) (QuantumScale*GetPixelAlpha(image,pixels));
2797 pixel+=(*k)*alpha*pixels[i];
2801 pixels+=GetPixelChannels(image);
2802 luminance_pixels+=GetPixelChannels(luminance_image);
2804 pixels+=(image->columns-1)*GetPixelChannels(image);
2805 luminance_pixels+=luminance_image->columns*
2806 GetPixelChannels(luminance_image);
2808 if (fabs((double) gamma) < MagickEpsilon)
2810 SetPixelChannel(blur_image,channel,p[center+i],q);
2813 gamma=PerceptibleReciprocal(gamma);
2814 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2816 p+=GetPixelChannels(image);
2817 l+=GetPixelChannels(luminance_image);
2818 q+=GetPixelChannels(blur_image);
2820 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
2821 if (sync == MagickFalse)
2823 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2828 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2829 #pragma omp critical (MagickCore_SelectiveBlurImage)
2831 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
2833 if (proceed == MagickFalse)
2837 blur_image->type=image->type;
2838 blur_view=DestroyCacheView(blur_view);
2839 image_view=DestroyCacheView(image_view);
2840 luminance_image=DestroyImage(luminance_image);
2841 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2842 if (status == MagickFalse)
2843 blur_image=DestroyImage(blur_image);
2848 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2852 % S h a d e I m a g e %
2856 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2858 % ShadeImage() shines a distant light on an image to create a
2859 % three-dimensional effect. You control the positioning of the light with
2860 % azimuth and elevation; azimuth is measured in degrees off the x axis
2861 % and elevation is measured in pixels above the Z axis.
2863 % The format of the ShadeImage method is:
2865 % Image *ShadeImage(const Image *image,const MagickBooleanType gray,
2866 % const double azimuth,const double elevation,ExceptionInfo *exception)
2868 % A description of each parameter follows:
2870 % o image: the image.
2872 % o gray: A value other than zero shades the intensity of each pixel.
2874 % o azimuth, elevation: Define the light source direction.
2876 % o exception: return any errors or warnings in this structure.
2879 MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
2880 const double azimuth,const double elevation,ExceptionInfo *exception)
2882 #define ShadeImageTag "Shade/Image"
2905 Initialize shaded image attributes.
2907 assert(image != (const Image *) NULL);
2908 assert(image->signature == MagickSignature);
2909 if (image->debug != MagickFalse)
2910 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2911 assert(exception != (ExceptionInfo *) NULL);
2912 assert(exception->signature == MagickSignature);
2913 linear_image=CloneImage(image,0,0,MagickTrue,exception);
2914 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2915 if ((linear_image == (Image *) NULL) || (shade_image == (Image *) NULL))
2917 if (linear_image != (Image *) NULL)
2918 linear_image=DestroyImage(linear_image);
2919 if (shade_image != (Image *) NULL)
2920 shade_image=DestroyImage(shade_image);
2921 return((Image *) NULL);
2923 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
2925 linear_image=DestroyImage(linear_image);
2926 shade_image=DestroyImage(shade_image);
2927 return((Image *) NULL);
2930 Compute the light vector.
2932 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
2933 cos(DegreesToRadians(elevation));
2934 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
2935 cos(DegreesToRadians(elevation));
2936 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
2942 image_view=AcquireVirtualCacheView(linear_image,exception);
2943 shade_view=AcquireAuthenticCacheView(shade_image,exception);
2944 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2945 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2946 magick_threads(linear_image,shade_image,linear_image->rows,1)
2948 for (y=0; y < (ssize_t) linear_image->rows; y++)
2958 register const Quantum
2970 if (status == MagickFalse)
2972 p=GetCacheViewVirtualPixels(image_view,-1,y-1,linear_image->columns+2,3,
2974 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
2976 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2982 Shade this row of pixels.
2984 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
2985 pre=p+GetPixelChannels(linear_image);
2986 center=pre+(linear_image->columns+2)*GetPixelChannels(linear_image);
2987 post=center+(linear_image->columns+2)*GetPixelChannels(linear_image);
2988 for (x=0; x < (ssize_t) linear_image->columns; x++)
2994 Determine the surface normal and compute shading.
2997 GetPixelIntensity(linear_image,pre-GetPixelChannels(linear_image))+
2998 GetPixelIntensity(linear_image,center-GetPixelChannels(linear_image))+
2999 GetPixelIntensity(linear_image,post-GetPixelChannels(linear_image))-
3000 GetPixelIntensity(linear_image,pre+GetPixelChannels(linear_image))-
3001 GetPixelIntensity(linear_image,center+GetPixelChannels(linear_image))-
3002 GetPixelIntensity(linear_image,post+GetPixelChannels(linear_image)));
3004 GetPixelIntensity(linear_image,post-GetPixelChannels(linear_image))+
3005 GetPixelIntensity(linear_image,post)+
3006 GetPixelIntensity(linear_image,post+GetPixelChannels(linear_image))-
3007 GetPixelIntensity(linear_image,pre-GetPixelChannels(linear_image))-
3008 GetPixelIntensity(linear_image,pre)-
3009 GetPixelIntensity(linear_image,pre+GetPixelChannels(linear_image)));
3010 if ((normal.x == 0.0) && (normal.y == 0.0))
3015 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3016 if (distance > MagickEpsilon)
3018 normal_distance=normal.x*normal.x+normal.y*normal.y+
3020 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3021 shade=distance/sqrt((double) normal_distance);
3024 for (i=0; i < (ssize_t) GetPixelChannels(linear_image); i++)
3033 channel=GetPixelChannelChannel(linear_image,i);
3034 traits=GetPixelChannelTraits(linear_image,channel);
3035 shade_traits=GetPixelChannelTraits(shade_image,channel);
3036 if ((traits == UndefinedPixelTrait) ||
3037 (shade_traits == UndefinedPixelTrait))
3039 if (((shade_traits & CopyPixelTrait) != 0) ||
3040 (GetPixelReadMask(linear_image,center) == 0))
3042 SetPixelChannel(shade_image,channel,center[i],q);
3045 if (gray != MagickFalse)
3047 SetPixelChannel(shade_image,channel,ClampToQuantum(shade),q);
3050 SetPixelChannel(shade_image,channel,ClampToQuantum(QuantumScale*shade*
3053 pre+=GetPixelChannels(linear_image);
3054 center+=GetPixelChannels(linear_image);
3055 post+=GetPixelChannels(linear_image);
3056 q+=GetPixelChannels(shade_image);
3058 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3060 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3065 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3066 #pragma omp critical (MagickCore_ShadeImage)
3068 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3069 if (proceed == MagickFalse)
3073 shade_view=DestroyCacheView(shade_view);
3074 image_view=DestroyCacheView(image_view);
3075 linear_image=DestroyImage(linear_image);
3076 if (status == MagickFalse)
3077 shade_image=DestroyImage(shade_image);
3078 return(shade_image);
3082 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3086 % S h a r p e n I m a g e %
3090 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3092 % SharpenImage() sharpens the image. We convolve the image with a Gaussian
3093 % operator of the given radius and standard deviation (sigma). For
3094 % reasonable results, radius should be larger than sigma. Use a radius of 0
3095 % and SharpenImage() selects a suitable radius for you.
3097 % Using a separable kernel would be faster, but the negative weights cancel
3098 % out on the corners of the kernel producing often undesirable ringing in the
3099 % filtered result; this can be avoided by using a 2D gaussian shaped image
3100 % sharpening kernel instead.
3102 % The format of the SharpenImage method is:
3104 % Image *SharpenImage(const Image *image,const double radius,
3105 % const double sigma,ExceptionInfo *exception)
3107 % A description of each parameter follows:
3109 % o image: the image.
3111 % o radius: the radius of the Gaussian, in pixels, not counting the center
3114 % o sigma: the standard deviation of the Laplacian, in pixels.
3116 % o exception: return any errors or warnings in this structure.
3119 MagickExport Image *SharpenImage(const Image *image,const double radius,
3120 const double sigma,ExceptionInfo *exception)
3143 assert(image != (const Image *) NULL);
3144 assert(image->signature == MagickSignature);
3145 if (image->debug != MagickFalse)
3146 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3147 assert(exception != (ExceptionInfo *) NULL);
3148 assert(exception->signature == MagickSignature);
3149 width=GetOptimalKernelWidth2D(radius,sigma);
3150 kernel_info=AcquireKernelInfo((const char *) NULL);
3151 if (kernel_info == (KernelInfo *) NULL)
3152 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3153 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3154 kernel_info->width=width;
3155 kernel_info->height=width;
3156 kernel_info->x=(ssize_t) (width-1)/2;
3157 kernel_info->y=(ssize_t) (width-1)/2;
3158 kernel_info->signature=MagickSignature;
3159 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
3160 AcquireAlignedMemory(kernel_info->width,kernel_info->height*
3161 sizeof(*kernel_info->values)));
3162 if (kernel_info->values == (MagickRealType *) NULL)
3164 kernel_info=DestroyKernelInfo(kernel_info);
3165 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3168 j=(ssize_t) (kernel_info->width-1)/2;
3170 for (v=(-j); v <= j; v++)
3172 for (u=(-j); u <= j; u++)
3174 kernel_info->values[i]=(MagickRealType) (-exp(-((double) u*u+v*v)/(2.0*
3175 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3176 normalize+=kernel_info->values[i];
3180 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
3182 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
3183 normalize+=kernel_info->values[i];
3184 gamma=PerceptibleReciprocal(normalize);
3185 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
3186 kernel_info->values[i]*=gamma;
3187 sharp_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
3188 UndefinedCompositeOp,0.0,exception);
3189 kernel_info=DestroyKernelInfo(kernel_info);
3190 return(sharp_image);
3194 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3198 % S p r e a d I m a g e %
3202 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3204 % SpreadImage() is a special effects method that randomly displaces each
3205 % pixel in a block defined by the radius parameter.
3207 % The format of the SpreadImage method is:
3209 % Image *SpreadImage(const Image *image,const double radius,
3210 % const PixelInterpolateMethod method,ExceptionInfo *exception)
3212 % A description of each parameter follows:
3214 % o image: the image.
3216 % o radius: choose a random pixel in a neighborhood of this extent.
3218 % o method: the pixel interpolation method.
3220 % o exception: return any errors or warnings in this structure.
3223 MagickExport Image *SpreadImage(const Image *image,const double radius,
3224 const PixelInterpolateMethod method,ExceptionInfo *exception)
3226 #define SpreadImageTag "Spread/Image"
3242 **restrict random_info;
3250 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3256 Initialize spread image attributes.
3258 assert(image != (Image *) NULL);
3259 assert(image->signature == MagickSignature);
3260 if (image->debug != MagickFalse)
3261 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3262 assert(exception != (ExceptionInfo *) NULL);
3263 assert(exception->signature == MagickSignature);
3264 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3266 if (spread_image == (Image *) NULL)
3267 return((Image *) NULL);
3268 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
3270 spread_image=DestroyImage(spread_image);
3271 return((Image *) NULL);
3278 width=GetOptimalKernelWidth1D(radius,0.5);
3279 random_info=AcquireRandomInfoThreadSet();
3280 image_view=AcquireVirtualCacheView(image,exception);
3281 spread_view=AcquireAuthenticCacheView(spread_image,exception);
3282 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3283 key=GetRandomSecretKey(random_info[0]);
3284 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3285 magick_threads(image,spread_image,image->rows,key == ~0UL)
3287 for (y=0; y < (ssize_t) image->rows; y++)
3290 id = GetOpenMPThreadId();
3298 if (status == MagickFalse)
3300 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
3302 if (q == (Quantum *) NULL)
3307 for (x=0; x < (ssize_t) image->columns; x++)
3312 point.x=GetPseudoRandomValue(random_info[id]);
3313 point.y=GetPseudoRandomValue(random_info[id]);
3314 status=InterpolatePixelChannels(image,image_view,spread_image,method,
3315 (double) x+width*point.x-0.5,(double) y+width*point.y-0.5,q,exception);
3316 q+=GetPixelChannels(spread_image);
3318 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
3320 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3325 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3326 #pragma omp critical (MagickCore_SpreadImage)
3328 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3329 if (proceed == MagickFalse)
3333 spread_view=DestroyCacheView(spread_view);
3334 image_view=DestroyCacheView(image_view);
3335 random_info=DestroyRandomInfoThreadSet(random_info);
3336 if (status == MagickFalse)
3337 spread_image=DestroyImage(spread_image);
3338 return(spread_image);
3342 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3346 % U n s h a r p M a s k I m a g e %
3350 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3352 % UnsharpMaskImage() sharpens one or more image channels. We convolve the
3353 % image with a Gaussian operator of the given radius and standard deviation
3354 % (sigma). For reasonable results, radius should be larger than sigma. Use a
3355 % radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
3357 % The format of the UnsharpMaskImage method is:
3359 % Image *UnsharpMaskImage(const Image *image,const double radius,
3360 % const double sigma,const double amount,const double threshold,
3361 % ExceptionInfo *exception)
3363 % A description of each parameter follows:
3365 % o image: the image.
3367 % o radius: the radius of the Gaussian, in pixels, not counting the center
3370 % o sigma: the standard deviation of the Gaussian, in pixels.
3372 % o gain: the percentage of the difference between the original and the
3373 % blur image that is added back into the original.
3375 % o threshold: the threshold in pixels needed to apply the diffence gain.
3377 % o exception: return any errors or warnings in this structure.
3380 MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
3381 const double sigma,const double gain,const double threshold,
3382 ExceptionInfo *exception)
3384 #define SharpenImageTag "Sharpen/Image"
3405 assert(image != (const Image *) NULL);
3406 assert(image->signature == MagickSignature);
3407 if (image->debug != MagickFalse)
3408 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3409 assert(exception != (ExceptionInfo *) NULL);
3410 unsharp_image=BlurImage(image,radius,sigma,exception);
3411 if (unsharp_image == (Image *) NULL)
3412 return((Image *) NULL);
3413 quantum_threshold=(double) QuantumRange*threshold;
3419 image_view=AcquireVirtualCacheView(image,exception);
3420 unsharp_view=AcquireAuthenticCacheView(unsharp_image,exception);
3421 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3422 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3423 magick_threads(image,unsharp_image,image->rows,1)
3425 for (y=0; y < (ssize_t) image->rows; y++)
3427 register const Quantum
3436 if (status == MagickFalse)
3438 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3439 q=QueueCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
3441 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3446 for (x=0; x < (ssize_t) image->columns; x++)
3451 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3463 channel=GetPixelChannelChannel(image,i);
3464 traits=GetPixelChannelTraits(image,channel);
3465 unsharp_traits=GetPixelChannelTraits(unsharp_image,channel);
3466 if ((traits == UndefinedPixelTrait) ||
3467 (unsharp_traits == UndefinedPixelTrait))
3469 if (((unsharp_traits & CopyPixelTrait) != 0) ||
3470 (GetPixelReadMask(image,p) != 0))
3472 SetPixelChannel(unsharp_image,channel,p[i],q);
3475 pixel=p[i]-(double) GetPixelChannel(unsharp_image,channel,q);
3476 if (fabs(2.0*pixel) < quantum_threshold)
3477 pixel=(double) p[i];
3479 pixel=(double) p[i]+gain*pixel;
3480 SetPixelChannel(unsharp_image,channel,ClampToQuantum(pixel),q);
3482 p+=GetPixelChannels(image);
3483 q+=GetPixelChannels(unsharp_image);
3485 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
3487 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3492 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3493 #pragma omp critical (MagickCore_UnsharpMaskImage)
3495 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
3496 if (proceed == MagickFalse)
3500 unsharp_image->type=image->type;
3501 unsharp_view=DestroyCacheView(unsharp_view);
3502 image_view=DestroyCacheView(image_view);
3503 if (status == MagickFalse)
3504 unsharp_image=DestroyImage(unsharp_image);
3505 return(unsharp_image);