2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % MagickCore Image Special Effects Methods %
20 % Copyright 1999-2017 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 % https://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/accelerate-private.h"
45 #include "MagickCore/annotate.h"
46 #include "MagickCore/artifact.h"
47 #include "MagickCore/attribute.h"
48 #include "MagickCore/cache.h"
49 #include "MagickCore/cache-view.h"
50 #include "MagickCore/channel.h"
51 #include "MagickCore/color.h"
52 #include "MagickCore/color-private.h"
53 #include "MagickCore/colorspace-private.h"
54 #include "MagickCore/composite.h"
55 #include "MagickCore/decorate.h"
56 #include "MagickCore/distort.h"
57 #include "MagickCore/draw.h"
58 #include "MagickCore/effect.h"
59 #include "MagickCore/enhance.h"
60 #include "MagickCore/exception.h"
61 #include "MagickCore/exception-private.h"
62 #include "MagickCore/fx.h"
63 #include "MagickCore/fx-private.h"
64 #include "MagickCore/gem.h"
65 #include "MagickCore/gem-private.h"
66 #include "MagickCore/geometry.h"
67 #include "MagickCore/layer.h"
68 #include "MagickCore/list.h"
69 #include "MagickCore/log.h"
70 #include "MagickCore/image.h"
71 #include "MagickCore/image-private.h"
72 #include "MagickCore/magick.h"
73 #include "MagickCore/memory_.h"
74 #include "MagickCore/monitor.h"
75 #include "MagickCore/monitor-private.h"
76 #include "MagickCore/option.h"
77 #include "MagickCore/pixel.h"
78 #include "MagickCore/pixel-accessor.h"
79 #include "MagickCore/property.h"
80 #include "MagickCore/quantum.h"
81 #include "MagickCore/quantum-private.h"
82 #include "MagickCore/random_.h"
83 #include "MagickCore/random-private.h"
84 #include "MagickCore/resample.h"
85 #include "MagickCore/resample-private.h"
86 #include "MagickCore/resize.h"
87 #include "MagickCore/resource_.h"
88 #include "MagickCore/splay-tree.h"
89 #include "MagickCore/statistic.h"
90 #include "MagickCore/string_.h"
91 #include "MagickCore/string-private.h"
92 #include "MagickCore/thread-private.h"
93 #include "MagickCore/transform.h"
94 #include "MagickCore/transform-private.h"
95 #include "MagickCore/utility.h"
100 #define LeftShiftOperator 0xf5U
101 #define RightShiftOperator 0xf6U
102 #define LessThanEqualOperator 0xf7U
103 #define GreaterThanEqualOperator 0xf8U
104 #define EqualOperator 0xf9U
105 #define NotEqualOperator 0xfaU
106 #define LogicalAndOperator 0xfbU
107 #define LogicalOrOperator 0xfcU
108 #define ExponentialNotation 0xfdU
136 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140 + A c q u i r e F x I n f o %
144 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
146 % AcquireFxInfo() allocates the FxInfo structure.
148 % The format of the AcquireFxInfo method is:
150 % FxInfo *AcquireFxInfo(Image *images,const char *expression,
151 % ExceptionInfo *exception)
153 % A description of each parameter follows:
155 % o images: the image sequence.
157 % o expression: the expression.
159 % o exception: return any errors or warnings in this structure.
162 MagickPrivate FxInfo *AcquireFxInfo(const Image *images,const char *expression,
163 ExceptionInfo *exception)
177 fx_info=(FxInfo *) AcquireMagickMemory(sizeof(*fx_info));
178 if (fx_info == (FxInfo *) NULL)
179 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
180 (void) ResetMagickMemory(fx_info,0,sizeof(*fx_info));
181 fx_info->exception=AcquireExceptionInfo();
182 fx_info->images=images;
183 fx_info->colors=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
184 RelinquishMagickMemory);
185 fx_info->symbols=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
186 RelinquishMagickMemory);
187 fx_info->view=(CacheView **) AcquireQuantumMemory(GetImageListLength(
188 fx_info->images),sizeof(*fx_info->view));
189 if (fx_info->view == (CacheView **) NULL)
190 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
192 next=GetFirstImageInList(fx_info->images);
193 for ( ; next != (Image *) NULL; next=next->next)
195 fx_info->view[i]=AcquireVirtualCacheView(next,exception);
198 fx_info->random_info=AcquireRandomInfo();
199 fx_info->expression=ConstantString(expression);
200 fx_info->file=stderr;
201 (void) SubstituteString(&fx_info->expression," ",""); /* compact string */
203 Force right-to-left associativity for unary negation.
205 (void) SubstituteString(&fx_info->expression,"-","-1.0*");
206 (void) SubstituteString(&fx_info->expression,"^-1.0*","^-");
207 (void) SubstituteString(&fx_info->expression,"E-1.0*","E-");
208 (void) SubstituteString(&fx_info->expression,"e-1.0*","e-");
210 Convert compound to simple operators.
213 *fx_op=(char) LeftShiftOperator;
214 (void) SubstituteString(&fx_info->expression,"<<",fx_op);
215 *fx_op=(char) RightShiftOperator;
216 (void) SubstituteString(&fx_info->expression,">>",fx_op);
217 *fx_op=(char) LessThanEqualOperator;
218 (void) SubstituteString(&fx_info->expression,"<=",fx_op);
219 *fx_op=(char) GreaterThanEqualOperator;
220 (void) SubstituteString(&fx_info->expression,">=",fx_op);
221 *fx_op=(char) EqualOperator;
222 (void) SubstituteString(&fx_info->expression,"==",fx_op);
223 *fx_op=(char) NotEqualOperator;
224 (void) SubstituteString(&fx_info->expression,"!=",fx_op);
225 *fx_op=(char) LogicalAndOperator;
226 (void) SubstituteString(&fx_info->expression,"&&",fx_op);
227 *fx_op=(char) LogicalOrOperator;
228 (void) SubstituteString(&fx_info->expression,"||",fx_op);
229 *fx_op=(char) ExponentialNotation;
230 (void) SubstituteString(&fx_info->expression,"**",fx_op);
235 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
239 % A d d N o i s e I m a g e %
243 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245 % AddNoiseImage() adds random noise to the image.
247 % The format of the AddNoiseImage method is:
249 % Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
250 % const double attenuate,ExceptionInfo *exception)
252 % A description of each parameter follows:
254 % o image: the image.
256 % o channel: the channel type.
258 % o noise_type: The type of noise: Uniform, Gaussian, Multiplicative,
259 % Impulse, Laplacian, or Poisson.
261 % o attenuate: attenuate the random distribution.
263 % o exception: return any errors or warnings in this structure.
266 MagickExport Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
267 const double attenuate,ExceptionInfo *exception)
269 #define AddNoiseImageTag "AddNoise/Image"
285 **magick_restrict random_info;
290 #if defined(MAGICKCORE_OPENMP_SUPPORT)
296 Initialize noise image attributes.
298 assert(image != (const Image *) NULL);
299 assert(image->signature == MagickCoreSignature);
300 if (image->debug != MagickFalse)
301 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
302 assert(exception != (ExceptionInfo *) NULL);
303 assert(exception->signature == MagickCoreSignature);
304 #if defined(MAGICKCORE_OPENCL_SUPPORT)
305 noise_image=AccelerateAddNoiseImage(image,noise_type,exception);
306 if (noise_image != (Image *) NULL)
309 noise_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
310 if (noise_image == (Image *) NULL)
311 return((Image *) NULL);
312 if (SetImageStorageClass(noise_image,DirectClass,exception) == MagickFalse)
314 noise_image=DestroyImage(noise_image);
315 return((Image *) NULL);
318 Add noise in each row.
322 random_info=AcquireRandomInfoThreadSet();
323 image_view=AcquireVirtualCacheView(image,exception);
324 noise_view=AcquireAuthenticCacheView(noise_image,exception);
325 #if defined(MAGICKCORE_OPENMP_SUPPORT)
326 key=GetRandomSecretKey(random_info[0]);
327 #pragma omp parallel for schedule(static,4) shared(progress,status) \
328 magick_threads(image,noise_image,image->rows,key == ~0UL)
330 for (y=0; y < (ssize_t) image->rows; y++)
333 id = GetOpenMPThreadId();
338 register const Quantum
347 if (status == MagickFalse)
349 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
350 q=QueueCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
352 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
357 for (x=0; x < (ssize_t) image->columns; x++)
362 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
364 PixelChannel channel=GetPixelChannelChannel(image,i);
365 PixelTrait traits=GetPixelChannelTraits(image,channel);
366 PixelTrait noise_traits=GetPixelChannelTraits(noise_image,channel);
367 if ((traits == UndefinedPixelTrait) ||
368 (noise_traits == UndefinedPixelTrait))
370 if (((noise_traits & CopyPixelTrait) != 0) ||
371 (GetPixelWriteMask(image,p) == 0))
373 SetPixelChannel(noise_image,channel,p[i],q);
376 SetPixelChannel(noise_image,channel,ClampToQuantum(
377 GenerateDifferentialNoise(random_info[id],p[i],noise_type,attenuate)),
380 p+=GetPixelChannels(image);
381 q+=GetPixelChannels(noise_image);
383 sync=SyncCacheViewAuthenticPixels(noise_view,exception);
384 if (sync == MagickFalse)
386 if (image->progress_monitor != (MagickProgressMonitor) NULL)
391 #if defined(MAGICKCORE_OPENMP_SUPPORT)
392 #pragma omp critical (MagickCore_AddNoiseImage)
394 proceed=SetImageProgress(image,AddNoiseImageTag,progress++,
396 if (proceed == MagickFalse)
400 noise_view=DestroyCacheView(noise_view);
401 image_view=DestroyCacheView(image_view);
402 random_info=DestroyRandomInfoThreadSet(random_info);
403 if (status == MagickFalse)
404 noise_image=DestroyImage(noise_image);
409 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
413 % B l u e S h i f t I m a g e %
417 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
419 % BlueShiftImage() mutes the colors of the image to simulate a scene at
420 % nighttime in the moonlight.
422 % The format of the BlueShiftImage method is:
424 % Image *BlueShiftImage(const Image *image,const double factor,
425 % ExceptionInfo *exception)
427 % A description of each parameter follows:
429 % o image: the image.
431 % o factor: the shift factor.
433 % o exception: return any errors or warnings in this structure.
436 MagickExport Image *BlueShiftImage(const Image *image,const double factor,
437 ExceptionInfo *exception)
439 #define BlueShiftImageTag "BlueShift/Image"
458 Allocate blue shift image.
460 assert(image != (const Image *) NULL);
461 assert(image->signature == MagickCoreSignature);
462 if (image->debug != MagickFalse)
463 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
464 assert(exception != (ExceptionInfo *) NULL);
465 assert(exception->signature == MagickCoreSignature);
466 shift_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
467 if (shift_image == (Image *) NULL)
468 return((Image *) NULL);
469 if (SetImageStorageClass(shift_image,DirectClass,exception) == MagickFalse)
471 shift_image=DestroyImage(shift_image);
472 return((Image *) NULL);
475 Blue-shift DirectClass image.
479 image_view=AcquireVirtualCacheView(image,exception);
480 shift_view=AcquireAuthenticCacheView(shift_image,exception);
481 #if defined(MAGICKCORE_OPENMP_SUPPORT)
482 #pragma omp parallel for schedule(static,4) shared(progress,status) \
483 magick_threads(image,shift_image,image->rows,1)
485 for (y=0; y < (ssize_t) image->rows; y++)
496 register const Quantum
505 if (status == MagickFalse)
507 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
508 q=QueueCacheViewAuthenticPixels(shift_view,0,y,shift_image->columns,1,
510 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
515 for (x=0; x < (ssize_t) image->columns; x++)
517 quantum=GetPixelRed(image,p);
518 if (GetPixelGreen(image,p) < quantum)
519 quantum=GetPixelGreen(image,p);
520 if (GetPixelBlue(image,p) < quantum)
521 quantum=GetPixelBlue(image,p);
522 pixel.red=0.5*(GetPixelRed(image,p)+factor*quantum);
523 pixel.green=0.5*(GetPixelGreen(image,p)+factor*quantum);
524 pixel.blue=0.5*(GetPixelBlue(image,p)+factor*quantum);
525 quantum=GetPixelRed(image,p);
526 if (GetPixelGreen(image,p) > quantum)
527 quantum=GetPixelGreen(image,p);
528 if (GetPixelBlue(image,p) > quantum)
529 quantum=GetPixelBlue(image,p);
530 pixel.red=0.5*(pixel.red+factor*quantum);
531 pixel.green=0.5*(pixel.green+factor*quantum);
532 pixel.blue=0.5*(pixel.blue+factor*quantum);
533 SetPixelRed(shift_image,ClampToQuantum(pixel.red),q);
534 SetPixelGreen(shift_image,ClampToQuantum(pixel.green),q);
535 SetPixelBlue(shift_image,ClampToQuantum(pixel.blue),q);
536 p+=GetPixelChannels(image);
537 q+=GetPixelChannels(shift_image);
539 sync=SyncCacheViewAuthenticPixels(shift_view,exception);
540 if (sync == MagickFalse)
542 if (image->progress_monitor != (MagickProgressMonitor) NULL)
547 #if defined(MAGICKCORE_OPENMP_SUPPORT)
548 #pragma omp critical (MagickCore_BlueShiftImage)
550 proceed=SetImageProgress(image,BlueShiftImageTag,progress++,
552 if (proceed == MagickFalse)
556 image_view=DestroyCacheView(image_view);
557 shift_view=DestroyCacheView(shift_view);
558 if (status == MagickFalse)
559 shift_image=DestroyImage(shift_image);
564 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
568 % C h a r c o a l I m a g e %
572 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
574 % CharcoalImage() creates a new image that is a copy of an existing one with
575 % the edge highlighted. It allocates the memory necessary for the new Image
576 % structure and returns a pointer to the new image.
578 % The format of the CharcoalImage method is:
580 % Image *CharcoalImage(const Image *image,const double radius,
581 % const double sigma,ExceptionInfo *exception)
583 % A description of each parameter follows:
585 % o image: the image.
587 % o radius: the radius of the pixel neighborhood.
589 % o sigma: the standard deviation of the Gaussian, in pixels.
591 % o exception: return any errors or warnings in this structure.
594 MagickExport Image *CharcoalImage(const Image *image,const double radius,
595 const double sigma,ExceptionInfo *exception)
602 assert(image != (Image *) NULL);
603 assert(image->signature == MagickCoreSignature);
604 if (image->debug != MagickFalse)
605 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
606 assert(exception != (ExceptionInfo *) NULL);
607 assert(exception->signature == MagickCoreSignature);
608 clone_image=CloneImage(image,0,0,MagickTrue,exception);
609 if (clone_image == (Image *) NULL)
610 return((Image *) NULL);
611 edge_image=EdgeImage(clone_image,radius,exception);
612 clone_image=DestroyImage(clone_image);
613 if (edge_image == (Image *) NULL)
614 return((Image *) NULL);
615 charcoal_image=BlurImage(edge_image,radius,sigma,exception);
616 edge_image=DestroyImage(edge_image);
617 if (charcoal_image == (Image *) NULL)
618 return((Image *) NULL);
619 (void) NormalizeImage(charcoal_image,exception);
620 (void) NegateImage(charcoal_image,MagickFalse,exception);
621 (void) GrayscaleImage(charcoal_image,image->intensity,exception);
622 return(charcoal_image);
626 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
630 % C o l o r i z e I m a g e %
634 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
636 % ColorizeImage() blends the fill color with each pixel in the image.
637 % A percentage blend is specified with opacity. Control the application
638 % of different color components by specifying a different percentage for
639 % each component (e.g. 90/100/10 is 90% red, 100% green, and 10% blue).
641 % The format of the ColorizeImage method is:
643 % Image *ColorizeImage(const Image *image,const char *blend,
644 % const PixelInfo *colorize,ExceptionInfo *exception)
646 % A description of each parameter follows:
648 % o image: the image.
650 % o blend: A character string indicating the level of blending as a
653 % o colorize: A color value.
655 % o exception: return any errors or warnings in this structure.
658 MagickExport Image *ColorizeImage(const Image *image,const char *blend,
659 const PixelInfo *colorize,ExceptionInfo *exception)
661 #define ColorizeImageTag "Colorize/Image"
662 #define Colorize(pixel,blend_percentage,colorize) \
663 (((pixel)*(100.0-(blend_percentage))+(colorize)*(blend_percentage))/100.0)
690 Allocate colorized image.
692 assert(image != (const Image *) NULL);
693 assert(image->signature == MagickCoreSignature);
694 if (image->debug != MagickFalse)
695 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
696 assert(exception != (ExceptionInfo *) NULL);
697 assert(exception->signature == MagickCoreSignature);
698 colorize_image=CloneImage(image,0,0,MagickTrue,exception);
699 if (colorize_image == (Image *) NULL)
700 return((Image *) NULL);
701 if (SetImageStorageClass(colorize_image,DirectClass,exception) == MagickFalse)
703 colorize_image=DestroyImage(colorize_image);
704 return((Image *) NULL);
706 if ((IsGrayColorspace(colorize_image->colorspace) != MagickFalse) ||
707 (IsPixelInfoGray(colorize) != MagickFalse))
708 (void) SetImageColorspace(colorize_image,sRGBColorspace,exception);
709 if ((colorize_image->alpha_trait == UndefinedPixelTrait) &&
710 (colorize->alpha_trait != UndefinedPixelTrait))
711 (void) SetImageAlpha(colorize_image,OpaqueAlpha,exception);
712 if (blend == (const char *) NULL)
713 return(colorize_image);
714 GetPixelInfo(colorize_image,&blend_percentage);
715 flags=ParseGeometry(blend,&geometry_info);
716 blend_percentage.red=geometry_info.rho;
717 blend_percentage.green=geometry_info.rho;
718 blend_percentage.blue=geometry_info.rho;
719 blend_percentage.black=geometry_info.rho;
720 blend_percentage.alpha=(MagickRealType) TransparentAlpha;
721 if ((flags & SigmaValue) != 0)
722 blend_percentage.green=geometry_info.sigma;
723 if ((flags & XiValue) != 0)
724 blend_percentage.blue=geometry_info.xi;
725 if ((flags & PsiValue) != 0)
726 blend_percentage.alpha=geometry_info.psi;
727 if (blend_percentage.colorspace == CMYKColorspace)
729 if ((flags & PsiValue) != 0)
730 blend_percentage.black=geometry_info.psi;
731 if ((flags & ChiValue) != 0)
732 blend_percentage.alpha=geometry_info.chi;
735 Colorize DirectClass image.
739 image_view=AcquireVirtualCacheView(colorize_image,exception);
740 #if defined(MAGICKCORE_OPENMP_SUPPORT)
741 #pragma omp parallel for schedule(static,4) shared(progress,status) \
742 magick_threads(colorize_image,colorize_image,colorize_image->rows,1)
744 for (y=0; y < (ssize_t) colorize_image->rows; y++)
755 if (status == MagickFalse)
757 q=GetCacheViewAuthenticPixels(image_view,0,y,colorize_image->columns,1,
759 if (q == (Quantum *) NULL)
764 for (x=0; x < (ssize_t) colorize_image->columns; x++)
769 for (i=0; i < (ssize_t) GetPixelChannels(colorize_image); i++)
771 PixelTrait traits=GetPixelChannelTraits(colorize_image,
773 if (traits == UndefinedPixelTrait)
775 if (((traits & CopyPixelTrait) != 0) ||
776 (GetPixelWriteMask(colorize_image,q) == 0))
778 SetPixelChannel(colorize_image,(PixelChannel) i,ClampToQuantum(
779 Colorize(q[i],GetPixelInfoChannel(&blend_percentage,(PixelChannel) i),
780 GetPixelInfoChannel(colorize,(PixelChannel) i))),q);
782 q+=GetPixelChannels(colorize_image);
784 sync=SyncCacheViewAuthenticPixels(image_view,exception);
785 if (sync == MagickFalse)
787 if (image->progress_monitor != (MagickProgressMonitor) NULL)
792 #if defined(MAGICKCORE_OPENMP_SUPPORT)
793 #pragma omp critical (MagickCore_ColorizeImage)
795 proceed=SetImageProgress(image,ColorizeImageTag,progress++,
796 colorize_image->rows);
797 if (proceed == MagickFalse)
801 image_view=DestroyCacheView(image_view);
802 if (status == MagickFalse)
803 colorize_image=DestroyImage(colorize_image);
804 return(colorize_image);
808 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
812 % C o l o r M a t r i x I m a g e %
816 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
818 % ColorMatrixImage() applies color transformation to an image. This method
819 % permits saturation changes, hue rotation, luminance to alpha, and various
820 % other effects. Although variable-sized transformation matrices can be used,
821 % typically one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA
822 % (or RGBA with offsets). The matrix is similar to those used by Adobe Flash
823 % except offsets are in column 6 rather than 5 (in support of CMYKA images)
824 % and offsets are normalized (divide Flash offset by 255).
826 % The format of the ColorMatrixImage method is:
828 % Image *ColorMatrixImage(const Image *image,
829 % const KernelInfo *color_matrix,ExceptionInfo *exception)
831 % A description of each parameter follows:
833 % o image: the image.
835 % o color_matrix: the color matrix.
837 % o exception: return any errors or warnings in this structure.
840 /* FUTURE: modify to make use of a MagickMatrix Mutliply function
841 That should be provided in "matrix.c"
842 (ASIDE: actually distorts should do this too but currently doesn't)
845 MagickExport Image *ColorMatrixImage(const Image *image,
846 const KernelInfo *color_matrix,ExceptionInfo *exception)
848 #define ColorMatrixImageTag "ColorMatrix/Image"
857 { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
858 { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
859 { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
860 { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
861 { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
862 { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }
883 Map given color_matrix, into a 6x6 matrix RGBKA and a constant
885 assert(image != (Image *) NULL);
886 assert(image->signature == MagickCoreSignature);
887 if (image->debug != MagickFalse)
888 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
889 assert(exception != (ExceptionInfo *) NULL);
890 assert(exception->signature == MagickCoreSignature);
892 for (v=0; v < (ssize_t) color_matrix->height; v++)
893 for (u=0; u < (ssize_t) color_matrix->width; u++)
895 if ((v < 6) && (u < 6))
896 ColorMatrix[v][u]=color_matrix->values[i];
900 Initialize color image.
902 color_image=CloneImage(image,0,0,MagickTrue,exception);
903 if (color_image == (Image *) NULL)
904 return((Image *) NULL);
905 if (SetImageStorageClass(color_image,DirectClass,exception) == MagickFalse)
907 color_image=DestroyImage(color_image);
908 return((Image *) NULL);
910 if (image->debug != MagickFalse)
913 format[MagickPathExtent],
916 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
917 " ColorMatrix image with color matrix:");
918 message=AcquireString("");
919 for (v=0; v < 6; v++)
922 (void) FormatLocaleString(format,MagickPathExtent,"%.20g: ",(double) v);
923 (void) ConcatenateString(&message,format);
924 for (u=0; u < 6; u++)
926 (void) FormatLocaleString(format,MagickPathExtent,"%+f ",
928 (void) ConcatenateString(&message,format);
930 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
932 message=DestroyString(message);
935 Apply the ColorMatrix to image.
939 image_view=AcquireVirtualCacheView(image,exception);
940 color_view=AcquireAuthenticCacheView(color_image,exception);
941 #if defined(MAGICKCORE_OPENMP_SUPPORT)
942 #pragma omp parallel for schedule(static,4) shared(progress,status) \
943 magick_threads(image,color_image,image->rows,1)
945 for (y=0; y < (ssize_t) image->rows; y++)
950 register const Quantum
959 if (status == MagickFalse)
961 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
962 q=GetCacheViewAuthenticPixels(color_view,0,y,color_image->columns,1,
964 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
969 GetPixelInfo(image,&pixel);
970 for (x=0; x < (ssize_t) image->columns; x++)
978 GetPixelInfoPixel(image,p,&pixel);
979 height=color_matrix->height > 6 ? 6UL : color_matrix->height;
980 for (v=0; v < (ssize_t) height; v++)
985 sum=ColorMatrix[v][0]*GetPixelRed(image,p)+ColorMatrix[v][1]*
986 GetPixelGreen(image,p)+ColorMatrix[v][2]*GetPixelBlue(image,p);
987 if (image->colorspace == CMYKColorspace)
988 sum+=ColorMatrix[v][3]*GetPixelBlack(image,p);
989 if (image->alpha_trait != UndefinedPixelTrait)
990 sum+=ColorMatrix[v][4]*GetPixelAlpha(image,p);
991 sum+=QuantumRange*ColorMatrix[v][5];
994 case 0: pixel.red=sum; break;
995 case 1: pixel.green=sum; break;
996 case 2: pixel.blue=sum; break;
997 case 3: pixel.black=sum; break;
998 case 4: pixel.alpha=sum; break;
1002 SetPixelViaPixelInfo(color_image,&pixel,q);
1003 p+=GetPixelChannels(image);
1004 q+=GetPixelChannels(color_image);
1006 if (SyncCacheViewAuthenticPixels(color_view,exception) == MagickFalse)
1008 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1013 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1014 #pragma omp critical (MagickCore_ColorMatrixImage)
1016 proceed=SetImageProgress(image,ColorMatrixImageTag,progress++,
1018 if (proceed == MagickFalse)
1022 color_view=DestroyCacheView(color_view);
1023 image_view=DestroyCacheView(image_view);
1024 if (status == MagickFalse)
1025 color_image=DestroyImage(color_image);
1026 return(color_image);
1030 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1034 + D e s t r o y F x I n f o %
1038 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1040 % DestroyFxInfo() deallocates memory associated with an FxInfo structure.
1042 % The format of the DestroyFxInfo method is:
1044 % ImageInfo *DestroyFxInfo(ImageInfo *fx_info)
1046 % A description of each parameter follows:
1048 % o fx_info: the fx info.
1051 MagickPrivate FxInfo *DestroyFxInfo(FxInfo *fx_info)
1056 fx_info->exception=DestroyExceptionInfo(fx_info->exception);
1057 fx_info->expression=DestroyString(fx_info->expression);
1058 fx_info->symbols=DestroySplayTree(fx_info->symbols);
1059 fx_info->colors=DestroySplayTree(fx_info->colors);
1060 for (i=(ssize_t) GetImageListLength(fx_info->images)-1; i >= 0; i--)
1061 fx_info->view[i]=DestroyCacheView(fx_info->view[i]);
1062 fx_info->view=(CacheView **) RelinquishMagickMemory(fx_info->view);
1063 fx_info->random_info=DestroyRandomInfo(fx_info->random_info);
1064 fx_info=(FxInfo *) RelinquishMagickMemory(fx_info);
1069 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1073 + F x E v a l u a t e C h a n n e l E x p r e s s i o n %
1077 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1079 % FxEvaluateChannelExpression() evaluates an expression and returns the
1082 % The format of the FxEvaluateExpression method is:
1084 % double FxEvaluateChannelExpression(FxInfo *fx_info,
1085 % const PixelChannel channel,const ssize_t x,const ssize_t y,
1086 % double *alpha,Exceptioninfo *exception)
1087 % double FxEvaluateExpression(FxInfo *fx_info,
1088 % double *alpha,Exceptioninfo *exception)
1090 % A description of each parameter follows:
1092 % o fx_info: the fx info.
1094 % o channel: the channel.
1096 % o x,y: the pixel position.
1098 % o alpha: the result.
1100 % o exception: return any errors or warnings in this structure.
1104 static double FxChannelStatistics(FxInfo *fx_info,Image *image,
1105 PixelChannel channel,const char *symbol,ExceptionInfo *exception)
1111 key[MagickPathExtent],
1112 statistic[MagickPathExtent];
1120 channel_mask=UndefinedChannel;
1121 for (p=symbol; (*p != '.') && (*p != '\0'); p++) ;
1127 option=ParseCommandOption(MagickPixelChannelOptions,MagickTrue,p+1);
1130 channel=(PixelChannel) option;
1131 channel_mask=SetPixelChannelMask(image,(ChannelType) (1 << channel));
1134 (void) FormatLocaleString(key,MagickPathExtent,"%p.%.20g.%s",(void *) image,
1135 (double) channel,symbol);
1136 value=(const char *) GetValueFromSplayTree(fx_info->symbols,key);
1137 if (value != (const char *) NULL)
1139 if (channel_mask != UndefinedChannel)
1140 (void) SetPixelChannelMask(image,channel_mask);
1141 return(QuantumScale*StringToDouble(value,(char **) NULL));
1143 (void) DeleteNodeFromSplayTree(fx_info->symbols,key);
1144 if (LocaleNCompare(symbol,"depth",5) == 0)
1149 depth=GetImageDepth(image,exception);
1150 (void) FormatLocaleString(statistic,MagickPathExtent,"%.20g",(double)
1153 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1159 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
1160 (void) FormatLocaleString(statistic,MagickPathExtent,"%g",kurtosis);
1162 if (LocaleNCompare(symbol,"maxima",6) == 0)
1168 (void) GetImageRange(image,&minima,&maxima,exception);
1169 (void) FormatLocaleString(statistic,MagickPathExtent,"%g",maxima);
1171 if (LocaleNCompare(symbol,"mean",4) == 0)
1177 (void) GetImageMean(image,&mean,&standard_deviation,exception);
1178 (void) FormatLocaleString(statistic,MagickPathExtent,"%g",mean);
1180 if (LocaleNCompare(symbol,"minima",6) == 0)
1186 (void) GetImageRange(image,&minima,&maxima,exception);
1187 (void) FormatLocaleString(statistic,MagickPathExtent,"%g",minima);
1189 if (LocaleNCompare(symbol,"skewness",8) == 0)
1195 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
1196 (void) FormatLocaleString(statistic,MagickPathExtent,"%g",skewness);
1198 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1204 (void) GetImageMean(image,&mean,&standard_deviation,exception);
1205 (void) FormatLocaleString(statistic,MagickPathExtent,"%g",
1206 standard_deviation);
1208 if (channel_mask != UndefinedChannel)
1209 (void) SetPixelChannelMask(image,channel_mask);
1210 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(key),
1211 ConstantString(statistic));
1212 return(QuantumScale*StringToDouble(statistic,(char **) NULL));
1216 FxEvaluateSubexpression(FxInfo *,const PixelChannel,const ssize_t,
1217 const ssize_t,const char *,size_t *,double *,ExceptionInfo *);
1219 static MagickOffsetType FxGCD(MagickOffsetType alpha,MagickOffsetType beta)
1222 return(FxGCD(beta,alpha % beta));
1226 static inline const char *FxSubexpression(const char *expression,
1227 ExceptionInfo *exception)
1236 subexpression=expression;
1237 while ((*subexpression != '\0') &&
1238 ((level != 1) || (strchr(")",(int) *subexpression) == (char *) NULL)))
1240 if (strchr("(",(int) *subexpression) != (char *) NULL)
1243 if (strchr(")",(int) *subexpression) != (char *) NULL)
1247 if (*subexpression == '\0')
1248 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1249 "UnbalancedParenthesis","`%s'",expression);
1250 return(subexpression);
1253 static double FxGetSymbol(FxInfo *fx_info,const PixelChannel channel,
1254 const ssize_t x,const ssize_t y,const char *expression,
1255 ExceptionInfo *exception)
1259 subexpression[MagickPathExtent],
1260 symbol[MagickPathExtent];
1288 i=GetImageIndexInList(fx_info->images);
1293 if (isalpha((int) ((unsigned char) *(p+1))) == 0)
1295 if (strchr("suv",(int) *p) != (char *) NULL)
1302 i=GetImageIndexInList(fx_info->images);
1305 case 'u': i=0; break;
1306 case 'v': i=1; break;
1313 for (p++; *p != '\0'; )
1327 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1328 &depth,&beta,exception);
1335 if ((*p == 'p') && (isalpha((int) ((unsigned char) *(p+1))) == 0))
1342 for (p++; *p != '\0'; )
1356 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1357 &depth,&beta,exception);
1367 for (p++; *p != '\0'; )
1381 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1382 &depth,&beta,exception);
1391 length=GetImageListLength(fx_info->images);
1393 i+=(ssize_t) length;
1396 image=GetImageFromList(fx_info->images,i);
1397 if (image == (Image *) NULL)
1399 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1400 "NoSuchImage","`%s'",expression);
1403 GetPixelInfo(image,&pixel);
1404 (void) InterpolatePixelInfo(image,fx_info->view[i],image->interpolate,
1405 point.x,point.y,&pixel,exception);
1406 if ((strlen(p) > 2) && (LocaleCompare(p,"intensity") != 0) &&
1407 (LocaleCompare(p,"luma") != 0) && (LocaleCompare(p,"luminance") != 0) &&
1408 (LocaleCompare(p,"hue") != 0) && (LocaleCompare(p,"saturation") != 0) &&
1409 (LocaleCompare(p,"lightness") != 0))
1412 name[MagickPathExtent];
1414 (void) CopyMagickString(name,p,MagickPathExtent);
1415 for (q=name+(strlen(name)-1); q > name; q--)
1425 if ((strlen(name) > 2) &&
1426 (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
1431 color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
1432 if (color != (PixelInfo *) NULL)
1442 status=QueryColorCompliance(name,AllCompliance,&pixel,
1443 fx_info->exception);
1444 if (status != MagickFalse)
1446 (void) AddValueToSplayTree(fx_info->colors,ConstantString(
1447 name),ClonePixelInfo(&pixel));
1453 (void) CopyMagickString(symbol,p,MagickPathExtent);
1454 StripString(symbol);
1455 if (*symbol == '\0')
1459 case RedPixelChannel: return(QuantumScale*pixel.red);
1460 case GreenPixelChannel: return(QuantumScale*pixel.green);
1461 case BluePixelChannel: return(QuantumScale*pixel.blue);
1462 case BlackPixelChannel:
1464 if (image->colorspace != CMYKColorspace)
1466 (void) ThrowMagickException(exception,GetMagickModule(),
1467 ImageError,"ColorSeparatedImageRequired","`%s'",
1471 return(QuantumScale*pixel.black);
1473 case AlphaPixelChannel:
1475 if (pixel.alpha_trait == UndefinedPixelTrait)
1477 alpha=(double) (QuantumScale*pixel.alpha);
1480 case IndexPixelChannel:
1482 case IntensityPixelChannel:
1485 quantum_pixel[MaxPixelChannels];
1487 SetPixelViaPixelInfo(image,&pixel,quantum_pixel);
1488 return(QuantumScale*GetPixelIntensity(image,quantum_pixel));
1493 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1494 "UnableToParseExpression","`%s'",p);
1502 if (LocaleCompare(symbol,"a") == 0)
1503 return((QuantumScale*pixel.alpha));
1509 if (LocaleCompare(symbol,"b") == 0)
1510 return(QuantumScale*pixel.blue);
1516 if (LocaleNCompare(symbol,"channel",7) == 0)
1524 flags=ParseGeometry(symbol+7,&channel_info);
1525 if (image->colorspace == CMYKColorspace)
1528 case CyanPixelChannel:
1530 if ((flags & RhoValue) == 0)
1532 return(channel_info.rho);
1534 case MagentaPixelChannel:
1536 if ((flags & SigmaValue) == 0)
1538 return(channel_info.sigma);
1540 case YellowPixelChannel:
1542 if ((flags & XiValue) == 0)
1544 return(channel_info.xi);
1546 case BlackPixelChannel:
1548 if ((flags & PsiValue) == 0)
1550 return(channel_info.psi);
1552 case AlphaPixelChannel:
1554 if ((flags & ChiValue) == 0)
1556 return(channel_info.chi);
1563 case RedPixelChannel:
1565 if ((flags & RhoValue) == 0)
1567 return(channel_info.rho);
1569 case GreenPixelChannel:
1571 if ((flags & SigmaValue) == 0)
1573 return(channel_info.sigma);
1575 case BluePixelChannel:
1577 if ((flags & XiValue) == 0)
1579 return(channel_info.xi);
1581 case BlackPixelChannel:
1583 if ((flags & ChiValue) == 0)
1585 return(channel_info.chi);
1587 case AlphaPixelChannel:
1589 if ((flags & PsiValue) == 0)
1591 return(channel_info.psi);
1597 if (LocaleCompare(symbol,"c") == 0)
1598 return(QuantumScale*pixel.red);
1604 if (LocaleNCompare(symbol,"depth",5) == 0)
1605 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1611 if (LocaleCompare(symbol,"g") == 0)
1612 return(QuantumScale*pixel.green);
1618 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1619 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1620 if (LocaleCompare(symbol,"k") == 0)
1622 if (image->colorspace != CMYKColorspace)
1624 (void) ThrowMagickException(exception,GetMagickModule(),
1625 OptionError,"ColorSeparatedImageRequired","`%s'",
1629 return(QuantumScale*pixel.black);
1636 if (LocaleCompare(symbol,"h") == 0)
1637 return((double) image->rows);
1638 if (LocaleCompare(symbol,"hue") == 0)
1645 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1654 if ((LocaleCompare(symbol,"image.depth") == 0) ||
1655 (LocaleCompare(symbol,"image.minima") == 0) ||
1656 (LocaleCompare(symbol,"image.maxima") == 0) ||
1657 (LocaleCompare(symbol,"image.mean") == 0) ||
1658 (LocaleCompare(symbol,"image.kurtosis") == 0) ||
1659 (LocaleCompare(symbol,"image.skewness") == 0) ||
1660 (LocaleCompare(symbol,"image.standard_deviation") == 0))
1661 return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
1662 if (LocaleCompare(symbol,"image.resolution.x") == 0)
1663 return(image->resolution.x);
1664 if (LocaleCompare(symbol,"image.resolution.y") == 0)
1665 return(image->resolution.y);
1666 if (LocaleCompare(symbol,"intensity") == 0)
1669 quantum_pixel[MaxPixelChannels];
1671 SetPixelViaPixelInfo(image,&pixel,quantum_pixel);
1672 return(QuantumScale*GetPixelIntensity(image,quantum_pixel));
1674 if (LocaleCompare(symbol,"i") == 0)
1681 if (LocaleCompare(symbol,"j") == 0)
1688 if (LocaleCompare(symbol,"lightness") == 0)
1695 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1699 if (LocaleCompare(symbol,"luma") == 0)
1704 luma=0.212656*pixel.red+0.715158*pixel.green+0.072186*pixel.blue;
1705 return(QuantumScale*luma);
1707 if (LocaleCompare(symbol,"luminance") == 0)
1712 luminence=0.212656*pixel.red+0.715158*pixel.green+0.072186*pixel.blue;
1713 return(QuantumScale*luminence);
1720 if (LocaleNCompare(symbol,"maxima",6) == 0)
1721 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1722 if (LocaleNCompare(symbol,"mean",4) == 0)
1723 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1724 if (LocaleNCompare(symbol,"minima",6) == 0)
1725 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1726 if (LocaleCompare(symbol,"m") == 0)
1727 return(QuantumScale*pixel.green);
1733 if (LocaleCompare(symbol,"n") == 0)
1734 return((double) GetImageListLength(fx_info->images));
1740 if (LocaleCompare(symbol,"o") == 0)
1741 return(QuantumScale*pixel.alpha);
1747 if (LocaleCompare(symbol,"page.height") == 0)
1748 return((double) image->page.height);
1749 if (LocaleCompare(symbol,"page.width") == 0)
1750 return((double) image->page.width);
1751 if (LocaleCompare(symbol,"page.x") == 0)
1752 return((double) image->page.x);
1753 if (LocaleCompare(symbol,"page.y") == 0)
1754 return((double) image->page.y);
1760 if (LocaleCompare(symbol,"quality") == 0)
1761 return((double) image->quality);
1767 if (LocaleCompare(symbol,"resolution.x") == 0)
1768 return(image->resolution.x);
1769 if (LocaleCompare(symbol,"resolution.y") == 0)
1770 return(image->resolution.y);
1771 if (LocaleCompare(symbol,"r") == 0)
1772 return(QuantumScale*pixel.red);
1778 if (LocaleCompare(symbol,"saturation") == 0)
1785 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1789 if (LocaleNCompare(symbol,"skewness",8) == 0)
1790 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1791 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1792 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1798 if (LocaleCompare(symbol,"t") == 0)
1799 return((double) GetImageIndexInList(fx_info->images));
1805 if (LocaleCompare(symbol,"w") == 0)
1806 return((double) image->columns);
1812 if (LocaleCompare(symbol,"y") == 0)
1813 return(QuantumScale*pixel.blue);
1819 if (LocaleCompare(symbol,"z") == 0)
1820 return((double)GetImageDepth(image, fx_info->exception));
1826 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1827 if (value != (const char *) NULL)
1828 return(StringToDouble(value,(char **) NULL));
1829 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1830 "UnableToParseExpression","`%s'",symbol);
1834 static const char *FxOperatorPrecedence(const char *expression,
1835 ExceptionInfo *exception)
1839 UndefinedPrecedence,
1841 BitwiseComplementPrecedence,
1843 ExponentialNotationPrecedence,
1847 RelationalPrecedence,
1848 EquivalencyPrecedence,
1849 BitwiseAndPrecedence,
1850 BitwiseOrPrecedence,
1851 LogicalAndPrecedence,
1852 LogicalOrPrecedence,
1854 AssignmentPrecedence,
1874 subexpression=(const char *) NULL;
1875 target=NullPrecedence;
1876 while (*expression != '\0')
1878 precedence=UndefinedPrecedence;
1879 if ((isspace((int) ((unsigned char) *expression)) != 0) || (c == (int) '@'))
1884 switch (*expression)
1889 #if defined(MAGICKCORE_HAVE_ACOSH)
1890 if (LocaleNCompare(expression,"acosh",5) == 0)
1896 #if defined(MAGICKCORE_HAVE_ASINH)
1897 if (LocaleNCompare(expression,"asinh",5) == 0)
1903 #if defined(MAGICKCORE_HAVE_ATANH)
1904 if (LocaleNCompare(expression,"atanh",5) == 0)
1910 if (LocaleNCompare(expression,"atan2",5) == 0)
1920 if ((isdigit((int) ((unsigned char) c)) != 0) &&
1921 ((LocaleNCompare(expression,"E+",2) == 0) ||
1922 (LocaleNCompare(expression,"E-",2) == 0)))
1924 expression+=2; /* scientific notation */
1931 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1932 (LocaleNCompare(expression,"j1",2) == 0))
1941 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1948 if ((c == (int) '{') || (c == (int) '['))
1951 if ((c == (int) '}') || (c == (int) ']'))
1954 switch ((unsigned char) *expression)
1959 precedence=BitwiseComplementPrecedence;
1965 precedence=ExponentPrecedence;
1970 if (((c != 0) && ((isdigit((int) ((unsigned char) c)) != 0) ||
1971 (strchr(")",(int) ((unsigned char) c)) != (char *) NULL))) &&
1972 (((islower((int) ((unsigned char) *expression)) != 0) ||
1973 (strchr("(",(int) ((unsigned char) *expression)) != (char *) NULL)) ||
1974 ((isdigit((int) ((unsigned char) c)) == 0) &&
1975 (isdigit((int) ((unsigned char) *expression)) != 0))) &&
1976 (strchr("xy",(int) ((unsigned char) *expression)) == (char *) NULL))
1977 precedence=MultiplyPrecedence;
1984 precedence=MultiplyPrecedence;
1990 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
1992 precedence=AdditionPrecedence;
1995 case LeftShiftOperator:
1996 case RightShiftOperator:
1998 precedence=ShiftPrecedence;
2002 case LessThanEqualOperator:
2003 case GreaterThanEqualOperator:
2006 precedence=RelationalPrecedence;
2010 case NotEqualOperator:
2012 precedence=EquivalencyPrecedence;
2017 precedence=BitwiseAndPrecedence;
2022 precedence=BitwiseOrPrecedence;
2025 case LogicalAndOperator:
2027 precedence=LogicalAndPrecedence;
2030 case LogicalOrOperator:
2032 precedence=LogicalOrPrecedence;
2035 case ExponentialNotation:
2037 precedence=ExponentialNotationPrecedence;
2043 precedence=TernaryPrecedence;
2048 precedence=AssignmentPrecedence;
2053 precedence=CommaPrecedence;
2058 precedence=SeparatorPrecedence;
2062 if ((precedence == BitwiseComplementPrecedence) ||
2063 (precedence == TernaryPrecedence) ||
2064 (precedence == AssignmentPrecedence))
2066 if (precedence > target)
2069 Right-to-left associativity.
2072 subexpression=expression;
2076 if (precedence >= target)
2079 Left-to-right associativity.
2082 subexpression=expression;
2084 if (strchr("(",(int) *expression) != (char *) NULL)
2085 expression=FxSubexpression(expression,exception);
2086 c=(int) (*expression++);
2088 return(subexpression);
2091 static double FxEvaluateSubexpression(FxInfo *fx_info,
2092 const PixelChannel channel,const ssize_t x,const ssize_t y,
2093 const char *expression,size_t *depth,double *beta,ExceptionInfo *exception)
2095 #define FxMaxParenthesisDepth 58
2099 subexpression[MagickPathExtent];
2109 if (exception->severity >= ErrorException)
2111 while (isspace((int) ((unsigned char) *expression)) != 0)
2113 if (*expression == '\0')
2115 *subexpression='\0';
2116 p=FxOperatorPrecedence(expression,exception);
2117 if (p != (const char *) NULL)
2119 (void) CopyMagickString(subexpression,expression,(size_t)
2121 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,depth,
2123 switch ((unsigned char) *p)
2127 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth,beta,
2129 *beta=(double) (~(size_t) *beta);
2134 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth,beta,
2136 return(*beta == 0.0 ? 1.0 : 0.0);
2140 *beta=pow(alpha,FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth,
2145 case ExponentialNotation:
2147 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth,beta,
2149 return(alpha*(*beta));
2153 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth,beta,
2157 (void) ThrowMagickException(exception,GetMagickModule(),
2158 OptionError,"DivideByZero","`%s'",expression);
2161 return(alpha/(*beta));
2165 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth,beta,
2167 *beta=fabs(floor((*beta)+0.5));
2170 (void) ThrowMagickException(exception,GetMagickModule(),
2171 OptionError,"DivideByZero","`%s'",expression);
2174 return(fmod(alpha,*beta));
2178 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth,beta,
2180 return(alpha+(*beta));
2184 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth,beta,
2186 return(alpha-(*beta));
2188 case LeftShiftOperator:
2190 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth,beta,
2192 *beta=(double) ((size_t) (alpha+0.5) << (size_t) (gamma+0.5));
2195 case RightShiftOperator:
2197 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth,beta,
2199 *beta=(double) ((size_t) (alpha+0.5) >> (size_t) (gamma+0.5));
2204 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth,beta,
2206 return(alpha < *beta ? 1.0 : 0.0);
2208 case LessThanEqualOperator:
2210 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth,beta,
2212 return(alpha <= *beta ? 1.0 : 0.0);
2216 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth,beta,
2218 return(alpha > *beta ? 1.0 : 0.0);
2220 case GreaterThanEqualOperator:
2222 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth,beta,
2224 return(alpha >= *beta ? 1.0 : 0.0);
2228 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth,beta,
2230 return(fabs(alpha-(*beta)) < MagickEpsilon ? 1.0 : 0.0);
2232 case NotEqualOperator:
2234 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth,beta,
2236 return(fabs(alpha-(*beta)) >= MagickEpsilon ? 1.0 : 0.0);
2240 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth,beta,
2242 *beta=(double) ((size_t) (alpha+0.5) & (size_t) (gamma+0.5));
2247 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth,beta,
2249 *beta=(double) ((size_t) (alpha+0.5) | (size_t) (gamma+0.5));
2252 case LogicalAndOperator:
2260 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,depth,beta,
2262 *beta=(gamma > 0.0) ? 1.0 : 0.0;
2265 case LogicalOrOperator:
2273 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,depth,beta,
2275 *beta=(gamma > 0.0) ? 1.0 : 0.0;
2280 (void) CopyMagickString(subexpression,++p,MagickPathExtent);
2282 p=StringToken(":",&q);
2283 if (q == (char *) NULL)
2285 (void) ThrowMagickException(exception,GetMagickModule(),
2286 OptionError,"UnableToParseExpression","`%s'",subexpression);
2289 if (fabs(alpha) >= MagickEpsilon)
2290 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,depth,beta,
2293 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,depth,beta,
2300 numeric[MagickPathExtent];
2303 while (isalpha((int) ((unsigned char) *q)) != 0)
2307 (void) ThrowMagickException(exception,GetMagickModule(),
2308 OptionError,"UnableToParseExpression","`%s'",subexpression);
2311 ClearMagickException(exception);
2312 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth,beta,
2314 (void) FormatLocaleString(numeric,MagickPathExtent,"%g",*beta);
2315 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2316 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2317 subexpression),ConstantString(numeric));
2322 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth,beta,
2328 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth,beta,
2334 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,depth,beta,
2340 if (strchr("(",(int) *expression) != (char *) NULL)
2343 if (*depth >= FxMaxParenthesisDepth)
2344 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
2345 "ParenthesisNestedTooDeeply","`%s'",expression);
2346 (void) CopyMagickString(subexpression,expression+1,MagickPathExtent);
2347 subexpression[strlen(subexpression)-1]='\0';
2348 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,depth,
2353 switch (*expression)
2357 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,depth,beta,
2363 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,depth,beta,
2369 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,depth,beta,
2371 return((double) (~(size_t) (gamma+0.5)));
2376 if (LocaleNCompare(expression,"abs",3) == 0)
2378 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,depth,
2380 return(fabs(alpha));
2382 #if defined(MAGICKCORE_HAVE_ACOSH)
2383 if (LocaleNCompare(expression,"acosh",5) == 0)
2385 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,depth,
2387 return(acosh(alpha));
2390 if (LocaleNCompare(expression,"acos",4) == 0)
2392 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,depth,
2394 return(acos(alpha));
2396 #if defined(MAGICKCORE_HAVE_J1)
2397 if (LocaleNCompare(expression,"airy",4) == 0)
2399 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,depth,
2403 gamma=2.0*j1((MagickPI*alpha))/(MagickPI*alpha);
2404 return(gamma*gamma);
2407 #if defined(MAGICKCORE_HAVE_ASINH)
2408 if (LocaleNCompare(expression,"asinh",5) == 0)
2410 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,depth,
2412 return(asinh(alpha));
2415 if (LocaleNCompare(expression,"asin",4) == 0)
2417 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,depth,
2419 return(asin(alpha));
2421 if (LocaleNCompare(expression,"alt",3) == 0)
2423 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,depth,
2425 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
2427 if (LocaleNCompare(expression,"atan2",5) == 0)
2429 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,depth,
2431 return(atan2(alpha,*beta));
2433 #if defined(MAGICKCORE_HAVE_ATANH)
2434 if (LocaleNCompare(expression,"atanh",5) == 0)
2436 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,depth,
2438 return(atanh(alpha));
2441 if (LocaleNCompare(expression,"atan",4) == 0)
2443 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,depth,
2445 return(atan(alpha));
2447 if (LocaleCompare(expression,"a") == 0)
2448 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2454 if (LocaleCompare(expression,"b") == 0)
2455 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2461 if (LocaleNCompare(expression,"ceil",4) == 0)
2463 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,depth,
2465 return(ceil(alpha));
2467 if (LocaleNCompare(expression,"clamp",5) == 0)
2469 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,depth,
2477 if (LocaleNCompare(expression,"cosh",4) == 0)
2479 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,depth,
2481 return(cosh(alpha));
2483 if (LocaleNCompare(expression,"cos",3) == 0)
2485 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,depth,
2489 if (LocaleCompare(expression,"c") == 0)
2490 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2496 if (LocaleNCompare(expression,"debug",5) == 0)
2501 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,depth,
2503 if (fx_info->images->colorspace == CMYKColorspace)
2506 case CyanPixelChannel: type="cyan"; break;
2507 case MagentaPixelChannel: type="magenta"; break;
2508 case YellowPixelChannel: type="yellow"; break;
2509 case AlphaPixelChannel: type="opacity"; break;
2510 case BlackPixelChannel: type="black"; break;
2511 default: type="unknown"; break;
2516 case RedPixelChannel: type="red"; break;
2517 case GreenPixelChannel: type="green"; break;
2518 case BluePixelChannel: type="blue"; break;
2519 case AlphaPixelChannel: type="opacity"; break;
2520 default: type="unknown"; break;
2522 (void) CopyMagickString(subexpression,expression+6,MagickPathExtent);
2523 if (strlen(subexpression) > 1)
2524 subexpression[strlen(subexpression)-1]='\0';
2525 if (fx_info->file != (FILE *) NULL)
2526 (void) FormatLocaleFile(fx_info->file,"%s[%.20g,%.20g].%s: "
2527 "%s=%.*g\n",fx_info->images->filename,(double) x,(double) y,type,
2528 subexpression,GetMagickPrecision(),alpha);
2531 if (LocaleNCompare(expression,"drc",3) == 0)
2533 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,depth,
2535 return((alpha/(*beta*(alpha-1.0)+1.0)));
2542 if (LocaleCompare(expression,"epsilon") == 0)
2543 return(MagickEpsilon);
2544 #if defined(MAGICKCORE_HAVE_ERF)
2545 if (LocaleNCompare(expression,"erf",3) == 0)
2547 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,depth,
2552 if (LocaleNCompare(expression,"exp",3) == 0)
2554 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,depth,
2558 if (LocaleCompare(expression,"e") == 0)
2559 return(2.7182818284590452354);
2565 if (LocaleNCompare(expression,"floor",5) == 0)
2567 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,depth,
2569 return(floor(alpha));
2576 if (LocaleNCompare(expression,"gauss",5) == 0)
2578 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,depth,
2580 gamma=exp((-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
2583 if (LocaleNCompare(expression,"gcd",3) == 0)
2588 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,depth,
2590 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType) (*beta+
2592 return((double) gcd);
2594 if (LocaleCompare(expression,"g") == 0)
2595 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2601 if (LocaleCompare(expression,"h") == 0)
2602 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2603 if (LocaleCompare(expression,"hue") == 0)
2604 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2605 if (LocaleNCompare(expression,"hypot",5) == 0)
2607 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,depth,
2609 return(hypot(alpha,*beta));
2616 if (LocaleCompare(expression,"k") == 0)
2617 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2623 if (LocaleCompare(expression,"intensity") == 0)
2624 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2625 if (LocaleNCompare(expression,"int",3) == 0)
2627 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,depth,
2629 return(floor(alpha));
2631 if (LocaleNCompare(expression,"isnan",5) == 0)
2633 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,depth,
2635 return((double) !!IsNaN(alpha));
2637 if (LocaleCompare(expression,"i") == 0)
2638 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2644 if (LocaleCompare(expression,"j") == 0)
2645 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2646 #if defined(MAGICKCORE_HAVE_J0)
2647 if (LocaleNCompare(expression,"j0",2) == 0)
2649 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,depth,
2654 #if defined(MAGICKCORE_HAVE_J1)
2655 if (LocaleNCompare(expression,"j1",2) == 0)
2657 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,depth,
2662 #if defined(MAGICKCORE_HAVE_J1)
2663 if (LocaleNCompare(expression,"jinc",4) == 0)
2665 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,depth,
2669 gamma=(2.0*j1((MagickPI*alpha))/(MagickPI*alpha));
2678 if (LocaleNCompare(expression,"ln",2) == 0)
2680 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,depth,
2684 if (LocaleNCompare(expression,"logtwo",6) == 0)
2686 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,depth,
2688 return(log10(alpha))/log10(2.0);
2690 if (LocaleNCompare(expression,"log",3) == 0)
2692 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,depth,
2694 return(log10(alpha));
2696 if (LocaleCompare(expression,"lightness") == 0)
2697 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2703 if (LocaleCompare(expression,"MaxRGB") == 0)
2704 return(QuantumRange);
2705 if (LocaleNCompare(expression,"maxima",6) == 0)
2707 if (LocaleNCompare(expression,"max",3) == 0)
2709 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,depth,
2711 return(alpha > *beta ? alpha : *beta);
2713 if (LocaleNCompare(expression,"minima",6) == 0)
2715 if (LocaleNCompare(expression,"min",3) == 0)
2717 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,depth,
2719 return(alpha < *beta ? alpha : *beta);
2721 if (LocaleNCompare(expression,"mod",3) == 0)
2723 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,depth,
2725 gamma=alpha-floor((alpha/(*beta)))*(*beta);
2728 if (LocaleCompare(expression,"m") == 0)
2729 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2735 if (LocaleNCompare(expression,"not",3) == 0)
2737 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,depth,
2739 return((double) (alpha < MagickEpsilon));
2741 if (LocaleCompare(expression,"n") == 0)
2742 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2748 if (LocaleCompare(expression,"Opaque") == 0)
2750 if (LocaleCompare(expression,"o") == 0)
2751 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2757 if (LocaleCompare(expression,"phi") == 0)
2759 if (LocaleCompare(expression,"pi") == 0)
2761 if (LocaleNCompare(expression,"pow",3) == 0)
2763 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,depth,
2765 return(pow(alpha,*beta));
2767 if (LocaleCompare(expression,"p") == 0)
2768 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2774 if (LocaleCompare(expression,"QuantumRange") == 0)
2775 return(QuantumRange);
2776 if (LocaleCompare(expression,"QuantumScale") == 0)
2777 return(QuantumScale);
2783 if (LocaleNCompare(expression,"rand",4) == 0)
2785 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2786 #pragma omp critical (MagickCore_FxEvaluateSubexpression)
2788 alpha=GetPseudoRandomValue(fx_info->random_info);
2791 if (LocaleNCompare(expression,"round",5) == 0)
2793 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,depth,
2795 return(floor(alpha+0.5));
2797 if (LocaleCompare(expression,"r") == 0)
2798 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2804 if (LocaleCompare(expression,"saturation") == 0)
2805 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2806 if (LocaleNCompare(expression,"sign",4) == 0)
2808 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,depth,
2810 return(alpha < 0.0 ? -1.0 : 1.0);
2812 if (LocaleNCompare(expression,"sinc",4) == 0)
2814 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,depth,
2818 gamma=sin((MagickPI*alpha))/(MagickPI*alpha);
2821 if (LocaleNCompare(expression,"sinh",4) == 0)
2823 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,depth,
2825 return(sinh(alpha));
2827 if (LocaleNCompare(expression,"sin",3) == 0)
2829 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,depth,
2833 if (LocaleNCompare(expression,"sqrt",4) == 0)
2835 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,depth,
2837 return(sqrt(alpha));
2839 if (LocaleNCompare(expression,"squish",6) == 0)
2841 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,depth,
2843 return((1.0/(1.0+exp(-alpha))));
2845 if (LocaleCompare(expression,"s") == 0)
2846 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2852 if (LocaleNCompare(expression,"tanh",4) == 0)
2854 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,depth,
2856 return(tanh(alpha));
2858 if (LocaleNCompare(expression,"tan",3) == 0)
2860 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,depth,
2864 if (LocaleCompare(expression,"Transparent") == 0)
2866 if (LocaleNCompare(expression,"trunc",5) == 0)
2868 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,depth,
2871 return(floor(alpha));
2872 return(ceil(alpha));
2874 if (LocaleCompare(expression,"t") == 0)
2875 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2881 if (LocaleCompare(expression,"u") == 0)
2882 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2888 if (LocaleCompare(expression,"v") == 0)
2889 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2895 if (LocaleNCompare(expression,"while",5) == 0)
2899 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,
2900 depth,beta,exception);
2901 } while (fabs(alpha) >= MagickEpsilon);
2904 if (LocaleCompare(expression,"w") == 0)
2905 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2911 if (LocaleCompare(expression,"y") == 0)
2912 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2918 if (LocaleCompare(expression,"z") == 0)
2919 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2925 q=(char *) expression;
2926 alpha=InterpretSiPrefixValue(expression,&q);
2927 if (q == expression)
2928 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2932 MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
2933 double *alpha,ExceptionInfo *exception)
2938 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2943 MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
2944 double *alpha,ExceptionInfo *exception)
2953 fx_info->file=(FILE *) NULL;
2954 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2960 MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
2961 const PixelChannel channel,const ssize_t x,const ssize_t y,
2962 double *alpha,ExceptionInfo *exception)
2972 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&depth,
2974 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2978 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2986 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2988 % FxImage() applies a mathematical expression to the specified image.
2990 % The format of the FxImage method is:
2992 % Image *FxImage(const Image *image,const char *expression,
2993 % ExceptionInfo *exception)
2995 % A description of each parameter follows:
2997 % o image: the image.
2999 % o expression: A mathematical expression.
3001 % o exception: return any errors or warnings in this structure.
3005 static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
3010 assert(fx_info != (FxInfo **) NULL);
3011 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
3012 if (fx_info[i] != (FxInfo *) NULL)
3013 fx_info[i]=DestroyFxInfo(fx_info[i]);
3014 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
3018 static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
3019 ExceptionInfo *exception)
3036 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
3037 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
3038 if (fx_info == (FxInfo **) NULL)
3040 (void) ThrowMagickException(exception,GetMagickModule(),
3041 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
3042 return((FxInfo **) NULL);
3044 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
3045 if (*expression != '@')
3046 fx_expression=ConstantString(expression);
3048 fx_expression=FileToString(expression+1,~0UL,exception);
3049 for (i=0; i < (ssize_t) number_threads; i++)
3054 fx_info[i]=AcquireFxInfo(image,fx_expression,exception);
3055 if (fx_info[i] == (FxInfo *) NULL)
3057 status=FxPreprocessExpression(fx_info[i],&alpha,exception);
3058 if (status == MagickFalse)
3061 fx_expression=DestroyString(fx_expression);
3062 if (i < (ssize_t) number_threads)
3063 fx_info=DestroyFxThreadSet(fx_info);
3067 MagickExport Image *FxImage(const Image *image,const char *expression,
3068 ExceptionInfo *exception)
3070 #define FxImageTag "Fx/Image"
3077 **magick_restrict fx_info;
3091 assert(image != (Image *) NULL);
3092 assert(image->signature == MagickCoreSignature);
3093 if (image->debug != MagickFalse)
3094 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3095 fx_info=AcquireFxThreadSet(image,expression,exception);
3096 if (fx_info == (FxInfo **) NULL)
3097 return((Image *) NULL);
3098 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3099 if (fx_image == (Image *) NULL)
3101 fx_info=DestroyFxThreadSet(fx_info);
3102 return((Image *) NULL);
3104 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
3106 fx_info=DestroyFxThreadSet(fx_info);
3107 fx_image=DestroyImage(fx_image);
3108 return((Image *) NULL);
3115 image_view=AcquireVirtualCacheView(image,exception);
3116 fx_view=AcquireAuthenticCacheView(fx_image,exception);
3117 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3118 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3119 magick_threads(image,fx_image,fx_image->rows,1)
3121 for (y=0; y < (ssize_t) fx_image->rows; y++)
3124 id = GetOpenMPThreadId();
3126 register const Quantum
3135 if (status == MagickFalse)
3137 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3138 q=QueueCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
3139 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3144 for (x=0; x < (ssize_t) fx_image->columns; x++)
3149 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3154 PixelChannel channel=GetPixelChannelChannel(image,i);
3155 PixelTrait traits=GetPixelChannelTraits(image,channel);
3156 PixelTrait fx_traits=GetPixelChannelTraits(fx_image,channel);
3157 if ((traits == UndefinedPixelTrait) ||
3158 (fx_traits == UndefinedPixelTrait))
3160 if (((fx_traits & CopyPixelTrait) != 0) ||
3161 (GetPixelWriteMask(image,p) == 0))
3163 SetPixelChannel(fx_image,channel,p[i],q);
3167 (void) FxEvaluateChannelExpression(fx_info[id],channel,x,y,&alpha,
3169 q[i]=ClampToQuantum(QuantumRange*alpha);
3171 p+=GetPixelChannels(image);
3172 q+=GetPixelChannels(fx_image);
3174 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3176 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3181 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3182 #pragma omp critical (MagickCore_FxImage)
3184 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3185 if (proceed == MagickFalse)
3189 fx_view=DestroyCacheView(fx_view);
3190 image_view=DestroyCacheView(image_view);
3191 fx_info=DestroyFxThreadSet(fx_info);
3192 if (status == MagickFalse)
3193 fx_image=DestroyImage(fx_image);
3198 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3202 % I m p l o d e I m a g e %
3206 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3208 % ImplodeImage() creates a new image that is a copy of an existing
3209 % one with the image pixels "implode" by the specified percentage. It
3210 % allocates the memory necessary for the new Image structure and returns a
3211 % pointer to the new image.
3213 % The format of the ImplodeImage method is:
3215 % Image *ImplodeImage(const Image *image,const double amount,
3216 % const PixelInterpolateMethod method,ExceptionInfo *exception)
3218 % A description of each parameter follows:
3220 % o implode_image: Method ImplodeImage returns a pointer to the image
3221 % after it is implode. A null image is returned if there is a memory
3224 % o image: the image.
3226 % o amount: Define the extent of the implosion.
3228 % o method: the pixel interpolation method.
3230 % o exception: return any errors or warnings in this structure.
3233 MagickExport Image *ImplodeImage(const Image *image,const double amount,
3234 const PixelInterpolateMethod method,ExceptionInfo *exception)
3236 #define ImplodeImageTag "Implode/Image"
3264 Initialize implode image attributes.
3266 assert(image != (Image *) NULL);
3267 assert(image->signature == MagickCoreSignature);
3268 if (image->debug != MagickFalse)
3269 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3270 assert(exception != (ExceptionInfo *) NULL);
3271 assert(exception->signature == MagickCoreSignature);
3272 canvas=CloneImage(image,0,0,MagickTrue,exception);
3273 if (canvas == (Image *) NULL)
3274 return((Image *) NULL);
3275 if ((canvas->alpha_trait == UndefinedPixelTrait) &&
3276 (canvas->background_color.alpha != OpaqueAlpha))
3277 (void) SetImageAlphaChannel(canvas,OpaqueAlphaChannel,exception);
3278 implode_image=CloneImage(canvas,canvas->columns,canvas->rows,MagickTrue,
3280 if (implode_image == (Image *) NULL)
3282 canvas=DestroyImage(canvas);
3283 return((Image *) NULL);
3285 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
3287 canvas=DestroyImage(canvas);
3288 implode_image=DestroyImage(implode_image);
3289 return((Image *) NULL);
3292 Compute scaling factor.
3296 center.x=0.5*canvas->columns;
3297 center.y=0.5*canvas->rows;
3299 if (canvas->columns > canvas->rows)
3300 scale.y=(double) canvas->columns/(double) canvas->rows;
3302 if (canvas->columns < canvas->rows)
3304 scale.x=(double) canvas->rows/(double) canvas->columns;
3312 canvas_view=AcquireVirtualCacheView(canvas,exception);
3313 interpolate_view=AcquireVirtualCacheView(canvas,exception);
3314 implode_view=AcquireAuthenticCacheView(implode_image,exception);
3315 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3316 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3317 magick_threads(canvas,implode_image,canvas->rows,1)
3319 for (y=0; y < (ssize_t) canvas->rows; y++)
3327 register const Quantum
3336 if (status == MagickFalse)
3338 p=GetCacheViewVirtualPixels(canvas_view,0,y,canvas->columns,1,exception);
3339 q=QueueCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
3341 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3346 delta.y=scale.y*(double) (y-center.y);
3347 for (x=0; x < (ssize_t) canvas->columns; x++)
3353 Determine if the pixel is within an ellipse.
3355 if (GetPixelWriteMask(canvas,p) == 0)
3357 SetPixelBackgoundColor(implode_image,q);
3358 p+=GetPixelChannels(canvas);
3359 q+=GetPixelChannels(implode_image);
3362 delta.x=scale.x*(double) (x-center.x);
3363 distance=delta.x*delta.x+delta.y*delta.y;
3364 if (distance >= (radius*radius))
3365 for (i=0; i < (ssize_t) GetPixelChannels(canvas); i++)
3367 PixelChannel channel = GetPixelChannelChannel(canvas,i);
3368 PixelTrait traits = GetPixelChannelTraits(canvas,channel);
3369 PixelTrait implode_traits = GetPixelChannelTraits(implode_image,
3371 if ((traits == UndefinedPixelTrait) ||
3372 (implode_traits == UndefinedPixelTrait))
3374 SetPixelChannel(implode_image,channel,p[i],q);
3386 factor=pow(sin(MagickPI*sqrt((double) distance)/radius/2),-amount);
3387 status=InterpolatePixelChannels(canvas,interpolate_view,implode_image,
3388 method,(double) (factor*delta.x/scale.x+center.x),(double) (factor*
3389 delta.y/scale.y+center.y),q,exception);
3391 p+=GetPixelChannels(canvas);
3392 q+=GetPixelChannels(implode_image);
3394 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3396 if (canvas->progress_monitor != (MagickProgressMonitor) NULL)
3401 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3402 #pragma omp critical (MagickCore_ImplodeImage)
3404 proceed=SetImageProgress(canvas,ImplodeImageTag,progress++,
3406 if (proceed == MagickFalse)
3410 implode_view=DestroyCacheView(implode_view);
3411 interpolate_view=DestroyCacheView(interpolate_view);
3412 canvas_view=DestroyCacheView(canvas_view);
3413 canvas=DestroyImage(canvas);
3414 if (status == MagickFalse)
3415 implode_image=DestroyImage(implode_image);
3416 return(implode_image);
3420 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3424 % M o r p h I m a g e s %
3428 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3430 % The MorphImages() method requires a minimum of two images. The first
3431 % image is transformed into the second by a number of intervening images
3432 % as specified by frames.
3434 % The format of the MorphImage method is:
3436 % Image *MorphImages(const Image *image,const size_t number_frames,
3437 % ExceptionInfo *exception)
3439 % A description of each parameter follows:
3441 % o image: the image.
3443 % o number_frames: Define the number of in-between image to generate.
3444 % The more in-between frames, the smoother the morph.
3446 % o exception: return any errors or warnings in this structure.
3449 MagickExport Image *MorphImages(const Image *image,const size_t number_frames,
3450 ExceptionInfo *exception)
3452 #define MorphImageTag "Morph/Image"
3468 register const Image
3478 Clone first frame in sequence.
3480 assert(image != (Image *) NULL);
3481 assert(image->signature == MagickCoreSignature);
3482 if (image->debug != MagickFalse)
3483 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3484 assert(exception != (ExceptionInfo *) NULL);
3485 assert(exception->signature == MagickCoreSignature);
3486 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3487 if (morph_images == (Image *) NULL)
3488 return((Image *) NULL);
3489 if (GetNextImageInList(image) == (Image *) NULL)
3494 for (n=1; n < (ssize_t) number_frames; n++)
3496 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3497 if (morph_image == (Image *) NULL)
3499 morph_images=DestroyImageList(morph_images);
3500 return((Image *) NULL);
3502 AppendImageToList(&morph_images,morph_image);
3503 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3508 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) n,
3510 if (proceed == MagickFalse)
3514 return(GetFirstImageInList(morph_images));
3517 Morph image sequence.
3522 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3524 for (n=0; n < (ssize_t) number_frames; n++)
3530 beta=(double) (n+1.0)/(double) (number_frames+1.0);
3532 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
3533 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*next->rows+beta*
3534 GetNextImageInList(next)->rows+0.5),next->filter,exception);
3535 if (morph_image == (Image *) NULL)
3537 morph_images=DestroyImageList(morph_images);
3538 return((Image *) NULL);
3540 status=SetImageStorageClass(morph_image,DirectClass,exception);
3541 if (status == MagickFalse)
3543 morph_image=DestroyImage(morph_image);
3544 return((Image *) NULL);
3546 AppendImageToList(&morph_images,morph_image);
3547 morph_images=GetLastImageInList(morph_images);
3548 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
3549 morph_images->rows,GetNextImageInList(next)->filter,exception);
3550 if (morph_image == (Image *) NULL)
3552 morph_images=DestroyImageList(morph_images);
3553 return((Image *) NULL);
3555 image_view=AcquireVirtualCacheView(morph_image,exception);
3556 morph_view=AcquireAuthenticCacheView(morph_images,exception);
3557 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3558 #pragma omp parallel for schedule(static,4) shared(status) \
3559 magick_threads(morph_image,morph_image,morph_image->rows,1)
3561 for (y=0; y < (ssize_t) morph_images->rows; y++)
3566 register const Quantum
3575 if (status == MagickFalse)
3577 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3579 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3581 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3586 for (x=0; x < (ssize_t) morph_images->columns; x++)
3591 for (i=0; i < (ssize_t) GetPixelChannels(morph_image); i++)
3593 PixelChannel channel=GetPixelChannelChannel(morph_image,i);
3594 PixelTrait traits=GetPixelChannelTraits(morph_image,channel);
3595 PixelTrait morph_traits=GetPixelChannelTraits(morph_images,channel);
3596 if ((traits == UndefinedPixelTrait) ||
3597 (morph_traits == UndefinedPixelTrait))
3599 if (((morph_traits & CopyPixelTrait) != 0) ||
3600 (GetPixelWriteMask(morph_images,p) == 0))
3602 SetPixelChannel(morph_image,channel,p[i],q);
3605 SetPixelChannel(morph_image,channel,ClampToQuantum(alpha*
3606 GetPixelChannel(morph_images,channel,q)+beta*p[i]),q);
3608 p+=GetPixelChannels(morph_image);
3609 q+=GetPixelChannels(morph_images);
3611 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3612 if (sync == MagickFalse)
3615 morph_view=DestroyCacheView(morph_view);
3616 image_view=DestroyCacheView(image_view);
3617 morph_image=DestroyImage(morph_image);
3619 if (n < (ssize_t) number_frames)
3622 Clone last frame in sequence.
3624 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3625 if (morph_image == (Image *) NULL)
3627 morph_images=DestroyImageList(morph_images);
3628 return((Image *) NULL);
3630 AppendImageToList(&morph_images,morph_image);
3631 morph_images=GetLastImageInList(morph_images);
3632 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3637 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3638 #pragma omp critical (MagickCore_MorphImages)
3640 proceed=SetImageProgress(image,MorphImageTag,scene,
3641 GetImageListLength(image));
3642 if (proceed == MagickFalse)
3647 if (GetNextImageInList(next) != (Image *) NULL)
3649 morph_images=DestroyImageList(morph_images);
3650 return((Image *) NULL);
3652 return(GetFirstImageInList(morph_images));
3656 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3660 % P l a s m a I m a g e %
3664 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3666 % PlasmaImage() initializes an image with plasma fractal values. The image
3667 % must be initialized with a base color and the random number generator
3668 % seeded before this method is called.
3670 % The format of the PlasmaImage method is:
3672 % MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
3673 % size_t attenuate,size_t depth,ExceptionInfo *exception)
3675 % A description of each parameter follows:
3677 % o image: the image.
3679 % o segment: Define the region to apply plasma fractals values.
3681 % o attenuate: Define the plasma attenuation factor.
3683 % o depth: Limit the plasma recursion depth.
3685 % o exception: return any errors or warnings in this structure.
3689 static inline Quantum PlasmaPixel(RandomInfo *random_info,
3690 const double pixel,const double noise)
3695 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
3698 return((Quantum) 0);
3699 if (plasma >= QuantumRange)
3700 return(QuantumRange);
3704 static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3705 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3706 const SegmentInfo *segment,size_t attenuate,size_t depth,
3707 ExceptionInfo *exception)
3712 register const Quantum
3728 if ((fabs(segment->x2-segment->x1) <= MagickEpsilon) &&
3729 (fabs(segment->y2-segment->y1) <= MagickEpsilon))
3740 Divide the area into quadrants and recurse.
3744 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3745 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
3746 local_info=(*segment);
3747 local_info.x2=(double) x_mid;
3748 local_info.y2=(double) y_mid;
3749 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3750 &local_info,attenuate,depth,exception);
3751 local_info=(*segment);
3752 local_info.y1=(double) y_mid;
3753 local_info.x2=(double) x_mid;
3754 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3755 &local_info,attenuate,depth,exception);
3756 local_info=(*segment);
3757 local_info.x1=(double) x_mid;
3758 local_info.y2=(double) y_mid;
3759 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3760 &local_info,attenuate,depth,exception);
3761 local_info=(*segment);
3762 local_info.x1=(double) x_mid;
3763 local_info.y1=(double) y_mid;
3764 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3765 &local_info,attenuate,depth,exception);
3768 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3769 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
3770 if ((fabs(segment->x1-x_mid) < MagickEpsilon) &&
3771 (fabs(segment->x2-x_mid) < MagickEpsilon) &&
3772 (fabs(segment->y1-y_mid) < MagickEpsilon) &&
3773 (fabs(segment->y2-y_mid) < MagickEpsilon))
3774 return(MagickFalse);
3776 Average pixels and apply plasma.
3778 plasma=(double) QuantumRange/(2.0*attenuate);
3779 if ((fabs(segment->x1-x_mid) > MagickEpsilon) ||
3780 (fabs(segment->x2-x_mid) > MagickEpsilon))
3785 x=(ssize_t) ceil(segment->x1-0.5);
3786 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),1,1,
3788 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),1,1,
3790 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
3791 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3792 (q == (Quantum *) NULL))
3794 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3796 PixelChannel channel=GetPixelChannelChannel(image,i);
3797 PixelTrait traits=GetPixelChannelTraits(image,channel);
3798 if (traits == UndefinedPixelTrait)
3800 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
3802 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3803 if (fabs(segment->x1-segment->x2) > MagickEpsilon)
3808 x=(ssize_t) ceil(segment->x2-0.5);
3809 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3811 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3813 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
3814 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3815 (q == (Quantum *) NULL))
3817 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3819 PixelChannel channel=GetPixelChannelChannel(image,i);
3820 PixelTrait traits=GetPixelChannelTraits(image,channel);
3821 if (traits == UndefinedPixelTrait)
3823 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
3825 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3828 if ((fabs(segment->y1-y_mid) > MagickEpsilon) ||
3829 (fabs(segment->y2-y_mid) > MagickEpsilon))
3831 if ((fabs(segment->x1-x_mid) > MagickEpsilon) ||
3832 (fabs(segment->y2-y_mid) > MagickEpsilon))
3837 y=(ssize_t) ceil(segment->y2-0.5);
3838 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3840 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3842 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
3843 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3844 (q == (Quantum *) NULL))
3846 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3848 PixelChannel channel=GetPixelChannelChannel(image,i);
3849 PixelTrait traits=GetPixelChannelTraits(image,channel);
3850 if (traits == UndefinedPixelTrait)
3852 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
3854 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3856 if (fabs(segment->y1-segment->y2) > MagickEpsilon)
3861 y=(ssize_t) ceil(segment->y1-0.5);
3862 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3864 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3866 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
3867 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3868 (q == (Quantum *) NULL))
3870 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3872 PixelChannel channel=GetPixelChannelChannel(image,i);
3873 PixelTrait traits=GetPixelChannelTraits(image,channel);
3874 if (traits == UndefinedPixelTrait)
3876 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
3878 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3881 if ((fabs(segment->x1-segment->x2) > MagickEpsilon) ||
3882 (fabs(segment->y1-segment->y2) > MagickEpsilon))
3887 x=(ssize_t) ceil(segment->x1-0.5);
3888 y=(ssize_t) ceil(segment->y1-0.5);
3889 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
3890 x=(ssize_t) ceil(segment->x2-0.5);
3891 y=(ssize_t) ceil(segment->y2-0.5);
3892 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
3893 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
3894 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3895 (q == (Quantum *) NULL))
3897 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3899 PixelChannel channel=GetPixelChannelChannel(image,i);
3900 PixelTrait traits=GetPixelChannelTraits(image,channel);
3901 if (traits == UndefinedPixelTrait)
3903 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
3905 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3907 if ((fabs(segment->x2-segment->x1) < 3.0) &&
3908 (fabs(segment->y2-segment->y1) < 3.0))
3910 return(MagickFalse);
3913 MagickExport MagickBooleanType PlasmaImage(Image *image,
3914 const SegmentInfo *segment,size_t attenuate,size_t depth,
3915 ExceptionInfo *exception)
3928 if (image->debug != MagickFalse)
3929 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3930 assert(image != (Image *) NULL);
3931 assert(image->signature == MagickCoreSignature);
3932 if (image->debug != MagickFalse)
3933 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3934 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
3935 return(MagickFalse);
3936 image_view=AcquireAuthenticCacheView(image,exception);
3937 u_view=AcquireVirtualCacheView(image,exception);
3938 v_view=AcquireVirtualCacheView(image,exception);
3939 random_info=AcquireRandomInfo();
3940 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3941 attenuate,depth,exception);
3942 random_info=DestroyRandomInfo(random_info);
3943 v_view=DestroyCacheView(v_view);
3944 u_view=DestroyCacheView(u_view);
3945 image_view=DestroyCacheView(image_view);
3950 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3954 % P o l a r o i d I m a g e %
3958 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3960 % PolaroidImage() simulates a Polaroid picture.
3962 % The format of the PolaroidImage method is:
3964 % Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
3965 % const char *caption,const double angle,
3966 % const PixelInterpolateMethod method,ExceptionInfo exception)
3968 % A description of each parameter follows:
3970 % o image: the image.
3972 % o draw_info: the draw info.
3974 % o caption: the Polaroid caption.
3976 % o angle: Apply the effect along this angle.
3978 % o method: the pixel interpolation method.
3980 % o exception: return any errors or warnings in this structure.
3983 MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
3984 const char *caption,const double angle,const PixelInterpolateMethod method,
3985 ExceptionInfo *exception)
4003 Simulate a Polaroid picture.
4005 assert(image != (Image *) NULL);
4006 assert(image->signature == MagickCoreSignature);
4007 if (image->debug != MagickFalse)
4008 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4009 assert(exception != (ExceptionInfo *) NULL);
4010 assert(exception->signature == MagickCoreSignature);
4011 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
4012 image->rows)/25.0,10.0);
4013 height=image->rows+2*quantum;
4014 caption_image=(Image *) NULL;
4015 if (caption != (const char *) NULL)
4018 geometry[MagickPathExtent],
4037 Generate caption image.
4039 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
4040 if (caption_image == (Image *) NULL)
4041 return((Image *) NULL);
4042 image_info=AcquireImageInfo();
4043 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
4044 text=InterpretImageProperties(image_info,(Image *) image,caption,
4046 image_info=DestroyImageInfo(image_info);
4047 (void) CloneString(&annotate_info->text,text);
4048 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
4050 status=SetImageExtent(caption_image,image->columns,(size_t) ((count+1)*
4051 (metrics.ascent-metrics.descent)+0.5),exception);
4052 if (status == MagickFalse)
4053 caption_image=DestroyImage(caption_image);
4056 caption_image->background_color=image->border_color;
4057 (void) SetImageBackgroundColor(caption_image,exception);
4058 (void) CloneString(&annotate_info->text,text);
4059 (void) FormatLocaleString(geometry,MagickPathExtent,"+0+%g",
4061 if (annotate_info->gravity == UndefinedGravity)
4062 (void) CloneString(&annotate_info->geometry,AcquireString(
4064 (void) AnnotateImage(caption_image,annotate_info,exception);
4065 height+=caption_image->rows;
4067 annotate_info=DestroyDrawInfo(annotate_info);
4068 text=DestroyString(text);
4070 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
4072 if (picture_image == (Image *) NULL)
4074 if (caption_image != (Image *) NULL)
4075 caption_image=DestroyImage(caption_image);
4076 return((Image *) NULL);
4078 picture_image->background_color=image->border_color;
4079 (void) SetImageBackgroundColor(picture_image,exception);
4080 (void) CompositeImage(picture_image,image,OverCompositeOp,MagickTrue,quantum,
4082 if (caption_image != (Image *) NULL)
4084 (void) CompositeImage(picture_image,caption_image,OverCompositeOp,
4085 MagickTrue,quantum,(ssize_t) (image->rows+3*quantum/2),exception);
4086 caption_image=DestroyImage(caption_image);
4088 (void) QueryColorCompliance("none",AllCompliance,
4089 &picture_image->background_color,exception);
4090 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
4091 rotate_image=RotateImage(picture_image,90.0,exception);
4092 picture_image=DestroyImage(picture_image);
4093 if (rotate_image == (Image *) NULL)
4094 return((Image *) NULL);
4095 picture_image=rotate_image;
4096 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
4097 picture_image->columns,method,exception);
4098 picture_image=DestroyImage(picture_image);
4099 if (bend_image == (Image *) NULL)
4100 return((Image *) NULL);
4101 picture_image=bend_image;
4102 rotate_image=RotateImage(picture_image,-90.0,exception);
4103 picture_image=DestroyImage(picture_image);
4104 if (rotate_image == (Image *) NULL)
4105 return((Image *) NULL);
4106 picture_image=rotate_image;
4107 picture_image->background_color=image->background_color;
4108 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
4110 if (polaroid_image == (Image *) NULL)
4112 picture_image=DestroyImage(picture_image);
4113 return(picture_image);
4115 flop_image=FlopImage(polaroid_image,exception);
4116 polaroid_image=DestroyImage(polaroid_image);
4117 if (flop_image == (Image *) NULL)
4119 picture_image=DestroyImage(picture_image);
4120 return(picture_image);
4122 polaroid_image=flop_image;
4123 (void) CompositeImage(polaroid_image,picture_image,OverCompositeOp,
4124 MagickTrue,(ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
4125 picture_image=DestroyImage(picture_image);
4126 (void) QueryColorCompliance("none",AllCompliance,
4127 &polaroid_image->background_color,exception);
4128 rotate_image=RotateImage(polaroid_image,angle,exception);
4129 polaroid_image=DestroyImage(polaroid_image);
4130 if (rotate_image == (Image *) NULL)
4131 return((Image *) NULL);
4132 polaroid_image=rotate_image;
4133 trim_image=TrimImage(polaroid_image,exception);
4134 polaroid_image=DestroyImage(polaroid_image);
4135 if (trim_image == (Image *) NULL)
4136 return((Image *) NULL);
4137 polaroid_image=trim_image;
4138 return(polaroid_image);
4142 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4146 % S e p i a T o n e I m a g e %
4150 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4152 % MagickSepiaToneImage() applies a special effect to the image, similar to the
4153 % effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4154 % 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4155 % threshold of 80% is a good starting point for a reasonable tone.
4157 % The format of the SepiaToneImage method is:
4159 % Image *SepiaToneImage(const Image *image,const double threshold,
4160 % ExceptionInfo *exception)
4162 % A description of each parameter follows:
4164 % o image: the image.
4166 % o threshold: the tone threshold.
4168 % o exception: return any errors or warnings in this structure.
4171 MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4172 ExceptionInfo *exception)
4174 #define SepiaToneImageTag "SepiaTone/Image"
4193 Initialize sepia-toned image attributes.
4195 assert(image != (const Image *) NULL);
4196 assert(image->signature == MagickCoreSignature);
4197 if (image->debug != MagickFalse)
4198 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4199 assert(exception != (ExceptionInfo *) NULL);
4200 assert(exception->signature == MagickCoreSignature);
4201 sepia_image=CloneImage(image,0,0,MagickTrue,exception);
4202 if (sepia_image == (Image *) NULL)
4203 return((Image *) NULL);
4204 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
4206 sepia_image=DestroyImage(sepia_image);
4207 return((Image *) NULL);
4210 Tone each row of the image.
4214 image_view=AcquireVirtualCacheView(image,exception);
4215 sepia_view=AcquireAuthenticCacheView(sepia_image,exception);
4216 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4217 #pragma omp parallel for schedule(static,4) shared(progress,status) \
4218 magick_threads(image,sepia_image,image->rows,1)
4220 for (y=0; y < (ssize_t) image->rows; y++)
4222 register const Quantum
4231 if (status == MagickFalse)
4233 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
4234 q=GetCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
4236 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
4241 for (x=0; x < (ssize_t) image->columns; x++)
4247 intensity=GetPixelIntensity(image,p);
4248 tone=intensity > threshold ? (double) QuantumRange : intensity+
4249 (double) QuantumRange-threshold;
4250 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
4251 tone=intensity > (7.0*threshold/6.0) ? (double) QuantumRange :
4252 intensity+(double) QuantumRange-7.0*threshold/6.0;
4253 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4254 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
4255 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
4257 if ((double) GetPixelGreen(image,q) < tone)
4258 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4259 if ((double) GetPixelBlue(image,q) < tone)
4260 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
4261 SetPixelAlpha(sepia_image,GetPixelAlpha(image,p),q);
4262 p+=GetPixelChannels(image);
4263 q+=GetPixelChannels(sepia_image);
4265 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4267 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4272 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4273 #pragma omp critical (MagickCore_SepiaToneImage)
4275 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4277 if (proceed == MagickFalse)
4281 sepia_view=DestroyCacheView(sepia_view);
4282 image_view=DestroyCacheView(image_view);
4283 (void) NormalizeImage(sepia_image,exception);
4284 (void) ContrastImage(sepia_image,MagickTrue,exception);
4285 if (status == MagickFalse)
4286 sepia_image=DestroyImage(sepia_image);
4287 return(sepia_image);
4291 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4295 % S h a d o w I m a g e %
4299 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4301 % ShadowImage() simulates a shadow from the specified image and returns it.
4303 % The format of the ShadowImage method is:
4305 % Image *ShadowImage(const Image *image,const double alpha,
4306 % const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4307 % ExceptionInfo *exception)
4309 % A description of each parameter follows:
4311 % o image: the image.
4313 % o alpha: percentage transparency.
4315 % o sigma: the standard deviation of the Gaussian, in pixels.
4317 % o x_offset: the shadow x-offset.
4319 % o y_offset: the shadow y-offset.
4321 % o exception: return any errors or warnings in this structure.
4324 MagickExport Image *ShadowImage(const Image *image,const double alpha,
4325 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4326 ExceptionInfo *exception)
4328 #define ShadowImageTag "Shadow/Image"
4353 assert(image != (Image *) NULL);
4354 assert(image->signature == MagickCoreSignature);
4355 if (image->debug != MagickFalse)
4356 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4357 assert(exception != (ExceptionInfo *) NULL);
4358 assert(exception->signature == MagickCoreSignature);
4359 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4360 if (clone_image == (Image *) NULL)
4361 return((Image *) NULL);
4362 if (IsGrayColorspace(image->colorspace) != MagickFalse)
4363 (void) SetImageColorspace(clone_image,sRGBColorspace,exception);
4364 (void) SetImageVirtualPixelMethod(clone_image,EdgeVirtualPixelMethod,
4366 border_info.width=(size_t) floor(2.0*sigma+0.5);
4367 border_info.height=(size_t) floor(2.0*sigma+0.5);
4370 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4372 clone_image->alpha_trait=BlendPixelTrait;
4373 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
4374 clone_image=DestroyImage(clone_image);
4375 if (border_image == (Image *) NULL)
4376 return((Image *) NULL);
4377 if (border_image->alpha_trait == UndefinedPixelTrait)
4378 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
4383 background_color=border_image->background_color;
4384 background_color.alpha_trait=BlendPixelTrait;
4385 image_view=AcquireAuthenticCacheView(border_image,exception);
4386 for (y=0; y < (ssize_t) border_image->rows; y++)
4394 if (status == MagickFalse)
4396 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
4398 if (q == (Quantum *) NULL)
4403 for (x=0; x < (ssize_t) border_image->columns; x++)
4405 if (border_image->alpha_trait != UndefinedPixelTrait)
4406 background_color.alpha=GetPixelAlpha(border_image,q)*alpha/100.0;
4407 SetPixelViaPixelInfo(border_image,&background_color,q);
4408 q+=GetPixelChannels(border_image);
4410 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4413 image_view=DestroyCacheView(image_view);
4414 if (status == MagickFalse)
4416 border_image=DestroyImage(border_image);
4417 return((Image *) NULL);
4419 channel_mask=SetImageChannelMask(border_image,AlphaChannel);
4420 shadow_image=BlurImage(border_image,0.0,sigma,exception);
4421 border_image=DestroyImage(border_image);
4422 if (shadow_image == (Image *) NULL)
4423 return((Image *) NULL);
4424 (void) SetPixelChannelMask(shadow_image,channel_mask);
4425 if (shadow_image->page.width == 0)
4426 shadow_image->page.width=shadow_image->columns;
4427 if (shadow_image->page.height == 0)
4428 shadow_image->page.height=shadow_image->rows;
4429 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4430 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4431 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4432 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
4433 return(shadow_image);
4437 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4441 % S k e t c h I m a g e %
4445 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4447 % SketchImage() simulates a pencil sketch. We convolve the image with a
4448 % Gaussian operator of the given radius and standard deviation (sigma). For
4449 % reasonable results, radius should be larger than sigma. Use a radius of 0
4450 % and SketchImage() selects a suitable radius for you. Angle gives the angle
4453 % The format of the SketchImage method is:
4455 % Image *SketchImage(const Image *image,const double radius,
4456 % const double sigma,const double angle,ExceptionInfo *exception)
4458 % A description of each parameter follows:
4460 % o image: the image.
4462 % o radius: the radius of the Gaussian, in pixels, not counting the
4465 % o sigma: the standard deviation of the Gaussian, in pixels.
4467 % o angle: apply the effect along this angle.
4469 % o exception: return any errors or warnings in this structure.
4472 MagickExport Image *SketchImage(const Image *image,const double radius,
4473 const double sigma,const double angle,ExceptionInfo *exception)
4489 **magick_restrict random_info;
4494 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4502 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4503 MagickTrue,exception);
4504 if (random_image == (Image *) NULL)
4505 return((Image *) NULL);
4507 random_info=AcquireRandomInfoThreadSet();
4508 random_view=AcquireAuthenticCacheView(random_image,exception);
4509 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4510 key=GetRandomSecretKey(random_info[0]);
4511 #pragma omp parallel for schedule(static,4) shared(status) \
4512 magick_threads(random_image,random_image,random_image->rows,key == ~0UL)
4514 for (y=0; y < (ssize_t) random_image->rows; y++)
4517 id = GetOpenMPThreadId();
4525 if (status == MagickFalse)
4527 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4529 if (q == (Quantum *) NULL)
4534 for (x=0; x < (ssize_t) random_image->columns; x++)
4542 if (GetPixelWriteMask(random_image,q) == 0)
4544 q+=GetPixelChannels(random_image);
4547 value=GetPseudoRandomValue(random_info[id]);
4548 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4550 PixelChannel channel=GetPixelChannelChannel(image,i);
4551 PixelTrait traits=GetPixelChannelTraits(image,channel);
4552 if (traits == UndefinedPixelTrait)
4554 q[i]=ClampToQuantum(QuantumRange*value);
4556 q+=GetPixelChannels(random_image);
4558 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4561 random_view=DestroyCacheView(random_view);
4562 random_info=DestroyRandomInfoThreadSet(random_info);
4563 if (status == MagickFalse)
4565 random_image=DestroyImage(random_image);
4566 return(random_image);
4568 blur_image=MotionBlurImage(random_image,radius,sigma,angle,exception);
4569 random_image=DestroyImage(random_image);
4570 if (blur_image == (Image *) NULL)
4571 return((Image *) NULL);
4572 dodge_image=EdgeImage(blur_image,radius,exception);
4573 blur_image=DestroyImage(blur_image);
4574 if (dodge_image == (Image *) NULL)
4575 return((Image *) NULL);
4576 (void) NormalizeImage(dodge_image,exception);
4577 (void) NegateImage(dodge_image,MagickFalse,exception);
4578 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
4579 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4580 if (sketch_image == (Image *) NULL)
4582 dodge_image=DestroyImage(dodge_image);
4583 return((Image *) NULL);
4585 (void) CompositeImage(sketch_image,dodge_image,ColorDodgeCompositeOp,
4586 MagickTrue,0,0,exception);
4587 dodge_image=DestroyImage(dodge_image);
4588 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4589 if (blend_image == (Image *) NULL)
4591 sketch_image=DestroyImage(sketch_image);
4592 return((Image *) NULL);
4594 if (blend_image->alpha_trait != BlendPixelTrait)
4595 (void) SetImageAlpha(blend_image,TransparentAlpha,exception);
4596 (void) SetImageArtifact(blend_image,"compose:args","20x80");
4597 (void) CompositeImage(sketch_image,blend_image,BlendCompositeOp,MagickTrue,
4599 blend_image=DestroyImage(blend_image);
4600 return(sketch_image);
4604 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4608 % S o l a r i z e I m a g e %
4612 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4614 % SolarizeImage() applies a special effect to the image, similar to the effect
4615 % achieved in a photo darkroom by selectively exposing areas of photo
4616 % sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4617 % measure of the extent of the solarization.
4619 % The format of the SolarizeImage method is:
4621 % MagickBooleanType SolarizeImage(Image *image,const double threshold,
4622 % ExceptionInfo *exception)
4624 % A description of each parameter follows:
4626 % o image: the image.
4628 % o threshold: Define the extent of the solarization.
4630 % o exception: return any errors or warnings in this structure.
4633 MagickExport MagickBooleanType SolarizeImage(Image *image,
4634 const double threshold,ExceptionInfo *exception)
4636 #define SolarizeImageTag "Solarize/Image"
4650 assert(image != (Image *) NULL);
4651 assert(image->signature == MagickCoreSignature);
4652 if (image->debug != MagickFalse)
4653 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4654 if (IsGrayColorspace(image->colorspace) != MagickFalse)
4655 (void) SetImageColorspace(image,sRGBColorspace,exception);
4656 if (image->storage_class == PseudoClass)
4664 for (i=0; i < (ssize_t) image->colors; i++)
4666 if ((double) image->colormap[i].red > threshold)
4667 image->colormap[i].red=QuantumRange-image->colormap[i].red;
4668 if ((double) image->colormap[i].green > threshold)
4669 image->colormap[i].green=QuantumRange-image->colormap[i].green;
4670 if ((double) image->colormap[i].blue > threshold)
4671 image->colormap[i].blue=QuantumRange-image->colormap[i].blue;
4679 image_view=AcquireAuthenticCacheView(image,exception);
4680 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4681 #pragma omp parallel for schedule(static,4) shared(progress,status) \
4682 magick_threads(image,image,image->rows,1)
4684 for (y=0; y < (ssize_t) image->rows; y++)
4692 if (status == MagickFalse)
4694 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
4695 if (q == (Quantum *) NULL)
4700 for (x=0; x < (ssize_t) image->columns; x++)
4705 if (GetPixelWriteMask(image,q) == 0)
4707 q+=GetPixelChannels(image);
4710 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4712 PixelChannel channel=GetPixelChannelChannel(image,i);
4713 PixelTrait traits=GetPixelChannelTraits(image,channel);
4714 if ((traits & UpdatePixelTrait) == 0)
4716 if ((double) q[i] > threshold)
4717 q[i]=QuantumRange-q[i];
4719 q+=GetPixelChannels(image);
4721 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4723 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4728 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4729 #pragma omp critical (MagickCore_SolarizeImage)
4731 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4732 if (proceed == MagickFalse)
4736 image_view=DestroyCacheView(image_view);
4741 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4745 % S t e g a n o I m a g e %
4749 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4751 % SteganoImage() hides a digital watermark within the image. Recover
4752 % the hidden watermark later to prove that the authenticity of an image.
4753 % Offset defines the start position within the image to hide the watermark.
4755 % The format of the SteganoImage method is:
4757 % Image *SteganoImage(const Image *image,Image *watermark,
4758 % ExceptionInfo *exception)
4760 % A description of each parameter follows:
4762 % o image: the image.
4764 % o watermark: the watermark image.
4766 % o exception: return any errors or warnings in this structure.
4769 MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4770 ExceptionInfo *exception)
4772 #define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
4773 #define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
4774 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
4775 #define SteganoImageTag "Stegano/Image"
4810 Initialize steganographic image attributes.
4812 assert(image != (const Image *) NULL);
4813 assert(image->signature == MagickCoreSignature);
4814 if (image->debug != MagickFalse)
4815 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4816 assert(watermark != (const Image *) NULL);
4817 assert(watermark->signature == MagickCoreSignature);
4818 assert(exception != (ExceptionInfo *) NULL);
4819 assert(exception->signature == MagickCoreSignature);
4821 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4822 if (stegano_image == (Image *) NULL)
4823 return((Image *) NULL);
4824 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
4825 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
4827 stegano_image=DestroyImage(stegano_image);
4828 return((Image *) NULL);
4831 Hide watermark in low-order bits of image.
4836 depth=stegano_image->depth;
4837 k=stegano_image->offset;
4839 watermark_view=AcquireVirtualCacheView(watermark,exception);
4840 stegano_view=AcquireAuthenticCacheView(stegano_image,exception);
4841 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
4843 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
4845 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
4850 (void) GetOneCacheViewVirtualPixelInfo(watermark_view,x,y,&pixel,
4852 offset=k/(ssize_t) stegano_image->columns;
4853 if (offset >= (ssize_t) stegano_image->rows)
4855 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4856 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4858 if (q == (Quantum *) NULL)
4864 SetPixelRed(stegano_image,SetBit(GetPixelRed(stegano_image,q),j,
4865 GetBit(GetPixelInfoIntensity(stegano_image,&pixel),i)),q);
4870 SetPixelGreen(stegano_image,SetBit(GetPixelGreen(stegano_image,q),j,
4871 GetBit(GetPixelInfoIntensity(stegano_image,&pixel),i)),q);
4876 SetPixelBlue(stegano_image,SetBit(GetPixelBlue(stegano_image,q),j,
4877 GetBit(GetPixelInfoIntensity(stegano_image,&pixel),i)),q);
4881 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
4887 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
4889 if (k == stegano_image->offset)
4893 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4898 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4900 if (proceed == MagickFalse)
4904 stegano_view=DestroyCacheView(stegano_view);
4905 watermark_view=DestroyCacheView(watermark_view);
4906 if (status == MagickFalse)
4907 stegano_image=DestroyImage(stegano_image);
4908 return(stegano_image);
4912 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4916 % S t e r e o A n a g l y p h I m a g e %
4920 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4922 % StereoAnaglyphImage() combines two images and produces a single image that
4923 % is the composite of a left and right image of a stereo pair. Special
4924 % red-green stereo glasses are required to view this effect.
4926 % The format of the StereoAnaglyphImage method is:
4928 % Image *StereoImage(const Image *left_image,const Image *right_image,
4929 % ExceptionInfo *exception)
4930 % Image *StereoAnaglyphImage(const Image *left_image,
4931 % const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
4932 % ExceptionInfo *exception)
4934 % A description of each parameter follows:
4936 % o left_image: the left image.
4938 % o right_image: the right image.
4940 % o exception: return any errors or warnings in this structure.
4942 % o x_offset: amount, in pixels, by which the left image is offset to the
4943 % right of the right image.
4945 % o y_offset: amount, in pixels, by which the left image is offset to the
4946 % bottom of the right image.
4950 MagickExport Image *StereoImage(const Image *left_image,
4951 const Image *right_image,ExceptionInfo *exception)
4953 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4956 MagickExport Image *StereoAnaglyphImage(const Image *left_image,
4957 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
4958 ExceptionInfo *exception)
4960 #define StereoImageTag "Stereo/Image"
4974 assert(left_image != (const Image *) NULL);
4975 assert(left_image->signature == MagickCoreSignature);
4976 if (left_image->debug != MagickFalse)
4977 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4978 left_image->filename);
4979 assert(right_image != (const Image *) NULL);
4980 assert(right_image->signature == MagickCoreSignature);
4981 assert(exception != (ExceptionInfo *) NULL);
4982 assert(exception->signature == MagickCoreSignature);
4983 assert(right_image != (const Image *) NULL);
4985 if ((left_image->columns != right_image->columns) ||
4986 (left_image->rows != right_image->rows))
4987 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4989 Initialize stereo image attributes.
4991 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4992 MagickTrue,exception);
4993 if (stereo_image == (Image *) NULL)
4994 return((Image *) NULL);
4995 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
4997 stereo_image=DestroyImage(stereo_image);
4998 return((Image *) NULL);
5000 (void) SetImageColorspace(stereo_image,sRGBColorspace,exception);
5002 Copy left image to red channel and right image to blue channel.
5005 for (y=0; y < (ssize_t) stereo_image->rows; y++)
5007 register const Quantum
5017 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
5019 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
5020 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
5021 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
5022 (r == (Quantum *) NULL))
5024 for (x=0; x < (ssize_t) stereo_image->columns; x++)
5026 SetPixelRed(image,GetPixelRed(left_image,p),r);
5027 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
5028 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
5029 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
5030 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
5031 GetPixelAlpha(right_image,q))/2,r);
5032 p+=GetPixelChannels(left_image);
5033 q+=GetPixelChannels(right_image);
5034 r+=GetPixelChannels(stereo_image);
5036 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
5038 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5043 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
5044 stereo_image->rows);
5045 if (proceed == MagickFalse)
5049 if (status == MagickFalse)
5050 stereo_image=DestroyImage(stereo_image);
5051 return(stereo_image);
5055 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5059 % S w i r l I m a g e %
5063 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5065 % SwirlImage() swirls the pixels about the center of the image, where
5066 % degrees indicates the sweep of the arc through which each pixel is moved.
5067 % You get a more dramatic effect as the degrees move from 1 to 360.
5069 % The format of the SwirlImage method is:
5071 % Image *SwirlImage(const Image *image,double degrees,
5072 % const PixelInterpolateMethod method,ExceptionInfo *exception)
5074 % A description of each parameter follows:
5076 % o image: the image.
5078 % o degrees: Define the tightness of the swirling effect.
5080 % o method: the pixel interpolation method.
5082 % o exception: return any errors or warnings in this structure.
5085 MagickExport Image *SwirlImage(const Image *image,double degrees,
5086 const PixelInterpolateMethod method,ExceptionInfo *exception)
5088 #define SwirlImageTag "Swirl/Image"
5116 Initialize swirl image attributes.
5118 assert(image != (const Image *) NULL);
5119 assert(image->signature == MagickCoreSignature);
5120 if (image->debug != MagickFalse)
5121 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5122 assert(exception != (ExceptionInfo *) NULL);
5123 assert(exception->signature == MagickCoreSignature);
5124 canvas=CloneImage(image,0,0,MagickTrue,exception);
5125 if (canvas == (Image *) NULL)
5126 return((Image *) NULL);
5127 if ((canvas->alpha_trait == UndefinedPixelTrait) &&
5128 (canvas->background_color.alpha != OpaqueAlpha))
5129 (void) SetImageAlphaChannel(canvas,OpaqueAlphaChannel,exception);
5130 swirl_image=CloneImage(canvas,canvas->columns,canvas->rows,MagickTrue,
5132 if (swirl_image == (Image *) NULL)
5134 canvas=DestroyImage(canvas);
5135 return((Image *) NULL);
5137 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
5139 canvas=DestroyImage(canvas);
5140 swirl_image=DestroyImage(swirl_image);
5141 return((Image *) NULL);
5144 Compute scaling factor.
5146 center.x=(double) canvas->columns/2.0;
5147 center.y=(double) canvas->rows/2.0;
5148 radius=MagickMax(center.x,center.y);
5151 if (canvas->columns > canvas->rows)
5152 scale.y=(double) canvas->columns/(double) canvas->rows;
5154 if (canvas->columns < canvas->rows)
5155 scale.x=(double) canvas->rows/(double) canvas->columns;
5156 degrees=(double) DegreesToRadians(degrees);
5162 canvas_view=AcquireVirtualCacheView(canvas,exception);
5163 interpolate_view=AcquireVirtualCacheView(image,exception);
5164 swirl_view=AcquireAuthenticCacheView(swirl_image,exception);
5165 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5166 #pragma omp parallel for schedule(static,4) shared(progress,status) \
5167 magick_threads(canvas,swirl_image,canvas->rows,1)
5169 for (y=0; y < (ssize_t) canvas->rows; y++)
5177 register const Quantum
5186 if (status == MagickFalse)
5188 p=GetCacheViewVirtualPixels(canvas_view,0,y,canvas->columns,1,exception);
5189 q=QueueCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
5191 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
5196 delta.y=scale.y*(double) (y-center.y);
5197 for (x=0; x < (ssize_t) canvas->columns; x++)
5200 Determine if the pixel is within an ellipse.
5202 if (GetPixelWriteMask(canvas,p) == 0)
5204 SetPixelBackgoundColor(swirl_image,q);
5205 p+=GetPixelChannels(canvas);
5206 q+=GetPixelChannels(swirl_image);
5209 delta.x=scale.x*(double) (x-center.x);
5210 distance=delta.x*delta.x+delta.y*delta.y;
5211 if (distance >= (radius*radius))
5216 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5218 PixelChannel channel = GetPixelChannelChannel(canvas,i);
5219 PixelTrait traits = GetPixelChannelTraits(canvas,channel);
5220 PixelTrait swirl_traits = GetPixelChannelTraits(swirl_image,
5222 if ((traits == UndefinedPixelTrait) ||
5223 (swirl_traits == UndefinedPixelTrait))
5225 SetPixelChannel(swirl_image,channel,p[i],q);
5238 factor=1.0-sqrt((double) distance)/radius;
5239 sine=sin((double) (degrees*factor*factor));
5240 cosine=cos((double) (degrees*factor*factor));
5241 status=InterpolatePixelChannels(canvas,interpolate_view,swirl_image,
5242 method,((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5243 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
5245 p+=GetPixelChannels(canvas);
5246 q+=GetPixelChannels(swirl_image);
5248 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5250 if (canvas->progress_monitor != (MagickProgressMonitor) NULL)
5255 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5256 #pragma omp critical (MagickCore_SwirlImage)
5258 proceed=SetImageProgress(canvas,SwirlImageTag,progress++,canvas->rows);
5259 if (proceed == MagickFalse)
5263 swirl_view=DestroyCacheView(swirl_view);
5264 interpolate_view=DestroyCacheView(interpolate_view);
5265 canvas_view=DestroyCacheView(canvas_view);
5266 canvas=DestroyImage(canvas);
5267 if (status == MagickFalse)
5268 swirl_image=DestroyImage(swirl_image);
5269 return(swirl_image);
5273 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5277 % T i n t I m a g e %
5281 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5283 % TintImage() applies a color vector to each pixel in the image. The length
5284 % of the vector is 0 for black and white and at its maximum for the midtones.
5285 % The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5287 % The format of the TintImage method is:
5289 % Image *TintImage(const Image *image,const char *blend,
5290 % const PixelInfo *tint,ExceptionInfo *exception)
5292 % A description of each parameter follows:
5294 % o image: the image.
5296 % o blend: A color value used for tinting.
5298 % o tint: A color value used for tinting.
5300 % o exception: return any errors or warnings in this structure.
5303 MagickExport Image *TintImage(const Image *image,const char *blend,
5304 const PixelInfo *tint,ExceptionInfo *exception)
5306 #define TintImageTag "Tint/Image"
5337 Allocate tint image.
5339 assert(image != (const Image *) NULL);
5340 assert(image->signature == MagickCoreSignature);
5341 if (image->debug != MagickFalse)
5342 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5343 assert(exception != (ExceptionInfo *) NULL);
5344 assert(exception->signature == MagickCoreSignature);
5345 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5346 if (tint_image == (Image *) NULL)
5347 return((Image *) NULL);
5348 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
5350 tint_image=DestroyImage(tint_image);
5351 return((Image *) NULL);
5353 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
5354 (IsPixelInfoGray(tint) == MagickFalse))
5355 (void) SetImageColorspace(tint_image,sRGBColorspace,exception);
5356 if (blend == (const char *) NULL)
5359 Determine RGB values of the color.
5361 GetPixelInfo(image,&color_vector);
5362 flags=ParseGeometry(blend,&geometry_info);
5363 color_vector.red=geometry_info.rho;
5364 color_vector.green=geometry_info.rho;
5365 color_vector.blue=geometry_info.rho;
5366 color_vector.alpha=(MagickRealType) OpaqueAlpha;
5367 if ((flags & SigmaValue) != 0)
5368 color_vector.green=geometry_info.sigma;
5369 if ((flags & XiValue) != 0)
5370 color_vector.blue=geometry_info.xi;
5371 if ((flags & PsiValue) != 0)
5372 color_vector.alpha=geometry_info.psi;
5373 if (image->colorspace == CMYKColorspace)
5375 color_vector.black=geometry_info.rho;
5376 if ((flags & PsiValue) != 0)
5377 color_vector.black=geometry_info.psi;
5378 if ((flags & ChiValue) != 0)
5379 color_vector.alpha=geometry_info.chi;
5381 intensity=(double) GetPixelInfoIntensity((const Image *) NULL,tint);
5382 color_vector.red=(double) (color_vector.red*tint->red/100.0-intensity);
5383 color_vector.green=(double) (color_vector.green*tint->green/100.0-intensity);
5384 color_vector.blue=(double) (color_vector.blue*tint->blue/100.0-intensity);
5385 color_vector.black=(double) (color_vector.black*tint->black/100.0-intensity);
5386 color_vector.alpha=(double) (color_vector.alpha*tint->alpha/100.0-intensity);
5392 image_view=AcquireVirtualCacheView(image,exception);
5393 tint_view=AcquireAuthenticCacheView(tint_image,exception);
5394 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5395 #pragma omp parallel for schedule(static,4) shared(progress,status) \
5396 magick_threads(image,tint_image,image->rows,1)
5398 for (y=0; y < (ssize_t) image->rows; y++)
5400 register const Quantum
5409 if (status == MagickFalse)
5411 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5412 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5414 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
5419 for (x=0; x < (ssize_t) image->columns; x++)
5430 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5432 PixelChannel channel=GetPixelChannelChannel(image,i);
5433 PixelTrait traits=GetPixelChannelTraits(image,channel);
5434 PixelTrait tint_traits=GetPixelChannelTraits(tint_image,channel);
5435 if ((traits == UndefinedPixelTrait) ||
5436 (tint_traits == UndefinedPixelTrait))
5438 if (((tint_traits & CopyPixelTrait) != 0) ||
5439 (GetPixelWriteMask(image,p) == 0))
5441 SetPixelChannel(tint_image,channel,p[i],q);
5445 GetPixelInfo(image,&pixel);
5446 weight=QuantumScale*GetPixelRed(image,p)-0.5;
5447 pixel.red=(double) GetPixelRed(image,p)+color_vector.red*(1.0-(4.0*
5449 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
5450 pixel.green=(double) GetPixelGreen(image,p)+color_vector.green*(1.0-(4.0*
5452 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
5453 pixel.blue=(double) GetPixelBlue(image,p)+color_vector.blue*(1.0-(4.0*
5455 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
5456 pixel.black=(double) GetPixelBlack(image,p)+color_vector.black*(1.0-(4.0*
5458 SetPixelViaPixelInfo(tint_image,&pixel,q);
5459 p+=GetPixelChannels(image);
5460 q+=GetPixelChannels(tint_image);
5462 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5464 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5469 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5470 #pragma omp critical (MagickCore_TintImage)
5472 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5473 if (proceed == MagickFalse)
5477 tint_view=DestroyCacheView(tint_view);
5478 image_view=DestroyCacheView(image_view);
5479 if (status == MagickFalse)
5480 tint_image=DestroyImage(tint_image);
5485 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5489 % V i g n e t t e I m a g e %
5493 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5495 % VignetteImage() softens the edges of the image in vignette style.
5497 % The format of the VignetteImage method is:
5499 % Image *VignetteImage(const Image *image,const double radius,
5500 % const double sigma,const ssize_t x,const ssize_t y,
5501 % ExceptionInfo *exception)
5503 % A description of each parameter follows:
5505 % o image: the image.
5507 % o radius: the radius of the pixel neighborhood.
5509 % o sigma: the standard deviation of the Gaussian, in pixels.
5511 % o x, y: Define the x and y ellipse offset.
5513 % o exception: return any errors or warnings in this structure.
5516 MagickExport Image *VignetteImage(const Image *image,const double radius,
5517 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
5520 ellipse[MagickPathExtent];
5531 assert(image != (Image *) NULL);
5532 assert(image->signature == MagickCoreSignature);
5533 if (image->debug != MagickFalse)
5534 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5535 assert(exception != (ExceptionInfo *) NULL);
5536 assert(exception->signature == MagickCoreSignature);
5537 canvas=CloneImage(image,0,0,MagickTrue,exception);
5538 if (canvas == (Image *) NULL)
5539 return((Image *) NULL);
5540 if ((canvas->alpha_trait == UndefinedPixelTrait) &&
5541 (canvas->background_color.alpha != OpaqueAlpha))
5542 (void) SetImageAlphaChannel(canvas,OpaqueAlphaChannel,exception);
5543 if (SetImageStorageClass(canvas,DirectClass,exception) == MagickFalse)
5545 canvas=DestroyImage(canvas);
5546 return((Image *) NULL);
5548 oval_image=CloneImage(canvas,canvas->columns,canvas->rows,MagickTrue,
5550 if (oval_image == (Image *) NULL)
5552 canvas=DestroyImage(canvas);
5553 return((Image *) NULL);
5555 (void) QueryColorCompliance("#000000",AllCompliance,
5556 &oval_image->background_color,exception);
5557 (void) SetImageBackgroundColor(oval_image,exception);
5558 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
5559 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5561 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5563 (void) FormatLocaleString(ellipse,MagickPathExtent,"ellipse %g,%g,%g,%g,"
5564 "0.0,360.0",image->columns/2.0,image->rows/2.0,image->columns/2.0-x,
5566 draw_info->primitive=AcquireString(ellipse);
5567 (void) DrawImage(oval_image,draw_info,exception);
5568 draw_info=DestroyDrawInfo(draw_info);
5569 blur_image=BlurImage(oval_image,radius,sigma,exception);
5570 oval_image=DestroyImage(oval_image);
5571 if (blur_image == (Image *) NULL)
5573 canvas=DestroyImage(canvas);
5574 return((Image *) NULL);
5576 blur_image->alpha_trait=UndefinedPixelTrait;
5577 (void) CompositeImage(canvas,blur_image,IntensityCompositeOp,MagickTrue,
5579 blur_image=DestroyImage(blur_image);
5580 vignette_image=MergeImageLayers(canvas,FlattenLayer,exception);
5581 canvas=DestroyImage(canvas);
5582 if (vignette_image != (Image *) NULL)
5583 (void) TransformImageColorspace(vignette_image,image->colorspace,exception);
5584 return(vignette_image);
5588 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5592 % W a v e I m a g e %
5596 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5598 % WaveImage() creates a "ripple" effect in the image by shifting the pixels
5599 % vertically along a sine wave whose amplitude and wavelength is specified
5600 % by the given parameters.
5602 % The format of the WaveImage method is:
5604 % Image *WaveImage(const Image *image,const double amplitude,
5605 % const double wave_length,const PixelInterpolateMethod method,
5606 % ExceptionInfo *exception)
5608 % A description of each parameter follows:
5610 % o image: the image.
5612 % o amplitude, wave_length: Define the amplitude and wave length of the
5615 % o interpolate: the pixel interpolation method.
5617 % o exception: return any errors or warnings in this structure.
5620 MagickExport Image *WaveImage(const Image *image,const double amplitude,
5621 const double wave_length,const PixelInterpolateMethod method,
5622 ExceptionInfo *exception)
5624 #define WaveImageTag "Wave/Image"
5650 Initialize wave image attributes.
5652 assert(image != (Image *) NULL);
5653 assert(image->signature == MagickCoreSignature);
5654 if (image->debug != MagickFalse)
5655 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5656 assert(exception != (ExceptionInfo *) NULL);
5657 assert(exception->signature == MagickCoreSignature);
5658 canvas=CloneImage(image,0,0,MagickTrue,exception);
5659 if (canvas == (Image *) NULL)
5660 return((Image *) NULL);
5661 if ((canvas->alpha_trait == UndefinedPixelTrait) &&
5662 (canvas->background_color.alpha != OpaqueAlpha))
5663 (void) SetImageAlpha(canvas,OpaqueAlpha,exception);
5664 wave_image=CloneImage(canvas,canvas->columns,(size_t) (canvas->rows+2.0*
5665 fabs(amplitude)),MagickTrue,exception);
5666 if (wave_image == (Image *) NULL)
5668 canvas=DestroyImage(canvas);
5669 return((Image *) NULL);
5671 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
5673 canvas=DestroyImage(canvas);
5674 wave_image=DestroyImage(wave_image);
5675 return((Image *) NULL);
5680 sine_map=(double *) AcquireQuantumMemory((size_t) wave_image->columns,
5682 if (sine_map == (double *) NULL)
5684 canvas=DestroyImage(canvas);
5685 wave_image=DestroyImage(wave_image);
5686 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5688 for (i=0; i < (ssize_t) wave_image->columns; i++)
5689 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5696 canvas_view=AcquireVirtualCacheView(canvas,exception);
5697 wave_view=AcquireAuthenticCacheView(wave_image,exception);
5698 (void) SetCacheViewVirtualPixelMethod(canvas_view,
5699 BackgroundVirtualPixelMethod);
5700 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5701 #pragma omp parallel for schedule(static,4) shared(progress,status) \
5702 magick_threads(canvas,wave_image,wave_image->rows,1)
5704 for (y=0; y < (ssize_t) wave_image->rows; y++)
5712 if (status == MagickFalse)
5714 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5716 if (q == (Quantum *) NULL)
5721 for (x=0; x < (ssize_t) wave_image->columns; x++)
5723 status=InterpolatePixelChannels(canvas,canvas_view,wave_image,method,
5724 (double) x,(double) (y-sine_map[x]),q,exception);
5725 q+=GetPixelChannels(wave_image);
5727 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5729 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5734 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5735 #pragma omp critical (MagickCore_WaveImage)
5737 proceed=SetImageProgress(canvas,WaveImageTag,progress++,canvas->rows);
5738 if (proceed == MagickFalse)
5742 wave_view=DestroyCacheView(wave_view);
5743 canvas_view=DestroyCacheView(canvas_view);
5744 canvas=DestroyImage(canvas);
5745 sine_map=(double *) RelinquishMagickMemory(sine_map);
5746 if (status == MagickFalse)
5747 wave_image=DestroyImage(wave_image);
5752 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5756 % W a v e l e t D e n o i s e I m a g e %
5760 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5762 % WaveletDenoiseImage() removes noise from the image using a wavelet
5763 % transform. The wavelet transform is a fast hierarchical scheme for
5764 % processing an image using a set of consecutive lowpass and high_pass filters,
5765 % followed by a decimation. This results in a decomposition into different
5766 % scales which can be regarded as different “frequency bands”, determined by
5767 % the mother wavelet. Adapted from dcraw.c by David Coffin.
5769 % The format of the WaveletDenoiseImage method is:
5771 % Image *WaveletDenoiseImage(const Image *image,const double threshold,
5772 % const double softness,ExceptionInfo *exception)
5774 % A description of each parameter follows:
5776 % o image: the image.
5778 % o threshold: set the threshold for smoothing.
5780 % o softness: attenuate the smoothing threshold.
5782 % o exception: return any errors or warnings in this structure.
5786 static inline void HatTransform(const float *magick_restrict pixels,
5787 const size_t stride,const size_t extent,const size_t scale,float *kernel)
5798 q=pixels+scale*stride;
5799 r=pixels+scale*stride;
5800 for (i=0; i < (ssize_t) scale; i++)
5802 kernel[i]=0.25f*(*p+(*p)+(*q)+(*r));
5807 for ( ; i < (ssize_t) (extent-scale); i++)
5809 kernel[i]=0.25f*(2.0f*(*p)+*(p-scale*stride)+*(p+scale*stride));
5813 r=pixels+stride*(extent-2);
5814 for ( ; i < (ssize_t) extent; i++)
5816 kernel[i]=0.25f*(*p+(*p)+(*q)+(*r));
5823 MagickExport Image *WaveletDenoiseImage(const Image *image,
5824 const double threshold,const double softness,ExceptionInfo *exception)
5850 noise_levels[] = { 0.8002f, 0.2735f, 0.1202f, 0.0585f, 0.0291f, 0.0152f,
5854 Initialize noise image attributes.
5856 assert(image != (const Image *) NULL);
5857 assert(image->signature == MagickCoreSignature);
5858 if (image->debug != MagickFalse)
5859 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5860 assert(exception != (ExceptionInfo *) NULL);
5861 assert(exception->signature == MagickCoreSignature);
5862 #if defined(MAGICKCORE_OPENCL_SUPPORT)
5863 noise_image=AccelerateWaveletDenoiseImage(image,threshold,exception);
5864 if (noise_image != (Image *) NULL)
5865 return(noise_image);
5867 noise_image=CloneImage(image,0,0,MagickTrue,exception);
5868 if (noise_image == (Image *) NULL)
5869 return((Image *) NULL);
5870 if (SetImageStorageClass(noise_image,DirectClass,exception) == MagickFalse)
5872 noise_image=DestroyImage(noise_image);
5873 return((Image *) NULL);
5875 if (AcquireMagickResource(WidthResource,4*image->columns) == MagickFalse)
5876 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5877 pixels_info=AcquireVirtualMemory(3*image->columns,image->rows*
5879 kernel=(float *) AcquireQuantumMemory(MagickMax(image->rows,image->columns),
5880 GetOpenMPMaximumThreads()*sizeof(*kernel));
5881 if ((pixels_info == (MemoryInfo *) NULL) || (kernel == (float *) NULL))
5883 if (kernel != (float *) NULL)
5884 kernel=(float *) RelinquishMagickMemory(kernel);
5885 if (pixels_info != (MemoryInfo *) NULL)
5886 pixels_info=RelinquishVirtualMemory(pixels_info);
5887 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5889 pixels=(float *) GetVirtualMemoryBlob(pixels_info);
5891 number_pixels=(MagickSizeType) image->columns*image->rows;
5892 image_view=AcquireAuthenticCacheView(image,exception);
5893 noise_view=AcquireAuthenticCacheView(noise_image,exception);
5894 for (channel=0; channel < (ssize_t) GetPixelChannels(image); channel++)
5913 if (status == MagickFalse)
5915 traits=GetPixelChannelTraits(image,(PixelChannel) channel);
5916 if (traits == UndefinedPixelTrait)
5918 pixel_channel=GetPixelChannelChannel(image,channel);
5919 if ((pixel_channel != RedPixelChannel) &&
5920 (pixel_channel != GreenPixelChannel) &&
5921 (pixel_channel != BluePixelChannel))
5924 Copy channel from image to wavelet pixel array.
5927 for (y=0; y < (ssize_t) image->rows; y++)
5929 register const Quantum
5935 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
5936 if (p == (const Quantum *) NULL)
5941 for (x=0; x < (ssize_t) image->columns; x++)
5943 pixels[i++]=(float) p[channel];
5944 p+=GetPixelChannels(image);
5948 Low pass filter outputs are called approximation kernel & high pass
5949 filters are referred to as detail kernel. The detail kernel
5950 have high values in the noisy parts of the signal.
5953 for (level=0; level < 5; level++)
5962 low_pass=(size_t) (number_pixels*((level & 0x01)+1));
5963 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5964 #pragma omp parallel for schedule(static,1) \
5965 magick_threads(image,image,image->rows,1)
5967 for (y=0; y < (ssize_t) image->rows; y++)
5970 id = GetOpenMPThreadId();
5979 p=kernel+id*image->columns;
5980 q=pixels+y*image->columns;
5981 HatTransform(q+high_pass,1,image->columns,(size_t) (1 << level),p);
5983 for (x=0; x < (ssize_t) image->columns; x++)
5986 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5987 #pragma omp parallel for schedule(static,1) \
5988 magick_threads(image,image,image->columns,1)
5990 for (x=0; x < (ssize_t) image->columns; x++)
5993 id = GetOpenMPThreadId();
6002 p=kernel+id*image->rows;
6003 q=pixels+x+low_pass;
6004 HatTransform(q,image->columns,image->rows,(size_t) (1 << level),p);
6005 for (y=0; y < (ssize_t) image->rows; y++)
6012 To threshold, each coefficient is compared to a threshold value and
6013 attenuated / shrunk by some factor.
6015 magnitude=threshold*noise_levels[level];
6016 for (i=0; i < (ssize_t) number_pixels; ++i)
6018 pixels[high_pass+i]-=pixels[low_pass+i];
6019 if (pixels[high_pass+i] < -magnitude)
6020 pixels[high_pass+i]+=magnitude-softness*magnitude;
6022 if (pixels[high_pass+i] > magnitude)
6023 pixels[high_pass+i]-=magnitude-softness*magnitude;
6025 pixels[high_pass+i]*=softness;
6027 pixels[i]+=pixels[high_pass+i];
6032 Reconstruct image from the thresholded wavelet kernel.
6035 for (y=0; y < (ssize_t) image->rows; y++)
6049 q=GetCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
6051 if (q == (Quantum *) NULL)
6056 offset=GetPixelChannelOffset(noise_image,pixel_channel);
6057 for (x=0; x < (ssize_t) image->columns; x++)
6062 pixel=(MagickRealType) pixels[i]+pixels[low_pass+i];
6063 q[offset]=ClampToQuantum(pixel);
6065 q+=GetPixelChannels(noise_image);
6067 sync=SyncCacheViewAuthenticPixels(noise_view,exception);
6068 if (sync == MagickFalse)
6071 if (image->progress_monitor != (MagickProgressMonitor) NULL)
6076 proceed=SetImageProgress(image,AddNoiseImageTag,(MagickOffsetType)
6077 channel,GetPixelChannels(image));
6078 if (proceed == MagickFalse)
6082 noise_view=DestroyCacheView(noise_view);
6083 image_view=DestroyCacheView(image_view);
6084 kernel=(float *) RelinquishMagickMemory(kernel);
6085 pixels_info=RelinquishVirtualMemory(pixels_info);
6086 if (status == MagickFalse)
6087 noise_image=DestroyImage(noise_image);
6088 return(noise_image);