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)
1005 geometry[MaxTextExtent];
1034 assert(image != (const Image *) NULL);
1035 assert(image->signature == MagickSignature);
1036 if (image->debug != MagickFalse)
1037 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1038 assert(exception != (ExceptionInfo *) NULL);
1039 assert(exception->signature == MagickSignature);
1043 (void) FormatLocaleString(geometry,MaxTextExtent,
1044 "blur:%.20gx%.20g;blur:%.20gx%.20g+90",radius,sigma,radius,sigma);
1045 kernel_info=AcquireKernelInfo(geometry);
1046 if (kernel_info == (KernelInfo *) NULL)
1047 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1048 edge_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1049 UndefinedCompositeOp,0.0,exception);
1050 kernel_info=DestroyKernelInfo(kernel_info);
1051 if (edge_image == (Image *) NULL)
1052 return((Image *) NULL);
1053 if (SetImageColorspace(edge_image,GRAYColorspace,exception) == MagickFalse)
1055 edge_image=DestroyImage(edge_image);
1056 return((Image *) NULL);
1059 Find the intensity gradient of the image.
1061 pixel_cache=AcquireMatrixInfo(edge_image->columns,edge_image->rows,
1062 sizeof(CannyInfo),exception);
1063 if (pixel_cache == (MatrixInfo *) NULL)
1065 edge_image=DestroyImage(edge_image);
1066 return((Image *) NULL);
1069 edge_view=AcquireVirtualCacheView(edge_image,exception);
1070 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1071 #pragma omp parallel for schedule(static,4) shared(status) \
1072 magick_threads(edge_image,edge_image,edge_image->rows,1)
1074 for (y=0; y < (ssize_t) edge_image->rows; y++)
1076 register const Quantum
1082 if (status == MagickFalse)
1084 p=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns+1,2,
1086 if (p == (const Quantum *) NULL)
1091 for (x=0; x < (ssize_t) edge_image->columns; x++)
1103 register const Quantum
1104 *restrict kernel_pixels;
1121 (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
1125 for (v=0; v < 2; v++)
1130 for (u=0; u < 2; u++)
1135 intensity=GetPixelIntensity(edge_image,kernel_pixels+u);
1136 dx+=0.5*Gx[v][u]*intensity;
1137 dy+=0.5*Gy[v][u]*intensity;
1139 kernel_pixels+=edge_image->columns+1;
1141 pixel.magnitude=sqrt(dx*dx+dy*dy);
1142 pixel.orientation=0;
1143 if (fabs(dx) > MagickEpsilon)
1151 if (theta < -2.41421356237)
1152 pixel.orientation=0;
1154 if (theta < -0.414213562373)
1155 pixel.orientation=1;
1157 pixel.orientation=2;
1161 if (theta > 2.41421356237)
1162 pixel.orientation=0;
1164 if (theta > 0.414213562373)
1165 pixel.orientation=3;
1167 pixel.orientation=2;
1170 if (SetMatrixElement(pixel_cache,x,y,&pixel) == MagickFalse)
1172 p+=GetPixelChannels(edge_image);
1175 edge_view=DestroyCacheView(edge_view);
1177 Non-maxima suppression, remove pixels that are not considered to be part
1180 (void) GetMatrixElement(pixel_cache,0,0,&pixel);
1181 max=pixel.intensity;
1182 min=pixel.intensity;
1183 edge_view=AcquireAuthenticCacheView(edge_image,exception);
1184 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1185 #pragma omp parallel for schedule(static,4) shared(status) \
1186 magick_threads(edge_image,edge_image,edge_image->rows,1)
1188 for (y=0; y < (ssize_t) edge_image->rows; y++)
1196 if (status == MagickFalse)
1198 q=GetCacheViewAuthenticPixels(edge_view,0,y,edge_image->columns,1,
1200 if (q == (Quantum *) NULL)
1205 for (x=0; x < (ssize_t) edge_image->columns; x++)
1212 (void) GetMatrixElement(pixel_cache,x,y,&pixel);
1213 switch (pixel.orientation)
1218 0 degrees, north and south.
1220 (void) GetMatrixElement(pixel_cache,x,y-1,&alpha_pixel);
1221 (void) GetMatrixElement(pixel_cache,x,y+1,&beta_pixel);
1227 45 degrees, northwest and southeast.
1229 (void) GetMatrixElement(pixel_cache,x-1,y-1,&alpha_pixel);
1230 (void) GetMatrixElement(pixel_cache,x+1,y+1,&beta_pixel);
1236 90 degrees, east and west.
1238 (void) GetMatrixElement(pixel_cache,x-1,y,&alpha_pixel);
1239 (void) GetMatrixElement(pixel_cache,x+1,y,&beta_pixel);
1245 135 degrees, northeast and southwest.
1247 (void) GetMatrixElement(pixel_cache,x+1,y-1,&beta_pixel);
1248 (void) GetMatrixElement(pixel_cache,x-1,y+1,&alpha_pixel);
1252 pixel.intensity=pixel.magnitude;
1253 if ((pixel.magnitude < alpha_pixel.magnitude) ||
1254 (pixel.magnitude < beta_pixel.magnitude))
1256 (void) SetMatrixElement(pixel_cache,x,y,&pixel);
1257 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1258 #pragma omp critical (MagickCore_CannyEdgeImage)
1261 if (pixel.intensity < min)
1262 min=pixel.intensity;
1263 if (pixel.intensity > max)
1264 max=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 lower_threshold=lower_percent*(max-min)+min;
1277 upper_threshold=upper_percent*(max-min)+min;
1279 Hysteresis threshold.
1281 edge_view=AcquireAuthenticCacheView(edge_image,exception);
1282 trace_view=AcquireAuthenticCacheView(edge_image,exception);
1283 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1284 #pragma omp parallel for schedule(static,4) shared(status) \
1285 magick_threads(edge_image,edge_image,edge_image->rows,1)
1287 for (y=0; y < (ssize_t) edge_image->rows; y++)
1292 if (status == MagickFalse)
1294 for (x=0; x < (ssize_t) edge_image->columns; x++)
1303 Edge if pixel gradient higher than upper threshold.
1305 status=GetMatrixElement(pixel_cache,x,y,&pixel);
1306 if (status == MagickFalse)
1308 q=GetCacheViewAuthenticPixels(edge_view,x,y,1,1,exception);
1309 if (q == (PixelPacket *) NULL)
1314 if ((pixel.intensity >= upper_threshold) &&
1315 (GetPixelIntensity(edge_image,q) == 0))
1318 status=SyncCacheViewAuthenticPixels(edge_view,exception);
1319 if (status == MagickFalse)
1321 status=TraceEdges(edge_image,trace_view,pixel_cache,x,y,
1322 lower_threshold,exception);
1323 if (status == MagickFalse)
1327 if (SyncCacheViewAuthenticPixels(edge_view,exception) == MagickFalse)
1330 trace_view=DestroyCacheView(trace_view);
1331 edge_view=DestroyCacheView(edge_view);
1332 pixel_cache=DestroyMatrixInfo(pixel_cache);
1337 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1341 % C o n v o l v e I m a g e %
1345 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1347 % ConvolveImage() applies a custom convolution kernel to the image.
1349 % The format of the ConvolveImage method is:
1351 % Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
1352 % ExceptionInfo *exception)
1354 % A description of each parameter follows:
1356 % o image: the image.
1358 % o kernel: the filtering kernel.
1360 % o exception: return any errors or warnings in this structure.
1363 MagickExport Image *ConvolveImage(const Image *image,
1364 const KernelInfo *kernel_info,ExceptionInfo *exception)
1369 convolve_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1370 UndefinedCompositeOp,0.0,exception);
1371 return(convolve_image);
1375 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1379 % D e s p e c k l e I m a g e %
1383 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1385 % DespeckleImage() reduces the speckle noise in an image while perserving the
1386 % edges of the original image. A speckle removing filter uses a complementary % hulling technique (raising pixels that are darker than their surrounding
1387 % neighbors, then complementarily lowering pixels that are brighter than their
1388 % surrounding neighbors) to reduce the speckle index of that image (reference
1389 % Crimmins speckle removal).
1391 % The format of the DespeckleImage method is:
1393 % Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1395 % A description of each parameter follows:
1397 % o image: the image.
1399 % o exception: return any errors or warnings in this structure.
1403 static void Hull(const Image *image,const ssize_t x_offset,
1404 const ssize_t y_offset,const size_t columns,const size_t rows,
1405 const int polarity,Quantum *restrict f,Quantum *restrict g)
1416 assert(image != (const Image *) NULL);
1417 assert(image->signature == MagickSignature);
1418 if (image->debug != MagickFalse)
1419 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1420 assert(f != (Quantum *) NULL);
1421 assert(g != (Quantum *) NULL);
1424 r=p+(y_offset*(columns+2)+x_offset);
1425 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1426 #pragma omp parallel for schedule(static,4) \
1427 magick_threads(image,image,1,1)
1429 for (y=0; y < (ssize_t) rows; y++)
1438 i=(2*y+1)+y*columns;
1440 for (x=0; x < (ssize_t) columns; x++)
1442 v=(MagickRealType) p[i];
1443 if ((MagickRealType) r[i] >= (v+ScaleCharToQuantum(2)))
1444 v+=ScaleCharToQuantum(1);
1449 for (x=0; x < (ssize_t) columns; x++)
1451 v=(MagickRealType) p[i];
1452 if ((MagickRealType) r[i] <= (v-ScaleCharToQuantum(2)))
1453 v-=ScaleCharToQuantum(1);
1460 r=q+(y_offset*(columns+2)+x_offset);
1461 s=q-(y_offset*(columns+2)+x_offset);
1462 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1463 #pragma omp parallel for schedule(static,4) \
1464 magick_threads(image,image,1,1)
1466 for (y=0; y < (ssize_t) rows; y++)
1475 i=(2*y+1)+y*columns;
1477 for (x=0; x < (ssize_t) columns; x++)
1479 v=(MagickRealType) q[i];
1480 if (((MagickRealType) s[i] >= (v+ScaleCharToQuantum(2))) &&
1481 ((MagickRealType) r[i] > v))
1482 v+=ScaleCharToQuantum(1);
1487 for (x=0; x < (ssize_t) columns; x++)
1489 v=(MagickRealType) q[i];
1490 if (((MagickRealType) s[i] <= (v-ScaleCharToQuantum(2))) &&
1491 ((MagickRealType) r[i] < v))
1492 v-=ScaleCharToQuantum(1);
1499 MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1501 #define DespeckleImageTag "Despeckle/Image"
1527 static const ssize_t
1528 X[4] = {0, 1, 1,-1},
1529 Y[4] = {1, 0, 1, 1};
1532 Allocate despeckled image.
1534 assert(image != (const Image *) NULL);
1535 assert(image->signature == MagickSignature);
1536 if (image->debug != MagickFalse)
1537 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1538 assert(exception != (ExceptionInfo *) NULL);
1539 assert(exception->signature == MagickSignature);
1540 despeckle_image=CloneImage(image,0,0,MagickTrue,exception);
1541 if (despeckle_image == (Image *) NULL)
1542 return((Image *) NULL);
1543 status=SetImageStorageClass(despeckle_image,DirectClass,exception);
1544 if (status == MagickFalse)
1546 despeckle_image=DestroyImage(despeckle_image);
1547 return((Image *) NULL);
1550 Allocate image buffer.
1552 length=(size_t) ((image->columns+2)*(image->rows+2));
1553 pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
1554 buffer_info=AcquireVirtualMemory(length,sizeof(*buffer));
1555 if ((pixel_info == (MemoryInfo *) NULL) ||
1556 (buffer_info == (MemoryInfo *) NULL))
1558 if (buffer_info != (MemoryInfo *) NULL)
1559 buffer_info=RelinquishVirtualMemory(buffer_info);
1560 if (pixel_info != (MemoryInfo *) NULL)
1561 pixel_info=RelinquishVirtualMemory(pixel_info);
1562 despeckle_image=DestroyImage(despeckle_image);
1563 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1565 pixels=(Quantum *) GetVirtualMemoryBlob(pixel_info);
1566 buffer=(Quantum *) GetVirtualMemoryBlob(buffer_info);
1568 Reduce speckle in the image.
1571 image_view=AcquireVirtualCacheView(image,exception);
1572 despeckle_view=AcquireAuthenticCacheView(despeckle_image,exception);
1573 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1590 if (status == MagickFalse)
1592 channel=GetPixelChannelChannel(image,i);
1593 traits=GetPixelChannelTraits(image,channel);
1594 despeckle_traits=GetPixelChannelTraits(despeckle_image,channel);
1595 if ((traits == UndefinedPixelTrait) ||
1596 (despeckle_traits == UndefinedPixelTrait))
1598 if ((despeckle_traits & CopyPixelTrait) != 0)
1600 (void) ResetMagickMemory(pixels,0,length*sizeof(*pixels));
1601 j=(ssize_t) image->columns+2;
1602 for (y=0; y < (ssize_t) image->rows; y++)
1604 register const Quantum
1607 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1608 if (p == (const Quantum *) NULL)
1614 for (x=0; x < (ssize_t) image->columns; x++)
1617 p+=GetPixelChannels(image);
1621 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
1622 for (k=0; k < 4; k++)
1624 Hull(image,X[k],Y[k],image->columns,image->rows,1,pixels,buffer);
1625 Hull(image,-X[k],-Y[k],image->columns,image->rows,1,pixels,buffer);
1626 Hull(image,-X[k],-Y[k],image->columns,image->rows,-1,pixels,buffer);
1627 Hull(image,X[k],Y[k],image->columns,image->rows,-1,pixels,buffer);
1629 j=(ssize_t) image->columns+2;
1630 for (y=0; y < (ssize_t) image->rows; y++)
1638 q=GetCacheViewAuthenticPixels(despeckle_view,0,y,despeckle_image->columns,
1640 if (q == (Quantum *) NULL)
1646 for (x=0; x < (ssize_t) image->columns; x++)
1648 SetPixelChannel(despeckle_image,channel,pixels[j++],q);
1649 q+=GetPixelChannels(despeckle_image);
1651 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1652 if (sync == MagickFalse)
1656 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1661 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1662 GetPixelChannels(image));
1663 if (proceed == MagickFalse)
1667 despeckle_view=DestroyCacheView(despeckle_view);
1668 image_view=DestroyCacheView(image_view);
1669 buffer_info=RelinquishVirtualMemory(buffer_info);
1670 pixel_info=RelinquishVirtualMemory(pixel_info);
1671 despeckle_image->type=image->type;
1672 if (status == MagickFalse)
1673 despeckle_image=DestroyImage(despeckle_image);
1674 return(despeckle_image);
1678 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1682 % E d g e I m a g e %
1686 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1688 % EdgeImage() finds edges in an image. Radius defines the radius of the
1689 % convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1692 % The format of the EdgeImage method is:
1694 % Image *EdgeImage(const Image *image,const double radius,
1695 % ExceptionInfo *exception)
1697 % A description of each parameter follows:
1699 % o image: the image.
1701 % o radius: the radius of the pixel neighborhood.
1703 % o exception: return any errors or warnings in this structure.
1706 MagickExport Image *EdgeImage(const Image *image,const double radius,
1707 ExceptionInfo *exception)
1721 assert(image != (const Image *) NULL);
1722 assert(image->signature == MagickSignature);
1723 if (image->debug != MagickFalse)
1724 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1725 assert(exception != (ExceptionInfo *) NULL);
1726 assert(exception->signature == MagickSignature);
1727 width=GetOptimalKernelWidth1D(radius,0.5);
1728 kernel_info=AcquireKernelInfo((const char *) NULL);
1729 if (kernel_info == (KernelInfo *) NULL)
1730 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1731 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1732 kernel_info->width=width;
1733 kernel_info->height=width;
1734 kernel_info->x=(ssize_t) (kernel_info->width-1)/2;
1735 kernel_info->y=(ssize_t) (kernel_info->height-1)/2;
1736 kernel_info->signature=MagickSignature;
1737 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
1738 AcquireAlignedMemory(kernel_info->width,kernel_info->height*
1739 sizeof(*kernel_info->values)));
1740 if (kernel_info->values == (MagickRealType *) NULL)
1742 kernel_info=DestroyKernelInfo(kernel_info);
1743 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1745 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
1746 kernel_info->values[i]=(-1.0);
1747 kernel_info->values[i/2]=(double) kernel_info->width*kernel_info->height-1.0;
1748 edge_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1749 UndefinedCompositeOp,0.0,exception);
1750 kernel_info=DestroyKernelInfo(kernel_info);
1755 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1759 % E m b o s s I m a g e %
1763 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1765 % EmbossImage() returns a grayscale image with a three-dimensional effect.
1766 % We convolve the image with a Gaussian operator of the given radius and
1767 % standard deviation (sigma). For reasonable results, radius should be
1768 % larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1771 % The format of the EmbossImage method is:
1773 % Image *EmbossImage(const Image *image,const double radius,
1774 % const double sigma,ExceptionInfo *exception)
1776 % A description of each parameter follows:
1778 % o image: the image.
1780 % o radius: the radius of the pixel neighborhood.
1782 % o sigma: the standard deviation of the Gaussian, in pixels.
1784 % o exception: return any errors or warnings in this structure.
1787 MagickExport Image *EmbossImage(const Image *image,const double radius,
1788 const double sigma,ExceptionInfo *exception)
1812 assert(image != (const Image *) NULL);
1813 assert(image->signature == MagickSignature);
1814 if (image->debug != MagickFalse)
1815 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1816 assert(exception != (ExceptionInfo *) NULL);
1817 assert(exception->signature == MagickSignature);
1818 width=GetOptimalKernelWidth1D(radius,sigma);
1819 kernel_info=AcquireKernelInfo((const char *) NULL);
1820 if (kernel_info == (KernelInfo *) NULL)
1821 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1822 kernel_info->width=width;
1823 kernel_info->height=width;
1824 kernel_info->x=(ssize_t) (width-1)/2;
1825 kernel_info->y=(ssize_t) (width-1)/2;
1826 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
1827 AcquireAlignedMemory(kernel_info->width,kernel_info->width*
1828 sizeof(*kernel_info->values)));
1829 if (kernel_info->values == (MagickRealType *) NULL)
1831 kernel_info=DestroyKernelInfo(kernel_info);
1832 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1834 j=(ssize_t) (kernel_info->width-1)/2;
1837 for (v=(-j); v <= j; v++)
1839 for (u=(-j); u <= j; u++)
1841 kernel_info->values[i]=(MagickRealType) (((u < 0) || (v < 0) ? -8.0 :
1842 8.0)*exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
1843 (2.0*MagickPI*MagickSigma*MagickSigma));
1845 kernel_info->values[i]=0.0;
1851 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
1852 normalize+=kernel_info->values[i];
1853 gamma=PerceptibleReciprocal(normalize);
1854 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
1855 kernel_info->values[i]*=gamma;
1856 emboss_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1857 UndefinedCompositeOp,0.0,exception);
1858 kernel_info=DestroyKernelInfo(kernel_info);
1859 if (emboss_image != (Image *) NULL)
1860 (void) EqualizeImage(emboss_image,exception);
1861 return(emboss_image);
1865 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1869 % G a u s s i a n B l u r I m a g e %
1873 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1875 % GaussianBlurImage() blurs an image. We convolve the image with a
1876 % Gaussian operator of the given radius and standard deviation (sigma).
1877 % For reasonable results, the radius should be larger than sigma. Use a
1878 % radius of 0 and GaussianBlurImage() selects a suitable radius for you
1880 % The format of the GaussianBlurImage method is:
1882 % Image *GaussianBlurImage(const Image *image,onst double radius,
1883 % const double sigma,ExceptionInfo *exception)
1885 % A description of each parameter follows:
1887 % o image: the image.
1889 % o radius: the radius of the Gaussian, in pixels, not counting the center
1892 % o sigma: the standard deviation of the Gaussian, in pixels.
1894 % o exception: return any errors or warnings in this structure.
1897 MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
1898 const double sigma,ExceptionInfo *exception)
1901 geometry[MaxTextExtent];
1909 assert(image != (const Image *) NULL);
1910 assert(image->signature == MagickSignature);
1911 if (image->debug != MagickFalse)
1912 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1913 assert(exception != (ExceptionInfo *) NULL);
1914 assert(exception->signature == MagickSignature);
1915 (void) FormatLocaleString(geometry,MaxTextExtent,"gaussian:%.20gx%.20g",
1917 kernel_info=AcquireKernelInfo(geometry);
1918 if (kernel_info == (KernelInfo *) NULL)
1919 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1920 blur_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1921 UndefinedCompositeOp,0.0,exception);
1922 kernel_info=DestroyKernelInfo(kernel_info);
1927 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1931 % M o t i o n B l u r I m a g e %
1935 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1937 % MotionBlurImage() simulates motion blur. We convolve the image with a
1938 % Gaussian operator of the given radius and standard deviation (sigma).
1939 % For reasonable results, radius should be larger than sigma. Use a
1940 % radius of 0 and MotionBlurImage() selects a suitable radius for you.
1941 % Angle gives the angle of the blurring motion.
1943 % Andrew Protano contributed this effect.
1945 % The format of the MotionBlurImage method is:
1947 % Image *MotionBlurImage(const Image *image,const double radius,
1948 % const double sigma,const double angle,ExceptionInfo *exception)
1950 % A description of each parameter follows:
1952 % o image: the image.
1954 % o radius: the radius of the Gaussian, in pixels, not counting
1957 % o sigma: the standard deviation of the Gaussian, in pixels.
1959 % o angle: Apply the effect along this angle.
1961 % o exception: return any errors or warnings in this structure.
1965 static MagickRealType *GetMotionBlurKernel(const size_t width,
1976 Generate a 1-D convolution kernel.
1978 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1979 kernel=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory((size_t)
1980 width,sizeof(*kernel)));
1981 if (kernel == (MagickRealType *) NULL)
1984 for (i=0; i < (ssize_t) width; i++)
1986 kernel[i]=(MagickRealType) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
1987 MagickSigma)))/(MagickSQ2PI*MagickSigma));
1988 normalize+=kernel[i];
1990 for (i=0; i < (ssize_t) width; i++)
1991 kernel[i]/=normalize;
1995 MagickExport Image *MotionBlurImage(const Image *image,const double radius,
1996 const double sigma,const double angle,ExceptionInfo *exception)
1998 #define BlurImageTag "Blur/Image"
2032 assert(image != (Image *) NULL);
2033 assert(image->signature == MagickSignature);
2034 if (image->debug != MagickFalse)
2035 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2036 assert(exception != (ExceptionInfo *) NULL);
2037 width=GetOptimalKernelWidth1D(radius,sigma);
2038 kernel=GetMotionBlurKernel(width,sigma);
2039 if (kernel == (MagickRealType *) NULL)
2040 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2041 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
2042 if (offset == (OffsetInfo *) NULL)
2044 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2045 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2047 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2048 if (blur_image == (Image *) NULL)
2050 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2051 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2052 return((Image *) NULL);
2054 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2056 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2057 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2058 blur_image=DestroyImage(blur_image);
2059 return((Image *) NULL);
2061 point.x=(double) width*sin(DegreesToRadians(angle));
2062 point.y=(double) width*cos(DegreesToRadians(angle));
2063 for (i=0; i < (ssize_t) width; i++)
2065 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
2066 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
2073 image_view=AcquireVirtualCacheView(image,exception);
2074 motion_view=AcquireVirtualCacheView(image,exception);
2075 blur_view=AcquireAuthenticCacheView(blur_image,exception);
2076 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2077 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2078 magick_threads(image,blur_image,image->rows,1)
2080 for (y=0; y < (ssize_t) image->rows; y++)
2082 register const Quantum
2091 if (status == MagickFalse)
2093 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2094 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2096 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2101 for (x=0; x < (ssize_t) image->columns; x++)
2106 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2120 register const Quantum
2123 register MagickRealType
2129 channel=GetPixelChannelChannel(image,i);
2130 traits=GetPixelChannelTraits(image,channel);
2131 blur_traits=GetPixelChannelTraits(blur_image,channel);
2132 if ((traits == UndefinedPixelTrait) ||
2133 (blur_traits == UndefinedPixelTrait))
2135 if (((blur_traits & CopyPixelTrait) != 0) ||
2136 (GetPixelReadMask(image,p) == 0))
2138 SetPixelChannel(blur_image,channel,p[i],q);
2143 if ((blur_traits & BlendPixelTrait) == 0)
2145 for (j=0; j < (ssize_t) width; j++)
2147 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+
2148 offset[j].y,1,1,exception);
2149 if (r == (const Quantum *) NULL)
2157 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
2162 for (j=0; j < (ssize_t) width; j++)
2164 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+offset[j].y,1,
2166 if (r == (const Quantum *) NULL)
2171 alpha=(double) (QuantumScale*GetPixelAlpha(image,r));
2172 pixel+=(*k)*alpha*r[i];
2176 gamma=PerceptibleReciprocal(gamma);
2177 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2179 p+=GetPixelChannels(image);
2180 q+=GetPixelChannels(blur_image);
2182 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2184 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2189 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2190 #pragma omp critical (MagickCore_MotionBlurImage)
2192 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2193 if (proceed == MagickFalse)
2197 blur_view=DestroyCacheView(blur_view);
2198 motion_view=DestroyCacheView(motion_view);
2199 image_view=DestroyCacheView(image_view);
2200 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2201 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2202 if (status == MagickFalse)
2203 blur_image=DestroyImage(blur_image);
2208 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2212 % P r e v i e w I m a g e %
2216 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2218 % PreviewImage() tiles 9 thumbnails of the specified image with an image
2219 % processing operation applied with varying parameters. This may be helpful
2220 % pin-pointing an appropriate parameter for a particular image processing
2223 % The format of the PreviewImages method is:
2225 % Image *PreviewImages(const Image *image,const PreviewType preview,
2226 % ExceptionInfo *exception)
2228 % A description of each parameter follows:
2230 % o image: the image.
2232 % o preview: the image processing operation.
2234 % o exception: return any errors or warnings in this structure.
2237 MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2238 ExceptionInfo *exception)
2240 #define NumberTiles 9
2241 #define PreviewImageTag "Preview/Image"
2242 #define DefaultPreviewGeometry "204x204+10+10"
2245 factor[MaxTextExtent],
2246 label[MaxTextExtent];
2291 Open output image file.
2293 assert(image != (Image *) NULL);
2294 assert(image->signature == MagickSignature);
2295 if (image->debug != MagickFalse)
2296 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2300 preview_info=AcquireImageInfo();
2301 SetGeometry(image,&geometry);
2302 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2303 &geometry.width,&geometry.height);
2304 images=NewImageList();
2306 GetQuantizeInfo(&quantize_info);
2312 for (i=0; i < NumberTiles; i++)
2314 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2315 if (thumbnail == (Image *) NULL)
2317 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2319 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel,exception);
2320 if (i == (NumberTiles/2))
2322 (void) QueryColorCompliance("#dfdfdf",AllCompliance,
2323 &thumbnail->matte_color,exception);
2324 AppendImageToList(&images,thumbnail);
2332 preview_image=RotateImage(thumbnail,degrees,exception);
2333 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
2339 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
2340 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",degrees,
2346 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2347 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
2348 preview_image=RollImage(thumbnail,x,y,exception);
2349 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
2350 (double) x,(double) y);
2355 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2356 if (preview_image == (Image *) NULL)
2358 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",2.0*
2360 (void) ModulateImage(preview_image,factor,exception);
2361 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2364 case SaturationPreview:
2366 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2367 if (preview_image == (Image *) NULL)
2369 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",2.0*percentage);
2370 (void) ModulateImage(preview_image,factor,exception);
2371 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2374 case BrightnessPreview:
2376 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2377 if (preview_image == (Image *) NULL)
2379 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
2380 (void) ModulateImage(preview_image,factor,exception);
2381 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2387 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2388 if (preview_image == (Image *) NULL)
2391 (void) GammaImage(preview_image,gamma,exception);
2392 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
2397 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2398 if (preview_image != (Image *) NULL)
2399 for (x=0; x < i; x++)
2400 (void) ContrastImage(preview_image,MagickTrue,exception);
2401 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
2407 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2408 if (preview_image == (Image *) NULL)
2410 for (x=0; x < i; x++)
2411 (void) ContrastImage(preview_image,MagickFalse,exception);
2412 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
2416 case GrayscalePreview:
2418 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2419 if (preview_image == (Image *) NULL)
2422 quantize_info.number_colors=colors;
2423 quantize_info.colorspace=GRAYColorspace;
2424 (void) QuantizeImage(&quantize_info,preview_image,exception);
2425 (void) FormatLocaleString(label,MaxTextExtent,
2426 "-colorspace gray -colors %.20g",(double) colors);
2429 case QuantizePreview:
2431 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2432 if (preview_image == (Image *) NULL)
2435 quantize_info.number_colors=colors;
2436 (void) QuantizeImage(&quantize_info,preview_image,exception);
2437 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
2441 case DespecklePreview:
2443 for (x=0; x < (i-1); x++)
2445 preview_image=DespeckleImage(thumbnail,exception);
2446 if (preview_image == (Image *) NULL)
2448 thumbnail=DestroyImage(thumbnail);
2449 thumbnail=preview_image;
2451 preview_image=DespeckleImage(thumbnail,exception);
2452 if (preview_image == (Image *) NULL)
2454 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
2458 case ReduceNoisePreview:
2460 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2461 (size_t) radius,exception);
2462 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
2465 case AddNoisePreview:
2471 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2476 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2481 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2486 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2491 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2496 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2501 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2505 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2506 (size_t) i,exception);
2507 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
2510 case SharpenPreview:
2512 preview_image=SharpenImage(thumbnail,radius,sigma,exception);
2513 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",radius,
2519 preview_image=BlurImage(thumbnail,radius,sigma,exception);
2520 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
2524 case ThresholdPreview:
2526 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2527 if (preview_image == (Image *) NULL)
2529 (void) BilevelImage(thumbnail,(double) (percentage*((double)
2530 QuantumRange+1.0))/100.0,exception);
2531 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",(double)
2532 (percentage*((double) QuantumRange+1.0))/100.0);
2535 case EdgeDetectPreview:
2537 preview_image=EdgeImage(thumbnail,radius,exception);
2538 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
2543 preview_image=SpreadImage(thumbnail,radius,thumbnail->interpolate,
2545 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",radius+0.5);
2548 case SolarizePreview:
2550 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2551 if (preview_image == (Image *) NULL)
2553 (void) SolarizeImage(preview_image,(double) QuantumRange*percentage/
2555 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
2556 (QuantumRange*percentage)/100.0);
2562 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2564 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",degrees,
2570 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2571 if (preview_image == (Image *) NULL)
2573 geometry.width=(size_t) (2*i+2);
2574 geometry.height=(size_t) (2*i+2);
2577 (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
2578 (void) FormatLocaleString(label,MaxTextExtent,
2579 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
2580 geometry.height,(double) geometry.x,(double) geometry.y);
2583 case SegmentPreview:
2585 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2586 if (preview_image == (Image *) NULL)
2589 (void) SegmentImage(preview_image,sRGBColorspace,MagickFalse,threshold,
2590 threshold,exception);
2591 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
2592 threshold,threshold);
2597 preview_image=SwirlImage(thumbnail,degrees,image->interpolate,
2599 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
2603 case ImplodePreview:
2606 preview_image=ImplodeImage(thumbnail,degrees,image->interpolate,
2608 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
2614 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,
2615 image->interpolate,exception);
2616 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",0.5*degrees,
2620 case OilPaintPreview:
2622 preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2624 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",radius,
2628 case CharcoalDrawingPreview:
2630 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
2632 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",radius,
2639 filename[MaxTextExtent];
2647 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2648 if (preview_image == (Image *) NULL)
2650 preview_info->quality=(size_t) percentage;
2651 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
2652 preview_info->quality);
2653 file=AcquireUniqueFileResource(filename);
2656 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
2657 "jpeg:%s",filename);
2658 status=WriteImage(preview_info,preview_image,exception);
2659 if (status != MagickFalse)
2664 (void) CopyMagickString(preview_info->filename,
2665 preview_image->filename,MaxTextExtent);
2666 quality_image=ReadImage(preview_info,exception);
2667 if (quality_image != (Image *) NULL)
2669 preview_image=DestroyImage(preview_image);
2670 preview_image=quality_image;
2673 (void) RelinquishUniqueFileResource(preview_image->filename);
2674 if ((GetBlobSize(preview_image)/1024) >= 1024)
2675 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
2676 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2679 if (GetBlobSize(preview_image) >= 1024)
2680 (void) FormatLocaleString(label,MaxTextExtent,
2681 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
2682 GetBlobSize(preview_image))/1024.0);
2684 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
2685 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
2689 thumbnail=DestroyImage(thumbnail);
2693 if (preview_image == (Image *) NULL)
2695 (void) DeleteImageProperty(preview_image,"label");
2696 (void) SetImageProperty(preview_image,"label",label,exception);
2697 AppendImageToList(&images,preview_image);
2698 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2700 if (proceed == MagickFalse)
2703 if (images == (Image *) NULL)
2705 preview_info=DestroyImageInfo(preview_info);
2706 return((Image *) NULL);
2711 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2712 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2713 montage_info->shadow=MagickTrue;
2714 (void) CloneString(&montage_info->tile,"3x3");
2715 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2716 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2717 montage_image=MontageImages(images,montage_info,exception);
2718 montage_info=DestroyMontageInfo(montage_info);
2719 images=DestroyImageList(images);
2720 if (montage_image == (Image *) NULL)
2721 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2722 if (montage_image->montage != (char *) NULL)
2725 Free image directory.
2727 montage_image->montage=(char *) RelinquishMagickMemory(
2728 montage_image->montage);
2729 if (image->directory != (char *) NULL)
2730 montage_image->directory=(char *) RelinquishMagickMemory(
2731 montage_image->directory);
2733 preview_info=DestroyImageInfo(preview_info);
2734 return(montage_image);
2738 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2742 % R o t a t i o n a l B l u r I m a g e %
2746 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2748 % RotationalBlurImage() applies a radial blur to the image.
2750 % Andrew Protano contributed this effect.
2752 % The format of the RotationalBlurImage method is:
2754 % Image *RotationalBlurImage(const Image *image,const double angle,
2755 % ExceptionInfo *exception)
2757 % A description of each parameter follows:
2759 % o image: the image.
2761 % o angle: the angle of the radial blur.
2765 % o exception: return any errors or warnings in this structure.
2768 MagickExport Image *RotationalBlurImage(const Image *image,const double angle,
2769 ExceptionInfo *exception)
2805 Allocate blur image.
2807 assert(image != (Image *) NULL);
2808 assert(image->signature == MagickSignature);
2809 if (image->debug != MagickFalse)
2810 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2811 assert(exception != (ExceptionInfo *) NULL);
2812 assert(exception->signature == MagickSignature);
2813 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2814 if (blur_image == (Image *) NULL)
2815 return((Image *) NULL);
2816 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2818 blur_image=DestroyImage(blur_image);
2819 return((Image *) NULL);
2821 blur_center.x=(double) (image->columns-1)/2.0;
2822 blur_center.y=(double) (image->rows-1)/2.0;
2823 blur_radius=hypot(blur_center.x,blur_center.y);
2824 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
2825 theta=DegreesToRadians(angle)/(double) (n-1);
2826 cos_theta=(double *) AcquireQuantumMemory((size_t) n,
2827 sizeof(*cos_theta));
2828 sin_theta=(double *) AcquireQuantumMemory((size_t) n,
2829 sizeof(*sin_theta));
2830 if ((cos_theta == (double *) NULL) ||
2831 (sin_theta == (double *) NULL))
2833 blur_image=DestroyImage(blur_image);
2834 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2836 offset=theta*(double) (n-1)/2.0;
2837 for (i=0; i < (ssize_t) n; i++)
2839 cos_theta[i]=cos((double) (theta*i-offset));
2840 sin_theta[i]=sin((double) (theta*i-offset));
2847 image_view=AcquireVirtualCacheView(image,exception);
2848 radial_view=AcquireVirtualCacheView(image,exception);
2849 blur_view=AcquireAuthenticCacheView(blur_image,exception);
2850 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2851 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2852 magick_threads(image,blur_image,image->rows,1)
2854 for (y=0; y < (ssize_t) image->rows; y++)
2856 register const Quantum
2865 if (status == MagickFalse)
2867 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2868 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2870 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2875 for (x=0; x < (ssize_t) image->columns; x++)
2889 center.x=(double) x-blur_center.x;
2890 center.y=(double) y-blur_center.y;
2891 radius=hypot((double) center.x,center.y);
2896 step=(size_t) (blur_radius/radius);
2903 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2916 register const Quantum
2922 channel=GetPixelChannelChannel(image,i);
2923 traits=GetPixelChannelTraits(image,channel);
2924 blur_traits=GetPixelChannelTraits(blur_image,channel);
2925 if ((traits == UndefinedPixelTrait) ||
2926 (blur_traits == UndefinedPixelTrait))
2928 if (((blur_traits & CopyPixelTrait) != 0) ||
2929 (GetPixelReadMask(image,p) == 0))
2931 SetPixelChannel(blur_image,channel,p[i],q);
2936 if ((blur_traits & BlendPixelTrait) == 0)
2938 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2940 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2941 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2942 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2944 if (r == (const Quantum *) NULL)
2952 gamma=PerceptibleReciprocal(gamma);
2953 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2956 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2958 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2959 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2960 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2962 if (r == (const Quantum *) NULL)
2967 pixel+=GetPixelAlpha(image,r)*r[i];
2968 gamma+=GetPixelAlpha(image,r);
2970 gamma=PerceptibleReciprocal(gamma);
2971 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2973 p+=GetPixelChannels(image);
2974 q+=GetPixelChannels(blur_image);
2976 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2978 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2983 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2984 #pragma omp critical (MagickCore_RotationalBlurImage)
2986 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2987 if (proceed == MagickFalse)
2991 blur_view=DestroyCacheView(blur_view);
2992 radial_view=DestroyCacheView(radial_view);
2993 image_view=DestroyCacheView(image_view);
2994 cos_theta=(double *) RelinquishMagickMemory(cos_theta);
2995 sin_theta=(double *) RelinquishMagickMemory(sin_theta);
2996 if (status == MagickFalse)
2997 blur_image=DestroyImage(blur_image);
3002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3006 % S e l e c t i v e B l u r I m a g e %
3010 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3012 % SelectiveBlurImage() selectively blur pixels within a contrast threshold.
3013 % It is similar to the unsharpen mask that sharpens everything with contrast
3014 % above a certain threshold.
3016 % The format of the SelectiveBlurImage method is:
3018 % Image *SelectiveBlurImage(const Image *image,const double radius,
3019 % const double sigma,const double threshold,ExceptionInfo *exception)
3021 % A description of each parameter follows:
3023 % o image: the image.
3025 % o radius: the radius of the Gaussian, in pixels, not counting the center
3028 % o sigma: the standard deviation of the Gaussian, in pixels.
3030 % o threshold: only pixels within this contrast threshold are included
3031 % in the blur operation.
3033 % o exception: return any errors or warnings in this structure.
3036 MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
3037 const double sigma,const double threshold,ExceptionInfo *exception)
3039 #define SelectiveBlurImageTag "SelectiveBlur/Image"
3073 Initialize blur image attributes.
3075 assert(image != (Image *) NULL);
3076 assert(image->signature == MagickSignature);
3077 if (image->debug != MagickFalse)
3078 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3079 assert(exception != (ExceptionInfo *) NULL);
3080 assert(exception->signature == MagickSignature);
3081 width=GetOptimalKernelWidth1D(radius,sigma);
3082 kernel=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory((size_t)
3083 width,width*sizeof(*kernel)));
3084 if (kernel == (MagickRealType *) NULL)
3085 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3086 j=(ssize_t) (width-1)/2;
3088 for (v=(-j); v <= j; v++)
3090 for (u=(-j); u <= j; u++)
3091 kernel[i++]=(MagickRealType) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
3092 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3094 if (image->debug != MagickFalse)
3097 format[MaxTextExtent],
3100 register const MagickRealType
3107 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
3108 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
3110 message=AcquireString("");
3112 for (v=0; v < (ssize_t) width; v++)
3115 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
3116 (void) ConcatenateString(&message,format);
3117 for (u=0; u < (ssize_t) width; u++)
3119 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",(double) *k++);
3120 (void) ConcatenateString(&message,format);
3122 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
3124 message=DestroyString(message);
3126 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3127 if (blur_image == (Image *) NULL)
3128 return((Image *) NULL);
3129 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
3131 blur_image=DestroyImage(blur_image);
3132 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3133 return((Image *) NULL);
3135 luminance_image=CloneImage(image,0,0,MagickTrue,exception);
3136 if (luminance_image == (Image *) NULL)
3138 blur_image=DestroyImage(blur_image);
3139 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3140 return((Image *) NULL);
3142 status=TransformImageColorspace(luminance_image,GRAYColorspace,exception);
3143 if (status == MagickFalse)
3145 luminance_image=DestroyImage(luminance_image);
3146 blur_image=DestroyImage(blur_image);
3147 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3148 return((Image *) NULL);
3151 Threshold blur image.
3155 center=(ssize_t) (GetPixelChannels(image)*(image->columns+width)*
3156 ((width-1)/2L)+GetPixelChannels(image)*((width-1)/2L));
3157 image_view=AcquireVirtualCacheView(image,exception);
3158 luminance_view=AcquireVirtualCacheView(luminance_image,exception);
3159 blur_view=AcquireAuthenticCacheView(blur_image,exception);
3160 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3161 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3162 magick_threads(image,blur_image,image->rows,1)
3164 for (y=0; y < (ssize_t) image->rows; y++)
3172 register const Quantum
3182 if (status == MagickFalse)
3184 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) (width-1)/2L),y-(ssize_t)
3185 ((width-1)/2L),image->columns+width,width,exception);
3186 l=GetCacheViewVirtualPixels(luminance_view,-((ssize_t) (width-1)/2L),y-
3187 (ssize_t) ((width-1)/2L),luminance_image->columns+width,width,exception);
3188 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3190 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3195 for (x=0; x < (ssize_t) image->columns; x++)
3203 intensity=GetPixelIntensity(image,p+center);
3204 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3218 register const MagickRealType
3221 register const Quantum
3222 *restrict luminance_pixels,
3231 channel=GetPixelChannelChannel(image,i);
3232 traits=GetPixelChannelTraits(image,channel);
3233 blur_traits=GetPixelChannelTraits(blur_image,channel);
3234 if ((traits == UndefinedPixelTrait) ||
3235 (blur_traits == UndefinedPixelTrait))
3237 if (((blur_traits & CopyPixelTrait) != 0) ||
3238 (GetPixelReadMask(image,p+center) == 0))
3240 SetPixelChannel(blur_image,channel,p[center+i],q);
3248 if ((blur_traits & BlendPixelTrait) == 0)
3250 for (v=0; v < (ssize_t) width; v++)
3252 for (u=0; u < (ssize_t) width; u++)
3254 contrast=GetPixelIntensity(luminance_image,luminance_pixels)-
3256 if (fabs(contrast) < threshold)
3258 pixel+=(*k)*pixels[i];
3262 pixels+=GetPixelChannels(image);
3263 luminance_pixels+=GetPixelChannels(luminance_image);
3265 pixels+=(image->columns-1)*GetPixelChannels(image);
3266 luminance_pixels+=luminance_image->columns*
3267 GetPixelChannels(luminance_image);
3269 if (fabs((double) gamma) < MagickEpsilon)
3271 SetPixelChannel(blur_image,channel,p[center+i],q);
3274 gamma=PerceptibleReciprocal(gamma);
3275 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3278 for (v=0; v < (ssize_t) width; v++)
3280 for (u=0; u < (ssize_t) width; u++)
3282 contrast=GetPixelIntensity(image,pixels)-intensity;
3283 if (fabs(contrast) < threshold)
3285 alpha=(double) (QuantumScale*GetPixelAlpha(image,pixels));
3286 pixel+=(*k)*alpha*pixels[i];
3290 pixels+=GetPixelChannels(image);
3291 luminance_pixels+=GetPixelChannels(luminance_image);
3293 pixels+=(image->columns-1)*GetPixelChannels(image);
3294 luminance_pixels+=luminance_image->columns*
3295 GetPixelChannels(luminance_image);
3297 if (fabs((double) gamma) < MagickEpsilon)
3299 SetPixelChannel(blur_image,channel,p[center+i],q);
3302 gamma=PerceptibleReciprocal(gamma);
3303 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3305 p+=GetPixelChannels(image);
3306 l+=GetPixelChannels(luminance_image);
3307 q+=GetPixelChannels(blur_image);
3309 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3310 if (sync == MagickFalse)
3312 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3317 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3318 #pragma omp critical (MagickCore_SelectiveBlurImage)
3320 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3322 if (proceed == MagickFalse)
3326 blur_image->type=image->type;
3327 blur_view=DestroyCacheView(blur_view);
3328 image_view=DestroyCacheView(image_view);
3329 luminance_image=DestroyImage(luminance_image);
3330 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3331 if (status == MagickFalse)
3332 blur_image=DestroyImage(blur_image);
3337 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3341 % S h a d e I m a g e %
3345 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3347 % ShadeImage() shines a distant light on an image to create a
3348 % three-dimensional effect. You control the positioning of the light with
3349 % azimuth and elevation; azimuth is measured in degrees off the x axis
3350 % and elevation is measured in pixels above the Z axis.
3352 % The format of the ShadeImage method is:
3354 % Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3355 % const double azimuth,const double elevation,ExceptionInfo *exception)
3357 % A description of each parameter follows:
3359 % o image: the image.
3361 % o gray: A value other than zero shades the intensity of each pixel.
3363 % o azimuth, elevation: Define the light source direction.
3365 % o exception: return any errors or warnings in this structure.
3368 MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3369 const double azimuth,const double elevation,ExceptionInfo *exception)
3371 #define ShadeImageTag "Shade/Image"
3394 Initialize shaded image attributes.
3396 assert(image != (const Image *) NULL);
3397 assert(image->signature == MagickSignature);
3398 if (image->debug != MagickFalse)
3399 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3400 assert(exception != (ExceptionInfo *) NULL);
3401 assert(exception->signature == MagickSignature);
3402 linear_image=CloneImage(image,0,0,MagickTrue,exception);
3403 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3404 if ((linear_image == (Image *) NULL) || (shade_image == (Image *) NULL))
3406 if (linear_image != (Image *) NULL)
3407 linear_image=DestroyImage(linear_image);
3408 if (shade_image != (Image *) NULL)
3409 shade_image=DestroyImage(shade_image);
3410 return((Image *) NULL);
3412 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
3414 linear_image=DestroyImage(linear_image);
3415 shade_image=DestroyImage(shade_image);
3416 return((Image *) NULL);
3419 Compute the light vector.
3421 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3422 cos(DegreesToRadians(elevation));
3423 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3424 cos(DegreesToRadians(elevation));
3425 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3431 image_view=AcquireVirtualCacheView(linear_image,exception);
3432 shade_view=AcquireAuthenticCacheView(shade_image,exception);
3433 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3434 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3435 magick_threads(linear_image,shade_image,linear_image->rows,1)
3437 for (y=0; y < (ssize_t) linear_image->rows; y++)
3447 register const Quantum
3459 if (status == MagickFalse)
3461 p=GetCacheViewVirtualPixels(image_view,-1,y-1,linear_image->columns+2,3,
3463 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3465 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3471 Shade this row of pixels.
3473 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
3474 pre=p+GetPixelChannels(linear_image);
3475 center=pre+(linear_image->columns+2)*GetPixelChannels(linear_image);
3476 post=center+(linear_image->columns+2)*GetPixelChannels(linear_image);
3477 for (x=0; x < (ssize_t) linear_image->columns; x++)
3483 Determine the surface normal and compute shading.
3486 GetPixelIntensity(linear_image,pre-GetPixelChannels(linear_image))+
3487 GetPixelIntensity(linear_image,center-GetPixelChannels(linear_image))+
3488 GetPixelIntensity(linear_image,post-GetPixelChannels(linear_image))-
3489 GetPixelIntensity(linear_image,pre+GetPixelChannels(linear_image))-
3490 GetPixelIntensity(linear_image,center+GetPixelChannels(linear_image))-
3491 GetPixelIntensity(linear_image,post+GetPixelChannels(linear_image)));
3493 GetPixelIntensity(linear_image,post-GetPixelChannels(linear_image))+
3494 GetPixelIntensity(linear_image,post)+
3495 GetPixelIntensity(linear_image,post+GetPixelChannels(linear_image))-
3496 GetPixelIntensity(linear_image,pre-GetPixelChannels(linear_image))-
3497 GetPixelIntensity(linear_image,pre)-
3498 GetPixelIntensity(linear_image,pre+GetPixelChannels(linear_image)));
3499 if ((normal.x == 0.0) && (normal.y == 0.0))
3504 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3505 if (distance > MagickEpsilon)
3507 normal_distance=normal.x*normal.x+normal.y*normal.y+
3509 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3510 shade=distance/sqrt((double) normal_distance);
3513 for (i=0; i < (ssize_t) GetPixelChannels(linear_image); i++)
3522 channel=GetPixelChannelChannel(linear_image,i);
3523 traits=GetPixelChannelTraits(linear_image,channel);
3524 shade_traits=GetPixelChannelTraits(shade_image,channel);
3525 if ((traits == UndefinedPixelTrait) ||
3526 (shade_traits == UndefinedPixelTrait))
3528 if (((shade_traits & CopyPixelTrait) != 0) ||
3529 (GetPixelReadMask(linear_image,center) == 0))
3531 SetPixelChannel(shade_image,channel,center[i],q);
3534 if (gray != MagickFalse)
3536 SetPixelChannel(shade_image,channel,ClampToQuantum(shade),q);
3539 SetPixelChannel(shade_image,channel,ClampToQuantum(QuantumScale*shade*
3542 pre+=GetPixelChannels(linear_image);
3543 center+=GetPixelChannels(linear_image);
3544 post+=GetPixelChannels(linear_image);
3545 q+=GetPixelChannels(shade_image);
3547 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3549 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3554 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3555 #pragma omp critical (MagickCore_ShadeImage)
3557 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3558 if (proceed == MagickFalse)
3562 shade_view=DestroyCacheView(shade_view);
3563 image_view=DestroyCacheView(image_view);
3564 linear_image=DestroyImage(linear_image);
3565 if (status == MagickFalse)
3566 shade_image=DestroyImage(shade_image);
3567 return(shade_image);
3571 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3575 % S h a r p e n I m a g e %
3579 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3581 % SharpenImage() sharpens the image. We convolve the image with a Gaussian
3582 % operator of the given radius and standard deviation (sigma). For
3583 % reasonable results, radius should be larger than sigma. Use a radius of 0
3584 % and SharpenImage() selects a suitable radius for you.
3586 % Using a separable kernel would be faster, but the negative weights cancel
3587 % out on the corners of the kernel producing often undesirable ringing in the
3588 % filtered result; this can be avoided by using a 2D gaussian shaped image
3589 % sharpening kernel instead.
3591 % The format of the SharpenImage method is:
3593 % Image *SharpenImage(const Image *image,const double radius,
3594 % const double sigma,ExceptionInfo *exception)
3596 % A description of each parameter follows:
3598 % o image: the image.
3600 % o radius: the radius of the Gaussian, in pixels, not counting the center
3603 % o sigma: the standard deviation of the Laplacian, in pixels.
3605 % o exception: return any errors or warnings in this structure.
3608 MagickExport Image *SharpenImage(const Image *image,const double radius,
3609 const double sigma,ExceptionInfo *exception)
3632 assert(image != (const Image *) NULL);
3633 assert(image->signature == MagickSignature);
3634 if (image->debug != MagickFalse)
3635 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3636 assert(exception != (ExceptionInfo *) NULL);
3637 assert(exception->signature == MagickSignature);
3638 width=GetOptimalKernelWidth2D(radius,sigma);
3639 kernel_info=AcquireKernelInfo((const char *) NULL);
3640 if (kernel_info == (KernelInfo *) NULL)
3641 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3642 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3643 kernel_info->width=width;
3644 kernel_info->height=width;
3645 kernel_info->x=(ssize_t) (width-1)/2;
3646 kernel_info->y=(ssize_t) (width-1)/2;
3647 kernel_info->signature=MagickSignature;
3648 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
3649 AcquireAlignedMemory(kernel_info->width,kernel_info->height*
3650 sizeof(*kernel_info->values)));
3651 if (kernel_info->values == (MagickRealType *) NULL)
3653 kernel_info=DestroyKernelInfo(kernel_info);
3654 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3657 j=(ssize_t) (kernel_info->width-1)/2;
3659 for (v=(-j); v <= j; v++)
3661 for (u=(-j); u <= j; u++)
3663 kernel_info->values[i]=(MagickRealType) (-exp(-((double) u*u+v*v)/(2.0*
3664 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3665 normalize+=kernel_info->values[i];
3669 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
3671 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
3672 normalize+=kernel_info->values[i];
3673 gamma=PerceptibleReciprocal(normalize);
3674 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
3675 kernel_info->values[i]*=gamma;
3676 sharp_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
3677 UndefinedCompositeOp,0.0,exception);
3678 kernel_info=DestroyKernelInfo(kernel_info);
3679 return(sharp_image);
3683 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3687 % S p r e a d I m a g e %
3691 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3693 % SpreadImage() is a special effects method that randomly displaces each
3694 % pixel in a block defined by the radius parameter.
3696 % The format of the SpreadImage method is:
3698 % Image *SpreadImage(const Image *image,const double radius,
3699 % const PixelInterpolateMethod method,ExceptionInfo *exception)
3701 % A description of each parameter follows:
3703 % o image: the image.
3705 % o radius: choose a random pixel in a neighborhood of this extent.
3707 % o method: the pixel interpolation method.
3709 % o exception: return any errors or warnings in this structure.
3712 MagickExport Image *SpreadImage(const Image *image,const double radius,
3713 const PixelInterpolateMethod method,ExceptionInfo *exception)
3715 #define SpreadImageTag "Spread/Image"
3731 **restrict random_info;
3739 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3745 Initialize spread image attributes.
3747 assert(image != (Image *) NULL);
3748 assert(image->signature == MagickSignature);
3749 if (image->debug != MagickFalse)
3750 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3751 assert(exception != (ExceptionInfo *) NULL);
3752 assert(exception->signature == MagickSignature);
3753 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3755 if (spread_image == (Image *) NULL)
3756 return((Image *) NULL);
3757 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
3759 spread_image=DestroyImage(spread_image);
3760 return((Image *) NULL);
3767 width=GetOptimalKernelWidth1D(radius,0.5);
3768 random_info=AcquireRandomInfoThreadSet();
3769 image_view=AcquireVirtualCacheView(image,exception);
3770 spread_view=AcquireAuthenticCacheView(spread_image,exception);
3771 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3772 key=GetRandomSecretKey(random_info[0]);
3773 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3774 magick_threads(image,spread_image,image->rows,key == ~0UL)
3776 for (y=0; y < (ssize_t) image->rows; y++)
3779 id = GetOpenMPThreadId();
3787 if (status == MagickFalse)
3789 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
3791 if (q == (Quantum *) NULL)
3796 for (x=0; x < (ssize_t) image->columns; x++)
3801 point.x=GetPseudoRandomValue(random_info[id]);
3802 point.y=GetPseudoRandomValue(random_info[id]);
3803 status=InterpolatePixelChannels(image,image_view,spread_image,method,
3804 (double) x+width*point.x-0.5,(double) y+width*point.y-0.5,q,exception);
3805 q+=GetPixelChannels(spread_image);
3807 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
3809 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3814 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3815 #pragma omp critical (MagickCore_SpreadImage)
3817 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3818 if (proceed == MagickFalse)
3822 spread_view=DestroyCacheView(spread_view);
3823 image_view=DestroyCacheView(image_view);
3824 random_info=DestroyRandomInfoThreadSet(random_info);
3825 if (status == MagickFalse)
3826 spread_image=DestroyImage(spread_image);
3827 return(spread_image);
3831 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3835 % U n s h a r p M a s k I m a g e %
3839 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3841 % UnsharpMaskImage() sharpens one or more image channels. We convolve the
3842 % image with a Gaussian operator of the given radius and standard deviation
3843 % (sigma). For reasonable results, radius should be larger than sigma. Use a
3844 % radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
3846 % The format of the UnsharpMaskImage method is:
3848 % Image *UnsharpMaskImage(const Image *image,const double radius,
3849 % const double sigma,const double amount,const double threshold,
3850 % ExceptionInfo *exception)
3852 % A description of each parameter follows:
3854 % o image: the image.
3856 % o radius: the radius of the Gaussian, in pixels, not counting the center
3859 % o sigma: the standard deviation of the Gaussian, in pixels.
3861 % o gain: the percentage of the difference between the original and the
3862 % blur image that is added back into the original.
3864 % o threshold: the threshold in pixels needed to apply the diffence gain.
3866 % o exception: return any errors or warnings in this structure.
3869 MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
3870 const double sigma,const double gain,const double threshold,
3871 ExceptionInfo *exception)
3873 #define SharpenImageTag "Sharpen/Image"
3894 assert(image != (const Image *) NULL);
3895 assert(image->signature == MagickSignature);
3896 if (image->debug != MagickFalse)
3897 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3898 assert(exception != (ExceptionInfo *) NULL);
3899 unsharp_image=BlurImage(image,radius,sigma,exception);
3900 if (unsharp_image == (Image *) NULL)
3901 return((Image *) NULL);
3902 quantum_threshold=(double) QuantumRange*threshold;
3908 image_view=AcquireVirtualCacheView(image,exception);
3909 unsharp_view=AcquireAuthenticCacheView(unsharp_image,exception);
3910 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3911 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3912 magick_threads(image,unsharp_image,image->rows,1)
3914 for (y=0; y < (ssize_t) image->rows; y++)
3916 register const Quantum
3925 if (status == MagickFalse)
3927 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3928 q=QueueCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
3930 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3935 for (x=0; x < (ssize_t) image->columns; x++)
3940 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3952 channel=GetPixelChannelChannel(image,i);
3953 traits=GetPixelChannelTraits(image,channel);
3954 unsharp_traits=GetPixelChannelTraits(unsharp_image,channel);
3955 if ((traits == UndefinedPixelTrait) ||
3956 (unsharp_traits == UndefinedPixelTrait))
3958 if (((unsharp_traits & CopyPixelTrait) != 0) ||
3959 (GetPixelReadMask(image,p) != 0))
3961 SetPixelChannel(unsharp_image,channel,p[i],q);
3964 pixel=p[i]-(double) GetPixelChannel(unsharp_image,channel,q);
3965 if (fabs(2.0*pixel) < quantum_threshold)
3966 pixel=(double) p[i];
3968 pixel=(double) p[i]+gain*pixel;
3969 SetPixelChannel(unsharp_image,channel,ClampToQuantum(pixel),q);
3971 p+=GetPixelChannels(image);
3972 q+=GetPixelChannels(unsharp_image);
3974 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
3976 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3981 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3982 #pragma omp critical (MagickCore_UnsharpMaskImage)
3984 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
3985 if (proceed == MagickFalse)
3989 unsharp_image->type=image->type;
3990 unsharp_view=DestroyCacheView(unsharp_view);
3991 image_view=DestroyCacheView(image_view);
3992 if (status == MagickFalse)
3993 unsharp_image=DestroyImage(unsharp_image);
3994 return(unsharp_image);