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,const double upper_percent,
866 % const size_t threshold,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
896 static MagickBooleanType IsAuthenticPixel(const Image *image,const ssize_t x,
899 if ((x < 0) || (x >= (ssize_t) image->columns))
901 if ((y < 0) || (y >= (ssize_t) image->rows))
906 static MagickBooleanType TraceEdge(Image *edge_image,CacheView *edge_view,
907 MatrixInfo *pixel_cache,const ssize_t x,const ssize_t y,
908 const double threshold,ExceptionInfo *exception)
919 q=GetCacheViewAuthenticPixels(edge_view,x,y,1,1,exception);
920 if ((q != (Quantum *) NULL) && (GetPixelIntensity(edge_image,q) == 0))
926 Edge due to pixel gradient between upper and lower thresholds.
929 status=SyncCacheViewAuthenticPixels(edge_view,exception);
930 if (status != MagickFalse)
932 for (v=(-1); v <= 1; v++)
937 for (u=(-1); u <= 1; u++)
939 if ((u == 0) && (v == 0))
941 if (IsAuthenticPixel(edge_image,x+u,y+v) == MagickFalse)
944 Not an edge if gradient value is below the lower threshold.
946 (void) GetMatrixElement(pixel_cache,x+u,y+v,&pixel);
947 if (pixel.intensity < threshold)
949 status=TraceEdge(edge_image,edge_view,pixel_cache,x+u,y+v,
950 threshold,exception);
951 if (status != MagickFalse)
961 MagickExport Image *CannyEdgeImage(const Image *image,const double radius,
962 const double sigma,const double lower_precent,const double upper_percent,
963 ExceptionInfo *exception)
969 geometry[MaxTextExtent];
998 assert(image != (const Image *) NULL);
999 assert(image->signature == MagickSignature);
1000 if (image->debug != MagickFalse)
1001 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1002 assert(exception != (ExceptionInfo *) NULL);
1003 assert(exception->signature == MagickSignature);
1007 (void) FormatLocaleString(geometry,MaxTextExtent,
1008 "blur:%.20gx%.20g;blur:%.20gx%.20g+90",radius,sigma,radius,sigma);
1009 kernel_info=AcquireKernelInfo(geometry);
1010 if (kernel_info == (KernelInfo *) NULL)
1011 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1012 edge_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1013 UndefinedCompositeOp,0.0,exception);
1014 kernel_info=DestroyKernelInfo(kernel_info);
1015 if (edge_image == (Image *) NULL)
1016 return((Image *) NULL);
1017 if (SetImageColorspace(edge_image,GRAYColorspace,exception) == MagickFalse)
1019 edge_image=DestroyImage(edge_image);
1020 return((Image *) NULL);
1023 Find the intensity gradient of the image.
1025 pixel_cache=AcquireMatrixInfo(edge_image->columns,edge_image->rows,
1026 sizeof(CannyInfo),exception);
1027 if (pixel_cache == (MatrixInfo *) NULL)
1029 edge_image=DestroyImage(edge_image);
1030 return((Image *) NULL);
1033 edge_view=AcquireVirtualCacheView(edge_image,exception);
1034 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1035 #pragma omp parallel for schedule(static,4) shared(status) \
1036 magick_threads(edge_image,edge_image,edge_image->rows,1)
1038 for (y=0; y < (ssize_t) edge_image->rows; y++)
1040 register const Quantum
1046 if (status == MagickFalse)
1048 p=GetCacheViewVirtualPixels(edge_view,-1,y-1,edge_image->columns+2,3,
1050 if (p == (const Quantum *) NULL)
1055 for (x=0; x < (ssize_t) edge_image->columns; x++)
1064 register const Quantum
1065 *restrict kernel_pixels;
1073 { -1.0, 0.0, +1.0 },
1074 { -2.0, 0.0, +2.0 },
1079 { +1.0, +2.0, +1.0 },
1081 { -1.0, -2.0, -1.0 }
1084 (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
1088 for (v=0; v < 3; v++)
1093 for (u=0; u < 3; u++)
1098 intensity=GetPixelIntensity(edge_image,kernel_pixels+u);
1099 dx+=Gx[v][u]*intensity;
1100 dy+=Gy[v][u]*intensity;
1102 kernel_pixels+=edge_image->columns+2;
1104 pixel.magnitude=sqrt(dx*dx+dy*dy);
1105 pixel.orientation=2;
1114 if (theta < -2.41421356237)
1115 pixel.orientation=0;
1117 if (theta < -0.414213562373)
1118 pixel.orientation=1;
1120 pixel.orientation=2;
1124 if (theta > 2.41421356237)
1125 pixel.orientation=0;
1127 if (theta > 0.414213562373)
1128 pixel.orientation=3;
1130 pixel.orientation=2;
1133 if (SetMatrixElement(pixel_cache,x,y,&pixel) == MagickFalse)
1135 p+=GetPixelChannels(edge_image);
1138 edge_view=DestroyCacheView(edge_view);
1140 Non-maxima suppression, remove pixels that are not considered to be part
1143 histogram=(size_t *) AcquireQuantumMemory(65536,sizeof(*histogram));
1144 if (histogram == (size_t *) NULL)
1146 pixel_cache=DestroyMatrixInfo(pixel_cache);
1147 edge_image=DestroyImage(edge_image);
1148 return((Image *) NULL);
1150 (void) ResetMagickMemory(histogram,0,65536*sizeof(*histogram));
1151 edge_view=AcquireAuthenticCacheView(edge_image,exception);
1152 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1153 #pragma omp parallel for schedule(static,4) shared(status) \
1154 magick_threads(edge_image,edge_image,edge_image->rows,1)
1156 for (y=0; y < (ssize_t) edge_image->rows; y++)
1164 if (status == MagickFalse)
1166 q=GetCacheViewAuthenticPixels(edge_view,0,y,edge_image->columns,1,
1168 if (q == (Quantum *) NULL)
1173 for (x=0; x < (ssize_t) edge_image->columns; x++)
1180 (void) GetMatrixElement(pixel_cache,x,y,&pixel);
1181 switch (pixel.orientation)
1188 (void) GetMatrixElement(pixel_cache,x,y-1,&alpha_pixel);
1189 (void) GetMatrixElement(pixel_cache,x,y+1,&beta_pixel);
1197 (void) GetMatrixElement(pixel_cache,x-1,y-1,&alpha_pixel);
1198 (void) GetMatrixElement(pixel_cache,x+1,y+1,&beta_pixel);
1206 (void) GetMatrixElement(pixel_cache,x-1,y,&alpha_pixel);
1207 (void) GetMatrixElement(pixel_cache,x+1,y,&beta_pixel);
1214 (void) GetMatrixElement(pixel_cache,x+1,y-1,&alpha_pixel);
1215 (void) GetMatrixElement(pixel_cache,x-1,y+1,&beta_pixel);
1218 pixel.intensity=pixel.magnitude;
1219 if ((pixel.magnitude < alpha_pixel.magnitude) ||
1220 (pixel.magnitude < beta_pixel.magnitude))
1223 if (pixel.magnitude > QuantumRange)
1224 pixel.intensity=QuantumRange;
1225 (void) SetMatrixElement(pixel_cache,x,y,&pixel);
1226 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1227 #pragma omp critical (MagickCore_CannyEdgeImage)
1229 histogram[ScaleQuantumToShort(ClampToQuantum(pixel.intensity))]++;
1231 q+=GetPixelChannels(edge_image);
1233 if (SyncCacheViewAuthenticPixels(edge_view,exception) == MagickFalse)
1236 edge_view=DestroyCacheView(edge_view);
1238 Estimate hysteresis threshold.
1240 number_pixels=(size_t) (lower_precent*(image->columns*image->rows-
1243 for (i=65535; count < (ssize_t) number_pixels; i--)
1244 count+=histogram[i];
1245 upper_threshold=(double) ScaleShortToQuantum((unsigned short) i);
1246 for (i=0; histogram[i] == 0; i++) ;
1247 lower_threshold=upper_percent*(upper_threshold+
1248 ScaleShortToQuantum((unsigned short) i));
1249 histogram=(size_t *) RelinquishMagickMemory(histogram);
1251 Hysteresis threshold.
1253 edge_view=AcquireAuthenticCacheView(edge_image,exception);
1254 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1255 #pragma omp parallel for schedule(static,4) shared(status) \
1256 magick_threads(edge_image,edge_image,edge_image->rows,1)
1258 for (y=0; y < (ssize_t) edge_image->rows; y++)
1266 if (status == MagickFalse)
1268 q=GetCacheViewAuthenticPixels(edge_view,0,y,edge_image->columns,1,
1270 if (q == (Quantum *) NULL)
1275 for (x=0; x < (ssize_t) edge_image->columns; x++)
1281 Edge if pixel gradient higher than upper threshold.
1283 (void) GetMatrixElement(pixel_cache,x,y,&pixel);
1284 if (pixel.intensity >= upper_threshold)
1285 (void) TraceEdge(edge_image,edge_view,pixel_cache,x,y,lower_threshold,
1288 if (SyncCacheViewAuthenticPixels(edge_view,exception) == MagickFalse)
1291 edge_view=DestroyCacheView(edge_view);
1292 pixel_cache=DestroyMatrixInfo(pixel_cache);
1297 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1301 % C o n v o l v e I m a g e %
1305 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1307 % ConvolveImage() applies a custom convolution kernel to the image.
1309 % The format of the ConvolveImage method is:
1311 % Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
1312 % ExceptionInfo *exception)
1314 % A description of each parameter follows:
1316 % o image: the image.
1318 % o kernel: the filtering kernel.
1320 % o exception: return any errors or warnings in this structure.
1323 MagickExport Image *ConvolveImage(const Image *image,
1324 const KernelInfo *kernel_info,ExceptionInfo *exception)
1329 convolve_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1330 UndefinedCompositeOp,0.0,exception);
1331 return(convolve_image);
1335 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1339 % D e s p e c k l e I m a g e %
1343 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1345 % DespeckleImage() reduces the speckle noise in an image while perserving the
1346 % edges of the original image. A speckle removing filter uses a complementary % hulling technique (raising pixels that are darker than their surrounding
1347 % neighbors, then complementarily lowering pixels that are brighter than their
1348 % surrounding neighbors) to reduce the speckle index of that image (reference
1349 % Crimmins speckle removal).
1351 % The format of the DespeckleImage method is:
1353 % Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1355 % A description of each parameter follows:
1357 % o image: the image.
1359 % o exception: return any errors or warnings in this structure.
1363 static void Hull(const Image *image,const ssize_t x_offset,
1364 const ssize_t y_offset,const size_t columns,const size_t rows,
1365 const int polarity,Quantum *restrict f,Quantum *restrict g)
1376 assert(image != (const Image *) NULL);
1377 assert(image->signature == MagickSignature);
1378 if (image->debug != MagickFalse)
1379 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1380 assert(f != (Quantum *) NULL);
1381 assert(g != (Quantum *) NULL);
1384 r=p+(y_offset*(columns+2)+x_offset);
1385 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1386 #pragma omp parallel for schedule(static,4) \
1387 magick_threads(image,image,1,1)
1389 for (y=0; y < (ssize_t) rows; y++)
1398 i=(2*y+1)+y*columns;
1400 for (x=0; x < (ssize_t) columns; x++)
1402 v=(MagickRealType) p[i];
1403 if ((MagickRealType) r[i] >= (v+ScaleCharToQuantum(2)))
1404 v+=ScaleCharToQuantum(1);
1409 for (x=0; x < (ssize_t) columns; x++)
1411 v=(MagickRealType) p[i];
1412 if ((MagickRealType) r[i] <= (v-ScaleCharToQuantum(2)))
1413 v-=ScaleCharToQuantum(1);
1420 r=q+(y_offset*(columns+2)+x_offset);
1421 s=q-(y_offset*(columns+2)+x_offset);
1422 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1423 #pragma omp parallel for schedule(static,4) \
1424 magick_threads(image,image,1,1)
1426 for (y=0; y < (ssize_t) rows; y++)
1435 i=(2*y+1)+y*columns;
1437 for (x=0; x < (ssize_t) columns; x++)
1439 v=(MagickRealType) q[i];
1440 if (((MagickRealType) s[i] >= (v+ScaleCharToQuantum(2))) &&
1441 ((MagickRealType) r[i] > v))
1442 v+=ScaleCharToQuantum(1);
1447 for (x=0; x < (ssize_t) columns; x++)
1449 v=(MagickRealType) q[i];
1450 if (((MagickRealType) s[i] <= (v-ScaleCharToQuantum(2))) &&
1451 ((MagickRealType) r[i] < v))
1452 v-=ScaleCharToQuantum(1);
1459 MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1461 #define DespeckleImageTag "Despeckle/Image"
1487 static const ssize_t
1488 X[4] = {0, 1, 1,-1},
1489 Y[4] = {1, 0, 1, 1};
1492 Allocate despeckled image.
1494 assert(image != (const Image *) NULL);
1495 assert(image->signature == MagickSignature);
1496 if (image->debug != MagickFalse)
1497 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1498 assert(exception != (ExceptionInfo *) NULL);
1499 assert(exception->signature == MagickSignature);
1500 despeckle_image=CloneImage(image,0,0,MagickTrue,exception);
1501 if (despeckle_image == (Image *) NULL)
1502 return((Image *) NULL);
1503 status=SetImageStorageClass(despeckle_image,DirectClass,exception);
1504 if (status == MagickFalse)
1506 despeckle_image=DestroyImage(despeckle_image);
1507 return((Image *) NULL);
1510 Allocate image buffer.
1512 length=(size_t) ((image->columns+2)*(image->rows+2));
1513 pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
1514 buffer_info=AcquireVirtualMemory(length,sizeof(*buffer));
1515 if ((pixel_info == (MemoryInfo *) NULL) ||
1516 (buffer_info == (MemoryInfo *) NULL))
1518 if (buffer_info != (MemoryInfo *) NULL)
1519 buffer_info=RelinquishVirtualMemory(buffer_info);
1520 if (pixel_info != (MemoryInfo *) NULL)
1521 pixel_info=RelinquishVirtualMemory(pixel_info);
1522 despeckle_image=DestroyImage(despeckle_image);
1523 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1525 pixels=(Quantum *) GetVirtualMemoryBlob(pixel_info);
1526 buffer=(Quantum *) GetVirtualMemoryBlob(buffer_info);
1528 Reduce speckle in the image.
1531 image_view=AcquireVirtualCacheView(image,exception);
1532 despeckle_view=AcquireAuthenticCacheView(despeckle_image,exception);
1533 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1550 if (status == MagickFalse)
1552 channel=GetPixelChannelChannel(image,i);
1553 traits=GetPixelChannelTraits(image,channel);
1554 despeckle_traits=GetPixelChannelTraits(despeckle_image,channel);
1555 if ((traits == UndefinedPixelTrait) ||
1556 (despeckle_traits == UndefinedPixelTrait))
1558 if ((despeckle_traits & CopyPixelTrait) != 0)
1560 (void) ResetMagickMemory(pixels,0,length*sizeof(*pixels));
1561 j=(ssize_t) image->columns+2;
1562 for (y=0; y < (ssize_t) image->rows; y++)
1564 register const Quantum
1567 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1568 if (p == (const Quantum *) NULL)
1574 for (x=0; x < (ssize_t) image->columns; x++)
1577 p+=GetPixelChannels(image);
1581 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
1582 for (k=0; k < 4; k++)
1584 Hull(image,X[k],Y[k],image->columns,image->rows,1,pixels,buffer);
1585 Hull(image,-X[k],-Y[k],image->columns,image->rows,1,pixels,buffer);
1586 Hull(image,-X[k],-Y[k],image->columns,image->rows,-1,pixels,buffer);
1587 Hull(image,X[k],Y[k],image->columns,image->rows,-1,pixels,buffer);
1589 j=(ssize_t) image->columns+2;
1590 for (y=0; y < (ssize_t) image->rows; y++)
1598 q=GetCacheViewAuthenticPixels(despeckle_view,0,y,despeckle_image->columns,
1600 if (q == (Quantum *) NULL)
1606 for (x=0; x < (ssize_t) image->columns; x++)
1608 SetPixelChannel(despeckle_image,channel,pixels[j++],q);
1609 q+=GetPixelChannels(despeckle_image);
1611 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1612 if (sync == MagickFalse)
1616 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1621 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1622 GetPixelChannels(image));
1623 if (proceed == MagickFalse)
1627 despeckle_view=DestroyCacheView(despeckle_view);
1628 image_view=DestroyCacheView(image_view);
1629 buffer_info=RelinquishVirtualMemory(buffer_info);
1630 pixel_info=RelinquishVirtualMemory(pixel_info);
1631 despeckle_image->type=image->type;
1632 if (status == MagickFalse)
1633 despeckle_image=DestroyImage(despeckle_image);
1634 return(despeckle_image);
1638 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1642 % E d g e I m a g e %
1646 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1648 % EdgeImage() finds edges in an image. Radius defines the radius of the
1649 % convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1652 % The format of the EdgeImage method is:
1654 % Image *EdgeImage(const Image *image,const double radius,
1655 % ExceptionInfo *exception)
1657 % A description of each parameter follows:
1659 % o image: the image.
1661 % o radius: the radius of the pixel neighborhood.
1663 % o exception: return any errors or warnings in this structure.
1666 MagickExport Image *EdgeImage(const Image *image,const double radius,
1667 ExceptionInfo *exception)
1681 assert(image != (const Image *) NULL);
1682 assert(image->signature == MagickSignature);
1683 if (image->debug != MagickFalse)
1684 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1685 assert(exception != (ExceptionInfo *) NULL);
1686 assert(exception->signature == MagickSignature);
1687 width=GetOptimalKernelWidth1D(radius,0.5);
1688 kernel_info=AcquireKernelInfo((const char *) NULL);
1689 if (kernel_info == (KernelInfo *) NULL)
1690 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1691 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1692 kernel_info->width=width;
1693 kernel_info->height=width;
1694 kernel_info->x=(ssize_t) (kernel_info->width-1)/2;
1695 kernel_info->y=(ssize_t) (kernel_info->height-1)/2;
1696 kernel_info->signature=MagickSignature;
1697 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
1698 AcquireAlignedMemory(kernel_info->width,kernel_info->height*
1699 sizeof(*kernel_info->values)));
1700 if (kernel_info->values == (MagickRealType *) NULL)
1702 kernel_info=DestroyKernelInfo(kernel_info);
1703 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1705 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
1706 kernel_info->values[i]=(-1.0);
1707 kernel_info->values[i/2]=(double) kernel_info->width*kernel_info->height-1.0;
1708 edge_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1709 UndefinedCompositeOp,0.0,exception);
1710 kernel_info=DestroyKernelInfo(kernel_info);
1715 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1719 % E m b o s s I m a g e %
1723 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1725 % EmbossImage() returns a grayscale image with a three-dimensional effect.
1726 % We convolve the image with a Gaussian operator of the given radius and
1727 % standard deviation (sigma). For reasonable results, radius should be
1728 % larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1731 % The format of the EmbossImage method is:
1733 % Image *EmbossImage(const Image *image,const double radius,
1734 % const double sigma,ExceptionInfo *exception)
1736 % A description of each parameter follows:
1738 % o image: the image.
1740 % o radius: the radius of the pixel neighborhood.
1742 % o sigma: the standard deviation of the Gaussian, in pixels.
1744 % o exception: return any errors or warnings in this structure.
1747 MagickExport Image *EmbossImage(const Image *image,const double radius,
1748 const double sigma,ExceptionInfo *exception)
1772 assert(image != (const Image *) NULL);
1773 assert(image->signature == MagickSignature);
1774 if (image->debug != MagickFalse)
1775 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1776 assert(exception != (ExceptionInfo *) NULL);
1777 assert(exception->signature == MagickSignature);
1778 width=GetOptimalKernelWidth1D(radius,sigma);
1779 kernel_info=AcquireKernelInfo((const char *) NULL);
1780 if (kernel_info == (KernelInfo *) NULL)
1781 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1782 kernel_info->width=width;
1783 kernel_info->height=width;
1784 kernel_info->x=(ssize_t) (width-1)/2;
1785 kernel_info->y=(ssize_t) (width-1)/2;
1786 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
1787 AcquireAlignedMemory(kernel_info->width,kernel_info->width*
1788 sizeof(*kernel_info->values)));
1789 if (kernel_info->values == (MagickRealType *) NULL)
1791 kernel_info=DestroyKernelInfo(kernel_info);
1792 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1794 j=(ssize_t) (kernel_info->width-1)/2;
1797 for (v=(-j); v <= j; v++)
1799 for (u=(-j); u <= j; u++)
1801 kernel_info->values[i]=(MagickRealType) (((u < 0) || (v < 0) ? -8.0 :
1802 8.0)*exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
1803 (2.0*MagickPI*MagickSigma*MagickSigma));
1805 kernel_info->values[i]=0.0;
1811 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
1812 normalize+=kernel_info->values[i];
1813 gamma=PerceptibleReciprocal(normalize);
1814 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
1815 kernel_info->values[i]*=gamma;
1816 emboss_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1817 UndefinedCompositeOp,0.0,exception);
1818 kernel_info=DestroyKernelInfo(kernel_info);
1819 if (emboss_image != (Image *) NULL)
1820 (void) EqualizeImage(emboss_image,exception);
1821 return(emboss_image);
1825 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1829 % G a u s s i a n B l u r I m a g e %
1833 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1835 % GaussianBlurImage() blurs an image. We convolve the image with a
1836 % Gaussian operator of the given radius and standard deviation (sigma).
1837 % For reasonable results, the radius should be larger than sigma. Use a
1838 % radius of 0 and GaussianBlurImage() selects a suitable radius for you
1840 % The format of the GaussianBlurImage method is:
1842 % Image *GaussianBlurImage(const Image *image,onst double radius,
1843 % const double sigma,ExceptionInfo *exception)
1845 % A description of each parameter follows:
1847 % o image: the image.
1849 % o radius: the radius of the Gaussian, in pixels, not counting the center
1852 % o sigma: the standard deviation of the Gaussian, in pixels.
1854 % o exception: return any errors or warnings in this structure.
1857 MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
1858 const double sigma,ExceptionInfo *exception)
1861 geometry[MaxTextExtent];
1869 assert(image != (const Image *) NULL);
1870 assert(image->signature == MagickSignature);
1871 if (image->debug != MagickFalse)
1872 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1873 assert(exception != (ExceptionInfo *) NULL);
1874 assert(exception->signature == MagickSignature);
1875 (void) FormatLocaleString(geometry,MaxTextExtent,"gaussian:%.20gx%.20g",
1877 kernel_info=AcquireKernelInfo(geometry);
1878 if (kernel_info == (KernelInfo *) NULL)
1879 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1880 blur_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1881 UndefinedCompositeOp,0.0,exception);
1882 kernel_info=DestroyKernelInfo(kernel_info);
1887 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1891 % M o t i o n B l u r I m a g e %
1895 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1897 % MotionBlurImage() simulates motion blur. We convolve the image with a
1898 % Gaussian operator of the given radius and standard deviation (sigma).
1899 % For reasonable results, radius should be larger than sigma. Use a
1900 % radius of 0 and MotionBlurImage() selects a suitable radius for you.
1901 % Angle gives the angle of the blurring motion.
1903 % Andrew Protano contributed this effect.
1905 % The format of the MotionBlurImage method is:
1907 % Image *MotionBlurImage(const Image *image,const double radius,
1908 % const double sigma,const double angle,ExceptionInfo *exception)
1910 % A description of each parameter follows:
1912 % o image: the image.
1914 % o radius: the radius of the Gaussian, in pixels, not counting
1917 % o sigma: the standard deviation of the Gaussian, in pixels.
1919 % o angle: Apply the effect along this angle.
1921 % o exception: return any errors or warnings in this structure.
1925 static MagickRealType *GetMotionBlurKernel(const size_t width,
1936 Generate a 1-D convolution kernel.
1938 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1939 kernel=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory((size_t)
1940 width,sizeof(*kernel)));
1941 if (kernel == (MagickRealType *) NULL)
1944 for (i=0; i < (ssize_t) width; i++)
1946 kernel[i]=(MagickRealType) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
1947 MagickSigma)))/(MagickSQ2PI*MagickSigma));
1948 normalize+=kernel[i];
1950 for (i=0; i < (ssize_t) width; i++)
1951 kernel[i]/=normalize;
1955 MagickExport Image *MotionBlurImage(const Image *image,const double radius,
1956 const double sigma,const double angle,ExceptionInfo *exception)
1958 #define BlurImageTag "Blur/Image"
1992 assert(image != (Image *) NULL);
1993 assert(image->signature == MagickSignature);
1994 if (image->debug != MagickFalse)
1995 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1996 assert(exception != (ExceptionInfo *) NULL);
1997 width=GetOptimalKernelWidth1D(radius,sigma);
1998 kernel=GetMotionBlurKernel(width,sigma);
1999 if (kernel == (MagickRealType *) NULL)
2000 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2001 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
2002 if (offset == (OffsetInfo *) NULL)
2004 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2005 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2007 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2008 if (blur_image == (Image *) NULL)
2010 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2011 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2012 return((Image *) NULL);
2014 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2016 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2017 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2018 blur_image=DestroyImage(blur_image);
2019 return((Image *) NULL);
2021 point.x=(double) width*sin(DegreesToRadians(angle));
2022 point.y=(double) width*cos(DegreesToRadians(angle));
2023 for (i=0; i < (ssize_t) width; i++)
2025 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
2026 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
2033 image_view=AcquireVirtualCacheView(image,exception);
2034 motion_view=AcquireVirtualCacheView(image,exception);
2035 blur_view=AcquireAuthenticCacheView(blur_image,exception);
2036 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2037 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2038 magick_threads(image,blur_image,image->rows,1)
2040 for (y=0; y < (ssize_t) image->rows; y++)
2042 register const Quantum
2051 if (status == MagickFalse)
2053 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2054 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2056 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2061 for (x=0; x < (ssize_t) image->columns; x++)
2066 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2080 register const Quantum
2083 register MagickRealType
2089 channel=GetPixelChannelChannel(image,i);
2090 traits=GetPixelChannelTraits(image,channel);
2091 blur_traits=GetPixelChannelTraits(blur_image,channel);
2092 if ((traits == UndefinedPixelTrait) ||
2093 (blur_traits == UndefinedPixelTrait))
2095 if (((blur_traits & CopyPixelTrait) != 0) ||
2096 (GetPixelReadMask(image,p) == 0))
2098 SetPixelChannel(blur_image,channel,p[i],q);
2103 if ((blur_traits & BlendPixelTrait) == 0)
2105 for (j=0; j < (ssize_t) width; j++)
2107 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+
2108 offset[j].y,1,1,exception);
2109 if (r == (const Quantum *) NULL)
2117 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
2122 for (j=0; j < (ssize_t) width; j++)
2124 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+offset[j].y,1,
2126 if (r == (const Quantum *) NULL)
2131 alpha=(double) (QuantumScale*GetPixelAlpha(image,r));
2132 pixel+=(*k)*alpha*r[i];
2136 gamma=PerceptibleReciprocal(gamma);
2137 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2139 p+=GetPixelChannels(image);
2140 q+=GetPixelChannels(blur_image);
2142 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2144 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2149 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2150 #pragma omp critical (MagickCore_MotionBlurImage)
2152 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2153 if (proceed == MagickFalse)
2157 blur_view=DestroyCacheView(blur_view);
2158 motion_view=DestroyCacheView(motion_view);
2159 image_view=DestroyCacheView(image_view);
2160 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2161 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2162 if (status == MagickFalse)
2163 blur_image=DestroyImage(blur_image);
2168 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2172 % P r e v i e w I m a g e %
2176 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2178 % PreviewImage() tiles 9 thumbnails of the specified image with an image
2179 % processing operation applied with varying parameters. This may be helpful
2180 % pin-pointing an appropriate parameter for a particular image processing
2183 % The format of the PreviewImages method is:
2185 % Image *PreviewImages(const Image *image,const PreviewType preview,
2186 % ExceptionInfo *exception)
2188 % A description of each parameter follows:
2190 % o image: the image.
2192 % o preview: the image processing operation.
2194 % o exception: return any errors or warnings in this structure.
2197 MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2198 ExceptionInfo *exception)
2200 #define NumberTiles 9
2201 #define PreviewImageTag "Preview/Image"
2202 #define DefaultPreviewGeometry "204x204+10+10"
2205 factor[MaxTextExtent],
2206 label[MaxTextExtent];
2251 Open output image file.
2253 assert(image != (Image *) NULL);
2254 assert(image->signature == MagickSignature);
2255 if (image->debug != MagickFalse)
2256 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2260 preview_info=AcquireImageInfo();
2261 SetGeometry(image,&geometry);
2262 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2263 &geometry.width,&geometry.height);
2264 images=NewImageList();
2266 GetQuantizeInfo(&quantize_info);
2272 for (i=0; i < NumberTiles; i++)
2274 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2275 if (thumbnail == (Image *) NULL)
2277 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2279 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel,exception);
2280 if (i == (NumberTiles/2))
2282 (void) QueryColorCompliance("#dfdfdf",AllCompliance,
2283 &thumbnail->matte_color,exception);
2284 AppendImageToList(&images,thumbnail);
2292 preview_image=RotateImage(thumbnail,degrees,exception);
2293 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
2299 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
2300 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",degrees,
2306 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2307 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
2308 preview_image=RollImage(thumbnail,x,y,exception);
2309 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
2310 (double) x,(double) y);
2315 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2316 if (preview_image == (Image *) NULL)
2318 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",2.0*
2320 (void) ModulateImage(preview_image,factor,exception);
2321 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2324 case SaturationPreview:
2326 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2327 if (preview_image == (Image *) NULL)
2329 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",2.0*percentage);
2330 (void) ModulateImage(preview_image,factor,exception);
2331 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2334 case BrightnessPreview:
2336 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2337 if (preview_image == (Image *) NULL)
2339 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
2340 (void) ModulateImage(preview_image,factor,exception);
2341 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2347 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2348 if (preview_image == (Image *) NULL)
2351 (void) GammaImage(preview_image,gamma,exception);
2352 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
2357 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2358 if (preview_image != (Image *) NULL)
2359 for (x=0; x < i; x++)
2360 (void) ContrastImage(preview_image,MagickTrue,exception);
2361 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
2367 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2368 if (preview_image == (Image *) NULL)
2370 for (x=0; x < i; x++)
2371 (void) ContrastImage(preview_image,MagickFalse,exception);
2372 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
2376 case GrayscalePreview:
2378 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2379 if (preview_image == (Image *) NULL)
2382 quantize_info.number_colors=colors;
2383 quantize_info.colorspace=GRAYColorspace;
2384 (void) QuantizeImage(&quantize_info,preview_image,exception);
2385 (void) FormatLocaleString(label,MaxTextExtent,
2386 "-colorspace gray -colors %.20g",(double) colors);
2389 case QuantizePreview:
2391 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2392 if (preview_image == (Image *) NULL)
2395 quantize_info.number_colors=colors;
2396 (void) QuantizeImage(&quantize_info,preview_image,exception);
2397 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
2401 case DespecklePreview:
2403 for (x=0; x < (i-1); x++)
2405 preview_image=DespeckleImage(thumbnail,exception);
2406 if (preview_image == (Image *) NULL)
2408 thumbnail=DestroyImage(thumbnail);
2409 thumbnail=preview_image;
2411 preview_image=DespeckleImage(thumbnail,exception);
2412 if (preview_image == (Image *) NULL)
2414 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
2418 case ReduceNoisePreview:
2420 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2421 (size_t) radius,exception);
2422 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
2425 case AddNoisePreview:
2431 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2436 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2441 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2446 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2451 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2456 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2461 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2465 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2466 (size_t) i,exception);
2467 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
2470 case SharpenPreview:
2472 preview_image=SharpenImage(thumbnail,radius,sigma,exception);
2473 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",radius,
2479 preview_image=BlurImage(thumbnail,radius,sigma,exception);
2480 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
2484 case ThresholdPreview:
2486 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2487 if (preview_image == (Image *) NULL)
2489 (void) BilevelImage(thumbnail,(double) (percentage*((double)
2490 QuantumRange+1.0))/100.0,exception);
2491 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",(double)
2492 (percentage*((double) QuantumRange+1.0))/100.0);
2495 case EdgeDetectPreview:
2497 preview_image=EdgeImage(thumbnail,radius,exception);
2498 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
2503 preview_image=SpreadImage(thumbnail,radius,thumbnail->interpolate,
2505 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",radius+0.5);
2508 case SolarizePreview:
2510 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2511 if (preview_image == (Image *) NULL)
2513 (void) SolarizeImage(preview_image,(double) QuantumRange*percentage/
2515 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
2516 (QuantumRange*percentage)/100.0);
2522 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2524 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",degrees,
2530 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2531 if (preview_image == (Image *) NULL)
2533 geometry.width=(size_t) (2*i+2);
2534 geometry.height=(size_t) (2*i+2);
2537 (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
2538 (void) FormatLocaleString(label,MaxTextExtent,
2539 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
2540 geometry.height,(double) geometry.x,(double) geometry.y);
2543 case SegmentPreview:
2545 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2546 if (preview_image == (Image *) NULL)
2549 (void) SegmentImage(preview_image,sRGBColorspace,MagickFalse,threshold,
2550 threshold,exception);
2551 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
2552 threshold,threshold);
2557 preview_image=SwirlImage(thumbnail,degrees,image->interpolate,
2559 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
2563 case ImplodePreview:
2566 preview_image=ImplodeImage(thumbnail,degrees,image->interpolate,
2568 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
2574 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,
2575 image->interpolate,exception);
2576 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",0.5*degrees,
2580 case OilPaintPreview:
2582 preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2584 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",radius,
2588 case CharcoalDrawingPreview:
2590 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
2592 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",radius,
2599 filename[MaxTextExtent];
2607 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2608 if (preview_image == (Image *) NULL)
2610 preview_info->quality=(size_t) percentage;
2611 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
2612 preview_info->quality);
2613 file=AcquireUniqueFileResource(filename);
2616 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
2617 "jpeg:%s",filename);
2618 status=WriteImage(preview_info,preview_image,exception);
2619 if (status != MagickFalse)
2624 (void) CopyMagickString(preview_info->filename,
2625 preview_image->filename,MaxTextExtent);
2626 quality_image=ReadImage(preview_info,exception);
2627 if (quality_image != (Image *) NULL)
2629 preview_image=DestroyImage(preview_image);
2630 preview_image=quality_image;
2633 (void) RelinquishUniqueFileResource(preview_image->filename);
2634 if ((GetBlobSize(preview_image)/1024) >= 1024)
2635 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
2636 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2639 if (GetBlobSize(preview_image) >= 1024)
2640 (void) FormatLocaleString(label,MaxTextExtent,
2641 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
2642 GetBlobSize(preview_image))/1024.0);
2644 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
2645 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
2649 thumbnail=DestroyImage(thumbnail);
2653 if (preview_image == (Image *) NULL)
2655 (void) DeleteImageProperty(preview_image,"label");
2656 (void) SetImageProperty(preview_image,"label",label,exception);
2657 AppendImageToList(&images,preview_image);
2658 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2660 if (proceed == MagickFalse)
2663 if (images == (Image *) NULL)
2665 preview_info=DestroyImageInfo(preview_info);
2666 return((Image *) NULL);
2671 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2672 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2673 montage_info->shadow=MagickTrue;
2674 (void) CloneString(&montage_info->tile,"3x3");
2675 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2676 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2677 montage_image=MontageImages(images,montage_info,exception);
2678 montage_info=DestroyMontageInfo(montage_info);
2679 images=DestroyImageList(images);
2680 if (montage_image == (Image *) NULL)
2681 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2682 if (montage_image->montage != (char *) NULL)
2685 Free image directory.
2687 montage_image->montage=(char *) RelinquishMagickMemory(
2688 montage_image->montage);
2689 if (image->directory != (char *) NULL)
2690 montage_image->directory=(char *) RelinquishMagickMemory(
2691 montage_image->directory);
2693 preview_info=DestroyImageInfo(preview_info);
2694 return(montage_image);
2698 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2702 % R o t a t i o n a l B l u r I m a g e %
2706 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2708 % RotationalBlurImage() applies a radial blur to the image.
2710 % Andrew Protano contributed this effect.
2712 % The format of the RotationalBlurImage method is:
2714 % Image *RotationalBlurImage(const Image *image,const double angle,
2715 % ExceptionInfo *exception)
2717 % A description of each parameter follows:
2719 % o image: the image.
2721 % o angle: the angle of the radial blur.
2725 % o exception: return any errors or warnings in this structure.
2728 MagickExport Image *RotationalBlurImage(const Image *image,const double angle,
2729 ExceptionInfo *exception)
2765 Allocate blur image.
2767 assert(image != (Image *) NULL);
2768 assert(image->signature == MagickSignature);
2769 if (image->debug != MagickFalse)
2770 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2771 assert(exception != (ExceptionInfo *) NULL);
2772 assert(exception->signature == MagickSignature);
2773 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2774 if (blur_image == (Image *) NULL)
2775 return((Image *) NULL);
2776 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2778 blur_image=DestroyImage(blur_image);
2779 return((Image *) NULL);
2781 blur_center.x=(double) (image->columns-1)/2.0;
2782 blur_center.y=(double) (image->rows-1)/2.0;
2783 blur_radius=hypot(blur_center.x,blur_center.y);
2784 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
2785 theta=DegreesToRadians(angle)/(double) (n-1);
2786 cos_theta=(double *) AcquireQuantumMemory((size_t) n,
2787 sizeof(*cos_theta));
2788 sin_theta=(double *) AcquireQuantumMemory((size_t) n,
2789 sizeof(*sin_theta));
2790 if ((cos_theta == (double *) NULL) ||
2791 (sin_theta == (double *) NULL))
2793 blur_image=DestroyImage(blur_image);
2794 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2796 offset=theta*(double) (n-1)/2.0;
2797 for (i=0; i < (ssize_t) n; i++)
2799 cos_theta[i]=cos((double) (theta*i-offset));
2800 sin_theta[i]=sin((double) (theta*i-offset));
2807 image_view=AcquireVirtualCacheView(image,exception);
2808 radial_view=AcquireVirtualCacheView(image,exception);
2809 blur_view=AcquireAuthenticCacheView(blur_image,exception);
2810 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2811 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2812 magick_threads(image,blur_image,image->rows,1)
2814 for (y=0; y < (ssize_t) image->rows; y++)
2816 register const Quantum
2825 if (status == MagickFalse)
2827 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2828 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2830 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2835 for (x=0; x < (ssize_t) image->columns; x++)
2849 center.x=(double) x-blur_center.x;
2850 center.y=(double) y-blur_center.y;
2851 radius=hypot((double) center.x,center.y);
2856 step=(size_t) (blur_radius/radius);
2863 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2876 register const Quantum
2882 channel=GetPixelChannelChannel(image,i);
2883 traits=GetPixelChannelTraits(image,channel);
2884 blur_traits=GetPixelChannelTraits(blur_image,channel);
2885 if ((traits == UndefinedPixelTrait) ||
2886 (blur_traits == UndefinedPixelTrait))
2888 if (((blur_traits & CopyPixelTrait) != 0) ||
2889 (GetPixelReadMask(image,p) == 0))
2891 SetPixelChannel(blur_image,channel,p[i],q);
2896 if ((blur_traits & BlendPixelTrait) == 0)
2898 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2900 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2901 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2902 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2904 if (r == (const Quantum *) NULL)
2912 gamma=PerceptibleReciprocal(gamma);
2913 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2916 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2918 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2919 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2920 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2922 if (r == (const Quantum *) NULL)
2927 pixel+=GetPixelAlpha(image,r)*r[i];
2928 gamma+=GetPixelAlpha(image,r);
2930 gamma=PerceptibleReciprocal(gamma);
2931 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2933 p+=GetPixelChannels(image);
2934 q+=GetPixelChannels(blur_image);
2936 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2938 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2943 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2944 #pragma omp critical (MagickCore_RotationalBlurImage)
2946 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2947 if (proceed == MagickFalse)
2951 blur_view=DestroyCacheView(blur_view);
2952 radial_view=DestroyCacheView(radial_view);
2953 image_view=DestroyCacheView(image_view);
2954 cos_theta=(double *) RelinquishMagickMemory(cos_theta);
2955 sin_theta=(double *) RelinquishMagickMemory(sin_theta);
2956 if (status == MagickFalse)
2957 blur_image=DestroyImage(blur_image);
2962 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2966 % S e l e c t i v e B l u r I m a g e %
2970 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2972 % SelectiveBlurImage() selectively blur pixels within a contrast threshold.
2973 % It is similar to the unsharpen mask that sharpens everything with contrast
2974 % above a certain threshold.
2976 % The format of the SelectiveBlurImage method is:
2978 % Image *SelectiveBlurImage(const Image *image,const double radius,
2979 % const double sigma,const double threshold,ExceptionInfo *exception)
2981 % A description of each parameter follows:
2983 % o image: the image.
2985 % o radius: the radius of the Gaussian, in pixels, not counting the center
2988 % o sigma: the standard deviation of the Gaussian, in pixels.
2990 % o threshold: only pixels within this contrast threshold are included
2991 % in the blur operation.
2993 % o exception: return any errors or warnings in this structure.
2996 MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
2997 const double sigma,const double threshold,ExceptionInfo *exception)
2999 #define SelectiveBlurImageTag "SelectiveBlur/Image"
3033 Initialize blur image attributes.
3035 assert(image != (Image *) NULL);
3036 assert(image->signature == MagickSignature);
3037 if (image->debug != MagickFalse)
3038 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3039 assert(exception != (ExceptionInfo *) NULL);
3040 assert(exception->signature == MagickSignature);
3041 width=GetOptimalKernelWidth1D(radius,sigma);
3042 kernel=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory((size_t)
3043 width,width*sizeof(*kernel)));
3044 if (kernel == (MagickRealType *) NULL)
3045 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3046 j=(ssize_t) (width-1)/2;
3048 for (v=(-j); v <= j; v++)
3050 for (u=(-j); u <= j; u++)
3051 kernel[i++]=(MagickRealType) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
3052 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3054 if (image->debug != MagickFalse)
3057 format[MaxTextExtent],
3060 register const MagickRealType
3067 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
3068 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
3070 message=AcquireString("");
3072 for (v=0; v < (ssize_t) width; v++)
3075 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
3076 (void) ConcatenateString(&message,format);
3077 for (u=0; u < (ssize_t) width; u++)
3079 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",(double) *k++);
3080 (void) ConcatenateString(&message,format);
3082 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
3084 message=DestroyString(message);
3086 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3087 if (blur_image == (Image *) NULL)
3088 return((Image *) NULL);
3089 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
3091 blur_image=DestroyImage(blur_image);
3092 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3093 return((Image *) NULL);
3095 luminance_image=CloneImage(image,0,0,MagickTrue,exception);
3096 if (luminance_image == (Image *) NULL)
3098 blur_image=DestroyImage(blur_image);
3099 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3100 return((Image *) NULL);
3102 status=TransformImageColorspace(luminance_image,GRAYColorspace,exception);
3103 if (status == MagickFalse)
3105 luminance_image=DestroyImage(luminance_image);
3106 blur_image=DestroyImage(blur_image);
3107 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3108 return((Image *) NULL);
3111 Threshold blur image.
3115 center=(ssize_t) (GetPixelChannels(image)*(image->columns+width)*
3116 ((width-1)/2L)+GetPixelChannels(image)*((width-1)/2L));
3117 image_view=AcquireVirtualCacheView(image,exception);
3118 luminance_view=AcquireVirtualCacheView(luminance_image,exception);
3119 blur_view=AcquireAuthenticCacheView(blur_image,exception);
3120 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3121 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3122 magick_threads(image,blur_image,image->rows,1)
3124 for (y=0; y < (ssize_t) image->rows; y++)
3132 register const Quantum
3142 if (status == MagickFalse)
3144 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) (width-1)/2L),y-(ssize_t)
3145 ((width-1)/2L),image->columns+width,width,exception);
3146 l=GetCacheViewVirtualPixels(luminance_view,-((ssize_t) (width-1)/2L),y-
3147 (ssize_t) ((width-1)/2L),luminance_image->columns+width,width,exception);
3148 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3150 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3155 for (x=0; x < (ssize_t) image->columns; x++)
3163 intensity=GetPixelIntensity(image,p+center);
3164 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3178 register const MagickRealType
3181 register const Quantum
3182 *restrict luminance_pixels,
3191 channel=GetPixelChannelChannel(image,i);
3192 traits=GetPixelChannelTraits(image,channel);
3193 blur_traits=GetPixelChannelTraits(blur_image,channel);
3194 if ((traits == UndefinedPixelTrait) ||
3195 (blur_traits == UndefinedPixelTrait))
3197 if (((blur_traits & CopyPixelTrait) != 0) ||
3198 (GetPixelReadMask(image,p+center) == 0))
3200 SetPixelChannel(blur_image,channel,p[center+i],q);
3208 if ((blur_traits & BlendPixelTrait) == 0)
3210 for (v=0; v < (ssize_t) width; v++)
3212 for (u=0; u < (ssize_t) width; u++)
3214 contrast=GetPixelIntensity(luminance_image,luminance_pixels)-
3216 if (fabs(contrast) < threshold)
3218 pixel+=(*k)*pixels[i];
3222 pixels+=GetPixelChannels(image);
3223 luminance_pixels+=GetPixelChannels(luminance_image);
3225 pixels+=(image->columns-1)*GetPixelChannels(image);
3226 luminance_pixels+=luminance_image->columns*
3227 GetPixelChannels(luminance_image);
3229 if (fabs((double) gamma) < MagickEpsilon)
3231 SetPixelChannel(blur_image,channel,p[center+i],q);
3234 gamma=PerceptibleReciprocal(gamma);
3235 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3238 for (v=0; v < (ssize_t) width; v++)
3240 for (u=0; u < (ssize_t) width; u++)
3242 contrast=GetPixelIntensity(image,pixels)-intensity;
3243 if (fabs(contrast) < threshold)
3245 alpha=(double) (QuantumScale*GetPixelAlpha(image,pixels));
3246 pixel+=(*k)*alpha*pixels[i];
3250 pixels+=GetPixelChannels(image);
3251 luminance_pixels+=GetPixelChannels(luminance_image);
3253 pixels+=(image->columns-1)*GetPixelChannels(image);
3254 luminance_pixels+=luminance_image->columns*
3255 GetPixelChannels(luminance_image);
3257 if (fabs((double) gamma) < MagickEpsilon)
3259 SetPixelChannel(blur_image,channel,p[center+i],q);
3262 gamma=PerceptibleReciprocal(gamma);
3263 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3265 p+=GetPixelChannels(image);
3266 l+=GetPixelChannels(luminance_image);
3267 q+=GetPixelChannels(blur_image);
3269 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3270 if (sync == MagickFalse)
3272 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3277 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3278 #pragma omp critical (MagickCore_SelectiveBlurImage)
3280 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3282 if (proceed == MagickFalse)
3286 blur_image->type=image->type;
3287 blur_view=DestroyCacheView(blur_view);
3288 image_view=DestroyCacheView(image_view);
3289 luminance_image=DestroyImage(luminance_image);
3290 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3291 if (status == MagickFalse)
3292 blur_image=DestroyImage(blur_image);
3297 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3301 % S h a d e I m a g e %
3305 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3307 % ShadeImage() shines a distant light on an image to create a
3308 % three-dimensional effect. You control the positioning of the light with
3309 % azimuth and elevation; azimuth is measured in degrees off the x axis
3310 % and elevation is measured in pixels above the Z axis.
3312 % The format of the ShadeImage method is:
3314 % Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3315 % const double azimuth,const double elevation,ExceptionInfo *exception)
3317 % A description of each parameter follows:
3319 % o image: the image.
3321 % o gray: A value other than zero shades the intensity of each pixel.
3323 % o azimuth, elevation: Define the light source direction.
3325 % o exception: return any errors or warnings in this structure.
3328 MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3329 const double azimuth,const double elevation,ExceptionInfo *exception)
3331 #define ShadeImageTag "Shade/Image"
3354 Initialize shaded image attributes.
3356 assert(image != (const Image *) NULL);
3357 assert(image->signature == MagickSignature);
3358 if (image->debug != MagickFalse)
3359 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3360 assert(exception != (ExceptionInfo *) NULL);
3361 assert(exception->signature == MagickSignature);
3362 linear_image=CloneImage(image,0,0,MagickTrue,exception);
3363 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3364 if ((linear_image == (Image *) NULL) || (shade_image == (Image *) NULL))
3366 if (linear_image != (Image *) NULL)
3367 linear_image=DestroyImage(linear_image);
3368 if (shade_image != (Image *) NULL)
3369 shade_image=DestroyImage(shade_image);
3370 return((Image *) NULL);
3372 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
3374 linear_image=DestroyImage(linear_image);
3375 shade_image=DestroyImage(shade_image);
3376 return((Image *) NULL);
3379 Compute the light vector.
3381 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3382 cos(DegreesToRadians(elevation));
3383 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3384 cos(DegreesToRadians(elevation));
3385 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3391 image_view=AcquireVirtualCacheView(linear_image,exception);
3392 shade_view=AcquireAuthenticCacheView(shade_image,exception);
3393 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3394 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3395 magick_threads(linear_image,shade_image,linear_image->rows,1)
3397 for (y=0; y < (ssize_t) linear_image->rows; y++)
3407 register const Quantum
3419 if (status == MagickFalse)
3421 p=GetCacheViewVirtualPixels(image_view,-1,y-1,linear_image->columns+2,3,
3423 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3425 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3431 Shade this row of pixels.
3433 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
3434 pre=p+GetPixelChannels(linear_image);
3435 center=pre+(linear_image->columns+2)*GetPixelChannels(linear_image);
3436 post=center+(linear_image->columns+2)*GetPixelChannels(linear_image);
3437 for (x=0; x < (ssize_t) linear_image->columns; x++)
3443 Determine the surface normal and compute shading.
3446 GetPixelIntensity(linear_image,pre-GetPixelChannels(linear_image))+
3447 GetPixelIntensity(linear_image,center-GetPixelChannels(linear_image))+
3448 GetPixelIntensity(linear_image,post-GetPixelChannels(linear_image))-
3449 GetPixelIntensity(linear_image,pre+GetPixelChannels(linear_image))-
3450 GetPixelIntensity(linear_image,center+GetPixelChannels(linear_image))-
3451 GetPixelIntensity(linear_image,post+GetPixelChannels(linear_image)));
3453 GetPixelIntensity(linear_image,post-GetPixelChannels(linear_image))+
3454 GetPixelIntensity(linear_image,post)+
3455 GetPixelIntensity(linear_image,post+GetPixelChannels(linear_image))-
3456 GetPixelIntensity(linear_image,pre-GetPixelChannels(linear_image))-
3457 GetPixelIntensity(linear_image,pre)-
3458 GetPixelIntensity(linear_image,pre+GetPixelChannels(linear_image)));
3459 if ((normal.x == 0.0) && (normal.y == 0.0))
3464 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3465 if (distance > MagickEpsilon)
3467 normal_distance=normal.x*normal.x+normal.y*normal.y+
3469 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3470 shade=distance/sqrt((double) normal_distance);
3473 for (i=0; i < (ssize_t) GetPixelChannels(linear_image); i++)
3482 channel=GetPixelChannelChannel(linear_image,i);
3483 traits=GetPixelChannelTraits(linear_image,channel);
3484 shade_traits=GetPixelChannelTraits(shade_image,channel);
3485 if ((traits == UndefinedPixelTrait) ||
3486 (shade_traits == UndefinedPixelTrait))
3488 if (((shade_traits & CopyPixelTrait) != 0) ||
3489 (GetPixelReadMask(linear_image,center) == 0))
3491 SetPixelChannel(shade_image,channel,center[i],q);
3494 if (gray != MagickFalse)
3496 SetPixelChannel(shade_image,channel,ClampToQuantum(shade),q);
3499 SetPixelChannel(shade_image,channel,ClampToQuantum(QuantumScale*shade*
3502 pre+=GetPixelChannels(linear_image);
3503 center+=GetPixelChannels(linear_image);
3504 post+=GetPixelChannels(linear_image);
3505 q+=GetPixelChannels(shade_image);
3507 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3509 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3514 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3515 #pragma omp critical (MagickCore_ShadeImage)
3517 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3518 if (proceed == MagickFalse)
3522 shade_view=DestroyCacheView(shade_view);
3523 image_view=DestroyCacheView(image_view);
3524 linear_image=DestroyImage(linear_image);
3525 if (status == MagickFalse)
3526 shade_image=DestroyImage(shade_image);
3527 return(shade_image);
3531 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3535 % S h a r p e n I m a g e %
3539 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3541 % SharpenImage() sharpens the image. We convolve the image with a Gaussian
3542 % operator of the given radius and standard deviation (sigma). For
3543 % reasonable results, radius should be larger than sigma. Use a radius of 0
3544 % and SharpenImage() selects a suitable radius for you.
3546 % Using a separable kernel would be faster, but the negative weights cancel
3547 % out on the corners of the kernel producing often undesirable ringing in the
3548 % filtered result; this can be avoided by using a 2D gaussian shaped image
3549 % sharpening kernel instead.
3551 % The format of the SharpenImage method is:
3553 % Image *SharpenImage(const Image *image,const double radius,
3554 % const double sigma,ExceptionInfo *exception)
3556 % A description of each parameter follows:
3558 % o image: the image.
3560 % o radius: the radius of the Gaussian, in pixels, not counting the center
3563 % o sigma: the standard deviation of the Laplacian, in pixels.
3565 % o exception: return any errors or warnings in this structure.
3568 MagickExport Image *SharpenImage(const Image *image,const double radius,
3569 const double sigma,ExceptionInfo *exception)
3592 assert(image != (const Image *) NULL);
3593 assert(image->signature == MagickSignature);
3594 if (image->debug != MagickFalse)
3595 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3596 assert(exception != (ExceptionInfo *) NULL);
3597 assert(exception->signature == MagickSignature);
3598 width=GetOptimalKernelWidth2D(radius,sigma);
3599 kernel_info=AcquireKernelInfo((const char *) NULL);
3600 if (kernel_info == (KernelInfo *) NULL)
3601 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3602 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3603 kernel_info->width=width;
3604 kernel_info->height=width;
3605 kernel_info->x=(ssize_t) (width-1)/2;
3606 kernel_info->y=(ssize_t) (width-1)/2;
3607 kernel_info->signature=MagickSignature;
3608 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
3609 AcquireAlignedMemory(kernel_info->width,kernel_info->height*
3610 sizeof(*kernel_info->values)));
3611 if (kernel_info->values == (MagickRealType *) NULL)
3613 kernel_info=DestroyKernelInfo(kernel_info);
3614 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3617 j=(ssize_t) (kernel_info->width-1)/2;
3619 for (v=(-j); v <= j; v++)
3621 for (u=(-j); u <= j; u++)
3623 kernel_info->values[i]=(MagickRealType) (-exp(-((double) u*u+v*v)/(2.0*
3624 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3625 normalize+=kernel_info->values[i];
3629 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
3631 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
3632 normalize+=kernel_info->values[i];
3633 gamma=PerceptibleReciprocal(normalize);
3634 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
3635 kernel_info->values[i]*=gamma;
3636 sharp_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
3637 UndefinedCompositeOp,0.0,exception);
3638 kernel_info=DestroyKernelInfo(kernel_info);
3639 return(sharp_image);
3643 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3647 % S p r e a d I m a g e %
3651 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3653 % SpreadImage() is a special effects method that randomly displaces each
3654 % pixel in a block defined by the radius parameter.
3656 % The format of the SpreadImage method is:
3658 % Image *SpreadImage(const Image *image,const double radius,
3659 % const PixelInterpolateMethod method,ExceptionInfo *exception)
3661 % A description of each parameter follows:
3663 % o image: the image.
3665 % o radius: choose a random pixel in a neighborhood of this extent.
3667 % o method: the pixel interpolation method.
3669 % o exception: return any errors or warnings in this structure.
3672 MagickExport Image *SpreadImage(const Image *image,const double radius,
3673 const PixelInterpolateMethod method,ExceptionInfo *exception)
3675 #define SpreadImageTag "Spread/Image"
3691 **restrict random_info;
3699 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3705 Initialize spread image attributes.
3707 assert(image != (Image *) NULL);
3708 assert(image->signature == MagickSignature);
3709 if (image->debug != MagickFalse)
3710 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3711 assert(exception != (ExceptionInfo *) NULL);
3712 assert(exception->signature == MagickSignature);
3713 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3715 if (spread_image == (Image *) NULL)
3716 return((Image *) NULL);
3717 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
3719 spread_image=DestroyImage(spread_image);
3720 return((Image *) NULL);
3727 width=GetOptimalKernelWidth1D(radius,0.5);
3728 random_info=AcquireRandomInfoThreadSet();
3729 image_view=AcquireVirtualCacheView(image,exception);
3730 spread_view=AcquireAuthenticCacheView(spread_image,exception);
3731 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3732 key=GetRandomSecretKey(random_info[0]);
3733 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3734 magick_threads(image,spread_image,image->rows,key == ~0UL)
3736 for (y=0; y < (ssize_t) image->rows; y++)
3739 id = GetOpenMPThreadId();
3747 if (status == MagickFalse)
3749 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
3751 if (q == (Quantum *) NULL)
3756 for (x=0; x < (ssize_t) image->columns; x++)
3761 point.x=GetPseudoRandomValue(random_info[id]);
3762 point.y=GetPseudoRandomValue(random_info[id]);
3763 status=InterpolatePixelChannels(image,image_view,spread_image,method,
3764 (double) x+width*point.x-0.5,(double) y+width*point.y-0.5,q,exception);
3765 q+=GetPixelChannels(spread_image);
3767 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
3769 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3774 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3775 #pragma omp critical (MagickCore_SpreadImage)
3777 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3778 if (proceed == MagickFalse)
3782 spread_view=DestroyCacheView(spread_view);
3783 image_view=DestroyCacheView(image_view);
3784 random_info=DestroyRandomInfoThreadSet(random_info);
3785 if (status == MagickFalse)
3786 spread_image=DestroyImage(spread_image);
3787 return(spread_image);
3791 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3795 % U n s h a r p M a s k I m a g e %
3799 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3801 % UnsharpMaskImage() sharpens one or more image channels. We convolve the
3802 % image with a Gaussian operator of the given radius and standard deviation
3803 % (sigma). For reasonable results, radius should be larger than sigma. Use a
3804 % radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
3806 % The format of the UnsharpMaskImage method is:
3808 % Image *UnsharpMaskImage(const Image *image,const double radius,
3809 % const double sigma,const double amount,const double threshold,
3810 % ExceptionInfo *exception)
3812 % A description of each parameter follows:
3814 % o image: the image.
3816 % o radius: the radius of the Gaussian, in pixels, not counting the center
3819 % o sigma: the standard deviation of the Gaussian, in pixels.
3821 % o gain: the percentage of the difference between the original and the
3822 % blur image that is added back into the original.
3824 % o threshold: the threshold in pixels needed to apply the diffence gain.
3826 % o exception: return any errors or warnings in this structure.
3829 MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
3830 const double sigma,const double gain,const double threshold,
3831 ExceptionInfo *exception)
3833 #define SharpenImageTag "Sharpen/Image"
3854 assert(image != (const Image *) NULL);
3855 assert(image->signature == MagickSignature);
3856 if (image->debug != MagickFalse)
3857 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3858 assert(exception != (ExceptionInfo *) NULL);
3859 unsharp_image=BlurImage(image,radius,sigma,exception);
3860 if (unsharp_image == (Image *) NULL)
3861 return((Image *) NULL);
3862 quantum_threshold=(double) QuantumRange*threshold;
3868 image_view=AcquireVirtualCacheView(image,exception);
3869 unsharp_view=AcquireAuthenticCacheView(unsharp_image,exception);
3870 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3871 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3872 magick_threads(image,unsharp_image,image->rows,1)
3874 for (y=0; y < (ssize_t) image->rows; y++)
3876 register const Quantum
3885 if (status == MagickFalse)
3887 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3888 q=QueueCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
3890 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3895 for (x=0; x < (ssize_t) image->columns; x++)
3900 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3912 channel=GetPixelChannelChannel(image,i);
3913 traits=GetPixelChannelTraits(image,channel);
3914 unsharp_traits=GetPixelChannelTraits(unsharp_image,channel);
3915 if ((traits == UndefinedPixelTrait) ||
3916 (unsharp_traits == UndefinedPixelTrait))
3918 if (((unsharp_traits & CopyPixelTrait) != 0) ||
3919 (GetPixelReadMask(image,p) != 0))
3921 SetPixelChannel(unsharp_image,channel,p[i],q);
3924 pixel=p[i]-(double) GetPixelChannel(unsharp_image,channel,q);
3925 if (fabs(2.0*pixel) < quantum_threshold)
3926 pixel=(double) p[i];
3928 pixel=(double) p[i]+gain*pixel;
3929 SetPixelChannel(unsharp_image,channel,ClampToQuantum(pixel),q);
3931 p+=GetPixelChannels(image);
3932 q+=GetPixelChannels(unsharp_image);
3934 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
3936 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3941 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3942 #pragma omp critical (MagickCore_UnsharpMaskImage)
3944 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
3945 if (proceed == MagickFalse)
3949 unsharp_image->type=image->type;
3950 unsharp_view=DestroyCacheView(unsharp_view);
3951 image_view=DestroyCacheView(image_view);
3952 if (status == MagickFalse)
3953 unsharp_image=DestroyImage(unsharp_image);
3954 return(unsharp_image);