2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % MagickCore Image Special Effects Methods %
20 % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "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/composite.h"
52 #include "MagickCore/decorate.h"
53 #include "MagickCore/draw.h"
54 #include "MagickCore/effect.h"
55 #include "MagickCore/enhance.h"
56 #include "MagickCore/exception.h"
57 #include "MagickCore/exception-private.h"
58 #include "MagickCore/fx.h"
59 #include "MagickCore/fx-private.h"
60 #include "MagickCore/gem.h"
61 #include "MagickCore/gem-private.h"
62 #include "MagickCore/geometry.h"
63 #include "MagickCore/layer.h"
64 #include "MagickCore/list.h"
65 #include "MagickCore/log.h"
66 #include "MagickCore/image.h"
67 #include "MagickCore/image-private.h"
68 #include "MagickCore/magick.h"
69 #include "MagickCore/memory_.h"
70 #include "MagickCore/monitor.h"
71 #include "MagickCore/monitor-private.h"
72 #include "MagickCore/option.h"
73 #include "MagickCore/pixel.h"
74 #include "MagickCore/pixel-accessor.h"
75 #include "MagickCore/property.h"
76 #include "MagickCore/quantum.h"
77 #include "MagickCore/quantum-private.h"
78 #include "MagickCore/random_.h"
79 #include "MagickCore/random-private.h"
80 #include "MagickCore/resample.h"
81 #include "MagickCore/resample-private.h"
82 #include "MagickCore/resize.h"
83 #include "MagickCore/shear.h"
84 #include "MagickCore/splay-tree.h"
85 #include "MagickCore/statistic.h"
86 #include "MagickCore/string_.h"
87 #include "MagickCore/string-private.h"
88 #include "MagickCore/thread-private.h"
89 #include "MagickCore/transform.h"
90 #include "MagickCore/utility.h"
95 #define LeftShiftOperator 0xf5
96 #define RightShiftOperator 0xf6
97 #define LessThanEqualOperator 0xf7
98 #define GreaterThanEqualOperator 0xf8
99 #define EqualOperator 0xf9
100 #define NotEqualOperator 0xfa
101 #define LogicalAndOperator 0xfb
102 #define LogicalOrOperator 0xfc
103 #define ExponentialNotation 0xfd
131 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
135 + A c q u i r e F x I n f o %
139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141 % AcquireFxInfo() allocates the FxInfo structure.
143 % The format of the AcquireFxInfo method is:
145 % FxInfo *AcquireFxInfo(Image *image,const char *expression)
147 % A description of each parameter follows:
149 % o image: the image.
151 % o expression: the expression.
154 MagickPrivate FxInfo *AcquireFxInfo(const Image *image,const char *expression)
168 fx_info=(FxInfo *) AcquireMagickMemory(sizeof(*fx_info));
169 if (fx_info == (FxInfo *) NULL)
170 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
171 (void) ResetMagickMemory(fx_info,0,sizeof(*fx_info));
172 fx_info->exception=AcquireExceptionInfo();
173 fx_info->images=image;
174 fx_info->colors=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
175 RelinquishMagickMemory);
176 fx_info->symbols=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
177 RelinquishMagickMemory);
178 fx_info->view=(CacheView **) AcquireQuantumMemory(GetImageListLength(
179 fx_info->images),sizeof(*fx_info->view));
180 if (fx_info->view == (CacheView **) NULL)
181 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
183 next=GetFirstImageInList(fx_info->images);
184 for ( ; next != (Image *) NULL; next=next->next)
186 fx_info->view[i]=AcquireCacheView(next);
189 fx_info->random_info=AcquireRandomInfo();
190 fx_info->expression=ConstantString(expression);
191 fx_info->file=stderr;
192 (void) SubstituteString(&fx_info->expression," ",""); /* compact string */
194 Force right-to-left associativity for unary negation.
196 (void) SubstituteString(&fx_info->expression,"-","-1.0*");
198 Convert complex to simple operators.
201 *fx_op=(char) LeftShiftOperator;
202 (void) SubstituteString(&fx_info->expression,"<<",fx_op);
203 *fx_op=(char) RightShiftOperator;
204 (void) SubstituteString(&fx_info->expression,">>",fx_op);
205 *fx_op=(char) LessThanEqualOperator;
206 (void) SubstituteString(&fx_info->expression,"<=",fx_op);
207 *fx_op=(char) GreaterThanEqualOperator;
208 (void) SubstituteString(&fx_info->expression,">=",fx_op);
209 *fx_op=(char) EqualOperator;
210 (void) SubstituteString(&fx_info->expression,"==",fx_op);
211 *fx_op=(char) NotEqualOperator;
212 (void) SubstituteString(&fx_info->expression,"!=",fx_op);
213 *fx_op=(char) LogicalAndOperator;
214 (void) SubstituteString(&fx_info->expression,"&&",fx_op);
215 *fx_op=(char) LogicalOrOperator;
216 (void) SubstituteString(&fx_info->expression,"||",fx_op);
217 *fx_op=(char) ExponentialNotation;
218 (void) SubstituteString(&fx_info->expression,"**",fx_op);
223 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227 % A d d N o i s e I m a g e %
231 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233 % AddNoiseImage() adds random noise to the image.
235 % The format of the AddNoiseImage method is:
237 % Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
238 % const double attenuate,ExceptionInfo *exception)
240 % A description of each parameter follows:
242 % o image: the image.
244 % o channel: the channel type.
246 % o noise_type: The type of noise: Uniform, Gaussian, Multiplicative,
247 % Impulse, Laplacian, or Poisson.
249 % o attenuate: attenuate the random distribution.
251 % o exception: return any errors or warnings in this structure.
254 MagickExport Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
255 const double attenuate,ExceptionInfo *exception)
257 #define AddNoiseImageTag "AddNoise/Image"
273 **restrict random_info;
279 Initialize noise image attributes.
281 assert(image != (const Image *) NULL);
282 assert(image->signature == MagickSignature);
283 if (image->debug != MagickFalse)
284 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
285 assert(exception != (ExceptionInfo *) NULL);
286 assert(exception->signature == MagickSignature);
287 noise_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
288 if (noise_image == (Image *) NULL)
289 return((Image *) NULL);
290 if (SetImageStorageClass(noise_image,DirectClass,exception) == MagickFalse)
292 noise_image=DestroyImage(noise_image);
293 return((Image *) NULL);
296 Add noise in each row.
300 random_info=AcquireRandomInfoThreadSet();
301 image_view=AcquireCacheView(image);
302 noise_view=AcquireCacheView(noise_image);
303 #if defined(MAGICKCORE_OPENMP_SUPPORT)
304 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
306 for (y=0; y < (ssize_t) image->rows; y++)
309 id = GetOpenMPThreadId();
314 register const Quantum
323 if (status == MagickFalse)
325 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
326 q=GetCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
328 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
333 for (x=0; x < (ssize_t) image->columns; x++)
338 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
347 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
348 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
349 noise_traits=GetPixelChannelMapTraits(noise_image,channel);
350 if ((traits == UndefinedPixelTrait) ||
351 (noise_traits == UndefinedPixelTrait))
353 if ((noise_traits & CopyPixelTrait) != 0)
355 SetPixelChannel(noise_image,channel,p[i],q);
358 SetPixelChannel(noise_image,channel,ClampToQuantum(
359 GenerateDifferentialNoise(random_info[id],p[i],noise_type,attenuate)),
362 p+=GetPixelChannels(image);
363 q+=GetPixelChannels(noise_image);
365 sync=SyncCacheViewAuthenticPixels(noise_view,exception);
366 if (sync == MagickFalse)
368 if (image->progress_monitor != (MagickProgressMonitor) NULL)
373 #if defined(MAGICKCORE_OPENMP_SUPPORT)
374 #pragma omp critical (MagickCore_AddNoiseImage)
376 proceed=SetImageProgress(image,AddNoiseImageTag,progress++,
378 if (proceed == MagickFalse)
382 noise_view=DestroyCacheView(noise_view);
383 image_view=DestroyCacheView(image_view);
384 random_info=DestroyRandomInfoThreadSet(random_info);
385 if (status == MagickFalse)
386 noise_image=DestroyImage(noise_image);
391 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
395 % B l u e S h i f t I m a g e %
399 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
401 % BlueShiftImage() mutes the colors of the image to simulate a scene at
402 % nighttime in the moonlight.
404 % The format of the BlueShiftImage method is:
406 % Image *BlueShiftImage(const Image *image,const double factor,
407 % ExceptionInfo *exception)
409 % A description of each parameter follows:
411 % o image: the image.
413 % o factor: the shift factor.
415 % o exception: return any errors or warnings in this structure.
418 MagickExport Image *BlueShiftImage(const Image *image,const double factor,
419 ExceptionInfo *exception)
421 #define BlueShiftImageTag "BlueShift/Image"
440 Allocate blue shift image.
442 assert(image != (const Image *) NULL);
443 assert(image->signature == MagickSignature);
444 if (image->debug != MagickFalse)
445 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
446 assert(exception != (ExceptionInfo *) NULL);
447 assert(exception->signature == MagickSignature);
448 shift_image=CloneImage(image,image->columns,image->rows,MagickTrue,
450 if (shift_image == (Image *) NULL)
451 return((Image *) NULL);
452 if (SetImageStorageClass(shift_image,DirectClass,exception) == MagickFalse)
454 shift_image=DestroyImage(shift_image);
455 return((Image *) NULL);
458 Blue-shift DirectClass image.
462 image_view=AcquireCacheView(image);
463 shift_view=AcquireCacheView(shift_image);
464 #if defined(MAGICKCORE_OPENMP_SUPPORT)
465 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
467 for (y=0; y < (ssize_t) image->rows; y++)
478 register const Quantum
487 if (status == MagickFalse)
489 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
490 q=QueueCacheViewAuthenticPixels(shift_view,0,y,shift_image->columns,1,
492 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
497 for (x=0; x < (ssize_t) image->columns; x++)
499 quantum=GetPixelRed(image,p);
500 if (GetPixelGreen(image,p) < quantum)
501 quantum=GetPixelGreen(image,p);
502 if (GetPixelBlue(image,p) < quantum)
503 quantum=GetPixelBlue(image,p);
504 pixel.red=0.5*(GetPixelRed(image,p)+factor*quantum);
505 pixel.green=0.5*(GetPixelGreen(image,p)+factor*quantum);
506 pixel.blue=0.5*(GetPixelBlue(image,p)+factor*quantum);
507 quantum=GetPixelRed(image,p);
508 if (GetPixelGreen(image,p) > quantum)
509 quantum=GetPixelGreen(image,p);
510 if (GetPixelBlue(image,p) > quantum)
511 quantum=GetPixelBlue(image,p);
512 pixel.red=0.5*(pixel.red+factor*quantum);
513 pixel.green=0.5*(pixel.green+factor*quantum);
514 pixel.blue=0.5*(pixel.blue+factor*quantum);
515 SetPixelRed(shift_image,ClampToQuantum(pixel.red),q);
516 SetPixelGreen(shift_image,ClampToQuantum(pixel.green),q);
517 SetPixelBlue(shift_image,ClampToQuantum(pixel.blue),q);
518 p+=GetPixelChannels(image);
519 q+=GetPixelChannels(shift_image);
521 sync=SyncCacheViewAuthenticPixels(shift_view,exception);
522 if (sync == MagickFalse)
524 if (image->progress_monitor != (MagickProgressMonitor) NULL)
529 #if defined(MAGICKCORE_OPENMP_SUPPORT)
530 #pragma omp critical (MagickCore_BlueShiftImage)
532 proceed=SetImageProgress(image,BlueShiftImageTag,progress++,
534 if (proceed == MagickFalse)
538 image_view=DestroyCacheView(image_view);
539 shift_view=DestroyCacheView(shift_view);
540 if (status == MagickFalse)
541 shift_image=DestroyImage(shift_image);
546 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
550 % C h a r c o a l I m a g e %
554 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
556 % CharcoalImage() creates a new image that is a copy of an existing one with
557 % the edge highlighted. It allocates the memory necessary for the new Image
558 % structure and returns a pointer to the new image.
560 % The format of the CharcoalImage method is:
562 % Image *CharcoalImage(const Image *image,const double radius,
563 % const double sigma,const double bias,ExceptionInfo *exception)
565 % A description of each parameter follows:
567 % o image: the image.
569 % o radius: the radius of the pixel neighborhood.
571 % o sigma: the standard deviation of the Gaussian, in pixels.
575 % o exception: return any errors or warnings in this structure.
578 MagickExport Image *CharcoalImage(const Image *image,const double radius,
579 const double sigma,const double bias,ExceptionInfo *exception)
586 assert(image != (Image *) NULL);
587 assert(image->signature == MagickSignature);
588 if (image->debug != MagickFalse)
589 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
590 assert(exception != (ExceptionInfo *) NULL);
591 assert(exception->signature == MagickSignature);
592 clone_image=CloneImage(image,0,0,MagickTrue,exception);
593 if (clone_image == (Image *) NULL)
594 return((Image *) NULL);
595 (void) SetImageType(clone_image,GrayscaleType,exception);
596 edge_image=EdgeImage(clone_image,radius,sigma,exception);
597 clone_image=DestroyImage(clone_image);
598 if (edge_image == (Image *) NULL)
599 return((Image *) NULL);
600 charcoal_image=BlurImage(edge_image,radius,sigma,bias,exception);
601 edge_image=DestroyImage(edge_image);
602 if (charcoal_image == (Image *) NULL)
603 return((Image *) NULL);
604 (void) NormalizeImage(charcoal_image,exception);
605 (void) NegateImage(charcoal_image,MagickFalse,exception);
606 (void) SetImageType(charcoal_image,GrayscaleType,exception);
607 return(charcoal_image);
611 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
615 % C o l o r i z e I m a g e %
619 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
621 % ColorizeImage() blends the fill color with each pixel in the image.
622 % A percentage blend is specified with opacity. Control the application
623 % of different color components by specifying a different percentage for
624 % each component (e.g. 90/100/10 is 90% red, 100% green, and 10% blue).
626 % The format of the ColorizeImage method is:
628 % Image *ColorizeImage(const Image *image,const char *blend,
629 % const PixelInfo *colorize,ExceptionInfo *exception)
631 % A description of each parameter follows:
633 % o image: the image.
635 % o blend: A character string indicating the level of blending as a
638 % o colorize: A color value.
640 % o exception: return any errors or warnings in this structure.
643 MagickExport Image *ColorizeImage(const Image *image,const char *blend,
644 const PixelInfo *colorize,ExceptionInfo *exception)
646 #define ColorizeImageTag "Colorize/Image"
674 Allocate colorized image.
676 assert(image != (const Image *) NULL);
677 assert(image->signature == MagickSignature);
678 if (image->debug != MagickFalse)
679 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
680 assert(exception != (ExceptionInfo *) NULL);
681 assert(exception->signature == MagickSignature);
682 colorize_image=CloneImage(image,image->columns,image->rows,MagickTrue,
684 if (colorize_image == (Image *) NULL)
685 return((Image *) NULL);
686 if (SetImageStorageClass(colorize_image,DirectClass,exception) == MagickFalse)
688 colorize_image=DestroyImage(colorize_image);
689 return((Image *) NULL);
691 if (blend == (const char *) NULL)
692 return(colorize_image);
694 Determine RGB values of the pen color.
696 GetPixelInfo(image,&pixel);
697 flags=ParseGeometry(blend,&geometry_info);
698 pixel.red=geometry_info.rho;
699 pixel.green=geometry_info.rho;
700 pixel.blue=geometry_info.rho;
702 if ((flags & SigmaValue) != 0)
703 pixel.green=geometry_info.sigma;
704 if ((flags & XiValue) != 0)
705 pixel.blue=geometry_info.xi;
706 if ((flags & PsiValue) != 0)
707 pixel.alpha=geometry_info.psi;
708 if (pixel.colorspace == CMYKColorspace)
710 pixel.black=geometry_info.rho;
711 if ((flags & PsiValue) != 0)
712 pixel.black=geometry_info.psi;
713 if ((flags & ChiValue) != 0)
714 pixel.alpha=geometry_info.chi;
717 Colorize DirectClass image.
721 image_view=AcquireCacheView(image);
722 colorize_view=AcquireCacheView(colorize_image);
723 #if defined(MAGICKCORE_OPENMP_SUPPORT)
724 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
726 for (y=0; y < (ssize_t) image->rows; y++)
731 register const Quantum
740 if (status == MagickFalse)
742 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
743 q=QueueCacheViewAuthenticPixels(colorize_view,0,y,colorize_image->columns,1,
745 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
750 for (x=0; x < (ssize_t) image->columns; x++)
755 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
764 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
765 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
766 colorize_traits=GetPixelChannelMapTraits(colorize_image,channel);
767 if ((traits == UndefinedPixelTrait) ||
768 (colorize_traits == UndefinedPixelTrait))
770 if ((colorize_traits & CopyPixelTrait) != 0)
772 SetPixelChannel(colorize_image,channel,p[i],q);
777 case RedPixelChannel:
779 SetPixelChannel(colorize_image,channel,ClampToQuantum((p[i]*
780 (100.0-pixel.red)+colorize->red*pixel.red)/100.0),q);
783 case GreenPixelChannel:
785 SetPixelChannel(colorize_image,channel,ClampToQuantum((p[i]*
786 (100.0-pixel.green)+colorize->green*pixel.green)/100.0),q);
789 case BluePixelChannel:
791 SetPixelChannel(colorize_image,channel,ClampToQuantum((p[i]*
792 (100.0-pixel.blue)+colorize->blue*pixel.blue)/100.0),q);
795 case BlackPixelChannel:
797 SetPixelChannel(colorize_image,channel,ClampToQuantum((p[i]*
798 (100.0-pixel.black)+colorize->black*pixel.black)/100.0),q);
801 case AlphaPixelChannel:
803 SetPixelChannel(colorize_image,channel,ClampToQuantum((p[i]*
804 (100.0-pixel.alpha)+colorize->alpha*pixel.alpha)/100.0),q);
809 SetPixelChannel(colorize_image,channel,p[i],q);
814 p+=GetPixelChannels(image);
815 q+=GetPixelChannels(colorize_image);
817 sync=SyncCacheViewAuthenticPixels(colorize_view,exception);
818 if (sync == MagickFalse)
820 if (image->progress_monitor != (MagickProgressMonitor) NULL)
825 #if defined(MAGICKCORE_OPENMP_SUPPORT)
826 #pragma omp critical (MagickCore_ColorizeImage)
828 proceed=SetImageProgress(image,ColorizeImageTag,progress++,image->rows);
829 if (proceed == MagickFalse)
833 image_view=DestroyCacheView(image_view);
834 colorize_view=DestroyCacheView(colorize_view);
835 if (status == MagickFalse)
836 colorize_image=DestroyImage(colorize_image);
837 return(colorize_image);
841 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
845 % C o l o r M a t r i x I m a g e %
849 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
851 % ColorMatrixImage() applies color transformation to an image. This method
852 % permits saturation changes, hue rotation, luminance to alpha, and various
853 % other effects. Although variable-sized transformation matrices can be used,
854 % typically one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA
855 % (or RGBA with offsets). The matrix is similar to those used by Adobe Flash
856 % except offsets are in column 6 rather than 5 (in support of CMYKA images)
857 % and offsets are normalized (divide Flash offset by 255).
859 % The format of the ColorMatrixImage method is:
861 % Image *ColorMatrixImage(const Image *image,
862 % const KernelInfo *color_matrix,ExceptionInfo *exception)
864 % A description of each parameter follows:
866 % o image: the image.
868 % o color_matrix: the color matrix.
870 % o exception: return any errors or warnings in this structure.
873 MagickExport Image *ColorMatrixImage(const Image *image,
874 const KernelInfo *color_matrix,ExceptionInfo *exception)
876 #define ColorMatrixImageTag "ColorMatrix/Image"
885 { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
886 { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
887 { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
888 { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
889 { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
890 { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }
913 assert(image != (Image *) NULL);
914 assert(image->signature == MagickSignature);
915 if (image->debug != MagickFalse)
916 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
917 assert(exception != (ExceptionInfo *) NULL);
918 assert(exception->signature == MagickSignature);
920 for (v=0; v < (ssize_t) color_matrix->height; v++)
921 for (u=0; u < (ssize_t) color_matrix->width; u++)
923 if ((v < 6) && (u < 6))
924 ColorMatrix[v][u]=color_matrix->values[i];
928 Initialize color image.
930 color_image=CloneImage(image,0,0,MagickTrue,exception);
931 if (color_image == (Image *) NULL)
932 return((Image *) NULL);
933 if (SetImageStorageClass(color_image,DirectClass,exception) == MagickFalse)
935 color_image=DestroyImage(color_image);
936 return((Image *) NULL);
938 if (image->debug != MagickFalse)
941 format[MaxTextExtent],
944 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
945 " ColorMatrix image with color matrix:");
946 message=AcquireString("");
947 for (v=0; v < 6; v++)
950 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
951 (void) ConcatenateString(&message,format);
952 for (u=0; u < 6; u++)
954 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",
956 (void) ConcatenateString(&message,format);
958 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
960 message=DestroyString(message);
967 image_view=AcquireCacheView(image);
968 color_view=AcquireCacheView(color_image);
969 #if defined(MAGICKCORE_OPENMP_SUPPORT)
970 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
972 for (y=0; y < (ssize_t) image->rows; y++)
977 register const Quantum
986 if (status == MagickFalse)
988 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
989 q=GetCacheViewAuthenticPixels(color_view,0,y,color_image->columns,1,
991 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
996 for (x=0; x < (ssize_t) image->columns; x++)
1004 height=color_matrix->height > 6 ? 6UL : color_matrix->height;
1005 for (v=0; v < (ssize_t) height; v++)
1007 pixel=ColorMatrix[v][0]*GetPixelRed(image,p)+ColorMatrix[v][1]*
1008 GetPixelGreen(image,p)+ColorMatrix[v][2]*GetPixelBlue(image,p);
1009 if (image->colorspace == CMYKColorspace)
1010 pixel+=ColorMatrix[v][3]*GetPixelBlack(image,p);
1011 if (image->matte != MagickFalse)
1012 pixel+=ColorMatrix[v][4]*GetPixelAlpha(image,p);
1013 pixel+=QuantumRange*ColorMatrix[v][5];
1016 case 0: SetPixelRed(color_image,ClampToQuantum(pixel),q); break;
1017 case 1: SetPixelGreen(color_image,ClampToQuantum(pixel),q); break;
1018 case 2: SetPixelBlue(color_image,ClampToQuantum(pixel),q); break;
1021 if (image->colorspace == CMYKColorspace)
1022 SetPixelBlack(color_image,ClampToQuantum(pixel),q);
1027 if (image->matte != MagickFalse)
1028 SetPixelAlpha(color_image,ClampToQuantum(pixel),q);
1033 p+=GetPixelChannels(image);
1034 q+=GetPixelChannels(color_image);
1036 if (SyncCacheViewAuthenticPixels(color_view,exception) == MagickFalse)
1038 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1043 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1044 #pragma omp critical (MagickCore_ColorMatrixImage)
1046 proceed=SetImageProgress(image,ColorMatrixImageTag,progress++,
1048 if (proceed == MagickFalse)
1052 color_view=DestroyCacheView(color_view);
1053 image_view=DestroyCacheView(image_view);
1054 if (status == MagickFalse)
1055 color_image=DestroyImage(color_image);
1056 return(color_image);
1060 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1064 + D e s t r o y F x I n f o %
1068 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1070 % DestroyFxInfo() deallocates memory associated with an FxInfo structure.
1072 % The format of the DestroyFxInfo method is:
1074 % ImageInfo *DestroyFxInfo(ImageInfo *fx_info)
1076 % A description of each parameter follows:
1078 % o fx_info: the fx info.
1081 MagickPrivate FxInfo *DestroyFxInfo(FxInfo *fx_info)
1086 fx_info->exception=DestroyExceptionInfo(fx_info->exception);
1087 fx_info->expression=DestroyString(fx_info->expression);
1088 fx_info->symbols=DestroySplayTree(fx_info->symbols);
1089 fx_info->colors=DestroySplayTree(fx_info->colors);
1090 for (i=(ssize_t) GetImageListLength(fx_info->images)-1; i >= 0; i--)
1091 fx_info->view[i]=DestroyCacheView(fx_info->view[i]);
1092 fx_info->view=(CacheView **) RelinquishMagickMemory(fx_info->view);
1093 fx_info->random_info=DestroyRandomInfo(fx_info->random_info);
1094 fx_info=(FxInfo *) RelinquishMagickMemory(fx_info);
1099 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1103 + 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 %
1107 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1109 % FxEvaluateChannelExpression() evaluates an expression and returns the
1112 % The format of the FxEvaluateExpression method is:
1114 % MagickRealType FxEvaluateChannelExpression(FxInfo *fx_info,
1115 % const PixelChannel channel,const ssize_t x,const ssize_t y,
1116 % MagickRealType *alpha,Exceptioninfo *exception)
1117 % MagickRealType FxEvaluateExpression(FxInfo *fx_info,
1118 % MagickRealType *alpha,Exceptioninfo *exception)
1120 % A description of each parameter follows:
1122 % o fx_info: the fx info.
1124 % o channel: the channel.
1126 % o x,y: the pixel position.
1128 % o alpha: the result.
1130 % o exception: return any errors or warnings in this structure.
1134 static inline double MagickMax(const double x,const double y)
1141 static inline double MagickMin(const double x,const double y)
1148 static MagickRealType FxChannelStatistics(FxInfo *fx_info,const Image *image,
1149 PixelChannel channel,const char *symbol,ExceptionInfo *exception)
1153 statistic[MaxTextExtent];
1161 for (p=symbol; (*p != '.') && (*p != '\0'); p++) ;
1163 switch (*++p) /* e.g. depth.r */
1165 case 'r': channel=RedPixelChannel; break;
1166 case 'g': channel=GreenPixelChannel; break;
1167 case 'b': channel=BluePixelChannel; break;
1168 case 'c': channel=CyanPixelChannel; break;
1169 case 'm': channel=MagentaPixelChannel; break;
1170 case 'y': channel=YellowPixelChannel; break;
1171 case 'k': channel=BlackPixelChannel; break;
1174 (void) FormatLocaleString(key,MaxTextExtent,"%p.%.20g.%s",(void *) image,
1175 (double) channel,symbol);
1176 value=(const char *) GetValueFromSplayTree(fx_info->symbols,key);
1177 if (value != (const char *) NULL)
1178 return(QuantumScale*StringToDouble(value,(char **) NULL));
1179 (void) DeleteNodeFromSplayTree(fx_info->symbols,key);
1180 if (LocaleNCompare(symbol,"depth",5) == 0)
1185 depth=GetImageDepth(image,exception);
1186 (void) FormatLocaleString(statistic,MaxTextExtent,"%.20g",(double) depth);
1188 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1194 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
1195 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",kurtosis);
1197 if (LocaleNCompare(symbol,"maxima",6) == 0)
1203 (void) GetImageRange(image,&minima,&maxima,exception);
1204 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",maxima);
1206 if (LocaleNCompare(symbol,"mean",4) == 0)
1212 (void) GetImageMean(image,&mean,&standard_deviation,exception);
1213 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",mean);
1215 if (LocaleNCompare(symbol,"minima",6) == 0)
1221 (void) GetImageRange(image,&minima,&maxima,exception);
1222 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",minima);
1224 if (LocaleNCompare(symbol,"skewness",8) == 0)
1230 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
1231 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",skewness);
1233 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1239 (void) GetImageMean(image,&mean,&standard_deviation,exception);
1240 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",
1241 standard_deviation);
1243 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(key),
1244 ConstantString(statistic));
1245 return(QuantumScale*StringToDouble(statistic,(char **) NULL));
1248 static MagickRealType
1249 FxEvaluateSubexpression(FxInfo *,const PixelChannel,const ssize_t,
1250 const ssize_t,const char *,MagickRealType *,ExceptionInfo *);
1252 static MagickOffsetType FxGCD(MagickOffsetType alpha,MagickOffsetType beta)
1255 return(FxGCD(beta,alpha % beta));
1259 static inline const char *FxSubexpression(const char *expression,
1260 ExceptionInfo *exception)
1269 subexpression=expression;
1270 while ((*subexpression != '\0') &&
1271 ((level != 1) || (strchr(")",(int) *subexpression) == (char *) NULL)))
1273 if (strchr("(",(int) *subexpression) != (char *) NULL)
1276 if (strchr(")",(int) *subexpression) != (char *) NULL)
1280 if (*subexpression == '\0')
1281 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1282 "UnbalancedParenthesis","`%s'",expression);
1283 return(subexpression);
1286 static MagickRealType FxGetSymbol(FxInfo *fx_info,const PixelChannel channel,
1287 const ssize_t x,const ssize_t y,const char *expression,
1288 ExceptionInfo *exception)
1292 subexpression[MaxTextExtent],
1293 symbol[MaxTextExtent];
1322 i=GetImageIndexInList(fx_info->images);
1326 if (isalpha((int) *(p+1)) == 0)
1328 if (strchr("suv",(int) *p) != (char *) NULL)
1335 i=GetImageIndexInList(fx_info->images);
1338 case 'u': i=0; break;
1339 case 'v': i=1; break;
1346 for (p++; *p != '\0'; )
1360 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1362 i=(ssize_t) (alpha+0.5);
1368 if ((isalpha((int) *(p+1)) == 0) && (*p == 'p'))
1375 for (p++; *p != '\0'; )
1389 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1400 for (p++; *p != '\0'; )
1414 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1424 length=GetImageListLength(fx_info->images);
1426 i+=(ssize_t) length;
1428 image=GetImageFromList(fx_info->images,i);
1429 if (image == (Image *) NULL)
1431 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1432 "NoSuchImage","`%s'",expression);
1435 GetPixelInfo(image,&pixel);
1436 (void) InterpolatePixelInfo(image,fx_info->view[i],image->interpolate,
1437 point.x,point.y,&pixel,exception);
1438 if ((strlen(p) > 2) &&
1439 (LocaleCompare(p,"intensity") != 0) &&
1440 (LocaleCompare(p,"luminance") != 0) &&
1441 (LocaleCompare(p,"hue") != 0) &&
1442 (LocaleCompare(p,"saturation") != 0) &&
1443 (LocaleCompare(p,"lightness") != 0))
1446 name[MaxTextExtent];
1448 (void) CopyMagickString(name,p,MaxTextExtent);
1449 for (q=name+(strlen(name)-1); q > name; q--)
1459 if ((strlen(name) > 2) &&
1460 (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
1465 color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
1466 if (color != (PixelInfo *) NULL)
1472 if (QueryColorCompliance(name,AllCompliance,&pixel,fx_info->exception) != MagickFalse)
1474 (void) AddValueToSplayTree(fx_info->colors,ConstantString(name),
1475 ClonePixelInfo(&pixel));
1480 (void) CopyMagickString(symbol,p,MaxTextExtent);
1481 StripString(symbol);
1482 if (*symbol == '\0')
1486 case RedPixelChannel: return(QuantumScale*pixel.red);
1487 case GreenPixelChannel: return(QuantumScale*pixel.green);
1488 case BluePixelChannel: return(QuantumScale*pixel.blue);
1489 case BlackPixelChannel:
1491 if (image->colorspace != CMYKColorspace)
1493 (void) ThrowMagickException(exception,GetMagickModule(),
1494 OptionError,"ColorSeparatedImageRequired","`%s'",
1498 return(QuantumScale*pixel.black);
1500 case AlphaPixelChannel:
1505 if (pixel.matte == MagickFalse)
1507 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
1510 case IntensityPixelChannel:
1512 return(QuantumScale*GetPixelInfoIntensity(&pixel));
1517 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1518 "UnableToParseExpression","`%s'",p);
1526 if (LocaleCompare(symbol,"a") == 0)
1527 return((MagickRealType) (QuantumScale*pixel.alpha));
1533 if (LocaleCompare(symbol,"b") == 0)
1534 return(QuantumScale*pixel.blue);
1540 if (LocaleNCompare(symbol,"channel",7) == 0)
1548 flags=ParseGeometry(symbol+7,&channel_info);
1549 if (image->colorspace == CMYKColorspace)
1552 case CyanPixelChannel:
1554 if ((flags & RhoValue) == 0)
1556 return(channel_info.rho);
1558 case MagentaPixelChannel:
1560 if ((flags & SigmaValue) == 0)
1562 return(channel_info.sigma);
1564 case YellowPixelChannel:
1566 if ((flags & XiValue) == 0)
1568 return(channel_info.xi);
1570 case BlackPixelChannel:
1572 if ((flags & PsiValue) == 0)
1574 return(channel_info.psi);
1576 case AlphaPixelChannel:
1578 if ((flags & ChiValue) == 0)
1580 return(channel_info.chi);
1587 case RedPixelChannel:
1589 if ((flags & RhoValue) == 0)
1591 return(channel_info.rho);
1593 case GreenPixelChannel:
1595 if ((flags & SigmaValue) == 0)
1597 return(channel_info.sigma);
1599 case BluePixelChannel:
1601 if ((flags & XiValue) == 0)
1603 return(channel_info.xi);
1605 case BlackPixelChannel:
1607 if ((flags & ChiValue) == 0)
1609 return(channel_info.chi);
1611 case AlphaPixelChannel:
1613 if ((flags & PsiValue) == 0)
1615 return(channel_info.psi);
1622 if (LocaleCompare(symbol,"c") == 0)
1623 return(QuantumScale*pixel.red);
1629 if (LocaleNCompare(symbol,"depth",5) == 0)
1630 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1636 if (LocaleCompare(symbol,"g") == 0)
1637 return(QuantumScale*pixel.green);
1643 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1644 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1645 if (LocaleCompare(symbol,"k") == 0)
1647 if (image->colorspace != CMYKColorspace)
1649 (void) ThrowMagickException(exception,GetMagickModule(),
1650 OptionError,"ColorSeparatedImageRequired","`%s'",
1654 return(QuantumScale*pixel.black);
1661 if (LocaleCompare(symbol,"h") == 0)
1662 return((MagickRealType) image->rows);
1663 if (LocaleCompare(symbol,"hue") == 0)
1670 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1679 if ((LocaleCompare(symbol,"image.depth") == 0) ||
1680 (LocaleCompare(symbol,"image.minima") == 0) ||
1681 (LocaleCompare(symbol,"image.maxima") == 0) ||
1682 (LocaleCompare(symbol,"image.mean") == 0) ||
1683 (LocaleCompare(symbol,"image.kurtosis") == 0) ||
1684 (LocaleCompare(symbol,"image.skewness") == 0) ||
1685 (LocaleCompare(symbol,"image.standard_deviation") == 0))
1686 return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
1687 if (LocaleCompare(symbol,"image.resolution.x") == 0)
1688 return(image->resolution.x);
1689 if (LocaleCompare(symbol,"image.resolution.y") == 0)
1690 return(image->resolution.y);
1691 if (LocaleCompare(symbol,"intensity") == 0)
1692 return(QuantumScale*GetPixelInfoIntensity(&pixel));
1693 if (LocaleCompare(symbol,"i") == 0)
1694 return((MagickRealType) x);
1700 if (LocaleCompare(symbol,"j") == 0)
1701 return((MagickRealType) y);
1707 if (LocaleCompare(symbol,"lightness") == 0)
1714 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1718 if (LocaleCompare(symbol,"luminance") == 0)
1723 luminence=0.2126*pixel.red+0.7152*pixel.green+0.0722*pixel.blue;
1724 return(QuantumScale*luminence);
1731 if (LocaleNCompare(symbol,"maxima",6) == 0)
1732 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1733 if (LocaleNCompare(symbol,"mean",4) == 0)
1734 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1735 if (LocaleNCompare(symbol,"minima",6) == 0)
1736 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1737 if (LocaleCompare(symbol,"m") == 0)
1738 return(QuantumScale*pixel.blue);
1744 if (LocaleCompare(symbol,"n") == 0)
1745 return((MagickRealType) GetImageListLength(fx_info->images));
1751 if (LocaleCompare(symbol,"o") == 0)
1752 return(QuantumScale*pixel.alpha);
1758 if (LocaleCompare(symbol,"page.height") == 0)
1759 return((MagickRealType) image->page.height);
1760 if (LocaleCompare(symbol,"page.width") == 0)
1761 return((MagickRealType) image->page.width);
1762 if (LocaleCompare(symbol,"page.x") == 0)
1763 return((MagickRealType) image->page.x);
1764 if (LocaleCompare(symbol,"page.y") == 0)
1765 return((MagickRealType) image->page.y);
1771 if (LocaleCompare(symbol,"resolution.x") == 0)
1772 return(image->resolution.x);
1773 if (LocaleCompare(symbol,"resolution.y") == 0)
1774 return(image->resolution.y);
1775 if (LocaleCompare(symbol,"r") == 0)
1776 return(QuantumScale*pixel.red);
1782 if (LocaleCompare(symbol,"saturation") == 0)
1789 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1793 if (LocaleNCompare(symbol,"skewness",8) == 0)
1794 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1795 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1796 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1802 if (LocaleCompare(symbol,"t") == 0)
1803 return((MagickRealType) GetImageIndexInList(fx_info->images));
1809 if (LocaleCompare(symbol,"w") == 0)
1810 return((MagickRealType) image->columns);
1816 if (LocaleCompare(symbol,"y") == 0)
1817 return(QuantumScale*pixel.green);
1823 if (LocaleCompare(symbol,"z") == 0)
1828 depth=(MagickRealType) GetImageDepth(image,fx_info->exception);
1836 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1837 if (value != (const char *) NULL)
1838 return((MagickRealType) StringToDouble(value,(char **) NULL));
1839 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1840 "UnableToParseExpression","`%s'",symbol);
1844 static const char *FxOperatorPrecedence(const char *expression,
1845 ExceptionInfo *exception)
1849 UndefinedPrecedence,
1851 BitwiseComplementPrecedence,
1853 ExponentialNotationPrecedence,
1857 RelationalPrecedence,
1858 EquivalencyPrecedence,
1859 BitwiseAndPrecedence,
1860 BitwiseOrPrecedence,
1861 LogicalAndPrecedence,
1862 LogicalOrPrecedence,
1864 AssignmentPrecedence,
1884 subexpression=(const char *) NULL;
1885 target=NullPrecedence;
1886 while (*expression != '\0')
1888 precedence=UndefinedPrecedence;
1889 if ((isspace((int) ((char) *expression)) != 0) || (c == (int) '@'))
1894 switch (*expression)
1899 #if defined(MAGICKCORE_HAVE_ACOSH)
1900 if (LocaleNCompare(expression,"acosh",5) == 0)
1906 #if defined(MAGICKCORE_HAVE_ASINH)
1907 if (LocaleNCompare(expression,"asinh",5) == 0)
1913 #if defined(MAGICKCORE_HAVE_ATANH)
1914 if (LocaleNCompare(expression,"atanh",5) == 0)
1925 if ((LocaleNCompare(expression,"E+",2) == 0) ||
1926 (LocaleNCompare(expression,"E-",2) == 0))
1928 expression+=2; /* scientific notation */
1935 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1936 (LocaleNCompare(expression,"j1",2) == 0))
1945 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1952 if ((c == (int) '{') || (c == (int) '['))
1955 if ((c == (int) '}') || (c == (int) ']'))
1958 switch ((unsigned char) *expression)
1963 precedence=BitwiseComplementPrecedence;
1969 precedence=ExponentPrecedence;
1974 if (((c != 0) && ((isdigit((int) ((char) c)) != 0) ||
1975 (strchr(")",c) != (char *) NULL))) &&
1976 (((islower((int) ((char) *expression)) != 0) ||
1977 (strchr("(",(int) *expression) != (char *) NULL)) ||
1978 ((isdigit((int) ((char) c)) == 0) &&
1979 (isdigit((int) ((char) *expression)) != 0))) &&
1980 (strchr("xy",(int) *expression) == (char *) NULL))
1981 precedence=MultiplyPrecedence;
1988 precedence=MultiplyPrecedence;
1994 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
1996 precedence=AdditionPrecedence;
1999 case LeftShiftOperator:
2000 case RightShiftOperator:
2002 precedence=ShiftPrecedence;
2006 case LessThanEqualOperator:
2007 case GreaterThanEqualOperator:
2010 precedence=RelationalPrecedence;
2014 case NotEqualOperator:
2016 precedence=EquivalencyPrecedence;
2021 precedence=BitwiseAndPrecedence;
2026 precedence=BitwiseOrPrecedence;
2029 case LogicalAndOperator:
2031 precedence=LogicalAndPrecedence;
2034 case LogicalOrOperator:
2036 precedence=LogicalOrPrecedence;
2039 case ExponentialNotation:
2041 precedence=ExponentialNotationPrecedence;
2047 precedence=TernaryPrecedence;
2052 precedence=AssignmentPrecedence;
2057 precedence=CommaPrecedence;
2062 precedence=SeparatorPrecedence;
2066 if ((precedence == BitwiseComplementPrecedence) ||
2067 (precedence == TernaryPrecedence) ||
2068 (precedence == AssignmentPrecedence))
2070 if (precedence > target)
2073 Right-to-left associativity.
2076 subexpression=expression;
2080 if (precedence >= target)
2083 Left-to-right associativity.
2086 subexpression=expression;
2088 if (strchr("(",(int) *expression) != (char *) NULL)
2089 expression=FxSubexpression(expression,exception);
2090 c=(int) (*expression++);
2092 return(subexpression);
2095 static MagickRealType FxEvaluateSubexpression(FxInfo *fx_info,
2096 const PixelChannel channel,const ssize_t x,const ssize_t y,
2097 const char *expression,MagickRealType *beta,ExceptionInfo *exception)
2101 subexpression[MaxTextExtent];
2111 if (exception->severity != UndefinedException)
2113 while (isspace((int) *expression) != 0)
2115 if (*expression == '\0')
2117 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
2118 "MissingExpression","`%s'",expression);
2121 *subexpression='\0';
2122 p=FxOperatorPrecedence(expression,exception);
2123 if (p != (const char *) NULL)
2125 (void) CopyMagickString(subexpression,expression,(size_t)
2127 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2129 switch ((unsigned char) *p)
2133 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2134 *beta=(MagickRealType) (~(size_t) *beta);
2139 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2140 return(*beta == 0.0 ? 1.0 : 0.0);
2144 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2145 channel,x,y,++p,beta,exception));
2149 case ExponentialNotation:
2151 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2152 return(alpha*(*beta));
2156 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2159 if (exception->severity == UndefinedException)
2160 (void) ThrowMagickException(exception,GetMagickModule(),
2161 OptionError,"DivideByZero","`%s'",expression);
2164 return(alpha/(*beta));
2168 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2169 *beta=fabs(floor(((double) *beta)+0.5));
2172 (void) ThrowMagickException(exception,GetMagickModule(),
2173 OptionError,"DivideByZero","`%s'",expression);
2176 return(fmod((double) alpha,(double) *beta));
2180 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2181 return(alpha+(*beta));
2185 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2186 return(alpha-(*beta));
2188 case LeftShiftOperator:
2190 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2191 *beta=(MagickRealType) ((size_t) (alpha+0.5) << (size_t)
2195 case RightShiftOperator:
2197 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2198 *beta=(MagickRealType) ((size_t) (alpha+0.5) >> (size_t)
2204 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2205 return(alpha < *beta ? 1.0 : 0.0);
2207 case LessThanEqualOperator:
2209 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2210 return(alpha <= *beta ? 1.0 : 0.0);
2214 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2215 return(alpha > *beta ? 1.0 : 0.0);
2217 case GreaterThanEqualOperator:
2219 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2220 return(alpha >= *beta ? 1.0 : 0.0);
2224 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2225 return(fabs(alpha-(*beta)) <= MagickEpsilon ? 1.0 : 0.0);
2227 case NotEqualOperator:
2229 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2230 return(fabs(alpha-(*beta)) > MagickEpsilon ? 1.0 : 0.0);
2234 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2235 *beta=(MagickRealType) ((size_t) (alpha+0.5) & (size_t)
2241 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2242 *beta=(MagickRealType) ((size_t) (alpha+0.5) | (size_t)
2246 case LogicalAndOperator:
2248 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2249 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2252 case LogicalOrOperator:
2254 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2255 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2263 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2265 p=StringToken(":",&q);
2266 if (q == (char *) NULL)
2268 (void) ThrowMagickException(exception,GetMagickModule(),
2269 OptionError,"UnableToParseExpression","`%s'",subexpression);
2272 if (fabs((double) alpha) > MagickEpsilon)
2273 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2275 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2281 numeric[MaxTextExtent];
2284 while (isalpha((int) ((unsigned char) *q)) != 0)
2288 (void) ThrowMagickException(exception,GetMagickModule(),
2289 OptionError,"UnableToParseExpression","`%s'",subexpression);
2292 ClearMagickException(exception);
2293 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2294 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
2296 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2297 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2298 subexpression),ConstantString(numeric));
2303 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2308 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2313 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2319 if (strchr("(",(int) *expression) != (char *) NULL)
2321 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2322 subexpression[strlen(subexpression)-1]='\0';
2323 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2327 switch (*expression)
2331 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2337 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2343 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2345 return((MagickRealType) (~(size_t) (gamma+0.5)));
2350 if (LocaleNCompare(expression,"abs",3) == 0)
2352 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2354 return((MagickRealType) fabs((double) alpha));
2356 #if defined(MAGICKCORE_HAVE_ACOSH)
2357 if (LocaleNCompare(expression,"acosh",5) == 0)
2359 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2361 return((MagickRealType) acosh((double) alpha));
2364 if (LocaleNCompare(expression,"acos",4) == 0)
2366 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2368 return((MagickRealType) acos((double) alpha));
2370 #if defined(MAGICKCORE_HAVE_J1)
2371 if (LocaleNCompare(expression,"airy",4) == 0)
2373 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2377 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
2378 return(gamma*gamma);
2381 #if defined(MAGICKCORE_HAVE_ASINH)
2382 if (LocaleNCompare(expression,"asinh",5) == 0)
2384 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2386 return((MagickRealType) asinh((double) alpha));
2389 if (LocaleNCompare(expression,"asin",4) == 0)
2391 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2393 return((MagickRealType) asin((double) alpha));
2395 if (LocaleNCompare(expression,"alt",3) == 0)
2397 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2399 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
2401 if (LocaleNCompare(expression,"atan2",5) == 0)
2403 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2405 return((MagickRealType) atan2((double) alpha,(double) *beta));
2407 #if defined(MAGICKCORE_HAVE_ATANH)
2408 if (LocaleNCompare(expression,"atanh",5) == 0)
2410 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2412 return((MagickRealType) atanh((double) alpha));
2415 if (LocaleNCompare(expression,"atan",4) == 0)
2417 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2419 return((MagickRealType) atan((double) alpha));
2421 if (LocaleCompare(expression,"a") == 0)
2422 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2428 if (LocaleCompare(expression,"b") == 0)
2429 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2435 if (LocaleNCompare(expression,"ceil",4) == 0)
2437 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2439 return((MagickRealType) ceil((double) alpha));
2441 if (LocaleNCompare(expression,"cosh",4) == 0)
2443 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2445 return((MagickRealType) cosh((double) alpha));
2447 if (LocaleNCompare(expression,"cos",3) == 0)
2449 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2451 return((MagickRealType) cos((double) alpha));
2453 if (LocaleCompare(expression,"c") == 0)
2454 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2460 if (LocaleNCompare(expression,"debug",5) == 0)
2465 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2467 if (fx_info->images->colorspace == CMYKColorspace)
2470 case CyanPixelChannel: type="cyan"; break;
2471 case MagentaPixelChannel: type="magenta"; break;
2472 case YellowPixelChannel: type="yellow"; break;
2473 case AlphaPixelChannel: type="opacity"; break;
2474 case BlackPixelChannel: type="black"; break;
2475 default: type="unknown"; break;
2480 case RedPixelChannel: type="red"; break;
2481 case GreenPixelChannel: type="green"; break;
2482 case BluePixelChannel: type="blue"; break;
2483 case AlphaPixelChannel: type="opacity"; break;
2484 default: type="unknown"; break;
2486 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2487 if (strlen(subexpression) > 1)
2488 subexpression[strlen(subexpression)-1]='\0';
2489 if (fx_info->file != (FILE *) NULL)
2490 (void) FormatLocaleFile(fx_info->file,
2491 "%s[%.20g,%.20g].%s: %s=%.*g\n",fx_info->images->filename,
2492 (double) x,(double) y,type,subexpression,GetMagickPrecision(),
2496 if (LocaleNCompare(expression,"drc",3) == 0)
2498 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2500 return((MagickRealType) (alpha/(*beta*(alpha-1.0)+1.0)));
2507 if (LocaleCompare(expression,"epsilon") == 0)
2508 return((MagickRealType) MagickEpsilon);
2509 if (LocaleNCompare(expression,"exp",3) == 0)
2511 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2513 return((MagickRealType) exp((double) alpha));
2515 if (LocaleCompare(expression,"e") == 0)
2516 return((MagickRealType) 2.7182818284590452354);
2522 if (LocaleNCompare(expression,"floor",5) == 0)
2524 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2526 return((MagickRealType) floor((double) alpha));
2533 if (LocaleNCompare(expression,"gauss",5) == 0)
2535 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2537 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
2538 return((MagickRealType) gamma);
2540 if (LocaleNCompare(expression,"gcd",3) == 0)
2545 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2547 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType)
2549 return((MagickRealType) gcd);
2551 if (LocaleCompare(expression,"g") == 0)
2552 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2558 if (LocaleCompare(expression,"h") == 0)
2559 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2560 if (LocaleCompare(expression,"hue") == 0)
2561 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2562 if (LocaleNCompare(expression,"hypot",5) == 0)
2564 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2566 return((MagickRealType) hypot((double) alpha,(double) *beta));
2573 if (LocaleCompare(expression,"k") == 0)
2574 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2580 if (LocaleCompare(expression,"intensity") == 0)
2581 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2582 if (LocaleNCompare(expression,"int",3) == 0)
2584 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2586 return((MagickRealType) floor(alpha));
2588 #if defined(MAGICKCORE_HAVE_ISNAN)
2589 if (LocaleNCompare(expression,"isnan",5) == 0)
2591 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2593 return((MagickRealType) !!isnan((double) alpha));
2596 if (LocaleCompare(expression,"i") == 0)
2597 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2603 if (LocaleCompare(expression,"j") == 0)
2604 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2605 #if defined(MAGICKCORE_HAVE_J0)
2606 if (LocaleNCompare(expression,"j0",2) == 0)
2608 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2610 return((MagickRealType) j0((double) alpha));
2613 #if defined(MAGICKCORE_HAVE_J1)
2614 if (LocaleNCompare(expression,"j1",2) == 0)
2616 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2618 return((MagickRealType) j1((double) alpha));
2621 #if defined(MAGICKCORE_HAVE_J1)
2622 if (LocaleNCompare(expression,"jinc",4) == 0)
2624 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2628 gamma=(MagickRealType) (2.0*j1((double) (MagickPI*alpha))/
2638 if (LocaleNCompare(expression,"ln",2) == 0)
2640 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2642 return((MagickRealType) log((double) alpha));
2644 if (LocaleNCompare(expression,"logtwo",6) == 0)
2646 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2648 return((MagickRealType) log10((double) alpha))/log10(2.0);
2650 if (LocaleNCompare(expression,"log",3) == 0)
2652 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2654 return((MagickRealType) log10((double) alpha));
2656 if (LocaleCompare(expression,"lightness") == 0)
2657 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2663 if (LocaleCompare(expression,"MaxRGB") == 0)
2664 return((MagickRealType) QuantumRange);
2665 if (LocaleNCompare(expression,"maxima",6) == 0)
2667 if (LocaleNCompare(expression,"max",3) == 0)
2669 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2671 return(alpha > *beta ? alpha : *beta);
2673 if (LocaleNCompare(expression,"minima",6) == 0)
2675 if (LocaleNCompare(expression,"min",3) == 0)
2677 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2679 return(alpha < *beta ? alpha : *beta);
2681 if (LocaleNCompare(expression,"mod",3) == 0)
2683 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2685 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2688 if (LocaleCompare(expression,"m") == 0)
2689 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2695 if (LocaleNCompare(expression,"not",3) == 0)
2697 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2699 return((MagickRealType) (alpha < MagickEpsilon));
2701 if (LocaleCompare(expression,"n") == 0)
2702 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2708 if (LocaleCompare(expression,"Opaque") == 0)
2710 if (LocaleCompare(expression,"o") == 0)
2711 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2717 if (LocaleCompare(expression,"phi") == 0)
2718 return((MagickRealType) MagickPHI);
2719 if (LocaleCompare(expression,"pi") == 0)
2720 return((MagickRealType) MagickPI);
2721 if (LocaleNCompare(expression,"pow",3) == 0)
2723 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2725 return((MagickRealType) pow((double) alpha,(double) *beta));
2727 if (LocaleCompare(expression,"p") == 0)
2728 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2734 if (LocaleCompare(expression,"QuantumRange") == 0)
2735 return((MagickRealType) QuantumRange);
2736 if (LocaleCompare(expression,"QuantumScale") == 0)
2737 return((MagickRealType) QuantumScale);
2743 if (LocaleNCompare(expression,"rand",4) == 0)
2744 return((MagickRealType) GetPseudoRandomValue(fx_info->random_info));
2745 if (LocaleNCompare(expression,"round",5) == 0)
2747 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2749 return((MagickRealType) floor((double) alpha+0.5));
2751 if (LocaleCompare(expression,"r") == 0)
2752 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2758 if (LocaleCompare(expression,"saturation") == 0)
2759 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2760 if (LocaleNCompare(expression,"sign",4) == 0)
2762 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2764 return(alpha < 0.0 ? -1.0 : 1.0);
2766 if (LocaleNCompare(expression,"sinc",4) == 0)
2768 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2772 gamma=(MagickRealType) (sin((double) (MagickPI*alpha))/
2776 if (LocaleNCompare(expression,"sinh",4) == 0)
2778 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2780 return((MagickRealType) sinh((double) alpha));
2782 if (LocaleNCompare(expression,"sin",3) == 0)
2784 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2786 return((MagickRealType) sin((double) alpha));
2788 if (LocaleNCompare(expression,"sqrt",4) == 0)
2790 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2792 return((MagickRealType) sqrt((double) alpha));
2794 if (LocaleNCompare(expression,"squish",6) == 0)
2796 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2798 return((MagickRealType) (1.0/(1.0+exp((double) (4.0*alpha)))));
2800 if (LocaleCompare(expression,"s") == 0)
2801 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2807 if (LocaleNCompare(expression,"tanh",4) == 0)
2809 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2811 return((MagickRealType) tanh((double) alpha));
2813 if (LocaleNCompare(expression,"tan",3) == 0)
2815 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2817 return((MagickRealType) tan((double) alpha));
2819 if (LocaleCompare(expression,"Transparent") == 0)
2821 if (LocaleNCompare(expression,"trunc",5) == 0)
2823 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2826 return((MagickRealType) floor((double) alpha));
2827 return((MagickRealType) ceil((double) alpha));
2829 if (LocaleCompare(expression,"t") == 0)
2830 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2836 if (LocaleCompare(expression,"u") == 0)
2837 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2843 if (LocaleCompare(expression,"v") == 0)
2844 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2850 if (LocaleNCompare(expression,"while",5) == 0)
2854 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2856 } while (fabs((double) alpha) >= MagickEpsilon);
2857 return((MagickRealType) *beta);
2859 if (LocaleCompare(expression,"w") == 0)
2860 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2866 if (LocaleCompare(expression,"y") == 0)
2867 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2873 if (LocaleCompare(expression,"z") == 0)
2874 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2880 q=(char *) expression;
2881 alpha=InterpretSiPrefixValue(expression,&q);
2882 if (q == expression)
2883 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2887 MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
2888 MagickRealType *alpha,ExceptionInfo *exception)
2893 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2898 MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
2899 MagickRealType *alpha,ExceptionInfo *exception)
2908 fx_info->file=(FILE *) NULL;
2909 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2915 MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
2916 const PixelChannel channel,const ssize_t x,const ssize_t y,
2917 MagickRealType *alpha,ExceptionInfo *exception)
2923 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2925 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2929 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2937 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2939 % FxImage() applies a mathematical expression to the specified image.
2941 % The format of the FxImage method is:
2943 % Image *FxImage(const Image *image,const char *expression,
2944 % ExceptionInfo *exception)
2946 % A description of each parameter follows:
2948 % o image: the image.
2950 % o expression: A mathematical expression.
2952 % o exception: return any errors or warnings in this structure.
2956 static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2961 assert(fx_info != (FxInfo **) NULL);
2962 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
2963 if (fx_info[i] != (FxInfo *) NULL)
2964 fx_info[i]=DestroyFxInfo(fx_info[i]);
2965 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
2969 static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2970 ExceptionInfo *exception)
2987 number_threads=GetOpenMPMaximumThreads();
2988 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
2989 if (fx_info == (FxInfo **) NULL)
2990 return((FxInfo **) NULL);
2991 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2992 if (*expression != '@')
2993 fx_expression=ConstantString(expression);
2995 fx_expression=FileToString(expression+1,~0,exception);
2996 for (i=0; i < (ssize_t) number_threads; i++)
2998 fx_info[i]=AcquireFxInfo(image,fx_expression);
2999 if (fx_info[i] == (FxInfo *) NULL)
3000 return(DestroyFxThreadSet(fx_info));
3001 (void) FxPreprocessExpression(fx_info[i],&alpha,fx_info[i]->exception);
3003 fx_expression=DestroyString(fx_expression);
3007 MagickExport Image *FxImage(const Image *image,const char *expression,
3008 ExceptionInfo *exception)
3010 #define FxImageTag "Fx/Image"
3034 assert(image != (Image *) NULL);
3035 assert(image->signature == MagickSignature);
3036 if (image->debug != MagickFalse)
3037 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3038 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3039 if (fx_image == (Image *) NULL)
3040 return((Image *) NULL);
3041 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
3043 fx_image=DestroyImage(fx_image);
3044 return((Image *) NULL);
3046 fx_info=AcquireFxThreadSet(image,expression,exception);
3047 if (fx_info == (FxInfo **) NULL)
3049 fx_image=DestroyImage(fx_image);
3050 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3052 status=FxPreprocessExpression(fx_info[0],&alpha,exception);
3053 if (status == MagickFalse)
3055 fx_image=DestroyImage(fx_image);
3056 fx_info=DestroyFxThreadSet(fx_info);
3057 return((Image *) NULL);
3064 image_view=AcquireCacheView(image);
3065 fx_view=AcquireCacheView(fx_image);
3066 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3067 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3069 for (y=0; y < (ssize_t) fx_image->rows; y++)
3072 id = GetOpenMPThreadId();
3074 register const Quantum
3083 if (status == MagickFalse)
3085 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3086 q=GetCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
3087 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3092 for (x=0; x < (ssize_t) fx_image->columns; x++)
3097 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3109 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3110 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3111 fx_traits=GetPixelChannelMapTraits(fx_image,channel);
3112 if ((traits == UndefinedPixelTrait) ||
3113 (fx_traits == UndefinedPixelTrait))
3115 if ((fx_traits & CopyPixelTrait) != 0)
3117 SetPixelChannel(fx_image,channel,p[i],q);
3121 (void) FxEvaluateChannelExpression(fx_info[id],(PixelChannel) i,x,y,
3123 q[i]=ClampToQuantum((MagickRealType) QuantumRange*alpha);
3125 p+=GetPixelChannels(image);
3126 q+=GetPixelChannels(fx_image);
3128 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3130 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3135 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3136 #pragma omp critical (MagickCore_FxImage)
3138 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3139 if (proceed == MagickFalse)
3143 fx_view=DestroyCacheView(fx_view);
3144 image_view=DestroyCacheView(image_view);
3145 fx_info=DestroyFxThreadSet(fx_info);
3146 if (status == MagickFalse)
3147 fx_image=DestroyImage(fx_image);
3152 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3156 % I m p l o d e I m a g e %
3160 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3162 % ImplodeImage() creates a new image that is a copy of an existing
3163 % one with the image pixels "implode" by the specified percentage. It
3164 % allocates the memory necessary for the new Image structure and returns a
3165 % pointer to the new image.
3167 % The format of the ImplodeImage method is:
3169 % Image *ImplodeImage(const Image *image,const double amount,
3170 % const PixelInterpolateMethod method,ExceptionInfo *exception)
3172 % A description of each parameter follows:
3174 % o implode_image: Method ImplodeImage returns a pointer to the image
3175 % after it is implode. A null image is returned if there is a memory
3178 % o image: the image.
3180 % o amount: Define the extent of the implosion.
3182 % o method: the pixel interpolation method.
3184 % o exception: return any errors or warnings in this structure.
3187 MagickExport Image *ImplodeImage(const Image *image,const double amount,
3188 const PixelInterpolateMethod method,ExceptionInfo *exception)
3190 #define ImplodeImageTag "Implode/Image"
3216 Initialize implode image attributes.
3218 assert(image != (Image *) NULL);
3219 assert(image->signature == MagickSignature);
3220 if (image->debug != MagickFalse)
3221 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3222 assert(exception != (ExceptionInfo *) NULL);
3223 assert(exception->signature == MagickSignature);
3224 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3226 if (implode_image == (Image *) NULL)
3227 return((Image *) NULL);
3228 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
3230 implode_image=DestroyImage(implode_image);
3231 return((Image *) NULL);
3233 if (implode_image->background_color.alpha != OpaqueAlpha)
3234 implode_image->matte=MagickTrue;
3236 Compute scaling factor.
3240 center.x=0.5*image->columns;
3241 center.y=0.5*image->rows;
3243 if (image->columns > image->rows)
3244 scale.y=(double) image->columns/(double) image->rows;
3246 if (image->columns < image->rows)
3248 scale.x=(double) image->rows/(double) image->columns;
3256 image_view=AcquireCacheView(image);
3257 implode_view=AcquireCacheView(implode_image);
3258 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3259 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
3261 for (y=0; y < (ssize_t) image->rows; y++)
3269 register const Quantum
3278 if (status == MagickFalse)
3280 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3281 q=GetCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
3283 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3288 delta.y=scale.y*(double) (y-center.y);
3289 for (x=0; x < (ssize_t) image->columns; x++)
3295 Determine if the pixel is within an ellipse.
3297 delta.x=scale.x*(double) (x-center.x);
3298 distance=delta.x*delta.x+delta.y*delta.y;
3299 if (distance >= (radius*radius))
3301 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3314 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/
3315 radius/2)),-amount);
3316 status=InterpolatePixelChannels(image,image_view,implode_image,method,
3317 (double) (factor*delta.x/scale.x+center.x),(double) (factor*delta.y/
3318 scale.y+center.y),q,exception);
3320 p+=GetPixelChannels(image);
3321 q+=GetPixelChannels(implode_image);
3323 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3325 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3330 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3331 #pragma omp critical (MagickCore_ImplodeImage)
3333 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3334 if (proceed == MagickFalse)
3338 implode_view=DestroyCacheView(implode_view);
3339 image_view=DestroyCacheView(image_view);
3340 if (status == MagickFalse)
3341 implode_image=DestroyImage(implode_image);
3342 return(implode_image);
3346 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3350 % M o r p h I m a g e s %
3354 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3356 % The MorphImages() method requires a minimum of two images. The first
3357 % image is transformed into the second by a number of intervening images
3358 % as specified by frames.
3360 % The format of the MorphImage method is:
3362 % Image *MorphImages(const Image *image,const size_t number_frames,
3363 % ExceptionInfo *exception)
3365 % A description of each parameter follows:
3367 % o image: the image.
3369 % o number_frames: Define the number of in-between image to generate.
3370 % The more in-between frames, the smoother the morph.
3372 % o exception: return any errors or warnings in this structure.
3375 MagickExport Image *MorphImages(const Image *image,
3376 const size_t number_frames,ExceptionInfo *exception)
3378 #define MorphImageTag "Morph/Image"
3394 register const Image
3404 Clone first frame in sequence.
3406 assert(image != (Image *) NULL);
3407 assert(image->signature == MagickSignature);
3408 if (image->debug != MagickFalse)
3409 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3410 assert(exception != (ExceptionInfo *) NULL);
3411 assert(exception->signature == MagickSignature);
3412 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3413 if (morph_images == (Image *) NULL)
3414 return((Image *) NULL);
3415 if (GetNextImageInList(image) == (Image *) NULL)
3420 for (i=1; i < (ssize_t) number_frames; i++)
3422 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3423 if (morph_image == (Image *) NULL)
3425 morph_images=DestroyImageList(morph_images);
3426 return((Image *) NULL);
3428 AppendImageToList(&morph_images,morph_image);
3429 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3434 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3436 if (proceed == MagickFalse)
3440 return(GetFirstImageInList(morph_images));
3443 Morph image sequence.
3448 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3450 for (i=0; i < (ssize_t) number_frames; i++)
3456 beta=(MagickRealType) (i+1.0)/(MagickRealType) (number_frames+1.0);
3458 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
3459 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*
3460 next->rows+beta*GetNextImageInList(next)->rows+0.5),
3461 next->filter,next->blur,exception);
3462 if (morph_image == (Image *) NULL)
3464 morph_images=DestroyImageList(morph_images);
3465 return((Image *) NULL);
3467 if (SetImageStorageClass(morph_image,DirectClass,exception) == MagickFalse)
3469 morph_image=DestroyImage(morph_image);
3470 return((Image *) NULL);
3472 AppendImageToList(&morph_images,morph_image);
3473 morph_images=GetLastImageInList(morph_images);
3474 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
3475 morph_images->rows,GetNextImageInList(next)->filter,
3476 GetNextImageInList(next)->blur,exception);
3477 if (morph_image == (Image *) NULL)
3479 morph_images=DestroyImageList(morph_images);
3480 return((Image *) NULL);
3482 image_view=AcquireCacheView(morph_image);
3483 morph_view=AcquireCacheView(morph_images);
3484 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3485 #pragma omp parallel for schedule(dynamic,4) shared(status)
3487 for (y=0; y < (ssize_t) morph_images->rows; y++)
3492 register const Quantum
3501 if (status == MagickFalse)
3503 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3505 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3507 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3512 for (x=0; x < (ssize_t) morph_images->columns; x++)
3514 SetPixelRed(morph_images,ClampToQuantum(alpha*
3515 GetPixelRed(morph_images,q)+beta*GetPixelRed(morph_image,p)),q);
3516 SetPixelGreen(morph_images,ClampToQuantum(alpha*
3517 GetPixelGreen(morph_images,q)+beta*GetPixelGreen(morph_image,p)),q);
3518 SetPixelBlue(morph_images,ClampToQuantum(alpha*
3519 GetPixelBlue(morph_images,q)+beta*GetPixelBlue(morph_image,p)),q);
3520 SetPixelAlpha(morph_images,ClampToQuantum(alpha*
3521 GetPixelAlpha(morph_images,q)+beta*GetPixelAlpha(morph_image,p)),q);
3522 if ((morph_image->colorspace == CMYKColorspace) &&
3523 (morph_images->colorspace == CMYKColorspace))
3524 SetPixelBlack(morph_images,ClampToQuantum(alpha*
3525 GetPixelBlack(morph_images,q)+beta*GetPixelBlack(morph_image,p)),
3527 p+=GetPixelChannels(morph_image);
3528 q+=GetPixelChannels(morph_images);
3530 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3531 if (sync == MagickFalse)
3534 morph_view=DestroyCacheView(morph_view);
3535 image_view=DestroyCacheView(image_view);
3536 morph_image=DestroyImage(morph_image);
3538 if (i < (ssize_t) number_frames)
3541 Clone last frame in sequence.
3543 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3544 if (morph_image == (Image *) NULL)
3546 morph_images=DestroyImageList(morph_images);
3547 return((Image *) NULL);
3549 AppendImageToList(&morph_images,morph_image);
3550 morph_images=GetLastImageInList(morph_images);
3551 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3556 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3557 #pragma omp critical (MagickCore_MorphImages)
3559 proceed=SetImageProgress(image,MorphImageTag,scene,
3560 GetImageListLength(image));
3561 if (proceed == MagickFalse)
3566 if (GetNextImageInList(next) != (Image *) NULL)
3568 morph_images=DestroyImageList(morph_images);
3569 return((Image *) NULL);
3571 return(GetFirstImageInList(morph_images));
3575 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3579 % P l a s m a I m a g e %
3583 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3585 % PlasmaImage() initializes an image with plasma fractal values. The image
3586 % must be initialized with a base color and the random number generator
3587 % seeded before this method is called.
3589 % The format of the PlasmaImage method is:
3591 % MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
3592 % size_t attenuate,size_t depth,ExceptionInfo *exception)
3594 % A description of each parameter follows:
3596 % o image: the image.
3598 % o segment: Define the region to apply plasma fractals values.
3600 % o attenuate: Define the plasma attenuation factor.
3602 % o depth: Limit the plasma recursion depth.
3604 % o exception: return any errors or warnings in this structure.
3608 static inline Quantum PlasmaPixel(RandomInfo *random_info,
3609 const MagickRealType pixel,const MagickRealType noise)
3614 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
3619 static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3620 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3621 const SegmentInfo *segment,size_t attenuate,size_t depth,
3622 ExceptionInfo *exception)
3633 register const Quantum
3649 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3657 Divide the area into quadrants and recurse.
3661 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3662 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
3663 local_info=(*segment);
3664 local_info.x2=(double) x_mid;
3665 local_info.y2=(double) y_mid;
3666 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3667 &local_info,attenuate,depth,exception);
3668 local_info=(*segment);
3669 local_info.y1=(double) y_mid;
3670 local_info.x2=(double) x_mid;
3671 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3672 &local_info,attenuate,depth,exception);
3673 local_info=(*segment);
3674 local_info.x1=(double) x_mid;
3675 local_info.y2=(double) y_mid;
3676 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3677 &local_info,attenuate,depth,exception);
3678 local_info=(*segment);
3679 local_info.x1=(double) x_mid;
3680 local_info.y1=(double) y_mid;
3681 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3682 &local_info,attenuate,depth,exception));
3684 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3685 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
3686 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3687 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3688 return(MagickFalse);
3690 Average pixels and apply plasma.
3692 plasma=(MagickRealType) QuantumRange/(2.0*attenuate);
3693 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3698 x=(ssize_t) ceil(segment->x1-0.5);
3699 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3701 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3703 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
3704 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3705 (q == (Quantum *) NULL))
3707 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3709 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3710 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3711 if (traits == UndefinedPixelTrait)
3713 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3715 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3716 if (segment->x1 != segment->x2)
3721 x=(ssize_t) ceil(segment->x2-0.5);
3722 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3724 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3726 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
3727 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3728 (q == (Quantum *) NULL))
3730 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3732 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3733 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3734 if (traits == UndefinedPixelTrait)
3736 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3738 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3741 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3743 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3748 y=(ssize_t) ceil(segment->y2-0.5);
3749 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3751 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3753 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
3754 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3755 (q == (Quantum *) NULL))
3757 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3759 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3760 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3761 if (traits == UndefinedPixelTrait)
3763 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3765 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3767 if (segment->y1 != segment->y2)
3772 y=(ssize_t) ceil(segment->y1-0.5);
3773 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3775 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3777 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
3778 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3779 (q == (Quantum *) NULL))
3781 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3783 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3784 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3785 if (traits == UndefinedPixelTrait)
3787 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3789 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3792 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3797 x=(ssize_t) ceil(segment->x1-0.5);
3798 y=(ssize_t) ceil(segment->y1-0.5);
3799 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
3800 x=(ssize_t) ceil(segment->x2-0.5);
3801 y=(ssize_t) ceil(segment->y2-0.5);
3802 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
3803 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
3804 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3805 (q == (Quantum *) NULL))
3807 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3809 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3810 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3811 if (traits == UndefinedPixelTrait)
3813 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3815 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3817 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3819 return(MagickFalse);
3822 MagickExport MagickBooleanType PlasmaImage(Image *image,
3823 const SegmentInfo *segment,size_t attenuate,size_t depth,
3824 ExceptionInfo *exception)
3837 if (image->debug != MagickFalse)
3838 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3839 assert(image != (Image *) NULL);
3840 assert(image->signature == MagickSignature);
3841 if (image->debug != MagickFalse)
3842 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3843 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
3844 return(MagickFalse);
3845 image_view=AcquireCacheView(image);
3846 u_view=AcquireCacheView(image);
3847 v_view=AcquireCacheView(image);
3848 random_info=AcquireRandomInfo();
3849 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3850 attenuate,depth,exception);
3851 random_info=DestroyRandomInfo(random_info);
3852 v_view=DestroyCacheView(v_view);
3853 u_view=DestroyCacheView(u_view);
3854 image_view=DestroyCacheView(image_view);
3859 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3863 % P o l a r o i d I m a g e %
3867 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3869 % PolaroidImage() simulates a Polaroid picture.
3871 % The format of the AnnotateImage method is:
3873 % Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
3874 % const double angle,const PixelInterpolateMethod method,
3875 % ExceptionInfo exception)
3877 % A description of each parameter follows:
3879 % o image: the image.
3881 % o draw_info: the draw info.
3883 % o angle: Apply the effect along this angle.
3885 % o method: the pixel interpolation method.
3887 % o exception: return any errors or warnings in this structure.
3890 MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
3891 const double angle,const PixelInterpolateMethod method,
3892 ExceptionInfo *exception)
3913 Simulate a Polaroid picture.
3915 assert(image != (Image *) NULL);
3916 assert(image->signature == MagickSignature);
3917 if (image->debug != MagickFalse)
3918 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3919 assert(exception != (ExceptionInfo *) NULL);
3920 assert(exception->signature == MagickSignature);
3921 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
3922 image->rows)/25.0,10.0);
3923 height=image->rows+2*quantum;
3924 caption_image=(Image *) NULL;
3925 value=GetImageProperty(image,"caption",exception);
3926 if (value != (const char *) NULL)
3930 geometry[MaxTextExtent];
3945 Generate caption image.
3947 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3948 if (caption_image == (Image *) NULL)
3949 return((Image *) NULL);
3950 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
3951 caption=InterpretImageProperties((ImageInfo *) NULL,(Image *) image,
3953 (void) CloneString(&annotate_info->text,caption);
3954 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
3955 &caption,exception);
3956 status=SetImageExtent(caption_image,image->columns,(size_t)
3957 ((count+1)*(metrics.ascent-metrics.descent)+0.5),exception);
3958 if (status == MagickFalse)
3959 caption_image=DestroyImage(caption_image);
3962 caption_image->background_color=image->border_color;
3963 (void) SetImageBackgroundColor(caption_image,exception);
3964 (void) CloneString(&annotate_info->text,caption);
3965 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
3967 if (annotate_info->gravity == UndefinedGravity)
3968 (void) CloneString(&annotate_info->geometry,AcquireString(
3970 (void) AnnotateImage(caption_image,annotate_info,exception);
3971 height+=caption_image->rows;
3973 annotate_info=DestroyDrawInfo(annotate_info);
3974 caption=DestroyString(caption);
3976 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
3978 if (picture_image == (Image *) NULL)
3980 if (caption_image != (Image *) NULL)
3981 caption_image=DestroyImage(caption_image);
3982 return((Image *) NULL);
3984 picture_image->background_color=image->border_color;
3985 (void) SetImageBackgroundColor(picture_image,exception);
3986 (void) CompositeImage(picture_image,OverCompositeOp,image,quantum,quantum,
3988 if (caption_image != (Image *) NULL)
3990 (void) CompositeImage(picture_image,OverCompositeOp,caption_image,
3991 quantum,(ssize_t) (image->rows+3*quantum/2),exception);
3992 caption_image=DestroyImage(caption_image);
3994 (void) QueryColorCompliance("none",AllCompliance,
3995 &picture_image->background_color,exception);
3996 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
3997 rotate_image=RotateImage(picture_image,90.0,exception);
3998 picture_image=DestroyImage(picture_image);
3999 if (rotate_image == (Image *) NULL)
4000 return((Image *) NULL);
4001 picture_image=rotate_image;
4002 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
4003 picture_image->columns,method,exception);
4004 picture_image=DestroyImage(picture_image);
4005 if (bend_image == (Image *) NULL)
4006 return((Image *) NULL);
4007 picture_image=bend_image;
4008 rotate_image=RotateImage(picture_image,-90.0,exception);
4009 picture_image=DestroyImage(picture_image);
4010 if (rotate_image == (Image *) NULL)
4011 return((Image *) NULL);
4012 picture_image=rotate_image;
4013 picture_image->background_color=image->background_color;
4014 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
4016 if (polaroid_image == (Image *) NULL)
4018 picture_image=DestroyImage(picture_image);
4019 return(picture_image);
4021 flop_image=FlopImage(polaroid_image,exception);
4022 polaroid_image=DestroyImage(polaroid_image);
4023 if (flop_image == (Image *) NULL)
4025 picture_image=DestroyImage(picture_image);
4026 return(picture_image);
4028 polaroid_image=flop_image;
4029 (void) CompositeImage(polaroid_image,OverCompositeOp,picture_image,
4030 (ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
4031 picture_image=DestroyImage(picture_image);
4032 (void) QueryColorCompliance("none",AllCompliance,
4033 &polaroid_image->background_color,exception);
4034 rotate_image=RotateImage(polaroid_image,angle,exception);
4035 polaroid_image=DestroyImage(polaroid_image);
4036 if (rotate_image == (Image *) NULL)
4037 return((Image *) NULL);
4038 polaroid_image=rotate_image;
4039 trim_image=TrimImage(polaroid_image,exception);
4040 polaroid_image=DestroyImage(polaroid_image);
4041 if (trim_image == (Image *) NULL)
4042 return((Image *) NULL);
4043 polaroid_image=trim_image;
4044 return(polaroid_image);
4048 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4052 % S e p i a T o n e I m a g e %
4056 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4058 % MagickSepiaToneImage() applies a special effect to the image, similar to the
4059 % effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4060 % 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4061 % threshold of 80% is a good starting point for a reasonable tone.
4063 % The format of the SepiaToneImage method is:
4065 % Image *SepiaToneImage(const Image *image,const double threshold,
4066 % ExceptionInfo *exception)
4068 % A description of each parameter follows:
4070 % o image: the image.
4072 % o threshold: the tone threshold.
4074 % o exception: return any errors or warnings in this structure.
4077 MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4078 ExceptionInfo *exception)
4080 #define SepiaToneImageTag "SepiaTone/Image"
4099 Initialize sepia-toned image attributes.
4101 assert(image != (const Image *) NULL);
4102 assert(image->signature == MagickSignature);
4103 if (image->debug != MagickFalse)
4104 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4105 assert(exception != (ExceptionInfo *) NULL);
4106 assert(exception->signature == MagickSignature);
4107 sepia_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
4108 if (sepia_image == (Image *) NULL)
4109 return((Image *) NULL);
4110 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
4112 sepia_image=DestroyImage(sepia_image);
4113 return((Image *) NULL);
4116 Tone each row of the image.
4120 image_view=AcquireCacheView(image);
4121 sepia_view=AcquireCacheView(sepia_image);
4122 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4123 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
4125 for (y=0; y < (ssize_t) image->rows; y++)
4127 register const Quantum
4136 if (status == MagickFalse)
4138 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
4139 q=QueueCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
4141 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
4146 for (x=0; x < (ssize_t) image->columns; x++)
4152 intensity=(MagickRealType) GetPixelIntensity(image,p);
4153 tone=intensity > threshold ? (MagickRealType) QuantumRange : intensity+
4154 (MagickRealType) QuantumRange-threshold;
4155 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
4156 tone=intensity > (7.0*threshold/6.0) ? (MagickRealType) QuantumRange :
4157 intensity+(MagickRealType) QuantumRange-7.0*threshold/6.0;
4158 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4159 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
4160 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
4162 if ((MagickRealType) GetPixelGreen(image,q) < tone)
4163 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4164 if ((MagickRealType) GetPixelBlue(image,q) < tone)
4165 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
4166 p+=GetPixelChannels(image);
4167 q+=GetPixelChannels(sepia_image);
4169 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4171 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4176 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4177 #pragma omp critical (MagickCore_SepiaToneImage)
4179 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4181 if (proceed == MagickFalse)
4185 sepia_view=DestroyCacheView(sepia_view);
4186 image_view=DestroyCacheView(image_view);
4187 (void) NormalizeImage(sepia_image,exception);
4188 (void) ContrastImage(sepia_image,MagickTrue,exception);
4189 if (status == MagickFalse)
4190 sepia_image=DestroyImage(sepia_image);
4191 return(sepia_image);
4195 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4199 % S h a d o w I m a g e %
4203 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4205 % ShadowImage() simulates a shadow from the specified image and returns it.
4207 % The format of the ShadowImage method is:
4209 % Image *ShadowImage(const Image *image,const double opacity,
4210 % const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4211 % ExceptionInfo *exception)
4213 % A description of each parameter follows:
4215 % o image: the image.
4217 % o opacity: percentage transparency.
4219 % o sigma: the standard deviation of the Gaussian, in pixels.
4221 % o x_offset: the shadow x-offset.
4223 % o y_offset: the shadow y-offset.
4225 % o exception: return any errors or warnings in this structure.
4228 MagickExport Image *ShadowImage(const Image *image,const double opacity,
4229 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4230 ExceptionInfo *exception)
4232 #define ShadowImageTag "Shadow/Image"
4245 assert(image != (Image *) NULL);
4246 assert(image->signature == MagickSignature);
4247 if (image->debug != MagickFalse)
4248 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4249 assert(exception != (ExceptionInfo *) NULL);
4250 assert(exception->signature == MagickSignature);
4251 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4252 if (clone_image == (Image *) NULL)
4253 return((Image *) NULL);
4254 (void) SetImageVirtualPixelMethod(clone_image,EdgeVirtualPixelMethod);
4255 clone_image->compose=OverCompositeOp;
4256 border_info.width=(size_t) floor(2.0*sigma+0.5);
4257 border_info.height=(size_t) floor(2.0*sigma+0.5);
4260 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4262 border_image=BorderImage(clone_image,&border_info,image->compose,exception);
4263 clone_image=DestroyImage(clone_image);
4264 if (border_image == (Image *) NULL)
4265 return((Image *) NULL);
4266 if (border_image->matte == MagickFalse)
4267 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
4271 (void) SetImageBackgroundColor(border_image,exception);
4272 channel_mask=SetPixelChannelMask(border_image,AlphaChannel);
4273 shadow_image=BlurImage(border_image,0.0,sigma,image->bias,exception);
4274 (void) SetPixelChannelMap(border_image,channel_mask);
4275 border_image=DestroyImage(border_image);
4276 if (shadow_image == (Image *) NULL)
4277 return((Image *) NULL);
4278 if (shadow_image->page.width == 0)
4279 shadow_image->page.width=shadow_image->columns;
4280 if (shadow_image->page.height == 0)
4281 shadow_image->page.height=shadow_image->rows;
4282 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4283 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4284 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4285 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
4286 return(shadow_image);
4290 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4294 % S k e t c h I m a g e %
4298 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4300 % SketchImage() simulates a pencil sketch. We convolve the image with a
4301 % Gaussian operator of the given radius and standard deviation (sigma). For
4302 % reasonable results, radius should be larger than sigma. Use a radius of 0
4303 % and SketchImage() selects a suitable radius for you. Angle gives the angle
4306 % The format of the SketchImage method is:
4308 % Image *SketchImage(const Image *image,const double radius,
4309 % const double sigma,const double angle,const double bias,
4310 % ExceptionInfo *exception)
4312 % A description of each parameter follows:
4314 % o image: the image.
4316 % o radius: the radius of the Gaussian, in pixels, not counting the
4319 % o sigma: the standard deviation of the Gaussian, in pixels.
4321 % o angle: apply the effect along this angle.
4325 % o exception: return any errors or warnings in this structure.
4328 MagickExport Image *SketchImage(const Image *image,const double radius,
4329 const double sigma,const double angle,const double bias,
4330 ExceptionInfo *exception)
4346 **restrict random_info;
4354 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4355 MagickTrue,exception);
4356 if (random_image == (Image *) NULL)
4357 return((Image *) NULL);
4359 random_info=AcquireRandomInfoThreadSet();
4360 random_view=AcquireCacheView(random_image);
4361 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4362 #pragma omp parallel for schedule(dynamic,4) shared(status)
4364 for (y=0; y < (ssize_t) random_image->rows; y++)
4367 id = GetOpenMPThreadId();
4375 if (status == MagickFalse)
4377 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4379 if (q == (Quantum *) NULL)
4384 for (x=0; x < (ssize_t) random_image->columns; x++)
4392 value=GetPseudoRandomValue(random_info[id]);
4393 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4398 traits=GetPixelChannelMapTraits(random_image,(PixelChannel) i);
4399 if (traits == UndefinedPixelTrait)
4401 q[i]=ClampToQuantum(QuantumRange*value);
4403 q+=GetPixelChannels(random_image);
4405 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4408 random_view=DestroyCacheView(random_view);
4409 random_info=DestroyRandomInfoThreadSet(random_info);
4410 if (status == MagickFalse)
4412 random_image=DestroyImage(random_image);
4413 return(random_image);
4415 blur_image=MotionBlurImage(random_image,radius,sigma,angle,bias,exception);
4416 random_image=DestroyImage(random_image);
4417 if (blur_image == (Image *) NULL)
4418 return((Image *) NULL);
4419 dodge_image=EdgeImage(blur_image,radius,sigma,exception);
4420 blur_image=DestroyImage(blur_image);
4421 if (dodge_image == (Image *) NULL)
4422 return((Image *) NULL);
4423 (void) NormalizeImage(dodge_image,exception);
4424 (void) NegateImage(dodge_image,MagickFalse,exception);
4425 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
4426 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4427 if (sketch_image == (Image *) NULL)
4429 dodge_image=DestroyImage(dodge_image);
4430 return((Image *) NULL);
4432 (void) CompositeImage(sketch_image,ColorDodgeCompositeOp,dodge_image,0,0,
4434 dodge_image=DestroyImage(dodge_image);
4435 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4436 if (blend_image == (Image *) NULL)
4438 sketch_image=DestroyImage(sketch_image);
4439 return((Image *) NULL);
4441 (void) SetImageArtifact(blend_image,"compose:args","20x80");
4442 (void) CompositeImage(sketch_image,BlendCompositeOp,blend_image,0,0,
4444 blend_image=DestroyImage(blend_image);
4445 return(sketch_image);
4449 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4453 % S o l a r i z e I m a g e %
4457 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4459 % SolarizeImage() applies a special effect to the image, similar to the effect
4460 % achieved in a photo darkroom by selectively exposing areas of photo
4461 % sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4462 % measure of the extent of the solarization.
4464 % The format of the SolarizeImage method is:
4466 % MagickBooleanType SolarizeImage(Image *image,const double threshold,
4467 % ExceptionInfo *exception)
4469 % A description of each parameter follows:
4471 % o image: the image.
4473 % o threshold: Define the extent of the solarization.
4475 % o exception: return any errors or warnings in this structure.
4478 MagickExport MagickBooleanType SolarizeImage(Image *image,
4479 const double threshold,ExceptionInfo *exception)
4481 #define SolarizeImageTag "Solarize/Image"
4495 assert(image != (Image *) NULL);
4496 assert(image->signature == MagickSignature);
4497 if (image->debug != MagickFalse)
4498 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4499 if (image->storage_class == PseudoClass)
4507 for (i=0; i < (ssize_t) image->colors; i++)
4509 if ((MagickRealType) image->colormap[i].red > threshold)
4510 image->colormap[i].red=(Quantum) QuantumRange-image->colormap[i].red;
4511 if ((MagickRealType) image->colormap[i].green > threshold)
4512 image->colormap[i].green=(Quantum) QuantumRange-
4513 image->colormap[i].green;
4514 if ((MagickRealType) image->colormap[i].blue > threshold)
4515 image->colormap[i].blue=(Quantum) QuantumRange-
4516 image->colormap[i].blue;
4524 image_view=AcquireCacheView(image);
4525 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4526 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
4528 for (y=0; y < (ssize_t) image->rows; y++)
4536 if (status == MagickFalse)
4538 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
4539 if (q == (Quantum *) NULL)
4544 for (x=0; x < (ssize_t) image->columns; x++)
4549 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4554 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
4555 if ((traits == UndefinedPixelTrait) ||
4556 ((traits & CopyPixelTrait) != 0))
4558 if ((MagickRealType) q[i] > threshold)
4559 q[i]=QuantumRange-q[i];
4561 q+=GetPixelChannels(image);
4563 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4565 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4570 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4571 #pragma omp critical (MagickCore_SolarizeImage)
4573 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4574 if (proceed == MagickFalse)
4578 image_view=DestroyCacheView(image_view);
4583 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4587 % S t e g a n o I m a g e %
4591 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4593 % SteganoImage() hides a digital watermark within the image. Recover
4594 % the hidden watermark later to prove that the authenticity of an image.
4595 % Offset defines the start position within the image to hide the watermark.
4597 % The format of the SteganoImage method is:
4599 % Image *SteganoImage(const Image *image,Image *watermark,
4600 % ExceptionInfo *exception)
4602 % A description of each parameter follows:
4604 % o image: the image.
4606 % o watermark: the watermark image.
4608 % o exception: return any errors or warnings in this structure.
4611 MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4612 ExceptionInfo *exception)
4614 #define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
4615 #define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
4616 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
4617 #define SteganoImageTag "Stegano/Image"
4652 Initialize steganographic image attributes.
4654 assert(image != (const Image *) NULL);
4655 assert(image->signature == MagickSignature);
4656 if (image->debug != MagickFalse)
4657 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4658 assert(watermark != (const Image *) NULL);
4659 assert(watermark->signature == MagickSignature);
4660 assert(exception != (ExceptionInfo *) NULL);
4661 assert(exception->signature == MagickSignature);
4663 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4664 if (stegano_image == (Image *) NULL)
4665 return((Image *) NULL);
4666 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
4668 stegano_image=DestroyImage(stegano_image);
4669 return((Image *) NULL);
4671 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
4673 Hide watermark in low-order bits of image.
4678 depth=stegano_image->depth;
4681 watermark_view=AcquireCacheView(watermark);
4682 stegano_view=AcquireCacheView(stegano_image);
4683 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
4685 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
4687 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
4690 virtual_pixel[CompositePixelChannel];
4692 (void) GetOneCacheViewVirtualPixel(watermark_view,x,y,virtual_pixel,
4694 pixel.red=(double) virtual_pixel[RedPixelChannel];
4695 pixel.green=(double) virtual_pixel[GreenPixelChannel];
4696 pixel.blue=(double) virtual_pixel[BluePixelChannel];
4697 pixel.alpha=(double) virtual_pixel[AlphaPixelChannel];
4698 if ((k/(ssize_t) stegano_image->columns) >= (ssize_t) stegano_image->rows)
4700 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4701 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4703 if (q == (Quantum *) NULL)
4709 SetPixelRed(image,SetBit(GetPixelRed(image,q),j,GetBit(
4710 GetPixelInfoIntensity(&pixel),i)),q);
4715 SetPixelGreen(image,SetBit(GetPixelGreen(image,q),j,GetBit(
4716 GetPixelInfoIntensity(&pixel),i)),q);
4721 SetPixelBlue(image,SetBit(GetPixelBlue(image,q),j,GetBit(
4722 GetPixelInfoIntensity(&pixel),i)),q);
4726 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
4732 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
4734 if (k == image->offset)
4738 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4743 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4745 if (proceed == MagickFalse)
4749 stegano_view=DestroyCacheView(stegano_view);
4750 watermark_view=DestroyCacheView(watermark_view);
4751 if (stegano_image->storage_class == PseudoClass)
4752 (void) SyncImage(stegano_image,exception);
4753 if (status == MagickFalse)
4755 stegano_image=DestroyImage(stegano_image);
4756 return((Image *) NULL);
4758 return(stegano_image);
4762 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4766 % S t e r e o A n a g l y p h I m a g e %
4770 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4772 % StereoAnaglyphImage() combines two images and produces a single image that
4773 % is the composite of a left and right image of a stereo pair. Special
4774 % red-green stereo glasses are required to view this effect.
4776 % The format of the StereoAnaglyphImage method is:
4778 % Image *StereoImage(const Image *left_image,const Image *right_image,
4779 % ExceptionInfo *exception)
4780 % Image *StereoAnaglyphImage(const Image *left_image,
4781 % const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
4782 % ExceptionInfo *exception)
4784 % A description of each parameter follows:
4786 % o left_image: the left image.
4788 % o right_image: the right image.
4790 % o exception: return any errors or warnings in this structure.
4792 % o x_offset: amount, in pixels, by which the left image is offset to the
4793 % right of the right image.
4795 % o y_offset: amount, in pixels, by which the left image is offset to the
4796 % bottom of the right image.
4800 MagickExport Image *StereoImage(const Image *left_image,
4801 const Image *right_image,ExceptionInfo *exception)
4803 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4806 MagickExport Image *StereoAnaglyphImage(const Image *left_image,
4807 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
4808 ExceptionInfo *exception)
4810 #define StereoImageTag "Stereo/Image"
4824 assert(left_image != (const Image *) NULL);
4825 assert(left_image->signature == MagickSignature);
4826 if (left_image->debug != MagickFalse)
4827 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4828 left_image->filename);
4829 assert(right_image != (const Image *) NULL);
4830 assert(right_image->signature == MagickSignature);
4831 assert(exception != (ExceptionInfo *) NULL);
4832 assert(exception->signature == MagickSignature);
4833 assert(right_image != (const Image *) NULL);
4835 if ((left_image->columns != right_image->columns) ||
4836 (left_image->rows != right_image->rows))
4837 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4839 Initialize stereo image attributes.
4841 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4842 MagickTrue,exception);
4843 if (stereo_image == (Image *) NULL)
4844 return((Image *) NULL);
4845 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
4847 stereo_image=DestroyImage(stereo_image);
4848 return((Image *) NULL);
4851 Copy left image to red channel and right image to blue channel.
4854 for (y=0; y < (ssize_t) stereo_image->rows; y++)
4856 register const Quantum
4866 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4868 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4869 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
4870 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4871 (r == (Quantum *) NULL))
4873 for (x=0; x < (ssize_t) stereo_image->columns; x++)
4875 SetPixelRed(image,GetPixelRed(left_image,p),r);
4876 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4877 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4878 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4879 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4880 GetPixelAlpha(right_image,q))/2,r);
4881 p+=GetPixelChannels(left_image);
4882 q+=GetPixelChannels(right_image);
4883 r+=GetPixelChannels(stereo_image);
4885 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4887 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4892 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4893 stereo_image->rows);
4894 if (proceed == MagickFalse)
4898 if (status == MagickFalse)
4900 stereo_image=DestroyImage(stereo_image);
4901 return((Image *) NULL);
4903 return(stereo_image);
4907 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4911 % S w i r l I m a g e %
4915 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4917 % SwirlImage() swirls the pixels about the center of the image, where
4918 % degrees indicates the sweep of the arc through which each pixel is moved.
4919 % You get a more dramatic effect as the degrees move from 1 to 360.
4921 % The format of the SwirlImage method is:
4923 % Image *SwirlImage(const Image *image,double degrees,
4924 % const PixelInterpolateMethod method,ExceptionInfo *exception)
4926 % A description of each parameter follows:
4928 % o image: the image.
4930 % o degrees: Define the tightness of the swirling effect.
4932 % o method: the pixel interpolation method.
4934 % o exception: return any errors or warnings in this structure.
4937 MagickExport Image *SwirlImage(const Image *image,double degrees,
4938 const PixelInterpolateMethod method,ExceptionInfo *exception)
4940 #define SwirlImageTag "Swirl/Image"
4966 Initialize swirl image attributes.
4968 assert(image != (const Image *) NULL);
4969 assert(image->signature == MagickSignature);
4970 if (image->debug != MagickFalse)
4971 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4972 assert(exception != (ExceptionInfo *) NULL);
4973 assert(exception->signature == MagickSignature);
4974 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
4975 if (swirl_image == (Image *) NULL)
4976 return((Image *) NULL);
4977 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
4979 swirl_image=DestroyImage(swirl_image);
4980 return((Image *) NULL);
4982 if (swirl_image->background_color.alpha != OpaqueAlpha)
4983 swirl_image->matte=MagickTrue;
4985 Compute scaling factor.
4987 center.x=(double) image->columns/2.0;
4988 center.y=(double) image->rows/2.0;
4989 radius=MagickMax(center.x,center.y);
4992 if (image->columns > image->rows)
4993 scale.y=(double) image->columns/(double) image->rows;
4995 if (image->columns < image->rows)
4996 scale.x=(double) image->rows/(double) image->columns;
4997 degrees=(double) DegreesToRadians(degrees);
5003 image_view=AcquireCacheView(image);
5004 swirl_view=AcquireCacheView(swirl_image);
5005 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5006 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
5008 for (y=0; y < (ssize_t) image->rows; y++)
5016 register const Quantum
5025 if (status == MagickFalse)
5027 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
5028 q=GetCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
5030 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
5035 delta.y=scale.y*(double) (y-center.y);
5036 for (x=0; x < (ssize_t) image->columns; x++)
5042 Determine if the pixel is within an ellipse.
5044 delta.x=scale.x*(double) (x-center.x);
5045 distance=delta.x*delta.x+delta.y*delta.y;
5046 if (distance >= (radius*radius))
5048 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5061 factor=1.0-sqrt((double) distance)/radius;
5062 sine=sin((double) (degrees*factor*factor));
5063 cosine=cos((double) (degrees*factor*factor));
5064 status=InterpolatePixelChannels(image,image_view,swirl_image,method,
5065 ((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5066 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
5068 p+=GetPixelChannels(image);
5069 q+=GetPixelChannels(swirl_image);
5071 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5073 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5078 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5079 #pragma omp critical (MagickCore_SwirlImage)
5081 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5082 if (proceed == MagickFalse)
5086 swirl_view=DestroyCacheView(swirl_view);
5087 image_view=DestroyCacheView(image_view);
5088 if (status == MagickFalse)
5089 swirl_image=DestroyImage(swirl_image);
5090 return(swirl_image);
5094 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5098 % T i n t I m a g e %
5102 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5104 % TintImage() applies a color vector to each pixel in the image. The length
5105 % of the vector is 0 for black and white and at its maximum for the midtones.
5106 % The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5108 % The format of the TintImage method is:
5110 % Image *TintImage(const Image *image,const char *blend,
5111 % const PixelInfo *tint,ExceptionInfo *exception)
5113 % A description of each parameter follows:
5115 % o image: the image.
5117 % o blend: A color value used for tinting.
5119 % o tint: A color value used for tinting.
5121 % o exception: return any errors or warnings in this structure.
5124 MagickExport Image *TintImage(const Image *image,const char *blend,
5125 const PixelInfo *tint,ExceptionInfo *exception)
5127 #define TintImageTag "Tint/Image"
5159 Allocate tint image.
5161 assert(image != (const Image *) NULL);
5162 assert(image->signature == MagickSignature);
5163 if (image->debug != MagickFalse)
5164 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5165 assert(exception != (ExceptionInfo *) NULL);
5166 assert(exception->signature == MagickSignature);
5167 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5168 if (tint_image == (Image *) NULL)
5169 return((Image *) NULL);
5170 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
5172 tint_image=DestroyImage(tint_image);
5173 return((Image *) NULL);
5175 if (blend == (const char *) NULL)
5178 Determine RGB values of the color.
5180 GetPixelInfo(image,&pixel);
5181 flags=ParseGeometry(blend,&geometry_info);
5182 pixel.red=geometry_info.rho;
5183 pixel.green=geometry_info.rho;
5184 pixel.blue=geometry_info.rho;
5185 pixel.alpha=OpaqueAlpha;
5186 if ((flags & SigmaValue) != 0)
5187 pixel.green=geometry_info.sigma;
5188 if ((flags & XiValue) != 0)
5189 pixel.blue=geometry_info.xi;
5190 if ((flags & PsiValue) != 0)
5191 pixel.alpha=geometry_info.psi;
5192 if (image->colorspace == CMYKColorspace)
5194 pixel.black=geometry_info.rho;
5195 if ((flags & PsiValue) != 0)
5196 pixel.black=geometry_info.psi;
5197 if ((flags & ChiValue) != 0)
5198 pixel.alpha=geometry_info.chi;
5200 intensity=(MagickRealType) GetPixelInfoIntensity(tint);
5201 color_vector.red=(MagickRealType) (pixel.red*tint->red/100.0-intensity);
5202 color_vector.green=(MagickRealType) (pixel.green*tint->green/100.0-intensity);
5203 color_vector.blue=(MagickRealType) (pixel.blue*tint->blue/100.0-intensity);
5204 color_vector.black=(MagickRealType) (pixel.black*tint->black/100.0-intensity);
5205 color_vector.alpha=(MagickRealType) (pixel.alpha*tint->alpha/100.0-intensity);
5211 image_view=AcquireCacheView(image);
5212 tint_view=AcquireCacheView(tint_image);
5213 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5214 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
5216 for (y=0; y < (ssize_t) image->rows; y++)
5218 register const Quantum
5227 if (status == MagickFalse)
5229 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5230 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5232 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
5237 for (x=0; x < (ssize_t) image->columns; x++)
5245 if ((GetPixelRedTraits(tint_image) & UpdatePixelTrait) != 0)
5247 weight=QuantumScale*GetPixelRed(image,p)-0.5;
5248 pixel.red=(MagickRealType) GetPixelRed(image,p)+
5249 color_vector.red*(1.0-(4.0*(weight*weight)));
5250 SetPixelRed(tint_image,ClampToQuantum(pixel.red),q);
5252 if ((GetPixelGreenTraits(tint_image) & UpdatePixelTrait) != 0)
5254 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
5255 pixel.green=(MagickRealType) GetPixelGreen(image,p)+
5256 color_vector.green*(1.0-(4.0*(weight*weight)));
5257 SetPixelGreen(tint_image,ClampToQuantum(pixel.green),q);
5259 if ((GetPixelBlueTraits(tint_image) & UpdatePixelTrait) != 0)
5261 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
5262 pixel.blue=(MagickRealType) GetPixelBlue(image,p)+
5263 color_vector.blue*(1.0-(4.0*(weight*weight)));
5264 SetPixelBlue(tint_image,ClampToQuantum(pixel.blue),q);
5266 if ((GetPixelBlackTraits(tint_image) & UpdatePixelTrait) != 0)
5268 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
5269 pixel.black=(MagickRealType) GetPixelBlack(image,p)+
5270 color_vector.black*(1.0-(4.0*(weight*weight)));
5271 SetPixelBlack(tint_image,ClampToQuantum(pixel.black),q);
5273 if ((GetPixelAlphaTraits(tint_image) & CopyPixelTrait) != 0)
5274 SetPixelAlpha(tint_image,GetPixelAlpha(image,p),q);
5275 p+=GetPixelChannels(image);
5276 q+=GetPixelChannels(tint_image);
5278 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5280 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5285 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5286 #pragma omp critical (MagickCore_TintImage)
5288 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5289 if (proceed == MagickFalse)
5293 tint_view=DestroyCacheView(tint_view);
5294 image_view=DestroyCacheView(image_view);
5295 if (status == MagickFalse)
5296 tint_image=DestroyImage(tint_image);
5301 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5305 % V i g n e t t e I m a g e %
5309 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5311 % VignetteImage() softens the edges of the image in vignette style.
5313 % The format of the VignetteImage method is:
5315 % Image *VignetteImage(const Image *image,const double radius,
5316 % const double sigma,const ssize_t x,const ssize_t y,
5317 % ExceptionInfo *exception)
5319 % A description of each parameter follows:
5321 % o image: the image.
5323 % o radius: the radius of the pixel neighborhood.
5325 % o sigma: the standard deviation of the Gaussian, in pixels.
5327 % o x, y: Define the x and y ellipse offset.
5329 % o exception: return any errors or warnings in this structure.
5332 MagickExport Image *VignetteImage(const Image *image,const double radius,
5333 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
5336 ellipse[MaxTextExtent];
5347 assert(image != (Image *) NULL);
5348 assert(image->signature == MagickSignature);
5349 if (image->debug != MagickFalse)
5350 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5351 assert(exception != (ExceptionInfo *) NULL);
5352 assert(exception->signature == MagickSignature);
5353 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5354 if (canvas_image == (Image *) NULL)
5355 return((Image *) NULL);
5356 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
5358 canvas_image=DestroyImage(canvas_image);
5359 return((Image *) NULL);
5361 canvas_image->matte=MagickTrue;
5362 oval_image=CloneImage(canvas_image,canvas_image->columns,
5363 canvas_image->rows,MagickTrue,exception);
5364 if (oval_image == (Image *) NULL)
5366 canvas_image=DestroyImage(canvas_image);
5367 return((Image *) NULL);
5369 (void) QueryColorCompliance("#000000",AllCompliance,
5370 &oval_image->background_color,exception);
5371 (void) SetImageBackgroundColor(oval_image,exception);
5372 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
5373 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5375 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5377 (void) FormatLocaleString(ellipse,MaxTextExtent,
5378 "ellipse %g,%g,%g,%g,0.0,360.0",image->columns/2.0,
5379 image->rows/2.0,image->columns/2.0-x,image->rows/2.0-y);
5380 draw_info->primitive=AcquireString(ellipse);
5381 (void) DrawImage(oval_image,draw_info,exception);
5382 draw_info=DestroyDrawInfo(draw_info);
5383 blur_image=BlurImage(oval_image,radius,sigma,image->bias,exception);
5384 oval_image=DestroyImage(oval_image);
5385 if (blur_image == (Image *) NULL)
5387 canvas_image=DestroyImage(canvas_image);
5388 return((Image *) NULL);
5390 blur_image->matte=MagickFalse;
5391 (void) CompositeImage(canvas_image,CopyOpacityCompositeOp,blur_image,0,0,
5393 blur_image=DestroyImage(blur_image);
5394 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5395 canvas_image=DestroyImage(canvas_image);
5396 return(vignette_image);
5400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5404 % W a v e I m a g e %
5408 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5410 % WaveImage() creates a "ripple" effect in the image by shifting the pixels
5411 % vertically along a sine wave whose amplitude and wavelength is specified
5412 % by the given parameters.
5414 % The format of the WaveImage method is:
5416 % Image *WaveImage(const Image *image,const double amplitude,
5417 % const double wave_length,const PixelInterpolateMethod method,
5418 % ExceptionInfo *exception)
5420 % A description of each parameter follows:
5422 % o image: the image.
5424 % o amplitude, wave_length: Define the amplitude and wave length of the
5427 % o interpolate: the pixel interpolation method.
5429 % o exception: return any errors or warnings in this structure.
5432 MagickExport Image *WaveImage(const Image *image,const double amplitude,
5433 const double wave_length,const PixelInterpolateMethod method,
5434 ExceptionInfo *exception)
5436 #define WaveImageTag "Wave/Image"
5461 Initialize wave image attributes.
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 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
5470 fabs(amplitude)),MagickTrue,exception);
5471 if (wave_image == (Image *) NULL)
5472 return((Image *) NULL);
5473 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
5475 wave_image=DestroyImage(wave_image);
5476 return((Image *) NULL);
5478 if (wave_image->background_color.alpha != OpaqueAlpha)
5479 wave_image->matte=MagickTrue;
5483 sine_map=(MagickRealType *) AcquireQuantumMemory((size_t) wave_image->columns,
5485 if (sine_map == (MagickRealType *) NULL)
5487 wave_image=DestroyImage(wave_image);
5488 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5490 for (i=0; i < (ssize_t) wave_image->columns; i++)
5491 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5498 image_view=AcquireCacheView(image);
5499 wave_view=AcquireCacheView(wave_image);
5500 (void) SetCacheViewVirtualPixelMethod(image_view,
5501 BackgroundVirtualPixelMethod);
5502 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5503 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
5505 for (y=0; y < (ssize_t) wave_image->rows; y++)
5513 if (status == MagickFalse)
5515 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5517 if (q == (Quantum *) NULL)
5522 for (x=0; x < (ssize_t) wave_image->columns; x++)
5524 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5525 (double) x,(double) (y-sine_map[x]),q,exception);
5526 q+=GetPixelChannels(wave_image);
5528 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5530 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5535 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5536 #pragma omp critical (MagickCore_WaveImage)
5538 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5539 if (proceed == MagickFalse)
5543 wave_view=DestroyCacheView(wave_view);
5544 image_view=DestroyCacheView(image_view);
5545 sine_map=(MagickRealType *) RelinquishMagickMemory(sine_map);
5546 if (status == MagickFalse)
5547 wave_image=DestroyImage(wave_image);