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-2011 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 "magick/studio.h"
44 #include "magick/accelerate.h"
45 #include "magick/blob.h"
46 #include "magick/cache-view.h"
47 #include "magick/color.h"
48 #include "magick/color-private.h"
49 #include "magick/colorspace.h"
50 #include "magick/constitute.h"
51 #include "magick/decorate.h"
52 #include "magick/draw.h"
53 #include "magick/enhance.h"
54 #include "magick/exception.h"
55 #include "magick/exception-private.h"
56 #include "magick/effect.h"
57 #include "magick/fx.h"
58 #include "magick/gem.h"
59 #include "magick/geometry.h"
60 #include "magick/image-private.h"
61 #include "magick/list.h"
62 #include "magick/log.h"
63 #include "magick/memory_.h"
64 #include "magick/monitor.h"
65 #include "magick/monitor-private.h"
66 #include "magick/montage.h"
67 #include "magick/morphology.h"
68 #include "magick/paint.h"
69 #include "magick/pixel-private.h"
70 #include "magick/property.h"
71 #include "magick/quantize.h"
72 #include "magick/quantum.h"
73 #include "magick/random_.h"
74 #include "magick/random-private.h"
75 #include "magick/resample.h"
76 #include "magick/resample-private.h"
77 #include "magick/resize.h"
78 #include "magick/resource_.h"
79 #include "magick/segment.h"
80 #include "magick/shear.h"
81 #include "magick/signature-private.h"
82 #include "magick/string_.h"
83 #include "magick/thread-private.h"
84 #include "magick/transform.h"
85 #include "magick/threshold.h"
88 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92 % A d a p t i v e B l u r I m a g e %
96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98 % AdaptiveBlurImage() adaptively blurs the image by blurring less
99 % intensely near image edges and more intensely far from edges. We blur the
100 % image with a Gaussian operator of the given radius and standard deviation
101 % (sigma). For reasonable results, radius should be larger than sigma. Use a
102 % radius of 0 and AdaptiveBlurImage() selects a suitable radius for you.
104 % The format of the AdaptiveBlurImage method is:
106 % Image *AdaptiveBlurImage(const Image *image,const double radius,
107 % const double sigma,ExceptionInfo *exception)
108 % Image *AdaptiveBlurImageChannel(const Image *image,
109 % const ChannelType channel,double radius,const double sigma,
110 % ExceptionInfo *exception)
112 % A description of each parameter follows:
114 % o image: the image.
116 % o channel: the channel type.
118 % o radius: the radius of the Gaussian, in pixels, not counting the center
121 % o sigma: the standard deviation of the Laplacian, in pixels.
123 % o exception: return any errors or warnings in this structure.
127 MagickExport Image *AdaptiveBlurImage(const Image *image,const double radius,
128 const double sigma,ExceptionInfo *exception)
133 blur_image=AdaptiveBlurImageChannel(image,DefaultChannels,radius,sigma,
138 MagickExport Image *AdaptiveBlurImageChannel(const Image *image,
139 const ChannelType channel,const double radius,const double sigma,
140 ExceptionInfo *exception)
142 #define AdaptiveBlurImageTag "Convolve/Image"
143 #define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
181 assert(image != (const Image *) NULL);
182 assert(image->signature == MagickSignature);
183 if (image->debug != MagickFalse)
184 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
185 assert(exception != (ExceptionInfo *) NULL);
186 assert(exception->signature == MagickSignature);
187 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
188 if (blur_image == (Image *) NULL)
189 return((Image *) NULL);
190 if (fabs(sigma) <= MagickEpsilon)
192 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
194 InheritException(exception,&blur_image->exception);
195 blur_image=DestroyImage(blur_image);
196 return((Image *) NULL);
199 Edge detect the image brighness channel, level, blur, and level again.
201 edge_image=EdgeImage(image,radius,exception);
202 if (edge_image == (Image *) NULL)
204 blur_image=DestroyImage(blur_image);
205 return((Image *) NULL);
207 (void) LevelImage(edge_image,"20%,95%");
208 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
209 if (gaussian_image != (Image *) NULL)
211 edge_image=DestroyImage(edge_image);
212 edge_image=gaussian_image;
214 (void) LevelImage(edge_image,"10%,95%");
216 Create a set of kernels from maximum (radius,sigma) to minimum.
218 width=GetOptimalKernelWidth2D(radius,sigma);
219 kernel=(double **) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
220 if (kernel == (double **) NULL)
222 edge_image=DestroyImage(edge_image);
223 blur_image=DestroyImage(blur_image);
224 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
226 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
227 for (i=0; i < (ssize_t) width; i+=2)
229 kernel[i]=(double *) AcquireQuantumMemory((size_t) (width-i),(width-i)*
231 if (kernel[i] == (double *) NULL)
234 j=(ssize_t) (width-i)/2;
236 for (v=(-j); v <= j; v++)
238 for (u=(-j); u <= j; u++)
240 kernel[i][k]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
241 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
242 normalize+=kernel[i][k];
246 if (fabs(normalize) <= MagickEpsilon)
248 normalize=1.0/normalize;
249 for (k=0; k < (j*j); k++)
250 kernel[i][k]=normalize*kernel[i][k];
252 if (i < (ssize_t) width)
254 for (i-=2; i >= 0; i-=2)
255 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
256 kernel=(double **) RelinquishMagickMemory(kernel);
257 edge_image=DestroyImage(edge_image);
258 blur_image=DestroyImage(blur_image);
259 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
262 Adaptively blur image.
266 GetMagickPixelPacket(image,&bias);
267 SetMagickPixelPacketBias(image,&bias);
268 image_view=AcquireCacheView(image);
269 edge_view=AcquireCacheView(edge_image);
270 blur_view=AcquireCacheView(blur_image);
271 #if defined(MAGICKCORE_OPENMP_SUPPORT)
272 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
274 for (y=0; y < (ssize_t) blur_image->rows; y++)
276 register const IndexPacket
279 register const PixelPacket
284 *restrict blur_indexes;
292 if (status == MagickFalse)
294 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
295 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
297 if ((r == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
302 blur_indexes=GetCacheViewAuthenticIndexQueue(blur_view);
303 for (x=0; x < (ssize_t) blur_image->columns; x++)
312 register const double
321 i=(ssize_t) ceil((double) width*QuantumScale*PixelIntensity(r)-0.5);
325 if (i > (ssize_t) width)
329 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-i)/2L),y-
330 (ssize_t) ((width-i)/2L),width-i,width-i,exception);
331 if (p == (const PixelPacket *) NULL)
333 indexes=GetCacheViewVirtualIndexQueue(image_view);
336 for (v=0; v < (ssize_t) (width-i); v++)
338 for (u=0; u < (ssize_t) (width-i); u++)
341 if (((channel & OpacityChannel) != 0) &&
342 (image->matte != MagickFalse))
343 alpha=(MagickRealType) (QuantumScale*GetAlphaPixelComponent(p));
344 if ((channel & RedChannel) != 0)
345 pixel.red+=(*k)*alpha*GetRedPixelComponent(p);
346 if ((channel & GreenChannel) != 0)
347 pixel.green+=(*k)*alpha*GetGreenPixelComponent(p);
348 if ((channel & BlueChannel) != 0)
349 pixel.blue+=(*k)*alpha*GetBluePixelComponent(p);
350 if ((channel & OpacityChannel) != 0)
351 pixel.opacity+=(*k)*GetOpacityPixelComponent(p);
352 if (((channel & IndexChannel) != 0) &&
353 (image->colorspace == CMYKColorspace))
354 pixel.index+=(*k)*alpha*indexes[x+(width-i)*v+u];
360 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
361 if ((channel & RedChannel) != 0)
362 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
363 if ((channel & GreenChannel) != 0)
364 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
365 if ((channel & BlueChannel) != 0)
366 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
367 if ((channel & OpacityChannel) != 0)
368 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
369 if (((channel & IndexChannel) != 0) &&
370 (image->colorspace == CMYKColorspace))
371 blur_indexes[x]=ClampToQuantum(gamma*GetIndexPixelComponent(&pixel));
375 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
377 if (image->progress_monitor != (MagickProgressMonitor) NULL)
382 #if defined(MAGICKCORE_OPENMP_SUPPORT)
383 #pragma omp critical (MagickCore_AdaptiveBlurImageChannel)
385 proceed=SetImageProgress(image,AdaptiveBlurImageTag,progress++,
387 if (proceed == MagickFalse)
391 blur_image->type=image->type;
392 blur_view=DestroyCacheView(blur_view);
393 edge_view=DestroyCacheView(edge_view);
394 image_view=DestroyCacheView(image_view);
395 edge_image=DestroyImage(edge_image);
396 for (i=0; i < (ssize_t) width; i+=2)
397 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
398 kernel=(double **) RelinquishMagickMemory(kernel);
399 if (status == MagickFalse)
400 blur_image=DestroyImage(blur_image);
405 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
409 % A d a p t i v e S h a r p e n I m a g e %
413 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
415 % AdaptiveSharpenImage() adaptively sharpens the image by sharpening more
416 % intensely near image edges and less intensely far from edges. We sharpen the
417 % image with a Gaussian operator of the given radius and standard deviation
418 % (sigma). For reasonable results, radius should be larger than sigma. Use a
419 % radius of 0 and AdaptiveSharpenImage() selects a suitable radius for you.
421 % The format of the AdaptiveSharpenImage method is:
423 % Image *AdaptiveSharpenImage(const Image *image,const double radius,
424 % const double sigma,ExceptionInfo *exception)
425 % Image *AdaptiveSharpenImageChannel(const Image *image,
426 % const ChannelType channel,double radius,const double sigma,
427 % ExceptionInfo *exception)
429 % A description of each parameter follows:
431 % o image: the image.
433 % o channel: the channel type.
435 % o radius: the radius of the Gaussian, in pixels, not counting the center
438 % o sigma: the standard deviation of the Laplacian, in pixels.
440 % o exception: return any errors or warnings in this structure.
444 MagickExport Image *AdaptiveSharpenImage(const Image *image,const double radius,
445 const double sigma,ExceptionInfo *exception)
450 sharp_image=AdaptiveSharpenImageChannel(image,DefaultChannels,radius,sigma,
455 MagickExport Image *AdaptiveSharpenImageChannel(const Image *image,
456 const ChannelType channel,const double radius,const double sigma,
457 ExceptionInfo *exception)
459 #define AdaptiveSharpenImageTag "Convolve/Image"
460 #define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
498 assert(image != (const Image *) NULL);
499 assert(image->signature == MagickSignature);
500 if (image->debug != MagickFalse)
501 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
502 assert(exception != (ExceptionInfo *) NULL);
503 assert(exception->signature == MagickSignature);
504 sharp_image=CloneImage(image,0,0,MagickTrue,exception);
505 if (sharp_image == (Image *) NULL)
506 return((Image *) NULL);
507 if (fabs(sigma) <= MagickEpsilon)
509 if (SetImageStorageClass(sharp_image,DirectClass) == MagickFalse)
511 InheritException(exception,&sharp_image->exception);
512 sharp_image=DestroyImage(sharp_image);
513 return((Image *) NULL);
516 Edge detect the image brighness channel, level, sharp, and level again.
518 edge_image=EdgeImage(image,radius,exception);
519 if (edge_image == (Image *) NULL)
521 sharp_image=DestroyImage(sharp_image);
522 return((Image *) NULL);
524 (void) LevelImage(edge_image,"20%,95%");
525 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
526 if (gaussian_image != (Image *) NULL)
528 edge_image=DestroyImage(edge_image);
529 edge_image=gaussian_image;
531 (void) LevelImage(edge_image,"10%,95%");
533 Create a set of kernels from maximum (radius,sigma) to minimum.
535 width=GetOptimalKernelWidth2D(radius,sigma);
536 kernel=(double **) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
537 if (kernel == (double **) NULL)
539 edge_image=DestroyImage(edge_image);
540 sharp_image=DestroyImage(sharp_image);
541 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
543 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
544 for (i=0; i < (ssize_t) width; i+=2)
546 kernel[i]=(double *) AcquireQuantumMemory((size_t) (width-i),(width-i)*
548 if (kernel[i] == (double *) NULL)
551 j=(ssize_t) (width-i)/2;
553 for (v=(-j); v <= j; v++)
555 for (u=(-j); u <= j; u++)
557 kernel[i][k]=(double) (-exp(-((double) u*u+v*v)/(2.0*MagickSigma*
558 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
559 normalize+=kernel[i][k];
563 if (fabs(normalize) <= MagickEpsilon)
565 normalize=1.0/normalize;
566 for (k=0; k < (j*j); k++)
567 kernel[i][k]=normalize*kernel[i][k];
569 if (i < (ssize_t) width)
571 for (i-=2; i >= 0; i-=2)
572 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
573 kernel=(double **) RelinquishMagickMemory(kernel);
574 edge_image=DestroyImage(edge_image);
575 sharp_image=DestroyImage(sharp_image);
576 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
579 Adaptively sharpen image.
583 GetMagickPixelPacket(image,&bias);
584 SetMagickPixelPacketBias(image,&bias);
585 image_view=AcquireCacheView(image);
586 edge_view=AcquireCacheView(edge_image);
587 sharp_view=AcquireCacheView(sharp_image);
588 #if defined(MAGICKCORE_OPENMP_SUPPORT)
589 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
591 for (y=0; y < (ssize_t) sharp_image->rows; y++)
593 register const IndexPacket
596 register const PixelPacket
601 *restrict sharp_indexes;
609 if (status == MagickFalse)
611 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
612 q=QueueCacheViewAuthenticPixels(sharp_view,0,y,sharp_image->columns,1,
614 if ((r == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
619 sharp_indexes=GetCacheViewAuthenticIndexQueue(sharp_view);
620 for (x=0; x < (ssize_t) sharp_image->columns; x++)
629 register const double
638 i=(ssize_t) ceil((double) width*(QuantumRange-QuantumScale*
639 PixelIntensity(r))-0.5);
643 if (i > (ssize_t) width)
647 p=GetCacheViewVirtualPixels(image_view,x-((ssize_t) (width-i)/2L),y-
648 (ssize_t) ((width-i)/2L),width-i,width-i,exception);
649 if (p == (const PixelPacket *) NULL)
651 indexes=GetCacheViewVirtualIndexQueue(image_view);
654 for (v=0; v < (ssize_t) (width-i); v++)
656 for (u=0; u < (ssize_t) (width-i); u++)
659 if (((channel & OpacityChannel) != 0) &&
660 (image->matte != MagickFalse))
661 alpha=(MagickRealType) (QuantumScale*GetAlphaPixelComponent(p));
662 if ((channel & RedChannel) != 0)
663 pixel.red+=(*k)*alpha*GetRedPixelComponent(p);
664 if ((channel & GreenChannel) != 0)
665 pixel.green+=(*k)*alpha*GetGreenPixelComponent(p);
666 if ((channel & BlueChannel) != 0)
667 pixel.blue+=(*k)*alpha*GetBluePixelComponent(p);
668 if ((channel & OpacityChannel) != 0)
669 pixel.opacity+=(*k)*GetOpacityPixelComponent(p);
670 if (((channel & IndexChannel) != 0) &&
671 (image->colorspace == CMYKColorspace))
672 pixel.index+=(*k)*alpha*indexes[x+(width-i)*v+u];
678 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
679 if ((channel & RedChannel) != 0)
680 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
681 if ((channel & GreenChannel) != 0)
682 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
683 if ((channel & BlueChannel) != 0)
684 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
685 if ((channel & OpacityChannel) != 0)
686 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
687 if (((channel & IndexChannel) != 0) &&
688 (image->colorspace == CMYKColorspace))
689 sharp_indexes[x]=ClampToQuantum(gamma*GetIndexPixelComponent(&pixel));
693 if (SyncCacheViewAuthenticPixels(sharp_view,exception) == MagickFalse)
695 if (image->progress_monitor != (MagickProgressMonitor) NULL)
700 #if defined(MAGICKCORE_OPENMP_SUPPORT)
701 #pragma omp critical (MagickCore_AdaptiveSharpenImageChannel)
703 proceed=SetImageProgress(image,AdaptiveSharpenImageTag,progress++,
705 if (proceed == MagickFalse)
709 sharp_image->type=image->type;
710 sharp_view=DestroyCacheView(sharp_view);
711 edge_view=DestroyCacheView(edge_view);
712 image_view=DestroyCacheView(image_view);
713 edge_image=DestroyImage(edge_image);
714 for (i=0; i < (ssize_t) width; i+=2)
715 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
716 kernel=(double **) RelinquishMagickMemory(kernel);
717 if (status == MagickFalse)
718 sharp_image=DestroyImage(sharp_image);
723 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
727 % B l u r I m a g e %
731 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
733 % BlurImage() blurs an image. We convolve the image with a Gaussian operator
734 % of the given radius and standard deviation (sigma). For reasonable results,
735 % the radius should be larger than sigma. Use a radius of 0 and BlurImage()
736 % selects a suitable radius for you.
738 % BlurImage() differs from GaussianBlurImage() in that it uses a separable
739 % kernel which is faster but mathematically equivalent to the non-separable
742 % The format of the BlurImage method is:
744 % Image *BlurImage(const Image *image,const double radius,
745 % const double sigma,ExceptionInfo *exception)
746 % Image *BlurImageChannel(const Image *image,const ChannelType channel,
747 % const double radius,const double sigma,ExceptionInfo *exception)
749 % A description of each parameter follows:
751 % o image: the image.
753 % o channel: the channel type.
755 % o radius: the radius of the Gaussian, in pixels, not counting the center
758 % o sigma: the standard deviation of the Gaussian, in pixels.
760 % o exception: return any errors or warnings in this structure.
764 MagickExport Image *BlurImage(const Image *image,const double radius,
765 const double sigma,ExceptionInfo *exception)
770 blur_image=BlurImageChannel(image,DefaultChannels,radius,sigma,exception);
774 static double *GetBlurKernel(const size_t width,const double sigma)
788 Generate a 1-D convolution kernel.
790 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
791 kernel=(double *) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
792 if (kernel == (double *) NULL)
797 for (k=(-j); k <= j; k++)
799 kernel[i]=(double) (exp(-((double) k*k)/(2.0*MagickSigma*MagickSigma))/
800 (MagickSQ2PI*MagickSigma));
801 normalize+=kernel[i];
804 for (i=0; i < (ssize_t) width; i++)
805 kernel[i]/=normalize;
809 MagickExport Image *BlurImageChannel(const Image *image,
810 const ChannelType channel,const double radius,const double sigma,
811 ExceptionInfo *exception)
813 #define BlurImageTag "Blur/Image"
845 Initialize blur image attributes.
847 assert(image != (Image *) NULL);
848 assert(image->signature == MagickSignature);
849 if (image->debug != MagickFalse)
850 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
851 assert(exception != (ExceptionInfo *) NULL);
852 assert(exception->signature == MagickSignature);
853 blur_image=CloneImage(image,0,0,MagickTrue,exception);
854 if (blur_image == (Image *) NULL)
855 return((Image *) NULL);
856 if (fabs(sigma) <= MagickEpsilon)
858 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
860 InheritException(exception,&blur_image->exception);
861 blur_image=DestroyImage(blur_image);
862 return((Image *) NULL);
864 width=GetOptimalKernelWidth1D(radius,sigma);
865 kernel=GetBlurKernel(width,sigma);
866 if (kernel == (double *) NULL)
868 blur_image=DestroyImage(blur_image);
869 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
871 if (image->debug != MagickFalse)
874 format[MaxTextExtent],
877 register const double
880 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
881 " BlurImage with %.20g kernel:",(double) width);
882 message=AcquireString("");
884 for (i=0; i < (ssize_t) width; i++)
887 (void) FormatMagickString(format,MaxTextExtent,"%.20g: ",(double) i);
888 (void) ConcatenateString(&message,format);
889 (void) FormatMagickString(format,MaxTextExtent,"%g ",*k++);
890 (void) ConcatenateString(&message,format);
891 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
893 message=DestroyString(message);
900 GetMagickPixelPacket(image,&bias);
901 SetMagickPixelPacketBias(image,&bias);
902 image_view=AcquireCacheView(image);
903 blur_view=AcquireCacheView(blur_image);
904 #if defined(MAGICKCORE_OPENMP_SUPPORT)
905 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
907 for (y=0; y < (ssize_t) blur_image->rows; y++)
909 register const IndexPacket
912 register const PixelPacket
916 *restrict blur_indexes;
924 if (status == MagickFalse)
926 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y,
927 image->columns+width,1,exception);
928 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
930 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
935 indexes=GetCacheViewVirtualIndexQueue(image_view);
936 blur_indexes=GetCacheViewAuthenticIndexQueue(blur_view);
937 for (x=0; x < (ssize_t) blur_image->columns; x++)
942 register const double
945 register const PixelPacket
946 *restrict kernel_pixels;
954 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
956 for (i=0; i < (ssize_t) width; i++)
958 pixel.red+=(*k)*kernel_pixels->red;
959 pixel.green+=(*k)*kernel_pixels->green;
960 pixel.blue+=(*k)*kernel_pixels->blue;
964 if ((channel & RedChannel) != 0)
965 SetRedPixelComponent(q,ClampRedPixelComponent(&pixel));
966 if ((channel & GreenChannel) != 0)
967 SetGreenPixelComponent(q,ClampGreenPixelComponent(&pixel));
968 if ((channel & BlueChannel) != 0)
969 SetBluePixelComponent(q,ClampBluePixelComponent(&pixel));
970 if ((channel & OpacityChannel) != 0)
974 for (i=0; i < (ssize_t) width; i++)
976 pixel.opacity+=(*k)*kernel_pixels->opacity;
980 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
982 if (((channel & IndexChannel) != 0) &&
983 (image->colorspace == CMYKColorspace))
985 register const IndexPacket
986 *restrict kernel_indexes;
989 kernel_indexes=indexes;
990 for (i=0; i < (ssize_t) width; i++)
992 pixel.index+=(*k)*(*kernel_indexes);
996 blur_indexes[x]=ClampToQuantum(pixel.index);
1006 for (i=0; i < (ssize_t) width; i++)
1008 alpha=(MagickRealType) (QuantumScale*
1009 GetAlphaPixelComponent(kernel_pixels));
1010 pixel.red+=(*k)*alpha*kernel_pixels->red;
1011 pixel.green+=(*k)*alpha*kernel_pixels->green;
1012 pixel.blue+=(*k)*alpha*kernel_pixels->blue;
1017 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1018 if ((channel & RedChannel) != 0)
1019 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
1020 if ((channel & GreenChannel) != 0)
1021 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
1022 if ((channel & BlueChannel) != 0)
1023 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
1024 if ((channel & OpacityChannel) != 0)
1028 for (i=0; i < (ssize_t) width; i++)
1030 pixel.opacity+=(*k)*kernel_pixels->opacity;
1034 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
1036 if (((channel & IndexChannel) != 0) &&
1037 (image->colorspace == CMYKColorspace))
1039 register const IndexPacket
1040 *restrict kernel_indexes;
1044 kernel_indexes=indexes;
1045 for (i=0; i < (ssize_t) width; i++)
1047 alpha=(MagickRealType) (QuantumScale*
1048 GetAlphaPixelComponent(kernel_pixels));
1049 pixel.index+=(*k)*alpha*(*kernel_indexes);
1054 blur_indexes[x]=ClampToQuantum(gamma*
1055 GetIndexPixelComponent(&pixel));
1062 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1064 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1069 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1070 #pragma omp critical (MagickCore_BlurImageChannel)
1072 proceed=SetImageProgress(image,BlurImageTag,progress++,blur_image->rows+
1073 blur_image->columns);
1074 if (proceed == MagickFalse)
1078 blur_view=DestroyCacheView(blur_view);
1079 image_view=DestroyCacheView(image_view);
1083 image_view=AcquireCacheView(blur_image);
1084 blur_view=AcquireCacheView(blur_image);
1085 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1086 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1088 for (x=0; x < (ssize_t) blur_image->columns; x++)
1090 register const IndexPacket
1093 register const PixelPacket
1096 register IndexPacket
1097 *restrict blur_indexes;
1099 register PixelPacket
1105 if (status == MagickFalse)
1107 p=GetCacheViewVirtualPixels(image_view,x,-((ssize_t) width/2L),1,
1108 image->rows+width,exception);
1109 q=GetCacheViewAuthenticPixels(blur_view,x,0,1,blur_image->rows,exception);
1110 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
1115 indexes=GetCacheViewVirtualIndexQueue(image_view);
1116 blur_indexes=GetCacheViewAuthenticIndexQueue(blur_view);
1117 for (y=0; y < (ssize_t) blur_image->rows; y++)
1122 register const double
1125 register const PixelPacket
1126 *restrict kernel_pixels;
1134 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
1136 for (i=0; i < (ssize_t) width; i++)
1138 pixel.red+=(*k)*kernel_pixels->red;
1139 pixel.green+=(*k)*kernel_pixels->green;
1140 pixel.blue+=(*k)*kernel_pixels->blue;
1144 if ((channel & RedChannel) != 0)
1145 SetRedPixelComponent(q,ClampRedPixelComponent(&pixel));
1146 if ((channel & GreenChannel) != 0)
1147 SetGreenPixelComponent(q,ClampGreenPixelComponent(&pixel));
1148 if ((channel & BlueChannel) != 0)
1149 SetBluePixelComponent(q,ClampBluePixelComponent(&pixel));
1150 if ((channel & OpacityChannel) != 0)
1154 for (i=0; i < (ssize_t) width; i++)
1156 pixel.opacity+=(*k)*kernel_pixels->opacity;
1160 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
1162 if (((channel & IndexChannel) != 0) &&
1163 (image->colorspace == CMYKColorspace))
1165 register const IndexPacket
1166 *restrict kernel_indexes;
1169 kernel_indexes=indexes;
1170 for (i=0; i < (ssize_t) width; i++)
1172 pixel.index+=(*k)*(*kernel_indexes);
1176 blur_indexes[y]=ClampToQuantum(pixel.index);
1186 for (i=0; i < (ssize_t) width; i++)
1188 alpha=(MagickRealType) (QuantumScale*
1189 GetAlphaPixelComponent(kernel_pixels));
1190 pixel.red+=(*k)*alpha*kernel_pixels->red;
1191 pixel.green+=(*k)*alpha*kernel_pixels->green;
1192 pixel.blue+=(*k)*alpha*kernel_pixels->blue;
1197 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1198 if ((channel & RedChannel) != 0)
1199 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
1200 if ((channel & GreenChannel) != 0)
1201 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
1202 if ((channel & BlueChannel) != 0)
1203 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
1204 if ((channel & OpacityChannel) != 0)
1208 for (i=0; i < (ssize_t) width; i++)
1210 pixel.opacity+=(*k)*kernel_pixels->opacity;
1214 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
1216 if (((channel & IndexChannel) != 0) &&
1217 (image->colorspace == CMYKColorspace))
1219 register const IndexPacket
1220 *restrict kernel_indexes;
1224 kernel_indexes=indexes;
1225 for (i=0; i < (ssize_t) width; i++)
1227 alpha=(MagickRealType) (QuantumScale*
1228 GetAlphaPixelComponent(kernel_pixels));
1229 pixel.index+=(*k)*alpha*(*kernel_indexes);
1234 blur_indexes[y]=ClampToQuantum(gamma*
1235 GetIndexPixelComponent(&pixel));
1242 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1244 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1249 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1250 #pragma omp critical (MagickCore_BlurImageChannel)
1252 proceed=SetImageProgress(image,BlurImageTag,progress++,blur_image->rows+
1253 blur_image->columns);
1254 if (proceed == MagickFalse)
1258 blur_view=DestroyCacheView(blur_view);
1259 image_view=DestroyCacheView(image_view);
1260 kernel=(double *) RelinquishMagickMemory(kernel);
1261 if (status == MagickFalse)
1262 blur_image=DestroyImage(blur_image);
1263 blur_image->type=image->type;
1268 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1272 % C o n v o l v e I m a g e %
1276 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1278 % ConvolveImage() applies a custom convolution kernel to the image.
1280 % The format of the ConvolveImage method is:
1282 % Image *ConvolveImage(const Image *image,const size_t order,
1283 % const double *kernel,ExceptionInfo *exception)
1284 % Image *ConvolveImageChannel(const Image *image,const ChannelType channel,
1285 % const size_t order,const double *kernel,ExceptionInfo *exception)
1287 % A description of each parameter follows:
1289 % o image: the image.
1291 % o channel: the channel type.
1293 % o order: the number of columns and rows in the filter kernel.
1295 % o kernel: An array of double representing the convolution kernel.
1297 % o exception: return any errors or warnings in this structure.
1301 MagickExport Image *ConvolveImage(const Image *image,const size_t order,
1302 const double *kernel,ExceptionInfo *exception)
1307 convolve_image=ConvolveImageChannel(image,DefaultChannels,order,kernel,
1309 return(convolve_image);
1312 MagickExport Image *ConvolveImageChannel(const Image *image,
1313 const ChannelType channel,const size_t order,const double *kernel,
1314 ExceptionInfo *exception)
1316 #define ConvolveImageTag "Convolve/Image"
1350 Initialize convolve image attributes.
1352 assert(image != (Image *) NULL);
1353 assert(image->signature == MagickSignature);
1354 if (image->debug != MagickFalse)
1355 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1356 assert(exception != (ExceptionInfo *) NULL);
1357 assert(exception->signature == MagickSignature);
1359 if ((width % 2) == 0)
1360 ThrowImageException(OptionError,"KernelWidthMustBeAnOddNumber");
1361 convolve_image=CloneImage(image,0,0,MagickTrue,exception);
1362 if (convolve_image == (Image *) NULL)
1363 return((Image *) NULL);
1364 if (SetImageStorageClass(convolve_image,DirectClass) == MagickFalse)
1366 InheritException(exception,&convolve_image->exception);
1367 convolve_image=DestroyImage(convolve_image);
1368 return((Image *) NULL);
1370 if (image->debug != MagickFalse)
1373 format[MaxTextExtent],
1376 register const double
1383 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1384 " ConvolveImage with %.20gx%.20g kernel:",(double) width,(double)
1386 message=AcquireString("");
1388 for (v=0; v < (ssize_t) width; v++)
1391 (void) FormatMagickString(format,MaxTextExtent,"%.20g: ",(double) v);
1392 (void) ConcatenateString(&message,format);
1393 for (u=0; u < (ssize_t) width; u++)
1395 (void) FormatMagickString(format,MaxTextExtent,"%g ",*k++);
1396 (void) ConcatenateString(&message,format);
1398 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
1400 message=DestroyString(message);
1405 normal_kernel=(double *) AcquireQuantumMemory(width*width,
1406 sizeof(*normal_kernel));
1407 if (normal_kernel == (double *) NULL)
1409 convolve_image=DestroyImage(convolve_image);
1410 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1413 for (i=0; i < (ssize_t) (width*width); i++)
1415 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1416 for (i=0; i < (ssize_t) (width*width); i++)
1417 normal_kernel[i]=gamma*kernel[i];
1423 GetMagickPixelPacket(image,&bias);
1424 SetMagickPixelPacketBias(image,&bias);
1425 image_view=AcquireCacheView(image);
1426 convolve_view=AcquireCacheView(convolve_image);
1427 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1428 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1430 for (y=0; y < (ssize_t) image->rows; y++)
1435 register const IndexPacket
1438 register const PixelPacket
1441 register IndexPacket
1442 *restrict convolve_indexes;
1444 register PixelPacket
1450 if (status == MagickFalse)
1452 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
1453 (width/2L),image->columns+width,width,exception);
1454 q=GetCacheViewAuthenticPixels(convolve_view,0,y,convolve_image->columns,1,
1456 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
1461 indexes=GetCacheViewVirtualIndexQueue(image_view);
1462 convolve_indexes=GetCacheViewAuthenticIndexQueue(convolve_view);
1463 for (x=0; x < (ssize_t) image->columns; x++)
1468 register const double
1471 register const PixelPacket
1472 *restrict kernel_pixels;
1483 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
1485 for (v=0; v < (ssize_t) width; v++)
1487 for (u=0; u < (ssize_t) width; u++)
1489 pixel.red+=(*k)*kernel_pixels[u].red;
1490 pixel.green+=(*k)*kernel_pixels[u].green;
1491 pixel.blue+=(*k)*kernel_pixels[u].blue;
1494 kernel_pixels+=image->columns+width;
1496 if ((channel & RedChannel) != 0)
1497 SetRedPixelComponent(q,ClampRedPixelComponent(&pixel));
1498 if ((channel & GreenChannel) != 0)
1499 SetGreenPixelComponent(q,ClampGreenPixelComponent(&pixel));
1500 if ((channel & BlueChannel) != 0)
1501 SetBluePixelComponent(q,ClampBluePixelComponent(&pixel));
1502 if ((channel & OpacityChannel) != 0)
1506 for (v=0; v < (ssize_t) width; v++)
1508 for (u=0; u < (ssize_t) width; u++)
1510 pixel.opacity+=(*k)*kernel_pixels[u].opacity;
1513 kernel_pixels+=image->columns+width;
1515 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
1517 if (((channel & IndexChannel) != 0) &&
1518 (image->colorspace == CMYKColorspace))
1520 register const IndexPacket
1521 *restrict kernel_indexes;
1524 kernel_indexes=indexes;
1525 for (v=0; v < (ssize_t) width; v++)
1527 for (u=0; u < (ssize_t) width; u++)
1529 pixel.index+=(*k)*kernel_indexes[u];
1532 kernel_indexes+=image->columns+width;
1534 convolve_indexes[x]=ClampToQuantum(pixel.index);
1544 for (v=0; v < (ssize_t) width; v++)
1546 for (u=0; u < (ssize_t) width; u++)
1548 alpha=(MagickRealType) (QuantumScale*(QuantumRange-
1549 kernel_pixels[u].opacity));
1550 pixel.red+=(*k)*alpha*kernel_pixels[u].red;
1551 pixel.green+=(*k)*alpha*kernel_pixels[u].green;
1552 pixel.blue+=(*k)*alpha*kernel_pixels[u].blue;
1556 kernel_pixels+=image->columns+width;
1558 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1559 if ((channel & RedChannel) != 0)
1560 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
1561 if ((channel & GreenChannel) != 0)
1562 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
1563 if ((channel & BlueChannel) != 0)
1564 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
1565 if ((channel & OpacityChannel) != 0)
1569 for (v=0; v < (ssize_t) width; v++)
1571 for (u=0; u < (ssize_t) width; u++)
1573 pixel.opacity+=(*k)*kernel_pixels[u].opacity;
1576 kernel_pixels+=image->columns+width;
1578 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
1580 if (((channel & IndexChannel) != 0) &&
1581 (image->colorspace == CMYKColorspace))
1583 register const IndexPacket
1584 *restrict kernel_indexes;
1588 kernel_indexes=indexes;
1589 for (v=0; v < (ssize_t) width; v++)
1591 for (u=0; u < (ssize_t) width; u++)
1593 alpha=(MagickRealType) (QuantumScale*(QuantumRange-
1594 kernel_pixels[u].opacity));
1595 pixel.index+=(*k)*alpha*kernel_indexes[u];
1598 kernel_pixels+=image->columns+width;
1599 kernel_indexes+=image->columns+width;
1601 convolve_indexes[x]=ClampToQuantum(gamma*
1602 GetIndexPixelComponent(&pixel));
1609 sync=SyncCacheViewAuthenticPixels(convolve_view,exception);
1610 if (sync == MagickFalse)
1612 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1617 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1618 #pragma omp critical (MagickCore_ConvolveImageChannel)
1620 proceed=SetImageProgress(image,ConvolveImageTag,progress++,image->rows);
1621 if (proceed == MagickFalse)
1625 convolve_image->type=image->type;
1626 convolve_view=DestroyCacheView(convolve_view);
1627 image_view=DestroyCacheView(image_view);
1628 normal_kernel=(double *) RelinquishMagickMemory(normal_kernel);
1629 if (status == MagickFalse)
1630 convolve_image=DestroyImage(convolve_image);
1631 return(convolve_image);
1635 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1639 % D e s p e c k l e I m a g e %
1643 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1645 % DespeckleImage() reduces the speckle noise in an image while perserving the
1646 % edges of the original image.
1648 % The format of the DespeckleImage method is:
1650 % Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1652 % A description of each parameter follows:
1654 % o image: the image.
1656 % o exception: return any errors or warnings in this structure.
1660 static void Hull(const ssize_t x_offset,const ssize_t y_offset,
1661 const size_t columns,const size_t rows,Quantum *f,Quantum *g,
1679 assert(f != (Quantum *) NULL);
1680 assert(g != (Quantum *) NULL);
1683 r=p+(y_offset*((ssize_t) columns+2)+x_offset);
1684 for (y=0; y < (ssize_t) rows; y++)
1690 for (x=(ssize_t) columns; x != 0; x--)
1692 v=(MagickRealType) (*p);
1693 if ((MagickRealType) *r >= (v+(MagickRealType) ScaleCharToQuantum(2)))
1694 v+=ScaleCharToQuantum(1);
1701 for (x=(ssize_t) columns; x != 0; x--)
1703 v=(MagickRealType) (*p);
1704 if ((MagickRealType) *r <= (v-(MagickRealType) ScaleCharToQuantum(2)))
1705 v-=(ssize_t) ScaleCharToQuantum(1);
1717 r=q+(y_offset*((ssize_t) columns+2)+x_offset);
1718 s=q-(y_offset*((ssize_t) columns+2)+x_offset);
1719 for (y=0; y < (ssize_t) rows; y++)
1726 for (x=(ssize_t) columns; x != 0; x--)
1728 v=(MagickRealType) (*q);
1729 if (((MagickRealType) *s >=
1730 (v+(MagickRealType) ScaleCharToQuantum(2))) &&
1731 ((MagickRealType) *r > v))
1732 v+=ScaleCharToQuantum(1);
1740 for (x=(ssize_t) columns; x != 0; x--)
1742 v=(MagickRealType) (*q);
1743 if (((MagickRealType) *s <=
1744 (v-(MagickRealType) ScaleCharToQuantum(2))) &&
1745 ((MagickRealType) *r < v))
1746 v-=(MagickRealType) ScaleCharToQuantum(1);
1760 MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1762 #define DespeckleImageTag "Despeckle/Image"
1785 static const ssize_t
1786 X[4] = {0, 1, 1,-1},
1787 Y[4] = {1, 0, 1, 1};
1790 Allocate despeckled image.
1792 assert(image != (const Image *) NULL);
1793 assert(image->signature == MagickSignature);
1794 if (image->debug != MagickFalse)
1795 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1796 assert(exception != (ExceptionInfo *) NULL);
1797 assert(exception->signature == MagickSignature);
1798 despeckle_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1800 if (despeckle_image == (Image *) NULL)
1801 return((Image *) NULL);
1802 if (SetImageStorageClass(despeckle_image,DirectClass) == MagickFalse)
1804 InheritException(exception,&despeckle_image->exception);
1805 despeckle_image=DestroyImage(despeckle_image);
1806 return((Image *) NULL);
1809 Allocate image buffers.
1811 length=(size_t) ((image->columns+2)*(image->rows+2));
1812 pixels=(Quantum *) AcquireQuantumMemory(length,2*sizeof(*pixels));
1813 buffers=(Quantum *) AcquireQuantumMemory(length,2*sizeof(*pixels));
1814 if ((pixels == (Quantum *) NULL) || (buffers == (Quantum *) NULL))
1816 if (buffers != (Quantum *) NULL)
1817 buffers=(Quantum *) RelinquishMagickMemory(buffers);
1818 if (pixels != (Quantum *) NULL)
1819 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1820 despeckle_image=DestroyImage(despeckle_image);
1821 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1824 Reduce speckle in the image.
1827 number_channels=(size_t) (image->colorspace == CMYKColorspace ? 5 : 4);
1828 image_view=AcquireCacheView(image);
1829 despeckle_view=AcquireCacheView(despeckle_image);
1830 for (i=0; i < (ssize_t) number_channels; i++)
1844 if (status == MagickFalse)
1847 (void) ResetMagickMemory(pixel,0,length*sizeof(*pixel));
1849 j=(ssize_t) image->columns+2;
1850 for (y=0; y < (ssize_t) image->rows; y++)
1852 register const IndexPacket
1855 register const PixelPacket
1858 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1859 if (p == (const PixelPacket *) NULL)
1861 indexes=GetCacheViewVirtualIndexQueue(image_view);
1863 for (x=0; x < (ssize_t) image->columns; x++)
1867 case 0: pixel[j]=GetRedPixelComponent(p); break;
1868 case 1: pixel[j]=GetGreenPixelComponent(p); break;
1869 case 2: pixel[j]=GetBluePixelComponent(p); break;
1870 case 3: pixel[j]=GetOpacityPixelComponent(p); break;
1871 case 4: pixel[j]=GetBlackPixelComponent(indexes,x); break;
1879 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
1880 for (k=0; k < 4; k++)
1882 Hull(X[k],Y[k],image->columns,image->rows,pixel,buffer,1);
1883 Hull(-X[k],-Y[k],image->columns,image->rows,pixel,buffer,1);
1884 Hull(-X[k],-Y[k],image->columns,image->rows,pixel,buffer,-1);
1885 Hull(X[k],Y[k],image->columns,image->rows,pixel,buffer,-1);
1887 j=(ssize_t) image->columns+2;
1888 for (y=0; y < (ssize_t) image->rows; y++)
1893 register IndexPacket
1896 register PixelPacket
1899 q=GetCacheViewAuthenticPixels(despeckle_view,0,y,despeckle_image->columns,
1901 if (q == (PixelPacket *) NULL)
1903 indexes=GetCacheViewAuthenticIndexQueue(image_view);
1905 for (x=0; x < (ssize_t) image->columns; x++)
1909 case 0: q->red=pixel[j]; break;
1910 case 1: q->green=pixel[j]; break;
1911 case 2: q->blue=pixel[j]; break;
1912 case 3: q->opacity=pixel[j]; break;
1913 case 4: indexes[x]=pixel[j]; break;
1919 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1920 if (sync == MagickFalse)
1927 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1932 proceed=SetImageProgress(image,DespeckleImageTag,(MagickOffsetType) i,
1934 if (proceed == MagickFalse)
1938 despeckle_view=DestroyCacheView(despeckle_view);
1939 image_view=DestroyCacheView(image_view);
1940 buffers=(Quantum *) RelinquishMagickMemory(buffers);
1941 pixels=(Quantum *) RelinquishMagickMemory(pixels);
1942 despeckle_image->type=image->type;
1943 if (status == MagickFalse)
1944 despeckle_image=DestroyImage(despeckle_image);
1945 return(despeckle_image);
1949 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1953 % E d g e I m a g e %
1957 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1959 % EdgeImage() finds edges in an image. Radius defines the radius of the
1960 % convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1963 % The format of the EdgeImage method is:
1965 % Image *EdgeImage(const Image *image,const double radius,
1966 % ExceptionInfo *exception)
1968 % A description of each parameter follows:
1970 % o image: the image.
1972 % o radius: the radius of the pixel neighborhood.
1974 % o exception: return any errors or warnings in this structure.
1977 MagickExport Image *EdgeImage(const Image *image,const double radius,
1978 ExceptionInfo *exception)
1992 assert(image != (const 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 assert(exception->signature == MagickSignature);
1998 width=GetOptimalKernelWidth1D(radius,0.5);
1999 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
2000 if (kernel == (double *) NULL)
2001 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2002 for (i=0; i < (ssize_t) (width*width); i++)
2004 kernel[i/2]=(double) (width*width-1.0);
2005 edge_image=ConvolveImage(image,width,kernel,exception);
2006 kernel=(double *) RelinquishMagickMemory(kernel);
2011 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2015 % E m b o s s I m a g e %
2019 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2021 % EmbossImage() returns a grayscale image with a three-dimensional effect.
2022 % We convolve the image with a Gaussian operator of the given radius and
2023 % standard deviation (sigma). For reasonable results, radius should be
2024 % larger than sigma. Use a radius of 0 and Emboss() selects a suitable
2027 % The format of the EmbossImage method is:
2029 % Image *EmbossImage(const Image *image,const double radius,
2030 % const double sigma,ExceptionInfo *exception)
2032 % A description of each parameter follows:
2034 % o image: the image.
2036 % o radius: the radius of the pixel neighborhood.
2038 % o sigma: the standard deviation of the Gaussian, in pixels.
2040 % o exception: return any errors or warnings in this structure.
2043 MagickExport Image *EmbossImage(const Image *image,const double radius,
2044 const double sigma,ExceptionInfo *exception)
2064 assert(image != (Image *) NULL);
2065 assert(image->signature == MagickSignature);
2066 if (image->debug != MagickFalse)
2067 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2068 assert(exception != (ExceptionInfo *) NULL);
2069 assert(exception->signature == MagickSignature);
2070 width=GetOptimalKernelWidth2D(radius,sigma);
2071 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
2072 if (kernel == (double *) NULL)
2073 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2074 j=(ssize_t) width/2;
2077 for (v=(-j); v <= j; v++)
2079 for (u=(-j); u <= j; u++)
2081 kernel[i]=(double) (((u < 0) || (v < 0) ? -8.0 : 8.0)*
2082 exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
2083 (2.0*MagickPI*MagickSigma*MagickSigma));
2090 emboss_image=ConvolveImage(image,width,kernel,exception);
2091 if (emboss_image != (Image *) NULL)
2092 (void) EqualizeImage(emboss_image);
2093 kernel=(double *) RelinquishMagickMemory(kernel);
2094 return(emboss_image);
2098 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2102 % F i l t e r I m a g e %
2106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2108 % FilterImage() applies a custom convolution kernel to the image.
2110 % The format of the FilterImage method is:
2112 % Image *FilterImage(const Image *image,const KernelInfo *kernel,
2113 % ExceptionInfo *exception)
2114 % Image *FilterImageChannel(const Image *image,const ChannelType channel,
2115 % const KernelInfo *kernel,ExceptionInfo *exception)
2117 % A description of each parameter follows:
2119 % o image: the image.
2121 % o channel: the channel type.
2123 % o kernel: the filtering kernel.
2125 % o exception: return any errors or warnings in this structure.
2129 MagickExport Image *FilterImage(const Image *image,const KernelInfo *kernel,
2130 ExceptionInfo *exception)
2135 filter_image=FilterImageChannel(image,DefaultChannels,kernel,exception);
2136 return(filter_image);
2139 MagickExport Image *FilterImageChannel(const Image *image,
2140 const ChannelType channel,const KernelInfo *kernel,ExceptionInfo *exception)
2142 #define FilterImageTag "Filter/Image"
2164 Initialize filter image attributes.
2166 assert(image != (Image *) NULL);
2167 assert(image->signature == MagickSignature);
2168 if (image->debug != MagickFalse)
2169 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2170 assert(exception != (ExceptionInfo *) NULL);
2171 assert(exception->signature == MagickSignature);
2172 if ((kernel->width % 2) == 0)
2173 ThrowImageException(OptionError,"KernelWidthMustBeAnOddNumber");
2174 filter_image=CloneImage(image,0,0,MagickTrue,exception);
2175 if (filter_image == (Image *) NULL)
2176 return((Image *) NULL);
2177 if (SetImageStorageClass(filter_image,DirectClass) == MagickFalse)
2179 InheritException(exception,&filter_image->exception);
2180 filter_image=DestroyImage(filter_image);
2181 return((Image *) NULL);
2183 if (image->debug != MagickFalse)
2186 format[MaxTextExtent],
2189 register const double
2196 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
2197 " FilterImage with %.20gx%.20g kernel:",(double) kernel->width,(double)
2199 message=AcquireString("");
2201 for (v=0; v < (ssize_t) kernel->height; v++)
2204 (void) FormatMagickString(format,MaxTextExtent,"%.20g: ",(double) v);
2205 (void) ConcatenateString(&message,format);
2206 for (u=0; u < (ssize_t) kernel->width; u++)
2208 (void) FormatMagickString(format,MaxTextExtent,"%g ",*k++);
2209 (void) ConcatenateString(&message,format);
2211 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
2213 message=DestroyString(message);
2215 status=AccelerateConvolveImage(image,kernel,filter_image,exception);
2216 if (status == MagickTrue)
2217 return(filter_image);
2223 GetMagickPixelPacket(image,&bias);
2224 SetMagickPixelPacketBias(image,&bias);
2225 image_view=AcquireCacheView(image);
2226 filter_view=AcquireCacheView(filter_image);
2227 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2228 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2230 for (y=0; y < (ssize_t) image->rows; y++)
2235 register const IndexPacket
2238 register const PixelPacket
2241 register IndexPacket
2242 *restrict filter_indexes;
2244 register PixelPacket
2250 if (status == MagickFalse)
2252 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) kernel->width/2L),
2253 y-(ssize_t) (kernel->height/2L),image->columns+kernel->width,
2254 kernel->height,exception);
2255 q=GetCacheViewAuthenticPixels(filter_view,0,y,filter_image->columns,1,
2257 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
2262 indexes=GetCacheViewVirtualIndexQueue(image_view);
2263 filter_indexes=GetCacheViewAuthenticIndexQueue(filter_view);
2264 for (x=0; x < (ssize_t) image->columns; x++)
2269 register const double
2272 register const PixelPacket
2273 *restrict kernel_pixels;
2284 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
2286 for (v=0; v < (ssize_t) kernel->width; v++)
2288 for (u=0; u < (ssize_t) kernel->height; u++)
2290 pixel.red+=(*k)*kernel_pixels[u].red;
2291 pixel.green+=(*k)*kernel_pixels[u].green;
2292 pixel.blue+=(*k)*kernel_pixels[u].blue;
2295 kernel_pixels+=image->columns+kernel->width;
2297 if ((channel & RedChannel) != 0)
2298 SetRedPixelComponent(q,ClampRedPixelComponent(&pixel));
2299 if ((channel & GreenChannel) != 0)
2300 SetGreenPixelComponent(q,ClampGreenPixelComponent(&pixel));
2301 if ((channel & BlueChannel) != 0)
2302 SetBluePixelComponent(q,ClampBluePixelComponent(&pixel));
2303 if ((channel & OpacityChannel) != 0)
2307 for (v=0; v < (ssize_t) kernel->width; v++)
2309 for (u=0; u < (ssize_t) kernel->height; u++)
2311 pixel.opacity+=(*k)*kernel_pixels[u].opacity;
2314 kernel_pixels+=image->columns+kernel->width;
2316 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
2318 if (((channel & IndexChannel) != 0) &&
2319 (image->colorspace == CMYKColorspace))
2321 register const IndexPacket
2322 *restrict kernel_indexes;
2325 kernel_indexes=indexes;
2326 for (v=0; v < (ssize_t) kernel->width; v++)
2328 for (u=0; u < (ssize_t) kernel->height; u++)
2330 pixel.index+=(*k)*kernel_indexes[u];
2333 kernel_indexes+=image->columns+kernel->width;
2335 filter_indexes[x]=ClampToQuantum(pixel.index);
2345 for (v=0; v < (ssize_t) kernel->width; v++)
2347 for (u=0; u < (ssize_t) kernel->height; u++)
2349 alpha=(MagickRealType) (QuantumScale*(QuantumRange-
2350 kernel_pixels[u].opacity));
2351 pixel.red+=(*k)*alpha*kernel_pixels[u].red;
2352 pixel.green+=(*k)*alpha*kernel_pixels[u].green;
2353 pixel.blue+=(*k)*alpha*kernel_pixels[u].blue;
2357 kernel_pixels+=image->columns+kernel->width;
2359 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
2360 if ((channel & RedChannel) != 0)
2361 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
2362 if ((channel & GreenChannel) != 0)
2363 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
2364 if ((channel & BlueChannel) != 0)
2365 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
2366 if ((channel & OpacityChannel) != 0)
2370 for (v=0; v < (ssize_t) kernel->width; v++)
2372 for (u=0; u < (ssize_t) kernel->height; u++)
2374 pixel.opacity+=(*k)*kernel_pixels[u].opacity;
2377 kernel_pixels+=image->columns+kernel->width;
2379 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
2381 if (((channel & IndexChannel) != 0) &&
2382 (image->colorspace == CMYKColorspace))
2384 register const IndexPacket
2385 *restrict kernel_indexes;
2389 kernel_indexes=indexes;
2390 for (v=0; v < (ssize_t) kernel->width; v++)
2392 for (u=0; u < (ssize_t) kernel->height; u++)
2394 alpha=(MagickRealType) (QuantumScale*(QuantumRange-
2395 kernel_pixels[u].opacity));
2396 pixel.index+=(*k)*alpha*kernel_indexes[u];
2399 kernel_pixels+=image->columns+kernel->width;
2400 kernel_indexes+=image->columns+kernel->width;
2402 filter_indexes[x]=ClampToQuantum(gamma*
2403 GetIndexPixelComponent(&pixel));
2410 sync=SyncCacheViewAuthenticPixels(filter_view,exception);
2411 if (sync == MagickFalse)
2413 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2418 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2419 #pragma omp critical (MagickCore_FilterImageChannel)
2421 proceed=SetImageProgress(image,FilterImageTag,progress++,image->rows);
2422 if (proceed == MagickFalse)
2426 filter_image->type=image->type;
2427 filter_view=DestroyCacheView(filter_view);
2428 image_view=DestroyCacheView(image_view);
2429 if (status == MagickFalse)
2430 filter_image=DestroyImage(filter_image);
2431 return(filter_image);
2435 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2439 % G a u s s i a n B l u r I m a g e %
2443 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2445 % GaussianBlurImage() blurs an image. We convolve the image with a
2446 % Gaussian operator of the given radius and standard deviation (sigma).
2447 % For reasonable results, the radius should be larger than sigma. Use a
2448 % radius of 0 and GaussianBlurImage() selects a suitable radius for you
2450 % The format of the GaussianBlurImage method is:
2452 % Image *GaussianBlurImage(const Image *image,onst double radius,
2453 % const double sigma,ExceptionInfo *exception)
2454 % Image *GaussianBlurImageChannel(const Image *image,
2455 % const ChannelType channel,const double radius,const double sigma,
2456 % ExceptionInfo *exception)
2458 % A description of each parameter follows:
2460 % o image: the image.
2462 % o channel: the channel type.
2464 % o radius: the radius of the Gaussian, in pixels, not counting the center
2467 % o sigma: the standard deviation of the Gaussian, in pixels.
2469 % o exception: return any errors or warnings in this structure.
2473 MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
2474 const double sigma,ExceptionInfo *exception)
2479 blur_image=GaussianBlurImageChannel(image,DefaultChannels,radius,sigma,
2484 MagickExport Image *GaussianBlurImageChannel(const Image *image,
2485 const ChannelType channel,const double radius,const double sigma,
2486 ExceptionInfo *exception)
2505 assert(image != (const Image *) NULL);
2506 assert(image->signature == MagickSignature);
2507 if (image->debug != MagickFalse)
2508 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2509 assert(exception != (ExceptionInfo *) NULL);
2510 assert(exception->signature == MagickSignature);
2511 width=GetOptimalKernelWidth2D(radius,sigma);
2512 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
2513 if (kernel == (double *) NULL)
2514 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2515 j=(ssize_t) width/2;
2517 for (v=(-j); v <= j; v++)
2519 for (u=(-j); u <= j; u++)
2520 kernel[i++]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
2521 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
2523 blur_image=ConvolveImageChannel(image,channel,width,kernel,exception);
2524 kernel=(double *) RelinquishMagickMemory(kernel);
2529 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2533 % M o t i o n B l u r I m a g e %
2537 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2539 % MotionBlurImage() simulates motion blur. We convolve the image with a
2540 % Gaussian operator of the given radius and standard deviation (sigma).
2541 % For reasonable results, radius should be larger than sigma. Use a
2542 % radius of 0 and MotionBlurImage() selects a suitable radius for you.
2543 % Angle gives the angle of the blurring motion.
2545 % Andrew Protano contributed this effect.
2547 % The format of the MotionBlurImage method is:
2549 % Image *MotionBlurImage(const Image *image,const double radius,
2550 % const double sigma,const double angle,ExceptionInfo *exception)
2551 % Image *MotionBlurImageChannel(const Image *image,const ChannelType channel,
2552 % const double radius,const double sigma,const double angle,
2553 % ExceptionInfo *exception)
2555 % A description of each parameter follows:
2557 % o image: the image.
2559 % o channel: the channel type.
2561 % o radius: the radius of the Gaussian, in pixels, not counting the center
2562 % o radius: the radius of the Gaussian, in pixels, not counting
2565 % o sigma: the standard deviation of the Gaussian, in pixels.
2567 % o angle: Apply the effect along this angle.
2569 % o exception: return any errors or warnings in this structure.
2573 static double *GetMotionBlurKernel(const size_t width,const double sigma)
2583 Generate a 1-D convolution kernel.
2585 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2586 kernel=(double *) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
2587 if (kernel == (double *) NULL)
2590 for (i=0; i < (ssize_t) width; i++)
2592 kernel[i]=(double) (exp((-((double) i*i)/(double) (2.0*MagickSigma*
2593 MagickSigma)))/(MagickSQ2PI*MagickSigma));
2594 normalize+=kernel[i];
2596 for (i=0; i < (ssize_t) width; i++)
2597 kernel[i]/=normalize;
2601 MagickExport Image *MotionBlurImage(const Image *image,const double radius,
2602 const double sigma,const double angle,ExceptionInfo *exception)
2607 motion_blur=MotionBlurImageChannel(image,DefaultChannels,radius,sigma,angle,
2609 return(motion_blur);
2612 MagickExport Image *MotionBlurImageChannel(const Image *image,
2613 const ChannelType channel,const double radius,const double sigma,
2614 const double angle,ExceptionInfo *exception)
2650 assert(image != (Image *) NULL);
2651 assert(image->signature == MagickSignature);
2652 if (image->debug != MagickFalse)
2653 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2654 assert(exception != (ExceptionInfo *) NULL);
2655 width=GetOptimalKernelWidth1D(radius,sigma);
2656 kernel=GetMotionBlurKernel(width,sigma);
2657 if (kernel == (double *) NULL)
2658 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2659 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
2660 if (offset == (OffsetInfo *) NULL)
2662 kernel=(double *) RelinquishMagickMemory(kernel);
2663 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2665 blur_image=CloneImage(image,0,0,MagickTrue,exception);
2666 if (blur_image == (Image *) NULL)
2668 kernel=(double *) RelinquishMagickMemory(kernel);
2669 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2670 return((Image *) NULL);
2672 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
2674 kernel=(double *) RelinquishMagickMemory(kernel);
2675 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2676 InheritException(exception,&blur_image->exception);
2677 blur_image=DestroyImage(blur_image);
2678 return((Image *) NULL);
2680 point.x=(double) width*sin(DegreesToRadians(angle));
2681 point.y=(double) width*cos(DegreesToRadians(angle));
2682 for (i=0; i < (ssize_t) width; i++)
2684 offset[i].x=(ssize_t) ceil((double) (i*point.y)/hypot(point.x,point.y)-0.5);
2685 offset[i].y=(ssize_t) ceil((double) (i*point.x)/hypot(point.x,point.y)-0.5);
2692 GetMagickPixelPacket(image,&bias);
2693 image_view=AcquireCacheView(image);
2694 blur_view=AcquireCacheView(blur_image);
2695 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2696 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
2698 for (y=0; y < (ssize_t) image->rows; y++)
2700 register IndexPacket
2701 *restrict blur_indexes;
2703 register PixelPacket
2709 if (status == MagickFalse)
2711 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
2713 if (q == (PixelPacket *) NULL)
2718 blur_indexes=GetCacheViewAuthenticIndexQueue(blur_view);
2719 for (x=0; x < (ssize_t) image->columns; x++)
2727 register const IndexPacket
2738 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
2740 for (i=0; i < (ssize_t) width; i++)
2742 (void) GetOneCacheViewVirtualPixel(image_view,x+offset[i].x,y+
2743 offset[i].y,&pixel,exception);
2744 qixel.red+=(*k)*pixel.red;
2745 qixel.green+=(*k)*pixel.green;
2746 qixel.blue+=(*k)*pixel.blue;
2747 qixel.opacity+=(*k)*pixel.opacity;
2748 if (image->colorspace == CMYKColorspace)
2750 indexes=GetCacheViewVirtualIndexQueue(image_view);
2751 qixel.index+=(*k)*(*indexes);
2755 if ((channel & RedChannel) != 0)
2756 q->red=ClampToQuantum(qixel.red);
2757 if ((channel & GreenChannel) != 0)
2758 q->green=ClampToQuantum(qixel.green);
2759 if ((channel & BlueChannel) != 0)
2760 q->blue=ClampToQuantum(qixel.blue);
2761 if ((channel & OpacityChannel) != 0)
2762 q->opacity=ClampToQuantum(qixel.opacity);
2763 if (((channel & IndexChannel) != 0) &&
2764 (image->colorspace == CMYKColorspace))
2765 blur_indexes[x]=(IndexPacket) ClampToQuantum(qixel.index);
2775 for (i=0; i < (ssize_t) width; i++)
2777 (void) GetOneCacheViewVirtualPixel(image_view,x+offset[i].x,y+
2778 offset[i].y,&pixel,exception);
2779 alpha=(MagickRealType) (QuantumScale*
2780 GetAlphaPixelComponent(&pixel));
2781 qixel.red+=(*k)*alpha*pixel.red;
2782 qixel.green+=(*k)*alpha*pixel.green;
2783 qixel.blue+=(*k)*alpha*pixel.blue;
2784 qixel.opacity+=(*k)*pixel.opacity;
2785 if (image->colorspace == CMYKColorspace)
2787 indexes=GetCacheViewVirtualIndexQueue(image_view);
2788 qixel.index+=(*k)*alpha*(*indexes);
2793 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
2794 if ((channel & RedChannel) != 0)
2795 q->red=ClampToQuantum(gamma*qixel.red);
2796 if ((channel & GreenChannel) != 0)
2797 q->green=ClampToQuantum(gamma*qixel.green);
2798 if ((channel & BlueChannel) != 0)
2799 q->blue=ClampToQuantum(gamma*qixel.blue);
2800 if ((channel & OpacityChannel) != 0)
2801 q->opacity=ClampToQuantum(qixel.opacity);
2802 if (((channel & IndexChannel) != 0) &&
2803 (image->colorspace == CMYKColorspace))
2804 blur_indexes[x]=(IndexPacket) ClampToQuantum(gamma*qixel.index);
2808 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
2810 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2815 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2816 #pragma omp critical (MagickCore_MotionBlurImageChannel)
2818 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
2819 if (proceed == MagickFalse)
2823 blur_view=DestroyCacheView(blur_view);
2824 image_view=DestroyCacheView(image_view);
2825 kernel=(double *) RelinquishMagickMemory(kernel);
2826 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
2827 if (status == MagickFalse)
2828 blur_image=DestroyImage(blur_image);
2833 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2837 % P r e v i e w I m a g e %
2841 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2843 % PreviewImage() tiles 9 thumbnails of the specified image with an image
2844 % processing operation applied with varying parameters. This may be helpful
2845 % pin-pointing an appropriate parameter for a particular image processing
2848 % The format of the PreviewImages method is:
2850 % Image *PreviewImages(const Image *image,const PreviewType preview,
2851 % ExceptionInfo *exception)
2853 % A description of each parameter follows:
2855 % o image: the image.
2857 % o preview: the image processing operation.
2859 % o exception: return any errors or warnings in this structure.
2862 MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
2863 ExceptionInfo *exception)
2865 #define NumberTiles 9
2866 #define PreviewImageTag "Preview/Image"
2867 #define DefaultPreviewGeometry "204x204+10+10"
2870 factor[MaxTextExtent],
2871 label[MaxTextExtent];
2913 Open output image file.
2915 assert(image != (Image *) NULL);
2916 assert(image->signature == MagickSignature);
2917 if (image->debug != MagickFalse)
2918 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2922 preview_info=AcquireImageInfo();
2923 SetGeometry(image,&geometry);
2924 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
2925 &geometry.width,&geometry.height);
2926 images=NewImageList();
2928 GetQuantizeInfo(&quantize_info);
2934 for (i=0; i < NumberTiles; i++)
2936 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
2937 if (thumbnail == (Image *) NULL)
2939 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
2941 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel);
2942 if (i == (NumberTiles/2))
2944 (void) QueryColorDatabase("#dfdfdf",&thumbnail->matte_color,exception);
2945 AppendImageToList(&images,thumbnail);
2953 preview_image=RotateImage(thumbnail,degrees,exception);
2954 (void) FormatMagickString(label,MaxTextExtent,"rotate %g",degrees);
2960 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
2961 (void) FormatMagickString(label,MaxTextExtent,"shear %gx%g",
2962 degrees,2.0*degrees);
2967 x=(ssize_t) ((i+1)*thumbnail->columns)/NumberTiles;
2968 y=(ssize_t) ((i+1)*thumbnail->rows)/NumberTiles;
2969 preview_image=RollImage(thumbnail,x,y,exception);
2970 (void) FormatMagickString(label,MaxTextExtent,"roll %+.20gx%+.20g",
2971 (double) x,(double) y);
2976 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2977 if (preview_image == (Image *) NULL)
2979 (void) FormatMagickString(factor,MaxTextExtent,"100,100,%g",
2981 (void) ModulateImage(preview_image,factor);
2982 (void) FormatMagickString(label,MaxTextExtent,"modulate %s",factor);
2985 case SaturationPreview:
2987 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2988 if (preview_image == (Image *) NULL)
2990 (void) FormatMagickString(factor,MaxTextExtent,"100,%g",
2992 (void) ModulateImage(preview_image,factor);
2993 (void) FormatMagickString(label,MaxTextExtent,"modulate %s",factor);
2996 case BrightnessPreview:
2998 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
2999 if (preview_image == (Image *) NULL)
3001 (void) FormatMagickString(factor,MaxTextExtent,"%g",2.0*percentage);
3002 (void) ModulateImage(preview_image,factor);
3003 (void) FormatMagickString(label,MaxTextExtent,"modulate %s",factor);
3009 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3010 if (preview_image == (Image *) NULL)
3013 (void) GammaImageChannel(preview_image,DefaultChannels,gamma);
3014 (void) FormatMagickString(label,MaxTextExtent,"gamma %g",gamma);
3019 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3020 if (preview_image != (Image *) NULL)
3021 for (x=0; x < i; x++)
3022 (void) ContrastImage(preview_image,MagickTrue);
3023 (void) FormatMagickString(label,MaxTextExtent,"contrast (%.20g)",
3029 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3030 if (preview_image == (Image *) NULL)
3032 for (x=0; x < i; x++)
3033 (void) ContrastImage(preview_image,MagickFalse);
3034 (void) FormatMagickString(label,MaxTextExtent,"+contrast (%.20g)",
3038 case GrayscalePreview:
3040 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3041 if (preview_image == (Image *) NULL)
3044 quantize_info.number_colors=colors;
3045 quantize_info.colorspace=GRAYColorspace;
3046 (void) QuantizeImage(&quantize_info,preview_image);
3047 (void) FormatMagickString(label,MaxTextExtent,
3048 "-colorspace gray -colors %.20g",(double) colors);
3051 case QuantizePreview:
3053 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3054 if (preview_image == (Image *) NULL)
3057 quantize_info.number_colors=colors;
3058 (void) QuantizeImage(&quantize_info,preview_image);
3059 (void) FormatMagickString(label,MaxTextExtent,"colors %.20g",(double)
3063 case DespecklePreview:
3065 for (x=0; x < (i-1); x++)
3067 preview_image=DespeckleImage(thumbnail,exception);
3068 if (preview_image == (Image *) NULL)
3070 thumbnail=DestroyImage(thumbnail);
3071 thumbnail=preview_image;
3073 preview_image=DespeckleImage(thumbnail,exception);
3074 if (preview_image == (Image *) NULL)
3076 (void) FormatMagickString(label,MaxTextExtent,"despeckle (%.20g)",
3080 case ReduceNoisePreview:
3082 preview_image=StatisticImage(thumbnail,NonpeakStatistic,(size_t) radius,
3083 (size_t) radius,exception);
3084 (void) FormatMagickString(label,MaxTextExtent,"noise %g",radius);
3087 case AddNoisePreview:
3093 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
3098 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
3103 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
3108 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
3113 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
3118 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
3123 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
3127 preview_image=StatisticImage(thumbnail,NonpeakStatistic,i,i,
3129 (void) FormatMagickString(label,MaxTextExtent,"+noise %s",factor);
3132 case SharpenPreview:
3134 preview_image=SharpenImage(thumbnail,radius,sigma,exception);
3135 (void) FormatMagickString(label,MaxTextExtent,"sharpen %gx%g",
3141 preview_image=BlurImage(thumbnail,radius,sigma,exception);
3142 (void) FormatMagickString(label,MaxTextExtent,"blur %gx%g",radius,
3146 case ThresholdPreview:
3148 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3149 if (preview_image == (Image *) NULL)
3151 (void) BilevelImage(thumbnail,
3152 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
3153 (void) FormatMagickString(label,MaxTextExtent,"threshold %g",
3154 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
3157 case EdgeDetectPreview:
3159 preview_image=EdgeImage(thumbnail,radius,exception);
3160 (void) FormatMagickString(label,MaxTextExtent,"edge %g",radius);
3165 preview_image=SpreadImage(thumbnail,radius,exception);
3166 (void) FormatMagickString(label,MaxTextExtent,"spread %g",
3170 case SolarizePreview:
3172 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3173 if (preview_image == (Image *) NULL)
3175 (void) SolarizeImage(preview_image,(double) QuantumRange*
3177 (void) FormatMagickString(label,MaxTextExtent,"solarize %g",
3178 (QuantumRange*percentage)/100.0);
3184 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
3186 (void) FormatMagickString(label,MaxTextExtent,"shade %gx%g",
3192 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3193 if (preview_image == (Image *) NULL)
3195 geometry.width=(size_t) (2*i+2);
3196 geometry.height=(size_t) (2*i+2);
3199 (void) RaiseImage(preview_image,&geometry,MagickTrue);
3200 (void) FormatMagickString(label,MaxTextExtent,
3201 "raise %.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
3202 geometry.height,(double) geometry.x,(double) geometry.y);
3205 case SegmentPreview:
3207 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3208 if (preview_image == (Image *) NULL)
3211 (void) SegmentImage(preview_image,RGBColorspace,MagickFalse,threshold,
3213 (void) FormatMagickString(label,MaxTextExtent,"segment %gx%g",
3214 threshold,threshold);
3219 preview_image=SwirlImage(thumbnail,degrees,exception);
3220 (void) FormatMagickString(label,MaxTextExtent,"swirl %g",degrees);
3224 case ImplodePreview:
3227 preview_image=ImplodeImage(thumbnail,degrees,exception);
3228 (void) FormatMagickString(label,MaxTextExtent,"implode %g",degrees);
3234 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,exception);
3235 (void) FormatMagickString(label,MaxTextExtent,"wave %gx%g",
3236 0.5*degrees,2.0*degrees);
3239 case OilPaintPreview:
3241 preview_image=OilPaintImage(thumbnail,(double) radius,exception);
3242 (void) FormatMagickString(label,MaxTextExtent,"paint %g",radius);
3245 case CharcoalDrawingPreview:
3247 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
3249 (void) FormatMagickString(label,MaxTextExtent,"charcoal %gx%g",
3256 filename[MaxTextExtent];
3264 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3265 if (preview_image == (Image *) NULL)
3267 preview_info->quality=(size_t) percentage;
3268 (void) FormatMagickString(factor,MaxTextExtent,"%.20g",(double)
3269 preview_info->quality);
3270 file=AcquireUniqueFileResource(filename);
3273 (void) FormatMagickString(preview_image->filename,MaxTextExtent,
3274 "jpeg:%s",filename);
3275 status=WriteImage(preview_info,preview_image);
3276 if (status != MagickFalse)
3281 (void) CopyMagickString(preview_info->filename,
3282 preview_image->filename,MaxTextExtent);
3283 quality_image=ReadImage(preview_info,exception);
3284 if (quality_image != (Image *) NULL)
3286 preview_image=DestroyImage(preview_image);
3287 preview_image=quality_image;
3290 (void) RelinquishUniqueFileResource(preview_image->filename);
3291 if ((GetBlobSize(preview_image)/1024) >= 1024)
3292 (void) FormatMagickString(label,MaxTextExtent,"quality %s\n%gmb ",
3293 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
3296 if (GetBlobSize(preview_image) >= 1024)
3297 (void) FormatMagickString(label,MaxTextExtent,
3298 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
3299 GetBlobSize(preview_image))/1024.0);
3301 (void) FormatMagickString(label,MaxTextExtent,"quality %s\n%.20gb ",
3302 factor,(double) GetBlobSize(thumbnail));
3306 thumbnail=DestroyImage(thumbnail);
3310 if (preview_image == (Image *) NULL)
3312 (void) DeleteImageProperty(preview_image,"label");
3313 (void) SetImageProperty(preview_image,"label",label);
3314 AppendImageToList(&images,preview_image);
3315 proceed=SetImageProgress(image,PreviewImageTag,(MagickOffsetType) i,
3317 if (proceed == MagickFalse)
3320 if (images == (Image *) NULL)
3322 preview_info=DestroyImageInfo(preview_info);
3323 return((Image *) NULL);
3328 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
3329 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
3330 montage_info->shadow=MagickTrue;
3331 (void) CloneString(&montage_info->tile,"3x3");
3332 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
3333 (void) CloneString(&montage_info->frame,DefaultTileFrame);
3334 montage_image=MontageImages(images,montage_info,exception);
3335 montage_info=DestroyMontageInfo(montage_info);
3336 images=DestroyImageList(images);
3337 if (montage_image == (Image *) NULL)
3338 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3339 if (montage_image->montage != (char *) NULL)
3342 Free image directory.
3344 montage_image->montage=(char *) RelinquishMagickMemory(
3345 montage_image->montage);
3346 if (image->directory != (char *) NULL)
3347 montage_image->directory=(char *) RelinquishMagickMemory(
3348 montage_image->directory);
3350 preview_info=DestroyImageInfo(preview_info);
3351 return(montage_image);
3355 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3359 % R a d i a l B l u r I m a g e %
3363 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3365 % RadialBlurImage() applies a radial blur to the image.
3367 % Andrew Protano contributed this effect.
3369 % The format of the RadialBlurImage method is:
3371 % Image *RadialBlurImage(const Image *image,const double angle,
3372 % ExceptionInfo *exception)
3373 % Image *RadialBlurImageChannel(const Image *image,const ChannelType channel,
3374 % const double angle,ExceptionInfo *exception)
3376 % A description of each parameter follows:
3378 % o image: the image.
3380 % o channel: the channel type.
3382 % o angle: the angle of the radial blur.
3384 % o exception: return any errors or warnings in this structure.
3388 MagickExport Image *RadialBlurImage(const Image *image,const double angle,
3389 ExceptionInfo *exception)
3394 blur_image=RadialBlurImageChannel(image,DefaultChannels,angle,exception);
3398 MagickExport Image *RadialBlurImageChannel(const Image *image,
3399 const ChannelType channel,const double angle,ExceptionInfo *exception)
3437 Allocate blur image.
3439 assert(image != (Image *) NULL);
3440 assert(image->signature == MagickSignature);
3441 if (image->debug != MagickFalse)
3442 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3443 assert(exception != (ExceptionInfo *) NULL);
3444 assert(exception->signature == MagickSignature);
3445 blur_image=CloneImage(image,0,0,MagickTrue,exception);
3446 if (blur_image == (Image *) NULL)
3447 return((Image *) NULL);
3448 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
3450 InheritException(exception,&blur_image->exception);
3451 blur_image=DestroyImage(blur_image);
3452 return((Image *) NULL);
3454 blur_center.x=(double) image->columns/2.0;
3455 blur_center.y=(double) image->rows/2.0;
3456 blur_radius=hypot(blur_center.x,blur_center.y);
3457 n=(size_t) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+2UL);
3458 theta=DegreesToRadians(angle)/(MagickRealType) (n-1);
3459 cos_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
3460 sizeof(*cos_theta));
3461 sin_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
3462 sizeof(*sin_theta));
3463 if ((cos_theta == (MagickRealType *) NULL) ||
3464 (sin_theta == (MagickRealType *) NULL))
3466 blur_image=DestroyImage(blur_image);
3467 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3469 offset=theta*(MagickRealType) (n-1)/2.0;
3470 for (i=0; i < (ssize_t) n; i++)
3472 cos_theta[i]=cos((double) (theta*i-offset));
3473 sin_theta[i]=sin((double) (theta*i-offset));
3480 GetMagickPixelPacket(image,&bias);
3481 image_view=AcquireCacheView(image);
3482 blur_view=AcquireCacheView(blur_image);
3483 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3484 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3486 for (y=0; y < (ssize_t) blur_image->rows; y++)
3488 register const IndexPacket
3491 register IndexPacket
3492 *restrict blur_indexes;
3494 register PixelPacket
3500 if (status == MagickFalse)
3502 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3504 if (q == (PixelPacket *) NULL)
3509 blur_indexes=GetCacheViewAuthenticIndexQueue(blur_view);
3510 for (x=0; x < (ssize_t) blur_image->columns; x++)
3531 center.x=(double) x-blur_center.x;
3532 center.y=(double) y-blur_center.y;
3533 radius=hypot((double) center.x,center.y);
3538 step=(size_t) (blur_radius/radius);
3547 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
3549 for (i=0; i < (ssize_t) n; i+=(ssize_t) step)
3551 (void) GetOneCacheViewVirtualPixel(image_view,(ssize_t)
3552 (blur_center.x+center.x*cos_theta[i]-center.y*sin_theta[i]+0.5),
3553 (ssize_t) (blur_center.y+center.x*sin_theta[i]+center.y*
3554 cos_theta[i]+0.5),&pixel,exception);
3555 qixel.red+=pixel.red;
3556 qixel.green+=pixel.green;
3557 qixel.blue+=pixel.blue;
3558 qixel.opacity+=pixel.opacity;
3559 if (image->colorspace == CMYKColorspace)
3561 indexes=GetCacheViewVirtualIndexQueue(image_view);
3562 qixel.index+=(*indexes);
3566 normalize=1.0/(fabs((double) normalize) <= MagickEpsilon ? 1.0 :
3568 if ((channel & RedChannel) != 0)
3569 q->red=ClampToQuantum(normalize*qixel.red);
3570 if ((channel & GreenChannel) != 0)
3571 q->green=ClampToQuantum(normalize*qixel.green);
3572 if ((channel & BlueChannel) != 0)
3573 q->blue=ClampToQuantum(normalize*qixel.blue);
3574 if ((channel & OpacityChannel) != 0)
3575 q->opacity=ClampToQuantum(normalize*qixel.opacity);
3576 if (((channel & IndexChannel) != 0) &&
3577 (image->colorspace == CMYKColorspace))
3578 blur_indexes[x]=(IndexPacket) ClampToQuantum(normalize*qixel.index);
3588 for (i=0; i < (ssize_t) n; i+=(ssize_t) step)
3590 (void) GetOneCacheViewVirtualPixel(image_view,(ssize_t)
3591 (blur_center.x+center.x*cos_theta[i]-center.y*sin_theta[i]+0.5),
3592 (ssize_t) (blur_center.y+center.x*sin_theta[i]+center.y*
3593 cos_theta[i]+0.5),&pixel,exception);
3594 alpha=(MagickRealType) (QuantumScale*
3595 GetAlphaPixelComponent(&pixel));
3596 qixel.red+=alpha*pixel.red;
3597 qixel.green+=alpha*pixel.green;
3598 qixel.blue+=alpha*pixel.blue;
3599 qixel.opacity+=pixel.opacity;
3600 if (image->colorspace == CMYKColorspace)
3602 indexes=GetCacheViewVirtualIndexQueue(image_view);
3603 qixel.index+=alpha*(*indexes);
3608 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3609 normalize=1.0/(fabs((double) normalize) <= MagickEpsilon ? 1.0 :
3611 if ((channel & RedChannel) != 0)
3612 q->red=ClampToQuantum(gamma*qixel.red);
3613 if ((channel & GreenChannel) != 0)
3614 q->green=ClampToQuantum(gamma*qixel.green);
3615 if ((channel & BlueChannel) != 0)
3616 q->blue=ClampToQuantum(gamma*qixel.blue);
3617 if ((channel & OpacityChannel) != 0)
3618 q->opacity=ClampToQuantum(normalize*qixel.opacity);
3619 if (((channel & IndexChannel) != 0) &&
3620 (image->colorspace == CMYKColorspace))
3621 blur_indexes[x]=(IndexPacket) ClampToQuantum(gamma*qixel.index);
3625 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
3627 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3632 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3633 #pragma omp critical (MagickCore_RadialBlurImageChannel)
3635 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
3636 if (proceed == MagickFalse)
3640 blur_view=DestroyCacheView(blur_view);
3641 image_view=DestroyCacheView(image_view);
3642 cos_theta=(MagickRealType *) RelinquishMagickMemory(cos_theta);
3643 sin_theta=(MagickRealType *) RelinquishMagickMemory(sin_theta);
3644 if (status == MagickFalse)
3645 blur_image=DestroyImage(blur_image);
3650 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3654 % S e l e c t i v e B l u r I m a g e %
3658 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3660 % SelectiveBlurImage() selectively blur pixels within a contrast threshold.
3661 % It is similar to the unsharpen mask that sharpens everything with contrast
3662 % above a certain threshold.
3664 % The format of the SelectiveBlurImage method is:
3666 % Image *SelectiveBlurImage(const Image *image,const double radius,
3667 % const double sigma,const double threshold,ExceptionInfo *exception)
3668 % Image *SelectiveBlurImageChannel(const Image *image,
3669 % const ChannelType channel,const double radius,const double sigma,
3670 % const double threshold,ExceptionInfo *exception)
3672 % A description of each parameter follows:
3674 % o image: the image.
3676 % o channel: the channel type.
3678 % o radius: the radius of the Gaussian, in pixels, not counting the center
3681 % o sigma: the standard deviation of the Gaussian, in pixels.
3683 % o threshold: only pixels within this contrast threshold are included
3684 % in the blur operation.
3686 % o exception: return any errors or warnings in this structure.
3690 static inline MagickBooleanType SelectiveContrast(const PixelPacket *p,
3691 const PixelPacket *q,const double threshold)
3693 if (fabs(PixelIntensity(p)-PixelIntensity(q)) < threshold)
3695 return(MagickFalse);
3698 MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
3699 const double sigma,const double threshold,ExceptionInfo *exception)
3704 blur_image=SelectiveBlurImageChannel(image,DefaultChannels,radius,sigma,
3705 threshold,exception);
3709 MagickExport Image *SelectiveBlurImageChannel(const Image *image,
3710 const ChannelType channel,const double radius,const double sigma,
3711 const double threshold,ExceptionInfo *exception)
3713 #define SelectiveBlurImageTag "SelectiveBlur/Image"
3747 Initialize blur image attributes.
3749 assert(image != (Image *) NULL);
3750 assert(image->signature == MagickSignature);
3751 if (image->debug != MagickFalse)
3752 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3753 assert(exception != (ExceptionInfo *) NULL);
3754 assert(exception->signature == MagickSignature);
3755 width=GetOptimalKernelWidth1D(radius,sigma);
3756 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
3757 if (kernel == (double *) NULL)
3758 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3759 j=(ssize_t) width/2;
3761 for (v=(-j); v <= j; v++)
3763 for (u=(-j); u <= j; u++)
3764 kernel[i++]=(double) (exp(-((double) u*u+v*v)/(2.0*MagickSigma*
3765 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
3767 if (image->debug != MagickFalse)
3770 format[MaxTextExtent],
3773 register const double
3780 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
3781 " SelectiveBlurImage with %.20gx%.20g kernel:",(double) width,(double)
3783 message=AcquireString("");
3785 for (v=0; v < (ssize_t) width; v++)
3788 (void) FormatMagickString(format,MaxTextExtent,"%.20g: ",(double) v);
3789 (void) ConcatenateString(&message,format);
3790 for (u=0; u < (ssize_t) width; u++)
3792 (void) FormatMagickString(format,MaxTextExtent,"%+f ",*k++);
3793 (void) ConcatenateString(&message,format);
3795 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
3797 message=DestroyString(message);
3799 blur_image=CloneImage(image,0,0,MagickTrue,exception);
3800 if (blur_image == (Image *) NULL)
3801 return((Image *) NULL);
3802 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
3804 InheritException(exception,&blur_image->exception);
3805 blur_image=DestroyImage(blur_image);
3806 return((Image *) NULL);
3809 Threshold blur image.
3813 GetMagickPixelPacket(image,&bias);
3814 SetMagickPixelPacketBias(image,&bias);
3815 image_view=AcquireCacheView(image);
3816 blur_view=AcquireCacheView(blur_image);
3817 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3818 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3820 for (y=0; y < (ssize_t) image->rows; y++)
3828 register const IndexPacket
3831 register const PixelPacket
3834 register IndexPacket
3835 *restrict blur_indexes;
3837 register PixelPacket
3843 if (status == MagickFalse)
3845 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
3846 (width/2L),image->columns+width,width,exception);
3847 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3849 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
3854 indexes=GetCacheViewVirtualIndexQueue(image_view);
3855 blur_indexes=GetCacheViewAuthenticIndexQueue(blur_view);
3856 for (x=0; x < (ssize_t) image->columns; x++)
3861 register const double
3875 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
3877 for (v=0; v < (ssize_t) width; v++)
3879 for (u=0; u < (ssize_t) width; u++)
3881 if (SelectiveContrast(p+u+j,q,threshold) != MagickFalse)
3883 pixel.red+=(*k)*(p+u+j)->red;
3884 pixel.green+=(*k)*(p+u+j)->green;
3885 pixel.blue+=(*k)*(p+u+j)->blue;
3890 j+=(ssize_t) (image->columns+width);
3894 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3895 if ((channel & RedChannel) != 0)
3896 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
3897 if ((channel & GreenChannel) != 0)
3898 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
3899 if ((channel & BlueChannel) != 0)
3900 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
3902 if ((channel & OpacityChannel) != 0)
3906 for (v=0; v < (ssize_t) width; v++)
3908 for (u=0; u < (ssize_t) width; u++)
3910 if (SelectiveContrast(p+u+j,q,threshold) != MagickFalse)
3912 pixel.opacity+=(*k)*(p+u+j)->opacity;
3917 j+=(ssize_t) (image->columns+width);
3921 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
3923 SetOpacityPixelComponent(q,ClampToQuantum(gamma*
3924 GetOpacityPixelComponent(&pixel)));
3927 if (((channel & IndexChannel) != 0) &&
3928 (image->colorspace == CMYKColorspace))
3932 for (v=0; v < (ssize_t) width; v++)
3934 for (u=0; u < (ssize_t) width; u++)
3936 if (SelectiveContrast(p+u+j,q,threshold) != MagickFalse)
3938 pixel.index+=(*k)*indexes[x+u+j];
3943 j+=(ssize_t) (image->columns+width);
3947 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
3949 blur_indexes[x]=ClampToQuantum(gamma*
3950 GetIndexPixelComponent(&pixel));
3959 for (v=0; v < (ssize_t) width; v++)
3961 for (u=0; u < (ssize_t) width; u++)
3963 if (SelectiveContrast(p+u+j,q,threshold) != MagickFalse)
3965 alpha=(MagickRealType) (QuantumScale*
3966 GetAlphaPixelComponent(p+u+j));
3967 pixel.red+=(*k)*alpha*(p+u+j)->red;
3968 pixel.green+=(*k)*alpha*(p+u+j)->green;
3969 pixel.blue+=(*k)*alpha*(p+u+j)->blue;
3970 pixel.opacity+=(*k)*(p+u+j)->opacity;
3975 j+=(ssize_t) (image->columns+width);
3979 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3980 if ((channel & RedChannel) != 0)
3981 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
3982 if ((channel & GreenChannel) != 0)
3983 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
3984 if ((channel & BlueChannel) != 0)
3985 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
3987 if ((channel & OpacityChannel) != 0)
3991 for (v=0; v < (ssize_t) width; v++)
3993 for (u=0; u < (ssize_t) width; u++)
3995 if (SelectiveContrast(p+u+j,q,threshold) != MagickFalse)
3997 pixel.opacity+=(*k)*(p+u+j)->opacity;
4002 j+=(ssize_t) (image->columns+width);
4006 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
4008 SetOpacityPixelComponent(q,
4009 ClampOpacityPixelComponent(&pixel));
4012 if (((channel & IndexChannel) != 0) &&
4013 (image->colorspace == CMYKColorspace))
4017 for (v=0; v < (ssize_t) width; v++)
4019 for (u=0; u < (ssize_t) width; u++)
4021 if (SelectiveContrast(p+u+j,q,threshold) != MagickFalse)
4023 alpha=(MagickRealType) (QuantumScale*
4024 GetAlphaPixelComponent(p+u+j));
4025 pixel.index+=(*k)*alpha*indexes[x+u+j];
4030 j+=(ssize_t) (image->columns+width);
4034 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
4036 blur_indexes[x]=ClampToQuantum(gamma*
4037 GetIndexPixelComponent(&pixel));
4044 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
4045 if (sync == MagickFalse)
4047 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4052 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4053 #pragma omp critical (MagickCore_SelectiveBlurImageChannel)
4055 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
4057 if (proceed == MagickFalse)
4061 blur_image->type=image->type;
4062 blur_view=DestroyCacheView(blur_view);
4063 image_view=DestroyCacheView(image_view);
4064 kernel=(double *) RelinquishMagickMemory(kernel);
4065 if (status == MagickFalse)
4066 blur_image=DestroyImage(blur_image);
4071 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4075 % S h a d e I m a g e %
4079 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4081 % ShadeImage() shines a distant light on an image to create a
4082 % three-dimensional effect. You control the positioning of the light with
4083 % azimuth and elevation; azimuth is measured in degrees off the x axis
4084 % and elevation is measured in pixels above the Z axis.
4086 % The format of the ShadeImage method is:
4088 % Image *ShadeImage(const Image *image,const MagickBooleanType gray,
4089 % const double azimuth,const double elevation,ExceptionInfo *exception)
4091 % A description of each parameter follows:
4093 % o image: the image.
4095 % o gray: A value other than zero shades the intensity of each pixel.
4097 % o azimuth, elevation: Define the light source direction.
4099 % o exception: return any errors or warnings in this structure.
4102 MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
4103 const double azimuth,const double elevation,ExceptionInfo *exception)
4105 #define ShadeImageTag "Shade/Image"
4127 Initialize shaded image attributes.
4129 assert(image != (const Image *) NULL);
4130 assert(image->signature == MagickSignature);
4131 if (image->debug != MagickFalse)
4132 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4133 assert(exception != (ExceptionInfo *) NULL);
4134 assert(exception->signature == MagickSignature);
4135 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
4136 if (shade_image == (Image *) NULL)
4137 return((Image *) NULL);
4138 if (SetImageStorageClass(shade_image,DirectClass) == MagickFalse)
4140 InheritException(exception,&shade_image->exception);
4141 shade_image=DestroyImage(shade_image);
4142 return((Image *) NULL);
4145 Compute the light vector.
4147 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
4148 cos(DegreesToRadians(elevation));
4149 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
4150 cos(DegreesToRadians(elevation));
4151 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
4157 image_view=AcquireCacheView(image);
4158 shade_view=AcquireCacheView(shade_image);
4159 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4160 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
4162 for (y=0; y < (ssize_t) image->rows; y++)
4172 register const PixelPacket
4178 register PixelPacket
4184 if (status == MagickFalse)
4186 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
4187 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
4189 if ((p == (PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
4195 Shade this row of pixels.
4197 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
4199 s1=s0+image->columns+2;
4200 s2=s1+image->columns+2;
4201 for (x=0; x < (ssize_t) image->columns; x++)
4204 Determine the surface normal and compute shading.
4206 normal.x=(double) (PixelIntensity(s0-1)+PixelIntensity(s1-1)+
4207 PixelIntensity(s2-1)-PixelIntensity(s0+1)-PixelIntensity(s1+1)-
4208 PixelIntensity(s2+1));
4209 normal.y=(double) (PixelIntensity(s2-1)+PixelIntensity(s2)+
4210 PixelIntensity(s2+1)-PixelIntensity(s0-1)-PixelIntensity(s0)-
4211 PixelIntensity(s0+1));
4212 if ((normal.x == 0.0) && (normal.y == 0.0))
4217 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
4218 if (distance > MagickEpsilon)
4221 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
4222 if (normal_distance > (MagickEpsilon*MagickEpsilon))
4223 shade=distance/sqrt((double) normal_distance);
4226 if (gray != MagickFalse)
4228 q->red=(Quantum) shade;
4229 q->green=(Quantum) shade;
4230 q->blue=(Quantum) shade;
4234 q->red=ClampToQuantum(QuantumScale*shade*s1->red);
4235 q->green=ClampToQuantum(QuantumScale*shade*s1->green);
4236 q->blue=ClampToQuantum(QuantumScale*shade*s1->blue);
4238 q->opacity=s1->opacity;
4244 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
4246 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4251 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4252 #pragma omp critical (MagickCore_ShadeImage)
4254 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
4255 if (proceed == MagickFalse)
4259 shade_view=DestroyCacheView(shade_view);
4260 image_view=DestroyCacheView(image_view);
4261 if (status == MagickFalse)
4262 shade_image=DestroyImage(shade_image);
4263 return(shade_image);
4267 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4271 % S h a r p e n I m a g e %
4275 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4277 % SharpenImage() sharpens the image. We convolve the image with a Gaussian
4278 % operator of the given radius and standard deviation (sigma). For
4279 % reasonable results, radius should be larger than sigma. Use a radius of 0
4280 % and SharpenImage() selects a suitable radius for you.
4282 % Using a separable kernel would be faster, but the negative weights cancel
4283 % out on the corners of the kernel producing often undesirable ringing in the
4284 % filtered result; this can be avoided by using a 2D gaussian shaped image
4285 % sharpening kernel instead.
4287 % The format of the SharpenImage method is:
4289 % Image *SharpenImage(const Image *image,const double radius,
4290 % const double sigma,ExceptionInfo *exception)
4291 % Image *SharpenImageChannel(const Image *image,const ChannelType channel,
4292 % const double radius,const double sigma,ExceptionInfo *exception)
4294 % A description of each parameter follows:
4296 % o image: the image.
4298 % o channel: the channel type.
4300 % o radius: the radius of the Gaussian, in pixels, not counting the center
4303 % o sigma: the standard deviation of the Laplacian, in pixels.
4305 % o exception: return any errors or warnings in this structure.
4309 MagickExport Image *SharpenImage(const Image *image,const double radius,
4310 const double sigma,ExceptionInfo *exception)
4315 sharp_image=SharpenImageChannel(image,DefaultChannels,radius,sigma,exception);
4316 return(sharp_image);
4319 MagickExport Image *SharpenImageChannel(const Image *image,
4320 const ChannelType channel,const double radius,const double sigma,
4321 ExceptionInfo *exception)
4341 assert(image != (const Image *) NULL);
4342 assert(image->signature == MagickSignature);
4343 if (image->debug != MagickFalse)
4344 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4345 assert(exception != (ExceptionInfo *) NULL);
4346 assert(exception->signature == MagickSignature);
4347 width=GetOptimalKernelWidth2D(radius,sigma);
4348 kernel=(double *) AcquireQuantumMemory((size_t) width*width,sizeof(*kernel));
4349 if (kernel == (double *) NULL)
4350 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
4352 j=(ssize_t) width/2;
4354 for (v=(-j); v <= j; v++)
4356 for (u=(-j); u <= j; u++)
4358 kernel[i]=(double) (-exp(-((double) u*u+v*v)/(2.0*MagickSigma*
4359 MagickSigma))/(2.0*MagickPI*MagickSigma*MagickSigma));
4360 normalize+=kernel[i];
4364 kernel[i/2]=(double) ((-2.0)*normalize);
4365 sharp_image=ConvolveImageChannel(image,channel,width,kernel,exception);
4366 kernel=(double *) RelinquishMagickMemory(kernel);
4367 return(sharp_image);
4371 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4375 % S p r e a d I m a g e %
4379 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4381 % SpreadImage() is a special effects method that randomly displaces each
4382 % pixel in a block defined by the radius parameter.
4384 % The format of the SpreadImage method is:
4386 % Image *SpreadImage(const Image *image,const double radius,
4387 % ExceptionInfo *exception)
4389 % A description of each parameter follows:
4391 % o image: the image.
4393 % o radius: Choose a random pixel in a neighborhood of this extent.
4395 % o exception: return any errors or warnings in this structure.
4398 MagickExport Image *SpreadImage(const Image *image,const double radius,
4399 ExceptionInfo *exception)
4401 #define SpreadImageTag "Spread/Image"
4419 **restrict random_info;
4422 **restrict resample_filter;
4431 Initialize spread image attributes.
4433 assert(image != (Image *) NULL);
4434 assert(image->signature == MagickSignature);
4435 if (image->debug != MagickFalse)
4436 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4437 assert(exception != (ExceptionInfo *) NULL);
4438 assert(exception->signature == MagickSignature);
4439 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
4441 if (spread_image == (Image *) NULL)
4442 return((Image *) NULL);
4443 if (SetImageStorageClass(spread_image,DirectClass) == MagickFalse)
4445 InheritException(exception,&spread_image->exception);
4446 spread_image=DestroyImage(spread_image);
4447 return((Image *) NULL);
4454 GetMagickPixelPacket(spread_image,&bias);
4455 width=GetOptimalKernelWidth1D(radius,0.5);
4456 resample_filter=AcquireResampleFilterThreadSet(image,
4457 UndefinedVirtualPixelMethod,MagickTrue,exception);
4458 random_info=AcquireRandomInfoThreadSet();
4459 image_view=AcquireCacheView(spread_image);
4460 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4461 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
4463 for (y=0; y < (ssize_t) spread_image->rows; y++)
4466 id = GetOpenMPThreadId();
4471 register IndexPacket
4474 register PixelPacket
4480 if (status == MagickFalse)
4482 q=QueueCacheViewAuthenticPixels(image_view,0,y,spread_image->columns,1,
4484 if (q == (PixelPacket *) NULL)
4489 indexes=GetCacheViewAuthenticIndexQueue(image_view);
4491 for (x=0; x < (ssize_t) spread_image->columns; x++)
4493 (void) ResamplePixelColor(resample_filter[id],(double) x+width*
4494 (GetPseudoRandomValue(random_info[id])-0.5),(double) y+width*
4495 (GetPseudoRandomValue(random_info[id])-0.5),&pixel);
4496 SetPixelPacket(spread_image,&pixel,q,indexes+x);
4499 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4501 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4506 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4507 #pragma omp critical (MagickCore_SpreadImage)
4509 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
4510 if (proceed == MagickFalse)
4514 image_view=DestroyCacheView(image_view);
4515 random_info=DestroyRandomInfoThreadSet(random_info);
4516 resample_filter=DestroyResampleFilterThreadSet(resample_filter);
4517 return(spread_image);
4521 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4525 % S t a t i s t i c I m a g e %
4529 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4531 % StatisticImage() makes each pixel the min / max / median / mode / etc. of
4532 % the neighborhood of the specified width and height.
4534 % The format of the StatisticImage method is:
4536 % Image *StatisticImage(const Image *image,const StatisticType type,
4537 % const size_t width,const size_t height,ExceptionInfo *exception)
4538 % Image *StatisticImageChannel(const Image *image,
4539 % const ChannelType channel,const StatisticType type,
4540 % const size_t width,const size_t height,ExceptionInfo *exception)
4542 % A description of each parameter follows:
4544 % o image: the image.
4546 % o channel: the image channel.
4548 % o type: the statistic type (median, mode, etc.).
4550 % o width: the width of the pixel neighborhood.
4552 % o height: the height of the pixel neighborhood.
4554 % o exception: return any errors or warnings in this structure.
4558 #define ListChannels 5
4560 typedef struct _ListNode
4568 typedef struct _SkipList
4577 typedef struct _PixelList
4585 lists[ListChannels];
4588 static PixelList *DestroyPixelList(PixelList *pixel_list)
4593 if (pixel_list == (PixelList *) NULL)
4594 return((PixelList *) NULL);
4595 for (i=0; i < ListChannels; i++)
4596 if (pixel_list->lists[i].nodes != (ListNode *) NULL)
4597 pixel_list->lists[i].nodes=(ListNode *) RelinquishMagickMemory(
4598 pixel_list->lists[i].nodes);
4599 pixel_list=(PixelList *) RelinquishMagickMemory(pixel_list);
4603 static PixelList **DestroyPixelListThreadSet(PixelList **pixel_list)
4608 assert(pixel_list != (PixelList **) NULL);
4609 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
4610 if (pixel_list[i] != (PixelList *) NULL)
4611 pixel_list[i]=DestroyPixelList(pixel_list[i]);
4612 pixel_list=(PixelList **) RelinquishMagickMemory(pixel_list);
4616 static PixelList *AcquirePixelList(const size_t width,const size_t height)
4624 pixel_list=(PixelList *) AcquireMagickMemory(sizeof(*pixel_list));
4625 if (pixel_list == (PixelList *) NULL)
4627 (void) ResetMagickMemory((void *) pixel_list,0,sizeof(*pixel_list));
4628 pixel_list->length=width*height;
4629 for (i=0; i < ListChannels; i++)
4631 pixel_list->lists[i].nodes=(ListNode *) AcquireQuantumMemory(65537UL,
4632 sizeof(*pixel_list->lists[i].nodes));
4633 if (pixel_list->lists[i].nodes == (ListNode *) NULL)
4634 return(DestroyPixelList(pixel_list));
4635 (void) ResetMagickMemory(pixel_list->lists[i].nodes,0,65537UL*
4636 sizeof(*pixel_list->lists[i].nodes));
4638 pixel_list->signature=MagickSignature;
4642 static PixelList **AcquirePixelListThreadSet(const size_t width,
4643 const size_t height)
4654 number_threads=GetOpenMPMaximumThreads();
4655 pixel_list=(PixelList **) AcquireQuantumMemory(number_threads,
4656 sizeof(*pixel_list));
4657 if (pixel_list == (PixelList **) NULL)
4658 return((PixelList **) NULL);
4659 (void) ResetMagickMemory(pixel_list,0,number_threads*sizeof(*pixel_list));
4660 for (i=0; i < (ssize_t) number_threads; i++)
4662 pixel_list[i]=AcquirePixelList(width,height);
4663 if (pixel_list[i] == (PixelList *) NULL)
4664 return(DestroyPixelListThreadSet(pixel_list));
4669 static void AddNodePixelList(PixelList *pixel_list,const ssize_t channel,
4683 Initialize the node.
4685 list=pixel_list->lists+channel;
4686 list->nodes[color].signature=pixel_list->signature;
4687 list->nodes[color].count=1;
4689 Determine where it belongs in the list.
4692 for (level=list->level; level >= 0; level--)
4694 while (list->nodes[search].next[level] < color)
4695 search=list->nodes[search].next[level];
4696 update[level]=search;
4699 Generate a pseudo-random level for this node.
4701 for (level=0; ; level++)
4703 pixel_list->seed=(pixel_list->seed*42893621L)+1L;
4704 if ((pixel_list->seed & 0x300) != 0x300)
4709 if (level > (list->level+2))
4710 level=list->level+2;
4712 If we're raising the list's level, link back to the root node.
4714 while (level > list->level)
4717 update[list->level]=65536UL;
4720 Link the node into the skip-list.
4724 list->nodes[color].next[level]=list->nodes[update[level]].next[level];
4725 list->nodes[update[level]].next[level]=color;
4726 } while (level-- > 0);
4729 static MagickPixelPacket GetMaximumPixelList(PixelList *pixel_list)
4747 channels[ListChannels],
4751 Find the maximum value for each of the color.
4753 for (channel=0; channel < 5; channel++)
4755 list=pixel_list->lists+channel;
4758 maximum=list->nodes[color].next[0];
4761 color=list->nodes[color].next[0];
4762 if (color > maximum)
4764 count+=list->nodes[color].count;
4765 } while (count < pixel_list->length);
4766 channels[channel]=(unsigned short) maximum;
4768 GetMagickPixelPacket((const Image *) NULL,&pixel);
4769 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4770 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4771 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
4772 pixel.opacity=(MagickRealType) ScaleShortToQuantum(channels[3]);
4773 pixel.index=(MagickRealType) ScaleShortToQuantum(channels[4]);
4777 static MagickPixelPacket GetMeanPixelList(PixelList *pixel_list)
4796 channels[ListChannels];
4799 Find the mean value for each of the color.
4801 for (channel=0; channel < 5; channel++)
4803 list=pixel_list->lists+channel;
4809 color=list->nodes[color].next[0];
4811 count+=list->nodes[color].count;
4812 } while (count < pixel_list->length);
4813 channels[channel]=(unsigned short) (mean/pixel_list->length);
4815 GetMagickPixelPacket((const Image *) NULL,&pixel);
4816 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4817 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4818 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
4819 pixel.opacity=(MagickRealType) ScaleShortToQuantum(channels[3]);
4820 pixel.index=(MagickRealType) ScaleShortToQuantum(channels[4]);
4824 static MagickPixelPacket GetMedianPixelList(PixelList *pixel_list)
4842 channels[ListChannels];
4845 Find the median value for each of the color.
4847 for (channel=0; channel < 5; channel++)
4849 list=pixel_list->lists+channel;
4854 color=list->nodes[color].next[0];
4855 count+=list->nodes[color].count;
4856 } while (count <= (pixel_list->length >> 1));
4857 channels[channel]=(unsigned short) color;
4859 GetMagickPixelPacket((const Image *) NULL,&pixel);
4860 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4861 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4862 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
4863 pixel.opacity=(MagickRealType) ScaleShortToQuantum(channels[3]);
4864 pixel.index=(MagickRealType) ScaleShortToQuantum(channels[4]);
4868 static MagickPixelPacket GetMinimumPixelList(PixelList *pixel_list)
4886 channels[ListChannels],
4890 Find the minimum value for each of the color.
4892 for (channel=0; channel < 5; channel++)
4894 list=pixel_list->lists+channel;
4897 minimum=list->nodes[color].next[0];
4900 color=list->nodes[color].next[0];
4901 if (color < minimum)
4903 count+=list->nodes[color].count;
4904 } while (count < pixel_list->length);
4905 channels[channel]=(unsigned short) minimum;
4907 GetMagickPixelPacket((const Image *) NULL,&pixel);
4908 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4909 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4910 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
4911 pixel.opacity=(MagickRealType) ScaleShortToQuantum(channels[3]);
4912 pixel.index=(MagickRealType) ScaleShortToQuantum(channels[4]);
4916 static MagickPixelPacket GetModePixelList(PixelList *pixel_list)
4939 Make each pixel the 'predominate color' of the specified neighborhood.
4941 for (channel=0; channel < 5; channel++)
4943 list=pixel_list->lists+channel;
4946 max_count=list->nodes[mode].count;
4950 color=list->nodes[color].next[0];
4951 if (list->nodes[color].count > max_count)
4954 max_count=list->nodes[mode].count;
4956 count+=list->nodes[color].count;
4957 } while (count < pixel_list->length);
4958 channels[channel]=(unsigned short) mode;
4960 GetMagickPixelPacket((const Image *) NULL,&pixel);
4961 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4962 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4963 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
4964 pixel.opacity=(MagickRealType) ScaleShortToQuantum(channels[3]);
4965 pixel.index=(MagickRealType) ScaleShortToQuantum(channels[4]);
4969 static MagickPixelPacket GetNonpeakPixelList(PixelList *pixel_list)
4992 Finds the non peak value for each of the colors.
4994 for (channel=0; channel < 5; channel++)
4996 list=pixel_list->lists+channel;
4998 next=list->nodes[color].next[0];
5004 next=list->nodes[color].next[0];
5005 count+=list->nodes[color].count;
5006 } while (count <= (pixel_list->length >> 1));
5007 if ((previous == 65536UL) && (next != 65536UL))
5010 if ((previous != 65536UL) && (next == 65536UL))
5012 channels[channel]=(unsigned short) color;
5014 GetMagickPixelPacket((const Image *) NULL,&pixel);
5015 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
5016 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
5017 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
5018 pixel.opacity=(MagickRealType) ScaleShortToQuantum(channels[3]);
5019 pixel.index=(MagickRealType) ScaleShortToQuantum(channels[4]);
5023 static inline void InsertPixelList(const Image *image,const PixelPacket *pixel,
5024 const IndexPacket *indexes,PixelList *pixel_list)
5032 index=ScaleQuantumToShort(pixel->red);
5033 signature=pixel_list->lists[0].nodes[index].signature;
5034 if (signature == pixel_list->signature)
5035 pixel_list->lists[0].nodes[index].count++;
5037 AddNodePixelList(pixel_list,0,index);
5038 index=ScaleQuantumToShort(pixel->green);
5039 signature=pixel_list->lists[1].nodes[index].signature;
5040 if (signature == pixel_list->signature)
5041 pixel_list->lists[1].nodes[index].count++;
5043 AddNodePixelList(pixel_list,1,index);
5044 index=ScaleQuantumToShort(pixel->blue);
5045 signature=pixel_list->lists[2].nodes[index].signature;
5046 if (signature == pixel_list->signature)
5047 pixel_list->lists[2].nodes[index].count++;
5049 AddNodePixelList(pixel_list,2,index);
5050 index=ScaleQuantumToShort(pixel->opacity);
5051 signature=pixel_list->lists[3].nodes[index].signature;
5052 if (signature == pixel_list->signature)
5053 pixel_list->lists[3].nodes[index].count++;
5055 AddNodePixelList(pixel_list,3,index);
5056 if (image->colorspace == CMYKColorspace)
5057 index=ScaleQuantumToShort(*indexes);
5058 signature=pixel_list->lists[4].nodes[index].signature;
5059 if (signature == pixel_list->signature)
5060 pixel_list->lists[4].nodes[index].count++;
5062 AddNodePixelList(pixel_list,4,index);
5065 static void ResetPixelList(PixelList *pixel_list)
5080 Reset the skip-list.
5082 for (channel=0; channel < 5; channel++)
5084 list=pixel_list->lists+channel;
5085 root=list->nodes+65536UL;
5087 for (level=0; level < 9; level++)
5088 root->next[level]=65536UL;
5090 pixel_list->seed=pixel_list->signature++;
5093 MagickExport Image *StatisticImage(const Image *image,const StatisticType type,
5094 const size_t width,const size_t height,ExceptionInfo *exception)
5099 statistic_image=StatisticImageChannel(image,DefaultChannels,type,width,
5101 return(statistic_image);
5104 MagickExport Image *StatisticImageChannel(const Image *image,
5105 const ChannelType channel,const StatisticType type,const size_t width,
5106 const size_t height,ExceptionInfo *exception)
5108 #define StatisticWidth \
5109 (width == 0 ? GetOptimalKernelWidth2D(width,0.5) : width)
5110 #define StatisticHeight \
5111 (height == 0 ? GetOptimalKernelWidth2D(height,0.5) : height)
5112 #define StatisticImageTag "Statistic/Image"
5128 **restrict pixel_list;
5134 Initialize statistics image attributes.
5136 assert(image != (Image *) NULL);
5137 assert(image->signature == MagickSignature);
5138 if (image->debug != MagickFalse)
5139 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5140 assert(exception != (ExceptionInfo *) NULL);
5141 assert(exception->signature == MagickSignature);
5142 statistic_image=CloneImage(image,image->columns,image->rows,MagickTrue,
5144 if (statistic_image == (Image *) NULL)
5145 return((Image *) NULL);
5146 if (SetImageStorageClass(statistic_image,DirectClass) == MagickFalse)
5148 InheritException(exception,&statistic_image->exception);
5149 statistic_image=DestroyImage(statistic_image);
5150 return((Image *) NULL);
5152 pixel_list=AcquirePixelListThreadSet(StatisticWidth,StatisticHeight);
5153 if (pixel_list == (PixelList **) NULL)
5155 statistic_image=DestroyImage(statistic_image);
5156 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5159 Make each pixel the min / max / median / mode / etc. of the neighborhood.
5163 image_view=AcquireCacheView(image);
5164 statistic_view=AcquireCacheView(statistic_image);
5165 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5166 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
5168 for (y=0; y < (ssize_t) statistic_image->rows; y++)
5171 id = GetOpenMPThreadId();
5173 register const IndexPacket
5176 register const PixelPacket
5179 register IndexPacket
5180 *restrict statistic_indexes;
5182 register PixelPacket
5188 if (status == MagickFalse)
5190 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) StatisticWidth/2L),y-
5191 (ssize_t) (StatisticHeight/2L),image->columns+StatisticWidth,
5192 StatisticHeight,exception);
5193 q=QueueCacheViewAuthenticPixels(statistic_view,0,y,statistic_image->columns, 1,exception);
5194 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
5199 indexes=GetCacheViewVirtualIndexQueue(image_view);
5200 statistic_indexes=GetCacheViewAuthenticIndexQueue(statistic_view);
5201 for (x=0; x < (ssize_t) statistic_image->columns; x++)
5206 register const IndexPacket
5209 register const PixelPacket
5218 ResetPixelList(pixel_list[id]);
5219 for (v=0; v < (ssize_t) StatisticHeight; v++)
5221 for (u=0; u < (ssize_t) StatisticWidth; u++)
5222 InsertPixelList(image,r+u,s+u,pixel_list[id]);
5223 r+=image->columns+StatisticWidth;
5224 s+=image->columns+StatisticWidth;
5228 case MaximumStatistic:
5230 pixel=GetMaximumPixelList(pixel_list[id]);
5235 pixel=GetMeanPixelList(pixel_list[id]);
5238 case MedianStatistic:
5241 pixel=GetMedianPixelList(pixel_list[id]);
5244 case MinimumStatistic:
5246 pixel=GetMinimumPixelList(pixel_list[id]);
5251 pixel=GetModePixelList(pixel_list[id]);
5254 case NonpeakStatistic:
5256 pixel=GetNonpeakPixelList(pixel_list[id]);
5260 if ((channel & RedChannel) != 0)
5261 q->red=ClampToQuantum(pixel.red);
5262 if ((channel & GreenChannel) != 0)
5263 q->green=ClampToQuantum(pixel.green);
5264 if ((channel & BlueChannel) != 0)
5265 q->blue=ClampToQuantum(pixel.blue);
5266 if (((channel & OpacityChannel) != 0) &&
5267 (image->matte != MagickFalse))
5268 q->opacity=ClampToQuantum(pixel.opacity);
5269 if (((channel & IndexChannel) != 0) &&
5270 (image->colorspace == CMYKColorspace))
5271 statistic_indexes[x]=(IndexPacket) ClampToQuantum(pixel.index);
5275 if (SyncCacheViewAuthenticPixels(statistic_view,exception) == MagickFalse)
5277 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5282 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5283 #pragma omp critical (MagickCore_StatisticImage)
5285 proceed=SetImageProgress(image,StatisticImageTag,progress++,
5287 if (proceed == MagickFalse)
5291 statistic_view=DestroyCacheView(statistic_view);
5292 image_view=DestroyCacheView(image_view);
5293 pixel_list=DestroyPixelListThreadSet(pixel_list);
5294 return(statistic_image);
5298 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5302 % U n s h a r p M a s k I m a g e %
5306 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5308 % UnsharpMaskImage() sharpens one or more image channels. We convolve the
5309 % image with a Gaussian operator of the given radius and standard deviation
5310 % (sigma). For reasonable results, radius should be larger than sigma. Use a
5311 % radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
5313 % The format of the UnsharpMaskImage method is:
5315 % Image *UnsharpMaskImage(const Image *image,const double radius,
5316 % const double sigma,const double amount,const double threshold,
5317 % ExceptionInfo *exception)
5318 % Image *UnsharpMaskImageChannel(const Image *image,
5319 % const ChannelType channel,const double radius,const double sigma,
5320 % const double amount,const double threshold,ExceptionInfo *exception)
5322 % A description of each parameter follows:
5324 % o image: the image.
5326 % o channel: the channel type.
5328 % o radius: the radius of the Gaussian, in pixels, not counting the center
5331 % o sigma: the standard deviation of the Gaussian, in pixels.
5333 % o amount: the percentage of the difference between the original and the
5334 % blur image that is added back into the original.
5336 % o threshold: the threshold in pixels needed to apply the diffence amount.
5338 % o exception: return any errors or warnings in this structure.
5342 MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
5343 const double sigma,const double amount,const double threshold,
5344 ExceptionInfo *exception)
5349 sharp_image=UnsharpMaskImageChannel(image,DefaultChannels,radius,sigma,amount,
5350 threshold,exception);
5351 return(sharp_image);
5354 MagickExport Image *UnsharpMaskImageChannel(const Image *image,
5355 const ChannelType channel,const double radius,const double sigma,
5356 const double amount,const double threshold,ExceptionInfo *exception)
5358 #define SharpenImageTag "Sharpen/Image"
5382 assert(image != (const Image *) NULL);
5383 assert(image->signature == MagickSignature);
5384 if (image->debug != MagickFalse)
5385 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5386 assert(exception != (ExceptionInfo *) NULL);
5387 unsharp_image=BlurImageChannel(image,channel,radius,sigma,exception);
5388 if (unsharp_image == (Image *) NULL)
5389 return((Image *) NULL);
5390 quantum_threshold=(MagickRealType) QuantumRange*threshold;
5396 GetMagickPixelPacket(image,&bias);
5397 image_view=AcquireCacheView(image);
5398 unsharp_view=AcquireCacheView(unsharp_image);
5399 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5400 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
5402 for (y=0; y < (ssize_t) image->rows; y++)
5407 register const IndexPacket
5410 register const PixelPacket
5413 register IndexPacket
5414 *restrict unsharp_indexes;
5416 register PixelPacket
5422 if (status == MagickFalse)
5424 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5425 q=GetCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
5427 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
5432 indexes=GetCacheViewVirtualIndexQueue(image_view);
5433 unsharp_indexes=GetCacheViewAuthenticIndexQueue(unsharp_view);
5435 for (x=0; x < (ssize_t) image->columns; x++)
5437 if ((channel & RedChannel) != 0)
5439 pixel.red=p->red-(MagickRealType) q->red;
5440 if (fabs(2.0*pixel.red) < quantum_threshold)
5441 pixel.red=(MagickRealType) GetRedPixelComponent(p);
5443 pixel.red=(MagickRealType) p->red+(pixel.red*amount);
5444 SetRedPixelComponent(q,ClampRedPixelComponent(&pixel));
5446 if ((channel & GreenChannel) != 0)
5448 pixel.green=p->green-(MagickRealType) q->green;
5449 if (fabs(2.0*pixel.green) < quantum_threshold)
5450 pixel.green=(MagickRealType) GetGreenPixelComponent(p);
5452 pixel.green=(MagickRealType) p->green+(pixel.green*amount);
5453 SetGreenPixelComponent(q,ClampGreenPixelComponent(&pixel));
5455 if ((channel & BlueChannel) != 0)
5457 pixel.blue=p->blue-(MagickRealType) q->blue;
5458 if (fabs(2.0*pixel.blue) < quantum_threshold)
5459 pixel.blue=(MagickRealType) GetBluePixelComponent(p);
5461 pixel.blue=(MagickRealType) p->blue+(pixel.blue*amount);
5462 SetBluePixelComponent(q,ClampBluePixelComponent(&pixel));
5464 if ((channel & OpacityChannel) != 0)
5466 pixel.opacity=p->opacity-(MagickRealType) q->opacity;
5467 if (fabs(2.0*pixel.opacity) < quantum_threshold)
5468 pixel.opacity=(MagickRealType) GetOpacityPixelComponent(p);
5470 pixel.opacity=p->opacity+(pixel.opacity*amount);
5471 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
5473 if (((channel & IndexChannel) != 0) &&
5474 (image->colorspace == CMYKColorspace))
5476 pixel.index=indexes[x]-(MagickRealType) unsharp_indexes[x];
5477 if (fabs(2.0*pixel.index) < quantum_threshold)
5478 pixel.index=(MagickRealType) indexes[x];
5480 pixel.index=(MagickRealType) indexes[x]+(pixel.index*amount);
5481 unsharp_indexes[x]=ClampToQuantum(pixel.index);
5486 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
5488 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5493 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5494 #pragma omp critical (MagickCore_UnsharpMaskImageChannel)
5496 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
5497 if (proceed == MagickFalse)
5501 unsharp_image->type=image->type;
5502 unsharp_view=DestroyCacheView(unsharp_view);
5503 image_view=DestroyCacheView(image_view);
5504 if (status == MagickFalse)
5505 unsharp_image=DestroyImage(unsharp_image);
5506 return(unsharp_image);