2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % MagickCore Image Special Effects Methods %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/annotate.h"
45 #include "MagickCore/artifact.h"
46 #include "MagickCore/attribute.h"
47 #include "MagickCore/cache.h"
48 #include "MagickCore/cache-view.h"
49 #include "MagickCore/color.h"
50 #include "MagickCore/color-private.h"
51 #include "MagickCore/colorspace-private.h"
52 #include "MagickCore/composite.h"
53 #include "MagickCore/decorate.h"
54 #include "MagickCore/distort.h"
55 #include "MagickCore/draw.h"
56 #include "MagickCore/effect.h"
57 #include "MagickCore/enhance.h"
58 #include "MagickCore/exception.h"
59 #include "MagickCore/exception-private.h"
60 #include "MagickCore/fx.h"
61 #include "MagickCore/fx-private.h"
62 #include "MagickCore/gem.h"
63 #include "MagickCore/gem-private.h"
64 #include "MagickCore/geometry.h"
65 #include "MagickCore/layer.h"
66 #include "MagickCore/list.h"
67 #include "MagickCore/log.h"
68 #include "MagickCore/image.h"
69 #include "MagickCore/image-private.h"
70 #include "MagickCore/magick.h"
71 #include "MagickCore/memory_.h"
72 #include "MagickCore/monitor.h"
73 #include "MagickCore/monitor-private.h"
74 #include "MagickCore/option.h"
75 #include "MagickCore/pixel.h"
76 #include "MagickCore/pixel-accessor.h"
77 #include "MagickCore/property.h"
78 #include "MagickCore/quantum.h"
79 #include "MagickCore/quantum-private.h"
80 #include "MagickCore/random_.h"
81 #include "MagickCore/random-private.h"
82 #include "MagickCore/resample.h"
83 #include "MagickCore/resample-private.h"
84 #include "MagickCore/resize.h"
85 #include "MagickCore/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;
290 Initialize noise image attributes.
292 assert(image != (const Image *) NULL);
293 assert(image->signature == MagickSignature);
294 if (image->debug != MagickFalse)
295 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
296 assert(exception != (ExceptionInfo *) NULL);
297 assert(exception->signature == MagickSignature);
298 noise_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
299 if (noise_image == (Image *) NULL)
300 return((Image *) NULL);
301 if (SetImageStorageClass(noise_image,DirectClass,exception) == MagickFalse)
303 noise_image=DestroyImage(noise_image);
304 return((Image *) NULL);
306 if (IsGrayColorspace(image->colorspace) != MagickFalse)
307 (void) TransformImageColorspace(noise_image,RGBColorspace,exception);
309 Add noise in each row.
313 random_info=AcquireRandomInfoThreadSet();
314 image_view=AcquireVirtualCacheView(image,exception);
315 noise_view=AcquireAuthenticCacheView(noise_image,exception);
316 key=GetRandomSecretKey(random_info[0]);
317 #if defined(MAGICKCORE_OPENMP_SUPPORT)
318 #pragma omp parallel for schedule(static,4) shared(progress,status) \
319 dynamic_number_threads(image,image->columns,image->rows,key == ~0UL)
321 for (y=0; y < (ssize_t) image->rows; y++)
324 id = GetOpenMPThreadId();
329 register const Quantum
338 if (status == MagickFalse)
340 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
341 q=QueueCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
343 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
348 for (x=0; x < (ssize_t) image->columns; x++)
353 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
362 channel=GetPixelChannelMapChannel(image,i);
363 traits=GetPixelChannelMapTraits(image,channel);
364 noise_traits=GetPixelChannelMapTraits(noise_image,channel);
365 if ((traits == UndefinedPixelTrait) ||
366 (noise_traits == UndefinedPixelTrait))
368 if (((noise_traits & CopyPixelTrait) != 0) ||
369 (GetPixelMask(image,p) != 0))
371 SetPixelChannel(noise_image,channel,p[i],q);
374 SetPixelChannel(noise_image,channel,ClampToQuantum(
375 GenerateDifferentialNoise(random_info[id],p[i],noise_type,attenuate)),
378 p+=GetPixelChannels(image);
379 q+=GetPixelChannels(noise_image);
381 sync=SyncCacheViewAuthenticPixels(noise_view,exception);
382 if (sync == MagickFalse)
384 if (image->progress_monitor != (MagickProgressMonitor) NULL)
389 #if defined(MAGICKCORE_OPENMP_SUPPORT)
390 #pragma omp critical (MagickCore_AddNoiseImage)
392 proceed=SetImageProgress(image,AddNoiseImageTag,progress++,
394 if (proceed == MagickFalse)
398 noise_view=DestroyCacheView(noise_view);
399 image_view=DestroyCacheView(image_view);
400 random_info=DestroyRandomInfoThreadSet(random_info);
401 if (status == MagickFalse)
402 noise_image=DestroyImage(noise_image);
407 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
411 % B l u e S h i f t I m a g e %
415 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
417 % BlueShiftImage() mutes the colors of the image to simulate a scene at
418 % nighttime in the moonlight.
420 % The format of the BlueShiftImage method is:
422 % Image *BlueShiftImage(const Image *image,const double factor,
423 % ExceptionInfo *exception)
425 % A description of each parameter follows:
427 % o image: the image.
429 % o factor: the shift factor.
431 % o exception: return any errors or warnings in this structure.
434 MagickExport Image *BlueShiftImage(const Image *image,const double factor,
435 ExceptionInfo *exception)
437 #define BlueShiftImageTag "BlueShift/Image"
456 Allocate blue shift image.
458 assert(image != (const Image *) NULL);
459 assert(image->signature == MagickSignature);
460 if (image->debug != MagickFalse)
461 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
462 assert(exception != (ExceptionInfo *) NULL);
463 assert(exception->signature == MagickSignature);
464 shift_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
465 if (shift_image == (Image *) NULL)
466 return((Image *) NULL);
467 if (SetImageStorageClass(shift_image,DirectClass,exception) == MagickFalse)
469 shift_image=DestroyImage(shift_image);
470 return((Image *) NULL);
473 Blue-shift DirectClass image.
477 image_view=AcquireVirtualCacheView(image,exception);
478 shift_view=AcquireAuthenticCacheView(shift_image,exception);
479 #if defined(MAGICKCORE_OPENMP_SUPPORT)
480 #pragma omp parallel for schedule(static,4) shared(progress,status) \
481 dynamic_number_threads(image,image->columns,image->rows,1)
483 for (y=0; y < (ssize_t) image->rows; y++)
494 register const Quantum
503 if (status == MagickFalse)
505 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
506 q=QueueCacheViewAuthenticPixels(shift_view,0,y,shift_image->columns,1,
508 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
513 for (x=0; x < (ssize_t) image->columns; x++)
515 quantum=GetPixelRed(image,p);
516 if (GetPixelGreen(image,p) < quantum)
517 quantum=GetPixelGreen(image,p);
518 if (GetPixelBlue(image,p) < quantum)
519 quantum=GetPixelBlue(image,p);
520 pixel.red=0.5*(GetPixelRed(image,p)+factor*quantum);
521 pixel.green=0.5*(GetPixelGreen(image,p)+factor*quantum);
522 pixel.blue=0.5*(GetPixelBlue(image,p)+factor*quantum);
523 quantum=GetPixelRed(image,p);
524 if (GetPixelGreen(image,p) > quantum)
525 quantum=GetPixelGreen(image,p);
526 if (GetPixelBlue(image,p) > quantum)
527 quantum=GetPixelBlue(image,p);
528 pixel.red=0.5*(pixel.red+factor*quantum);
529 pixel.green=0.5*(pixel.green+factor*quantum);
530 pixel.blue=0.5*(pixel.blue+factor*quantum);
531 SetPixelRed(shift_image,ClampToQuantum(pixel.red),q);
532 SetPixelGreen(shift_image,ClampToQuantum(pixel.green),q);
533 SetPixelBlue(shift_image,ClampToQuantum(pixel.blue),q);
534 p+=GetPixelChannels(image);
535 q+=GetPixelChannels(shift_image);
537 sync=SyncCacheViewAuthenticPixels(shift_view,exception);
538 if (sync == MagickFalse)
540 if (image->progress_monitor != (MagickProgressMonitor) NULL)
545 #if defined(MAGICKCORE_OPENMP_SUPPORT)
546 #pragma omp critical (MagickCore_BlueShiftImage)
548 proceed=SetImageProgress(image,BlueShiftImageTag,progress++,
550 if (proceed == MagickFalse)
554 image_view=DestroyCacheView(image_view);
555 shift_view=DestroyCacheView(shift_view);
556 if (status == MagickFalse)
557 shift_image=DestroyImage(shift_image);
562 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
566 % C h a r c o a l I m a g e %
570 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
572 % CharcoalImage() creates a new image that is a copy of an existing one with
573 % the edge highlighted. It allocates the memory necessary for the new Image
574 % structure and returns a pointer to the new image.
576 % The format of the CharcoalImage method is:
578 % Image *CharcoalImage(const Image *image,const double radius,
579 % const double sigma,ExceptionInfo *exception)
581 % A description of each parameter follows:
583 % o image: the image.
585 % o radius: the radius of the pixel neighborhood.
587 % o sigma: the standard deviation of the Gaussian, in pixels.
589 % o exception: return any errors or warnings in this structure.
592 MagickExport Image *CharcoalImage(const Image *image,const double radius,
593 const double sigma,ExceptionInfo *exception)
600 assert(image != (Image *) NULL);
601 assert(image->signature == MagickSignature);
602 if (image->debug != MagickFalse)
603 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
604 assert(exception != (ExceptionInfo *) NULL);
605 assert(exception->signature == MagickSignature);
606 clone_image=CloneImage(image,0,0,MagickTrue,exception);
607 if (clone_image == (Image *) NULL)
608 return((Image *) NULL);
609 (void) SetImageType(clone_image,GrayscaleType,exception);
610 edge_image=EdgeImage(clone_image,radius,sigma,exception);
611 clone_image=DestroyImage(clone_image);
612 if (edge_image == (Image *) NULL)
613 return((Image *) NULL);
614 charcoal_image=BlurImage(edge_image,radius,sigma,exception);
615 edge_image=DestroyImage(edge_image);
616 if (charcoal_image == (Image *) NULL)
617 return((Image *) NULL);
618 (void) NormalizeImage(charcoal_image,exception);
619 (void) NegateImage(charcoal_image,MagickFalse,exception);
620 (void) SetImageType(charcoal_image,GrayscaleType,exception);
621 return(charcoal_image);
625 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
629 % C o l o r i z e I m a g e %
633 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
635 % ColorizeImage() blends the fill color with each pixel in the image.
636 % A percentage blend is specified with opacity. Control the application
637 % of different color components by specifying a different percentage for
638 % each component (e.g. 90/100/10 is 90% red, 100% green, and 10% blue).
640 % The format of the ColorizeImage method is:
642 % Image *ColorizeImage(const Image *image,const char *blend,
643 % const PixelInfo *colorize,ExceptionInfo *exception)
645 % A description of each parameter follows:
647 % o image: the image.
649 % o blend: A character string indicating the level of blending as a
652 % o colorize: A color value.
654 % o exception: return any errors or warnings in this structure.
657 MagickExport Image *ColorizeImage(const Image *image,const char *blend,
658 const PixelInfo *colorize,ExceptionInfo *exception)
660 #define ColorizeImageTag "Colorize/Image"
661 #define Colorize(pixel,blend_percentage,colorize) \
662 (pixel)=((pixel)*(100.0-(blend_percentage))+(colorize)*(blend_percentage))/100.0;
690 Allocate colorized image.
692 assert(image != (const Image *) NULL);
693 assert(image->signature == MagickSignature);
694 if (image->debug != MagickFalse)
695 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
696 assert(exception != (ExceptionInfo *) NULL);
697 assert(exception->signature == MagickSignature);
698 colorize_image=CloneImage(image,image->columns,image->rows,MagickTrue,
700 if (colorize_image == (Image *) NULL)
701 return((Image *) NULL);
702 if (SetImageStorageClass(colorize_image,DirectClass,exception) == MagickFalse)
704 colorize_image=DestroyImage(colorize_image);
705 return((Image *) NULL);
707 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
708 (IsPixelInfoGray(colorize) != MagickFalse))
709 (void) SetImageColorspace(colorize_image,RGBColorspace,exception);
710 if ((colorize_image->matte == MagickFalse) &&
711 (colorize->matte != MagickFalse))
712 (void) SetImageAlpha(colorize_image,OpaqueAlpha,exception);
713 if (blend == (const char *) NULL)
714 return(colorize_image);
715 GetPixelInfo(image,&blend_percentage);
716 flags=ParseGeometry(blend,&geometry_info);
717 blend_percentage.red=geometry_info.rho;
718 blend_percentage.green=geometry_info.rho;
719 blend_percentage.blue=geometry_info.rho;
720 blend_percentage.black=geometry_info.rho;
721 blend_percentage.alpha=geometry_info.rho;
722 if ((flags & SigmaValue) != 0)
723 blend_percentage.green=geometry_info.sigma;
724 if ((flags & XiValue) != 0)
725 blend_percentage.blue=geometry_info.xi;
726 if ((flags & PsiValue) != 0)
727 blend_percentage.alpha=geometry_info.psi;
728 if (blend_percentage.colorspace == CMYKColorspace)
730 if ((flags & PsiValue) != 0)
731 blend_percentage.black=geometry_info.psi;
732 if ((flags & ChiValue) != 0)
733 blend_percentage.alpha=geometry_info.chi;
736 Colorize DirectClass image.
740 image_view=AcquireVirtualCacheView(image,exception);
741 colorize_view=AcquireAuthenticCacheView(colorize_image,exception);
742 #if defined(MAGICKCORE_OPENMP_SUPPORT)
743 #pragma omp parallel for schedule(static,4) shared(progress,status) \
744 dynamic_number_threads(image,image->columns,image->rows,1)
746 for (y=0; y < (ssize_t) image->rows; y++)
754 register const Quantum
763 if (status == MagickFalse)
765 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
766 q=QueueCacheViewAuthenticPixels(colorize_view,0,y,colorize_image->columns,1,
768 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
773 GetPixelInfo(colorize_image,&pixel);
774 for (x=0; x < (ssize_t) image->columns; x++)
776 if (GetPixelMask(colorize_image,q) != 0)
778 p+=GetPixelChannels(image);
779 q+=GetPixelChannels(colorize_image);
782 GetPixelInfoPixel(image,p,&pixel);
783 Colorize(pixel.red,blend_percentage.red,colorize->red);
784 Colorize(pixel.green,blend_percentage.green,colorize->green);
785 Colorize(pixel.blue,blend_percentage.blue,colorize->blue);
786 Colorize(pixel.black,blend_percentage.black,colorize->black);
787 Colorize(pixel.alpha,blend_percentage.alpha,colorize->alpha);
788 SetPixelInfoPixel(colorize_image,&pixel,q);
789 p+=GetPixelChannels(image);
790 q+=GetPixelChannels(colorize_image);
792 sync=SyncCacheViewAuthenticPixels(colorize_view,exception);
793 if (sync == MagickFalse)
795 if (image->progress_monitor != (MagickProgressMonitor) NULL)
800 #if defined(MAGICKCORE_OPENMP_SUPPORT)
801 #pragma omp critical (MagickCore_ColorizeImage)
803 proceed=SetImageProgress(image,ColorizeImageTag,progress++,image->rows);
804 if (proceed == MagickFalse)
808 image_view=DestroyCacheView(image_view);
809 colorize_view=DestroyCacheView(colorize_view);
810 if (status == MagickFalse)
811 colorize_image=DestroyImage(colorize_image);
812 return(colorize_image);
816 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
820 % C o l o r M a t r i x I m a g e %
824 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
826 % ColorMatrixImage() applies color transformation to an image. This method
827 % permits saturation changes, hue rotation, luminance to alpha, and various
828 % other effects. Although variable-sized transformation matrices can be used,
829 % typically one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA
830 % (or RGBA with offsets). The matrix is similar to those used by Adobe Flash
831 % except offsets are in column 6 rather than 5 (in support of CMYKA images)
832 % and offsets are normalized (divide Flash offset by 255).
834 % The format of the ColorMatrixImage method is:
836 % Image *ColorMatrixImage(const Image *image,
837 % const KernelInfo *color_matrix,ExceptionInfo *exception)
839 % A description of each parameter follows:
841 % o image: the image.
843 % o color_matrix: the color matrix.
845 % o exception: return any errors or warnings in this structure.
848 /* FUTURE: modify to make use of a MagickMatrix Mutliply function
849 That should be provided in "matrix.c"
850 (ASIDE: actually distorts should do this too but currently doesn't)
853 MagickExport Image *ColorMatrixImage(const Image *image,
854 const KernelInfo *color_matrix,ExceptionInfo *exception)
856 #define ColorMatrixImageTag "ColorMatrix/Image"
865 { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
866 { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
867 { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
868 { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
869 { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
870 { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }
891 Map given color_matrix, into a 6x6 matrix RGBKA and a constant
893 assert(image != (Image *) NULL);
894 assert(image->signature == MagickSignature);
895 if (image->debug != MagickFalse)
896 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
897 assert(exception != (ExceptionInfo *) NULL);
898 assert(exception->signature == MagickSignature);
900 for (v=0; v < (ssize_t) color_matrix->height; v++)
901 for (u=0; u < (ssize_t) color_matrix->width; u++)
903 if ((v < 6) && (u < 6))
904 ColorMatrix[v][u]=color_matrix->values[i];
908 Initialize color image.
910 color_image=CloneImage(image,0,0,MagickTrue,exception);
911 if (color_image == (Image *) NULL)
912 return((Image *) NULL);
913 if (SetImageStorageClass(color_image,DirectClass,exception) == MagickFalse)
915 color_image=DestroyImage(color_image);
916 return((Image *) NULL);
918 if (image->debug != MagickFalse)
921 format[MaxTextExtent],
924 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
925 " ColorMatrix image with color matrix:");
926 message=AcquireString("");
927 for (v=0; v < 6; v++)
930 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
931 (void) ConcatenateString(&message,format);
932 for (u=0; u < 6; u++)
934 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",
936 (void) ConcatenateString(&message,format);
938 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
940 message=DestroyString(message);
943 Apply the ColorMatrix to image.
947 image_view=AcquireVirtualCacheView(image,exception);
948 color_view=AcquireAuthenticCacheView(color_image,exception);
949 #if defined(MAGICKCORE_OPENMP_SUPPORT)
950 #pragma omp parallel for schedule(static,4) shared(progress,status) \
951 dynamic_number_threads(image,image->columns,image->rows,1)
953 for (y=0; y < (ssize_t) image->rows; y++)
958 register const Quantum
967 if (status == MagickFalse)
969 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
970 q=GetCacheViewAuthenticPixels(color_view,0,y,color_image->columns,1,
972 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
977 GetPixelInfo(image,&pixel);
978 for (x=0; x < (ssize_t) image->columns; x++)
986 GetPixelInfoPixel(image,p,&pixel);
987 height=color_matrix->height > 6 ? 6UL : color_matrix->height;
988 for (v=0; v < (ssize_t) height; v++)
993 sum=ColorMatrix[v][0]*GetPixelRed(image,p)+ColorMatrix[v][1]*
994 GetPixelGreen(image,p)+ColorMatrix[v][2]*GetPixelBlue(image,p);
995 if (image->colorspace == CMYKColorspace)
996 sum+=ColorMatrix[v][3]*GetPixelBlack(image,p);
997 if (image->matte != MagickFalse)
998 sum+=ColorMatrix[v][4]*GetPixelAlpha(image,p);
999 sum+=QuantumRange*ColorMatrix[v][5];
1002 case 0: pixel.red=sum; break;
1003 case 1: pixel.green=sum; break;
1004 case 2: pixel.blue=sum; break;
1005 case 3: pixel.black=sum; break;
1006 case 4: pixel.alpha=sum; break;
1010 SetPixelInfoPixel(color_image,&pixel,q);
1011 p+=GetPixelChannels(image);
1012 q+=GetPixelChannels(color_image);
1014 if (SyncCacheViewAuthenticPixels(color_view,exception) == MagickFalse)
1016 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1021 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1022 #pragma omp critical (MagickCore_ColorMatrixImage)
1024 proceed=SetImageProgress(image,ColorMatrixImageTag,progress++,
1026 if (proceed == MagickFalse)
1030 color_view=DestroyCacheView(color_view);
1031 image_view=DestroyCacheView(image_view);
1032 if (status == MagickFalse)
1033 color_image=DestroyImage(color_image);
1034 return(color_image);
1038 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1042 + D e s t r o y F x I n f o %
1046 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1048 % DestroyFxInfo() deallocates memory associated with an FxInfo structure.
1050 % The format of the DestroyFxInfo method is:
1052 % ImageInfo *DestroyFxInfo(ImageInfo *fx_info)
1054 % A description of each parameter follows:
1056 % o fx_info: the fx info.
1059 MagickPrivate FxInfo *DestroyFxInfo(FxInfo *fx_info)
1064 fx_info->exception=DestroyExceptionInfo(fx_info->exception);
1065 fx_info->expression=DestroyString(fx_info->expression);
1066 fx_info->symbols=DestroySplayTree(fx_info->symbols);
1067 fx_info->colors=DestroySplayTree(fx_info->colors);
1068 for (i=(ssize_t) GetImageListLength(fx_info->images)-1; i >= 0; i--)
1069 fx_info->view[i]=DestroyCacheView(fx_info->view[i]);
1070 fx_info->view=(CacheView **) RelinquishMagickMemory(fx_info->view);
1071 fx_info->random_info=DestroyRandomInfo(fx_info->random_info);
1072 fx_info=(FxInfo *) RelinquishMagickMemory(fx_info);
1077 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1081 + 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 %
1085 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1087 % FxEvaluateChannelExpression() evaluates an expression and returns the
1090 % The format of the FxEvaluateExpression method is:
1092 % double FxEvaluateChannelExpression(FxInfo *fx_info,
1093 % const PixelChannel channel,const ssize_t x,const ssize_t y,
1094 % double *alpha,Exceptioninfo *exception)
1095 % double FxEvaluateExpression(FxInfo *fx_info,
1096 % double *alpha,Exceptioninfo *exception)
1098 % A description of each parameter follows:
1100 % o fx_info: the fx info.
1102 % o channel: the channel.
1104 % o x,y: the pixel position.
1106 % o alpha: the result.
1108 % o exception: return any errors or warnings in this structure.
1112 static inline double MagickMax(const double x,const double y)
1119 static inline double MagickMin(const double x,const double y)
1126 static double FxChannelStatistics(FxInfo *fx_info,Image *image,
1127 PixelChannel channel,const char *symbol,ExceptionInfo *exception)
1134 statistic[MaxTextExtent];
1142 channel_mask=UndefinedChannel;
1143 for (p=symbol; (*p != '.') && (*p != '\0'); p++) ;
1149 option=ParseCommandOption(MagickPixelChannelOptions,MagickTrue,p+1);
1152 channel=(PixelChannel) option;
1153 channel_mask=(ChannelType) (channel_mask | (1 << channel));
1154 SetPixelChannelMapMask(image,channel_mask);
1157 (void) FormatLocaleString(key,MaxTextExtent,"%p.%.20g.%s",(void *) image,
1158 (double) channel,symbol);
1159 value=(const char *) GetValueFromSplayTree(fx_info->symbols,key);
1160 if (value != (const char *) NULL)
1162 if (channel_mask != UndefinedChannel)
1163 SetPixelChannelMapMask(image,channel_mask);
1164 return(QuantumScale*StringToDouble(value,(char **) NULL));
1166 (void) DeleteNodeFromSplayTree(fx_info->symbols,key);
1167 if (LocaleNCompare(symbol,"depth",5) == 0)
1172 depth=GetImageDepth(image,exception);
1173 (void) FormatLocaleString(statistic,MaxTextExtent,"%.20g",(double) depth);
1175 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1181 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
1182 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",kurtosis);
1184 if (LocaleNCompare(symbol,"maxima",6) == 0)
1190 (void) GetImageRange(image,&minima,&maxima,exception);
1191 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",maxima);
1193 if (LocaleNCompare(symbol,"mean",4) == 0)
1199 (void) GetImageMean(image,&mean,&standard_deviation,exception);
1200 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",mean);
1202 if (LocaleNCompare(symbol,"minima",6) == 0)
1208 (void) GetImageRange(image,&minima,&maxima,exception);
1209 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",minima);
1211 if (LocaleNCompare(symbol,"skewness",8) == 0)
1217 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
1218 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",skewness);
1220 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1226 (void) GetImageMean(image,&mean,&standard_deviation,exception);
1227 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",
1228 standard_deviation);
1230 if (channel_mask != UndefinedChannel)
1231 SetPixelChannelMapMask(image,channel_mask);
1232 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(key),
1233 ConstantString(statistic));
1234 return(QuantumScale*StringToDouble(statistic,(char **) NULL));
1238 FxEvaluateSubexpression(FxInfo *,const PixelChannel,const ssize_t,
1239 const ssize_t,const char *,double *,ExceptionInfo *);
1241 static MagickOffsetType FxGCD(MagickOffsetType alpha,MagickOffsetType beta)
1244 return(FxGCD(beta,alpha % beta));
1248 static inline const char *FxSubexpression(const char *expression,
1249 ExceptionInfo *exception)
1258 subexpression=expression;
1259 while ((*subexpression != '\0') &&
1260 ((level != 1) || (strchr(")",(int) *subexpression) == (char *) NULL)))
1262 if (strchr("(",(int) *subexpression) != (char *) NULL)
1265 if (strchr(")",(int) *subexpression) != (char *) NULL)
1269 if (*subexpression == '\0')
1270 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1271 "UnbalancedParenthesis","'%s'",expression);
1272 return(subexpression);
1275 static double FxGetSymbol(FxInfo *fx_info,const PixelChannel channel,
1276 const ssize_t x,const ssize_t y,const char *expression,
1277 ExceptionInfo *exception)
1281 subexpression[MaxTextExtent],
1282 symbol[MaxTextExtent];
1309 i=GetImageIndexInList(fx_info->images);
1313 if (isalpha((int) *(p+1)) == 0)
1315 if (strchr("suv",(int) *p) != (char *) NULL)
1322 i=GetImageIndexInList(fx_info->images);
1325 case 'u': i=0; break;
1326 case 'v': i=1; break;
1333 for (p++; *p != '\0'; )
1347 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1349 i=(ssize_t) (alpha+0.5);
1355 if ((isalpha((int) *(p+1)) == 0) && (*p == 'p'))
1362 for (p++; *p != '\0'; )
1376 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1387 for (p++; *p != '\0'; )
1401 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1411 length=GetImageListLength(fx_info->images);
1413 i+=(ssize_t) length;
1415 image=GetImageFromList(fx_info->images,i);
1416 if (image == (Image *) NULL)
1418 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1419 "NoSuchImage","'%s'",expression);
1422 GetPixelInfo(image,&pixel);
1423 (void) InterpolatePixelInfo(image,fx_info->view[i],image->interpolate,
1424 point.x,point.y,&pixel,exception);
1425 if ((strlen(p) > 2) && (LocaleCompare(p,"intensity") != 0) &&
1426 (LocaleCompare(p,"luminance") != 0) && (LocaleCompare(p,"hue") != 0) &&
1427 (LocaleCompare(p,"saturation") != 0) &&
1428 (LocaleCompare(p,"lightness") != 0))
1431 name[MaxTextExtent];
1433 (void) CopyMagickString(name,p,MaxTextExtent);
1434 for (q=name+(strlen(name)-1); q > name; q--)
1444 if ((strlen(name) > 2) &&
1445 (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
1450 color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
1451 if (color != (PixelInfo *) NULL)
1461 status=QueryColorCompliance(name,AllCompliance,&pixel,
1462 fx_info->exception);
1463 if (status != MagickFalse)
1465 (void) AddValueToSplayTree(fx_info->colors,ConstantString(
1466 name),ClonePixelInfo(&pixel));
1472 (void) CopyMagickString(symbol,p,MaxTextExtent);
1473 StripString(symbol);
1474 if (*symbol == '\0')
1478 case RedPixelChannel: return(QuantumScale*pixel.red);
1479 case GreenPixelChannel: return(QuantumScale*pixel.green);
1480 case BluePixelChannel: return(QuantumScale*pixel.blue);
1481 case BlackPixelChannel:
1483 if (image->colorspace != CMYKColorspace)
1485 (void) ThrowMagickException(exception,GetMagickModule(),
1486 ImageError,"ColorSeparatedImageRequired","'%s'",
1490 return(QuantumScale*pixel.black);
1492 case AlphaPixelChannel:
1497 if (pixel.matte == MagickFalse)
1499 alpha=(double) (QuantumScale*pixel.alpha);
1502 case IndexPixelChannel:
1504 case IntensityPixelChannel:
1506 return(QuantumScale*GetPixelInfoIntensity(&pixel));
1511 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1512 "UnableToParseExpression","'%s'",p);
1520 if (LocaleCompare(symbol,"a") == 0)
1521 return((double) (QuantumScale*pixel.alpha));
1527 if (LocaleCompare(symbol,"b") == 0)
1528 return(QuantumScale*pixel.blue);
1534 if (LocaleNCompare(symbol,"channel",7) == 0)
1542 flags=ParseGeometry(symbol+7,&channel_info);
1543 if (image->colorspace == CMYKColorspace)
1546 case CyanPixelChannel:
1548 if ((flags & RhoValue) == 0)
1550 return(channel_info.rho);
1552 case MagentaPixelChannel:
1554 if ((flags & SigmaValue) == 0)
1556 return(channel_info.sigma);
1558 case YellowPixelChannel:
1560 if ((flags & XiValue) == 0)
1562 return(channel_info.xi);
1564 case BlackPixelChannel:
1566 if ((flags & PsiValue) == 0)
1568 return(channel_info.psi);
1570 case AlphaPixelChannel:
1572 if ((flags & ChiValue) == 0)
1574 return(channel_info.chi);
1581 case RedPixelChannel:
1583 if ((flags & RhoValue) == 0)
1585 return(channel_info.rho);
1587 case GreenPixelChannel:
1589 if ((flags & SigmaValue) == 0)
1591 return(channel_info.sigma);
1593 case BluePixelChannel:
1595 if ((flags & XiValue) == 0)
1597 return(channel_info.xi);
1599 case BlackPixelChannel:
1601 if ((flags & ChiValue) == 0)
1603 return(channel_info.chi);
1605 case AlphaPixelChannel:
1607 if ((flags & PsiValue) == 0)
1609 return(channel_info.psi);
1616 if (LocaleCompare(symbol,"c") == 0)
1617 return(QuantumScale*pixel.red);
1623 if (LocaleNCompare(symbol,"depth",5) == 0)
1624 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1630 if (LocaleCompare(symbol,"g") == 0)
1631 return(QuantumScale*pixel.green);
1637 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1638 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1639 if (LocaleCompare(symbol,"k") == 0)
1641 if (image->colorspace != CMYKColorspace)
1643 (void) ThrowMagickException(exception,GetMagickModule(),
1644 OptionError,"ColorSeparatedImageRequired","'%s'",
1648 return(QuantumScale*pixel.black);
1655 if (LocaleCompare(symbol,"h") == 0)
1656 return((double) image->rows);
1657 if (LocaleCompare(symbol,"hue") == 0)
1664 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1673 if ((LocaleCompare(symbol,"image.depth") == 0) ||
1674 (LocaleCompare(symbol,"image.minima") == 0) ||
1675 (LocaleCompare(symbol,"image.maxima") == 0) ||
1676 (LocaleCompare(symbol,"image.mean") == 0) ||
1677 (LocaleCompare(symbol,"image.kurtosis") == 0) ||
1678 (LocaleCompare(symbol,"image.skewness") == 0) ||
1679 (LocaleCompare(symbol,"image.standard_deviation") == 0))
1680 return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
1681 if (LocaleCompare(symbol,"image.resolution.x") == 0)
1682 return(image->resolution.x);
1683 if (LocaleCompare(symbol,"image.resolution.y") == 0)
1684 return(image->resolution.y);
1685 if (LocaleCompare(symbol,"intensity") == 0)
1686 return(QuantumScale*GetPixelInfoIntensity(&pixel));
1687 if (LocaleCompare(symbol,"i") == 0)
1694 if (LocaleCompare(symbol,"j") == 0)
1701 if (LocaleCompare(symbol,"lightness") == 0)
1708 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1712 if (LocaleCompare(symbol,"luminance") == 0)
1717 luminence=0.2126*pixel.red+0.7152*pixel.green+0.0722*pixel.blue;
1718 return(QuantumScale*luminence);
1725 if (LocaleNCompare(symbol,"maxima",6) == 0)
1726 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1727 if (LocaleNCompare(symbol,"mean",4) == 0)
1728 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1729 if (LocaleNCompare(symbol,"minima",6) == 0)
1730 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1731 if (LocaleCompare(symbol,"m") == 0)
1732 return(QuantumScale*pixel.blue);
1738 if (LocaleCompare(symbol,"n") == 0)
1739 return((double) GetImageListLength(fx_info->images));
1745 if (LocaleCompare(symbol,"o") == 0)
1746 return(QuantumScale*pixel.alpha);
1752 if (LocaleCompare(symbol,"page.height") == 0)
1753 return((double) image->page.height);
1754 if (LocaleCompare(symbol,"page.width") == 0)
1755 return((double) image->page.width);
1756 if (LocaleCompare(symbol,"page.x") == 0)
1757 return((double) image->page.x);
1758 if (LocaleCompare(symbol,"page.y") == 0)
1759 return((double) image->page.y);
1765 if (LocaleCompare(symbol,"resolution.x") == 0)
1766 return(image->resolution.x);
1767 if (LocaleCompare(symbol,"resolution.y") == 0)
1768 return(image->resolution.y);
1769 if (LocaleCompare(symbol,"r") == 0)
1770 return(QuantumScale*pixel.red);
1776 if (LocaleCompare(symbol,"saturation") == 0)
1783 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1787 if (LocaleNCompare(symbol,"skewness",8) == 0)
1788 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1789 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1790 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1796 if (LocaleCompare(symbol,"t") == 0)
1797 return((double) GetImageIndexInList(fx_info->images));
1803 if (LocaleCompare(symbol,"w") == 0)
1804 return((double) image->columns);
1810 if (LocaleCompare(symbol,"y") == 0)
1811 return(QuantumScale*pixel.green);
1817 if (LocaleCompare(symbol,"z") == 0)
1822 depth=(double) GetImageDepth(image,fx_info->exception);
1830 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1831 if (value != (const char *) NULL)
1832 return((double) StringToDouble(value,(char **) NULL));
1833 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1834 "UnableToParseExpression","'%s'",symbol);
1838 static const char *FxOperatorPrecedence(const char *expression,
1839 ExceptionInfo *exception)
1843 UndefinedPrecedence,
1845 BitwiseComplementPrecedence,
1847 ExponentialNotationPrecedence,
1851 RelationalPrecedence,
1852 EquivalencyPrecedence,
1853 BitwiseAndPrecedence,
1854 BitwiseOrPrecedence,
1855 LogicalAndPrecedence,
1856 LogicalOrPrecedence,
1858 AssignmentPrecedence,
1878 subexpression=(const char *) NULL;
1879 target=NullPrecedence;
1880 while (*expression != '\0')
1882 precedence=UndefinedPrecedence;
1883 if ((isspace((int) ((char) *expression)) != 0) || (c == (int) '@'))
1888 switch (*expression)
1893 #if defined(MAGICKCORE_HAVE_ACOSH)
1894 if (LocaleNCompare(expression,"acosh",5) == 0)
1900 #if defined(MAGICKCORE_HAVE_ASINH)
1901 if (LocaleNCompare(expression,"asinh",5) == 0)
1907 #if defined(MAGICKCORE_HAVE_ATANH)
1908 if (LocaleNCompare(expression,"atanh",5) == 0)
1914 if (LocaleNCompare(expression,"atan2",5) == 0)
1924 if ((LocaleNCompare(expression,"E+",2) == 0) ||
1925 (LocaleNCompare(expression,"E-",2) == 0))
1927 expression+=2; /* scientific notation */
1934 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1935 (LocaleNCompare(expression,"j1",2) == 0))
1944 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1951 if ((c == (int) '{') || (c == (int) '['))
1954 if ((c == (int) '}') || (c == (int) ']'))
1957 switch ((unsigned char) *expression)
1962 precedence=BitwiseComplementPrecedence;
1968 precedence=ExponentPrecedence;
1973 if (((c != 0) && ((isdigit((int) ((char) c)) != 0) ||
1974 (strchr(")",c) != (char *) NULL))) &&
1975 (((islower((int) ((char) *expression)) != 0) ||
1976 (strchr("(",(int) *expression) != (char *) NULL)) ||
1977 ((isdigit((int) ((char) c)) == 0) &&
1978 (isdigit((int) ((char) *expression)) != 0))) &&
1979 (strchr("xy",(int) *expression) == (char *) NULL))
1980 precedence=MultiplyPrecedence;
1987 precedence=MultiplyPrecedence;
1993 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
1995 precedence=AdditionPrecedence;
1998 case LeftShiftOperator:
1999 case RightShiftOperator:
2001 precedence=ShiftPrecedence;
2005 case LessThanEqualOperator:
2006 case GreaterThanEqualOperator:
2009 precedence=RelationalPrecedence;
2013 case NotEqualOperator:
2015 precedence=EquivalencyPrecedence;
2020 precedence=BitwiseAndPrecedence;
2025 precedence=BitwiseOrPrecedence;
2028 case LogicalAndOperator:
2030 precedence=LogicalAndPrecedence;
2033 case LogicalOrOperator:
2035 precedence=LogicalOrPrecedence;
2038 case ExponentialNotation:
2040 precedence=ExponentialNotationPrecedence;
2046 precedence=TernaryPrecedence;
2051 precedence=AssignmentPrecedence;
2056 precedence=CommaPrecedence;
2061 precedence=SeparatorPrecedence;
2065 if ((precedence == BitwiseComplementPrecedence) ||
2066 (precedence == TernaryPrecedence) ||
2067 (precedence == AssignmentPrecedence))
2069 if (precedence > target)
2072 Right-to-left associativity.
2075 subexpression=expression;
2079 if (precedence >= target)
2082 Left-to-right associativity.
2085 subexpression=expression;
2087 if (strchr("(",(int) *expression) != (char *) NULL)
2088 expression=FxSubexpression(expression,exception);
2089 c=(int) (*expression++);
2091 return(subexpression);
2094 static double FxEvaluateSubexpression(FxInfo *fx_info,
2095 const PixelChannel channel,const ssize_t x,const ssize_t y,
2096 const char *expression,double *beta,ExceptionInfo *exception)
2100 subexpression[MaxTextExtent];
2110 if (exception->severity != UndefinedException)
2112 while (isspace((int) *expression) != 0)
2114 if (*expression == '\0')
2116 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
2117 "MissingExpression","'%s'",expression);
2120 *subexpression='\0';
2121 p=FxOperatorPrecedence(expression,exception);
2122 if (p != (const char *) NULL)
2124 (void) CopyMagickString(subexpression,expression,(size_t)
2126 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2128 switch ((unsigned char) *p)
2132 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2133 *beta=(double) (~(size_t) *beta);
2138 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2139 return(*beta == 0.0 ? 1.0 : 0.0);
2143 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2144 channel,x,y,++p,beta,exception));
2148 case ExponentialNotation:
2150 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2151 return(alpha*(*beta));
2155 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2158 if (exception->severity == UndefinedException)
2159 (void) ThrowMagickException(exception,GetMagickModule(),
2160 OptionError,"DivideByZero","'%s'",expression);
2163 return(alpha/(*beta));
2167 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2168 *beta=fabs(floor(((double) *beta)+0.5));
2171 (void) ThrowMagickException(exception,GetMagickModule(),
2172 OptionError,"DivideByZero","'%s'",expression);
2175 return(fmod((double) alpha,(double) *beta));
2179 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2180 return(alpha+(*beta));
2184 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2185 return(alpha-(*beta));
2187 case LeftShiftOperator:
2189 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2190 *beta=(double) ((size_t) (alpha+0.5) << (size_t) (gamma+0.5));
2193 case RightShiftOperator:
2195 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2196 *beta=(double) ((size_t) (alpha+0.5) >> (size_t) (gamma+0.5));
2201 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2202 return(alpha < *beta ? 1.0 : 0.0);
2204 case LessThanEqualOperator:
2206 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2207 return(alpha <= *beta ? 1.0 : 0.0);
2211 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2212 return(alpha > *beta ? 1.0 : 0.0);
2214 case GreaterThanEqualOperator:
2216 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2217 return(alpha >= *beta ? 1.0 : 0.0);
2221 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2222 return(fabs(alpha-(*beta)) < MagickEpsilon ? MagickEpsilon : 0.0);
2224 case NotEqualOperator:
2226 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2227 return(fabs(alpha-(*beta)) >= MagickEpsilon ? 1.0 : 0.0);
2231 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2232 *beta=(double) ((size_t) (alpha+0.5) & (size_t) (gamma+0.5));
2237 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2238 *beta=(double) ((size_t) (alpha+0.5) | (size_t) (gamma+0.5));
2241 case LogicalAndOperator:
2243 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2244 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2247 case LogicalOrOperator:
2249 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2250 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2258 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2260 p=StringToken(":",&q);
2261 if (q == (char *) NULL)
2263 (void) ThrowMagickException(exception,GetMagickModule(),
2264 OptionError,"UnableToParseExpression","'%s'",subexpression);
2267 if (fabs((double) alpha) >= MagickEpsilon)
2268 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2270 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2276 numeric[MaxTextExtent];
2279 while (isalpha((int) ((unsigned char) *q)) != 0)
2283 (void) ThrowMagickException(exception,GetMagickModule(),
2284 OptionError,"UnableToParseExpression","'%s'",subexpression);
2287 ClearMagickException(exception);
2288 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2289 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
2291 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2292 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2293 subexpression),ConstantString(numeric));
2298 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2303 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2308 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2314 if (strchr("(",(int) *expression) != (char *) NULL)
2316 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2317 subexpression[strlen(subexpression)-1]='\0';
2318 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2322 switch (*expression)
2326 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2332 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2338 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2340 return((double) (~(size_t) (gamma+0.5)));
2345 if (LocaleNCompare(expression,"abs",3) == 0)
2347 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2349 return((double) fabs((double) alpha));
2351 #if defined(MAGICKCORE_HAVE_ACOSH)
2352 if (LocaleNCompare(expression,"acosh",5) == 0)
2354 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2356 return((double) acosh((double) alpha));
2359 if (LocaleNCompare(expression,"acos",4) == 0)
2361 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2363 return((double) acos((double) alpha));
2365 #if defined(MAGICKCORE_HAVE_J1)
2366 if (LocaleNCompare(expression,"airy",4) == 0)
2368 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2372 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
2373 return(gamma*gamma);
2376 #if defined(MAGICKCORE_HAVE_ASINH)
2377 if (LocaleNCompare(expression,"asinh",5) == 0)
2379 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2381 return((double) asinh((double) alpha));
2384 if (LocaleNCompare(expression,"asin",4) == 0)
2386 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2388 return((double) asin((double) alpha));
2390 if (LocaleNCompare(expression,"alt",3) == 0)
2392 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2394 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
2396 if (LocaleNCompare(expression,"atan2",5) == 0)
2398 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2400 return((double) atan2((double) alpha,(double) *beta));
2402 #if defined(MAGICKCORE_HAVE_ATANH)
2403 if (LocaleNCompare(expression,"atanh",5) == 0)
2405 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2407 return((double) atanh((double) alpha));
2410 if (LocaleNCompare(expression,"atan",4) == 0)
2412 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2414 return((double) atan((double) alpha));
2416 if (LocaleCompare(expression,"a") == 0)
2417 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2423 if (LocaleCompare(expression,"b") == 0)
2424 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2430 if (LocaleNCompare(expression,"ceil",4) == 0)
2432 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2434 return((double) ceil((double) alpha));
2436 if (LocaleNCompare(expression,"cosh",4) == 0)
2438 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2440 return((double) cosh((double) alpha));
2442 if (LocaleNCompare(expression,"cos",3) == 0)
2444 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2446 return((double) cos((double) alpha));
2448 if (LocaleCompare(expression,"c") == 0)
2449 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2455 if (LocaleNCompare(expression,"debug",5) == 0)
2460 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2462 if (fx_info->images->colorspace == CMYKColorspace)
2465 case CyanPixelChannel: type="cyan"; break;
2466 case MagentaPixelChannel: type="magenta"; break;
2467 case YellowPixelChannel: type="yellow"; break;
2468 case AlphaPixelChannel: type="opacity"; break;
2469 case BlackPixelChannel: type="black"; break;
2470 default: type="unknown"; break;
2475 case RedPixelChannel: type="red"; break;
2476 case GreenPixelChannel: type="green"; break;
2477 case BluePixelChannel: type="blue"; break;
2478 case AlphaPixelChannel: type="opacity"; break;
2479 default: type="unknown"; break;
2481 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2482 if (strlen(subexpression) > 1)
2483 subexpression[strlen(subexpression)-1]='\0';
2484 if (fx_info->file != (FILE *) NULL)
2485 (void) FormatLocaleFile(fx_info->file,"%s[%.20g,%.20g].%s: "
2486 "%s=%.*g\n",fx_info->images->filename,(double) x,(double) y,type,
2487 subexpression,GetMagickPrecision(),(double) alpha);
2490 if (LocaleNCompare(expression,"drc",3) == 0)
2492 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2494 return((double) (alpha/(*beta*(alpha-1.0)+1.0)));
2501 if (LocaleCompare(expression,"epsilon") == 0)
2502 return((double) MagickEpsilon);
2503 if (LocaleNCompare(expression,"exp",3) == 0)
2505 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2507 return((double) exp((double) alpha));
2509 if (LocaleCompare(expression,"e") == 0)
2510 return((double) 2.7182818284590452354);
2516 if (LocaleNCompare(expression,"floor",5) == 0)
2518 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2520 return((double) floor((double) alpha));
2527 if (LocaleNCompare(expression,"gauss",5) == 0)
2529 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2531 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
2532 return((double) gamma);
2534 if (LocaleNCompare(expression,"gcd",3) == 0)
2539 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2541 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType) (*beta+
2543 return((double) gcd);
2545 if (LocaleCompare(expression,"g") == 0)
2546 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2552 if (LocaleCompare(expression,"h") == 0)
2553 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2554 if (LocaleCompare(expression,"hue") == 0)
2555 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2556 if (LocaleNCompare(expression,"hypot",5) == 0)
2558 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2560 return((double) hypot((double) alpha,(double) *beta));
2567 if (LocaleCompare(expression,"k") == 0)
2568 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2574 if (LocaleCompare(expression,"intensity") == 0)
2575 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2576 if (LocaleNCompare(expression,"int",3) == 0)
2578 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2580 return((double) floor(alpha));
2582 #if defined(MAGICKCORE_HAVE_ISNAN)
2583 if (LocaleNCompare(expression,"isnan",5) == 0)
2585 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2587 return((double) !!isnan((double) alpha));
2590 if (LocaleCompare(expression,"i") == 0)
2591 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2597 if (LocaleCompare(expression,"j") == 0)
2598 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2599 #if defined(MAGICKCORE_HAVE_J0)
2600 if (LocaleNCompare(expression,"j0",2) == 0)
2602 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2604 return((double) j0((double) alpha));
2607 #if defined(MAGICKCORE_HAVE_J1)
2608 if (LocaleNCompare(expression,"j1",2) == 0)
2610 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2612 return((double) j1((double) alpha));
2615 #if defined(MAGICKCORE_HAVE_J1)
2616 if (LocaleNCompare(expression,"jinc",4) == 0)
2618 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2622 gamma=(double) (2.0*j1((double) (MagickPI*alpha))/(MagickPI*
2632 if (LocaleNCompare(expression,"ln",2) == 0)
2634 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2636 return((double) log((double) alpha));
2638 if (LocaleNCompare(expression,"logtwo",6) == 0)
2640 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2642 return((double) log10((double) alpha))/log10(2.0);
2644 if (LocaleNCompare(expression,"log",3) == 0)
2646 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2648 return((double) log10((double) alpha));
2650 if (LocaleCompare(expression,"lightness") == 0)
2651 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2657 if (LocaleCompare(expression,"MaxRGB") == 0)
2658 return((double) QuantumRange);
2659 if (LocaleNCompare(expression,"maxima",6) == 0)
2661 if (LocaleNCompare(expression,"max",3) == 0)
2663 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2665 return(alpha > *beta ? alpha : *beta);
2667 if (LocaleNCompare(expression,"minima",6) == 0)
2669 if (LocaleNCompare(expression,"min",3) == 0)
2671 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2673 return(alpha < *beta ? alpha : *beta);
2675 if (LocaleNCompare(expression,"mod",3) == 0)
2677 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2679 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2682 if (LocaleCompare(expression,"m") == 0)
2683 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2689 if (LocaleNCompare(expression,"not",3) == 0)
2691 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2693 return((double) (alpha < MagickEpsilon));
2695 if (LocaleCompare(expression,"n") == 0)
2696 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2702 if (LocaleCompare(expression,"Opaque") == 0)
2704 if (LocaleCompare(expression,"o") == 0)
2705 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2711 if (LocaleCompare(expression,"phi") == 0)
2712 return((double) MagickPHI);
2713 if (LocaleCompare(expression,"pi") == 0)
2714 return((double) MagickPI);
2715 if (LocaleNCompare(expression,"pow",3) == 0)
2717 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2719 return((double) pow((double) alpha,(double) *beta));
2721 if (LocaleCompare(expression,"p") == 0)
2722 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2728 if (LocaleCompare(expression,"QuantumRange") == 0)
2729 return((double) QuantumRange);
2730 if (LocaleCompare(expression,"QuantumScale") == 0)
2731 return((double) QuantumScale);
2737 if (LocaleNCompare(expression,"rand",4) == 0)
2738 return((double) GetPseudoRandomValue(fx_info->random_info));
2739 if (LocaleNCompare(expression,"round",5) == 0)
2741 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2743 return((double) floor((double) alpha+0.5));
2745 if (LocaleCompare(expression,"r") == 0)
2746 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2752 if (LocaleCompare(expression,"saturation") == 0)
2753 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2754 if (LocaleNCompare(expression,"sign",4) == 0)
2756 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2758 return(alpha < 0.0 ? -1.0 : 1.0);
2760 if (LocaleNCompare(expression,"sinc",4) == 0)
2762 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2766 gamma=(double) (sin((double) (MagickPI*alpha))/
2770 if (LocaleNCompare(expression,"sinh",4) == 0)
2772 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2774 return((double) sinh((double) alpha));
2776 if (LocaleNCompare(expression,"sin",3) == 0)
2778 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2780 return((double) sin((double) alpha));
2782 if (LocaleNCompare(expression,"sqrt",4) == 0)
2784 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2786 return((double) sqrt((double) alpha));
2788 if (LocaleNCompare(expression,"squish",6) == 0)
2790 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2792 return((double) (1.0/(1.0+exp((double) (-alpha)))));
2794 if (LocaleCompare(expression,"s") == 0)
2795 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2801 if (LocaleNCompare(expression,"tanh",4) == 0)
2803 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2805 return((double) tanh((double) alpha));
2807 if (LocaleNCompare(expression,"tan",3) == 0)
2809 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2811 return((double) tan((double) alpha));
2813 if (LocaleCompare(expression,"Transparent") == 0)
2815 if (LocaleNCompare(expression,"trunc",5) == 0)
2817 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2820 return((double) floor((double) alpha));
2821 return((double) ceil((double) alpha));
2823 if (LocaleCompare(expression,"t") == 0)
2824 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2830 if (LocaleCompare(expression,"u") == 0)
2831 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2837 if (LocaleCompare(expression,"v") == 0)
2838 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2844 if (LocaleNCompare(expression,"while",5) == 0)
2848 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2850 } while (fabs((double) alpha) >= MagickEpsilon);
2851 return((double) *beta);
2853 if (LocaleCompare(expression,"w") == 0)
2854 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2860 if (LocaleCompare(expression,"y") == 0)
2861 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2867 if (LocaleCompare(expression,"z") == 0)
2868 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2874 q=(char *) expression;
2875 alpha=InterpretSiPrefixValue(expression,&q);
2876 if (q == expression)
2877 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2881 MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
2882 double *alpha,ExceptionInfo *exception)
2887 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2892 MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
2893 double *alpha,ExceptionInfo *exception)
2902 fx_info->file=(FILE *) NULL;
2903 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2909 MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
2910 const PixelChannel channel,const ssize_t x,const ssize_t y,
2911 double *alpha,ExceptionInfo *exception)
2917 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2919 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2923 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2931 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2933 % FxImage() applies a mathematical expression to the specified image.
2935 % The format of the FxImage method is:
2937 % Image *FxImage(const Image *image,const char *expression,
2938 % ExceptionInfo *exception)
2940 % A description of each parameter follows:
2942 % o image: the image.
2944 % o expression: A mathematical expression.
2946 % o exception: return any errors or warnings in this structure.
2950 static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2955 assert(fx_info != (FxInfo **) NULL);
2956 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
2957 if (fx_info[i] != (FxInfo *) NULL)
2958 fx_info[i]=DestroyFxInfo(fx_info[i]);
2959 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
2963 static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2964 ExceptionInfo *exception)
2981 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
2982 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
2983 if (fx_info == (FxInfo **) NULL)
2984 return((FxInfo **) NULL);
2985 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2986 if (*expression != '@')
2987 fx_expression=ConstantString(expression);
2989 fx_expression=FileToString(expression+1,~0,exception);
2990 for (i=0; i < (ssize_t) number_threads; i++)
2992 fx_info[i]=AcquireFxInfo(image,fx_expression,exception);
2993 if (fx_info[i] == (FxInfo *) NULL)
2994 return(DestroyFxThreadSet(fx_info));
2995 (void) FxPreprocessExpression(fx_info[i],&alpha,fx_info[i]->exception);
2997 fx_expression=DestroyString(fx_expression);
3001 MagickExport Image *FxImage(const Image *image,const char *expression,
3002 ExceptionInfo *exception)
3004 #define FxImageTag "Fx/Image"
3028 assert(image != (Image *) NULL);
3029 assert(image->signature == MagickSignature);
3030 if (image->debug != MagickFalse)
3031 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3032 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3033 if (fx_image == (Image *) NULL)
3034 return((Image *) NULL);
3035 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
3037 fx_image=DestroyImage(fx_image);
3038 return((Image *) NULL);
3040 fx_info=AcquireFxThreadSet(image,expression,exception);
3041 if (fx_info == (FxInfo **) NULL)
3043 fx_image=DestroyImage(fx_image);
3044 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3046 status=FxPreprocessExpression(fx_info[0],&alpha,exception);
3047 if (status == MagickFalse)
3049 fx_image=DestroyImage(fx_image);
3050 fx_info=DestroyFxThreadSet(fx_info);
3051 return((Image *) NULL);
3058 image_view=AcquireVirtualCacheView(image,exception);
3059 fx_view=AcquireAuthenticCacheView(fx_image,exception);
3060 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3061 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3062 dynamic_number_threads(image,image->columns,image->rows,1)
3064 for (y=0; y < (ssize_t) fx_image->rows; y++)
3067 id = GetOpenMPThreadId();
3069 register const Quantum
3078 if (status == MagickFalse)
3080 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3081 q=QueueCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
3082 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3087 for (x=0; x < (ssize_t) fx_image->columns; x++)
3092 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3104 channel=GetPixelChannelMapChannel(image,i);
3105 traits=GetPixelChannelMapTraits(image,channel);
3106 fx_traits=GetPixelChannelMapTraits(fx_image,channel);
3107 if ((traits == UndefinedPixelTrait) ||
3108 (fx_traits == UndefinedPixelTrait))
3110 if (((fx_traits & CopyPixelTrait) != 0) ||
3111 (GetPixelMask(image,p) != 0))
3113 SetPixelChannel(fx_image,channel,p[i],q);
3117 (void) FxEvaluateChannelExpression(fx_info[id],channel,x,y,&alpha,
3119 q[i]=ClampToQuantum(QuantumRange*alpha);
3121 p+=GetPixelChannels(image);
3122 q+=GetPixelChannels(fx_image);
3124 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3126 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3131 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3132 #pragma omp critical (MagickCore_FxImage)
3134 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3135 if (proceed == MagickFalse)
3139 fx_view=DestroyCacheView(fx_view);
3140 image_view=DestroyCacheView(image_view);
3141 fx_info=DestroyFxThreadSet(fx_info);
3142 if (status == MagickFalse)
3143 fx_image=DestroyImage(fx_image);
3148 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3152 % I m p l o d e I m a g e %
3156 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3158 % ImplodeImage() creates a new image that is a copy of an existing
3159 % one with the image pixels "implode" by the specified percentage. It
3160 % allocates the memory necessary for the new Image structure and returns a
3161 % pointer to the new image.
3163 % The format of the ImplodeImage method is:
3165 % Image *ImplodeImage(const Image *image,const double amount,
3166 % const PixelInterpolateMethod method,ExceptionInfo *exception)
3168 % A description of each parameter follows:
3170 % o implode_image: Method ImplodeImage returns a pointer to the image
3171 % after it is implode. A null image is returned if there is a memory
3174 % o image: the image.
3176 % o amount: Define the extent of the implosion.
3178 % o method: the pixel interpolation method.
3180 % o exception: return any errors or warnings in this structure.
3183 MagickExport Image *ImplodeImage(const Image *image,const double amount,
3184 const PixelInterpolateMethod method,ExceptionInfo *exception)
3186 #define ImplodeImageTag "Implode/Image"
3212 Initialize implode image attributes.
3214 assert(image != (Image *) NULL);
3215 assert(image->signature == MagickSignature);
3216 if (image->debug != MagickFalse)
3217 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3218 assert(exception != (ExceptionInfo *) NULL);
3219 assert(exception->signature == MagickSignature);
3220 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3222 if (implode_image == (Image *) NULL)
3223 return((Image *) NULL);
3224 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
3226 implode_image=DestroyImage(implode_image);
3227 return((Image *) NULL);
3229 if (implode_image->background_color.alpha != OpaqueAlpha)
3230 implode_image->matte=MagickTrue;
3232 Compute scaling factor.
3236 center.x=0.5*image->columns;
3237 center.y=0.5*image->rows;
3239 if (image->columns > image->rows)
3240 scale.y=(double) image->columns/(double) image->rows;
3242 if (image->columns < image->rows)
3244 scale.x=(double) image->rows/(double) image->columns;
3252 image_view=AcquireVirtualCacheView(image,exception);
3253 implode_view=AcquireAuthenticCacheView(implode_image,exception);
3254 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3255 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3256 dynamic_number_threads(image,image->columns,image->rows,1)
3258 for (y=0; y < (ssize_t) image->rows; y++)
3266 register const Quantum
3275 if (status == MagickFalse)
3277 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3278 q=QueueCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
3280 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3285 delta.y=scale.y*(double) (y-center.y);
3286 for (x=0; x < (ssize_t) image->columns; x++)
3292 Determine if the pixel is within an ellipse.
3294 if (GetPixelMask(image,p) != 0)
3296 p+=GetPixelChannels(image);
3297 q+=GetPixelChannels(implode_image);
3300 delta.x=scale.x*(double) (x-center.x);
3301 distance=delta.x*delta.x+delta.y*delta.y;
3302 if (distance >= (radius*radius))
3303 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3312 channel=GetPixelChannelMapChannel(image,i);
3313 traits=GetPixelChannelMapTraits(image,channel);
3314 implode_traits=GetPixelChannelMapTraits(implode_image,channel);
3315 if ((traits == UndefinedPixelTrait) ||
3316 (implode_traits == UndefinedPixelTrait))
3318 SetPixelChannel(implode_image,channel,p[i],q);
3330 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/radius/
3332 status=InterpolatePixelChannels(image,image_view,implode_image,method,
3333 (double) (factor*delta.x/scale.x+center.x),(double) (factor*delta.y/
3334 scale.y+center.y),q,exception);
3336 p+=GetPixelChannels(image);
3337 q+=GetPixelChannels(implode_image);
3339 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3341 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3346 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3347 #pragma omp critical (MagickCore_ImplodeImage)
3349 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3350 if (proceed == MagickFalse)
3354 implode_view=DestroyCacheView(implode_view);
3355 image_view=DestroyCacheView(image_view);
3356 if (status == MagickFalse)
3357 implode_image=DestroyImage(implode_image);
3358 return(implode_image);
3362 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3366 % M o r p h I m a g e s %
3370 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3372 % The MorphImages() method requires a minimum of two images. The first
3373 % image is transformed into the second by a number of intervening images
3374 % as specified by frames.
3376 % The format of the MorphImage method is:
3378 % Image *MorphImages(const Image *image,const size_t number_frames,
3379 % ExceptionInfo *exception)
3381 % A description of each parameter follows:
3383 % o image: the image.
3385 % o number_frames: Define the number of in-between image to generate.
3386 % The more in-between frames, the smoother the morph.
3388 % o exception: return any errors or warnings in this structure.
3391 MagickExport Image *MorphImages(const Image *image,
3392 const size_t number_frames,ExceptionInfo *exception)
3394 #define MorphImageTag "Morph/Image"
3410 register const Image
3420 Clone first frame in sequence.
3422 assert(image != (Image *) NULL);
3423 assert(image->signature == MagickSignature);
3424 if (image->debug != MagickFalse)
3425 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3426 assert(exception != (ExceptionInfo *) NULL);
3427 assert(exception->signature == MagickSignature);
3428 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3429 if (morph_images == (Image *) NULL)
3430 return((Image *) NULL);
3431 if (GetNextImageInList(image) == (Image *) NULL)
3436 for (i=1; i < (ssize_t) number_frames; i++)
3438 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3439 if (morph_image == (Image *) NULL)
3441 morph_images=DestroyImageList(morph_images);
3442 return((Image *) NULL);
3444 AppendImageToList(&morph_images,morph_image);
3445 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3450 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3452 if (proceed == MagickFalse)
3456 return(GetFirstImageInList(morph_images));
3459 Morph image sequence.
3464 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3466 for (i=0; i < (ssize_t) number_frames; i++)
3472 beta=(double) (i+1.0)/(double) (number_frames+1.0);
3474 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
3475 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*next->rows+beta*
3476 GetNextImageInList(next)->rows+0.5),next->filter,exception);
3477 if (morph_image == (Image *) NULL)
3479 morph_images=DestroyImageList(morph_images);
3480 return((Image *) NULL);
3482 status=SetImageStorageClass(morph_image,DirectClass,exception);
3483 if (status == MagickFalse)
3485 morph_image=DestroyImage(morph_image);
3486 return((Image *) NULL);
3488 AppendImageToList(&morph_images,morph_image);
3489 morph_images=GetLastImageInList(morph_images);
3490 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
3491 morph_images->rows,GetNextImageInList(next)->filter,exception);
3492 if (morph_image == (Image *) NULL)
3494 morph_images=DestroyImageList(morph_images);
3495 return((Image *) NULL);
3497 image_view=AcquireVirtualCacheView(morph_image,exception);
3498 morph_view=AcquireAuthenticCacheView(morph_images,exception);
3499 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3500 #pragma omp parallel for schedule(static,4) shared(status) \
3501 dynamic_number_threads(image,image->columns,image->rows,1)
3503 for (y=0; y < (ssize_t) morph_images->rows; y++)
3508 register const Quantum
3517 if (status == MagickFalse)
3519 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3521 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3523 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3528 for (x=0; x < (ssize_t) morph_images->columns; x++)
3533 for (i=0; i < (ssize_t) GetPixelChannels(morph_image); i++)
3542 channel=GetPixelChannelMapChannel(image,i);
3543 traits=GetPixelChannelMapTraits(image,channel);
3544 morph_traits=GetPixelChannelMapTraits(morph_image,channel);
3545 if ((traits == UndefinedPixelTrait) ||
3546 (morph_traits == UndefinedPixelTrait))
3548 if (((morph_traits & CopyPixelTrait) != 0) ||
3549 (GetPixelMask(image,p) != 0))
3551 SetPixelChannel(morph_image,channel,p[i],q);
3554 SetPixelChannel(morph_image,channel,ClampToQuantum(alpha*
3555 GetPixelChannel(morph_images,channel,q)+beta*p[i]),q);
3557 p+=GetPixelChannels(morph_image);
3558 q+=GetPixelChannels(morph_images);
3560 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3561 if (sync == MagickFalse)
3564 morph_view=DestroyCacheView(morph_view);
3565 image_view=DestroyCacheView(image_view);
3566 morph_image=DestroyImage(morph_image);
3568 if (i < (ssize_t) number_frames)
3571 Clone last frame in sequence.
3573 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3574 if (morph_image == (Image *) NULL)
3576 morph_images=DestroyImageList(morph_images);
3577 return((Image *) NULL);
3579 AppendImageToList(&morph_images,morph_image);
3580 morph_images=GetLastImageInList(morph_images);
3581 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3586 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3587 #pragma omp critical (MagickCore_MorphImages)
3589 proceed=SetImageProgress(image,MorphImageTag,scene,
3590 GetImageListLength(image));
3591 if (proceed == MagickFalse)
3596 if (GetNextImageInList(next) != (Image *) NULL)
3598 morph_images=DestroyImageList(morph_images);
3599 return((Image *) NULL);
3601 return(GetFirstImageInList(morph_images));
3605 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3609 % P l a s m a I m a g e %
3613 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3615 % PlasmaImage() initializes an image with plasma fractal values. The image
3616 % must be initialized with a base color and the random number generator
3617 % seeded before this method is called.
3619 % The format of the PlasmaImage method is:
3621 % MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
3622 % size_t attenuate,size_t depth,ExceptionInfo *exception)
3624 % A description of each parameter follows:
3626 % o image: the image.
3628 % o segment: Define the region to apply plasma fractals values.
3630 % o attenuate: Define the plasma attenuation factor.
3632 % o depth: Limit the plasma recursion depth.
3634 % o exception: return any errors or warnings in this structure.
3638 static inline Quantum PlasmaPixel(RandomInfo *random_info,
3639 const double pixel,const double noise)
3644 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
3649 static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3650 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3651 const SegmentInfo *segment,size_t attenuate,size_t depth,
3652 ExceptionInfo *exception)
3663 register const Quantum
3679 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3687 Divide the area into quadrants and recurse.
3691 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3692 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
3693 local_info=(*segment);
3694 local_info.x2=(double) x_mid;
3695 local_info.y2=(double) y_mid;
3696 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3697 &local_info,attenuate,depth,exception);
3698 local_info=(*segment);
3699 local_info.y1=(double) y_mid;
3700 local_info.x2=(double) x_mid;
3701 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3702 &local_info,attenuate,depth,exception);
3703 local_info=(*segment);
3704 local_info.x1=(double) x_mid;
3705 local_info.y2=(double) y_mid;
3706 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3707 &local_info,attenuate,depth,exception);
3708 local_info=(*segment);
3709 local_info.x1=(double) x_mid;
3710 local_info.y1=(double) y_mid;
3711 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3712 &local_info,attenuate,depth,exception));
3714 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3715 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
3716 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3717 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3718 return(MagickFalse);
3720 Average pixels and apply plasma.
3722 plasma=(double) QuantumRange/(2.0*attenuate);
3723 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3728 x=(ssize_t) ceil(segment->x1-0.5);
3729 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),1,1,
3731 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),1,1,
3733 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
3734 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3735 (q == (Quantum *) NULL))
3737 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3739 channel=GetPixelChannelMapChannel(image,i);
3740 traits=GetPixelChannelMapTraits(image,channel);
3741 if (traits == UndefinedPixelTrait)
3743 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3745 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3746 if (segment->x1 != segment->x2)
3751 x=(ssize_t) ceil(segment->x2-0.5);
3752 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3754 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3756 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
3757 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3758 (q == (Quantum *) NULL))
3760 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3762 channel=GetPixelChannelMapChannel(image,i);
3763 traits=GetPixelChannelMapTraits(image,channel);
3764 if (traits == UndefinedPixelTrait)
3766 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3768 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3771 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3773 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3778 y=(ssize_t) ceil(segment->y2-0.5);
3779 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3781 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3783 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
3784 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3785 (q == (Quantum *) NULL))
3787 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3789 channel=GetPixelChannelMapChannel(image,i);
3790 traits=GetPixelChannelMapTraits(image,channel);
3791 if (traits == UndefinedPixelTrait)
3793 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3795 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3797 if (segment->y1 != segment->y2)
3802 y=(ssize_t) ceil(segment->y1-0.5);
3803 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3805 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3807 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
3808 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3809 (q == (Quantum *) NULL))
3811 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3813 channel=GetPixelChannelMapChannel(image,i);
3814 traits=GetPixelChannelMapTraits(image,channel);
3815 if (traits == UndefinedPixelTrait)
3817 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3819 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3822 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3827 x=(ssize_t) ceil(segment->x1-0.5);
3828 y=(ssize_t) ceil(segment->y1-0.5);
3829 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
3830 x=(ssize_t) ceil(segment->x2-0.5);
3831 y=(ssize_t) ceil(segment->y2-0.5);
3832 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
3833 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
3834 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3835 (q == (Quantum *) NULL))
3837 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3839 channel=GetPixelChannelMapChannel(image,i);
3840 traits=GetPixelChannelMapTraits(image,channel);
3841 if (traits == UndefinedPixelTrait)
3843 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3845 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3847 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3849 return(MagickFalse);
3852 MagickExport MagickBooleanType PlasmaImage(Image *image,
3853 const SegmentInfo *segment,size_t attenuate,size_t depth,
3854 ExceptionInfo *exception)
3867 if (image->debug != MagickFalse)
3868 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3869 assert(image != (Image *) NULL);
3870 assert(image->signature == MagickSignature);
3871 if (image->debug != MagickFalse)
3872 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3873 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
3874 return(MagickFalse);
3875 image_view=AcquireAuthenticCacheView(image,exception);
3876 u_view=AcquireVirtualCacheView(image,exception);
3877 v_view=AcquireVirtualCacheView(image,exception);
3878 random_info=AcquireRandomInfo();
3879 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3880 attenuate,depth,exception);
3881 random_info=DestroyRandomInfo(random_info);
3882 v_view=DestroyCacheView(v_view);
3883 u_view=DestroyCacheView(u_view);
3884 image_view=DestroyCacheView(image_view);
3889 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3893 % P o l a r o i d I m a g e %
3897 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3899 % PolaroidImage() simulates a Polaroid picture.
3901 % The format of the AnnotateImage method is:
3903 % Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
3904 % const char *caption,const double angle,
3905 % const PixelInterpolateMethod method,ExceptionInfo exception)
3907 % A description of each parameter follows:
3909 % o image: the image.
3911 % o draw_info: the draw info.
3913 % o caption: the Polaroid caption.
3915 % o angle: Apply the effect along this angle.
3917 % o method: the pixel interpolation method.
3919 % o exception: return any errors or warnings in this structure.
3922 MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
3923 const char *caption,const double angle,const PixelInterpolateMethod method,
3924 ExceptionInfo *exception)
3942 Simulate a Polaroid picture.
3944 assert(image != (Image *) NULL);
3945 assert(image->signature == MagickSignature);
3946 if (image->debug != MagickFalse)
3947 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3948 assert(exception != (ExceptionInfo *) NULL);
3949 assert(exception->signature == MagickSignature);
3950 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
3951 image->rows)/25.0,10.0);
3952 height=image->rows+2*quantum;
3953 caption_image=(Image *) NULL;
3954 if (caption != (const char *) NULL)
3957 geometry[MaxTextExtent],
3973 Generate caption image.
3975 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3976 if (caption_image == (Image *) NULL)
3977 return((Image *) NULL);
3978 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
3979 text=InterpretImageProperties((ImageInfo *) NULL,(Image *) image,caption,
3981 (void) CloneString(&annotate_info->text,text);
3982 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
3984 status=SetImageExtent(caption_image,image->columns,(size_t) ((count+1)*
3985 (metrics.ascent-metrics.descent)+0.5),exception);
3986 if (status == MagickFalse)
3987 caption_image=DestroyImage(caption_image);
3990 caption_image->background_color=image->border_color;
3991 (void) SetImageBackgroundColor(caption_image,exception);
3992 (void) CloneString(&annotate_info->text,text);
3993 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
3995 if (annotate_info->gravity == UndefinedGravity)
3996 (void) CloneString(&annotate_info->geometry,AcquireString(
3998 (void) AnnotateImage(caption_image,annotate_info,exception);
3999 height+=caption_image->rows;
4001 annotate_info=DestroyDrawInfo(annotate_info);
4002 text=DestroyString(text);
4004 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
4006 if (picture_image == (Image *) NULL)
4008 if (caption_image != (Image *) NULL)
4009 caption_image=DestroyImage(caption_image);
4010 return((Image *) NULL);
4012 picture_image->background_color=image->border_color;
4013 (void) SetImageBackgroundColor(picture_image,exception);
4014 (void) CompositeImage(picture_image,image,OverCompositeOp,MagickTrue,quantum,
4016 if (caption_image != (Image *) NULL)
4018 (void) CompositeImage(picture_image,caption_image,OverCompositeOp,
4019 MagickTrue,quantum,(ssize_t) (image->rows+3*quantum/2),exception);
4020 caption_image=DestroyImage(caption_image);
4022 (void) QueryColorCompliance("none",AllCompliance,
4023 &picture_image->background_color,exception);
4024 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
4025 rotate_image=RotateImage(picture_image,90.0,exception);
4026 picture_image=DestroyImage(picture_image);
4027 if (rotate_image == (Image *) NULL)
4028 return((Image *) NULL);
4029 picture_image=rotate_image;
4030 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
4031 picture_image->columns,method,exception);
4032 picture_image=DestroyImage(picture_image);
4033 if (bend_image == (Image *) NULL)
4034 return((Image *) NULL);
4035 picture_image=bend_image;
4036 rotate_image=RotateImage(picture_image,-90.0,exception);
4037 picture_image=DestroyImage(picture_image);
4038 if (rotate_image == (Image *) NULL)
4039 return((Image *) NULL);
4040 picture_image=rotate_image;
4041 picture_image->background_color=image->background_color;
4042 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
4044 if (polaroid_image == (Image *) NULL)
4046 picture_image=DestroyImage(picture_image);
4047 return(picture_image);
4049 flop_image=FlopImage(polaroid_image,exception);
4050 polaroid_image=DestroyImage(polaroid_image);
4051 if (flop_image == (Image *) NULL)
4053 picture_image=DestroyImage(picture_image);
4054 return(picture_image);
4056 polaroid_image=flop_image;
4057 (void) CompositeImage(polaroid_image,picture_image,OverCompositeOp,
4058 MagickTrue,(ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
4059 picture_image=DestroyImage(picture_image);
4060 (void) QueryColorCompliance("none",AllCompliance,
4061 &polaroid_image->background_color,exception);
4062 rotate_image=RotateImage(polaroid_image,angle,exception);
4063 polaroid_image=DestroyImage(polaroid_image);
4064 if (rotate_image == (Image *) NULL)
4065 return((Image *) NULL);
4066 polaroid_image=rotate_image;
4067 trim_image=TrimImage(polaroid_image,exception);
4068 polaroid_image=DestroyImage(polaroid_image);
4069 if (trim_image == (Image *) NULL)
4070 return((Image *) NULL);
4071 polaroid_image=trim_image;
4072 return(polaroid_image);
4076 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4080 % S e p i a T o n e I m a g e %
4084 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4086 % MagickSepiaToneImage() applies a special effect to the image, similar to the
4087 % effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4088 % 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4089 % threshold of 80% is a good starting point for a reasonable tone.
4091 % The format of the SepiaToneImage method is:
4093 % Image *SepiaToneImage(const Image *image,const double threshold,
4094 % ExceptionInfo *exception)
4096 % A description of each parameter follows:
4098 % o image: the image.
4100 % o threshold: the tone threshold.
4102 % o exception: return any errors or warnings in this structure.
4105 MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4106 ExceptionInfo *exception)
4108 #define SepiaToneImageTag "SepiaTone/Image"
4127 Initialize sepia-toned image attributes.
4129 assert(image != (const Image *) NULL);
4130 assert(image->signature == MagickSignature);
4131 if (image->debug != MagickFalse)
4132 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4133 assert(exception != (ExceptionInfo *) NULL);
4134 assert(exception->signature == MagickSignature);
4135 sepia_image=CloneImage(image,0,0,MagickTrue,exception);
4136 if (sepia_image == (Image *) NULL)
4137 return((Image *) NULL);
4138 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
4140 sepia_image=DestroyImage(sepia_image);
4141 return((Image *) NULL);
4144 Tone each row of the image.
4148 image_view=AcquireVirtualCacheView(image,exception);
4149 sepia_view=AcquireAuthenticCacheView(sepia_image,exception);
4150 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4151 #pragma omp parallel for schedule(static,4) shared(progress,status) \
4152 dynamic_number_threads(image,image->columns,image->rows,1)
4154 for (y=0; y < (ssize_t) image->rows; y++)
4156 register const Quantum
4165 if (status == MagickFalse)
4167 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
4168 q=GetCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
4170 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
4175 for (x=0; x < (ssize_t) image->columns; x++)
4181 intensity=GetPixelIntensity(image,p);
4182 tone=intensity > threshold ? (double) QuantumRange : intensity+
4183 (double) QuantumRange-threshold;
4184 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
4185 tone=intensity > (7.0*threshold/6.0) ? (double) QuantumRange :
4186 intensity+(double) QuantumRange-7.0*threshold/6.0;
4187 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4188 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
4189 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
4191 if ((double) GetPixelGreen(image,q) < tone)
4192 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4193 if ((double) GetPixelBlue(image,q) < tone)
4194 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
4195 p+=GetPixelChannels(image);
4196 q+=GetPixelChannels(sepia_image);
4198 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4200 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4205 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4206 #pragma omp critical (MagickCore_SepiaToneImage)
4208 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4210 if (proceed == MagickFalse)
4214 sepia_view=DestroyCacheView(sepia_view);
4215 image_view=DestroyCacheView(image_view);
4216 (void) NormalizeImage(sepia_image,exception);
4217 (void) ContrastImage(sepia_image,MagickTrue,exception);
4218 if (status == MagickFalse)
4219 sepia_image=DestroyImage(sepia_image);
4220 return(sepia_image);
4224 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4228 % S h a d o w I m a g e %
4232 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4234 % ShadowImage() simulates a shadow from the specified image and returns it.
4236 % The format of the ShadowImage method is:
4238 % Image *ShadowImage(const Image *image,const double alpha,
4239 % const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4240 % ExceptionInfo *exception)
4242 % A description of each parameter follows:
4244 % o image: the image.
4246 % o alpha: percentage transparency.
4248 % o sigma: the standard deviation of the Gaussian, in pixels.
4250 % o x_offset: the shadow x-offset.
4252 % o y_offset: the shadow y-offset.
4254 % o exception: return any errors or warnings in this structure.
4257 MagickExport Image *ShadowImage(const Image *image,const double alpha,
4258 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4259 ExceptionInfo *exception)
4261 #define ShadowImageTag "Shadow/Image"
4283 assert(image != (Image *) NULL);
4284 assert(image->signature == MagickSignature);
4285 if (image->debug != MagickFalse)
4286 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4287 assert(exception != (ExceptionInfo *) NULL);
4288 assert(exception->signature == MagickSignature);
4289 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4290 if (clone_image == (Image *) NULL)
4291 return((Image *) NULL);
4292 if (IsGrayColorspace(image->colorspace) != MagickFalse)
4293 (void) TransformImageColorspace(clone_image,RGBColorspace,exception);
4294 (void) SetImageVirtualPixelMethod(clone_image,TransparentVirtualPixelMethod,
4296 border_info.width=(size_t) floor(2.0*sigma+0.5);
4297 border_info.height=(size_t) floor(2.0*sigma+0.5);
4300 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4302 clone_image->matte=MagickTrue;
4303 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
4304 clone_image=DestroyImage(clone_image);
4305 if (border_image == (Image *) NULL)
4306 return((Image *) NULL);
4307 if (border_image->matte == MagickFalse)
4308 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
4313 image_view=AcquireAuthenticCacheView(border_image,exception);
4314 for (y=0; y < (ssize_t) border_image->rows; y++)
4325 if (status == MagickFalse)
4327 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
4329 if (q == (Quantum *) NULL)
4334 background_color=border_image->background_color;
4335 background_color.matte=MagickTrue;
4336 for (x=0; x < (ssize_t) border_image->columns; x++)
4338 if (border_image->matte != MagickFalse)
4339 background_color.alpha=GetPixelAlpha(border_image,q)*alpha/100.0;
4340 SetPixelInfoPixel(border_image,&background_color,q);
4341 q+=GetPixelChannels(border_image);
4343 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4346 image_view=DestroyCacheView(image_view);
4347 if (status == MagickFalse)
4349 border_image=DestroyImage(border_image);
4350 return((Image *) NULL);
4352 channel_mask=SetPixelChannelMask(border_image,AlphaChannel);
4353 shadow_image=BlurImage(border_image,0.0,sigma,exception);
4354 border_image=DestroyImage(border_image);
4355 if (shadow_image == (Image *) NULL)
4356 return((Image *) NULL);
4357 (void) SetPixelChannelMapMask(shadow_image,channel_mask);
4358 if (shadow_image->page.width == 0)
4359 shadow_image->page.width=shadow_image->columns;
4360 if (shadow_image->page.height == 0)
4361 shadow_image->page.height=shadow_image->rows;
4362 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4363 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4364 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4365 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
4366 return(shadow_image);
4370 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4374 % S k e t c h I m a g e %
4378 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4380 % SketchImage() simulates a pencil sketch. We convolve the image with a
4381 % Gaussian operator of the given radius and standard deviation (sigma). For
4382 % reasonable results, radius should be larger than sigma. Use a radius of 0
4383 % and SketchImage() selects a suitable radius for you. Angle gives the angle
4386 % The format of the SketchImage method is:
4388 % Image *SketchImage(const Image *image,const double radius,
4389 % const double sigma,const double angle,ExceptionInfo *exception)
4391 % A description of each parameter follows:
4393 % o image: the image.
4395 % o radius: the radius of the Gaussian, in pixels, not counting the
4398 % o sigma: the standard deviation of the Gaussian, in pixels.
4400 % o angle: apply the effect along this angle.
4402 % o exception: return any errors or warnings in this structure.
4405 MagickExport Image *SketchImage(const Image *image,const double radius,
4406 const double sigma,const double angle,ExceptionInfo *exception)
4422 **restrict random_info;
4433 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4434 MagickTrue,exception);
4435 if (random_image == (Image *) NULL)
4436 return((Image *) NULL);
4438 random_info=AcquireRandomInfoThreadSet();
4439 key=GetRandomSecretKey(random_info[0]);
4440 random_view=AcquireAuthenticCacheView(random_image,exception);
4441 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4442 #pragma omp parallel for schedule(static,4) shared(status) \
4443 dynamic_number_threads(image,image->columns,image->rows,key == ~0UL)
4445 for (y=0; y < (ssize_t) random_image->rows; y++)
4448 id = GetOpenMPThreadId();
4456 if (status == MagickFalse)
4458 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4460 if (q == (Quantum *) NULL)
4465 for (x=0; x < (ssize_t) random_image->columns; x++)
4473 if (GetPixelMask(random_image,q) != 0)
4475 q+=GetPixelChannels(random_image);
4478 value=GetPseudoRandomValue(random_info[id]);
4479 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4487 channel=GetPixelChannelMapChannel(image,i);
4488 traits=GetPixelChannelMapTraits(image,channel);
4489 if (traits == UndefinedPixelTrait)
4491 q[i]=ClampToQuantum(QuantumRange*value);
4493 q+=GetPixelChannels(random_image);
4495 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4498 random_view=DestroyCacheView(random_view);
4499 random_info=DestroyRandomInfoThreadSet(random_info);
4500 if (status == MagickFalse)
4502 random_image=DestroyImage(random_image);
4503 return(random_image);
4505 blur_image=MotionBlurImage(random_image,radius,sigma,angle,exception);
4506 random_image=DestroyImage(random_image);
4507 if (blur_image == (Image *) NULL)
4508 return((Image *) NULL);
4509 dodge_image=EdgeImage(blur_image,radius,1.0,exception);
4510 blur_image=DestroyImage(blur_image);
4511 if (dodge_image == (Image *) NULL)
4512 return((Image *) NULL);
4513 (void) NormalizeImage(dodge_image,exception);
4514 (void) NegateImage(dodge_image,MagickFalse,exception);
4515 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
4516 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4517 if (sketch_image == (Image *) NULL)
4519 dodge_image=DestroyImage(dodge_image);
4520 return((Image *) NULL);
4522 (void) CompositeImage(sketch_image,dodge_image,ColorDodgeCompositeOp,
4523 MagickTrue,0,0,exception);
4524 dodge_image=DestroyImage(dodge_image);
4525 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4526 if (blend_image == (Image *) NULL)
4528 sketch_image=DestroyImage(sketch_image);
4529 return((Image *) NULL);
4531 (void) SetImageArtifact(blend_image,"compose:args","20x80");
4532 (void) CompositeImage(sketch_image,blend_image,BlendCompositeOp,MagickTrue,
4534 blend_image=DestroyImage(blend_image);
4535 return(sketch_image);
4539 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4543 % S o l a r i z e I m a g e %
4547 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4549 % SolarizeImage() applies a special effect to the image, similar to the effect
4550 % achieved in a photo darkroom by selectively exposing areas of photo
4551 % sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4552 % measure of the extent of the solarization.
4554 % The format of the SolarizeImage method is:
4556 % MagickBooleanType SolarizeImage(Image *image,const double threshold,
4557 % ExceptionInfo *exception)
4559 % A description of each parameter follows:
4561 % o image: the image.
4563 % o threshold: Define the extent of the solarization.
4565 % o exception: return any errors or warnings in this structure.
4568 MagickExport MagickBooleanType SolarizeImage(Image *image,
4569 const double threshold,ExceptionInfo *exception)
4571 #define SolarizeImageTag "Solarize/Image"
4585 assert(image != (Image *) NULL);
4586 assert(image->signature == MagickSignature);
4587 if (image->debug != MagickFalse)
4588 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4589 if (image->storage_class == PseudoClass)
4597 for (i=0; i < (ssize_t) image->colors; i++)
4599 if ((double) image->colormap[i].red > threshold)
4600 image->colormap[i].red=QuantumRange-image->colormap[i].red;
4601 if ((double) image->colormap[i].green > threshold)
4602 image->colormap[i].green=QuantumRange-
4603 image->colormap[i].green;
4604 if ((double) image->colormap[i].blue > threshold)
4605 image->colormap[i].blue=QuantumRange-
4606 image->colormap[i].blue;
4614 image_view=AcquireAuthenticCacheView(image,exception);
4615 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4616 #pragma omp parallel for schedule(static,4) shared(progress,status) \
4617 dynamic_number_threads(image,image->columns,image->rows,1)
4619 for (y=0; y < (ssize_t) image->rows; y++)
4627 if (status == MagickFalse)
4629 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
4630 if (q == (Quantum *) NULL)
4635 for (x=0; x < (ssize_t) image->columns; x++)
4640 if (GetPixelMask(image,q) != 0)
4642 q+=GetPixelChannels(image);
4645 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4653 channel=GetPixelChannelMapChannel(image,i);
4654 traits=GetPixelChannelMapTraits(image,channel);
4655 if ((traits == UndefinedPixelTrait) ||
4656 ((traits & CopyPixelTrait) != 0))
4658 if ((double) q[i] > threshold)
4659 q[i]=QuantumRange-q[i];
4661 q+=GetPixelChannels(image);
4663 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4665 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4670 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4671 #pragma omp critical (MagickCore_SolarizeImage)
4673 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4674 if (proceed == MagickFalse)
4678 image_view=DestroyCacheView(image_view);
4683 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4687 % S t e g a n o I m a g e %
4691 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4693 % SteganoImage() hides a digital watermark within the image. Recover
4694 % the hidden watermark later to prove that the authenticity of an image.
4695 % Offset defines the start position within the image to hide the watermark.
4697 % The format of the SteganoImage method is:
4699 % Image *SteganoImage(const Image *image,Image *watermark,
4700 % ExceptionInfo *exception)
4702 % A description of each parameter follows:
4704 % o image: the image.
4706 % o watermark: the watermark image.
4708 % o exception: return any errors or warnings in this structure.
4711 MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4712 ExceptionInfo *exception)
4714 #define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
4715 #define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
4716 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
4717 #define SteganoImageTag "Stegano/Image"
4752 Initialize steganographic image attributes.
4754 assert(image != (const Image *) NULL);
4755 assert(image->signature == MagickSignature);
4756 if (image->debug != MagickFalse)
4757 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4758 assert(watermark != (const Image *) NULL);
4759 assert(watermark->signature == MagickSignature);
4760 assert(exception != (ExceptionInfo *) NULL);
4761 assert(exception->signature == MagickSignature);
4763 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4764 if (stegano_image == (Image *) NULL)
4765 return((Image *) NULL);
4766 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
4767 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
4769 stegano_image=DestroyImage(stegano_image);
4770 return((Image *) NULL);
4773 Hide watermark in low-order bits of image.
4778 depth=stegano_image->depth;
4779 k=stegano_image->offset;
4781 watermark_view=AcquireVirtualCacheView(watermark,exception);
4782 stegano_view=AcquireAuthenticCacheView(stegano_image,exception);
4783 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
4785 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
4787 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
4792 (void) GetOneCacheViewVirtualPixelInfo(watermark_view,x,y,&pixel,
4794 offset=k/(ssize_t) stegano_image->columns;
4795 if (offset >= (ssize_t) stegano_image->rows)
4797 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4798 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4800 if (q == (Quantum *) NULL)
4806 SetPixelRed(stegano_image,SetBit(GetPixelRed(stegano_image,q),j,
4807 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
4812 SetPixelGreen(stegano_image,SetBit(GetPixelGreen(stegano_image,q),j,
4813 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
4818 SetPixelBlue(stegano_image,SetBit(GetPixelBlue(stegano_image,q),j,
4819 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
4823 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
4829 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
4831 if (k == stegano_image->offset)
4835 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4840 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4842 if (proceed == MagickFalse)
4846 stegano_view=DestroyCacheView(stegano_view);
4847 watermark_view=DestroyCacheView(watermark_view);
4848 if (status == MagickFalse)
4850 stegano_image=DestroyImage(stegano_image);
4851 return((Image *) NULL);
4853 return(stegano_image);
4857 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4861 % S t e r e o A n a g l y p h I m a g e %
4865 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4867 % StereoAnaglyphImage() combines two images and produces a single image that
4868 % is the composite of a left and right image of a stereo pair. Special
4869 % red-green stereo glasses are required to view this effect.
4871 % The format of the StereoAnaglyphImage method is:
4873 % Image *StereoImage(const Image *left_image,const Image *right_image,
4874 % ExceptionInfo *exception)
4875 % Image *StereoAnaglyphImage(const Image *left_image,
4876 % const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
4877 % ExceptionInfo *exception)
4879 % A description of each parameter follows:
4881 % o left_image: the left image.
4883 % o right_image: the right image.
4885 % o exception: return any errors or warnings in this structure.
4887 % o x_offset: amount, in pixels, by which the left image is offset to the
4888 % right of the right image.
4890 % o y_offset: amount, in pixels, by which the left image is offset to the
4891 % bottom of the right image.
4895 MagickExport Image *StereoImage(const Image *left_image,
4896 const Image *right_image,ExceptionInfo *exception)
4898 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4901 MagickExport Image *StereoAnaglyphImage(const Image *left_image,
4902 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
4903 ExceptionInfo *exception)
4905 #define StereoImageTag "Stereo/Image"
4919 assert(left_image != (const Image *) NULL);
4920 assert(left_image->signature == MagickSignature);
4921 if (left_image->debug != MagickFalse)
4922 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4923 left_image->filename);
4924 assert(right_image != (const Image *) NULL);
4925 assert(right_image->signature == MagickSignature);
4926 assert(exception != (ExceptionInfo *) NULL);
4927 assert(exception->signature == MagickSignature);
4928 assert(right_image != (const Image *) NULL);
4930 if ((left_image->columns != right_image->columns) ||
4931 (left_image->rows != right_image->rows))
4932 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4934 Initialize stereo image attributes.
4936 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4937 MagickTrue,exception);
4938 if (stereo_image == (Image *) NULL)
4939 return((Image *) NULL);
4940 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
4942 stereo_image=DestroyImage(stereo_image);
4943 return((Image *) NULL);
4945 (void) SetImageColorspace(stereo_image,sRGBColorspace,exception);
4947 Copy left image to red channel and right image to blue channel.
4950 for (y=0; y < (ssize_t) stereo_image->rows; y++)
4952 register const Quantum
4962 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4964 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4965 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
4966 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4967 (r == (Quantum *) NULL))
4969 for (x=0; x < (ssize_t) stereo_image->columns; x++)
4971 SetPixelRed(image,GetPixelRed(left_image,p),r);
4972 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4973 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4974 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4975 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4976 GetPixelAlpha(right_image,q))/2,r);
4977 p+=GetPixelChannels(left_image);
4978 q+=GetPixelChannels(right_image);
4979 r+=GetPixelChannels(stereo_image);
4981 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4983 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4988 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4989 stereo_image->rows);
4990 if (proceed == MagickFalse)
4994 if (status == MagickFalse)
4996 stereo_image=DestroyImage(stereo_image);
4997 return((Image *) NULL);
4999 return(stereo_image);
5003 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5007 % S w i r l I m a g e %
5011 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5013 % SwirlImage() swirls the pixels about the center of the image, where
5014 % degrees indicates the sweep of the arc through which each pixel is moved.
5015 % You get a more dramatic effect as the degrees move from 1 to 360.
5017 % The format of the SwirlImage method is:
5019 % Image *SwirlImage(const Image *image,double degrees,
5020 % const PixelInterpolateMethod method,ExceptionInfo *exception)
5022 % A description of each parameter follows:
5024 % o image: the image.
5026 % o degrees: Define the tightness of the swirling effect.
5028 % o method: the pixel interpolation method.
5030 % o exception: return any errors or warnings in this structure.
5033 MagickExport Image *SwirlImage(const Image *image,double degrees,
5034 const PixelInterpolateMethod method,ExceptionInfo *exception)
5036 #define SwirlImageTag "Swirl/Image"
5062 Initialize swirl image attributes.
5064 assert(image != (const Image *) NULL);
5065 assert(image->signature == MagickSignature);
5066 if (image->debug != MagickFalse)
5067 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5068 assert(exception != (ExceptionInfo *) NULL);
5069 assert(exception->signature == MagickSignature);
5070 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5071 if (swirl_image == (Image *) NULL)
5072 return((Image *) NULL);
5073 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
5075 swirl_image=DestroyImage(swirl_image);
5076 return((Image *) NULL);
5078 if (swirl_image->background_color.alpha != OpaqueAlpha)
5079 swirl_image->matte=MagickTrue;
5081 Compute scaling factor.
5083 center.x=(double) image->columns/2.0;
5084 center.y=(double) image->rows/2.0;
5085 radius=MagickMax(center.x,center.y);
5088 if (image->columns > image->rows)
5089 scale.y=(double) image->columns/(double) image->rows;
5091 if (image->columns < image->rows)
5092 scale.x=(double) image->rows/(double) image->columns;
5093 degrees=(double) DegreesToRadians(degrees);
5099 image_view=AcquireVirtualCacheView(image,exception);
5100 swirl_view=AcquireAuthenticCacheView(swirl_image,exception);
5101 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5102 #pragma omp parallel for schedule(static,4) shared(progress,status) \
5103 dynamic_number_threads(image,image->columns,image->rows,1)
5105 for (y=0; y < (ssize_t) image->rows; y++)
5113 register const Quantum
5122 if (status == MagickFalse)
5124 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
5125 q=QueueCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
5127 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
5132 delta.y=scale.y*(double) (y-center.y);
5133 for (x=0; x < (ssize_t) image->columns; x++)
5136 Determine if the pixel is within an ellipse.
5138 if (GetPixelMask(image,p) != 0)
5140 p+=GetPixelChannels(image);
5141 q+=GetPixelChannels(swirl_image);
5144 delta.x=scale.x*(double) (x-center.x);
5145 distance=delta.x*delta.x+delta.y*delta.y;
5146 if (distance >= (radius*radius))
5151 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5160 channel=GetPixelChannelMapChannel(image,i);
5161 traits=GetPixelChannelMapTraits(image,channel);
5162 swirl_traits=GetPixelChannelMapTraits(swirl_image,channel);
5163 if ((traits == UndefinedPixelTrait) ||
5164 (swirl_traits == UndefinedPixelTrait))
5166 SetPixelChannel(swirl_image,channel,p[i],q);
5179 factor=1.0-sqrt((double) distance)/radius;
5180 sine=sin((double) (degrees*factor*factor));
5181 cosine=cos((double) (degrees*factor*factor));
5182 status=InterpolatePixelChannels(image,image_view,swirl_image,method,
5183 ((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5184 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
5186 p+=GetPixelChannels(image);
5187 q+=GetPixelChannels(swirl_image);
5189 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5191 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5196 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5197 #pragma omp critical (MagickCore_SwirlImage)
5199 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5200 if (proceed == MagickFalse)
5204 swirl_view=DestroyCacheView(swirl_view);
5205 image_view=DestroyCacheView(image_view);
5206 if (status == MagickFalse)
5207 swirl_image=DestroyImage(swirl_image);
5208 return(swirl_image);
5212 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5216 % T i n t I m a g e %
5220 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5222 % TintImage() applies a color vector to each pixel in the image. The length
5223 % of the vector is 0 for black and white and at its maximum for the midtones.
5224 % The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5226 % The format of the TintImage method is:
5228 % Image *TintImage(const Image *image,const char *blend,
5229 % const PixelInfo *tint,ExceptionInfo *exception)
5231 % A description of each parameter follows:
5233 % o image: the image.
5235 % o blend: A color value used for tinting.
5237 % o tint: A color value used for tinting.
5239 % o exception: return any errors or warnings in this structure.
5242 MagickExport Image *TintImage(const Image *image,const char *blend,
5243 const PixelInfo *tint,ExceptionInfo *exception)
5245 #define TintImageTag "Tint/Image"
5276 Allocate tint image.
5278 assert(image != (const Image *) NULL);
5279 assert(image->signature == MagickSignature);
5280 if (image->debug != MagickFalse)
5281 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5282 assert(exception != (ExceptionInfo *) NULL);
5283 assert(exception->signature == MagickSignature);
5284 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5285 if (tint_image == (Image *) NULL)
5286 return((Image *) NULL);
5287 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
5289 tint_image=DestroyImage(tint_image);
5290 return((Image *) NULL);
5292 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
5293 (IsPixelInfoGray(tint) == MagickFalse))
5294 (void) SetImageColorspace(tint_image,RGBColorspace,exception);
5295 if (blend == (const char *) NULL)
5298 Determine RGB values of the color.
5300 GetPixelInfo(image,&color_vector);
5301 flags=ParseGeometry(blend,&geometry_info);
5302 color_vector.red=geometry_info.rho;
5303 color_vector.green=geometry_info.rho;
5304 color_vector.blue=geometry_info.rho;
5305 color_vector.alpha=OpaqueAlpha;
5306 if ((flags & SigmaValue) != 0)
5307 color_vector.green=geometry_info.sigma;
5308 if ((flags & XiValue) != 0)
5309 color_vector.blue=geometry_info.xi;
5310 if ((flags & PsiValue) != 0)
5311 color_vector.alpha=geometry_info.psi;
5312 if (image->colorspace == CMYKColorspace)
5314 color_vector.black=geometry_info.rho;
5315 if ((flags & PsiValue) != 0)
5316 color_vector.black=geometry_info.psi;
5317 if ((flags & ChiValue) != 0)
5318 color_vector.alpha=geometry_info.chi;
5320 intensity=(double) GetPixelInfoIntensity(tint);
5321 color_vector.red=(double) (color_vector.red*tint->red/100.0-
5323 color_vector.green=(double) (color_vector.green*tint->green/100.0-
5325 color_vector.blue=(double) (color_vector.blue*tint->blue/100.0-
5327 color_vector.black=(double) (color_vector.black*tint->black/100.0-
5329 color_vector.alpha=(double) (color_vector.alpha*tint->alpha/100.0-
5336 image_view=AcquireVirtualCacheView(image,exception);
5337 tint_view=AcquireAuthenticCacheView(tint_image,exception);
5338 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5339 #pragma omp parallel for schedule(static,4) shared(progress,status) \
5340 dynamic_number_threads(image,image->columns,image->rows,1)
5342 for (y=0; y < (ssize_t) image->rows; y++)
5344 register const Quantum
5353 if (status == MagickFalse)
5355 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5356 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5358 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
5363 for (x=0; x < (ssize_t) image->columns; x++)
5374 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5383 channel=GetPixelChannelMapChannel(image,i);
5384 traits=GetPixelChannelMapTraits(image,channel);
5385 tint_traits=GetPixelChannelMapTraits(tint_image,channel);
5386 if ((traits == UndefinedPixelTrait) ||
5387 (tint_traits == UndefinedPixelTrait))
5389 if (((tint_traits & CopyPixelTrait) != 0) ||
5390 (GetPixelMask(image,p) != 0))
5392 SetPixelChannel(tint_image,channel,p[i],q);
5396 GetPixelInfo(image,&pixel);
5397 weight=QuantumScale*GetPixelRed(image,p)-0.5;
5398 pixel.red=(double) GetPixelRed(image,p)+color_vector.red*(1.0-(4.0*
5400 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
5401 pixel.green=(double) GetPixelGreen(image,p)+color_vector.green*(1.0-(4.0*
5403 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
5404 pixel.blue=(double) GetPixelBlue(image,p)+color_vector.blue*(1.0-(4.0*
5406 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
5407 pixel.black=(double) GetPixelBlack(image,p)+color_vector.black*(1.0-(4.0*
5409 SetPixelInfoPixel(tint_image,&pixel,q);
5410 p+=GetPixelChannels(image);
5411 q+=GetPixelChannels(tint_image);
5413 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5415 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5420 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5421 #pragma omp critical (MagickCore_TintImage)
5423 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5424 if (proceed == MagickFalse)
5428 tint_view=DestroyCacheView(tint_view);
5429 image_view=DestroyCacheView(image_view);
5430 if (status == MagickFalse)
5431 tint_image=DestroyImage(tint_image);
5436 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5440 % V i g n e t t e I m a g e %
5444 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5446 % VignetteImage() softens the edges of the image in vignette style.
5448 % The format of the VignetteImage method is:
5450 % Image *VignetteImage(const Image *image,const double radius,
5451 % const double sigma,const ssize_t x,const ssize_t y,
5452 % ExceptionInfo *exception)
5454 % A description of each parameter follows:
5456 % o image: the image.
5458 % o radius: the radius of the pixel neighborhood.
5460 % o sigma: the standard deviation of the Gaussian, in pixels.
5462 % o x, y: Define the x and y ellipse offset.
5464 % o exception: return any errors or warnings in this structure.
5467 MagickExport Image *VignetteImage(const Image *image,const double radius,
5468 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
5471 ellipse[MaxTextExtent];
5482 assert(image != (Image *) NULL);
5483 assert(image->signature == MagickSignature);
5484 if (image->debug != MagickFalse)
5485 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5486 assert(exception != (ExceptionInfo *) NULL);
5487 assert(exception->signature == MagickSignature);
5488 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5489 if (canvas_image == (Image *) NULL)
5490 return((Image *) NULL);
5491 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
5493 canvas_image=DestroyImage(canvas_image);
5494 return((Image *) NULL);
5496 canvas_image->matte=MagickTrue;
5497 oval_image=CloneImage(canvas_image,canvas_image->columns,canvas_image->rows,
5498 MagickTrue,exception);
5499 if (oval_image == (Image *) NULL)
5501 canvas_image=DestroyImage(canvas_image);
5502 return((Image *) NULL);
5504 (void) QueryColorCompliance("#000000",AllCompliance,
5505 &oval_image->background_color,exception);
5506 (void) SetImageBackgroundColor(oval_image,exception);
5507 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
5508 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5510 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5512 (void) FormatLocaleString(ellipse,MaxTextExtent,"ellipse %g,%g,%g,%g,"
5513 "0.0,360.0",image->columns/2.0,image->rows/2.0,image->columns/2.0-x,
5515 draw_info->primitive=AcquireString(ellipse);
5516 (void) DrawImage(oval_image,draw_info,exception);
5517 draw_info=DestroyDrawInfo(draw_info);
5518 blur_image=BlurImage(oval_image,radius,sigma,exception);
5519 oval_image=DestroyImage(oval_image);
5520 if (blur_image == (Image *) NULL)
5522 canvas_image=DestroyImage(canvas_image);
5523 return((Image *) NULL);
5525 blur_image->matte=MagickFalse;
5526 (void) CompositeImage(canvas_image,blur_image,IntensityCompositeOp,MagickTrue,
5528 blur_image=DestroyImage(blur_image);
5529 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5530 canvas_image=DestroyImage(canvas_image);
5531 if (vignette_image != (Image *) NULL)
5532 (void) TransformImageColorspace(vignette_image,image->colorspace,exception);
5533 return(vignette_image);
5537 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5541 % W a v e I m a g e %
5545 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5547 % WaveImage() creates a "ripple" effect in the image by shifting the pixels
5548 % vertically along a sine wave whose amplitude and wavelength is specified
5549 % by the given parameters.
5551 % The format of the WaveImage method is:
5553 % Image *WaveImage(const Image *image,const double amplitude,
5554 % const double wave_length,const PixelInterpolateMethod method,
5555 % ExceptionInfo *exception)
5557 % A description of each parameter follows:
5559 % o image: the image.
5561 % o amplitude, wave_length: Define the amplitude and wave length of the
5564 % o interpolate: the pixel interpolation method.
5566 % o exception: return any errors or warnings in this structure.
5569 MagickExport Image *WaveImage(const Image *image,const double amplitude,
5570 const double wave_length,const PixelInterpolateMethod method,
5571 ExceptionInfo *exception)
5573 #define WaveImageTag "Wave/Image"
5598 Initialize wave image attributes.
5600 assert(image != (Image *) NULL);
5601 assert(image->signature == MagickSignature);
5602 if (image->debug != MagickFalse)
5603 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5604 assert(exception != (ExceptionInfo *) NULL);
5605 assert(exception->signature == MagickSignature);
5606 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
5607 fabs(amplitude)),MagickTrue,exception);
5608 if (wave_image == (Image *) NULL)
5609 return((Image *) NULL);
5610 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
5612 wave_image=DestroyImage(wave_image);
5613 return((Image *) NULL);
5615 if (wave_image->background_color.alpha != OpaqueAlpha)
5616 wave_image->matte=MagickTrue;
5620 sine_map=(double *) AcquireQuantumMemory((size_t) wave_image->columns,
5622 if (sine_map == (double *) NULL)
5624 wave_image=DestroyImage(wave_image);
5625 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5627 for (i=0; i < (ssize_t) wave_image->columns; i++)
5628 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5635 image_view=AcquireVirtualCacheView(image,exception);
5636 wave_view=AcquireAuthenticCacheView(wave_image,exception);
5637 (void) SetCacheViewVirtualPixelMethod(image_view,
5638 BackgroundVirtualPixelMethod);
5639 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5640 #pragma omp parallel for schedule(static,4) shared(progress,status) \
5641 dynamic_number_threads(image,image->columns,image->rows,1)
5643 for (y=0; y < (ssize_t) wave_image->rows; y++)
5651 if (status == MagickFalse)
5653 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5655 if (q == (Quantum *) NULL)
5660 for (x=0; x < (ssize_t) wave_image->columns; x++)
5662 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5663 (double) x,(double) (y-sine_map[x]),q,exception);
5664 q+=GetPixelChannels(wave_image);
5666 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5668 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5673 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5674 #pragma omp critical (MagickCore_WaveImage)
5676 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5677 if (proceed == MagickFalse)
5681 wave_view=DestroyCacheView(wave_view);
5682 image_view=DestroyCacheView(image_view);
5683 sine_map=(double *) RelinquishMagickMemory(sine_map);
5684 if (status == MagickFalse)
5685 wave_image=DestroyImage(wave_image);