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 a n n y E d g e I m a g e %
857 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
859 % CannyEdgeImage() uses a multi-stage algorithm to detect a wide range of
862 % The format of the EdgeImage method is:
864 % Image *CannyEdgeImage(const Image *image,const double radius,
865 % const double sigma,const double lower_precent,
866 % const double upper_percent,ExceptionInfo *exception)
868 % A description of each parameter follows:
870 % o image: the image.
872 % o channel: the channel type.
874 % o radius: the radius of the gaussian smoothing filter.
876 % o sigma: the sigma of the gaussian smoothing filter.
878 % o lower_precent: percentage of edge pixels in the lower threshold.
880 % o upper_percent: percentage of edge pixels in the upper threshold.
882 % o exception: return any errors or warnings in this structure.
886 typedef struct _CannyInfo
900 static inline MagickBooleanType IsAuthenticPixel(const Image *image,
901 const ssize_t x,const ssize_t y)
903 if ((x < 0) || (x >= (ssize_t) image->columns))
905 if ((y < 0) || (y >= (ssize_t) image->rows))
910 static MagickBooleanType TraceEdges(Image *edge_image,CacheView *trace_view,
911 MatrixInfo *pixel_cache,const ssize_t x,const ssize_t y,
912 const double lower_threshold,ExceptionInfo *exception)
920 if (GetMatrixElement(pixel_cache,0,0,&pixel) == MagickFalse)
924 if (SetMatrixElement(pixel_cache,0,0,&pixel) == MagickFalse)
938 status=GetMatrixElement(pixel_cache,(ssize_t) number_edges,0,&pixel);
939 if (status == MagickFalse)
943 for (v=(-1); v <= 1; v++)
948 for (u=(-1); u <= 1; u++)
953 if ((u == 0) && (v == 0))
955 if (IsAuthenticPixel(edge_image,x_offset+u,y_offset+v) == MagickFalse)
958 Not an edge if gradient value is below the lower threshold.
960 q=GetCacheViewAuthenticPixels(trace_view,x_offset+u,y_offset+v,1,1,
962 if (q == (Quantum *) NULL)
964 status=GetMatrixElement(pixel_cache,x_offset+u,y_offset+v,&pixel);
965 if (status == MagickFalse)
967 if ((pixel.intensity >= lower_threshold) &&
968 (GetPixelIntensity(edge_image,q) == 0))
971 status=SyncCacheViewAuthenticPixels(trace_view,exception);
972 if (status == MagickFalse)
974 status=GetMatrixElement(pixel_cache,(ssize_t) number_edges,0,
976 if (status == MagickFalse)
980 status=SetMatrixElement(pixel_cache,(ssize_t) number_edges,0,
982 if (status == MagickFalse)
988 } while (number_edges != 0);
993 MagickExport Image *CannyEdgeImage(const Image *image,const double radius,
994 const double sigma,const double lower_precent,const double upper_percent,
995 ExceptionInfo *exception)
1002 geometry[MaxTextExtent];
1031 assert(image != (const Image *) NULL);
1032 assert(image->signature == MagickSignature);
1033 if (image->debug != MagickFalse)
1034 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1035 assert(exception != (ExceptionInfo *) NULL);
1036 assert(exception->signature == MagickSignature);
1040 (void) FormatLocaleString(geometry,MaxTextExtent,
1041 "blur:%.20gx%.20g;blur:%.20gx%.20g+90",radius,sigma,radius,sigma);
1042 kernel_info=AcquireKernelInfo(geometry);
1043 if (kernel_info == (KernelInfo *) NULL)
1044 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1045 edge_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1046 UndefinedCompositeOp,0.0,exception);
1047 kernel_info=DestroyKernelInfo(kernel_info);
1048 if (edge_image == (Image *) NULL)
1049 return((Image *) NULL);
1050 if (SetImageColorspace(edge_image,GRAYColorspace,exception) == MagickFalse)
1052 edge_image=DestroyImage(edge_image);
1053 return((Image *) NULL);
1056 Find the intensity gradient of the image.
1058 pixel_cache=AcquireMatrixInfo(edge_image->columns,edge_image->rows,
1059 sizeof(CannyInfo),exception);
1060 if (pixel_cache == (MatrixInfo *) NULL)
1062 edge_image=DestroyImage(edge_image);
1063 return((Image *) NULL);
1066 edge_view=AcquireVirtualCacheView(edge_image,exception);
1067 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1068 #pragma omp parallel for schedule(static,4) shared(status) \
1069 magick_threads(edge_image,edge_image,edge_image->rows,1)
1071 for (y=0; y < (ssize_t) edge_image->rows; y++)
1073 register const Quantum
1079 if (status == MagickFalse)
1081 p=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns+1,2,
1083 if (p == (const Quantum *) NULL)
1088 for (x=0; x < (ssize_t) edge_image->columns; x++)
1100 register const Quantum
1101 *restrict kernel_pixels;
1118 (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
1122 for (v=0; v < 2; v++)
1127 for (u=0; u < 2; u++)
1132 intensity=GetPixelIntensity(edge_image,kernel_pixels+u);
1133 dx+=3.0*Gx[v][u]*intensity/2.0;
1134 dy+=3.0*Gy[v][u]*intensity/2.0;
1136 kernel_pixels+=edge_image->columns+1;
1138 pixel.magnitude=sqrt(dx*dx+dy*dy);
1139 pixel.orientation=0;
1140 if (fabs(dx) > MagickEpsilon)
1148 if (theta < -2.41421356237)
1149 pixel.orientation=2;
1151 if (theta < -0.414213562373)
1152 pixel.orientation=1;
1154 pixel.orientation=0;
1158 if (theta > 2.41421356237)
1159 pixel.orientation=2;
1161 if (theta > 0.414213562373)
1162 pixel.orientation=3;
1164 pixel.orientation=0;
1167 if (SetMatrixElement(pixel_cache,x,y,&pixel) == MagickFalse)
1169 p+=GetPixelChannels(edge_image);
1172 edge_view=DestroyCacheView(edge_view);
1174 Non-maxima suppression, remove pixels that are not considered to be part
1177 histogram=(size_t *) AcquireQuantumMemory(65536,sizeof(*histogram));
1178 if (histogram == (size_t *) NULL)
1180 pixel_cache=DestroyMatrixInfo(pixel_cache);
1181 edge_image=DestroyImage(edge_image);
1182 return((Image *) NULL);
1184 (void) ResetMagickMemory(histogram,0,65536*sizeof(*histogram));
1185 edge_view=AcquireAuthenticCacheView(edge_image,exception);
1186 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1187 #pragma omp parallel for schedule(static,4) shared(status) \
1188 magick_threads(edge_image,edge_image,edge_image->rows,1)
1190 for (y=0; y < (ssize_t) edge_image->rows; y++)
1198 if (status == MagickFalse)
1200 q=GetCacheViewAuthenticPixels(edge_view,0,y,edge_image->columns,1,
1202 if (q == (Quantum *) NULL)
1207 for (x=0; x < (ssize_t) edge_image->columns; x++)
1214 (void) GetMatrixElement(pixel_cache,x,y,&pixel);
1215 switch (pixel.orientation)
1222 (void) GetMatrixElement(pixel_cache,x-1,y,&alpha_pixel);
1223 (void) GetMatrixElement(pixel_cache,x+1,y,&beta_pixel);
1231 (void) GetMatrixElement(pixel_cache,x-1,y-1,&alpha_pixel);
1232 (void) GetMatrixElement(pixel_cache,x+1,y+1,&beta_pixel);
1240 (void) GetMatrixElement(pixel_cache,x,y-1,&alpha_pixel);
1241 (void) GetMatrixElement(pixel_cache,x,y+1,&beta_pixel);
1249 (void) GetMatrixElement(pixel_cache,x-1,y+1,&alpha_pixel);
1250 (void) GetMatrixElement(pixel_cache,x+1,y-1,&beta_pixel);
1254 pixel.intensity=pixel.magnitude;
1255 if ((pixel.magnitude < alpha_pixel.magnitude) ||
1256 (pixel.magnitude < beta_pixel.magnitude))
1259 if (pixel.magnitude > QuantumRange)
1260 pixel.intensity=QuantumRange;
1261 (void) SetMatrixElement(pixel_cache,x,y,&pixel);
1262 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1263 #pragma omp critical (MagickCore_CannyEdgeImage)
1265 histogram[ScaleQuantumToShort(ClampToQuantum(pixel.intensity))]++;
1267 q+=GetPixelChannels(edge_image);
1269 if (SyncCacheViewAuthenticPixels(edge_view,exception) == MagickFalse)
1272 edge_view=DestroyCacheView(edge_view);
1274 Estimate hysteresis threshold.
1276 number_pixels=(size_t) (lower_precent*(image->columns*image->rows-
1279 for (i=65535; count < (ssize_t) number_pixels; i--)
1280 count+=histogram[i];
1281 upper_threshold=(double) ScaleShortToQuantum((unsigned short) i);
1282 for (i=0; histogram[i] == 0; i++) ;
1283 lower_threshold=upper_percent*(upper_threshold+
1284 ScaleShortToQuantum((unsigned short) i));
1285 histogram=(size_t *) RelinquishMagickMemory(histogram);
1287 Hysteresis threshold.
1289 edge_view=AcquireAuthenticCacheView(edge_image,exception);
1290 trace_view=AcquireAuthenticCacheView(edge_image,exception);
1291 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1292 #pragma omp parallel for schedule(static,4) shared(status) \
1293 magick_threads(edge_image,edge_image,edge_image->rows,1)
1295 for (y=0; y < (ssize_t) edge_image->rows; y++)
1300 if (status == MagickFalse)
1302 for (x=0; x < (ssize_t) edge_image->columns; x++)
1311 Edge if pixel gradient higher than upper threshold.
1313 status=GetMatrixElement(pixel_cache,x,y,&pixel);
1314 if (status == MagickFalse)
1316 q=GetCacheViewAuthenticPixels(edge_view,x,y,1,1,exception);
1317 if (q == (PixelPacket *) NULL)
1322 if ((pixel.intensity >= upper_threshold) &&
1323 (GetPixelIntensity(edge_image,q) == 0))
1326 status=SyncCacheViewAuthenticPixels(edge_view,exception);
1327 if (status == MagickFalse)
1329 status=TraceEdges(edge_image,trace_view,pixel_cache,x,y,
1330 lower_threshold,exception);
1331 if (status == MagickFalse)
1335 if (SyncCacheViewAuthenticPixels(edge_view,exception) == MagickFalse)
1338 trace_view=DestroyCacheView(trace_view);
1339 edge_view=DestroyCacheView(edge_view);
1340 pixel_cache=DestroyMatrixInfo(pixel_cache);
1345 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1349 % C o n v o l v e I m a g e %
1353 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1355 % ConvolveImage() applies a custom convolution kernel to the image.
1357 % The format of the ConvolveImage method is:
1359 % Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
1360 % ExceptionInfo *exception)
1362 % A description of each parameter follows:
1364 % o image: the image.
1366 % o kernel: the filtering kernel.
1368 % o exception: return any errors or warnings in this structure.
1371 MagickExport Image *ConvolveImage(const Image *image,
1372 const KernelInfo *kernel_info,ExceptionInfo *exception)
1377 convolve_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1378 UndefinedCompositeOp,0.0,exception);
1379 return(convolve_image);
1383 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1387 % D e s p e c k l e I m a g e %
1391 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1393 % DespeckleImage() reduces the speckle noise in an image while perserving the
1394 % edges of the original image. A speckle removing filter uses a complementary % hulling technique (raising pixels that are darker than their surrounding
1395 % neighbors, then complementarily lowering pixels that are brighter than their
1396 % surrounding neighbors) to reduce the speckle index of that image (reference
1397 % Crimmins speckle removal).
1399 % The format of the DespeckleImage method is:
1401 % Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1403 % A description of each parameter follows:
1405 % o image: the image.
1407 % o exception: return any errors or warnings in this structure.
1411 static void Hull(const Image *image,const ssize_t x_offset,
1412 const ssize_t y_offset,const size_t columns,const size_t rows,
1413 const int polarity,Quantum *restrict f,Quantum *restrict g)
1424 assert(image != (const Image *) NULL);
1425 assert(image->signature == MagickSignature);
1426 if (image->debug != MagickFalse)
1427 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1428 assert(f != (Quantum *) NULL);
1429 assert(g != (Quantum *) NULL);
1432 r=p+(y_offset*(columns+2)+x_offset);
1433 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1434 #pragma omp parallel for schedule(static,4) \
1435 magick_threads(image,image,1,1)
1437 for (y=0; y < (ssize_t) rows; y++)
1446 i=(2*y+1)+y*columns;
1448 for (x=0; x < (ssize_t) columns; x++)
1450 v=(MagickRealType) p[i];
1451 if ((MagickRealType) r[i] >= (v+ScaleCharToQuantum(2)))
1452 v+=ScaleCharToQuantum(1);
1457 for (x=0; x < (ssize_t) columns; x++)
1459 v=(MagickRealType) p[i];
1460 if ((MagickRealType) r[i] <= (v-ScaleCharToQuantum(2)))
1461 v-=ScaleCharToQuantum(1);
1468 r=q+(y_offset*(columns+2)+x_offset);
1469 s=q-(y_offset*(columns+2)+x_offset);
1470 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1471 #pragma omp parallel for schedule(static,4) \
1472 magick_threads(image,image,1,1)
1474 for (y=0; y < (ssize_t) rows; y++)
1483 i=(2*y+1)+y*columns;
1485 for (x=0; x < (ssize_t) columns; x++)
1487 v=(MagickRealType) q[i];
1488 if (((MagickRealType) s[i] >= (v+ScaleCharToQuantum(2))) &&
1489 ((MagickRealType) r[i] > v))
1490 v+=ScaleCharToQuantum(1);
1495 for (x=0; x < (ssize_t) columns; x++)
1497 v=(MagickRealType) q[i];
1498 if (((MagickRealType) s[i] <= (v-ScaleCharToQuantum(2))) &&
1499 ((MagickRealType) r[i] < v))
1500 v-=ScaleCharToQuantum(1);
1507 MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1509 #define DespeckleImageTag "Despeckle/Image"
1535 static const ssize_t
1536 X[4] = {0, 1, 1,-1},
1537 Y[4] = {1, 0, 1, 1};
1540 Allocate despeckled image.
1542 assert(image != (const Image *) NULL);
1543 assert(image->signature == MagickSignature);
1544 if (image->debug != MagickFalse)
1545 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1546 assert(exception != (ExceptionInfo *) NULL);
1547 assert(exception->signature == MagickSignature);
1548 despeckle_image=CloneImage(image,0,0,MagickTrue,exception);
1549 if (despeckle_image == (Image *) NULL)
1550 return((Image *) NULL);
1551 status=SetImageStorageClass(despeckle_image,DirectClass,exception);
1552 if (status == MagickFalse)
1554 despeckle_image=DestroyImage(despeckle_image);
1555 return((Image *) NULL);
1558 Allocate image buffer.
1560 length=(size_t) ((image->columns+2)*(image->rows+2));
1561 pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
1562 buffer_info=AcquireVirtualMemory(length,sizeof(*buffer));
1563 if ((pixel_info == (MemoryInfo *) NULL) ||
1564 (buffer_info == (MemoryInfo *) NULL))
1566 if (buffer_info != (MemoryInfo *) NULL)
1567 buffer_info=RelinquishVirtualMemory(buffer_info);
1568 if (pixel_info != (MemoryInfo *) NULL)
1569 pixel_info=RelinquishVirtualMemory(pixel_info);
1570 despeckle_image=DestroyImage(despeckle_image);
1571 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1573 pixels=(Quantum *) GetVirtualMemoryBlob(pixel_info);
1574 buffer=(Quantum *) GetVirtualMemoryBlob(buffer_info);
1576 Reduce speckle in the image.
1579 image_view=AcquireVirtualCacheView(image,exception);
1580 despeckle_view=AcquireAuthenticCacheView(despeckle_image,exception);
1581 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1598 if (status == MagickFalse)
1600 channel=GetPixelChannelChannel(image,i);
1601 traits=GetPixelChannelTraits(image,channel);
1602 despeckle_traits=GetPixelChannelTraits(despeckle_image,channel);
1603 if ((traits == UndefinedPixelTrait) ||
1604 (despeckle_traits == UndefinedPixelTrait))
1606 if ((despeckle_traits & CopyPixelTrait) != 0)
1608 (void) ResetMagickMemory(pixels,0,length*sizeof(*pixels));
1609 j=(ssize_t) image->columns+2;
1610 for (y=0; y < (ssize_t) image->rows; y++)
1612 register const Quantum
1615 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1616 if (p == (const Quantum *) NULL)
1622 for (x=0; x < (ssize_t) image->columns; x++)
1625 p+=GetPixelChannels(image);
1629 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
1630 for (k=0; k < 4; k++)
1632 Hull(image,X[k],Y[k],image->columns,image->rows,1,pixels,buffer);
1633 Hull(image,-X[k],-Y[k],image->columns,image->rows,1,pixels,buffer);
1634 Hull(image,-X[k],-Y[k],image->columns,image->rows,-1,pixels,buffer);
1635 Hull(image,X[k],Y[k],image->columns,image->rows,-1,pixels,buffer);
1637 j=(ssize_t) image->columns+2;
1638 for (y=0; y < (ssize_t) image->rows; y++)
1646 q=GetCacheViewAuthenticPixels(despeckle_view,0,y,despeckle_image->columns,
1648 if (q == (Quantum *) NULL)
1654 for (x=0; x < (ssize_t) image->columns; x++)
1656 SetPixelChannel(despeckle_image,channel,pixels[j++],q);
1657 q+=GetPixelChannels(despeckle_image);
1659 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1660 if (sync == MagickFalse)
1664 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1669 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1670 GetPixelChannels(image));
1671 if (proceed == MagickFalse)
1675 despeckle_view=DestroyCacheView(despeckle_view);
1676 image_view=DestroyCacheView(image_view);
1677 buffer_info=RelinquishVirtualMemory(buffer_info);
1678 pixel_info=RelinquishVirtualMemory(pixel_info);
1679 despeckle_image->type=image->type;
1680 if (status == MagickFalse)
1681 despeckle_image=DestroyImage(despeckle_image);
1682 return(despeckle_image);
1686 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1690 % E d g e I m a g e %
1694 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1696 % EdgeImage() finds edges in an image. Radius defines the radius of the
1697 % convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1700 % The format of the EdgeImage method is:
1702 % Image *EdgeImage(const Image *image,const double radius,
1703 % ExceptionInfo *exception)
1705 % A description of each parameter follows:
1707 % o image: the image.
1709 % o radius: the radius of the pixel neighborhood.
1711 % o exception: return any errors or warnings in this structure.
1714 MagickExport Image *EdgeImage(const Image *image,const double radius,
1715 ExceptionInfo *exception)
1729 assert(image != (const Image *) NULL);
1730 assert(image->signature == MagickSignature);
1731 if (image->debug != MagickFalse)
1732 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1733 assert(exception != (ExceptionInfo *) NULL);
1734 assert(exception->signature == MagickSignature);
1735 width=GetOptimalKernelWidth1D(radius,0.5);
1736 kernel_info=AcquireKernelInfo((const char *) NULL);
1737 if (kernel_info == (KernelInfo *) NULL)
1738 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1739 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1740 kernel_info->width=width;
1741 kernel_info->height=width;
1742 kernel_info->x=(ssize_t) (kernel_info->width-1)/2;
1743 kernel_info->y=(ssize_t) (kernel_info->height-1)/2;
1744 kernel_info->signature=MagickSignature;
1745 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
1746 AcquireAlignedMemory(kernel_info->width,kernel_info->height*
1747 sizeof(*kernel_info->values)));
1748 if (kernel_info->values == (MagickRealType *) NULL)
1750 kernel_info=DestroyKernelInfo(kernel_info);
1751 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1753 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
1754 kernel_info->values[i]=(-1.0);
1755 kernel_info->values[i/2]=(double) kernel_info->width*kernel_info->height-1.0;
1756 edge_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1757 UndefinedCompositeOp,0.0,exception);
1758 kernel_info=DestroyKernelInfo(kernel_info);
1763 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1767 % E m b o s s I m a g e %
1771 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1773 % EmbossImage() returns a grayscale image with a three-dimensional effect.
1774 % We convolve the image with a Gaussian operator of the given radius and
1775 % standard deviation (sigma). For reasonable results, radius should be
1776 % larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1779 % The format of the EmbossImage method is:
1781 % Image *EmbossImage(const Image *image,const double radius,
1782 % const double sigma,ExceptionInfo *exception)
1784 % A description of each parameter follows:
1786 % o image: the image.
1788 % o radius: the radius of the pixel neighborhood.
1790 % o sigma: the standard deviation of the Gaussian, in pixels.
1792 % o exception: return any errors or warnings in this structure.
1795 MagickExport Image *EmbossImage(const Image *image,const double radius,
1796 const double sigma,ExceptionInfo *exception)
1820 assert(image != (const Image *) NULL);
1821 assert(image->signature == MagickSignature);
1822 if (image->debug != MagickFalse)
1823 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1824 assert(exception != (ExceptionInfo *) NULL);
1825 assert(exception->signature == MagickSignature);
1826 width=GetOptimalKernelWidth1D(radius,sigma);
1827 kernel_info=AcquireKernelInfo((const char *) NULL);
1828 if (kernel_info == (KernelInfo *) NULL)
1829 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1830 kernel_info->width=width;
1831 kernel_info->height=width;
1832 kernel_info->x=(ssize_t) (width-1)/2;
1833 kernel_info->y=(ssize_t) (width-1)/2;
1834 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
1835 AcquireAlignedMemory(kernel_info->width,kernel_info->width*
1836 sizeof(*kernel_info->values)));
1837 if (kernel_info->values == (MagickRealType *) NULL)
1839 kernel_info=DestroyKernelInfo(kernel_info);
1840 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1842 j=(ssize_t) (kernel_info->width-1)/2;
1845 for (v=(-j); v <= j; v++)
1847 for (u=(-j); u <= j; u++)
1849 kernel_info->values[i]=(MagickRealType) (((u < 0) || (v < 0) ? -8.0 :
1850 8.0)*exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
1851 (2.0*MagickPI*MagickSigma*MagickSigma));
1853 kernel_info->values[i]=0.0;
1859 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
1860 normalize+=kernel_info->values[i];
1861 gamma=PerceptibleReciprocal(normalize);
1862 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
1863 kernel_info->values[i]*=gamma;
1864 emboss_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1865 UndefinedCompositeOp,0.0,exception);
1866 kernel_info=DestroyKernelInfo(kernel_info);
1867 if (emboss_image != (Image *) NULL)
1868 (void) EqualizeImage(emboss_image,exception);
1869 return(emboss_image);
1873 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1877 % G a u s s i a n B l u r I m a g e %
1881 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1883 % GaussianBlurImage() blurs an image. We convolve the image with a
1884 % Gaussian operator of the given radius and standard deviation (sigma).
1885 % For reasonable results, the radius should be larger than sigma. Use a
1886 % radius of 0 and GaussianBlurImage() selects a suitable radius for you
1888 % The format of the GaussianBlurImage method is:
1890 % Image *GaussianBlurImage(const Image *image,onst double radius,
1891 % const double sigma,ExceptionInfo *exception)
1893 % A description of each parameter follows:
1895 % o image: the image.
1897 % o radius: the radius of the Gaussian, in pixels, not counting the center
1900 % o sigma: the standard deviation of the Gaussian, in pixels.
1902 % o exception: return any errors or warnings in this structure.
1905 MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
1906 const double sigma,ExceptionInfo *exception)
1909 geometry[MaxTextExtent];
1917 assert(image != (const Image *) NULL);
1918 assert(image->signature == MagickSignature);
1919 if (image->debug != MagickFalse)
1920 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1921 assert(exception != (ExceptionInfo *) NULL);
1922 assert(exception->signature == MagickSignature);
1923 (void) FormatLocaleString(geometry,MaxTextExtent,"gaussian:%.20gx%.20g",
1925 kernel_info=AcquireKernelInfo(geometry);
1926 if (kernel_info == (KernelInfo *) NULL)
1927 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1928 blur_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1929 UndefinedCompositeOp,0.0,exception);
1930 kernel_info=DestroyKernelInfo(kernel_info);
1935 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1939 % M o t i o n B l u r I m a g e %
1943 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1945 % MotionBlurImage() simulates motion blur. We convolve the image with a
1946 % Gaussian operator of the given radius and standard deviation (sigma).
1947 % For reasonable results, radius should be larger than sigma. Use a
1948 % radius of 0 and MotionBlurImage() selects a suitable radius for you.
1949 % Angle gives the angle of the blurring motion.
1951 % Andrew Protano contributed this effect.
1953 % The format of the MotionBlurImage method is:
1955 % Image *MotionBlurImage(const Image *image,const double radius,
1956 % const double sigma,const double angle,ExceptionInfo *exception)
1958 % A description of each parameter follows:
1960 % o image: the image.
1962 % o radius: the radius of the Gaussian, in pixels, not counting
1965 % o sigma: the standard deviation of the Gaussian, in pixels.
1967 % o angle: Apply the effect along this angle.
1969 % o exception: return any errors or warnings in this structure.
1973 static MagickRealType *GetMotionBlurKernel(const size_t width,
1984 Generate a 1-D convolution kernel.
1986 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1987 kernel=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory((size_t)
1988 width,sizeof(*kernel)));
1989 if (kernel == (MagickRealType *) NULL)
1992 for (i=0; i < (ssize_t) width; i++)
1994 kernel[i]=(MagickRealType) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
1995 MagickSigma)))/(MagickSQ2PI*MagickSigma));
1996 normalize+=kernel[i];
1998 for (i=0; i < (ssize_t) width; i++)
1999 kernel[i]/=normalize;
2003 MagickExport Image *MotionBlurImage(const Image *image,const double radius,
2004 const double sigma,const double angle,ExceptionInfo *exception)
2006 #define BlurImageTag "Blur/Image"
2040 assert(image != (Image *) NULL);
2041 assert(image->signature == MagickSignature);
2042 if (image->debug != MagickFalse)
2043 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2044 assert(exception != (ExceptionInfo *) NULL);
2045 width=GetOptimalKernelWidth1D(radius,sigma);
2046 kernel=GetMotionBlurKernel(width,sigma);
2047 if (kernel == (MagickRealType *) NULL)
2048 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2049 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
2050 if (offset == (OffsetInfo *) NULL)
2052 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2053 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2055 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2056 if (blur_image == (Image *) NULL)
2058 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2059 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2060 return((Image *) NULL);
2062 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2064 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2065 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2066 blur_image=DestroyImage(blur_image);
2067 return((Image *) NULL);
2069 point.x=(double) width*sin(DegreesToRadians(angle));
2070 point.y=(double) width*cos(DegreesToRadians(angle));
2071 for (i=0; i < (ssize_t) width; i++)
2073 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
2074 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
2081 image_view=AcquireVirtualCacheView(image,exception);
2082 motion_view=AcquireVirtualCacheView(image,exception);
2083 blur_view=AcquireAuthenticCacheView(blur_image,exception);
2084 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2085 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2086 magick_threads(image,blur_image,image->rows,1)
2088 for (y=0; y < (ssize_t) image->rows; y++)
2090 register const Quantum
2099 if (status == MagickFalse)
2101 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2102 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2104 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2109 for (x=0; x < (ssize_t) image->columns; x++)
2114 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2128 register const Quantum
2131 register MagickRealType
2137 channel=GetPixelChannelChannel(image,i);
2138 traits=GetPixelChannelTraits(image,channel);
2139 blur_traits=GetPixelChannelTraits(blur_image,channel);
2140 if ((traits == UndefinedPixelTrait) ||
2141 (blur_traits == UndefinedPixelTrait))
2143 if (((blur_traits & CopyPixelTrait) != 0) ||
2144 (GetPixelReadMask(image,p) == 0))
2146 SetPixelChannel(blur_image,channel,p[i],q);
2151 if ((blur_traits & BlendPixelTrait) == 0)
2153 for (j=0; j < (ssize_t) width; j++)
2155 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+
2156 offset[j].y,1,1,exception);
2157 if (r == (const Quantum *) NULL)
2165 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
2170 for (j=0; j < (ssize_t) width; j++)
2172 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+offset[j].y,1,
2174 if (r == (const Quantum *) NULL)
2179 alpha=(double) (QuantumScale*GetPixelAlpha(image,r));
2180 pixel+=(*k)*alpha*r[i];
2184 gamma=PerceptibleReciprocal(gamma);
2185 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2187 p+=GetPixelChannels(image);
2188 q+=GetPixelChannels(blur_image);
2190 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2192 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2197 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2198 #pragma omp critical (MagickCore_MotionBlurImage)
2200 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2201 if (proceed == MagickFalse)
2205 blur_view=DestroyCacheView(blur_view);
2206 motion_view=DestroyCacheView(motion_view);
2207 image_view=DestroyCacheView(image_view);
2208 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2209 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2210 if (status == MagickFalse)
2211 blur_image=DestroyImage(blur_image);
2216 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2220 % P r e v i e w I m a g e %
2224 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2226 % PreviewImage() tiles 9 thumbnails of the specified image with an image
2227 % processing operation applied with varying parameters. This may be helpful
2228 % pin-pointing an appropriate parameter for a particular image processing
2231 % The format of the PreviewImages method is:
2233 % Image *PreviewImages(const Image *image,const PreviewType preview,
2234 % ExceptionInfo *exception)
2236 % A description of each parameter follows:
2238 % o image: the image.
2240 % o preview: the image processing operation.
2242 % o exception: return any errors or warnings in this structure.
2245 MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2246 ExceptionInfo *exception)
2248 #define NumberTiles 9
2249 #define PreviewImageTag "Preview/Image"
2250 #define DefaultPreviewGeometry "204x204+10+10"
2253 factor[MaxTextExtent],
2254 label[MaxTextExtent];
2299 Open output image file.
2301 assert(image != (Image *) NULL);
2302 assert(image->signature == MagickSignature);
2303 if (image->debug != MagickFalse)
2304 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2308 preview_info=AcquireImageInfo();
2309 SetGeometry(image,&geometry);
2310 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2311 &geometry.width,&geometry.height);
2312 images=NewImageList();
2314 GetQuantizeInfo(&quantize_info);
2320 for (i=0; i < NumberTiles; i++)
2322 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2323 if (thumbnail == (Image *) NULL)
2325 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2327 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel,exception);
2328 if (i == (NumberTiles/2))
2330 (void) QueryColorCompliance("#dfdfdf",AllCompliance,
2331 &thumbnail->matte_color,exception);
2332 AppendImageToList(&images,thumbnail);
2340 preview_image=RotateImage(thumbnail,degrees,exception);
2341 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
2347 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
2348 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",degrees,
2354 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2355 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
2356 preview_image=RollImage(thumbnail,x,y,exception);
2357 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
2358 (double) x,(double) y);
2363 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2364 if (preview_image == (Image *) NULL)
2366 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",2.0*
2368 (void) ModulateImage(preview_image,factor,exception);
2369 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2372 case SaturationPreview:
2374 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2375 if (preview_image == (Image *) NULL)
2377 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",2.0*percentage);
2378 (void) ModulateImage(preview_image,factor,exception);
2379 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2382 case BrightnessPreview:
2384 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2385 if (preview_image == (Image *) NULL)
2387 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
2388 (void) ModulateImage(preview_image,factor,exception);
2389 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2395 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2396 if (preview_image == (Image *) NULL)
2399 (void) GammaImage(preview_image,gamma,exception);
2400 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
2405 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2406 if (preview_image != (Image *) NULL)
2407 for (x=0; x < i; x++)
2408 (void) ContrastImage(preview_image,MagickTrue,exception);
2409 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
2415 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2416 if (preview_image == (Image *) NULL)
2418 for (x=0; x < i; x++)
2419 (void) ContrastImage(preview_image,MagickFalse,exception);
2420 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
2424 case GrayscalePreview:
2426 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2427 if (preview_image == (Image *) NULL)
2430 quantize_info.number_colors=colors;
2431 quantize_info.colorspace=GRAYColorspace;
2432 (void) QuantizeImage(&quantize_info,preview_image,exception);
2433 (void) FormatLocaleString(label,MaxTextExtent,
2434 "-colorspace gray -colors %.20g",(double) colors);
2437 case QuantizePreview:
2439 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2440 if (preview_image == (Image *) NULL)
2443 quantize_info.number_colors=colors;
2444 (void) QuantizeImage(&quantize_info,preview_image,exception);
2445 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
2449 case DespecklePreview:
2451 for (x=0; x < (i-1); x++)
2453 preview_image=DespeckleImage(thumbnail,exception);
2454 if (preview_image == (Image *) NULL)
2456 thumbnail=DestroyImage(thumbnail);
2457 thumbnail=preview_image;
2459 preview_image=DespeckleImage(thumbnail,exception);
2460 if (preview_image == (Image *) NULL)
2462 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
2466 case ReduceNoisePreview:
2468 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2469 (size_t) radius,exception);
2470 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
2473 case AddNoisePreview:
2479 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2484 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2489 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2494 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2499 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2504 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2509 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2513 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2514 (size_t) i,exception);
2515 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
2518 case SharpenPreview:
2520 preview_image=SharpenImage(thumbnail,radius,sigma,exception);
2521 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",radius,
2527 preview_image=BlurImage(thumbnail,radius,sigma,exception);
2528 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
2532 case ThresholdPreview:
2534 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2535 if (preview_image == (Image *) NULL)
2537 (void) BilevelImage(thumbnail,(double) (percentage*((double)
2538 QuantumRange+1.0))/100.0,exception);
2539 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",(double)
2540 (percentage*((double) QuantumRange+1.0))/100.0);
2543 case EdgeDetectPreview:
2545 preview_image=EdgeImage(thumbnail,radius,exception);
2546 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
2551 preview_image=SpreadImage(thumbnail,radius,thumbnail->interpolate,
2553 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",radius+0.5);
2556 case SolarizePreview:
2558 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2559 if (preview_image == (Image *) NULL)
2561 (void) SolarizeImage(preview_image,(double) QuantumRange*percentage/
2563 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
2564 (QuantumRange*percentage)/100.0);
2570 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2572 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",degrees,
2578 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2579 if (preview_image == (Image *) NULL)
2581 geometry.width=(size_t) (2*i+2);
2582 geometry.height=(size_t) (2*i+2);
2585 (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
2586 (void) FormatLocaleString(label,MaxTextExtent,
2587 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
2588 geometry.height,(double) geometry.x,(double) geometry.y);
2591 case SegmentPreview:
2593 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2594 if (preview_image == (Image *) NULL)
2597 (void) SegmentImage(preview_image,sRGBColorspace,MagickFalse,threshold,
2598 threshold,exception);
2599 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
2600 threshold,threshold);
2605 preview_image=SwirlImage(thumbnail,degrees,image->interpolate,
2607 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
2611 case ImplodePreview:
2614 preview_image=ImplodeImage(thumbnail,degrees,image->interpolate,
2616 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
2622 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,
2623 image->interpolate,exception);
2624 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",0.5*degrees,
2628 case OilPaintPreview:
2630 preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2632 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",radius,
2636 case CharcoalDrawingPreview:
2638 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
2640 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",radius,
2647 filename[MaxTextExtent];
2655 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2656 if (preview_image == (Image *) NULL)
2658 preview_info->quality=(size_t) percentage;
2659 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
2660 preview_info->quality);
2661 file=AcquireUniqueFileResource(filename);
2664 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
2665 "jpeg:%s",filename);
2666 status=WriteImage(preview_info,preview_image,exception);
2667 if (status != MagickFalse)
2672 (void) CopyMagickString(preview_info->filename,
2673 preview_image->filename,MaxTextExtent);
2674 quality_image=ReadImage(preview_info,exception);
2675 if (quality_image != (Image *) NULL)
2677 preview_image=DestroyImage(preview_image);
2678 preview_image=quality_image;
2681 (void) RelinquishUniqueFileResource(preview_image->filename);
2682 if ((GetBlobSize(preview_image)/1024) >= 1024)
2683 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
2684 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2687 if (GetBlobSize(preview_image) >= 1024)
2688 (void) FormatLocaleString(label,MaxTextExtent,
2689 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
2690 GetBlobSize(preview_image))/1024.0);
2692 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
2693 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
2697 thumbnail=DestroyImage(thumbnail);
2701 if (preview_image == (Image *) NULL)
2703 (void) DeleteImageProperty(preview_image,"label");
2704 (void) SetImageProperty(preview_image,"label",label,exception);
2705 AppendImageToList(&images,preview_image);
2706 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2708 if (proceed == MagickFalse)
2711 if (images == (Image *) NULL)
2713 preview_info=DestroyImageInfo(preview_info);
2714 return((Image *) NULL);
2719 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2720 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2721 montage_info->shadow=MagickTrue;
2722 (void) CloneString(&montage_info->tile,"3x3");
2723 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2724 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2725 montage_image=MontageImages(images,montage_info,exception);
2726 montage_info=DestroyMontageInfo(montage_info);
2727 images=DestroyImageList(images);
2728 if (montage_image == (Image *) NULL)
2729 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2730 if (montage_image->montage != (char *) NULL)
2733 Free image directory.
2735 montage_image->montage=(char *) RelinquishMagickMemory(
2736 montage_image->montage);
2737 if (image->directory != (char *) NULL)
2738 montage_image->directory=(char *) RelinquishMagickMemory(
2739 montage_image->directory);
2741 preview_info=DestroyImageInfo(preview_info);
2742 return(montage_image);
2746 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2750 % R o t a t i o n a l B l u r I m a g e %
2754 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2756 % RotationalBlurImage() applies a radial blur to the image.
2758 % Andrew Protano contributed this effect.
2760 % The format of the RotationalBlurImage method is:
2762 % Image *RotationalBlurImage(const Image *image,const double angle,
2763 % ExceptionInfo *exception)
2765 % A description of each parameter follows:
2767 % o image: the image.
2769 % o angle: the angle of the radial blur.
2773 % o exception: return any errors or warnings in this structure.
2776 MagickExport Image *RotationalBlurImage(const Image *image,const double angle,
2777 ExceptionInfo *exception)
2813 Allocate blur image.
2815 assert(image != (Image *) NULL);
2816 assert(image->signature == MagickSignature);
2817 if (image->debug != MagickFalse)
2818 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2819 assert(exception != (ExceptionInfo *) NULL);
2820 assert(exception->signature == MagickSignature);
2821 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2822 if (blur_image == (Image *) NULL)
2823 return((Image *) NULL);
2824 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2826 blur_image=DestroyImage(blur_image);
2827 return((Image *) NULL);
2829 blur_center.x=(double) (image->columns-1)/2.0;
2830 blur_center.y=(double) (image->rows-1)/2.0;
2831 blur_radius=hypot(blur_center.x,blur_center.y);
2832 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
2833 theta=DegreesToRadians(angle)/(double) (n-1);
2834 cos_theta=(double *) AcquireQuantumMemory((size_t) n,
2835 sizeof(*cos_theta));
2836 sin_theta=(double *) AcquireQuantumMemory((size_t) n,
2837 sizeof(*sin_theta));
2838 if ((cos_theta == (double *) NULL) ||
2839 (sin_theta == (double *) NULL))
2841 blur_image=DestroyImage(blur_image);
2842 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2844 offset=theta*(double) (n-1)/2.0;
2845 for (i=0; i < (ssize_t) n; i++)
2847 cos_theta[i]=cos((double) (theta*i-offset));
2848 sin_theta[i]=sin((double) (theta*i-offset));
2855 image_view=AcquireVirtualCacheView(image,exception);
2856 radial_view=AcquireVirtualCacheView(image,exception);
2857 blur_view=AcquireAuthenticCacheView(blur_image,exception);
2858 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2859 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2860 magick_threads(image,blur_image,image->rows,1)
2862 for (y=0; y < (ssize_t) image->rows; y++)
2864 register const Quantum
2873 if (status == MagickFalse)
2875 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2876 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2878 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2883 for (x=0; x < (ssize_t) image->columns; x++)
2897 center.x=(double) x-blur_center.x;
2898 center.y=(double) y-blur_center.y;
2899 radius=hypot((double) center.x,center.y);
2904 step=(size_t) (blur_radius/radius);
2911 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2924 register const Quantum
2930 channel=GetPixelChannelChannel(image,i);
2931 traits=GetPixelChannelTraits(image,channel);
2932 blur_traits=GetPixelChannelTraits(blur_image,channel);
2933 if ((traits == UndefinedPixelTrait) ||
2934 (blur_traits == UndefinedPixelTrait))
2936 if (((blur_traits & CopyPixelTrait) != 0) ||
2937 (GetPixelReadMask(image,p) == 0))
2939 SetPixelChannel(blur_image,channel,p[i],q);
2944 if ((blur_traits & BlendPixelTrait) == 0)
2946 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2948 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2949 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2950 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2952 if (r == (const Quantum *) NULL)
2960 gamma=PerceptibleReciprocal(gamma);
2961 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2964 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2966 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2967 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2968 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2970 if (r == (const Quantum *) NULL)
2975 pixel+=GetPixelAlpha(image,r)*r[i];
2976 gamma+=GetPixelAlpha(image,r);
2978 gamma=PerceptibleReciprocal(gamma);
2979 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2981 p+=GetPixelChannels(image);
2982 q+=GetPixelChannels(blur_image);
2984 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2986 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2991 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2992 #pragma omp critical (MagickCore_RotationalBlurImage)
2994 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2995 if (proceed == MagickFalse)
2999 blur_view=DestroyCacheView(blur_view);
3000 radial_view=DestroyCacheView(radial_view);
3001 image_view=DestroyCacheView(image_view);
3002 cos_theta=(double *) RelinquishMagickMemory(cos_theta);
3003 sin_theta=(double *) RelinquishMagickMemory(sin_theta);
3004 if (status == MagickFalse)
3005 blur_image=DestroyImage(blur_image);
3010 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3014 % S e l e c t i v e B l u r I m a g e %
3018 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3020 % SelectiveBlurImage() selectively blur pixels within a contrast threshold.
3021 % It is similar to the unsharpen mask that sharpens everything with contrast
3022 % above a certain threshold.
3024 % The format of the SelectiveBlurImage method is:
3026 % Image *SelectiveBlurImage(const Image *image,const double radius,
3027 % const double sigma,const double threshold,ExceptionInfo *exception)
3029 % A description of each parameter follows:
3031 % o image: the image.
3033 % o radius: the radius of the Gaussian, in pixels, not counting the center
3036 % o sigma: the standard deviation of the Gaussian, in pixels.
3038 % o threshold: only pixels within this contrast threshold are included
3039 % in the blur operation.
3041 % o exception: return any errors or warnings in this structure.
3044 MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
3045 const double sigma,const double threshold,ExceptionInfo *exception)
3047 #define SelectiveBlurImageTag "SelectiveBlur/Image"
3081 Initialize blur image attributes.
3083 assert(image != (Image *) NULL);
3084 assert(image->signature == MagickSignature);
3085 if (image->debug != MagickFalse)
3086 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3087 assert(exception != (ExceptionInfo *) NULL);
3088 assert(exception->signature == MagickSignature);
3089 width=GetOptimalKernelWidth1D(radius,sigma);
3090 kernel=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory((size_t)
3091 width,width*sizeof(*kernel)));
3092 if (kernel == (MagickRealType *) NULL)
3093 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3094 j=(ssize_t) (width-1)/2;
3096 for (v=(-j); v <= j; v++)
3098 for (u=(-j); u <= j; u++)
3099 kernel[i++]=(MagickRealType) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
3100 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3102 if (image->debug != MagickFalse)
3105 format[MaxTextExtent],
3108 register const MagickRealType
3115 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
3116 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
3118 message=AcquireString("");
3120 for (v=0; v < (ssize_t) width; v++)
3123 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
3124 (void) ConcatenateString(&message,format);
3125 for (u=0; u < (ssize_t) width; u++)
3127 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",(double) *k++);
3128 (void) ConcatenateString(&message,format);
3130 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
3132 message=DestroyString(message);
3134 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3135 if (blur_image == (Image *) NULL)
3136 return((Image *) NULL);
3137 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
3139 blur_image=DestroyImage(blur_image);
3140 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3141 return((Image *) NULL);
3143 luminance_image=CloneImage(image,0,0,MagickTrue,exception);
3144 if (luminance_image == (Image *) NULL)
3146 blur_image=DestroyImage(blur_image);
3147 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3148 return((Image *) NULL);
3150 status=TransformImageColorspace(luminance_image,GRAYColorspace,exception);
3151 if (status == MagickFalse)
3153 luminance_image=DestroyImage(luminance_image);
3154 blur_image=DestroyImage(blur_image);
3155 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3156 return((Image *) NULL);
3159 Threshold blur image.
3163 center=(ssize_t) (GetPixelChannels(image)*(image->columns+width)*
3164 ((width-1)/2L)+GetPixelChannels(image)*((width-1)/2L));
3165 image_view=AcquireVirtualCacheView(image,exception);
3166 luminance_view=AcquireVirtualCacheView(luminance_image,exception);
3167 blur_view=AcquireAuthenticCacheView(blur_image,exception);
3168 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3169 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3170 magick_threads(image,blur_image,image->rows,1)
3172 for (y=0; y < (ssize_t) image->rows; y++)
3180 register const Quantum
3190 if (status == MagickFalse)
3192 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) (width-1)/2L),y-(ssize_t)
3193 ((width-1)/2L),image->columns+width,width,exception);
3194 l=GetCacheViewVirtualPixels(luminance_view,-((ssize_t) (width-1)/2L),y-
3195 (ssize_t) ((width-1)/2L),luminance_image->columns+width,width,exception);
3196 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3198 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3203 for (x=0; x < (ssize_t) image->columns; x++)
3211 intensity=GetPixelIntensity(image,p+center);
3212 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3226 register const MagickRealType
3229 register const Quantum
3230 *restrict luminance_pixels,
3239 channel=GetPixelChannelChannel(image,i);
3240 traits=GetPixelChannelTraits(image,channel);
3241 blur_traits=GetPixelChannelTraits(blur_image,channel);
3242 if ((traits == UndefinedPixelTrait) ||
3243 (blur_traits == UndefinedPixelTrait))
3245 if (((blur_traits & CopyPixelTrait) != 0) ||
3246 (GetPixelReadMask(image,p+center) == 0))
3248 SetPixelChannel(blur_image,channel,p[center+i],q);
3256 if ((blur_traits & BlendPixelTrait) == 0)
3258 for (v=0; v < (ssize_t) width; v++)
3260 for (u=0; u < (ssize_t) width; u++)
3262 contrast=GetPixelIntensity(luminance_image,luminance_pixels)-
3264 if (fabs(contrast) < threshold)
3266 pixel+=(*k)*pixels[i];
3270 pixels+=GetPixelChannels(image);
3271 luminance_pixels+=GetPixelChannels(luminance_image);
3273 pixels+=(image->columns-1)*GetPixelChannels(image);
3274 luminance_pixels+=luminance_image->columns*
3275 GetPixelChannels(luminance_image);
3277 if (fabs((double) gamma) < MagickEpsilon)
3279 SetPixelChannel(blur_image,channel,p[center+i],q);
3282 gamma=PerceptibleReciprocal(gamma);
3283 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3286 for (v=0; v < (ssize_t) width; v++)
3288 for (u=0; u < (ssize_t) width; u++)
3290 contrast=GetPixelIntensity(image,pixels)-intensity;
3291 if (fabs(contrast) < threshold)
3293 alpha=(double) (QuantumScale*GetPixelAlpha(image,pixels));
3294 pixel+=(*k)*alpha*pixels[i];
3298 pixels+=GetPixelChannels(image);
3299 luminance_pixels+=GetPixelChannels(luminance_image);
3301 pixels+=(image->columns-1)*GetPixelChannels(image);
3302 luminance_pixels+=luminance_image->columns*
3303 GetPixelChannels(luminance_image);
3305 if (fabs((double) gamma) < MagickEpsilon)
3307 SetPixelChannel(blur_image,channel,p[center+i],q);
3310 gamma=PerceptibleReciprocal(gamma);
3311 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3313 p+=GetPixelChannels(image);
3314 l+=GetPixelChannels(luminance_image);
3315 q+=GetPixelChannels(blur_image);
3317 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3318 if (sync == MagickFalse)
3320 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3325 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3326 #pragma omp critical (MagickCore_SelectiveBlurImage)
3328 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3330 if (proceed == MagickFalse)
3334 blur_image->type=image->type;
3335 blur_view=DestroyCacheView(blur_view);
3336 image_view=DestroyCacheView(image_view);
3337 luminance_image=DestroyImage(luminance_image);
3338 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3339 if (status == MagickFalse)
3340 blur_image=DestroyImage(blur_image);
3345 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3349 % S h a d e I m a g e %
3353 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3355 % ShadeImage() shines a distant light on an image to create a
3356 % three-dimensional effect. You control the positioning of the light with
3357 % azimuth and elevation; azimuth is measured in degrees off the x axis
3358 % and elevation is measured in pixels above the Z axis.
3360 % The format of the ShadeImage method is:
3362 % Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3363 % const double azimuth,const double elevation,ExceptionInfo *exception)
3365 % A description of each parameter follows:
3367 % o image: the image.
3369 % o gray: A value other than zero shades the intensity of each pixel.
3371 % o azimuth, elevation: Define the light source direction.
3373 % o exception: return any errors or warnings in this structure.
3376 MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3377 const double azimuth,const double elevation,ExceptionInfo *exception)
3379 #define ShadeImageTag "Shade/Image"
3402 Initialize shaded image attributes.
3404 assert(image != (const Image *) NULL);
3405 assert(image->signature == MagickSignature);
3406 if (image->debug != MagickFalse)
3407 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3408 assert(exception != (ExceptionInfo *) NULL);
3409 assert(exception->signature == MagickSignature);
3410 linear_image=CloneImage(image,0,0,MagickTrue,exception);
3411 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3412 if ((linear_image == (Image *) NULL) || (shade_image == (Image *) NULL))
3414 if (linear_image != (Image *) NULL)
3415 linear_image=DestroyImage(linear_image);
3416 if (shade_image != (Image *) NULL)
3417 shade_image=DestroyImage(shade_image);
3418 return((Image *) NULL);
3420 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
3422 linear_image=DestroyImage(linear_image);
3423 shade_image=DestroyImage(shade_image);
3424 return((Image *) NULL);
3427 Compute the light vector.
3429 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3430 cos(DegreesToRadians(elevation));
3431 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3432 cos(DegreesToRadians(elevation));
3433 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3439 image_view=AcquireVirtualCacheView(linear_image,exception);
3440 shade_view=AcquireAuthenticCacheView(shade_image,exception);
3441 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3442 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3443 magick_threads(linear_image,shade_image,linear_image->rows,1)
3445 for (y=0; y < (ssize_t) linear_image->rows; y++)
3455 register const Quantum
3467 if (status == MagickFalse)
3469 p=GetCacheViewVirtualPixels(image_view,-1,y-1,linear_image->columns+2,3,
3471 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3473 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3479 Shade this row of pixels.
3481 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
3482 pre=p+GetPixelChannels(linear_image);
3483 center=pre+(linear_image->columns+2)*GetPixelChannels(linear_image);
3484 post=center+(linear_image->columns+2)*GetPixelChannels(linear_image);
3485 for (x=0; x < (ssize_t) linear_image->columns; x++)
3491 Determine the surface normal and compute shading.
3494 GetPixelIntensity(linear_image,pre-GetPixelChannels(linear_image))+
3495 GetPixelIntensity(linear_image,center-GetPixelChannels(linear_image))+
3496 GetPixelIntensity(linear_image,post-GetPixelChannels(linear_image))-
3497 GetPixelIntensity(linear_image,pre+GetPixelChannels(linear_image))-
3498 GetPixelIntensity(linear_image,center+GetPixelChannels(linear_image))-
3499 GetPixelIntensity(linear_image,post+GetPixelChannels(linear_image)));
3501 GetPixelIntensity(linear_image,post-GetPixelChannels(linear_image))+
3502 GetPixelIntensity(linear_image,post)+
3503 GetPixelIntensity(linear_image,post+GetPixelChannels(linear_image))-
3504 GetPixelIntensity(linear_image,pre-GetPixelChannels(linear_image))-
3505 GetPixelIntensity(linear_image,pre)-
3506 GetPixelIntensity(linear_image,pre+GetPixelChannels(linear_image)));
3507 if ((normal.x == 0.0) && (normal.y == 0.0))
3512 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3513 if (distance > MagickEpsilon)
3515 normal_distance=normal.x*normal.x+normal.y*normal.y+
3517 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3518 shade=distance/sqrt((double) normal_distance);
3521 for (i=0; i < (ssize_t) GetPixelChannels(linear_image); i++)
3530 channel=GetPixelChannelChannel(linear_image,i);
3531 traits=GetPixelChannelTraits(linear_image,channel);
3532 shade_traits=GetPixelChannelTraits(shade_image,channel);
3533 if ((traits == UndefinedPixelTrait) ||
3534 (shade_traits == UndefinedPixelTrait))
3536 if (((shade_traits & CopyPixelTrait) != 0) ||
3537 (GetPixelReadMask(linear_image,center) == 0))
3539 SetPixelChannel(shade_image,channel,center[i],q);
3542 if (gray != MagickFalse)
3544 SetPixelChannel(shade_image,channel,ClampToQuantum(shade),q);
3547 SetPixelChannel(shade_image,channel,ClampToQuantum(QuantumScale*shade*
3550 pre+=GetPixelChannels(linear_image);
3551 center+=GetPixelChannels(linear_image);
3552 post+=GetPixelChannels(linear_image);
3553 q+=GetPixelChannels(shade_image);
3555 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3557 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3562 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3563 #pragma omp critical (MagickCore_ShadeImage)
3565 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3566 if (proceed == MagickFalse)
3570 shade_view=DestroyCacheView(shade_view);
3571 image_view=DestroyCacheView(image_view);
3572 linear_image=DestroyImage(linear_image);
3573 if (status == MagickFalse)
3574 shade_image=DestroyImage(shade_image);
3575 return(shade_image);
3579 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3583 % S h a r p e n I m a g e %
3587 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3589 % SharpenImage() sharpens the image. We convolve the image with a Gaussian
3590 % operator of the given radius and standard deviation (sigma). For
3591 % reasonable results, radius should be larger than sigma. Use a radius of 0
3592 % and SharpenImage() selects a suitable radius for you.
3594 % Using a separable kernel would be faster, but the negative weights cancel
3595 % out on the corners of the kernel producing often undesirable ringing in the
3596 % filtered result; this can be avoided by using a 2D gaussian shaped image
3597 % sharpening kernel instead.
3599 % The format of the SharpenImage method is:
3601 % Image *SharpenImage(const Image *image,const double radius,
3602 % const double sigma,ExceptionInfo *exception)
3604 % A description of each parameter follows:
3606 % o image: the image.
3608 % o radius: the radius of the Gaussian, in pixels, not counting the center
3611 % o sigma: the standard deviation of the Laplacian, in pixels.
3613 % o exception: return any errors or warnings in this structure.
3616 MagickExport Image *SharpenImage(const Image *image,const double radius,
3617 const double sigma,ExceptionInfo *exception)
3640 assert(image != (const Image *) NULL);
3641 assert(image->signature == MagickSignature);
3642 if (image->debug != MagickFalse)
3643 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3644 assert(exception != (ExceptionInfo *) NULL);
3645 assert(exception->signature == MagickSignature);
3646 width=GetOptimalKernelWidth2D(radius,sigma);
3647 kernel_info=AcquireKernelInfo((const char *) NULL);
3648 if (kernel_info == (KernelInfo *) NULL)
3649 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3650 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3651 kernel_info->width=width;
3652 kernel_info->height=width;
3653 kernel_info->x=(ssize_t) (width-1)/2;
3654 kernel_info->y=(ssize_t) (width-1)/2;
3655 kernel_info->signature=MagickSignature;
3656 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
3657 AcquireAlignedMemory(kernel_info->width,kernel_info->height*
3658 sizeof(*kernel_info->values)));
3659 if (kernel_info->values == (MagickRealType *) NULL)
3661 kernel_info=DestroyKernelInfo(kernel_info);
3662 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3665 j=(ssize_t) (kernel_info->width-1)/2;
3667 for (v=(-j); v <= j; v++)
3669 for (u=(-j); u <= j; u++)
3671 kernel_info->values[i]=(MagickRealType) (-exp(-((double) u*u+v*v)/(2.0*
3672 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3673 normalize+=kernel_info->values[i];
3677 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
3679 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
3680 normalize+=kernel_info->values[i];
3681 gamma=PerceptibleReciprocal(normalize);
3682 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
3683 kernel_info->values[i]*=gamma;
3684 sharp_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
3685 UndefinedCompositeOp,0.0,exception);
3686 kernel_info=DestroyKernelInfo(kernel_info);
3687 return(sharp_image);
3691 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3695 % S p r e a d I m a g e %
3699 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3701 % SpreadImage() is a special effects method that randomly displaces each
3702 % pixel in a block defined by the radius parameter.
3704 % The format of the SpreadImage method is:
3706 % Image *SpreadImage(const Image *image,const double radius,
3707 % const PixelInterpolateMethod method,ExceptionInfo *exception)
3709 % A description of each parameter follows:
3711 % o image: the image.
3713 % o radius: choose a random pixel in a neighborhood of this extent.
3715 % o method: the pixel interpolation method.
3717 % o exception: return any errors or warnings in this structure.
3720 MagickExport Image *SpreadImage(const Image *image,const double radius,
3721 const PixelInterpolateMethod method,ExceptionInfo *exception)
3723 #define SpreadImageTag "Spread/Image"
3739 **restrict random_info;
3747 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3753 Initialize spread image attributes.
3755 assert(image != (Image *) NULL);
3756 assert(image->signature == MagickSignature);
3757 if (image->debug != MagickFalse)
3758 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3759 assert(exception != (ExceptionInfo *) NULL);
3760 assert(exception->signature == MagickSignature);
3761 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3763 if (spread_image == (Image *) NULL)
3764 return((Image *) NULL);
3765 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
3767 spread_image=DestroyImage(spread_image);
3768 return((Image *) NULL);
3775 width=GetOptimalKernelWidth1D(radius,0.5);
3776 random_info=AcquireRandomInfoThreadSet();
3777 image_view=AcquireVirtualCacheView(image,exception);
3778 spread_view=AcquireAuthenticCacheView(spread_image,exception);
3779 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3780 key=GetRandomSecretKey(random_info[0]);
3781 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3782 magick_threads(image,spread_image,image->rows,key == ~0UL)
3784 for (y=0; y < (ssize_t) image->rows; y++)
3787 id = GetOpenMPThreadId();
3795 if (status == MagickFalse)
3797 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
3799 if (q == (Quantum *) NULL)
3804 for (x=0; x < (ssize_t) image->columns; x++)
3809 point.x=GetPseudoRandomValue(random_info[id]);
3810 point.y=GetPseudoRandomValue(random_info[id]);
3811 status=InterpolatePixelChannels(image,image_view,spread_image,method,
3812 (double) x+width*point.x-0.5,(double) y+width*point.y-0.5,q,exception);
3813 q+=GetPixelChannels(spread_image);
3815 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
3817 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3822 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3823 #pragma omp critical (MagickCore_SpreadImage)
3825 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3826 if (proceed == MagickFalse)
3830 spread_view=DestroyCacheView(spread_view);
3831 image_view=DestroyCacheView(image_view);
3832 random_info=DestroyRandomInfoThreadSet(random_info);
3833 if (status == MagickFalse)
3834 spread_image=DestroyImage(spread_image);
3835 return(spread_image);
3839 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3843 % U n s h a r p M a s k I m a g e %
3847 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3849 % UnsharpMaskImage() sharpens one or more image channels. We convolve the
3850 % image with a Gaussian operator of the given radius and standard deviation
3851 % (sigma). For reasonable results, radius should be larger than sigma. Use a
3852 % radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
3854 % The format of the UnsharpMaskImage method is:
3856 % Image *UnsharpMaskImage(const Image *image,const double radius,
3857 % const double sigma,const double amount,const double threshold,
3858 % ExceptionInfo *exception)
3860 % A description of each parameter follows:
3862 % o image: the image.
3864 % o radius: the radius of the Gaussian, in pixels, not counting the center
3867 % o sigma: the standard deviation of the Gaussian, in pixels.
3869 % o gain: the percentage of the difference between the original and the
3870 % blur image that is added back into the original.
3872 % o threshold: the threshold in pixels needed to apply the diffence gain.
3874 % o exception: return any errors or warnings in this structure.
3877 MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
3878 const double sigma,const double gain,const double threshold,
3879 ExceptionInfo *exception)
3881 #define SharpenImageTag "Sharpen/Image"
3902 assert(image != (const Image *) NULL);
3903 assert(image->signature == MagickSignature);
3904 if (image->debug != MagickFalse)
3905 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3906 assert(exception != (ExceptionInfo *) NULL);
3907 unsharp_image=BlurImage(image,radius,sigma,exception);
3908 if (unsharp_image == (Image *) NULL)
3909 return((Image *) NULL);
3910 quantum_threshold=(double) QuantumRange*threshold;
3916 image_view=AcquireVirtualCacheView(image,exception);
3917 unsharp_view=AcquireAuthenticCacheView(unsharp_image,exception);
3918 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3919 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3920 magick_threads(image,unsharp_image,image->rows,1)
3922 for (y=0; y < (ssize_t) image->rows; y++)
3924 register const Quantum
3933 if (status == MagickFalse)
3935 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3936 q=QueueCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
3938 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3943 for (x=0; x < (ssize_t) image->columns; x++)
3948 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3960 channel=GetPixelChannelChannel(image,i);
3961 traits=GetPixelChannelTraits(image,channel);
3962 unsharp_traits=GetPixelChannelTraits(unsharp_image,channel);
3963 if ((traits == UndefinedPixelTrait) ||
3964 (unsharp_traits == UndefinedPixelTrait))
3966 if (((unsharp_traits & CopyPixelTrait) != 0) ||
3967 (GetPixelReadMask(image,p) != 0))
3969 SetPixelChannel(unsharp_image,channel,p[i],q);
3972 pixel=p[i]-(double) GetPixelChannel(unsharp_image,channel,q);
3973 if (fabs(2.0*pixel) < quantum_threshold)
3974 pixel=(double) p[i];
3976 pixel=(double) p[i]+gain*pixel;
3977 SetPixelChannel(unsharp_image,channel,ClampToQuantum(pixel),q);
3979 p+=GetPixelChannels(image);
3980 q+=GetPixelChannels(unsharp_image);
3982 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
3984 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3989 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3990 #pragma omp critical (MagickCore_UnsharpMaskImage)
3992 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
3993 if (proceed == MagickFalse)
3997 unsharp_image->type=image->type;
3998 unsharp_view=DestroyCacheView(unsharp_view);
3999 image_view=DestroyCacheView(image_view);
4000 if (status == MagickFalse)
4001 unsharp_image=DestroyImage(unsharp_image);
4002 return(unsharp_image);