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/matrix.h"
66 #include "MagickCore/memory_.h"
67 #include "MagickCore/memory-private.h"
68 #include "MagickCore/monitor.h"
69 #include "MagickCore/monitor-private.h"
70 #include "MagickCore/montage.h"
71 #include "MagickCore/morphology.h"
72 #include "MagickCore/morphology-private.h"
73 #include "MagickCore/paint.h"
74 #include "MagickCore/pixel-accessor.h"
75 #include "MagickCore/pixel-private.h"
76 #include "MagickCore/property.h"
77 #include "MagickCore/quantize.h"
78 #include "MagickCore/quantum.h"
79 #include "MagickCore/quantum-private.h"
80 #include "MagickCore/random_.h"
81 #include "MagickCore/random-private.h"
82 #include "MagickCore/resample.h"
83 #include "MagickCore/resample-private.h"
84 #include "MagickCore/resize.h"
85 #include "MagickCore/resource_.h"
86 #include "MagickCore/segment.h"
87 #include "MagickCore/shear.h"
88 #include "MagickCore/signature-private.h"
89 #include "MagickCore/statistic.h"
90 #include "MagickCore/string_.h"
91 #include "MagickCore/thread-private.h"
92 #include "MagickCore/transform.h"
93 #include "MagickCore/threshold.h"
96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
100 % A d a p t i v e B l u r I m a g e %
104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 % AdaptiveBlurImage() adaptively blurs the image by blurring less
107 % intensely near image edges and more intensely far from edges. We blur the
108 % image with a Gaussian operator of the given radius and standard deviation
109 % (sigma). For reasonable results, radius should be larger than sigma. Use a
110 % radius of 0 and AdaptiveBlurImage() selects a suitable radius for you.
112 % The format of the AdaptiveBlurImage method is:
114 % Image *AdaptiveBlurImage(const Image *image,const double radius,
115 % const double sigma,ExceptionInfo *exception)
117 % A description of each parameter follows:
119 % o image: the image.
121 % o radius: the radius of the Gaussian, in pixels, not counting the center
124 % o sigma: the standard deviation of the Laplacian, in pixels.
126 % o exception: return any errors or warnings in this structure.
130 MagickExport MagickBooleanType AdaptiveLevelImage(Image *image,
131 const char *levels,ExceptionInfo *exception)
150 if (levels == (char *) NULL)
152 flags=ParseGeometry(levels,&geometry_info);
153 black_point=geometry_info.rho;
154 white_point=(double) QuantumRange;
155 if ((flags & SigmaValue) != 0)
156 white_point=geometry_info.sigma;
158 if ((flags & XiValue) != 0)
159 gamma=geometry_info.xi;
160 if ((flags & PercentValue) != 0)
162 black_point*=(double) image->columns*image->rows/100.0;
163 white_point*=(double) image->columns*image->rows/100.0;
165 if ((flags & SigmaValue) == 0)
166 white_point=(double) QuantumRange-black_point;
167 if ((flags & AspectValue ) == 0)
168 status=LevelImage(image,black_point,white_point,gamma,exception);
170 status=LevelizeImage(image,black_point,white_point,gamma,exception);
174 MagickExport Image *AdaptiveBlurImage(const Image *image,const double radius,
175 const double sigma,ExceptionInfo *exception)
177 #define AdaptiveBlurImageTag "Convolve/Image"
178 #define MagickSigma (fabs(sigma) < MagickEpsilon ? MagickEpsilon : sigma)
215 assert(image != (const Image *) NULL);
216 assert(image->signature == MagickSignature);
217 if (image->debug != MagickFalse)
218 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
219 assert(exception != (ExceptionInfo *) NULL);
220 assert(exception->signature == MagickSignature);
221 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
222 if (blur_image == (Image *) NULL)
223 return((Image *) NULL);
224 if (fabs(sigma) < MagickEpsilon)
226 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
228 blur_image=DestroyImage(blur_image);
229 return((Image *) NULL);
232 Edge detect the image brighness channel, level, blur, and level again.
234 edge_image=EdgeImage(image,radius,exception);
235 if (edge_image == (Image *) NULL)
237 blur_image=DestroyImage(blur_image);
238 return((Image *) NULL);
240 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
241 gaussian_image=BlurImage(edge_image,radius,sigma,exception);
242 if (gaussian_image != (Image *) NULL)
244 edge_image=DestroyImage(edge_image);
245 edge_image=gaussian_image;
247 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
249 Create a set of kernels from maximum (radius,sigma) to minimum.
251 width=GetOptimalKernelWidth2D(radius,sigma);
252 kernel=(MagickRealType **) MagickAssumeAligned(AcquireAlignedMemory((size_t)
253 width,sizeof(*kernel)));
254 if (kernel == (MagickRealType **) NULL)
256 edge_image=DestroyImage(edge_image);
257 blur_image=DestroyImage(blur_image);
258 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
260 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
261 for (i=0; i < (ssize_t) width; i+=2)
263 kernel[i]=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory(
264 (size_t) (width-i),(width-i)*sizeof(**kernel)));
265 if (kernel[i] == (MagickRealType *) NULL)
268 j=(ssize_t) (width-i-1)/2;
270 for (v=(-j); v <= j; v++)
272 for (u=(-j); u <= j; u++)
274 kernel[i][k]=(MagickRealType) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
275 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
276 normalize+=kernel[i][k];
280 kernel[i][(j-1)/2]+=(1.0-normalize);
281 if (sigma < MagickEpsilon)
282 kernel[i][(j-1)/2]=1.0;
284 if (i < (ssize_t) width)
286 for (i-=2; i >= 0; i-=2)
287 kernel[i]=(MagickRealType *) RelinquishAlignedMemory(kernel[i]);
288 kernel=(MagickRealType **) RelinquishAlignedMemory(kernel);
289 edge_image=DestroyImage(edge_image);
290 blur_image=DestroyImage(blur_image);
291 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
294 Adaptively blur image.
298 image_view=AcquireVirtualCacheView(image,exception);
299 edge_view=AcquireVirtualCacheView(edge_image,exception);
300 blur_view=AcquireAuthenticCacheView(blur_image,exception);
301 #if defined(MAGICKCORE_OPENMP_SUPPORT)
302 #pragma omp parallel for schedule(static,4) shared(progress,status) \
303 magick_threads(image,blur_image,blur_image->rows,1)
305 for (y=0; y < (ssize_t) blur_image->rows; y++)
307 register const Quantum
316 if (status == MagickFalse)
318 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
319 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
321 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
326 for (x=0; x < (ssize_t) blur_image->columns; x++)
328 register const Quantum
338 j=(ssize_t) ceil((double) width*QuantumScale*
339 GetPixelIntensity(edge_image,r)-0.5);
343 if (j > (ssize_t) width)
347 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
348 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
349 if (p == (const Quantum *) NULL)
351 center=(ssize_t) GetPixelChannels(image)*(width-j)*((width-j)/2L)+
352 GetPixelChannels(image)*((width-j)/2L);
353 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
367 register const MagickRealType
370 register const Quantum
379 channel=GetPixelChannelChannel(image,i);
380 traits=GetPixelChannelTraits(image,channel);
381 blur_traits=GetPixelChannelTraits(blur_image,channel);
382 if ((traits == UndefinedPixelTrait) ||
383 (blur_traits == UndefinedPixelTrait))
385 if (((blur_traits & CopyPixelTrait) != 0) ||
386 (GetPixelReadMask(image,p+center) == 0))
388 SetPixelChannel(blur_image,channel,p[center+i],q);
395 if ((blur_traits & BlendPixelTrait) == 0)
400 for (v=0; v < (ssize_t) (width-j); v++)
402 for (u=0; u < (ssize_t) (width-j); u++)
404 pixel+=(*k)*pixels[i];
407 pixels+=GetPixelChannels(image);
410 gamma=PerceptibleReciprocal(gamma);
411 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
417 for (v=0; v < (ssize_t) (width-j); v++)
419 for (u=0; u < (ssize_t) (width-j); u++)
421 alpha=(double) (QuantumScale*GetPixelAlpha(image,pixels));
422 pixel+=(*k)*alpha*pixels[i];
425 pixels+=GetPixelChannels(image);
428 gamma=PerceptibleReciprocal(gamma);
429 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
431 q+=GetPixelChannels(blur_image);
432 r+=GetPixelChannels(edge_image);
434 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
436 if (image->progress_monitor != (MagickProgressMonitor) NULL)
441 #if defined(MAGICKCORE_OPENMP_SUPPORT)
442 #pragma omp critical (MagickCore_AdaptiveBlurImage)
444 proceed=SetImageProgress(image,AdaptiveBlurImageTag,progress++,
446 if (proceed == MagickFalse)
450 blur_image->type=image->type;
451 blur_view=DestroyCacheView(blur_view);
452 edge_view=DestroyCacheView(edge_view);
453 image_view=DestroyCacheView(image_view);
454 edge_image=DestroyImage(edge_image);
455 for (i=0; i < (ssize_t) width; i+=2)
456 kernel[i]=(MagickRealType *) RelinquishAlignedMemory(kernel[i]);
457 kernel=(MagickRealType **) RelinquishAlignedMemory(kernel);
458 if (status == MagickFalse)
459 blur_image=DestroyImage(blur_image);
464 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
468 % A d a p t i v e S h a r p e n I m a g e %
472 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
474 % AdaptiveSharpenImage() adaptively sharpens the image by sharpening more
475 % intensely near image edges and less intensely far from edges. We sharpen the
476 % image with a Gaussian operator of the given radius and standard deviation
477 % (sigma). For reasonable results, radius should be larger than sigma. Use a
478 % radius of 0 and AdaptiveSharpenImage() selects a suitable radius for you.
480 % The format of the AdaptiveSharpenImage method is:
482 % Image *AdaptiveSharpenImage(const Image *image,const double radius,
483 % const double sigma,ExceptionInfo *exception)
485 % A description of each parameter follows:
487 % o image: the image.
489 % o radius: the radius of the Gaussian, in pixels, not counting the center
492 % o sigma: the standard deviation of the Laplacian, in pixels.
494 % o exception: return any errors or warnings in this structure.
497 MagickExport Image *AdaptiveSharpenImage(const Image *image,const double radius,
498 const double sigma,ExceptionInfo *exception)
500 #define AdaptiveSharpenImageTag "Convolve/Image"
501 #define MagickSigma (fabs(sigma) < MagickEpsilon ? MagickEpsilon : sigma)
538 assert(image != (const Image *) NULL);
539 assert(image->signature == MagickSignature);
540 if (image->debug != MagickFalse)
541 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
542 assert(exception != (ExceptionInfo *) NULL);
543 assert(exception->signature == MagickSignature);
544 sharp_image=CloneImage(image,0,0,MagickTrue,exception);
545 if (sharp_image == (Image *) NULL)
546 return((Image *) NULL);
547 if (fabs(sigma) < MagickEpsilon)
549 if (SetImageStorageClass(sharp_image,DirectClass,exception) == MagickFalse)
551 sharp_image=DestroyImage(sharp_image);
552 return((Image *) NULL);
555 Edge detect the image brighness channel, level, sharp, and level again.
557 edge_image=EdgeImage(image,radius,exception);
558 if (edge_image == (Image *) NULL)
560 sharp_image=DestroyImage(sharp_image);
561 return((Image *) NULL);
563 (void) AdaptiveLevelImage(edge_image,"20%,95%",exception);
564 gaussian_image=BlurImage(edge_image,radius,sigma,exception);
565 if (gaussian_image != (Image *) NULL)
567 edge_image=DestroyImage(edge_image);
568 edge_image=gaussian_image;
570 (void) AdaptiveLevelImage(edge_image,"10%,95%",exception);
572 Create a set of kernels from maximum (radius,sigma) to minimum.
574 width=GetOptimalKernelWidth2D(radius,sigma);
575 kernel=(MagickRealType **) MagickAssumeAligned(AcquireAlignedMemory((size_t)
576 width,sizeof(*kernel)));
577 if (kernel == (MagickRealType **) NULL)
579 edge_image=DestroyImage(edge_image);
580 sharp_image=DestroyImage(sharp_image);
581 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
583 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
584 for (i=0; i < (ssize_t) width; i+=2)
586 kernel[i]=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory(
587 (size_t) (width-i),(width-i)*sizeof(**kernel)));
588 if (kernel[i] == (MagickRealType *) NULL)
591 j=(ssize_t) (width-i)/2;
593 for (v=(-j); v <= j; v++)
595 for (u=(-j); u <= j; u++)
597 kernel[i][k]=(MagickRealType) (-exp(-((double) u*u+v*v)/(2.0*
598 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
599 normalize+=kernel[i][k];
603 kernel[i][(k-1)/2]=(double) ((-2.0)*normalize);
604 if (sigma < MagickEpsilon)
605 kernel[i][(k-1)/2]=1.0;
607 if (i < (ssize_t) width)
609 for (i-=2; i >= 0; i-=2)
610 kernel[i]=(MagickRealType *) RelinquishAlignedMemory(kernel[i]);
611 kernel=(MagickRealType **) RelinquishAlignedMemory(kernel);
612 edge_image=DestroyImage(edge_image);
613 sharp_image=DestroyImage(sharp_image);
614 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
617 Adaptively sharpen image.
621 image_view=AcquireVirtualCacheView(image,exception);
622 edge_view=AcquireVirtualCacheView(edge_image,exception);
623 sharp_view=AcquireAuthenticCacheView(sharp_image,exception);
624 #if defined(MAGICKCORE_OPENMP_SUPPORT)
625 #pragma omp parallel for schedule(static,4) shared(progress,status) \
626 magick_threads(image,sharp_image,sharp_image->rows,1)
628 for (y=0; y < (ssize_t) sharp_image->rows; y++)
630 register const Quantum
639 if (status == MagickFalse)
641 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
642 q=QueueCacheViewAuthenticPixels(sharp_view,0,y,sharp_image->columns,1,
644 if ((r == (const Quantum *) NULL) || (q == (Quantum *) NULL))
649 for (x=0; x < (ssize_t) sharp_image->columns; x++)
651 register const Quantum
661 j=(ssize_t) ceil((double) width*(1.0-QuantumScale*
662 GetPixelIntensity(edge_image,r))-0.5);
666 if (j > (ssize_t) width)
670 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-j)/2L),y-
671 (ssize_t) ((width-j)/2L),width-j,width-j,exception);
672 if (p == (const Quantum *) NULL)
674 center=(ssize_t) GetPixelChannels(image)*(width-j)*((width-j)/2L)+
675 GetPixelChannels(image)*((width-j)/2);
676 for (i=0; i < (ssize_t) GetPixelChannels(sharp_image); i++)
690 register const MagickRealType
693 register const Quantum
702 channel=GetPixelChannelChannel(image,i);
703 traits=GetPixelChannelTraits(image,channel);
704 sharp_traits=GetPixelChannelTraits(sharp_image,channel);
705 if ((traits == UndefinedPixelTrait) ||
706 (sharp_traits == UndefinedPixelTrait))
708 if (((sharp_traits & CopyPixelTrait) != 0) ||
709 (GetPixelReadMask(image,p+center) == 0))
711 SetPixelChannel(sharp_image,channel,p[center+i],q);
718 if ((sharp_traits & BlendPixelTrait) == 0)
723 for (v=0; v < (ssize_t) (width-j); v++)
725 for (u=0; u < (ssize_t) (width-j); u++)
727 pixel+=(*k)*pixels[i];
730 pixels+=GetPixelChannels(image);
733 gamma=PerceptibleReciprocal(gamma);
734 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
740 for (v=0; v < (ssize_t) (width-j); v++)
742 for (u=0; u < (ssize_t) (width-j); u++)
744 alpha=(double) (QuantumScale*GetPixelAlpha(image,pixels));
745 pixel+=(*k)*alpha*pixels[i];
748 pixels+=GetPixelChannels(image);
751 gamma=PerceptibleReciprocal(gamma);
752 SetPixelChannel(sharp_image,channel,ClampToQuantum(gamma*pixel),q);
754 q+=GetPixelChannels(sharp_image);
755 r+=GetPixelChannels(edge_image);
757 if (SyncCacheViewAuthenticPixels(sharp_view,exception) == MagickFalse)
759 if (image->progress_monitor != (MagickProgressMonitor) NULL)
764 #if defined(MAGICKCORE_OPENMP_SUPPORT)
765 #pragma omp critical (MagickCore_AdaptiveSharpenImage)
767 proceed=SetImageProgress(image,AdaptiveSharpenImageTag,progress++,
769 if (proceed == MagickFalse)
773 sharp_image->type=image->type;
774 sharp_view=DestroyCacheView(sharp_view);
775 edge_view=DestroyCacheView(edge_view);
776 image_view=DestroyCacheView(image_view);
777 edge_image=DestroyImage(edge_image);
778 for (i=0; i < (ssize_t) width; i+=2)
779 kernel[i]=(MagickRealType *) RelinquishAlignedMemory(kernel[i]);
780 kernel=(MagickRealType **) RelinquishAlignedMemory(kernel);
781 if (status == MagickFalse)
782 sharp_image=DestroyImage(sharp_image);
787 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
791 % B l u r I m a g e %
795 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
797 % BlurImage() blurs an image. We convolve the image with a Gaussian operator
798 % of the given radius and standard deviation (sigma). For reasonable results,
799 % the radius should be larger than sigma. Use a radius of 0 and BlurImage()
800 % selects a suitable radius for you.
802 % The format of the BlurImage method is:
804 % Image *BlurImage(const Image *image,const double radius,
805 % const double sigma,ExceptionInfo *exception)
807 % A description of each parameter follows:
809 % o image: the image.
811 % o radius: the radius of the Gaussian, in pixels, not counting the center
814 % o sigma: the standard deviation of the Gaussian, in pixels.
816 % o exception: return any errors or warnings in this structure.
819 MagickExport Image *BlurImage(const Image *image,const double radius,
820 const double sigma,ExceptionInfo *exception)
823 geometry[MaxTextExtent];
831 assert(image != (const Image *) NULL);
832 assert(image->signature == MagickSignature);
833 if (image->debug != MagickFalse)
834 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
835 assert(exception != (ExceptionInfo *) NULL);
836 assert(exception->signature == MagickSignature);
837 (void) FormatLocaleString(geometry,MaxTextExtent,
838 "blur:%.20gx%.20g;blur:%.20gx%.20g+90",radius,sigma,radius,sigma);
839 kernel_info=AcquireKernelInfo(geometry);
840 if (kernel_info == (KernelInfo *) NULL)
841 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
842 blur_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
843 UndefinedCompositeOp,0.0,exception);
844 kernel_info=DestroyKernelInfo(kernel_info);
849 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
853 % C o n v o l v e I m a g e %
857 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
859 % ConvolveImage() applies a custom convolution kernel to the image.
861 % The format of the ConvolveImage method is:
863 % Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
864 % ExceptionInfo *exception)
866 % A description of each parameter follows:
868 % o image: the image.
870 % o kernel: the filtering kernel.
872 % o exception: return any errors or warnings in this structure.
875 MagickExport Image *ConvolveImage(const Image *image,
876 const KernelInfo *kernel_info,ExceptionInfo *exception)
881 convolve_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
882 UndefinedCompositeOp,0.0,exception);
883 return(convolve_image);
887 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
891 % D e s p e c k l e I m a g e %
895 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
897 % DespeckleImage() reduces the speckle noise in an image while perserving the
898 % edges of the original image. A speckle removing filter uses a complementary % hulling technique (raising pixels that are darker than their surrounding
899 % neighbors, then complementarily lowering pixels that are brighter than their
900 % surrounding neighbors) to reduce the speckle index of that image (reference
901 % Crimmins speckle removal).
903 % The format of the DespeckleImage method is:
905 % Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
907 % A description of each parameter follows:
909 % o image: the image.
911 % o exception: return any errors or warnings in this structure.
915 static void Hull(const Image *image,const ssize_t x_offset,
916 const ssize_t y_offset,const size_t columns,const size_t rows,
917 const int polarity,Quantum *restrict f,Quantum *restrict g)
928 assert(image != (const Image *) NULL);
929 assert(image->signature == MagickSignature);
930 if (image->debug != MagickFalse)
931 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
932 assert(f != (Quantum *) NULL);
933 assert(g != (Quantum *) NULL);
936 r=p+(y_offset*(columns+2)+x_offset);
937 #if defined(MAGICKCORE_OPENMP_SUPPORT)
938 #pragma omp parallel for schedule(static,4) \
939 magick_threads(image,image,1,1)
941 for (y=0; y < (ssize_t) rows; y++)
952 for (x=0; x < (ssize_t) columns; x++)
954 v=(MagickRealType) p[i];
955 if ((MagickRealType) r[i] >= (v+ScaleCharToQuantum(2)))
956 v+=ScaleCharToQuantum(1);
961 for (x=0; x < (ssize_t) columns; x++)
963 v=(MagickRealType) p[i];
964 if ((MagickRealType) r[i] <= (v-ScaleCharToQuantum(2)))
965 v-=ScaleCharToQuantum(1);
972 r=q+(y_offset*(columns+2)+x_offset);
973 s=q-(y_offset*(columns+2)+x_offset);
974 #if defined(MAGICKCORE_OPENMP_SUPPORT)
975 #pragma omp parallel for schedule(static,4) \
976 magick_threads(image,image,1,1)
978 for (y=0; y < (ssize_t) rows; y++)
989 for (x=0; x < (ssize_t) columns; x++)
991 v=(MagickRealType) q[i];
992 if (((MagickRealType) s[i] >= (v+ScaleCharToQuantum(2))) &&
993 ((MagickRealType) r[i] > v))
994 v+=ScaleCharToQuantum(1);
999 for (x=0; x < (ssize_t) columns; x++)
1001 v=(MagickRealType) q[i];
1002 if (((MagickRealType) s[i] <= (v-ScaleCharToQuantum(2))) &&
1003 ((MagickRealType) r[i] < v))
1004 v-=ScaleCharToQuantum(1);
1011 MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1013 #define DespeckleImageTag "Despeckle/Image"
1039 static const ssize_t
1040 X[4] = {0, 1, 1,-1},
1041 Y[4] = {1, 0, 1, 1};
1044 Allocate despeckled image.
1046 assert(image != (const Image *) NULL);
1047 assert(image->signature == MagickSignature);
1048 if (image->debug != MagickFalse)
1049 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1050 assert(exception != (ExceptionInfo *) NULL);
1051 assert(exception->signature == MagickSignature);
1052 despeckle_image=CloneImage(image,0,0,MagickTrue,exception);
1053 if (despeckle_image == (Image *) NULL)
1054 return((Image *) NULL);
1055 status=SetImageStorageClass(despeckle_image,DirectClass,exception);
1056 if (status == MagickFalse)
1058 despeckle_image=DestroyImage(despeckle_image);
1059 return((Image *) NULL);
1062 Allocate image buffer.
1064 length=(size_t) ((image->columns+2)*(image->rows+2));
1065 pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
1066 buffer_info=AcquireVirtualMemory(length,sizeof(*buffer));
1067 if ((pixel_info == (MemoryInfo *) NULL) ||
1068 (buffer_info == (MemoryInfo *) NULL))
1070 if (buffer_info != (MemoryInfo *) NULL)
1071 buffer_info=RelinquishVirtualMemory(buffer_info);
1072 if (pixel_info != (MemoryInfo *) NULL)
1073 pixel_info=RelinquishVirtualMemory(pixel_info);
1074 despeckle_image=DestroyImage(despeckle_image);
1075 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1077 pixels=(Quantum *) GetVirtualMemoryBlob(pixel_info);
1078 buffer=(Quantum *) GetVirtualMemoryBlob(buffer_info);
1080 Reduce speckle in the image.
1083 image_view=AcquireVirtualCacheView(image,exception);
1084 despeckle_view=AcquireAuthenticCacheView(despeckle_image,exception);
1085 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1102 if (status == MagickFalse)
1104 channel=GetPixelChannelChannel(image,i);
1105 traits=GetPixelChannelTraits(image,channel);
1106 despeckle_traits=GetPixelChannelTraits(despeckle_image,channel);
1107 if ((traits == UndefinedPixelTrait) ||
1108 (despeckle_traits == UndefinedPixelTrait))
1110 if ((despeckle_traits & CopyPixelTrait) != 0)
1112 (void) ResetMagickMemory(pixels,0,length*sizeof(*pixels));
1113 j=(ssize_t) image->columns+2;
1114 for (y=0; y < (ssize_t) image->rows; y++)
1116 register const Quantum
1119 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1120 if (p == (const Quantum *) NULL)
1126 for (x=0; x < (ssize_t) image->columns; x++)
1129 p+=GetPixelChannels(image);
1133 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
1134 for (k=0; k < 4; k++)
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);
1139 Hull(image,X[k],Y[k],image->columns,image->rows,-1,pixels,buffer);
1141 j=(ssize_t) image->columns+2;
1142 for (y=0; y < (ssize_t) image->rows; y++)
1150 q=GetCacheViewAuthenticPixels(despeckle_view,0,y,despeckle_image->columns,
1152 if (q == (Quantum *) NULL)
1158 for (x=0; x < (ssize_t) image->columns; x++)
1160 SetPixelChannel(despeckle_image,channel,pixels[j++],q);
1161 q+=GetPixelChannels(despeckle_image);
1163 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1164 if (sync == MagickFalse)
1168 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1173 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1174 GetPixelChannels(image));
1175 if (proceed == MagickFalse)
1179 despeckle_view=DestroyCacheView(despeckle_view);
1180 image_view=DestroyCacheView(image_view);
1181 buffer_info=RelinquishVirtualMemory(buffer_info);
1182 pixel_info=RelinquishVirtualMemory(pixel_info);
1183 despeckle_image->type=image->type;
1184 if (status == MagickFalse)
1185 despeckle_image=DestroyImage(despeckle_image);
1186 return(despeckle_image);
1190 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1194 % E d g e I m a g e %
1198 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1200 % EdgeImage() finds edges in an image. Radius defines the radius of the
1201 % convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1204 % The format of the EdgeImage method is:
1206 % Image *EdgeImage(const Image *image,const double radius,
1207 % ExceptionInfo *exception)
1209 % A description of each parameter follows:
1211 % o image: the image.
1213 % o radius: the radius of the pixel neighborhood.
1215 % o exception: return any errors or warnings in this structure.
1218 MagickExport Image *EdgeImage(const Image *image,const double radius,
1219 ExceptionInfo *exception)
1233 assert(image != (const Image *) NULL);
1234 assert(image->signature == MagickSignature);
1235 if (image->debug != MagickFalse)
1236 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1237 assert(exception != (ExceptionInfo *) NULL);
1238 assert(exception->signature == MagickSignature);
1239 width=GetOptimalKernelWidth1D(radius,0.5);
1240 kernel_info=AcquireKernelInfo((const char *) NULL);
1241 if (kernel_info == (KernelInfo *) NULL)
1242 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1243 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1244 kernel_info->width=width;
1245 kernel_info->height=width;
1246 kernel_info->x=(ssize_t) (kernel_info->width-1)/2;
1247 kernel_info->y=(ssize_t) (kernel_info->height-1)/2;
1248 kernel_info->signature=MagickSignature;
1249 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
1250 AcquireAlignedMemory(kernel_info->width,kernel_info->height*
1251 sizeof(*kernel_info->values)));
1252 if (kernel_info->values == (MagickRealType *) NULL)
1254 kernel_info=DestroyKernelInfo(kernel_info);
1255 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1257 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
1258 kernel_info->values[i]=(-1.0);
1259 kernel_info->values[i/2]=(double) kernel_info->width*kernel_info->height-1.0;
1260 edge_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1261 UndefinedCompositeOp,0.0,exception);
1262 kernel_info=DestroyKernelInfo(kernel_info);
1267 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1271 % E m b o s s I m a g e %
1275 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1277 % EmbossImage() returns a grayscale image with a three-dimensional effect.
1278 % We convolve the image with a Gaussian operator of the given radius and
1279 % standard deviation (sigma). For reasonable results, radius should be
1280 % larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1283 % The format of the EmbossImage method is:
1285 % Image *EmbossImage(const Image *image,const double radius,
1286 % const double sigma,ExceptionInfo *exception)
1288 % A description of each parameter follows:
1290 % o image: the image.
1292 % o radius: the radius of the pixel neighborhood.
1294 % o sigma: the standard deviation of the Gaussian, in pixels.
1296 % o exception: return any errors or warnings in this structure.
1299 MagickExport Image *EmbossImage(const Image *image,const double radius,
1300 const double sigma,ExceptionInfo *exception)
1324 assert(image != (const Image *) NULL);
1325 assert(image->signature == MagickSignature);
1326 if (image->debug != MagickFalse)
1327 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1328 assert(exception != (ExceptionInfo *) NULL);
1329 assert(exception->signature == MagickSignature);
1330 width=GetOptimalKernelWidth1D(radius,sigma);
1331 kernel_info=AcquireKernelInfo((const char *) NULL);
1332 if (kernel_info == (KernelInfo *) NULL)
1333 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1334 kernel_info->width=width;
1335 kernel_info->height=width;
1336 kernel_info->x=(ssize_t) (width-1)/2;
1337 kernel_info->y=(ssize_t) (width-1)/2;
1338 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
1339 AcquireAlignedMemory(kernel_info->width,kernel_info->width*
1340 sizeof(*kernel_info->values)));
1341 if (kernel_info->values == (MagickRealType *) NULL)
1343 kernel_info=DestroyKernelInfo(kernel_info);
1344 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1346 j=(ssize_t) (kernel_info->width-1)/2;
1349 for (v=(-j); v <= j; v++)
1351 for (u=(-j); u <= j; u++)
1353 kernel_info->values[i]=(MagickRealType) (((u < 0) || (v < 0) ? -8.0 :
1354 8.0)*exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
1355 (2.0*MagickPI*MagickSigma*MagickSigma));
1357 kernel_info->values[i]=0.0;
1363 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
1364 normalize+=kernel_info->values[i];
1365 gamma=PerceptibleReciprocal(normalize);
1366 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
1367 kernel_info->values[i]*=gamma;
1368 emboss_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1369 UndefinedCompositeOp,0.0,exception);
1370 kernel_info=DestroyKernelInfo(kernel_info);
1371 if (emboss_image != (Image *) NULL)
1372 (void) EqualizeImage(emboss_image,exception);
1373 return(emboss_image);
1377 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1381 % G a u s s i a n B l u r I m a g e %
1385 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1387 % GaussianBlurImage() blurs an image. We convolve the image with a
1388 % Gaussian operator of the given radius and standard deviation (sigma).
1389 % For reasonable results, the radius should be larger than sigma. Use a
1390 % radius of 0 and GaussianBlurImage() selects a suitable radius for you
1392 % The format of the GaussianBlurImage method is:
1394 % Image *GaussianBlurImage(const Image *image,onst double radius,
1395 % const double sigma,ExceptionInfo *exception)
1397 % A description of each parameter follows:
1399 % o image: the image.
1401 % o radius: the radius of the Gaussian, in pixels, not counting the center
1404 % o sigma: the standard deviation of the Gaussian, in pixels.
1406 % o exception: return any errors or warnings in this structure.
1409 MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
1410 const double sigma,ExceptionInfo *exception)
1413 geometry[MaxTextExtent];
1421 assert(image != (const Image *) NULL);
1422 assert(image->signature == MagickSignature);
1423 if (image->debug != MagickFalse)
1424 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1425 assert(exception != (ExceptionInfo *) NULL);
1426 assert(exception->signature == MagickSignature);
1427 (void) FormatLocaleString(geometry,MaxTextExtent,"gaussian:%.20gx%.20g",
1429 kernel_info=AcquireKernelInfo(geometry);
1430 if (kernel_info == (KernelInfo *) NULL)
1431 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1432 blur_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1433 UndefinedCompositeOp,0.0,exception);
1434 kernel_info=DestroyKernelInfo(kernel_info);
1439 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1443 % M o t i o n B l u r I m a g e %
1447 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1449 % MotionBlurImage() simulates motion blur. We convolve the image with a
1450 % Gaussian operator of the given radius and standard deviation (sigma).
1451 % For reasonable results, radius should be larger than sigma. Use a
1452 % radius of 0 and MotionBlurImage() selects a suitable radius for you.
1453 % Angle gives the angle of the blurring motion.
1455 % Andrew Protano contributed this effect.
1457 % The format of the MotionBlurImage method is:
1459 % Image *MotionBlurImage(const Image *image,const double radius,
1460 % const double sigma,const double angle,ExceptionInfo *exception)
1462 % A description of each parameter follows:
1464 % o image: the image.
1466 % o radius: the radius of the Gaussian, in pixels, not counting
1469 % o sigma: the standard deviation of the Gaussian, in pixels.
1471 % o angle: Apply the effect along this angle.
1473 % o exception: return any errors or warnings in this structure.
1477 static MagickRealType *GetMotionBlurKernel(const size_t width,
1488 Generate a 1-D convolution kernel.
1490 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1491 kernel=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory((size_t)
1492 width,sizeof(*kernel)));
1493 if (kernel == (MagickRealType *) NULL)
1496 for (i=0; i < (ssize_t) width; i++)
1498 kernel[i]=(MagickRealType) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
1499 MagickSigma)))/(MagickSQ2PI*MagickSigma));
1500 normalize+=kernel[i];
1502 for (i=0; i < (ssize_t) width; i++)
1503 kernel[i]/=normalize;
1507 MagickExport Image *MotionBlurImage(const Image *image,const double radius,
1508 const double sigma,const double angle,ExceptionInfo *exception)
1510 #define BlurImageTag "Blur/Image"
1544 assert(image != (Image *) NULL);
1545 assert(image->signature == MagickSignature);
1546 if (image->debug != MagickFalse)
1547 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1548 assert(exception != (ExceptionInfo *) NULL);
1549 width=GetOptimalKernelWidth1D(radius,sigma);
1550 kernel=GetMotionBlurKernel(width,sigma);
1551 if (kernel == (MagickRealType *) NULL)
1552 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1553 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
1554 if (offset == (OffsetInfo *) NULL)
1556 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
1557 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1559 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1560 if (blur_image == (Image *) NULL)
1562 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
1563 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1564 return((Image *) NULL);
1566 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
1568 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
1569 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1570 blur_image=DestroyImage(blur_image);
1571 return((Image *) NULL);
1573 point.x=(double) width*sin(DegreesToRadians(angle));
1574 point.y=(double) width*cos(DegreesToRadians(angle));
1575 for (i=0; i < (ssize_t) width; i++)
1577 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
1578 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
1585 image_view=AcquireVirtualCacheView(image,exception);
1586 motion_view=AcquireVirtualCacheView(image,exception);
1587 blur_view=AcquireAuthenticCacheView(blur_image,exception);
1588 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1589 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1590 magick_threads(image,blur_image,image->rows,1)
1592 for (y=0; y < (ssize_t) image->rows; y++)
1594 register const Quantum
1603 if (status == MagickFalse)
1605 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1606 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
1608 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1613 for (x=0; x < (ssize_t) image->columns; x++)
1618 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1632 register const Quantum
1635 register MagickRealType
1641 channel=GetPixelChannelChannel(image,i);
1642 traits=GetPixelChannelTraits(image,channel);
1643 blur_traits=GetPixelChannelTraits(blur_image,channel);
1644 if ((traits == UndefinedPixelTrait) ||
1645 (blur_traits == UndefinedPixelTrait))
1647 if (((blur_traits & CopyPixelTrait) != 0) ||
1648 (GetPixelReadMask(image,p) == 0))
1650 SetPixelChannel(blur_image,channel,p[i],q);
1655 if ((blur_traits & BlendPixelTrait) == 0)
1657 for (j=0; j < (ssize_t) width; j++)
1659 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+
1660 offset[j].y,1,1,exception);
1661 if (r == (const Quantum *) NULL)
1669 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
1674 for (j=0; j < (ssize_t) width; j++)
1676 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+offset[j].y,1,
1678 if (r == (const Quantum *) NULL)
1683 alpha=(double) (QuantumScale*GetPixelAlpha(image,r));
1684 pixel+=(*k)*alpha*r[i];
1688 gamma=PerceptibleReciprocal(gamma);
1689 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
1691 p+=GetPixelChannels(image);
1692 q+=GetPixelChannels(blur_image);
1694 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1696 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1701 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1702 #pragma omp critical (MagickCore_MotionBlurImage)
1704 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
1705 if (proceed == MagickFalse)
1709 blur_view=DestroyCacheView(blur_view);
1710 motion_view=DestroyCacheView(motion_view);
1711 image_view=DestroyCacheView(image_view);
1712 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
1713 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
1714 if (status == MagickFalse)
1715 blur_image=DestroyImage(blur_image);
1720 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1724 % P r e v i e w I m a g e %
1728 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1730 % PreviewImage() tiles 9 thumbnails of the specified image with an image
1731 % processing operation applied with varying parameters. This may be helpful
1732 % pin-pointing an appropriate parameter for a particular image processing
1735 % The format of the PreviewImages method is:
1737 % Image *PreviewImages(const Image *image,const PreviewType preview,
1738 % ExceptionInfo *exception)
1740 % A description of each parameter follows:
1742 % o image: the image.
1744 % o preview: the image processing operation.
1746 % o exception: return any errors or warnings in this structure.
1749 MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
1750 ExceptionInfo *exception)
1752 #define NumberTiles 9
1753 #define PreviewImageTag "Preview/Image"
1754 #define DefaultPreviewGeometry "204x204+10+10"
1757 factor[MaxTextExtent],
1758 label[MaxTextExtent];
1803 Open output image file.
1805 assert(image != (Image *) NULL);
1806 assert(image->signature == MagickSignature);
1807 if (image->debug != MagickFalse)
1808 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1812 preview_info=AcquireImageInfo();
1813 SetGeometry(image,&geometry);
1814 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
1815 &geometry.width,&geometry.height);
1816 images=NewImageList();
1818 GetQuantizeInfo(&quantize_info);
1824 for (i=0; i < NumberTiles; i++)
1826 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
1827 if (thumbnail == (Image *) NULL)
1829 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
1831 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel,exception);
1832 if (i == (NumberTiles/2))
1834 (void) QueryColorCompliance("#dfdfdf",AllCompliance,
1835 &thumbnail->matte_color,exception);
1836 AppendImageToList(&images,thumbnail);
1844 preview_image=RotateImage(thumbnail,degrees,exception);
1845 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
1851 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
1852 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",degrees,
1858 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
1859 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
1860 preview_image=RollImage(thumbnail,x,y,exception);
1861 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
1862 (double) x,(double) y);
1867 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1868 if (preview_image == (Image *) NULL)
1870 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",2.0*
1872 (void) ModulateImage(preview_image,factor,exception);
1873 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
1876 case SaturationPreview:
1878 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1879 if (preview_image == (Image *) NULL)
1881 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",2.0*percentage);
1882 (void) ModulateImage(preview_image,factor,exception);
1883 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
1886 case BrightnessPreview:
1888 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1889 if (preview_image == (Image *) NULL)
1891 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
1892 (void) ModulateImage(preview_image,factor,exception);
1893 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
1899 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1900 if (preview_image == (Image *) NULL)
1903 (void) GammaImage(preview_image,gamma,exception);
1904 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
1909 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1910 if (preview_image != (Image *) NULL)
1911 for (x=0; x < i; x++)
1912 (void) ContrastImage(preview_image,MagickTrue,exception);
1913 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
1919 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1920 if (preview_image == (Image *) NULL)
1922 for (x=0; x < i; x++)
1923 (void) ContrastImage(preview_image,MagickFalse,exception);
1924 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
1928 case GrayscalePreview:
1930 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1931 if (preview_image == (Image *) NULL)
1934 quantize_info.number_colors=colors;
1935 quantize_info.colorspace=GRAYColorspace;
1936 (void) QuantizeImage(&quantize_info,preview_image,exception);
1937 (void) FormatLocaleString(label,MaxTextExtent,
1938 "-colorspace gray -colors %.20g",(double) colors);
1941 case QuantizePreview:
1943 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
1944 if (preview_image == (Image *) NULL)
1947 quantize_info.number_colors=colors;
1948 (void) QuantizeImage(&quantize_info,preview_image,exception);
1949 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
1953 case DespecklePreview:
1955 for (x=0; x < (i-1); x++)
1957 preview_image=DespeckleImage(thumbnail,exception);
1958 if (preview_image == (Image *) NULL)
1960 thumbnail=DestroyImage(thumbnail);
1961 thumbnail=preview_image;
1963 preview_image=DespeckleImage(thumbnail,exception);
1964 if (preview_image == (Image *) NULL)
1966 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
1970 case ReduceNoisePreview:
1972 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
1973 (size_t) radius,exception);
1974 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
1977 case AddNoisePreview:
1983 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
1988 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
1993 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
1998 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2003 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2008 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2013 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2017 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2018 (size_t) i,exception);
2019 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
2022 case SharpenPreview:
2024 preview_image=SharpenImage(thumbnail,radius,sigma,exception);
2025 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",radius,
2031 preview_image=BlurImage(thumbnail,radius,sigma,exception);
2032 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
2036 case ThresholdPreview:
2038 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2039 if (preview_image == (Image *) NULL)
2041 (void) BilevelImage(thumbnail,(double) (percentage*((double)
2042 QuantumRange+1.0))/100.0,exception);
2043 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",(double)
2044 (percentage*((double) QuantumRange+1.0))/100.0);
2047 case EdgeDetectPreview:
2049 preview_image=EdgeImage(thumbnail,radius,exception);
2050 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
2055 preview_image=SpreadImage(thumbnail,radius,thumbnail->interpolate,
2057 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",radius+0.5);
2060 case SolarizePreview:
2062 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2063 if (preview_image == (Image *) NULL)
2065 (void) SolarizeImage(preview_image,(double) QuantumRange*percentage/
2067 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
2068 (QuantumRange*percentage)/100.0);
2074 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2076 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",degrees,
2082 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2083 if (preview_image == (Image *) NULL)
2085 geometry.width=(size_t) (2*i+2);
2086 geometry.height=(size_t) (2*i+2);
2089 (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
2090 (void) FormatLocaleString(label,MaxTextExtent,
2091 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
2092 geometry.height,(double) geometry.x,(double) geometry.y);
2095 case SegmentPreview:
2097 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2098 if (preview_image == (Image *) NULL)
2101 (void) SegmentImage(preview_image,sRGBColorspace,MagickFalse,threshold,
2102 threshold,exception);
2103 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
2104 threshold,threshold);
2109 preview_image=SwirlImage(thumbnail,degrees,image->interpolate,
2111 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
2115 case ImplodePreview:
2118 preview_image=ImplodeImage(thumbnail,degrees,image->interpolate,
2120 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
2126 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,
2127 image->interpolate,exception);
2128 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",0.5*degrees,
2132 case OilPaintPreview:
2134 preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2136 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",radius,
2140 case CharcoalDrawingPreview:
2142 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
2144 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",radius,
2151 filename[MaxTextExtent];
2159 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2160 if (preview_image == (Image *) NULL)
2162 preview_info->quality=(size_t) percentage;
2163 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
2164 preview_info->quality);
2165 file=AcquireUniqueFileResource(filename);
2168 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
2169 "jpeg:%s",filename);
2170 status=WriteImage(preview_info,preview_image,exception);
2171 if (status != MagickFalse)
2176 (void) CopyMagickString(preview_info->filename,
2177 preview_image->filename,MaxTextExtent);
2178 quality_image=ReadImage(preview_info,exception);
2179 if (quality_image != (Image *) NULL)
2181 preview_image=DestroyImage(preview_image);
2182 preview_image=quality_image;
2185 (void) RelinquishUniqueFileResource(preview_image->filename);
2186 if ((GetBlobSize(preview_image)/1024) >= 1024)
2187 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
2188 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2191 if (GetBlobSize(preview_image) >= 1024)
2192 (void) FormatLocaleString(label,MaxTextExtent,
2193 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
2194 GetBlobSize(preview_image))/1024.0);
2196 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
2197 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
2201 thumbnail=DestroyImage(thumbnail);
2205 if (preview_image == (Image *) NULL)
2207 (void) DeleteImageProperty(preview_image,"label");
2208 (void) SetImageProperty(preview_image,"label",label,exception);
2209 AppendImageToList(&images,preview_image);
2210 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2212 if (proceed == MagickFalse)
2215 if (images == (Image *) NULL)
2217 preview_info=DestroyImageInfo(preview_info);
2218 return((Image *) NULL);
2223 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2224 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2225 montage_info->shadow=MagickTrue;
2226 (void) CloneString(&montage_info->tile,"3x3");
2227 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2228 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2229 montage_image=MontageImages(images,montage_info,exception);
2230 montage_info=DestroyMontageInfo(montage_info);
2231 images=DestroyImageList(images);
2232 if (montage_image == (Image *) NULL)
2233 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2234 if (montage_image->montage != (char *) NULL)
2237 Free image directory.
2239 montage_image->montage=(char *) RelinquishMagickMemory(
2240 montage_image->montage);
2241 if (image->directory != (char *) NULL)
2242 montage_image->directory=(char *) RelinquishMagickMemory(
2243 montage_image->directory);
2245 preview_info=DestroyImageInfo(preview_info);
2246 return(montage_image);
2250 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2254 % R o t a t i o n a l B l u r I m a g e %
2258 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2260 % RotationalBlurImage() applies a radial blur to the image.
2262 % Andrew Protano contributed this effect.
2264 % The format of the RotationalBlurImage method is:
2266 % Image *RotationalBlurImage(const Image *image,const double angle,
2267 % ExceptionInfo *exception)
2269 % A description of each parameter follows:
2271 % o image: the image.
2273 % o angle: the angle of the radial blur.
2277 % o exception: return any errors or warnings in this structure.
2280 MagickExport Image *RotationalBlurImage(const Image *image,const double angle,
2281 ExceptionInfo *exception)
2317 Allocate blur image.
2319 assert(image != (Image *) NULL);
2320 assert(image->signature == MagickSignature);
2321 if (image->debug != MagickFalse)
2322 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2323 assert(exception != (ExceptionInfo *) NULL);
2324 assert(exception->signature == MagickSignature);
2325 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2326 if (blur_image == (Image *) NULL)
2327 return((Image *) NULL);
2328 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2330 blur_image=DestroyImage(blur_image);
2331 return((Image *) NULL);
2333 blur_center.x=(double) (image->columns-1)/2.0;
2334 blur_center.y=(double) (image->rows-1)/2.0;
2335 blur_radius=hypot(blur_center.x,blur_center.y);
2336 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
2337 theta=DegreesToRadians(angle)/(double) (n-1);
2338 cos_theta=(double *) AcquireQuantumMemory((size_t) n,
2339 sizeof(*cos_theta));
2340 sin_theta=(double *) AcquireQuantumMemory((size_t) n,
2341 sizeof(*sin_theta));
2342 if ((cos_theta == (double *) NULL) ||
2343 (sin_theta == (double *) NULL))
2345 blur_image=DestroyImage(blur_image);
2346 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2348 offset=theta*(double) (n-1)/2.0;
2349 for (i=0; i < (ssize_t) n; i++)
2351 cos_theta[i]=cos((double) (theta*i-offset));
2352 sin_theta[i]=sin((double) (theta*i-offset));
2359 image_view=AcquireVirtualCacheView(image,exception);
2360 radial_view=AcquireVirtualCacheView(image,exception);
2361 blur_view=AcquireAuthenticCacheView(blur_image,exception);
2362 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2363 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2364 magick_threads(image,blur_image,image->rows,1)
2366 for (y=0; y < (ssize_t) image->rows; y++)
2368 register const Quantum
2377 if (status == MagickFalse)
2379 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2380 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2382 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2387 for (x=0; x < (ssize_t) image->columns; x++)
2401 center.x=(double) x-blur_center.x;
2402 center.y=(double) y-blur_center.y;
2403 radius=hypot((double) center.x,center.y);
2408 step=(size_t) (blur_radius/radius);
2415 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2428 register const Quantum
2434 channel=GetPixelChannelChannel(image,i);
2435 traits=GetPixelChannelTraits(image,channel);
2436 blur_traits=GetPixelChannelTraits(blur_image,channel);
2437 if ((traits == UndefinedPixelTrait) ||
2438 (blur_traits == UndefinedPixelTrait))
2440 if (((blur_traits & CopyPixelTrait) != 0) ||
2441 (GetPixelReadMask(image,p) == 0))
2443 SetPixelChannel(blur_image,channel,p[i],q);
2448 if ((blur_traits & BlendPixelTrait) == 0)
2450 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2452 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2453 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2454 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2456 if (r == (const Quantum *) NULL)
2464 gamma=PerceptibleReciprocal(gamma);
2465 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2468 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2470 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2471 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2472 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2474 if (r == (const Quantum *) NULL)
2479 pixel+=GetPixelAlpha(image,r)*r[i];
2480 gamma+=GetPixelAlpha(image,r);
2482 gamma=PerceptibleReciprocal(gamma);
2483 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2485 p+=GetPixelChannels(image);
2486 q+=GetPixelChannels(blur_image);
2488 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2490 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2495 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2496 #pragma omp critical (MagickCore_RotationalBlurImage)
2498 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2499 if (proceed == MagickFalse)
2503 blur_view=DestroyCacheView(blur_view);
2504 radial_view=DestroyCacheView(radial_view);
2505 image_view=DestroyCacheView(image_view);
2506 cos_theta=(double *) RelinquishMagickMemory(cos_theta);
2507 sin_theta=(double *) RelinquishMagickMemory(sin_theta);
2508 if (status == MagickFalse)
2509 blur_image=DestroyImage(blur_image);
2514 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2518 % S e l e c t i v e B l u r I m a g e %
2522 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2524 % SelectiveBlurImage() selectively blur pixels within a contrast threshold.
2525 % It is similar to the unsharpen mask that sharpens everything with contrast
2526 % above a certain threshold.
2528 % The format of the SelectiveBlurImage method is:
2530 % Image *SelectiveBlurImage(const Image *image,const double radius,
2531 % const double sigma,const double threshold,ExceptionInfo *exception)
2533 % A description of each parameter follows:
2535 % o image: the image.
2537 % o radius: the radius of the Gaussian, in pixels, not counting the center
2540 % o sigma: the standard deviation of the Gaussian, in pixels.
2542 % o threshold: only pixels within this contrast threshold are included
2543 % in the blur operation.
2545 % o exception: return any errors or warnings in this structure.
2548 MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
2549 const double sigma,const double threshold,ExceptionInfo *exception)
2551 #define SelectiveBlurImageTag "SelectiveBlur/Image"
2585 Initialize blur image attributes.
2587 assert(image != (Image *) NULL);
2588 assert(image->signature == MagickSignature);
2589 if (image->debug != MagickFalse)
2590 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2591 assert(exception != (ExceptionInfo *) NULL);
2592 assert(exception->signature == MagickSignature);
2593 width=GetOptimalKernelWidth1D(radius,sigma);
2594 kernel=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory((size_t)
2595 width,width*sizeof(*kernel)));
2596 if (kernel == (MagickRealType *) NULL)
2597 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2598 j=(ssize_t) (width-1)/2;
2600 for (v=(-j); v <= j; v++)
2602 for (u=(-j); u <= j; u++)
2603 kernel[i++]=(MagickRealType) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
2604 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
2606 if (image->debug != MagickFalse)
2609 format[MaxTextExtent],
2612 register const MagickRealType
2619 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
2620 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
2622 message=AcquireString("");
2624 for (v=0; v < (ssize_t) width; v++)
2627 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
2628 (void) ConcatenateString(&message,format);
2629 for (u=0; u < (ssize_t) width; u++)
2631 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",(double) *k++);
2632 (void) ConcatenateString(&message,format);
2634 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
2636 message=DestroyString(message);
2638 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2639 if (blur_image == (Image *) NULL)
2640 return((Image *) NULL);
2641 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2643 blur_image=DestroyImage(blur_image);
2644 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2645 return((Image *) NULL);
2647 luminance_image=CloneImage(image,0,0,MagickTrue,exception);
2648 if (luminance_image == (Image *) NULL)
2650 blur_image=DestroyImage(blur_image);
2651 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2652 return((Image *) NULL);
2654 status=TransformImageColorspace(luminance_image,GRAYColorspace,exception);
2655 if (status == MagickFalse)
2657 luminance_image=DestroyImage(luminance_image);
2658 blur_image=DestroyImage(blur_image);
2659 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2660 return((Image *) NULL);
2663 Threshold blur image.
2667 center=(ssize_t) (GetPixelChannels(image)*(image->columns+width)*
2668 ((width-1)/2L)+GetPixelChannels(image)*((width-1)/2L));
2669 image_view=AcquireVirtualCacheView(image,exception);
2670 luminance_view=AcquireVirtualCacheView(luminance_image,exception);
2671 blur_view=AcquireAuthenticCacheView(blur_image,exception);
2672 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2673 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2674 magick_threads(image,blur_image,image->rows,1)
2676 for (y=0; y < (ssize_t) image->rows; y++)
2684 register const Quantum
2694 if (status == MagickFalse)
2696 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) (width-1)/2L),y-(ssize_t)
2697 ((width-1)/2L),image->columns+width,width,exception);
2698 l=GetCacheViewVirtualPixels(luminance_view,-((ssize_t) (width-1)/2L),y-
2699 (ssize_t) ((width-1)/2L),luminance_image->columns+width,width,exception);
2700 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2702 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2707 for (x=0; x < (ssize_t) image->columns; x++)
2715 intensity=GetPixelIntensity(image,p+center);
2716 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2730 register const MagickRealType
2733 register const Quantum
2734 *restrict luminance_pixels,
2743 channel=GetPixelChannelChannel(image,i);
2744 traits=GetPixelChannelTraits(image,channel);
2745 blur_traits=GetPixelChannelTraits(blur_image,channel);
2746 if ((traits == UndefinedPixelTrait) ||
2747 (blur_traits == UndefinedPixelTrait))
2749 if (((blur_traits & CopyPixelTrait) != 0) ||
2750 (GetPixelReadMask(image,p+center) == 0))
2752 SetPixelChannel(blur_image,channel,p[center+i],q);
2760 if ((blur_traits & BlendPixelTrait) == 0)
2762 for (v=0; v < (ssize_t) width; v++)
2764 for (u=0; u < (ssize_t) width; u++)
2766 contrast=GetPixelIntensity(luminance_image,luminance_pixels)-
2768 if (fabs(contrast) < threshold)
2770 pixel+=(*k)*pixels[i];
2774 pixels+=GetPixelChannels(image);
2775 luminance_pixels+=GetPixelChannels(luminance_image);
2777 pixels+=(image->columns-1)*GetPixelChannels(image);
2778 luminance_pixels+=luminance_image->columns*
2779 GetPixelChannels(luminance_image);
2781 if (fabs((double) gamma) < MagickEpsilon)
2783 SetPixelChannel(blur_image,channel,p[center+i],q);
2786 gamma=PerceptibleReciprocal(gamma);
2787 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2790 for (v=0; v < (ssize_t) width; v++)
2792 for (u=0; u < (ssize_t) width; u++)
2794 contrast=GetPixelIntensity(image,pixels)-intensity;
2795 if (fabs(contrast) < threshold)
2797 alpha=(double) (QuantumScale*GetPixelAlpha(image,pixels));
2798 pixel+=(*k)*alpha*pixels[i];
2802 pixels+=GetPixelChannels(image);
2803 luminance_pixels+=GetPixelChannels(luminance_image);
2805 pixels+=(image->columns-1)*GetPixelChannels(image);
2806 luminance_pixels+=luminance_image->columns*
2807 GetPixelChannels(luminance_image);
2809 if (fabs((double) gamma) < MagickEpsilon)
2811 SetPixelChannel(blur_image,channel,p[center+i],q);
2814 gamma=PerceptibleReciprocal(gamma);
2815 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2817 p+=GetPixelChannels(image);
2818 l+=GetPixelChannels(luminance_image);
2819 q+=GetPixelChannels(blur_image);
2821 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
2822 if (sync == MagickFalse)
2824 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2829 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2830 #pragma omp critical (MagickCore_SelectiveBlurImage)
2832 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
2834 if (proceed == MagickFalse)
2838 blur_image->type=image->type;
2839 blur_view=DestroyCacheView(blur_view);
2840 image_view=DestroyCacheView(image_view);
2841 luminance_image=DestroyImage(luminance_image);
2842 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2843 if (status == MagickFalse)
2844 blur_image=DestroyImage(blur_image);
2849 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2853 % S h a d e I m a g e %
2857 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2859 % ShadeImage() shines a distant light on an image to create a
2860 % three-dimensional effect. You control the positioning of the light with
2861 % azimuth and elevation; azimuth is measured in degrees off the x axis
2862 % and elevation is measured in pixels above the Z axis.
2864 % The format of the ShadeImage method is:
2866 % Image *ShadeImage(const Image *image,const MagickBooleanType gray,
2867 % const double azimuth,const double elevation,ExceptionInfo *exception)
2869 % A description of each parameter follows:
2871 % o image: the image.
2873 % o gray: A value other than zero shades the intensity of each pixel.
2875 % o azimuth, elevation: Define the light source direction.
2877 % o exception: return any errors or warnings in this structure.
2880 MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
2881 const double azimuth,const double elevation,ExceptionInfo *exception)
2883 #define ShadeImageTag "Shade/Image"
2906 Initialize shaded image attributes.
2908 assert(image != (const Image *) NULL);
2909 assert(image->signature == MagickSignature);
2910 if (image->debug != MagickFalse)
2911 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2912 assert(exception != (ExceptionInfo *) NULL);
2913 assert(exception->signature == MagickSignature);
2914 linear_image=CloneImage(image,0,0,MagickTrue,exception);
2915 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2916 if ((linear_image == (Image *) NULL) || (shade_image == (Image *) NULL))
2918 if (linear_image != (Image *) NULL)
2919 linear_image=DestroyImage(linear_image);
2920 if (shade_image != (Image *) NULL)
2921 shade_image=DestroyImage(shade_image);
2922 return((Image *) NULL);
2924 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
2926 linear_image=DestroyImage(linear_image);
2927 shade_image=DestroyImage(shade_image);
2928 return((Image *) NULL);
2931 Compute the light vector.
2933 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
2934 cos(DegreesToRadians(elevation));
2935 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
2936 cos(DegreesToRadians(elevation));
2937 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
2943 image_view=AcquireVirtualCacheView(linear_image,exception);
2944 shade_view=AcquireAuthenticCacheView(shade_image,exception);
2945 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2946 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2947 magick_threads(linear_image,shade_image,linear_image->rows,1)
2949 for (y=0; y < (ssize_t) linear_image->rows; y++)
2959 register const Quantum
2971 if (status == MagickFalse)
2973 p=GetCacheViewVirtualPixels(image_view,-1,y-1,linear_image->columns+2,3,
2975 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
2977 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2983 Shade this row of pixels.
2985 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
2986 pre=p+GetPixelChannels(linear_image);
2987 center=pre+(linear_image->columns+2)*GetPixelChannels(linear_image);
2988 post=center+(linear_image->columns+2)*GetPixelChannels(linear_image);
2989 for (x=0; x < (ssize_t) linear_image->columns; x++)
2995 Determine the surface normal and compute shading.
2998 GetPixelIntensity(linear_image,pre-GetPixelChannels(linear_image))+
2999 GetPixelIntensity(linear_image,center-GetPixelChannels(linear_image))+
3000 GetPixelIntensity(linear_image,post-GetPixelChannels(linear_image))-
3001 GetPixelIntensity(linear_image,pre+GetPixelChannels(linear_image))-
3002 GetPixelIntensity(linear_image,center+GetPixelChannels(linear_image))-
3003 GetPixelIntensity(linear_image,post+GetPixelChannels(linear_image)));
3005 GetPixelIntensity(linear_image,post-GetPixelChannels(linear_image))+
3006 GetPixelIntensity(linear_image,post)+
3007 GetPixelIntensity(linear_image,post+GetPixelChannels(linear_image))-
3008 GetPixelIntensity(linear_image,pre-GetPixelChannels(linear_image))-
3009 GetPixelIntensity(linear_image,pre)-
3010 GetPixelIntensity(linear_image,pre+GetPixelChannels(linear_image)));
3011 if ((normal.x == 0.0) && (normal.y == 0.0))
3016 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3017 if (distance > MagickEpsilon)
3019 normal_distance=normal.x*normal.x+normal.y*normal.y+
3021 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3022 shade=distance/sqrt((double) normal_distance);
3025 for (i=0; i < (ssize_t) GetPixelChannels(linear_image); i++)
3034 channel=GetPixelChannelChannel(linear_image,i);
3035 traits=GetPixelChannelTraits(linear_image,channel);
3036 shade_traits=GetPixelChannelTraits(shade_image,channel);
3037 if ((traits == UndefinedPixelTrait) ||
3038 (shade_traits == UndefinedPixelTrait))
3040 if (((shade_traits & CopyPixelTrait) != 0) ||
3041 (GetPixelReadMask(linear_image,center) == 0))
3043 SetPixelChannel(shade_image,channel,center[i],q);
3046 if (gray != MagickFalse)
3048 SetPixelChannel(shade_image,channel,ClampToQuantum(shade),q);
3051 SetPixelChannel(shade_image,channel,ClampToQuantum(QuantumScale*shade*
3054 pre+=GetPixelChannels(linear_image);
3055 center+=GetPixelChannels(linear_image);
3056 post+=GetPixelChannels(linear_image);
3057 q+=GetPixelChannels(shade_image);
3059 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3061 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3066 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3067 #pragma omp critical (MagickCore_ShadeImage)
3069 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3070 if (proceed == MagickFalse)
3074 shade_view=DestroyCacheView(shade_view);
3075 image_view=DestroyCacheView(image_view);
3076 linear_image=DestroyImage(linear_image);
3077 if (status == MagickFalse)
3078 shade_image=DestroyImage(shade_image);
3079 return(shade_image);
3083 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3087 % S h a r p e n I m a g e %
3091 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3093 % SharpenImage() sharpens the image. We convolve the image with a Gaussian
3094 % operator of the given radius and standard deviation (sigma). For
3095 % reasonable results, radius should be larger than sigma. Use a radius of 0
3096 % and SharpenImage() selects a suitable radius for you.
3098 % Using a separable kernel would be faster, but the negative weights cancel
3099 % out on the corners of the kernel producing often undesirable ringing in the
3100 % filtered result; this can be avoided by using a 2D gaussian shaped image
3101 % sharpening kernel instead.
3103 % The format of the SharpenImage method is:
3105 % Image *SharpenImage(const Image *image,const double radius,
3106 % const double sigma,ExceptionInfo *exception)
3108 % A description of each parameter follows:
3110 % o image: the image.
3112 % o radius: the radius of the Gaussian, in pixels, not counting the center
3115 % o sigma: the standard deviation of the Laplacian, in pixels.
3117 % o exception: return any errors or warnings in this structure.
3120 MagickExport Image *SharpenImage(const Image *image,const double radius,
3121 const double sigma,ExceptionInfo *exception)
3144 assert(image != (const Image *) NULL);
3145 assert(image->signature == MagickSignature);
3146 if (image->debug != MagickFalse)
3147 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3148 assert(exception != (ExceptionInfo *) NULL);
3149 assert(exception->signature == MagickSignature);
3150 width=GetOptimalKernelWidth2D(radius,sigma);
3151 kernel_info=AcquireKernelInfo((const char *) NULL);
3152 if (kernel_info == (KernelInfo *) NULL)
3153 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3154 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3155 kernel_info->width=width;
3156 kernel_info->height=width;
3157 kernel_info->x=(ssize_t) (width-1)/2;
3158 kernel_info->y=(ssize_t) (width-1)/2;
3159 kernel_info->signature=MagickSignature;
3160 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
3161 AcquireAlignedMemory(kernel_info->width,kernel_info->height*
3162 sizeof(*kernel_info->values)));
3163 if (kernel_info->values == (MagickRealType *) NULL)
3165 kernel_info=DestroyKernelInfo(kernel_info);
3166 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3169 j=(ssize_t) (kernel_info->width-1)/2;
3171 for (v=(-j); v <= j; v++)
3173 for (u=(-j); u <= j; u++)
3175 kernel_info->values[i]=(MagickRealType) (-exp(-((double) u*u+v*v)/(2.0*
3176 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3177 normalize+=kernel_info->values[i];
3181 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
3183 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
3184 normalize+=kernel_info->values[i];
3185 gamma=PerceptibleReciprocal(normalize);
3186 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
3187 kernel_info->values[i]*=gamma;
3188 sharp_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
3189 UndefinedCompositeOp,0.0,exception);
3190 kernel_info=DestroyKernelInfo(kernel_info);
3191 return(sharp_image);
3195 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3199 % S p r e a d I m a g e %
3203 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3205 % SpreadImage() is a special effects method that randomly displaces each
3206 % pixel in a block defined by the radius parameter.
3208 % The format of the SpreadImage method is:
3210 % Image *SpreadImage(const Image *image,const double radius,
3211 % const PixelInterpolateMethod method,ExceptionInfo *exception)
3213 % A description of each parameter follows:
3215 % o image: the image.
3217 % o radius: choose a random pixel in a neighborhood of this extent.
3219 % o method: the pixel interpolation method.
3221 % o exception: return any errors or warnings in this structure.
3224 MagickExport Image *SpreadImage(const Image *image,const double radius,
3225 const PixelInterpolateMethod method,ExceptionInfo *exception)
3227 #define SpreadImageTag "Spread/Image"
3243 **restrict random_info;
3251 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3257 Initialize spread image attributes.
3259 assert(image != (Image *) NULL);
3260 assert(image->signature == MagickSignature);
3261 if (image->debug != MagickFalse)
3262 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3263 assert(exception != (ExceptionInfo *) NULL);
3264 assert(exception->signature == MagickSignature);
3265 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3267 if (spread_image == (Image *) NULL)
3268 return((Image *) NULL);
3269 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
3271 spread_image=DestroyImage(spread_image);
3272 return((Image *) NULL);
3279 width=GetOptimalKernelWidth1D(radius,0.5);
3280 random_info=AcquireRandomInfoThreadSet();
3281 image_view=AcquireVirtualCacheView(image,exception);
3282 spread_view=AcquireAuthenticCacheView(spread_image,exception);
3283 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3284 key=GetRandomSecretKey(random_info[0]);
3285 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3286 magick_threads(image,spread_image,image->rows,key == ~0UL)
3288 for (y=0; y < (ssize_t) image->rows; y++)
3291 id = GetOpenMPThreadId();
3299 if (status == MagickFalse)
3301 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
3303 if (q == (Quantum *) NULL)
3308 for (x=0; x < (ssize_t) image->columns; x++)
3313 point.x=GetPseudoRandomValue(random_info[id]);
3314 point.y=GetPseudoRandomValue(random_info[id]);
3315 status=InterpolatePixelChannels(image,image_view,spread_image,method,
3316 (double) x+width*point.x-0.5,(double) y+width*point.y-0.5,q,exception);
3317 q+=GetPixelChannels(spread_image);
3319 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
3321 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3326 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3327 #pragma omp critical (MagickCore_SpreadImage)
3329 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3330 if (proceed == MagickFalse)
3334 spread_view=DestroyCacheView(spread_view);
3335 image_view=DestroyCacheView(image_view);
3336 random_info=DestroyRandomInfoThreadSet(random_info);
3337 if (status == MagickFalse)
3338 spread_image=DestroyImage(spread_image);
3339 return(spread_image);
3343 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3347 % U n s h a r p M a s k I m a g e %
3351 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3353 % UnsharpMaskImage() sharpens one or more image channels. We convolve the
3354 % image with a Gaussian operator of the given radius and standard deviation
3355 % (sigma). For reasonable results, radius should be larger than sigma. Use a
3356 % radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
3358 % The format of the UnsharpMaskImage method is:
3360 % Image *UnsharpMaskImage(const Image *image,const double radius,
3361 % const double sigma,const double amount,const double threshold,
3362 % ExceptionInfo *exception)
3364 % A description of each parameter follows:
3366 % o image: the image.
3368 % o radius: the radius of the Gaussian, in pixels, not counting the center
3371 % o sigma: the standard deviation of the Gaussian, in pixels.
3373 % o gain: the percentage of the difference between the original and the
3374 % blur image that is added back into the original.
3376 % o threshold: the threshold in pixels needed to apply the diffence gain.
3378 % o exception: return any errors or warnings in this structure.
3381 MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
3382 const double sigma,const double gain,const double threshold,
3383 ExceptionInfo *exception)
3385 #define SharpenImageTag "Sharpen/Image"
3406 assert(image != (const Image *) NULL);
3407 assert(image->signature == MagickSignature);
3408 if (image->debug != MagickFalse)
3409 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3410 assert(exception != (ExceptionInfo *) NULL);
3411 unsharp_image=BlurImage(image,radius,sigma,exception);
3412 if (unsharp_image == (Image *) NULL)
3413 return((Image *) NULL);
3414 quantum_threshold=(double) QuantumRange*threshold;
3420 image_view=AcquireVirtualCacheView(image,exception);
3421 unsharp_view=AcquireAuthenticCacheView(unsharp_image,exception);
3422 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3423 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3424 magick_threads(image,unsharp_image,image->rows,1)
3426 for (y=0; y < (ssize_t) image->rows; y++)
3428 register const Quantum
3437 if (status == MagickFalse)
3439 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3440 q=QueueCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
3442 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3447 for (x=0; x < (ssize_t) image->columns; x++)
3452 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3464 channel=GetPixelChannelChannel(image,i);
3465 traits=GetPixelChannelTraits(image,channel);
3466 unsharp_traits=GetPixelChannelTraits(unsharp_image,channel);
3467 if ((traits == UndefinedPixelTrait) ||
3468 (unsharp_traits == UndefinedPixelTrait))
3470 if (((unsharp_traits & CopyPixelTrait) != 0) ||
3471 (GetPixelReadMask(image,p) != 0))
3473 SetPixelChannel(unsharp_image,channel,p[i],q);
3476 pixel=p[i]-(double) GetPixelChannel(unsharp_image,channel,q);
3477 if (fabs(2.0*pixel) < quantum_threshold)
3478 pixel=(double) p[i];
3480 pixel=(double) p[i]+gain*pixel;
3481 SetPixelChannel(unsharp_image,channel,ClampToQuantum(pixel),q);
3483 p+=GetPixelChannels(image);
3484 q+=GetPixelChannels(unsharp_image);
3486 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
3488 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3493 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3494 #pragma omp critical (MagickCore_UnsharpMaskImage)
3496 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
3497 if (proceed == MagickFalse)
3501 unsharp_image->type=image->type;
3502 unsharp_view=DestroyCacheView(unsharp_view);
3503 image_view=DestroyCacheView(image_view);
3504 if (status == MagickFalse)
3505 unsharp_image=DestroyImage(unsharp_image);
3506 return(unsharp_image);