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-2010 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/paint.h"
68 #include "magick/pixel-private.h"
69 #include "magick/property.h"
70 #include "magick/quantize.h"
71 #include "magick/quantum.h"
72 #include "magick/random_.h"
73 #include "magick/random-private.h"
74 #include "magick/resample.h"
75 #include "magick/resample-private.h"
76 #include "magick/resize.h"
77 #include "magick/resource_.h"
78 #include "magick/segment.h"
79 #include "magick/shear.h"
80 #include "magick/signature-private.h"
81 #include "magick/string_.h"
82 #include "magick/thread-private.h"
83 #include "magick/transform.h"
84 #include "magick/threshold.h"
87 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91 % A d a p t i v e B l u r I m a g e %
95 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97 % AdaptiveBlurImage() adaptively blurs the image by blurring less
98 % intensely near image edges and more intensely far from edges. We blur the
99 % image with a Gaussian operator of the given radius and standard deviation
100 % (sigma). For reasonable results, radius should be larger than sigma. Use a
101 % radius of 0 and AdaptiveBlurImage() selects a suitable radius for you.
103 % The format of the AdaptiveBlurImage method is:
105 % Image *AdaptiveBlurImage(const Image *image,const double radius,
106 % const double sigma,ExceptionInfo *exception)
107 % Image *AdaptiveBlurImageChannel(const Image *image,
108 % const ChannelType channel,double radius,const double sigma,
109 % ExceptionInfo *exception)
111 % A description of each parameter follows:
113 % o image: the image.
115 % o channel: the channel type.
117 % o radius: the radius of the Gaussian, in pixels, not counting the center
120 % o sigma: the standard deviation of the Laplacian, in pixels.
122 % o exception: return any errors or warnings in this structure.
126 MagickExport Image *AdaptiveBlurImage(const Image *image,const double radius,
127 const double sigma,ExceptionInfo *exception)
132 blur_image=AdaptiveBlurImageChannel(image,DefaultChannels,radius,sigma,
137 MagickExport Image *AdaptiveBlurImageChannel(const Image *image,
138 const ChannelType channel,const double radius,const double sigma,
139 ExceptionInfo *exception)
141 #define AdaptiveBlurImageTag "Convolve/Image"
142 #define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
178 assert(image != (const Image *) NULL);
179 assert(image->signature == MagickSignature);
180 if (image->debug != MagickFalse)
181 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
182 assert(exception != (ExceptionInfo *) NULL);
183 assert(exception->signature == MagickSignature);
184 blur_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
185 if (blur_image == (Image *) NULL)
186 return((Image *) NULL);
187 if (fabs(sigma) <= MagickEpsilon)
189 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
191 InheritException(exception,&blur_image->exception);
192 blur_image=DestroyImage(blur_image);
193 return((Image *) NULL);
196 Edge detect the image brighness channel, level, blur, and level again.
198 edge_image=EdgeImage(image,radius,exception);
199 if (edge_image == (Image *) NULL)
201 blur_image=DestroyImage(blur_image);
202 return((Image *) NULL);
204 (void) LevelImage(edge_image,"20%,95%");
205 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
206 if (gaussian_image != (Image *) NULL)
208 edge_image=DestroyImage(edge_image);
209 edge_image=gaussian_image;
211 (void) LevelImage(edge_image,"10%,95%");
213 Create a set of kernels from maximum (radius,sigma) to minimum.
215 width=GetOptimalKernelWidth2D(radius,sigma);
216 kernel=(double **) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
217 if (kernel == (double **) NULL)
219 edge_image=DestroyImage(edge_image);
220 blur_image=DestroyImage(blur_image);
221 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
223 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
224 for (i=0; i < (long) width; i+=2)
226 kernel[i]=(double *) AcquireQuantumMemory((size_t) (width-i),(width-i)*
228 if (kernel[i] == (double *) NULL)
231 j=(long) (width-i)/2;
233 for (v=(-j); v <= j; v++)
235 for (u=(-j); u <= j; u++)
237 kernel[i][k]=exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
238 (2.0*MagickPI*MagickSigma*MagickSigma);
239 normalize+=kernel[i][k];
243 if (fabs(normalize) <= MagickEpsilon)
245 normalize=1.0/normalize;
246 for (k=0; k < (j*j); k++)
247 kernel[i][k]=normalize*kernel[i][k];
249 if (i < (long) width)
251 for (i-=2; i >= 0; i-=2)
252 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
253 kernel=(double **) RelinquishMagickMemory(kernel);
254 edge_image=DestroyImage(edge_image);
255 blur_image=DestroyImage(blur_image);
256 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
259 Adaptively blur image.
263 GetMagickPixelPacket(image,&bias);
264 SetMagickPixelPacketBias(image,&bias);
265 image_view=AcquireCacheView(image);
266 edge_view=AcquireCacheView(edge_image);
267 blur_view=AcquireCacheView(blur_image);
268 #if defined(MAGICKCORE_OPENMP_SUPPORT)
269 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
271 for (y=0; y < (long) blur_image->rows; y++)
273 register const IndexPacket
276 register const PixelPacket
281 *restrict blur_indexes;
289 if (status == MagickFalse)
291 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
292 q=QueueCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
294 if ((r == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
299 blur_indexes=GetCacheViewAuthenticIndexQueue(blur_view);
300 for (x=0; x < (long) blur_image->columns; x++)
309 register const double
318 i=(long) (width*QuantumScale*PixelIntensity(r)+0.5);
322 if (i > (long) width)
326 p=GetCacheViewVirtualPixels(image_view,x-((long) (width-i)/2L),y-(long)
327 ((width-i)/2L),width-i,width-i,exception);
328 if (p == (const PixelPacket *) NULL)
330 indexes=GetCacheViewVirtualIndexQueue(image_view);
333 for (v=0; v < (long) (width-i); v++)
335 for (u=0; u < (long) (width-i); u++)
338 if (((channel & OpacityChannel) != 0) &&
339 (image->matte != MagickFalse))
340 alpha=(MagickRealType) (QuantumScale*GetAlphaPixelComponent(p));
341 if ((channel & RedChannel) != 0)
342 pixel.red+=(*k)*alpha*GetRedPixelComponent(p);
343 if ((channel & GreenChannel) != 0)
344 pixel.green+=(*k)*alpha*GetGreenPixelComponent(p);
345 if ((channel & BlueChannel) != 0)
346 pixel.blue+=(*k)*alpha*GetBluePixelComponent(p);
347 if ((channel & OpacityChannel) != 0)
348 pixel.opacity+=(*k)*GetOpacityPixelComponent(p);
349 if (((channel & IndexChannel) != 0) &&
350 (image->colorspace == CMYKColorspace))
351 pixel.index+=(*k)*alpha*indexes[x+(width-i)*v+u];
357 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
358 if ((channel & RedChannel) != 0)
359 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
360 if ((channel & GreenChannel) != 0)
361 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
362 if ((channel & BlueChannel) != 0)
363 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
364 if ((channel & OpacityChannel) != 0)
365 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
366 if (((channel & IndexChannel) != 0) &&
367 (image->colorspace == CMYKColorspace))
368 blur_indexes[x]=ClampToQuantum(gamma*GetIndexPixelComponent(&pixel));
372 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
374 if (image->progress_monitor != (MagickProgressMonitor) NULL)
379 #if defined(MAGICKCORE_OPENMP_SUPPORT)
380 #pragma omp critical (MagickCore_AdaptiveBlurImageChannel)
382 proceed=SetImageProgress(image,AdaptiveBlurImageTag,progress++,
384 if (proceed == MagickFalse)
388 blur_image->type=image->type;
389 blur_view=DestroyCacheView(blur_view);
390 edge_view=DestroyCacheView(edge_view);
391 image_view=DestroyCacheView(image_view);
392 edge_image=DestroyImage(edge_image);
393 for (i=0; i < (long) width; i+=2)
394 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
395 kernel=(double **) RelinquishMagickMemory(kernel);
396 if (status == MagickFalse)
397 blur_image=DestroyImage(blur_image);
402 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
406 % A d a p t i v e S h a r p e n I m a g e %
410 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
412 % AdaptiveSharpenImage() adaptively sharpens the image by sharpening more
413 % intensely near image edges and less intensely far from edges. We sharpen the
414 % image with a Gaussian operator of the given radius and standard deviation
415 % (sigma). For reasonable results, radius should be larger than sigma. Use a
416 % radius of 0 and AdaptiveSharpenImage() selects a suitable radius for you.
418 % The format of the AdaptiveSharpenImage method is:
420 % Image *AdaptiveSharpenImage(const Image *image,const double radius,
421 % const double sigma,ExceptionInfo *exception)
422 % Image *AdaptiveSharpenImageChannel(const Image *image,
423 % const ChannelType channel,double radius,const double sigma,
424 % ExceptionInfo *exception)
426 % A description of each parameter follows:
428 % o image: the image.
430 % o channel: the channel type.
432 % o radius: the radius of the Gaussian, in pixels, not counting the center
435 % o sigma: the standard deviation of the Laplacian, in pixels.
437 % o exception: return any errors or warnings in this structure.
441 MagickExport Image *AdaptiveSharpenImage(const Image *image,const double radius,
442 const double sigma,ExceptionInfo *exception)
447 sharp_image=AdaptiveSharpenImageChannel(image,DefaultChannels,radius,sigma,
452 MagickExport Image *AdaptiveSharpenImageChannel(const Image *image,
453 const ChannelType channel,const double radius,const double sigma,
454 ExceptionInfo *exception)
456 #define AdaptiveSharpenImageTag "Convolve/Image"
457 #define MagickSigma (fabs(sigma) <= MagickEpsilon ? 1.0 : sigma)
493 assert(image != (const Image *) NULL);
494 assert(image->signature == MagickSignature);
495 if (image->debug != MagickFalse)
496 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
497 assert(exception != (ExceptionInfo *) NULL);
498 assert(exception->signature == MagickSignature);
499 sharp_image=CloneImage(image,0,0,MagickTrue,exception);
500 if (sharp_image == (Image *) NULL)
501 return((Image *) NULL);
502 if (fabs(sigma) <= MagickEpsilon)
504 if (SetImageStorageClass(sharp_image,DirectClass) == MagickFalse)
506 InheritException(exception,&sharp_image->exception);
507 sharp_image=DestroyImage(sharp_image);
508 return((Image *) NULL);
511 Edge detect the image brighness channel, level, sharp, and level again.
513 edge_image=EdgeImage(image,radius,exception);
514 if (edge_image == (Image *) NULL)
516 sharp_image=DestroyImage(sharp_image);
517 return((Image *) NULL);
519 (void) LevelImage(edge_image,"20%,95%");
520 gaussian_image=GaussianBlurImage(edge_image,radius,sigma,exception);
521 if (gaussian_image != (Image *) NULL)
523 edge_image=DestroyImage(edge_image);
524 edge_image=gaussian_image;
526 (void) LevelImage(edge_image,"10%,95%");
528 Create a set of kernels from maximum (radius,sigma) to minimum.
530 width=GetOptimalKernelWidth2D(radius,sigma);
531 kernel=(double **) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
532 if (kernel == (double **) NULL)
534 edge_image=DestroyImage(edge_image);
535 sharp_image=DestroyImage(sharp_image);
536 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
538 (void) ResetMagickMemory(kernel,0,(size_t) width*sizeof(*kernel));
539 for (i=0; i < (long) width; i+=2)
541 kernel[i]=(double *) AcquireQuantumMemory((size_t) (width-i),(width-i)*
543 if (kernel[i] == (double *) NULL)
546 j=(long) (width-i)/2;
548 for (v=(-j); v <= j; v++)
550 for (u=(-j); u <= j; u++)
552 kernel[i][k]=(-exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
553 (2.0*MagickPI*MagickSigma*MagickSigma));
554 normalize+=kernel[i][k];
558 if (fabs(normalize) <= MagickEpsilon)
560 normalize=1.0/normalize;
561 for (k=0; k < (j*j); k++)
562 kernel[i][k]=normalize*kernel[i][k];
564 if (i < (long) width)
566 for (i-=2; i >= 0; i-=2)
567 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
568 kernel=(double **) RelinquishMagickMemory(kernel);
569 edge_image=DestroyImage(edge_image);
570 sharp_image=DestroyImage(sharp_image);
571 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
574 Adaptively sharpen image.
578 GetMagickPixelPacket(image,&bias);
579 SetMagickPixelPacketBias(image,&bias);
580 image_view=AcquireCacheView(image);
581 edge_view=AcquireCacheView(edge_image);
582 sharp_view=AcquireCacheView(sharp_image);
583 #if defined(MAGICKCORE_OPENMP_SUPPORT)
584 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
586 for (y=0; y < (long) sharp_image->rows; y++)
588 register const IndexPacket
591 register const PixelPacket
596 *restrict sharp_indexes;
604 if (status == MagickFalse)
606 r=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns,1,exception);
607 q=QueueCacheViewAuthenticPixels(sharp_view,0,y,sharp_image->columns,1,
609 if ((r == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
614 sharp_indexes=GetCacheViewAuthenticIndexQueue(sharp_view);
615 for (x=0; x < (long) sharp_image->columns; x++)
624 register const double
633 i=(long) (width*(QuantumRange-QuantumScale*PixelIntensity(r))+0.5);
637 if (i > (long) width)
641 p=GetCacheViewVirtualPixels(image_view,x-((long) (width-i)/2L),y-(long)
642 ((width-i)/2L),width-i,width-i,exception);
643 if (p == (const PixelPacket *) NULL)
645 indexes=GetCacheViewVirtualIndexQueue(image_view);
648 for (v=0; v < (long) (width-i); v++)
650 for (u=0; u < (long) (width-i); u++)
653 if (((channel & OpacityChannel) != 0) &&
654 (image->matte != MagickFalse))
655 alpha=(MagickRealType) (QuantumScale*GetAlphaPixelComponent(p));
656 if ((channel & RedChannel) != 0)
657 pixel.red+=(*k)*alpha*GetRedPixelComponent(p);
658 if ((channel & GreenChannel) != 0)
659 pixel.green+=(*k)*alpha*GetGreenPixelComponent(p);
660 if ((channel & BlueChannel) != 0)
661 pixel.blue+=(*k)*alpha*GetBluePixelComponent(p);
662 if ((channel & OpacityChannel) != 0)
663 pixel.opacity+=(*k)*GetOpacityPixelComponent(p);
664 if (((channel & IndexChannel) != 0) &&
665 (image->colorspace == CMYKColorspace))
666 pixel.index+=(*k)*alpha*indexes[x+(width-i)*v+u];
672 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
673 if ((channel & RedChannel) != 0)
674 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
675 if ((channel & GreenChannel) != 0)
676 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
677 if ((channel & BlueChannel) != 0)
678 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
679 if ((channel & OpacityChannel) != 0)
680 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
681 if (((channel & IndexChannel) != 0) &&
682 (image->colorspace == CMYKColorspace))
683 sharp_indexes[x]=ClampToQuantum(gamma*GetIndexPixelComponent(&pixel));
687 if (SyncCacheViewAuthenticPixels(sharp_view,exception) == MagickFalse)
689 if (image->progress_monitor != (MagickProgressMonitor) NULL)
694 #if defined(MAGICKCORE_OPENMP_SUPPORT)
695 #pragma omp critical (MagickCore_AdaptiveSharpenImageChannel)
697 proceed=SetImageProgress(image,AdaptiveSharpenImageTag,progress++,
699 if (proceed == MagickFalse)
703 sharp_image->type=image->type;
704 sharp_view=DestroyCacheView(sharp_view);
705 edge_view=DestroyCacheView(edge_view);
706 image_view=DestroyCacheView(image_view);
707 edge_image=DestroyImage(edge_image);
708 for (i=0; i < (long) width; i+=2)
709 kernel[i]=(double *) RelinquishMagickMemory(kernel[i]);
710 kernel=(double **) RelinquishMagickMemory(kernel);
711 if (status == MagickFalse)
712 sharp_image=DestroyImage(sharp_image);
717 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
721 % B l u r I m a g e %
725 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
727 % BlurImage() blurs an image. We convolve the image with a Gaussian operator
728 % of the given radius and standard deviation (sigma). For reasonable results,
729 % the radius should be larger than sigma. Use a radius of 0 and BlurImage()
730 % selects a suitable radius for you.
732 % BlurImage() differs from GaussianBlurImage() in that it uses a separable
733 % kernel which is faster but mathematically equivalent to the non-separable
736 % The format of the BlurImage method is:
738 % Image *BlurImage(const Image *image,const double radius,
739 % const double sigma,ExceptionInfo *exception)
740 % Image *BlurImageChannel(const Image *image,const ChannelType channel,
741 % const double radius,const double sigma,ExceptionInfo *exception)
743 % A description of each parameter follows:
745 % o image: the image.
747 % o channel: the channel type.
749 % o radius: the radius of the Gaussian, in pixels, not counting the center
752 % o sigma: the standard deviation of the Gaussian, in pixels.
754 % o exception: return any errors or warnings in this structure.
758 MagickExport Image *BlurImage(const Image *image,const double radius,
759 const double sigma,ExceptionInfo *exception)
764 blur_image=BlurImageChannel(image,DefaultChannels,radius,sigma,exception);
768 static double *GetBlurKernel(const unsigned long width,const double sigma)
782 Generate a 1-D convolution kernel.
784 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
785 kernel=(double *) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
786 if (kernel == (double *) NULL)
791 for (k=(-j); k <= j; k++)
793 kernel[i]=exp(-((double) k*k)/(2.0*MagickSigma*MagickSigma))/
794 (MagickSQ2PI*MagickSigma);
795 normalize+=kernel[i];
798 for (i=0; i < (long) width; i++)
799 kernel[i]/=normalize;
803 MagickExport Image *BlurImageChannel(const Image *image,
804 const ChannelType channel,const double radius,const double sigma,
805 ExceptionInfo *exception)
807 #define BlurImageTag "Blur/Image"
837 Initialize blur image attributes.
839 assert(image != (Image *) NULL);
840 assert(image->signature == MagickSignature);
841 if (image->debug != MagickFalse)
842 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
843 assert(exception != (ExceptionInfo *) NULL);
844 assert(exception->signature == MagickSignature);
845 blur_image=CloneImage(image,0,0,MagickTrue,exception);
846 if (blur_image == (Image *) NULL)
847 return((Image *) NULL);
848 if (fabs(sigma) <= MagickEpsilon)
850 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
852 InheritException(exception,&blur_image->exception);
853 blur_image=DestroyImage(blur_image);
854 return((Image *) NULL);
856 width=GetOptimalKernelWidth1D(radius,sigma);
857 kernel=GetBlurKernel(width,sigma);
858 if (kernel == (double *) NULL)
860 blur_image=DestroyImage(blur_image);
861 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
863 if (image->debug != MagickFalse)
866 format[MaxTextExtent],
869 register const double
872 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
873 " BlurImage with %ld kernel:",width);
874 message=AcquireString("");
876 for (i=0; i < (long) width; i++)
879 (void) FormatMagickString(format,MaxTextExtent,"%ld: ",i);
880 (void) ConcatenateString(&message,format);
881 (void) FormatMagickString(format,MaxTextExtent,"%g ",*k++);
882 (void) ConcatenateString(&message,format);
883 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
885 message=DestroyString(message);
892 GetMagickPixelPacket(image,&bias);
893 SetMagickPixelPacketBias(image,&bias);
894 image_view=AcquireCacheView(image);
895 blur_view=AcquireCacheView(blur_image);
896 #if defined(MAGICKCORE_OPENMP_SUPPORT)
897 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
899 for (y=0; y < (long) blur_image->rows; y++)
901 register const IndexPacket
904 register const PixelPacket
908 *restrict blur_indexes;
916 if (status == MagickFalse)
918 p=GetCacheViewVirtualPixels(image_view,-((long) width/2L),y,image->columns+
920 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
922 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
927 indexes=GetCacheViewVirtualIndexQueue(image_view);
928 blur_indexes=GetCacheViewAuthenticIndexQueue(blur_view);
929 for (x=0; x < (long) blur_image->columns; x++)
934 register const double
937 register const PixelPacket
938 *restrict kernel_pixels;
946 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
948 for (i=0; i < (long) width; i++)
950 pixel.red+=(*k)*kernel_pixels->red;
951 pixel.green+=(*k)*kernel_pixels->green;
952 pixel.blue+=(*k)*kernel_pixels->blue;
956 if ((channel & RedChannel) != 0)
957 SetRedPixelComponent(q,ClampRedPixelComponent(&pixel));
958 if ((channel & GreenChannel) != 0)
959 SetGreenPixelComponent(q,ClampGreenPixelComponent(&pixel));
960 if ((channel & BlueChannel) != 0)
961 SetBluePixelComponent(q,ClampBluePixelComponent(&pixel));
962 if ((channel & OpacityChannel) != 0)
966 for (i=0; i < (long) width; i++)
968 pixel.opacity+=(*k)*kernel_pixels->opacity;
972 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
974 if (((channel & IndexChannel) != 0) &&
975 (image->colorspace == CMYKColorspace))
977 register const IndexPacket
978 *restrict kernel_indexes;
981 kernel_indexes=indexes;
982 for (i=0; i < (long) width; i++)
984 pixel.index+=(*k)*(*kernel_indexes);
988 blur_indexes[x]=ClampToQuantum(pixel.index);
998 for (i=0; i < (long) width; i++)
1000 alpha=(MagickRealType) (QuantumScale*
1001 GetAlphaPixelComponent(kernel_pixels));
1002 pixel.red+=(*k)*alpha*kernel_pixels->red;
1003 pixel.green+=(*k)*alpha*kernel_pixels->green;
1004 pixel.blue+=(*k)*alpha*kernel_pixels->blue;
1009 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1010 if ((channel & RedChannel) != 0)
1011 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
1012 if ((channel & GreenChannel) != 0)
1013 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
1014 if ((channel & BlueChannel) != 0)
1015 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
1016 if ((channel & OpacityChannel) != 0)
1020 for (i=0; i < (long) width; i++)
1022 pixel.opacity+=(*k)*kernel_pixels->opacity;
1026 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
1028 if (((channel & IndexChannel) != 0) &&
1029 (image->colorspace == CMYKColorspace))
1031 register const IndexPacket
1032 *restrict kernel_indexes;
1036 kernel_indexes=indexes;
1037 for (i=0; i < (long) width; i++)
1039 alpha=(MagickRealType) (QuantumScale*
1040 GetAlphaPixelComponent(kernel_pixels));
1041 pixel.index+=(*k)*alpha*(*kernel_indexes);
1046 blur_indexes[x]=ClampToQuantum(gamma*
1047 GetIndexPixelComponent(&pixel));
1053 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1055 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1060 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1061 #pragma omp critical (MagickCore_BlurImageChannel)
1063 proceed=SetImageProgress(image,BlurImageTag,progress++,blur_image->rows+
1064 blur_image->columns);
1065 if (proceed == MagickFalse)
1069 blur_view=DestroyCacheView(blur_view);
1070 image_view=DestroyCacheView(image_view);
1074 image_view=AcquireCacheView(blur_image);
1075 blur_view=AcquireCacheView(blur_image);
1076 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1077 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1079 for (x=0; x < (long) blur_image->columns; x++)
1081 register const IndexPacket
1084 register const PixelPacket
1087 register IndexPacket
1088 *restrict blur_indexes;
1093 register PixelPacket
1096 if (status == MagickFalse)
1098 p=GetCacheViewVirtualPixels(image_view,x,-((long) width/2L),1,image->rows+
1100 q=GetCacheViewAuthenticPixels(blur_view,x,0,1,blur_image->rows,exception);
1101 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
1106 indexes=GetCacheViewVirtualIndexQueue(image_view);
1107 blur_indexes=GetCacheViewAuthenticIndexQueue(blur_view);
1108 for (y=0; y < (long) blur_image->rows; y++)
1113 register const double
1116 register const PixelPacket
1117 *restrict kernel_pixels;
1125 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
1127 for (i=0; i < (long) width; i++)
1129 pixel.red+=(*k)*kernel_pixels->red;
1130 pixel.green+=(*k)*kernel_pixels->green;
1131 pixel.blue+=(*k)*kernel_pixels->blue;
1135 if ((channel & RedChannel) != 0)
1136 SetRedPixelComponent(q,ClampRedPixelComponent(&pixel));
1137 if ((channel & GreenChannel) != 0)
1138 SetGreenPixelComponent(q,ClampGreenPixelComponent(&pixel));
1139 if ((channel & BlueChannel) != 0)
1140 SetBluePixelComponent(q,ClampBluePixelComponent(&pixel));
1141 if ((channel & OpacityChannel) != 0)
1145 for (i=0; i < (long) width; i++)
1147 pixel.opacity+=(*k)*kernel_pixels->opacity;
1151 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
1153 if (((channel & IndexChannel) != 0) &&
1154 (image->colorspace == CMYKColorspace))
1156 register const IndexPacket
1157 *restrict kernel_indexes;
1160 kernel_indexes=indexes;
1161 for (i=0; i < (long) width; i++)
1163 pixel.index+=(*k)*(*kernel_indexes);
1167 blur_indexes[y]=ClampToQuantum(pixel.index);
1177 for (i=0; i < (long) width; i++)
1179 alpha=(MagickRealType) (QuantumScale*
1180 GetAlphaPixelComponent(kernel_pixels));
1181 pixel.red+=(*k)*alpha*kernel_pixels->red;
1182 pixel.green+=(*k)*alpha*kernel_pixels->green;
1183 pixel.blue+=(*k)*alpha*kernel_pixels->blue;
1188 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1189 if ((channel & RedChannel) != 0)
1190 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
1191 if ((channel & GreenChannel) != 0)
1192 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
1193 if ((channel & BlueChannel) != 0)
1194 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
1195 if ((channel & OpacityChannel) != 0)
1199 for (i=0; i < (long) width; i++)
1201 pixel.opacity+=(*k)*kernel_pixels->opacity;
1205 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
1207 if (((channel & IndexChannel) != 0) &&
1208 (image->colorspace == CMYKColorspace))
1210 register const IndexPacket
1211 *restrict kernel_indexes;
1215 kernel_indexes=indexes;
1216 for (i=0; i < (long) width; i++)
1218 alpha=(MagickRealType) (QuantumScale*
1219 GetAlphaPixelComponent(kernel_pixels));
1220 pixel.index+=(*k)*alpha*(*kernel_indexes);
1225 blur_indexes[y]=ClampToQuantum(gamma*
1226 GetIndexPixelComponent(&pixel));
1232 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
1234 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1239 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1240 #pragma omp critical (MagickCore_BlurImageChannel)
1242 proceed=SetImageProgress(image,BlurImageTag,progress++,blur_image->rows+
1243 blur_image->columns);
1244 if (proceed == MagickFalse)
1248 blur_view=DestroyCacheView(blur_view);
1249 image_view=DestroyCacheView(image_view);
1250 kernel=(double *) RelinquishMagickMemory(kernel);
1251 if (status == MagickFalse)
1252 blur_image=DestroyImage(blur_image);
1253 blur_image->type=image->type;
1258 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1262 % C o n v o l v e I m a g e %
1266 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1268 % ConvolveImage() applies a custom convolution kernel to the image.
1270 % The format of the ConvolveImage method is:
1272 % Image *ConvolveImage(const Image *image,const unsigned long order,
1273 % const double *kernel,ExceptionInfo *exception)
1274 % Image *ConvolveImageChannel(const Image *image,const ChannelType channel,
1275 % const unsigned long order,const double *kernel,
1276 % ExceptionInfo *exception)
1278 % A description of each parameter follows:
1280 % o image: the image.
1282 % o channel: the channel type.
1284 % o order: the number of columns and rows in the filter kernel.
1286 % o kernel: An array of double representing the convolution kernel.
1288 % o exception: return any errors or warnings in this structure.
1292 MagickExport Image *ConvolveImage(const Image *image,const unsigned long order,
1293 const double *kernel,ExceptionInfo *exception)
1298 convolve_image=ConvolveImageChannel(image,DefaultChannels,order,kernel,
1300 return(convolve_image);
1303 MagickExport Image *ConvolveImageChannel(const Image *image,
1304 const ChannelType channel,const unsigned long order,const double *kernel,
1305 ExceptionInfo *exception)
1307 #define ConvolveImageTag "Convolve/Image"
1339 Initialize convolve image attributes.
1341 assert(image != (Image *) NULL);
1342 assert(image->signature == MagickSignature);
1343 if (image->debug != MagickFalse)
1344 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1345 assert(exception != (ExceptionInfo *) NULL);
1346 assert(exception->signature == MagickSignature);
1348 if ((width % 2) == 0)
1349 ThrowImageException(OptionError,"KernelWidthMustBeAnOddNumber");
1350 convolve_image=CloneImage(image,0,0,MagickTrue,exception);
1351 if (convolve_image == (Image *) NULL)
1352 return((Image *) NULL);
1353 if (SetImageStorageClass(convolve_image,DirectClass) == MagickFalse)
1355 InheritException(exception,&convolve_image->exception);
1356 convolve_image=DestroyImage(convolve_image);
1357 return((Image *) NULL);
1359 if (image->debug != MagickFalse)
1362 format[MaxTextExtent],
1369 register const double
1372 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
1373 " ConvolveImage with %ldx%ld kernel:",width,width);
1374 message=AcquireString("");
1376 for (v=0; v < (long) width; v++)
1379 (void) FormatMagickString(format,MaxTextExtent,"%ld: ",v);
1380 (void) ConcatenateString(&message,format);
1381 for (u=0; u < (long) width; u++)
1383 (void) FormatMagickString(format,MaxTextExtent,"%g ",*k++);
1384 (void) ConcatenateString(&message,format);
1386 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
1388 message=DestroyString(message);
1393 normal_kernel=(double *) AcquireQuantumMemory(width*width,
1394 sizeof(*normal_kernel));
1395 if (normal_kernel == (double *) NULL)
1397 convolve_image=DestroyImage(convolve_image);
1398 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1401 for (i=0; i < (long) (width*width); i++)
1403 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1404 for (i=0; i < (long) (width*width); i++)
1405 normal_kernel[i]=gamma*kernel[i];
1411 GetMagickPixelPacket(image,&bias);
1412 SetMagickPixelPacketBias(image,&bias);
1413 image_view=AcquireCacheView(image);
1414 convolve_view=AcquireCacheView(convolve_image);
1415 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1416 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1418 for (y=0; y < (long) image->rows; y++)
1423 register const IndexPacket
1426 register const PixelPacket
1429 register IndexPacket
1430 *restrict convolve_indexes;
1435 register PixelPacket
1438 if (status == MagickFalse)
1440 p=GetCacheViewVirtualPixels(image_view,-((long) width/2L),y-(long) (width/
1441 2L),image->columns+width,width,exception);
1442 q=GetCacheViewAuthenticPixels(convolve_view,0,y,convolve_image->columns,1,
1444 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
1449 indexes=GetCacheViewVirtualIndexQueue(image_view);
1450 convolve_indexes=GetCacheViewAuthenticIndexQueue(convolve_view);
1451 for (x=0; x < (long) image->columns; x++)
1459 register const double
1462 register const PixelPacket
1463 *restrict kernel_pixels;
1471 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
1473 for (v=0; v < (long) width; v++)
1475 for (u=0; u < (long) width; u++)
1477 pixel.red+=(*k)*kernel_pixels[u].red;
1478 pixel.green+=(*k)*kernel_pixels[u].green;
1479 pixel.blue+=(*k)*kernel_pixels[u].blue;
1482 kernel_pixels+=image->columns+width;
1484 if ((channel & RedChannel) != 0)
1485 SetRedPixelComponent(q,ClampRedPixelComponent(&pixel));
1486 if ((channel & GreenChannel) != 0)
1487 SetGreenPixelComponent(q,ClampGreenPixelComponent(&pixel));
1488 if ((channel & BlueChannel) != 0)
1489 SetBluePixelComponent(q,ClampBluePixelComponent(&pixel));
1490 if ((channel & OpacityChannel) != 0)
1494 for (v=0; v < (long) width; v++)
1496 for (u=0; u < (long) width; u++)
1498 pixel.opacity+=(*k)*kernel_pixels[u].opacity;
1501 kernel_pixels+=image->columns+width;
1503 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
1505 if (((channel & IndexChannel) != 0) &&
1506 (image->colorspace == CMYKColorspace))
1508 register const IndexPacket
1509 *restrict kernel_indexes;
1512 kernel_indexes=indexes;
1513 for (v=0; v < (long) width; v++)
1515 for (u=0; u < (long) width; u++)
1517 pixel.index+=(*k)*kernel_indexes[u];
1520 kernel_indexes+=image->columns+width;
1522 convolve_indexes[x]=ClampToQuantum(pixel.index);
1532 for (v=0; v < (long) width; v++)
1534 for (u=0; u < (long) width; u++)
1536 alpha=(MagickRealType) (QuantumScale*(QuantumRange-
1537 kernel_pixels[u].opacity));
1538 pixel.red+=(*k)*alpha*kernel_pixels[u].red;
1539 pixel.green+=(*k)*alpha*kernel_pixels[u].green;
1540 pixel.blue+=(*k)*alpha*kernel_pixels[u].blue;
1544 kernel_pixels+=image->columns+width;
1546 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1547 if ((channel & RedChannel) != 0)
1548 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
1549 if ((channel & GreenChannel) != 0)
1550 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
1551 if ((channel & BlueChannel) != 0)
1552 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
1553 if ((channel & OpacityChannel) != 0)
1557 for (v=0; v < (long) width; v++)
1559 for (u=0; u < (long) width; u++)
1561 pixel.opacity+=(*k)*kernel_pixels[u].opacity;
1564 kernel_pixels+=image->columns+width;
1566 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
1568 if (((channel & IndexChannel) != 0) &&
1569 (image->colorspace == CMYKColorspace))
1571 register const IndexPacket
1572 *restrict kernel_indexes;
1576 kernel_indexes=indexes;
1577 for (v=0; v < (long) width; v++)
1579 for (u=0; u < (long) width; u++)
1581 alpha=(MagickRealType) (QuantumScale*(QuantumRange-
1582 kernel_pixels[u].opacity));
1583 pixel.index+=(*k)*alpha*kernel_indexes[u];
1586 kernel_pixels+=image->columns+width;
1587 kernel_indexes+=image->columns+width;
1589 convolve_indexes[x]=ClampToQuantum(gamma*
1590 GetIndexPixelComponent(&pixel));
1596 sync=SyncCacheViewAuthenticPixels(convolve_view,exception);
1597 if (sync == MagickFalse)
1599 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1604 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1605 #pragma omp critical (MagickCore_ConvolveImageChannel)
1607 proceed=SetImageProgress(image,ConvolveImageTag,progress++,image->rows);
1608 if (proceed == MagickFalse)
1612 convolve_image->type=image->type;
1613 convolve_view=DestroyCacheView(convolve_view);
1614 image_view=DestroyCacheView(image_view);
1615 normal_kernel=(double *) RelinquishMagickMemory(normal_kernel);
1616 if (status == MagickFalse)
1617 convolve_image=DestroyImage(convolve_image);
1618 return(convolve_image);
1622 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1626 % D e s p e c k l e I m a g e %
1630 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1632 % DespeckleImage() reduces the speckle noise in an image while perserving the
1633 % edges of the original image.
1635 % The format of the DespeckleImage method is:
1637 % Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1639 % A description of each parameter follows:
1641 % o image: the image.
1643 % o exception: return any errors or warnings in this structure.
1647 static Quantum **DestroyPixelThreadSet(Quantum **pixels)
1652 assert(pixels != (Quantum **) NULL);
1653 for (i=0; i < (long) GetOpenMPMaximumThreads(); i++)
1654 if (pixels[i] != (Quantum *) NULL)
1655 pixels[i]=(Quantum *) RelinquishMagickMemory(pixels[i]);
1656 pixels=(Quantum **) RelinquishAlignedMemory(pixels);
1660 static Quantum **AcquirePixelThreadSet(const size_t count)
1671 number_threads=GetOpenMPMaximumThreads();
1672 pixels=(Quantum **) AcquireAlignedMemory(number_threads,sizeof(*pixels));
1673 if (pixels == (Quantum **) NULL)
1674 return((Quantum **) NULL);
1675 (void) ResetMagickMemory(pixels,0,number_threads*sizeof(*pixels));
1676 for (i=0; i < (long) number_threads; i++)
1678 pixels[i]=(Quantum *) AcquireQuantumMemory(count,sizeof(**pixels));
1679 if (pixels[i] == (Quantum *) NULL)
1680 return(DestroyPixelThreadSet(pixels));
1685 static void Hull(const long x_offset,const long y_offset,
1686 const unsigned long columns,const unsigned long rows,Quantum *f,Quantum *g,
1704 assert(f != (Quantum *) NULL);
1705 assert(g != (Quantum *) NULL);
1708 r=p+(y_offset*((long) columns+2)+x_offset);
1709 for (y=0; y < (long) rows; y++)
1715 for (x=(long) columns; x != 0; x--)
1717 v=(MagickRealType) (*p);
1718 if ((MagickRealType) *r >= (v+(MagickRealType) ScaleCharToQuantum(2)))
1719 v+=ScaleCharToQuantum(1);
1726 for (x=(long) columns; x != 0; x--)
1728 v=(MagickRealType) (*p);
1729 if ((MagickRealType) *r <= (v-(MagickRealType) ScaleCharToQuantum(2)))
1730 v-=(long) ScaleCharToQuantum(1);
1742 r=q+(y_offset*((long) columns+2)+x_offset);
1743 s=q-(y_offset*((long) columns+2)+x_offset);
1744 for (y=0; y < (long) rows; y++)
1751 for (x=(long) columns; x != 0; x--)
1753 v=(MagickRealType) (*q);
1754 if (((MagickRealType) *s >=
1755 (v+(MagickRealType) ScaleCharToQuantum(2))) &&
1756 ((MagickRealType) *r > v))
1757 v+=ScaleCharToQuantum(1);
1765 for (x=(long) columns; x != 0; x--)
1767 v=(MagickRealType) (*q);
1768 if (((MagickRealType) *s <=
1769 (v-(MagickRealType) ScaleCharToQuantum(2))) &&
1770 ((MagickRealType) *r < v))
1771 v-=(MagickRealType) ScaleCharToQuantum(1);
1785 MagickExport Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
1787 #define DespeckleImageTag "Despeckle/Image"
1810 X[4] = {0, 1, 1,-1},
1811 Y[4] = {1, 0, 1, 1};
1814 Allocate despeckled image.
1816 assert(image != (const Image *) NULL);
1817 assert(image->signature == MagickSignature);
1818 if (image->debug != MagickFalse)
1819 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1820 assert(exception != (ExceptionInfo *) NULL);
1821 assert(exception->signature == MagickSignature);
1822 despeckle_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1824 if (despeckle_image == (Image *) NULL)
1825 return((Image *) NULL);
1826 if (SetImageStorageClass(despeckle_image,DirectClass) == MagickFalse)
1828 InheritException(exception,&despeckle_image->exception);
1829 despeckle_image=DestroyImage(despeckle_image);
1830 return((Image *) NULL);
1833 Allocate image buffers.
1835 length=(size_t) ((image->columns+2)*(image->rows+2));
1836 pixels=AcquirePixelThreadSet(length);
1837 buffers=AcquirePixelThreadSet(length);
1838 if ((pixels == (Quantum **) NULL) || (buffers == (Quantum **) NULL))
1840 if (buffers != (Quantum **) NULL)
1841 buffers=DestroyPixelThreadSet(buffers);
1842 if (pixels != (Quantum **) NULL)
1843 pixels=DestroyPixelThreadSet(pixels);
1844 despeckle_image=DestroyImage(despeckle_image);
1845 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1848 Reduce speckle in the image.
1851 image_view=AcquireCacheView(image);
1852 despeckle_view=AcquireCacheView(despeckle_image);
1853 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1854 #pragma omp parallel for schedule(dynamic,4) shared(status)
1856 for (channel=0; channel <= 3; channel++)
1871 if (status == MagickFalse)
1873 id=GetOpenMPThreadId();
1875 (void) ResetMagickMemory(pixel,0,length*sizeof(*pixel));
1877 j=(long) image->columns+2;
1878 for (y=0; y < (long) image->rows; y++)
1880 register const PixelPacket
1883 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1884 if (p == (const PixelPacket *) NULL)
1887 for (x=0; x < (long) image->columns; x++)
1891 case 0: pixel[j]=GetRedPixelComponent(p); break;
1892 case 1: pixel[j]=GetGreenPixelComponent(p); break;
1893 case 2: pixel[j]=GetBluePixelComponent(p); break;
1894 case 3: pixel[j]=GetOpacityPixelComponent(p); break;
1902 (void) ResetMagickMemory(buffer,0,length*sizeof(*buffer));
1903 for (i=0; i < 4; i++)
1905 Hull(X[i],Y[i],image->columns,image->rows,pixel,buffer,1);
1906 Hull(-X[i],-Y[i],image->columns,image->rows,pixel,buffer,1);
1907 Hull(-X[i],-Y[i],image->columns,image->rows,pixel,buffer,-1);
1908 Hull(X[i],Y[i],image->columns,image->rows,pixel,buffer,-1);
1910 j=(long) image->columns+2;
1911 for (y=0; y < (long) image->rows; y++)
1916 register PixelPacket
1919 q=GetCacheViewAuthenticPixels(despeckle_view,0,y,despeckle_image->columns,
1921 if (q == (PixelPacket *) NULL)
1924 for (x=0; x < (long) image->columns; x++)
1928 case 0: q->red=pixel[j]; break;
1929 case 1: q->green=pixel[j]; break;
1930 case 2: q->blue=pixel[j]; break;
1931 case 3: q->opacity=pixel[j]; break;
1937 sync=SyncCacheViewAuthenticPixels(despeckle_view,exception);
1938 if (sync == MagickFalse)
1945 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1950 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1951 #pragma omp critical (MagickCore_DespeckleImage)
1953 proceed=SetImageProgress(image,DespeckleImageTag,channel,3);
1954 if (proceed == MagickFalse)
1958 despeckle_view=DestroyCacheView(despeckle_view);
1959 image_view=DestroyCacheView(image_view);
1960 buffers=DestroyPixelThreadSet(buffers);
1961 pixels=DestroyPixelThreadSet(pixels);
1962 despeckle_image->type=image->type;
1963 if (status == MagickFalse)
1964 despeckle_image=DestroyImage(despeckle_image);
1965 return(despeckle_image);
1969 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1973 % E d g e I m a g e %
1977 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1979 % EdgeImage() finds edges in an image. Radius defines the radius of the
1980 % convolution filter. Use a radius of 0 and EdgeImage() selects a suitable
1983 % The format of the EdgeImage method is:
1985 % Image *EdgeImage(const Image *image,const double radius,
1986 % ExceptionInfo *exception)
1988 % A description of each parameter follows:
1990 % o image: the image.
1992 % o radius: the radius of the pixel neighborhood.
1994 % o exception: return any errors or warnings in this structure.
1997 MagickExport Image *EdgeImage(const Image *image,const double radius,
1998 ExceptionInfo *exception)
2012 assert(image != (const Image *) NULL);
2013 assert(image->signature == MagickSignature);
2014 if (image->debug != MagickFalse)
2015 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2016 assert(exception != (ExceptionInfo *) NULL);
2017 assert(exception->signature == MagickSignature);
2018 width=GetOptimalKernelWidth1D(radius,0.5);
2019 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
2020 if (kernel == (double *) NULL)
2021 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2022 for (i=0; i < (long) (width*width); i++)
2024 kernel[i/2]=(double) (width*width-1.0);
2025 edge_image=ConvolveImage(image,width,kernel,exception);
2026 kernel=(double *) RelinquishMagickMemory(kernel);
2031 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2035 % E m b o s s I m a g e %
2039 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2041 % EmbossImage() returns a grayscale image with a three-dimensional effect.
2042 % We convolve the image with a Gaussian operator of the given radius and
2043 % standard deviation (sigma). For reasonable results, radius should be
2044 % larger than sigma. Use a radius of 0 and Emboss() selects a suitable
2047 % The format of the EmbossImage method is:
2049 % Image *EmbossImage(const Image *image,const double radius,
2050 % const double sigma,ExceptionInfo *exception)
2052 % A description of each parameter follows:
2054 % o image: the image.
2056 % o radius: the radius of the pixel neighborhood.
2058 % o sigma: the standard deviation of the Gaussian, in pixels.
2060 % o exception: return any errors or warnings in this structure.
2063 MagickExport Image *EmbossImage(const Image *image,const double radius,
2064 const double sigma,ExceptionInfo *exception)
2084 assert(image != (Image *) NULL);
2085 assert(image->signature == MagickSignature);
2086 if (image->debug != MagickFalse)
2087 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2088 assert(exception != (ExceptionInfo *) NULL);
2089 assert(exception->signature == MagickSignature);
2090 width=GetOptimalKernelWidth2D(radius,sigma);
2091 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
2092 if (kernel == (double *) NULL)
2093 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2097 for (v=(-j); v <= j; v++)
2099 for (u=(-j); u <= j; u++)
2101 kernel[i]=((u < 0) || (v < 0) ? -8.0 : 8.0)*
2102 exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
2103 (2.0*MagickPI*MagickSigma*MagickSigma);
2110 emboss_image=ConvolveImage(image,width,kernel,exception);
2111 if (emboss_image != (Image *) NULL)
2112 (void) EqualizeImage(emboss_image);
2113 kernel=(double *) RelinquishMagickMemory(kernel);
2114 return(emboss_image);
2118 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2122 % F i l t e r I m a g e %
2126 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2128 % FilterImage() applies a custom convolution kernel to the image.
2130 % The format of the FilterImage method is:
2132 % Image *FilterImage(const Image *image,const MagickKernel *kernel,
2133 % ExceptionInfo *exception)
2134 % Image *FilterImageChannel(const Image *image,const ChannelType channel,
2135 % const MagickKernel *kernel,ExceptionInfo *exception)
2137 % A description of each parameter follows:
2139 % o image: the image.
2141 % o channel: the channel type.
2143 % o kernel: the filtering kernel.
2145 % o exception: return any errors or warnings in this structure.
2149 MagickExport Image *FilterImage(const Image *image,const MagickKernel *kernel,
2150 ExceptionInfo *exception)
2155 filter_image=FilterImageChannel(image,DefaultChannels,kernel,exception);
2156 return(filter_image);
2159 MagickExport Image *FilterImageChannel(const Image *image,
2160 const ChannelType channel,const MagickKernel *kernel,ExceptionInfo *exception)
2162 #define FilterImageTag "Filter/Image"
2182 Initialize filter image attributes.
2184 assert(image != (Image *) NULL);
2185 assert(image->signature == MagickSignature);
2186 if (image->debug != MagickFalse)
2187 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2188 assert(exception != (ExceptionInfo *) NULL);
2189 assert(exception->signature == MagickSignature);
2190 if ((kernel->width % 2) == 0)
2191 ThrowImageException(OptionError,"KernelWidthMustBeAnOddNumber");
2192 filter_image=CloneImage(image,0,0,MagickTrue,exception);
2193 if (filter_image == (Image *) NULL)
2194 return((Image *) NULL);
2195 if (SetImageStorageClass(filter_image,DirectClass) == MagickFalse)
2197 InheritException(exception,&filter_image->exception);
2198 filter_image=DestroyImage(filter_image);
2199 return((Image *) NULL);
2201 if (image->debug != MagickFalse)
2204 format[MaxTextExtent],
2211 register const double
2214 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
2215 " FilterImage with %ldx%ld kernel:",kernel->width,kernel->height);
2216 message=AcquireString("");
2218 for (v=0; v < (long) kernel->height; v++)
2221 (void) FormatMagickString(format,MaxTextExtent,"%ld: ",v);
2222 (void) ConcatenateString(&message,format);
2223 for (u=0; u < (long) kernel->width; u++)
2225 (void) FormatMagickString(format,MaxTextExtent,"%g ",*k++);
2226 (void) ConcatenateString(&message,format);
2228 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
2230 message=DestroyString(message);
2232 status=AccelerateConvolveImage(image,kernel,filter_image,exception);
2233 if (status == MagickTrue)
2234 return(filter_image);
2240 GetMagickPixelPacket(image,&bias);
2241 SetMagickPixelPacketBias(image,&bias);
2242 image_view=AcquireCacheView(image);
2243 filter_view=AcquireCacheView(filter_image);
2244 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2245 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2247 for (y=0; y < (long) image->rows; y++)
2252 register const IndexPacket
2255 register const PixelPacket
2258 register IndexPacket
2259 *restrict filter_indexes;
2264 register PixelPacket
2267 if (status == MagickFalse)
2269 p=GetCacheViewVirtualPixels(image_view,-((long) kernel->width/2L),
2270 y-(long) (kernel->height/2L),image->columns+kernel->width,kernel->height,
2272 q=GetCacheViewAuthenticPixels(filter_view,0,y,filter_image->columns,1,
2274 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
2279 indexes=GetCacheViewVirtualIndexQueue(image_view);
2280 filter_indexes=GetCacheViewAuthenticIndexQueue(filter_view);
2281 for (x=0; x < (long) image->columns; x++)
2289 register const double
2292 register const PixelPacket
2293 *restrict kernel_pixels;
2301 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
2303 for (v=0; v < (long) kernel->width; v++)
2305 for (u=0; u < (long) kernel->height; u++)
2307 pixel.red+=(*k)*kernel_pixels[u].red;
2308 pixel.green+=(*k)*kernel_pixels[u].green;
2309 pixel.blue+=(*k)*kernel_pixels[u].blue;
2312 kernel_pixels+=image->columns+kernel->width;
2314 if ((channel & RedChannel) != 0)
2315 SetRedPixelComponent(q,ClampRedPixelComponent(&pixel));
2316 if ((channel & GreenChannel) != 0)
2317 SetGreenPixelComponent(q,ClampGreenPixelComponent(&pixel));
2318 if ((channel & BlueChannel) != 0)
2319 SetBluePixelComponent(q,ClampBluePixelComponent(&pixel));
2320 if ((channel & OpacityChannel) != 0)
2324 for (v=0; v < (long) kernel->width; v++)
2326 for (u=0; u < (long) kernel->height; u++)
2328 pixel.opacity+=(*k)*kernel_pixels[u].opacity;
2331 kernel_pixels+=image->columns+kernel->width;
2333 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
2335 if (((channel & IndexChannel) != 0) &&
2336 (image->colorspace == CMYKColorspace))
2338 register const IndexPacket
2339 *restrict kernel_indexes;
2342 kernel_indexes=indexes;
2343 for (v=0; v < (long) kernel->width; v++)
2345 for (u=0; u < (long) kernel->height; u++)
2347 pixel.index+=(*k)*kernel_indexes[u];
2350 kernel_indexes+=image->columns+kernel->width;
2352 filter_indexes[x]=ClampToQuantum(pixel.index);
2362 for (v=0; v < (long) kernel->width; v++)
2364 for (u=0; u < (long) kernel->height; u++)
2366 alpha=(MagickRealType) (QuantumScale*(QuantumRange-
2367 kernel_pixels[u].opacity));
2368 pixel.red+=(*k)*alpha*kernel_pixels[u].red;
2369 pixel.green+=(*k)*alpha*kernel_pixels[u].green;
2370 pixel.blue+=(*k)*alpha*kernel_pixels[u].blue;
2374 kernel_pixels+=image->columns+kernel->width;
2376 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
2377 if ((channel & RedChannel) != 0)
2378 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
2379 if ((channel & GreenChannel) != 0)
2380 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
2381 if ((channel & BlueChannel) != 0)
2382 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
2383 if ((channel & OpacityChannel) != 0)
2387 for (v=0; v < (long) kernel->width; v++)
2389 for (u=0; u < (long) kernel->height; u++)
2391 pixel.opacity+=(*k)*kernel_pixels[u].opacity;
2394 kernel_pixels+=image->columns+kernel->width;
2396 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
2398 if (((channel & IndexChannel) != 0) &&
2399 (image->colorspace == CMYKColorspace))
2401 register const IndexPacket
2402 *restrict kernel_indexes;
2406 kernel_indexes=indexes;
2407 for (v=0; v < (long) kernel->width; v++)
2409 for (u=0; u < (long) kernel->height; u++)
2411 alpha=(MagickRealType) (QuantumScale*(QuantumRange-
2412 kernel_pixels[u].opacity));
2413 pixel.index+=(*k)*alpha*kernel_indexes[u];
2416 kernel_pixels+=image->columns+kernel->width;
2417 kernel_indexes+=image->columns+kernel->width;
2419 filter_indexes[x]=ClampToQuantum(gamma*
2420 GetIndexPixelComponent(&pixel));
2426 sync=SyncCacheViewAuthenticPixels(filter_view,exception);
2427 if (sync == MagickFalse)
2429 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2434 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2435 #pragma omp critical (MagickCore_FilterImageChannel)
2437 proceed=SetImageProgress(image,FilterImageTag,progress++,image->rows);
2438 if (proceed == MagickFalse)
2442 filter_image->type=image->type;
2443 filter_view=DestroyCacheView(filter_view);
2444 image_view=DestroyCacheView(image_view);
2445 if (status == MagickFalse)
2446 filter_image=DestroyImage(filter_image);
2447 return(filter_image);
2451 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2455 % G a u s s i a n B l u r I m a g e %
2459 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2461 % GaussianBlurImage() blurs an image. We convolve the image with a
2462 % Gaussian operator of the given radius and standard deviation (sigma).
2463 % For reasonable results, the radius should be larger than sigma. Use a
2464 % radius of 0 and GaussianBlurImage() selects a suitable radius for you
2466 % The format of the GaussianBlurImage method is:
2468 % Image *GaussianBlurImage(const Image *image,onst double radius,
2469 % const double sigma,ExceptionInfo *exception)
2470 % Image *GaussianBlurImageChannel(const Image *image,
2471 % const ChannelType channel,const double radius,const double sigma,
2472 % ExceptionInfo *exception)
2474 % A description of each parameter follows:
2476 % o image: the image.
2478 % o channel: the channel type.
2480 % o radius: the radius of the Gaussian, in pixels, not counting the center
2483 % o sigma: the standard deviation of the Gaussian, in pixels.
2485 % o exception: return any errors or warnings in this structure.
2489 MagickExport Image *GaussianBlurImage(const Image *image,const double radius,
2490 const double sigma,ExceptionInfo *exception)
2495 blur_image=GaussianBlurImageChannel(image,DefaultChannels,radius,sigma,
2500 MagickExport Image *GaussianBlurImageChannel(const Image *image,
2501 const ChannelType channel,const double radius,const double sigma,
2502 ExceptionInfo *exception)
2521 assert(image != (const Image *) NULL);
2522 assert(image->signature == MagickSignature);
2523 if (image->debug != MagickFalse)
2524 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2525 assert(exception != (ExceptionInfo *) NULL);
2526 assert(exception->signature == MagickSignature);
2527 width=GetOptimalKernelWidth2D(radius,sigma);
2528 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
2529 if (kernel == (double *) NULL)
2530 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2533 for (v=(-j); v <= j; v++)
2535 for (u=(-j); u <= j; u++)
2536 kernel[i++]=exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
2537 (2.0*MagickPI*MagickSigma*MagickSigma);
2539 blur_image=ConvolveImageChannel(image,channel,width,kernel,exception);
2540 kernel=(double *) RelinquishMagickMemory(kernel);
2545 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2549 % M e d i a n F i l t e r I m a g e %
2553 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2555 % MedianFilterImage() applies a digital filter that improves the quality
2556 % of a noisy image. Each pixel is replaced by the median in a set of
2557 % neighboring pixels as defined by radius.
2559 % The algorithm was contributed by Mike Edmonds and implements an insertion
2560 % sort for selecting median color-channel values. For more on this algorithm
2561 % see "Skip Lists: A probabilistic Alternative to Balanced Trees" by William
2562 % Pugh in the June 1990 of Communications of the ACM.
2564 % The format of the MedianFilterImage method is:
2566 % Image *MedianFilterImage(const Image *image,const double radius,
2567 % ExceptionInfo *exception)
2569 % A description of each parameter follows:
2571 % o image: the image.
2573 % o radius: the radius of the pixel neighborhood.
2575 % o exception: return any errors or warnings in this structure.
2579 #define MedianListChannels 5
2581 typedef struct _MedianListNode
2589 typedef struct _MedianSkipList
2598 typedef struct _MedianPixelList
2606 lists[MedianListChannels];
2609 static MedianPixelList *DestroyMedianPixelList(MedianPixelList *pixel_list)
2614 if (pixel_list == (MedianPixelList *) NULL)
2615 return((MedianPixelList *) NULL);
2616 for (i=0; i < MedianListChannels; i++)
2617 if (pixel_list->lists[i].nodes != (MedianListNode *) NULL)
2618 pixel_list->lists[i].nodes=(MedianListNode *) RelinquishMagickMemory(
2619 pixel_list->lists[i].nodes);
2620 pixel_list=(MedianPixelList *) RelinquishAlignedMemory(pixel_list);
2624 static MedianPixelList **DestroyMedianPixelListThreadSet(
2625 MedianPixelList **pixel_list)
2630 assert(pixel_list != (MedianPixelList **) NULL);
2631 for (i=0; i < (long) GetOpenMPMaximumThreads(); i++)
2632 if (pixel_list[i] != (MedianPixelList *) NULL)
2633 pixel_list[i]=DestroyMedianPixelList(pixel_list[i]);
2634 pixel_list=(MedianPixelList **) RelinquishAlignedMemory(pixel_list);
2638 static MedianPixelList *AcquireMedianPixelList(const unsigned long width)
2646 pixel_list=(MedianPixelList *) AcquireAlignedMemory(1,sizeof(*pixel_list));
2647 if (pixel_list == (MedianPixelList *) NULL)
2649 (void) ResetMagickMemory((void *) pixel_list,0,sizeof(*pixel_list));
2650 pixel_list->center=width*width/2;
2651 for (i=0; i < MedianListChannels; i++)
2653 pixel_list->lists[i].nodes=(MedianListNode *) AcquireQuantumMemory(65537UL,
2654 sizeof(*pixel_list->lists[i].nodes));
2655 if (pixel_list->lists[i].nodes == (MedianListNode *) NULL)
2656 return(DestroyMedianPixelList(pixel_list));
2657 (void) ResetMagickMemory(pixel_list->lists[i].nodes,0,65537UL*
2658 sizeof(*pixel_list->lists[i].nodes));
2660 pixel_list->signature=MagickSignature;
2664 static MedianPixelList **AcquireMedianPixelListThreadSet(
2665 const unsigned long width)
2676 number_threads=GetOpenMPMaximumThreads();
2677 pixel_list=(MedianPixelList **) AcquireAlignedMemory(number_threads,
2678 sizeof(*pixel_list));
2679 if (pixel_list == (MedianPixelList **) NULL)
2680 return((MedianPixelList **) NULL);
2681 (void) ResetMagickMemory(pixel_list,0,number_threads*sizeof(*pixel_list));
2682 for (i=0; i < (long) number_threads; i++)
2684 pixel_list[i]=AcquireMedianPixelList(width);
2685 if (pixel_list[i] == (MedianPixelList *) NULL)
2686 return(DestroyMedianPixelListThreadSet(pixel_list));
2691 static void AddNodeMedianPixelList(MedianPixelList *pixel_list,
2692 const long channel,const unsigned long color)
2697 register MedianSkipList
2705 Initialize the node.
2707 list=pixel_list->lists+channel;
2708 list->nodes[color].signature=pixel_list->signature;
2709 list->nodes[color].count=1;
2711 Determine where it belongs in the list.
2714 for (level=list->level; level >= 0; level--)
2716 while (list->nodes[search].next[level] < color)
2717 search=list->nodes[search].next[level];
2718 update[level]=search;
2721 Generate a pseudo-random level for this node.
2723 for (level=0; ; level++)
2725 pixel_list->seed=(pixel_list->seed*42893621L)+1L;
2726 if ((pixel_list->seed & 0x300) != 0x300)
2731 if (level > (list->level+2))
2732 level=list->level+2;
2734 If we're raising the list's level, link back to the root node.
2736 while (level > list->level)
2739 update[list->level]=65536UL;
2742 Link the node into the skip-list.
2746 list->nodes[color].next[level]=list->nodes[update[level]].next[level];
2747 list->nodes[update[level]].next[level]=color;
2749 while (level-- > 0);
2752 static MagickPixelPacket GetMedianPixelList(MedianPixelList *pixel_list)
2760 register MedianSkipList
2769 channels[MedianListChannels];
2772 Find the median value for each of the color.
2774 center=pixel_list->center;
2775 for (channel=0; channel < 5; channel++)
2777 list=pixel_list->lists+channel;
2782 color=list->nodes[color].next[0];
2783 count+=list->nodes[color].count;
2785 while (count <= center);
2786 channels[channel]=(unsigned short) color;
2788 GetMagickPixelPacket((const Image *) NULL,&pixel);
2789 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
2790 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
2791 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
2792 pixel.opacity=(MagickRealType) ScaleShortToQuantum(channels[3]);
2793 pixel.index=(MagickRealType) ScaleShortToQuantum(channels[4]);
2797 static inline void InsertMedianPixelList(const Image *image,
2798 const PixelPacket *pixel,const IndexPacket *indexes,
2799 MedianPixelList *pixel_list)
2807 index=ScaleQuantumToShort(pixel->red);
2808 signature=pixel_list->lists[0].nodes[index].signature;
2809 if (signature == pixel_list->signature)
2810 pixel_list->lists[0].nodes[index].count++;
2812 AddNodeMedianPixelList(pixel_list,0,index);
2813 index=ScaleQuantumToShort(pixel->green);
2814 signature=pixel_list->lists[1].nodes[index].signature;
2815 if (signature == pixel_list->signature)
2816 pixel_list->lists[1].nodes[index].count++;
2818 AddNodeMedianPixelList(pixel_list,1,index);
2819 index=ScaleQuantumToShort(pixel->blue);
2820 signature=pixel_list->lists[2].nodes[index].signature;
2821 if (signature == pixel_list->signature)
2822 pixel_list->lists[2].nodes[index].count++;
2824 AddNodeMedianPixelList(pixel_list,2,index);
2825 index=ScaleQuantumToShort(pixel->opacity);
2826 signature=pixel_list->lists[3].nodes[index].signature;
2827 if (signature == pixel_list->signature)
2828 pixel_list->lists[3].nodes[index].count++;
2830 AddNodeMedianPixelList(pixel_list,3,index);
2831 if (image->colorspace == CMYKColorspace)
2832 index=ScaleQuantumToShort(*indexes);
2833 signature=pixel_list->lists[4].nodes[index].signature;
2834 if (signature == pixel_list->signature)
2835 pixel_list->lists[4].nodes[index].count++;
2837 AddNodeMedianPixelList(pixel_list,4,index);
2840 static void ResetMedianPixelList(MedianPixelList *pixel_list)
2848 register MedianListNode
2851 register MedianSkipList
2855 Reset the skip-list.
2857 for (channel=0; channel < 5; channel++)
2859 list=pixel_list->lists+channel;
2860 root=list->nodes+65536UL;
2862 for (level=0; level < 9; level++)
2863 root->next[level]=65536UL;
2865 pixel_list->seed=pixel_list->signature++;
2868 MagickExport Image *MedianFilterImage(const Image *image,const double radius,
2869 ExceptionInfo *exception)
2871 #define MedianFilterImageTag "MedianFilter/Image"
2888 **restrict pixel_list;
2894 Initialize median image attributes.
2896 assert(image != (Image *) NULL);
2897 assert(image->signature == MagickSignature);
2898 if (image->debug != MagickFalse)
2899 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2900 assert(exception != (ExceptionInfo *) NULL);
2901 assert(exception->signature == MagickSignature);
2902 width=GetOptimalKernelWidth2D(radius,0.5);
2903 if ((image->columns < width) || (image->rows < width))
2904 ThrowImageException(OptionError,"ImageSmallerThanKernelRadius");
2905 median_image=CloneImage(image,image->columns,image->rows,MagickTrue,
2907 if (median_image == (Image *) NULL)
2908 return((Image *) NULL);
2909 if (SetImageStorageClass(median_image,DirectClass) == MagickFalse)
2911 InheritException(exception,&median_image->exception);
2912 median_image=DestroyImage(median_image);
2913 return((Image *) NULL);
2915 pixel_list=AcquireMedianPixelListThreadSet(width);
2916 if (pixel_list == (MedianPixelList **) NULL)
2918 median_image=DestroyImage(median_image);
2919 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2922 Median filter each image row.
2926 image_view=AcquireCacheView(image);
2927 median_view=AcquireCacheView(median_image);
2928 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2929 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
2931 for (y=0; y < (long) median_image->rows; y++)
2933 register const IndexPacket
2936 register const PixelPacket
2939 register IndexPacket
2940 *restrict median_indexes;
2946 register PixelPacket
2949 if (status == MagickFalse)
2951 p=GetCacheViewVirtualPixels(image_view,-((long) width/2L),y-(long) (width/
2952 2L),image->columns+width,width,exception);
2953 q=QueueCacheViewAuthenticPixels(median_view,0,y,median_image->columns,1,
2955 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
2960 indexes=GetCacheViewVirtualIndexQueue(image_view);
2961 median_indexes=GetCacheViewAuthenticIndexQueue(median_view);
2962 id=GetOpenMPThreadId();
2963 for (x=0; x < (long) median_image->columns; x++)
2968 register const PixelPacket
2971 register const IndexPacket
2980 ResetMedianPixelList(pixel_list[id]);
2981 for (v=0; v < (long) width; v++)
2983 for (u=0; u < (long) width; u++)
2984 InsertMedianPixelList(image,r+u,s+u,pixel_list[id]);
2985 r+=image->columns+width;
2986 s+=image->columns+width;
2988 pixel=GetMedianPixelList(pixel_list[id]);
2989 SetPixelPacket(median_image,&pixel,q,median_indexes+x);
2993 if (SyncCacheViewAuthenticPixels(median_view,exception) == MagickFalse)
2995 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3000 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3001 #pragma omp critical (MagickCore_MedianFilterImage)
3003 proceed=SetImageProgress(image,MedianFilterImageTag,progress++,
3005 if (proceed == MagickFalse)
3009 median_view=DestroyCacheView(median_view);
3010 image_view=DestroyCacheView(image_view);
3011 pixel_list=DestroyMedianPixelListThreadSet(pixel_list);
3012 return(median_image);
3016 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3020 % M o t i o n B l u r I m a g e %
3024 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3026 % MotionBlurImage() simulates motion blur. We convolve the image with a
3027 % Gaussian operator of the given radius and standard deviation (sigma).
3028 % For reasonable results, radius should be larger than sigma. Use a
3029 % radius of 0 and MotionBlurImage() selects a suitable radius for you.
3030 % Angle gives the angle of the blurring motion.
3032 % Andrew Protano contributed this effect.
3034 % The format of the MotionBlurImage method is:
3036 % Image *MotionBlurImage(const Image *image,const double radius,
3037 % const double sigma,const double angle,ExceptionInfo *exception)
3038 % Image *MotionBlurImageChannel(const Image *image,const ChannelType channel,
3039 % const double radius,const double sigma,const double angle,
3040 % ExceptionInfo *exception)
3042 % A description of each parameter follows:
3044 % o image: the image.
3046 % o channel: the channel type.
3048 % o radius: the radius of the Gaussian, in pixels, not counting the center
3049 % o radius: the radius of the Gaussian, in pixels, not counting
3052 % o sigma: the standard deviation of the Gaussian, in pixels.
3054 % o angle: Apply the effect along this angle.
3056 % o exception: return any errors or warnings in this structure.
3060 static double *GetMotionBlurKernel(const unsigned long width,const double sigma)
3070 Generate a 1-D convolution kernel.
3072 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3073 kernel=(double *) AcquireQuantumMemory((size_t) width,sizeof(*kernel));
3074 if (kernel == (double *) NULL)
3077 for (i=0; i < (long) width; i++)
3079 kernel[i]=exp((-((double) i*i)/(double) (2.0*MagickSigma*MagickSigma)))/
3080 (MagickSQ2PI*MagickSigma);
3081 normalize+=kernel[i];
3083 for (i=0; i < (long) width; i++)
3084 kernel[i]/=normalize;
3088 MagickExport Image *MotionBlurImage(const Image *image,const double radius,
3089 const double sigma,const double angle,ExceptionInfo *exception)
3094 motion_blur=MotionBlurImageChannel(image,DefaultChannels,radius,sigma,angle,
3096 return(motion_blur);
3099 MagickExport Image *MotionBlurImageChannel(const Image *image,
3100 const ChannelType channel,const double radius,const double sigma,
3101 const double angle,ExceptionInfo *exception)
3103 typedef struct _OffsetInfo
3142 assert(image != (Image *) NULL);
3143 assert(image->signature == MagickSignature);
3144 if (image->debug != MagickFalse)
3145 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3146 assert(exception != (ExceptionInfo *) NULL);
3147 width=GetOptimalKernelWidth1D(radius,sigma);
3148 kernel=GetMotionBlurKernel(width,sigma);
3149 if (kernel == (double *) NULL)
3150 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3151 offset=(OffsetInfo *) AcquireQuantumMemory(width,sizeof(*offset));
3152 if (offset == (OffsetInfo *) NULL)
3154 kernel=(double *) RelinquishMagickMemory(kernel);
3155 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3157 blur_image=CloneImage(image,0,0,MagickTrue,exception);
3158 if (blur_image == (Image *) NULL)
3160 kernel=(double *) RelinquishMagickMemory(kernel);
3161 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
3162 return((Image *) NULL);
3164 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
3166 kernel=(double *) RelinquishMagickMemory(kernel);
3167 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
3168 InheritException(exception,&blur_image->exception);
3169 blur_image=DestroyImage(blur_image);
3170 return((Image *) NULL);
3172 point.x=(double) width*sin(DegreesToRadians(angle));
3173 point.y=(double) width*cos(DegreesToRadians(angle));
3174 for (i=0; i < (long) width; i++)
3176 offset[i].x=(long) ((i*point.y)/hypot(point.x,point.y)+0.5);
3177 offset[i].y=(long) ((i*point.x)/hypot(point.x,point.y)+0.5);
3184 GetMagickPixelPacket(image,&bias);
3185 image_view=AcquireCacheView(image);
3186 blur_view=AcquireCacheView(blur_image);
3187 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3188 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3190 for (y=0; y < (long) image->rows; y++)
3192 register IndexPacket
3193 *restrict blur_indexes;
3198 register PixelPacket
3201 if (status == MagickFalse)
3203 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3205 if (q == (PixelPacket *) NULL)
3210 blur_indexes=GetCacheViewAuthenticIndexQueue(blur_view);
3211 for (x=0; x < (long) image->columns; x++)
3225 register const IndexPacket
3230 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
3232 for (i=0; i < (long) width; i++)
3234 (void) GetOneCacheViewVirtualPixel(image_view,x+offset[i].x,y+
3235 offset[i].y,&pixel,exception);
3236 qixel.red+=(*k)*pixel.red;
3237 qixel.green+=(*k)*pixel.green;
3238 qixel.blue+=(*k)*pixel.blue;
3239 qixel.opacity+=(*k)*pixel.opacity;
3240 if (image->colorspace == CMYKColorspace)
3242 indexes=GetCacheViewVirtualIndexQueue(image_view);
3243 qixel.index+=(*k)*(*indexes);
3247 if ((channel & RedChannel) != 0)
3248 q->red=ClampToQuantum(qixel.red);
3249 if ((channel & GreenChannel) != 0)
3250 q->green=ClampToQuantum(qixel.green);
3251 if ((channel & BlueChannel) != 0)
3252 q->blue=ClampToQuantum(qixel.blue);
3253 if ((channel & OpacityChannel) != 0)
3254 q->opacity=ClampToQuantum(qixel.opacity);
3255 if (((channel & IndexChannel) != 0) &&
3256 (image->colorspace == CMYKColorspace))
3257 blur_indexes[x]=(IndexPacket) ClampToQuantum(qixel.index);
3267 for (i=0; i < (long) width; i++)
3269 (void) GetOneCacheViewVirtualPixel(image_view,x+offset[i].x,y+
3270 offset[i].y,&pixel,exception);
3271 alpha=(MagickRealType) (QuantumScale*GetAlphaPixelComponent(&pixel));
3272 qixel.red+=(*k)*alpha*pixel.red;
3273 qixel.green+=(*k)*alpha*pixel.green;
3274 qixel.blue+=(*k)*alpha*pixel.blue;
3275 qixel.opacity+=(*k)*pixel.opacity;
3276 if (image->colorspace == CMYKColorspace)
3278 indexes=GetCacheViewVirtualIndexQueue(image_view);
3279 qixel.index+=(*k)*alpha*(*indexes);
3284 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
3285 if ((channel & RedChannel) != 0)
3286 q->red=ClampToQuantum(gamma*qixel.red);
3287 if ((channel & GreenChannel) != 0)
3288 q->green=ClampToQuantum(gamma*qixel.green);
3289 if ((channel & BlueChannel) != 0)
3290 q->blue=ClampToQuantum(gamma*qixel.blue);
3291 if ((channel & OpacityChannel) != 0)
3292 q->opacity=ClampToQuantum(qixel.opacity);
3293 if (((channel & IndexChannel) != 0) &&
3294 (image->colorspace == CMYKColorspace))
3295 blur_indexes[x]=(IndexPacket) ClampToQuantum(gamma*qixel.index);
3299 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
3301 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3306 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3307 #pragma omp critical (MagickCore_MotionBlurImageChannel)
3309 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
3310 if (proceed == MagickFalse)
3314 blur_view=DestroyCacheView(blur_view);
3315 image_view=DestroyCacheView(image_view);
3316 kernel=(double *) RelinquishMagickMemory(kernel);
3317 offset=(OffsetInfo *) RelinquishMagickMemory(offset);
3318 if (status == MagickFalse)
3319 blur_image=DestroyImage(blur_image);
3324 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3328 % P r e v i e w I m a g e %
3332 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3334 % PreviewImage() tiles 9 thumbnails of the specified image with an image
3335 % processing operation applied with varying parameters. This may be helpful
3336 % pin-pointing an appropriate parameter for a particular image processing
3339 % The format of the PreviewImages method is:
3341 % Image *PreviewImages(const Image *image,const PreviewType preview,
3342 % ExceptionInfo *exception)
3344 % A description of each parameter follows:
3346 % o image: the image.
3348 % o preview: the image processing operation.
3350 % o exception: return any errors or warnings in this structure.
3353 MagickExport Image *PreviewImage(const Image *image,const PreviewType preview,
3354 ExceptionInfo *exception)
3356 #define NumberTiles 9
3357 #define PreviewImageTag "Preview/Image"
3358 #define DefaultPreviewGeometry "204x204+10+10"
3361 factor[MaxTextExtent],
3362 label[MaxTextExtent];
3404 Open output image file.
3406 assert(image != (Image *) NULL);
3407 assert(image->signature == MagickSignature);
3408 if (image->debug != MagickFalse)
3409 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3413 preview_info=AcquireImageInfo();
3414 SetGeometry(image,&geometry);
3415 (void) ParseMetaGeometry(DefaultPreviewGeometry,&geometry.x,&geometry.y,
3416 &geometry.width,&geometry.height);
3417 images=NewImageList();
3419 GetQuantizeInfo(&quantize_info);
3425 for (i=0; i < NumberTiles; i++)
3427 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
3428 if (thumbnail == (Image *) NULL)
3430 (void) SetImageProgressMonitor(thumbnail,(MagickProgressMonitor) NULL,
3432 (void) SetImageProperty(thumbnail,"label",DefaultTileLabel);
3433 if (i == (NumberTiles/2))
3435 (void) QueryColorDatabase("#dfdfdf",&thumbnail->matte_color,exception);
3436 AppendImageToList(&images,thumbnail);
3444 preview_image=RotateImage(thumbnail,degrees,exception);
3445 (void) FormatMagickString(label,MaxTextExtent,"rotate %g",degrees);
3451 preview_image=ShearImage(thumbnail,degrees,degrees,exception);
3452 (void) FormatMagickString(label,MaxTextExtent,"shear %gx%g",
3453 degrees,2.0*degrees);
3458 x=(long) ((i+1)*thumbnail->columns)/NumberTiles;
3459 y=(long) ((i+1)*thumbnail->rows)/NumberTiles;
3460 preview_image=RollImage(thumbnail,x,y,exception);
3461 (void) FormatMagickString(label,MaxTextExtent,"roll %ldx%ld",x,y);
3466 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3467 if (preview_image == (Image *) NULL)
3469 (void) FormatMagickString(factor,MaxTextExtent,"100,100,%g",
3471 (void) ModulateImage(preview_image,factor);
3472 (void) FormatMagickString(label,MaxTextExtent,"modulate %s",factor);
3475 case SaturationPreview:
3477 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3478 if (preview_image == (Image *) NULL)
3480 (void) FormatMagickString(factor,MaxTextExtent,"100,%g",
3482 (void) ModulateImage(preview_image,factor);
3483 (void) FormatMagickString(label,MaxTextExtent,"modulate %s",factor);
3486 case BrightnessPreview:
3488 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3489 if (preview_image == (Image *) NULL)
3491 (void) FormatMagickString(factor,MaxTextExtent,"%g",2.0*percentage);
3492 (void) ModulateImage(preview_image,factor);
3493 (void) FormatMagickString(label,MaxTextExtent,"modulate %s",factor);
3499 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3500 if (preview_image == (Image *) NULL)
3503 (void) GammaImageChannel(preview_image,DefaultChannels,gamma);
3504 (void) FormatMagickString(label,MaxTextExtent,"gamma %g",gamma);
3509 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3510 if (preview_image != (Image *) NULL)
3511 for (x=0; x < i; x++)
3512 (void) ContrastImage(preview_image,MagickTrue);
3513 (void) FormatMagickString(label,MaxTextExtent,"contrast (%ld)",i+1);
3518 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3519 if (preview_image == (Image *) NULL)
3521 for (x=0; x < i; x++)
3522 (void) ContrastImage(preview_image,MagickFalse);
3523 (void) FormatMagickString(label,MaxTextExtent,"+contrast (%ld)",i+1);
3526 case GrayscalePreview:
3528 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3529 if (preview_image == (Image *) NULL)
3532 quantize_info.number_colors=colors;
3533 quantize_info.colorspace=GRAYColorspace;
3534 (void) QuantizeImage(&quantize_info,preview_image);
3535 (void) FormatMagickString(label,MaxTextExtent,
3536 "-colorspace gray -colors %ld",colors);
3539 case QuantizePreview:
3541 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3542 if (preview_image == (Image *) NULL)
3545 quantize_info.number_colors=colors;
3546 (void) QuantizeImage(&quantize_info,preview_image);
3547 (void) FormatMagickString(label,MaxTextExtent,"colors %ld",colors);
3550 case DespecklePreview:
3552 for (x=0; x < (i-1); x++)
3554 preview_image=DespeckleImage(thumbnail,exception);
3555 if (preview_image == (Image *) NULL)
3557 thumbnail=DestroyImage(thumbnail);
3558 thumbnail=preview_image;
3560 preview_image=DespeckleImage(thumbnail,exception);
3561 if (preview_image == (Image *) NULL)
3563 (void) FormatMagickString(label,MaxTextExtent,"despeckle (%ld)",i+1);
3566 case ReduceNoisePreview:
3568 preview_image=ReduceNoiseImage(thumbnail,radius,exception);
3569 (void) FormatMagickString(label,MaxTextExtent,"noise %g",radius);
3572 case AddNoisePreview:
3578 (void) CopyMagickString(factor,"uniform",MaxTextExtent);
3583 (void) CopyMagickString(factor,"gaussian",MaxTextExtent);
3588 (void) CopyMagickString(factor,"multiplicative",MaxTextExtent);
3593 (void) CopyMagickString(factor,"impulse",MaxTextExtent);
3598 (void) CopyMagickString(factor,"laplacian",MaxTextExtent);
3603 (void) CopyMagickString(factor,"Poisson",MaxTextExtent);
3608 (void) CopyMagickString(thumbnail->magick,"NULL",MaxTextExtent);
3612 preview_image=ReduceNoiseImage(thumbnail,(double) i,exception);
3613 (void) FormatMagickString(label,MaxTextExtent,"+noise %s",factor);
3616 case SharpenPreview:
3618 preview_image=SharpenImage(thumbnail,radius,sigma,exception);
3619 (void) FormatMagickString(label,MaxTextExtent,"sharpen %gx%g",
3625 preview_image=BlurImage(thumbnail,radius,sigma,exception);
3626 (void) FormatMagickString(label,MaxTextExtent,"blur %gx%g",radius,
3630 case ThresholdPreview:
3632 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3633 if (preview_image == (Image *) NULL)
3635 (void) BilevelImage(thumbnail,
3636 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
3637 (void) FormatMagickString(label,MaxTextExtent,"threshold %g",
3638 (double) (percentage*((MagickRealType) QuantumRange+1.0))/100.0);
3641 case EdgeDetectPreview:
3643 preview_image=EdgeImage(thumbnail,radius,exception);
3644 (void) FormatMagickString(label,MaxTextExtent,"edge %g",radius);
3649 preview_image=SpreadImage(thumbnail,radius,exception);
3650 (void) FormatMagickString(label,MaxTextExtent,"spread %g",
3654 case SolarizePreview:
3656 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3657 if (preview_image == (Image *) NULL)
3659 (void) SolarizeImage(preview_image,(double) QuantumRange*
3661 (void) FormatMagickString(label,MaxTextExtent,"solarize %g",
3662 (QuantumRange*percentage)/100.0);
3668 preview_image=ShadeImage(thumbnail,MagickTrue,degrees,degrees,
3670 (void) FormatMagickString(label,MaxTextExtent,"shade %gx%g",
3676 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3677 if (preview_image == (Image *) NULL)
3679 geometry.width=(unsigned long) (2*i+2);
3680 geometry.height=(unsigned long) (2*i+2);
3683 (void) RaiseImage(preview_image,&geometry,MagickTrue);
3684 (void) FormatMagickString(label,MaxTextExtent,"raise %lux%lu%+ld%+ld",
3685 geometry.width,geometry.height,geometry.x,geometry.y);
3688 case SegmentPreview:
3690 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3691 if (preview_image == (Image *) NULL)
3694 (void) SegmentImage(preview_image,RGBColorspace,MagickFalse,threshold,
3696 (void) FormatMagickString(label,MaxTextExtent,"segment %gx%g",
3697 threshold,threshold);
3702 preview_image=SwirlImage(thumbnail,degrees,exception);
3703 (void) FormatMagickString(label,MaxTextExtent,"swirl %g",degrees);
3707 case ImplodePreview:
3710 preview_image=ImplodeImage(thumbnail,degrees,exception);
3711 (void) FormatMagickString(label,MaxTextExtent,"implode %g",degrees);
3717 preview_image=WaveImage(thumbnail,0.5*degrees,2.0*degrees,exception);
3718 (void) FormatMagickString(label,MaxTextExtent,"wave %gx%g",
3719 0.5*degrees,2.0*degrees);
3722 case OilPaintPreview:
3724 preview_image=OilPaintImage(thumbnail,(double) radius,exception);
3725 (void) FormatMagickString(label,MaxTextExtent,"paint %g",radius);
3728 case CharcoalDrawingPreview:
3730 preview_image=CharcoalImage(thumbnail,(double) radius,(double) sigma,
3732 (void) FormatMagickString(label,MaxTextExtent,"charcoal %gx%g",
3739 filename[MaxTextExtent];
3747 preview_image=CloneImage(thumbnail,0,0,MagickTrue,exception);
3748 if (preview_image == (Image *) NULL)
3750 preview_info->quality=(unsigned long) percentage;
3751 (void) FormatMagickString(factor,MaxTextExtent,"%lu",
3752 preview_info->quality);
3753 file=AcquireUniqueFileResource(filename);
3756 (void) FormatMagickString(preview_image->filename,MaxTextExtent,
3757 "jpeg:%s",filename);
3758 status=WriteImage(preview_info,preview_image);
3759 if (status != MagickFalse)
3764 (void) CopyMagickString(preview_info->filename,
3765 preview_image->filename,MaxTextExtent);
3766 quality_image=ReadImage(preview_info,exception);
3767 if (quality_image != (Image *) NULL)
3769 preview_image=DestroyImage(preview_image);
3770 preview_image=quality_image;
3773 (void) RelinquishUniqueFileResource(preview_image->filename);
3774 if ((GetBlobSize(preview_image)/1024) >= 1024)
3775 (void) FormatMagickString(label,MaxTextExtent,"quality %s\n%gmb ",
3776 factor,(double) ((MagickOffsetType) GetBlobSize(preview_image))/
3779 if (GetBlobSize(preview_image) >= 1024)
3780 (void) FormatMagickString(label,MaxTextExtent,
3781 "quality %s\n%gkb ",factor,(double) ((MagickOffsetType)
3782 GetBlobSize(preview_image))/1024.0);
3784 (void) FormatMagickString(label,MaxTextExtent,"quality %s\n%lub ",
3785 factor,(unsigned long) GetBlobSize(thumbnail));
3789 thumbnail=DestroyImage(thumbnail);
3793 if (preview_image == (Image *) NULL)
3795 (void) DeleteImageProperty(preview_image,"label");
3796 (void) SetImageProperty(preview_image,"label",label);
3797 AppendImageToList(&images,preview_image);
3798 proceed=SetImageProgress(image,PreviewImageTag,i,NumberTiles);
3799 if (proceed == MagickFalse)
3802 if (images == (Image *) NULL)
3804 preview_info=DestroyImageInfo(preview_info);
3805 return((Image *) NULL);
3810 montage_info=CloneMontageInfo(preview_info,(MontageInfo *) NULL);
3811 (void) CopyMagickString(montage_info->filename,image->filename,MaxTextExtent);
3812 montage_info->shadow=MagickTrue;
3813 (void) CloneString(&montage_info->tile,"3x3");
3814 (void) CloneString(&montage_info->geometry,DefaultPreviewGeometry);
3815 (void) CloneString(&montage_info->frame,DefaultTileFrame);
3816 montage_image=MontageImages(images,montage_info,exception);
3817 montage_info=DestroyMontageInfo(montage_info);
3818 images=DestroyImageList(images);
3819 if (montage_image == (Image *) NULL)
3820 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3821 if (montage_image->montage != (char *) NULL)
3824 Free image directory.
3826 montage_image->montage=(char *) RelinquishMagickMemory(
3827 montage_image->montage);
3828 if (image->directory != (char *) NULL)
3829 montage_image->directory=(char *) RelinquishMagickMemory(
3830 montage_image->directory);
3832 preview_info=DestroyImageInfo(preview_info);
3833 return(montage_image);
3837 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3841 % R a d i a l B l u r I m a g e %
3845 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3847 % RadialBlurImage() applies a radial blur to the image.
3849 % Andrew Protano contributed this effect.
3851 % The format of the RadialBlurImage method is:
3853 % Image *RadialBlurImage(const Image *image,const double angle,
3854 % ExceptionInfo *exception)
3855 % Image *RadialBlurImageChannel(const Image *image,const ChannelType channel,
3856 % const double angle,ExceptionInfo *exception)
3858 % A description of each parameter follows:
3860 % o image: the image.
3862 % o channel: the channel type.
3864 % o angle: the angle of the radial blur.
3866 % o exception: return any errors or warnings in this structure.
3870 MagickExport Image *RadialBlurImage(const Image *image,const double angle,
3871 ExceptionInfo *exception)
3876 blur_image=RadialBlurImageChannel(image,DefaultChannels,angle,exception);
3880 MagickExport Image *RadialBlurImageChannel(const Image *image,
3881 const ChannelType channel,const double angle,ExceptionInfo *exception)
3917 Allocate blur image.
3919 assert(image != (Image *) NULL);
3920 assert(image->signature == MagickSignature);
3921 if (image->debug != MagickFalse)
3922 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3923 assert(exception != (ExceptionInfo *) NULL);
3924 assert(exception->signature == MagickSignature);
3925 blur_image=CloneImage(image,0,0,MagickTrue,exception);
3926 if (blur_image == (Image *) NULL)
3927 return((Image *) NULL);
3928 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
3930 InheritException(exception,&blur_image->exception);
3931 blur_image=DestroyImage(blur_image);
3932 return((Image *) NULL);
3934 blur_center.x=(double) image->columns/2.0;
3935 blur_center.y=(double) image->rows/2.0;
3936 blur_radius=hypot(blur_center.x,blur_center.y);
3937 n=(unsigned long) fabs(4.0*DegreesToRadians(angle)*sqrt((double) blur_radius)+
3939 theta=DegreesToRadians(angle)/(MagickRealType) (n-1);
3940 cos_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
3941 sizeof(*cos_theta));
3942 sin_theta=(MagickRealType *) AcquireQuantumMemory((size_t) n,
3943 sizeof(*sin_theta));
3944 if ((cos_theta == (MagickRealType *) NULL) ||
3945 (sin_theta == (MagickRealType *) NULL))
3947 blur_image=DestroyImage(blur_image);
3948 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3950 offset=theta*(MagickRealType) (n-1)/2.0;
3951 for (i=0; i < (long) n; i++)
3953 cos_theta[i]=cos((double) (theta*i-offset));
3954 sin_theta[i]=sin((double) (theta*i-offset));
3961 GetMagickPixelPacket(image,&bias);
3962 image_view=AcquireCacheView(image);
3963 blur_view=AcquireCacheView(blur_image);
3964 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3965 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3967 for (y=0; y < (long) blur_image->rows; y++)
3969 register const IndexPacket
3972 register IndexPacket
3973 *restrict blur_indexes;
3978 register PixelPacket
3981 if (status == MagickFalse)
3983 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
3985 if (q == (PixelPacket *) NULL)
3990 blur_indexes=GetCacheViewAuthenticIndexQueue(blur_view);
3991 for (x=0; x < (long) blur_image->columns; x++)
4012 center.x=(double) x-blur_center.x;
4013 center.y=(double) y-blur_center.y;
4014 radius=hypot((double) center.x,center.y);
4019 step=(unsigned long) (blur_radius/radius);
4028 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
4030 for (i=0; i < (long) n; i+=step)
4032 (void) GetOneCacheViewVirtualPixel(image_view,(long) (blur_center.x+
4033 center.x*cos_theta[i]-center.y*sin_theta[i]+0.5),(long) (
4034 blur_center.y+center.x*sin_theta[i]+center.y*cos_theta[i]+0.5),
4036 qixel.red+=pixel.red;
4037 qixel.green+=pixel.green;
4038 qixel.blue+=pixel.blue;
4039 qixel.opacity+=pixel.opacity;
4040 if (image->colorspace == CMYKColorspace)
4042 indexes=GetCacheViewVirtualIndexQueue(image_view);
4043 qixel.index+=(*indexes);
4047 normalize=1.0/(fabs((double) normalize) <= MagickEpsilon ? 1.0 :
4049 if ((channel & RedChannel) != 0)
4050 q->red=ClampToQuantum(normalize*qixel.red);
4051 if ((channel & GreenChannel) != 0)
4052 q->green=ClampToQuantum(normalize*qixel.green);
4053 if ((channel & BlueChannel) != 0)
4054 q->blue=ClampToQuantum(normalize*qixel.blue);
4055 if ((channel & OpacityChannel) != 0)
4056 q->opacity=ClampToQuantum(normalize*qixel.opacity);
4057 if (((channel & IndexChannel) != 0) &&
4058 (image->colorspace == CMYKColorspace))
4059 blur_indexes[x]=(IndexPacket) ClampToQuantum(normalize*qixel.index);
4069 for (i=0; i < (long) n; i+=step)
4071 (void) GetOneCacheViewVirtualPixel(image_view,(long) (blur_center.x+
4072 center.x*cos_theta[i]-center.y*sin_theta[i]+0.5),(long) (
4073 blur_center.y+center.x*sin_theta[i]+center.y*cos_theta[i]+0.5),
4075 alpha=(MagickRealType) (QuantumScale*
4076 GetAlphaPixelComponent(&pixel));
4077 qixel.red+=alpha*pixel.red;
4078 qixel.green+=alpha*pixel.green;
4079 qixel.blue+=alpha*pixel.blue;
4080 qixel.opacity+=pixel.opacity;
4081 if (image->colorspace == CMYKColorspace)
4083 indexes=GetCacheViewVirtualIndexQueue(image_view);
4084 qixel.index+=alpha*(*indexes);
4089 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
4090 normalize=1.0/(fabs((double) normalize) <= MagickEpsilon ? 1.0 :
4092 if ((channel & RedChannel) != 0)
4093 q->red=ClampToQuantum(gamma*qixel.red);
4094 if ((channel & GreenChannel) != 0)
4095 q->green=ClampToQuantum(gamma*qixel.green);
4096 if ((channel & BlueChannel) != 0)
4097 q->blue=ClampToQuantum(gamma*qixel.blue);
4098 if ((channel & OpacityChannel) != 0)
4099 q->opacity=ClampToQuantum(normalize*qixel.opacity);
4100 if (((channel & IndexChannel) != 0) &&
4101 (image->colorspace == CMYKColorspace))
4102 blur_indexes[x]=(IndexPacket) ClampToQuantum(gamma*qixel.index);
4106 if (SyncCacheViewAuthenticPixels(blur_view,exception) == MagickFalse)
4108 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4113 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4114 #pragma omp critical (MagickCore_RadialBlurImageChannel)
4116 proceed=SetImageProgress(image,BlurImageTag,progress++,image->rows);
4117 if (proceed == MagickFalse)
4121 blur_view=DestroyCacheView(blur_view);
4122 image_view=DestroyCacheView(image_view);
4123 cos_theta=(MagickRealType *) RelinquishMagickMemory(cos_theta);
4124 sin_theta=(MagickRealType *) RelinquishMagickMemory(sin_theta);
4125 if (status == MagickFalse)
4126 blur_image=DestroyImage(blur_image);
4131 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4135 % R e d u c e N o i s e I m a g e %
4139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4141 % ReduceNoiseImage() smooths the contours of an image while still preserving
4142 % edge information. The algorithm works by replacing each pixel with its
4143 % neighbor closest in value. A neighbor is defined by radius. Use a radius
4144 % of 0 and ReduceNoise() selects a suitable radius for you.
4146 % The format of the ReduceNoiseImage method is:
4148 % Image *ReduceNoiseImage(const Image *image,const double radius,
4149 % ExceptionInfo *exception)
4151 % A description of each parameter follows:
4153 % o image: the image.
4155 % o radius: the radius of the pixel neighborhood.
4157 % o exception: return any errors or warnings in this structure.
4161 static MagickPixelPacket GetNonpeakMedianPixelList(MedianPixelList *pixel_list)
4169 register MedianSkipList
4183 Finds the median value for each of the color.
4185 center=pixel_list->center;
4186 for (channel=0; channel < 5; channel++)
4188 list=pixel_list->lists+channel;
4190 next=list->nodes[color].next[0];
4196 next=list->nodes[color].next[0];
4197 count+=list->nodes[color].count;
4199 while (count <= center);
4200 if ((previous == 65536UL) && (next != 65536UL))
4203 if ((previous != 65536UL) && (next == 65536UL))
4205 channels[channel]=(unsigned short) color;
4207 GetMagickPixelPacket((const Image *) NULL,&pixel);
4208 pixel.red=(MagickRealType) ScaleShortToQuantum(channels[0]);
4209 pixel.green=(MagickRealType) ScaleShortToQuantum(channels[1]);
4210 pixel.blue=(MagickRealType) ScaleShortToQuantum(channels[2]);
4211 pixel.opacity=(MagickRealType) ScaleShortToQuantum(channels[3]);
4212 pixel.index=(MagickRealType) ScaleShortToQuantum(channels[4]);
4216 MagickExport Image *ReduceNoiseImage(const Image *image,const double radius,
4217 ExceptionInfo *exception)
4219 #define ReduceNoiseImageTag "ReduceNoise/Image"
4236 **restrict pixel_list;
4242 Initialize noise image attributes.
4244 assert(image != (Image *) NULL);
4245 assert(image->signature == MagickSignature);
4246 if (image->debug != MagickFalse)
4247 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4248 assert(exception != (ExceptionInfo *) NULL);
4249 assert(exception->signature == MagickSignature);
4250 width=GetOptimalKernelWidth2D(radius,0.5);
4251 if ((image->columns < width) || (image->rows < width))
4252 ThrowImageException(OptionError,"ImageSmallerThanKernelRadius");
4253 noise_image=CloneImage(image,image->columns,image->rows,MagickTrue,
4255 if (noise_image == (Image *) NULL)
4256 return((Image *) NULL);
4257 if (SetImageStorageClass(noise_image,DirectClass) == MagickFalse)
4259 InheritException(exception,&noise_image->exception);
4260 noise_image=DestroyImage(noise_image);
4261 return((Image *) NULL);
4263 pixel_list=AcquireMedianPixelListThreadSet(width);
4264 if (pixel_list == (MedianPixelList **) NULL)
4266 noise_image=DestroyImage(noise_image);
4267 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
4274 image_view=AcquireCacheView(image);
4275 noise_view=AcquireCacheView(noise_image);
4276 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4277 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
4279 for (y=0; y < (long) noise_image->rows; y++)
4281 register const IndexPacket
4284 register const PixelPacket
4287 register IndexPacket
4288 *restrict noise_indexes;
4294 register PixelPacket
4297 if (status == MagickFalse)
4299 p=GetCacheViewVirtualPixels(image_view,-((long) width/2L),y-(long) (width/
4300 2L),image->columns+width,width,exception);
4301 q=QueueCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
4303 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
4308 indexes=GetCacheViewVirtualIndexQueue(image_view);
4309 noise_indexes=GetCacheViewAuthenticIndexQueue(noise_view);
4310 id=GetOpenMPThreadId();
4311 for (x=0; x < (long) noise_image->columns; x++)
4316 register const PixelPacket
4319 register const IndexPacket
4328 ResetMedianPixelList(pixel_list[id]);
4329 for (v=0; v < (long) width; v++)
4331 for (u=0; u < (long) width; u++)
4332 InsertMedianPixelList(image,r+u,s+u,pixel_list[id]);
4333 r+=image->columns+width;
4334 s+=image->columns+width;
4336 pixel=GetNonpeakMedianPixelList(pixel_list[id]);
4337 SetPixelPacket(noise_image,&pixel,q,noise_indexes+x);
4341 if (SyncCacheViewAuthenticPixels(noise_view,exception) == MagickFalse)
4343 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4348 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4349 #pragma omp critical (MagickCore_ReduceNoiseImage)
4351 proceed=SetImageProgress(image,ReduceNoiseImageTag,progress++,
4353 if (proceed == MagickFalse)
4357 noise_view=DestroyCacheView(noise_view);
4358 image_view=DestroyCacheView(image_view);
4359 pixel_list=DestroyMedianPixelListThreadSet(pixel_list);
4360 return(noise_image);
4364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4368 % S e l e c t i v e B l u r I m a g e %
4372 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4374 % SelectiveBlurImage() selectively blur pixels within a contrast threshold.
4375 % It is similar to the unsharpen mask that sharpens everything with contrast
4376 % above a certain threshold.
4378 % The format of the SelectiveBlurImage method is:
4380 % Image *SelectiveBlurImage(const Image *image,const double radius,
4381 % const double sigma,const double threshold,ExceptionInfo *exception)
4382 % Image *SelectiveBlurImageChannel(const Image *image,
4383 % const ChannelType channel,const double radius,const double sigma,
4384 % const double threshold,ExceptionInfo *exception)
4386 % A description of each parameter follows:
4388 % o image: the image.
4390 % o channel: the channel type.
4392 % o radius: the radius of the Gaussian, in pixels, not counting the center
4395 % o sigma: the standard deviation of the Gaussian, in pixels.
4397 % o threshold: only pixels within this contrast threshold are included
4398 % in the blur operation.
4400 % o exception: return any errors or warnings in this structure.
4404 static inline MagickBooleanType SelectiveContrast(const PixelPacket *p,
4405 const PixelPacket *q,const double threshold)
4407 if (fabs(PixelIntensity(p)-PixelIntensity(q)) < threshold)
4409 return(MagickFalse);
4412 MagickExport Image *SelectiveBlurImage(const Image *image,const double radius,
4413 const double sigma,const double threshold,ExceptionInfo *exception)
4418 blur_image=SelectiveBlurImageChannel(image,DefaultChannels,radius,sigma,
4419 threshold,exception);
4423 MagickExport Image *SelectiveBlurImageChannel(const Image *image,
4424 const ChannelType channel,const double radius,const double sigma,
4425 const double threshold,ExceptionInfo *exception)
4427 #define SelectiveBlurImageTag "SelectiveBlur/Image"
4459 Initialize blur image attributes.
4461 assert(image != (Image *) NULL);
4462 assert(image->signature == MagickSignature);
4463 if (image->debug != MagickFalse)
4464 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4465 assert(exception != (ExceptionInfo *) NULL);
4466 assert(exception->signature == MagickSignature);
4467 width=GetOptimalKernelWidth1D(radius,sigma);
4468 kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
4469 if (kernel == (double *) NULL)
4470 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
4473 for (v=(-j); v <= j; v++)
4475 for (u=(-j); u <= j; u++)
4476 kernel[i++]=exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
4477 (2.0*MagickPI*MagickSigma*MagickSigma);
4479 if (image->debug != MagickFalse)
4482 format[MaxTextExtent],
4489 register const double
4492 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
4493 " SelectiveBlurImage with %ldx%ld kernel:",width,width);
4494 message=AcquireString("");
4496 for (v=0; v < (long) width; v++)
4499 (void) FormatMagickString(format,MaxTextExtent,"%ld: ",v);
4500 (void) ConcatenateString(&message,format);
4501 for (u=0; u < (long) width; u++)
4503 (void) FormatMagickString(format,MaxTextExtent,"%+f ",*k++);
4504 (void) ConcatenateString(&message,format);
4506 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
4508 message=DestroyString(message);
4510 blur_image=CloneImage(image,0,0,MagickTrue,exception);
4511 if (blur_image == (Image *) NULL)
4512 return((Image *) NULL);
4513 if (SetImageStorageClass(blur_image,DirectClass) == MagickFalse)
4515 InheritException(exception,&blur_image->exception);
4516 blur_image=DestroyImage(blur_image);
4517 return((Image *) NULL);
4520 Threshold blur image.
4524 GetMagickPixelPacket(image,&bias);
4525 SetMagickPixelPacketBias(image,&bias);
4526 image_view=AcquireCacheView(image);
4527 blur_view=AcquireCacheView(blur_image);
4528 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4529 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
4531 for (y=0; y < (long) image->rows; y++)
4539 register const IndexPacket
4542 register const PixelPacket
4545 register IndexPacket
4546 *restrict blur_indexes;
4551 register PixelPacket
4554 if (status == MagickFalse)
4556 p=GetCacheViewVirtualPixels(image_view,-((long) width/2L),y-(long) (width/
4557 2L),image->columns+width,width,exception);
4558 q=GetCacheViewAuthenticPixels(blur_view,0,y,blur_image->columns,1,
4560 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
4565 indexes=GetCacheViewVirtualIndexQueue(image_view);
4566 blur_indexes=GetCacheViewAuthenticIndexQueue(blur_view);
4567 for (x=0; x < (long) image->columns; x++)
4576 register const double
4586 if (((channel & OpacityChannel) == 0) || (image->matte == MagickFalse))
4588 for (v=0; v < (long) width; v++)
4590 for (u=0; u < (long) width; u++)
4592 if (SelectiveContrast(p+u+j,q,threshold) != MagickFalse)
4594 pixel.red+=(*k)*(p+u+j)->red;
4595 pixel.green+=(*k)*(p+u+j)->green;
4596 pixel.blue+=(*k)*(p+u+j)->blue;
4601 j+=image->columns+width;
4605 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
4606 if ((channel & RedChannel) != 0)
4607 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
4608 if ((channel & GreenChannel) != 0)
4609 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
4610 if ((channel & BlueChannel) != 0)
4611 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
4613 if ((channel & OpacityChannel) != 0)
4617 for (v=0; v < (long) width; v++)
4619 for (u=0; u < (long) width; u++)
4621 if (SelectiveContrast(p+u+j,q,threshold) != MagickFalse)
4623 pixel.opacity+=(*k)*(p+u+j)->opacity;
4628 j+=image->columns+width;
4632 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
4634 SetOpacityPixelComponent(q,ClampToQuantum(gamma*
4635 GetOpacityPixelComponent(&pixel)));
4638 if (((channel & IndexChannel) != 0) &&
4639 (image->colorspace == CMYKColorspace))
4643 for (v=0; v < (long) width; v++)
4645 for (u=0; u < (long) width; u++)
4647 if (SelectiveContrast(p+u+j,q,threshold) != MagickFalse)
4649 pixel.index+=(*k)*indexes[x+u+j];
4654 j+=image->columns+width;
4658 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
4660 blur_indexes[x]=ClampToQuantum(gamma*
4661 GetIndexPixelComponent(&pixel));
4670 for (v=0; v < (long) width; v++)
4672 for (u=0; u < (long) width; u++)
4674 if (SelectiveContrast(p+u+j,q,threshold) != MagickFalse)
4676 alpha=(MagickRealType) (QuantumScale*
4677 GetAlphaPixelComponent(p+u+j));
4678 pixel.red+=(*k)*alpha*(p+u+j)->red;
4679 pixel.green+=(*k)*alpha*(p+u+j)->green;
4680 pixel.blue+=(*k)*alpha*(p+u+j)->blue;
4681 pixel.opacity+=(*k)*(p+u+j)->opacity;
4686 j+=image->columns+width;
4690 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
4691 if ((channel & RedChannel) != 0)
4692 q->red=ClampToQuantum(gamma*GetRedPixelComponent(&pixel));
4693 if ((channel & GreenChannel) != 0)
4694 q->green=ClampToQuantum(gamma*GetGreenPixelComponent(&pixel));
4695 if ((channel & BlueChannel) != 0)
4696 q->blue=ClampToQuantum(gamma*GetBluePixelComponent(&pixel));
4698 if ((channel & OpacityChannel) != 0)
4702 for (v=0; v < (long) width; v++)
4704 for (u=0; u < (long) width; u++)
4706 if (SelectiveContrast(p+u+j,q,threshold) != MagickFalse)
4708 pixel.opacity+=(*k)*(p+u+j)->opacity;
4713 j+=image->columns+width;
4717 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
4719 SetOpacityPixelComponent(q,
4720 ClampOpacityPixelComponent(&pixel));
4723 if (((channel & IndexChannel) != 0) &&
4724 (image->colorspace == CMYKColorspace))
4728 for (v=0; v < (long) width; v++)
4730 for (u=0; u < (long) width; u++)
4732 if (SelectiveContrast(p+u+j,q,threshold) != MagickFalse)
4734 alpha=(MagickRealType) (QuantumScale*
4735 GetAlphaPixelComponent(p+u+j));
4736 pixel.index+=(*k)*alpha*indexes[x+u+j];
4741 j+=image->columns+width;
4745 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 :
4747 blur_indexes[x]=ClampToQuantum(gamma*
4748 GetIndexPixelComponent(&pixel));
4755 sync=SyncCacheViewAuthenticPixels(blur_view,exception);
4756 if (sync == MagickFalse)
4758 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4763 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4764 #pragma omp critical (MagickCore_SelectiveBlurImageChannel)
4766 proceed=SetImageProgress(image,SelectiveBlurImageTag,progress++,
4768 if (proceed == MagickFalse)
4772 blur_image->type=image->type;
4773 blur_view=DestroyCacheView(blur_view);
4774 image_view=DestroyCacheView(image_view);
4775 kernel=(double *) RelinquishMagickMemory(kernel);
4776 if (status == MagickFalse)
4777 blur_image=DestroyImage(blur_image);
4782 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4786 % S h a d e I m a g e %
4790 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4792 % ShadeImage() shines a distant light on an image to create a
4793 % three-dimensional effect. You control the positioning of the light with
4794 % azimuth and elevation; azimuth is measured in degrees off the x axis
4795 % and elevation is measured in pixels above the Z axis.
4797 % The format of the ShadeImage method is:
4799 % Image *ShadeImage(const Image *image,const MagickBooleanType gray,
4800 % const double azimuth,const double elevation,ExceptionInfo *exception)
4802 % A description of each parameter follows:
4804 % o image: the image.
4806 % o gray: A value other than zero shades the intensity of each pixel.
4808 % o azimuth, elevation: Define the light source direction.
4810 % o exception: return any errors or warnings in this structure.
4813 MagickExport Image *ShadeImage(const Image *image,const MagickBooleanType gray,
4814 const double azimuth,const double elevation,ExceptionInfo *exception)
4816 #define ShadeImageTag "Shade/Image"
4836 Initialize shaded image attributes.
4838 assert(image != (const Image *) NULL);
4839 assert(image->signature == MagickSignature);
4840 if (image->debug != MagickFalse)
4841 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4842 assert(exception != (ExceptionInfo *) NULL);
4843 assert(exception->signature == MagickSignature);
4844 shade_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
4845 if (shade_image == (Image *) NULL)
4846 return((Image *) NULL);
4847 if (SetImageStorageClass(shade_image,DirectClass) == MagickFalse)
4849 InheritException(exception,&shade_image->exception);
4850 shade_image=DestroyImage(shade_image);
4851 return((Image *) NULL);
4854 Compute the light vector.
4856 light.x=(double) QuantumRange*cos(DegreesToRadians(azimuth))*
4857 cos(DegreesToRadians(elevation));
4858 light.y=(double) QuantumRange*sin(DegreesToRadians(azimuth))*
4859 cos(DegreesToRadians(elevation));
4860 light.z=(double) QuantumRange*sin(DegreesToRadians(elevation));
4866 image_view=AcquireCacheView(image);
4867 shade_view=AcquireCacheView(shade_image);
4868 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4869 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
4871 for (y=0; y < (long) image->rows; y++)
4881 register const PixelPacket
4890 register PixelPacket
4893 if (status == MagickFalse)
4895 p=GetCacheViewVirtualPixels(image_view,-1,y-1,image->columns+2,3,exception);
4896 q=QueueCacheViewAuthenticPixels(shade_view,0,y,shade_image->columns,1,
4898 if ((p == (PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
4904 Shade this row of pixels.
4906 normal.z=2.0*(double) QuantumRange; /* constant Z of surface normal */
4908 s1=s0+image->columns+2;
4909 s2=s1+image->columns+2;
4910 for (x=0; x < (long) image->columns; x++)
4913 Determine the surface normal and compute shading.
4915 normal.x=(double) (PixelIntensity(s0-1)+PixelIntensity(s1-1)+
4916 PixelIntensity(s2-1)-PixelIntensity(s0+1)-PixelIntensity(s1+1)-
4917 PixelIntensity(s2+1));
4918 normal.y=(double) (PixelIntensity(s2-1)+PixelIntensity(s2)+
4919 PixelIntensity(s2+1)-PixelIntensity(s0-1)-PixelIntensity(s0)-
4920 PixelIntensity(s0+1));
4921 if ((normal.x == 0.0) && (normal.y == 0.0))
4926 distance=normal.x*light.x+normal.y*light.y+normal.z*light.z;
4927 if (distance > MagickEpsilon)
4930 normal.x*normal.x+normal.y*normal.y+normal.z*normal.z;
4931 if (normal_distance > (MagickEpsilon*MagickEpsilon))
4932 shade=distance/sqrt((double) normal_distance);
4935 if (gray != MagickFalse)
4937 q->red=(Quantum) shade;
4938 q->green=(Quantum) shade;
4939 q->blue=(Quantum) shade;
4943 q->red=ClampToQuantum(QuantumScale*shade*s1->red);
4944 q->green=ClampToQuantum(QuantumScale*shade*s1->green);
4945 q->blue=ClampToQuantum(QuantumScale*shade*s1->blue);
4947 q->opacity=s1->opacity;
4953 if (SyncCacheViewAuthenticPixels(shade_view,exception) == MagickFalse)
4955 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4960 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4961 #pragma omp critical (MagickCore_ShadeImage)
4963 proceed=SetImageProgress(image,ShadeImageTag,progress++,image->rows);
4964 if (proceed == MagickFalse)
4968 shade_view=DestroyCacheView(shade_view);
4969 image_view=DestroyCacheView(image_view);
4970 if (status == MagickFalse)
4971 shade_image=DestroyImage(shade_image);
4972 return(shade_image);
4976 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4980 % S h a r p e n I m a g e %
4984 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4986 % SharpenImage() sharpens the image. We convolve the image with a Gaussian
4987 % operator of the given radius and standard deviation (sigma). For
4988 % reasonable results, radius should be larger than sigma. Use a radius of 0
4989 % and SharpenImage() selects a suitable radius for you.
4991 % Using a separable kernel would be faster, but the negative weights cancel
4992 % out on the corners of the kernel producing often undesirable ringing in the
4993 % filtered result; this can be avoided by using a 2D gaussian shaped image
4994 % sharpening kernel instead.
4996 % The format of the SharpenImage method is:
4998 % Image *SharpenImage(const Image *image,const double radius,
4999 % const double sigma,ExceptionInfo *exception)
5000 % Image *SharpenImageChannel(const Image *image,const ChannelType channel,
5001 % const double radius,const double sigma,ExceptionInfo *exception)
5003 % A description of each parameter follows:
5005 % o image: the image.
5007 % o channel: the channel type.
5009 % o radius: the radius of the Gaussian, in pixels, not counting the center
5012 % o sigma: the standard deviation of the Laplacian, in pixels.
5014 % o exception: return any errors or warnings in this structure.
5018 MagickExport Image *SharpenImage(const Image *image,const double radius,
5019 const double sigma,ExceptionInfo *exception)
5024 sharp_image=SharpenImageChannel(image,DefaultChannels,radius,sigma,exception);
5025 return(sharp_image);
5028 MagickExport Image *SharpenImageChannel(const Image *image,
5029 const ChannelType channel,const double radius,const double sigma,
5030 ExceptionInfo *exception)
5050 assert(image != (const Image *) NULL);
5051 assert(image->signature == MagickSignature);
5052 if (image->debug != MagickFalse)
5053 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5054 assert(exception != (ExceptionInfo *) NULL);
5055 assert(exception->signature == MagickSignature);
5056 width=GetOptimalKernelWidth2D(radius,sigma);
5057 kernel=(double *) AcquireQuantumMemory((size_t) width*width,sizeof(*kernel));
5058 if (kernel == (double *) NULL)
5059 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5063 for (v=(-j); v <= j; v++)
5065 for (u=(-j); u <= j; u++)
5067 kernel[i]=(-exp(-((double) u*u+v*v)/(2.0*MagickSigma*MagickSigma))/
5068 (2.0*MagickPI*MagickSigma*MagickSigma));
5069 normalize+=kernel[i];
5073 kernel[i/2]=(double) ((-2.0)*normalize);
5074 sharp_image=ConvolveImageChannel(image,channel,width,kernel,exception);
5075 kernel=(double *) RelinquishMagickMemory(kernel);
5076 return(sharp_image);
5080 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5084 % S p r e a d I m a g e %
5088 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5090 % SpreadImage() is a special effects method that randomly displaces each
5091 % pixel in a block defined by the radius parameter.
5093 % The format of the SpreadImage method is:
5095 % Image *SpreadImage(const Image *image,const double radius,
5096 % ExceptionInfo *exception)
5098 % A description of each parameter follows:
5100 % o image: the image.
5102 % o radius: Choose a random pixel in a neighborhood of this extent.
5104 % o exception: return any errors or warnings in this structure.
5107 MagickExport Image *SpreadImage(const Image *image,const double radius,
5108 ExceptionInfo *exception)
5110 #define SpreadImageTag "Spread/Image"
5129 **restrict random_info;
5132 **restrict resample_filter;
5138 Initialize spread image attributes.
5140 assert(image != (Image *) NULL);
5141 assert(image->signature == MagickSignature);
5142 if (image->debug != MagickFalse)
5143 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5144 assert(exception != (ExceptionInfo *) NULL);
5145 assert(exception->signature == MagickSignature);
5146 spread_image=CloneImage(image,image->columns,image->rows,MagickTrue,
5148 if (spread_image == (Image *) NULL)
5149 return((Image *) NULL);
5150 if (SetImageStorageClass(spread_image,DirectClass) == MagickFalse)
5152 InheritException(exception,&spread_image->exception);
5153 spread_image=DestroyImage(spread_image);
5154 return((Image *) NULL);
5161 GetMagickPixelPacket(spread_image,&bias);
5162 width=GetOptimalKernelWidth1D(radius,0.5);
5163 resample_filter=AcquireResampleFilterThreadSet(image,MagickTrue,exception);
5164 random_info=AcquireRandomInfoThreadSet();
5165 image_view=AcquireCacheView(spread_image);
5166 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5167 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
5169 for (y=0; y < (long) spread_image->rows; y++)
5174 register IndexPacket
5181 register PixelPacket
5184 if (status == MagickFalse)
5186 q=QueueCacheViewAuthenticPixels(image_view,0,y,spread_image->columns,1,
5188 if (q == (PixelPacket *) NULL)
5193 indexes=GetCacheViewAuthenticIndexQueue(image_view);
5195 id=GetOpenMPThreadId();
5196 for (x=0; x < (long) spread_image->columns; x++)
5198 (void) ResamplePixelColor(resample_filter[id],(double) x+width*
5199 (GetPseudoRandomValue(random_info[id])-0.5),(double) y+width*
5200 (GetPseudoRandomValue(random_info[id])-0.5),&pixel);
5201 SetPixelPacket(spread_image,&pixel,q,indexes+x);
5204 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
5206 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5211 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5212 #pragma omp critical (MagickCore_SpreadImage)
5214 proceed=SetImageProgress(image,SpreadImageTag,progress++,image->rows);
5215 if (proceed == MagickFalse)
5219 image_view=DestroyCacheView(image_view);
5220 random_info=DestroyRandomInfoThreadSet(random_info);
5221 resample_filter=DestroyResampleFilterThreadSet(resample_filter);
5222 return(spread_image);
5226 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5230 % U n s h a r p M a s k I m a g e %
5234 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5236 % UnsharpMaskImage() sharpens one or more image channels. We convolve the
5237 % image with a Gaussian operator of the given radius and standard deviation
5238 % (sigma). For reasonable results, radius should be larger than sigma. Use a
5239 % radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
5241 % The format of the UnsharpMaskImage method is:
5243 % Image *UnsharpMaskImage(const Image *image,const double radius,
5244 % const double sigma,const double amount,const double threshold,
5245 % ExceptionInfo *exception)
5246 % Image *UnsharpMaskImageChannel(const Image *image,
5247 % const ChannelType channel,const double radius,const double sigma,
5248 % const double amount,const double threshold,ExceptionInfo *exception)
5250 % A description of each parameter follows:
5252 % o image: the image.
5254 % o channel: the channel type.
5256 % o radius: the radius of the Gaussian, in pixels, not counting the center
5259 % o sigma: the standard deviation of the Gaussian, in pixels.
5261 % o amount: the percentage of the difference between the original and the
5262 % blur image that is added back into the original.
5264 % o threshold: the threshold in pixels needed to apply the diffence amount.
5266 % o exception: return any errors or warnings in this structure.
5270 MagickExport Image *UnsharpMaskImage(const Image *image,const double radius,
5271 const double sigma,const double amount,const double threshold,
5272 ExceptionInfo *exception)
5277 sharp_image=UnsharpMaskImageChannel(image,DefaultChannels,radius,sigma,amount,
5278 threshold,exception);
5279 return(sharp_image);
5282 MagickExport Image *UnsharpMaskImageChannel(const Image *image,
5283 const ChannelType channel,const double radius,const double sigma,
5284 const double amount,const double threshold,ExceptionInfo *exception)
5286 #define SharpenImageTag "Sharpen/Image"
5308 assert(image != (const Image *) NULL);
5309 assert(image->signature == MagickSignature);
5310 if (image->debug != MagickFalse)
5311 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5312 assert(exception != (ExceptionInfo *) NULL);
5313 unsharp_image=BlurImageChannel(image,channel,radius,sigma,exception);
5314 if (unsharp_image == (Image *) NULL)
5315 return((Image *) NULL);
5316 quantum_threshold=(MagickRealType) QuantumRange*threshold;
5322 GetMagickPixelPacket(image,&bias);
5323 image_view=AcquireCacheView(image);
5324 unsharp_view=AcquireCacheView(unsharp_image);
5325 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5326 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
5328 for (y=0; y < (long) image->rows; y++)
5333 register const IndexPacket
5336 register const PixelPacket
5339 register IndexPacket
5340 *restrict unsharp_indexes;
5345 register PixelPacket
5348 if (status == MagickFalse)
5350 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5351 q=GetCacheViewAuthenticPixels(unsharp_view,0,y,unsharp_image->columns,1,
5353 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
5358 indexes=GetCacheViewVirtualIndexQueue(image_view);
5359 unsharp_indexes=GetCacheViewAuthenticIndexQueue(unsharp_view);
5361 for (x=0; x < (long) image->columns; x++)
5363 if ((channel & RedChannel) != 0)
5365 pixel.red=p->red-(MagickRealType) q->red;
5366 if (fabs(2.0*pixel.red) < quantum_threshold)
5367 pixel.red=(MagickRealType) GetRedPixelComponent(p);
5369 pixel.red=(MagickRealType) p->red+(pixel.red*amount);
5370 SetRedPixelComponent(q,ClampRedPixelComponent(&pixel));
5372 if ((channel & GreenChannel) != 0)
5374 pixel.green=p->green-(MagickRealType) q->green;
5375 if (fabs(2.0*pixel.green) < quantum_threshold)
5376 pixel.green=(MagickRealType) GetGreenPixelComponent(p);
5378 pixel.green=(MagickRealType) p->green+(pixel.green*amount);
5379 SetGreenPixelComponent(q,ClampGreenPixelComponent(&pixel));
5381 if ((channel & BlueChannel) != 0)
5383 pixel.blue=p->blue-(MagickRealType) q->blue;
5384 if (fabs(2.0*pixel.blue) < quantum_threshold)
5385 pixel.blue=(MagickRealType) GetBluePixelComponent(p);
5387 pixel.blue=(MagickRealType) p->blue+(pixel.blue*amount);
5388 SetBluePixelComponent(q,ClampBluePixelComponent(&pixel));
5390 if ((channel & OpacityChannel) != 0)
5392 pixel.opacity=p->opacity-(MagickRealType) q->opacity;
5393 if (fabs(2.0*pixel.opacity) < quantum_threshold)
5394 pixel.opacity=(MagickRealType) GetOpacityPixelComponent(p);
5396 pixel.opacity=p->opacity+(pixel.opacity*amount);
5397 SetOpacityPixelComponent(q,ClampOpacityPixelComponent(&pixel));
5399 if (((channel & IndexChannel) != 0) &&
5400 (image->colorspace == CMYKColorspace))
5402 pixel.index=unsharp_indexes[x]-(MagickRealType) indexes[x];
5403 if (fabs(2.0*pixel.index) < quantum_threshold)
5404 pixel.index=(MagickRealType) unsharp_indexes[x];
5406 pixel.index=(MagickRealType) unsharp_indexes[x]+(pixel.index*
5408 unsharp_indexes[x]=ClampToQuantum(pixel.index);
5413 if (SyncCacheViewAuthenticPixels(unsharp_view,exception) == MagickFalse)
5415 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5420 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5421 #pragma omp critical (MagickCore_UnsharpMaskImageChannel)
5423 proceed=SetImageProgress(image,SharpenImageTag,progress++,image->rows);
5424 if (proceed == MagickFalse)
5428 unsharp_image->type=image->type;
5429 unsharp_view=DestroyCacheView(unsharp_view);
5430 image_view=DestroyCacheView(image_view);
5431 if (status == MagickFalse)
5432 unsharp_image=DestroyImage(unsharp_image);
5433 return(unsharp_image);