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_percent,
866 % const double upper_percent,ExceptionInfo *exception)
868 % A description of each parameter follows:
870 % o image: the image.
872 % o radius: the radius of the gaussian smoothing filter.
874 % o sigma: the sigma of the gaussian smoothing filter.
876 % o lower_precent: percentage of edge pixels in the lower threshold.
878 % o upper_percent: percentage of edge pixels in the upper threshold.
880 % o exception: return any errors or warnings in this structure.
884 typedef struct _CannyInfo
894 typedef struct _EdgeInfo
901 static inline MagickBooleanType IsAuthenticPixel(const Image *image,
902 const ssize_t x,const ssize_t y)
904 if ((x < 0) || (x >= (ssize_t) image->columns))
906 if ((y < 0) || (y >= (ssize_t) image->rows))
911 static MagickBooleanType TraceEdges(Image *edge_image,CacheView *trace_view,
912 MatrixInfo *pixel_cache,MatrixInfo *edge_cache,const ssize_t x,
913 const ssize_t y,const double lower_threshold,ExceptionInfo *exception)
924 if (GetMatrixElement(edge_cache,0,0,&edge) == MagickFalse)
928 if (SetMatrixElement(edge_cache,0,0,&edge) == MagickFalse)
940 status=GetMatrixElement(edge_cache,(ssize_t) number_edges,0,&edge);
941 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,edge.x+u,edge.y+v) == MagickFalse)
958 Not an edge if gradient value is below the lower threshold.
960 q=GetCacheViewAuthenticPixels(trace_view,edge.x+u,edge.y+v,1,1,
962 if (q == (Quantum *) NULL)
964 status=GetMatrixElement(pixel_cache,edge.x+u,edge.y+v,&pixel);
965 if (status == MagickFalse)
967 if ((GetPixelIntensity(edge_image,q) == 0.0) &&
968 (pixel.intensity >= lower_threshold))
971 status=SyncCacheViewAuthenticPixels(trace_view,exception);
972 if (status == MagickFalse)
976 status=SetMatrixElement(edge_cache,(ssize_t) number_edges,0,&edge);
977 if (status == MagickFalse)
983 } while (number_edges != 0);
988 MagickExport Image *CannyEdgeImage(const Image *image,const double radius,
989 const double sigma,const double lower_percent,const double upper_percent,
990 ExceptionInfo *exception)
1000 geometry[MaxTextExtent];
1024 assert(image != (const Image *) NULL);
1025 assert(image->signature == MagickSignature);
1026 if (image->debug != MagickFalse)
1027 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1028 assert(exception != (ExceptionInfo *) NULL);
1029 assert(exception->signature == MagickSignature);
1033 (void) FormatLocaleString(geometry,MaxTextExtent,
1034 "blur:%.20gx%.20g;blur:%.20gx%.20g+90",radius,sigma,radius,sigma);
1035 kernel_info=AcquireKernelInfo(geometry);
1036 if (kernel_info == (KernelInfo *) NULL)
1037 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1038 edge_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1039 UndefinedCompositeOp,0.0,exception);
1040 kernel_info=DestroyKernelInfo(kernel_info);
1041 if (edge_image == (Image *) NULL)
1042 return((Image *) NULL);
1043 if (SetImageColorspace(edge_image,GRAYColorspace,exception) == MagickFalse)
1045 edge_image=DestroyImage(edge_image);
1046 return((Image *) NULL);
1049 Find the intensity gradient of the image.
1051 pixel_cache=AcquireMatrixInfo(edge_image->columns,edge_image->rows,
1052 sizeof(CannyInfo),exception);
1053 if (pixel_cache == (MatrixInfo *) NULL)
1055 edge_image=DestroyImage(edge_image);
1056 return((Image *) NULL);
1059 edge_view=AcquireVirtualCacheView(edge_image,exception);
1060 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1061 #pragma omp parallel for schedule(static,4) shared(status) \
1062 magick_threads(edge_image,edge_image,edge_image->rows,1)
1064 for (y=0; y < (ssize_t) edge_image->rows; y++)
1066 register const Quantum
1072 if (status == MagickFalse)
1074 p=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns+1,2,
1076 if (p == (const Quantum *) NULL)
1081 for (x=0; x < (ssize_t) edge_image->columns; x++)
1090 register const Quantum
1091 *restrict kernel_pixels;
1108 (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
1112 for (v=0; v < 2; v++)
1117 for (u=0; u < 2; u++)
1122 intensity=GetPixelIntensity(edge_image,kernel_pixels+u);
1123 dx+=0.5*Gx[v][u]*intensity;
1124 dy+=0.5*Gy[v][u]*intensity;
1126 kernel_pixels+=edge_image->columns+1;
1128 pixel.magnitude=sqrt(dx*dx+dy*dy);
1129 pixel.orientation=0;
1130 if (fabs(dx) > MagickEpsilon)
1138 if (slope < -2.41421356237)
1139 pixel.orientation=0;
1141 if (slope < -0.414213562373)
1142 pixel.orientation=1;
1144 pixel.orientation=2;
1148 if (slope > 2.41421356237)
1149 pixel.orientation=0;
1151 if (slope > 0.414213562373)
1152 pixel.orientation=3;
1154 pixel.orientation=2;
1157 if (SetMatrixElement(pixel_cache,x,y,&pixel) == MagickFalse)
1159 p+=GetPixelChannels(edge_image);
1162 edge_view=DestroyCacheView(edge_view);
1164 Non-maxima suppression, remove pixels that are not considered to be part
1167 (void) GetMatrixElement(pixel_cache,0,0,&pixel);
1168 max=pixel.intensity;
1169 min=pixel.intensity;
1170 edge_view=AcquireAuthenticCacheView(edge_image,exception);
1171 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1172 #pragma omp parallel for schedule(static,4) shared(status) \
1173 magick_threads(edge_image,edge_image,edge_image->rows,1)
1175 for (y=0; y < (ssize_t) edge_image->rows; y++)
1183 if (status == MagickFalse)
1185 q=GetCacheViewAuthenticPixels(edge_view,0,y,edge_image->columns,1,
1187 if (q == (Quantum *) NULL)
1192 for (x=0; x < (ssize_t) edge_image->columns; x++)
1199 (void) GetMatrixElement(pixel_cache,x,y,&pixel);
1200 switch (pixel.orientation)
1205 0 degrees, north and south.
1207 (void) GetMatrixElement(pixel_cache,x,y-1,&alpha_pixel);
1208 (void) GetMatrixElement(pixel_cache,x,y+1,&beta_pixel);
1214 45 degrees, northwest and southeast.
1216 (void) GetMatrixElement(pixel_cache,x-1,y-1,&alpha_pixel);
1217 (void) GetMatrixElement(pixel_cache,x+1,y+1,&beta_pixel);
1223 90 degrees, east and west.
1225 (void) GetMatrixElement(pixel_cache,x-1,y,&alpha_pixel);
1226 (void) GetMatrixElement(pixel_cache,x+1,y,&beta_pixel);
1232 135 degrees, northeast and southwest.
1234 (void) GetMatrixElement(pixel_cache,x+1,y-1,&beta_pixel);
1235 (void) GetMatrixElement(pixel_cache,x-1,y+1,&alpha_pixel);
1239 pixel.intensity=pixel.magnitude;
1240 if ((pixel.magnitude < alpha_pixel.magnitude) ||
1241 (pixel.magnitude < beta_pixel.magnitude))
1243 (void) SetMatrixElement(pixel_cache,x,y,&pixel);
1244 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1245 #pragma omp critical (MagickCore_CannyEdgeImage)
1248 if (pixel.intensity < min)
1249 min=pixel.intensity;
1250 if (pixel.intensity > max)
1251 max=pixel.intensity;
1254 q+=GetPixelChannels(edge_image);
1256 if (SyncCacheViewAuthenticPixels(edge_view,exception) == MagickFalse)
1259 edge_view=DestroyCacheView(edge_view);
1261 Estimate hysteresis threshold.
1263 lower_threshold=lower_percent*(max-min)+min;
1264 upper_threshold=upper_percent*(max-min)+min;
1266 Hysteresis threshold.
1268 edge_cache=AcquireMatrixInfo(edge_image->columns,edge_image->rows,
1269 sizeof(EdgeInfo),exception);
1270 if (edge_cache == (MatrixInfo *) NULL)
1272 pixel_cache=DestroyMatrixInfo(pixel_cache);
1273 edge_image=DestroyImage(edge_image);
1274 return((Image *) NULL);
1276 edge_view=AcquireAuthenticCacheView(edge_image,exception);
1277 trace_view=AcquireAuthenticCacheView(edge_image,exception);
1278 for (y=0; y < (ssize_t) edge_image->rows; y++)
1283 if (status == MagickFalse)
1285 for (x=0; x < (ssize_t) edge_image->columns; x++)
1294 Edge if pixel gradient higher than upper threshold.
1296 status=GetMatrixElement(pixel_cache,x,y,&pixel);
1297 if (status == MagickFalse)
1299 q=GetCacheViewAuthenticPixels(edge_view,x,y,1,1,exception);
1300 if (q == (Quantum *) NULL)
1305 if ((GetPixelIntensity(edge_image,q) == 0.0) &&
1306 (pixel.intensity >= upper_threshold))
1309 status=SyncCacheViewAuthenticPixels(edge_view,exception);
1310 if (status == MagickFalse)
1312 status=TraceEdges(edge_image,trace_view,pixel_cache,edge_cache,x,y,
1313 lower_threshold,exception);
1314 if (status == MagickFalse)
1322 trace_view=DestroyCacheView(trace_view);
1323 edge_view=DestroyCacheView(edge_view);
1324 edge_cache=DestroyMatrixInfo(edge_cache);
1325 pixel_cache=DestroyMatrixInfo(pixel_cache);
1330 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1334 % C o n v o l v e I m a g e %
1338 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1340 % ConvolveImage() applies a custom convolution kernel to the image.
1342 % The format of the ConvolveImage method is:
1344 % Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
1345 % ExceptionInfo *exception)
1347 % A description of each parameter follows:
1349 % o image: the image.
1351 % o kernel: the filtering kernel.
1353 % o exception: return any errors or warnings in this structure.
1356 MagickExport Image *ConvolveImage(const Image *image,
1357 const KernelInfo *kernel_info,ExceptionInfo *exception)
1362 convolve_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1363 UndefinedCompositeOp,0.0,exception);
1364 return(convolve_image);
1368 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1372 % D e s p e c k l e I m a g e %
1376 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1378 % DespeckleImage() reduces the speckle noise in an image while perserving the
1379 % edges of the original image. A speckle removing filter uses a complementary % hulling technique (raising pixels that are darker than their surrounding
1380 % neighbors, then complementarily lowering pixels that are brighter than their
1381 % surrounding neighbors) to reduce the speckle index of that image (reference
1382 % Crimmins speckle removal).
1384 % The format of the DespeckleImage method is:
1386 % Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1388 % A description of each parameter follows:
1390 % o image: the image.
1392 % o exception: return any errors or warnings in this structure.
1396 static void Hull(const Image *image,const ssize_t x_offset,
1397 const ssize_t y_offset,const size_t columns,const size_t rows,
1398 const int polarity,Quantum *restrict f,Quantum *restrict g)
1409 assert(image != (const Image *) NULL);
1410 assert(image->signature == MagickSignature);
1411 if (image->debug != MagickFalse)
1412 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1413 assert(f != (Quantum *) NULL);
1414 assert(g != (Quantum *) NULL);
1417 r=p+(y_offset*(columns+2)+x_offset);
1418 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1419 #pragma omp parallel for schedule(static,4) \
1420 magick_threads(image,image,1,1)
1422 for (y=0; y < (ssize_t) rows; y++)
1431 i=(2*y+1)+y*columns;
1433 for (x=0; x < (ssize_t) columns; x++)
1435 v=(MagickRealType) p[i];
1436 if ((MagickRealType) r[i] >= (v+ScaleCharToQuantum(2)))
1437 v+=ScaleCharToQuantum(1);
1442 for (x=0; x < (ssize_t) columns; x++)
1444 v=(MagickRealType) p[i];
1445 if ((MagickRealType) r[i] <= (v-ScaleCharToQuantum(2)))
1446 v-=ScaleCharToQuantum(1);
1453 r=q+(y_offset*(columns+2)+x_offset);
1454 s=q-(y_offset*(columns+2)+x_offset);
1455 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1456 #pragma omp parallel for schedule(static,4) \
1457 magick_threads(image,image,1,1)
1459 for (y=0; y < (ssize_t) rows; y++)
1468 i=(2*y+1)+y*columns;
1470 for (x=0; x < (ssize_t) columns; x++)
1472 v=(MagickRealType) q[i];
1473 if (((MagickRealType) s[i] >= (v+ScaleCharToQuantum(2))) &&
1474 ((MagickRealType) r[i] > v))
1475 v+=ScaleCharToQuantum(1);
1480 for (x=0; x < (ssize_t) columns; x++)
1482 v=(MagickRealType) q[i];
1483 if (((MagickRealType) s[i] <= (v-ScaleCharToQuantum(2))) &&
1484 ((MagickRealType) r[i] < v))
1485 v-=ScaleCharToQuantum(1);
1492 MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1494 #define DespeckleImageTag "Despeckle/Image"
1520 static const ssize_t
1521 X[4] = {0, 1, 1,-1},
1522 Y[4] = {1, 0, 1, 1};
1525 Allocate despeckled image.
1527 assert(image != (const Image *) NULL);
1528 assert(image->signature == MagickSignature);
1529 if (image->debug != MagickFalse)
1530 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1531 assert(exception != (ExceptionInfo *) NULL);
1532 assert(exception->signature == MagickSignature);
1533 despeckle_image=CloneImage(image,0,0,MagickTrue,exception);
1534 if (despeckle_image == (Image *) NULL)
1535 return((Image *) NULL);
1536 status=SetImageStorageClass(despeckle_image,DirectClass,exception);
1537 if (status == MagickFalse)
1539 despeckle_image=DestroyImage(despeckle_image);
1540 return((Image *) NULL);
1543 Allocate image buffer.
1545 length=(size_t) ((image->columns+2)*(image->rows+2));
1546 pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
1547 buffer_info=AcquireVirtualMemory(length,sizeof(*buffer));
1548 if ((pixel_info == (MemoryInfo *) NULL) ||
1549 (buffer_info == (MemoryInfo *) NULL))
1551 if (buffer_info != (MemoryInfo *) NULL)
1552 buffer_info=RelinquishVirtualMemory(buffer_info);
1553 if (pixel_info != (MemoryInfo *) NULL)
1554 pixel_info=RelinquishVirtualMemory(pixel_info);
1555 despeckle_image=DestroyImage(despeckle_image);
1556 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1558 pixels=(Quantum *) GetVirtualMemoryBlob(pixel_info);
1559 buffer=(Quantum *) GetVirtualMemoryBlob(buffer_info);
1561 Reduce speckle in the image.
1564 image_view=AcquireVirtualCacheView(image,exception);
1565 despeckle_view=AcquireAuthenticCacheView(despeckle_image,exception);
1566 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1583 if (status == MagickFalse)
1585 channel=GetPixelChannelChannel(image,i);
1586 traits=GetPixelChannelTraits(image,channel);
1587 despeckle_traits=GetPixelChannelTraits(despeckle_image,channel);
1588 if ((traits == UndefinedPixelTrait) ||
1589 (despeckle_traits == UndefinedPixelTrait))
1591 if ((despeckle_traits & CopyPixelTrait) != 0)
1593 (void) ResetMagickMemory(pixels,0,length*sizeof(*pixels));
1594 j=(ssize_t) image->columns+2;
1595 for (y=0; y < (ssize_t) image->rows; y++)
1597 register const Quantum
1600 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1601 if (p == (const Quantum *) NULL)
1607 for (x=0; x < (ssize_t) image->columns; x++)
1610 p+=GetPixelChannels(image);
1614 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
1615 for (k=0; k < 4; k++)
1617 Hull(image,X[k],Y[k],image->columns,image->rows,1,pixels,buffer);
1618 Hull(image,-X[k],-Y[k],image->columns,image->rows,1,pixels,buffer);
1619 Hull(image,-X[k],-Y[k],image->columns,image->rows,-1,pixels,buffer);
1620 Hull(image,X[k],Y[k],image->columns,image->rows,-1,pixels,buffer);
1622 j=(ssize_t) image->columns+2;
1623 for (y=0; y < (ssize_t) image->rows; y++)
1631 q=GetCacheViewAuthenticPixels(despeckle_view,0,y,despeckle_image->columns,
1633 if (q == (Quantum *) NULL)
1639 for (x=0; x < (ssize_t) image->columns; x++)
1641 SetPixelChannel(despeckle_image,channel,pixels[j++],q);
1642 q+=GetPixelChannels(despeckle_image);
1644 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1645 if (sync == MagickFalse)
1649 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1654 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1655 GetPixelChannels(image));
1656 if (proceed == MagickFalse)
1660 despeckle_view=DestroyCacheView(despeckle_view);
1661 image_view=DestroyCacheView(image_view);
1662 buffer_info=RelinquishVirtualMemory(buffer_info);
1663 pixel_info=RelinquishVirtualMemory(pixel_info);
1664 despeckle_image->type=image->type;
1665 if (status == MagickFalse)
1666 despeckle_image=DestroyImage(despeckle_image);
1667 return(despeckle_image);
1671 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1675 % E d g e I m a g e %
1679 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1681 % EdgeImage() finds edges in an image. Radius defines the radius of the
1682 % convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1685 % The format of the EdgeImage method is:
1687 % Image *EdgeImage(const Image *image,const double radius,
1688 % ExceptionInfo *exception)
1690 % A description of each parameter follows:
1692 % o image: the image.
1694 % o radius: the radius of the pixel neighborhood.
1696 % o exception: return any errors or warnings in this structure.
1699 MagickExport Image *EdgeImage(const Image *image,const double radius,
1700 ExceptionInfo *exception)
1714 assert(image != (const Image *) NULL);
1715 assert(image->signature == MagickSignature);
1716 if (image->debug != MagickFalse)
1717 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1718 assert(exception != (ExceptionInfo *) NULL);
1719 assert(exception->signature == MagickSignature);
1720 width=GetOptimalKernelWidth1D(radius,0.5);
1721 kernel_info=AcquireKernelInfo((const char *) NULL);
1722 if (kernel_info == (KernelInfo *) NULL)
1723 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1724 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1725 kernel_info->width=width;
1726 kernel_info->height=width;
1727 kernel_info->x=(ssize_t) (kernel_info->width-1)/2;
1728 kernel_info->y=(ssize_t) (kernel_info->height-1)/2;
1729 kernel_info->signature=MagickSignature;
1730 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
1731 AcquireAlignedMemory(kernel_info->width,kernel_info->height*
1732 sizeof(*kernel_info->values)));
1733 if (kernel_info->values == (MagickRealType *) NULL)
1735 kernel_info=DestroyKernelInfo(kernel_info);
1736 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1738 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
1739 kernel_info->values[i]=(-1.0);
1740 kernel_info->values[i/2]=(double) kernel_info->width*kernel_info->height-1.0;
1741 edge_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1742 UndefinedCompositeOp,0.0,exception);
1743 kernel_info=DestroyKernelInfo(kernel_info);
1748 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1752 % E m b o s s I m a g e %
1756 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1758 % EmbossImage() returns a grayscale image with a three-dimensional effect.
1759 % We convolve the image with a Gaussian operator of the given radius and
1760 % standard deviation (sigma). For reasonable results, radius should be
1761 % larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1764 % The format of the EmbossImage method is:
1766 % Image *EmbossImage(const Image *image,const double radius,
1767 % const double sigma,ExceptionInfo *exception)
1769 % A description of each parameter follows:
1771 % o image: the image.
1773 % o radius: the radius of the pixel neighborhood.
1775 % o sigma: the standard deviation of the Gaussian, in pixels.
1777 % o exception: return any errors or warnings in this structure.
1780 MagickExport Image *EmbossImage(const Image *image,const double radius,
1781 const double sigma,ExceptionInfo *exception)
1805 assert(image != (const Image *) NULL);
1806 assert(image->signature == MagickSignature);
1807 if (image->debug != MagickFalse)
1808 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1809 assert(exception != (ExceptionInfo *) NULL);
1810 assert(exception->signature == MagickSignature);
1811 width=GetOptimalKernelWidth1D(radius,sigma);
1812 kernel_info=AcquireKernelInfo((const char *) NULL);
1813 if (kernel_info == (KernelInfo *) NULL)
1814 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1815 kernel_info->width=width;
1816 kernel_info->height=width;
1817 kernel_info->x=(ssize_t) (width-1)/2;
1818 kernel_info->y=(ssize_t) (width-1)/2;
1819 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
1820 AcquireAlignedMemory(kernel_info->width,kernel_info->width*
1821 sizeof(*kernel_info->values)));
1822 if (kernel_info->values == (MagickRealType *) NULL)
1824 kernel_info=DestroyKernelInfo(kernel_info);
1825 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1827 j=(ssize_t) (kernel_info->width-1)/2;
1830 for (v=(-j); v <= j; v++)
1832 for (u=(-j); u <= j; u++)
1834 kernel_info->values[i]=(MagickRealType) (((u < 0) || (v < 0) ? -8.0 :
1835 8.0)*exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
1836 (2.0*MagickPI*MagickSigma*MagickSigma));
1838 kernel_info->values[i]=0.0;
1844 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
1845 normalize+=kernel_info->values[i];
1846 gamma=PerceptibleReciprocal(normalize);
1847 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
1848 kernel_info->values[i]*=gamma;
1849 emboss_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1850 UndefinedCompositeOp,0.0,exception);
1851 kernel_info=DestroyKernelInfo(kernel_info);
1852 if (emboss_image != (Image *) NULL)
1853 (void) EqualizeImage(emboss_image,exception);
1854 return(emboss_image);
1858 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1862 % G a u s s i a n B l u r I m a g e %
1866 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1868 % GaussianBlurImage() blurs an image. We convolve the image with a
1869 % Gaussian operator of the given radius and standard deviation (sigma).
1870 % For reasonable results, the radius should be larger than sigma. Use a
1871 % radius of 0 and GaussianBlurImage() selects a suitable radius for you
1873 % The format of the GaussianBlurImage method is:
1875 % Image *GaussianBlurImage(const Image *image,onst double radius,
1876 % const double sigma,ExceptionInfo *exception)
1878 % A description of each parameter follows:
1880 % o image: the image.
1882 % o radius: the radius of the Gaussian, in pixels, not counting the center
1885 % o sigma: the standard deviation of the Gaussian, in pixels.
1887 % o exception: return any errors or warnings in this structure.
1890 MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
1891 const double sigma,ExceptionInfo *exception)
1894 geometry[MaxTextExtent];
1902 assert(image != (const Image *) NULL);
1903 assert(image->signature == MagickSignature);
1904 if (image->debug != MagickFalse)
1905 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1906 assert(exception != (ExceptionInfo *) NULL);
1907 assert(exception->signature == MagickSignature);
1908 (void) FormatLocaleString(geometry,MaxTextExtent,"gaussian:%.20gx%.20g",
1910 kernel_info=AcquireKernelInfo(geometry);
1911 if (kernel_info == (KernelInfo *) NULL)
1912 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1913 blur_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1914 UndefinedCompositeOp,0.0,exception);
1915 kernel_info=DestroyKernelInfo(kernel_info);
1920 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1924 % M o t i o n B l u r I m a g e %
1928 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1930 % MotionBlurImage() simulates motion blur. We convolve the image with a
1931 % Gaussian operator of the given radius and standard deviation (sigma).
1932 % For reasonable results, radius should be larger than sigma. Use a
1933 % radius of 0 and MotionBlurImage() selects a suitable radius for you.
1934 % Angle gives the angle of the blurring motion.
1936 % Andrew Protano contributed this effect.
1938 % The format of the MotionBlurImage method is:
1940 % Image *MotionBlurImage(const Image *image,const double radius,
1941 % const double sigma,const double angle,ExceptionInfo *exception)
1943 % A description of each parameter follows:
1945 % o image: the image.
1947 % o radius: the radius of the Gaussian, in pixels, not counting
1950 % o sigma: the standard deviation of the Gaussian, in pixels.
1952 % o angle: Apply the effect along this angle.
1954 % o exception: return any errors or warnings in this structure.
1958 static MagickRealType *GetMotionBlurKernel(const size_t width,
1969 Generate a 1-D convolution kernel.
1971 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1972 kernel=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory((size_t)
1973 width,sizeof(*kernel)));
1974 if (kernel == (MagickRealType *) NULL)
1977 for (i=0; i < (ssize_t) width; i++)
1979 kernel[i]=(MagickRealType) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
1980 MagickSigma)))/(MagickSQ2PI*MagickSigma));
1981 normalize+=kernel[i];
1983 for (i=0; i < (ssize_t) width; i++)
1984 kernel[i]/=normalize;
1988 MagickExport Image *MotionBlurImage(const Image *image,const double radius,
1989 const double sigma,const double angle,ExceptionInfo *exception)
1991 #define BlurImageTag "Blur/Image"
2025 assert(image != (Image *) NULL);
2026 assert(image->signature == MagickSignature);
2027 if (image->debug != MagickFalse)
2028 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2029 assert(exception != (ExceptionInfo *) NULL);
2030 width=GetOptimalKernelWidth1D(radius,sigma);
2031 kernel=GetMotionBlurKernel(width,sigma);
2032 if (kernel == (MagickRealType *) NULL)
2033 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2034 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
2035 if (offset == (OffsetInfo *) NULL)
2037 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2038 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2040 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2041 if (blur_image == (Image *) NULL)
2043 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2044 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2045 return((Image *) NULL);
2047 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2049 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2050 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2051 blur_image=DestroyImage(blur_image);
2052 return((Image *) NULL);
2054 point.x=(double) width*sin(DegreesToRadians(angle));
2055 point.y=(double) width*cos(DegreesToRadians(angle));
2056 for (i=0; i < (ssize_t) width; i++)
2058 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
2059 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
2066 image_view=AcquireVirtualCacheView(image,exception);
2067 motion_view=AcquireVirtualCacheView(image,exception);
2068 blur_view=AcquireAuthenticCacheView(blur_image,exception);
2069 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2070 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2071 magick_threads(image,blur_image,image->rows,1)
2073 for (y=0; y < (ssize_t) image->rows; y++)
2075 register const Quantum
2084 if (status == MagickFalse)
2086 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2087 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2089 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2094 for (x=0; x < (ssize_t) image->columns; x++)
2099 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2113 register const Quantum
2116 register MagickRealType
2122 channel=GetPixelChannelChannel(image,i);
2123 traits=GetPixelChannelTraits(image,channel);
2124 blur_traits=GetPixelChannelTraits(blur_image,channel);
2125 if ((traits == UndefinedPixelTrait) ||
2126 (blur_traits == UndefinedPixelTrait))
2128 if (((blur_traits & CopyPixelTrait) != 0) ||
2129 (GetPixelReadMask(image,p) == 0))
2131 SetPixelChannel(blur_image,channel,p[i],q);
2136 if ((blur_traits & BlendPixelTrait) == 0)
2138 for (j=0; j < (ssize_t) width; j++)
2140 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+
2141 offset[j].y,1,1,exception);
2142 if (r == (const Quantum *) NULL)
2150 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
2155 for (j=0; j < (ssize_t) width; j++)
2157 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+offset[j].y,1,
2159 if (r == (const Quantum *) NULL)
2164 alpha=(double) (QuantumScale*GetPixelAlpha(image,r));
2165 pixel+=(*k)*alpha*r[i];
2169 gamma=PerceptibleReciprocal(gamma);
2170 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2172 p+=GetPixelChannels(image);
2173 q+=GetPixelChannels(blur_image);
2175 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2177 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2182 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2183 #pragma omp critical (MagickCore_MotionBlurImage)
2185 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2186 if (proceed == MagickFalse)
2190 blur_view=DestroyCacheView(blur_view);
2191 motion_view=DestroyCacheView(motion_view);
2192 image_view=DestroyCacheView(image_view);
2193 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2194 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2195 if (status == MagickFalse)
2196 blur_image=DestroyImage(blur_image);
2201 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2205 % P r e v i e w I m a g e %
2209 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2211 % PreviewImage() tiles 9 thumbnails of the specified image with an image
2212 % processing operation applied with varying parameters. This may be helpful
2213 % pin-pointing an appropriate parameter for a particular image processing
2216 % The format of the PreviewImages method is:
2218 % Image *PreviewImages(const Image *image,const PreviewType preview,
2219 % ExceptionInfo *exception)
2221 % A description of each parameter follows:
2223 % o image: the image.
2225 % o preview: the image processing operation.
2227 % o exception: return any errors or warnings in this structure.
2230 MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2231 ExceptionInfo *exception)
2233 #define NumberTiles 9
2234 #define PreviewImageTag "Preview/Image"
2235 #define DefaultPreviewGeometry "204x204+10+10"
2238 factor[MaxTextExtent],
2239 label[MaxTextExtent];
2284 Open output image file.
2286 assert(image != (Image *) NULL);
2287 assert(image->signature == MagickSignature);
2288 if (image->debug != MagickFalse)
2289 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2293 preview_info=AcquireImageInfo();
2294 SetGeometry(image,&geometry);
2295 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2296 &geometry.width,&geometry.height);
2297 images=NewImageList();
2299 GetQuantizeInfo(&quantize_info);
2305 for (i=0; i < NumberTiles; i++)
2307 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2308 if (thumbnail == (Image *) NULL)
2310 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2312 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel,exception);
2313 if (i == (NumberTiles/2))
2315 (void) QueryColorCompliance("#dfdfdf",AllCompliance,
2316 &thumbnail->matte_color,exception);
2317 AppendImageToList(&images,thumbnail);
2325 preview_image=RotateImage(thumbnail,degrees,exception);
2326 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
2332 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
2333 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",degrees,
2339 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2340 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
2341 preview_image=RollImage(thumbnail,x,y,exception);
2342 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
2343 (double) x,(double) y);
2348 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2349 if (preview_image == (Image *) NULL)
2351 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",2.0*
2353 (void) ModulateImage(preview_image,factor,exception);
2354 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2357 case SaturationPreview:
2359 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2360 if (preview_image == (Image *) NULL)
2362 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",2.0*percentage);
2363 (void) ModulateImage(preview_image,factor,exception);
2364 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2367 case BrightnessPreview:
2369 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2370 if (preview_image == (Image *) NULL)
2372 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
2373 (void) ModulateImage(preview_image,factor,exception);
2374 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2380 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2381 if (preview_image == (Image *) NULL)
2384 (void) GammaImage(preview_image,gamma,exception);
2385 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
2390 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2391 if (preview_image != (Image *) NULL)
2392 for (x=0; x < i; x++)
2393 (void) ContrastImage(preview_image,MagickTrue,exception);
2394 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
2400 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2401 if (preview_image == (Image *) NULL)
2403 for (x=0; x < i; x++)
2404 (void) ContrastImage(preview_image,MagickFalse,exception);
2405 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
2409 case GrayscalePreview:
2411 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2412 if (preview_image == (Image *) NULL)
2415 quantize_info.number_colors=colors;
2416 quantize_info.colorspace=GRAYColorspace;
2417 (void) QuantizeImage(&quantize_info,preview_image,exception);
2418 (void) FormatLocaleString(label,MaxTextExtent,
2419 "-colorspace gray -colors %.20g",(double) colors);
2422 case QuantizePreview:
2424 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2425 if (preview_image == (Image *) NULL)
2428 quantize_info.number_colors=colors;
2429 (void) QuantizeImage(&quantize_info,preview_image,exception);
2430 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
2434 case DespecklePreview:
2436 for (x=0; x < (i-1); x++)
2438 preview_image=DespeckleImage(thumbnail,exception);
2439 if (preview_image == (Image *) NULL)
2441 thumbnail=DestroyImage(thumbnail);
2442 thumbnail=preview_image;
2444 preview_image=DespeckleImage(thumbnail,exception);
2445 if (preview_image == (Image *) NULL)
2447 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
2451 case ReduceNoisePreview:
2453 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2454 (size_t) radius,exception);
2455 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
2458 case AddNoisePreview:
2464 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2469 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2474 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2479 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2484 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2489 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2494 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2498 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2499 (size_t) i,exception);
2500 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
2503 case SharpenPreview:
2505 preview_image=SharpenImage(thumbnail,radius,sigma,exception);
2506 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",radius,
2512 preview_image=BlurImage(thumbnail,radius,sigma,exception);
2513 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
2517 case ThresholdPreview:
2519 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2520 if (preview_image == (Image *) NULL)
2522 (void) BilevelImage(thumbnail,(double) (percentage*((double)
2523 QuantumRange+1.0))/100.0,exception);
2524 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",(double)
2525 (percentage*((double) QuantumRange+1.0))/100.0);
2528 case EdgeDetectPreview:
2530 preview_image=EdgeImage(thumbnail,radius,exception);
2531 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
2536 preview_image=SpreadImage(thumbnail,radius,thumbnail->interpolate,
2538 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",radius+0.5);
2541 case SolarizePreview:
2543 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2544 if (preview_image == (Image *) NULL)
2546 (void) SolarizeImage(preview_image,(double) QuantumRange*percentage/
2548 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
2549 (QuantumRange*percentage)/100.0);
2555 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2557 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",degrees,
2563 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2564 if (preview_image == (Image *) NULL)
2566 geometry.width=(size_t) (2*i+2);
2567 geometry.height=(size_t) (2*i+2);
2570 (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
2571 (void) FormatLocaleString(label,MaxTextExtent,
2572 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
2573 geometry.height,(double) geometry.x,(double) geometry.y);
2576 case SegmentPreview:
2578 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2579 if (preview_image == (Image *) NULL)
2582 (void) SegmentImage(preview_image,sRGBColorspace,MagickFalse,threshold,
2583 threshold,exception);
2584 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
2585 threshold,threshold);
2590 preview_image=SwirlImage(thumbnail,degrees,image->interpolate,
2592 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
2596 case ImplodePreview:
2599 preview_image=ImplodeImage(thumbnail,degrees,image->interpolate,
2601 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
2607 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,
2608 image->interpolate,exception);
2609 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",0.5*degrees,
2613 case OilPaintPreview:
2615 preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2617 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",radius,
2621 case CharcoalDrawingPreview:
2623 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
2625 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",radius,
2632 filename[MaxTextExtent];
2640 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2641 if (preview_image == (Image *) NULL)
2643 preview_info->quality=(size_t) percentage;
2644 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
2645 preview_info->quality);
2646 file=AcquireUniqueFileResource(filename);
2649 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
2650 "jpeg:%s",filename);
2651 status=WriteImage(preview_info,preview_image,exception);
2652 if (status != MagickFalse)
2657 (void) CopyMagickString(preview_info->filename,
2658 preview_image->filename,MaxTextExtent);
2659 quality_image=ReadImage(preview_info,exception);
2660 if (quality_image != (Image *) NULL)
2662 preview_image=DestroyImage(preview_image);
2663 preview_image=quality_image;
2666 (void) RelinquishUniqueFileResource(preview_image->filename);
2667 if ((GetBlobSize(preview_image)/1024) >= 1024)
2668 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
2669 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2672 if (GetBlobSize(preview_image) >= 1024)
2673 (void) FormatLocaleString(label,MaxTextExtent,
2674 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
2675 GetBlobSize(preview_image))/1024.0);
2677 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
2678 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
2682 thumbnail=DestroyImage(thumbnail);
2686 if (preview_image == (Image *) NULL)
2688 (void) DeleteImageProperty(preview_image,"label");
2689 (void) SetImageProperty(preview_image,"label",label,exception);
2690 AppendImageToList(&images,preview_image);
2691 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2693 if (proceed == MagickFalse)
2696 if (images == (Image *) NULL)
2698 preview_info=DestroyImageInfo(preview_info);
2699 return((Image *) NULL);
2704 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2705 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2706 montage_info->shadow=MagickTrue;
2707 (void) CloneString(&montage_info->tile,"3x3");
2708 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2709 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2710 montage_image=MontageImages(images,montage_info,exception);
2711 montage_info=DestroyMontageInfo(montage_info);
2712 images=DestroyImageList(images);
2713 if (montage_image == (Image *) NULL)
2714 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2715 if (montage_image->montage != (char *) NULL)
2718 Free image directory.
2720 montage_image->montage=(char *) RelinquishMagickMemory(
2721 montage_image->montage);
2722 if (image->directory != (char *) NULL)
2723 montage_image->directory=(char *) RelinquishMagickMemory(
2724 montage_image->directory);
2726 preview_info=DestroyImageInfo(preview_info);
2727 return(montage_image);
2731 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2735 % R o t a t i o n a l B l u r I m a g e %
2739 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2741 % RotationalBlurImage() applies a radial blur to the image.
2743 % Andrew Protano contributed this effect.
2745 % The format of the RotationalBlurImage method is:
2747 % Image *RotationalBlurImage(const Image *image,const double angle,
2748 % ExceptionInfo *exception)
2750 % A description of each parameter follows:
2752 % o image: the image.
2754 % o angle: the angle of the radial blur.
2758 % o exception: return any errors or warnings in this structure.
2761 MagickExport Image *RotationalBlurImage(const Image *image,const double angle,
2762 ExceptionInfo *exception)
2798 Allocate blur image.
2800 assert(image != (Image *) NULL);
2801 assert(image->signature == MagickSignature);
2802 if (image->debug != MagickFalse)
2803 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2804 assert(exception != (ExceptionInfo *) NULL);
2805 assert(exception->signature == MagickSignature);
2806 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2807 if (blur_image == (Image *) NULL)
2808 return((Image *) NULL);
2809 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2811 blur_image=DestroyImage(blur_image);
2812 return((Image *) NULL);
2814 blur_center.x=(double) (image->columns-1)/2.0;
2815 blur_center.y=(double) (image->rows-1)/2.0;
2816 blur_radius=hypot(blur_center.x,blur_center.y);
2817 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
2818 theta=DegreesToRadians(angle)/(double) (n-1);
2819 cos_theta=(double *) AcquireQuantumMemory((size_t) n,
2820 sizeof(*cos_theta));
2821 sin_theta=(double *) AcquireQuantumMemory((size_t) n,
2822 sizeof(*sin_theta));
2823 if ((cos_theta == (double *) NULL) ||
2824 (sin_theta == (double *) NULL))
2826 blur_image=DestroyImage(blur_image);
2827 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2829 offset=theta*(double) (n-1)/2.0;
2830 for (i=0; i < (ssize_t) n; i++)
2832 cos_theta[i]=cos((double) (theta*i-offset));
2833 sin_theta[i]=sin((double) (theta*i-offset));
2840 image_view=AcquireVirtualCacheView(image,exception);
2841 radial_view=AcquireVirtualCacheView(image,exception);
2842 blur_view=AcquireAuthenticCacheView(blur_image,exception);
2843 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2844 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2845 magick_threads(image,blur_image,image->rows,1)
2847 for (y=0; y < (ssize_t) image->rows; y++)
2849 register const Quantum
2858 if (status == MagickFalse)
2860 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2861 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2863 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2868 for (x=0; x < (ssize_t) image->columns; x++)
2882 center.x=(double) x-blur_center.x;
2883 center.y=(double) y-blur_center.y;
2884 radius=hypot((double) center.x,center.y);
2889 step=(size_t) (blur_radius/radius);
2896 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2909 register const Quantum
2915 channel=GetPixelChannelChannel(image,i);
2916 traits=GetPixelChannelTraits(image,channel);
2917 blur_traits=GetPixelChannelTraits(blur_image,channel);
2918 if ((traits == UndefinedPixelTrait) ||
2919 (blur_traits == UndefinedPixelTrait))
2921 if (((blur_traits & CopyPixelTrait) != 0) ||
2922 (GetPixelReadMask(image,p) == 0))
2924 SetPixelChannel(blur_image,channel,p[i],q);
2929 if ((blur_traits & BlendPixelTrait) == 0)
2931 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2933 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2934 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2935 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2937 if (r == (const Quantum *) NULL)
2945 gamma=PerceptibleReciprocal(gamma);
2946 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2949 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2951 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2952 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2953 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2955 if (r == (const Quantum *) NULL)
2960 pixel+=GetPixelAlpha(image,r)*r[i];
2961 gamma+=GetPixelAlpha(image,r);
2963 gamma=PerceptibleReciprocal(gamma);
2964 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2966 p+=GetPixelChannels(image);
2967 q+=GetPixelChannels(blur_image);
2969 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2971 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2976 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2977 #pragma omp critical (MagickCore_RotationalBlurImage)
2979 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2980 if (proceed == MagickFalse)
2984 blur_view=DestroyCacheView(blur_view);
2985 radial_view=DestroyCacheView(radial_view);
2986 image_view=DestroyCacheView(image_view);
2987 cos_theta=(double *) RelinquishMagickMemory(cos_theta);
2988 sin_theta=(double *) RelinquishMagickMemory(sin_theta);
2989 if (status == MagickFalse)
2990 blur_image=DestroyImage(blur_image);
2995 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2999 % S e l e c t i v e B l u r I m a g e %
3003 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3005 % SelectiveBlurImage() selectively blur pixels within a contrast threshold.
3006 % It is similar to the unsharpen mask that sharpens everything with contrast
3007 % above a certain threshold.
3009 % The format of the SelectiveBlurImage method is:
3011 % Image *SelectiveBlurImage(const Image *image,const double radius,
3012 % const double sigma,const double threshold,ExceptionInfo *exception)
3014 % A description of each parameter follows:
3016 % o image: the image.
3018 % o radius: the radius of the Gaussian, in pixels, not counting the center
3021 % o sigma: the standard deviation of the Gaussian, in pixels.
3023 % o threshold: only pixels within this contrast threshold are included
3024 % in the blur operation.
3026 % o exception: return any errors or warnings in this structure.
3029 MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
3030 const double sigma,const double threshold,ExceptionInfo *exception)
3032 #define SelectiveBlurImageTag "SelectiveBlur/Image"
3066 Initialize blur image attributes.
3068 assert(image != (Image *) NULL);
3069 assert(image->signature == MagickSignature);
3070 if (image->debug != MagickFalse)
3071 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3072 assert(exception != (ExceptionInfo *) NULL);
3073 assert(exception->signature == MagickSignature);
3074 width=GetOptimalKernelWidth1D(radius,sigma);
3075 kernel=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory((size_t)
3076 width,width*sizeof(*kernel)));
3077 if (kernel == (MagickRealType *) NULL)
3078 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3079 j=(ssize_t) (width-1)/2;
3081 for (v=(-j); v <= j; v++)
3083 for (u=(-j); u <= j; u++)
3084 kernel[i++]=(MagickRealType) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
3085 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3087 if (image->debug != MagickFalse)
3090 format[MaxTextExtent],
3093 register const MagickRealType
3100 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
3101 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
3103 message=AcquireString("");
3105 for (v=0; v < (ssize_t) width; v++)
3108 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
3109 (void) ConcatenateString(&message,format);
3110 for (u=0; u < (ssize_t) width; u++)
3112 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",(double) *k++);
3113 (void) ConcatenateString(&message,format);
3115 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
3117 message=DestroyString(message);
3119 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3120 if (blur_image == (Image *) NULL)
3121 return((Image *) NULL);
3122 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
3124 blur_image=DestroyImage(blur_image);
3125 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3126 return((Image *) NULL);
3128 luminance_image=CloneImage(image,0,0,MagickTrue,exception);
3129 if (luminance_image == (Image *) NULL)
3131 blur_image=DestroyImage(blur_image);
3132 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3133 return((Image *) NULL);
3135 status=TransformImageColorspace(luminance_image,GRAYColorspace,exception);
3136 if (status == MagickFalse)
3138 luminance_image=DestroyImage(luminance_image);
3139 blur_image=DestroyImage(blur_image);
3140 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3141 return((Image *) NULL);
3144 Threshold blur image.
3148 center=(ssize_t) (GetPixelChannels(image)*(image->columns+width)*
3149 ((width-1)/2L)+GetPixelChannels(image)*((width-1)/2L));
3150 image_view=AcquireVirtualCacheView(image,exception);
3151 luminance_view=AcquireVirtualCacheView(luminance_image,exception);
3152 blur_view=AcquireAuthenticCacheView(blur_image,exception);
3153 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3154 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3155 magick_threads(image,blur_image,image->rows,1)
3157 for (y=0; y < (ssize_t) image->rows; y++)
3165 register const Quantum
3175 if (status == MagickFalse)
3177 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) (width-1)/2L),y-(ssize_t)
3178 ((width-1)/2L),image->columns+width,width,exception);
3179 l=GetCacheViewVirtualPixels(luminance_view,-((ssize_t) (width-1)/2L),y-
3180 (ssize_t) ((width-1)/2L),luminance_image->columns+width,width,exception);
3181 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3183 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3188 for (x=0; x < (ssize_t) image->columns; x++)
3196 intensity=GetPixelIntensity(image,p+center);
3197 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3211 register const MagickRealType
3214 register const Quantum
3215 *restrict luminance_pixels,
3224 channel=GetPixelChannelChannel(image,i);
3225 traits=GetPixelChannelTraits(image,channel);
3226 blur_traits=GetPixelChannelTraits(blur_image,channel);
3227 if ((traits == UndefinedPixelTrait) ||
3228 (blur_traits == UndefinedPixelTrait))
3230 if (((blur_traits & CopyPixelTrait) != 0) ||
3231 (GetPixelReadMask(image,p+center) == 0))
3233 SetPixelChannel(blur_image,channel,p[center+i],q);
3241 if ((blur_traits & BlendPixelTrait) == 0)
3243 for (v=0; v < (ssize_t) width; v++)
3245 for (u=0; u < (ssize_t) width; u++)
3247 contrast=GetPixelIntensity(luminance_image,luminance_pixels)-
3249 if (fabs(contrast) < threshold)
3251 pixel+=(*k)*pixels[i];
3255 pixels+=GetPixelChannels(image);
3256 luminance_pixels+=GetPixelChannels(luminance_image);
3258 pixels+=(image->columns-1)*GetPixelChannels(image);
3259 luminance_pixels+=luminance_image->columns*
3260 GetPixelChannels(luminance_image);
3262 if (fabs((double) gamma) < MagickEpsilon)
3264 SetPixelChannel(blur_image,channel,p[center+i],q);
3267 gamma=PerceptibleReciprocal(gamma);
3268 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3271 for (v=0; v < (ssize_t) width; v++)
3273 for (u=0; u < (ssize_t) width; u++)
3275 contrast=GetPixelIntensity(image,pixels)-intensity;
3276 if (fabs(contrast) < threshold)
3278 alpha=(double) (QuantumScale*GetPixelAlpha(image,pixels));
3279 pixel+=(*k)*alpha*pixels[i];
3283 pixels+=GetPixelChannels(image);
3284 luminance_pixels+=GetPixelChannels(luminance_image);
3286 pixels+=(image->columns-1)*GetPixelChannels(image);
3287 luminance_pixels+=luminance_image->columns*
3288 GetPixelChannels(luminance_image);
3290 if (fabs((double) gamma) < MagickEpsilon)
3292 SetPixelChannel(blur_image,channel,p[center+i],q);
3295 gamma=PerceptibleReciprocal(gamma);
3296 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3298 p+=GetPixelChannels(image);
3299 l+=GetPixelChannels(luminance_image);
3300 q+=GetPixelChannels(blur_image);
3302 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3303 if (sync == MagickFalse)
3305 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3310 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3311 #pragma omp critical (MagickCore_SelectiveBlurImage)
3313 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3315 if (proceed == MagickFalse)
3319 blur_image->type=image->type;
3320 blur_view=DestroyCacheView(blur_view);
3321 image_view=DestroyCacheView(image_view);
3322 luminance_image=DestroyImage(luminance_image);
3323 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3324 if (status == MagickFalse)
3325 blur_image=DestroyImage(blur_image);
3330 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3334 % S h a d e I m a g e %
3338 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3340 % ShadeImage() shines a distant light on an image to create a
3341 % three-dimensional effect. You control the positioning of the light with
3342 % azimuth and elevation; azimuth is measured in degrees off the x axis
3343 % and elevation is measured in pixels above the Z axis.
3345 % The format of the ShadeImage method is:
3347 % Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3348 % const double azimuth,const double elevation,ExceptionInfo *exception)
3350 % A description of each parameter follows:
3352 % o image: the image.
3354 % o gray: A value other than zero shades the intensity of each pixel.
3356 % o azimuth, elevation: Define the light source direction.
3358 % o exception: return any errors or warnings in this structure.
3361 MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3362 const double azimuth,const double elevation,ExceptionInfo *exception)
3364 #define ShadeImageTag "Shade/Image"
3387 Initialize shaded image attributes.
3389 assert(image != (const Image *) NULL);
3390 assert(image->signature == MagickSignature);
3391 if (image->debug != MagickFalse)
3392 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3393 assert(exception != (ExceptionInfo *) NULL);
3394 assert(exception->signature == MagickSignature);
3395 linear_image=CloneImage(image,0,0,MagickTrue,exception);
3396 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3397 if ((linear_image == (Image *) NULL) || (shade_image == (Image *) NULL))
3399 if (linear_image != (Image *) NULL)
3400 linear_image=DestroyImage(linear_image);
3401 if (shade_image != (Image *) NULL)
3402 shade_image=DestroyImage(shade_image);
3403 return((Image *) NULL);
3405 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
3407 linear_image=DestroyImage(linear_image);
3408 shade_image=DestroyImage(shade_image);
3409 return((Image *) NULL);
3412 Compute the light vector.
3414 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3415 cos(DegreesToRadians(elevation));
3416 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3417 cos(DegreesToRadians(elevation));
3418 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3424 image_view=AcquireVirtualCacheView(linear_image,exception);
3425 shade_view=AcquireAuthenticCacheView(shade_image,exception);
3426 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3427 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3428 magick_threads(linear_image,shade_image,linear_image->rows,1)
3430 for (y=0; y < (ssize_t) linear_image->rows; y++)
3440 register const Quantum
3452 if (status == MagickFalse)
3454 p=GetCacheViewVirtualPixels(image_view,-1,y-1,linear_image->columns+2,3,
3456 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3458 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3464 Shade this row of pixels.
3466 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
3467 pre=p+GetPixelChannels(linear_image);
3468 center=pre+(linear_image->columns+2)*GetPixelChannels(linear_image);
3469 post=center+(linear_image->columns+2)*GetPixelChannels(linear_image);
3470 for (x=0; x < (ssize_t) linear_image->columns; x++)
3476 Determine the surface normal and compute shading.
3479 GetPixelIntensity(linear_image,pre-GetPixelChannels(linear_image))+
3480 GetPixelIntensity(linear_image,center-GetPixelChannels(linear_image))+
3481 GetPixelIntensity(linear_image,post-GetPixelChannels(linear_image))-
3482 GetPixelIntensity(linear_image,pre+GetPixelChannels(linear_image))-
3483 GetPixelIntensity(linear_image,center+GetPixelChannels(linear_image))-
3484 GetPixelIntensity(linear_image,post+GetPixelChannels(linear_image)));
3486 GetPixelIntensity(linear_image,post-GetPixelChannels(linear_image))+
3487 GetPixelIntensity(linear_image,post)+
3488 GetPixelIntensity(linear_image,post+GetPixelChannels(linear_image))-
3489 GetPixelIntensity(linear_image,pre-GetPixelChannels(linear_image))-
3490 GetPixelIntensity(linear_image,pre)-
3491 GetPixelIntensity(linear_image,pre+GetPixelChannels(linear_image)));
3492 if ((normal.x == 0.0) && (normal.y == 0.0))
3497 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3498 if (distance > MagickEpsilon)
3500 normal_distance=normal.x*normal.x+normal.y*normal.y+
3502 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3503 shade=distance/sqrt((double) normal_distance);
3506 for (i=0; i < (ssize_t) GetPixelChannels(linear_image); i++)
3515 channel=GetPixelChannelChannel(linear_image,i);
3516 traits=GetPixelChannelTraits(linear_image,channel);
3517 shade_traits=GetPixelChannelTraits(shade_image,channel);
3518 if ((traits == UndefinedPixelTrait) ||
3519 (shade_traits == UndefinedPixelTrait))
3521 if (((shade_traits & CopyPixelTrait) != 0) ||
3522 (GetPixelReadMask(linear_image,center) == 0))
3524 SetPixelChannel(shade_image,channel,center[i],q);
3527 if (gray != MagickFalse)
3529 SetPixelChannel(shade_image,channel,ClampToQuantum(shade),q);
3532 SetPixelChannel(shade_image,channel,ClampToQuantum(QuantumScale*shade*
3535 pre+=GetPixelChannels(linear_image);
3536 center+=GetPixelChannels(linear_image);
3537 post+=GetPixelChannels(linear_image);
3538 q+=GetPixelChannels(shade_image);
3540 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3542 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3547 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3548 #pragma omp critical (MagickCore_ShadeImage)
3550 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3551 if (proceed == MagickFalse)
3555 shade_view=DestroyCacheView(shade_view);
3556 image_view=DestroyCacheView(image_view);
3557 linear_image=DestroyImage(linear_image);
3558 if (status == MagickFalse)
3559 shade_image=DestroyImage(shade_image);
3560 return(shade_image);
3564 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3568 % S h a r p e n I m a g e %
3572 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3574 % SharpenImage() sharpens the image. We convolve the image with a Gaussian
3575 % operator of the given radius and standard deviation (sigma). For
3576 % reasonable results, radius should be larger than sigma. Use a radius of 0
3577 % and SharpenImage() selects a suitable radius for you.
3579 % Using a separable kernel would be faster, but the negative weights cancel
3580 % out on the corners of the kernel producing often undesirable ringing in the
3581 % filtered result; this can be avoided by using a 2D gaussian shaped image
3582 % sharpening kernel instead.
3584 % The format of the SharpenImage method is:
3586 % Image *SharpenImage(const Image *image,const double radius,
3587 % const double sigma,ExceptionInfo *exception)
3589 % A description of each parameter follows:
3591 % o image: the image.
3593 % o radius: the radius of the Gaussian, in pixels, not counting the center
3596 % o sigma: the standard deviation of the Laplacian, in pixels.
3598 % o exception: return any errors or warnings in this structure.
3601 MagickExport Image *SharpenImage(const Image *image,const double radius,
3602 const double sigma,ExceptionInfo *exception)
3625 assert(image != (const Image *) NULL);
3626 assert(image->signature == MagickSignature);
3627 if (image->debug != MagickFalse)
3628 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3629 assert(exception != (ExceptionInfo *) NULL);
3630 assert(exception->signature == MagickSignature);
3631 width=GetOptimalKernelWidth2D(radius,sigma);
3632 kernel_info=AcquireKernelInfo((const char *) NULL);
3633 if (kernel_info == (KernelInfo *) NULL)
3634 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3635 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3636 kernel_info->width=width;
3637 kernel_info->height=width;
3638 kernel_info->x=(ssize_t) (width-1)/2;
3639 kernel_info->y=(ssize_t) (width-1)/2;
3640 kernel_info->signature=MagickSignature;
3641 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
3642 AcquireAlignedMemory(kernel_info->width,kernel_info->height*
3643 sizeof(*kernel_info->values)));
3644 if (kernel_info->values == (MagickRealType *) NULL)
3646 kernel_info=DestroyKernelInfo(kernel_info);
3647 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3650 j=(ssize_t) (kernel_info->width-1)/2;
3652 for (v=(-j); v <= j; v++)
3654 for (u=(-j); u <= j; u++)
3656 kernel_info->values[i]=(MagickRealType) (-exp(-((double) u*u+v*v)/(2.0*
3657 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3658 normalize+=kernel_info->values[i];
3662 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
3664 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
3665 normalize+=kernel_info->values[i];
3666 gamma=PerceptibleReciprocal(normalize);
3667 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
3668 kernel_info->values[i]*=gamma;
3669 sharp_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
3670 UndefinedCompositeOp,0.0,exception);
3671 kernel_info=DestroyKernelInfo(kernel_info);
3672 return(sharp_image);
3676 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3680 % S p r e a d I m a g e %
3684 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3686 % SpreadImage() is a special effects method that randomly displaces each
3687 % pixel in a block defined by the radius parameter.
3689 % The format of the SpreadImage method is:
3691 % Image *SpreadImage(const Image *image,const double radius,
3692 % const PixelInterpolateMethod method,ExceptionInfo *exception)
3694 % A description of each parameter follows:
3696 % o image: the image.
3698 % o radius: choose a random pixel in a neighborhood of this extent.
3700 % o method: the pixel interpolation method.
3702 % o exception: return any errors or warnings in this structure.
3705 MagickExport Image *SpreadImage(const Image *image,const double radius,
3706 const PixelInterpolateMethod method,ExceptionInfo *exception)
3708 #define SpreadImageTag "Spread/Image"
3724 **restrict random_info;
3732 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3738 Initialize spread image attributes.
3740 assert(image != (Image *) NULL);
3741 assert(image->signature == MagickSignature);
3742 if (image->debug != MagickFalse)
3743 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3744 assert(exception != (ExceptionInfo *) NULL);
3745 assert(exception->signature == MagickSignature);
3746 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3748 if (spread_image == (Image *) NULL)
3749 return((Image *) NULL);
3750 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
3752 spread_image=DestroyImage(spread_image);
3753 return((Image *) NULL);
3760 width=GetOptimalKernelWidth1D(radius,0.5);
3761 random_info=AcquireRandomInfoThreadSet();
3762 image_view=AcquireVirtualCacheView(image,exception);
3763 spread_view=AcquireAuthenticCacheView(spread_image,exception);
3764 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3765 key=GetRandomSecretKey(random_info[0]);
3766 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3767 magick_threads(image,spread_image,image->rows,key == ~0UL)
3769 for (y=0; y < (ssize_t) image->rows; y++)
3772 id = GetOpenMPThreadId();
3780 if (status == MagickFalse)
3782 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
3784 if (q == (Quantum *) NULL)
3789 for (x=0; x < (ssize_t) image->columns; x++)
3794 point.x=GetPseudoRandomValue(random_info[id]);
3795 point.y=GetPseudoRandomValue(random_info[id]);
3796 status=InterpolatePixelChannels(image,image_view,spread_image,method,
3797 (double) x+width*point.x-0.5,(double) y+width*point.y-0.5,q,exception);
3798 q+=GetPixelChannels(spread_image);
3800 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
3802 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3807 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3808 #pragma omp critical (MagickCore_SpreadImage)
3810 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3811 if (proceed == MagickFalse)
3815 spread_view=DestroyCacheView(spread_view);
3816 image_view=DestroyCacheView(image_view);
3817 random_info=DestroyRandomInfoThreadSet(random_info);
3818 if (status == MagickFalse)
3819 spread_image=DestroyImage(spread_image);
3820 return(spread_image);
3824 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3828 % U n s h a r p M a s k I m a g e %
3832 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3834 % UnsharpMaskImage() sharpens one or more image channels. We convolve the
3835 % image with a Gaussian operator of the given radius and standard deviation
3836 % (sigma). For reasonable results, radius should be larger than sigma. Use a
3837 % radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
3839 % The format of the UnsharpMaskImage method is:
3841 % Image *UnsharpMaskImage(const Image *image,const double radius,
3842 % const double sigma,const double amount,const double threshold,
3843 % ExceptionInfo *exception)
3845 % A description of each parameter follows:
3847 % o image: the image.
3849 % o radius: the radius of the Gaussian, in pixels, not counting the center
3852 % o sigma: the standard deviation of the Gaussian, in pixels.
3854 % o gain: the percentage of the difference between the original and the
3855 % blur image that is added back into the original.
3857 % o threshold: the threshold in pixels needed to apply the diffence gain.
3859 % o exception: return any errors or warnings in this structure.
3862 MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
3863 const double sigma,const double gain,const double threshold,
3864 ExceptionInfo *exception)
3866 #define SharpenImageTag "Sharpen/Image"
3887 assert(image != (const Image *) NULL);
3888 assert(image->signature == MagickSignature);
3889 if (image->debug != MagickFalse)
3890 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3891 assert(exception != (ExceptionInfo *) NULL);
3892 unsharp_image=BlurImage(image,radius,sigma,exception);
3893 if (unsharp_image == (Image *) NULL)
3894 return((Image *) NULL);
3895 quantum_threshold=(double) QuantumRange*threshold;
3901 image_view=AcquireVirtualCacheView(image,exception);
3902 unsharp_view=AcquireAuthenticCacheView(unsharp_image,exception);
3903 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3904 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3905 magick_threads(image,unsharp_image,image->rows,1)
3907 for (y=0; y < (ssize_t) image->rows; y++)
3909 register const Quantum
3918 if (status == MagickFalse)
3920 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3921 q=QueueCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
3923 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3928 for (x=0; x < (ssize_t) image->columns; x++)
3933 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3945 channel=GetPixelChannelChannel(image,i);
3946 traits=GetPixelChannelTraits(image,channel);
3947 unsharp_traits=GetPixelChannelTraits(unsharp_image,channel);
3948 if ((traits == UndefinedPixelTrait) ||
3949 (unsharp_traits == UndefinedPixelTrait))
3951 if (((unsharp_traits & CopyPixelTrait) != 0) ||
3952 (GetPixelReadMask(image,p) != 0))
3954 SetPixelChannel(unsharp_image,channel,p[i],q);
3957 pixel=p[i]-(double) GetPixelChannel(unsharp_image,channel,q);
3958 if (fabs(2.0*pixel) < quantum_threshold)
3959 pixel=(double) p[i];
3961 pixel=(double) p[i]+gain*pixel;
3962 SetPixelChannel(unsharp_image,channel,ClampToQuantum(pixel),q);
3964 p+=GetPixelChannels(image);
3965 q+=GetPixelChannels(unsharp_image);
3967 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
3969 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3974 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3975 #pragma omp critical (MagickCore_UnsharpMaskImage)
3977 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
3978 if (proceed == MagickFalse)
3982 unsharp_image->type=image->type;
3983 unsharp_view=DestroyCacheView(unsharp_view);
3984 image_view=DestroyCacheView(image_view);
3985 if (status == MagickFalse)
3986 unsharp_image=DestroyImage(unsharp_image);
3987 return(unsharp_image);