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
900 static MagickBooleanType IsAuthenticPixel(const Image *image,const ssize_t x,
903 if ((x < 0) || (x >= (ssize_t) image->columns))
905 if ((y < 0) || (y >= (ssize_t) image->rows))
910 static MagickBooleanType TraceEdges(Image *edge_image,CacheView *trace_view,
911 MatrixInfo *pixel_cache,const ssize_t x,const ssize_t y,
912 const double lower_threshold,ExceptionInfo *exception)
920 if (GetMatrixElement(pixel_cache,0,0,&pixel) == MagickFalse)
924 if (SetMatrixElement(pixel_cache,0,0,&pixel) == MagickFalse)
938 status=GetMatrixElement(pixel_cache,(ssize_t) number_edges,0,&pixel);
939 if (status == MagickFalse)
943 for (v=(-1); v <= 1; v++)
948 for (u=(-1); u <= 1; u++)
953 if ((u == 0) && (v == 0))
955 if (IsAuthenticPixel(edge_image,x_offset+u,y_offset+v) == MagickFalse)
958 Not an edge if gradient value is below the lower threshold.
960 q=GetCacheViewAuthenticPixels(trace_view,x_offset+u,y_offset+v,1,1,
962 if (q == (Quantum *) NULL)
964 status=GetMatrixElement(pixel_cache,x_offset+u,y_offset+v,&pixel);
965 if (status == MagickFalse)
967 if ((pixel.intensity >= lower_threshold) &&
968 (GetPixelIntensity(edge_image,q) == 0))
971 status=SyncCacheViewAuthenticPixels(trace_view,exception);
972 if (status == MagickFalse)
974 status=GetMatrixElement(pixel_cache,(ssize_t) number_edges,0,
976 if (status == MagickFalse)
980 status=SetMatrixElement(pixel_cache,(ssize_t) number_edges,0,
982 if (status == MagickFalse)
988 } while (number_edges != 0);
993 MagickExport Image *CannyEdgeImage(const Image *image,const double radius,
994 const double sigma,const double lower_precent,const double upper_percent,
995 ExceptionInfo *exception)
1002 geometry[MaxTextExtent];
1031 assert(image != (const Image *) NULL);
1032 assert(image->signature == MagickSignature);
1033 if (image->debug != MagickFalse)
1034 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1035 assert(exception != (ExceptionInfo *) NULL);
1036 assert(exception->signature == MagickSignature);
1040 (void) FormatLocaleString(geometry,MaxTextExtent,
1041 "blur:%.20gx%.20g;blur:%.20gx%.20g+90",radius,sigma,radius,sigma);
1042 kernel_info=AcquireKernelInfo(geometry);
1043 if (kernel_info == (KernelInfo *) NULL)
1044 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1045 edge_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1046 UndefinedCompositeOp,0.0,exception);
1047 kernel_info=DestroyKernelInfo(kernel_info);
1048 if (edge_image == (Image *) NULL)
1049 return((Image *) NULL);
1050 if (SetImageColorspace(edge_image,GRAYColorspace,exception) == MagickFalse)
1052 edge_image=DestroyImage(edge_image);
1053 return((Image *) NULL);
1056 Find the intensity gradient of the image.
1058 pixel_cache=AcquireMatrixInfo(edge_image->columns,edge_image->rows,
1059 sizeof(CannyInfo),exception);
1060 if (pixel_cache == (MatrixInfo *) NULL)
1062 edge_image=DestroyImage(edge_image);
1063 return((Image *) NULL);
1066 edge_view=AcquireVirtualCacheView(edge_image,exception);
1067 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1068 #pragma omp parallel for schedule(static,4) shared(status) \
1069 magick_threads(edge_image,edge_image,edge_image->rows,1)
1071 for (y=0; y < (ssize_t) edge_image->rows; y++)
1073 register const Quantum
1079 if (status == MagickFalse)
1081 p=GetCacheViewVirtualPixels(edge_view,-1,y-1,edge_image->columns+2,3,
1083 if (p == (const Quantum *) NULL)
1088 for (x=0; x < (ssize_t) edge_image->columns; x++)
1097 register const Quantum
1098 *restrict kernel_pixels;
1106 { -1.0, 0.0, +1.0 },
1107 { -2.0, 0.0, +2.0 },
1112 { +1.0, +2.0, +1.0 },
1114 { -1.0, -2.0, -1.0 }
1117 (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
1121 for (v=0; v < 3; v++)
1126 for (u=0; u < 3; u++)
1131 intensity=GetPixelIntensity(edge_image,kernel_pixels+u);
1132 dx+=Gx[v][u]*intensity;
1133 dy+=Gy[v][u]*intensity;
1135 kernel_pixels+=edge_image->columns+2;
1137 pixel.magnitude=sqrt(dx*dx+dy*dy);
1138 pixel.orientation=2;
1147 if (theta < -2.41421356237)
1148 pixel.orientation=0;
1150 if (theta < -0.414213562373)
1151 pixel.orientation=1;
1153 pixel.orientation=2;
1157 if (theta > 2.41421356237)
1158 pixel.orientation=0;
1160 if (theta > 0.414213562373)
1161 pixel.orientation=3;
1163 pixel.orientation=2;
1166 if (SetMatrixElement(pixel_cache,x,y,&pixel) == MagickFalse)
1168 p+=GetPixelChannels(edge_image);
1171 edge_view=DestroyCacheView(edge_view);
1173 Non-maxima suppression, remove pixels that are not considered to be part
1176 histogram=(size_t *) AcquireQuantumMemory(65536,sizeof(*histogram));
1177 if (histogram == (size_t *) NULL)
1179 pixel_cache=DestroyMatrixInfo(pixel_cache);
1180 edge_image=DestroyImage(edge_image);
1181 return((Image *) NULL);
1183 (void) ResetMagickMemory(histogram,0,65536*sizeof(*histogram));
1184 edge_view=AcquireAuthenticCacheView(edge_image,exception);
1185 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1186 #pragma omp parallel for schedule(static,4) shared(status) \
1187 magick_threads(edge_image,edge_image,edge_image->rows,1)
1189 for (y=0; y < (ssize_t) edge_image->rows; y++)
1197 if (status == MagickFalse)
1199 q=GetCacheViewAuthenticPixels(edge_view,0,y,edge_image->columns,1,
1201 if (q == (Quantum *) NULL)
1206 for (x=0; x < (ssize_t) edge_image->columns; x++)
1213 (void) GetMatrixElement(pixel_cache,x,y,&pixel);
1214 switch (pixel.orientation)
1221 (void) GetMatrixElement(pixel_cache,x,y-1,&alpha_pixel);
1222 (void) GetMatrixElement(pixel_cache,x,y+1,&beta_pixel);
1230 (void) GetMatrixElement(pixel_cache,x-1,y-1,&alpha_pixel);
1231 (void) GetMatrixElement(pixel_cache,x+1,y+1,&beta_pixel);
1239 (void) GetMatrixElement(pixel_cache,x-1,y,&alpha_pixel);
1240 (void) GetMatrixElement(pixel_cache,x+1,y,&beta_pixel);
1247 (void) GetMatrixElement(pixel_cache,x+1,y-1,&alpha_pixel);
1248 (void) GetMatrixElement(pixel_cache,x-1,y+1,&beta_pixel);
1251 pixel.intensity=pixel.magnitude;
1252 if ((pixel.magnitude < alpha_pixel.magnitude) ||
1253 (pixel.magnitude < beta_pixel.magnitude))
1256 if (pixel.magnitude > QuantumRange)
1257 pixel.intensity=QuantumRange;
1258 (void) SetMatrixElement(pixel_cache,x,y,&pixel);
1259 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1260 #pragma omp critical (MagickCore_CannyEdgeImage)
1262 histogram[ScaleQuantumToShort(ClampToQuantum(pixel.intensity))]++;
1264 q+=GetPixelChannels(edge_image);
1266 if (SyncCacheViewAuthenticPixels(edge_view,exception) == MagickFalse)
1269 edge_view=DestroyCacheView(edge_view);
1271 Estimate hysteresis threshold.
1273 number_pixels=(size_t) (lower_precent*(image->columns*image->rows-
1276 for (i=65535; count < (ssize_t) number_pixels; i--)
1277 count+=histogram[i];
1278 upper_threshold=(double) ScaleShortToQuantum((unsigned short) i);
1279 for (i=0; histogram[i] == 0; i++) ;
1280 lower_threshold=upper_percent*(upper_threshold+
1281 ScaleShortToQuantum((unsigned short) i));
1282 histogram=(size_t *) RelinquishMagickMemory(histogram);
1284 Hysteresis threshold.
1286 edge_view=AcquireAuthenticCacheView(edge_image,exception);
1287 trace_view=AcquireAuthenticCacheView(edge_image,exception);
1288 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1289 #pragma omp parallel for schedule(static,4) shared(status) \
1290 magick_threads(edge_image,edge_image,edge_image->rows,1)
1292 for (y=0; y < (ssize_t) edge_image->rows; y++)
1300 if (status == MagickFalse)
1302 q=GetCacheViewAuthenticPixels(edge_view,0,y,edge_image->columns,1,
1304 if (q == (Quantum *) NULL)
1309 for (x=0; x < (ssize_t) edge_image->columns; x++)
1315 Edge if pixel gradient higher than upper threshold.
1317 status=GetMatrixElement(pixel_cache,x,y,&pixel);
1318 if (status == MagickFalse)
1320 if ((pixel.intensity >= upper_threshold) &&
1321 (GetPixelIntensity(edge_image,q) == 0))
1324 status=TraceEdges(edge_image,trace_view,pixel_cache,x,y,
1325 lower_threshold,exception);
1326 if (status == MagickFalse)
1329 q+=GetPixelChannels(edge_image);
1331 if (SyncCacheViewAuthenticPixels(edge_view,exception) == MagickFalse)
1334 trace_view=DestroyCacheView(edge_view);
1335 edge_view=DestroyCacheView(edge_view);
1336 pixel_cache=DestroyMatrixInfo(pixel_cache);
1341 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1345 % C o n v o l v e I m a g e %
1349 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1351 % ConvolveImage() applies a custom convolution kernel to the image.
1353 % The format of the ConvolveImage method is:
1355 % Image *ConvolveImage(const Image *image,const KernelInfo *kernel,
1356 % ExceptionInfo *exception)
1358 % A description of each parameter follows:
1360 % o image: the image.
1362 % o kernel: the filtering kernel.
1364 % o exception: return any errors or warnings in this structure.
1367 MagickExport Image *ConvolveImage(const Image *image,
1368 const KernelInfo *kernel_info,ExceptionInfo *exception)
1373 convolve_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1374 UndefinedCompositeOp,0.0,exception);
1375 return(convolve_image);
1379 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1383 % D e s p e c k l e I m a g e %
1387 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1389 % DespeckleImage() reduces the speckle noise in an image while perserving the
1390 % edges of the original image. A speckle removing filter uses a complementary % hulling technique (raising pixels that are darker than their surrounding
1391 % neighbors, then complementarily lowering pixels that are brighter than their
1392 % surrounding neighbors) to reduce the speckle index of that image (reference
1393 % Crimmins speckle removal).
1395 % The format of the DespeckleImage method is:
1397 % Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1399 % A description of each parameter follows:
1401 % o image: the image.
1403 % o exception: return any errors or warnings in this structure.
1407 static void Hull(const Image *image,const ssize_t x_offset,
1408 const ssize_t y_offset,const size_t columns,const size_t rows,
1409 const int polarity,Quantum *restrict f,Quantum *restrict g)
1420 assert(image != (const Image *) NULL);
1421 assert(image->signature == MagickSignature);
1422 if (image->debug != MagickFalse)
1423 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1424 assert(f != (Quantum *) NULL);
1425 assert(g != (Quantum *) NULL);
1428 r=p+(y_offset*(columns+2)+x_offset);
1429 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1430 #pragma omp parallel for schedule(static,4) \
1431 magick_threads(image,image,1,1)
1433 for (y=0; y < (ssize_t) rows; y++)
1442 i=(2*y+1)+y*columns;
1444 for (x=0; x < (ssize_t) columns; x++)
1446 v=(MagickRealType) p[i];
1447 if ((MagickRealType) r[i] >= (v+ScaleCharToQuantum(2)))
1448 v+=ScaleCharToQuantum(1);
1453 for (x=0; x < (ssize_t) columns; x++)
1455 v=(MagickRealType) p[i];
1456 if ((MagickRealType) r[i] <= (v-ScaleCharToQuantum(2)))
1457 v-=ScaleCharToQuantum(1);
1464 r=q+(y_offset*(columns+2)+x_offset);
1465 s=q-(y_offset*(columns+2)+x_offset);
1466 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1467 #pragma omp parallel for schedule(static,4) \
1468 magick_threads(image,image,1,1)
1470 for (y=0; y < (ssize_t) rows; y++)
1479 i=(2*y+1)+y*columns;
1481 for (x=0; x < (ssize_t) columns; x++)
1483 v=(MagickRealType) q[i];
1484 if (((MagickRealType) s[i] >= (v+ScaleCharToQuantum(2))) &&
1485 ((MagickRealType) r[i] > v))
1486 v+=ScaleCharToQuantum(1);
1491 for (x=0; x < (ssize_t) columns; x++)
1493 v=(MagickRealType) q[i];
1494 if (((MagickRealType) s[i] <= (v-ScaleCharToQuantum(2))) &&
1495 ((MagickRealType) r[i] < v))
1496 v-=ScaleCharToQuantum(1);
1503 MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1505 #define DespeckleImageTag "Despeckle/Image"
1531 static const ssize_t
1532 X[4] = {0, 1, 1,-1},
1533 Y[4] = {1, 0, 1, 1};
1536 Allocate despeckled image.
1538 assert(image != (const Image *) NULL);
1539 assert(image->signature == MagickSignature);
1540 if (image->debug != MagickFalse)
1541 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1542 assert(exception != (ExceptionInfo *) NULL);
1543 assert(exception->signature == MagickSignature);
1544 despeckle_image=CloneImage(image,0,0,MagickTrue,exception);
1545 if (despeckle_image == (Image *) NULL)
1546 return((Image *) NULL);
1547 status=SetImageStorageClass(despeckle_image,DirectClass,exception);
1548 if (status == MagickFalse)
1550 despeckle_image=DestroyImage(despeckle_image);
1551 return((Image *) NULL);
1554 Allocate image buffer.
1556 length=(size_t) ((image->columns+2)*(image->rows+2));
1557 pixel_info=AcquireVirtualMemory(length,sizeof(*pixels));
1558 buffer_info=AcquireVirtualMemory(length,sizeof(*buffer));
1559 if ((pixel_info == (MemoryInfo *) NULL) ||
1560 (buffer_info == (MemoryInfo *) NULL))
1562 if (buffer_info != (MemoryInfo *) NULL)
1563 buffer_info=RelinquishVirtualMemory(buffer_info);
1564 if (pixel_info != (MemoryInfo *) NULL)
1565 pixel_info=RelinquishVirtualMemory(pixel_info);
1566 despeckle_image=DestroyImage(despeckle_image);
1567 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1569 pixels=(Quantum *) GetVirtualMemoryBlob(pixel_info);
1570 buffer=(Quantum *) GetVirtualMemoryBlob(buffer_info);
1572 Reduce speckle in the image.
1575 image_view=AcquireVirtualCacheView(image,exception);
1576 despeckle_view=AcquireAuthenticCacheView(despeckle_image,exception);
1577 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1594 if (status == MagickFalse)
1596 channel=GetPixelChannelChannel(image,i);
1597 traits=GetPixelChannelTraits(image,channel);
1598 despeckle_traits=GetPixelChannelTraits(despeckle_image,channel);
1599 if ((traits == UndefinedPixelTrait) ||
1600 (despeckle_traits == UndefinedPixelTrait))
1602 if ((despeckle_traits & CopyPixelTrait) != 0)
1604 (void) ResetMagickMemory(pixels,0,length*sizeof(*pixels));
1605 j=(ssize_t) image->columns+2;
1606 for (y=0; y < (ssize_t) image->rows; y++)
1608 register const Quantum
1611 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1612 if (p == (const Quantum *) NULL)
1618 for (x=0; x < (ssize_t) image->columns; x++)
1621 p+=GetPixelChannels(image);
1625 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
1626 for (k=0; k < 4; k++)
1628 Hull(image,X[k],Y[k],image->columns,image->rows,1,pixels,buffer);
1629 Hull(image,-X[k],-Y[k],image->columns,image->rows,1,pixels,buffer);
1630 Hull(image,-X[k],-Y[k],image->columns,image->rows,-1,pixels,buffer);
1631 Hull(image,X[k],Y[k],image->columns,image->rows,-1,pixels,buffer);
1633 j=(ssize_t) image->columns+2;
1634 for (y=0; y < (ssize_t) image->rows; y++)
1642 q=GetCacheViewAuthenticPixels(despeckle_view,0,y,despeckle_image->columns,
1644 if (q == (Quantum *) NULL)
1650 for (x=0; x < (ssize_t) image->columns; x++)
1652 SetPixelChannel(despeckle_image,channel,pixels[j++],q);
1653 q+=GetPixelChannels(despeckle_image);
1655 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1656 if (sync == MagickFalse)
1660 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1665 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1666 GetPixelChannels(image));
1667 if (proceed == MagickFalse)
1671 despeckle_view=DestroyCacheView(despeckle_view);
1672 image_view=DestroyCacheView(image_view);
1673 buffer_info=RelinquishVirtualMemory(buffer_info);
1674 pixel_info=RelinquishVirtualMemory(pixel_info);
1675 despeckle_image->type=image->type;
1676 if (status == MagickFalse)
1677 despeckle_image=DestroyImage(despeckle_image);
1678 return(despeckle_image);
1682 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1686 % E d g e I m a g e %
1690 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1692 % EdgeImage() finds edges in an image. Radius defines the radius of the
1693 % convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1696 % The format of the EdgeImage method is:
1698 % Image *EdgeImage(const Image *image,const double radius,
1699 % ExceptionInfo *exception)
1701 % A description of each parameter follows:
1703 % o image: the image.
1705 % o radius: the radius of the pixel neighborhood.
1707 % o exception: return any errors or warnings in this structure.
1710 MagickExport Image *EdgeImage(const Image *image,const double radius,
1711 ExceptionInfo *exception)
1725 assert(image != (const Image *) NULL);
1726 assert(image->signature == MagickSignature);
1727 if (image->debug != MagickFalse)
1728 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1729 assert(exception != (ExceptionInfo *) NULL);
1730 assert(exception->signature == MagickSignature);
1731 width=GetOptimalKernelWidth1D(radius,0.5);
1732 kernel_info=AcquireKernelInfo((const char *) NULL);
1733 if (kernel_info == (KernelInfo *) NULL)
1734 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1735 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
1736 kernel_info->width=width;
1737 kernel_info->height=width;
1738 kernel_info->x=(ssize_t) (kernel_info->width-1)/2;
1739 kernel_info->y=(ssize_t) (kernel_info->height-1)/2;
1740 kernel_info->signature=MagickSignature;
1741 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
1742 AcquireAlignedMemory(kernel_info->width,kernel_info->height*
1743 sizeof(*kernel_info->values)));
1744 if (kernel_info->values == (MagickRealType *) NULL)
1746 kernel_info=DestroyKernelInfo(kernel_info);
1747 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1749 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
1750 kernel_info->values[i]=(-1.0);
1751 kernel_info->values[i/2]=(double) kernel_info->width*kernel_info->height-1.0;
1752 edge_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1753 UndefinedCompositeOp,0.0,exception);
1754 kernel_info=DestroyKernelInfo(kernel_info);
1759 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1763 % E m b o s s I m a g e %
1767 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1769 % EmbossImage() returns a grayscale image with a three-dimensional effect.
1770 % We convolve the image with a Gaussian operator of the given radius and
1771 % standard deviation (sigma). For reasonable results, radius should be
1772 % larger than sigma. Use a radius of 0 and Emboss() selects a suitable
1775 % The format of the EmbossImage method is:
1777 % Image *EmbossImage(const Image *image,const double radius,
1778 % const double sigma,ExceptionInfo *exception)
1780 % A description of each parameter follows:
1782 % o image: the image.
1784 % o radius: the radius of the pixel neighborhood.
1786 % o sigma: the standard deviation of the Gaussian, in pixels.
1788 % o exception: return any errors or warnings in this structure.
1791 MagickExport Image *EmbossImage(const Image *image,const double radius,
1792 const double sigma,ExceptionInfo *exception)
1816 assert(image != (const Image *) NULL);
1817 assert(image->signature == MagickSignature);
1818 if (image->debug != MagickFalse)
1819 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1820 assert(exception != (ExceptionInfo *) NULL);
1821 assert(exception->signature == MagickSignature);
1822 width=GetOptimalKernelWidth1D(radius,sigma);
1823 kernel_info=AcquireKernelInfo((const char *) NULL);
1824 if (kernel_info == (KernelInfo *) NULL)
1825 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1826 kernel_info->width=width;
1827 kernel_info->height=width;
1828 kernel_info->x=(ssize_t) (width-1)/2;
1829 kernel_info->y=(ssize_t) (width-1)/2;
1830 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
1831 AcquireAlignedMemory(kernel_info->width,kernel_info->width*
1832 sizeof(*kernel_info->values)));
1833 if (kernel_info->values == (MagickRealType *) NULL)
1835 kernel_info=DestroyKernelInfo(kernel_info);
1836 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1838 j=(ssize_t) (kernel_info->width-1)/2;
1841 for (v=(-j); v <= j; v++)
1843 for (u=(-j); u <= j; u++)
1845 kernel_info->values[i]=(MagickRealType) (((u < 0) || (v < 0) ? -8.0 :
1846 8.0)*exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
1847 (2.0*MagickPI*MagickSigma*MagickSigma));
1849 kernel_info->values[i]=0.0;
1855 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
1856 normalize+=kernel_info->values[i];
1857 gamma=PerceptibleReciprocal(normalize);
1858 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
1859 kernel_info->values[i]*=gamma;
1860 emboss_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1861 UndefinedCompositeOp,0.0,exception);
1862 kernel_info=DestroyKernelInfo(kernel_info);
1863 if (emboss_image != (Image *) NULL)
1864 (void) EqualizeImage(emboss_image,exception);
1865 return(emboss_image);
1869 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1873 % G a u s s i a n B l u r I m a g e %
1877 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1879 % GaussianBlurImage() blurs an image. We convolve the image with a
1880 % Gaussian operator of the given radius and standard deviation (sigma).
1881 % For reasonable results, the radius should be larger than sigma. Use a
1882 % radius of 0 and GaussianBlurImage() selects a suitable radius for you
1884 % The format of the GaussianBlurImage method is:
1886 % Image *GaussianBlurImage(const Image *image,onst double radius,
1887 % const double sigma,ExceptionInfo *exception)
1889 % A description of each parameter follows:
1891 % o image: the image.
1893 % o radius: the radius of the Gaussian, in pixels, not counting the center
1896 % o sigma: the standard deviation of the Gaussian, in pixels.
1898 % o exception: return any errors or warnings in this structure.
1901 MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
1902 const double sigma,ExceptionInfo *exception)
1905 geometry[MaxTextExtent];
1913 assert(image != (const Image *) NULL);
1914 assert(image->signature == MagickSignature);
1915 if (image->debug != MagickFalse)
1916 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1917 assert(exception != (ExceptionInfo *) NULL);
1918 assert(exception->signature == MagickSignature);
1919 (void) FormatLocaleString(geometry,MaxTextExtent,"gaussian:%.20gx%.20g",
1921 kernel_info=AcquireKernelInfo(geometry);
1922 if (kernel_info == (KernelInfo *) NULL)
1923 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1924 blur_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
1925 UndefinedCompositeOp,0.0,exception);
1926 kernel_info=DestroyKernelInfo(kernel_info);
1931 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1935 % M o t i o n B l u r I m a g e %
1939 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1941 % MotionBlurImage() simulates motion blur. We convolve the image with a
1942 % Gaussian operator of the given radius and standard deviation (sigma).
1943 % For reasonable results, radius should be larger than sigma. Use a
1944 % radius of 0 and MotionBlurImage() selects a suitable radius for you.
1945 % Angle gives the angle of the blurring motion.
1947 % Andrew Protano contributed this effect.
1949 % The format of the MotionBlurImage method is:
1951 % Image *MotionBlurImage(const Image *image,const double radius,
1952 % const double sigma,const double angle,ExceptionInfo *exception)
1954 % A description of each parameter follows:
1956 % o image: the image.
1958 % o radius: the radius of the Gaussian, in pixels, not counting
1961 % o sigma: the standard deviation of the Gaussian, in pixels.
1963 % o angle: Apply the effect along this angle.
1965 % o exception: return any errors or warnings in this structure.
1969 static MagickRealType *GetMotionBlurKernel(const size_t width,
1980 Generate a 1-D convolution kernel.
1982 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1983 kernel=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory((size_t)
1984 width,sizeof(*kernel)));
1985 if (kernel == (MagickRealType *) NULL)
1988 for (i=0; i < (ssize_t) width; i++)
1990 kernel[i]=(MagickRealType) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
1991 MagickSigma)))/(MagickSQ2PI*MagickSigma));
1992 normalize+=kernel[i];
1994 for (i=0; i < (ssize_t) width; i++)
1995 kernel[i]/=normalize;
1999 MagickExport Image *MotionBlurImage(const Image *image,const double radius,
2000 const double sigma,const double angle,ExceptionInfo *exception)
2002 #define BlurImageTag "Blur/Image"
2036 assert(image != (Image *) NULL);
2037 assert(image->signature == MagickSignature);
2038 if (image->debug != MagickFalse)
2039 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2040 assert(exception != (ExceptionInfo *) NULL);
2041 width=GetOptimalKernelWidth1D(radius,sigma);
2042 kernel=GetMotionBlurKernel(width,sigma);
2043 if (kernel == (MagickRealType *) NULL)
2044 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2045 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
2046 if (offset == (OffsetInfo *) NULL)
2048 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2049 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2051 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2052 if (blur_image == (Image *) NULL)
2054 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2055 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2056 return((Image *) NULL);
2058 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2060 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2061 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2062 blur_image=DestroyImage(blur_image);
2063 return((Image *) NULL);
2065 point.x=(double) width*sin(DegreesToRadians(angle));
2066 point.y=(double) width*cos(DegreesToRadians(angle));
2067 for (i=0; i < (ssize_t) width; i++)
2069 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
2070 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
2077 image_view=AcquireVirtualCacheView(image,exception);
2078 motion_view=AcquireVirtualCacheView(image,exception);
2079 blur_view=AcquireAuthenticCacheView(blur_image,exception);
2080 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2081 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2082 magick_threads(image,blur_image,image->rows,1)
2084 for (y=0; y < (ssize_t) image->rows; y++)
2086 register const Quantum
2095 if (status == MagickFalse)
2097 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2098 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2100 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2105 for (x=0; x < (ssize_t) image->columns; x++)
2110 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2124 register const Quantum
2127 register MagickRealType
2133 channel=GetPixelChannelChannel(image,i);
2134 traits=GetPixelChannelTraits(image,channel);
2135 blur_traits=GetPixelChannelTraits(blur_image,channel);
2136 if ((traits == UndefinedPixelTrait) ||
2137 (blur_traits == UndefinedPixelTrait))
2139 if (((blur_traits & CopyPixelTrait) != 0) ||
2140 (GetPixelReadMask(image,p) == 0))
2142 SetPixelChannel(blur_image,channel,p[i],q);
2147 if ((blur_traits & BlendPixelTrait) == 0)
2149 for (j=0; j < (ssize_t) width; j++)
2151 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+
2152 offset[j].y,1,1,exception);
2153 if (r == (const Quantum *) NULL)
2161 SetPixelChannel(blur_image,channel,ClampToQuantum(pixel),q);
2166 for (j=0; j < (ssize_t) width; j++)
2168 r=GetCacheViewVirtualPixels(motion_view,x+offset[j].x,y+offset[j].y,1,
2170 if (r == (const Quantum *) NULL)
2175 alpha=(double) (QuantumScale*GetPixelAlpha(image,r));
2176 pixel+=(*k)*alpha*r[i];
2180 gamma=PerceptibleReciprocal(gamma);
2181 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2183 p+=GetPixelChannels(image);
2184 q+=GetPixelChannels(blur_image);
2186 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2188 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2193 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2194 #pragma omp critical (MagickCore_MotionBlurImage)
2196 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2197 if (proceed == MagickFalse)
2201 blur_view=DestroyCacheView(blur_view);
2202 motion_view=DestroyCacheView(motion_view);
2203 image_view=DestroyCacheView(image_view);
2204 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
2205 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2206 if (status == MagickFalse)
2207 blur_image=DestroyImage(blur_image);
2212 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2216 % P r e v i e w I m a g e %
2220 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2222 % PreviewImage() tiles 9 thumbnails of the specified image with an image
2223 % processing operation applied with varying parameters. This may be helpful
2224 % pin-pointing an appropriate parameter for a particular image processing
2227 % The format of the PreviewImages method is:
2229 % Image *PreviewImages(const Image *image,const PreviewType preview,
2230 % ExceptionInfo *exception)
2232 % A description of each parameter follows:
2234 % o image: the image.
2236 % o preview: the image processing operation.
2238 % o exception: return any errors or warnings in this structure.
2241 MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2242 ExceptionInfo *exception)
2244 #define NumberTiles 9
2245 #define PreviewImageTag "Preview/Image"
2246 #define DefaultPreviewGeometry "204x204+10+10"
2249 factor[MaxTextExtent],
2250 label[MaxTextExtent];
2295 Open output image file.
2297 assert(image != (Image *) NULL);
2298 assert(image->signature == MagickSignature);
2299 if (image->debug != MagickFalse)
2300 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2304 preview_info=AcquireImageInfo();
2305 SetGeometry(image,&geometry);
2306 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2307 &geometry.width,&geometry.height);
2308 images=NewImageList();
2310 GetQuantizeInfo(&quantize_info);
2316 for (i=0; i < NumberTiles; i++)
2318 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2319 if (thumbnail == (Image *) NULL)
2321 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2323 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel,exception);
2324 if (i == (NumberTiles/2))
2326 (void) QueryColorCompliance("#dfdfdf",AllCompliance,
2327 &thumbnail->matte_color,exception);
2328 AppendImageToList(&images,thumbnail);
2336 preview_image=RotateImage(thumbnail,degrees,exception);
2337 (void) FormatLocaleString(label,MaxTextExtent,"rotate %g",degrees);
2343 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
2344 (void) FormatLocaleString(label,MaxTextExtent,"shear %gx%g",degrees,
2350 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2351 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
2352 preview_image=RollImage(thumbnail,x,y,exception);
2353 (void) FormatLocaleString(label,MaxTextExtent,"roll %+.20gx%+.20g",
2354 (double) x,(double) y);
2359 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2360 if (preview_image == (Image *) NULL)
2362 (void) FormatLocaleString(factor,MaxTextExtent,"100,100,%g",2.0*
2364 (void) ModulateImage(preview_image,factor,exception);
2365 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2368 case SaturationPreview:
2370 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2371 if (preview_image == (Image *) NULL)
2373 (void) FormatLocaleString(factor,MaxTextExtent,"100,%g",2.0*percentage);
2374 (void) ModulateImage(preview_image,factor,exception);
2375 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2378 case BrightnessPreview:
2380 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2381 if (preview_image == (Image *) NULL)
2383 (void) FormatLocaleString(factor,MaxTextExtent,"%g",2.0*percentage);
2384 (void) ModulateImage(preview_image,factor,exception);
2385 (void) FormatLocaleString(label,MaxTextExtent,"modulate %s",factor);
2391 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2392 if (preview_image == (Image *) NULL)
2395 (void) GammaImage(preview_image,gamma,exception);
2396 (void) FormatLocaleString(label,MaxTextExtent,"gamma %g",gamma);
2401 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2402 if (preview_image != (Image *) NULL)
2403 for (x=0; x < i; x++)
2404 (void) ContrastImage(preview_image,MagickTrue,exception);
2405 (void) FormatLocaleString(label,MaxTextExtent,"contrast (%.20g)",
2411 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2412 if (preview_image == (Image *) NULL)
2414 for (x=0; x < i; x++)
2415 (void) ContrastImage(preview_image,MagickFalse,exception);
2416 (void) FormatLocaleString(label,MaxTextExtent,"+contrast (%.20g)",
2420 case GrayscalePreview:
2422 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2423 if (preview_image == (Image *) NULL)
2426 quantize_info.number_colors=colors;
2427 quantize_info.colorspace=GRAYColorspace;
2428 (void) QuantizeImage(&quantize_info,preview_image,exception);
2429 (void) FormatLocaleString(label,MaxTextExtent,
2430 "-colorspace gray -colors %.20g",(double) colors);
2433 case QuantizePreview:
2435 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2436 if (preview_image == (Image *) NULL)
2439 quantize_info.number_colors=colors;
2440 (void) QuantizeImage(&quantize_info,preview_image,exception);
2441 (void) FormatLocaleString(label,MaxTextExtent,"colors %.20g",(double)
2445 case DespecklePreview:
2447 for (x=0; x < (i-1); x++)
2449 preview_image=DespeckleImage(thumbnail,exception);
2450 if (preview_image == (Image *) NULL)
2452 thumbnail=DestroyImage(thumbnail);
2453 thumbnail=preview_image;
2455 preview_image=DespeckleImage(thumbnail,exception);
2456 if (preview_image == (Image *) NULL)
2458 (void) FormatLocaleString(label,MaxTextExtent,"despeckle (%.20g)",
2462 case ReduceNoisePreview:
2464 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
2465 (size_t) radius,exception);
2466 (void) FormatLocaleString(label,MaxTextExtent,"noise %g",radius);
2469 case AddNoisePreview:
2475 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
2480 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
2485 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
2490 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
2495 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
2500 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
2505 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
2509 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) i,
2510 (size_t) i,exception);
2511 (void) FormatLocaleString(label,MaxTextExtent,"+noise %s",factor);
2514 case SharpenPreview:
2516 preview_image=SharpenImage(thumbnail,radius,sigma,exception);
2517 (void) FormatLocaleString(label,MaxTextExtent,"sharpen %gx%g",radius,
2523 preview_image=BlurImage(thumbnail,radius,sigma,exception);
2524 (void) FormatLocaleString(label,MaxTextExtent,"blur %gx%g",radius,
2528 case ThresholdPreview:
2530 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2531 if (preview_image == (Image *) NULL)
2533 (void) BilevelImage(thumbnail,(double) (percentage*((double)
2534 QuantumRange+1.0))/100.0,exception);
2535 (void) FormatLocaleString(label,MaxTextExtent,"threshold %g",(double)
2536 (percentage*((double) QuantumRange+1.0))/100.0);
2539 case EdgeDetectPreview:
2541 preview_image=EdgeImage(thumbnail,radius,exception);
2542 (void) FormatLocaleString(label,MaxTextExtent,"edge %g",radius);
2547 preview_image=SpreadImage(thumbnail,radius,thumbnail->interpolate,
2549 (void) FormatLocaleString(label,MaxTextExtent,"spread %g",radius+0.5);
2552 case SolarizePreview:
2554 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2555 if (preview_image == (Image *) NULL)
2557 (void) SolarizeImage(preview_image,(double) QuantumRange*percentage/
2559 (void) FormatLocaleString(label,MaxTextExtent,"solarize %g",
2560 (QuantumRange*percentage)/100.0);
2566 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
2568 (void) FormatLocaleString(label,MaxTextExtent,"shade %gx%g",degrees,
2574 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2575 if (preview_image == (Image *) NULL)
2577 geometry.width=(size_t) (2*i+2);
2578 geometry.height=(size_t) (2*i+2);
2581 (void) RaiseImage(preview_image,&geometry,MagickTrue,exception);
2582 (void) FormatLocaleString(label,MaxTextExtent,
2583 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
2584 geometry.height,(double) geometry.x,(double) geometry.y);
2587 case SegmentPreview:
2589 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2590 if (preview_image == (Image *) NULL)
2593 (void) SegmentImage(preview_image,sRGBColorspace,MagickFalse,threshold,
2594 threshold,exception);
2595 (void) FormatLocaleString(label,MaxTextExtent,"segment %gx%g",
2596 threshold,threshold);
2601 preview_image=SwirlImage(thumbnail,degrees,image->interpolate,
2603 (void) FormatLocaleString(label,MaxTextExtent,"swirl %g",degrees);
2607 case ImplodePreview:
2610 preview_image=ImplodeImage(thumbnail,degrees,image->interpolate,
2612 (void) FormatLocaleString(label,MaxTextExtent,"implode %g",degrees);
2618 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,
2619 image->interpolate,exception);
2620 (void) FormatLocaleString(label,MaxTextExtent,"wave %gx%g",0.5*degrees,
2624 case OilPaintPreview:
2626 preview_image=OilPaintImage(thumbnail,(double) radius,(double) sigma,
2628 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",radius,
2632 case CharcoalDrawingPreview:
2634 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
2636 (void) FormatLocaleString(label,MaxTextExtent,"charcoal %gx%g",radius,
2643 filename[MaxTextExtent];
2651 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2652 if (preview_image == (Image *) NULL)
2654 preview_info->quality=(size_t) percentage;
2655 (void) FormatLocaleString(factor,MaxTextExtent,"%.20g",(double)
2656 preview_info->quality);
2657 file=AcquireUniqueFileResource(filename);
2660 (void) FormatLocaleString(preview_image->filename,MaxTextExtent,
2661 "jpeg:%s",filename);
2662 status=WriteImage(preview_info,preview_image,exception);
2663 if (status != MagickFalse)
2668 (void) CopyMagickString(preview_info->filename,
2669 preview_image->filename,MaxTextExtent);
2670 quality_image=ReadImage(preview_info,exception);
2671 if (quality_image != (Image *) NULL)
2673 preview_image=DestroyImage(preview_image);
2674 preview_image=quality_image;
2677 (void) RelinquishUniqueFileResource(preview_image->filename);
2678 if ((GetBlobSize(preview_image)/1024) >= 1024)
2679 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%gmb ",
2680 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
2683 if (GetBlobSize(preview_image) >= 1024)
2684 (void) FormatLocaleString(label,MaxTextExtent,
2685 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
2686 GetBlobSize(preview_image))/1024.0);
2688 (void) FormatLocaleString(label,MaxTextExtent,"quality %s\n%.20gb ",
2689 factor,(double) ((MagickOffsetType) GetBlobSize(thumbnail)));
2693 thumbnail=DestroyImage(thumbnail);
2697 if (preview_image == (Image *) NULL)
2699 (void) DeleteImageProperty(preview_image,"label");
2700 (void) SetImageProperty(preview_image,"label",label,exception);
2701 AppendImageToList(&images,preview_image);
2702 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
2704 if (proceed == MagickFalse)
2707 if (images == (Image *) NULL)
2709 preview_info=DestroyImageInfo(preview_info);
2710 return((Image *) NULL);
2715 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
2716 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
2717 montage_info->shadow=MagickTrue;
2718 (void) CloneString(&montage_info->tile,"3x3");
2719 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
2720 (void) CloneString(&montage_info->frame,DefaultTileFrame);
2721 montage_image=MontageImages(images,montage_info,exception);
2722 montage_info=DestroyMontageInfo(montage_info);
2723 images=DestroyImageList(images);
2724 if (montage_image == (Image *) NULL)
2725 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2726 if (montage_image->montage != (char *) NULL)
2729 Free image directory.
2731 montage_image->montage=(char *) RelinquishMagickMemory(
2732 montage_image->montage);
2733 if (image->directory != (char *) NULL)
2734 montage_image->directory=(char *) RelinquishMagickMemory(
2735 montage_image->directory);
2737 preview_info=DestroyImageInfo(preview_info);
2738 return(montage_image);
2742 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2746 % R o t a t i o n a l B l u r I m a g e %
2750 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2752 % RotationalBlurImage() applies a radial blur to the image.
2754 % Andrew Protano contributed this effect.
2756 % The format of the RotationalBlurImage method is:
2758 % Image *RotationalBlurImage(const Image *image,const double angle,
2759 % ExceptionInfo *exception)
2761 % A description of each parameter follows:
2763 % o image: the image.
2765 % o angle: the angle of the radial blur.
2769 % o exception: return any errors or warnings in this structure.
2772 MagickExport Image *RotationalBlurImage(const Image *image,const double angle,
2773 ExceptionInfo *exception)
2809 Allocate blur image.
2811 assert(image != (Image *) NULL);
2812 assert(image->signature == MagickSignature);
2813 if (image->debug != MagickFalse)
2814 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2815 assert(exception != (ExceptionInfo *) NULL);
2816 assert(exception->signature == MagickSignature);
2817 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2818 if (blur_image == (Image *) NULL)
2819 return((Image *) NULL);
2820 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
2822 blur_image=DestroyImage(blur_image);
2823 return((Image *) NULL);
2825 blur_center.x=(double) (image->columns-1)/2.0;
2826 blur_center.y=(double) (image->rows-1)/2.0;
2827 blur_radius=hypot(blur_center.x,blur_center.y);
2828 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
2829 theta=DegreesToRadians(angle)/(double) (n-1);
2830 cos_theta=(double *) AcquireQuantumMemory((size_t) n,
2831 sizeof(*cos_theta));
2832 sin_theta=(double *) AcquireQuantumMemory((size_t) n,
2833 sizeof(*sin_theta));
2834 if ((cos_theta == (double *) NULL) ||
2835 (sin_theta == (double *) NULL))
2837 blur_image=DestroyImage(blur_image);
2838 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2840 offset=theta*(double) (n-1)/2.0;
2841 for (i=0; i < (ssize_t) n; i++)
2843 cos_theta[i]=cos((double) (theta*i-offset));
2844 sin_theta[i]=sin((double) (theta*i-offset));
2851 image_view=AcquireVirtualCacheView(image,exception);
2852 radial_view=AcquireVirtualCacheView(image,exception);
2853 blur_view=AcquireAuthenticCacheView(blur_image,exception);
2854 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2855 #pragma omp parallel for schedule(static,4) shared(progress,status) \
2856 magick_threads(image,blur_image,image->rows,1)
2858 for (y=0; y < (ssize_t) image->rows; y++)
2860 register const Quantum
2869 if (status == MagickFalse)
2871 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2872 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2874 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2879 for (x=0; x < (ssize_t) image->columns; x++)
2893 center.x=(double) x-blur_center.x;
2894 center.y=(double) y-blur_center.y;
2895 radius=hypot((double) center.x,center.y);
2900 step=(size_t) (blur_radius/radius);
2907 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2920 register const Quantum
2926 channel=GetPixelChannelChannel(image,i);
2927 traits=GetPixelChannelTraits(image,channel);
2928 blur_traits=GetPixelChannelTraits(blur_image,channel);
2929 if ((traits == UndefinedPixelTrait) ||
2930 (blur_traits == UndefinedPixelTrait))
2932 if (((blur_traits & CopyPixelTrait) != 0) ||
2933 (GetPixelReadMask(image,p) == 0))
2935 SetPixelChannel(blur_image,channel,p[i],q);
2940 if ((blur_traits & BlendPixelTrait) == 0)
2942 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2944 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2945 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2946 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2948 if (r == (const Quantum *) NULL)
2956 gamma=PerceptibleReciprocal(gamma);
2957 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2960 for (j=0; j < (ssize_t) n; j+=(ssize_t) step)
2962 r=GetCacheViewVirtualPixels(radial_view, (ssize_t) (blur_center.x+
2963 center.x*cos_theta[j]-center.y*sin_theta[j]+0.5),(ssize_t)
2964 (blur_center.y+center.x*sin_theta[j]+center.y*cos_theta[j]+0.5),
2966 if (r == (const Quantum *) NULL)
2971 pixel+=GetPixelAlpha(image,r)*r[i];
2972 gamma+=GetPixelAlpha(image,r);
2974 gamma=PerceptibleReciprocal(gamma);
2975 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
2977 p+=GetPixelChannels(image);
2978 q+=GetPixelChannels(blur_image);
2980 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2982 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2987 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2988 #pragma omp critical (MagickCore_RotationalBlurImage)
2990 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2991 if (proceed == MagickFalse)
2995 blur_view=DestroyCacheView(blur_view);
2996 radial_view=DestroyCacheView(radial_view);
2997 image_view=DestroyCacheView(image_view);
2998 cos_theta=(double *) RelinquishMagickMemory(cos_theta);
2999 sin_theta=(double *) RelinquishMagickMemory(sin_theta);
3000 if (status == MagickFalse)
3001 blur_image=DestroyImage(blur_image);
3006 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3010 % S e l e c t i v e B l u r I m a g e %
3014 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3016 % SelectiveBlurImage() selectively blur pixels within a contrast threshold.
3017 % It is similar to the unsharpen mask that sharpens everything with contrast
3018 % above a certain threshold.
3020 % The format of the SelectiveBlurImage method is:
3022 % Image *SelectiveBlurImage(const Image *image,const double radius,
3023 % const double sigma,const double threshold,ExceptionInfo *exception)
3025 % A description of each parameter follows:
3027 % o image: the image.
3029 % o radius: the radius of the Gaussian, in pixels, not counting the center
3032 % o sigma: the standard deviation of the Gaussian, in pixels.
3034 % o threshold: only pixels within this contrast threshold are included
3035 % in the blur operation.
3037 % o exception: return any errors or warnings in this structure.
3040 MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
3041 const double sigma,const double threshold,ExceptionInfo *exception)
3043 #define SelectiveBlurImageTag "SelectiveBlur/Image"
3077 Initialize blur image attributes.
3079 assert(image != (Image *) NULL);
3080 assert(image->signature == MagickSignature);
3081 if (image->debug != MagickFalse)
3082 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3083 assert(exception != (ExceptionInfo *) NULL);
3084 assert(exception->signature == MagickSignature);
3085 width=GetOptimalKernelWidth1D(radius,sigma);
3086 kernel=(MagickRealType *) MagickAssumeAligned(AcquireAlignedMemory((size_t)
3087 width,width*sizeof(*kernel)));
3088 if (kernel == (MagickRealType *) NULL)
3089 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3090 j=(ssize_t) (width-1)/2;
3092 for (v=(-j); v <= j; v++)
3094 for (u=(-j); u <= j; u++)
3095 kernel[i++]=(MagickRealType) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
3096 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3098 if (image->debug != MagickFalse)
3101 format[MaxTextExtent],
3104 register const MagickRealType
3111 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
3112 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
3114 message=AcquireString("");
3116 for (v=0; v < (ssize_t) width; v++)
3119 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
3120 (void) ConcatenateString(&message,format);
3121 for (u=0; u < (ssize_t) width; u++)
3123 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",(double) *k++);
3124 (void) ConcatenateString(&message,format);
3126 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
3128 message=DestroyString(message);
3130 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3131 if (blur_image == (Image *) NULL)
3132 return((Image *) NULL);
3133 if (SetImageStorageClass(blur_image,DirectClass,exception) == MagickFalse)
3135 blur_image=DestroyImage(blur_image);
3136 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3137 return((Image *) NULL);
3139 luminance_image=CloneImage(image,0,0,MagickTrue,exception);
3140 if (luminance_image == (Image *) NULL)
3142 blur_image=DestroyImage(blur_image);
3143 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3144 return((Image *) NULL);
3146 status=TransformImageColorspace(luminance_image,GRAYColorspace,exception);
3147 if (status == MagickFalse)
3149 luminance_image=DestroyImage(luminance_image);
3150 blur_image=DestroyImage(blur_image);
3151 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3152 return((Image *) NULL);
3155 Threshold blur image.
3159 center=(ssize_t) (GetPixelChannels(image)*(image->columns+width)*
3160 ((width-1)/2L)+GetPixelChannels(image)*((width-1)/2L));
3161 image_view=AcquireVirtualCacheView(image,exception);
3162 luminance_view=AcquireVirtualCacheView(luminance_image,exception);
3163 blur_view=AcquireAuthenticCacheView(blur_image,exception);
3164 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3165 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3166 magick_threads(image,blur_image,image->rows,1)
3168 for (y=0; y < (ssize_t) image->rows; y++)
3176 register const Quantum
3186 if (status == MagickFalse)
3188 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) (width-1)/2L),y-(ssize_t)
3189 ((width-1)/2L),image->columns+width,width,exception);
3190 l=GetCacheViewVirtualPixels(luminance_view,-((ssize_t) (width-1)/2L),y-
3191 (ssize_t) ((width-1)/2L),luminance_image->columns+width,width,exception);
3192 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3194 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3199 for (x=0; x < (ssize_t) image->columns; x++)
3207 intensity=GetPixelIntensity(image,p+center);
3208 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3222 register const MagickRealType
3225 register const Quantum
3226 *restrict luminance_pixels,
3235 channel=GetPixelChannelChannel(image,i);
3236 traits=GetPixelChannelTraits(image,channel);
3237 blur_traits=GetPixelChannelTraits(blur_image,channel);
3238 if ((traits == UndefinedPixelTrait) ||
3239 (blur_traits == UndefinedPixelTrait))
3241 if (((blur_traits & CopyPixelTrait) != 0) ||
3242 (GetPixelReadMask(image,p+center) == 0))
3244 SetPixelChannel(blur_image,channel,p[center+i],q);
3252 if ((blur_traits & BlendPixelTrait) == 0)
3254 for (v=0; v < (ssize_t) width; v++)
3256 for (u=0; u < (ssize_t) width; u++)
3258 contrast=GetPixelIntensity(luminance_image,luminance_pixels)-
3260 if (fabs(contrast) < threshold)
3262 pixel+=(*k)*pixels[i];
3266 pixels+=GetPixelChannels(image);
3267 luminance_pixels+=GetPixelChannels(luminance_image);
3269 pixels+=(image->columns-1)*GetPixelChannels(image);
3270 luminance_pixels+=luminance_image->columns*
3271 GetPixelChannels(luminance_image);
3273 if (fabs((double) gamma) < MagickEpsilon)
3275 SetPixelChannel(blur_image,channel,p[center+i],q);
3278 gamma=PerceptibleReciprocal(gamma);
3279 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3282 for (v=0; v < (ssize_t) width; v++)
3284 for (u=0; u < (ssize_t) width; u++)
3286 contrast=GetPixelIntensity(image,pixels)-intensity;
3287 if (fabs(contrast) < threshold)
3289 alpha=(double) (QuantumScale*GetPixelAlpha(image,pixels));
3290 pixel+=(*k)*alpha*pixels[i];
3294 pixels+=GetPixelChannels(image);
3295 luminance_pixels+=GetPixelChannels(luminance_image);
3297 pixels+=(image->columns-1)*GetPixelChannels(image);
3298 luminance_pixels+=luminance_image->columns*
3299 GetPixelChannels(luminance_image);
3301 if (fabs((double) gamma) < MagickEpsilon)
3303 SetPixelChannel(blur_image,channel,p[center+i],q);
3306 gamma=PerceptibleReciprocal(gamma);
3307 SetPixelChannel(blur_image,channel,ClampToQuantum(gamma*pixel),q);
3309 p+=GetPixelChannels(image);
3310 l+=GetPixelChannels(luminance_image);
3311 q+=GetPixelChannels(blur_image);
3313 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
3314 if (sync == MagickFalse)
3316 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3321 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3322 #pragma omp critical (MagickCore_SelectiveBlurImage)
3324 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
3326 if (proceed == MagickFalse)
3330 blur_image->type=image->type;
3331 blur_view=DestroyCacheView(blur_view);
3332 image_view=DestroyCacheView(image_view);
3333 luminance_image=DestroyImage(luminance_image);
3334 kernel=(MagickRealType *) RelinquishAlignedMemory(kernel);
3335 if (status == MagickFalse)
3336 blur_image=DestroyImage(blur_image);
3341 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3345 % S h a d e I m a g e %
3349 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3351 % ShadeImage() shines a distant light on an image to create a
3352 % three-dimensional effect. You control the positioning of the light with
3353 % azimuth and elevation; azimuth is measured in degrees off the x axis
3354 % and elevation is measured in pixels above the Z axis.
3356 % The format of the ShadeImage method is:
3358 % Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3359 % const double azimuth,const double elevation,ExceptionInfo *exception)
3361 % A description of each parameter follows:
3363 % o image: the image.
3365 % o gray: A value other than zero shades the intensity of each pixel.
3367 % o azimuth, elevation: Define the light source direction.
3369 % o exception: return any errors or warnings in this structure.
3372 MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
3373 const double azimuth,const double elevation,ExceptionInfo *exception)
3375 #define ShadeImageTag "Shade/Image"
3398 Initialize shaded image attributes.
3400 assert(image != (const Image *) NULL);
3401 assert(image->signature == MagickSignature);
3402 if (image->debug != MagickFalse)
3403 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3404 assert(exception != (ExceptionInfo *) NULL);
3405 assert(exception->signature == MagickSignature);
3406 linear_image=CloneImage(image,0,0,MagickTrue,exception);
3407 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3408 if ((linear_image == (Image *) NULL) || (shade_image == (Image *) NULL))
3410 if (linear_image != (Image *) NULL)
3411 linear_image=DestroyImage(linear_image);
3412 if (shade_image != (Image *) NULL)
3413 shade_image=DestroyImage(shade_image);
3414 return((Image *) NULL);
3416 if (SetImageStorageClass(shade_image,DirectClass,exception) == MagickFalse)
3418 linear_image=DestroyImage(linear_image);
3419 shade_image=DestroyImage(shade_image);
3420 return((Image *) NULL);
3423 Compute the light vector.
3425 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
3426 cos(DegreesToRadians(elevation));
3427 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
3428 cos(DegreesToRadians(elevation));
3429 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
3435 image_view=AcquireVirtualCacheView(linear_image,exception);
3436 shade_view=AcquireAuthenticCacheView(shade_image,exception);
3437 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3438 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3439 magick_threads(linear_image,shade_image,linear_image->rows,1)
3441 for (y=0; y < (ssize_t) linear_image->rows; y++)
3451 register const Quantum
3463 if (status == MagickFalse)
3465 p=GetCacheViewVirtualPixels(image_view,-1,y-1,linear_image->columns+2,3,
3467 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
3469 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3475 Shade this row of pixels.
3477 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
3478 pre=p+GetPixelChannels(linear_image);
3479 center=pre+(linear_image->columns+2)*GetPixelChannels(linear_image);
3480 post=center+(linear_image->columns+2)*GetPixelChannels(linear_image);
3481 for (x=0; x < (ssize_t) linear_image->columns; x++)
3487 Determine the surface normal and compute shading.
3490 GetPixelIntensity(linear_image,pre-GetPixelChannels(linear_image))+
3491 GetPixelIntensity(linear_image,center-GetPixelChannels(linear_image))+
3492 GetPixelIntensity(linear_image,post-GetPixelChannels(linear_image))-
3493 GetPixelIntensity(linear_image,pre+GetPixelChannels(linear_image))-
3494 GetPixelIntensity(linear_image,center+GetPixelChannels(linear_image))-
3495 GetPixelIntensity(linear_image,post+GetPixelChannels(linear_image)));
3497 GetPixelIntensity(linear_image,post-GetPixelChannels(linear_image))+
3498 GetPixelIntensity(linear_image,post)+
3499 GetPixelIntensity(linear_image,post+GetPixelChannels(linear_image))-
3500 GetPixelIntensity(linear_image,pre-GetPixelChannels(linear_image))-
3501 GetPixelIntensity(linear_image,pre)-
3502 GetPixelIntensity(linear_image,pre+GetPixelChannels(linear_image)));
3503 if ((normal.x == 0.0) && (normal.y == 0.0))
3508 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
3509 if (distance > MagickEpsilon)
3511 normal_distance=normal.x*normal.x+normal.y*normal.y+
3513 if (normal_distance > (MagickEpsilon*MagickEpsilon))
3514 shade=distance/sqrt((double) normal_distance);
3517 for (i=0; i < (ssize_t) GetPixelChannels(linear_image); i++)
3526 channel=GetPixelChannelChannel(linear_image,i);
3527 traits=GetPixelChannelTraits(linear_image,channel);
3528 shade_traits=GetPixelChannelTraits(shade_image,channel);
3529 if ((traits == UndefinedPixelTrait) ||
3530 (shade_traits == UndefinedPixelTrait))
3532 if (((shade_traits & CopyPixelTrait) != 0) ||
3533 (GetPixelReadMask(linear_image,center) == 0))
3535 SetPixelChannel(shade_image,channel,center[i],q);
3538 if (gray != MagickFalse)
3540 SetPixelChannel(shade_image,channel,ClampToQuantum(shade),q);
3543 SetPixelChannel(shade_image,channel,ClampToQuantum(QuantumScale*shade*
3546 pre+=GetPixelChannels(linear_image);
3547 center+=GetPixelChannels(linear_image);
3548 post+=GetPixelChannels(linear_image);
3549 q+=GetPixelChannels(shade_image);
3551 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
3553 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3558 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3559 #pragma omp critical (MagickCore_ShadeImage)
3561 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
3562 if (proceed == MagickFalse)
3566 shade_view=DestroyCacheView(shade_view);
3567 image_view=DestroyCacheView(image_view);
3568 linear_image=DestroyImage(linear_image);
3569 if (status == MagickFalse)
3570 shade_image=DestroyImage(shade_image);
3571 return(shade_image);
3575 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3579 % S h a r p e n I m a g e %
3583 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3585 % SharpenImage() sharpens the image. We convolve the image with a Gaussian
3586 % operator of the given radius and standard deviation (sigma). For
3587 % reasonable results, radius should be larger than sigma. Use a radius of 0
3588 % and SharpenImage() selects a suitable radius for you.
3590 % Using a separable kernel would be faster, but the negative weights cancel
3591 % out on the corners of the kernel producing often undesirable ringing in the
3592 % filtered result; this can be avoided by using a 2D gaussian shaped image
3593 % sharpening kernel instead.
3595 % The format of the SharpenImage method is:
3597 % Image *SharpenImage(const Image *image,const double radius,
3598 % const double sigma,ExceptionInfo *exception)
3600 % A description of each parameter follows:
3602 % o image: the image.
3604 % o radius: the radius of the Gaussian, in pixels, not counting the center
3607 % o sigma: the standard deviation of the Laplacian, in pixels.
3609 % o exception: return any errors or warnings in this structure.
3612 MagickExport Image *SharpenImage(const Image *image,const double radius,
3613 const double sigma,ExceptionInfo *exception)
3636 assert(image != (const Image *) NULL);
3637 assert(image->signature == MagickSignature);
3638 if (image->debug != MagickFalse)
3639 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3640 assert(exception != (ExceptionInfo *) NULL);
3641 assert(exception->signature == MagickSignature);
3642 width=GetOptimalKernelWidth2D(radius,sigma);
3643 kernel_info=AcquireKernelInfo((const char *) NULL);
3644 if (kernel_info == (KernelInfo *) NULL)
3645 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3646 (void) ResetMagickMemory(kernel_info,0,sizeof(*kernel_info));
3647 kernel_info->width=width;
3648 kernel_info->height=width;
3649 kernel_info->x=(ssize_t) (width-1)/2;
3650 kernel_info->y=(ssize_t) (width-1)/2;
3651 kernel_info->signature=MagickSignature;
3652 kernel_info->values=(MagickRealType *) MagickAssumeAligned(
3653 AcquireAlignedMemory(kernel_info->width,kernel_info->height*
3654 sizeof(*kernel_info->values)));
3655 if (kernel_info->values == (MagickRealType *) NULL)
3657 kernel_info=DestroyKernelInfo(kernel_info);
3658 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3661 j=(ssize_t) (kernel_info->width-1)/2;
3663 for (v=(-j); v <= j; v++)
3665 for (u=(-j); u <= j; u++)
3667 kernel_info->values[i]=(MagickRealType) (-exp(-((double) u*u+v*v)/(2.0*
3668 MagickSigma*MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3669 normalize+=kernel_info->values[i];
3673 kernel_info->values[i/2]=(double) ((-2.0)*normalize);
3675 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
3676 normalize+=kernel_info->values[i];
3677 gamma=PerceptibleReciprocal(normalize);
3678 for (i=0; i < (ssize_t) (kernel_info->width*kernel_info->height); i++)
3679 kernel_info->values[i]*=gamma;
3680 sharp_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
3681 UndefinedCompositeOp,0.0,exception);
3682 kernel_info=DestroyKernelInfo(kernel_info);
3683 return(sharp_image);
3687 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3691 % S p r e a d I m a g e %
3695 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3697 % SpreadImage() is a special effects method that randomly displaces each
3698 % pixel in a block defined by the radius parameter.
3700 % The format of the SpreadImage method is:
3702 % Image *SpreadImage(const Image *image,const double radius,
3703 % const PixelInterpolateMethod method,ExceptionInfo *exception)
3705 % A description of each parameter follows:
3707 % o image: the image.
3709 % o radius: choose a random pixel in a neighborhood of this extent.
3711 % o method: the pixel interpolation method.
3713 % o exception: return any errors or warnings in this structure.
3716 MagickExport Image *SpreadImage(const Image *image,const double radius,
3717 const PixelInterpolateMethod method,ExceptionInfo *exception)
3719 #define SpreadImageTag "Spread/Image"
3735 **restrict random_info;
3743 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3749 Initialize spread image attributes.
3751 assert(image != (Image *) NULL);
3752 assert(image->signature == MagickSignature);
3753 if (image->debug != MagickFalse)
3754 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3755 assert(exception != (ExceptionInfo *) NULL);
3756 assert(exception->signature == MagickSignature);
3757 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3759 if (spread_image == (Image *) NULL)
3760 return((Image *) NULL);
3761 if (SetImageStorageClass(spread_image,DirectClass,exception) == MagickFalse)
3763 spread_image=DestroyImage(spread_image);
3764 return((Image *) NULL);
3771 width=GetOptimalKernelWidth1D(radius,0.5);
3772 random_info=AcquireRandomInfoThreadSet();
3773 image_view=AcquireVirtualCacheView(image,exception);
3774 spread_view=AcquireAuthenticCacheView(spread_image,exception);
3775 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3776 key=GetRandomSecretKey(random_info[0]);
3777 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3778 magick_threads(image,spread_image,image->rows,key == ~0UL)
3780 for (y=0; y < (ssize_t) image->rows; y++)
3783 id = GetOpenMPThreadId();
3791 if (status == MagickFalse)
3793 q=QueueCacheViewAuthenticPixels(spread_view,0,y,spread_image->columns,1,
3795 if (q == (Quantum *) NULL)
3800 for (x=0; x < (ssize_t) image->columns; x++)
3805 point.x=GetPseudoRandomValue(random_info[id]);
3806 point.y=GetPseudoRandomValue(random_info[id]);
3807 status=InterpolatePixelChannels(image,image_view,spread_image,method,
3808 (double) x+width*point.x-0.5,(double) y+width*point.y-0.5,q,exception);
3809 q+=GetPixelChannels(spread_image);
3811 if (SyncCacheViewAuthenticPixels(spread_view,exception) == MagickFalse)
3813 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3818 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3819 #pragma omp critical (MagickCore_SpreadImage)
3821 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
3822 if (proceed == MagickFalse)
3826 spread_view=DestroyCacheView(spread_view);
3827 image_view=DestroyCacheView(image_view);
3828 random_info=DestroyRandomInfoThreadSet(random_info);
3829 if (status == MagickFalse)
3830 spread_image=DestroyImage(spread_image);
3831 return(spread_image);
3835 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3839 % U n s h a r p M a s k I m a g e %
3843 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3845 % UnsharpMaskImage() sharpens one or more image channels. We convolve the
3846 % image with a Gaussian operator of the given radius and standard deviation
3847 % (sigma). For reasonable results, radius should be larger than sigma. Use a
3848 % radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
3850 % The format of the UnsharpMaskImage method is:
3852 % Image *UnsharpMaskImage(const Image *image,const double radius,
3853 % const double sigma,const double amount,const double threshold,
3854 % ExceptionInfo *exception)
3856 % A description of each parameter follows:
3858 % o image: the image.
3860 % o radius: the radius of the Gaussian, in pixels, not counting the center
3863 % o sigma: the standard deviation of the Gaussian, in pixels.
3865 % o gain: the percentage of the difference between the original and the
3866 % blur image that is added back into the original.
3868 % o threshold: the threshold in pixels needed to apply the diffence gain.
3870 % o exception: return any errors or warnings in this structure.
3873 MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
3874 const double sigma,const double gain,const double threshold,
3875 ExceptionInfo *exception)
3877 #define SharpenImageTag "Sharpen/Image"
3898 assert(image != (const Image *) NULL);
3899 assert(image->signature == MagickSignature);
3900 if (image->debug != MagickFalse)
3901 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3902 assert(exception != (ExceptionInfo *) NULL);
3903 unsharp_image=BlurImage(image,radius,sigma,exception);
3904 if (unsharp_image == (Image *) NULL)
3905 return((Image *) NULL);
3906 quantum_threshold=(double) QuantumRange*threshold;
3912 image_view=AcquireVirtualCacheView(image,exception);
3913 unsharp_view=AcquireAuthenticCacheView(unsharp_image,exception);
3914 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3915 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3916 magick_threads(image,unsharp_image,image->rows,1)
3918 for (y=0; y < (ssize_t) image->rows; y++)
3920 register const Quantum
3929 if (status == MagickFalse)
3931 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3932 q=QueueCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
3934 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3939 for (x=0; x < (ssize_t) image->columns; x++)
3944 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3956 channel=GetPixelChannelChannel(image,i);
3957 traits=GetPixelChannelTraits(image,channel);
3958 unsharp_traits=GetPixelChannelTraits(unsharp_image,channel);
3959 if ((traits == UndefinedPixelTrait) ||
3960 (unsharp_traits == UndefinedPixelTrait))
3962 if (((unsharp_traits & CopyPixelTrait) != 0) ||
3963 (GetPixelReadMask(image,p) != 0))
3965 SetPixelChannel(unsharp_image,channel,p[i],q);
3968 pixel=p[i]-(double) GetPixelChannel(unsharp_image,channel,q);
3969 if (fabs(2.0*pixel) < quantum_threshold)
3970 pixel=(double) p[i];
3972 pixel=(double) p[i]+gain*pixel;
3973 SetPixelChannel(unsharp_image,channel,ClampToQuantum(pixel),q);
3975 p+=GetPixelChannels(image);
3976 q+=GetPixelChannels(unsharp_image);
3978 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
3980 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3985 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3986 #pragma omp critical (MagickCore_UnsharpMaskImage)
3988 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
3989 if (proceed == MagickFalse)
3993 unsharp_image->type=image->type;
3994 unsharp_view=DestroyCacheView(unsharp_view);
3995 image_view=DestroyCacheView(image_view);
3996 if (status == MagickFalse)
3997 unsharp_image=DestroyImage(unsharp_image);
3998 return(unsharp_image);