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*");
204 Convert complex to simple operators.
207 *fx_op=(char) LeftShiftOperator;
208 (void) SubstituteString(&fx_info->expression,"<<",fx_op);
209 *fx_op=(char) RightShiftOperator;
210 (void) SubstituteString(&fx_info->expression,">>",fx_op);
211 *fx_op=(char) LessThanEqualOperator;
212 (void) SubstituteString(&fx_info->expression,"<=",fx_op);
213 *fx_op=(char) GreaterThanEqualOperator;
214 (void) SubstituteString(&fx_info->expression,">=",fx_op);
215 *fx_op=(char) EqualOperator;
216 (void) SubstituteString(&fx_info->expression,"==",fx_op);
217 *fx_op=(char) NotEqualOperator;
218 (void) SubstituteString(&fx_info->expression,"!=",fx_op);
219 *fx_op=(char) LogicalAndOperator;
220 (void) SubstituteString(&fx_info->expression,"&&",fx_op);
221 *fx_op=(char) LogicalOrOperator;
222 (void) SubstituteString(&fx_info->expression,"||",fx_op);
223 *fx_op=(char) ExponentialNotation;
224 (void) SubstituteString(&fx_info->expression,"**",fx_op);
229 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233 % A d d N o i s e I m a g e %
237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
239 % AddNoiseImage() adds random noise to the image.
241 % The format of the AddNoiseImage method is:
243 % Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
244 % const double attenuate,ExceptionInfo *exception)
246 % A description of each parameter follows:
248 % o image: the image.
250 % o channel: the channel type.
252 % o noise_type: The type of noise: Uniform, Gaussian, Multiplicative,
253 % Impulse, Laplacian, or Poisson.
255 % o attenuate: attenuate the random distribution.
257 % o exception: return any errors or warnings in this structure.
260 MagickExport Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
261 const double attenuate,ExceptionInfo *exception)
263 #define AddNoiseImageTag "AddNoise/Image"
279 **restrict random_info;
288 Initialize noise image attributes.
290 assert(image != (const Image *) NULL);
291 assert(image->signature == MagickSignature);
292 if (image->debug != MagickFalse)
293 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
294 assert(exception != (ExceptionInfo *) NULL);
295 assert(exception->signature == MagickSignature);
296 noise_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
297 if (noise_image == (Image *) NULL)
298 return((Image *) NULL);
299 if (SetImageStorageClass(noise_image,DirectClass,exception) == MagickFalse)
301 noise_image=DestroyImage(noise_image);
302 return((Image *) NULL);
304 if (IsGrayColorspace(image->colorspace) != MagickFalse)
305 (void) TransformImageColorspace(noise_image,RGBColorspace,exception);
307 Add noise in each row.
311 random_info=AcquireRandomInfoThreadSet();
312 image_view=AcquireVirtualCacheView(image,exception);
313 noise_view=AcquireAuthenticCacheView(noise_image,exception);
314 key=GetRandomSecretKey(random_info[0]);
315 #if defined(MAGICKCORE_OPENMP_SUPPORT)
316 #pragma omp parallel for schedule(static,4) shared(progress,status) \
317 dynamic_number_threads(image,image->columns,image->rows,key == ~0UL)
319 for (y=0; y < (ssize_t) image->rows; y++)
322 id = GetOpenMPThreadId();
327 register const Quantum
336 if (status == MagickFalse)
338 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
339 q=QueueCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
341 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
346 for (x=0; x < (ssize_t) image->columns; x++)
351 if (GetPixelMask(image,p) != 0)
353 p+=GetPixelChannels(image);
354 q+=GetPixelChannels(noise_image);
357 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
366 channel=GetPixelChannelMapChannel(image,i);
367 traits=GetPixelChannelMapTraits(image,channel);
368 noise_traits=GetPixelChannelMapTraits(noise_image,channel);
369 if ((traits == UndefinedPixelTrait) ||
370 (noise_traits == UndefinedPixelTrait))
372 if ((noise_traits & CopyPixelTrait) != 0)
374 SetPixelChannel(noise_image,channel,p[i],q);
377 SetPixelChannel(noise_image,channel,ClampToQuantum(
378 GenerateDifferentialNoise(random_info[id],p[i],noise_type,attenuate)),
381 p+=GetPixelChannels(image);
382 q+=GetPixelChannels(noise_image);
384 sync=SyncCacheViewAuthenticPixels(noise_view,exception);
385 if (sync == MagickFalse)
387 if (image->progress_monitor != (MagickProgressMonitor) NULL)
392 #if defined(MAGICKCORE_OPENMP_SUPPORT)
393 #pragma omp critical (MagickCore_AddNoiseImage)
395 proceed=SetImageProgress(image,AddNoiseImageTag,progress++,
397 if (proceed == MagickFalse)
401 noise_view=DestroyCacheView(noise_view);
402 image_view=DestroyCacheView(image_view);
403 random_info=DestroyRandomInfoThreadSet(random_info);
404 if (status == MagickFalse)
405 noise_image=DestroyImage(noise_image);
410 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
414 % B l u e S h i f t I m a g e %
418 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
420 % BlueShiftImage() mutes the colors of the image to simulate a scene at
421 % nighttime in the moonlight.
423 % The format of the BlueShiftImage method is:
425 % Image *BlueShiftImage(const Image *image,const double factor,
426 % ExceptionInfo *exception)
428 % A description of each parameter follows:
430 % o image: the image.
432 % o factor: the shift factor.
434 % o exception: return any errors or warnings in this structure.
437 MagickExport Image *BlueShiftImage(const Image *image,const double factor,
438 ExceptionInfo *exception)
440 #define BlueShiftImageTag "BlueShift/Image"
459 Allocate blue shift image.
461 assert(image != (const Image *) NULL);
462 assert(image->signature == MagickSignature);
463 if (image->debug != MagickFalse)
464 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
465 assert(exception != (ExceptionInfo *) NULL);
466 assert(exception->signature == MagickSignature);
467 shift_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
468 if (shift_image == (Image *) NULL)
469 return((Image *) NULL);
470 if (SetImageStorageClass(shift_image,DirectClass,exception) == MagickFalse)
472 shift_image=DestroyImage(shift_image);
473 return((Image *) NULL);
476 Blue-shift DirectClass image.
480 image_view=AcquireVirtualCacheView(image,exception);
481 shift_view=AcquireAuthenticCacheView(shift_image,exception);
482 #if defined(MAGICKCORE_OPENMP_SUPPORT)
483 #pragma omp parallel for schedule(static,4) shared(progress,status) \
484 dynamic_number_threads(image,image->columns,image->rows,1)
486 for (y=0; y < (ssize_t) image->rows; y++)
497 register const Quantum
506 if (status == MagickFalse)
508 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
509 q=QueueCacheViewAuthenticPixels(shift_view,0,y,shift_image->columns,1,
511 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
516 for (x=0; x < (ssize_t) image->columns; x++)
518 quantum=GetPixelRed(image,p);
519 if (GetPixelGreen(image,p) < quantum)
520 quantum=GetPixelGreen(image,p);
521 if (GetPixelBlue(image,p) < quantum)
522 quantum=GetPixelBlue(image,p);
523 pixel.red=0.5*(GetPixelRed(image,p)+factor*quantum);
524 pixel.green=0.5*(GetPixelGreen(image,p)+factor*quantum);
525 pixel.blue=0.5*(GetPixelBlue(image,p)+factor*quantum);
526 quantum=GetPixelRed(image,p);
527 if (GetPixelGreen(image,p) > quantum)
528 quantum=GetPixelGreen(image,p);
529 if (GetPixelBlue(image,p) > quantum)
530 quantum=GetPixelBlue(image,p);
531 pixel.red=0.5*(pixel.red+factor*quantum);
532 pixel.green=0.5*(pixel.green+factor*quantum);
533 pixel.blue=0.5*(pixel.blue+factor*quantum);
534 SetPixelRed(shift_image,ClampToQuantum(pixel.red),q);
535 SetPixelGreen(shift_image,ClampToQuantum(pixel.green),q);
536 SetPixelBlue(shift_image,ClampToQuantum(pixel.blue),q);
537 p+=GetPixelChannels(image);
538 q+=GetPixelChannels(shift_image);
540 sync=SyncCacheViewAuthenticPixels(shift_view,exception);
541 if (sync == MagickFalse)
543 if (image->progress_monitor != (MagickProgressMonitor) NULL)
548 #if defined(MAGICKCORE_OPENMP_SUPPORT)
549 #pragma omp critical (MagickCore_BlueShiftImage)
551 proceed=SetImageProgress(image,BlueShiftImageTag,progress++,
553 if (proceed == MagickFalse)
557 image_view=DestroyCacheView(image_view);
558 shift_view=DestroyCacheView(shift_view);
559 if (status == MagickFalse)
560 shift_image=DestroyImage(shift_image);
565 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
569 % C h a r c o a l I m a g e %
573 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
575 % CharcoalImage() creates a new image that is a copy of an existing one with
576 % the edge highlighted. It allocates the memory necessary for the new Image
577 % structure and returns a pointer to the new image.
579 % The format of the CharcoalImage method is:
581 % Image *CharcoalImage(const Image *image,const double radius,
582 % const double sigma,ExceptionInfo *exception)
584 % A description of each parameter follows:
586 % o image: the image.
588 % o radius: the radius of the pixel neighborhood.
590 % o sigma: the standard deviation of the Gaussian, in pixels.
592 % o exception: return any errors or warnings in this structure.
595 MagickExport Image *CharcoalImage(const Image *image,const double radius,
596 const double sigma,ExceptionInfo *exception)
603 assert(image != (Image *) NULL);
604 assert(image->signature == MagickSignature);
605 if (image->debug != MagickFalse)
606 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
607 assert(exception != (ExceptionInfo *) NULL);
608 assert(exception->signature == MagickSignature);
609 clone_image=CloneImage(image,0,0,MagickTrue,exception);
610 if (clone_image == (Image *) NULL)
611 return((Image *) NULL);
612 (void) SetImageType(clone_image,GrayscaleType,exception);
613 edge_image=EdgeImage(clone_image,radius,sigma,exception);
614 clone_image=DestroyImage(clone_image);
615 if (edge_image == (Image *) NULL)
616 return((Image *) NULL);
617 charcoal_image=BlurImage(edge_image,radius,sigma,exception);
618 edge_image=DestroyImage(edge_image);
619 if (charcoal_image == (Image *) NULL)
620 return((Image *) NULL);
621 (void) NormalizeImage(charcoal_image,exception);
622 (void) NegateImage(charcoal_image,MagickFalse,exception);
623 (void) SetImageType(charcoal_image,GrayscaleType,exception);
624 return(charcoal_image);
628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
632 % C o l o r i z e I m a g e %
636 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
638 % ColorizeImage() blends the fill color with each pixel in the image.
639 % A percentage blend is specified with opacity. Control the application
640 % of different color components by specifying a different percentage for
641 % each component (e.g. 90/100/10 is 90% red, 100% green, and 10% blue).
643 % The format of the ColorizeImage method is:
645 % Image *ColorizeImage(const Image *image,const char *blend,
646 % const PixelInfo *colorize,ExceptionInfo *exception)
648 % A description of each parameter follows:
650 % o image: the image.
652 % o blend: A character string indicating the level of blending as a
655 % o colorize: A color value.
657 % o exception: return any errors or warnings in this structure.
660 MagickExport Image *ColorizeImage(const Image *image,const char *blend,
661 const PixelInfo *colorize,ExceptionInfo *exception)
663 #define ColorizeImageTag "Colorize/Image"
664 #define Colorize(pixel,blend_percentage,colorize) \
665 (pixel)=((pixel)*(100.0-(blend_percentage))+(colorize)*(blend_percentage))/100.0;
693 Allocate colorized image.
695 assert(image != (const Image *) NULL);
696 assert(image->signature == MagickSignature);
697 if (image->debug != MagickFalse)
698 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
699 assert(exception != (ExceptionInfo *) NULL);
700 assert(exception->signature == MagickSignature);
701 colorize_image=CloneImage(image,image->columns,image->rows,MagickTrue,
703 if (colorize_image == (Image *) NULL)
704 return((Image *) NULL);
705 if (SetImageStorageClass(colorize_image,DirectClass,exception) == MagickFalse)
707 colorize_image=DestroyImage(colorize_image);
708 return((Image *) NULL);
710 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
711 (IsPixelInfoGray(colorize) != MagickFalse))
712 (void) SetImageColorspace(colorize_image,RGBColorspace,exception);
713 if ((colorize_image->matte == MagickFalse) &&
714 (colorize->matte != MagickFalse))
715 (void) SetImageAlpha(colorize_image,OpaqueAlpha,exception);
716 if (blend == (const char *) NULL)
717 return(colorize_image);
718 GetPixelInfo(image,&blend_percentage);
719 flags=ParseGeometry(blend,&geometry_info);
720 blend_percentage.red=geometry_info.rho;
721 blend_percentage.green=geometry_info.rho;
722 blend_percentage.blue=geometry_info.rho;
723 blend_percentage.black=geometry_info.rho;
724 blend_percentage.alpha=geometry_info.rho;
725 if ((flags & SigmaValue) != 0)
726 blend_percentage.green=geometry_info.sigma;
727 if ((flags & XiValue) != 0)
728 blend_percentage.blue=geometry_info.xi;
729 if ((flags & PsiValue) != 0)
730 blend_percentage.alpha=geometry_info.psi;
731 if (blend_percentage.colorspace == CMYKColorspace)
733 if ((flags & PsiValue) != 0)
734 blend_percentage.black=geometry_info.psi;
735 if ((flags & ChiValue) != 0)
736 blend_percentage.alpha=geometry_info.chi;
739 Colorize DirectClass image.
743 image_view=AcquireVirtualCacheView(image,exception);
744 colorize_view=AcquireAuthenticCacheView(colorize_image,exception);
745 #if defined(MAGICKCORE_OPENMP_SUPPORT)
746 #pragma omp parallel for schedule(static,4) shared(progress,status) \
747 dynamic_number_threads(image,image->columns,image->rows,1)
749 for (y=0; y < (ssize_t) image->rows; y++)
757 register const Quantum
766 if (status == MagickFalse)
768 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
769 q=QueueCacheViewAuthenticPixels(colorize_view,0,y,colorize_image->columns,1,
771 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
776 GetPixelInfo(colorize_image,&pixel);
777 for (x=0; x < (ssize_t) image->columns; x++)
779 if (GetPixelMask(colorize_image,q) != 0)
781 p+=GetPixelChannels(image);
782 q+=GetPixelChannels(colorize_image);
785 GetPixelInfoPixel(image,p,&pixel);
786 Colorize(pixel.red,blend_percentage.red,colorize->red);
787 Colorize(pixel.green,blend_percentage.green,colorize->green);
788 Colorize(pixel.blue,blend_percentage.blue,colorize->blue);
789 Colorize(pixel.black,blend_percentage.black,colorize->black);
790 Colorize(pixel.alpha,blend_percentage.alpha,colorize->alpha);
791 SetPixelInfoPixel(colorize_image,&pixel,q);
792 p+=GetPixelChannels(image);
793 q+=GetPixelChannels(colorize_image);
795 sync=SyncCacheViewAuthenticPixels(colorize_view,exception);
796 if (sync == MagickFalse)
798 if (image->progress_monitor != (MagickProgressMonitor) NULL)
803 #if defined(MAGICKCORE_OPENMP_SUPPORT)
804 #pragma omp critical (MagickCore_ColorizeImage)
806 proceed=SetImageProgress(image,ColorizeImageTag,progress++,image->rows);
807 if (proceed == MagickFalse)
811 image_view=DestroyCacheView(image_view);
812 colorize_view=DestroyCacheView(colorize_view);
813 if (status == MagickFalse)
814 colorize_image=DestroyImage(colorize_image);
815 return(colorize_image);
819 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
823 % C o l o r M a t r i x I m a g e %
827 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
829 % ColorMatrixImage() applies color transformation to an image. This method
830 % permits saturation changes, hue rotation, luminance to alpha, and various
831 % other effects. Although variable-sized transformation matrices can be used,
832 % typically one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA
833 % (or RGBA with offsets). The matrix is similar to those used by Adobe Flash
834 % except offsets are in column 6 rather than 5 (in support of CMYKA images)
835 % and offsets are normalized (divide Flash offset by 255).
837 % The format of the ColorMatrixImage method is:
839 % Image *ColorMatrixImage(const Image *image,
840 % const KernelInfo *color_matrix,ExceptionInfo *exception)
842 % A description of each parameter follows:
844 % o image: the image.
846 % o color_matrix: the color matrix.
848 % o exception: return any errors or warnings in this structure.
851 /* FUTURE: modify to make use of a MagickMatrix Mutliply function
852 That should be provided in "matrix.c"
853 (ASIDE: actually distorts should do this too but currently doesn't)
856 MagickExport Image *ColorMatrixImage(const Image *image,
857 const KernelInfo *color_matrix,ExceptionInfo *exception)
859 #define ColorMatrixImageTag "ColorMatrix/Image"
868 { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
869 { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
870 { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
871 { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
872 { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
873 { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }
894 Map given color_matrix, into a 6x6 matrix RGBKA and a constant
896 assert(image != (Image *) NULL);
897 assert(image->signature == MagickSignature);
898 if (image->debug != MagickFalse)
899 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
900 assert(exception != (ExceptionInfo *) NULL);
901 assert(exception->signature == MagickSignature);
903 for (v=0; v < (ssize_t) color_matrix->height; v++)
904 for (u=0; u < (ssize_t) color_matrix->width; u++)
906 if ((v < 6) && (u < 6))
907 ColorMatrix[v][u]=color_matrix->values[i];
911 Initialize color image.
913 color_image=CloneImage(image,0,0,MagickTrue,exception);
914 if (color_image == (Image *) NULL)
915 return((Image *) NULL);
916 if (SetImageStorageClass(color_image,DirectClass,exception) == MagickFalse)
918 color_image=DestroyImage(color_image);
919 return((Image *) NULL);
921 if (image->debug != MagickFalse)
924 format[MaxTextExtent],
927 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
928 " ColorMatrix image with color matrix:");
929 message=AcquireString("");
930 for (v=0; v < 6; v++)
933 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
934 (void) ConcatenateString(&message,format);
935 for (u=0; u < 6; u++)
937 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",
939 (void) ConcatenateString(&message,format);
941 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
943 message=DestroyString(message);
946 Apply the ColorMatrix to image.
950 image_view=AcquireVirtualCacheView(image,exception);
951 color_view=AcquireAuthenticCacheView(color_image,exception);
952 #if defined(MAGICKCORE_OPENMP_SUPPORT)
953 #pragma omp parallel for schedule(static,4) shared(progress,status) \
954 dynamic_number_threads(image,image->columns,image->rows,1)
956 for (y=0; y < (ssize_t) image->rows; y++)
961 register const Quantum
970 if (status == MagickFalse)
972 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
973 q=GetCacheViewAuthenticPixels(color_view,0,y,color_image->columns,1,
975 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
980 GetPixelInfo(image,&pixel);
981 for (x=0; x < (ssize_t) image->columns; x++)
989 GetPixelInfoPixel(image,p,&pixel);
990 height=color_matrix->height > 6 ? 6UL : color_matrix->height;
991 for (v=0; v < (ssize_t) height; v++)
996 sum=ColorMatrix[v][0]*GetPixelRed(image,p)+ColorMatrix[v][1]*
997 GetPixelGreen(image,p)+ColorMatrix[v][2]*GetPixelBlue(image,p);
998 if (image->colorspace == CMYKColorspace)
999 sum+=ColorMatrix[v][3]*GetPixelBlack(image,p);
1000 if (image->matte != MagickFalse)
1001 sum+=ColorMatrix[v][4]*GetPixelAlpha(image,p);
1002 sum+=QuantumRange*ColorMatrix[v][5];
1005 case 0: pixel.red=sum; break;
1006 case 1: pixel.green=sum; break;
1007 case 2: pixel.blue=sum; break;
1008 case 3: pixel.black=sum; break;
1009 case 4: pixel.alpha=sum; break;
1013 SetPixelInfoPixel(color_image,&pixel,q);
1014 p+=GetPixelChannels(image);
1015 q+=GetPixelChannels(color_image);
1017 if (SyncCacheViewAuthenticPixels(color_view,exception) == MagickFalse)
1019 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1024 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1025 #pragma omp critical (MagickCore_ColorMatrixImage)
1027 proceed=SetImageProgress(image,ColorMatrixImageTag,progress++,
1029 if (proceed == MagickFalse)
1033 color_view=DestroyCacheView(color_view);
1034 image_view=DestroyCacheView(image_view);
1035 if (status == MagickFalse)
1036 color_image=DestroyImage(color_image);
1037 return(color_image);
1041 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1045 + D e s t r o y F x I n f o %
1049 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1051 % DestroyFxInfo() deallocates memory associated with an FxInfo structure.
1053 % The format of the DestroyFxInfo method is:
1055 % ImageInfo *DestroyFxInfo(ImageInfo *fx_info)
1057 % A description of each parameter follows:
1059 % o fx_info: the fx info.
1062 MagickPrivate FxInfo *DestroyFxInfo(FxInfo *fx_info)
1067 fx_info->exception=DestroyExceptionInfo(fx_info->exception);
1068 fx_info->expression=DestroyString(fx_info->expression);
1069 fx_info->symbols=DestroySplayTree(fx_info->symbols);
1070 fx_info->colors=DestroySplayTree(fx_info->colors);
1071 for (i=(ssize_t) GetImageListLength(fx_info->images)-1; i >= 0; i--)
1072 fx_info->view[i]=DestroyCacheView(fx_info->view[i]);
1073 fx_info->view=(CacheView **) RelinquishMagickMemory(fx_info->view);
1074 fx_info->random_info=DestroyRandomInfo(fx_info->random_info);
1075 fx_info=(FxInfo *) RelinquishMagickMemory(fx_info);
1080 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1084 + 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 %
1088 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1090 % FxEvaluateChannelExpression() evaluates an expression and returns the
1093 % The format of the FxEvaluateExpression method is:
1095 % MagickRealType FxEvaluateChannelExpression(FxInfo *fx_info,
1096 % const PixelChannel channel,const ssize_t x,const ssize_t y,
1097 % MagickRealType *alpha,Exceptioninfo *exception)
1098 % MagickRealType FxEvaluateExpression(FxInfo *fx_info,
1099 % MagickRealType *alpha,Exceptioninfo *exception)
1101 % A description of each parameter follows:
1103 % o fx_info: the fx info.
1105 % o channel: the channel.
1107 % o x,y: the pixel position.
1109 % o alpha: the result.
1111 % o exception: return any errors or warnings in this structure.
1115 static inline double MagickMax(const double x,const double y)
1122 static inline double MagickMin(const double x,const double y)
1129 static MagickRealType FxChannelStatistics(FxInfo *fx_info,const Image *image,
1130 PixelChannel channel,const char *symbol,ExceptionInfo *exception)
1134 statistic[MaxTextExtent];
1142 for (p=symbol; (*p != '.') && (*p != '\0'); p++) ;
1144 switch (*++p) /* e.g. depth.r */
1146 case 'r': channel=RedPixelChannel; break;
1147 case 'g': channel=GreenPixelChannel; break;
1148 case 'b': channel=BluePixelChannel; break;
1149 case 'c': channel=CyanPixelChannel; break;
1150 case 'm': channel=MagentaPixelChannel; break;
1151 case 'y': channel=YellowPixelChannel; break;
1152 case 'k': channel=BlackPixelChannel; break;
1155 (void) FormatLocaleString(key,MaxTextExtent,"%p.%.20g.%s",(void *) image,
1156 (double) channel,symbol);
1157 value=(const char *) GetValueFromSplayTree(fx_info->symbols,key);
1158 if (value != (const char *) NULL)
1159 return(QuantumScale*StringToDouble(value,(char **) NULL));
1160 (void) DeleteNodeFromSplayTree(fx_info->symbols,key);
1161 if (LocaleNCompare(symbol,"depth",5) == 0)
1166 depth=GetImageDepth(image,exception);
1167 (void) FormatLocaleString(statistic,MaxTextExtent,"%.20g",(double) depth);
1169 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1175 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
1176 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",kurtosis);
1178 if (LocaleNCompare(symbol,"maxima",6) == 0)
1184 (void) GetImageRange(image,&minima,&maxima,exception);
1185 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",maxima);
1187 if (LocaleNCompare(symbol,"mean",4) == 0)
1193 (void) GetImageMean(image,&mean,&standard_deviation,exception);
1194 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",mean);
1196 if (LocaleNCompare(symbol,"minima",6) == 0)
1202 (void) GetImageRange(image,&minima,&maxima,exception);
1203 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",minima);
1205 if (LocaleNCompare(symbol,"skewness",8) == 0)
1211 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
1212 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",skewness);
1214 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1220 (void) GetImageMean(image,&mean,&standard_deviation,exception);
1221 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",
1222 standard_deviation);
1224 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(key),
1225 ConstantString(statistic));
1226 return(QuantumScale*StringToDouble(statistic,(char **) NULL));
1229 static MagickRealType
1230 FxEvaluateSubexpression(FxInfo *,const PixelChannel,const ssize_t,
1231 const ssize_t,const char *,MagickRealType *,ExceptionInfo *);
1233 static MagickOffsetType FxGCD(MagickOffsetType alpha,MagickOffsetType beta)
1236 return(FxGCD(beta,alpha % beta));
1240 static inline const char *FxSubexpression(const char *expression,
1241 ExceptionInfo *exception)
1250 subexpression=expression;
1251 while ((*subexpression != '\0') &&
1252 ((level != 1) || (strchr(")",(int) *subexpression) == (char *) NULL)))
1254 if (strchr("(",(int) *subexpression) != (char *) NULL)
1257 if (strchr(")",(int) *subexpression) != (char *) NULL)
1261 if (*subexpression == '\0')
1262 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1263 "UnbalancedParenthesis","'%s'",expression);
1264 return(subexpression);
1267 static MagickRealType FxGetSymbol(FxInfo *fx_info,const PixelChannel channel,
1268 const ssize_t x,const ssize_t y,const char *expression,
1269 ExceptionInfo *exception)
1273 subexpression[MaxTextExtent],
1274 symbol[MaxTextExtent];
1301 i=GetImageIndexInList(fx_info->images);
1305 if (isalpha((int) *(p+1)) == 0)
1307 if (strchr("suv",(int) *p) != (char *) NULL)
1314 i=GetImageIndexInList(fx_info->images);
1317 case 'u': i=0; break;
1318 case 'v': i=1; break;
1325 for (p++; *p != '\0'; )
1339 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1341 i=(ssize_t) (alpha+0.5);
1347 if ((isalpha((int) *(p+1)) == 0) && (*p == 'p'))
1354 for (p++; *p != '\0'; )
1368 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1379 for (p++; *p != '\0'; )
1393 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1403 length=GetImageListLength(fx_info->images);
1405 i+=(ssize_t) length;
1407 image=GetImageFromList(fx_info->images,i);
1408 if (image == (Image *) NULL)
1410 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1411 "NoSuchImage","'%s'",expression);
1414 GetPixelInfo(image,&pixel);
1415 (void) InterpolatePixelInfo(image,fx_info->view[i],image->interpolate,
1416 point.x,point.y,&pixel,exception);
1417 if ((strlen(p) > 2) && (LocaleCompare(p,"intensity") != 0) &&
1418 (LocaleCompare(p,"luminance") != 0) && (LocaleCompare(p,"hue") != 0) &&
1419 (LocaleCompare(p,"saturation") != 0) &&
1420 (LocaleCompare(p,"lightness") != 0))
1423 name[MaxTextExtent];
1425 (void) CopyMagickString(name,p,MaxTextExtent);
1426 for (q=name+(strlen(name)-1); q > name; q--)
1436 if ((strlen(name) > 2) &&
1437 (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
1442 color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
1443 if (color != (PixelInfo *) NULL)
1453 status=QueryColorCompliance(name,AllCompliance,&pixel,
1454 fx_info->exception);
1455 if (status != MagickFalse)
1457 (void) AddValueToSplayTree(fx_info->colors,ConstantString(
1458 name),ClonePixelInfo(&pixel));
1464 (void) CopyMagickString(symbol,p,MaxTextExtent);
1465 StripString(symbol);
1466 if (*symbol == '\0')
1470 case RedPixelChannel: return(QuantumScale*pixel.red);
1471 case GreenPixelChannel: return(QuantumScale*pixel.green);
1472 case BluePixelChannel: return(QuantumScale*pixel.blue);
1473 case BlackPixelChannel:
1475 if (image->colorspace != CMYKColorspace)
1477 (void) ThrowMagickException(exception,GetMagickModule(),
1478 ImageError,"ColorSeparatedImageRequired","'%s'",
1482 return(QuantumScale*pixel.black);
1484 case AlphaPixelChannel:
1489 if (pixel.matte == MagickFalse)
1491 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
1494 case IndexPixelChannel:
1496 case IntensityPixelChannel:
1498 return(QuantumScale*GetPixelInfoIntensity(&pixel));
1503 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1504 "UnableToParseExpression","'%s'",p);
1512 if (LocaleCompare(symbol,"a") == 0)
1513 return((MagickRealType) (QuantumScale*pixel.alpha));
1519 if (LocaleCompare(symbol,"b") == 0)
1520 return(QuantumScale*pixel.blue);
1526 if (LocaleNCompare(symbol,"channel",7) == 0)
1534 flags=ParseGeometry(symbol+7,&channel_info);
1535 if (image->colorspace == CMYKColorspace)
1538 case CyanPixelChannel:
1540 if ((flags & RhoValue) == 0)
1542 return(channel_info.rho);
1544 case MagentaPixelChannel:
1546 if ((flags & SigmaValue) == 0)
1548 return(channel_info.sigma);
1550 case YellowPixelChannel:
1552 if ((flags & XiValue) == 0)
1554 return(channel_info.xi);
1556 case BlackPixelChannel:
1558 if ((flags & PsiValue) == 0)
1560 return(channel_info.psi);
1562 case AlphaPixelChannel:
1564 if ((flags & ChiValue) == 0)
1566 return(channel_info.chi);
1573 case RedPixelChannel:
1575 if ((flags & RhoValue) == 0)
1577 return(channel_info.rho);
1579 case GreenPixelChannel:
1581 if ((flags & SigmaValue) == 0)
1583 return(channel_info.sigma);
1585 case BluePixelChannel:
1587 if ((flags & XiValue) == 0)
1589 return(channel_info.xi);
1591 case BlackPixelChannel:
1593 if ((flags & ChiValue) == 0)
1595 return(channel_info.chi);
1597 case AlphaPixelChannel:
1599 if ((flags & PsiValue) == 0)
1601 return(channel_info.psi);
1608 if (LocaleCompare(symbol,"c") == 0)
1609 return(QuantumScale*pixel.red);
1615 if (LocaleNCompare(symbol,"depth",5) == 0)
1616 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1622 if (LocaleCompare(symbol,"g") == 0)
1623 return(QuantumScale*pixel.green);
1629 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1630 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1631 if (LocaleCompare(symbol,"k") == 0)
1633 if (image->colorspace != CMYKColorspace)
1635 (void) ThrowMagickException(exception,GetMagickModule(),
1636 OptionError,"ColorSeparatedImageRequired","'%s'",
1640 return(QuantumScale*pixel.black);
1647 if (LocaleCompare(symbol,"h") == 0)
1648 return((MagickRealType) image->rows);
1649 if (LocaleCompare(symbol,"hue") == 0)
1656 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1665 if ((LocaleCompare(symbol,"image.depth") == 0) ||
1666 (LocaleCompare(symbol,"image.minima") == 0) ||
1667 (LocaleCompare(symbol,"image.maxima") == 0) ||
1668 (LocaleCompare(symbol,"image.mean") == 0) ||
1669 (LocaleCompare(symbol,"image.kurtosis") == 0) ||
1670 (LocaleCompare(symbol,"image.skewness") == 0) ||
1671 (LocaleCompare(symbol,"image.standard_deviation") == 0))
1672 return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
1673 if (LocaleCompare(symbol,"image.resolution.x") == 0)
1674 return(image->resolution.x);
1675 if (LocaleCompare(symbol,"image.resolution.y") == 0)
1676 return(image->resolution.y);
1677 if (LocaleCompare(symbol,"intensity") == 0)
1678 return(QuantumScale*GetPixelInfoIntensity(&pixel));
1679 if (LocaleCompare(symbol,"i") == 0)
1680 return((MagickRealType) x);
1686 if (LocaleCompare(symbol,"j") == 0)
1687 return((MagickRealType) y);
1693 if (LocaleCompare(symbol,"lightness") == 0)
1700 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1704 if (LocaleCompare(symbol,"luminance") == 0)
1709 luminence=0.2126*pixel.red+0.7152*pixel.green+0.0722*pixel.blue;
1710 return(QuantumScale*luminence);
1717 if (LocaleNCompare(symbol,"maxima",6) == 0)
1718 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1719 if (LocaleNCompare(symbol,"mean",4) == 0)
1720 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1721 if (LocaleNCompare(symbol,"minima",6) == 0)
1722 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1723 if (LocaleCompare(symbol,"m") == 0)
1724 return(QuantumScale*pixel.blue);
1730 if (LocaleCompare(symbol,"n") == 0)
1731 return((MagickRealType) GetImageListLength(fx_info->images));
1737 if (LocaleCompare(symbol,"o") == 0)
1738 return(QuantumScale*pixel.alpha);
1744 if (LocaleCompare(symbol,"page.height") == 0)
1745 return((MagickRealType) image->page.height);
1746 if (LocaleCompare(symbol,"page.width") == 0)
1747 return((MagickRealType) image->page.width);
1748 if (LocaleCompare(symbol,"page.x") == 0)
1749 return((MagickRealType) image->page.x);
1750 if (LocaleCompare(symbol,"page.y") == 0)
1751 return((MagickRealType) image->page.y);
1757 if (LocaleCompare(symbol,"resolution.x") == 0)
1758 return(image->resolution.x);
1759 if (LocaleCompare(symbol,"resolution.y") == 0)
1760 return(image->resolution.y);
1761 if (LocaleCompare(symbol,"r") == 0)
1762 return(QuantumScale*pixel.red);
1768 if (LocaleCompare(symbol,"saturation") == 0)
1775 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1779 if (LocaleNCompare(symbol,"skewness",8) == 0)
1780 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1781 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1782 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1788 if (LocaleCompare(symbol,"t") == 0)
1789 return((MagickRealType) GetImageIndexInList(fx_info->images));
1795 if (LocaleCompare(symbol,"w") == 0)
1796 return((MagickRealType) image->columns);
1802 if (LocaleCompare(symbol,"y") == 0)
1803 return(QuantumScale*pixel.green);
1809 if (LocaleCompare(symbol,"z") == 0)
1814 depth=(MagickRealType) GetImageDepth(image,fx_info->exception);
1822 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1823 if (value != (const char *) NULL)
1824 return((MagickRealType) StringToDouble(value,(char **) NULL));
1825 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1826 "UnableToParseExpression","'%s'",symbol);
1830 static const char *FxOperatorPrecedence(const char *expression,
1831 ExceptionInfo *exception)
1835 UndefinedPrecedence,
1837 BitwiseComplementPrecedence,
1839 ExponentialNotationPrecedence,
1843 RelationalPrecedence,
1844 EquivalencyPrecedence,
1845 BitwiseAndPrecedence,
1846 BitwiseOrPrecedence,
1847 LogicalAndPrecedence,
1848 LogicalOrPrecedence,
1850 AssignmentPrecedence,
1870 subexpression=(const char *) NULL;
1871 target=NullPrecedence;
1872 while (*expression != '\0')
1874 precedence=UndefinedPrecedence;
1875 if ((isspace((int) ((char) *expression)) != 0) || (c == (int) '@'))
1880 switch (*expression)
1885 #if defined(MAGICKCORE_HAVE_ACOSH)
1886 if (LocaleNCompare(expression,"acosh",5) == 0)
1892 #if defined(MAGICKCORE_HAVE_ASINH)
1893 if (LocaleNCompare(expression,"asinh",5) == 0)
1899 #if defined(MAGICKCORE_HAVE_ATANH)
1900 if (LocaleNCompare(expression,"atanh",5) == 0)
1906 if (LocaleNCompare(expression,"atan2",5) == 0)
1916 if ((LocaleNCompare(expression,"E+",2) == 0) ||
1917 (LocaleNCompare(expression,"E-",2) == 0))
1919 expression+=2; /* scientific notation */
1926 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1927 (LocaleNCompare(expression,"j1",2) == 0))
1936 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1943 if ((c == (int) '{') || (c == (int) '['))
1946 if ((c == (int) '}') || (c == (int) ']'))
1949 switch ((unsigned char) *expression)
1954 precedence=BitwiseComplementPrecedence;
1960 precedence=ExponentPrecedence;
1965 if (((c != 0) && ((isdigit((int) ((char) c)) != 0) ||
1966 (strchr(")",c) != (char *) NULL))) &&
1967 (((islower((int) ((char) *expression)) != 0) ||
1968 (strchr("(",(int) *expression) != (char *) NULL)) ||
1969 ((isdigit((int) ((char) c)) == 0) &&
1970 (isdigit((int) ((char) *expression)) != 0))) &&
1971 (strchr("xy",(int) *expression) == (char *) NULL))
1972 precedence=MultiplyPrecedence;
1979 precedence=MultiplyPrecedence;
1985 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
1987 precedence=AdditionPrecedence;
1990 case LeftShiftOperator:
1991 case RightShiftOperator:
1993 precedence=ShiftPrecedence;
1997 case LessThanEqualOperator:
1998 case GreaterThanEqualOperator:
2001 precedence=RelationalPrecedence;
2005 case NotEqualOperator:
2007 precedence=EquivalencyPrecedence;
2012 precedence=BitwiseAndPrecedence;
2017 precedence=BitwiseOrPrecedence;
2020 case LogicalAndOperator:
2022 precedence=LogicalAndPrecedence;
2025 case LogicalOrOperator:
2027 precedence=LogicalOrPrecedence;
2030 case ExponentialNotation:
2032 precedence=ExponentialNotationPrecedence;
2038 precedence=TernaryPrecedence;
2043 precedence=AssignmentPrecedence;
2048 precedence=CommaPrecedence;
2053 precedence=SeparatorPrecedence;
2057 if ((precedence == BitwiseComplementPrecedence) ||
2058 (precedence == TernaryPrecedence) ||
2059 (precedence == AssignmentPrecedence))
2061 if (precedence > target)
2064 Right-to-left associativity.
2067 subexpression=expression;
2071 if (precedence >= target)
2074 Left-to-right associativity.
2077 subexpression=expression;
2079 if (strchr("(",(int) *expression) != (char *) NULL)
2080 expression=FxSubexpression(expression,exception);
2081 c=(int) (*expression++);
2083 return(subexpression);
2086 static MagickRealType FxEvaluateSubexpression(FxInfo *fx_info,
2087 const PixelChannel channel,const ssize_t x,const ssize_t y,
2088 const char *expression,MagickRealType *beta,ExceptionInfo *exception)
2092 subexpression[MaxTextExtent];
2102 if (exception->severity != UndefinedException)
2104 while (isspace((int) *expression) != 0)
2106 if (*expression == '\0')
2108 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
2109 "MissingExpression","'%s'",expression);
2112 *subexpression='\0';
2113 p=FxOperatorPrecedence(expression,exception);
2114 if (p != (const char *) NULL)
2116 (void) CopyMagickString(subexpression,expression,(size_t)
2118 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2120 switch ((unsigned char) *p)
2124 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2125 *beta=(MagickRealType) (~(size_t) *beta);
2130 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2131 return(*beta == 0.0 ? 1.0 : 0.0);
2135 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2136 channel,x,y,++p,beta,exception));
2140 case ExponentialNotation:
2142 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2143 return(alpha*(*beta));
2147 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2150 if (exception->severity == UndefinedException)
2151 (void) ThrowMagickException(exception,GetMagickModule(),
2152 OptionError,"DivideByZero","'%s'",expression);
2155 return(alpha/(*beta));
2159 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2160 *beta=fabs(floor(((double) *beta)+0.5));
2163 (void) ThrowMagickException(exception,GetMagickModule(),
2164 OptionError,"DivideByZero","'%s'",expression);
2167 return(fmod((double) alpha,(double) *beta));
2171 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2172 return(alpha+(*beta));
2176 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2177 return(alpha-(*beta));
2179 case LeftShiftOperator:
2181 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2182 *beta=(MagickRealType) ((size_t) (alpha+0.5) << (size_t) (gamma+0.5));
2185 case RightShiftOperator:
2187 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2188 *beta=(MagickRealType) ((size_t) (alpha+0.5) >> (size_t) (gamma+0.5));
2193 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2194 return(alpha < *beta ? 1.0 : 0.0);
2196 case LessThanEqualOperator:
2198 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2199 return(alpha <= *beta ? 1.0 : 0.0);
2203 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2204 return(alpha > *beta ? 1.0 : 0.0);
2206 case GreaterThanEqualOperator:
2208 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2209 return(alpha >= *beta ? 1.0 : 0.0);
2213 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2214 return(fabs(alpha-(*beta)) < MagickEpsilon ? MagickEpsilon : 0.0);
2216 case NotEqualOperator:
2218 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2219 return(fabs(alpha-(*beta)) >= MagickEpsilon ? 1.0 : 0.0);
2223 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2224 *beta=(MagickRealType) ((size_t) (alpha+0.5) & (size_t) (gamma+0.5));
2229 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2230 *beta=(MagickRealType) ((size_t) (alpha+0.5) | (size_t) (gamma+0.5));
2233 case LogicalAndOperator:
2235 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2236 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2239 case LogicalOrOperator:
2241 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2242 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2250 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2252 p=StringToken(":",&q);
2253 if (q == (char *) NULL)
2255 (void) ThrowMagickException(exception,GetMagickModule(),
2256 OptionError,"UnableToParseExpression","'%s'",subexpression);
2259 if (fabs((double) alpha) >= MagickEpsilon)
2260 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2262 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2268 numeric[MaxTextExtent];
2271 while (isalpha((int) ((unsigned char) *q)) != 0)
2275 (void) ThrowMagickException(exception,GetMagickModule(),
2276 OptionError,"UnableToParseExpression","'%s'",subexpression);
2279 ClearMagickException(exception);
2280 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2281 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
2283 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2284 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2285 subexpression),ConstantString(numeric));
2290 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2295 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2300 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2306 if (strchr("(",(int) *expression) != (char *) NULL)
2308 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2309 subexpression[strlen(subexpression)-1]='\0';
2310 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2314 switch (*expression)
2318 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2324 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2330 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2332 return((MagickRealType) (~(size_t) (gamma+0.5)));
2337 if (LocaleNCompare(expression,"abs",3) == 0)
2339 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2341 return((MagickRealType) fabs((double) alpha));
2343 #if defined(MAGICKCORE_HAVE_ACOSH)
2344 if (LocaleNCompare(expression,"acosh",5) == 0)
2346 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2348 return((MagickRealType) acosh((double) alpha));
2351 if (LocaleNCompare(expression,"acos",4) == 0)
2353 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2355 return((MagickRealType) acos((double) alpha));
2357 #if defined(MAGICKCORE_HAVE_J1)
2358 if (LocaleNCompare(expression,"airy",4) == 0)
2360 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2364 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
2365 return(gamma*gamma);
2368 #if defined(MAGICKCORE_HAVE_ASINH)
2369 if (LocaleNCompare(expression,"asinh",5) == 0)
2371 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2373 return((MagickRealType) asinh((double) alpha));
2376 if (LocaleNCompare(expression,"asin",4) == 0)
2378 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2380 return((MagickRealType) asin((double) alpha));
2382 if (LocaleNCompare(expression,"alt",3) == 0)
2384 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2386 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
2388 if (LocaleNCompare(expression,"atan2",5) == 0)
2390 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2392 return((MagickRealType) atan2((double) alpha,(double) *beta));
2394 #if defined(MAGICKCORE_HAVE_ATANH)
2395 if (LocaleNCompare(expression,"atanh",5) == 0)
2397 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2399 return((MagickRealType) atanh((double) alpha));
2402 if (LocaleNCompare(expression,"atan",4) == 0)
2404 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2406 return((MagickRealType) atan((double) alpha));
2408 if (LocaleCompare(expression,"a") == 0)
2409 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2415 if (LocaleCompare(expression,"b") == 0)
2416 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2422 if (LocaleNCompare(expression,"ceil",4) == 0)
2424 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2426 return((MagickRealType) ceil((double) alpha));
2428 if (LocaleNCompare(expression,"cosh",4) == 0)
2430 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2432 return((MagickRealType) cosh((double) alpha));
2434 if (LocaleNCompare(expression,"cos",3) == 0)
2436 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2438 return((MagickRealType) cos((double) alpha));
2440 if (LocaleCompare(expression,"c") == 0)
2441 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2447 if (LocaleNCompare(expression,"debug",5) == 0)
2452 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2454 if (fx_info->images->colorspace == CMYKColorspace)
2457 case CyanPixelChannel: type="cyan"; break;
2458 case MagentaPixelChannel: type="magenta"; break;
2459 case YellowPixelChannel: type="yellow"; break;
2460 case AlphaPixelChannel: type="opacity"; break;
2461 case BlackPixelChannel: type="black"; break;
2462 default: type="unknown"; break;
2467 case RedPixelChannel: type="red"; break;
2468 case GreenPixelChannel: type="green"; break;
2469 case BluePixelChannel: type="blue"; break;
2470 case AlphaPixelChannel: type="opacity"; break;
2471 default: type="unknown"; break;
2473 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2474 if (strlen(subexpression) > 1)
2475 subexpression[strlen(subexpression)-1]='\0';
2476 if (fx_info->file != (FILE *) NULL)
2477 (void) FormatLocaleFile(fx_info->file,"%s[%.20g,%.20g].%s: "
2478 "%s=%.*g\n",fx_info->images->filename,(double) x,(double) y,type,
2479 subexpression,GetMagickPrecision(),(double) alpha);
2482 if (LocaleNCompare(expression,"drc",3) == 0)
2484 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2486 return((MagickRealType) (alpha/(*beta*(alpha-1.0)+1.0)));
2493 if (LocaleCompare(expression,"epsilon") == 0)
2494 return((MagickRealType) MagickEpsilon);
2495 if (LocaleNCompare(expression,"exp",3) == 0)
2497 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2499 return((MagickRealType) exp((double) alpha));
2501 if (LocaleCompare(expression,"e") == 0)
2502 return((MagickRealType) 2.7182818284590452354);
2508 if (LocaleNCompare(expression,"floor",5) == 0)
2510 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2512 return((MagickRealType) floor((double) alpha));
2519 if (LocaleNCompare(expression,"gauss",5) == 0)
2521 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2523 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
2524 return((MagickRealType) gamma);
2526 if (LocaleNCompare(expression,"gcd",3) == 0)
2531 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2533 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType) (*beta+
2535 return((MagickRealType) gcd);
2537 if (LocaleCompare(expression,"g") == 0)
2538 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2544 if (LocaleCompare(expression,"h") == 0)
2545 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2546 if (LocaleCompare(expression,"hue") == 0)
2547 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2548 if (LocaleNCompare(expression,"hypot",5) == 0)
2550 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2552 return((MagickRealType) hypot((double) alpha,(double) *beta));
2559 if (LocaleCompare(expression,"k") == 0)
2560 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2566 if (LocaleCompare(expression,"intensity") == 0)
2567 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2568 if (LocaleNCompare(expression,"int",3) == 0)
2570 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2572 return((MagickRealType) floor(alpha));
2574 #if defined(MAGICKCORE_HAVE_ISNAN)
2575 if (LocaleNCompare(expression,"isnan",5) == 0)
2577 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2579 return((MagickRealType) !!isnan((double) alpha));
2582 if (LocaleCompare(expression,"i") == 0)
2583 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2589 if (LocaleCompare(expression,"j") == 0)
2590 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2591 #if defined(MAGICKCORE_HAVE_J0)
2592 if (LocaleNCompare(expression,"j0",2) == 0)
2594 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2596 return((MagickRealType) j0((double) alpha));
2599 #if defined(MAGICKCORE_HAVE_J1)
2600 if (LocaleNCompare(expression,"j1",2) == 0)
2602 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2604 return((MagickRealType) j1((double) alpha));
2607 #if defined(MAGICKCORE_HAVE_J1)
2608 if (LocaleNCompare(expression,"jinc",4) == 0)
2610 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2614 gamma=(MagickRealType) (2.0*j1((double) (MagickPI*alpha))/(MagickPI*
2624 if (LocaleNCompare(expression,"ln",2) == 0)
2626 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2628 return((MagickRealType) log((double) alpha));
2630 if (LocaleNCompare(expression,"logtwo",6) == 0)
2632 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2634 return((MagickRealType) log10((double) alpha))/log10(2.0);
2636 if (LocaleNCompare(expression,"log",3) == 0)
2638 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2640 return((MagickRealType) log10((double) alpha));
2642 if (LocaleCompare(expression,"lightness") == 0)
2643 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2649 if (LocaleCompare(expression,"MaxRGB") == 0)
2650 return((MagickRealType) QuantumRange);
2651 if (LocaleNCompare(expression,"maxima",6) == 0)
2653 if (LocaleNCompare(expression,"max",3) == 0)
2655 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2657 return(alpha > *beta ? alpha : *beta);
2659 if (LocaleNCompare(expression,"minima",6) == 0)
2661 if (LocaleNCompare(expression,"min",3) == 0)
2663 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2665 return(alpha < *beta ? alpha : *beta);
2667 if (LocaleNCompare(expression,"mod",3) == 0)
2669 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2671 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2674 if (LocaleCompare(expression,"m") == 0)
2675 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2681 if (LocaleNCompare(expression,"not",3) == 0)
2683 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2685 return((MagickRealType) (alpha < MagickEpsilon));
2687 if (LocaleCompare(expression,"n") == 0)
2688 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2694 if (LocaleCompare(expression,"Opaque") == 0)
2696 if (LocaleCompare(expression,"o") == 0)
2697 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2703 if (LocaleCompare(expression,"phi") == 0)
2704 return((MagickRealType) MagickPHI);
2705 if (LocaleCompare(expression,"pi") == 0)
2706 return((MagickRealType) MagickPI);
2707 if (LocaleNCompare(expression,"pow",3) == 0)
2709 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2711 return((MagickRealType) pow((double) alpha,(double) *beta));
2713 if (LocaleCompare(expression,"p") == 0)
2714 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2720 if (LocaleCompare(expression,"QuantumRange") == 0)
2721 return((MagickRealType) QuantumRange);
2722 if (LocaleCompare(expression,"QuantumScale") == 0)
2723 return((MagickRealType) QuantumScale);
2729 if (LocaleNCompare(expression,"rand",4) == 0)
2730 return((MagickRealType) GetPseudoRandomValue(fx_info->random_info));
2731 if (LocaleNCompare(expression,"round",5) == 0)
2733 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2735 return((MagickRealType) floor((double) alpha+0.5));
2737 if (LocaleCompare(expression,"r") == 0)
2738 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2744 if (LocaleCompare(expression,"saturation") == 0)
2745 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2746 if (LocaleNCompare(expression,"sign",4) == 0)
2748 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2750 return(alpha < 0.0 ? -1.0 : 1.0);
2752 if (LocaleNCompare(expression,"sinc",4) == 0)
2754 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2758 gamma=(MagickRealType) (sin((double) (MagickPI*alpha))/
2762 if (LocaleNCompare(expression,"sinh",4) == 0)
2764 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2766 return((MagickRealType) sinh((double) alpha));
2768 if (LocaleNCompare(expression,"sin",3) == 0)
2770 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2772 return((MagickRealType) sin((double) alpha));
2774 if (LocaleNCompare(expression,"sqrt",4) == 0)
2776 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2778 return((MagickRealType) sqrt((double) alpha));
2780 if (LocaleNCompare(expression,"squish",6) == 0)
2782 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2784 return((MagickRealType) (1.0/(1.0+exp((double) (-alpha)))));
2786 if (LocaleCompare(expression,"s") == 0)
2787 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2793 if (LocaleNCompare(expression,"tanh",4) == 0)
2795 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2797 return((MagickRealType) tanh((double) alpha));
2799 if (LocaleNCompare(expression,"tan",3) == 0)
2801 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2803 return((MagickRealType) tan((double) alpha));
2805 if (LocaleCompare(expression,"Transparent") == 0)
2807 if (LocaleNCompare(expression,"trunc",5) == 0)
2809 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2812 return((MagickRealType) floor((double) alpha));
2813 return((MagickRealType) ceil((double) alpha));
2815 if (LocaleCompare(expression,"t") == 0)
2816 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2822 if (LocaleCompare(expression,"u") == 0)
2823 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2829 if (LocaleCompare(expression,"v") == 0)
2830 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2836 if (LocaleNCompare(expression,"while",5) == 0)
2840 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2842 } while (fabs((double) alpha) >= MagickEpsilon);
2843 return((MagickRealType) *beta);
2845 if (LocaleCompare(expression,"w") == 0)
2846 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2852 if (LocaleCompare(expression,"y") == 0)
2853 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2859 if (LocaleCompare(expression,"z") == 0)
2860 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2866 q=(char *) expression;
2867 alpha=InterpretSiPrefixValue(expression,&q);
2868 if (q == expression)
2869 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2873 MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
2874 MagickRealType *alpha,ExceptionInfo *exception)
2879 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2884 MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
2885 MagickRealType *alpha,ExceptionInfo *exception)
2894 fx_info->file=(FILE *) NULL;
2895 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2901 MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
2902 const PixelChannel channel,const ssize_t x,const ssize_t y,
2903 MagickRealType *alpha,ExceptionInfo *exception)
2909 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2911 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2915 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2923 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2925 % FxImage() applies a mathematical expression to the specified image.
2927 % The format of the FxImage method is:
2929 % Image *FxImage(const Image *image,const char *expression,
2930 % ExceptionInfo *exception)
2932 % A description of each parameter follows:
2934 % o image: the image.
2936 % o expression: A mathematical expression.
2938 % o exception: return any errors or warnings in this structure.
2942 static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2947 assert(fx_info != (FxInfo **) NULL);
2948 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
2949 if (fx_info[i] != (FxInfo *) NULL)
2950 fx_info[i]=DestroyFxInfo(fx_info[i]);
2951 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
2955 static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2956 ExceptionInfo *exception)
2973 number_threads=GetOpenMPMaximumThreads();
2974 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
2975 if (fx_info == (FxInfo **) NULL)
2976 return((FxInfo **) NULL);
2977 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2978 if (*expression != '@')
2979 fx_expression=ConstantString(expression);
2981 fx_expression=FileToString(expression+1,~0,exception);
2982 for (i=0; i < (ssize_t) number_threads; i++)
2984 fx_info[i]=AcquireFxInfo(image,fx_expression,exception);
2985 if (fx_info[i] == (FxInfo *) NULL)
2986 return(DestroyFxThreadSet(fx_info));
2987 (void) FxPreprocessExpression(fx_info[i],&alpha,fx_info[i]->exception);
2989 fx_expression=DestroyString(fx_expression);
2993 MagickExport Image *FxImage(const Image *image,const char *expression,
2994 ExceptionInfo *exception)
2996 #define FxImageTag "Fx/Image"
3020 assert(image != (Image *) NULL);
3021 assert(image->signature == MagickSignature);
3022 if (image->debug != MagickFalse)
3023 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3024 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3025 if (fx_image == (Image *) NULL)
3026 return((Image *) NULL);
3027 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
3029 fx_image=DestroyImage(fx_image);
3030 return((Image *) NULL);
3032 fx_info=AcquireFxThreadSet(image,expression,exception);
3033 if (fx_info == (FxInfo **) NULL)
3035 fx_image=DestroyImage(fx_image);
3036 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3038 status=FxPreprocessExpression(fx_info[0],&alpha,exception);
3039 if (status == MagickFalse)
3041 fx_image=DestroyImage(fx_image);
3042 fx_info=DestroyFxThreadSet(fx_info);
3043 return((Image *) NULL);
3050 image_view=AcquireVirtualCacheView(image,exception);
3051 fx_view=AcquireAuthenticCacheView(fx_image,exception);
3052 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3053 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3054 dynamic_number_threads(image,image->columns,image->rows,1)
3056 for (y=0; y < (ssize_t) fx_image->rows; y++)
3059 id = GetOpenMPThreadId();
3061 register const Quantum
3070 if (status == MagickFalse)
3072 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3073 q=QueueCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
3074 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3079 for (x=0; x < (ssize_t) fx_image->columns; x++)
3084 if (GetPixelMask(image,p) != 0)
3086 p+=GetPixelChannels(image);
3087 q+=GetPixelChannels(fx_image);
3090 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3102 channel=GetPixelChannelMapChannel(image,i);
3103 traits=GetPixelChannelMapTraits(image,channel);
3104 fx_traits=GetPixelChannelMapTraits(fx_image,channel);
3105 if ((traits == UndefinedPixelTrait) ||
3106 (fx_traits == UndefinedPixelTrait))
3108 if ((fx_traits & CopyPixelTrait) != 0)
3110 SetPixelChannel(fx_image,channel,p[i],q);
3114 (void) FxEvaluateChannelExpression(fx_info[id],channel,x,y,&alpha,
3116 q[i]=ClampToQuantum(QuantumRange*alpha);
3118 p+=GetPixelChannels(image);
3119 q+=GetPixelChannels(fx_image);
3121 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3123 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3128 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3129 #pragma omp critical (MagickCore_FxImage)
3131 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3132 if (proceed == MagickFalse)
3136 fx_view=DestroyCacheView(fx_view);
3137 image_view=DestroyCacheView(image_view);
3138 fx_info=DestroyFxThreadSet(fx_info);
3139 if (status == MagickFalse)
3140 fx_image=DestroyImage(fx_image);
3145 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3149 % I m p l o d e I m a g e %
3153 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3155 % ImplodeImage() creates a new image that is a copy of an existing
3156 % one with the image pixels "implode" by the specified percentage. It
3157 % allocates the memory necessary for the new Image structure and returns a
3158 % pointer to the new image.
3160 % The format of the ImplodeImage method is:
3162 % Image *ImplodeImage(const Image *image,const double amount,
3163 % const PixelInterpolateMethod method,ExceptionInfo *exception)
3165 % A description of each parameter follows:
3167 % o implode_image: Method ImplodeImage returns a pointer to the image
3168 % after it is implode. A null image is returned if there is a memory
3171 % o image: the image.
3173 % o amount: Define the extent of the implosion.
3175 % o method: the pixel interpolation method.
3177 % o exception: return any errors or warnings in this structure.
3180 MagickExport Image *ImplodeImage(const Image *image,const double amount,
3181 const PixelInterpolateMethod method,ExceptionInfo *exception)
3183 #define ImplodeImageTag "Implode/Image"
3209 Initialize implode image attributes.
3211 assert(image != (Image *) NULL);
3212 assert(image->signature == MagickSignature);
3213 if (image->debug != MagickFalse)
3214 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3215 assert(exception != (ExceptionInfo *) NULL);
3216 assert(exception->signature == MagickSignature);
3217 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3219 if (implode_image == (Image *) NULL)
3220 return((Image *) NULL);
3221 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
3223 implode_image=DestroyImage(implode_image);
3224 return((Image *) NULL);
3226 if (implode_image->background_color.alpha != OpaqueAlpha)
3227 implode_image->matte=MagickTrue;
3229 Compute scaling factor.
3233 center.x=0.5*image->columns;
3234 center.y=0.5*image->rows;
3236 if (image->columns > image->rows)
3237 scale.y=(double) image->columns/(double) image->rows;
3239 if (image->columns < image->rows)
3241 scale.x=(double) image->rows/(double) image->columns;
3249 image_view=AcquireVirtualCacheView(image,exception);
3250 implode_view=AcquireAuthenticCacheView(implode_image,exception);
3251 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3252 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3253 dynamic_number_threads(image,image->columns,image->rows,1)
3255 for (y=0; y < (ssize_t) image->rows; y++)
3263 register const Quantum
3272 if (status == MagickFalse)
3274 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3275 q=QueueCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
3277 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3282 delta.y=scale.y*(double) (y-center.y);
3283 for (x=0; x < (ssize_t) image->columns; x++)
3289 Determine if the pixel is within an ellipse.
3291 if (GetPixelMask(image,p) != 0)
3293 p+=GetPixelChannels(image);
3294 q+=GetPixelChannels(implode_image);
3297 delta.x=scale.x*(double) (x-center.x);
3298 distance=delta.x*delta.x+delta.y*delta.y;
3299 if (distance >= (radius*radius))
3300 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3309 channel=GetPixelChannelMapChannel(image,i);
3310 traits=GetPixelChannelMapTraits(image,channel);
3311 implode_traits=GetPixelChannelMapTraits(implode_image,channel);
3312 if ((traits == UndefinedPixelTrait) ||
3313 (implode_traits == UndefinedPixelTrait))
3315 SetPixelChannel(implode_image,channel,p[i],q);
3327 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/radius/
3329 status=InterpolatePixelChannels(image,image_view,implode_image,method,
3330 (double) (factor*delta.x/scale.x+center.x),(double) (factor*delta.y/
3331 scale.y+center.y),q,exception);
3333 p+=GetPixelChannels(image);
3334 q+=GetPixelChannels(implode_image);
3336 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3338 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3343 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3344 #pragma omp critical (MagickCore_ImplodeImage)
3346 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3347 if (proceed == MagickFalse)
3351 implode_view=DestroyCacheView(implode_view);
3352 image_view=DestroyCacheView(image_view);
3353 if (status == MagickFalse)
3354 implode_image=DestroyImage(implode_image);
3355 return(implode_image);
3359 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3363 % M o r p h I m a g e s %
3367 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3369 % The MorphImages() method requires a minimum of two images. The first
3370 % image is transformed into the second by a number of intervening images
3371 % as specified by frames.
3373 % The format of the MorphImage method is:
3375 % Image *MorphImages(const Image *image,const size_t number_frames,
3376 % ExceptionInfo *exception)
3378 % A description of each parameter follows:
3380 % o image: the image.
3382 % o number_frames: Define the number of in-between image to generate.
3383 % The more in-between frames, the smoother the morph.
3385 % o exception: return any errors or warnings in this structure.
3388 MagickExport Image *MorphImages(const Image *image,
3389 const size_t number_frames,ExceptionInfo *exception)
3391 #define MorphImageTag "Morph/Image"
3407 register const Image
3417 Clone first frame in sequence.
3419 assert(image != (Image *) NULL);
3420 assert(image->signature == MagickSignature);
3421 if (image->debug != MagickFalse)
3422 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3423 assert(exception != (ExceptionInfo *) NULL);
3424 assert(exception->signature == MagickSignature);
3425 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3426 if (morph_images == (Image *) NULL)
3427 return((Image *) NULL);
3428 if (GetNextImageInList(image) == (Image *) NULL)
3433 for (i=1; i < (ssize_t) number_frames; i++)
3435 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3436 if (morph_image == (Image *) NULL)
3438 morph_images=DestroyImageList(morph_images);
3439 return((Image *) NULL);
3441 AppendImageToList(&morph_images,morph_image);
3442 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3447 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3449 if (proceed == MagickFalse)
3453 return(GetFirstImageInList(morph_images));
3456 Morph image sequence.
3461 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3463 for (i=0; i < (ssize_t) number_frames; i++)
3469 beta=(MagickRealType) (i+1.0)/(MagickRealType) (number_frames+1.0);
3471 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
3472 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*next->rows+beta*
3473 GetNextImageInList(next)->rows+0.5),next->filter,exception);
3474 if (morph_image == (Image *) NULL)
3476 morph_images=DestroyImageList(morph_images);
3477 return((Image *) NULL);
3479 status=SetImageStorageClass(morph_image,DirectClass,exception);
3480 if (status == MagickFalse)
3482 morph_image=DestroyImage(morph_image);
3483 return((Image *) NULL);
3485 AppendImageToList(&morph_images,morph_image);
3486 morph_images=GetLastImageInList(morph_images);
3487 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
3488 morph_images->rows,GetNextImageInList(next)->filter,exception);
3489 if (morph_image == (Image *) NULL)
3491 morph_images=DestroyImageList(morph_images);
3492 return((Image *) NULL);
3494 image_view=AcquireVirtualCacheView(morph_image,exception);
3495 morph_view=AcquireAuthenticCacheView(morph_images,exception);
3496 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3497 #pragma omp parallel for schedule(static,4) shared(status) \
3498 dynamic_number_threads(image,image->columns,image->rows,1)
3500 for (y=0; y < (ssize_t) morph_images->rows; y++)
3505 register const Quantum
3514 if (status == MagickFalse)
3516 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3518 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3520 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3525 for (x=0; x < (ssize_t) morph_images->columns; x++)
3530 if (GetPixelMask(image,p) != 0)
3532 p+=GetPixelChannels(image);
3533 q+=GetPixelChannels(morph_image);
3536 for (i=0; i < (ssize_t) GetPixelChannels(morph_image); i++)
3545 channel=GetPixelChannelMapChannel(image,i);
3546 traits=GetPixelChannelMapTraits(image,channel);
3547 morph_traits=GetPixelChannelMapTraits(morph_image,channel);
3548 if ((traits == UndefinedPixelTrait) ||
3549 (morph_traits == UndefinedPixelTrait))
3551 if ((morph_traits & CopyPixelTrait) != 0)
3553 SetPixelChannel(morph_image,channel,p[i],q);
3556 SetPixelChannel(morph_image,channel,ClampToQuantum(alpha*
3557 GetPixelChannel(morph_images,channel,q)+beta*p[i]),q);
3559 p+=GetPixelChannels(morph_image);
3560 q+=GetPixelChannels(morph_images);
3562 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3563 if (sync == MagickFalse)
3566 morph_view=DestroyCacheView(morph_view);
3567 image_view=DestroyCacheView(image_view);
3568 morph_image=DestroyImage(morph_image);
3570 if (i < (ssize_t) number_frames)
3573 Clone last frame in sequence.
3575 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3576 if (morph_image == (Image *) NULL)
3578 morph_images=DestroyImageList(morph_images);
3579 return((Image *) NULL);
3581 AppendImageToList(&morph_images,morph_image);
3582 morph_images=GetLastImageInList(morph_images);
3583 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3588 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3589 #pragma omp critical (MagickCore_MorphImages)
3591 proceed=SetImageProgress(image,MorphImageTag,scene,
3592 GetImageListLength(image));
3593 if (proceed == MagickFalse)
3598 if (GetNextImageInList(next) != (Image *) NULL)
3600 morph_images=DestroyImageList(morph_images);
3601 return((Image *) NULL);
3603 return(GetFirstImageInList(morph_images));
3607 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3611 % P l a s m a I m a g e %
3615 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3617 % PlasmaImage() initializes an image with plasma fractal values. The image
3618 % must be initialized with a base color and the random number generator
3619 % seeded before this method is called.
3621 % The format of the PlasmaImage method is:
3623 % MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
3624 % size_t attenuate,size_t depth,ExceptionInfo *exception)
3626 % A description of each parameter follows:
3628 % o image: the image.
3630 % o segment: Define the region to apply plasma fractals values.
3632 % o attenuate: Define the plasma attenuation factor.
3634 % o depth: Limit the plasma recursion depth.
3636 % o exception: return any errors or warnings in this structure.
3640 static inline Quantum PlasmaPixel(RandomInfo *random_info,
3641 const MagickRealType pixel,const MagickRealType noise)
3646 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
3651 static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3652 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3653 const SegmentInfo *segment,size_t attenuate,size_t depth,
3654 ExceptionInfo *exception)
3665 register const Quantum
3681 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3689 Divide the area into quadrants and recurse.
3693 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3694 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
3695 local_info=(*segment);
3696 local_info.x2=(double) x_mid;
3697 local_info.y2=(double) y_mid;
3698 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3699 &local_info,attenuate,depth,exception);
3700 local_info=(*segment);
3701 local_info.y1=(double) y_mid;
3702 local_info.x2=(double) x_mid;
3703 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3704 &local_info,attenuate,depth,exception);
3705 local_info=(*segment);
3706 local_info.x1=(double) x_mid;
3707 local_info.y2=(double) y_mid;
3708 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3709 &local_info,attenuate,depth,exception);
3710 local_info=(*segment);
3711 local_info.x1=(double) x_mid;
3712 local_info.y1=(double) y_mid;
3713 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3714 &local_info,attenuate,depth,exception));
3716 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3717 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
3718 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3719 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3720 return(MagickFalse);
3722 Average pixels and apply plasma.
3724 plasma=(MagickRealType) QuantumRange/(2.0*attenuate);
3725 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3730 x=(ssize_t) ceil(segment->x1-0.5);
3731 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),1,1,
3733 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),1,1,
3735 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
3736 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3737 (q == (Quantum *) NULL))
3739 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3741 channel=GetPixelChannelMapChannel(image,i);
3742 traits=GetPixelChannelMapTraits(image,channel);
3743 if (traits == UndefinedPixelTrait)
3745 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3747 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3748 if (segment->x1 != segment->x2)
3753 x=(ssize_t) ceil(segment->x2-0.5);
3754 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3756 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3758 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
3759 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3760 (q == (Quantum *) NULL))
3762 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3764 channel=GetPixelChannelMapChannel(image,i);
3765 traits=GetPixelChannelMapTraits(image,channel);
3766 if (traits == UndefinedPixelTrait)
3768 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3770 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3773 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3775 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3780 y=(ssize_t) ceil(segment->y2-0.5);
3781 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3783 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3785 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
3786 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3787 (q == (Quantum *) NULL))
3789 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3791 channel=GetPixelChannelMapChannel(image,i);
3792 traits=GetPixelChannelMapTraits(image,channel);
3793 if (traits == UndefinedPixelTrait)
3795 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3797 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3799 if (segment->y1 != segment->y2)
3804 y=(ssize_t) ceil(segment->y1-0.5);
3805 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3807 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3809 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
3810 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3811 (q == (Quantum *) NULL))
3813 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3815 channel=GetPixelChannelMapChannel(image,i);
3816 traits=GetPixelChannelMapTraits(image,channel);
3817 if (traits == UndefinedPixelTrait)
3819 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3821 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3824 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3829 x=(ssize_t) ceil(segment->x1-0.5);
3830 y=(ssize_t) ceil(segment->y1-0.5);
3831 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
3832 x=(ssize_t) ceil(segment->x2-0.5);
3833 y=(ssize_t) ceil(segment->y2-0.5);
3834 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
3835 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
3836 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3837 (q == (Quantum *) NULL))
3839 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3841 channel=GetPixelChannelMapChannel(image,i);
3842 traits=GetPixelChannelMapTraits(image,channel);
3843 if (traits == UndefinedPixelTrait)
3845 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3847 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3849 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3851 return(MagickFalse);
3854 MagickExport MagickBooleanType PlasmaImage(Image *image,
3855 const SegmentInfo *segment,size_t attenuate,size_t depth,
3856 ExceptionInfo *exception)
3869 if (image->debug != MagickFalse)
3870 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3871 assert(image != (Image *) NULL);
3872 assert(image->signature == MagickSignature);
3873 if (image->debug != MagickFalse)
3874 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3875 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
3876 return(MagickFalse);
3877 image_view=AcquireAuthenticCacheView(image,exception);
3878 u_view=AcquireVirtualCacheView(image,exception);
3879 v_view=AcquireVirtualCacheView(image,exception);
3880 random_info=AcquireRandomInfo();
3881 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3882 attenuate,depth,exception);
3883 random_info=DestroyRandomInfo(random_info);
3884 v_view=DestroyCacheView(v_view);
3885 u_view=DestroyCacheView(u_view);
3886 image_view=DestroyCacheView(image_view);
3891 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3895 % P o l a r o i d I m a g e %
3899 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3901 % PolaroidImage() simulates a Polaroid picture.
3903 % The format of the AnnotateImage method is:
3905 % Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
3906 % const char *caption,const double angle,
3907 % const PixelInterpolateMethod method,ExceptionInfo exception)
3909 % A description of each parameter follows:
3911 % o image: the image.
3913 % o draw_info: the draw info.
3915 % o caption: the Polaroid caption.
3917 % o angle: Apply the effect along this angle.
3919 % o method: the pixel interpolation method.
3921 % o exception: return any errors or warnings in this structure.
3924 MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
3925 const char *caption,const double angle,const PixelInterpolateMethod method,
3926 ExceptionInfo *exception)
3944 Simulate a Polaroid picture.
3946 assert(image != (Image *) NULL);
3947 assert(image->signature == MagickSignature);
3948 if (image->debug != MagickFalse)
3949 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3950 assert(exception != (ExceptionInfo *) NULL);
3951 assert(exception->signature == MagickSignature);
3952 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
3953 image->rows)/25.0,10.0);
3954 height=image->rows+2*quantum;
3955 caption_image=(Image *) NULL;
3956 if (caption != (const char *) NULL)
3959 geometry[MaxTextExtent],
3975 Generate caption image.
3977 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3978 if (caption_image == (Image *) NULL)
3979 return((Image *) NULL);
3980 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
3981 text=InterpretImageProperties((ImageInfo *) NULL,(Image *) image,caption,
3983 (void) CloneString(&annotate_info->text,text);
3984 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
3986 status=SetImageExtent(caption_image,image->columns,(size_t) ((count+1)*
3987 (metrics.ascent-metrics.descent)+0.5),exception);
3988 if (status == MagickFalse)
3989 caption_image=DestroyImage(caption_image);
3992 caption_image->background_color=image->border_color;
3993 (void) SetImageBackgroundColor(caption_image,exception);
3994 (void) CloneString(&annotate_info->text,text);
3995 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
3997 if (annotate_info->gravity == UndefinedGravity)
3998 (void) CloneString(&annotate_info->geometry,AcquireString(
4000 (void) AnnotateImage(caption_image,annotate_info,exception);
4001 height+=caption_image->rows;
4003 annotate_info=DestroyDrawInfo(annotate_info);
4004 text=DestroyString(text);
4006 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
4008 if (picture_image == (Image *) NULL)
4010 if (caption_image != (Image *) NULL)
4011 caption_image=DestroyImage(caption_image);
4012 return((Image *) NULL);
4014 picture_image->background_color=image->border_color;
4015 (void) SetImageBackgroundColor(picture_image,exception);
4016 (void) CompositeImage(picture_image,image,OverCompositeOp,MagickTrue,quantum,
4018 if (caption_image != (Image *) NULL)
4020 (void) CompositeImage(picture_image,caption_image,OverCompositeOp,
4021 MagickTrue,quantum,(ssize_t) (image->rows+3*quantum/2),exception);
4022 caption_image=DestroyImage(caption_image);
4024 (void) QueryColorCompliance("none",AllCompliance,
4025 &picture_image->background_color,exception);
4026 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
4027 rotate_image=RotateImage(picture_image,90.0,exception);
4028 picture_image=DestroyImage(picture_image);
4029 if (rotate_image == (Image *) NULL)
4030 return((Image *) NULL);
4031 picture_image=rotate_image;
4032 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
4033 picture_image->columns,method,exception);
4034 picture_image=DestroyImage(picture_image);
4035 if (bend_image == (Image *) NULL)
4036 return((Image *) NULL);
4037 picture_image=bend_image;
4038 rotate_image=RotateImage(picture_image,-90.0,exception);
4039 picture_image=DestroyImage(picture_image);
4040 if (rotate_image == (Image *) NULL)
4041 return((Image *) NULL);
4042 picture_image=rotate_image;
4043 picture_image->background_color=image->background_color;
4044 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
4046 if (polaroid_image == (Image *) NULL)
4048 picture_image=DestroyImage(picture_image);
4049 return(picture_image);
4051 flop_image=FlopImage(polaroid_image,exception);
4052 polaroid_image=DestroyImage(polaroid_image);
4053 if (flop_image == (Image *) NULL)
4055 picture_image=DestroyImage(picture_image);
4056 return(picture_image);
4058 polaroid_image=flop_image;
4059 (void) CompositeImage(polaroid_image,picture_image,OverCompositeOp,
4060 MagickTrue,(ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
4061 picture_image=DestroyImage(picture_image);
4062 (void) QueryColorCompliance("none",AllCompliance,
4063 &polaroid_image->background_color,exception);
4064 rotate_image=RotateImage(polaroid_image,angle,exception);
4065 polaroid_image=DestroyImage(polaroid_image);
4066 if (rotate_image == (Image *) NULL)
4067 return((Image *) NULL);
4068 polaroid_image=rotate_image;
4069 trim_image=TrimImage(polaroid_image,exception);
4070 polaroid_image=DestroyImage(polaroid_image);
4071 if (trim_image == (Image *) NULL)
4072 return((Image *) NULL);
4073 polaroid_image=trim_image;
4074 return(polaroid_image);
4078 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4082 % S e p i a T o n e I m a g e %
4086 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4088 % MagickSepiaToneImage() applies a special effect to the image, similar to the
4089 % effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4090 % 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4091 % threshold of 80% is a good starting point for a reasonable tone.
4093 % The format of the SepiaToneImage method is:
4095 % Image *SepiaToneImage(const Image *image,const double threshold,
4096 % ExceptionInfo *exception)
4098 % A description of each parameter follows:
4100 % o image: the image.
4102 % o threshold: the tone threshold.
4104 % o exception: return any errors or warnings in this structure.
4107 MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4108 ExceptionInfo *exception)
4110 #define SepiaToneImageTag "SepiaTone/Image"
4129 Initialize sepia-toned image attributes.
4131 assert(image != (const Image *) NULL);
4132 assert(image->signature == MagickSignature);
4133 if (image->debug != MagickFalse)
4134 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4135 assert(exception != (ExceptionInfo *) NULL);
4136 assert(exception->signature == MagickSignature);
4137 sepia_image=CloneImage(image,0,0,MagickTrue,exception);
4138 if (sepia_image == (Image *) NULL)
4139 return((Image *) NULL);
4140 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
4142 sepia_image=DestroyImage(sepia_image);
4143 return((Image *) NULL);
4146 Tone each row of the image.
4150 image_view=AcquireVirtualCacheView(image,exception);
4151 sepia_view=AcquireAuthenticCacheView(sepia_image,exception);
4152 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4153 #pragma omp parallel for schedule(static,4) shared(progress,status) \
4154 dynamic_number_threads(image,image->columns,image->rows,1)
4156 for (y=0; y < (ssize_t) image->rows; y++)
4158 register const Quantum
4167 if (status == MagickFalse)
4169 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
4170 q=GetCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
4172 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
4177 for (x=0; x < (ssize_t) image->columns; x++)
4183 intensity=(MagickRealType) GetPixelIntensity(image,p);
4184 tone=intensity > threshold ? (MagickRealType) QuantumRange : intensity+
4185 (MagickRealType) QuantumRange-threshold;
4186 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
4187 tone=intensity > (7.0*threshold/6.0) ? (MagickRealType) QuantumRange :
4188 intensity+(MagickRealType) QuantumRange-7.0*threshold/6.0;
4189 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4190 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
4191 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
4193 if ((MagickRealType) GetPixelGreen(image,q) < tone)
4194 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4195 if ((MagickRealType) GetPixelBlue(image,q) < tone)
4196 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
4197 p+=GetPixelChannels(image);
4198 q+=GetPixelChannels(sepia_image);
4200 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4202 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4207 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4208 #pragma omp critical (MagickCore_SepiaToneImage)
4210 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4212 if (proceed == MagickFalse)
4216 sepia_view=DestroyCacheView(sepia_view);
4217 image_view=DestroyCacheView(image_view);
4218 (void) NormalizeImage(sepia_image,exception);
4219 (void) ContrastImage(sepia_image,MagickTrue,exception);
4220 if (status == MagickFalse)
4221 sepia_image=DestroyImage(sepia_image);
4222 return(sepia_image);
4226 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4230 % S h a d o w I m a g e %
4234 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4236 % ShadowImage() simulates a shadow from the specified image and returns it.
4238 % The format of the ShadowImage method is:
4240 % Image *ShadowImage(const Image *image,const double alpha,
4241 % const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4242 % ExceptionInfo *exception)
4244 % A description of each parameter follows:
4246 % o image: the image.
4248 % o alpha: percentage transparency.
4250 % o sigma: the standard deviation of the Gaussian, in pixels.
4252 % o x_offset: the shadow x-offset.
4254 % o y_offset: the shadow y-offset.
4256 % o exception: return any errors or warnings in this structure.
4259 MagickExport Image *ShadowImage(const Image *image,const double alpha,
4260 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4261 ExceptionInfo *exception)
4263 #define ShadowImageTag "Shadow/Image"
4285 assert(image != (Image *) NULL);
4286 assert(image->signature == MagickSignature);
4287 if (image->debug != MagickFalse)
4288 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4289 assert(exception != (ExceptionInfo *) NULL);
4290 assert(exception->signature == MagickSignature);
4291 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4292 if (clone_image == (Image *) NULL)
4293 return((Image *) NULL);
4294 if (IsGrayColorspace(image->colorspace) != MagickFalse)
4295 (void) TransformImageColorspace(clone_image,RGBColorspace,exception);
4296 (void) SetImageVirtualPixelMethod(clone_image,TransparentVirtualPixelMethod,
4298 border_info.width=(size_t) floor(2.0*sigma+0.5);
4299 border_info.height=(size_t) floor(2.0*sigma+0.5);
4302 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4304 clone_image->matte=MagickTrue;
4305 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
4306 clone_image=DestroyImage(clone_image);
4307 if (border_image == (Image *) NULL)
4308 return((Image *) NULL);
4309 if (border_image->matte == MagickFalse)
4310 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
4315 image_view=AcquireAuthenticCacheView(border_image,exception);
4316 for (y=0; y < (ssize_t) border_image->rows; y++)
4327 if (status == MagickFalse)
4329 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
4331 if (q == (Quantum *) NULL)
4336 background_color=border_image->background_color;
4337 background_color.matte=MagickTrue;
4338 for (x=0; x < (ssize_t) border_image->columns; x++)
4340 if (border_image->matte != MagickFalse)
4341 background_color.alpha=GetPixelAlpha(border_image,q)*alpha/100.0;
4342 SetPixelInfoPixel(border_image,&background_color,q);
4343 q+=GetPixelChannels(border_image);
4345 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4348 image_view=DestroyCacheView(image_view);
4349 if (status == MagickFalse)
4351 border_image=DestroyImage(border_image);
4352 return((Image *) NULL);
4354 channel_mask=SetPixelChannelMask(border_image,AlphaChannel);
4355 shadow_image=BlurImage(border_image,0.0,sigma,exception);
4356 border_image=DestroyImage(border_image);
4357 if (shadow_image == (Image *) NULL)
4358 return((Image *) NULL);
4359 (void) SetPixelChannelMapMask(shadow_image,channel_mask);
4360 if (shadow_image->page.width == 0)
4361 shadow_image->page.width=shadow_image->columns;
4362 if (shadow_image->page.height == 0)
4363 shadow_image->page.height=shadow_image->rows;
4364 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4365 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4366 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4367 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
4368 return(shadow_image);
4372 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4376 % S k e t c h I m a g e %
4380 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4382 % SketchImage() simulates a pencil sketch. We convolve the image with a
4383 % Gaussian operator of the given radius and standard deviation (sigma). For
4384 % reasonable results, radius should be larger than sigma. Use a radius of 0
4385 % and SketchImage() selects a suitable radius for you. Angle gives the angle
4388 % The format of the SketchImage method is:
4390 % Image *SketchImage(const Image *image,const double radius,
4391 % const double sigma,const double angle,ExceptionInfo *exception)
4393 % A description of each parameter follows:
4395 % o image: the image.
4397 % o radius: the radius of the Gaussian, in pixels, not counting the
4400 % o sigma: the standard deviation of the Gaussian, in pixels.
4402 % o angle: apply the effect along this angle.
4404 % o exception: return any errors or warnings in this structure.
4407 MagickExport Image *SketchImage(const Image *image,const double radius,
4408 const double sigma,const double angle,ExceptionInfo *exception)
4424 **restrict random_info;
4435 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4436 MagickTrue,exception);
4437 if (random_image == (Image *) NULL)
4438 return((Image *) NULL);
4440 random_info=AcquireRandomInfoThreadSet();
4441 key=GetRandomSecretKey(random_info[0]);
4442 random_view=AcquireAuthenticCacheView(random_image,exception);
4443 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4444 #pragma omp parallel for schedule(static,4) shared(status) \
4445 dynamic_number_threads(image,image->columns,image->rows,key == ~0UL)
4447 for (y=0; y < (ssize_t) random_image->rows; y++)
4450 id = GetOpenMPThreadId();
4458 if (status == MagickFalse)
4460 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4462 if (q == (Quantum *) NULL)
4467 for (x=0; x < (ssize_t) random_image->columns; x++)
4475 if (GetPixelMask(random_image,q) != 0)
4477 q+=GetPixelChannels(random_image);
4480 value=GetPseudoRandomValue(random_info[id]);
4481 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4489 channel=GetPixelChannelMapChannel(image,i);
4490 traits=GetPixelChannelMapTraits(image,channel);
4491 if (traits == UndefinedPixelTrait)
4493 q[i]=ClampToQuantum(QuantumRange*value);
4495 q+=GetPixelChannels(random_image);
4497 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4500 random_view=DestroyCacheView(random_view);
4501 random_info=DestroyRandomInfoThreadSet(random_info);
4502 if (status == MagickFalse)
4504 random_image=DestroyImage(random_image);
4505 return(random_image);
4507 blur_image=MotionBlurImage(random_image,radius,sigma,angle,exception);
4508 random_image=DestroyImage(random_image);
4509 if (blur_image == (Image *) NULL)
4510 return((Image *) NULL);
4511 dodge_image=EdgeImage(blur_image,radius,1.0,exception);
4512 blur_image=DestroyImage(blur_image);
4513 if (dodge_image == (Image *) NULL)
4514 return((Image *) NULL);
4515 (void) NormalizeImage(dodge_image,exception);
4516 (void) NegateImage(dodge_image,MagickFalse,exception);
4517 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
4518 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4519 if (sketch_image == (Image *) NULL)
4521 dodge_image=DestroyImage(dodge_image);
4522 return((Image *) NULL);
4524 (void) CompositeImage(sketch_image,dodge_image,ColorDodgeCompositeOp,
4525 MagickTrue,0,0,exception);
4526 dodge_image=DestroyImage(dodge_image);
4527 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4528 if (blend_image == (Image *) NULL)
4530 sketch_image=DestroyImage(sketch_image);
4531 return((Image *) NULL);
4533 (void) SetImageArtifact(blend_image,"compose:args","20x80");
4534 (void) CompositeImage(sketch_image,blend_image,BlendCompositeOp,MagickTrue,
4536 blend_image=DestroyImage(blend_image);
4537 return(sketch_image);
4541 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4545 % S o l a r i z e I m a g e %
4549 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4551 % SolarizeImage() applies a special effect to the image, similar to the effect
4552 % achieved in a photo darkroom by selectively exposing areas of photo
4553 % sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4554 % measure of the extent of the solarization.
4556 % The format of the SolarizeImage method is:
4558 % MagickBooleanType SolarizeImage(Image *image,const double threshold,
4559 % ExceptionInfo *exception)
4561 % A description of each parameter follows:
4563 % o image: the image.
4565 % o threshold: Define the extent of the solarization.
4567 % o exception: return any errors or warnings in this structure.
4570 MagickExport MagickBooleanType SolarizeImage(Image *image,
4571 const double threshold,ExceptionInfo *exception)
4573 #define SolarizeImageTag "Solarize/Image"
4587 assert(image != (Image *) NULL);
4588 assert(image->signature == MagickSignature);
4589 if (image->debug != MagickFalse)
4590 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4591 if (image->storage_class == PseudoClass)
4599 for (i=0; i < (ssize_t) image->colors; i++)
4601 if ((MagickRealType) image->colormap[i].red > threshold)
4602 image->colormap[i].red=QuantumRange-image->colormap[i].red;
4603 if ((MagickRealType) image->colormap[i].green > threshold)
4604 image->colormap[i].green=QuantumRange-
4605 image->colormap[i].green;
4606 if ((MagickRealType) image->colormap[i].blue > threshold)
4607 image->colormap[i].blue=QuantumRange-
4608 image->colormap[i].blue;
4616 image_view=AcquireAuthenticCacheView(image,exception);
4617 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4618 #pragma omp parallel for schedule(static,4) shared(progress,status) \
4619 dynamic_number_threads(image,image->columns,image->rows,1)
4621 for (y=0; y < (ssize_t) image->rows; y++)
4629 if (status == MagickFalse)
4631 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
4632 if (q == (Quantum *) NULL)
4637 for (x=0; x < (ssize_t) image->columns; x++)
4642 if (GetPixelMask(image,q) != 0)
4644 q+=GetPixelChannels(image);
4647 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4655 channel=GetPixelChannelMapChannel(image,i);
4656 traits=GetPixelChannelMapTraits(image,channel);
4657 if ((traits == UndefinedPixelTrait) ||
4658 ((traits & CopyPixelTrait) != 0))
4660 if ((MagickRealType) q[i] > threshold)
4661 q[i]=QuantumRange-q[i];
4663 q+=GetPixelChannels(image);
4665 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4667 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4672 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4673 #pragma omp critical (MagickCore_SolarizeImage)
4675 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4676 if (proceed == MagickFalse)
4680 image_view=DestroyCacheView(image_view);
4685 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4689 % S t e g a n o I m a g e %
4693 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4695 % SteganoImage() hides a digital watermark within the image. Recover
4696 % the hidden watermark later to prove that the authenticity of an image.
4697 % Offset defines the start position within the image to hide the watermark.
4699 % The format of the SteganoImage method is:
4701 % Image *SteganoImage(const Image *image,Image *watermark,
4702 % ExceptionInfo *exception)
4704 % A description of each parameter follows:
4706 % o image: the image.
4708 % o watermark: the watermark image.
4710 % o exception: return any errors or warnings in this structure.
4713 MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4714 ExceptionInfo *exception)
4716 #define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
4717 #define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
4718 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
4719 #define SteganoImageTag "Stegano/Image"
4754 Initialize steganographic image attributes.
4756 assert(image != (const Image *) NULL);
4757 assert(image->signature == MagickSignature);
4758 if (image->debug != MagickFalse)
4759 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4760 assert(watermark != (const Image *) NULL);
4761 assert(watermark->signature == MagickSignature);
4762 assert(exception != (ExceptionInfo *) NULL);
4763 assert(exception->signature == MagickSignature);
4765 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4766 if (stegano_image == (Image *) NULL)
4767 return((Image *) NULL);
4768 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
4769 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
4771 stegano_image=DestroyImage(stegano_image);
4772 return((Image *) NULL);
4775 Hide watermark in low-order bits of image.
4780 depth=stegano_image->depth;
4781 k=stegano_image->offset;
4783 watermark_view=AcquireVirtualCacheView(watermark,exception);
4784 stegano_view=AcquireAuthenticCacheView(stegano_image,exception);
4785 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
4787 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
4789 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
4794 (void) GetOneCacheViewVirtualPixelInfo(watermark_view,x,y,&pixel,
4796 offset=k/(ssize_t) stegano_image->columns;
4797 if (offset >= (ssize_t) stegano_image->rows)
4799 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4800 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4802 if (q == (Quantum *) NULL)
4808 SetPixelRed(stegano_image,SetBit(GetPixelRed(stegano_image,q),j,
4809 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
4814 SetPixelGreen(stegano_image,SetBit(GetPixelGreen(stegano_image,q),j,
4815 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
4820 SetPixelBlue(stegano_image,SetBit(GetPixelBlue(stegano_image,q),j,
4821 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
4825 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
4831 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
4833 if (k == stegano_image->offset)
4837 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4842 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4844 if (proceed == MagickFalse)
4848 stegano_view=DestroyCacheView(stegano_view);
4849 watermark_view=DestroyCacheView(watermark_view);
4850 if (status == MagickFalse)
4852 stegano_image=DestroyImage(stegano_image);
4853 return((Image *) NULL);
4855 return(stegano_image);
4859 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4863 % S t e r e o A n a g l y p h I m a g e %
4867 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4869 % StereoAnaglyphImage() combines two images and produces a single image that
4870 % is the composite of a left and right image of a stereo pair. Special
4871 % red-green stereo glasses are required to view this effect.
4873 % The format of the StereoAnaglyphImage method is:
4875 % Image *StereoImage(const Image *left_image,const Image *right_image,
4876 % ExceptionInfo *exception)
4877 % Image *StereoAnaglyphImage(const Image *left_image,
4878 % const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
4879 % ExceptionInfo *exception)
4881 % A description of each parameter follows:
4883 % o left_image: the left image.
4885 % o right_image: the right image.
4887 % o exception: return any errors or warnings in this structure.
4889 % o x_offset: amount, in pixels, by which the left image is offset to the
4890 % right of the right image.
4892 % o y_offset: amount, in pixels, by which the left image is offset to the
4893 % bottom of the right image.
4897 MagickExport Image *StereoImage(const Image *left_image,
4898 const Image *right_image,ExceptionInfo *exception)
4900 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4903 MagickExport Image *StereoAnaglyphImage(const Image *left_image,
4904 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
4905 ExceptionInfo *exception)
4907 #define StereoImageTag "Stereo/Image"
4921 assert(left_image != (const Image *) NULL);
4922 assert(left_image->signature == MagickSignature);
4923 if (left_image->debug != MagickFalse)
4924 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4925 left_image->filename);
4926 assert(right_image != (const Image *) NULL);
4927 assert(right_image->signature == MagickSignature);
4928 assert(exception != (ExceptionInfo *) NULL);
4929 assert(exception->signature == MagickSignature);
4930 assert(right_image != (const Image *) NULL);
4932 if ((left_image->columns != right_image->columns) ||
4933 (left_image->rows != right_image->rows))
4934 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4936 Initialize stereo image attributes.
4938 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4939 MagickTrue,exception);
4940 if (stereo_image == (Image *) NULL)
4941 return((Image *) NULL);
4942 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
4944 stereo_image=DestroyImage(stereo_image);
4945 return((Image *) NULL);
4947 (void) SetImageColorspace(stereo_image,sRGBColorspace,exception);
4949 Copy left image to red channel and right image to blue channel.
4952 for (y=0; y < (ssize_t) stereo_image->rows; y++)
4954 register const Quantum
4964 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4966 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4967 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
4968 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4969 (r == (Quantum *) NULL))
4971 for (x=0; x < (ssize_t) stereo_image->columns; x++)
4973 SetPixelRed(image,GetPixelRed(left_image,p),r);
4974 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4975 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4976 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4977 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4978 GetPixelAlpha(right_image,q))/2,r);
4979 p+=GetPixelChannels(left_image);
4980 q+=GetPixelChannels(right_image);
4981 r+=GetPixelChannels(stereo_image);
4983 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4985 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4990 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4991 stereo_image->rows);
4992 if (proceed == MagickFalse)
4996 if (status == MagickFalse)
4998 stereo_image=DestroyImage(stereo_image);
4999 return((Image *) NULL);
5001 return(stereo_image);
5005 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5009 % S w i r l I m a g e %
5013 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5015 % SwirlImage() swirls the pixels about the center of the image, where
5016 % degrees indicates the sweep of the arc through which each pixel is moved.
5017 % You get a more dramatic effect as the degrees move from 1 to 360.
5019 % The format of the SwirlImage method is:
5021 % Image *SwirlImage(const Image *image,double degrees,
5022 % const PixelInterpolateMethod method,ExceptionInfo *exception)
5024 % A description of each parameter follows:
5026 % o image: the image.
5028 % o degrees: Define the tightness of the swirling effect.
5030 % o method: the pixel interpolation method.
5032 % o exception: return any errors or warnings in this structure.
5035 MagickExport Image *SwirlImage(const Image *image,double degrees,
5036 const PixelInterpolateMethod method,ExceptionInfo *exception)
5038 #define SwirlImageTag "Swirl/Image"
5064 Initialize swirl image attributes.
5066 assert(image != (const Image *) NULL);
5067 assert(image->signature == MagickSignature);
5068 if (image->debug != MagickFalse)
5069 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5070 assert(exception != (ExceptionInfo *) NULL);
5071 assert(exception->signature == MagickSignature);
5072 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5073 if (swirl_image == (Image *) NULL)
5074 return((Image *) NULL);
5075 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
5077 swirl_image=DestroyImage(swirl_image);
5078 return((Image *) NULL);
5080 if (swirl_image->background_color.alpha != OpaqueAlpha)
5081 swirl_image->matte=MagickTrue;
5083 Compute scaling factor.
5085 center.x=(double) image->columns/2.0;
5086 center.y=(double) image->rows/2.0;
5087 radius=MagickMax(center.x,center.y);
5090 if (image->columns > image->rows)
5091 scale.y=(double) image->columns/(double) image->rows;
5093 if (image->columns < image->rows)
5094 scale.x=(double) image->rows/(double) image->columns;
5095 degrees=(double) DegreesToRadians(degrees);
5101 image_view=AcquireVirtualCacheView(image,exception);
5102 swirl_view=AcquireAuthenticCacheView(swirl_image,exception);
5103 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5104 #pragma omp parallel for schedule(static,4) shared(progress,status) \
5105 dynamic_number_threads(image,image->columns,image->rows,1)
5107 for (y=0; y < (ssize_t) image->rows; y++)
5115 register const Quantum
5124 if (status == MagickFalse)
5126 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
5127 q=QueueCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
5129 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
5134 delta.y=scale.y*(double) (y-center.y);
5135 for (x=0; x < (ssize_t) image->columns; x++)
5138 Determine if the pixel is within an ellipse.
5140 if (GetPixelMask(image,p) != 0)
5142 p+=GetPixelChannels(image);
5143 q+=GetPixelChannels(swirl_image);
5146 delta.x=scale.x*(double) (x-center.x);
5147 distance=delta.x*delta.x+delta.y*delta.y;
5148 if (distance >= (radius*radius))
5153 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5162 channel=GetPixelChannelMapChannel(image,i);
5163 traits=GetPixelChannelMapTraits(image,channel);
5164 swirl_traits=GetPixelChannelMapTraits(swirl_image,channel);
5165 if ((traits == UndefinedPixelTrait) ||
5166 (swirl_traits == UndefinedPixelTrait))
5168 SetPixelChannel(swirl_image,channel,p[i],q);
5181 factor=1.0-sqrt((double) distance)/radius;
5182 sine=sin((double) (degrees*factor*factor));
5183 cosine=cos((double) (degrees*factor*factor));
5184 status=InterpolatePixelChannels(image,image_view,swirl_image,method,
5185 ((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5186 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
5188 p+=GetPixelChannels(image);
5189 q+=GetPixelChannels(swirl_image);
5191 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5193 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5198 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5199 #pragma omp critical (MagickCore_SwirlImage)
5201 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5202 if (proceed == MagickFalse)
5206 swirl_view=DestroyCacheView(swirl_view);
5207 image_view=DestroyCacheView(image_view);
5208 if (status == MagickFalse)
5209 swirl_image=DestroyImage(swirl_image);
5210 return(swirl_image);
5214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5218 % T i n t I m a g e %
5222 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5224 % TintImage() applies a color vector to each pixel in the image. The length
5225 % of the vector is 0 for black and white and at its maximum for the midtones.
5226 % The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5228 % The format of the TintImage method is:
5230 % Image *TintImage(const Image *image,const char *blend,
5231 % const PixelInfo *tint,ExceptionInfo *exception)
5233 % A description of each parameter follows:
5235 % o image: the image.
5237 % o blend: A color value used for tinting.
5239 % o tint: A color value used for tinting.
5241 % o exception: return any errors or warnings in this structure.
5244 MagickExport Image *TintImage(const Image *image,const char *blend,
5245 const PixelInfo *tint,ExceptionInfo *exception)
5247 #define TintImageTag "Tint/Image"
5278 Allocate tint image.
5280 assert(image != (const Image *) NULL);
5281 assert(image->signature == MagickSignature);
5282 if (image->debug != MagickFalse)
5283 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5284 assert(exception != (ExceptionInfo *) NULL);
5285 assert(exception->signature == MagickSignature);
5286 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5287 if (tint_image == (Image *) NULL)
5288 return((Image *) NULL);
5289 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
5291 tint_image=DestroyImage(tint_image);
5292 return((Image *) NULL);
5294 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
5295 (IsPixelInfoGray(tint) == MagickFalse))
5296 (void) SetImageColorspace(tint_image,RGBColorspace,exception);
5297 if (blend == (const char *) NULL)
5300 Determine RGB values of the color.
5302 GetPixelInfo(image,&color_vector);
5303 flags=ParseGeometry(blend,&geometry_info);
5304 color_vector.red=geometry_info.rho;
5305 color_vector.green=geometry_info.rho;
5306 color_vector.blue=geometry_info.rho;
5307 color_vector.alpha=OpaqueAlpha;
5308 if ((flags & SigmaValue) != 0)
5309 color_vector.green=geometry_info.sigma;
5310 if ((flags & XiValue) != 0)
5311 color_vector.blue=geometry_info.xi;
5312 if ((flags & PsiValue) != 0)
5313 color_vector.alpha=geometry_info.psi;
5314 if (image->colorspace == CMYKColorspace)
5316 color_vector.black=geometry_info.rho;
5317 if ((flags & PsiValue) != 0)
5318 color_vector.black=geometry_info.psi;
5319 if ((flags & ChiValue) != 0)
5320 color_vector.alpha=geometry_info.chi;
5322 intensity=(MagickRealType) GetPixelInfoIntensity(tint);
5323 color_vector.red=(MagickRealType) (color_vector.red*tint->red/100.0-
5325 color_vector.green=(MagickRealType) (color_vector.green*tint->green/100.0-
5327 color_vector.blue=(MagickRealType) (color_vector.blue*tint->blue/100.0-
5329 color_vector.black=(MagickRealType) (color_vector.black*tint->black/100.0-
5331 color_vector.alpha=(MagickRealType) (color_vector.alpha*tint->alpha/100.0-
5338 image_view=AcquireVirtualCacheView(image,exception);
5339 tint_view=AcquireAuthenticCacheView(tint_image,exception);
5340 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5341 #pragma omp parallel for schedule(static,4) shared(progress,status) \
5342 dynamic_number_threads(image,image->columns,image->rows,1)
5344 for (y=0; y < (ssize_t) image->rows; y++)
5346 register const Quantum
5355 if (status == MagickFalse)
5357 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5358 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5360 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
5365 for (x=0; x < (ssize_t) image->columns; x++)
5376 if (GetPixelMask(image,p) != 0)
5378 p+=GetPixelChannels(image);
5379 q+=GetPixelChannels(tint_image);
5382 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5391 channel=GetPixelChannelMapChannel(image,i);
5392 traits=GetPixelChannelMapTraits(image,channel);
5393 tint_traits=GetPixelChannelMapTraits(tint_image,channel);
5394 if ((traits == UndefinedPixelTrait) ||
5395 (tint_traits == UndefinedPixelTrait))
5397 if ((tint_traits & CopyPixelTrait) != 0)
5399 SetPixelChannel(tint_image,channel,p[i],q);
5403 GetPixelInfo(image,&pixel);
5404 weight=QuantumScale*GetPixelRed(image,p)-0.5;
5405 pixel.red=(MagickRealType) GetPixelRed(image,p)+color_vector.red*
5406 (1.0-(4.0*(weight*weight)));
5407 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
5408 pixel.green=(MagickRealType) GetPixelGreen(image,p)+color_vector.green*
5409 (1.0-(4.0*(weight*weight)));
5410 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
5411 pixel.blue=(MagickRealType) GetPixelBlue(image,p)+color_vector.blue*
5412 (1.0-(4.0*(weight*weight)));
5413 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
5414 pixel.black=(MagickRealType) GetPixelBlack(image,p)+color_vector.black*
5415 (1.0-(4.0*(weight*weight)));
5416 SetPixelInfoPixel(tint_image,&pixel,q);
5417 p+=GetPixelChannels(image);
5418 q+=GetPixelChannels(tint_image);
5420 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5422 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5427 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5428 #pragma omp critical (MagickCore_TintImage)
5430 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5431 if (proceed == MagickFalse)
5435 tint_view=DestroyCacheView(tint_view);
5436 image_view=DestroyCacheView(image_view);
5437 if (status == MagickFalse)
5438 tint_image=DestroyImage(tint_image);
5443 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5447 % V i g n e t t e I m a g e %
5451 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5453 % VignetteImage() softens the edges of the image in vignette style.
5455 % The format of the VignetteImage method is:
5457 % Image *VignetteImage(const Image *image,const double radius,
5458 % const double sigma,const ssize_t x,const ssize_t y,
5459 % ExceptionInfo *exception)
5461 % A description of each parameter follows:
5463 % o image: the image.
5465 % o radius: the radius of the pixel neighborhood.
5467 % o sigma: the standard deviation of the Gaussian, in pixels.
5469 % o x, y: Define the x and y ellipse offset.
5471 % o exception: return any errors or warnings in this structure.
5474 MagickExport Image *VignetteImage(const Image *image,const double radius,
5475 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
5478 ellipse[MaxTextExtent];
5489 assert(image != (Image *) NULL);
5490 assert(image->signature == MagickSignature);
5491 if (image->debug != MagickFalse)
5492 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5493 assert(exception != (ExceptionInfo *) NULL);
5494 assert(exception->signature == MagickSignature);
5495 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5496 if (canvas_image == (Image *) NULL)
5497 return((Image *) NULL);
5498 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
5500 canvas_image=DestroyImage(canvas_image);
5501 return((Image *) NULL);
5503 canvas_image->matte=MagickTrue;
5504 oval_image=CloneImage(canvas_image,canvas_image->columns,canvas_image->rows,
5505 MagickTrue,exception);
5506 if (oval_image == (Image *) NULL)
5508 canvas_image=DestroyImage(canvas_image);
5509 return((Image *) NULL);
5511 (void) QueryColorCompliance("#000000",AllCompliance,
5512 &oval_image->background_color,exception);
5513 (void) SetImageBackgroundColor(oval_image,exception);
5514 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
5515 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5517 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5519 (void) FormatLocaleString(ellipse,MaxTextExtent,"ellipse %g,%g,%g,%g,"
5520 "0.0,360.0",image->columns/2.0,image->rows/2.0,image->columns/2.0-x,
5522 draw_info->primitive=AcquireString(ellipse);
5523 (void) DrawImage(oval_image,draw_info,exception);
5524 draw_info=DestroyDrawInfo(draw_info);
5525 blur_image=BlurImage(oval_image,radius,sigma,exception);
5526 oval_image=DestroyImage(oval_image);
5527 if (blur_image == (Image *) NULL)
5529 canvas_image=DestroyImage(canvas_image);
5530 return((Image *) NULL);
5532 blur_image->matte=MagickFalse;
5533 (void) CompositeImage(canvas_image,blur_image,IntensityCompositeOp,MagickTrue,
5535 blur_image=DestroyImage(blur_image);
5536 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5537 canvas_image=DestroyImage(canvas_image);
5538 if (vignette_image != (Image *) NULL)
5539 (void) TransformImageColorspace(vignette_image,image->colorspace,exception);
5540 return(vignette_image);
5544 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5548 % W a v e I m a g e %
5552 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5554 % WaveImage() creates a "ripple" effect in the image by shifting the pixels
5555 % vertically along a sine wave whose amplitude and wavelength is specified
5556 % by the given parameters.
5558 % The format of the WaveImage method is:
5560 % Image *WaveImage(const Image *image,const double amplitude,
5561 % const double wave_length,const PixelInterpolateMethod method,
5562 % ExceptionInfo *exception)
5564 % A description of each parameter follows:
5566 % o image: the image.
5568 % o amplitude, wave_length: Define the amplitude and wave length of the
5571 % o interpolate: the pixel interpolation method.
5573 % o exception: return any errors or warnings in this structure.
5576 MagickExport Image *WaveImage(const Image *image,const double amplitude,
5577 const double wave_length,const PixelInterpolateMethod method,
5578 ExceptionInfo *exception)
5580 #define WaveImageTag "Wave/Image"
5605 Initialize wave image attributes.
5607 assert(image != (Image *) NULL);
5608 assert(image->signature == MagickSignature);
5609 if (image->debug != MagickFalse)
5610 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5611 assert(exception != (ExceptionInfo *) NULL);
5612 assert(exception->signature == MagickSignature);
5613 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
5614 fabs(amplitude)),MagickTrue,exception);
5615 if (wave_image == (Image *) NULL)
5616 return((Image *) NULL);
5617 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
5619 wave_image=DestroyImage(wave_image);
5620 return((Image *) NULL);
5622 if (wave_image->background_color.alpha != OpaqueAlpha)
5623 wave_image->matte=MagickTrue;
5627 sine_map=(MagickRealType *) AcquireQuantumMemory((size_t) wave_image->columns,
5629 if (sine_map == (MagickRealType *) NULL)
5631 wave_image=DestroyImage(wave_image);
5632 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5634 for (i=0; i < (ssize_t) wave_image->columns; i++)
5635 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5642 image_view=AcquireVirtualCacheView(image,exception);
5643 wave_view=AcquireAuthenticCacheView(wave_image,exception);
5644 (void) SetCacheViewVirtualPixelMethod(image_view,
5645 BackgroundVirtualPixelMethod);
5646 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5647 #pragma omp parallel for schedule(static,4) shared(progress,status) \
5648 dynamic_number_threads(image,image->columns,image->rows,1)
5650 for (y=0; y < (ssize_t) wave_image->rows; y++)
5658 if (status == MagickFalse)
5660 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5662 if (q == (Quantum *) NULL)
5667 for (x=0; x < (ssize_t) wave_image->columns; x++)
5669 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5670 (double) x,(double) (y-sine_map[x]),q,exception);
5671 q+=GetPixelChannels(wave_image);
5673 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5675 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5680 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5681 #pragma omp critical (MagickCore_WaveImage)
5683 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5684 if (proceed == MagickFalse)
5688 wave_view=DestroyCacheView(wave_view);
5689 image_view=DestroyCacheView(image_view);
5690 sine_map=(MagickRealType *) RelinquishMagickMemory(sine_map);
5691 if (status == MagickFalse)
5692 wave_image=DestroyImage(wave_image);