2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % MagickCore Image Special Effects Methods %
20 % Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/annotate.h"
45 #include "MagickCore/artifact.h"
46 #include "MagickCore/attribute.h"
47 #include "MagickCore/cache.h"
48 #include "MagickCore/cache-view.h"
49 #include "MagickCore/color.h"
50 #include "MagickCore/color-private.h"
51 #include "MagickCore/colorspace-private.h"
52 #include "MagickCore/composite.h"
53 #include "MagickCore/decorate.h"
54 #include "MagickCore/distort.h"
55 #include "MagickCore/draw.h"
56 #include "MagickCore/effect.h"
57 #include "MagickCore/enhance.h"
58 #include "MagickCore/exception.h"
59 #include "MagickCore/exception-private.h"
60 #include "MagickCore/fx.h"
61 #include "MagickCore/fx-private.h"
62 #include "MagickCore/gem.h"
63 #include "MagickCore/gem-private.h"
64 #include "MagickCore/geometry.h"
65 #include "MagickCore/layer.h"
66 #include "MagickCore/list.h"
67 #include "MagickCore/log.h"
68 #include "MagickCore/image.h"
69 #include "MagickCore/image-private.h"
70 #include "MagickCore/magick.h"
71 #include "MagickCore/memory_.h"
72 #include "MagickCore/monitor.h"
73 #include "MagickCore/monitor-private.h"
74 #include "MagickCore/option.h"
75 #include "MagickCore/pixel.h"
76 #include "MagickCore/pixel-accessor.h"
77 #include "MagickCore/property.h"
78 #include "MagickCore/quantum.h"
79 #include "MagickCore/quantum-private.h"
80 #include "MagickCore/random_.h"
81 #include "MagickCore/random-private.h"
82 #include "MagickCore/resample.h"
83 #include "MagickCore/resample-private.h"
84 #include "MagickCore/resize.h"
85 #include "MagickCore/resource_.h"
86 #include "MagickCore/splay-tree.h"
87 #include "MagickCore/statistic.h"
88 #include "MagickCore/string_.h"
89 #include "MagickCore/string-private.h"
90 #include "MagickCore/thread-private.h"
91 #include "MagickCore/transform.h"
92 #include "MagickCore/utility.h"
97 #define LeftShiftOperator 0xf5
98 #define RightShiftOperator 0xf6
99 #define LessThanEqualOperator 0xf7
100 #define GreaterThanEqualOperator 0xf8
101 #define EqualOperator 0xf9
102 #define NotEqualOperator 0xfa
103 #define LogicalAndOperator 0xfb
104 #define LogicalOrOperator 0xfc
105 #define ExponentialNotation 0xfd
133 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
137 + A c q u i r e F x I n f o %
141 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143 % AcquireFxInfo() allocates the FxInfo structure.
145 % The format of the AcquireFxInfo method is:
147 % FxInfo *AcquireFxInfo(Image *image,const char *expression,
148 % ExceptionInfo *exception)
150 % A description of each parameter follows:
152 % o image: the image.
154 % o expression: the expression.
156 % o exception: return any errors or warnings in this structure.
159 MagickPrivate FxInfo *AcquireFxInfo(const Image *image,const char *expression,
160 ExceptionInfo *exception)
174 fx_info=(FxInfo *) AcquireMagickMemory(sizeof(*fx_info));
175 if (fx_info == (FxInfo *) NULL)
176 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
177 (void) ResetMagickMemory(fx_info,0,sizeof(*fx_info));
178 fx_info->exception=AcquireExceptionInfo();
179 fx_info->images=image;
180 fx_info->colors=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
181 RelinquishMagickMemory);
182 fx_info->symbols=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
183 RelinquishMagickMemory);
184 fx_info->view=(CacheView **) AcquireQuantumMemory(GetImageListLength(
185 fx_info->images),sizeof(*fx_info->view));
186 if (fx_info->view == (CacheView **) NULL)
187 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
189 next=GetFirstImageInList(fx_info->images);
190 for ( ; next != (Image *) NULL; next=next->next)
192 fx_info->view[i]=AcquireVirtualCacheView(next,exception);
195 fx_info->random_info=AcquireRandomInfo();
196 fx_info->expression=ConstantString(expression);
197 fx_info->file=stderr;
198 (void) SubstituteString(&fx_info->expression," ",""); /* compact string */
200 Force right-to-left associativity for unary negation.
202 (void) SubstituteString(&fx_info->expression,"-","-1.0*");
203 (void) SubstituteString(&fx_info->expression,"E-1.0*","E-");
204 (void) SubstituteString(&fx_info->expression,"e-1.0*","e-");
206 Convert complex to simple operators.
209 *fx_op=(char) LeftShiftOperator;
210 (void) SubstituteString(&fx_info->expression,"<<",fx_op);
211 *fx_op=(char) RightShiftOperator;
212 (void) SubstituteString(&fx_info->expression,">>",fx_op);
213 *fx_op=(char) LessThanEqualOperator;
214 (void) SubstituteString(&fx_info->expression,"<=",fx_op);
215 *fx_op=(char) GreaterThanEqualOperator;
216 (void) SubstituteString(&fx_info->expression,">=",fx_op);
217 *fx_op=(char) EqualOperator;
218 (void) SubstituteString(&fx_info->expression,"==",fx_op);
219 *fx_op=(char) NotEqualOperator;
220 (void) SubstituteString(&fx_info->expression,"!=",fx_op);
221 *fx_op=(char) LogicalAndOperator;
222 (void) SubstituteString(&fx_info->expression,"&&",fx_op);
223 *fx_op=(char) LogicalOrOperator;
224 (void) SubstituteString(&fx_info->expression,"||",fx_op);
225 *fx_op=(char) ExponentialNotation;
226 (void) SubstituteString(&fx_info->expression,"**",fx_op);
231 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
235 % A d d N o i s e I m a g e %
239 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241 % AddNoiseImage() adds random noise to the image.
243 % The format of the AddNoiseImage method is:
245 % Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
246 % const double attenuate,ExceptionInfo *exception)
248 % A description of each parameter follows:
250 % o image: the image.
252 % o channel: the channel type.
254 % o noise_type: The type of noise: Uniform, Gaussian, Multiplicative,
255 % Impulse, Laplacian, or Poisson.
257 % o attenuate: attenuate the random distribution.
259 % o exception: return any errors or warnings in this structure.
262 MagickExport Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
263 const double attenuate,ExceptionInfo *exception)
265 #define AddNoiseImageTag "AddNoise/Image"
281 **restrict random_info;
286 #if defined(MAGICKCORE_OPENMP_SUPPORT)
292 Initialize noise image attributes.
294 assert(image != (const Image *) NULL);
295 assert(image->signature == MagickSignature);
296 if (image->debug != MagickFalse)
297 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
298 assert(exception != (ExceptionInfo *) NULL);
299 assert(exception->signature == MagickSignature);
300 noise_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
301 if (noise_image == (Image *) NULL)
302 return((Image *) NULL);
303 if (SetImageStorageClass(noise_image,DirectClass,exception) == MagickFalse)
305 noise_image=DestroyImage(noise_image);
306 return((Image *) NULL);
308 if (IsGrayColorspace(image->colorspace) != MagickFalse)
309 (void) TransformImageColorspace(noise_image,RGBColorspace,exception);
311 Add noise in each row.
315 random_info=AcquireRandomInfoThreadSet();
316 image_view=AcquireVirtualCacheView(image,exception);
317 noise_view=AcquireAuthenticCacheView(noise_image,exception);
318 #if defined(MAGICKCORE_OPENMP_SUPPORT)
319 key=GetRandomSecretKey(random_info[0]);
320 #pragma omp parallel for schedule(static,4) shared(progress,status) \
321 dynamic_number_threads(image,noise_image,image->rows,key == ~0UL)
323 for (y=0; y < (ssize_t) image->rows; y++)
326 id = GetOpenMPThreadId();
331 register const Quantum
340 if (status == MagickFalse)
342 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
343 q=QueueCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
345 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
350 for (x=0; x < (ssize_t) image->columns; x++)
355 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
364 channel=GetPixelChannelChannel(image,i);
365 traits=GetPixelChannelTraits(image,channel);
366 noise_traits=GetPixelChannelTraits(noise_image,channel);
367 if ((traits == UndefinedPixelTrait) ||
368 (noise_traits == UndefinedPixelTrait))
370 if (((noise_traits & CopyPixelTrait) != 0) ||
371 (GetPixelMask(image,p) != 0))
373 SetPixelChannel(noise_image,channel,p[i],q);
376 SetPixelChannel(noise_image,channel,ClampToQuantum(
377 GenerateDifferentialNoise(random_info[id],p[i],noise_type,attenuate)),
380 p+=GetPixelChannels(image);
381 q+=GetPixelChannels(noise_image);
383 sync=SyncCacheViewAuthenticPixels(noise_view,exception);
384 if (sync == MagickFalse)
386 if (image->progress_monitor != (MagickProgressMonitor) NULL)
391 #if defined(MAGICKCORE_OPENMP_SUPPORT)
392 #pragma omp critical (MagickCore_AddNoiseImage)
394 proceed=SetImageProgress(image,AddNoiseImageTag,progress++,
396 if (proceed == MagickFalse)
400 noise_view=DestroyCacheView(noise_view);
401 image_view=DestroyCacheView(image_view);
402 random_info=DestroyRandomInfoThreadSet(random_info);
403 if (status == MagickFalse)
404 noise_image=DestroyImage(noise_image);
409 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
413 % B l u e S h i f t I m a g e %
417 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
419 % BlueShiftImage() mutes the colors of the image to simulate a scene at
420 % nighttime in the moonlight.
422 % The format of the BlueShiftImage method is:
424 % Image *BlueShiftImage(const Image *image,const double factor,
425 % ExceptionInfo *exception)
427 % A description of each parameter follows:
429 % o image: the image.
431 % o factor: the shift factor.
433 % o exception: return any errors or warnings in this structure.
436 MagickExport Image *BlueShiftImage(const Image *image,const double factor,
437 ExceptionInfo *exception)
439 #define BlueShiftImageTag "BlueShift/Image"
458 Allocate blue shift image.
460 assert(image != (const Image *) NULL);
461 assert(image->signature == MagickSignature);
462 if (image->debug != MagickFalse)
463 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
464 assert(exception != (ExceptionInfo *) NULL);
465 assert(exception->signature == MagickSignature);
466 shift_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
467 if (shift_image == (Image *) NULL)
468 return((Image *) NULL);
469 if (SetImageStorageClass(shift_image,DirectClass,exception) == MagickFalse)
471 shift_image=DestroyImage(shift_image);
472 return((Image *) NULL);
475 Blue-shift DirectClass image.
479 image_view=AcquireVirtualCacheView(image,exception);
480 shift_view=AcquireAuthenticCacheView(shift_image,exception);
481 #if defined(MAGICKCORE_OPENMP_SUPPORT)
482 #pragma omp parallel for schedule(static,4) shared(progress,status) \
483 dynamic_number_threads(image,shift_image,image->rows,1)
485 for (y=0; y < (ssize_t) image->rows; y++)
496 register const Quantum
505 if (status == MagickFalse)
507 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
508 q=QueueCacheViewAuthenticPixels(shift_view,0,y,shift_image->columns,1,
510 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
515 for (x=0; x < (ssize_t) image->columns; x++)
517 quantum=GetPixelRed(image,p);
518 if (GetPixelGreen(image,p) < quantum)
519 quantum=GetPixelGreen(image,p);
520 if (GetPixelBlue(image,p) < quantum)
521 quantum=GetPixelBlue(image,p);
522 pixel.red=0.5*(GetPixelRed(image,p)+factor*quantum);
523 pixel.green=0.5*(GetPixelGreen(image,p)+factor*quantum);
524 pixel.blue=0.5*(GetPixelBlue(image,p)+factor*quantum);
525 quantum=GetPixelRed(image,p);
526 if (GetPixelGreen(image,p) > quantum)
527 quantum=GetPixelGreen(image,p);
528 if (GetPixelBlue(image,p) > quantum)
529 quantum=GetPixelBlue(image,p);
530 pixel.red=0.5*(pixel.red+factor*quantum);
531 pixel.green=0.5*(pixel.green+factor*quantum);
532 pixel.blue=0.5*(pixel.blue+factor*quantum);
533 SetPixelRed(shift_image,ClampToQuantum(pixel.red),q);
534 SetPixelGreen(shift_image,ClampToQuantum(pixel.green),q);
535 SetPixelBlue(shift_image,ClampToQuantum(pixel.blue),q);
536 p+=GetPixelChannels(image);
537 q+=GetPixelChannels(shift_image);
539 sync=SyncCacheViewAuthenticPixels(shift_view,exception);
540 if (sync == MagickFalse)
542 if (image->progress_monitor != (MagickProgressMonitor) NULL)
547 #if defined(MAGICKCORE_OPENMP_SUPPORT)
548 #pragma omp critical (MagickCore_BlueShiftImage)
550 proceed=SetImageProgress(image,BlueShiftImageTag,progress++,
552 if (proceed == MagickFalse)
556 image_view=DestroyCacheView(image_view);
557 shift_view=DestroyCacheView(shift_view);
558 if (status == MagickFalse)
559 shift_image=DestroyImage(shift_image);
564 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
568 % C h a r c o a l I m a g e %
572 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
574 % CharcoalImage() creates a new image that is a copy of an existing one with
575 % the edge highlighted. It allocates the memory necessary for the new Image
576 % structure and returns a pointer to the new image.
578 % The format of the CharcoalImage method is:
580 % Image *CharcoalImage(const Image *image,const double radius,
581 % const double sigma,ExceptionInfo *exception)
583 % A description of each parameter follows:
585 % o image: the image.
587 % o radius: the radius of the pixel neighborhood.
589 % o sigma: the standard deviation of the Gaussian, in pixels.
591 % o exception: return any errors or warnings in this structure.
594 MagickExport Image *CharcoalImage(const Image *image,const double radius,
595 const double sigma,ExceptionInfo *exception)
602 assert(image != (Image *) NULL);
603 assert(image->signature == MagickSignature);
604 if (image->debug != MagickFalse)
605 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
606 assert(exception != (ExceptionInfo *) NULL);
607 assert(exception->signature == MagickSignature);
608 clone_image=CloneImage(image,0,0,MagickTrue,exception);
609 if (clone_image == (Image *) NULL)
610 return((Image *) NULL);
611 (void) SetImageType(clone_image,GrayscaleType,exception);
612 edge_image=EdgeImage(clone_image,radius,sigma,exception);
613 clone_image=DestroyImage(clone_image);
614 if (edge_image == (Image *) NULL)
615 return((Image *) NULL);
616 charcoal_image=BlurImage(edge_image,radius,sigma,exception);
617 edge_image=DestroyImage(edge_image);
618 if (charcoal_image == (Image *) NULL)
619 return((Image *) NULL);
620 (void) NormalizeImage(charcoal_image,exception);
621 (void) NegateImage(charcoal_image,MagickFalse,exception);
622 (void) SetImageType(charcoal_image,GrayscaleType,exception);
623 return(charcoal_image);
627 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
631 % C o l o r i z e I m a g e %
635 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
637 % ColorizeImage() blends the fill color with each pixel in the image.
638 % A percentage blend is specified with opacity. Control the application
639 % of different color components by specifying a different percentage for
640 % each component (e.g. 90/100/10 is 90% red, 100% green, and 10% blue).
642 % The format of the ColorizeImage method is:
644 % Image *ColorizeImage(const Image *image,const char *blend,
645 % const PixelInfo *colorize,ExceptionInfo *exception)
647 % A description of each parameter follows:
649 % o image: the image.
651 % o blend: A character string indicating the level of blending as a
654 % o colorize: A color value.
656 % o exception: return any errors or warnings in this structure.
659 MagickExport Image *ColorizeImage(const Image *image,const char *blend,
660 const PixelInfo *colorize,ExceptionInfo *exception)
662 #define ColorizeImageTag "Colorize/Image"
663 #define Colorize(pixel,blend_percentage,colorize) \
664 (((pixel)*(100.0-(blend_percentage))+(colorize)*(blend_percentage))/100.0)
692 Allocate colorized image.
694 assert(image != (const Image *) NULL);
695 assert(image->signature == MagickSignature);
696 if (image->debug != MagickFalse)
697 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
698 assert(exception != (ExceptionInfo *) NULL);
699 assert(exception->signature == MagickSignature);
700 colorize_image=CloneImage(image,image->columns,image->rows,MagickTrue,
702 if (colorize_image == (Image *) NULL)
703 return((Image *) NULL);
704 if (SetImageStorageClass(colorize_image,DirectClass,exception) == MagickFalse)
706 colorize_image=DestroyImage(colorize_image);
707 return((Image *) NULL);
709 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
710 (IsPixelInfoGray(colorize) != MagickFalse))
711 (void) SetImageColorspace(colorize_image,RGBColorspace,exception);
712 if ((colorize_image->alpha_trait != BlendPixelTrait) &&
713 (colorize->alpha_trait == BlendPixelTrait))
714 (void) SetImageAlpha(colorize_image,OpaqueAlpha,exception);
715 if (blend == (const char *) NULL)
716 return(colorize_image);
717 GetPixelInfo(image,&blend_percentage);
718 flags=ParseGeometry(blend,&geometry_info);
719 blend_percentage.red=geometry_info.rho;
720 blend_percentage.green=geometry_info.rho;
721 blend_percentage.blue=geometry_info.rho;
722 blend_percentage.black=geometry_info.rho;
723 blend_percentage.alpha=geometry_info.rho;
724 if ((flags & SigmaValue) != 0)
725 blend_percentage.green=geometry_info.sigma;
726 if ((flags & XiValue) != 0)
727 blend_percentage.blue=geometry_info.xi;
728 if ((flags & PsiValue) != 0)
729 blend_percentage.alpha=geometry_info.psi;
730 if (blend_percentage.colorspace == CMYKColorspace)
732 if ((flags & PsiValue) != 0)
733 blend_percentage.black=geometry_info.psi;
734 if ((flags & ChiValue) != 0)
735 blend_percentage.alpha=geometry_info.chi;
738 Colorize DirectClass image.
742 image_view=AcquireVirtualCacheView(image,exception);
743 colorize_view=AcquireAuthenticCacheView(colorize_image,exception);
744 #if defined(MAGICKCORE_OPENMP_SUPPORT)
745 #pragma omp parallel for schedule(static,4) shared(progress,status) \
746 dynamic_number_threads(image,colorize_image,image->rows,1)
748 for (y=0; y < (ssize_t) image->rows; y++)
753 register const Quantum
762 if (status == MagickFalse)
764 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
765 q=QueueCacheViewAuthenticPixels(colorize_view,0,y,colorize_image->columns,1,
767 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
772 for (x=0; x < (ssize_t) image->columns; x++)
777 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
786 channel=GetPixelChannelChannel(image,i);
787 traits=GetPixelChannelTraits(image,channel);
788 colorize_traits=GetPixelChannelTraits(colorize_image,channel);
789 if ((traits == UndefinedPixelTrait) ||
790 (colorize_traits == UndefinedPixelTrait))
792 if (((colorize_traits & CopyPixelTrait) != 0) ||
793 (GetPixelMask(image,p) != 0))
795 SetPixelChannel(colorize_image,channel,p[i],q);
798 SetPixelChannel(colorize_image,channel,
799 ClampToQuantum(Colorize(p[i],GetPixelInfoChannel(&blend_percentage,
800 channel),GetPixelInfoChannel(colorize,channel))),q);
802 p+=GetPixelChannels(image);
803 q+=GetPixelChannels(colorize_image);
805 sync=SyncCacheViewAuthenticPixels(colorize_view,exception);
806 if (sync == MagickFalse)
808 if (image->progress_monitor != (MagickProgressMonitor) NULL)
813 #if defined(MAGICKCORE_OPENMP_SUPPORT)
814 #pragma omp critical (MagickCore_ColorizeImage)
816 proceed=SetImageProgress(image,ColorizeImageTag,progress++,image->rows);
817 if (proceed == MagickFalse)
821 image_view=DestroyCacheView(image_view);
822 colorize_view=DestroyCacheView(colorize_view);
823 if (status == MagickFalse)
824 colorize_image=DestroyImage(colorize_image);
825 return(colorize_image);
829 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
833 % C o l o r M a t r i x I m a g e %
837 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
839 % ColorMatrixImage() applies color transformation to an image. This method
840 % permits saturation changes, hue rotation, luminance to alpha, and various
841 % other effects. Although variable-sized transformation matrices can be used,
842 % typically one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA
843 % (or RGBA with offsets). The matrix is similar to those used by Adobe Flash
844 % except offsets are in column 6 rather than 5 (in support of CMYKA images)
845 % and offsets are normalized (divide Flash offset by 255).
847 % The format of the ColorMatrixImage method is:
849 % Image *ColorMatrixImage(const Image *image,
850 % const KernelInfo *color_matrix,ExceptionInfo *exception)
852 % A description of each parameter follows:
854 % o image: the image.
856 % o color_matrix: the color matrix.
858 % o exception: return any errors or warnings in this structure.
861 /* FUTURE: modify to make use of a MagickMatrix Mutliply function
862 That should be provided in "matrix.c"
863 (ASIDE: actually distorts should do this too but currently doesn't)
866 MagickExport Image *ColorMatrixImage(const Image *image,
867 const KernelInfo *color_matrix,ExceptionInfo *exception)
869 #define ColorMatrixImageTag "ColorMatrix/Image"
878 { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
879 { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
880 { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
881 { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
882 { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
883 { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }
904 Map given color_matrix, into a 6x6 matrix RGBKA and a constant
906 assert(image != (Image *) NULL);
907 assert(image->signature == MagickSignature);
908 if (image->debug != MagickFalse)
909 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
910 assert(exception != (ExceptionInfo *) NULL);
911 assert(exception->signature == MagickSignature);
913 for (v=0; v < (ssize_t) color_matrix->height; v++)
914 for (u=0; u < (ssize_t) color_matrix->width; u++)
916 if ((v < 6) && (u < 6))
917 ColorMatrix[v][u]=color_matrix->values[i];
921 Initialize color image.
923 color_image=CloneImage(image,0,0,MagickTrue,exception);
924 if (color_image == (Image *) NULL)
925 return((Image *) NULL);
926 if (SetImageStorageClass(color_image,DirectClass,exception) == MagickFalse)
928 color_image=DestroyImage(color_image);
929 return((Image *) NULL);
931 if (image->debug != MagickFalse)
934 format[MaxTextExtent],
937 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
938 " ColorMatrix image with color matrix:");
939 message=AcquireString("");
940 for (v=0; v < 6; v++)
943 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
944 (void) ConcatenateString(&message,format);
945 for (u=0; u < 6; u++)
947 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",
949 (void) ConcatenateString(&message,format);
951 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
953 message=DestroyString(message);
956 Apply the ColorMatrix to image.
960 image_view=AcquireVirtualCacheView(image,exception);
961 color_view=AcquireAuthenticCacheView(color_image,exception);
962 #if defined(MAGICKCORE_OPENMP_SUPPORT)
963 #pragma omp parallel for schedule(static,4) shared(progress,status) \
964 dynamic_number_threads(image,color_image,image->rows,1)
966 for (y=0; y < (ssize_t) image->rows; y++)
971 register const Quantum
980 if (status == MagickFalse)
982 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
983 q=GetCacheViewAuthenticPixels(color_view,0,y,color_image->columns,1,
985 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
990 GetPixelInfo(image,&pixel);
991 for (x=0; x < (ssize_t) image->columns; x++)
999 GetPixelInfoPixel(image,p,&pixel);
1000 height=color_matrix->height > 6 ? 6UL : color_matrix->height;
1001 for (v=0; v < (ssize_t) height; v++)
1006 sum=ColorMatrix[v][0]*GetPixelRed(image,p)+ColorMatrix[v][1]*
1007 GetPixelGreen(image,p)+ColorMatrix[v][2]*GetPixelBlue(image,p);
1008 if (image->colorspace == CMYKColorspace)
1009 sum+=ColorMatrix[v][3]*GetPixelBlack(image,p);
1010 if (image->alpha_trait == BlendPixelTrait)
1011 sum+=ColorMatrix[v][4]*GetPixelAlpha(image,p);
1012 sum+=QuantumRange*ColorMatrix[v][5];
1015 case 0: pixel.red=sum; break;
1016 case 1: pixel.green=sum; break;
1017 case 2: pixel.blue=sum; break;
1018 case 3: pixel.black=sum; break;
1019 case 4: pixel.alpha=sum; break;
1023 SetPixelInfoPixel(color_image,&pixel,q);
1024 p+=GetPixelChannels(image);
1025 q+=GetPixelChannels(color_image);
1027 if (SyncCacheViewAuthenticPixels(color_view,exception) == MagickFalse)
1029 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1034 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1035 #pragma omp critical (MagickCore_ColorMatrixImage)
1037 proceed=SetImageProgress(image,ColorMatrixImageTag,progress++,
1039 if (proceed == MagickFalse)
1043 color_view=DestroyCacheView(color_view);
1044 image_view=DestroyCacheView(image_view);
1045 if (status == MagickFalse)
1046 color_image=DestroyImage(color_image);
1047 return(color_image);
1051 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1055 + D e s t r o y F x I n f o %
1059 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1061 % DestroyFxInfo() deallocates memory associated with an FxInfo structure.
1063 % The format of the DestroyFxInfo method is:
1065 % ImageInfo *DestroyFxInfo(ImageInfo *fx_info)
1067 % A description of each parameter follows:
1069 % o fx_info: the fx info.
1072 MagickPrivate FxInfo *DestroyFxInfo(FxInfo *fx_info)
1077 fx_info->exception=DestroyExceptionInfo(fx_info->exception);
1078 fx_info->expression=DestroyString(fx_info->expression);
1079 fx_info->symbols=DestroySplayTree(fx_info->symbols);
1080 fx_info->colors=DestroySplayTree(fx_info->colors);
1081 for (i=(ssize_t) GetImageListLength(fx_info->images)-1; i >= 0; i--)
1082 fx_info->view[i]=DestroyCacheView(fx_info->view[i]);
1083 fx_info->view=(CacheView **) RelinquishMagickMemory(fx_info->view);
1084 fx_info->random_info=DestroyRandomInfo(fx_info->random_info);
1085 fx_info=(FxInfo *) RelinquishMagickMemory(fx_info);
1090 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1094 + 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 %
1098 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1100 % FxEvaluateChannelExpression() evaluates an expression and returns the
1103 % The format of the FxEvaluateExpression method is:
1105 % double FxEvaluateChannelExpression(FxInfo *fx_info,
1106 % const PixelChannel channel,const ssize_t x,const ssize_t y,
1107 % double *alpha,Exceptioninfo *exception)
1108 % double FxEvaluateExpression(FxInfo *fx_info,
1109 % double *alpha,Exceptioninfo *exception)
1111 % A description of each parameter follows:
1113 % o fx_info: the fx info.
1115 % o channel: the channel.
1117 % o x,y: the pixel position.
1119 % o alpha: the result.
1121 % o exception: return any errors or warnings in this structure.
1125 static inline double MagickMax(const double x,const double y)
1132 static inline double MagickMin(const double x,const double y)
1139 static double FxChannelStatistics(FxInfo *fx_info,Image *image,
1140 PixelChannel channel,const char *symbol,ExceptionInfo *exception)
1147 statistic[MaxTextExtent];
1155 channel_mask=UndefinedChannel;
1156 for (p=symbol; (*p != '.') && (*p != '\0'); p++) ;
1162 option=ParseCommandOption(MagickPixelChannelOptions,MagickTrue,p+1);
1165 channel=(PixelChannel) option;
1166 channel_mask=(ChannelType) (channel_mask | (1 << channel));
1167 SetPixelChannelMask(image,channel_mask);
1170 (void) FormatLocaleString(key,MaxTextExtent,"%p.%.20g.%s",(void *) image,
1171 (double) channel,symbol);
1172 value=(const char *) GetValueFromSplayTree(fx_info->symbols,key);
1173 if (value != (const char *) NULL)
1175 if (channel_mask != UndefinedChannel)
1176 SetPixelChannelMask(image,channel_mask);
1177 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 if (channel_mask != UndefinedChannel)
1244 SetPixelChannelMask(image,channel_mask);
1245 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(key),
1246 ConstantString(statistic));
1247 return(QuantumScale*StringToDouble(statistic,(char **) NULL));
1251 FxEvaluateSubexpression(FxInfo *,const PixelChannel,const ssize_t,
1252 const ssize_t,const char *,double *,ExceptionInfo *);
1254 static MagickOffsetType FxGCD(MagickOffsetType alpha,MagickOffsetType beta)
1257 return(FxGCD(beta,alpha % beta));
1261 static inline const char *FxSubexpression(const char *expression,
1262 ExceptionInfo *exception)
1271 subexpression=expression;
1272 while ((*subexpression != '\0') &&
1273 ((level != 1) || (strchr(")",(int) *subexpression) == (char *) NULL)))
1275 if (strchr("(",(int) *subexpression) != (char *) NULL)
1278 if (strchr(")",(int) *subexpression) != (char *) NULL)
1282 if (*subexpression == '\0')
1283 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1284 "UnbalancedParenthesis","'%s'",expression);
1285 return(subexpression);
1288 static double FxGetSymbol(FxInfo *fx_info,const PixelChannel channel,
1289 const ssize_t x,const ssize_t y,const char *expression,
1290 ExceptionInfo *exception)
1294 subexpression[MaxTextExtent],
1295 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) && (LocaleCompare(p,"intensity") != 0) &&
1439 (LocaleCompare(p,"luminance") != 0) && (LocaleCompare(p,"hue") != 0) &&
1440 (LocaleCompare(p,"saturation") != 0) &&
1441 (LocaleCompare(p,"lightness") != 0))
1444 name[MaxTextExtent];
1446 (void) CopyMagickString(name,p,MaxTextExtent);
1447 for (q=name+(strlen(name)-1); q > name; q--)
1457 if ((strlen(name) > 2) &&
1458 (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
1463 color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
1464 if (color != (PixelInfo *) NULL)
1474 status=QueryColorCompliance(name,AllCompliance,&pixel,
1475 fx_info->exception);
1476 if (status != MagickFalse)
1478 (void) AddValueToSplayTree(fx_info->colors,ConstantString(
1479 name),ClonePixelInfo(&pixel));
1485 (void) CopyMagickString(symbol,p,MaxTextExtent);
1486 StripString(symbol);
1487 if (*symbol == '\0')
1491 case RedPixelChannel: return(QuantumScale*pixel.red);
1492 case GreenPixelChannel: return(QuantumScale*pixel.green);
1493 case BluePixelChannel: return(QuantumScale*pixel.blue);
1494 case BlackPixelChannel:
1496 if (image->colorspace != CMYKColorspace)
1498 (void) ThrowMagickException(exception,GetMagickModule(),
1499 ImageError,"ColorSeparatedImageRequired","'%s'",
1503 return(QuantumScale*pixel.black);
1505 case AlphaPixelChannel:
1510 if (pixel.alpha_trait != BlendPixelTrait)
1512 alpha=(double) (QuantumScale*pixel.alpha);
1515 case IndexPixelChannel:
1517 case IntensityPixelChannel:
1519 return(QuantumScale*GetPixelInfoIntensity(&pixel));
1524 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1525 "UnableToParseExpression","'%s'",p);
1533 if (LocaleCompare(symbol,"a") == 0)
1534 return((double) (QuantumScale*pixel.alpha));
1540 if (LocaleCompare(symbol,"b") == 0)
1541 return(QuantumScale*pixel.blue);
1547 if (LocaleNCompare(symbol,"channel",7) == 0)
1555 flags=ParseGeometry(symbol+7,&channel_info);
1556 if (image->colorspace == CMYKColorspace)
1559 case CyanPixelChannel:
1561 if ((flags & RhoValue) == 0)
1563 return(channel_info.rho);
1565 case MagentaPixelChannel:
1567 if ((flags & SigmaValue) == 0)
1569 return(channel_info.sigma);
1571 case YellowPixelChannel:
1573 if ((flags & XiValue) == 0)
1575 return(channel_info.xi);
1577 case BlackPixelChannel:
1579 if ((flags & PsiValue) == 0)
1581 return(channel_info.psi);
1583 case AlphaPixelChannel:
1585 if ((flags & ChiValue) == 0)
1587 return(channel_info.chi);
1594 case RedPixelChannel:
1596 if ((flags & RhoValue) == 0)
1598 return(channel_info.rho);
1600 case GreenPixelChannel:
1602 if ((flags & SigmaValue) == 0)
1604 return(channel_info.sigma);
1606 case BluePixelChannel:
1608 if ((flags & XiValue) == 0)
1610 return(channel_info.xi);
1612 case BlackPixelChannel:
1614 if ((flags & ChiValue) == 0)
1616 return(channel_info.chi);
1618 case AlphaPixelChannel:
1620 if ((flags & PsiValue) == 0)
1622 return(channel_info.psi);
1629 if (LocaleCompare(symbol,"c") == 0)
1630 return(QuantumScale*pixel.red);
1636 if (LocaleNCompare(symbol,"depth",5) == 0)
1637 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1643 if (LocaleCompare(symbol,"g") == 0)
1644 return(QuantumScale*pixel.green);
1650 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1651 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1652 if (LocaleCompare(symbol,"k") == 0)
1654 if (image->colorspace != CMYKColorspace)
1656 (void) ThrowMagickException(exception,GetMagickModule(),
1657 OptionError,"ColorSeparatedImageRequired","'%s'",
1661 return(QuantumScale*pixel.black);
1668 if (LocaleCompare(symbol,"h") == 0)
1669 return((double) image->rows);
1670 if (LocaleCompare(symbol,"hue") == 0)
1677 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1686 if ((LocaleCompare(symbol,"image.depth") == 0) ||
1687 (LocaleCompare(symbol,"image.minima") == 0) ||
1688 (LocaleCompare(symbol,"image.maxima") == 0) ||
1689 (LocaleCompare(symbol,"image.mean") == 0) ||
1690 (LocaleCompare(symbol,"image.kurtosis") == 0) ||
1691 (LocaleCompare(symbol,"image.skewness") == 0) ||
1692 (LocaleCompare(symbol,"image.standard_deviation") == 0))
1693 return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
1694 if (LocaleCompare(symbol,"image.resolution.x") == 0)
1695 return(image->resolution.x);
1696 if (LocaleCompare(symbol,"image.resolution.y") == 0)
1697 return(image->resolution.y);
1698 if (LocaleCompare(symbol,"intensity") == 0)
1699 return(QuantumScale*GetPixelInfoIntensity(&pixel));
1700 if (LocaleCompare(symbol,"i") == 0)
1707 if (LocaleCompare(symbol,"j") == 0)
1714 if (LocaleCompare(symbol,"lightness") == 0)
1721 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1725 if (LocaleCompare(symbol,"luminance") == 0)
1730 luminence=0.21267f*pixel.red+0.71516f*pixel.green+0.07217f*pixel.blue;
1731 return(QuantumScale*luminence);
1738 if (LocaleNCompare(symbol,"maxima",6) == 0)
1739 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1740 if (LocaleNCompare(symbol,"mean",4) == 0)
1741 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1742 if (LocaleNCompare(symbol,"minima",6) == 0)
1743 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1744 if (LocaleCompare(symbol,"m") == 0)
1745 return(QuantumScale*pixel.blue);
1751 if (LocaleCompare(symbol,"n") == 0)
1752 return((double) GetImageListLength(fx_info->images));
1758 if (LocaleCompare(symbol,"o") == 0)
1759 return(QuantumScale*pixel.alpha);
1765 if (LocaleCompare(symbol,"page.height") == 0)
1766 return((double) image->page.height);
1767 if (LocaleCompare(symbol,"page.width") == 0)
1768 return((double) image->page.width);
1769 if (LocaleCompare(symbol,"page.x") == 0)
1770 return((double) image->page.x);
1771 if (LocaleCompare(symbol,"page.y") == 0)
1772 return((double) image->page.y);
1778 if (LocaleCompare(symbol,"resolution.x") == 0)
1779 return(image->resolution.x);
1780 if (LocaleCompare(symbol,"resolution.y") == 0)
1781 return(image->resolution.y);
1782 if (LocaleCompare(symbol,"r") == 0)
1783 return(QuantumScale*pixel.red);
1789 if (LocaleCompare(symbol,"saturation") == 0)
1796 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1800 if (LocaleNCompare(symbol,"skewness",8) == 0)
1801 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1802 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1803 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1809 if (LocaleCompare(symbol,"t") == 0)
1810 return((double) GetImageIndexInList(fx_info->images));
1816 if (LocaleCompare(symbol,"w") == 0)
1817 return((double) image->columns);
1823 if (LocaleCompare(symbol,"y") == 0)
1824 return(QuantumScale*pixel.green);
1830 if (LocaleCompare(symbol,"z") == 0)
1835 depth=(double) GetImageDepth(image,fx_info->exception);
1843 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1844 if (value != (const char *) NULL)
1845 return((double) StringToDouble(value,(char **) NULL));
1846 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1847 "UnableToParseExpression","'%s'",symbol);
1851 static const char *FxOperatorPrecedence(const char *expression,
1852 ExceptionInfo *exception)
1856 UndefinedPrecedence,
1858 BitwiseComplementPrecedence,
1860 ExponentialNotationPrecedence,
1864 RelationalPrecedence,
1865 EquivalencyPrecedence,
1866 BitwiseAndPrecedence,
1867 BitwiseOrPrecedence,
1868 LogicalAndPrecedence,
1869 LogicalOrPrecedence,
1871 AssignmentPrecedence,
1891 subexpression=(const char *) NULL;
1892 target=NullPrecedence;
1893 while (*expression != '\0')
1895 precedence=UndefinedPrecedence;
1896 if ((isspace((int) ((char) *expression)) != 0) || (c == (int) '@'))
1901 switch (*expression)
1906 #if defined(MAGICKCORE_HAVE_ACOSH)
1907 if (LocaleNCompare(expression,"acosh",5) == 0)
1913 #if defined(MAGICKCORE_HAVE_ASINH)
1914 if (LocaleNCompare(expression,"asinh",5) == 0)
1920 #if defined(MAGICKCORE_HAVE_ATANH)
1921 if (LocaleNCompare(expression,"atanh",5) == 0)
1927 if (LocaleNCompare(expression,"atan2",5) == 0)
1937 if ((LocaleNCompare(expression,"E+",2) == 0) ||
1938 (LocaleNCompare(expression,"E-",2) == 0))
1940 expression+=2; /* scientific notation */
1947 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1948 (LocaleNCompare(expression,"j1",2) == 0))
1957 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1964 if ((c == (int) '{') || (c == (int) '['))
1967 if ((c == (int) '}') || (c == (int) ']'))
1970 switch ((unsigned char) *expression)
1975 precedence=BitwiseComplementPrecedence;
1981 precedence=ExponentPrecedence;
1986 if (((c != 0) && ((isdigit((int) ((char) c)) != 0) ||
1987 (strchr(")",c) != (char *) NULL))) &&
1988 (((islower((int) ((char) *expression)) != 0) ||
1989 (strchr("(",(int) *expression) != (char *) NULL)) ||
1990 ((isdigit((int) ((char) c)) == 0) &&
1991 (isdigit((int) ((char) *expression)) != 0))) &&
1992 (strchr("xy",(int) *expression) == (char *) NULL))
1993 precedence=MultiplyPrecedence;
2000 precedence=MultiplyPrecedence;
2006 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
2008 precedence=AdditionPrecedence;
2011 case LeftShiftOperator:
2012 case RightShiftOperator:
2014 precedence=ShiftPrecedence;
2018 case LessThanEqualOperator:
2019 case GreaterThanEqualOperator:
2022 precedence=RelationalPrecedence;
2026 case NotEqualOperator:
2028 precedence=EquivalencyPrecedence;
2033 precedence=BitwiseAndPrecedence;
2038 precedence=BitwiseOrPrecedence;
2041 case LogicalAndOperator:
2043 precedence=LogicalAndPrecedence;
2046 case LogicalOrOperator:
2048 precedence=LogicalOrPrecedence;
2051 case ExponentialNotation:
2053 precedence=ExponentialNotationPrecedence;
2059 precedence=TernaryPrecedence;
2064 precedence=AssignmentPrecedence;
2069 precedence=CommaPrecedence;
2074 precedence=SeparatorPrecedence;
2078 if ((precedence == BitwiseComplementPrecedence) ||
2079 (precedence == TernaryPrecedence) ||
2080 (precedence == AssignmentPrecedence))
2082 if (precedence > target)
2085 Right-to-left associativity.
2088 subexpression=expression;
2092 if (precedence >= target)
2095 Left-to-right associativity.
2098 subexpression=expression;
2100 if (strchr("(",(int) *expression) != (char *) NULL)
2101 expression=FxSubexpression(expression,exception);
2102 c=(int) (*expression++);
2104 return(subexpression);
2107 static double FxEvaluateSubexpression(FxInfo *fx_info,
2108 const PixelChannel channel,const ssize_t x,const ssize_t y,
2109 const char *expression,double *beta,ExceptionInfo *exception)
2113 subexpression[MaxTextExtent];
2123 if (exception->severity != UndefinedException)
2125 while (isspace((int) *expression) != 0)
2127 if (*expression == '\0')
2129 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
2130 "MissingExpression","'%s'",expression);
2133 *subexpression='\0';
2134 p=FxOperatorPrecedence(expression,exception);
2135 if (p != (const char *) NULL)
2137 (void) CopyMagickString(subexpression,expression,(size_t)
2139 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2141 switch ((unsigned char) *p)
2145 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2146 *beta=(double) (~(size_t) *beta);
2151 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2152 return(*beta == 0.0 ? 1.0 : 0.0);
2156 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2157 channel,x,y,++p,beta,exception));
2161 case ExponentialNotation:
2163 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2164 return(alpha*(*beta));
2168 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2171 if (exception->severity == UndefinedException)
2172 (void) ThrowMagickException(exception,GetMagickModule(),
2173 OptionError,"DivideByZero","'%s'",expression);
2176 return(alpha/(*beta));
2180 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2181 *beta=fabs(floor(((double) *beta)+0.5));
2184 (void) ThrowMagickException(exception,GetMagickModule(),
2185 OptionError,"DivideByZero","'%s'",expression);
2188 return(fmod((double) alpha,(double) *beta));
2192 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2193 return(alpha+(*beta));
2197 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2198 return(alpha-(*beta));
2200 case LeftShiftOperator:
2202 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2203 *beta=(double) ((size_t) (alpha+0.5) << (size_t) (gamma+0.5));
2206 case RightShiftOperator:
2208 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2209 *beta=(double) ((size_t) (alpha+0.5) >> (size_t) (gamma+0.5));
2214 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2215 return(alpha < *beta ? 1.0 : 0.0);
2217 case LessThanEqualOperator:
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(alpha > *beta ? 1.0 : 0.0);
2227 case GreaterThanEqualOperator:
2229 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2230 return(alpha >= *beta ? 1.0 : 0.0);
2234 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2235 return(fabs(alpha-(*beta)) < MagickEpsilon ? MagickEpsilon : 0.0);
2237 case NotEqualOperator:
2239 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2240 return(fabs(alpha-(*beta)) >= MagickEpsilon ? 1.0 : 0.0);
2244 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2245 *beta=(double) ((size_t) (alpha+0.5) & (size_t) (gamma+0.5));
2250 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2251 *beta=(double) ((size_t) (alpha+0.5) | (size_t) (gamma+0.5));
2254 case LogicalAndOperator:
2256 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2257 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2260 case LogicalOrOperator:
2262 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2263 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2271 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2273 p=StringToken(":",&q);
2274 if (q == (char *) NULL)
2276 (void) ThrowMagickException(exception,GetMagickModule(),
2277 OptionError,"UnableToParseExpression","'%s'",subexpression);
2280 if (fabs((double) alpha) >= MagickEpsilon)
2281 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2283 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2289 numeric[MaxTextExtent];
2292 while (isalpha((int) ((unsigned char) *q)) != 0)
2296 (void) ThrowMagickException(exception,GetMagickModule(),
2297 OptionError,"UnableToParseExpression","'%s'",subexpression);
2300 ClearMagickException(exception);
2301 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2302 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
2304 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2305 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2306 subexpression),ConstantString(numeric));
2311 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2316 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2321 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2327 if (strchr("(",(int) *expression) != (char *) NULL)
2329 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2330 subexpression[strlen(subexpression)-1]='\0';
2331 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2335 switch (*expression)
2339 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2345 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2351 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2353 return((double) (~(size_t) (gamma+0.5)));
2358 if (LocaleNCompare(expression,"abs",3) == 0)
2360 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2362 return((double) fabs((double) alpha));
2364 #if defined(MAGICKCORE_HAVE_ACOSH)
2365 if (LocaleNCompare(expression,"acosh",5) == 0)
2367 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2369 return((double) acosh((double) alpha));
2372 if (LocaleNCompare(expression,"acos",4) == 0)
2374 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2376 return((double) acos((double) alpha));
2378 #if defined(MAGICKCORE_HAVE_J1)
2379 if (LocaleNCompare(expression,"airy",4) == 0)
2381 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2385 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
2386 return(gamma*gamma);
2389 #if defined(MAGICKCORE_HAVE_ASINH)
2390 if (LocaleNCompare(expression,"asinh",5) == 0)
2392 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2394 return((double) asinh((double) alpha));
2397 if (LocaleNCompare(expression,"asin",4) == 0)
2399 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2401 return((double) asin((double) alpha));
2403 if (LocaleNCompare(expression,"alt",3) == 0)
2405 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2407 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
2409 if (LocaleNCompare(expression,"atan2",5) == 0)
2411 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2413 return((double) atan2((double) alpha,(double) *beta));
2415 #if defined(MAGICKCORE_HAVE_ATANH)
2416 if (LocaleNCompare(expression,"atanh",5) == 0)
2418 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2420 return((double) atanh((double) alpha));
2423 if (LocaleNCompare(expression,"atan",4) == 0)
2425 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2427 return((double) atan((double) alpha));
2429 if (LocaleCompare(expression,"a") == 0)
2430 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2436 if (LocaleCompare(expression,"b") == 0)
2437 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2443 if (LocaleNCompare(expression,"ceil",4) == 0)
2445 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2447 return((double) ceil((double) alpha));
2449 if (LocaleNCompare(expression,"cosh",4) == 0)
2451 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2453 return((double) cosh((double) alpha));
2455 if (LocaleNCompare(expression,"cos",3) == 0)
2457 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2459 return((double) cos((double) alpha));
2461 if (LocaleCompare(expression,"c") == 0)
2462 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2468 if (LocaleNCompare(expression,"debug",5) == 0)
2473 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2475 if (fx_info->images->colorspace == CMYKColorspace)
2478 case CyanPixelChannel: type="cyan"; break;
2479 case MagentaPixelChannel: type="magenta"; break;
2480 case YellowPixelChannel: type="yellow"; break;
2481 case AlphaPixelChannel: type="opacity"; break;
2482 case BlackPixelChannel: type="black"; break;
2483 default: type="unknown"; break;
2488 case RedPixelChannel: type="red"; break;
2489 case GreenPixelChannel: type="green"; break;
2490 case BluePixelChannel: type="blue"; break;
2491 case AlphaPixelChannel: type="opacity"; break;
2492 default: type="unknown"; break;
2494 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2495 if (strlen(subexpression) > 1)
2496 subexpression[strlen(subexpression)-1]='\0';
2497 if (fx_info->file != (FILE *) NULL)
2498 (void) FormatLocaleFile(fx_info->file,"%s[%.20g,%.20g].%s: "
2499 "%s=%.*g\n",fx_info->images->filename,(double) x,(double) y,type,
2500 subexpression,GetMagickPrecision(),(double) alpha);
2503 if (LocaleNCompare(expression,"drc",3) == 0)
2505 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2507 return((double) (alpha/(*beta*(alpha-1.0)+1.0)));
2514 if (LocaleCompare(expression,"epsilon") == 0)
2515 return((double) MagickEpsilon);
2516 if (LocaleNCompare(expression,"exp",3) == 0)
2518 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2520 return((double) exp((double) alpha));
2522 if (LocaleCompare(expression,"e") == 0)
2523 return((double) 2.7182818284590452354);
2529 if (LocaleNCompare(expression,"floor",5) == 0)
2531 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2533 return((double) floor((double) alpha));
2540 if (LocaleNCompare(expression,"gauss",5) == 0)
2542 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2544 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
2545 return((double) gamma);
2547 if (LocaleNCompare(expression,"gcd",3) == 0)
2552 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2554 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType) (*beta+
2556 return((double) gcd);
2558 if (LocaleCompare(expression,"g") == 0)
2559 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2565 if (LocaleCompare(expression,"h") == 0)
2566 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2567 if (LocaleCompare(expression,"hue") == 0)
2568 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2569 if (LocaleNCompare(expression,"hypot",5) == 0)
2571 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2573 return((double) hypot((double) alpha,(double) *beta));
2580 if (LocaleCompare(expression,"k") == 0)
2581 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2587 if (LocaleCompare(expression,"intensity") == 0)
2588 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2589 if (LocaleNCompare(expression,"int",3) == 0)
2591 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2593 return((double) floor(alpha));
2595 #if defined(MAGICKCORE_HAVE_ISNAN)
2596 if (LocaleNCompare(expression,"isnan",5) == 0)
2598 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2600 return((double) !!isnan((double) alpha));
2603 if (LocaleCompare(expression,"i") == 0)
2604 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2610 if (LocaleCompare(expression,"j") == 0)
2611 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2612 #if defined(MAGICKCORE_HAVE_J0)
2613 if (LocaleNCompare(expression,"j0",2) == 0)
2615 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2617 return((double) j0((double) alpha));
2620 #if defined(MAGICKCORE_HAVE_J1)
2621 if (LocaleNCompare(expression,"j1",2) == 0)
2623 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2625 return((double) j1((double) alpha));
2628 #if defined(MAGICKCORE_HAVE_J1)
2629 if (LocaleNCompare(expression,"jinc",4) == 0)
2631 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2635 gamma=(double) (2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha));
2644 if (LocaleNCompare(expression,"ln",2) == 0)
2646 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2648 return((double) log((double) alpha));
2650 if (LocaleNCompare(expression,"logtwo",6) == 0)
2652 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2654 return((double) log10((double) alpha))/log10(2.0);
2656 if (LocaleNCompare(expression,"log",3) == 0)
2658 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2660 return((double) log10((double) alpha));
2662 if (LocaleCompare(expression,"lightness") == 0)
2663 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2669 if (LocaleCompare(expression,"MaxRGB") == 0)
2670 return((double) QuantumRange);
2671 if (LocaleNCompare(expression,"maxima",6) == 0)
2673 if (LocaleNCompare(expression,"max",3) == 0)
2675 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2677 return(alpha > *beta ? alpha : *beta);
2679 if (LocaleNCompare(expression,"minima",6) == 0)
2681 if (LocaleNCompare(expression,"min",3) == 0)
2683 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2685 return(alpha < *beta ? alpha : *beta);
2687 if (LocaleNCompare(expression,"mod",3) == 0)
2689 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2691 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2694 if (LocaleCompare(expression,"m") == 0)
2695 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2701 if (LocaleNCompare(expression,"not",3) == 0)
2703 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2705 return((double) (alpha < MagickEpsilon));
2707 if (LocaleCompare(expression,"n") == 0)
2708 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2714 if (LocaleCompare(expression,"Opaque") == 0)
2716 if (LocaleCompare(expression,"o") == 0)
2717 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2723 if (LocaleCompare(expression,"phi") == 0)
2724 return((double) MagickPHI);
2725 if (LocaleCompare(expression,"pi") == 0)
2726 return((double) MagickPI);
2727 if (LocaleNCompare(expression,"pow",3) == 0)
2729 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2731 return((double) pow((double) alpha,(double) *beta));
2733 if (LocaleCompare(expression,"p") == 0)
2734 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2740 if (LocaleCompare(expression,"QuantumRange") == 0)
2741 return((double) QuantumRange);
2742 if (LocaleCompare(expression,"QuantumScale") == 0)
2743 return((double) QuantumScale);
2749 if (LocaleNCompare(expression,"rand",4) == 0)
2750 return((double) GetPseudoRandomValue(fx_info->random_info));
2751 if (LocaleNCompare(expression,"round",5) == 0)
2753 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2755 return((double) floor((double) alpha+0.5));
2757 if (LocaleCompare(expression,"r") == 0)
2758 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2764 if (LocaleCompare(expression,"saturation") == 0)
2765 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2766 if (LocaleNCompare(expression,"sign",4) == 0)
2768 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2770 return(alpha < 0.0 ? -1.0 : 1.0);
2772 if (LocaleNCompare(expression,"sinc",4) == 0)
2774 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2778 gamma=(double) (sin((double) (MagickPI*alpha))/
2782 if (LocaleNCompare(expression,"sinh",4) == 0)
2784 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2786 return((double) sinh((double) alpha));
2788 if (LocaleNCompare(expression,"sin",3) == 0)
2790 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2792 return((double) sin((double) alpha));
2794 if (LocaleNCompare(expression,"sqrt",4) == 0)
2796 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2798 return((double) sqrt((double) alpha));
2800 if (LocaleNCompare(expression,"squish",6) == 0)
2802 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2804 return((double) (1.0/(1.0+exp((double) (-alpha)))));
2806 if (LocaleCompare(expression,"s") == 0)
2807 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2813 if (LocaleNCompare(expression,"tanh",4) == 0)
2815 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2817 return((double) tanh((double) alpha));
2819 if (LocaleNCompare(expression,"tan",3) == 0)
2821 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2823 return((double) tan((double) alpha));
2825 if (LocaleCompare(expression,"Transparent") == 0)
2827 if (LocaleNCompare(expression,"trunc",5) == 0)
2829 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2832 return((double) floor((double) alpha));
2833 return((double) ceil((double) alpha));
2835 if (LocaleCompare(expression,"t") == 0)
2836 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2842 if (LocaleCompare(expression,"u") == 0)
2843 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2849 if (LocaleCompare(expression,"v") == 0)
2850 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2856 if (LocaleNCompare(expression,"while",5) == 0)
2860 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2862 } while (fabs((double) alpha) >= MagickEpsilon);
2863 return((double) *beta);
2865 if (LocaleCompare(expression,"w") == 0)
2866 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2872 if (LocaleCompare(expression,"y") == 0)
2873 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2879 if (LocaleCompare(expression,"z") == 0)
2880 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2886 q=(char *) expression;
2887 alpha=InterpretSiPrefixValue(expression,&q);
2888 if (q == expression)
2889 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2893 MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
2894 double *alpha,ExceptionInfo *exception)
2899 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2904 MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
2905 double *alpha,ExceptionInfo *exception)
2914 fx_info->file=(FILE *) NULL;
2915 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2921 MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
2922 const PixelChannel channel,const ssize_t x,const ssize_t y,
2923 double *alpha,ExceptionInfo *exception)
2929 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2931 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2935 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2943 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2945 % FxImage() applies a mathematical expression to the specified image.
2947 % The format of the FxImage method is:
2949 % Image *FxImage(const Image *image,const char *expression,
2950 % ExceptionInfo *exception)
2952 % A description of each parameter follows:
2954 % o image: the image.
2956 % o expression: A mathematical expression.
2958 % o exception: return any errors or warnings in this structure.
2962 static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2967 assert(fx_info != (FxInfo **) NULL);
2968 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
2969 if (fx_info[i] != (FxInfo *) NULL)
2970 fx_info[i]=DestroyFxInfo(fx_info[i]);
2971 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
2975 static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2976 ExceptionInfo *exception)
2993 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
2994 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
2995 if (fx_info == (FxInfo **) NULL)
2997 (void) ThrowMagickException(exception,GetMagickModule(),
2998 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
2999 return((FxInfo **) NULL);
3001 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
3002 if (*expression != '@')
3003 fx_expression=ConstantString(expression);
3005 fx_expression=FileToString(expression+1,~0,exception);
3006 for (i=0; i < (ssize_t) number_threads; i++)
3011 fx_info[i]=AcquireFxInfo(image,fx_expression,exception);
3012 if (fx_info[i] == (FxInfo *) NULL)
3014 status=FxPreprocessExpression(fx_info[i],&alpha,exception);
3015 if (status == MagickFalse)
3018 fx_expression=DestroyString(fx_expression);
3019 if (i < (ssize_t) number_threads)
3020 fx_info=DestroyFxThreadSet(fx_info);
3024 MagickExport Image *FxImage(const Image *image,const char *expression,
3025 ExceptionInfo *exception)
3027 #define FxImageTag "Fx/Image"
3048 assert(image != (Image *) NULL);
3049 assert(image->signature == MagickSignature);
3050 if (image->debug != MagickFalse)
3051 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3052 fx_info=AcquireFxThreadSet(image,expression,exception);
3053 if (fx_info == (FxInfo **) NULL)
3054 return((Image *) NULL);
3055 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3056 if (fx_image == (Image *) NULL)
3058 fx_info=DestroyFxThreadSet(fx_info);
3059 return((Image *) NULL);
3061 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
3063 fx_info=DestroyFxThreadSet(fx_info);
3064 fx_image=DestroyImage(fx_image);
3065 return((Image *) NULL);
3072 image_view=AcquireVirtualCacheView(image,exception);
3073 fx_view=AcquireAuthenticCacheView(fx_image,exception);
3074 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3075 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3076 dynamic_number_threads(image,fx_image,fx_image->rows,1)
3078 for (y=0; y < (ssize_t) fx_image->rows; y++)
3081 id = GetOpenMPThreadId();
3083 register const Quantum
3092 if (status == MagickFalse)
3094 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3095 q=QueueCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
3096 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3101 for (x=0; x < (ssize_t) fx_image->columns; x++)
3106 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3118 channel=GetPixelChannelChannel(image,i);
3119 traits=GetPixelChannelTraits(image,channel);
3120 fx_traits=GetPixelChannelTraits(fx_image,channel);
3121 if ((traits == UndefinedPixelTrait) ||
3122 (fx_traits == UndefinedPixelTrait))
3124 if (((fx_traits & CopyPixelTrait) != 0) ||
3125 (GetPixelMask(image,p) != 0))
3127 SetPixelChannel(fx_image,channel,p[i],q);
3131 (void) FxEvaluateChannelExpression(fx_info[id],channel,x,y,&alpha,
3133 q[i]=ClampToQuantum(QuantumRange*alpha);
3135 p+=GetPixelChannels(image);
3136 q+=GetPixelChannels(fx_image);
3138 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3140 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3145 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3146 #pragma omp critical (MagickCore_FxImage)
3148 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3149 if (proceed == MagickFalse)
3153 fx_view=DestroyCacheView(fx_view);
3154 image_view=DestroyCacheView(image_view);
3155 fx_info=DestroyFxThreadSet(fx_info);
3156 if (status == MagickFalse)
3157 fx_image=DestroyImage(fx_image);
3162 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3166 % I m p l o d e I m a g e %
3170 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3172 % ImplodeImage() creates a new image that is a copy of an existing
3173 % one with the image pixels "implode" by the specified percentage. It
3174 % allocates the memory necessary for the new Image structure and returns a
3175 % pointer to the new image.
3177 % The format of the ImplodeImage method is:
3179 % Image *ImplodeImage(const Image *image,const double amount,
3180 % const PixelInterpolateMethod method,ExceptionInfo *exception)
3182 % A description of each parameter follows:
3184 % o implode_image: Method ImplodeImage returns a pointer to the image
3185 % after it is implode. A null image is returned if there is a memory
3188 % o image: the image.
3190 % o amount: Define the extent of the implosion.
3192 % o method: the pixel interpolation method.
3194 % o exception: return any errors or warnings in this structure.
3197 MagickExport Image *ImplodeImage(const Image *image,const double amount,
3198 const PixelInterpolateMethod method,ExceptionInfo *exception)
3200 #define ImplodeImageTag "Implode/Image"
3226 Initialize implode image attributes.
3228 assert(image != (Image *) NULL);
3229 assert(image->signature == MagickSignature);
3230 if (image->debug != MagickFalse)
3231 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3232 assert(exception != (ExceptionInfo *) NULL);
3233 assert(exception->signature == MagickSignature);
3234 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3236 if (implode_image == (Image *) NULL)
3237 return((Image *) NULL);
3238 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
3240 implode_image=DestroyImage(implode_image);
3241 return((Image *) NULL);
3243 if (implode_image->background_color.alpha != OpaqueAlpha)
3244 implode_image->alpha_trait=BlendPixelTrait;
3246 Compute scaling factor.
3250 center.x=0.5*image->columns;
3251 center.y=0.5*image->rows;
3253 if (image->columns > image->rows)
3254 scale.y=(double) image->columns/(double) image->rows;
3256 if (image->columns < image->rows)
3258 scale.x=(double) image->rows/(double) image->columns;
3266 image_view=AcquireVirtualCacheView(image,exception);
3267 implode_view=AcquireAuthenticCacheView(implode_image,exception);
3268 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3269 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3270 dynamic_number_threads(image,implode_image,image->rows,1)
3272 for (y=0; y < (ssize_t) image->rows; y++)
3280 register const Quantum
3289 if (status == MagickFalse)
3291 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3292 q=QueueCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
3294 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3299 delta.y=scale.y*(double) (y-center.y);
3300 for (x=0; x < (ssize_t) image->columns; x++)
3306 Determine if the pixel is within an ellipse.
3308 if (GetPixelMask(image,p) != 0)
3310 p+=GetPixelChannels(image);
3311 q+=GetPixelChannels(implode_image);
3314 delta.x=scale.x*(double) (x-center.x);
3315 distance=delta.x*delta.x+delta.y*delta.y;
3316 if (distance >= (radius*radius))
3317 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3326 channel=GetPixelChannelChannel(image,i);
3327 traits=GetPixelChannelTraits(image,channel);
3328 implode_traits=GetPixelChannelTraits(implode_image,channel);
3329 if ((traits == UndefinedPixelTrait) ||
3330 (implode_traits == UndefinedPixelTrait))
3332 SetPixelChannel(implode_image,channel,p[i],q);
3344 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/radius/
3346 status=InterpolatePixelChannels(image,image_view,implode_image,method,
3347 (double) (factor*delta.x/scale.x+center.x),(double) (factor*delta.y/
3348 scale.y+center.y),q,exception);
3350 p+=GetPixelChannels(image);
3351 q+=GetPixelChannels(implode_image);
3353 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3355 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3360 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3361 #pragma omp critical (MagickCore_ImplodeImage)
3363 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3364 if (proceed == MagickFalse)
3368 implode_view=DestroyCacheView(implode_view);
3369 image_view=DestroyCacheView(image_view);
3370 if (status == MagickFalse)
3371 implode_image=DestroyImage(implode_image);
3372 return(implode_image);
3376 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3380 % M o r p h I m a g e s %
3384 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3386 % The MorphImages() method requires a minimum of two images. The first
3387 % image is transformed into the second by a number of intervening images
3388 % as specified by frames.
3390 % The format of the MorphImage method is:
3392 % Image *MorphImages(const Image *image,const size_t number_frames,
3393 % ExceptionInfo *exception)
3395 % A description of each parameter follows:
3397 % o image: the image.
3399 % o number_frames: Define the number of in-between image to generate.
3400 % The more in-between frames, the smoother the morph.
3402 % o exception: return any errors or warnings in this structure.
3405 MagickExport Image *MorphImages(const Image *image,const size_t number_frames,
3406 ExceptionInfo *exception)
3408 #define MorphImageTag "Morph/Image"
3424 register const Image
3434 Clone first frame in sequence.
3436 assert(image != (Image *) NULL);
3437 assert(image->signature == MagickSignature);
3438 if (image->debug != MagickFalse)
3439 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3440 assert(exception != (ExceptionInfo *) NULL);
3441 assert(exception->signature == MagickSignature);
3442 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3443 if (morph_images == (Image *) NULL)
3444 return((Image *) NULL);
3445 if (GetNextImageInList(image) == (Image *) NULL)
3450 for (i=1; i < (ssize_t) number_frames; i++)
3452 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3453 if (morph_image == (Image *) NULL)
3455 morph_images=DestroyImageList(morph_images);
3456 return((Image *) NULL);
3458 AppendImageToList(&morph_images,morph_image);
3459 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3464 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3466 if (proceed == MagickFalse)
3470 return(GetFirstImageInList(morph_images));
3473 Morph image sequence.
3478 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3480 for (i=0; i < (ssize_t) number_frames; i++)
3486 beta=(double) (i+1.0)/(double) (number_frames+1.0);
3488 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
3489 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*next->rows+beta*
3490 GetNextImageInList(next)->rows+0.5),next->filter,exception);
3491 if (morph_image == (Image *) NULL)
3493 morph_images=DestroyImageList(morph_images);
3494 return((Image *) NULL);
3496 status=SetImageStorageClass(morph_image,DirectClass,exception);
3497 if (status == MagickFalse)
3499 morph_image=DestroyImage(morph_image);
3500 return((Image *) NULL);
3502 AppendImageToList(&morph_images,morph_image);
3503 morph_images=GetLastImageInList(morph_images);
3504 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
3505 morph_images->rows,GetNextImageInList(next)->filter,exception);
3506 if (morph_image == (Image *) NULL)
3508 morph_images=DestroyImageList(morph_images);
3509 return((Image *) NULL);
3511 image_view=AcquireVirtualCacheView(morph_image,exception);
3512 morph_view=AcquireAuthenticCacheView(morph_images,exception);
3513 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3514 #pragma omp parallel for schedule(static,4) shared(status) \
3515 dynamic_number_threads(morph_image,morph_image,morph_image->rows,1)
3517 for (y=0; y < (ssize_t) morph_images->rows; y++)
3522 register const Quantum
3531 if (status == MagickFalse)
3533 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3535 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3537 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3542 for (x=0; x < (ssize_t) morph_images->columns; x++)
3547 for (i=0; i < (ssize_t) GetPixelChannels(morph_image); i++)
3556 channel=GetPixelChannelChannel(image,i);
3557 traits=GetPixelChannelTraits(image,channel);
3558 morph_traits=GetPixelChannelTraits(morph_image,channel);
3559 if ((traits == UndefinedPixelTrait) ||
3560 (morph_traits == UndefinedPixelTrait))
3562 if (((morph_traits & CopyPixelTrait) != 0) ||
3563 (GetPixelMask(image,p) != 0))
3565 SetPixelChannel(morph_image,channel,p[i],q);
3568 SetPixelChannel(morph_image,channel,ClampToQuantum(alpha*
3569 GetPixelChannel(morph_images,channel,q)+beta*p[i]),q);
3571 p+=GetPixelChannels(morph_image);
3572 q+=GetPixelChannels(morph_images);
3574 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3575 if (sync == MagickFalse)
3578 morph_view=DestroyCacheView(morph_view);
3579 image_view=DestroyCacheView(image_view);
3580 morph_image=DestroyImage(morph_image);
3582 if (i < (ssize_t) number_frames)
3585 Clone last frame in sequence.
3587 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3588 if (morph_image == (Image *) NULL)
3590 morph_images=DestroyImageList(morph_images);
3591 return((Image *) NULL);
3593 AppendImageToList(&morph_images,morph_image);
3594 morph_images=GetLastImageInList(morph_images);
3595 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3600 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3601 #pragma omp critical (MagickCore_MorphImages)
3603 proceed=SetImageProgress(image,MorphImageTag,scene,
3604 GetImageListLength(image));
3605 if (proceed == MagickFalse)
3610 if (GetNextImageInList(next) != (Image *) NULL)
3612 morph_images=DestroyImageList(morph_images);
3613 return((Image *) NULL);
3615 return(GetFirstImageInList(morph_images));
3619 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3623 % P l a s m a I m a g e %
3627 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3629 % PlasmaImage() initializes an image with plasma fractal values. The image
3630 % must be initialized with a base color and the random number generator
3631 % seeded before this method is called.
3633 % The format of the PlasmaImage method is:
3635 % MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
3636 % size_t attenuate,size_t depth,ExceptionInfo *exception)
3638 % A description of each parameter follows:
3640 % o image: the image.
3642 % o segment: Define the region to apply plasma fractals values.
3644 % o attenuate: Define the plasma attenuation factor.
3646 % o depth: Limit the plasma recursion depth.
3648 % o exception: return any errors or warnings in this structure.
3652 static inline Quantum PlasmaPixel(RandomInfo *random_info,
3653 const double pixel,const double noise)
3658 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
3663 static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3664 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3665 const SegmentInfo *segment,size_t attenuate,size_t depth,
3666 ExceptionInfo *exception)
3677 register const Quantum
3693 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3701 Divide the area into quadrants and recurse.
3705 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3706 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
3707 local_info=(*segment);
3708 local_info.x2=(double) x_mid;
3709 local_info.y2=(double) y_mid;
3710 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3711 &local_info,attenuate,depth,exception);
3712 local_info=(*segment);
3713 local_info.y1=(double) y_mid;
3714 local_info.x2=(double) x_mid;
3715 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3716 &local_info,attenuate,depth,exception);
3717 local_info=(*segment);
3718 local_info.x1=(double) x_mid;
3719 local_info.y2=(double) y_mid;
3720 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3721 &local_info,attenuate,depth,exception);
3722 local_info=(*segment);
3723 local_info.x1=(double) x_mid;
3724 local_info.y1=(double) y_mid;
3725 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3726 &local_info,attenuate,depth,exception));
3728 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3729 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
3730 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3731 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3732 return(MagickFalse);
3734 Average pixels and apply plasma.
3736 plasma=(double) QuantumRange/(2.0*attenuate);
3737 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3742 x=(ssize_t) ceil(segment->x1-0.5);
3743 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),1,1,
3745 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),1,1,
3747 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
3748 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3749 (q == (Quantum *) NULL))
3751 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3753 channel=GetPixelChannelChannel(image,i);
3754 traits=GetPixelChannelTraits(image,channel);
3755 if (traits == UndefinedPixelTrait)
3757 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3759 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3760 if (segment->x1 != segment->x2)
3765 x=(ssize_t) ceil(segment->x2-0.5);
3766 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3768 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3770 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
3771 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3772 (q == (Quantum *) NULL))
3774 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3776 channel=GetPixelChannelChannel(image,i);
3777 traits=GetPixelChannelTraits(image,channel);
3778 if (traits == UndefinedPixelTrait)
3780 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3782 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3785 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3787 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3792 y=(ssize_t) ceil(segment->y2-0.5);
3793 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3795 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3797 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
3798 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3799 (q == (Quantum *) NULL))
3801 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3803 channel=GetPixelChannelChannel(image,i);
3804 traits=GetPixelChannelTraits(image,channel);
3805 if (traits == UndefinedPixelTrait)
3807 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3809 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3811 if (segment->y1 != segment->y2)
3816 y=(ssize_t) ceil(segment->y1-0.5);
3817 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3819 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3821 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
3822 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3823 (q == (Quantum *) NULL))
3825 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3827 channel=GetPixelChannelChannel(image,i);
3828 traits=GetPixelChannelTraits(image,channel);
3829 if (traits == UndefinedPixelTrait)
3831 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3833 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3836 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3841 x=(ssize_t) ceil(segment->x1-0.5);
3842 y=(ssize_t) ceil(segment->y1-0.5);
3843 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
3844 x=(ssize_t) ceil(segment->x2-0.5);
3845 y=(ssize_t) ceil(segment->y2-0.5);
3846 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
3847 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
3848 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3849 (q == (Quantum *) NULL))
3851 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3853 channel=GetPixelChannelChannel(image,i);
3854 traits=GetPixelChannelTraits(image,channel);
3855 if (traits == UndefinedPixelTrait)
3857 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3859 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3861 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3863 return(MagickFalse);
3866 MagickExport MagickBooleanType PlasmaImage(Image *image,
3867 const SegmentInfo *segment,size_t attenuate,size_t depth,
3868 ExceptionInfo *exception)
3881 if (image->debug != MagickFalse)
3882 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3883 assert(image != (Image *) NULL);
3884 assert(image->signature == MagickSignature);
3885 if (image->debug != MagickFalse)
3886 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3887 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
3888 return(MagickFalse);
3889 image_view=AcquireAuthenticCacheView(image,exception);
3890 u_view=AcquireVirtualCacheView(image,exception);
3891 v_view=AcquireVirtualCacheView(image,exception);
3892 random_info=AcquireRandomInfo();
3893 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3894 attenuate,depth,exception);
3895 random_info=DestroyRandomInfo(random_info);
3896 v_view=DestroyCacheView(v_view);
3897 u_view=DestroyCacheView(u_view);
3898 image_view=DestroyCacheView(image_view);
3903 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3907 % P o l a r o i d I m a g e %
3911 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3913 % PolaroidImage() simulates a Polaroid picture.
3915 % The format of the AnnotateImage method is:
3917 % Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
3918 % const char *caption,const double angle,
3919 % const PixelInterpolateMethod method,ExceptionInfo exception)
3921 % A description of each parameter follows:
3923 % o image: the image.
3925 % o draw_info: the draw info.
3927 % o caption: the Polaroid caption.
3929 % o angle: Apply the effect along this angle.
3931 % o method: the pixel interpolation method.
3933 % o exception: return any errors or warnings in this structure.
3936 MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
3937 const char *caption,const double angle,const PixelInterpolateMethod method,
3938 ExceptionInfo *exception)
3956 Simulate a Polaroid picture.
3958 assert(image != (Image *) NULL);
3959 assert(image->signature == MagickSignature);
3960 if (image->debug != MagickFalse)
3961 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3962 assert(exception != (ExceptionInfo *) NULL);
3963 assert(exception->signature == MagickSignature);
3964 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
3965 image->rows)/25.0,10.0);
3966 height=image->rows+2*quantum;
3967 caption_image=(Image *) NULL;
3968 if (caption != (const char *) NULL)
3971 geometry[MaxTextExtent],
3987 Generate caption image.
3989 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3990 if (caption_image == (Image *) NULL)
3991 return((Image *) NULL);
3992 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
3993 text=InterpretImageProperties((ImageInfo *) NULL,(Image *) image,caption,
3995 (void) CloneString(&annotate_info->text,text);
3996 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
3998 status=SetImageExtent(caption_image,image->columns,(size_t) ((count+1)*
3999 (metrics.ascent-metrics.descent)+0.5),exception);
4000 if (status == MagickFalse)
4001 caption_image=DestroyImage(caption_image);
4004 caption_image->background_color=image->border_color;
4005 (void) SetImageBackgroundColor(caption_image,exception);
4006 (void) CloneString(&annotate_info->text,text);
4007 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
4009 if (annotate_info->gravity == UndefinedGravity)
4010 (void) CloneString(&annotate_info->geometry,AcquireString(
4012 (void) AnnotateImage(caption_image,annotate_info,exception);
4013 height+=caption_image->rows;
4015 annotate_info=DestroyDrawInfo(annotate_info);
4016 text=DestroyString(text);
4018 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
4020 if (picture_image == (Image *) NULL)
4022 if (caption_image != (Image *) NULL)
4023 caption_image=DestroyImage(caption_image);
4024 return((Image *) NULL);
4026 picture_image->background_color=image->border_color;
4027 (void) SetImageBackgroundColor(picture_image,exception);
4028 (void) CompositeImage(picture_image,image,OverCompositeOp,MagickTrue,quantum,
4030 if (caption_image != (Image *) NULL)
4032 (void) CompositeImage(picture_image,caption_image,OverCompositeOp,
4033 MagickTrue,quantum,(ssize_t) (image->rows+3*quantum/2),exception);
4034 caption_image=DestroyImage(caption_image);
4036 (void) QueryColorCompliance("none",AllCompliance,
4037 &picture_image->background_color,exception);
4038 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
4039 rotate_image=RotateImage(picture_image,90.0,exception);
4040 picture_image=DestroyImage(picture_image);
4041 if (rotate_image == (Image *) NULL)
4042 return((Image *) NULL);
4043 picture_image=rotate_image;
4044 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
4045 picture_image->columns,method,exception);
4046 picture_image=DestroyImage(picture_image);
4047 if (bend_image == (Image *) NULL)
4048 return((Image *) NULL);
4049 picture_image=bend_image;
4050 rotate_image=RotateImage(picture_image,-90.0,exception);
4051 picture_image=DestroyImage(picture_image);
4052 if (rotate_image == (Image *) NULL)
4053 return((Image *) NULL);
4054 picture_image=rotate_image;
4055 picture_image->background_color=image->background_color;
4056 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
4058 if (polaroid_image == (Image *) NULL)
4060 picture_image=DestroyImage(picture_image);
4061 return(picture_image);
4063 flop_image=FlopImage(polaroid_image,exception);
4064 polaroid_image=DestroyImage(polaroid_image);
4065 if (flop_image == (Image *) NULL)
4067 picture_image=DestroyImage(picture_image);
4068 return(picture_image);
4070 polaroid_image=flop_image;
4071 (void) CompositeImage(polaroid_image,picture_image,OverCompositeOp,
4072 MagickTrue,(ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
4073 picture_image=DestroyImage(picture_image);
4074 (void) QueryColorCompliance("none",AllCompliance,
4075 &polaroid_image->background_color,exception);
4076 rotate_image=RotateImage(polaroid_image,angle,exception);
4077 polaroid_image=DestroyImage(polaroid_image);
4078 if (rotate_image == (Image *) NULL)
4079 return((Image *) NULL);
4080 polaroid_image=rotate_image;
4081 trim_image=TrimImage(polaroid_image,exception);
4082 polaroid_image=DestroyImage(polaroid_image);
4083 if (trim_image == (Image *) NULL)
4084 return((Image *) NULL);
4085 polaroid_image=trim_image;
4086 return(polaroid_image);
4090 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4094 % S e p i a T o n e I m a g e %
4098 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4100 % MagickSepiaToneImage() applies a special effect to the image, similar to the
4101 % effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4102 % 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4103 % threshold of 80% is a good starting point for a reasonable tone.
4105 % The format of the SepiaToneImage method is:
4107 % Image *SepiaToneImage(const Image *image,const double threshold,
4108 % ExceptionInfo *exception)
4110 % A description of each parameter follows:
4112 % o image: the image.
4114 % o threshold: the tone threshold.
4116 % o exception: return any errors or warnings in this structure.
4119 MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4120 ExceptionInfo *exception)
4122 #define SepiaToneImageTag "SepiaTone/Image"
4141 Initialize sepia-toned image attributes.
4143 assert(image != (const Image *) NULL);
4144 assert(image->signature == MagickSignature);
4145 if (image->debug != MagickFalse)
4146 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4147 assert(exception != (ExceptionInfo *) NULL);
4148 assert(exception->signature == MagickSignature);
4149 sepia_image=CloneImage(image,0,0,MagickTrue,exception);
4150 if (sepia_image == (Image *) NULL)
4151 return((Image *) NULL);
4152 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
4154 sepia_image=DestroyImage(sepia_image);
4155 return((Image *) NULL);
4158 Tone each row of the image.
4162 image_view=AcquireVirtualCacheView(image,exception);
4163 sepia_view=AcquireAuthenticCacheView(sepia_image,exception);
4164 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4165 #pragma omp parallel for schedule(static,4) shared(progress,status) \
4166 dynamic_number_threads(image,sepia_image,image->rows,1)
4168 for (y=0; y < (ssize_t) image->rows; y++)
4170 register const Quantum
4179 if (status == MagickFalse)
4181 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
4182 q=GetCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
4184 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
4189 for (x=0; x < (ssize_t) image->columns; x++)
4195 intensity=GetPixelIntensity(image,p);
4196 tone=intensity > threshold ? (double) QuantumRange : intensity+
4197 (double) QuantumRange-threshold;
4198 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
4199 tone=intensity > (7.0*threshold/6.0) ? (double) QuantumRange :
4200 intensity+(double) QuantumRange-7.0*threshold/6.0;
4201 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4202 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
4203 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
4205 if ((double) GetPixelGreen(image,q) < tone)
4206 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4207 if ((double) GetPixelBlue(image,q) < tone)
4208 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
4209 p+=GetPixelChannels(image);
4210 q+=GetPixelChannels(sepia_image);
4212 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4214 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4219 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4220 #pragma omp critical (MagickCore_SepiaToneImage)
4222 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4224 if (proceed == MagickFalse)
4228 sepia_view=DestroyCacheView(sepia_view);
4229 image_view=DestroyCacheView(image_view);
4230 (void) NormalizeImage(sepia_image,exception);
4231 (void) ContrastImage(sepia_image,MagickTrue,exception);
4232 if (status == MagickFalse)
4233 sepia_image=DestroyImage(sepia_image);
4234 return(sepia_image);
4238 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4242 % S h a d o w I m a g e %
4246 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4248 % ShadowImage() simulates a shadow from the specified image and returns it.
4250 % The format of the ShadowImage method is:
4252 % Image *ShadowImage(const Image *image,const double alpha,
4253 % const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4254 % ExceptionInfo *exception)
4256 % A description of each parameter follows:
4258 % o image: the image.
4260 % o alpha: percentage transparency.
4262 % o sigma: the standard deviation of the Gaussian, in pixels.
4264 % o x_offset: the shadow x-offset.
4266 % o y_offset: the shadow y-offset.
4268 % o exception: return any errors or warnings in this structure.
4271 MagickExport Image *ShadowImage(const Image *image,const double alpha,
4272 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4273 ExceptionInfo *exception)
4275 #define ShadowImageTag "Shadow/Image"
4297 assert(image != (Image *) NULL);
4298 assert(image->signature == MagickSignature);
4299 if (image->debug != MagickFalse)
4300 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4301 assert(exception != (ExceptionInfo *) NULL);
4302 assert(exception->signature == MagickSignature);
4303 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4304 if (clone_image == (Image *) NULL)
4305 return((Image *) NULL);
4306 if (IsGrayColorspace(image->colorspace) != MagickFalse)
4307 (void) TransformImageColorspace(clone_image,RGBColorspace,exception);
4308 (void) SetImageVirtualPixelMethod(clone_image,TransparentVirtualPixelMethod,
4310 border_info.width=(size_t) floor(2.0*sigma+0.5);
4311 border_info.height=(size_t) floor(2.0*sigma+0.5);
4314 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4316 clone_image->alpha_trait=BlendPixelTrait;
4317 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
4318 clone_image=DestroyImage(clone_image);
4319 if (border_image == (Image *) NULL)
4320 return((Image *) NULL);
4321 if (border_image->alpha_trait != BlendPixelTrait)
4322 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
4327 image_view=AcquireAuthenticCacheView(border_image,exception);
4328 for (y=0; y < (ssize_t) border_image->rows; y++)
4339 if (status == MagickFalse)
4341 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
4343 if (q == (Quantum *) NULL)
4348 background_color=border_image->background_color;
4349 background_color.alpha_trait=BlendPixelTrait;
4350 for (x=0; x < (ssize_t) border_image->columns; x++)
4352 if (border_image->alpha_trait == BlendPixelTrait)
4353 background_color.alpha=GetPixelAlpha(border_image,q)*alpha/100.0;
4354 SetPixelInfoPixel(border_image,&background_color,q);
4355 q+=GetPixelChannels(border_image);
4357 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4360 image_view=DestroyCacheView(image_view);
4361 if (status == MagickFalse)
4363 border_image=DestroyImage(border_image);
4364 return((Image *) NULL);
4366 channel_mask=SetImageChannelMask(border_image,AlphaChannel);
4367 shadow_image=BlurImage(border_image,0.0,sigma,exception);
4368 border_image=DestroyImage(border_image);
4369 if (shadow_image == (Image *) NULL)
4370 return((Image *) NULL);
4371 (void) SetPixelChannelMask(shadow_image,channel_mask);
4372 if (shadow_image->page.width == 0)
4373 shadow_image->page.width=shadow_image->columns;
4374 if (shadow_image->page.height == 0)
4375 shadow_image->page.height=shadow_image->rows;
4376 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4377 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4378 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4379 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
4380 return(shadow_image);
4384 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4388 % S k e t c h I m a g e %
4392 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4394 % SketchImage() simulates a pencil sketch. We convolve the image with a
4395 % Gaussian operator of the given radius and standard deviation (sigma). For
4396 % reasonable results, radius should be larger than sigma. Use a radius of 0
4397 % and SketchImage() selects a suitable radius for you. Angle gives the angle
4400 % The format of the SketchImage method is:
4402 % Image *SketchImage(const Image *image,const double radius,
4403 % const double sigma,const double angle,ExceptionInfo *exception)
4405 % A description of each parameter follows:
4407 % o image: the image.
4409 % o radius: the radius of the Gaussian, in pixels, not counting the
4412 % o sigma: the standard deviation of the Gaussian, in pixels.
4414 % o angle: apply the effect along this angle.
4416 % o exception: return any errors or warnings in this structure.
4419 MagickExport Image *SketchImage(const Image *image,const double radius,
4420 const double sigma,const double angle,ExceptionInfo *exception)
4436 **restrict random_info;
4441 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4449 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4450 MagickTrue,exception);
4451 if (random_image == (Image *) NULL)
4452 return((Image *) NULL);
4454 random_info=AcquireRandomInfoThreadSet();
4455 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4456 key=GetRandomSecretKey(random_info[0]);
4458 random_view=AcquireAuthenticCacheView(random_image,exception);
4459 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4460 #pragma omp parallel for schedule(static,4) shared(status) \
4461 dynamic_number_threads(random_image,random_image,random_image->rows,key == ~0UL)
4463 for (y=0; y < (ssize_t) random_image->rows; y++)
4466 id = GetOpenMPThreadId();
4474 if (status == MagickFalse)
4476 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4478 if (q == (Quantum *) NULL)
4483 for (x=0; x < (ssize_t) random_image->columns; x++)
4491 if (GetPixelMask(random_image,q) != 0)
4493 q+=GetPixelChannels(random_image);
4496 value=GetPseudoRandomValue(random_info[id]);
4497 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4505 channel=GetPixelChannelChannel(image,i);
4506 traits=GetPixelChannelTraits(image,channel);
4507 if (traits == UndefinedPixelTrait)
4509 q[i]=ClampToQuantum(QuantumRange*value);
4511 q+=GetPixelChannels(random_image);
4513 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4516 random_view=DestroyCacheView(random_view);
4517 random_info=DestroyRandomInfoThreadSet(random_info);
4518 if (status == MagickFalse)
4520 random_image=DestroyImage(random_image);
4521 return(random_image);
4523 blur_image=MotionBlurImage(random_image,radius,sigma,angle,exception);
4524 random_image=DestroyImage(random_image);
4525 if (blur_image == (Image *) NULL)
4526 return((Image *) NULL);
4527 dodge_image=EdgeImage(blur_image,radius,1.0,exception);
4528 blur_image=DestroyImage(blur_image);
4529 if (dodge_image == (Image *) NULL)
4530 return((Image *) NULL);
4531 (void) NormalizeImage(dodge_image,exception);
4532 (void) NegateImage(dodge_image,MagickFalse,exception);
4533 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
4534 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4535 if (sketch_image == (Image *) NULL)
4537 dodge_image=DestroyImage(dodge_image);
4538 return((Image *) NULL);
4540 (void) CompositeImage(sketch_image,dodge_image,ColorDodgeCompositeOp,
4541 MagickTrue,0,0,exception);
4542 dodge_image=DestroyImage(dodge_image);
4543 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4544 if (blend_image == (Image *) NULL)
4546 sketch_image=DestroyImage(sketch_image);
4547 return((Image *) NULL);
4549 (void) SetImageArtifact(blend_image,"compose:args","20x80");
4550 (void) CompositeImage(sketch_image,blend_image,BlendCompositeOp,MagickTrue,
4552 blend_image=DestroyImage(blend_image);
4553 return(sketch_image);
4557 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4561 % S o l a r i z e I m a g e %
4565 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4567 % SolarizeImage() applies a special effect to the image, similar to the effect
4568 % achieved in a photo darkroom by selectively exposing areas of photo
4569 % sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4570 % measure of the extent of the solarization.
4572 % The format of the SolarizeImage method is:
4574 % MagickBooleanType SolarizeImage(Image *image,const double threshold,
4575 % ExceptionInfo *exception)
4577 % A description of each parameter follows:
4579 % o image: the image.
4581 % o threshold: Define the extent of the solarization.
4583 % o exception: return any errors or warnings in this structure.
4586 MagickExport MagickBooleanType SolarizeImage(Image *image,
4587 const double threshold,ExceptionInfo *exception)
4589 #define SolarizeImageTag "Solarize/Image"
4603 assert(image != (Image *) NULL);
4604 assert(image->signature == MagickSignature);
4605 if (image->debug != MagickFalse)
4606 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4607 if (image->storage_class == PseudoClass)
4615 for (i=0; i < (ssize_t) image->colors; i++)
4617 if ((double) image->colormap[i].red > threshold)
4618 image->colormap[i].red=QuantumRange-image->colormap[i].red;
4619 if ((double) image->colormap[i].green > threshold)
4620 image->colormap[i].green=QuantumRange-
4621 image->colormap[i].green;
4622 if ((double) image->colormap[i].blue > threshold)
4623 image->colormap[i].blue=QuantumRange-
4624 image->colormap[i].blue;
4632 image_view=AcquireAuthenticCacheView(image,exception);
4633 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4634 #pragma omp parallel for schedule(static,4) shared(progress,status) \
4635 dynamic_number_threads(image,image,image->rows,1)
4637 for (y=0; y < (ssize_t) image->rows; y++)
4645 if (status == MagickFalse)
4647 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
4648 if (q == (Quantum *) NULL)
4653 for (x=0; x < (ssize_t) image->columns; x++)
4658 if (GetPixelMask(image,q) != 0)
4660 q+=GetPixelChannels(image);
4663 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4671 channel=GetPixelChannelChannel(image,i);
4672 traits=GetPixelChannelTraits(image,channel);
4673 if ((traits == UndefinedPixelTrait) ||
4674 ((traits & CopyPixelTrait) != 0))
4676 if ((double) q[i] > threshold)
4677 q[i]=QuantumRange-q[i];
4679 q+=GetPixelChannels(image);
4681 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4683 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4688 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4689 #pragma omp critical (MagickCore_SolarizeImage)
4691 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4692 if (proceed == MagickFalse)
4696 image_view=DestroyCacheView(image_view);
4701 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4705 % S t e g a n o I m a g e %
4709 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4711 % SteganoImage() hides a digital watermark within the image. Recover
4712 % the hidden watermark later to prove that the authenticity of an image.
4713 % Offset defines the start position within the image to hide the watermark.
4715 % The format of the SteganoImage method is:
4717 % Image *SteganoImage(const Image *image,Image *watermark,
4718 % ExceptionInfo *exception)
4720 % A description of each parameter follows:
4722 % o image: the image.
4724 % o watermark: the watermark image.
4726 % o exception: return any errors or warnings in this structure.
4729 MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4730 ExceptionInfo *exception)
4732 #define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
4733 #define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
4734 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
4735 #define SteganoImageTag "Stegano/Image"
4770 Initialize steganographic image attributes.
4772 assert(image != (const Image *) NULL);
4773 assert(image->signature == MagickSignature);
4774 if (image->debug != MagickFalse)
4775 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4776 assert(watermark != (const Image *) NULL);
4777 assert(watermark->signature == MagickSignature);
4778 assert(exception != (ExceptionInfo *) NULL);
4779 assert(exception->signature == MagickSignature);
4781 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4782 if (stegano_image == (Image *) NULL)
4783 return((Image *) NULL);
4784 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
4785 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
4787 stegano_image=DestroyImage(stegano_image);
4788 return((Image *) NULL);
4791 Hide watermark in low-order bits of image.
4796 depth=stegano_image->depth;
4797 k=stegano_image->offset;
4799 watermark_view=AcquireVirtualCacheView(watermark,exception);
4800 stegano_view=AcquireAuthenticCacheView(stegano_image,exception);
4801 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
4803 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
4805 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
4810 (void) GetOneCacheViewVirtualPixelInfo(watermark_view,x,y,&pixel,
4812 offset=k/(ssize_t) stegano_image->columns;
4813 if (offset >= (ssize_t) stegano_image->rows)
4815 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4816 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4818 if (q == (Quantum *) NULL)
4824 SetPixelRed(stegano_image,SetBit(GetPixelRed(stegano_image,q),j,
4825 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
4830 SetPixelGreen(stegano_image,SetBit(GetPixelGreen(stegano_image,q),j,
4831 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
4836 SetPixelBlue(stegano_image,SetBit(GetPixelBlue(stegano_image,q),j,
4837 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
4841 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
4847 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
4849 if (k == stegano_image->offset)
4853 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4858 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4860 if (proceed == MagickFalse)
4864 stegano_view=DestroyCacheView(stegano_view);
4865 watermark_view=DestroyCacheView(watermark_view);
4866 if (status == MagickFalse)
4867 stegano_image=DestroyImage(stegano_image);
4868 return(stegano_image);
4872 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4876 % S t e r e o A n a g l y p h I m a g e %
4880 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4882 % StereoAnaglyphImage() combines two images and produces a single image that
4883 % is the composite of a left and right image of a stereo pair. Special
4884 % red-green stereo glasses are required to view this effect.
4886 % The format of the StereoAnaglyphImage method is:
4888 % Image *StereoImage(const Image *left_image,const Image *right_image,
4889 % ExceptionInfo *exception)
4890 % Image *StereoAnaglyphImage(const Image *left_image,
4891 % const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
4892 % ExceptionInfo *exception)
4894 % A description of each parameter follows:
4896 % o left_image: the left image.
4898 % o right_image: the right image.
4900 % o exception: return any errors or warnings in this structure.
4902 % o x_offset: amount, in pixels, by which the left image is offset to the
4903 % right of the right image.
4905 % o y_offset: amount, in pixels, by which the left image is offset to the
4906 % bottom of the right image.
4910 MagickExport Image *StereoImage(const Image *left_image,
4911 const Image *right_image,ExceptionInfo *exception)
4913 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4916 MagickExport Image *StereoAnaglyphImage(const Image *left_image,
4917 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
4918 ExceptionInfo *exception)
4920 #define StereoImageTag "Stereo/Image"
4934 assert(left_image != (const Image *) NULL);
4935 assert(left_image->signature == MagickSignature);
4936 if (left_image->debug != MagickFalse)
4937 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4938 left_image->filename);
4939 assert(right_image != (const Image *) NULL);
4940 assert(right_image->signature == MagickSignature);
4941 assert(exception != (ExceptionInfo *) NULL);
4942 assert(exception->signature == MagickSignature);
4943 assert(right_image != (const Image *) NULL);
4945 if ((left_image->columns != right_image->columns) ||
4946 (left_image->rows != right_image->rows))
4947 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4949 Initialize stereo image attributes.
4951 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4952 MagickTrue,exception);
4953 if (stereo_image == (Image *) NULL)
4954 return((Image *) NULL);
4955 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
4957 stereo_image=DestroyImage(stereo_image);
4958 return((Image *) NULL);
4960 (void) SetImageColorspace(stereo_image,sRGBColorspace,exception);
4962 Copy left image to red channel and right image to blue channel.
4965 for (y=0; y < (ssize_t) stereo_image->rows; y++)
4967 register const Quantum
4977 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4979 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4980 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
4981 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4982 (r == (Quantum *) NULL))
4984 for (x=0; x < (ssize_t) stereo_image->columns; x++)
4986 SetPixelRed(image,GetPixelRed(left_image,p),r);
4987 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4988 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4989 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4990 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4991 GetPixelAlpha(right_image,q))/2,r);
4992 p+=GetPixelChannels(left_image);
4993 q+=GetPixelChannels(right_image);
4994 r+=GetPixelChannels(stereo_image);
4996 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4998 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5003 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
5004 stereo_image->rows);
5005 if (proceed == MagickFalse)
5009 if (status == MagickFalse)
5010 stereo_image=DestroyImage(stereo_image);
5011 return(stereo_image);
5015 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5019 % S w i r l I m a g e %
5023 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5025 % SwirlImage() swirls the pixels about the center of the image, where
5026 % degrees indicates the sweep of the arc through which each pixel is moved.
5027 % You get a more dramatic effect as the degrees move from 1 to 360.
5029 % The format of the SwirlImage method is:
5031 % Image *SwirlImage(const Image *image,double degrees,
5032 % const PixelInterpolateMethod method,ExceptionInfo *exception)
5034 % A description of each parameter follows:
5036 % o image: the image.
5038 % o degrees: Define the tightness of the swirling effect.
5040 % o method: the pixel interpolation method.
5042 % o exception: return any errors or warnings in this structure.
5045 MagickExport Image *SwirlImage(const Image *image,double degrees,
5046 const PixelInterpolateMethod method,ExceptionInfo *exception)
5048 #define SwirlImageTag "Swirl/Image"
5074 Initialize swirl image attributes.
5076 assert(image != (const Image *) NULL);
5077 assert(image->signature == MagickSignature);
5078 if (image->debug != MagickFalse)
5079 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5080 assert(exception != (ExceptionInfo *) NULL);
5081 assert(exception->signature == MagickSignature);
5082 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5083 if (swirl_image == (Image *) NULL)
5084 return((Image *) NULL);
5085 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
5087 swirl_image=DestroyImage(swirl_image);
5088 return((Image *) NULL);
5090 if (swirl_image->background_color.alpha != OpaqueAlpha)
5091 swirl_image->alpha_trait=BlendPixelTrait;
5093 Compute scaling factor.
5095 center.x=(double) image->columns/2.0;
5096 center.y=(double) image->rows/2.0;
5097 radius=MagickMax(center.x,center.y);
5100 if (image->columns > image->rows)
5101 scale.y=(double) image->columns/(double) image->rows;
5103 if (image->columns < image->rows)
5104 scale.x=(double) image->rows/(double) image->columns;
5105 degrees=(double) DegreesToRadians(degrees);
5111 image_view=AcquireVirtualCacheView(image,exception);
5112 swirl_view=AcquireAuthenticCacheView(swirl_image,exception);
5113 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5114 #pragma omp parallel for schedule(static,4) shared(progress,status) \
5115 dynamic_number_threads(image,swirl_image,image->rows,1)
5117 for (y=0; y < (ssize_t) image->rows; y++)
5125 register const Quantum
5134 if (status == MagickFalse)
5136 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
5137 q=QueueCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
5139 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
5144 delta.y=scale.y*(double) (y-center.y);
5145 for (x=0; x < (ssize_t) image->columns; x++)
5148 Determine if the pixel is within an ellipse.
5150 if (GetPixelMask(image,p) != 0)
5152 p+=GetPixelChannels(image);
5153 q+=GetPixelChannels(swirl_image);
5156 delta.x=scale.x*(double) (x-center.x);
5157 distance=delta.x*delta.x+delta.y*delta.y;
5158 if (distance >= (radius*radius))
5163 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5172 channel=GetPixelChannelChannel(image,i);
5173 traits=GetPixelChannelTraits(image,channel);
5174 swirl_traits=GetPixelChannelTraits(swirl_image,channel);
5175 if ((traits == UndefinedPixelTrait) ||
5176 (swirl_traits == UndefinedPixelTrait))
5178 SetPixelChannel(swirl_image,channel,p[i],q);
5191 factor=1.0-sqrt((double) distance)/radius;
5192 sine=sin((double) (degrees*factor*factor));
5193 cosine=cos((double) (degrees*factor*factor));
5194 status=InterpolatePixelChannels(image,image_view,swirl_image,method,
5195 ((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5196 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
5198 p+=GetPixelChannels(image);
5199 q+=GetPixelChannels(swirl_image);
5201 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5203 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5208 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5209 #pragma omp critical (MagickCore_SwirlImage)
5211 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5212 if (proceed == MagickFalse)
5216 swirl_view=DestroyCacheView(swirl_view);
5217 image_view=DestroyCacheView(image_view);
5218 if (status == MagickFalse)
5219 swirl_image=DestroyImage(swirl_image);
5220 return(swirl_image);
5224 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5228 % T i n t I m a g e %
5232 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5234 % TintImage() applies a color vector to each pixel in the image. The length
5235 % of the vector is 0 for black and white and at its maximum for the midtones.
5236 % The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5238 % The format of the TintImage method is:
5240 % Image *TintImage(const Image *image,const char *blend,
5241 % const PixelInfo *tint,ExceptionInfo *exception)
5243 % A description of each parameter follows:
5245 % o image: the image.
5247 % o blend: A color value used for tinting.
5249 % o tint: A color value used for tinting.
5251 % o exception: return any errors or warnings in this structure.
5254 MagickExport Image *TintImage(const Image *image,const char *blend,
5255 const PixelInfo *tint,ExceptionInfo *exception)
5257 #define TintImageTag "Tint/Image"
5288 Allocate tint image.
5290 assert(image != (const Image *) NULL);
5291 assert(image->signature == MagickSignature);
5292 if (image->debug != MagickFalse)
5293 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5294 assert(exception != (ExceptionInfo *) NULL);
5295 assert(exception->signature == MagickSignature);
5296 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5297 if (tint_image == (Image *) NULL)
5298 return((Image *) NULL);
5299 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
5301 tint_image=DestroyImage(tint_image);
5302 return((Image *) NULL);
5304 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
5305 (IsPixelInfoGray(tint) == MagickFalse))
5306 (void) SetImageColorspace(tint_image,RGBColorspace,exception);
5307 if (blend == (const char *) NULL)
5310 Determine RGB values of the color.
5312 GetPixelInfo(image,&color_vector);
5313 flags=ParseGeometry(blend,&geometry_info);
5314 color_vector.red=geometry_info.rho;
5315 color_vector.green=geometry_info.rho;
5316 color_vector.blue=geometry_info.rho;
5317 color_vector.alpha=OpaqueAlpha;
5318 if ((flags & SigmaValue) != 0)
5319 color_vector.green=geometry_info.sigma;
5320 if ((flags & XiValue) != 0)
5321 color_vector.blue=geometry_info.xi;
5322 if ((flags & PsiValue) != 0)
5323 color_vector.alpha=geometry_info.psi;
5324 if (image->colorspace == CMYKColorspace)
5326 color_vector.black=geometry_info.rho;
5327 if ((flags & PsiValue) != 0)
5328 color_vector.black=geometry_info.psi;
5329 if ((flags & ChiValue) != 0)
5330 color_vector.alpha=geometry_info.chi;
5332 intensity=(double) GetPixelInfoIntensity(tint);
5333 color_vector.red=(double) (color_vector.red*tint->red/100.0-
5335 color_vector.green=(double) (color_vector.green*tint->green/100.0-
5337 color_vector.blue=(double) (color_vector.blue*tint->blue/100.0-
5339 color_vector.black=(double) (color_vector.black*tint->black/100.0-
5341 color_vector.alpha=(double) (color_vector.alpha*tint->alpha/100.0-
5348 image_view=AcquireVirtualCacheView(image,exception);
5349 tint_view=AcquireAuthenticCacheView(tint_image,exception);
5350 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5351 #pragma omp parallel for schedule(static,4) shared(progress,status) \
5352 dynamic_number_threads(image,tint_image,image->rows,1)
5354 for (y=0; y < (ssize_t) image->rows; y++)
5356 register const Quantum
5365 if (status == MagickFalse)
5367 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5368 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5370 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
5375 for (x=0; x < (ssize_t) image->columns; x++)
5386 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5395 channel=GetPixelChannelChannel(image,i);
5396 traits=GetPixelChannelTraits(image,channel);
5397 tint_traits=GetPixelChannelTraits(tint_image,channel);
5398 if ((traits == UndefinedPixelTrait) ||
5399 (tint_traits == UndefinedPixelTrait))
5401 if (((tint_traits & CopyPixelTrait) != 0) ||
5402 (GetPixelMask(image,p) != 0))
5404 SetPixelChannel(tint_image,channel,p[i],q);
5408 GetPixelInfo(image,&pixel);
5409 weight=QuantumScale*GetPixelRed(image,p)-0.5;
5410 pixel.red=(double) GetPixelRed(image,p)+color_vector.red*(1.0-(4.0*
5412 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
5413 pixel.green=(double) GetPixelGreen(image,p)+color_vector.green*(1.0-(4.0*
5415 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
5416 pixel.blue=(double) GetPixelBlue(image,p)+color_vector.blue*(1.0-(4.0*
5418 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
5419 pixel.black=(double) GetPixelBlack(image,p)+color_vector.black*(1.0-(4.0*
5421 SetPixelInfoPixel(tint_image,&pixel,q);
5422 p+=GetPixelChannels(image);
5423 q+=GetPixelChannels(tint_image);
5425 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5427 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5432 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5433 #pragma omp critical (MagickCore_TintImage)
5435 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5436 if (proceed == MagickFalse)
5440 tint_view=DestroyCacheView(tint_view);
5441 image_view=DestroyCacheView(image_view);
5442 if (status == MagickFalse)
5443 tint_image=DestroyImage(tint_image);
5448 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5452 % V i g n e t t e I m a g e %
5456 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5458 % VignetteImage() softens the edges of the image in vignette style.
5460 % The format of the VignetteImage method is:
5462 % Image *VignetteImage(const Image *image,const double radius,
5463 % const double sigma,const ssize_t x,const ssize_t y,
5464 % ExceptionInfo *exception)
5466 % A description of each parameter follows:
5468 % o image: the image.
5470 % o radius: the radius of the pixel neighborhood.
5472 % o sigma: the standard deviation of the Gaussian, in pixels.
5474 % o x, y: Define the x and y ellipse offset.
5476 % o exception: return any errors or warnings in this structure.
5479 MagickExport Image *VignetteImage(const Image *image,const double radius,
5480 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
5483 ellipse[MaxTextExtent];
5494 assert(image != (Image *) NULL);
5495 assert(image->signature == MagickSignature);
5496 if (image->debug != MagickFalse)
5497 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5498 assert(exception != (ExceptionInfo *) NULL);
5499 assert(exception->signature == MagickSignature);
5500 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5501 if (canvas_image == (Image *) NULL)
5502 return((Image *) NULL);
5503 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
5505 canvas_image=DestroyImage(canvas_image);
5506 return((Image *) NULL);
5508 canvas_image->alpha_trait=BlendPixelTrait;
5509 oval_image=CloneImage(canvas_image,canvas_image->columns,canvas_image->rows,
5510 MagickTrue,exception);
5511 if (oval_image == (Image *) NULL)
5513 canvas_image=DestroyImage(canvas_image);
5514 return((Image *) NULL);
5516 (void) QueryColorCompliance("#000000",AllCompliance,
5517 &oval_image->background_color,exception);
5518 (void) SetImageBackgroundColor(oval_image,exception);
5519 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
5520 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5522 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5524 (void) FormatLocaleString(ellipse,MaxTextExtent,"ellipse %g,%g,%g,%g,"
5525 "0.0,360.0",image->columns/2.0,image->rows/2.0,image->columns/2.0-x,
5527 draw_info->primitive=AcquireString(ellipse);
5528 (void) DrawImage(oval_image,draw_info,exception);
5529 draw_info=DestroyDrawInfo(draw_info);
5530 blur_image=BlurImage(oval_image,radius,sigma,exception);
5531 oval_image=DestroyImage(oval_image);
5532 if (blur_image == (Image *) NULL)
5534 canvas_image=DestroyImage(canvas_image);
5535 return((Image *) NULL);
5537 blur_image->alpha_trait=UndefinedPixelTrait;
5538 (void) CompositeImage(canvas_image,blur_image,IntensityCompositeOp,MagickTrue,
5540 blur_image=DestroyImage(blur_image);
5541 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5542 canvas_image=DestroyImage(canvas_image);
5543 if (vignette_image != (Image *) NULL)
5544 (void) TransformImageColorspace(vignette_image,image->colorspace,exception);
5545 return(vignette_image);
5549 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5553 % W a v e I m a g e %
5557 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5559 % WaveImage() creates a "ripple" effect in the image by shifting the pixels
5560 % vertically along a sine wave whose amplitude and wavelength is specified
5561 % by the given parameters.
5563 % The format of the WaveImage method is:
5565 % Image *WaveImage(const Image *image,const double amplitude,
5566 % const double wave_length,const PixelInterpolateMethod method,
5567 % ExceptionInfo *exception)
5569 % A description of each parameter follows:
5571 % o image: the image.
5573 % o amplitude, wave_length: Define the amplitude and wave length of the
5576 % o interpolate: the pixel interpolation method.
5578 % o exception: return any errors or warnings in this structure.
5581 MagickExport Image *WaveImage(const Image *image,const double amplitude,
5582 const double wave_length,const PixelInterpolateMethod method,
5583 ExceptionInfo *exception)
5585 #define WaveImageTag "Wave/Image"
5610 Initialize wave image attributes.
5612 assert(image != (Image *) NULL);
5613 assert(image->signature == MagickSignature);
5614 if (image->debug != MagickFalse)
5615 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5616 assert(exception != (ExceptionInfo *) NULL);
5617 assert(exception->signature == MagickSignature);
5618 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
5619 fabs(amplitude)),MagickTrue,exception);
5620 if (wave_image == (Image *) NULL)
5621 return((Image *) NULL);
5622 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
5624 wave_image=DestroyImage(wave_image);
5625 return((Image *) NULL);
5627 if (wave_image->background_color.alpha != OpaqueAlpha)
5628 wave_image->alpha_trait=BlendPixelTrait;
5632 sine_map=(double *) AcquireQuantumMemory((size_t) wave_image->columns,
5634 if (sine_map == (double *) NULL)
5636 wave_image=DestroyImage(wave_image);
5637 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5639 for (i=0; i < (ssize_t) wave_image->columns; i++)
5640 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5647 image_view=AcquireVirtualCacheView(image,exception);
5648 wave_view=AcquireAuthenticCacheView(wave_image,exception);
5649 (void) SetCacheViewVirtualPixelMethod(image_view,
5650 BackgroundVirtualPixelMethod);
5651 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5652 #pragma omp parallel for schedule(static,4) shared(progress,status) \
5653 dynamic_number_threads(image,wave_image,wave_image->rows,1)
5655 for (y=0; y < (ssize_t) wave_image->rows; y++)
5663 if (status == MagickFalse)
5665 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5667 if (q == (Quantum *) NULL)
5672 for (x=0; x < (ssize_t) wave_image->columns; x++)
5674 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5675 (double) x,(double) (y-sine_map[x]),q,exception);
5676 q+=GetPixelChannels(wave_image);
5678 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5680 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5685 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5686 #pragma omp critical (MagickCore_WaveImage)
5688 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5689 if (proceed == MagickFalse)
5693 wave_view=DestroyCacheView(wave_view);
5694 image_view=DestroyCacheView(image_view);
5695 sine_map=(double *) RelinquishMagickMemory(sine_map);
5696 if (status == MagickFalse)
5697 wave_image=DestroyImage(wave_image);