2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % MagickCore Image Special Effects Methods %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/annotate.h"
45 #include "MagickCore/artifact.h"
46 #include "MagickCore/attribute.h"
47 #include "MagickCore/cache.h"
48 #include "MagickCore/cache-view.h"
49 #include "MagickCore/color.h"
50 #include "MagickCore/color-private.h"
51 #include "MagickCore/colorspace-private.h"
52 #include "MagickCore/composite.h"
53 #include "MagickCore/decorate.h"
54 #include "MagickCore/distort.h"
55 #include "MagickCore/draw.h"
56 #include "MagickCore/effect.h"
57 #include "MagickCore/enhance.h"
58 #include "MagickCore/exception.h"
59 #include "MagickCore/exception-private.h"
60 #include "MagickCore/fx.h"
61 #include "MagickCore/fx-private.h"
62 #include "MagickCore/gem.h"
63 #include "MagickCore/gem-private.h"
64 #include "MagickCore/geometry.h"
65 #include "MagickCore/layer.h"
66 #include "MagickCore/list.h"
67 #include "MagickCore/log.h"
68 #include "MagickCore/image.h"
69 #include "MagickCore/image-private.h"
70 #include "MagickCore/magick.h"
71 #include "MagickCore/memory_.h"
72 #include "MagickCore/monitor.h"
73 #include "MagickCore/monitor-private.h"
74 #include "MagickCore/option.h"
75 #include "MagickCore/pixel.h"
76 #include "MagickCore/pixel-accessor.h"
77 #include "MagickCore/property.h"
78 #include "MagickCore/quantum.h"
79 #include "MagickCore/quantum-private.h"
80 #include "MagickCore/random_.h"
81 #include "MagickCore/random-private.h"
82 #include "MagickCore/resample.h"
83 #include "MagickCore/resample-private.h"
84 #include "MagickCore/resize.h"
85 #include "MagickCore/splay-tree.h"
86 #include "MagickCore/statistic.h"
87 #include "MagickCore/string_.h"
88 #include "MagickCore/string-private.h"
89 #include "MagickCore/thread-private.h"
90 #include "MagickCore/transform.h"
91 #include "MagickCore/utility.h"
96 #define LeftShiftOperator 0xf5
97 #define RightShiftOperator 0xf6
98 #define LessThanEqualOperator 0xf7
99 #define GreaterThanEqualOperator 0xf8
100 #define EqualOperator 0xf9
101 #define NotEqualOperator 0xfa
102 #define LogicalAndOperator 0xfb
103 #define LogicalOrOperator 0xfc
104 #define ExponentialNotation 0xfd
132 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
136 + A c q u i r e F x I n f o %
140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142 % AcquireFxInfo() allocates the FxInfo structure.
144 % The format of the AcquireFxInfo method is:
146 % FxInfo *AcquireFxInfo(Image *image,const char *expression)
148 % A description of each parameter follows:
150 % o image: the image.
152 % o expression: the expression.
155 MagickPrivate FxInfo *AcquireFxInfo(const Image *image,const char *expression)
169 fx_info=(FxInfo *) AcquireMagickMemory(sizeof(*fx_info));
170 if (fx_info == (FxInfo *) NULL)
171 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
172 (void) ResetMagickMemory(fx_info,0,sizeof(*fx_info));
173 fx_info->exception=AcquireExceptionInfo();
174 fx_info->images=image;
175 fx_info->colors=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
176 RelinquishMagickMemory);
177 fx_info->symbols=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
178 RelinquishMagickMemory);
179 fx_info->view=(CacheView **) AcquireQuantumMemory(GetImageListLength(
180 fx_info->images),sizeof(*fx_info->view));
181 if (fx_info->view == (CacheView **) NULL)
182 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
184 next=GetFirstImageInList(fx_info->images);
185 for ( ; next != (Image *) NULL; next=next->next)
187 fx_info->view[i]=AcquireCacheView(next);
190 fx_info->random_info=AcquireRandomInfo();
191 fx_info->expression=ConstantString(expression);
192 fx_info->file=stderr;
193 (void) SubstituteString(&fx_info->expression," ",""); /* compact string */
195 Force right-to-left associativity for unary negation.
197 (void) SubstituteString(&fx_info->expression,"-","-1.0*");
199 Convert complex to simple operators.
202 *fx_op=(char) LeftShiftOperator;
203 (void) SubstituteString(&fx_info->expression,"<<",fx_op);
204 *fx_op=(char) RightShiftOperator;
205 (void) SubstituteString(&fx_info->expression,">>",fx_op);
206 *fx_op=(char) LessThanEqualOperator;
207 (void) SubstituteString(&fx_info->expression,"<=",fx_op);
208 *fx_op=(char) GreaterThanEqualOperator;
209 (void) SubstituteString(&fx_info->expression,">=",fx_op);
210 *fx_op=(char) EqualOperator;
211 (void) SubstituteString(&fx_info->expression,"==",fx_op);
212 *fx_op=(char) NotEqualOperator;
213 (void) SubstituteString(&fx_info->expression,"!=",fx_op);
214 *fx_op=(char) LogicalAndOperator;
215 (void) SubstituteString(&fx_info->expression,"&&",fx_op);
216 *fx_op=(char) LogicalOrOperator;
217 (void) SubstituteString(&fx_info->expression,"||",fx_op);
218 *fx_op=(char) ExponentialNotation;
219 (void) SubstituteString(&fx_info->expression,"**",fx_op);
224 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
228 % A d d N o i s e I m a g e %
232 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234 % AddNoiseImage() adds random noise to the image.
236 % The format of the AddNoiseImage method is:
238 % Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
239 % const double attenuate,ExceptionInfo *exception)
241 % A description of each parameter follows:
243 % o image: the image.
245 % o channel: the channel type.
247 % o noise_type: The type of noise: Uniform, Gaussian, Multiplicative,
248 % Impulse, Laplacian, or Poisson.
250 % o attenuate: attenuate the random distribution.
252 % o exception: return any errors or warnings in this structure.
255 MagickExport Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
256 const double attenuate,ExceptionInfo *exception)
258 #define AddNoiseImageTag "AddNoise/Image"
275 **restrict random_info;
281 Initialize noise image attributes.
283 assert(image != (const Image *) NULL);
284 assert(image->signature == MagickSignature);
285 if (image->debug != MagickFalse)
286 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
287 assert(exception != (ExceptionInfo *) NULL);
288 assert(exception->signature == MagickSignature);
289 noise_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
290 if (noise_image == (Image *) NULL)
291 return((Image *) NULL);
292 if (SetImageStorageClass(noise_image,DirectClass,exception) == MagickFalse)
294 noise_image=DestroyImage(noise_image);
295 return((Image *) NULL);
298 Add noise in each row.
302 random_info=AcquireRandomInfoThreadSet();
303 concurrent=GetRandomSecretKey(random_info[0]) == ~0UL ? MagickTrue :
305 image_view=AcquireCacheView(image);
306 noise_view=AcquireCacheView(noise_image);
307 #if defined(MAGICKCORE_OPENMP_SUPPORT)
308 #pragma omp parallel for schedule(static,4) shared(progress,status) omp_concurrent(concurrent)
310 for (y=0; y < (ssize_t) image->rows; y++)
313 id = GetOpenMPThreadId();
318 register const Quantum
327 if (status == MagickFalse)
329 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
330 q=QueueCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
332 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
337 for (x=0; x < (ssize_t) image->columns; x++)
342 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
351 channel=GetPixelChannelMapChannel(image,i);
352 traits=GetPixelChannelMapTraits(image,channel);
353 noise_traits=GetPixelChannelMapTraits(noise_image,channel);
354 if ((traits == UndefinedPixelTrait) ||
355 (noise_traits == UndefinedPixelTrait))
357 if (((noise_traits & CopyPixelTrait) != 0) ||
358 (GetPixelMask(image,p) != 0))
360 SetPixelChannel(noise_image,channel,p[i],q);
363 SetPixelChannel(noise_image,channel,ClampToQuantum(
364 GenerateDifferentialNoise(random_info[id],p[i],noise_type,attenuate)),
367 p+=GetPixelChannels(image);
368 q+=GetPixelChannels(noise_image);
370 sync=SyncCacheViewAuthenticPixels(noise_view,exception);
371 if (sync == MagickFalse)
373 if (image->progress_monitor != (MagickProgressMonitor) NULL)
378 #if defined(MAGICKCORE_OPENMP_SUPPORT)
379 #pragma omp critical (MagickCore_AddNoiseImage)
381 proceed=SetImageProgress(image,AddNoiseImageTag,progress++,
383 if (proceed == MagickFalse)
387 noise_view=DestroyCacheView(noise_view);
388 image_view=DestroyCacheView(image_view);
389 random_info=DestroyRandomInfoThreadSet(random_info);
390 if (status == MagickFalse)
391 noise_image=DestroyImage(noise_image);
396 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
400 % B l u e S h i f t I m a g e %
404 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
406 % BlueShiftImage() mutes the colors of the image to simulate a scene at
407 % nighttime in the moonlight.
409 % The format of the BlueShiftImage method is:
411 % Image *BlueShiftImage(const Image *image,const double factor,
412 % ExceptionInfo *exception)
414 % A description of each parameter follows:
416 % o image: the image.
418 % o factor: the shift factor.
420 % o exception: return any errors or warnings in this structure.
423 MagickExport Image *BlueShiftImage(const Image *image,const double factor,
424 ExceptionInfo *exception)
426 #define BlueShiftImageTag "BlueShift/Image"
445 Allocate blue shift image.
447 assert(image != (const Image *) NULL);
448 assert(image->signature == MagickSignature);
449 if (image->debug != MagickFalse)
450 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
451 assert(exception != (ExceptionInfo *) NULL);
452 assert(exception->signature == MagickSignature);
453 shift_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
454 if (shift_image == (Image *) NULL)
455 return((Image *) NULL);
456 if (SetImageStorageClass(shift_image,DirectClass,exception) == MagickFalse)
458 shift_image=DestroyImage(shift_image);
459 return((Image *) NULL);
462 Blue-shift DirectClass image.
466 image_view=AcquireCacheView(image);
467 shift_view=AcquireCacheView(shift_image);
468 #if defined(MAGICKCORE_OPENMP_SUPPORT)
469 #pragma omp parallel for schedule(static,4) shared(progress,status)
471 for (y=0; y < (ssize_t) image->rows; y++)
482 register const Quantum
491 if (status == MagickFalse)
493 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
494 q=QueueCacheViewAuthenticPixels(shift_view,0,y,shift_image->columns,1,
496 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
501 for (x=0; x < (ssize_t) image->columns; x++)
503 quantum=GetPixelRed(image,p);
504 if (GetPixelGreen(image,p) < quantum)
505 quantum=GetPixelGreen(image,p);
506 if (GetPixelBlue(image,p) < quantum)
507 quantum=GetPixelBlue(image,p);
508 pixel.red=0.5*(GetPixelRed(image,p)+factor*quantum);
509 pixel.green=0.5*(GetPixelGreen(image,p)+factor*quantum);
510 pixel.blue=0.5*(GetPixelBlue(image,p)+factor*quantum);
511 quantum=GetPixelRed(image,p);
512 if (GetPixelGreen(image,p) > quantum)
513 quantum=GetPixelGreen(image,p);
514 if (GetPixelBlue(image,p) > quantum)
515 quantum=GetPixelBlue(image,p);
516 pixel.red=0.5*(pixel.red+factor*quantum);
517 pixel.green=0.5*(pixel.green+factor*quantum);
518 pixel.blue=0.5*(pixel.blue+factor*quantum);
519 SetPixelRed(shift_image,ClampToQuantum(pixel.red),q);
520 SetPixelGreen(shift_image,ClampToQuantum(pixel.green),q);
521 SetPixelBlue(shift_image,ClampToQuantum(pixel.blue),q);
522 p+=GetPixelChannels(image);
523 q+=GetPixelChannels(shift_image);
525 sync=SyncCacheViewAuthenticPixels(shift_view,exception);
526 if (sync == MagickFalse)
528 if (image->progress_monitor != (MagickProgressMonitor) NULL)
533 #if defined(MAGICKCORE_OPENMP_SUPPORT)
534 #pragma omp critical (MagickCore_BlueShiftImage)
536 proceed=SetImageProgress(image,BlueShiftImageTag,progress++,
538 if (proceed == MagickFalse)
542 image_view=DestroyCacheView(image_view);
543 shift_view=DestroyCacheView(shift_view);
544 if (status == MagickFalse)
545 shift_image=DestroyImage(shift_image);
550 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
554 % C h a r c o a l I m a g e %
558 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
560 % CharcoalImage() creates a new image that is a copy of an existing one with
561 % the edge highlighted. It allocates the memory necessary for the new Image
562 % structure and returns a pointer to the new image.
564 % The format of the CharcoalImage method is:
566 % Image *CharcoalImage(const Image *image,const double radius,
567 % const double sigma,ExceptionInfo *exception)
569 % A description of each parameter follows:
571 % o image: the image.
573 % o radius: the radius of the pixel neighborhood.
575 % o sigma: the standard deviation of the Gaussian, in pixels.
577 % o exception: return any errors or warnings in this structure.
580 MagickExport Image *CharcoalImage(const Image *image,const double radius,
581 const double sigma,ExceptionInfo *exception)
588 assert(image != (Image *) NULL);
589 assert(image->signature == MagickSignature);
590 if (image->debug != MagickFalse)
591 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
592 assert(exception != (ExceptionInfo *) NULL);
593 assert(exception->signature == MagickSignature);
594 clone_image=CloneImage(image,0,0,MagickTrue,exception);
595 if (clone_image == (Image *) NULL)
596 return((Image *) NULL);
597 (void) SetImageType(clone_image,GrayscaleType,exception);
598 edge_image=EdgeImage(clone_image,radius,sigma,exception);
599 clone_image=DestroyImage(clone_image);
600 if (edge_image == (Image *) NULL)
601 return((Image *) NULL);
602 charcoal_image=BlurImage(edge_image,radius,sigma,exception);
603 edge_image=DestroyImage(edge_image);
604 if (charcoal_image == (Image *) NULL)
605 return((Image *) NULL);
606 (void) NormalizeImage(charcoal_image,exception);
607 (void) NegateImage(charcoal_image,MagickFalse,exception);
608 (void) SetImageType(charcoal_image,GrayscaleType,exception);
609 return(charcoal_image);
613 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
617 % C o l o r i z e I m a g e %
621 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
623 % ColorizeImage() blends the fill color with each pixel in the image.
624 % A percentage blend is specified with opacity. Control the application
625 % of different color components by specifying a different percentage for
626 % each component (e.g. 90/100/10 is 90% red, 100% green, and 10% blue).
628 % The format of the ColorizeImage method is:
630 % Image *ColorizeImage(const Image *image,const char *blend,
631 % const PixelInfo *colorize,ExceptionInfo *exception)
633 % A description of each parameter follows:
635 % o image: the image.
637 % o blend: A character string indicating the level of blending as a
640 % o colorize: A color value.
642 % o exception: return any errors or warnings in this structure.
645 MagickExport Image *ColorizeImage(const Image *image,const char *blend,
646 const PixelInfo *colorize,ExceptionInfo *exception)
648 #define ColorizeImageTag "Colorize/Image"
649 #define Colorize(pixel,blend_percentage,colorize) \
650 (pixel)=((pixel)*(100.0-(blend_percentage))+(colorize)*(blend_percentage))/100.0;
678 Allocate colorized image.
680 assert(image != (const Image *) NULL);
681 assert(image->signature == MagickSignature);
682 if (image->debug != MagickFalse)
683 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
684 assert(exception != (ExceptionInfo *) NULL);
685 assert(exception->signature == MagickSignature);
686 colorize_image=CloneImage(image,image->columns,image->rows,MagickTrue,
688 if (colorize_image == (Image *) NULL)
689 return((Image *) NULL);
690 if (SetImageStorageClass(colorize_image,DirectClass,exception) == MagickFalse)
692 colorize_image=DestroyImage(colorize_image);
693 return((Image *) NULL);
695 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
696 (IsPixelInfoGray(colorize) != MagickFalse))
697 (void) SetImageColorspace(colorize_image,sRGBColorspace,exception);
698 if ((colorize_image->matte == MagickFalse) &&
699 (colorize->matte != MagickFalse))
700 (void) SetImageAlpha(colorize_image,OpaqueAlpha,exception);
701 if (blend == (const char *) NULL)
702 return(colorize_image);
703 GetPixelInfo(image,&blend_percentage);
704 flags=ParseGeometry(blend,&geometry_info);
705 blend_percentage.red=geometry_info.rho;
706 blend_percentage.green=geometry_info.rho;
707 blend_percentage.blue=geometry_info.rho;
708 blend_percentage.black=geometry_info.rho;
709 blend_percentage.alpha=100.0;
710 if ((flags & SigmaValue) != 0)
711 blend_percentage.green=geometry_info.sigma;
712 if ((flags & XiValue) != 0)
713 blend_percentage.blue=geometry_info.xi;
714 if ((flags & PsiValue) != 0)
715 blend_percentage.alpha=geometry_info.psi;
716 if (blend_percentage.colorspace == CMYKColorspace)
718 if ((flags & PsiValue) != 0)
719 blend_percentage.black=geometry_info.psi;
720 if ((flags & ChiValue) != 0)
721 blend_percentage.alpha=geometry_info.chi;
724 Colorize DirectClass image.
728 image_view=AcquireCacheView(image);
729 colorize_view=AcquireCacheView(colorize_image);
730 #if defined(MAGICKCORE_OPENMP_SUPPORT)
731 #pragma omp parallel for schedule(static,4) shared(progress,status)
733 for (y=0; y < (ssize_t) image->rows; y++)
741 register const Quantum
750 if (status == MagickFalse)
752 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
753 q=QueueCacheViewAuthenticPixels(colorize_view,0,y,colorize_image->columns,1,
755 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
760 GetPixelInfo(colorize_image,&pixel);
761 for (x=0; x < (ssize_t) image->columns; x++)
763 if (GetPixelMask(colorize_image,q) != 0)
765 p+=GetPixelChannels(image);
766 q+=GetPixelChannels(colorize_image);
769 GetPixelInfoPixel(image,p,&pixel);
770 Colorize(pixel.red,blend_percentage.red,colorize->red);
771 Colorize(pixel.green,blend_percentage.green,colorize->green);
772 Colorize(pixel.blue,blend_percentage.blue,colorize->blue);
773 Colorize(pixel.black,blend_percentage.black,colorize->black);
774 Colorize(pixel.alpha,blend_percentage.alpha,colorize->alpha);
775 SetPixelInfoPixel(colorize_image,&pixel,q);
776 p+=GetPixelChannels(image);
777 q+=GetPixelChannels(colorize_image);
779 sync=SyncCacheViewAuthenticPixels(colorize_view,exception);
780 if (sync == MagickFalse)
782 if (image->progress_monitor != (MagickProgressMonitor) NULL)
787 #if defined(MAGICKCORE_OPENMP_SUPPORT)
788 #pragma omp critical (MagickCore_ColorizeImage)
790 proceed=SetImageProgress(image,ColorizeImageTag,progress++,image->rows);
791 if (proceed == MagickFalse)
795 image_view=DestroyCacheView(image_view);
796 colorize_view=DestroyCacheView(colorize_view);
797 if (status == MagickFalse)
798 colorize_image=DestroyImage(colorize_image);
799 return(colorize_image);
803 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
807 % C o l o r M a t r i x I m a g e %
811 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
813 % ColorMatrixImage() applies color transformation to an image. This method
814 % permits saturation changes, hue rotation, luminance to alpha, and various
815 % other effects. Although variable-sized transformation matrices can be used,
816 % typically one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA
817 % (or RGBA with offsets). The matrix is similar to those used by Adobe Flash
818 % except offsets are in column 6 rather than 5 (in support of CMYKA images)
819 % and offsets are normalized (divide Flash offset by 255).
821 % The format of the ColorMatrixImage method is:
823 % Image *ColorMatrixImage(const Image *image,
824 % const KernelInfo *color_matrix,ExceptionInfo *exception)
826 % A description of each parameter follows:
828 % o image: the image.
830 % o color_matrix: the color matrix.
832 % o exception: return any errors or warnings in this structure.
835 /* FUTURE: modify to make use of a MagickMatrix Mutliply function
836 That should be provided in "matrix.c"
837 (ASIDE: actually distorts should do this too but currently doesn't)
840 MagickExport Image *ColorMatrixImage(const Image *image,
841 const KernelInfo *color_matrix,ExceptionInfo *exception)
843 #define ColorMatrixImageTag "ColorMatrix/Image"
852 { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
853 { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
854 { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
855 { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
856 { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
857 { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }
878 Map given color_matrix, into a 6x6 matrix RGBKA and a constant
880 assert(image != (Image *) NULL);
881 assert(image->signature == MagickSignature);
882 if (image->debug != MagickFalse)
883 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
884 assert(exception != (ExceptionInfo *) NULL);
885 assert(exception->signature == MagickSignature);
887 for (v=0; v < (ssize_t) color_matrix->height; v++)
888 for (u=0; u < (ssize_t) color_matrix->width; u++)
890 if ((v < 6) && (u < 6))
891 ColorMatrix[v][u]=color_matrix->values[i];
895 Initialize color image.
897 color_image=CloneImage(image,0,0,MagickTrue,exception);
898 if (color_image == (Image *) NULL)
899 return((Image *) NULL);
900 if (SetImageStorageClass(color_image,DirectClass,exception) == MagickFalse)
902 color_image=DestroyImage(color_image);
903 return((Image *) NULL);
905 if (image->debug != MagickFalse)
908 format[MaxTextExtent],
911 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
912 " ColorMatrix image with color matrix:");
913 message=AcquireString("");
914 for (v=0; v < 6; v++)
917 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
918 (void) ConcatenateString(&message,format);
919 for (u=0; u < 6; u++)
921 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",
923 (void) ConcatenateString(&message,format);
925 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
927 message=DestroyString(message);
930 Apply the ColorMatrix to image.
934 image_view=AcquireCacheView(image);
935 color_view=AcquireCacheView(color_image);
936 #if defined(MAGICKCORE_OPENMP_SUPPORT)
937 #pragma omp parallel for schedule(static,4) shared(progress,status)
939 for (y=0; y < (ssize_t) image->rows; y++)
944 register const Quantum
953 if (status == MagickFalse)
955 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
956 q=GetCacheViewAuthenticPixels(color_view,0,y,color_image->columns,1,
958 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
963 GetPixelInfo(image,&pixel);
964 for (x=0; x < (ssize_t) image->columns; x++)
972 GetPixelInfoPixel(image,p,&pixel);
973 height=color_matrix->height > 6 ? 6UL : color_matrix->height;
974 for (v=0; v < (ssize_t) height; v++)
979 sum=ColorMatrix[v][0]*GetPixelRed(image,p)+ColorMatrix[v][1]*
980 GetPixelGreen(image,p)+ColorMatrix[v][2]*GetPixelBlue(image,p);
981 if (image->colorspace == CMYKColorspace)
982 sum+=ColorMatrix[v][3]*GetPixelBlack(image,p);
983 if (image->matte != MagickFalse)
984 sum+=ColorMatrix[v][4]*GetPixelAlpha(image,p);
985 sum+=QuantumRange*ColorMatrix[v][5];
988 case 0: pixel.red=sum; break;
989 case 1: pixel.green=sum; break;
990 case 2: pixel.blue=sum; break;
991 case 3: pixel.black=sum; break;
992 case 4: pixel.alpha=sum; break;
996 SetPixelInfoPixel(color_image,&pixel,q);
997 p+=GetPixelChannels(image);
998 q+=GetPixelChannels(color_image);
1000 if (SyncCacheViewAuthenticPixels(color_view,exception) == MagickFalse)
1002 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1007 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1008 #pragma omp critical (MagickCore_ColorMatrixImage)
1010 proceed=SetImageProgress(image,ColorMatrixImageTag,progress++,
1012 if (proceed == MagickFalse)
1016 color_view=DestroyCacheView(color_view);
1017 image_view=DestroyCacheView(image_view);
1018 if (status == MagickFalse)
1019 color_image=DestroyImage(color_image);
1020 return(color_image);
1024 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1028 + D e s t r o y F x I n f o %
1032 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1034 % DestroyFxInfo() deallocates memory associated with an FxInfo structure.
1036 % The format of the DestroyFxInfo method is:
1038 % ImageInfo *DestroyFxInfo(ImageInfo *fx_info)
1040 % A description of each parameter follows:
1042 % o fx_info: the fx info.
1045 MagickPrivate FxInfo *DestroyFxInfo(FxInfo *fx_info)
1050 fx_info->exception=DestroyExceptionInfo(fx_info->exception);
1051 fx_info->expression=DestroyString(fx_info->expression);
1052 fx_info->symbols=DestroySplayTree(fx_info->symbols);
1053 fx_info->colors=DestroySplayTree(fx_info->colors);
1054 for (i=(ssize_t) GetImageListLength(fx_info->images)-1; i >= 0; i--)
1055 fx_info->view[i]=DestroyCacheView(fx_info->view[i]);
1056 fx_info->view=(CacheView **) RelinquishMagickMemory(fx_info->view);
1057 fx_info->random_info=DestroyRandomInfo(fx_info->random_info);
1058 fx_info=(FxInfo *) RelinquishMagickMemory(fx_info);
1063 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1067 + 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 %
1071 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1073 % FxEvaluateChannelExpression() evaluates an expression and returns the
1076 % The format of the FxEvaluateExpression method is:
1078 % MagickRealType FxEvaluateChannelExpression(FxInfo *fx_info,
1079 % const PixelChannel channel,const ssize_t x,const ssize_t y,
1080 % MagickRealType *alpha,Exceptioninfo *exception)
1081 % MagickRealType FxEvaluateExpression(FxInfo *fx_info,
1082 % MagickRealType *alpha,Exceptioninfo *exception)
1084 % A description of each parameter follows:
1086 % o fx_info: the fx info.
1088 % o channel: the channel.
1090 % o x,y: the pixel position.
1092 % o alpha: the result.
1094 % o exception: return any errors or warnings in this structure.
1098 static inline double MagickMax(const double x,const double y)
1105 static inline double MagickMin(const double x,const double y)
1112 static MagickRealType FxChannelStatistics(FxInfo *fx_info,const Image *image,
1113 PixelChannel channel,const char *symbol,ExceptionInfo *exception)
1117 statistic[MaxTextExtent];
1125 for (p=symbol; (*p != '.') && (*p != '\0'); p++) ;
1127 switch (*++p) /* e.g. depth.r */
1129 case 'r': channel=RedPixelChannel; break;
1130 case 'g': channel=GreenPixelChannel; break;
1131 case 'b': channel=BluePixelChannel; break;
1132 case 'c': channel=CyanPixelChannel; break;
1133 case 'm': channel=MagentaPixelChannel; break;
1134 case 'y': channel=YellowPixelChannel; break;
1135 case 'k': channel=BlackPixelChannel; break;
1138 (void) FormatLocaleString(key,MaxTextExtent,"%p.%.20g.%s",(void *) image,
1139 (double) channel,symbol);
1140 value=(const char *) GetValueFromSplayTree(fx_info->symbols,key);
1141 if (value != (const char *) NULL)
1142 return(QuantumScale*StringToDouble(value,(char **) NULL));
1143 (void) DeleteNodeFromSplayTree(fx_info->symbols,key);
1144 if (LocaleNCompare(symbol,"depth",5) == 0)
1149 depth=GetImageDepth(image,exception);
1150 (void) FormatLocaleString(statistic,MaxTextExtent,"%.20g",(double) depth);
1152 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1158 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
1159 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",kurtosis);
1161 if (LocaleNCompare(symbol,"maxima",6) == 0)
1167 (void) GetImageRange(image,&minima,&maxima,exception);
1168 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",maxima);
1170 if (LocaleNCompare(symbol,"mean",4) == 0)
1176 (void) GetImageMean(image,&mean,&standard_deviation,exception);
1177 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",mean);
1179 if (LocaleNCompare(symbol,"minima",6) == 0)
1185 (void) GetImageRange(image,&minima,&maxima,exception);
1186 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",minima);
1188 if (LocaleNCompare(symbol,"skewness",8) == 0)
1194 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
1195 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",skewness);
1197 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1203 (void) GetImageMean(image,&mean,&standard_deviation,exception);
1204 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",
1205 standard_deviation);
1207 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(key),
1208 ConstantString(statistic));
1209 return(QuantumScale*StringToDouble(statistic,(char **) NULL));
1212 static MagickRealType
1213 FxEvaluateSubexpression(FxInfo *,const PixelChannel,const ssize_t,
1214 const ssize_t,const char *,MagickRealType *,ExceptionInfo *);
1216 static MagickOffsetType FxGCD(MagickOffsetType alpha,MagickOffsetType beta)
1219 return(FxGCD(beta,alpha % beta));
1223 static inline const char *FxSubexpression(const char *expression,
1224 ExceptionInfo *exception)
1233 subexpression=expression;
1234 while ((*subexpression != '\0') &&
1235 ((level != 1) || (strchr(")",(int) *subexpression) == (char *) NULL)))
1237 if (strchr("(",(int) *subexpression) != (char *) NULL)
1240 if (strchr(")",(int) *subexpression) != (char *) NULL)
1244 if (*subexpression == '\0')
1245 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1246 "UnbalancedParenthesis","`%s'",expression);
1247 return(subexpression);
1250 static MagickRealType FxGetSymbol(FxInfo *fx_info,const PixelChannel channel,
1251 const ssize_t x,const ssize_t y,const char *expression,
1252 ExceptionInfo *exception)
1256 subexpression[MaxTextExtent],
1257 symbol[MaxTextExtent];
1284 i=GetImageIndexInList(fx_info->images);
1288 if (isalpha((int) *(p+1)) == 0)
1290 if (strchr("suv",(int) *p) != (char *) NULL)
1297 i=GetImageIndexInList(fx_info->images);
1300 case 'u': i=0; break;
1301 case 'v': i=1; break;
1308 for (p++; *p != '\0'; )
1322 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1324 i=(ssize_t) (alpha+0.5);
1330 if ((isalpha((int) *(p+1)) == 0) && (*p == 'p'))
1337 for (p++; *p != '\0'; )
1351 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1362 for (p++; *p != '\0'; )
1376 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1386 length=GetImageListLength(fx_info->images);
1388 i+=(ssize_t) length;
1390 image=GetImageFromList(fx_info->images,i);
1391 if (image == (Image *) NULL)
1393 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1394 "NoSuchImage","`%s'",expression);
1397 GetPixelInfo(image,&pixel);
1398 (void) InterpolatePixelInfo(image,fx_info->view[i],image->interpolate,
1399 point.x,point.y,&pixel,exception);
1400 if ((strlen(p) > 2) && (LocaleCompare(p,"intensity") != 0) &&
1401 (LocaleCompare(p,"luminance") != 0) && (LocaleCompare(p,"hue") != 0) &&
1402 (LocaleCompare(p,"saturation") != 0) &&
1403 (LocaleCompare(p,"lightness") != 0))
1406 name[MaxTextExtent];
1408 (void) CopyMagickString(name,p,MaxTextExtent);
1409 for (q=name+(strlen(name)-1); q > name; q--)
1419 if ((strlen(name) > 2) &&
1420 (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
1425 color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
1426 if (color != (PixelInfo *) NULL)
1436 status=QueryColorCompliance(name,AllCompliance,&pixel,
1437 fx_info->exception);
1438 if (status != MagickFalse)
1440 (void) AddValueToSplayTree(fx_info->colors,ConstantString(
1441 name),ClonePixelInfo(&pixel));
1447 (void) CopyMagickString(symbol,p,MaxTextExtent);
1448 StripString(symbol);
1449 if (*symbol == '\0')
1453 case RedPixelChannel: return(QuantumScale*pixel.red);
1454 case GreenPixelChannel: return(QuantumScale*pixel.green);
1455 case BluePixelChannel: return(QuantumScale*pixel.blue);
1456 case BlackPixelChannel:
1458 if (image->colorspace != CMYKColorspace)
1460 (void) ThrowMagickException(exception,GetMagickModule(),
1461 ImageError,"ColorSeparatedImageRequired","`%s'",
1465 return(QuantumScale*pixel.black);
1467 case AlphaPixelChannel:
1472 if (pixel.matte == MagickFalse)
1474 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
1477 case IndexPixelChannel:
1479 case IntensityPixelChannel:
1481 return(QuantumScale*GetPixelInfoIntensity(&pixel));
1486 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1487 "UnableToParseExpression","`%s'",p);
1495 if (LocaleCompare(symbol,"a") == 0)
1496 return((MagickRealType) (QuantumScale*pixel.alpha));
1502 if (LocaleCompare(symbol,"b") == 0)
1503 return(QuantumScale*pixel.blue);
1509 if (LocaleNCompare(symbol,"channel",7) == 0)
1517 flags=ParseGeometry(symbol+7,&channel_info);
1518 if (image->colorspace == CMYKColorspace)
1521 case CyanPixelChannel:
1523 if ((flags & RhoValue) == 0)
1525 return(channel_info.rho);
1527 case MagentaPixelChannel:
1529 if ((flags & SigmaValue) == 0)
1531 return(channel_info.sigma);
1533 case YellowPixelChannel:
1535 if ((flags & XiValue) == 0)
1537 return(channel_info.xi);
1539 case BlackPixelChannel:
1541 if ((flags & PsiValue) == 0)
1543 return(channel_info.psi);
1545 case AlphaPixelChannel:
1547 if ((flags & ChiValue) == 0)
1549 return(channel_info.chi);
1556 case RedPixelChannel:
1558 if ((flags & RhoValue) == 0)
1560 return(channel_info.rho);
1562 case GreenPixelChannel:
1564 if ((flags & SigmaValue) == 0)
1566 return(channel_info.sigma);
1568 case BluePixelChannel:
1570 if ((flags & XiValue) == 0)
1572 return(channel_info.xi);
1574 case BlackPixelChannel:
1576 if ((flags & ChiValue) == 0)
1578 return(channel_info.chi);
1580 case AlphaPixelChannel:
1582 if ((flags & PsiValue) == 0)
1584 return(channel_info.psi);
1591 if (LocaleCompare(symbol,"c") == 0)
1592 return(QuantumScale*pixel.red);
1598 if (LocaleNCompare(symbol,"depth",5) == 0)
1599 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1605 if (LocaleCompare(symbol,"g") == 0)
1606 return(QuantumScale*pixel.green);
1612 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1613 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1614 if (LocaleCompare(symbol,"k") == 0)
1616 if (image->colorspace != CMYKColorspace)
1618 (void) ThrowMagickException(exception,GetMagickModule(),
1619 OptionError,"ColorSeparatedImageRequired","`%s'",
1623 return(QuantumScale*pixel.black);
1630 if (LocaleCompare(symbol,"h") == 0)
1631 return((MagickRealType) image->rows);
1632 if (LocaleCompare(symbol,"hue") == 0)
1639 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1648 if ((LocaleCompare(symbol,"image.depth") == 0) ||
1649 (LocaleCompare(symbol,"image.minima") == 0) ||
1650 (LocaleCompare(symbol,"image.maxima") == 0) ||
1651 (LocaleCompare(symbol,"image.mean") == 0) ||
1652 (LocaleCompare(symbol,"image.kurtosis") == 0) ||
1653 (LocaleCompare(symbol,"image.skewness") == 0) ||
1654 (LocaleCompare(symbol,"image.standard_deviation") == 0))
1655 return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
1656 if (LocaleCompare(symbol,"image.resolution.x") == 0)
1657 return(image->resolution.x);
1658 if (LocaleCompare(symbol,"image.resolution.y") == 0)
1659 return(image->resolution.y);
1660 if (LocaleCompare(symbol,"intensity") == 0)
1661 return(QuantumScale*GetPixelInfoIntensity(&pixel));
1662 if (LocaleCompare(symbol,"i") == 0)
1663 return((MagickRealType) x);
1669 if (LocaleCompare(symbol,"j") == 0)
1670 return((MagickRealType) y);
1676 if (LocaleCompare(symbol,"lightness") == 0)
1683 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1687 if (LocaleCompare(symbol,"luminance") == 0)
1692 luminence=0.2126*pixel.red+0.7152*pixel.green+0.0722*pixel.blue;
1693 return(QuantumScale*luminence);
1700 if (LocaleNCompare(symbol,"maxima",6) == 0)
1701 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1702 if (LocaleNCompare(symbol,"mean",4) == 0)
1703 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1704 if (LocaleNCompare(symbol,"minima",6) == 0)
1705 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1706 if (LocaleCompare(symbol,"m") == 0)
1707 return(QuantumScale*pixel.blue);
1713 if (LocaleCompare(symbol,"n") == 0)
1714 return((MagickRealType) GetImageListLength(fx_info->images));
1720 if (LocaleCompare(symbol,"o") == 0)
1721 return(QuantumScale*pixel.alpha);
1727 if (LocaleCompare(symbol,"page.height") == 0)
1728 return((MagickRealType) image->page.height);
1729 if (LocaleCompare(symbol,"page.width") == 0)
1730 return((MagickRealType) image->page.width);
1731 if (LocaleCompare(symbol,"page.x") == 0)
1732 return((MagickRealType) image->page.x);
1733 if (LocaleCompare(symbol,"page.y") == 0)
1734 return((MagickRealType) image->page.y);
1740 if (LocaleCompare(symbol,"resolution.x") == 0)
1741 return(image->resolution.x);
1742 if (LocaleCompare(symbol,"resolution.y") == 0)
1743 return(image->resolution.y);
1744 if (LocaleCompare(symbol,"r") == 0)
1745 return(QuantumScale*pixel.red);
1751 if (LocaleCompare(symbol,"saturation") == 0)
1758 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1762 if (LocaleNCompare(symbol,"skewness",8) == 0)
1763 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1764 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1765 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1771 if (LocaleCompare(symbol,"t") == 0)
1772 return((MagickRealType) GetImageIndexInList(fx_info->images));
1778 if (LocaleCompare(symbol,"w") == 0)
1779 return((MagickRealType) image->columns);
1785 if (LocaleCompare(symbol,"y") == 0)
1786 return(QuantumScale*pixel.green);
1792 if (LocaleCompare(symbol,"z") == 0)
1797 depth=(MagickRealType) GetImageDepth(image,fx_info->exception);
1805 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1806 if (value != (const char *) NULL)
1807 return((MagickRealType) StringToDouble(value,(char **) NULL));
1808 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1809 "UnableToParseExpression","`%s'",symbol);
1813 static const char *FxOperatorPrecedence(const char *expression,
1814 ExceptionInfo *exception)
1818 UndefinedPrecedence,
1820 BitwiseComplementPrecedence,
1822 ExponentialNotationPrecedence,
1826 RelationalPrecedence,
1827 EquivalencyPrecedence,
1828 BitwiseAndPrecedence,
1829 BitwiseOrPrecedence,
1830 LogicalAndPrecedence,
1831 LogicalOrPrecedence,
1833 AssignmentPrecedence,
1853 subexpression=(const char *) NULL;
1854 target=NullPrecedence;
1855 while (*expression != '\0')
1857 precedence=UndefinedPrecedence;
1858 if ((isspace((int) ((char) *expression)) != 0) || (c == (int) '@'))
1863 switch (*expression)
1868 #if defined(MAGICKCORE_HAVE_ACOSH)
1869 if (LocaleNCompare(expression,"acosh",5) == 0)
1875 #if defined(MAGICKCORE_HAVE_ASINH)
1876 if (LocaleNCompare(expression,"asinh",5) == 0)
1882 #if defined(MAGICKCORE_HAVE_ATANH)
1883 if (LocaleNCompare(expression,"atanh",5) == 0)
1894 if ((LocaleNCompare(expression,"E+",2) == 0) ||
1895 (LocaleNCompare(expression,"E-",2) == 0))
1897 expression+=2; /* scientific notation */
1904 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1905 (LocaleNCompare(expression,"j1",2) == 0))
1914 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1921 if ((c == (int) '{') || (c == (int) '['))
1924 if ((c == (int) '}') || (c == (int) ']'))
1927 switch ((unsigned char) *expression)
1932 precedence=BitwiseComplementPrecedence;
1938 precedence=ExponentPrecedence;
1943 if (((c != 0) && ((isdigit((int) ((char) c)) != 0) ||
1944 (strchr(")",c) != (char *) NULL))) &&
1945 (((islower((int) ((char) *expression)) != 0) ||
1946 (strchr("(",(int) *expression) != (char *) NULL)) ||
1947 ((isdigit((int) ((char) c)) == 0) &&
1948 (isdigit((int) ((char) *expression)) != 0))) &&
1949 (strchr("xy",(int) *expression) == (char *) NULL))
1950 precedence=MultiplyPrecedence;
1957 precedence=MultiplyPrecedence;
1963 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
1965 precedence=AdditionPrecedence;
1968 case LeftShiftOperator:
1969 case RightShiftOperator:
1971 precedence=ShiftPrecedence;
1975 case LessThanEqualOperator:
1976 case GreaterThanEqualOperator:
1979 precedence=RelationalPrecedence;
1983 case NotEqualOperator:
1985 precedence=EquivalencyPrecedence;
1990 precedence=BitwiseAndPrecedence;
1995 precedence=BitwiseOrPrecedence;
1998 case LogicalAndOperator:
2000 precedence=LogicalAndPrecedence;
2003 case LogicalOrOperator:
2005 precedence=LogicalOrPrecedence;
2008 case ExponentialNotation:
2010 precedence=ExponentialNotationPrecedence;
2016 precedence=TernaryPrecedence;
2021 precedence=AssignmentPrecedence;
2026 precedence=CommaPrecedence;
2031 precedence=SeparatorPrecedence;
2035 if ((precedence == BitwiseComplementPrecedence) ||
2036 (precedence == TernaryPrecedence) ||
2037 (precedence == AssignmentPrecedence))
2039 if (precedence > target)
2042 Right-to-left associativity.
2045 subexpression=expression;
2049 if (precedence >= target)
2052 Left-to-right associativity.
2055 subexpression=expression;
2057 if (strchr("(",(int) *expression) != (char *) NULL)
2058 expression=FxSubexpression(expression,exception);
2059 c=(int) (*expression++);
2061 return(subexpression);
2064 static MagickRealType FxEvaluateSubexpression(FxInfo *fx_info,
2065 const PixelChannel channel,const ssize_t x,const ssize_t y,
2066 const char *expression,MagickRealType *beta,ExceptionInfo *exception)
2070 subexpression[MaxTextExtent];
2080 if (exception->severity != UndefinedException)
2082 while (isspace((int) *expression) != 0)
2084 if (*expression == '\0')
2086 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
2087 "MissingExpression","`%s'",expression);
2090 *subexpression='\0';
2091 p=FxOperatorPrecedence(expression,exception);
2092 if (p != (const char *) NULL)
2094 (void) CopyMagickString(subexpression,expression,(size_t)
2096 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2098 switch ((unsigned char) *p)
2102 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2103 *beta=(MagickRealType) (~(size_t) *beta);
2108 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2109 return(*beta == 0.0 ? 1.0 : 0.0);
2113 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2114 channel,x,y,++p,beta,exception));
2118 case ExponentialNotation:
2120 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2121 return(alpha*(*beta));
2125 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2128 if (exception->severity == UndefinedException)
2129 (void) ThrowMagickException(exception,GetMagickModule(),
2130 OptionError,"DivideByZero","`%s'",expression);
2133 return(alpha/(*beta));
2137 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2138 *beta=fabs(floor(((double) *beta)+0.5));
2141 (void) ThrowMagickException(exception,GetMagickModule(),
2142 OptionError,"DivideByZero","`%s'",expression);
2145 return(fmod((double) alpha,(double) *beta));
2149 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2150 return(alpha+(*beta));
2154 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2155 return(alpha-(*beta));
2157 case LeftShiftOperator:
2159 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2160 *beta=(MagickRealType) ((size_t) (alpha+0.5) << (size_t) (gamma+0.5));
2163 case RightShiftOperator:
2165 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2166 *beta=(MagickRealType) ((size_t) (alpha+0.5) >> (size_t) (gamma+0.5));
2171 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2172 return(alpha < *beta ? 1.0 : 0.0);
2174 case LessThanEqualOperator:
2176 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2177 return(alpha <= *beta ? 1.0 : 0.0);
2181 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2182 return(alpha > *beta ? 1.0 : 0.0);
2184 case GreaterThanEqualOperator:
2186 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2187 return(alpha >= *beta ? 1.0 : 0.0);
2191 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2192 return(fabs(alpha-(*beta)) <= MagickEpsilon ? 1.0 : 0.0);
2194 case NotEqualOperator:
2196 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2197 return(fabs(alpha-(*beta)) > MagickEpsilon ? 1.0 : 0.0);
2201 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2202 *beta=(MagickRealType) ((size_t) (alpha+0.5) & (size_t) (gamma+0.5));
2207 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2208 *beta=(MagickRealType) ((size_t) (alpha+0.5) | (size_t) (gamma+0.5));
2211 case LogicalAndOperator:
2213 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2214 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2217 case LogicalOrOperator:
2219 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2220 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2228 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2230 p=StringToken(":",&q);
2231 if (q == (char *) NULL)
2233 (void) ThrowMagickException(exception,GetMagickModule(),
2234 OptionError,"UnableToParseExpression","`%s'",subexpression);
2237 if (fabs((double) alpha) > MagickEpsilon)
2238 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2240 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2246 numeric[MaxTextExtent];
2249 while (isalpha((int) ((unsigned char) *q)) != 0)
2253 (void) ThrowMagickException(exception,GetMagickModule(),
2254 OptionError,"UnableToParseExpression","`%s'",subexpression);
2257 ClearMagickException(exception);
2258 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2259 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
2261 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2262 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2263 subexpression),ConstantString(numeric));
2268 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2273 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2278 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2284 if (strchr("(",(int) *expression) != (char *) NULL)
2286 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2287 subexpression[strlen(subexpression)-1]='\0';
2288 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2292 switch (*expression)
2296 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2302 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2308 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2310 return((MagickRealType) (~(size_t) (gamma+0.5)));
2315 if (LocaleNCompare(expression,"abs",3) == 0)
2317 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2319 return((MagickRealType) fabs((double) alpha));
2321 #if defined(MAGICKCORE_HAVE_ACOSH)
2322 if (LocaleNCompare(expression,"acosh",5) == 0)
2324 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2326 return((MagickRealType) acosh((double) alpha));
2329 if (LocaleNCompare(expression,"acos",4) == 0)
2331 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2333 return((MagickRealType) acos((double) alpha));
2335 #if defined(MAGICKCORE_HAVE_J1)
2336 if (LocaleNCompare(expression,"airy",4) == 0)
2338 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2342 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
2343 return(gamma*gamma);
2346 #if defined(MAGICKCORE_HAVE_ASINH)
2347 if (LocaleNCompare(expression,"asinh",5) == 0)
2349 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2351 return((MagickRealType) asinh((double) alpha));
2354 if (LocaleNCompare(expression,"asin",4) == 0)
2356 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2358 return((MagickRealType) asin((double) alpha));
2360 if (LocaleNCompare(expression,"alt",3) == 0)
2362 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2364 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
2366 if (LocaleNCompare(expression,"atan2",5) == 0)
2368 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2370 return((MagickRealType) atan2((double) alpha,(double) *beta));
2372 #if defined(MAGICKCORE_HAVE_ATANH)
2373 if (LocaleNCompare(expression,"atanh",5) == 0)
2375 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2377 return((MagickRealType) atanh((double) alpha));
2380 if (LocaleNCompare(expression,"atan",4) == 0)
2382 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2384 return((MagickRealType) atan((double) alpha));
2386 if (LocaleCompare(expression,"a") == 0)
2387 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2393 if (LocaleCompare(expression,"b") == 0)
2394 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2400 if (LocaleNCompare(expression,"ceil",4) == 0)
2402 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2404 return((MagickRealType) ceil((double) alpha));
2406 if (LocaleNCompare(expression,"cosh",4) == 0)
2408 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2410 return((MagickRealType) cosh((double) alpha));
2412 if (LocaleNCompare(expression,"cos",3) == 0)
2414 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2416 return((MagickRealType) cos((double) alpha));
2418 if (LocaleCompare(expression,"c") == 0)
2419 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2425 if (LocaleNCompare(expression,"debug",5) == 0)
2430 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2432 if (fx_info->images->colorspace == CMYKColorspace)
2435 case CyanPixelChannel: type="cyan"; break;
2436 case MagentaPixelChannel: type="magenta"; break;
2437 case YellowPixelChannel: type="yellow"; break;
2438 case AlphaPixelChannel: type="opacity"; break;
2439 case BlackPixelChannel: type="black"; break;
2440 default: type="unknown"; break;
2445 case RedPixelChannel: type="red"; break;
2446 case GreenPixelChannel: type="green"; break;
2447 case BluePixelChannel: type="blue"; break;
2448 case AlphaPixelChannel: type="opacity"; break;
2449 default: type="unknown"; break;
2451 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2452 if (strlen(subexpression) > 1)
2453 subexpression[strlen(subexpression)-1]='\0';
2454 if (fx_info->file != (FILE *) NULL)
2455 (void) FormatLocaleFile(fx_info->file,"%s[%.20g,%.20g].%s: "
2456 "%s=%.*g\n",fx_info->images->filename,(double) x,(double) y,type,
2457 subexpression,GetMagickPrecision(),(double) alpha);
2460 if (LocaleNCompare(expression,"drc",3) == 0)
2462 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2464 return((MagickRealType) (alpha/(*beta*(alpha-1.0)+1.0)));
2471 if (LocaleCompare(expression,"epsilon") == 0)
2472 return((MagickRealType) MagickEpsilon);
2473 if (LocaleNCompare(expression,"exp",3) == 0)
2475 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2477 return((MagickRealType) exp((double) alpha));
2479 if (LocaleCompare(expression,"e") == 0)
2480 return((MagickRealType) 2.7182818284590452354);
2486 if (LocaleNCompare(expression,"floor",5) == 0)
2488 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2490 return((MagickRealType) floor((double) alpha));
2497 if (LocaleNCompare(expression,"gauss",5) == 0)
2499 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2501 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
2502 return((MagickRealType) gamma);
2504 if (LocaleNCompare(expression,"gcd",3) == 0)
2509 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2511 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType) (*beta+
2513 return((MagickRealType) gcd);
2515 if (LocaleCompare(expression,"g") == 0)
2516 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2522 if (LocaleCompare(expression,"h") == 0)
2523 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2524 if (LocaleCompare(expression,"hue") == 0)
2525 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2526 if (LocaleNCompare(expression,"hypot",5) == 0)
2528 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2530 return((MagickRealType) hypot((double) alpha,(double) *beta));
2537 if (LocaleCompare(expression,"k") == 0)
2538 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2544 if (LocaleCompare(expression,"intensity") == 0)
2545 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2546 if (LocaleNCompare(expression,"int",3) == 0)
2548 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2550 return((MagickRealType) floor(alpha));
2552 #if defined(MAGICKCORE_HAVE_ISNAN)
2553 if (LocaleNCompare(expression,"isnan",5) == 0)
2555 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2557 return((MagickRealType) !!isnan((double) alpha));
2560 if (LocaleCompare(expression,"i") == 0)
2561 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2567 if (LocaleCompare(expression,"j") == 0)
2568 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2569 #if defined(MAGICKCORE_HAVE_J0)
2570 if (LocaleNCompare(expression,"j0",2) == 0)
2572 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2574 return((MagickRealType) j0((double) alpha));
2577 #if defined(MAGICKCORE_HAVE_J1)
2578 if (LocaleNCompare(expression,"j1",2) == 0)
2580 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2582 return((MagickRealType) j1((double) alpha));
2585 #if defined(MAGICKCORE_HAVE_J1)
2586 if (LocaleNCompare(expression,"jinc",4) == 0)
2588 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2592 gamma=(MagickRealType) (2.0*j1((double) (MagickPI*alpha))/(MagickPI*
2602 if (LocaleNCompare(expression,"ln",2) == 0)
2604 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2606 return((MagickRealType) log((double) alpha));
2608 if (LocaleNCompare(expression,"logtwo",6) == 0)
2610 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2612 return((MagickRealType) log10((double) alpha))/log10(2.0);
2614 if (LocaleNCompare(expression,"log",3) == 0)
2616 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2618 return((MagickRealType) log10((double) alpha));
2620 if (LocaleCompare(expression,"lightness") == 0)
2621 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2627 if (LocaleCompare(expression,"MaxRGB") == 0)
2628 return((MagickRealType) QuantumRange);
2629 if (LocaleNCompare(expression,"maxima",6) == 0)
2631 if (LocaleNCompare(expression,"max",3) == 0)
2633 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2635 return(alpha > *beta ? alpha : *beta);
2637 if (LocaleNCompare(expression,"minima",6) == 0)
2639 if (LocaleNCompare(expression,"min",3) == 0)
2641 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2643 return(alpha < *beta ? alpha : *beta);
2645 if (LocaleNCompare(expression,"mod",3) == 0)
2647 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2649 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2652 if (LocaleCompare(expression,"m") == 0)
2653 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2659 if (LocaleNCompare(expression,"not",3) == 0)
2661 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2663 return((MagickRealType) (alpha < MagickEpsilon));
2665 if (LocaleCompare(expression,"n") == 0)
2666 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2672 if (LocaleCompare(expression,"Opaque") == 0)
2674 if (LocaleCompare(expression,"o") == 0)
2675 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2681 if (LocaleCompare(expression,"phi") == 0)
2682 return((MagickRealType) MagickPHI);
2683 if (LocaleCompare(expression,"pi") == 0)
2684 return((MagickRealType) MagickPI);
2685 if (LocaleNCompare(expression,"pow",3) == 0)
2687 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2689 return((MagickRealType) pow((double) alpha,(double) *beta));
2691 if (LocaleCompare(expression,"p") == 0)
2692 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2698 if (LocaleCompare(expression,"QuantumRange") == 0)
2699 return((MagickRealType) QuantumRange);
2700 if (LocaleCompare(expression,"QuantumScale") == 0)
2701 return((MagickRealType) QuantumScale);
2707 if (LocaleNCompare(expression,"rand",4) == 0)
2708 return((MagickRealType) GetPseudoRandomValue(fx_info->random_info));
2709 if (LocaleNCompare(expression,"round",5) == 0)
2711 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2713 return((MagickRealType) floor((double) alpha+0.5));
2715 if (LocaleCompare(expression,"r") == 0)
2716 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2722 if (LocaleCompare(expression,"saturation") == 0)
2723 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2724 if (LocaleNCompare(expression,"sign",4) == 0)
2726 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2728 return(alpha < 0.0 ? -1.0 : 1.0);
2730 if (LocaleNCompare(expression,"sinc",4) == 0)
2732 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2736 gamma=(MagickRealType) (sin((double) (MagickPI*alpha))/
2740 if (LocaleNCompare(expression,"sinh",4) == 0)
2742 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2744 return((MagickRealType) sinh((double) alpha));
2746 if (LocaleNCompare(expression,"sin",3) == 0)
2748 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2750 return((MagickRealType) sin((double) alpha));
2752 if (LocaleNCompare(expression,"sqrt",4) == 0)
2754 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2756 return((MagickRealType) sqrt((double) alpha));
2758 if (LocaleNCompare(expression,"squish",6) == 0)
2760 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2762 return((MagickRealType) (1.0/(1.0+exp((double) (4.0*alpha)))));
2764 if (LocaleCompare(expression,"s") == 0)
2765 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2771 if (LocaleNCompare(expression,"tanh",4) == 0)
2773 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2775 return((MagickRealType) tanh((double) alpha));
2777 if (LocaleNCompare(expression,"tan",3) == 0)
2779 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2781 return((MagickRealType) tan((double) alpha));
2783 if (LocaleCompare(expression,"Transparent") == 0)
2785 if (LocaleNCompare(expression,"trunc",5) == 0)
2787 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2790 return((MagickRealType) floor((double) alpha));
2791 return((MagickRealType) ceil((double) alpha));
2793 if (LocaleCompare(expression,"t") == 0)
2794 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2800 if (LocaleCompare(expression,"u") == 0)
2801 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2807 if (LocaleCompare(expression,"v") == 0)
2808 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2814 if (LocaleNCompare(expression,"while",5) == 0)
2818 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2820 } while (fabs((double) alpha) >= MagickEpsilon);
2821 return((MagickRealType) *beta);
2823 if (LocaleCompare(expression,"w") == 0)
2824 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2830 if (LocaleCompare(expression,"y") == 0)
2831 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2837 if (LocaleCompare(expression,"z") == 0)
2838 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2844 q=(char *) expression;
2845 alpha=InterpretSiPrefixValue(expression,&q);
2846 if (q == expression)
2847 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2851 MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
2852 MagickRealType *alpha,ExceptionInfo *exception)
2857 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2862 MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
2863 MagickRealType *alpha,ExceptionInfo *exception)
2872 fx_info->file=(FILE *) NULL;
2873 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2879 MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
2880 const PixelChannel channel,const ssize_t x,const ssize_t y,
2881 MagickRealType *alpha,ExceptionInfo *exception)
2887 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2889 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2893 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2901 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2903 % FxImage() applies a mathematical expression to the specified image.
2905 % The format of the FxImage method is:
2907 % Image *FxImage(const Image *image,const char *expression,
2908 % ExceptionInfo *exception)
2910 % A description of each parameter follows:
2912 % o image: the image.
2914 % o expression: A mathematical expression.
2916 % o exception: return any errors or warnings in this structure.
2920 static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2925 assert(fx_info != (FxInfo **) NULL);
2926 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
2927 if (fx_info[i] != (FxInfo *) NULL)
2928 fx_info[i]=DestroyFxInfo(fx_info[i]);
2929 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
2933 static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2934 ExceptionInfo *exception)
2951 number_threads=GetOpenMPMaximumThreads();
2952 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
2953 if (fx_info == (FxInfo **) NULL)
2954 return((FxInfo **) NULL);
2955 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2956 if (*expression != '@')
2957 fx_expression=ConstantString(expression);
2959 fx_expression=FileToString(expression+1,~0,exception);
2960 for (i=0; i < (ssize_t) number_threads; i++)
2962 fx_info[i]=AcquireFxInfo(image,fx_expression);
2963 if (fx_info[i] == (FxInfo *) NULL)
2964 return(DestroyFxThreadSet(fx_info));
2965 (void) FxPreprocessExpression(fx_info[i],&alpha,fx_info[i]->exception);
2967 fx_expression=DestroyString(fx_expression);
2971 MagickExport Image *FxImage(const Image *image,const char *expression,
2972 ExceptionInfo *exception)
2974 #define FxImageTag "Fx/Image"
2998 assert(image != (Image *) NULL);
2999 assert(image->signature == MagickSignature);
3000 if (image->debug != MagickFalse)
3001 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3002 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3003 if (fx_image == (Image *) NULL)
3004 return((Image *) NULL);
3005 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
3007 fx_image=DestroyImage(fx_image);
3008 return((Image *) NULL);
3010 fx_info=AcquireFxThreadSet(image,expression,exception);
3011 if (fx_info == (FxInfo **) NULL)
3013 fx_image=DestroyImage(fx_image);
3014 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3016 status=FxPreprocessExpression(fx_info[0],&alpha,exception);
3017 if (status == MagickFalse)
3019 fx_image=DestroyImage(fx_image);
3020 fx_info=DestroyFxThreadSet(fx_info);
3021 return((Image *) NULL);
3028 image_view=AcquireCacheView(image);
3029 fx_view=AcquireCacheView(fx_image);
3030 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3031 #pragma omp parallel for schedule(static,4) shared(progress,status)
3033 for (y=0; y < (ssize_t) fx_image->rows; y++)
3036 id = GetOpenMPThreadId();
3038 register const Quantum
3047 if (status == MagickFalse)
3049 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3050 q=QueueCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
3051 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3056 for (x=0; x < (ssize_t) fx_image->columns; x++)
3061 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3073 channel=GetPixelChannelMapChannel(image,i);
3074 traits=GetPixelChannelMapTraits(image,channel);
3075 fx_traits=GetPixelChannelMapTraits(fx_image,channel);
3076 if ((traits == UndefinedPixelTrait) ||
3077 (fx_traits == UndefinedPixelTrait))
3079 if (((fx_traits & CopyPixelTrait) != 0) ||
3080 (GetPixelMask(image,p) != 0))
3082 SetPixelChannel(fx_image,channel,p[i],q);
3086 (void) FxEvaluateChannelExpression(fx_info[id],channel,x,y,&alpha,
3088 q[i]=ClampToQuantum((MagickRealType) QuantumRange*alpha);
3090 p+=GetPixelChannels(image);
3091 q+=GetPixelChannels(fx_image);
3093 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3095 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3100 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3101 #pragma omp critical (MagickCore_FxImage)
3103 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3104 if (proceed == MagickFalse)
3108 fx_view=DestroyCacheView(fx_view);
3109 image_view=DestroyCacheView(image_view);
3110 fx_info=DestroyFxThreadSet(fx_info);
3111 if (status == MagickFalse)
3112 fx_image=DestroyImage(fx_image);
3117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3121 % I m p l o d e I m a g e %
3125 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3127 % ImplodeImage() creates a new image that is a copy of an existing
3128 % one with the image pixels "implode" by the specified percentage. It
3129 % allocates the memory necessary for the new Image structure and returns a
3130 % pointer to the new image.
3132 % The format of the ImplodeImage method is:
3134 % Image *ImplodeImage(const Image *image,const double amount,
3135 % const PixelInterpolateMethod method,ExceptionInfo *exception)
3137 % A description of each parameter follows:
3139 % o implode_image: Method ImplodeImage returns a pointer to the image
3140 % after it is implode. A null image is returned if there is a memory
3143 % o image: the image.
3145 % o amount: Define the extent of the implosion.
3147 % o method: the pixel interpolation method.
3149 % o exception: return any errors or warnings in this structure.
3152 MagickExport Image *ImplodeImage(const Image *image,const double amount,
3153 const PixelInterpolateMethod method,ExceptionInfo *exception)
3155 #define ImplodeImageTag "Implode/Image"
3181 Initialize implode image attributes.
3183 assert(image != (Image *) NULL);
3184 assert(image->signature == MagickSignature);
3185 if (image->debug != MagickFalse)
3186 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3187 assert(exception != (ExceptionInfo *) NULL);
3188 assert(exception->signature == MagickSignature);
3189 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3191 if (implode_image == (Image *) NULL)
3192 return((Image *) NULL);
3193 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
3195 implode_image=DestroyImage(implode_image);
3196 return((Image *) NULL);
3198 if (implode_image->background_color.alpha != OpaqueAlpha)
3199 implode_image->matte=MagickTrue;
3201 Compute scaling factor.
3205 center.x=0.5*image->columns;
3206 center.y=0.5*image->rows;
3208 if (image->columns > image->rows)
3209 scale.y=(double) image->columns/(double) image->rows;
3211 if (image->columns < image->rows)
3213 scale.x=(double) image->rows/(double) image->columns;
3221 image_view=AcquireCacheView(image);
3222 implode_view=AcquireCacheView(implode_image);
3223 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3224 #pragma omp parallel for schedule(static,4) shared(progress,status)
3226 for (y=0; y < (ssize_t) image->rows; y++)
3234 register const Quantum
3243 if (status == MagickFalse)
3245 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3246 q=QueueCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
3248 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3253 delta.y=scale.y*(double) (y-center.y);
3254 for (x=0; x < (ssize_t) image->columns; x++)
3260 Determine if the pixel is within an ellipse.
3262 if (GetPixelMask(image,p) != 0)
3264 p+=GetPixelChannels(image);
3265 q+=GetPixelChannels(implode_image);
3268 delta.x=scale.x*(double) (x-center.x);
3269 distance=delta.x*delta.x+delta.y*delta.y;
3270 if (distance >= (radius*radius))
3271 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3280 channel=GetPixelChannelMapChannel(image,i);
3281 traits=GetPixelChannelMapTraits(image,channel);
3282 implode_traits=GetPixelChannelMapTraits(implode_image,channel);
3283 if ((traits == UndefinedPixelTrait) ||
3284 (implode_traits == UndefinedPixelTrait))
3286 SetPixelChannel(implode_image,channel,p[i],q);
3298 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/radius/
3300 status=InterpolatePixelChannels(image,image_view,implode_image,method,
3301 (double) (factor*delta.x/scale.x+center.x),(double) (factor*delta.y/
3302 scale.y+center.y),q,exception);
3304 p+=GetPixelChannels(image);
3305 q+=GetPixelChannels(implode_image);
3307 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3309 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3314 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3315 #pragma omp critical (MagickCore_ImplodeImage)
3317 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3318 if (proceed == MagickFalse)
3322 implode_view=DestroyCacheView(implode_view);
3323 image_view=DestroyCacheView(image_view);
3324 if (status == MagickFalse)
3325 implode_image=DestroyImage(implode_image);
3326 return(implode_image);
3330 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3334 % M o r p h I m a g e s %
3338 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3340 % The MorphImages() method requires a minimum of two images. The first
3341 % image is transformed into the second by a number of intervening images
3342 % as specified by frames.
3344 % The format of the MorphImage method is:
3346 % Image *MorphImages(const Image *image,const size_t number_frames,
3347 % ExceptionInfo *exception)
3349 % A description of each parameter follows:
3351 % o image: the image.
3353 % o number_frames: Define the number of in-between image to generate.
3354 % The more in-between frames, the smoother the morph.
3356 % o exception: return any errors or warnings in this structure.
3359 MagickExport Image *MorphImages(const Image *image,
3360 const size_t number_frames,ExceptionInfo *exception)
3362 #define MorphImageTag "Morph/Image"
3378 register const Image
3388 Clone first frame in sequence.
3390 assert(image != (Image *) NULL);
3391 assert(image->signature == MagickSignature);
3392 if (image->debug != MagickFalse)
3393 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3394 assert(exception != (ExceptionInfo *) NULL);
3395 assert(exception->signature == MagickSignature);
3396 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3397 if (morph_images == (Image *) NULL)
3398 return((Image *) NULL);
3399 if (GetNextImageInList(image) == (Image *) NULL)
3404 for (i=1; i < (ssize_t) number_frames; i++)
3406 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3407 if (morph_image == (Image *) NULL)
3409 morph_images=DestroyImageList(morph_images);
3410 return((Image *) NULL);
3412 AppendImageToList(&morph_images,morph_image);
3413 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3418 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3420 if (proceed == MagickFalse)
3424 return(GetFirstImageInList(morph_images));
3427 Morph image sequence.
3432 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3434 for (i=0; i < (ssize_t) number_frames; i++)
3440 beta=(MagickRealType) (i+1.0)/(MagickRealType) (number_frames+1.0);
3442 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
3443 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*next->rows+beta*
3444 GetNextImageInList(next)->rows+0.5),next->filter,exception);
3445 if (morph_image == (Image *) NULL)
3447 morph_images=DestroyImageList(morph_images);
3448 return((Image *) NULL);
3450 status=SetImageStorageClass(morph_image,DirectClass,exception);
3451 if (status == MagickFalse)
3453 morph_image=DestroyImage(morph_image);
3454 return((Image *) NULL);
3456 AppendImageToList(&morph_images,morph_image);
3457 morph_images=GetLastImageInList(morph_images);
3458 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
3459 morph_images->rows,GetNextImageInList(next)->filter,exception);
3460 if (morph_image == (Image *) NULL)
3462 morph_images=DestroyImageList(morph_images);
3463 return((Image *) NULL);
3465 image_view=AcquireCacheView(morph_image);
3466 morph_view=AcquireCacheView(morph_images);
3467 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3468 #pragma omp parallel for schedule(static,4) shared(status)
3470 for (y=0; y < (ssize_t) morph_images->rows; y++)
3475 register const Quantum
3484 if (status == MagickFalse)
3486 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3488 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3490 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3495 for (x=0; x < (ssize_t) morph_images->columns; x++)
3500 for (i=0; i < (ssize_t) GetPixelChannels(morph_image); i++)
3509 channel=GetPixelChannelMapChannel(image,i);
3510 traits=GetPixelChannelMapTraits(image,channel);
3511 morph_traits=GetPixelChannelMapTraits(morph_image,channel);
3512 if ((traits == UndefinedPixelTrait) ||
3513 (morph_traits == UndefinedPixelTrait))
3515 if (((morph_traits & CopyPixelTrait) != 0) ||
3516 (GetPixelMask(image,p) != 0))
3518 SetPixelChannel(morph_image,channel,p[i],q);
3521 SetPixelChannel(morph_image,channel,ClampToQuantum(alpha*
3522 GetPixelChannel(morph_images,channel,q)+beta*p[i]),q);
3524 p+=GetPixelChannels(morph_image);
3525 q+=GetPixelChannels(morph_images);
3527 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3528 if (sync == MagickFalse)
3531 morph_view=DestroyCacheView(morph_view);
3532 image_view=DestroyCacheView(image_view);
3533 morph_image=DestroyImage(morph_image);
3535 if (i < (ssize_t) number_frames)
3538 Clone last frame in sequence.
3540 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3541 if (morph_image == (Image *) NULL)
3543 morph_images=DestroyImageList(morph_images);
3544 return((Image *) NULL);
3546 AppendImageToList(&morph_images,morph_image);
3547 morph_images=GetLastImageInList(morph_images);
3548 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3553 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3554 #pragma omp critical (MagickCore_MorphImages)
3556 proceed=SetImageProgress(image,MorphImageTag,scene,
3557 GetImageListLength(image));
3558 if (proceed == MagickFalse)
3563 if (GetNextImageInList(next) != (Image *) NULL)
3565 morph_images=DestroyImageList(morph_images);
3566 return((Image *) NULL);
3568 return(GetFirstImageInList(morph_images));
3572 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3576 % P l a s m a I m a g e %
3580 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3582 % PlasmaImage() initializes an image with plasma fractal values. The image
3583 % must be initialized with a base color and the random number generator
3584 % seeded before this method is called.
3586 % The format of the PlasmaImage method is:
3588 % MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
3589 % size_t attenuate,size_t depth,ExceptionInfo *exception)
3591 % A description of each parameter follows:
3593 % o image: the image.
3595 % o segment: Define the region to apply plasma fractals values.
3597 % o attenuate: Define the plasma attenuation factor.
3599 % o depth: Limit the plasma recursion depth.
3601 % o exception: return any errors or warnings in this structure.
3605 static inline Quantum PlasmaPixel(RandomInfo *random_info,
3606 const MagickRealType pixel,const MagickRealType noise)
3611 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
3616 static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3617 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3618 const SegmentInfo *segment,size_t attenuate,size_t depth,
3619 ExceptionInfo *exception)
3630 register const Quantum
3646 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3654 Divide the area into quadrants and recurse.
3658 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3659 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
3660 local_info=(*segment);
3661 local_info.x2=(double) x_mid;
3662 local_info.y2=(double) y_mid;
3663 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3664 &local_info,attenuate,depth,exception);
3665 local_info=(*segment);
3666 local_info.y1=(double) y_mid;
3667 local_info.x2=(double) x_mid;
3668 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3669 &local_info,attenuate,depth,exception);
3670 local_info=(*segment);
3671 local_info.x1=(double) x_mid;
3672 local_info.y2=(double) y_mid;
3673 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3674 &local_info,attenuate,depth,exception);
3675 local_info=(*segment);
3676 local_info.x1=(double) x_mid;
3677 local_info.y1=(double) y_mid;
3678 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3679 &local_info,attenuate,depth,exception));
3681 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3682 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
3683 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3684 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3685 return(MagickFalse);
3687 Average pixels and apply plasma.
3689 plasma=(MagickRealType) QuantumRange/(2.0*attenuate);
3690 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3695 x=(ssize_t) ceil(segment->x1-0.5);
3696 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),1,1,
3698 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),1,1,
3700 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
3701 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3702 (q == (Quantum *) NULL))
3704 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3706 channel=GetPixelChannelMapChannel(image,i);
3707 traits=GetPixelChannelMapTraits(image,channel);
3708 if (traits == UndefinedPixelTrait)
3710 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3712 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3713 if (segment->x1 != segment->x2)
3718 x=(ssize_t) ceil(segment->x2-0.5);
3719 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3721 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3723 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
3724 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3725 (q == (Quantum *) NULL))
3727 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3729 channel=GetPixelChannelMapChannel(image,i);
3730 traits=GetPixelChannelMapTraits(image,channel);
3731 if (traits == UndefinedPixelTrait)
3733 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3735 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3738 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3740 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3745 y=(ssize_t) ceil(segment->y2-0.5);
3746 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3748 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3750 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
3751 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3752 (q == (Quantum *) NULL))
3754 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3756 channel=GetPixelChannelMapChannel(image,i);
3757 traits=GetPixelChannelMapTraits(image,channel);
3758 if (traits == UndefinedPixelTrait)
3760 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3762 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3764 if (segment->y1 != segment->y2)
3769 y=(ssize_t) ceil(segment->y1-0.5);
3770 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3772 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3774 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
3775 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3776 (q == (Quantum *) NULL))
3778 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3780 channel=GetPixelChannelMapChannel(image,i);
3781 traits=GetPixelChannelMapTraits(image,channel);
3782 if (traits == UndefinedPixelTrait)
3784 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3786 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3789 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3794 x=(ssize_t) ceil(segment->x1-0.5);
3795 y=(ssize_t) ceil(segment->y1-0.5);
3796 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
3797 x=(ssize_t) ceil(segment->x2-0.5);
3798 y=(ssize_t) ceil(segment->y2-0.5);
3799 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
3800 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
3801 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3802 (q == (Quantum *) NULL))
3804 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3806 channel=GetPixelChannelMapChannel(image,i);
3807 traits=GetPixelChannelMapTraits(image,channel);
3808 if (traits == UndefinedPixelTrait)
3810 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3812 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3814 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3816 return(MagickFalse);
3819 MagickExport MagickBooleanType PlasmaImage(Image *image,
3820 const SegmentInfo *segment,size_t attenuate,size_t depth,
3821 ExceptionInfo *exception)
3834 if (image->debug != MagickFalse)
3835 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3836 assert(image != (Image *) NULL);
3837 assert(image->signature == MagickSignature);
3838 if (image->debug != MagickFalse)
3839 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3840 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
3841 return(MagickFalse);
3842 image_view=AcquireCacheView(image);
3843 u_view=AcquireCacheView(image);
3844 v_view=AcquireCacheView(image);
3845 random_info=AcquireRandomInfo();
3846 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3847 attenuate,depth,exception);
3848 random_info=DestroyRandomInfo(random_info);
3849 v_view=DestroyCacheView(v_view);
3850 u_view=DestroyCacheView(u_view);
3851 image_view=DestroyCacheView(image_view);
3856 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3860 % P o l a r o i d I m a g e %
3864 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3866 % PolaroidImage() simulates a Polaroid picture.
3868 % The format of the AnnotateImage method is:
3870 % Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
3871 % const char *caption,const double angle,
3872 % const PixelInterpolateMethod method,ExceptionInfo exception)
3874 % A description of each parameter follows:
3876 % o image: the image.
3878 % o draw_info: the draw info.
3880 % o caption: the Polaroid caption.
3882 % o angle: Apply the effect along this angle.
3884 % o method: the pixel interpolation method.
3886 % o exception: return any errors or warnings in this structure.
3889 MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
3890 const char *caption,const double angle,const PixelInterpolateMethod method,
3891 ExceptionInfo *exception)
3909 Simulate a Polaroid picture.
3911 assert(image != (Image *) NULL);
3912 assert(image->signature == MagickSignature);
3913 if (image->debug != MagickFalse)
3914 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3915 assert(exception != (ExceptionInfo *) NULL);
3916 assert(exception->signature == MagickSignature);
3917 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
3918 image->rows)/25.0,10.0);
3919 height=image->rows+2*quantum;
3920 caption_image=(Image *) NULL;
3921 if (caption != (const char *) NULL)
3924 geometry[MaxTextExtent],
3940 Generate caption image.
3942 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3943 if (caption_image == (Image *) NULL)
3944 return((Image *) NULL);
3945 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
3946 text=InterpretImageProperties((ImageInfo *) NULL,(Image *) image,caption,
3948 (void) CloneString(&annotate_info->text,text);
3949 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
3951 status=SetImageExtent(caption_image,image->columns,(size_t) ((count+1)*
3952 (metrics.ascent-metrics.descent)+0.5),exception);
3953 if (status == MagickFalse)
3954 caption_image=DestroyImage(caption_image);
3957 caption_image->background_color=image->border_color;
3958 (void) SetImageBackgroundColor(caption_image,exception);
3959 (void) CloneString(&annotate_info->text,text);
3960 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
3962 if (annotate_info->gravity == UndefinedGravity)
3963 (void) CloneString(&annotate_info->geometry,AcquireString(
3965 (void) AnnotateImage(caption_image,annotate_info,exception);
3966 height+=caption_image->rows;
3968 annotate_info=DestroyDrawInfo(annotate_info);
3969 text=DestroyString(text);
3971 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
3973 if (picture_image == (Image *) NULL)
3975 if (caption_image != (Image *) NULL)
3976 caption_image=DestroyImage(caption_image);
3977 return((Image *) NULL);
3979 picture_image->background_color=image->border_color;
3980 (void) SetImageBackgroundColor(picture_image,exception);
3981 (void) CompositeImage(picture_image,image,OverCompositeOp,MagickTrue,quantum,
3983 if (caption_image != (Image *) NULL)
3985 (void) CompositeImage(picture_image,caption_image,OverCompositeOp,
3986 MagickTrue,quantum,(ssize_t) (image->rows+3*quantum/2),exception);
3987 caption_image=DestroyImage(caption_image);
3989 (void) QueryColorCompliance("none",AllCompliance,
3990 &picture_image->background_color,exception);
3991 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
3992 rotate_image=RotateImage(picture_image,90.0,exception);
3993 picture_image=DestroyImage(picture_image);
3994 if (rotate_image == (Image *) NULL)
3995 return((Image *) NULL);
3996 picture_image=rotate_image;
3997 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
3998 picture_image->columns,method,exception);
3999 picture_image=DestroyImage(picture_image);
4000 if (bend_image == (Image *) NULL)
4001 return((Image *) NULL);
4002 picture_image=bend_image;
4003 rotate_image=RotateImage(picture_image,-90.0,exception);
4004 picture_image=DestroyImage(picture_image);
4005 if (rotate_image == (Image *) NULL)
4006 return((Image *) NULL);
4007 picture_image=rotate_image;
4008 picture_image->background_color=image->background_color;
4009 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
4011 if (polaroid_image == (Image *) NULL)
4013 picture_image=DestroyImage(picture_image);
4014 return(picture_image);
4016 flop_image=FlopImage(polaroid_image,exception);
4017 polaroid_image=DestroyImage(polaroid_image);
4018 if (flop_image == (Image *) NULL)
4020 picture_image=DestroyImage(picture_image);
4021 return(picture_image);
4023 polaroid_image=flop_image;
4024 (void) CompositeImage(polaroid_image,picture_image,OverCompositeOp,
4025 MagickTrue,(ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
4026 picture_image=DestroyImage(picture_image);
4027 (void) QueryColorCompliance("none",AllCompliance,
4028 &polaroid_image->background_color,exception);
4029 rotate_image=RotateImage(polaroid_image,angle,exception);
4030 polaroid_image=DestroyImage(polaroid_image);
4031 if (rotate_image == (Image *) NULL)
4032 return((Image *) NULL);
4033 polaroid_image=rotate_image;
4034 trim_image=TrimImage(polaroid_image,exception);
4035 polaroid_image=DestroyImage(polaroid_image);
4036 if (trim_image == (Image *) NULL)
4037 return((Image *) NULL);
4038 polaroid_image=trim_image;
4039 return(polaroid_image);
4043 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4047 % S e p i a T o n e I m a g e %
4051 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4053 % MagickSepiaToneImage() applies a special effect to the image, similar to the
4054 % effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4055 % 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4056 % threshold of 80% is a good starting point for a reasonable tone.
4058 % The format of the SepiaToneImage method is:
4060 % Image *SepiaToneImage(const Image *image,const double threshold,
4061 % ExceptionInfo *exception)
4063 % A description of each parameter follows:
4065 % o image: the image.
4067 % o threshold: the tone threshold.
4069 % o exception: return any errors or warnings in this structure.
4072 MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4073 ExceptionInfo *exception)
4075 #define SepiaToneImageTag "SepiaTone/Image"
4094 Initialize sepia-toned image attributes.
4096 assert(image != (const Image *) NULL);
4097 assert(image->signature == MagickSignature);
4098 if (image->debug != MagickFalse)
4099 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4100 assert(exception != (ExceptionInfo *) NULL);
4101 assert(exception->signature == MagickSignature);
4102 sepia_image=CloneImage(image,0,0,MagickTrue,exception);
4103 if (sepia_image == (Image *) NULL)
4104 return((Image *) NULL);
4105 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
4107 sepia_image=DestroyImage(sepia_image);
4108 return((Image *) NULL);
4111 Tone each row of the image.
4115 image_view=AcquireCacheView(image);
4116 sepia_view=AcquireCacheView(sepia_image);
4117 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4118 #pragma omp parallel for schedule(static,4) shared(progress,status)
4120 for (y=0; y < (ssize_t) image->rows; y++)
4122 register const Quantum
4131 if (status == MagickFalse)
4133 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
4134 q=GetCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
4136 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
4141 for (x=0; x < (ssize_t) image->columns; x++)
4147 intensity=(MagickRealType) GetPixelIntensity(image,p);
4148 tone=intensity > threshold ? (MagickRealType) QuantumRange : intensity+
4149 (MagickRealType) QuantumRange-threshold;
4150 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
4151 tone=intensity > (7.0*threshold/6.0) ? (MagickRealType) QuantumRange :
4152 intensity+(MagickRealType) QuantumRange-7.0*threshold/6.0;
4153 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4154 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
4155 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
4157 if ((MagickRealType) GetPixelGreen(image,q) < tone)
4158 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4159 if ((MagickRealType) GetPixelBlue(image,q) < tone)
4160 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
4161 p+=GetPixelChannels(image);
4162 q+=GetPixelChannels(sepia_image);
4164 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4166 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4171 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4172 #pragma omp critical (MagickCore_SepiaToneImage)
4174 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4176 if (proceed == MagickFalse)
4180 sepia_view=DestroyCacheView(sepia_view);
4181 image_view=DestroyCacheView(image_view);
4182 (void) NormalizeImage(sepia_image,exception);
4183 (void) ContrastImage(sepia_image,MagickTrue,exception);
4184 if (status == MagickFalse)
4185 sepia_image=DestroyImage(sepia_image);
4186 return(sepia_image);
4190 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4194 % S h a d o w I m a g e %
4198 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4200 % ShadowImage() simulates a shadow from the specified image and returns it.
4202 % The format of the ShadowImage method is:
4204 % Image *ShadowImage(const Image *image,const double alpha,
4205 % const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4206 % ExceptionInfo *exception)
4208 % A description of each parameter follows:
4210 % o image: the image.
4212 % o alpha: percentage transparency.
4214 % o sigma: the standard deviation of the Gaussian, in pixels.
4216 % o x_offset: the shadow x-offset.
4218 % o y_offset: the shadow y-offset.
4220 % o exception: return any errors or warnings in this structure.
4223 MagickExport Image *ShadowImage(const Image *image,const double alpha,
4224 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4225 ExceptionInfo *exception)
4227 #define ShadowImageTag "Shadow/Image"
4249 assert(image != (Image *) NULL);
4250 assert(image->signature == MagickSignature);
4251 if (image->debug != MagickFalse)
4252 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4253 assert(exception != (ExceptionInfo *) NULL);
4254 assert(exception->signature == MagickSignature);
4255 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4256 if (clone_image == (Image *) NULL)
4257 return((Image *) NULL);
4258 if (IsGrayColorspace(image->colorspace) != MagickFalse)
4259 (void) TransformImageColorspace(clone_image,sRGBColorspace,exception);
4260 (void) SetImageVirtualPixelMethod(clone_image,EdgeVirtualPixelMethod,
4262 border_info.width=(size_t) floor(2.0*sigma+0.5);
4263 border_info.height=(size_t) floor(2.0*sigma+0.5);
4266 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4268 clone_image->matte=MagickTrue;
4269 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
4270 clone_image=DestroyImage(clone_image);
4271 if (border_image == (Image *) NULL)
4272 return((Image *) NULL);
4273 if (border_image->matte == MagickFalse)
4274 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
4279 image_view=AcquireCacheView(border_image);
4280 for (y=0; y < (ssize_t) border_image->rows; y++)
4291 if (status == MagickFalse)
4293 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
4295 if (q == (Quantum *) NULL)
4300 background_color=border_image->background_color;
4301 background_color.matte=MagickTrue;
4302 for (x=0; x < (ssize_t) border_image->columns; x++)
4304 if (border_image->matte != MagickFalse)
4305 background_color.alpha=GetPixelAlpha(border_image,q)*alpha/100.0;
4306 SetPixelInfoPixel(border_image,&background_color,q);
4307 q+=GetPixelChannels(border_image);
4309 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4312 image_view=DestroyCacheView(image_view);
4313 if (status == MagickFalse)
4315 border_image=DestroyImage(border_image);
4316 return((Image *) NULL);
4318 channel_mask=SetPixelChannelMask(border_image,AlphaChannel);
4319 shadow_image=BlurImage(border_image,0.0,sigma,exception);
4320 border_image=DestroyImage(border_image);
4321 if (shadow_image == (Image *) NULL)
4322 return((Image *) NULL);
4323 (void) SetPixelChannelMapMask(shadow_image,channel_mask);
4324 if (shadow_image->page.width == 0)
4325 shadow_image->page.width=shadow_image->columns;
4326 if (shadow_image->page.height == 0)
4327 shadow_image->page.height=shadow_image->rows;
4328 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4329 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4330 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4331 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
4332 return(shadow_image);
4336 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4340 % S k e t c h I m a g e %
4344 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4346 % SketchImage() simulates a pencil sketch. We convolve the image with a
4347 % Gaussian operator of the given radius and standard deviation (sigma). For
4348 % reasonable results, radius should be larger than sigma. Use a radius of 0
4349 % and SketchImage() selects a suitable radius for you. Angle gives the angle
4352 % The format of the SketchImage method is:
4354 % Image *SketchImage(const Image *image,const double radius,
4355 % const double sigma,const double angle,ExceptionInfo *exception)
4357 % A description of each parameter follows:
4359 % o image: the image.
4361 % o radius: the radius of the Gaussian, in pixels, not counting the
4364 % o sigma: the standard deviation of the Gaussian, in pixels.
4366 % o angle: apply the effect along this angle.
4368 % o exception: return any errors or warnings in this structure.
4371 MagickExport Image *SketchImage(const Image *image,const double radius,
4372 const double sigma,const double angle,ExceptionInfo *exception)
4389 **restrict random_info;
4397 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4398 MagickTrue,exception);
4399 if (random_image == (Image *) NULL)
4400 return((Image *) NULL);
4402 random_info=AcquireRandomInfoThreadSet();
4403 concurrent=GetRandomSecretKey(random_info[0]) == ~0UL ? MagickTrue :
4405 random_view=AcquireCacheView(random_image);
4406 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4407 #pragma omp parallel for schedule(static,4) shared(status) omp_concurrent(concurrent)
4409 for (y=0; y < (ssize_t) random_image->rows; y++)
4412 id = GetOpenMPThreadId();
4420 if (status == MagickFalse)
4422 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4424 if (q == (Quantum *) NULL)
4429 for (x=0; x < (ssize_t) random_image->columns; x++)
4437 if (GetPixelMask(random_image,q) != 0)
4439 q+=GetPixelChannels(random_image);
4442 value=GetPseudoRandomValue(random_info[id]);
4443 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4451 channel=GetPixelChannelMapChannel(image,i);
4452 traits=GetPixelChannelMapTraits(image,channel);
4453 if (traits == UndefinedPixelTrait)
4455 q[i]=ClampToQuantum(QuantumRange*value);
4457 q+=GetPixelChannels(random_image);
4459 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4462 random_view=DestroyCacheView(random_view);
4463 random_info=DestroyRandomInfoThreadSet(random_info);
4464 if (status == MagickFalse)
4466 random_image=DestroyImage(random_image);
4467 return(random_image);
4469 blur_image=MotionBlurImage(random_image,radius,sigma,angle,exception);
4470 random_image=DestroyImage(random_image);
4471 if (blur_image == (Image *) NULL)
4472 return((Image *) NULL);
4473 dodge_image=EdgeImage(blur_image,radius,1.0,exception);
4474 blur_image=DestroyImage(blur_image);
4475 if (dodge_image == (Image *) NULL)
4476 return((Image *) NULL);
4477 (void) NormalizeImage(dodge_image,exception);
4478 (void) NegateImage(dodge_image,MagickFalse,exception);
4479 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
4480 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4481 if (sketch_image == (Image *) NULL)
4483 dodge_image=DestroyImage(dodge_image);
4484 return((Image *) NULL);
4486 (void) CompositeImage(sketch_image,dodge_image,ColorDodgeCompositeOp,
4487 MagickTrue,0,0,exception);
4488 dodge_image=DestroyImage(dodge_image);
4489 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4490 if (blend_image == (Image *) NULL)
4492 sketch_image=DestroyImage(sketch_image);
4493 return((Image *) NULL);
4495 (void) SetImageArtifact(blend_image,"compose:args","20x80");
4496 (void) CompositeImage(sketch_image,blend_image,BlendCompositeOp,MagickTrue,
4498 blend_image=DestroyImage(blend_image);
4499 return(sketch_image);
4503 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4507 % S o l a r i z e I m a g e %
4511 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4513 % SolarizeImage() applies a special effect to the image, similar to the effect
4514 % achieved in a photo darkroom by selectively exposing areas of photo
4515 % sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4516 % measure of the extent of the solarization.
4518 % The format of the SolarizeImage method is:
4520 % MagickBooleanType SolarizeImage(Image *image,const double threshold,
4521 % ExceptionInfo *exception)
4523 % A description of each parameter follows:
4525 % o image: the image.
4527 % o threshold: Define the extent of the solarization.
4529 % o exception: return any errors or warnings in this structure.
4532 MagickExport MagickBooleanType SolarizeImage(Image *image,
4533 const double threshold,ExceptionInfo *exception)
4535 #define SolarizeImageTag "Solarize/Image"
4549 assert(image != (Image *) NULL);
4550 assert(image->signature == MagickSignature);
4551 if (image->debug != MagickFalse)
4552 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4553 if (image->storage_class == PseudoClass)
4561 for (i=0; i < (ssize_t) image->colors; i++)
4563 if ((MagickRealType) image->colormap[i].red > threshold)
4564 image->colormap[i].red=(Quantum) QuantumRange-image->colormap[i].red;
4565 if ((MagickRealType) image->colormap[i].green > threshold)
4566 image->colormap[i].green=(Quantum) QuantumRange-
4567 image->colormap[i].green;
4568 if ((MagickRealType) image->colormap[i].blue > threshold)
4569 image->colormap[i].blue=(Quantum) QuantumRange-
4570 image->colormap[i].blue;
4578 image_view=AcquireCacheView(image);
4579 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4580 #pragma omp parallel for schedule(static,4) shared(progress,status)
4582 for (y=0; y < (ssize_t) image->rows; y++)
4590 if (status == MagickFalse)
4592 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
4593 if (q == (Quantum *) NULL)
4598 for (x=0; x < (ssize_t) image->columns; x++)
4603 if (GetPixelMask(image,q) != 0)
4605 q+=GetPixelChannels(image);
4608 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4616 channel=GetPixelChannelMapChannel(image,i);
4617 traits=GetPixelChannelMapTraits(image,channel);
4618 if ((traits == UndefinedPixelTrait) ||
4619 ((traits & CopyPixelTrait) != 0))
4621 if ((MagickRealType) q[i] > threshold)
4622 q[i]=QuantumRange-q[i];
4624 q+=GetPixelChannels(image);
4626 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4628 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4633 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4634 #pragma omp critical (MagickCore_SolarizeImage)
4636 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4637 if (proceed == MagickFalse)
4641 image_view=DestroyCacheView(image_view);
4646 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4650 % S t e g a n o I m a g e %
4654 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4656 % SteganoImage() hides a digital watermark within the image. Recover
4657 % the hidden watermark later to prove that the authenticity of an image.
4658 % Offset defines the start position within the image to hide the watermark.
4660 % The format of the SteganoImage method is:
4662 % Image *SteganoImage(const Image *image,Image *watermark,
4663 % ExceptionInfo *exception)
4665 % A description of each parameter follows:
4667 % o image: the image.
4669 % o watermark: the watermark image.
4671 % o exception: return any errors or warnings in this structure.
4674 MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4675 ExceptionInfo *exception)
4677 #define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
4678 #define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
4679 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
4680 #define SteganoImageTag "Stegano/Image"
4715 Initialize steganographic image attributes.
4717 assert(image != (const Image *) NULL);
4718 assert(image->signature == MagickSignature);
4719 if (image->debug != MagickFalse)
4720 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4721 assert(watermark != (const Image *) NULL);
4722 assert(watermark->signature == MagickSignature);
4723 assert(exception != (ExceptionInfo *) NULL);
4724 assert(exception->signature == MagickSignature);
4726 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4727 if (stegano_image == (Image *) NULL)
4728 return((Image *) NULL);
4729 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
4730 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
4732 stegano_image=DestroyImage(stegano_image);
4733 return((Image *) NULL);
4736 Hide watermark in low-order bits of image.
4741 depth=stegano_image->depth;
4742 k=stegano_image->offset;
4744 watermark_view=AcquireCacheView(watermark);
4745 stegano_view=AcquireCacheView(stegano_image);
4746 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
4748 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
4750 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
4755 (void) GetOneCacheViewVirtualPixelInfo(watermark_view,x,y,&pixel,
4757 offset=k/(ssize_t) stegano_image->columns;
4758 if (offset >= (ssize_t) stegano_image->rows)
4760 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4761 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4763 if (q == (Quantum *) NULL)
4769 SetPixelRed(stegano_image,SetBit(GetPixelRed(stegano_image,q),j,
4770 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
4775 SetPixelGreen(stegano_image,SetBit(GetPixelGreen(stegano_image,q),j,
4776 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
4781 SetPixelBlue(stegano_image,SetBit(GetPixelBlue(stegano_image,q),j,
4782 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
4786 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
4792 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
4794 if (k == stegano_image->offset)
4798 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4803 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4805 if (proceed == MagickFalse)
4809 stegano_view=DestroyCacheView(stegano_view);
4810 watermark_view=DestroyCacheView(watermark_view);
4811 if (status == MagickFalse)
4813 stegano_image=DestroyImage(stegano_image);
4814 return((Image *) NULL);
4816 return(stegano_image);
4820 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4824 % S t e r e o A n a g l y p h I m a g e %
4828 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4830 % StereoAnaglyphImage() combines two images and produces a single image that
4831 % is the composite of a left and right image of a stereo pair. Special
4832 % red-green stereo glasses are required to view this effect.
4834 % The format of the StereoAnaglyphImage method is:
4836 % Image *StereoImage(const Image *left_image,const Image *right_image,
4837 % ExceptionInfo *exception)
4838 % Image *StereoAnaglyphImage(const Image *left_image,
4839 % const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
4840 % ExceptionInfo *exception)
4842 % A description of each parameter follows:
4844 % o left_image: the left image.
4846 % o right_image: the right image.
4848 % o exception: return any errors or warnings in this structure.
4850 % o x_offset: amount, in pixels, by which the left image is offset to the
4851 % right of the right image.
4853 % o y_offset: amount, in pixels, by which the left image is offset to the
4854 % bottom of the right image.
4858 MagickExport Image *StereoImage(const Image *left_image,
4859 const Image *right_image,ExceptionInfo *exception)
4861 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4864 MagickExport Image *StereoAnaglyphImage(const Image *left_image,
4865 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
4866 ExceptionInfo *exception)
4868 #define StereoImageTag "Stereo/Image"
4882 assert(left_image != (const Image *) NULL);
4883 assert(left_image->signature == MagickSignature);
4884 if (left_image->debug != MagickFalse)
4885 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4886 left_image->filename);
4887 assert(right_image != (const Image *) NULL);
4888 assert(right_image->signature == MagickSignature);
4889 assert(exception != (ExceptionInfo *) NULL);
4890 assert(exception->signature == MagickSignature);
4891 assert(right_image != (const Image *) NULL);
4893 if ((left_image->columns != right_image->columns) ||
4894 (left_image->rows != right_image->rows))
4895 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4897 Initialize stereo image attributes.
4899 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4900 MagickTrue,exception);
4901 if (stereo_image == (Image *) NULL)
4902 return((Image *) NULL);
4903 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
4905 stereo_image=DestroyImage(stereo_image);
4906 return((Image *) NULL);
4909 Copy left image to red channel and right image to blue channel.
4912 for (y=0; y < (ssize_t) stereo_image->rows; y++)
4914 register const Quantum
4924 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4926 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4927 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
4928 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4929 (r == (Quantum *) NULL))
4931 for (x=0; x < (ssize_t) stereo_image->columns; x++)
4933 SetPixelRed(image,GetPixelRed(left_image,p),r);
4934 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4935 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4936 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4937 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4938 GetPixelAlpha(right_image,q))/2,r);
4939 p+=GetPixelChannels(left_image);
4940 q+=GetPixelChannels(right_image);
4941 r+=GetPixelChannels(stereo_image);
4943 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4945 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4950 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4951 stereo_image->rows);
4952 if (proceed == MagickFalse)
4956 if (status == MagickFalse)
4958 stereo_image=DestroyImage(stereo_image);
4959 return((Image *) NULL);
4961 return(stereo_image);
4965 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4969 % S w i r l I m a g e %
4973 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4975 % SwirlImage() swirls the pixels about the center of the image, where
4976 % degrees indicates the sweep of the arc through which each pixel is moved.
4977 % You get a more dramatic effect as the degrees move from 1 to 360.
4979 % The format of the SwirlImage method is:
4981 % Image *SwirlImage(const Image *image,double degrees,
4982 % const PixelInterpolateMethod method,ExceptionInfo *exception)
4984 % A description of each parameter follows:
4986 % o image: the image.
4988 % o degrees: Define the tightness of the swirling effect.
4990 % o method: the pixel interpolation method.
4992 % o exception: return any errors or warnings in this structure.
4995 MagickExport Image *SwirlImage(const Image *image,double degrees,
4996 const PixelInterpolateMethod method,ExceptionInfo *exception)
4998 #define SwirlImageTag "Swirl/Image"
5024 Initialize swirl image attributes.
5026 assert(image != (const Image *) NULL);
5027 assert(image->signature == MagickSignature);
5028 if (image->debug != MagickFalse)
5029 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5030 assert(exception != (ExceptionInfo *) NULL);
5031 assert(exception->signature == MagickSignature);
5032 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5033 if (swirl_image == (Image *) NULL)
5034 return((Image *) NULL);
5035 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
5037 swirl_image=DestroyImage(swirl_image);
5038 return((Image *) NULL);
5040 if (swirl_image->background_color.alpha != OpaqueAlpha)
5041 swirl_image->matte=MagickTrue;
5043 Compute scaling factor.
5045 center.x=(double) image->columns/2.0;
5046 center.y=(double) image->rows/2.0;
5047 radius=MagickMax(center.x,center.y);
5050 if (image->columns > image->rows)
5051 scale.y=(double) image->columns/(double) image->rows;
5053 if (image->columns < image->rows)
5054 scale.x=(double) image->rows/(double) image->columns;
5055 degrees=(double) DegreesToRadians(degrees);
5061 image_view=AcquireCacheView(image);
5062 swirl_view=AcquireCacheView(swirl_image);
5063 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5064 #pragma omp parallel for schedule(static,4) shared(progress,status)
5066 for (y=0; y < (ssize_t) image->rows; y++)
5074 register const Quantum
5083 if (status == MagickFalse)
5085 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
5086 q=QueueCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
5088 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
5093 delta.y=scale.y*(double) (y-center.y);
5094 for (x=0; x < (ssize_t) image->columns; x++)
5097 Determine if the pixel is within an ellipse.
5099 if (GetPixelMask(image,p) != 0)
5101 p+=GetPixelChannels(image);
5102 q+=GetPixelChannels(swirl_image);
5105 delta.x=scale.x*(double) (x-center.x);
5106 distance=delta.x*delta.x+delta.y*delta.y;
5107 if (distance >= (radius*radius))
5112 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5121 channel=GetPixelChannelMapChannel(image,i);
5122 traits=GetPixelChannelMapTraits(image,channel);
5123 swirl_traits=GetPixelChannelMapTraits(swirl_image,channel);
5124 if ((traits == UndefinedPixelTrait) ||
5125 (swirl_traits == UndefinedPixelTrait))
5127 SetPixelChannel(swirl_image,channel,p[i],q);
5140 factor=1.0-sqrt((double) distance)/radius;
5141 sine=sin((double) (degrees*factor*factor));
5142 cosine=cos((double) (degrees*factor*factor));
5143 status=InterpolatePixelChannels(image,image_view,swirl_image,method,
5144 ((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5145 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
5147 p+=GetPixelChannels(image);
5148 q+=GetPixelChannels(swirl_image);
5150 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5152 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5157 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5158 #pragma omp critical (MagickCore_SwirlImage)
5160 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5161 if (proceed == MagickFalse)
5165 swirl_view=DestroyCacheView(swirl_view);
5166 image_view=DestroyCacheView(image_view);
5167 if (status == MagickFalse)
5168 swirl_image=DestroyImage(swirl_image);
5169 return(swirl_image);
5173 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5177 % T i n t I m a g e %
5181 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5183 % TintImage() applies a color vector to each pixel in the image. The length
5184 % of the vector is 0 for black and white and at its maximum for the midtones.
5185 % The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5187 % The format of the TintImage method is:
5189 % Image *TintImage(const Image *image,const char *blend,
5190 % const PixelInfo *tint,ExceptionInfo *exception)
5192 % A description of each parameter follows:
5194 % o image: the image.
5196 % o blend: A color value used for tinting.
5198 % o tint: A color value used for tinting.
5200 % o exception: return any errors or warnings in this structure.
5203 MagickExport Image *TintImage(const Image *image,const char *blend,
5204 const PixelInfo *tint,ExceptionInfo *exception)
5206 #define TintImageTag "Tint/Image"
5237 Allocate tint image.
5239 assert(image != (const Image *) NULL);
5240 assert(image->signature == MagickSignature);
5241 if (image->debug != MagickFalse)
5242 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5243 assert(exception != (ExceptionInfo *) NULL);
5244 assert(exception->signature == MagickSignature);
5245 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5246 if (tint_image == (Image *) NULL)
5247 return((Image *) NULL);
5248 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
5250 tint_image=DestroyImage(tint_image);
5251 return((Image *) NULL);
5253 if (blend == (const char *) NULL)
5256 Determine RGB values of the color.
5258 GetPixelInfo(image,&color_vector);
5259 flags=ParseGeometry(blend,&geometry_info);
5260 color_vector.red=geometry_info.rho;
5261 color_vector.green=geometry_info.rho;
5262 color_vector.blue=geometry_info.rho;
5263 color_vector.alpha=OpaqueAlpha;
5264 if ((flags & SigmaValue) != 0)
5265 color_vector.green=geometry_info.sigma;
5266 if ((flags & XiValue) != 0)
5267 color_vector.blue=geometry_info.xi;
5268 if ((flags & PsiValue) != 0)
5269 color_vector.alpha=geometry_info.psi;
5270 if (image->colorspace == CMYKColorspace)
5272 color_vector.black=geometry_info.rho;
5273 if ((flags & PsiValue) != 0)
5274 color_vector.black=geometry_info.psi;
5275 if ((flags & ChiValue) != 0)
5276 color_vector.alpha=geometry_info.chi;
5278 intensity=(MagickRealType) GetPixelInfoIntensity(tint);
5279 color_vector.red=(MagickRealType) (color_vector.red*tint->red/100.0-
5281 color_vector.green=(MagickRealType) (color_vector.green*tint->green/100.0-
5283 color_vector.blue=(MagickRealType) (color_vector.blue*tint->blue/100.0-
5285 color_vector.black=(MagickRealType) (color_vector.black*tint->black/100.0-
5287 color_vector.alpha=(MagickRealType) (color_vector.alpha*tint->alpha/100.0-
5294 image_view=AcquireCacheView(image);
5295 tint_view=AcquireCacheView(tint_image);
5296 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5297 #pragma omp parallel for schedule(static,4) shared(progress,status)
5299 for (y=0; y < (ssize_t) image->rows; y++)
5301 register const Quantum
5310 if (status == MagickFalse)
5312 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5313 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5315 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
5320 for (x=0; x < (ssize_t) image->columns; x++)
5331 if (GetPixelMask(image,p) != 0)
5333 p+=GetPixelChannels(image);
5334 q+=GetPixelChannels(tint_image);
5337 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5346 channel=GetPixelChannelMapChannel(image,i);
5347 traits=GetPixelChannelMapTraits(image,channel);
5348 tint_traits=GetPixelChannelMapTraits(tint_image,channel);
5349 if ((traits == UndefinedPixelTrait) ||
5350 (tint_traits == UndefinedPixelTrait))
5352 if ((tint_traits & CopyPixelTrait) != 0)
5354 SetPixelChannel(tint_image,channel,p[i],q);
5358 GetPixelInfo(image,&pixel);
5359 weight=QuantumScale*GetPixelRed(image,p)-0.5;
5360 pixel.red=(MagickRealType) GetPixelRed(image,p)+color_vector.red*
5361 (1.0-(4.0*(weight*weight)));
5362 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
5363 pixel.green=(MagickRealType) GetPixelGreen(image,p)+color_vector.green*
5364 (1.0-(4.0*(weight*weight)));
5365 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
5366 pixel.blue=(MagickRealType) GetPixelBlue(image,p)+color_vector.blue*
5367 (1.0-(4.0*(weight*weight)));
5368 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
5369 pixel.black=(MagickRealType) GetPixelBlack(image,p)+color_vector.black*
5370 (1.0-(4.0*(weight*weight)));
5371 SetPixelInfoPixel(tint_image,&pixel,q);
5372 p+=GetPixelChannels(image);
5373 q+=GetPixelChannels(tint_image);
5375 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5377 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5382 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5383 #pragma omp critical (MagickCore_TintImage)
5385 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5386 if (proceed == MagickFalse)
5390 tint_view=DestroyCacheView(tint_view);
5391 image_view=DestroyCacheView(image_view);
5392 if (status == MagickFalse)
5393 tint_image=DestroyImage(tint_image);
5398 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5402 % V i g n e t t e I m a g e %
5406 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5408 % VignetteImage() softens the edges of the image in vignette style.
5410 % The format of the VignetteImage method is:
5412 % Image *VignetteImage(const Image *image,const double radius,
5413 % const double sigma,const ssize_t x,const ssize_t y,
5414 % ExceptionInfo *exception)
5416 % A description of each parameter follows:
5418 % o image: the image.
5420 % o radius: the radius of the pixel neighborhood.
5422 % o sigma: the standard deviation of the Gaussian, in pixels.
5424 % o x, y: Define the x and y ellipse offset.
5426 % o exception: return any errors or warnings in this structure.
5429 MagickExport Image *VignetteImage(const Image *image,const double radius,
5430 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
5433 ellipse[MaxTextExtent];
5444 assert(image != (Image *) NULL);
5445 assert(image->signature == MagickSignature);
5446 if (image->debug != MagickFalse)
5447 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5448 assert(exception != (ExceptionInfo *) NULL);
5449 assert(exception->signature == MagickSignature);
5450 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5451 if (canvas_image == (Image *) NULL)
5452 return((Image *) NULL);
5453 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
5455 canvas_image=DestroyImage(canvas_image);
5456 return((Image *) NULL);
5458 canvas_image->matte=MagickTrue;
5459 oval_image=CloneImage(canvas_image,canvas_image->columns,canvas_image->rows,
5460 MagickTrue,exception);
5461 if (oval_image == (Image *) NULL)
5463 canvas_image=DestroyImage(canvas_image);
5464 return((Image *) NULL);
5466 (void) QueryColorCompliance("#000000",AllCompliance,
5467 &oval_image->background_color,exception);
5468 (void) SetImageBackgroundColor(oval_image,exception);
5469 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
5470 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5472 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5474 (void) FormatLocaleString(ellipse,MaxTextExtent,"ellipse %g,%g,%g,%g,"
5475 "0.0,360.0",image->columns/2.0,image->rows/2.0,image->columns/2.0-x,
5477 draw_info->primitive=AcquireString(ellipse);
5478 (void) DrawImage(oval_image,draw_info,exception);
5479 draw_info=DestroyDrawInfo(draw_info);
5480 blur_image=BlurImage(oval_image,radius,sigma,exception);
5481 oval_image=DestroyImage(oval_image);
5482 if (blur_image == (Image *) NULL)
5484 canvas_image=DestroyImage(canvas_image);
5485 return((Image *) NULL);
5487 blur_image->matte=MagickFalse;
5488 (void) CompositeImage(canvas_image,blur_image,IntensityCompositeOp,MagickTrue,
5490 blur_image=DestroyImage(blur_image);
5491 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5492 canvas_image=DestroyImage(canvas_image);
5493 return(vignette_image);
5497 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5501 % W a v e I m a g e %
5505 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5507 % WaveImage() creates a "ripple" effect in the image by shifting the pixels
5508 % vertically along a sine wave whose amplitude and wavelength is specified
5509 % by the given parameters.
5511 % The format of the WaveImage method is:
5513 % Image *WaveImage(const Image *image,const double amplitude,
5514 % const double wave_length,const PixelInterpolateMethod method,
5515 % ExceptionInfo *exception)
5517 % A description of each parameter follows:
5519 % o image: the image.
5521 % o amplitude, wave_length: Define the amplitude and wave length of the
5524 % o interpolate: the pixel interpolation method.
5526 % o exception: return any errors or warnings in this structure.
5529 MagickExport Image *WaveImage(const Image *image,const double amplitude,
5530 const double wave_length,const PixelInterpolateMethod method,
5531 ExceptionInfo *exception)
5533 #define WaveImageTag "Wave/Image"
5558 Initialize wave image attributes.
5560 assert(image != (Image *) NULL);
5561 assert(image->signature == MagickSignature);
5562 if (image->debug != MagickFalse)
5563 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5564 assert(exception != (ExceptionInfo *) NULL);
5565 assert(exception->signature == MagickSignature);
5566 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
5567 fabs(amplitude)),MagickTrue,exception);
5568 if (wave_image == (Image *) NULL)
5569 return((Image *) NULL);
5570 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
5572 wave_image=DestroyImage(wave_image);
5573 return((Image *) NULL);
5575 if (wave_image->background_color.alpha != OpaqueAlpha)
5576 wave_image->matte=MagickTrue;
5580 sine_map=(MagickRealType *) AcquireQuantumMemory((size_t) wave_image->columns,
5582 if (sine_map == (MagickRealType *) NULL)
5584 wave_image=DestroyImage(wave_image);
5585 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5587 for (i=0; i < (ssize_t) wave_image->columns; i++)
5588 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5595 image_view=AcquireCacheView(image);
5596 wave_view=AcquireCacheView(wave_image);
5597 (void) SetCacheViewVirtualPixelMethod(image_view,
5598 BackgroundVirtualPixelMethod);
5599 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5600 #pragma omp parallel for schedule(static,4) shared(progress,status)
5602 for (y=0; y < (ssize_t) wave_image->rows; y++)
5610 if (status == MagickFalse)
5612 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5614 if (q == (Quantum *) NULL)
5619 for (x=0; x < (ssize_t) wave_image->columns; x++)
5621 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5622 (double) x,(double) (y-sine_map[x]),q,exception);
5623 q+=GetPixelChannels(wave_image);
5625 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5627 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5632 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5633 #pragma omp critical (MagickCore_WaveImage)
5635 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5636 if (proceed == MagickFalse)
5640 wave_view=DestroyCacheView(wave_view);
5641 image_view=DestroyCacheView(image_view);
5642 sine_map=(MagickRealType *) RelinquishMagickMemory(sine_map);
5643 if (status == MagickFalse)
5644 wave_image=DestroyImage(wave_image);