2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % MagickCore Image Special Effects Methods %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/annotate.h"
45 #include "MagickCore/artifact.h"
46 #include "MagickCore/attribute.h"
47 #include "MagickCore/cache.h"
48 #include "MagickCore/cache-view.h"
49 #include "MagickCore/color.h"
50 #include "MagickCore/color-private.h"
51 #include "MagickCore/colorspace-private.h"
52 #include "MagickCore/composite.h"
53 #include "MagickCore/decorate.h"
54 #include "MagickCore/distort.h"
55 #include "MagickCore/draw.h"
56 #include "MagickCore/effect.h"
57 #include "MagickCore/enhance.h"
58 #include "MagickCore/exception.h"
59 #include "MagickCore/exception-private.h"
60 #include "MagickCore/fx.h"
61 #include "MagickCore/fx-private.h"
62 #include "MagickCore/gem.h"
63 #include "MagickCore/gem-private.h"
64 #include "MagickCore/geometry.h"
65 #include "MagickCore/layer.h"
66 #include "MagickCore/list.h"
67 #include "MagickCore/log.h"
68 #include "MagickCore/image.h"
69 #include "MagickCore/image-private.h"
70 #include "MagickCore/magick.h"
71 #include "MagickCore/memory_.h"
72 #include "MagickCore/monitor.h"
73 #include "MagickCore/monitor-private.h"
74 #include "MagickCore/option.h"
75 #include "MagickCore/pixel.h"
76 #include "MagickCore/pixel-accessor.h"
77 #include "MagickCore/property.h"
78 #include "MagickCore/quantum.h"
79 #include "MagickCore/quantum-private.h"
80 #include "MagickCore/random_.h"
81 #include "MagickCore/random-private.h"
82 #include "MagickCore/resample.h"
83 #include "MagickCore/resample-private.h"
84 #include "MagickCore/resize.h"
85 #include "MagickCore/splay-tree.h"
86 #include "MagickCore/statistic.h"
87 #include "MagickCore/string_.h"
88 #include "MagickCore/string-private.h"
89 #include "MagickCore/thread-private.h"
90 #include "MagickCore/transform.h"
91 #include "MagickCore/utility.h"
96 #define LeftShiftOperator 0xf5
97 #define RightShiftOperator 0xf6
98 #define LessThanEqualOperator 0xf7
99 #define GreaterThanEqualOperator 0xf8
100 #define EqualOperator 0xf9
101 #define NotEqualOperator 0xfa
102 #define LogicalAndOperator 0xfb
103 #define LogicalOrOperator 0xfc
104 #define ExponentialNotation 0xfd
132 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
136 + A c q u i r e F x I n f o %
140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142 % AcquireFxInfo() allocates the FxInfo structure.
144 % The format of the AcquireFxInfo method is:
146 % FxInfo *AcquireFxInfo(Image *image,const char *expression,
147 % ExceptionInfo *exception)
149 % A description of each parameter follows:
151 % o image: the image.
153 % o expression: the expression.
155 % o exception: return any errors or warnings in this structure.
158 MagickPrivate FxInfo *AcquireFxInfo(const Image *image,const char *expression,
159 ExceptionInfo *exception)
173 fx_info=(FxInfo *) AcquireMagickMemory(sizeof(*fx_info));
174 if (fx_info == (FxInfo *) NULL)
175 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
176 (void) ResetMagickMemory(fx_info,0,sizeof(*fx_info));
177 fx_info->exception=AcquireExceptionInfo();
178 fx_info->images=image;
179 fx_info->colors=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
180 RelinquishMagickMemory);
181 fx_info->symbols=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
182 RelinquishMagickMemory);
183 fx_info->view=(CacheView **) AcquireQuantumMemory(GetImageListLength(
184 fx_info->images),sizeof(*fx_info->view));
185 if (fx_info->view == (CacheView **) NULL)
186 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
188 next=GetFirstImageInList(fx_info->images);
189 for ( ; next != (Image *) NULL; next=next->next)
191 fx_info->view[i]=AcquireVirtualCacheView(next,exception);
194 fx_info->random_info=AcquireRandomInfo();
195 fx_info->expression=ConstantString(expression);
196 fx_info->file=stderr;
197 (void) SubstituteString(&fx_info->expression," ",""); /* compact string */
199 Force right-to-left associativity for unary negation.
201 (void) SubstituteString(&fx_info->expression,"-","-1.0*");
203 Convert complex to simple operators.
206 *fx_op=(char) LeftShiftOperator;
207 (void) SubstituteString(&fx_info->expression,"<<",fx_op);
208 *fx_op=(char) RightShiftOperator;
209 (void) SubstituteString(&fx_info->expression,">>",fx_op);
210 *fx_op=(char) LessThanEqualOperator;
211 (void) SubstituteString(&fx_info->expression,"<=",fx_op);
212 *fx_op=(char) GreaterThanEqualOperator;
213 (void) SubstituteString(&fx_info->expression,">=",fx_op);
214 *fx_op=(char) EqualOperator;
215 (void) SubstituteString(&fx_info->expression,"==",fx_op);
216 *fx_op=(char) NotEqualOperator;
217 (void) SubstituteString(&fx_info->expression,"!=",fx_op);
218 *fx_op=(char) LogicalAndOperator;
219 (void) SubstituteString(&fx_info->expression,"&&",fx_op);
220 *fx_op=(char) LogicalOrOperator;
221 (void) SubstituteString(&fx_info->expression,"||",fx_op);
222 *fx_op=(char) ExponentialNotation;
223 (void) SubstituteString(&fx_info->expression,"**",fx_op);
228 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
232 % A d d N o i s e I m a g e %
236 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
238 % AddNoiseImage() adds random noise to the image.
240 % The format of the AddNoiseImage method is:
242 % Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
243 % const double attenuate,ExceptionInfo *exception)
245 % A description of each parameter follows:
247 % o image: the image.
249 % o channel: the channel type.
251 % o noise_type: The type of noise: Uniform, Gaussian, Multiplicative,
252 % Impulse, Laplacian, or Poisson.
254 % o attenuate: attenuate the random distribution.
256 % o exception: return any errors or warnings in this structure.
259 MagickExport Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
260 const double attenuate,ExceptionInfo *exception)
262 #define AddNoiseImageTag "AddNoise/Image"
279 **restrict random_info;
285 Initialize noise image attributes.
287 assert(image != (const Image *) NULL);
288 assert(image->signature == MagickSignature);
289 if (image->debug != MagickFalse)
290 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
291 assert(exception != (ExceptionInfo *) NULL);
292 assert(exception->signature == MagickSignature);
293 noise_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
294 if (noise_image == (Image *) NULL)
295 return((Image *) NULL);
296 if (SetImageStorageClass(noise_image,DirectClass,exception) == MagickFalse)
298 noise_image=DestroyImage(noise_image);
299 return((Image *) NULL);
302 Add noise in each row.
306 random_info=AcquireRandomInfoThreadSet();
307 concurrent=GetRandomSecretKey(random_info[0]) == ~0UL ? MagickTrue :
309 image_view=AcquireVirtualCacheView(image,exception);
310 noise_view=AcquireAuthenticCacheView(noise_image,exception);
311 #if defined(MAGICKCORE_OPENMP_SUPPORT)
312 #pragma omp parallel for schedule(static,4) shared(progress,status) omp_concurrent(concurrent)
314 for (y=0; y < (ssize_t) image->rows; y++)
317 id = GetOpenMPThreadId();
322 register const Quantum
331 if (status == MagickFalse)
333 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
334 q=QueueCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
336 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
341 for (x=0; x < (ssize_t) image->columns; x++)
346 if (GetPixelMask(image,p) != 0)
348 p+=GetPixelChannels(image);
349 q+=GetPixelChannels(noise_image);
352 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
361 channel=GetPixelChannelMapChannel(image,i);
362 traits=GetPixelChannelMapTraits(image,channel);
363 noise_traits=GetPixelChannelMapTraits(noise_image,channel);
364 if ((traits == UndefinedPixelTrait) ||
365 (noise_traits == UndefinedPixelTrait))
367 if ((noise_traits & CopyPixelTrait) != 0)
369 SetPixelChannel(noise_image,channel,p[i],q);
372 SetPixelChannel(noise_image,channel,ClampToQuantum(
373 GenerateDifferentialNoise(random_info[id],p[i],noise_type,attenuate)),
376 p+=GetPixelChannels(image);
377 q+=GetPixelChannels(noise_image);
379 sync=SyncCacheViewAuthenticPixels(noise_view,exception);
380 if (sync == MagickFalse)
382 if (image->progress_monitor != (MagickProgressMonitor) NULL)
387 #if defined(MAGICKCORE_OPENMP_SUPPORT)
388 #pragma omp critical (MagickCore_AddNoiseImage)
390 proceed=SetImageProgress(image,AddNoiseImageTag,progress++,
392 if (proceed == MagickFalse)
396 noise_view=DestroyCacheView(noise_view);
397 image_view=DestroyCacheView(image_view);
398 random_info=DestroyRandomInfoThreadSet(random_info);
399 if (status == MagickFalse)
400 noise_image=DestroyImage(noise_image);
405 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
409 % B l u e S h i f t I m a g e %
413 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
415 % BlueShiftImage() mutes the colors of the image to simulate a scene at
416 % nighttime in the moonlight.
418 % The format of the BlueShiftImage method is:
420 % Image *BlueShiftImage(const Image *image,const double factor,
421 % ExceptionInfo *exception)
423 % A description of each parameter follows:
425 % o image: the image.
427 % o factor: the shift factor.
429 % o exception: return any errors or warnings in this structure.
432 MagickExport Image *BlueShiftImage(const Image *image,const double factor,
433 ExceptionInfo *exception)
435 #define BlueShiftImageTag "BlueShift/Image"
454 Allocate blue shift image.
456 assert(image != (const Image *) NULL);
457 assert(image->signature == MagickSignature);
458 if (image->debug != MagickFalse)
459 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
460 assert(exception != (ExceptionInfo *) NULL);
461 assert(exception->signature == MagickSignature);
462 shift_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
463 if (shift_image == (Image *) NULL)
464 return((Image *) NULL);
465 if (SetImageStorageClass(shift_image,DirectClass,exception) == MagickFalse)
467 shift_image=DestroyImage(shift_image);
468 return((Image *) NULL);
471 Blue-shift DirectClass image.
475 image_view=AcquireVirtualCacheView(image,exception);
476 shift_view=AcquireAuthenticCacheView(shift_image,exception);
477 #if defined(MAGICKCORE_OPENMP_SUPPORT)
478 #pragma omp parallel for schedule(static,4) shared(progress,status)
480 for (y=0; y < (ssize_t) image->rows; y++)
491 register const Quantum
500 if (status == MagickFalse)
502 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
503 q=QueueCacheViewAuthenticPixels(shift_view,0,y,shift_image->columns,1,
505 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
510 for (x=0; x < (ssize_t) image->columns; x++)
512 quantum=GetPixelRed(image,p);
513 if (GetPixelGreen(image,p) < quantum)
514 quantum=GetPixelGreen(image,p);
515 if (GetPixelBlue(image,p) < quantum)
516 quantum=GetPixelBlue(image,p);
517 pixel.red=0.5*(GetPixelRed(image,p)+factor*quantum);
518 pixel.green=0.5*(GetPixelGreen(image,p)+factor*quantum);
519 pixel.blue=0.5*(GetPixelBlue(image,p)+factor*quantum);
520 quantum=GetPixelRed(image,p);
521 if (GetPixelGreen(image,p) > quantum)
522 quantum=GetPixelGreen(image,p);
523 if (GetPixelBlue(image,p) > quantum)
524 quantum=GetPixelBlue(image,p);
525 pixel.red=0.5*(pixel.red+factor*quantum);
526 pixel.green=0.5*(pixel.green+factor*quantum);
527 pixel.blue=0.5*(pixel.blue+factor*quantum);
528 SetPixelRed(shift_image,ClampToQuantum(pixel.red),q);
529 SetPixelGreen(shift_image,ClampToQuantum(pixel.green),q);
530 SetPixelBlue(shift_image,ClampToQuantum(pixel.blue),q);
531 p+=GetPixelChannels(image);
532 q+=GetPixelChannels(shift_image);
534 sync=SyncCacheViewAuthenticPixels(shift_view,exception);
535 if (sync == MagickFalse)
537 if (image->progress_monitor != (MagickProgressMonitor) NULL)
542 #if defined(MAGICKCORE_OPENMP_SUPPORT)
543 #pragma omp critical (MagickCore_BlueShiftImage)
545 proceed=SetImageProgress(image,BlueShiftImageTag,progress++,
547 if (proceed == MagickFalse)
551 image_view=DestroyCacheView(image_view);
552 shift_view=DestroyCacheView(shift_view);
553 if (status == MagickFalse)
554 shift_image=DestroyImage(shift_image);
559 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
563 % C h a r c o a l I m a g e %
567 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
569 % CharcoalImage() creates a new image that is a copy of an existing one with
570 % the edge highlighted. It allocates the memory necessary for the new Image
571 % structure and returns a pointer to the new image.
573 % The format of the CharcoalImage method is:
575 % Image *CharcoalImage(const Image *image,const double radius,
576 % const double sigma,ExceptionInfo *exception)
578 % A description of each parameter follows:
580 % o image: the image.
582 % o radius: the radius of the pixel neighborhood.
584 % o sigma: the standard deviation of the Gaussian, in pixels.
586 % o exception: return any errors or warnings in this structure.
589 MagickExport Image *CharcoalImage(const Image *image,const double radius,
590 const double sigma,ExceptionInfo *exception)
597 assert(image != (Image *) NULL);
598 assert(image->signature == MagickSignature);
599 if (image->debug != MagickFalse)
600 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
601 assert(exception != (ExceptionInfo *) NULL);
602 assert(exception->signature == MagickSignature);
603 clone_image=CloneImage(image,0,0,MagickTrue,exception);
604 if (clone_image == (Image *) NULL)
605 return((Image *) NULL);
606 (void) SetImageType(clone_image,GrayscaleType,exception);
607 edge_image=EdgeImage(clone_image,radius,sigma,exception);
608 clone_image=DestroyImage(clone_image);
609 if (edge_image == (Image *) NULL)
610 return((Image *) NULL);
611 charcoal_image=BlurImage(edge_image,radius,sigma,exception);
612 edge_image=DestroyImage(edge_image);
613 if (charcoal_image == (Image *) NULL)
614 return((Image *) NULL);
615 (void) NormalizeImage(charcoal_image,exception);
616 (void) NegateImage(charcoal_image,MagickFalse,exception);
617 (void) SetImageType(charcoal_image,GrayscaleType,exception);
618 return(charcoal_image);
622 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
626 % C o l o r i z e I m a g e %
630 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
632 % ColorizeImage() blends the fill color with each pixel in the image.
633 % A percentage blend is specified with opacity. Control the application
634 % of different color components by specifying a different percentage for
635 % each component (e.g. 90/100/10 is 90% red, 100% green, and 10% blue).
637 % The format of the ColorizeImage method is:
639 % Image *ColorizeImage(const Image *image,const char *blend,
640 % const PixelInfo *colorize,ExceptionInfo *exception)
642 % A description of each parameter follows:
644 % o image: the image.
646 % o blend: A character string indicating the level of blending as a
649 % o colorize: A color value.
651 % o exception: return any errors or warnings in this structure.
654 MagickExport Image *ColorizeImage(const Image *image,const char *blend,
655 const PixelInfo *colorize,ExceptionInfo *exception)
657 #define ColorizeImageTag "Colorize/Image"
658 #define Colorize(pixel,blend_percentage,colorize) \
659 (pixel)=((pixel)*(100.0-(blend_percentage))+(colorize)*(blend_percentage))/100.0;
687 Allocate colorized image.
689 assert(image != (const Image *) NULL);
690 assert(image->signature == MagickSignature);
691 if (image->debug != MagickFalse)
692 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
693 assert(exception != (ExceptionInfo *) NULL);
694 assert(exception->signature == MagickSignature);
695 colorize_image=CloneImage(image,image->columns,image->rows,MagickTrue,
697 if (colorize_image == (Image *) NULL)
698 return((Image *) NULL);
699 if (SetImageStorageClass(colorize_image,DirectClass,exception) == MagickFalse)
701 colorize_image=DestroyImage(colorize_image);
702 return((Image *) NULL);
704 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
705 (IsPixelInfoGray(colorize) != MagickFalse))
706 (void) SetImageColorspace(colorize_image,sRGBColorspace,exception);
707 if ((colorize_image->matte == MagickFalse) &&
708 (colorize->matte != MagickFalse))
709 (void) SetImageAlpha(colorize_image,OpaqueAlpha,exception);
710 if (blend == (const char *) NULL)
711 return(colorize_image);
712 GetPixelInfo(image,&blend_percentage);
713 flags=ParseGeometry(blend,&geometry_info);
714 blend_percentage.red=geometry_info.rho;
715 blend_percentage.green=geometry_info.rho;
716 blend_percentage.blue=geometry_info.rho;
717 blend_percentage.black=geometry_info.rho;
718 blend_percentage.alpha=geometry_info.rho;
719 if ((flags & SigmaValue) != 0)
720 blend_percentage.green=geometry_info.sigma;
721 if ((flags & XiValue) != 0)
722 blend_percentage.blue=geometry_info.xi;
723 if ((flags & PsiValue) != 0)
724 blend_percentage.alpha=geometry_info.psi;
725 if (blend_percentage.colorspace == CMYKColorspace)
727 if ((flags & PsiValue) != 0)
728 blend_percentage.black=geometry_info.psi;
729 if ((flags & ChiValue) != 0)
730 blend_percentage.alpha=geometry_info.chi;
733 Colorize DirectClass image.
737 image_view=AcquireVirtualCacheView(image,exception);
738 colorize_view=AcquireAuthenticCacheView(colorize_image,exception);
739 #if defined(MAGICKCORE_OPENMP_SUPPORT)
740 #pragma omp parallel for schedule(static,4) shared(progress,status)
742 for (y=0; y < (ssize_t) image->rows; y++)
750 register const Quantum
759 if (status == MagickFalse)
761 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
762 q=QueueCacheViewAuthenticPixels(colorize_view,0,y,colorize_image->columns,1,
764 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
769 GetPixelInfo(colorize_image,&pixel);
770 for (x=0; x < (ssize_t) image->columns; x++)
772 if (GetPixelMask(colorize_image,q) != 0)
774 p+=GetPixelChannels(image);
775 q+=GetPixelChannels(colorize_image);
778 GetPixelInfoPixel(image,p,&pixel);
779 Colorize(pixel.red,blend_percentage.red,colorize->red);
780 Colorize(pixel.green,blend_percentage.green,colorize->green);
781 Colorize(pixel.blue,blend_percentage.blue,colorize->blue);
782 Colorize(pixel.black,blend_percentage.black,colorize->black);
783 Colorize(pixel.alpha,blend_percentage.alpha,colorize->alpha);
784 SetPixelInfoPixel(colorize_image,&pixel,q);
785 p+=GetPixelChannels(image);
786 q+=GetPixelChannels(colorize_image);
788 sync=SyncCacheViewAuthenticPixels(colorize_view,exception);
789 if (sync == MagickFalse)
791 if (image->progress_monitor != (MagickProgressMonitor) NULL)
796 #if defined(MAGICKCORE_OPENMP_SUPPORT)
797 #pragma omp critical (MagickCore_ColorizeImage)
799 proceed=SetImageProgress(image,ColorizeImageTag,progress++,image->rows);
800 if (proceed == MagickFalse)
804 image_view=DestroyCacheView(image_view);
805 colorize_view=DestroyCacheView(colorize_view);
806 if (status == MagickFalse)
807 colorize_image=DestroyImage(colorize_image);
808 return(colorize_image);
812 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
816 % C o l o r M a t r i x I m a g e %
820 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
822 % ColorMatrixImage() applies color transformation to an image. This method
823 % permits saturation changes, hue rotation, luminance to alpha, and various
824 % other effects. Although variable-sized transformation matrices can be used,
825 % typically one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA
826 % (or RGBA with offsets). The matrix is similar to those used by Adobe Flash
827 % except offsets are in column 6 rather than 5 (in support of CMYKA images)
828 % and offsets are normalized (divide Flash offset by 255).
830 % The format of the ColorMatrixImage method is:
832 % Image *ColorMatrixImage(const Image *image,
833 % const KernelInfo *color_matrix,ExceptionInfo *exception)
835 % A description of each parameter follows:
837 % o image: the image.
839 % o color_matrix: the color matrix.
841 % o exception: return any errors or warnings in this structure.
844 /* FUTURE: modify to make use of a MagickMatrix Mutliply function
845 That should be provided in "matrix.c"
846 (ASIDE: actually distorts should do this too but currently doesn't)
849 MagickExport Image *ColorMatrixImage(const Image *image,
850 const KernelInfo *color_matrix,ExceptionInfo *exception)
852 #define ColorMatrixImageTag "ColorMatrix/Image"
861 { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
862 { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
863 { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
864 { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
865 { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
866 { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }
887 Map given color_matrix, into a 6x6 matrix RGBKA and a constant
889 assert(image != (Image *) NULL);
890 assert(image->signature == MagickSignature);
891 if (image->debug != MagickFalse)
892 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
893 assert(exception != (ExceptionInfo *) NULL);
894 assert(exception->signature == MagickSignature);
896 for (v=0; v < (ssize_t) color_matrix->height; v++)
897 for (u=0; u < (ssize_t) color_matrix->width; u++)
899 if ((v < 6) && (u < 6))
900 ColorMatrix[v][u]=color_matrix->values[i];
904 Initialize color image.
906 color_image=CloneImage(image,0,0,MagickTrue,exception);
907 if (color_image == (Image *) NULL)
908 return((Image *) NULL);
909 if (SetImageStorageClass(color_image,DirectClass,exception) == MagickFalse)
911 color_image=DestroyImage(color_image);
912 return((Image *) NULL);
914 if (image->debug != MagickFalse)
917 format[MaxTextExtent],
920 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
921 " ColorMatrix image with color matrix:");
922 message=AcquireString("");
923 for (v=0; v < 6; v++)
926 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
927 (void) ConcatenateString(&message,format);
928 for (u=0; u < 6; u++)
930 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",
932 (void) ConcatenateString(&message,format);
934 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
936 message=DestroyString(message);
939 Apply the ColorMatrix to image.
943 image_view=AcquireVirtualCacheView(image,exception);
944 color_view=AcquireAuthenticCacheView(color_image,exception);
945 #if defined(MAGICKCORE_OPENMP_SUPPORT)
946 #pragma omp parallel for schedule(static,4) shared(progress,status)
948 for (y=0; y < (ssize_t) image->rows; y++)
953 register const Quantum
962 if (status == MagickFalse)
964 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
965 q=GetCacheViewAuthenticPixels(color_view,0,y,color_image->columns,1,
967 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
972 GetPixelInfo(image,&pixel);
973 for (x=0; x < (ssize_t) image->columns; x++)
981 GetPixelInfoPixel(image,p,&pixel);
982 height=color_matrix->height > 6 ? 6UL : color_matrix->height;
983 for (v=0; v < (ssize_t) height; v++)
988 sum=ColorMatrix[v][0]*GetPixelRed(image,p)+ColorMatrix[v][1]*
989 GetPixelGreen(image,p)+ColorMatrix[v][2]*GetPixelBlue(image,p);
990 if (image->colorspace == CMYKColorspace)
991 sum+=ColorMatrix[v][3]*GetPixelBlack(image,p);
992 if (image->matte != MagickFalse)
993 sum+=ColorMatrix[v][4]*GetPixelAlpha(image,p);
994 sum+=QuantumRange*ColorMatrix[v][5];
997 case 0: pixel.red=sum; break;
998 case 1: pixel.green=sum; break;
999 case 2: pixel.blue=sum; break;
1000 case 3: pixel.black=sum; break;
1001 case 4: pixel.alpha=sum; break;
1005 SetPixelInfoPixel(color_image,&pixel,q);
1006 p+=GetPixelChannels(image);
1007 q+=GetPixelChannels(color_image);
1009 if (SyncCacheViewAuthenticPixels(color_view,exception) == MagickFalse)
1011 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1016 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1017 #pragma omp critical (MagickCore_ColorMatrixImage)
1019 proceed=SetImageProgress(image,ColorMatrixImageTag,progress++,
1021 if (proceed == MagickFalse)
1025 color_view=DestroyCacheView(color_view);
1026 image_view=DestroyCacheView(image_view);
1027 if (status == MagickFalse)
1028 color_image=DestroyImage(color_image);
1029 return(color_image);
1033 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1037 + D e s t r o y F x I n f o %
1041 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1043 % DestroyFxInfo() deallocates memory associated with an FxInfo structure.
1045 % The format of the DestroyFxInfo method is:
1047 % ImageInfo *DestroyFxInfo(ImageInfo *fx_info)
1049 % A description of each parameter follows:
1051 % o fx_info: the fx info.
1054 MagickPrivate FxInfo *DestroyFxInfo(FxInfo *fx_info)
1059 fx_info->exception=DestroyExceptionInfo(fx_info->exception);
1060 fx_info->expression=DestroyString(fx_info->expression);
1061 fx_info->symbols=DestroySplayTree(fx_info->symbols);
1062 fx_info->colors=DestroySplayTree(fx_info->colors);
1063 for (i=(ssize_t) GetImageListLength(fx_info->images)-1; i >= 0; i--)
1064 fx_info->view[i]=DestroyCacheView(fx_info->view[i]);
1065 fx_info->view=(CacheView **) RelinquishMagickMemory(fx_info->view);
1066 fx_info->random_info=DestroyRandomInfo(fx_info->random_info);
1067 fx_info=(FxInfo *) RelinquishMagickMemory(fx_info);
1072 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1076 + 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 %
1080 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1082 % FxEvaluateChannelExpression() evaluates an expression and returns the
1085 % The format of the FxEvaluateExpression method is:
1087 % MagickRealType FxEvaluateChannelExpression(FxInfo *fx_info,
1088 % const PixelChannel channel,const ssize_t x,const ssize_t y,
1089 % MagickRealType *alpha,Exceptioninfo *exception)
1090 % MagickRealType FxEvaluateExpression(FxInfo *fx_info,
1091 % MagickRealType *alpha,Exceptioninfo *exception)
1093 % A description of each parameter follows:
1095 % o fx_info: the fx info.
1097 % o channel: the channel.
1099 % o x,y: the pixel position.
1101 % o alpha: the result.
1103 % o exception: return any errors or warnings in this structure.
1107 static inline double MagickMax(const double x,const double y)
1114 static inline double MagickMin(const double x,const double y)
1121 static MagickRealType FxChannelStatistics(FxInfo *fx_info,const Image *image,
1122 PixelChannel channel,const char *symbol,ExceptionInfo *exception)
1126 statistic[MaxTextExtent];
1134 for (p=symbol; (*p != '.') && (*p != '\0'); p++) ;
1136 switch (*++p) /* e.g. depth.r */
1138 case 'r': channel=RedPixelChannel; break;
1139 case 'g': channel=GreenPixelChannel; break;
1140 case 'b': channel=BluePixelChannel; break;
1141 case 'c': channel=CyanPixelChannel; break;
1142 case 'm': channel=MagentaPixelChannel; break;
1143 case 'y': channel=YellowPixelChannel; break;
1144 case 'k': channel=BlackPixelChannel; break;
1147 (void) FormatLocaleString(key,MaxTextExtent,"%p.%.20g.%s",(void *) image,
1148 (double) channel,symbol);
1149 value=(const char *) GetValueFromSplayTree(fx_info->symbols,key);
1150 if (value != (const char *) NULL)
1151 return(QuantumScale*StringToDouble(value,(char **) NULL));
1152 (void) DeleteNodeFromSplayTree(fx_info->symbols,key);
1153 if (LocaleNCompare(symbol,"depth",5) == 0)
1158 depth=GetImageDepth(image,exception);
1159 (void) FormatLocaleString(statistic,MaxTextExtent,"%.20g",(double) depth);
1161 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1167 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
1168 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",kurtosis);
1170 if (LocaleNCompare(symbol,"maxima",6) == 0)
1176 (void) GetImageRange(image,&minima,&maxima,exception);
1177 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",maxima);
1179 if (LocaleNCompare(symbol,"mean",4) == 0)
1185 (void) GetImageMean(image,&mean,&standard_deviation,exception);
1186 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",mean);
1188 if (LocaleNCompare(symbol,"minima",6) == 0)
1194 (void) GetImageRange(image,&minima,&maxima,exception);
1195 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",minima);
1197 if (LocaleNCompare(symbol,"skewness",8) == 0)
1203 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
1204 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",skewness);
1206 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1212 (void) GetImageMean(image,&mean,&standard_deviation,exception);
1213 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",
1214 standard_deviation);
1216 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(key),
1217 ConstantString(statistic));
1218 return(QuantumScale*StringToDouble(statistic,(char **) NULL));
1221 static MagickRealType
1222 FxEvaluateSubexpression(FxInfo *,const PixelChannel,const ssize_t,
1223 const ssize_t,const char *,MagickRealType *,ExceptionInfo *);
1225 static MagickOffsetType FxGCD(MagickOffsetType alpha,MagickOffsetType beta)
1228 return(FxGCD(beta,alpha % beta));
1232 static inline const char *FxSubexpression(const char *expression,
1233 ExceptionInfo *exception)
1242 subexpression=expression;
1243 while ((*subexpression != '\0') &&
1244 ((level != 1) || (strchr(")",(int) *subexpression) == (char *) NULL)))
1246 if (strchr("(",(int) *subexpression) != (char *) NULL)
1249 if (strchr(")",(int) *subexpression) != (char *) NULL)
1253 if (*subexpression == '\0')
1254 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1255 "UnbalancedParenthesis","'%s'",expression);
1256 return(subexpression);
1259 static MagickRealType FxGetSymbol(FxInfo *fx_info,const PixelChannel channel,
1260 const ssize_t x,const ssize_t y,const char *expression,
1261 ExceptionInfo *exception)
1265 subexpression[MaxTextExtent],
1266 symbol[MaxTextExtent];
1293 i=GetImageIndexInList(fx_info->images);
1297 if (isalpha((int) *(p+1)) == 0)
1299 if (strchr("suv",(int) *p) != (char *) NULL)
1306 i=GetImageIndexInList(fx_info->images);
1309 case 'u': i=0; break;
1310 case 'v': i=1; break;
1317 for (p++; *p != '\0'; )
1331 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1333 i=(ssize_t) (alpha+0.5);
1339 if ((isalpha((int) *(p+1)) == 0) && (*p == 'p'))
1346 for (p++; *p != '\0'; )
1360 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1371 for (p++; *p != '\0'; )
1385 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1395 length=GetImageListLength(fx_info->images);
1397 i+=(ssize_t) length;
1399 image=GetImageFromList(fx_info->images,i);
1400 if (image == (Image *) NULL)
1402 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1403 "NoSuchImage","'%s'",expression);
1406 GetPixelInfo(image,&pixel);
1407 (void) InterpolatePixelInfo(image,fx_info->view[i],image->interpolate,
1408 point.x,point.y,&pixel,exception);
1409 if ((strlen(p) > 2) && (LocaleCompare(p,"intensity") != 0) &&
1410 (LocaleCompare(p,"luminance") != 0) && (LocaleCompare(p,"hue") != 0) &&
1411 (LocaleCompare(p,"saturation") != 0) &&
1412 (LocaleCompare(p,"lightness") != 0))
1415 name[MaxTextExtent];
1417 (void) CopyMagickString(name,p,MaxTextExtent);
1418 for (q=name+(strlen(name)-1); q > name; q--)
1428 if ((strlen(name) > 2) &&
1429 (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
1434 color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
1435 if (color != (PixelInfo *) NULL)
1445 status=QueryColorCompliance(name,AllCompliance,&pixel,
1446 fx_info->exception);
1447 if (status != MagickFalse)
1449 (void) AddValueToSplayTree(fx_info->colors,ConstantString(
1450 name),ClonePixelInfo(&pixel));
1456 (void) CopyMagickString(symbol,p,MaxTextExtent);
1457 StripString(symbol);
1458 if (*symbol == '\0')
1462 case RedPixelChannel: return(QuantumScale*pixel.red);
1463 case GreenPixelChannel: return(QuantumScale*pixel.green);
1464 case BluePixelChannel: return(QuantumScale*pixel.blue);
1465 case BlackPixelChannel:
1467 if (image->colorspace != CMYKColorspace)
1469 (void) ThrowMagickException(exception,GetMagickModule(),
1470 ImageError,"ColorSeparatedImageRequired","'%s'",
1474 return(QuantumScale*pixel.black);
1476 case AlphaPixelChannel:
1481 if (pixel.matte == MagickFalse)
1483 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
1486 case IndexPixelChannel:
1488 case IntensityPixelChannel:
1490 return(QuantumScale*GetPixelInfoIntensity(&pixel));
1495 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1496 "UnableToParseExpression","'%s'",p);
1504 if (LocaleCompare(symbol,"a") == 0)
1505 return((MagickRealType) (QuantumScale*pixel.alpha));
1511 if (LocaleCompare(symbol,"b") == 0)
1512 return(QuantumScale*pixel.blue);
1518 if (LocaleNCompare(symbol,"channel",7) == 0)
1526 flags=ParseGeometry(symbol+7,&channel_info);
1527 if (image->colorspace == CMYKColorspace)
1530 case CyanPixelChannel:
1532 if ((flags & RhoValue) == 0)
1534 return(channel_info.rho);
1536 case MagentaPixelChannel:
1538 if ((flags & SigmaValue) == 0)
1540 return(channel_info.sigma);
1542 case YellowPixelChannel:
1544 if ((flags & XiValue) == 0)
1546 return(channel_info.xi);
1548 case BlackPixelChannel:
1550 if ((flags & PsiValue) == 0)
1552 return(channel_info.psi);
1554 case AlphaPixelChannel:
1556 if ((flags & ChiValue) == 0)
1558 return(channel_info.chi);
1565 case RedPixelChannel:
1567 if ((flags & RhoValue) == 0)
1569 return(channel_info.rho);
1571 case GreenPixelChannel:
1573 if ((flags & SigmaValue) == 0)
1575 return(channel_info.sigma);
1577 case BluePixelChannel:
1579 if ((flags & XiValue) == 0)
1581 return(channel_info.xi);
1583 case BlackPixelChannel:
1585 if ((flags & ChiValue) == 0)
1587 return(channel_info.chi);
1589 case AlphaPixelChannel:
1591 if ((flags & PsiValue) == 0)
1593 return(channel_info.psi);
1600 if (LocaleCompare(symbol,"c") == 0)
1601 return(QuantumScale*pixel.red);
1607 if (LocaleNCompare(symbol,"depth",5) == 0)
1608 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1614 if (LocaleCompare(symbol,"g") == 0)
1615 return(QuantumScale*pixel.green);
1621 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1622 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1623 if (LocaleCompare(symbol,"k") == 0)
1625 if (image->colorspace != CMYKColorspace)
1627 (void) ThrowMagickException(exception,GetMagickModule(),
1628 OptionError,"ColorSeparatedImageRequired","'%s'",
1632 return(QuantumScale*pixel.black);
1639 if (LocaleCompare(symbol,"h") == 0)
1640 return((MagickRealType) image->rows);
1641 if (LocaleCompare(symbol,"hue") == 0)
1648 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1657 if ((LocaleCompare(symbol,"image.depth") == 0) ||
1658 (LocaleCompare(symbol,"image.minima") == 0) ||
1659 (LocaleCompare(symbol,"image.maxima") == 0) ||
1660 (LocaleCompare(symbol,"image.mean") == 0) ||
1661 (LocaleCompare(symbol,"image.kurtosis") == 0) ||
1662 (LocaleCompare(symbol,"image.skewness") == 0) ||
1663 (LocaleCompare(symbol,"image.standard_deviation") == 0))
1664 return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
1665 if (LocaleCompare(symbol,"image.resolution.x") == 0)
1666 return(image->resolution.x);
1667 if (LocaleCompare(symbol,"image.resolution.y") == 0)
1668 return(image->resolution.y);
1669 if (LocaleCompare(symbol,"intensity") == 0)
1670 return(QuantumScale*GetPixelInfoIntensity(&pixel));
1671 if (LocaleCompare(symbol,"i") == 0)
1672 return((MagickRealType) x);
1678 if (LocaleCompare(symbol,"j") == 0)
1679 return((MagickRealType) y);
1685 if (LocaleCompare(symbol,"lightness") == 0)
1692 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1696 if (LocaleCompare(symbol,"luminance") == 0)
1701 luminence=0.2126*pixel.red+0.7152*pixel.green+0.0722*pixel.blue;
1702 return(QuantumScale*luminence);
1709 if (LocaleNCompare(symbol,"maxima",6) == 0)
1710 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1711 if (LocaleNCompare(symbol,"mean",4) == 0)
1712 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1713 if (LocaleNCompare(symbol,"minima",6) == 0)
1714 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1715 if (LocaleCompare(symbol,"m") == 0)
1716 return(QuantumScale*pixel.blue);
1722 if (LocaleCompare(symbol,"n") == 0)
1723 return((MagickRealType) GetImageListLength(fx_info->images));
1729 if (LocaleCompare(symbol,"o") == 0)
1730 return(QuantumScale*pixel.alpha);
1736 if (LocaleCompare(symbol,"page.height") == 0)
1737 return((MagickRealType) image->page.height);
1738 if (LocaleCompare(symbol,"page.width") == 0)
1739 return((MagickRealType) image->page.width);
1740 if (LocaleCompare(symbol,"page.x") == 0)
1741 return((MagickRealType) image->page.x);
1742 if (LocaleCompare(symbol,"page.y") == 0)
1743 return((MagickRealType) image->page.y);
1749 if (LocaleCompare(symbol,"resolution.x") == 0)
1750 return(image->resolution.x);
1751 if (LocaleCompare(symbol,"resolution.y") == 0)
1752 return(image->resolution.y);
1753 if (LocaleCompare(symbol,"r") == 0)
1754 return(QuantumScale*pixel.red);
1760 if (LocaleCompare(symbol,"saturation") == 0)
1767 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1771 if (LocaleNCompare(symbol,"skewness",8) == 0)
1772 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1773 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1774 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1780 if (LocaleCompare(symbol,"t") == 0)
1781 return((MagickRealType) GetImageIndexInList(fx_info->images));
1787 if (LocaleCompare(symbol,"w") == 0)
1788 return((MagickRealType) image->columns);
1794 if (LocaleCompare(symbol,"y") == 0)
1795 return(QuantumScale*pixel.green);
1801 if (LocaleCompare(symbol,"z") == 0)
1806 depth=(MagickRealType) GetImageDepth(image,fx_info->exception);
1814 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1815 if (value != (const char *) NULL)
1816 return((MagickRealType) StringToDouble(value,(char **) NULL));
1817 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1818 "UnableToParseExpression","'%s'",symbol);
1822 static const char *FxOperatorPrecedence(const char *expression,
1823 ExceptionInfo *exception)
1827 UndefinedPrecedence,
1829 BitwiseComplementPrecedence,
1831 ExponentialNotationPrecedence,
1835 RelationalPrecedence,
1836 EquivalencyPrecedence,
1837 BitwiseAndPrecedence,
1838 BitwiseOrPrecedence,
1839 LogicalAndPrecedence,
1840 LogicalOrPrecedence,
1842 AssignmentPrecedence,
1862 subexpression=(const char *) NULL;
1863 target=NullPrecedence;
1864 while (*expression != '\0')
1866 precedence=UndefinedPrecedence;
1867 if ((isspace((int) ((char) *expression)) != 0) || (c == (int) '@'))
1872 switch (*expression)
1877 #if defined(MAGICKCORE_HAVE_ACOSH)
1878 if (LocaleNCompare(expression,"acosh",5) == 0)
1884 #if defined(MAGICKCORE_HAVE_ASINH)
1885 if (LocaleNCompare(expression,"asinh",5) == 0)
1891 #if defined(MAGICKCORE_HAVE_ATANH)
1892 if (LocaleNCompare(expression,"atanh",5) == 0)
1903 if ((LocaleNCompare(expression,"E+",2) == 0) ||
1904 (LocaleNCompare(expression,"E-",2) == 0))
1906 expression+=2; /* scientific notation */
1913 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1914 (LocaleNCompare(expression,"j1",2) == 0))
1923 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1930 if ((c == (int) '{') || (c == (int) '['))
1933 if ((c == (int) '}') || (c == (int) ']'))
1936 switch ((unsigned char) *expression)
1941 precedence=BitwiseComplementPrecedence;
1947 precedence=ExponentPrecedence;
1952 if (((c != 0) && ((isdigit((int) ((char) c)) != 0) ||
1953 (strchr(")",c) != (char *) NULL))) &&
1954 (((islower((int) ((char) *expression)) != 0) ||
1955 (strchr("(",(int) *expression) != (char *) NULL)) ||
1956 ((isdigit((int) ((char) c)) == 0) &&
1957 (isdigit((int) ((char) *expression)) != 0))) &&
1958 (strchr("xy",(int) *expression) == (char *) NULL))
1959 precedence=MultiplyPrecedence;
1966 precedence=MultiplyPrecedence;
1972 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
1974 precedence=AdditionPrecedence;
1977 case LeftShiftOperator:
1978 case RightShiftOperator:
1980 precedence=ShiftPrecedence;
1984 case LessThanEqualOperator:
1985 case GreaterThanEqualOperator:
1988 precedence=RelationalPrecedence;
1992 case NotEqualOperator:
1994 precedence=EquivalencyPrecedence;
1999 precedence=BitwiseAndPrecedence;
2004 precedence=BitwiseOrPrecedence;
2007 case LogicalAndOperator:
2009 precedence=LogicalAndPrecedence;
2012 case LogicalOrOperator:
2014 precedence=LogicalOrPrecedence;
2017 case ExponentialNotation:
2019 precedence=ExponentialNotationPrecedence;
2025 precedence=TernaryPrecedence;
2030 precedence=AssignmentPrecedence;
2035 precedence=CommaPrecedence;
2040 precedence=SeparatorPrecedence;
2044 if ((precedence == BitwiseComplementPrecedence) ||
2045 (precedence == TernaryPrecedence) ||
2046 (precedence == AssignmentPrecedence))
2048 if (precedence > target)
2051 Right-to-left associativity.
2054 subexpression=expression;
2058 if (precedence >= target)
2061 Left-to-right associativity.
2064 subexpression=expression;
2066 if (strchr("(",(int) *expression) != (char *) NULL)
2067 expression=FxSubexpression(expression,exception);
2068 c=(int) (*expression++);
2070 return(subexpression);
2073 static MagickRealType FxEvaluateSubexpression(FxInfo *fx_info,
2074 const PixelChannel channel,const ssize_t x,const ssize_t y,
2075 const char *expression,MagickRealType *beta,ExceptionInfo *exception)
2079 subexpression[MaxTextExtent];
2089 if (exception->severity != UndefinedException)
2091 while (isspace((int) *expression) != 0)
2093 if (*expression == '\0')
2095 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
2096 "MissingExpression","'%s'",expression);
2099 *subexpression='\0';
2100 p=FxOperatorPrecedence(expression,exception);
2101 if (p != (const char *) NULL)
2103 (void) CopyMagickString(subexpression,expression,(size_t)
2105 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2107 switch ((unsigned char) *p)
2111 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2112 *beta=(MagickRealType) (~(size_t) *beta);
2117 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2118 return(*beta == 0.0 ? 1.0 : 0.0);
2122 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2123 channel,x,y,++p,beta,exception));
2127 case ExponentialNotation:
2129 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2130 return(alpha*(*beta));
2134 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2137 if (exception->severity == UndefinedException)
2138 (void) ThrowMagickException(exception,GetMagickModule(),
2139 OptionError,"DivideByZero","'%s'",expression);
2142 return(alpha/(*beta));
2146 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2147 *beta=fabs(floor(((double) *beta)+0.5));
2150 (void) ThrowMagickException(exception,GetMagickModule(),
2151 OptionError,"DivideByZero","'%s'",expression);
2154 return(fmod((double) alpha,(double) *beta));
2158 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2159 return(alpha+(*beta));
2163 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2164 return(alpha-(*beta));
2166 case LeftShiftOperator:
2168 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2169 *beta=(MagickRealType) ((size_t) (alpha+0.5) << (size_t) (gamma+0.5));
2172 case RightShiftOperator:
2174 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2175 *beta=(MagickRealType) ((size_t) (alpha+0.5) >> (size_t) (gamma+0.5));
2180 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2181 return(alpha < *beta ? 1.0 : 0.0);
2183 case LessThanEqualOperator:
2185 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2186 return(alpha <= *beta ? 1.0 : 0.0);
2190 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2191 return(alpha > *beta ? 1.0 : 0.0);
2193 case GreaterThanEqualOperator:
2195 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2196 return(alpha >= *beta ? 1.0 : 0.0);
2200 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2201 return(fabs(alpha-(*beta)) <= MagickEpsilon ? 1.0 : 0.0);
2203 case NotEqualOperator:
2205 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2206 return(fabs(alpha-(*beta)) > MagickEpsilon ? 1.0 : 0.0);
2210 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2211 *beta=(MagickRealType) ((size_t) (alpha+0.5) & (size_t) (gamma+0.5));
2216 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2217 *beta=(MagickRealType) ((size_t) (alpha+0.5) | (size_t) (gamma+0.5));
2220 case LogicalAndOperator:
2222 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2223 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2226 case LogicalOrOperator:
2228 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2229 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2237 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2239 p=StringToken(":",&q);
2240 if (q == (char *) NULL)
2242 (void) ThrowMagickException(exception,GetMagickModule(),
2243 OptionError,"UnableToParseExpression","'%s'",subexpression);
2246 if (fabs((double) alpha) > MagickEpsilon)
2247 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2249 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2255 numeric[MaxTextExtent];
2258 while (isalpha((int) ((unsigned char) *q)) != 0)
2262 (void) ThrowMagickException(exception,GetMagickModule(),
2263 OptionError,"UnableToParseExpression","'%s'",subexpression);
2266 ClearMagickException(exception);
2267 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2268 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
2270 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2271 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2272 subexpression),ConstantString(numeric));
2277 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2282 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2287 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2293 if (strchr("(",(int) *expression) != (char *) NULL)
2295 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2296 subexpression[strlen(subexpression)-1]='\0';
2297 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2301 switch (*expression)
2305 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2311 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2317 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2319 return((MagickRealType) (~(size_t) (gamma+0.5)));
2324 if (LocaleNCompare(expression,"abs",3) == 0)
2326 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2328 return((MagickRealType) fabs((double) alpha));
2330 #if defined(MAGICKCORE_HAVE_ACOSH)
2331 if (LocaleNCompare(expression,"acosh",5) == 0)
2333 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2335 return((MagickRealType) acosh((double) alpha));
2338 if (LocaleNCompare(expression,"acos",4) == 0)
2340 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2342 return((MagickRealType) acos((double) alpha));
2344 #if defined(MAGICKCORE_HAVE_J1)
2345 if (LocaleNCompare(expression,"airy",4) == 0)
2347 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2351 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
2352 return(gamma*gamma);
2355 #if defined(MAGICKCORE_HAVE_ASINH)
2356 if (LocaleNCompare(expression,"asinh",5) == 0)
2358 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2360 return((MagickRealType) asinh((double) alpha));
2363 if (LocaleNCompare(expression,"asin",4) == 0)
2365 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2367 return((MagickRealType) asin((double) alpha));
2369 if (LocaleNCompare(expression,"alt",3) == 0)
2371 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2373 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
2375 if (LocaleNCompare(expression,"atan2",5) == 0)
2377 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2379 return((MagickRealType) atan2((double) alpha,(double) *beta));
2381 #if defined(MAGICKCORE_HAVE_ATANH)
2382 if (LocaleNCompare(expression,"atanh",5) == 0)
2384 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2386 return((MagickRealType) atanh((double) alpha));
2389 if (LocaleNCompare(expression,"atan",4) == 0)
2391 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2393 return((MagickRealType) atan((double) alpha));
2395 if (LocaleCompare(expression,"a") == 0)
2396 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2402 if (LocaleCompare(expression,"b") == 0)
2403 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2409 if (LocaleNCompare(expression,"ceil",4) == 0)
2411 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2413 return((MagickRealType) ceil((double) alpha));
2415 if (LocaleNCompare(expression,"cosh",4) == 0)
2417 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2419 return((MagickRealType) cosh((double) alpha));
2421 if (LocaleNCompare(expression,"cos",3) == 0)
2423 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2425 return((MagickRealType) cos((double) alpha));
2427 if (LocaleCompare(expression,"c") == 0)
2428 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2434 if (LocaleNCompare(expression,"debug",5) == 0)
2439 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2441 if (fx_info->images->colorspace == CMYKColorspace)
2444 case CyanPixelChannel: type="cyan"; break;
2445 case MagentaPixelChannel: type="magenta"; break;
2446 case YellowPixelChannel: type="yellow"; break;
2447 case AlphaPixelChannel: type="opacity"; break;
2448 case BlackPixelChannel: type="black"; break;
2449 default: type="unknown"; break;
2454 case RedPixelChannel: type="red"; break;
2455 case GreenPixelChannel: type="green"; break;
2456 case BluePixelChannel: type="blue"; break;
2457 case AlphaPixelChannel: type="opacity"; break;
2458 default: type="unknown"; break;
2460 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2461 if (strlen(subexpression) > 1)
2462 subexpression[strlen(subexpression)-1]='\0';
2463 if (fx_info->file != (FILE *) NULL)
2464 (void) FormatLocaleFile(fx_info->file,"%s[%.20g,%.20g].%s: "
2465 "%s=%.*g\n",fx_info->images->filename,(double) x,(double) y,type,
2466 subexpression,GetMagickPrecision(),(double) alpha);
2469 if (LocaleNCompare(expression,"drc",3) == 0)
2471 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2473 return((MagickRealType) (alpha/(*beta*(alpha-1.0)+1.0)));
2480 if (LocaleCompare(expression,"epsilon") == 0)
2481 return((MagickRealType) MagickEpsilon);
2482 if (LocaleNCompare(expression,"exp",3) == 0)
2484 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2486 return((MagickRealType) exp((double) alpha));
2488 if (LocaleCompare(expression,"e") == 0)
2489 return((MagickRealType) 2.7182818284590452354);
2495 if (LocaleNCompare(expression,"floor",5) == 0)
2497 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2499 return((MagickRealType) floor((double) alpha));
2506 if (LocaleNCompare(expression,"gauss",5) == 0)
2508 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2510 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
2511 return((MagickRealType) gamma);
2513 if (LocaleNCompare(expression,"gcd",3) == 0)
2518 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2520 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType) (*beta+
2522 return((MagickRealType) gcd);
2524 if (LocaleCompare(expression,"g") == 0)
2525 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2531 if (LocaleCompare(expression,"h") == 0)
2532 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2533 if (LocaleCompare(expression,"hue") == 0)
2534 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2535 if (LocaleNCompare(expression,"hypot",5) == 0)
2537 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2539 return((MagickRealType) hypot((double) alpha,(double) *beta));
2546 if (LocaleCompare(expression,"k") == 0)
2547 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2553 if (LocaleCompare(expression,"intensity") == 0)
2554 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2555 if (LocaleNCompare(expression,"int",3) == 0)
2557 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2559 return((MagickRealType) floor(alpha));
2561 #if defined(MAGICKCORE_HAVE_ISNAN)
2562 if (LocaleNCompare(expression,"isnan",5) == 0)
2564 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2566 return((MagickRealType) !!isnan((double) alpha));
2569 if (LocaleCompare(expression,"i") == 0)
2570 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2576 if (LocaleCompare(expression,"j") == 0)
2577 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2578 #if defined(MAGICKCORE_HAVE_J0)
2579 if (LocaleNCompare(expression,"j0",2) == 0)
2581 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2583 return((MagickRealType) j0((double) alpha));
2586 #if defined(MAGICKCORE_HAVE_J1)
2587 if (LocaleNCompare(expression,"j1",2) == 0)
2589 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2591 return((MagickRealType) j1((double) alpha));
2594 #if defined(MAGICKCORE_HAVE_J1)
2595 if (LocaleNCompare(expression,"jinc",4) == 0)
2597 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2601 gamma=(MagickRealType) (2.0*j1((double) (MagickPI*alpha))/(MagickPI*
2611 if (LocaleNCompare(expression,"ln",2) == 0)
2613 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2615 return((MagickRealType) log((double) alpha));
2617 if (LocaleNCompare(expression,"logtwo",6) == 0)
2619 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2621 return((MagickRealType) log10((double) alpha))/log10(2.0);
2623 if (LocaleNCompare(expression,"log",3) == 0)
2625 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2627 return((MagickRealType) log10((double) alpha));
2629 if (LocaleCompare(expression,"lightness") == 0)
2630 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2636 if (LocaleCompare(expression,"MaxRGB") == 0)
2637 return((MagickRealType) QuantumRange);
2638 if (LocaleNCompare(expression,"maxima",6) == 0)
2640 if (LocaleNCompare(expression,"max",3) == 0)
2642 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2644 return(alpha > *beta ? alpha : *beta);
2646 if (LocaleNCompare(expression,"minima",6) == 0)
2648 if (LocaleNCompare(expression,"min",3) == 0)
2650 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2652 return(alpha < *beta ? alpha : *beta);
2654 if (LocaleNCompare(expression,"mod",3) == 0)
2656 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2658 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2661 if (LocaleCompare(expression,"m") == 0)
2662 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2668 if (LocaleNCompare(expression,"not",3) == 0)
2670 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2672 return((MagickRealType) (alpha < MagickEpsilon));
2674 if (LocaleCompare(expression,"n") == 0)
2675 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2681 if (LocaleCompare(expression,"Opaque") == 0)
2683 if (LocaleCompare(expression,"o") == 0)
2684 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2690 if (LocaleCompare(expression,"phi") == 0)
2691 return((MagickRealType) MagickPHI);
2692 if (LocaleCompare(expression,"pi") == 0)
2693 return((MagickRealType) MagickPI);
2694 if (LocaleNCompare(expression,"pow",3) == 0)
2696 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2698 return((MagickRealType) pow((double) alpha,(double) *beta));
2700 if (LocaleCompare(expression,"p") == 0)
2701 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2707 if (LocaleCompare(expression,"QuantumRange") == 0)
2708 return((MagickRealType) QuantumRange);
2709 if (LocaleCompare(expression,"QuantumScale") == 0)
2710 return((MagickRealType) QuantumScale);
2716 if (LocaleNCompare(expression,"rand",4) == 0)
2717 return((MagickRealType) GetPseudoRandomValue(fx_info->random_info));
2718 if (LocaleNCompare(expression,"round",5) == 0)
2720 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2722 return((MagickRealType) floor((double) alpha+0.5));
2724 if (LocaleCompare(expression,"r") == 0)
2725 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2731 if (LocaleCompare(expression,"saturation") == 0)
2732 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2733 if (LocaleNCompare(expression,"sign",4) == 0)
2735 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2737 return(alpha < 0.0 ? -1.0 : 1.0);
2739 if (LocaleNCompare(expression,"sinc",4) == 0)
2741 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2745 gamma=(MagickRealType) (sin((double) (MagickPI*alpha))/
2749 if (LocaleNCompare(expression,"sinh",4) == 0)
2751 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2753 return((MagickRealType) sinh((double) alpha));
2755 if (LocaleNCompare(expression,"sin",3) == 0)
2757 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2759 return((MagickRealType) sin((double) alpha));
2761 if (LocaleNCompare(expression,"sqrt",4) == 0)
2763 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2765 return((MagickRealType) sqrt((double) alpha));
2767 if (LocaleNCompare(expression,"squish",6) == 0)
2769 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2771 return((MagickRealType) (1.0/(1.0+exp((double) (4.0*alpha)))));
2773 if (LocaleCompare(expression,"s") == 0)
2774 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2780 if (LocaleNCompare(expression,"tanh",4) == 0)
2782 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2784 return((MagickRealType) tanh((double) alpha));
2786 if (LocaleNCompare(expression,"tan",3) == 0)
2788 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2790 return((MagickRealType) tan((double) alpha));
2792 if (LocaleCompare(expression,"Transparent") == 0)
2794 if (LocaleNCompare(expression,"trunc",5) == 0)
2796 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2799 return((MagickRealType) floor((double) alpha));
2800 return((MagickRealType) ceil((double) alpha));
2802 if (LocaleCompare(expression,"t") == 0)
2803 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2809 if (LocaleCompare(expression,"u") == 0)
2810 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2816 if (LocaleCompare(expression,"v") == 0)
2817 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2823 if (LocaleNCompare(expression,"while",5) == 0)
2827 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2829 } while (fabs((double) alpha) >= MagickEpsilon);
2830 return((MagickRealType) *beta);
2832 if (LocaleCompare(expression,"w") == 0)
2833 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2839 if (LocaleCompare(expression,"y") == 0)
2840 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2846 if (LocaleCompare(expression,"z") == 0)
2847 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2853 q=(char *) expression;
2854 alpha=InterpretSiPrefixValue(expression,&q);
2855 if (q == expression)
2856 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2860 MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
2861 MagickRealType *alpha,ExceptionInfo *exception)
2866 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2871 MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
2872 MagickRealType *alpha,ExceptionInfo *exception)
2881 fx_info->file=(FILE *) NULL;
2882 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2888 MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
2889 const PixelChannel channel,const ssize_t x,const ssize_t y,
2890 MagickRealType *alpha,ExceptionInfo *exception)
2896 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2898 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2902 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2910 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2912 % FxImage() applies a mathematical expression to the specified image.
2914 % The format of the FxImage method is:
2916 % Image *FxImage(const Image *image,const char *expression,
2917 % ExceptionInfo *exception)
2919 % A description of each parameter follows:
2921 % o image: the image.
2923 % o expression: A mathematical expression.
2925 % o exception: return any errors or warnings in this structure.
2929 static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2934 assert(fx_info != (FxInfo **) NULL);
2935 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
2936 if (fx_info[i] != (FxInfo *) NULL)
2937 fx_info[i]=DestroyFxInfo(fx_info[i]);
2938 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
2942 static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2943 ExceptionInfo *exception)
2960 number_threads=GetOpenMPMaximumThreads();
2961 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
2962 if (fx_info == (FxInfo **) NULL)
2963 return((FxInfo **) NULL);
2964 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2965 if (*expression != '@')
2966 fx_expression=ConstantString(expression);
2968 fx_expression=FileToString(expression+1,~0,exception);
2969 for (i=0; i < (ssize_t) number_threads; i++)
2971 fx_info[i]=AcquireFxInfo(image,fx_expression,exception);
2972 if (fx_info[i] == (FxInfo *) NULL)
2973 return(DestroyFxThreadSet(fx_info));
2974 (void) FxPreprocessExpression(fx_info[i],&alpha,fx_info[i]->exception);
2976 fx_expression=DestroyString(fx_expression);
2980 MagickExport Image *FxImage(const Image *image,const char *expression,
2981 ExceptionInfo *exception)
2983 #define FxImageTag "Fx/Image"
3007 assert(image != (Image *) NULL);
3008 assert(image->signature == MagickSignature);
3009 if (image->debug != MagickFalse)
3010 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3011 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3012 if (fx_image == (Image *) NULL)
3013 return((Image *) NULL);
3014 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
3016 fx_image=DestroyImage(fx_image);
3017 return((Image *) NULL);
3019 fx_info=AcquireFxThreadSet(image,expression,exception);
3020 if (fx_info == (FxInfo **) NULL)
3022 fx_image=DestroyImage(fx_image);
3023 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3025 status=FxPreprocessExpression(fx_info[0],&alpha,exception);
3026 if (status == MagickFalse)
3028 fx_image=DestroyImage(fx_image);
3029 fx_info=DestroyFxThreadSet(fx_info);
3030 return((Image *) NULL);
3037 image_view=AcquireVirtualCacheView(image,exception);
3038 fx_view=AcquireAuthenticCacheView(fx_image,exception);
3039 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3040 #pragma omp parallel for schedule(static,4) shared(progress,status)
3042 for (y=0; y < (ssize_t) fx_image->rows; y++)
3045 id = GetOpenMPThreadId();
3047 register const Quantum
3056 if (status == MagickFalse)
3058 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3059 q=QueueCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
3060 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3065 for (x=0; x < (ssize_t) fx_image->columns; x++)
3070 if (GetPixelMask(image,p) != 0)
3072 p+=GetPixelChannels(image);
3073 q+=GetPixelChannels(fx_image);
3076 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3088 channel=GetPixelChannelMapChannel(image,i);
3089 traits=GetPixelChannelMapTraits(image,channel);
3090 fx_traits=GetPixelChannelMapTraits(fx_image,channel);
3091 if ((traits == UndefinedPixelTrait) ||
3092 (fx_traits == UndefinedPixelTrait))
3094 if ((fx_traits & CopyPixelTrait) != 0)
3096 SetPixelChannel(fx_image,channel,p[i],q);
3100 (void) FxEvaluateChannelExpression(fx_info[id],channel,x,y,&alpha,
3102 q[i]=ClampToQuantum((MagickRealType) QuantumRange*alpha);
3104 p+=GetPixelChannels(image);
3105 q+=GetPixelChannels(fx_image);
3107 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3109 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3114 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3115 #pragma omp critical (MagickCore_FxImage)
3117 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3118 if (proceed == MagickFalse)
3122 fx_view=DestroyCacheView(fx_view);
3123 image_view=DestroyCacheView(image_view);
3124 fx_info=DestroyFxThreadSet(fx_info);
3125 if (status == MagickFalse)
3126 fx_image=DestroyImage(fx_image);
3131 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3135 % I m p l o d e I m a g e %
3139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3141 % ImplodeImage() creates a new image that is a copy of an existing
3142 % one with the image pixels "implode" by the specified percentage. It
3143 % allocates the memory necessary for the new Image structure and returns a
3144 % pointer to the new image.
3146 % The format of the ImplodeImage method is:
3148 % Image *ImplodeImage(const Image *image,const double amount,
3149 % const PixelInterpolateMethod method,ExceptionInfo *exception)
3151 % A description of each parameter follows:
3153 % o implode_image: Method ImplodeImage returns a pointer to the image
3154 % after it is implode. A null image is returned if there is a memory
3157 % o image: the image.
3159 % o amount: Define the extent of the implosion.
3161 % o method: the pixel interpolation method.
3163 % o exception: return any errors or warnings in this structure.
3166 MagickExport Image *ImplodeImage(const Image *image,const double amount,
3167 const PixelInterpolateMethod method,ExceptionInfo *exception)
3169 #define ImplodeImageTag "Implode/Image"
3195 Initialize implode image attributes.
3197 assert(image != (Image *) NULL);
3198 assert(image->signature == MagickSignature);
3199 if (image->debug != MagickFalse)
3200 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3201 assert(exception != (ExceptionInfo *) NULL);
3202 assert(exception->signature == MagickSignature);
3203 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3205 if (implode_image == (Image *) NULL)
3206 return((Image *) NULL);
3207 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
3209 implode_image=DestroyImage(implode_image);
3210 return((Image *) NULL);
3212 if (implode_image->background_color.alpha != OpaqueAlpha)
3213 implode_image->matte=MagickTrue;
3215 Compute scaling factor.
3219 center.x=0.5*image->columns;
3220 center.y=0.5*image->rows;
3222 if (image->columns > image->rows)
3223 scale.y=(double) image->columns/(double) image->rows;
3225 if (image->columns < image->rows)
3227 scale.x=(double) image->rows/(double) image->columns;
3235 image_view=AcquireVirtualCacheView(image,exception);
3236 implode_view=AcquireAuthenticCacheView(implode_image,exception);
3237 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3238 #pragma omp parallel for schedule(static,4) shared(progress,status)
3240 for (y=0; y < (ssize_t) image->rows; y++)
3248 register const Quantum
3257 if (status == MagickFalse)
3259 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3260 q=QueueCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
3262 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3267 delta.y=scale.y*(double) (y-center.y);
3268 for (x=0; x < (ssize_t) image->columns; x++)
3274 Determine if the pixel is within an ellipse.
3276 if (GetPixelMask(image,p) != 0)
3278 p+=GetPixelChannels(image);
3279 q+=GetPixelChannels(implode_image);
3282 delta.x=scale.x*(double) (x-center.x);
3283 distance=delta.x*delta.x+delta.y*delta.y;
3284 if (distance >= (radius*radius))
3285 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3294 channel=GetPixelChannelMapChannel(image,i);
3295 traits=GetPixelChannelMapTraits(image,channel);
3296 implode_traits=GetPixelChannelMapTraits(implode_image,channel);
3297 if ((traits == UndefinedPixelTrait) ||
3298 (implode_traits == UndefinedPixelTrait))
3300 SetPixelChannel(implode_image,channel,p[i],q);
3312 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/radius/
3314 status=InterpolatePixelChannels(image,image_view,implode_image,method,
3315 (double) (factor*delta.x/scale.x+center.x),(double) (factor*delta.y/
3316 scale.y+center.y),q,exception);
3318 p+=GetPixelChannels(image);
3319 q+=GetPixelChannels(implode_image);
3321 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3323 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3328 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3329 #pragma omp critical (MagickCore_ImplodeImage)
3331 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3332 if (proceed == MagickFalse)
3336 implode_view=DestroyCacheView(implode_view);
3337 image_view=DestroyCacheView(image_view);
3338 if (status == MagickFalse)
3339 implode_image=DestroyImage(implode_image);
3340 return(implode_image);
3344 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3348 % M o r p h I m a g e s %
3352 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3354 % The MorphImages() method requires a minimum of two images. The first
3355 % image is transformed into the second by a number of intervening images
3356 % as specified by frames.
3358 % The format of the MorphImage method is:
3360 % Image *MorphImages(const Image *image,const size_t number_frames,
3361 % ExceptionInfo *exception)
3363 % A description of each parameter follows:
3365 % o image: the image.
3367 % o number_frames: Define the number of in-between image to generate.
3368 % The more in-between frames, the smoother the morph.
3370 % o exception: return any errors or warnings in this structure.
3373 MagickExport Image *MorphImages(const Image *image,
3374 const size_t number_frames,ExceptionInfo *exception)
3376 #define MorphImageTag "Morph/Image"
3392 register const Image
3402 Clone first frame in sequence.
3404 assert(image != (Image *) NULL);
3405 assert(image->signature == MagickSignature);
3406 if (image->debug != MagickFalse)
3407 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3408 assert(exception != (ExceptionInfo *) NULL);
3409 assert(exception->signature == MagickSignature);
3410 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3411 if (morph_images == (Image *) NULL)
3412 return((Image *) NULL);
3413 if (GetNextImageInList(image) == (Image *) NULL)
3418 for (i=1; i < (ssize_t) number_frames; i++)
3420 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3421 if (morph_image == (Image *) NULL)
3423 morph_images=DestroyImageList(morph_images);
3424 return((Image *) NULL);
3426 AppendImageToList(&morph_images,morph_image);
3427 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3432 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3434 if (proceed == MagickFalse)
3438 return(GetFirstImageInList(morph_images));
3441 Morph image sequence.
3446 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3448 for (i=0; i < (ssize_t) number_frames; i++)
3454 beta=(MagickRealType) (i+1.0)/(MagickRealType) (number_frames+1.0);
3456 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
3457 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*next->rows+beta*
3458 GetNextImageInList(next)->rows+0.5),next->filter,exception);
3459 if (morph_image == (Image *) NULL)
3461 morph_images=DestroyImageList(morph_images);
3462 return((Image *) NULL);
3464 status=SetImageStorageClass(morph_image,DirectClass,exception);
3465 if (status == MagickFalse)
3467 morph_image=DestroyImage(morph_image);
3468 return((Image *) NULL);
3470 AppendImageToList(&morph_images,morph_image);
3471 morph_images=GetLastImageInList(morph_images);
3472 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
3473 morph_images->rows,GetNextImageInList(next)->filter,exception);
3474 if (morph_image == (Image *) NULL)
3476 morph_images=DestroyImageList(morph_images);
3477 return((Image *) NULL);
3479 image_view=AcquireVirtualCacheView(morph_image,exception);
3480 morph_view=AcquireAuthenticCacheView(morph_images,exception);
3481 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3482 #pragma omp parallel for schedule(static,4) shared(status)
3484 for (y=0; y < (ssize_t) morph_images->rows; y++)
3489 register const Quantum
3498 if (status == MagickFalse)
3500 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3502 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3504 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3509 for (x=0; x < (ssize_t) morph_images->columns; x++)
3514 if (GetPixelMask(image,p) != 0)
3516 p+=GetPixelChannels(image);
3517 q+=GetPixelChannels(morph_image);
3520 for (i=0; i < (ssize_t) GetPixelChannels(morph_image); i++)
3529 channel=GetPixelChannelMapChannel(image,i);
3530 traits=GetPixelChannelMapTraits(image,channel);
3531 morph_traits=GetPixelChannelMapTraits(morph_image,channel);
3532 if ((traits == UndefinedPixelTrait) ||
3533 (morph_traits == UndefinedPixelTrait))
3535 if ((morph_traits & CopyPixelTrait) != 0)
3537 SetPixelChannel(morph_image,channel,p[i],q);
3540 SetPixelChannel(morph_image,channel,ClampToQuantum(alpha*
3541 GetPixelChannel(morph_images,channel,q)+beta*p[i]),q);
3543 p+=GetPixelChannels(morph_image);
3544 q+=GetPixelChannels(morph_images);
3546 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3547 if (sync == MagickFalse)
3550 morph_view=DestroyCacheView(morph_view);
3551 image_view=DestroyCacheView(image_view);
3552 morph_image=DestroyImage(morph_image);
3554 if (i < (ssize_t) number_frames)
3557 Clone last frame in sequence.
3559 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3560 if (morph_image == (Image *) NULL)
3562 morph_images=DestroyImageList(morph_images);
3563 return((Image *) NULL);
3565 AppendImageToList(&morph_images,morph_image);
3566 morph_images=GetLastImageInList(morph_images);
3567 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3572 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3573 #pragma omp critical (MagickCore_MorphImages)
3575 proceed=SetImageProgress(image,MorphImageTag,scene,
3576 GetImageListLength(image));
3577 if (proceed == MagickFalse)
3582 if (GetNextImageInList(next) != (Image *) NULL)
3584 morph_images=DestroyImageList(morph_images);
3585 return((Image *) NULL);
3587 return(GetFirstImageInList(morph_images));
3591 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3595 % P l a s m a I m a g e %
3599 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3601 % PlasmaImage() initializes an image with plasma fractal values. The image
3602 % must be initialized with a base color and the random number generator
3603 % seeded before this method is called.
3605 % The format of the PlasmaImage method is:
3607 % MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
3608 % size_t attenuate,size_t depth,ExceptionInfo *exception)
3610 % A description of each parameter follows:
3612 % o image: the image.
3614 % o segment: Define the region to apply plasma fractals values.
3616 % o attenuate: Define the plasma attenuation factor.
3618 % o depth: Limit the plasma recursion depth.
3620 % o exception: return any errors or warnings in this structure.
3624 static inline Quantum PlasmaPixel(RandomInfo *random_info,
3625 const MagickRealType pixel,const MagickRealType noise)
3630 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
3635 static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3636 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3637 const SegmentInfo *segment,size_t attenuate,size_t depth,
3638 ExceptionInfo *exception)
3649 register const Quantum
3665 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3673 Divide the area into quadrants and recurse.
3677 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3678 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
3679 local_info=(*segment);
3680 local_info.x2=(double) x_mid;
3681 local_info.y2=(double) y_mid;
3682 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3683 &local_info,attenuate,depth,exception);
3684 local_info=(*segment);
3685 local_info.y1=(double) y_mid;
3686 local_info.x2=(double) x_mid;
3687 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3688 &local_info,attenuate,depth,exception);
3689 local_info=(*segment);
3690 local_info.x1=(double) x_mid;
3691 local_info.y2=(double) y_mid;
3692 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3693 &local_info,attenuate,depth,exception);
3694 local_info=(*segment);
3695 local_info.x1=(double) x_mid;
3696 local_info.y1=(double) y_mid;
3697 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3698 &local_info,attenuate,depth,exception));
3700 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3701 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
3702 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3703 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3704 return(MagickFalse);
3706 Average pixels and apply plasma.
3708 plasma=(MagickRealType) QuantumRange/(2.0*attenuate);
3709 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3714 x=(ssize_t) ceil(segment->x1-0.5);
3715 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),1,1,
3717 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),1,1,
3719 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
3720 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3721 (q == (Quantum *) NULL))
3723 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3725 channel=GetPixelChannelMapChannel(image,i);
3726 traits=GetPixelChannelMapTraits(image,channel);
3727 if (traits == UndefinedPixelTrait)
3729 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3731 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3732 if (segment->x1 != segment->x2)
3737 x=(ssize_t) ceil(segment->x2-0.5);
3738 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3740 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3742 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
3743 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3744 (q == (Quantum *) NULL))
3746 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3748 channel=GetPixelChannelMapChannel(image,i);
3749 traits=GetPixelChannelMapTraits(image,channel);
3750 if (traits == UndefinedPixelTrait)
3752 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3754 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3757 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3759 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3764 y=(ssize_t) ceil(segment->y2-0.5);
3765 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3767 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3769 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
3770 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3771 (q == (Quantum *) NULL))
3773 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3775 channel=GetPixelChannelMapChannel(image,i);
3776 traits=GetPixelChannelMapTraits(image,channel);
3777 if (traits == UndefinedPixelTrait)
3779 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3781 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3783 if (segment->y1 != segment->y2)
3788 y=(ssize_t) ceil(segment->y1-0.5);
3789 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3791 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3793 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
3794 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3795 (q == (Quantum *) NULL))
3797 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3799 channel=GetPixelChannelMapChannel(image,i);
3800 traits=GetPixelChannelMapTraits(image,channel);
3801 if (traits == UndefinedPixelTrait)
3803 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3805 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3808 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3813 x=(ssize_t) ceil(segment->x1-0.5);
3814 y=(ssize_t) ceil(segment->y1-0.5);
3815 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
3816 x=(ssize_t) ceil(segment->x2-0.5);
3817 y=(ssize_t) ceil(segment->y2-0.5);
3818 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
3819 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
3820 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3821 (q == (Quantum *) NULL))
3823 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3825 channel=GetPixelChannelMapChannel(image,i);
3826 traits=GetPixelChannelMapTraits(image,channel);
3827 if (traits == UndefinedPixelTrait)
3829 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3831 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3833 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3835 return(MagickFalse);
3838 MagickExport MagickBooleanType PlasmaImage(Image *image,
3839 const SegmentInfo *segment,size_t attenuate,size_t depth,
3840 ExceptionInfo *exception)
3853 if (image->debug != MagickFalse)
3854 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3855 assert(image != (Image *) NULL);
3856 assert(image->signature == MagickSignature);
3857 if (image->debug != MagickFalse)
3858 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3859 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
3860 return(MagickFalse);
3861 image_view=AcquireAuthenticCacheView(image,exception);
3862 u_view=AcquireVirtualCacheView(image,exception);
3863 v_view=AcquireVirtualCacheView(image,exception);
3864 random_info=AcquireRandomInfo();
3865 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3866 attenuate,depth,exception);
3867 random_info=DestroyRandomInfo(random_info);
3868 v_view=DestroyCacheView(v_view);
3869 u_view=DestroyCacheView(u_view);
3870 image_view=DestroyCacheView(image_view);
3875 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3879 % P o l a r o i d I m a g e %
3883 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3885 % PolaroidImage() simulates a Polaroid picture.
3887 % The format of the AnnotateImage method is:
3889 % Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
3890 % const char *caption,const double angle,
3891 % const PixelInterpolateMethod method,ExceptionInfo exception)
3893 % A description of each parameter follows:
3895 % o image: the image.
3897 % o draw_info: the draw info.
3899 % o caption: the Polaroid caption.
3901 % o angle: Apply the effect along this angle.
3903 % o method: the pixel interpolation method.
3905 % o exception: return any errors or warnings in this structure.
3908 MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
3909 const char *caption,const double angle,const PixelInterpolateMethod method,
3910 ExceptionInfo *exception)
3928 Simulate a Polaroid picture.
3930 assert(image != (Image *) NULL);
3931 assert(image->signature == MagickSignature);
3932 if (image->debug != MagickFalse)
3933 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3934 assert(exception != (ExceptionInfo *) NULL);
3935 assert(exception->signature == MagickSignature);
3936 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
3937 image->rows)/25.0,10.0);
3938 height=image->rows+2*quantum;
3939 caption_image=(Image *) NULL;
3940 if (caption != (const char *) NULL)
3943 geometry[MaxTextExtent],
3959 Generate caption image.
3961 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3962 if (caption_image == (Image *) NULL)
3963 return((Image *) NULL);
3964 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
3965 text=InterpretImageProperties((ImageInfo *) NULL,(Image *) image,caption,
3967 (void) CloneString(&annotate_info->text,text);
3968 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
3970 status=SetImageExtent(caption_image,image->columns,(size_t) ((count+1)*
3971 (metrics.ascent-metrics.descent)+0.5),exception);
3972 if (status == MagickFalse)
3973 caption_image=DestroyImage(caption_image);
3976 caption_image->background_color=image->border_color;
3977 (void) SetImageBackgroundColor(caption_image,exception);
3978 (void) CloneString(&annotate_info->text,text);
3979 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
3981 if (annotate_info->gravity == UndefinedGravity)
3982 (void) CloneString(&annotate_info->geometry,AcquireString(
3984 (void) AnnotateImage(caption_image,annotate_info,exception);
3985 height+=caption_image->rows;
3987 annotate_info=DestroyDrawInfo(annotate_info);
3988 text=DestroyString(text);
3990 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
3992 if (picture_image == (Image *) NULL)
3994 if (caption_image != (Image *) NULL)
3995 caption_image=DestroyImage(caption_image);
3996 return((Image *) NULL);
3998 picture_image->background_color=image->border_color;
3999 (void) SetImageBackgroundColor(picture_image,exception);
4000 (void) CompositeImage(picture_image,image,OverCompositeOp,MagickTrue,quantum,
4002 if (caption_image != (Image *) NULL)
4004 (void) CompositeImage(picture_image,caption_image,OverCompositeOp,
4005 MagickTrue,quantum,(ssize_t) (image->rows+3*quantum/2),exception);
4006 caption_image=DestroyImage(caption_image);
4008 (void) QueryColorCompliance("none",AllCompliance,
4009 &picture_image->background_color,exception);
4010 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
4011 rotate_image=RotateImage(picture_image,90.0,exception);
4012 picture_image=DestroyImage(picture_image);
4013 if (rotate_image == (Image *) NULL)
4014 return((Image *) NULL);
4015 picture_image=rotate_image;
4016 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
4017 picture_image->columns,method,exception);
4018 picture_image=DestroyImage(picture_image);
4019 if (bend_image == (Image *) NULL)
4020 return((Image *) NULL);
4021 picture_image=bend_image;
4022 rotate_image=RotateImage(picture_image,-90.0,exception);
4023 picture_image=DestroyImage(picture_image);
4024 if (rotate_image == (Image *) NULL)
4025 return((Image *) NULL);
4026 picture_image=rotate_image;
4027 picture_image->background_color=image->background_color;
4028 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
4030 if (polaroid_image == (Image *) NULL)
4032 picture_image=DestroyImage(picture_image);
4033 return(picture_image);
4035 flop_image=FlopImage(polaroid_image,exception);
4036 polaroid_image=DestroyImage(polaroid_image);
4037 if (flop_image == (Image *) NULL)
4039 picture_image=DestroyImage(picture_image);
4040 return(picture_image);
4042 polaroid_image=flop_image;
4043 (void) CompositeImage(polaroid_image,picture_image,OverCompositeOp,
4044 MagickTrue,(ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
4045 picture_image=DestroyImage(picture_image);
4046 (void) QueryColorCompliance("none",AllCompliance,
4047 &polaroid_image->background_color,exception);
4048 rotate_image=RotateImage(polaroid_image,angle,exception);
4049 polaroid_image=DestroyImage(polaroid_image);
4050 if (rotate_image == (Image *) NULL)
4051 return((Image *) NULL);
4052 polaroid_image=rotate_image;
4053 trim_image=TrimImage(polaroid_image,exception);
4054 polaroid_image=DestroyImage(polaroid_image);
4055 if (trim_image == (Image *) NULL)
4056 return((Image *) NULL);
4057 polaroid_image=trim_image;
4058 return(polaroid_image);
4062 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4066 % S e p i a T o n e I m a g e %
4070 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4072 % MagickSepiaToneImage() applies a special effect to the image, similar to the
4073 % effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4074 % 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4075 % threshold of 80% is a good starting point for a reasonable tone.
4077 % The format of the SepiaToneImage method is:
4079 % Image *SepiaToneImage(const Image *image,const double threshold,
4080 % ExceptionInfo *exception)
4082 % A description of each parameter follows:
4084 % o image: the image.
4086 % o threshold: the tone threshold.
4088 % o exception: return any errors or warnings in this structure.
4091 MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4092 ExceptionInfo *exception)
4094 #define SepiaToneImageTag "SepiaTone/Image"
4113 Initialize sepia-toned image attributes.
4115 assert(image != (const Image *) NULL);
4116 assert(image->signature == MagickSignature);
4117 if (image->debug != MagickFalse)
4118 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4119 assert(exception != (ExceptionInfo *) NULL);
4120 assert(exception->signature == MagickSignature);
4121 sepia_image=CloneImage(image,0,0,MagickTrue,exception);
4122 if (sepia_image == (Image *) NULL)
4123 return((Image *) NULL);
4124 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
4126 sepia_image=DestroyImage(sepia_image);
4127 return((Image *) NULL);
4130 Tone each row of the image.
4134 image_view=AcquireVirtualCacheView(image,exception);
4135 sepia_view=AcquireAuthenticCacheView(sepia_image,exception);
4136 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4137 #pragma omp parallel for schedule(static,4) shared(progress,status)
4139 for (y=0; y < (ssize_t) image->rows; y++)
4141 register const Quantum
4150 if (status == MagickFalse)
4152 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
4153 q=GetCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
4155 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
4160 for (x=0; x < (ssize_t) image->columns; x++)
4166 intensity=(MagickRealType) GetPixelIntensity(image,p);
4167 tone=intensity > threshold ? (MagickRealType) QuantumRange : intensity+
4168 (MagickRealType) QuantumRange-threshold;
4169 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
4170 tone=intensity > (7.0*threshold/6.0) ? (MagickRealType) QuantumRange :
4171 intensity+(MagickRealType) QuantumRange-7.0*threshold/6.0;
4172 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4173 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
4174 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
4176 if ((MagickRealType) GetPixelGreen(image,q) < tone)
4177 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4178 if ((MagickRealType) GetPixelBlue(image,q) < tone)
4179 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
4180 p+=GetPixelChannels(image);
4181 q+=GetPixelChannels(sepia_image);
4183 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4185 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4190 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4191 #pragma omp critical (MagickCore_SepiaToneImage)
4193 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4195 if (proceed == MagickFalse)
4199 sepia_view=DestroyCacheView(sepia_view);
4200 image_view=DestroyCacheView(image_view);
4201 (void) NormalizeImage(sepia_image,exception);
4202 (void) ContrastImage(sepia_image,MagickTrue,exception);
4203 if (status == MagickFalse)
4204 sepia_image=DestroyImage(sepia_image);
4205 return(sepia_image);
4209 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4213 % S h a d o w I m a g e %
4217 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4219 % ShadowImage() simulates a shadow from the specified image and returns it.
4221 % The format of the ShadowImage method is:
4223 % Image *ShadowImage(const Image *image,const double alpha,
4224 % const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4225 % ExceptionInfo *exception)
4227 % A description of each parameter follows:
4229 % o image: the image.
4231 % o alpha: percentage transparency.
4233 % o sigma: the standard deviation of the Gaussian, in pixels.
4235 % o x_offset: the shadow x-offset.
4237 % o y_offset: the shadow y-offset.
4239 % o exception: return any errors or warnings in this structure.
4242 MagickExport Image *ShadowImage(const Image *image,const double alpha,
4243 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4244 ExceptionInfo *exception)
4246 #define ShadowImageTag "Shadow/Image"
4268 assert(image != (Image *) NULL);
4269 assert(image->signature == MagickSignature);
4270 if (image->debug != MagickFalse)
4271 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4272 assert(exception != (ExceptionInfo *) NULL);
4273 assert(exception->signature == MagickSignature);
4274 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4275 if (clone_image == (Image *) NULL)
4276 return((Image *) NULL);
4277 if (IsGrayColorspace(image->colorspace) != MagickFalse)
4278 (void) TransformImageColorspace(clone_image,sRGBColorspace,exception);
4279 (void) SetImageVirtualPixelMethod(clone_image,EdgeVirtualPixelMethod,
4281 border_info.width=(size_t) floor(2.0*sigma+0.5);
4282 border_info.height=(size_t) floor(2.0*sigma+0.5);
4285 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4287 clone_image->matte=MagickTrue;
4288 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
4289 clone_image=DestroyImage(clone_image);
4290 if (border_image == (Image *) NULL)
4291 return((Image *) NULL);
4292 if (border_image->matte == MagickFalse)
4293 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
4298 image_view=AcquireAuthenticCacheView(border_image,exception);
4299 for (y=0; y < (ssize_t) border_image->rows; y++)
4310 if (status == MagickFalse)
4312 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
4314 if (q == (Quantum *) NULL)
4319 background_color=border_image->background_color;
4320 background_color.matte=MagickTrue;
4321 for (x=0; x < (ssize_t) border_image->columns; x++)
4323 if (border_image->matte != MagickFalse)
4324 background_color.alpha=GetPixelAlpha(border_image,q)*alpha/100.0;
4325 SetPixelInfoPixel(border_image,&background_color,q);
4326 q+=GetPixelChannels(border_image);
4328 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4331 image_view=DestroyCacheView(image_view);
4332 if (status == MagickFalse)
4334 border_image=DestroyImage(border_image);
4335 return((Image *) NULL);
4337 channel_mask=SetPixelChannelMask(border_image,AlphaChannel);
4338 shadow_image=BlurImage(border_image,0.0,sigma,exception);
4339 border_image=DestroyImage(border_image);
4340 if (shadow_image == (Image *) NULL)
4341 return((Image *) NULL);
4342 (void) SetPixelChannelMapMask(shadow_image,channel_mask);
4343 if (shadow_image->page.width == 0)
4344 shadow_image->page.width=shadow_image->columns;
4345 if (shadow_image->page.height == 0)
4346 shadow_image->page.height=shadow_image->rows;
4347 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4348 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4349 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4350 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
4351 return(shadow_image);
4355 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4359 % S k e t c h I m a g e %
4363 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4365 % SketchImage() simulates a pencil sketch. We convolve the image with a
4366 % Gaussian operator of the given radius and standard deviation (sigma). For
4367 % reasonable results, radius should be larger than sigma. Use a radius of 0
4368 % and SketchImage() selects a suitable radius for you. Angle gives the angle
4371 % The format of the SketchImage method is:
4373 % Image *SketchImage(const Image *image,const double radius,
4374 % const double sigma,const double angle,ExceptionInfo *exception)
4376 % A description of each parameter follows:
4378 % o image: the image.
4380 % o radius: the radius of the Gaussian, in pixels, not counting the
4383 % o sigma: the standard deviation of the Gaussian, in pixels.
4385 % o angle: apply the effect along this angle.
4387 % o exception: return any errors or warnings in this structure.
4390 MagickExport Image *SketchImage(const Image *image,const double radius,
4391 const double sigma,const double angle,ExceptionInfo *exception)
4408 **restrict random_info;
4416 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4417 MagickTrue,exception);
4418 if (random_image == (Image *) NULL)
4419 return((Image *) NULL);
4421 random_info=AcquireRandomInfoThreadSet();
4422 concurrent=GetRandomSecretKey(random_info[0]) == ~0UL ? MagickTrue :
4424 random_view=AcquireAuthenticCacheView(random_image,exception);
4425 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4426 #pragma omp parallel for schedule(static,4) shared(status) omp_concurrent(concurrent)
4428 for (y=0; y < (ssize_t) random_image->rows; y++)
4431 id = GetOpenMPThreadId();
4439 if (status == MagickFalse)
4441 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4443 if (q == (Quantum *) NULL)
4448 for (x=0; x < (ssize_t) random_image->columns; x++)
4456 if (GetPixelMask(random_image,q) != 0)
4458 q+=GetPixelChannels(random_image);
4461 value=GetPseudoRandomValue(random_info[id]);
4462 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4470 channel=GetPixelChannelMapChannel(image,i);
4471 traits=GetPixelChannelMapTraits(image,channel);
4472 if (traits == UndefinedPixelTrait)
4474 q[i]=ClampToQuantum(QuantumRange*value);
4476 q+=GetPixelChannels(random_image);
4478 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4481 random_view=DestroyCacheView(random_view);
4482 random_info=DestroyRandomInfoThreadSet(random_info);
4483 if (status == MagickFalse)
4485 random_image=DestroyImage(random_image);
4486 return(random_image);
4488 blur_image=MotionBlurImage(random_image,radius,sigma,angle,exception);
4489 random_image=DestroyImage(random_image);
4490 if (blur_image == (Image *) NULL)
4491 return((Image *) NULL);
4492 dodge_image=EdgeImage(blur_image,radius,1.0,exception);
4493 blur_image=DestroyImage(blur_image);
4494 if (dodge_image == (Image *) NULL)
4495 return((Image *) NULL);
4496 (void) NormalizeImage(dodge_image,exception);
4497 (void) NegateImage(dodge_image,MagickFalse,exception);
4498 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
4499 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4500 if (sketch_image == (Image *) NULL)
4502 dodge_image=DestroyImage(dodge_image);
4503 return((Image *) NULL);
4505 (void) CompositeImage(sketch_image,dodge_image,ColorDodgeCompositeOp,
4506 MagickTrue,0,0,exception);
4507 dodge_image=DestroyImage(dodge_image);
4508 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4509 if (blend_image == (Image *) NULL)
4511 sketch_image=DestroyImage(sketch_image);
4512 return((Image *) NULL);
4514 (void) SetImageArtifact(blend_image,"compose:args","20x80");
4515 (void) CompositeImage(sketch_image,blend_image,BlendCompositeOp,MagickTrue,
4517 blend_image=DestroyImage(blend_image);
4518 return(sketch_image);
4522 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4526 % S o l a r i z e I m a g e %
4530 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4532 % SolarizeImage() applies a special effect to the image, similar to the effect
4533 % achieved in a photo darkroom by selectively exposing areas of photo
4534 % sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4535 % measure of the extent of the solarization.
4537 % The format of the SolarizeImage method is:
4539 % MagickBooleanType SolarizeImage(Image *image,const double threshold,
4540 % ExceptionInfo *exception)
4542 % A description of each parameter follows:
4544 % o image: the image.
4546 % o threshold: Define the extent of the solarization.
4548 % o exception: return any errors or warnings in this structure.
4551 MagickExport MagickBooleanType SolarizeImage(Image *image,
4552 const double threshold,ExceptionInfo *exception)
4554 #define SolarizeImageTag "Solarize/Image"
4568 assert(image != (Image *) NULL);
4569 assert(image->signature == MagickSignature);
4570 if (image->debug != MagickFalse)
4571 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4572 if (image->storage_class == PseudoClass)
4580 for (i=0; i < (ssize_t) image->colors; i++)
4582 if ((MagickRealType) image->colormap[i].red > threshold)
4583 image->colormap[i].red=(Quantum) QuantumRange-image->colormap[i].red;
4584 if ((MagickRealType) image->colormap[i].green > threshold)
4585 image->colormap[i].green=(Quantum) QuantumRange-
4586 image->colormap[i].green;
4587 if ((MagickRealType) image->colormap[i].blue > threshold)
4588 image->colormap[i].blue=(Quantum) QuantumRange-
4589 image->colormap[i].blue;
4597 image_view=AcquireAuthenticCacheView(image,exception);
4598 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4599 #pragma omp parallel for schedule(static,4) shared(progress,status)
4601 for (y=0; y < (ssize_t) image->rows; y++)
4609 if (status == MagickFalse)
4611 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
4612 if (q == (Quantum *) NULL)
4617 for (x=0; x < (ssize_t) image->columns; x++)
4622 if (GetPixelMask(image,q) != 0)
4624 q+=GetPixelChannels(image);
4627 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4635 channel=GetPixelChannelMapChannel(image,i);
4636 traits=GetPixelChannelMapTraits(image,channel);
4637 if ((traits == UndefinedPixelTrait) ||
4638 ((traits & CopyPixelTrait) != 0))
4640 if ((MagickRealType) q[i] > threshold)
4641 q[i]=QuantumRange-q[i];
4643 q+=GetPixelChannels(image);
4645 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4647 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4652 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4653 #pragma omp critical (MagickCore_SolarizeImage)
4655 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4656 if (proceed == MagickFalse)
4660 image_view=DestroyCacheView(image_view);
4665 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4669 % S t e g a n o I m a g e %
4673 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4675 % SteganoImage() hides a digital watermark within the image. Recover
4676 % the hidden watermark later to prove that the authenticity of an image.
4677 % Offset defines the start position within the image to hide the watermark.
4679 % The format of the SteganoImage method is:
4681 % Image *SteganoImage(const Image *image,Image *watermark,
4682 % ExceptionInfo *exception)
4684 % A description of each parameter follows:
4686 % o image: the image.
4688 % o watermark: the watermark image.
4690 % o exception: return any errors or warnings in this structure.
4693 MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4694 ExceptionInfo *exception)
4696 #define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
4697 #define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
4698 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
4699 #define SteganoImageTag "Stegano/Image"
4734 Initialize steganographic image attributes.
4736 assert(image != (const Image *) NULL);
4737 assert(image->signature == MagickSignature);
4738 if (image->debug != MagickFalse)
4739 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4740 assert(watermark != (const Image *) NULL);
4741 assert(watermark->signature == MagickSignature);
4742 assert(exception != (ExceptionInfo *) NULL);
4743 assert(exception->signature == MagickSignature);
4745 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4746 if (stegano_image == (Image *) NULL)
4747 return((Image *) NULL);
4748 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
4749 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
4751 stegano_image=DestroyImage(stegano_image);
4752 return((Image *) NULL);
4755 Hide watermark in low-order bits of image.
4760 depth=stegano_image->depth;
4761 k=stegano_image->offset;
4763 watermark_view=AcquireVirtualCacheView(watermark,exception);
4764 stegano_view=AcquireAuthenticCacheView(stegano_image,exception);
4765 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
4767 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
4769 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
4774 (void) GetOneCacheViewVirtualPixelInfo(watermark_view,x,y,&pixel,
4776 offset=k/(ssize_t) stegano_image->columns;
4777 if (offset >= (ssize_t) stegano_image->rows)
4779 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4780 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4782 if (q == (Quantum *) NULL)
4788 SetPixelRed(stegano_image,SetBit(GetPixelRed(stegano_image,q),j,
4789 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
4794 SetPixelGreen(stegano_image,SetBit(GetPixelGreen(stegano_image,q),j,
4795 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
4800 SetPixelBlue(stegano_image,SetBit(GetPixelBlue(stegano_image,q),j,
4801 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
4805 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
4811 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
4813 if (k == stegano_image->offset)
4817 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4822 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4824 if (proceed == MagickFalse)
4828 stegano_view=DestroyCacheView(stegano_view);
4829 watermark_view=DestroyCacheView(watermark_view);
4830 if (status == MagickFalse)
4832 stegano_image=DestroyImage(stegano_image);
4833 return((Image *) NULL);
4835 return(stegano_image);
4839 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4843 % S t e r e o A n a g l y p h I m a g e %
4847 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4849 % StereoAnaglyphImage() combines two images and produces a single image that
4850 % is the composite of a left and right image of a stereo pair. Special
4851 % red-green stereo glasses are required to view this effect.
4853 % The format of the StereoAnaglyphImage method is:
4855 % Image *StereoImage(const Image *left_image,const Image *right_image,
4856 % ExceptionInfo *exception)
4857 % Image *StereoAnaglyphImage(const Image *left_image,
4858 % const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
4859 % ExceptionInfo *exception)
4861 % A description of each parameter follows:
4863 % o left_image: the left image.
4865 % o right_image: the right image.
4867 % o exception: return any errors or warnings in this structure.
4869 % o x_offset: amount, in pixels, by which the left image is offset to the
4870 % right of the right image.
4872 % o y_offset: amount, in pixels, by which the left image is offset to the
4873 % bottom of the right image.
4877 MagickExport Image *StereoImage(const Image *left_image,
4878 const Image *right_image,ExceptionInfo *exception)
4880 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4883 MagickExport Image *StereoAnaglyphImage(const Image *left_image,
4884 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
4885 ExceptionInfo *exception)
4887 #define StereoImageTag "Stereo/Image"
4901 assert(left_image != (const Image *) NULL);
4902 assert(left_image->signature == MagickSignature);
4903 if (left_image->debug != MagickFalse)
4904 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4905 left_image->filename);
4906 assert(right_image != (const Image *) NULL);
4907 assert(right_image->signature == MagickSignature);
4908 assert(exception != (ExceptionInfo *) NULL);
4909 assert(exception->signature == MagickSignature);
4910 assert(right_image != (const Image *) NULL);
4912 if ((left_image->columns != right_image->columns) ||
4913 (left_image->rows != right_image->rows))
4914 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4916 Initialize stereo image attributes.
4918 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4919 MagickTrue,exception);
4920 if (stereo_image == (Image *) NULL)
4921 return((Image *) NULL);
4922 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
4924 stereo_image=DestroyImage(stereo_image);
4925 return((Image *) NULL);
4928 Copy left image to red channel and right image to blue channel.
4931 for (y=0; y < (ssize_t) stereo_image->rows; y++)
4933 register const Quantum
4943 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4945 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4946 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
4947 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4948 (r == (Quantum *) NULL))
4950 for (x=0; x < (ssize_t) stereo_image->columns; x++)
4952 SetPixelRed(image,GetPixelRed(left_image,p),r);
4953 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4954 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4955 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4956 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4957 GetPixelAlpha(right_image,q))/2,r);
4958 p+=GetPixelChannels(left_image);
4959 q+=GetPixelChannels(right_image);
4960 r+=GetPixelChannels(stereo_image);
4962 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4964 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4969 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4970 stereo_image->rows);
4971 if (proceed == MagickFalse)
4975 if (status == MagickFalse)
4977 stereo_image=DestroyImage(stereo_image);
4978 return((Image *) NULL);
4980 return(stereo_image);
4984 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4988 % S w i r l I m a g e %
4992 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4994 % SwirlImage() swirls the pixels about the center of the image, where
4995 % degrees indicates the sweep of the arc through which each pixel is moved.
4996 % You get a more dramatic effect as the degrees move from 1 to 360.
4998 % The format of the SwirlImage method is:
5000 % Image *SwirlImage(const Image *image,double degrees,
5001 % const PixelInterpolateMethod method,ExceptionInfo *exception)
5003 % A description of each parameter follows:
5005 % o image: the image.
5007 % o degrees: Define the tightness of the swirling effect.
5009 % o method: the pixel interpolation method.
5011 % o exception: return any errors or warnings in this structure.
5014 MagickExport Image *SwirlImage(const Image *image,double degrees,
5015 const PixelInterpolateMethod method,ExceptionInfo *exception)
5017 #define SwirlImageTag "Swirl/Image"
5043 Initialize swirl image attributes.
5045 assert(image != (const Image *) NULL);
5046 assert(image->signature == MagickSignature);
5047 if (image->debug != MagickFalse)
5048 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5049 assert(exception != (ExceptionInfo *) NULL);
5050 assert(exception->signature == MagickSignature);
5051 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5052 if (swirl_image == (Image *) NULL)
5053 return((Image *) NULL);
5054 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
5056 swirl_image=DestroyImage(swirl_image);
5057 return((Image *) NULL);
5059 if (swirl_image->background_color.alpha != OpaqueAlpha)
5060 swirl_image->matte=MagickTrue;
5062 Compute scaling factor.
5064 center.x=(double) image->columns/2.0;
5065 center.y=(double) image->rows/2.0;
5066 radius=MagickMax(center.x,center.y);
5069 if (image->columns > image->rows)
5070 scale.y=(double) image->columns/(double) image->rows;
5072 if (image->columns < image->rows)
5073 scale.x=(double) image->rows/(double) image->columns;
5074 degrees=(double) DegreesToRadians(degrees);
5080 image_view=AcquireVirtualCacheView(image,exception);
5081 swirl_view=AcquireAuthenticCacheView(swirl_image,exception);
5082 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5083 #pragma omp parallel for schedule(static,4) shared(progress,status)
5085 for (y=0; y < (ssize_t) image->rows; y++)
5093 register const Quantum
5102 if (status == MagickFalse)
5104 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
5105 q=QueueCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
5107 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
5112 delta.y=scale.y*(double) (y-center.y);
5113 for (x=0; x < (ssize_t) image->columns; x++)
5116 Determine if the pixel is within an ellipse.
5118 if (GetPixelMask(image,p) != 0)
5120 p+=GetPixelChannels(image);
5121 q+=GetPixelChannels(swirl_image);
5124 delta.x=scale.x*(double) (x-center.x);
5125 distance=delta.x*delta.x+delta.y*delta.y;
5126 if (distance >= (radius*radius))
5131 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5140 channel=GetPixelChannelMapChannel(image,i);
5141 traits=GetPixelChannelMapTraits(image,channel);
5142 swirl_traits=GetPixelChannelMapTraits(swirl_image,channel);
5143 if ((traits == UndefinedPixelTrait) ||
5144 (swirl_traits == UndefinedPixelTrait))
5146 SetPixelChannel(swirl_image,channel,p[i],q);
5159 factor=1.0-sqrt((double) distance)/radius;
5160 sine=sin((double) (degrees*factor*factor));
5161 cosine=cos((double) (degrees*factor*factor));
5162 status=InterpolatePixelChannels(image,image_view,swirl_image,method,
5163 ((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5164 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
5166 p+=GetPixelChannels(image);
5167 q+=GetPixelChannels(swirl_image);
5169 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5171 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5176 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5177 #pragma omp critical (MagickCore_SwirlImage)
5179 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5180 if (proceed == MagickFalse)
5184 swirl_view=DestroyCacheView(swirl_view);
5185 image_view=DestroyCacheView(image_view);
5186 if (status == MagickFalse)
5187 swirl_image=DestroyImage(swirl_image);
5188 return(swirl_image);
5192 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5196 % T i n t I m a g e %
5200 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5202 % TintImage() applies a color vector to each pixel in the image. The length
5203 % of the vector is 0 for black and white and at its maximum for the midtones.
5204 % The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5206 % The format of the TintImage method is:
5208 % Image *TintImage(const Image *image,const char *blend,
5209 % const PixelInfo *tint,ExceptionInfo *exception)
5211 % A description of each parameter follows:
5213 % o image: the image.
5215 % o blend: A color value used for tinting.
5217 % o tint: A color value used for tinting.
5219 % o exception: return any errors or warnings in this structure.
5222 MagickExport Image *TintImage(const Image *image,const char *blend,
5223 const PixelInfo *tint,ExceptionInfo *exception)
5225 #define TintImageTag "Tint/Image"
5256 Allocate tint image.
5258 assert(image != (const Image *) NULL);
5259 assert(image->signature == MagickSignature);
5260 if (image->debug != MagickFalse)
5261 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5262 assert(exception != (ExceptionInfo *) NULL);
5263 assert(exception->signature == MagickSignature);
5264 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5265 if (tint_image == (Image *) NULL)
5266 return((Image *) NULL);
5267 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
5269 tint_image=DestroyImage(tint_image);
5270 return((Image *) NULL);
5272 if (blend == (const char *) NULL)
5275 Determine RGB values of the color.
5277 GetPixelInfo(image,&color_vector);
5278 flags=ParseGeometry(blend,&geometry_info);
5279 color_vector.red=geometry_info.rho;
5280 color_vector.green=geometry_info.rho;
5281 color_vector.blue=geometry_info.rho;
5282 color_vector.alpha=OpaqueAlpha;
5283 if ((flags & SigmaValue) != 0)
5284 color_vector.green=geometry_info.sigma;
5285 if ((flags & XiValue) != 0)
5286 color_vector.blue=geometry_info.xi;
5287 if ((flags & PsiValue) != 0)
5288 color_vector.alpha=geometry_info.psi;
5289 if (image->colorspace == CMYKColorspace)
5291 color_vector.black=geometry_info.rho;
5292 if ((flags & PsiValue) != 0)
5293 color_vector.black=geometry_info.psi;
5294 if ((flags & ChiValue) != 0)
5295 color_vector.alpha=geometry_info.chi;
5297 intensity=(MagickRealType) GetPixelInfoIntensity(tint);
5298 color_vector.red=(MagickRealType) (color_vector.red*tint->red/100.0-
5300 color_vector.green=(MagickRealType) (color_vector.green*tint->green/100.0-
5302 color_vector.blue=(MagickRealType) (color_vector.blue*tint->blue/100.0-
5304 color_vector.black=(MagickRealType) (color_vector.black*tint->black/100.0-
5306 color_vector.alpha=(MagickRealType) (color_vector.alpha*tint->alpha/100.0-
5313 image_view=AcquireVirtualCacheView(image,exception);
5314 tint_view=AcquireAuthenticCacheView(tint_image,exception);
5315 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5316 #pragma omp parallel for schedule(static,4) shared(progress,status)
5318 for (y=0; y < (ssize_t) image->rows; y++)
5320 register const Quantum
5329 if (status == MagickFalse)
5331 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5332 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5334 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
5339 for (x=0; x < (ssize_t) image->columns; x++)
5350 if (GetPixelMask(image,p) != 0)
5352 p+=GetPixelChannels(image);
5353 q+=GetPixelChannels(tint_image);
5356 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5365 channel=GetPixelChannelMapChannel(image,i);
5366 traits=GetPixelChannelMapTraits(image,channel);
5367 tint_traits=GetPixelChannelMapTraits(tint_image,channel);
5368 if ((traits == UndefinedPixelTrait) ||
5369 (tint_traits == UndefinedPixelTrait))
5371 if ((tint_traits & CopyPixelTrait) != 0)
5373 SetPixelChannel(tint_image,channel,p[i],q);
5377 GetPixelInfo(image,&pixel);
5378 weight=QuantumScale*GetPixelRed(image,p)-0.5;
5379 pixel.red=(MagickRealType) GetPixelRed(image,p)+color_vector.red*
5380 (1.0-(4.0*(weight*weight)));
5381 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
5382 pixel.green=(MagickRealType) GetPixelGreen(image,p)+color_vector.green*
5383 (1.0-(4.0*(weight*weight)));
5384 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
5385 pixel.blue=(MagickRealType) GetPixelBlue(image,p)+color_vector.blue*
5386 (1.0-(4.0*(weight*weight)));
5387 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
5388 pixel.black=(MagickRealType) GetPixelBlack(image,p)+color_vector.black*
5389 (1.0-(4.0*(weight*weight)));
5390 SetPixelInfoPixel(tint_image,&pixel,q);
5391 p+=GetPixelChannels(image);
5392 q+=GetPixelChannels(tint_image);
5394 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5396 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5401 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5402 #pragma omp critical (MagickCore_TintImage)
5404 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5405 if (proceed == MagickFalse)
5409 tint_view=DestroyCacheView(tint_view);
5410 image_view=DestroyCacheView(image_view);
5411 if (status == MagickFalse)
5412 tint_image=DestroyImage(tint_image);
5417 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5421 % V i g n e t t e I m a g e %
5425 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5427 % VignetteImage() softens the edges of the image in vignette style.
5429 % The format of the VignetteImage method is:
5431 % Image *VignetteImage(const Image *image,const double radius,
5432 % const double sigma,const ssize_t x,const ssize_t y,
5433 % ExceptionInfo *exception)
5435 % A description of each parameter follows:
5437 % o image: the image.
5439 % o radius: the radius of the pixel neighborhood.
5441 % o sigma: the standard deviation of the Gaussian, in pixels.
5443 % o x, y: Define the x and y ellipse offset.
5445 % o exception: return any errors or warnings in this structure.
5448 MagickExport Image *VignetteImage(const Image *image,const double radius,
5449 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
5452 ellipse[MaxTextExtent];
5463 assert(image != (Image *) NULL);
5464 assert(image->signature == MagickSignature);
5465 if (image->debug != MagickFalse)
5466 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5467 assert(exception != (ExceptionInfo *) NULL);
5468 assert(exception->signature == MagickSignature);
5469 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5470 if (canvas_image == (Image *) NULL)
5471 return((Image *) NULL);
5472 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
5474 canvas_image=DestroyImage(canvas_image);
5475 return((Image *) NULL);
5477 canvas_image->matte=MagickTrue;
5478 oval_image=CloneImage(canvas_image,canvas_image->columns,canvas_image->rows,
5479 MagickTrue,exception);
5480 if (oval_image == (Image *) NULL)
5482 canvas_image=DestroyImage(canvas_image);
5483 return((Image *) NULL);
5485 (void) QueryColorCompliance("#000000",AllCompliance,
5486 &oval_image->background_color,exception);
5487 (void) SetImageBackgroundColor(oval_image,exception);
5488 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
5489 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5491 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5493 (void) FormatLocaleString(ellipse,MaxTextExtent,"ellipse %g,%g,%g,%g,"
5494 "0.0,360.0",image->columns/2.0,image->rows/2.0,image->columns/2.0-x,
5496 draw_info->primitive=AcquireString(ellipse);
5497 (void) DrawImage(oval_image,draw_info,exception);
5498 draw_info=DestroyDrawInfo(draw_info);
5499 blur_image=BlurImage(oval_image,radius,sigma,exception);
5500 oval_image=DestroyImage(oval_image);
5501 if (blur_image == (Image *) NULL)
5503 canvas_image=DestroyImage(canvas_image);
5504 return((Image *) NULL);
5506 blur_image->matte=MagickFalse;
5507 (void) CompositeImage(canvas_image,blur_image,IntensityCompositeOp,MagickTrue,
5509 blur_image=DestroyImage(blur_image);
5510 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5511 canvas_image=DestroyImage(canvas_image);
5512 return(vignette_image);
5516 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5520 % W a v e I m a g e %
5524 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5526 % WaveImage() creates a "ripple" effect in the image by shifting the pixels
5527 % vertically along a sine wave whose amplitude and wavelength is specified
5528 % by the given parameters.
5530 % The format of the WaveImage method is:
5532 % Image *WaveImage(const Image *image,const double amplitude,
5533 % const double wave_length,const PixelInterpolateMethod method,
5534 % ExceptionInfo *exception)
5536 % A description of each parameter follows:
5538 % o image: the image.
5540 % o amplitude, wave_length: Define the amplitude and wave length of the
5543 % o interpolate: the pixel interpolation method.
5545 % o exception: return any errors or warnings in this structure.
5548 MagickExport Image *WaveImage(const Image *image,const double amplitude,
5549 const double wave_length,const PixelInterpolateMethod method,
5550 ExceptionInfo *exception)
5552 #define WaveImageTag "Wave/Image"
5577 Initialize wave image attributes.
5579 assert(image != (Image *) NULL);
5580 assert(image->signature == MagickSignature);
5581 if (image->debug != MagickFalse)
5582 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5583 assert(exception != (ExceptionInfo *) NULL);
5584 assert(exception->signature == MagickSignature);
5585 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
5586 fabs(amplitude)),MagickTrue,exception);
5587 if (wave_image == (Image *) NULL)
5588 return((Image *) NULL);
5589 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
5591 wave_image=DestroyImage(wave_image);
5592 return((Image *) NULL);
5594 if (wave_image->background_color.alpha != OpaqueAlpha)
5595 wave_image->matte=MagickTrue;
5599 sine_map=(MagickRealType *) AcquireQuantumMemory((size_t) wave_image->columns,
5601 if (sine_map == (MagickRealType *) NULL)
5603 wave_image=DestroyImage(wave_image);
5604 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5606 for (i=0; i < (ssize_t) wave_image->columns; i++)
5607 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5614 image_view=AcquireVirtualCacheView(image,exception);
5615 wave_view=AcquireAuthenticCacheView(wave_image,exception);
5616 (void) SetCacheViewVirtualPixelMethod(image_view,
5617 BackgroundVirtualPixelMethod);
5618 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5619 #pragma omp parallel for schedule(static,4) shared(progress,status)
5621 for (y=0; y < (ssize_t) wave_image->rows; y++)
5629 if (status == MagickFalse)
5631 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5633 if (q == (Quantum *) NULL)
5638 for (x=0; x < (ssize_t) wave_image->columns; x++)
5640 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5641 (double) x,(double) (y-sine_map[x]),q,exception);
5642 q+=GetPixelChannels(wave_image);
5644 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5646 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5651 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5652 #pragma omp critical (MagickCore_WaveImage)
5654 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5655 if (proceed == MagickFalse)
5659 wave_view=DestroyCacheView(wave_view);
5660 image_view=DestroyCacheView(image_view);
5661 sine_map=(MagickRealType *) RelinquishMagickMemory(sine_map);
5662 if (status == MagickFalse)
5663 wave_image=DestroyImage(wave_image);