2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % MagickCore Image Special Effects Methods %
20 % Copyright 1999-2015 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/channel.h"
50 #include "MagickCore/color.h"
51 #include "MagickCore/color-private.h"
52 #include "MagickCore/colorspace-private.h"
53 #include "MagickCore/composite.h"
54 #include "MagickCore/decorate.h"
55 #include "MagickCore/distort.h"
56 #include "MagickCore/draw.h"
57 #include "MagickCore/effect.h"
58 #include "MagickCore/enhance.h"
59 #include "MagickCore/exception.h"
60 #include "MagickCore/exception-private.h"
61 #include "MagickCore/fx.h"
62 #include "MagickCore/fx-private.h"
63 #include "MagickCore/gem.h"
64 #include "MagickCore/gem-private.h"
65 #include "MagickCore/geometry.h"
66 #include "MagickCore/layer.h"
67 #include "MagickCore/list.h"
68 #include "MagickCore/log.h"
69 #include "MagickCore/image.h"
70 #include "MagickCore/image-private.h"
71 #include "MagickCore/magick.h"
72 #include "MagickCore/memory_.h"
73 #include "MagickCore/monitor.h"
74 #include "MagickCore/monitor-private.h"
75 #include "MagickCore/option.h"
76 #include "MagickCore/pixel.h"
77 #include "MagickCore/pixel-accessor.h"
78 #include "MagickCore/property.h"
79 #include "MagickCore/quantum.h"
80 #include "MagickCore/quantum-private.h"
81 #include "MagickCore/random_.h"
82 #include "MagickCore/random-private.h"
83 #include "MagickCore/resample.h"
84 #include "MagickCore/resample-private.h"
85 #include "MagickCore/resize.h"
86 #include "MagickCore/resource_.h"
87 #include "MagickCore/splay-tree.h"
88 #include "MagickCore/statistic.h"
89 #include "MagickCore/string_.h"
90 #include "MagickCore/string-private.h"
91 #include "MagickCore/thread-private.h"
92 #include "MagickCore/transform.h"
93 #include "MagickCore/utility.h"
98 #define LeftShiftOperator 0xf5U
99 #define RightShiftOperator 0xf6U
100 #define LessThanEqualOperator 0xf7U
101 #define GreaterThanEqualOperator 0xf8U
102 #define EqualOperator 0xf9U
103 #define NotEqualOperator 0xfaU
104 #define LogicalAndOperator 0xfbU
105 #define LogicalOrOperator 0xfcU
106 #define ExponentialNotation 0xfdU
134 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138 + A c q u i r e F x I n f o %
142 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144 % AcquireFxInfo() allocates the FxInfo structure.
146 % The format of the AcquireFxInfo method is:
148 % FxInfo *AcquireFxInfo(Image *image,const char *expression,
149 % ExceptionInfo *exception)
151 % A description of each parameter follows:
153 % o image: the image.
155 % o expression: the expression.
157 % o exception: return any errors or warnings in this structure.
160 MagickPrivate FxInfo *AcquireFxInfo(const Image *image,const char *expression,
161 ExceptionInfo *exception)
175 fx_info=(FxInfo *) AcquireMagickMemory(sizeof(*fx_info));
176 if (fx_info == (FxInfo *) NULL)
177 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
178 (void) ResetMagickMemory(fx_info,0,sizeof(*fx_info));
179 fx_info->exception=AcquireExceptionInfo();
180 fx_info->images=image;
181 fx_info->colors=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
182 RelinquishAlignedMemory);
183 fx_info->symbols=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
184 RelinquishMagickMemory);
185 fx_info->view=(CacheView **) AcquireQuantumMemory(GetImageListLength(
186 fx_info->images),sizeof(*fx_info->view));
187 if (fx_info->view == (CacheView **) NULL)
188 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
190 next=GetFirstImageInList(fx_info->images);
191 for ( ; next != (Image *) NULL; next=next->next)
193 fx_info->view[i]=AcquireVirtualCacheView(next,exception);
196 fx_info->random_info=AcquireRandomInfo();
197 fx_info->expression=ConstantString(expression);
198 fx_info->file=stderr;
199 (void) SubstituteString(&fx_info->expression," ",""); /* compact string */
201 Force right-to-left associativity for unary negation.
203 (void) SubstituteString(&fx_info->expression,"-","-1.0*");
204 (void) SubstituteString(&fx_info->expression,"^-1.0*","^-");
205 (void) SubstituteString(&fx_info->expression,"E-1.0*","E-");
206 (void) SubstituteString(&fx_info->expression,"e-1.0*","e-");
208 Convert compound to simple operators.
211 *fx_op=(char) LeftShiftOperator;
212 (void) SubstituteString(&fx_info->expression,"<<",fx_op);
213 *fx_op=(char) RightShiftOperator;
214 (void) SubstituteString(&fx_info->expression,">>",fx_op);
215 *fx_op=(char) LessThanEqualOperator;
216 (void) SubstituteString(&fx_info->expression,"<=",fx_op);
217 *fx_op=(char) GreaterThanEqualOperator;
218 (void) SubstituteString(&fx_info->expression,">=",fx_op);
219 *fx_op=(char) EqualOperator;
220 (void) SubstituteString(&fx_info->expression,"==",fx_op);
221 *fx_op=(char) NotEqualOperator;
222 (void) SubstituteString(&fx_info->expression,"!=",fx_op);
223 *fx_op=(char) LogicalAndOperator;
224 (void) SubstituteString(&fx_info->expression,"&&",fx_op);
225 *fx_op=(char) LogicalOrOperator;
226 (void) SubstituteString(&fx_info->expression,"||",fx_op);
227 *fx_op=(char) ExponentialNotation;
228 (void) SubstituteString(&fx_info->expression,"**",fx_op);
233 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
237 % A d d N o i s e I m a g e %
241 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243 % AddNoiseImage() adds random noise to the image.
245 % The format of the AddNoiseImage method is:
247 % Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
248 % const double attenuate,ExceptionInfo *exception)
250 % A description of each parameter follows:
252 % o image: the image.
254 % o channel: the channel type.
256 % o noise_type: The type of noise: Uniform, Gaussian, Multiplicative,
257 % Impulse, Laplacian, or Poisson.
259 % o attenuate: attenuate the random distribution.
261 % o exception: return any errors or warnings in this structure.
264 MagickExport Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
265 const double attenuate,ExceptionInfo *exception)
267 #define AddNoiseImageTag "AddNoise/Image"
283 **restrict random_info;
288 #if defined(MAGICKCORE_OPENMP_SUPPORT)
294 Initialize noise image attributes.
296 assert(image != (const Image *) NULL);
297 assert(image->signature == MagickSignature);
298 if (image->debug != MagickFalse)
299 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
300 assert(exception != (ExceptionInfo *) NULL);
301 assert(exception->signature == MagickSignature);
302 noise_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
303 if (noise_image == (Image *) NULL)
304 return((Image *) NULL);
305 if (SetImageStorageClass(noise_image,DirectClass,exception) == MagickFalse)
307 noise_image=DestroyImage(noise_image);
308 return((Image *) NULL);
311 Add noise in each row.
315 random_info=AcquireRandomInfoThreadSet();
316 image_view=AcquireVirtualCacheView(image,exception);
317 noise_view=AcquireAuthenticCacheView(noise_image,exception);
318 #if defined(MAGICKCORE_OPENMP_SUPPORT)
319 key=GetRandomSecretKey(random_info[0]);
320 #pragma omp parallel for schedule(static,4) shared(progress,status) \
321 magick_threads(image,noise_image,image->rows,key == ~0UL)
323 for (y=0; y < (ssize_t) image->rows; y++)
326 id = GetOpenMPThreadId();
331 register const Quantum
340 if (status == MagickFalse)
342 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
343 q=QueueCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
345 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
350 for (x=0; x < (ssize_t) image->columns; x++)
355 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
357 PixelChannel channel=GetPixelChannelChannel(image,i);
358 PixelTrait traits=GetPixelChannelTraits(image,channel);
359 PixelTrait noise_traits=GetPixelChannelTraits(noise_image,channel);
360 if ((traits == UndefinedPixelTrait) ||
361 (noise_traits == UndefinedPixelTrait))
363 if (((noise_traits & CopyPixelTrait) != 0) ||
364 (GetPixelReadMask(image,p) == 0))
366 SetPixelChannel(noise_image,channel,p[i],q);
369 SetPixelChannel(noise_image,channel,ClampToQuantum(
370 GenerateDifferentialNoise(random_info[id],p[i],noise_type,attenuate)),
373 p+=GetPixelChannels(image);
374 q+=GetPixelChannels(noise_image);
376 sync=SyncCacheViewAuthenticPixels(noise_view,exception);
377 if (sync == MagickFalse)
379 if (image->progress_monitor != (MagickProgressMonitor) NULL)
384 #if defined(MAGICKCORE_OPENMP_SUPPORT)
385 #pragma omp critical (MagickCore_AddNoiseImage)
387 proceed=SetImageProgress(image,AddNoiseImageTag,progress++,
389 if (proceed == MagickFalse)
393 noise_view=DestroyCacheView(noise_view);
394 image_view=DestroyCacheView(image_view);
395 random_info=DestroyRandomInfoThreadSet(random_info);
396 if (status == MagickFalse)
397 noise_image=DestroyImage(noise_image);
402 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
406 % B l u e S h i f t I m a g e %
410 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
412 % BlueShiftImage() mutes the colors of the image to simulate a scene at
413 % nighttime in the moonlight.
415 % The format of the BlueShiftImage method is:
417 % Image *BlueShiftImage(const Image *image,const double factor,
418 % ExceptionInfo *exception)
420 % A description of each parameter follows:
422 % o image: the image.
424 % o factor: the shift factor.
426 % o exception: return any errors or warnings in this structure.
429 MagickExport Image *BlueShiftImage(const Image *image,const double factor,
430 ExceptionInfo *exception)
432 #define BlueShiftImageTag "BlueShift/Image"
451 Allocate blue shift image.
453 assert(image != (const Image *) NULL);
454 assert(image->signature == MagickSignature);
455 if (image->debug != MagickFalse)
456 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
457 assert(exception != (ExceptionInfo *) NULL);
458 assert(exception->signature == MagickSignature);
459 shift_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
460 if (shift_image == (Image *) NULL)
461 return((Image *) NULL);
462 if (SetImageStorageClass(shift_image,DirectClass,exception) == MagickFalse)
464 shift_image=DestroyImage(shift_image);
465 return((Image *) NULL);
468 Blue-shift DirectClass image.
472 image_view=AcquireVirtualCacheView(image,exception);
473 shift_view=AcquireAuthenticCacheView(shift_image,exception);
474 #if defined(MAGICKCORE_OPENMP_SUPPORT)
475 #pragma omp parallel for schedule(static,4) shared(progress,status) \
476 magick_threads(image,shift_image,image->rows,1)
478 for (y=0; y < (ssize_t) image->rows; y++)
489 register const Quantum
498 if (status == MagickFalse)
500 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
501 q=QueueCacheViewAuthenticPixels(shift_view,0,y,shift_image->columns,1,
503 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
508 for (x=0; x < (ssize_t) image->columns; x++)
510 quantum=GetPixelRed(image,p);
511 if (GetPixelGreen(image,p) < quantum)
512 quantum=GetPixelGreen(image,p);
513 if (GetPixelBlue(image,p) < quantum)
514 quantum=GetPixelBlue(image,p);
515 pixel.red=0.5*(GetPixelRed(image,p)+factor*quantum);
516 pixel.green=0.5*(GetPixelGreen(image,p)+factor*quantum);
517 pixel.blue=0.5*(GetPixelBlue(image,p)+factor*quantum);
518 quantum=GetPixelRed(image,p);
519 if (GetPixelGreen(image,p) > quantum)
520 quantum=GetPixelGreen(image,p);
521 if (GetPixelBlue(image,p) > quantum)
522 quantum=GetPixelBlue(image,p);
523 pixel.red=0.5*(pixel.red+factor*quantum);
524 pixel.green=0.5*(pixel.green+factor*quantum);
525 pixel.blue=0.5*(pixel.blue+factor*quantum);
526 SetPixelRed(shift_image,ClampToQuantum(pixel.red),q);
527 SetPixelGreen(shift_image,ClampToQuantum(pixel.green),q);
528 SetPixelBlue(shift_image,ClampToQuantum(pixel.blue),q);
529 p+=GetPixelChannels(image);
530 q+=GetPixelChannels(shift_image);
532 sync=SyncCacheViewAuthenticPixels(shift_view,exception);
533 if (sync == MagickFalse)
535 if (image->progress_monitor != (MagickProgressMonitor) NULL)
540 #if defined(MAGICKCORE_OPENMP_SUPPORT)
541 #pragma omp critical (MagickCore_BlueShiftImage)
543 proceed=SetImageProgress(image,BlueShiftImageTag,progress++,
545 if (proceed == MagickFalse)
549 image_view=DestroyCacheView(image_view);
550 shift_view=DestroyCacheView(shift_view);
551 if (status == MagickFalse)
552 shift_image=DestroyImage(shift_image);
557 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
561 % C h a r c o a l I m a g e %
565 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
567 % CharcoalImage() creates a new image that is a copy of an existing one with
568 % the edge highlighted. It allocates the memory necessary for the new Image
569 % structure and returns a pointer to the new image.
571 % The format of the CharcoalImage method is:
573 % Image *CharcoalImage(const Image *image,const double radius,
574 % const double sigma,ExceptionInfo *exception)
576 % A description of each parameter follows:
578 % o image: the image.
580 % o radius: the radius of the pixel neighborhood.
582 % o sigma: the standard deviation of the Gaussian, in pixels.
584 % o exception: return any errors or warnings in this structure.
587 MagickExport Image *CharcoalImage(const Image *image,const double radius,
588 const double sigma,ExceptionInfo *exception)
595 assert(image != (Image *) NULL);
596 assert(image->signature == MagickSignature);
597 if (image->debug != MagickFalse)
598 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
599 assert(exception != (ExceptionInfo *) NULL);
600 assert(exception->signature == MagickSignature);
601 clone_image=CloneImage(image,0,0,MagickTrue,exception);
602 if (clone_image == (Image *) NULL)
603 return((Image *) NULL);
604 edge_image=EdgeImage(clone_image,radius,exception);
605 clone_image=DestroyImage(clone_image);
606 if (edge_image == (Image *) NULL)
607 return((Image *) NULL);
608 charcoal_image=BlurImage(edge_image,radius,sigma,exception);
609 edge_image=DestroyImage(edge_image);
610 if (charcoal_image == (Image *) NULL)
611 return((Image *) NULL);
612 (void) NormalizeImage(charcoal_image,exception);
613 (void) NegateImage(charcoal_image,MagickFalse,exception);
614 (void) GrayscaleImage(charcoal_image,image->intensity,exception);
615 return(charcoal_image);
619 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
623 % C o l o r i z e I m a g e %
627 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
629 % ColorizeImage() blends the fill color with each pixel in the image.
630 % A percentage blend is specified with opacity. Control the application
631 % of different color components by specifying a different percentage for
632 % each component (e.g. 90/100/10 is 90% red, 100% green, and 10% blue).
634 % The format of the ColorizeImage method is:
636 % Image *ColorizeImage(const Image *image,const char *blend,
637 % const PixelInfo *colorize,ExceptionInfo *exception)
639 % A description of each parameter follows:
641 % o image: the image.
643 % o blend: A character string indicating the level of blending as a
646 % o colorize: A color value.
648 % o exception: return any errors or warnings in this structure.
651 MagickExport Image *ColorizeImage(const Image *image,const char *blend,
652 const PixelInfo *colorize,ExceptionInfo *exception)
654 #define ColorizeImageTag "Colorize/Image"
655 #define Colorize(pixel,blend_percentage,colorize) \
656 (((pixel)*(100.0-(blend_percentage))+(colorize)*(blend_percentage))/100.0)
683 Allocate colorized image.
685 assert(image != (const Image *) NULL);
686 assert(image->signature == MagickSignature);
687 if (image->debug != MagickFalse)
688 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
689 assert(exception != (ExceptionInfo *) NULL);
690 assert(exception->signature == MagickSignature);
691 colorize_image=CloneImage(image,0,0,MagickTrue,exception);
692 if (colorize_image == (Image *) NULL)
693 return((Image *) NULL);
694 if (SetImageStorageClass(colorize_image,DirectClass,exception) == MagickFalse)
696 colorize_image=DestroyImage(colorize_image);
697 return((Image *) NULL);
699 if ((IsGrayColorspace(colorize_image->colorspace) != MagickFalse) ||
700 (IsPixelInfoGray(colorize) != MagickFalse))
701 (void) SetImageColorspace(colorize_image,sRGBColorspace,exception);
702 if ((colorize_image->alpha_trait == UndefinedPixelTrait) &&
703 (colorize->alpha_trait != UndefinedPixelTrait))
704 (void) SetImageAlpha(colorize_image,OpaqueAlpha,exception);
705 if (blend == (const char *) NULL)
706 return(colorize_image);
707 GetPixelInfo(colorize_image,&blend_percentage);
708 flags=ParseGeometry(blend,&geometry_info);
709 blend_percentage.red=geometry_info.rho;
710 blend_percentage.green=geometry_info.rho;
711 blend_percentage.blue=geometry_info.rho;
712 blend_percentage.black=geometry_info.rho;
713 blend_percentage.alpha=(MagickRealType) TransparentAlpha;
714 if ((flags & SigmaValue) != 0)
715 blend_percentage.green=geometry_info.sigma;
716 if ((flags & XiValue) != 0)
717 blend_percentage.blue=geometry_info.xi;
718 if ((flags & PsiValue) != 0)
719 blend_percentage.alpha=geometry_info.psi;
720 if (blend_percentage.colorspace == CMYKColorspace)
722 if ((flags & PsiValue) != 0)
723 blend_percentage.black=geometry_info.psi;
724 if ((flags & ChiValue) != 0)
725 blend_percentage.alpha=geometry_info.chi;
728 Colorize DirectClass image.
732 image_view=AcquireVirtualCacheView(colorize_image,exception);
733 #if defined(MAGICKCORE_OPENMP_SUPPORT)
734 #pragma omp parallel for schedule(static,4) shared(progress,status) \
735 magick_threads(colorize_image,colorize_image,colorize_image->rows,1)
737 for (y=0; y < (ssize_t) colorize_image->rows; y++)
748 if (status == MagickFalse)
750 q=GetCacheViewAuthenticPixels(image_view,0,y,colorize_image->columns,1,
752 if (q == (Quantum *) NULL)
757 for (x=0; x < (ssize_t) colorize_image->columns; x++)
762 for (i=0; i < (ssize_t) GetPixelChannels(colorize_image); i++)
764 PixelTrait traits=GetPixelChannelTraits(colorize_image,
766 if (traits == UndefinedPixelTrait)
768 if (((traits & CopyPixelTrait) != 0) ||
769 (GetPixelReadMask(colorize_image,q) == 0))
771 SetPixelChannel(colorize_image,(PixelChannel) i,ClampToQuantum(
772 Colorize(q[i],GetPixelInfoChannel(&blend_percentage,(PixelChannel) i),
773 GetPixelInfoChannel(colorize,(PixelChannel) i))),q);
775 q+=GetPixelChannels(colorize_image);
777 sync=SyncCacheViewAuthenticPixels(image_view,exception);
778 if (sync == MagickFalse)
780 if (image->progress_monitor != (MagickProgressMonitor) NULL)
785 #if defined(MAGICKCORE_OPENMP_SUPPORT)
786 #pragma omp critical (MagickCore_ColorizeImage)
788 proceed=SetImageProgress(image,ColorizeImageTag,progress++,
789 colorize_image->rows);
790 if (proceed == MagickFalse)
794 image_view=DestroyCacheView(image_view);
795 if (status == MagickFalse)
796 colorize_image=DestroyImage(colorize_image);
797 return(colorize_image);
801 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
805 % C o l o r M a t r i x I m a g e %
809 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
811 % ColorMatrixImage() applies color transformation to an image. This method
812 % permits saturation changes, hue rotation, luminance to alpha, and various
813 % other effects. Although variable-sized transformation matrices can be used,
814 % typically one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA
815 % (or RGBA with offsets). The matrix is similar to those used by Adobe Flash
816 % except offsets are in column 6 rather than 5 (in support of CMYKA images)
817 % and offsets are normalized (divide Flash offset by 255).
819 % The format of the ColorMatrixImage method is:
821 % Image *ColorMatrixImage(const Image *image,
822 % const KernelInfo *color_matrix,ExceptionInfo *exception)
824 % A description of each parameter follows:
826 % o image: the image.
828 % o color_matrix: the color matrix.
830 % o exception: return any errors or warnings in this structure.
833 /* FUTURE: modify to make use of a MagickMatrix Mutliply function
834 That should be provided in "matrix.c"
835 (ASIDE: actually distorts should do this too but currently doesn't)
838 MagickExport Image *ColorMatrixImage(const Image *image,
839 const KernelInfo *color_matrix,ExceptionInfo *exception)
841 #define ColorMatrixImageTag "ColorMatrix/Image"
850 { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
851 { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
852 { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
853 { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
854 { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
855 { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }
876 Map given color_matrix, into a 6x6 matrix RGBKA and a constant
878 assert(image != (Image *) NULL);
879 assert(image->signature == MagickSignature);
880 if (image->debug != MagickFalse)
881 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
882 assert(exception != (ExceptionInfo *) NULL);
883 assert(exception->signature == MagickSignature);
885 for (v=0; v < (ssize_t) color_matrix->height; v++)
886 for (u=0; u < (ssize_t) color_matrix->width; u++)
888 if ((v < 6) && (u < 6))
889 ColorMatrix[v][u]=color_matrix->values[i];
893 Initialize color image.
895 color_image=CloneImage(image,0,0,MagickTrue,exception);
896 if (color_image == (Image *) NULL)
897 return((Image *) NULL);
898 if (SetImageStorageClass(color_image,DirectClass,exception) == MagickFalse)
900 color_image=DestroyImage(color_image);
901 return((Image *) NULL);
903 if (image->debug != MagickFalse)
906 format[MagickPathExtent],
909 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
910 " ColorMatrix image with color matrix:");
911 message=AcquireString("");
912 for (v=0; v < 6; v++)
915 (void) FormatLocaleString(format,MagickPathExtent,"%.20g: ",(double) v);
916 (void) ConcatenateString(&message,format);
917 for (u=0; u < 6; u++)
919 (void) FormatLocaleString(format,MagickPathExtent,"%+f ",
921 (void) ConcatenateString(&message,format);
923 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
925 message=DestroyString(message);
928 Apply the ColorMatrix to image.
932 image_view=AcquireVirtualCacheView(image,exception);
933 color_view=AcquireAuthenticCacheView(color_image,exception);
934 #if defined(MAGICKCORE_OPENMP_SUPPORT)
935 #pragma omp parallel for schedule(static,4) shared(progress,status) \
936 magick_threads(image,color_image,image->rows,1)
938 for (y=0; y < (ssize_t) image->rows; y++)
943 register const Quantum
952 if (status == MagickFalse)
954 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
955 q=GetCacheViewAuthenticPixels(color_view,0,y,color_image->columns,1,
957 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
962 GetPixelInfo(image,&pixel);
963 for (x=0; x < (ssize_t) image->columns; x++)
971 GetPixelInfoPixel(image,p,&pixel);
972 height=color_matrix->height > 6 ? 6UL : color_matrix->height;
973 for (v=0; v < (ssize_t) height; v++)
978 sum=ColorMatrix[v][0]*GetPixelRed(image,p)+ColorMatrix[v][1]*
979 GetPixelGreen(image,p)+ColorMatrix[v][2]*GetPixelBlue(image,p);
980 if (image->colorspace == CMYKColorspace)
981 sum+=ColorMatrix[v][3]*GetPixelBlack(image,p);
982 if (image->alpha_trait != UndefinedPixelTrait)
983 sum+=ColorMatrix[v][4]*GetPixelAlpha(image,p);
984 sum+=QuantumRange*ColorMatrix[v][5];
987 case 0: pixel.red=sum; break;
988 case 1: pixel.green=sum; break;
989 case 2: pixel.blue=sum; break;
990 case 3: pixel.black=sum; break;
991 case 4: pixel.alpha=sum; break;
995 SetPixelViaPixelInfo(color_image,&pixel,q);
996 p+=GetPixelChannels(image);
997 q+=GetPixelChannels(color_image);
999 if (SyncCacheViewAuthenticPixels(color_view,exception) == MagickFalse)
1001 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1006 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1007 #pragma omp critical (MagickCore_ColorMatrixImage)
1009 proceed=SetImageProgress(image,ColorMatrixImageTag,progress++,
1011 if (proceed == MagickFalse)
1015 color_view=DestroyCacheView(color_view);
1016 image_view=DestroyCacheView(image_view);
1017 if (status == MagickFalse)
1018 color_image=DestroyImage(color_image);
1019 return(color_image);
1023 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1027 + D e s t r o y F x I n f o %
1031 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1033 % DestroyFxInfo() deallocates memory associated with an FxInfo structure.
1035 % The format of the DestroyFxInfo method is:
1037 % ImageInfo *DestroyFxInfo(ImageInfo *fx_info)
1039 % A description of each parameter follows:
1041 % o fx_info: the fx info.
1044 MagickPrivate FxInfo *DestroyFxInfo(FxInfo *fx_info)
1049 fx_info->exception=DestroyExceptionInfo(fx_info->exception);
1050 fx_info->expression=DestroyString(fx_info->expression);
1051 fx_info->symbols=DestroySplayTree(fx_info->symbols);
1052 fx_info->colors=DestroySplayTree(fx_info->colors);
1053 for (i=(ssize_t) GetImageListLength(fx_info->images)-1; i >= 0; i--)
1054 fx_info->view[i]=DestroyCacheView(fx_info->view[i]);
1055 fx_info->view=(CacheView **) RelinquishMagickMemory(fx_info->view);
1056 fx_info->random_info=DestroyRandomInfo(fx_info->random_info);
1057 fx_info=(FxInfo *) RelinquishMagickMemory(fx_info);
1062 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1066 + 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 %
1070 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1072 % FxEvaluateChannelExpression() evaluates an expression and returns the
1075 % The format of the FxEvaluateExpression method is:
1077 % double FxEvaluateChannelExpression(FxInfo *fx_info,
1078 % const PixelChannel channel,const ssize_t x,const ssize_t y,
1079 % double *alpha,Exceptioninfo *exception)
1080 % double FxEvaluateExpression(FxInfo *fx_info,
1081 % double *alpha,Exceptioninfo *exception)
1083 % A description of each parameter follows:
1085 % o fx_info: the fx info.
1087 % o channel: the channel.
1089 % o x,y: the pixel position.
1091 % o alpha: the result.
1093 % o exception: return any errors or warnings in this structure.
1097 static double FxChannelStatistics(FxInfo *fx_info,Image *image,
1098 PixelChannel channel,const char *symbol,ExceptionInfo *exception)
1104 key[MagickPathExtent],
1105 statistic[MagickPathExtent];
1113 channel_mask=UndefinedChannel;
1114 for (p=symbol; (*p != '.') && (*p != '\0'); p++) ;
1120 option=ParseCommandOption(MagickPixelChannelOptions,MagickTrue,p+1);
1123 channel=(PixelChannel) option;
1124 channel_mask=(ChannelType) (channel_mask | (1 << channel));
1125 (void) SetPixelChannelMask(image,channel_mask);
1128 (void) FormatLocaleString(key,MagickPathExtent,"%p.%.20g.%s",(void *) image,
1129 (double) channel,symbol);
1130 value=(const char *) GetValueFromSplayTree(fx_info->symbols,key);
1131 if (value != (const char *) NULL)
1133 if (channel_mask != UndefinedChannel)
1134 (void) SetPixelChannelMask(image,channel_mask);
1135 return(QuantumScale*StringToDouble(value,(char **) NULL));
1137 (void) DeleteNodeFromSplayTree(fx_info->symbols,key);
1138 if (LocaleNCompare(symbol,"depth",5) == 0)
1143 depth=GetImageDepth(image,exception);
1144 (void) FormatLocaleString(statistic,MagickPathExtent,"%.20g",(double) depth);
1146 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1152 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
1153 (void) FormatLocaleString(statistic,MagickPathExtent,"%g",kurtosis);
1155 if (LocaleNCompare(symbol,"maxima",6) == 0)
1161 (void) GetImageRange(image,&minima,&maxima,exception);
1162 (void) FormatLocaleString(statistic,MagickPathExtent,"%g",maxima);
1164 if (LocaleNCompare(symbol,"mean",4) == 0)
1170 (void) GetImageMean(image,&mean,&standard_deviation,exception);
1171 (void) FormatLocaleString(statistic,MagickPathExtent,"%g",mean);
1173 if (LocaleNCompare(symbol,"minima",6) == 0)
1179 (void) GetImageRange(image,&minima,&maxima,exception);
1180 (void) FormatLocaleString(statistic,MagickPathExtent,"%g",minima);
1182 if (LocaleNCompare(symbol,"skewness",8) == 0)
1188 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
1189 (void) FormatLocaleString(statistic,MagickPathExtent,"%g",skewness);
1191 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1197 (void) GetImageMean(image,&mean,&standard_deviation,exception);
1198 (void) FormatLocaleString(statistic,MagickPathExtent,"%g",
1199 standard_deviation);
1201 if (channel_mask != UndefinedChannel)
1202 (void) SetPixelChannelMask(image,channel_mask);
1203 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(key),
1204 ConstantString(statistic));
1205 return(QuantumScale*StringToDouble(statistic,(char **) NULL));
1209 FxEvaluateSubexpression(FxInfo *,const PixelChannel,const ssize_t,
1210 const ssize_t,const char *,double *,ExceptionInfo *);
1212 static MagickOffsetType FxGCD(MagickOffsetType alpha,MagickOffsetType beta)
1215 return(FxGCD(beta,alpha % beta));
1219 static inline const char *FxSubexpression(const char *expression,
1220 ExceptionInfo *exception)
1229 subexpression=expression;
1230 while ((*subexpression != '\0') &&
1231 ((level != 1) || (strchr(")",(int) *subexpression) == (char *) NULL)))
1233 if (strchr("(",(int) *subexpression) != (char *) NULL)
1236 if (strchr(")",(int) *subexpression) != (char *) NULL)
1240 if (*subexpression == '\0')
1241 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1242 "UnbalancedParenthesis","`%s'",expression);
1243 return(subexpression);
1246 static double FxGetSymbol(FxInfo *fx_info,const PixelChannel channel,
1247 const ssize_t x,const ssize_t y,const char *expression,
1248 ExceptionInfo *exception)
1252 subexpression[MagickPathExtent],
1253 symbol[MagickPathExtent];
1280 i=GetImageIndexInList(fx_info->images);
1284 if (isalpha((int) ((unsigned char) *(p+1))) == 0)
1286 if (strchr("suv",(int) *p) != (char *) NULL)
1293 i=GetImageIndexInList(fx_info->images);
1296 case 'u': i=0; break;
1297 case 'v': i=1; break;
1304 for (p++; *p != '\0'; )
1318 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1320 i=(ssize_t) (alpha+0.5);
1326 if ((*p == 'p') && (isalpha((int) ((unsigned char) *(p+1))) == 0))
1333 for (p++; *p != '\0'; )
1347 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1358 for (p++; *p != '\0'; )
1372 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1382 length=GetImageListLength(fx_info->images);
1384 i+=(ssize_t) length;
1387 image=GetImageFromList(fx_info->images,i);
1388 if (image == (Image *) NULL)
1390 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1391 "NoSuchImage","`%s'",expression);
1394 GetPixelInfo(image,&pixel);
1395 (void) InterpolatePixelInfo(image,fx_info->view[i],image->interpolate,
1396 point.x,point.y,&pixel,exception);
1397 if ((strlen(p) > 2) && (LocaleCompare(p,"intensity") != 0) &&
1398 (LocaleCompare(p,"luma") != 0) && (LocaleCompare(p,"luminance") != 0) &&
1399 (LocaleCompare(p,"hue") != 0) && (LocaleCompare(p,"saturation") != 0) &&
1400 (LocaleCompare(p,"lightness") != 0))
1403 name[MagickPathExtent];
1405 (void) CopyMagickString(name,p,MagickPathExtent);
1406 for (q=name+(strlen(name)-1); q > name; q--)
1416 if ((strlen(name) > 2) &&
1417 (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
1422 color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
1423 if (color != (PixelInfo *) NULL)
1433 status=QueryColorCompliance(name,AllCompliance,&pixel,
1434 fx_info->exception);
1435 if (status != MagickFalse)
1437 (void) AddValueToSplayTree(fx_info->colors,ConstantString(
1438 name),ClonePixelInfo(&pixel));
1444 (void) CopyMagickString(symbol,p,MagickPathExtent);
1445 StripString(symbol);
1446 if (*symbol == '\0')
1450 case RedPixelChannel: return(QuantumScale*pixel.red);
1451 case GreenPixelChannel: return(QuantumScale*pixel.green);
1452 case BluePixelChannel: return(QuantumScale*pixel.blue);
1453 case BlackPixelChannel:
1455 if (image->colorspace != CMYKColorspace)
1457 (void) ThrowMagickException(exception,GetMagickModule(),
1458 ImageError,"ColorSeparatedImageRequired","`%s'",
1462 return(QuantumScale*pixel.black);
1464 case AlphaPixelChannel:
1469 if (pixel.alpha_trait == UndefinedPixelTrait)
1471 alpha=(double) (QuantumScale*pixel.alpha);
1474 case IndexPixelChannel:
1476 case IntensityPixelChannel:
1479 quantum_pixel[MaxPixelChannels];
1481 SetPixelViaPixelInfo(image,&pixel,quantum_pixel);
1482 return(QuantumScale*GetPixelIntensity(image,quantum_pixel));
1487 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1488 "UnableToParseExpression","`%s'",p);
1496 if (LocaleCompare(symbol,"a") == 0)
1497 return((double) (QuantumScale*pixel.alpha));
1503 if (LocaleCompare(symbol,"b") == 0)
1504 return(QuantumScale*pixel.blue);
1510 if (LocaleNCompare(symbol,"channel",7) == 0)
1518 flags=ParseGeometry(symbol+7,&channel_info);
1519 if (image->colorspace == CMYKColorspace)
1522 case CyanPixelChannel:
1524 if ((flags & RhoValue) == 0)
1526 return(channel_info.rho);
1528 case MagentaPixelChannel:
1530 if ((flags & SigmaValue) == 0)
1532 return(channel_info.sigma);
1534 case YellowPixelChannel:
1536 if ((flags & XiValue) == 0)
1538 return(channel_info.xi);
1540 case BlackPixelChannel:
1542 if ((flags & PsiValue) == 0)
1544 return(channel_info.psi);
1546 case AlphaPixelChannel:
1548 if ((flags & ChiValue) == 0)
1550 return(channel_info.chi);
1557 case RedPixelChannel:
1559 if ((flags & RhoValue) == 0)
1561 return(channel_info.rho);
1563 case GreenPixelChannel:
1565 if ((flags & SigmaValue) == 0)
1567 return(channel_info.sigma);
1569 case BluePixelChannel:
1571 if ((flags & XiValue) == 0)
1573 return(channel_info.xi);
1575 case BlackPixelChannel:
1577 if ((flags & ChiValue) == 0)
1579 return(channel_info.chi);
1581 case AlphaPixelChannel:
1583 if ((flags & PsiValue) == 0)
1585 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((double) 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)
1663 quantum_pixel[MaxPixelChannels];
1665 SetPixelViaPixelInfo(image,&pixel,quantum_pixel);
1666 return(QuantumScale*GetPixelIntensity(image,quantum_pixel));
1668 if (LocaleCompare(symbol,"i") == 0)
1675 if (LocaleCompare(symbol,"j") == 0)
1682 if (LocaleCompare(symbol,"lightness") == 0)
1689 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1693 if (LocaleCompare(symbol,"luma") == 0)
1698 luma=0.212656*pixel.red+0.715158*pixel.green+0.072186*pixel.blue;
1699 return(QuantumScale*luma);
1701 if (LocaleCompare(symbol,"luminance") == 0)
1706 luminence=0.212656*pixel.red+0.715158*pixel.green+0.072186*pixel.blue;
1707 return(QuantumScale*luminence);
1714 if (LocaleNCompare(symbol,"maxima",6) == 0)
1715 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1716 if (LocaleNCompare(symbol,"mean",4) == 0)
1717 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1718 if (LocaleNCompare(symbol,"minima",6) == 0)
1719 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1720 if (LocaleCompare(symbol,"m") == 0)
1721 return(QuantumScale*pixel.green);
1727 if (LocaleCompare(symbol,"n") == 0)
1728 return((double) GetImageListLength(fx_info->images));
1734 if (LocaleCompare(symbol,"o") == 0)
1735 return(QuantumScale*pixel.alpha);
1741 if (LocaleCompare(symbol,"page.height") == 0)
1742 return((double) image->page.height);
1743 if (LocaleCompare(symbol,"page.width") == 0)
1744 return((double) image->page.width);
1745 if (LocaleCompare(symbol,"page.x") == 0)
1746 return((double) image->page.x);
1747 if (LocaleCompare(symbol,"page.y") == 0)
1748 return((double) image->page.y);
1754 if (LocaleCompare(symbol,"resolution.x") == 0)
1755 return(image->resolution.x);
1756 if (LocaleCompare(symbol,"resolution.y") == 0)
1757 return(image->resolution.y);
1758 if (LocaleCompare(symbol,"r") == 0)
1759 return(QuantumScale*pixel.red);
1765 if (LocaleCompare(symbol,"saturation") == 0)
1772 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1776 if (LocaleNCompare(symbol,"skewness",8) == 0)
1777 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1778 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1779 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1785 if (LocaleCompare(symbol,"t") == 0)
1786 return((double) GetImageIndexInList(fx_info->images));
1792 if (LocaleCompare(symbol,"w") == 0)
1793 return((double) image->columns);
1799 if (LocaleCompare(symbol,"y") == 0)
1800 return(QuantumScale*pixel.blue);
1806 if (LocaleCompare(symbol,"z") == 0)
1811 depth=(double) GetImageDepth(image,fx_info->exception);
1819 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1820 if (value != (const char *) NULL)
1821 return((double) StringToDouble(value,(char **) NULL));
1822 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1823 "UnableToParseExpression","`%s'",symbol);
1827 static const char *FxOperatorPrecedence(const char *expression,
1828 ExceptionInfo *exception)
1832 UndefinedPrecedence,
1834 BitwiseComplementPrecedence,
1836 ExponentialNotationPrecedence,
1840 RelationalPrecedence,
1841 EquivalencyPrecedence,
1842 BitwiseAndPrecedence,
1843 BitwiseOrPrecedence,
1844 LogicalAndPrecedence,
1845 LogicalOrPrecedence,
1847 AssignmentPrecedence,
1867 subexpression=(const char *) NULL;
1868 target=NullPrecedence;
1869 while (*expression != '\0')
1871 precedence=UndefinedPrecedence;
1872 if ((isspace((int) ((unsigned char) *expression)) != 0) || (c == (int) '@'))
1877 switch (*expression)
1882 #if defined(MAGICKCORE_HAVE_ACOSH)
1883 if (LocaleNCompare(expression,"acosh",5) == 0)
1889 #if defined(MAGICKCORE_HAVE_ASINH)
1890 if (LocaleNCompare(expression,"asinh",5) == 0)
1896 #if defined(MAGICKCORE_HAVE_ATANH)
1897 if (LocaleNCompare(expression,"atanh",5) == 0)
1903 if (LocaleNCompare(expression,"atan2",5) == 0)
1913 if ((isdigit((int) ((unsigned char) c)) != 0) &&
1914 ((LocaleNCompare(expression,"E+",2) == 0) ||
1915 (LocaleNCompare(expression,"E-",2) == 0)))
1917 expression+=2; /* scientific notation */
1924 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1925 (LocaleNCompare(expression,"j1",2) == 0))
1934 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1941 if ((c == (int) '{') || (c == (int) '['))
1944 if ((c == (int) '}') || (c == (int) ']'))
1947 switch ((unsigned char) *expression)
1952 precedence=BitwiseComplementPrecedence;
1958 precedence=ExponentPrecedence;
1963 if (((c != 0) && ((isdigit((int) ((unsigned char) c)) != 0) ||
1964 (strchr(")",(int) ((unsigned char) c)) != (char *) NULL))) &&
1965 (((islower((int) ((unsigned char) *expression)) != 0) ||
1966 (strchr("(",(int) ((unsigned char) *expression)) != (char *) NULL)) ||
1967 ((isdigit((int) ((unsigned char) c)) == 0) &&
1968 (isdigit((int) ((unsigned char) *expression)) != 0))) &&
1969 (strchr("xy",(int) ((unsigned char) *expression)) == (char *) NULL))
1970 precedence=MultiplyPrecedence;
1977 precedence=MultiplyPrecedence;
1983 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
1985 precedence=AdditionPrecedence;
1988 case LeftShiftOperator:
1989 case RightShiftOperator:
1991 precedence=ShiftPrecedence;
1995 case LessThanEqualOperator:
1996 case GreaterThanEqualOperator:
1999 precedence=RelationalPrecedence;
2003 case NotEqualOperator:
2005 precedence=EquivalencyPrecedence;
2010 precedence=BitwiseAndPrecedence;
2015 precedence=BitwiseOrPrecedence;
2018 case LogicalAndOperator:
2020 precedence=LogicalAndPrecedence;
2023 case LogicalOrOperator:
2025 precedence=LogicalOrPrecedence;
2028 case ExponentialNotation:
2030 precedence=ExponentialNotationPrecedence;
2036 precedence=TernaryPrecedence;
2041 precedence=AssignmentPrecedence;
2046 precedence=CommaPrecedence;
2051 precedence=SeparatorPrecedence;
2055 if ((precedence == BitwiseComplementPrecedence) ||
2056 (precedence == TernaryPrecedence) ||
2057 (precedence == AssignmentPrecedence))
2059 if (precedence > target)
2062 Right-to-left associativity.
2065 subexpression=expression;
2069 if (precedence >= target)
2072 Left-to-right associativity.
2075 subexpression=expression;
2077 if (strchr("(",(int) *expression) != (char *) NULL)
2078 expression=FxSubexpression(expression,exception);
2079 c=(int) (*expression++);
2081 return(subexpression);
2084 static double FxEvaluateSubexpression(FxInfo *fx_info,
2085 const PixelChannel channel,const ssize_t x,const ssize_t y,
2086 const char *expression,double *beta,ExceptionInfo *exception)
2090 subexpression[MagickPathExtent];
2100 if (exception->severity >= ErrorException)
2102 while (isspace((int) ((unsigned char) *expression)) != 0)
2104 if (*expression == '\0')
2106 *subexpression='\0';
2107 p=FxOperatorPrecedence(expression,exception);
2108 if (p != (const char *) NULL)
2110 (void) CopyMagickString(subexpression,expression,(size_t)
2112 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2114 switch ((unsigned char) *p)
2118 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2119 *beta=(double) (~(size_t) *beta);
2124 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2125 return(*beta == 0.0 ? 1.0 : 0.0);
2129 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2130 channel,x,y,++p,beta,exception));
2134 case ExponentialNotation:
2136 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2137 return(alpha*(*beta));
2141 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2144 (void) ThrowMagickException(exception,GetMagickModule(),
2145 OptionError,"DivideByZero","`%s'",expression);
2148 return(alpha/(*beta));
2152 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2153 *beta=fabs(floor(((double) *beta)+0.5));
2156 (void) ThrowMagickException(exception,GetMagickModule(),
2157 OptionError,"DivideByZero","`%s'",expression);
2160 return(fmod((double) alpha,(double) *beta));
2164 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2165 return(alpha+(*beta));
2169 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2170 return(alpha-(*beta));
2172 case LeftShiftOperator:
2174 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2175 *beta=(double) ((size_t) (alpha+0.5) << (size_t) (gamma+0.5));
2178 case RightShiftOperator:
2180 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2181 *beta=(double) ((size_t) (alpha+0.5) >> (size_t) (gamma+0.5));
2186 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2187 return(alpha < *beta ? 1.0 : 0.0);
2189 case LessThanEqualOperator:
2191 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2192 return(alpha <= *beta ? 1.0 : 0.0);
2196 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2197 return(alpha > *beta ? 1.0 : 0.0);
2199 case GreaterThanEqualOperator:
2201 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2202 return(alpha >= *beta ? 1.0 : 0.0);
2206 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2207 return(fabs(alpha-(*beta)) < MagickEpsilon ? 1.0 : 0.0);
2209 case NotEqualOperator:
2211 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2212 return(fabs(alpha-(*beta)) >= MagickEpsilon ? 1.0 : 0.0);
2216 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2217 *beta=(double) ((size_t) (alpha+0.5) & (size_t) (gamma+0.5));
2222 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2223 *beta=(double) ((size_t) (alpha+0.5) | (size_t) (gamma+0.5));
2226 case LogicalAndOperator:
2228 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2229 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2232 case LogicalOrOperator:
2234 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2235 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2243 (void) CopyMagickString(subexpression,++p,MagickPathExtent);
2245 p=StringToken(":",&q);
2246 if (q == (char *) NULL)
2248 (void) ThrowMagickException(exception,GetMagickModule(),
2249 OptionError,"UnableToParseExpression","`%s'",subexpression);
2252 if (fabs((double) alpha) >= MagickEpsilon)
2253 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2255 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2261 numeric[MagickPathExtent];
2264 while (isalpha((int) ((unsigned char) *q)) != 0)
2268 (void) ThrowMagickException(exception,GetMagickModule(),
2269 OptionError,"UnableToParseExpression","`%s'",subexpression);
2272 ClearMagickException(exception);
2273 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2274 (void) FormatLocaleString(numeric,MagickPathExtent,"%g",(double)
2276 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2277 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2278 subexpression),ConstantString(numeric));
2283 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2288 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2293 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2299 if (strchr("(",(int) *expression) != (char *) NULL)
2301 (void) CopyMagickString(subexpression,expression+1,MagickPathExtent);
2302 subexpression[strlen(subexpression)-1]='\0';
2303 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2307 switch (*expression)
2311 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2317 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2323 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2325 return((double) (~(size_t) (gamma+0.5)));
2330 if (LocaleNCompare(expression,"abs",3) == 0)
2332 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2334 return((double) fabs((double) alpha));
2336 #if defined(MAGICKCORE_HAVE_ACOSH)
2337 if (LocaleNCompare(expression,"acosh",5) == 0)
2339 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2341 return((double) acosh((double) alpha));
2344 if (LocaleNCompare(expression,"acos",4) == 0)
2346 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2348 return((double) acos((double) alpha));
2350 #if defined(MAGICKCORE_HAVE_J1)
2351 if (LocaleNCompare(expression,"airy",4) == 0)
2353 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2357 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
2358 return(gamma*gamma);
2361 #if defined(MAGICKCORE_HAVE_ASINH)
2362 if (LocaleNCompare(expression,"asinh",5) == 0)
2364 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2366 return((double) asinh((double) alpha));
2369 if (LocaleNCompare(expression,"asin",4) == 0)
2371 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2373 return((double) asin((double) alpha));
2375 if (LocaleNCompare(expression,"alt",3) == 0)
2377 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2379 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
2381 if (LocaleNCompare(expression,"atan2",5) == 0)
2383 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2385 return((double) atan2((double) alpha,(double) *beta));
2387 #if defined(MAGICKCORE_HAVE_ATANH)
2388 if (LocaleNCompare(expression,"atanh",5) == 0)
2390 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2392 return((double) atanh((double) alpha));
2395 if (LocaleNCompare(expression,"atan",4) == 0)
2397 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2399 return((double) atan((double) alpha));
2401 if (LocaleCompare(expression,"a") == 0)
2402 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2408 if (LocaleCompare(expression,"b") == 0)
2409 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2415 if (LocaleNCompare(expression,"ceil",4) == 0)
2417 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2419 return((double) ceil((double) alpha));
2421 if (LocaleNCompare(expression,"clamp",5) == 0)
2423 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2431 if (LocaleNCompare(expression,"cosh",4) == 0)
2433 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2435 return((double) cosh((double) alpha));
2437 if (LocaleNCompare(expression,"cos",3) == 0)
2439 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2441 return((double) cos((double) alpha));
2443 if (LocaleCompare(expression,"c") == 0)
2444 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2450 if (LocaleNCompare(expression,"debug",5) == 0)
2455 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2457 if (fx_info->images->colorspace == CMYKColorspace)
2460 case CyanPixelChannel: type="cyan"; break;
2461 case MagentaPixelChannel: type="magenta"; break;
2462 case YellowPixelChannel: type="yellow"; break;
2463 case AlphaPixelChannel: type="opacity"; break;
2464 case BlackPixelChannel: type="black"; break;
2465 default: type="unknown"; break;
2470 case RedPixelChannel: type="red"; break;
2471 case GreenPixelChannel: type="green"; break;
2472 case BluePixelChannel: type="blue"; break;
2473 case AlphaPixelChannel: type="opacity"; break;
2474 default: type="unknown"; break;
2476 (void) CopyMagickString(subexpression,expression+6,MagickPathExtent);
2477 if (strlen(subexpression) > 1)
2478 subexpression[strlen(subexpression)-1]='\0';
2479 if (fx_info->file != (FILE *) NULL)
2480 (void) FormatLocaleFile(fx_info->file,"%s[%.20g,%.20g].%s: "
2481 "%s=%.*g\n",fx_info->images->filename,(double) x,(double) y,type,
2482 subexpression,GetMagickPrecision(),(double) alpha);
2485 if (LocaleNCompare(expression,"drc",3) == 0)
2487 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2489 return((double) (alpha/(*beta*(alpha-1.0)+1.0)));
2496 if (LocaleCompare(expression,"epsilon") == 0)
2497 return((double) MagickEpsilon);
2498 if (LocaleNCompare(expression,"exp",3) == 0)
2500 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2502 return((double) exp((double) alpha));
2504 if (LocaleCompare(expression,"e") == 0)
2505 return((double) 2.7182818284590452354);
2511 if (LocaleNCompare(expression,"floor",5) == 0)
2513 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2515 return((double) floor((double) alpha));
2522 if (LocaleNCompare(expression,"gauss",5) == 0)
2524 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2526 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
2527 return((double) gamma);
2529 if (LocaleNCompare(expression,"gcd",3) == 0)
2534 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2536 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType) (*beta+
2538 return((double) gcd);
2540 if (LocaleCompare(expression,"g") == 0)
2541 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2547 if (LocaleCompare(expression,"h") == 0)
2548 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2549 if (LocaleCompare(expression,"hue") == 0)
2550 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2551 if (LocaleNCompare(expression,"hypot",5) == 0)
2553 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2555 return((double) hypot((double) alpha,(double) *beta));
2562 if (LocaleCompare(expression,"k") == 0)
2563 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2569 if (LocaleCompare(expression,"intensity") == 0)
2570 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2571 if (LocaleNCompare(expression,"int",3) == 0)
2573 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2575 return((double) floor(alpha));
2577 #if defined(MAGICKCORE_HAVE_ISNAN)
2578 if (LocaleNCompare(expression,"isnan",5) == 0)
2580 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2582 return((double) !!isnan((double) alpha));
2585 if (LocaleCompare(expression,"i") == 0)
2586 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2592 if (LocaleCompare(expression,"j") == 0)
2593 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2594 #if defined(MAGICKCORE_HAVE_J0)
2595 if (LocaleNCompare(expression,"j0",2) == 0)
2597 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2599 return((double) j0((double) alpha));
2602 #if defined(MAGICKCORE_HAVE_J1)
2603 if (LocaleNCompare(expression,"j1",2) == 0)
2605 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2607 return((double) j1((double) alpha));
2610 #if defined(MAGICKCORE_HAVE_J1)
2611 if (LocaleNCompare(expression,"jinc",4) == 0)
2613 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2617 gamma=(double) (2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha));
2626 if (LocaleNCompare(expression,"ln",2) == 0)
2628 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2630 return((double) log((double) alpha));
2632 if (LocaleNCompare(expression,"logtwo",6) == 0)
2634 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2636 return((double) log10((double) alpha))/log10(2.0);
2638 if (LocaleNCompare(expression,"log",3) == 0)
2640 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2642 return((double) log10((double) alpha));
2644 if (LocaleCompare(expression,"lightness") == 0)
2645 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2651 if (LocaleCompare(expression,"MaxRGB") == 0)
2652 return((double) QuantumRange);
2653 if (LocaleNCompare(expression,"maxima",6) == 0)
2655 if (LocaleNCompare(expression,"max",3) == 0)
2657 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2659 return(alpha > *beta ? alpha : *beta);
2661 if (LocaleNCompare(expression,"minima",6) == 0)
2663 if (LocaleNCompare(expression,"min",3) == 0)
2665 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2667 return(alpha < *beta ? alpha : *beta);
2669 if (LocaleNCompare(expression,"mod",3) == 0)
2671 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2673 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2676 if (LocaleCompare(expression,"m") == 0)
2677 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2683 if (LocaleNCompare(expression,"not",3) == 0)
2685 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2687 return((double) (alpha < MagickEpsilon));
2689 if (LocaleCompare(expression,"n") == 0)
2690 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2696 if (LocaleCompare(expression,"Opaque") == 0)
2698 if (LocaleCompare(expression,"o") == 0)
2699 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2705 if (LocaleCompare(expression,"phi") == 0)
2706 return((double) MagickPHI);
2707 if (LocaleCompare(expression,"pi") == 0)
2708 return((double) MagickPI);
2709 if (LocaleNCompare(expression,"pow",3) == 0)
2711 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2713 return((double) pow((double) alpha,(double) *beta));
2715 if (LocaleCompare(expression,"p") == 0)
2716 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2722 if (LocaleCompare(expression,"QuantumRange") == 0)
2723 return((double) QuantumRange);
2724 if (LocaleCompare(expression,"QuantumScale") == 0)
2725 return((double) QuantumScale);
2731 if (LocaleNCompare(expression,"rand",4) == 0)
2736 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2737 #pragma omp critical (MagickCore_FxEvaluateSubexpression)
2739 alpha=GetPseudoRandomValue(fx_info->random_info);
2742 if (LocaleNCompare(expression,"round",5) == 0)
2744 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2746 return((double) floor((double) alpha+0.5));
2748 if (LocaleCompare(expression,"r") == 0)
2749 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2755 if (LocaleCompare(expression,"saturation") == 0)
2756 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2757 if (LocaleNCompare(expression,"sign",4) == 0)
2759 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2761 return(alpha < 0.0 ? -1.0 : 1.0);
2763 if (LocaleNCompare(expression,"sinc",4) == 0)
2765 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2769 gamma=(double) (sin((double) (MagickPI*alpha))/
2773 if (LocaleNCompare(expression,"sinh",4) == 0)
2775 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2777 return((double) sinh((double) alpha));
2779 if (LocaleNCompare(expression,"sin",3) == 0)
2781 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2783 return((double) sin((double) alpha));
2785 if (LocaleNCompare(expression,"sqrt",4) == 0)
2787 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2789 return((double) sqrt((double) alpha));
2791 if (LocaleNCompare(expression,"squish",6) == 0)
2793 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2795 return((double) (1.0/(1.0+exp((double) (-alpha)))));
2797 if (LocaleCompare(expression,"s") == 0)
2798 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2804 if (LocaleNCompare(expression,"tanh",4) == 0)
2806 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2808 return((double) tanh((double) alpha));
2810 if (LocaleNCompare(expression,"tan",3) == 0)
2812 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2814 return((double) tan((double) alpha));
2816 if (LocaleCompare(expression,"Transparent") == 0)
2818 if (LocaleNCompare(expression,"trunc",5) == 0)
2820 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2823 return((double) floor((double) alpha));
2824 return((double) ceil((double) alpha));
2826 if (LocaleCompare(expression,"t") == 0)
2827 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2833 if (LocaleCompare(expression,"u") == 0)
2834 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2840 if (LocaleCompare(expression,"v") == 0)
2841 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2847 if (LocaleNCompare(expression,"while",5) == 0)
2851 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2853 } while (fabs((double) alpha) >= MagickEpsilon);
2854 return((double) *beta);
2856 if (LocaleCompare(expression,"w") == 0)
2857 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2863 if (LocaleCompare(expression,"y") == 0)
2864 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2870 if (LocaleCompare(expression,"z") == 0)
2871 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2877 q=(char *) expression;
2878 alpha=InterpretSiPrefixValue(expression,&q);
2879 if (q == expression)
2880 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2884 MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
2885 double *alpha,ExceptionInfo *exception)
2890 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2895 MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
2896 double *alpha,ExceptionInfo *exception)
2905 fx_info->file=(FILE *) NULL;
2906 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2912 MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
2913 const PixelChannel channel,const ssize_t x,const ssize_t y,
2914 double *alpha,ExceptionInfo *exception)
2920 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2922 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2926 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2934 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2936 % FxImage() applies a mathematical expression to the specified image.
2938 % The format of the FxImage method is:
2940 % Image *FxImage(const Image *image,const char *expression,
2941 % ExceptionInfo *exception)
2943 % A description of each parameter follows:
2945 % o image: the image.
2947 % o expression: A mathematical expression.
2949 % o exception: return any errors or warnings in this structure.
2953 static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2958 assert(fx_info != (FxInfo **) NULL);
2959 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
2960 if (fx_info[i] != (FxInfo *) NULL)
2961 fx_info[i]=DestroyFxInfo(fx_info[i]);
2962 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
2966 static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2967 ExceptionInfo *exception)
2984 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
2985 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
2986 if (fx_info == (FxInfo **) NULL)
2988 (void) ThrowMagickException(exception,GetMagickModule(),
2989 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
2990 return((FxInfo **) NULL);
2992 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2993 if (*expression != '@')
2994 fx_expression=ConstantString(expression);
2996 fx_expression=FileToString(expression+1,~0UL,exception);
2997 for (i=0; i < (ssize_t) number_threads; i++)
3002 fx_info[i]=AcquireFxInfo(image,fx_expression,exception);
3003 if (fx_info[i] == (FxInfo *) NULL)
3005 status=FxPreprocessExpression(fx_info[i],&alpha,exception);
3006 if (status == MagickFalse)
3009 fx_expression=DestroyString(fx_expression);
3010 if (i < (ssize_t) number_threads)
3011 fx_info=DestroyFxThreadSet(fx_info);
3015 MagickExport Image *FxImage(const Image *image,const char *expression,
3016 ExceptionInfo *exception)
3018 #define FxImageTag "Fx/Image"
3039 assert(image != (Image *) NULL);
3040 assert(image->signature == MagickSignature);
3041 if (image->debug != MagickFalse)
3042 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3043 fx_info=AcquireFxThreadSet(image,expression,exception);
3044 if (fx_info == (FxInfo **) NULL)
3045 return((Image *) NULL);
3046 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3047 if (fx_image == (Image *) NULL)
3049 fx_info=DestroyFxThreadSet(fx_info);
3050 return((Image *) NULL);
3052 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
3054 fx_info=DestroyFxThreadSet(fx_info);
3055 fx_image=DestroyImage(fx_image);
3056 return((Image *) NULL);
3063 image_view=AcquireVirtualCacheView(image,exception);
3064 fx_view=AcquireAuthenticCacheView(fx_image,exception);
3065 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3066 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3067 magick_threads(image,fx_image,fx_image->rows,1)
3069 for (y=0; y < (ssize_t) fx_image->rows; y++)
3072 id = GetOpenMPThreadId();
3074 register const Quantum
3083 if (status == MagickFalse)
3085 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3086 q=QueueCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
3087 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3092 for (x=0; x < (ssize_t) fx_image->columns; x++)
3097 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3102 PixelChannel channel=GetPixelChannelChannel(image,i);
3103 PixelTrait traits=GetPixelChannelTraits(image,channel);
3104 PixelTrait fx_traits=GetPixelChannelTraits(fx_image,channel);
3105 if ((traits == UndefinedPixelTrait) ||
3106 (fx_traits == UndefinedPixelTrait))
3108 if (((fx_traits & CopyPixelTrait) != 0) ||
3109 (GetPixelReadMask(image,p) == 0))
3111 SetPixelChannel(fx_image,channel,p[i],q);
3115 (void) FxEvaluateChannelExpression(fx_info[id],channel,x,y,&alpha,
3117 q[i]=ClampToQuantum(QuantumRange*alpha);
3119 p+=GetPixelChannels(image);
3120 q+=GetPixelChannels(fx_image);
3122 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3124 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3129 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3130 #pragma omp critical (MagickCore_FxImage)
3132 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3133 if (proceed == MagickFalse)
3137 fx_view=DestroyCacheView(fx_view);
3138 image_view=DestroyCacheView(image_view);
3139 fx_info=DestroyFxThreadSet(fx_info);
3140 if (status == MagickFalse)
3141 fx_image=DestroyImage(fx_image);
3146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3150 % I m p l o d e I m a g e %
3154 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3156 % ImplodeImage() creates a new image that is a copy of an existing
3157 % one with the image pixels "implode" by the specified percentage. It
3158 % allocates the memory necessary for the new Image structure and returns a
3159 % pointer to the new image.
3161 % The format of the ImplodeImage method is:
3163 % Image *ImplodeImage(const Image *image,const double amount,
3164 % const PixelInterpolateMethod method,ExceptionInfo *exception)
3166 % A description of each parameter follows:
3168 % o implode_image: Method ImplodeImage returns a pointer to the image
3169 % after it is implode. A null image is returned if there is a memory
3172 % o image: the image.
3174 % o amount: Define the extent of the implosion.
3176 % o method: the pixel interpolation method.
3178 % o exception: return any errors or warnings in this structure.
3181 MagickExport Image *ImplodeImage(const Image *image,const double amount,
3182 const PixelInterpolateMethod method,ExceptionInfo *exception)
3184 #define ImplodeImageTag "Implode/Image"
3211 Initialize implode image attributes.
3213 assert(image != (Image *) NULL);
3214 assert(image->signature == MagickSignature);
3215 if (image->debug != MagickFalse)
3216 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3217 assert(exception != (ExceptionInfo *) NULL);
3218 assert(exception->signature == MagickSignature);
3219 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3221 if (implode_image == (Image *) NULL)
3222 return((Image *) NULL);
3223 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
3225 implode_image=DestroyImage(implode_image);
3226 return((Image *) NULL);
3228 if (implode_image->background_color.alpha != OpaqueAlpha)
3229 implode_image->alpha_trait=BlendPixelTrait;
3231 Compute scaling factor.
3235 center.x=0.5*image->columns;
3236 center.y=0.5*image->rows;
3238 if (image->columns > image->rows)
3239 scale.y=(double) image->columns/(double) image->rows;
3241 if (image->columns < image->rows)
3243 scale.x=(double) image->rows/(double) image->columns;
3251 image_view=AcquireVirtualCacheView(image,exception);
3252 interpolate_view=AcquireVirtualCacheView(image,exception);
3253 implode_view=AcquireAuthenticCacheView(implode_image,exception);
3254 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3255 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3256 magick_threads(image,implode_image,image->rows,1)
3258 for (y=0; y < (ssize_t) image->rows; y++)
3266 register const Quantum
3275 if (status == MagickFalse)
3277 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3278 q=QueueCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
3280 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3285 delta.y=scale.y*(double) (y-center.y);
3286 for (x=0; x < (ssize_t) image->columns; x++)
3292 Determine if the pixel is within an ellipse.
3294 if (GetPixelReadMask(image,p) == 0)
3296 SetPixelBackgoundColor(implode_image,q);
3297 p+=GetPixelChannels(image);
3298 q+=GetPixelChannels(implode_image);
3301 delta.x=scale.x*(double) (x-center.x);
3302 distance=delta.x*delta.x+delta.y*delta.y;
3303 if (distance >= (radius*radius))
3304 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3306 PixelChannel channel=GetPixelChannelChannel(image,i);
3307 PixelTrait traits=GetPixelChannelTraits(image,channel);
3308 PixelTrait implode_traits=GetPixelChannelTraits(implode_image,
3310 if ((traits == UndefinedPixelTrait) ||
3311 (implode_traits == UndefinedPixelTrait))
3313 SetPixelChannel(implode_image,channel,p[i],q);
3325 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/radius/
3327 status=InterpolatePixelChannels(image,interpolate_view,implode_image,
3328 method,(double) (factor*delta.x/scale.x+center.x),(double) (factor*
3329 delta.y/scale.y+center.y),q,exception);
3331 p+=GetPixelChannels(image);
3332 q+=GetPixelChannels(implode_image);
3334 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3336 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3341 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3342 #pragma omp critical (MagickCore_ImplodeImage)
3344 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3345 if (proceed == MagickFalse)
3349 implode_view=DestroyCacheView(implode_view);
3350 interpolate_view=DestroyCacheView(interpolate_view);
3351 image_view=DestroyCacheView(image_view);
3352 if (status == MagickFalse)
3353 implode_image=DestroyImage(implode_image);
3354 return(implode_image);
3358 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3362 % M o r p h I m a g e s %
3366 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3368 % The MorphImages() method requires a minimum of two images. The first
3369 % image is transformed into the second by a number of intervening images
3370 % as specified by frames.
3372 % The format of the MorphImage method is:
3374 % Image *MorphImages(const Image *image,const size_t number_frames,
3375 % ExceptionInfo *exception)
3377 % A description of each parameter follows:
3379 % o image: the image.
3381 % o number_frames: Define the number of in-between image to generate.
3382 % The more in-between frames, the smoother the morph.
3384 % o exception: return any errors or warnings in this structure.
3387 MagickExport Image *MorphImages(const Image *image,const size_t number_frames,
3388 ExceptionInfo *exception)
3390 #define MorphImageTag "Morph/Image"
3406 register const Image
3416 Clone first frame in sequence.
3418 assert(image != (Image *) NULL);
3419 assert(image->signature == MagickSignature);
3420 if (image->debug != MagickFalse)
3421 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3422 assert(exception != (ExceptionInfo *) NULL);
3423 assert(exception->signature == MagickSignature);
3424 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3425 if (morph_images == (Image *) NULL)
3426 return((Image *) NULL);
3427 if (GetNextImageInList(image) == (Image *) NULL)
3432 for (i=1; i < (ssize_t) number_frames; i++)
3434 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3435 if (morph_image == (Image *) NULL)
3437 morph_images=DestroyImageList(morph_images);
3438 return((Image *) NULL);
3440 AppendImageToList(&morph_images,morph_image);
3441 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3446 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3448 if (proceed == MagickFalse)
3452 return(GetFirstImageInList(morph_images));
3455 Morph image sequence.
3460 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3462 for (i=0; i < (ssize_t) number_frames; i++)
3468 beta=(double) (i+1.0)/(double) (number_frames+1.0);
3470 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
3471 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*next->rows+beta*
3472 GetNextImageInList(next)->rows+0.5),next->filter,exception);
3473 if (morph_image == (Image *) NULL)
3475 morph_images=DestroyImageList(morph_images);
3476 return((Image *) NULL);
3478 status=SetImageStorageClass(morph_image,DirectClass,exception);
3479 if (status == MagickFalse)
3481 morph_image=DestroyImage(morph_image);
3482 return((Image *) NULL);
3484 AppendImageToList(&morph_images,morph_image);
3485 morph_images=GetLastImageInList(morph_images);
3486 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
3487 morph_images->rows,GetNextImageInList(next)->filter,exception);
3488 if (morph_image == (Image *) NULL)
3490 morph_images=DestroyImageList(morph_images);
3491 return((Image *) NULL);
3493 image_view=AcquireVirtualCacheView(morph_image,exception);
3494 morph_view=AcquireAuthenticCacheView(morph_images,exception);
3495 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3496 #pragma omp parallel for schedule(static,4) shared(status) \
3497 magick_threads(morph_image,morph_image,morph_image->rows,1)
3499 for (y=0; y < (ssize_t) morph_images->rows; y++)
3504 register const Quantum
3513 if (status == MagickFalse)
3515 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3517 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3519 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3524 for (x=0; x < (ssize_t) morph_images->columns; x++)
3529 for (i=0; i < (ssize_t) GetPixelChannels(morph_image); i++)
3531 PixelChannel channel=GetPixelChannelChannel(morph_image,i);
3532 PixelTrait traits=GetPixelChannelTraits(morph_image,channel);
3533 PixelTrait morph_traits=GetPixelChannelTraits(morph_images,channel);
3534 if ((traits == UndefinedPixelTrait) ||
3535 (morph_traits == UndefinedPixelTrait))
3537 if (((morph_traits & CopyPixelTrait) != 0) ||
3538 (GetPixelReadMask(morph_images,p) == 0))
3540 SetPixelChannel(morph_image,channel,p[i],q);
3543 SetPixelChannel(morph_image,channel,ClampToQuantum(alpha*
3544 GetPixelChannel(morph_images,channel,q)+beta*p[i]),q);
3546 p+=GetPixelChannels(morph_image);
3547 q+=GetPixelChannels(morph_images);
3549 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3550 if (sync == MagickFalse)
3553 morph_view=DestroyCacheView(morph_view);
3554 image_view=DestroyCacheView(image_view);
3555 morph_image=DestroyImage(morph_image);
3557 if (i < (ssize_t) number_frames)
3560 Clone last frame in sequence.
3562 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3563 if (morph_image == (Image *) NULL)
3565 morph_images=DestroyImageList(morph_images);
3566 return((Image *) NULL);
3568 AppendImageToList(&morph_images,morph_image);
3569 morph_images=GetLastImageInList(morph_images);
3570 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3575 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3576 #pragma omp critical (MagickCore_MorphImages)
3578 proceed=SetImageProgress(image,MorphImageTag,scene,
3579 GetImageListLength(image));
3580 if (proceed == MagickFalse)
3585 if (GetNextImageInList(next) != (Image *) NULL)
3587 morph_images=DestroyImageList(morph_images);
3588 return((Image *) NULL);
3590 return(GetFirstImageInList(morph_images));
3594 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3598 % P l a s m a I m a g e %
3602 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3604 % PlasmaImage() initializes an image with plasma fractal values. The image
3605 % must be initialized with a base color and the random number generator
3606 % seeded before this method is called.
3608 % The format of the PlasmaImage method is:
3610 % MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
3611 % size_t attenuate,size_t depth,ExceptionInfo *exception)
3613 % A description of each parameter follows:
3615 % o image: the image.
3617 % o segment: Define the region to apply plasma fractals values.
3619 % o attenuate: Define the plasma attenuation factor.
3621 % o depth: Limit the plasma recursion depth.
3623 % o exception: return any errors or warnings in this structure.
3627 static inline Quantum PlasmaPixel(RandomInfo *random_info,
3628 const double pixel,const double noise)
3633 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
3638 static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3639 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3640 const SegmentInfo *segment,size_t attenuate,size_t depth,
3641 ExceptionInfo *exception)
3646 register const Quantum
3662 if ((fabs(segment->x2-segment->x1) <= MagickEpsilon) &&
3663 (fabs(segment->y2-segment->y1) <= MagickEpsilon))
3674 Divide the area into quadrants and recurse.
3678 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3679 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
3680 local_info=(*segment);
3681 local_info.x2=(double) x_mid;
3682 local_info.y2=(double) y_mid;
3683 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3684 &local_info,attenuate,depth,exception);
3685 local_info=(*segment);
3686 local_info.y1=(double) y_mid;
3687 local_info.x2=(double) x_mid;
3688 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3689 &local_info,attenuate,depth,exception);
3690 local_info=(*segment);
3691 local_info.x1=(double) x_mid;
3692 local_info.y2=(double) y_mid;
3693 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3694 &local_info,attenuate,depth,exception);
3695 local_info=(*segment);
3696 local_info.x1=(double) x_mid;
3697 local_info.y1=(double) y_mid;
3698 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3699 &local_info,attenuate,depth,exception);
3702 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3703 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
3704 if ((fabs(segment->x1-x_mid) < MagickEpsilon) &&
3705 (fabs(segment->x2-x_mid) < MagickEpsilon) &&
3706 (fabs(segment->y1-y_mid) < MagickEpsilon) &&
3707 (fabs(segment->y2-y_mid) < MagickEpsilon))
3708 return(MagickFalse);
3710 Average pixels and apply plasma.
3712 plasma=(double) QuantumRange/(2.0*attenuate);
3713 if ((fabs(segment->x1-x_mid) > MagickEpsilon) ||
3714 (fabs(segment->x2-x_mid) > MagickEpsilon))
3719 x=(ssize_t) ceil(segment->x1-0.5);
3720 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),1,1,
3722 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),1,1,
3724 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
3725 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3726 (q == (Quantum *) NULL))
3728 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3730 PixelChannel channel=GetPixelChannelChannel(image,i);
3731 PixelTrait traits=GetPixelChannelTraits(image,channel);
3732 if (traits == UndefinedPixelTrait)
3734 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
3736 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3737 if (fabs(segment->x1-segment->x2) > MagickEpsilon)
3742 x=(ssize_t) ceil(segment->x2-0.5);
3743 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3745 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3747 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
3748 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3749 (q == (Quantum *) NULL))
3751 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3753 PixelChannel channel=GetPixelChannelChannel(image,i);
3754 PixelTrait traits=GetPixelChannelTraits(image,channel);
3755 if (traits == UndefinedPixelTrait)
3757 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
3759 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3762 if ((fabs(segment->y1-y_mid) > MagickEpsilon) ||
3763 (fabs(segment->y2-y_mid) > MagickEpsilon))
3765 if ((fabs(segment->x1-x_mid) > MagickEpsilon) ||
3766 (fabs(segment->y2-y_mid) > MagickEpsilon))
3771 y=(ssize_t) ceil(segment->y2-0.5);
3772 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3774 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3776 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
3777 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3778 (q == (Quantum *) NULL))
3780 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3782 PixelChannel channel=GetPixelChannelChannel(image,i);
3783 PixelTrait traits=GetPixelChannelTraits(image,channel);
3784 if (traits == UndefinedPixelTrait)
3786 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
3788 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3790 if (fabs(segment->y1-segment->y2) > MagickEpsilon)
3795 y=(ssize_t) ceil(segment->y1-0.5);
3796 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3798 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3800 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,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 PixelChannel channel=GetPixelChannelChannel(image,i);
3807 PixelTrait traits=GetPixelChannelTraits(image,channel);
3808 if (traits == UndefinedPixelTrait)
3810 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
3812 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3815 if ((fabs(segment->x1-segment->x2) > MagickEpsilon) ||
3816 (fabs(segment->y1-segment->y2) > MagickEpsilon))
3821 x=(ssize_t) ceil(segment->x1-0.5);
3822 y=(ssize_t) ceil(segment->y1-0.5);
3823 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
3824 x=(ssize_t) ceil(segment->x2-0.5);
3825 y=(ssize_t) ceil(segment->y2-0.5);
3826 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
3827 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
3828 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3829 (q == (Quantum *) NULL))
3831 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3833 PixelChannel channel=GetPixelChannelChannel(image,i);
3834 PixelTrait traits=GetPixelChannelTraits(image,channel);
3835 if (traits == UndefinedPixelTrait)
3837 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
3839 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3841 if ((fabs(segment->x2-segment->x1) < 3.0) &&
3842 (fabs(segment->y2-segment->y1) < 3.0))
3844 return(MagickFalse);
3847 MagickExport MagickBooleanType PlasmaImage(Image *image,
3848 const SegmentInfo *segment,size_t attenuate,size_t depth,
3849 ExceptionInfo *exception)
3862 if (image->debug != MagickFalse)
3863 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3864 assert(image != (Image *) NULL);
3865 assert(image->signature == MagickSignature);
3866 if (image->debug != MagickFalse)
3867 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3868 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
3869 return(MagickFalse);
3870 image_view=AcquireAuthenticCacheView(image,exception);
3871 u_view=AcquireVirtualCacheView(image,exception);
3872 v_view=AcquireVirtualCacheView(image,exception);
3873 random_info=AcquireRandomInfo();
3874 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3875 attenuate,depth,exception);
3876 random_info=DestroyRandomInfo(random_info);
3877 v_view=DestroyCacheView(v_view);
3878 u_view=DestroyCacheView(u_view);
3879 image_view=DestroyCacheView(image_view);
3884 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3888 % P o l a r o i d I m a g e %
3892 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3894 % PolaroidImage() simulates a Polaroid picture.
3896 % The format of the AnnotateImage method is:
3898 % Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
3899 % const char *caption,const double angle,
3900 % const PixelInterpolateMethod method,ExceptionInfo exception)
3902 % A description of each parameter follows:
3904 % o image: the image.
3906 % o draw_info: the draw info.
3908 % o caption: the Polaroid caption.
3910 % o angle: Apply the effect along this angle.
3912 % o method: the pixel interpolation method.
3914 % o exception: return any errors or warnings in this structure.
3917 MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
3918 const char *caption,const double angle,const PixelInterpolateMethod method,
3919 ExceptionInfo *exception)
3937 Simulate a Polaroid picture.
3939 assert(image != (Image *) NULL);
3940 assert(image->signature == MagickSignature);
3941 if (image->debug != MagickFalse)
3942 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3943 assert(exception != (ExceptionInfo *) NULL);
3944 assert(exception->signature == MagickSignature);
3945 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
3946 image->rows)/25.0,10.0);
3947 height=image->rows+2*quantum;
3948 caption_image=(Image *) NULL;
3949 if (caption != (const char *) NULL)
3952 geometry[MagickPathExtent],
3971 Generate caption image.
3973 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3974 if (caption_image == (Image *) NULL)
3975 return((Image *) NULL);
3976 image_info=AcquireImageInfo();
3977 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
3978 text=InterpretImageProperties(image_info,(Image *) image,caption,
3980 image_info=DestroyImageInfo(image_info);
3981 (void) CloneString(&annotate_info->text,text);
3982 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
3984 status=SetImageExtent(caption_image,image->columns,(size_t) ((count+1)*
3985 (metrics.ascent-metrics.descent)+0.5),exception);
3986 if (status == MagickFalse)
3987 caption_image=DestroyImage(caption_image);
3990 caption_image->background_color=image->border_color;
3991 (void) SetImageBackgroundColor(caption_image,exception);
3992 (void) CloneString(&annotate_info->text,text);
3993 (void) FormatLocaleString(geometry,MagickPathExtent,"+0+%g",
3995 if (annotate_info->gravity == UndefinedGravity)
3996 (void) CloneString(&annotate_info->geometry,AcquireString(
3998 (void) AnnotateImage(caption_image,annotate_info,exception);
3999 height+=caption_image->rows;
4001 annotate_info=DestroyDrawInfo(annotate_info);
4002 text=DestroyString(text);
4004 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
4006 if (picture_image == (Image *) NULL)
4008 if (caption_image != (Image *) NULL)
4009 caption_image=DestroyImage(caption_image);
4010 return((Image *) NULL);
4012 picture_image->background_color=image->border_color;
4013 (void) SetImageBackgroundColor(picture_image,exception);
4014 (void) CompositeImage(picture_image,image,OverCompositeOp,MagickTrue,quantum,
4016 if (caption_image != (Image *) NULL)
4018 (void) CompositeImage(picture_image,caption_image,OverCompositeOp,
4019 MagickTrue,quantum,(ssize_t) (image->rows+3*quantum/2),exception);
4020 caption_image=DestroyImage(caption_image);
4022 (void) QueryColorCompliance("none",AllCompliance,
4023 &picture_image->background_color,exception);
4024 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
4025 rotate_image=RotateImage(picture_image,90.0,exception);
4026 picture_image=DestroyImage(picture_image);
4027 if (rotate_image == (Image *) NULL)
4028 return((Image *) NULL);
4029 picture_image=rotate_image;
4030 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
4031 picture_image->columns,method,exception);
4032 picture_image=DestroyImage(picture_image);
4033 if (bend_image == (Image *) NULL)
4034 return((Image *) NULL);
4035 picture_image=bend_image;
4036 rotate_image=RotateImage(picture_image,-90.0,exception);
4037 picture_image=DestroyImage(picture_image);
4038 if (rotate_image == (Image *) NULL)
4039 return((Image *) NULL);
4040 picture_image=rotate_image;
4041 picture_image->background_color=image->background_color;
4042 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
4044 if (polaroid_image == (Image *) NULL)
4046 picture_image=DestroyImage(picture_image);
4047 return(picture_image);
4049 flop_image=FlopImage(polaroid_image,exception);
4050 polaroid_image=DestroyImage(polaroid_image);
4051 if (flop_image == (Image *) NULL)
4053 picture_image=DestroyImage(picture_image);
4054 return(picture_image);
4056 polaroid_image=flop_image;
4057 (void) CompositeImage(polaroid_image,picture_image,OverCompositeOp,
4058 MagickTrue,(ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
4059 picture_image=DestroyImage(picture_image);
4060 (void) QueryColorCompliance("none",AllCompliance,
4061 &polaroid_image->background_color,exception);
4062 rotate_image=RotateImage(polaroid_image,angle,exception);
4063 polaroid_image=DestroyImage(polaroid_image);
4064 if (rotate_image == (Image *) NULL)
4065 return((Image *) NULL);
4066 polaroid_image=rotate_image;
4067 trim_image=TrimImage(polaroid_image,exception);
4068 polaroid_image=DestroyImage(polaroid_image);
4069 if (trim_image == (Image *) NULL)
4070 return((Image *) NULL);
4071 polaroid_image=trim_image;
4072 return(polaroid_image);
4076 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4080 % S e p i a T o n e I m a g e %
4084 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4086 % MagickSepiaToneImage() applies a special effect to the image, similar to the
4087 % effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4088 % 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4089 % threshold of 80% is a good starting point for a reasonable tone.
4091 % The format of the SepiaToneImage method is:
4093 % Image *SepiaToneImage(const Image *image,const double threshold,
4094 % ExceptionInfo *exception)
4096 % A description of each parameter follows:
4098 % o image: the image.
4100 % o threshold: the tone threshold.
4102 % o exception: return any errors or warnings in this structure.
4105 MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4106 ExceptionInfo *exception)
4108 #define SepiaToneImageTag "SepiaTone/Image"
4127 Initialize sepia-toned image attributes.
4129 assert(image != (const Image *) NULL);
4130 assert(image->signature == MagickSignature);
4131 if (image->debug != MagickFalse)
4132 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4133 assert(exception != (ExceptionInfo *) NULL);
4134 assert(exception->signature == MagickSignature);
4135 sepia_image=CloneImage(image,0,0,MagickTrue,exception);
4136 if (sepia_image == (Image *) NULL)
4137 return((Image *) NULL);
4138 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
4140 sepia_image=DestroyImage(sepia_image);
4141 return((Image *) NULL);
4144 Tone each row of the image.
4148 image_view=AcquireVirtualCacheView(image,exception);
4149 sepia_view=AcquireAuthenticCacheView(sepia_image,exception);
4150 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4151 #pragma omp parallel for schedule(static,4) shared(progress,status) \
4152 magick_threads(image,sepia_image,image->rows,1)
4154 for (y=0; y < (ssize_t) image->rows; y++)
4156 register const Quantum
4165 if (status == MagickFalse)
4167 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
4168 q=GetCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
4170 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
4175 for (x=0; x < (ssize_t) image->columns; x++)
4181 intensity=GetPixelIntensity(image,p);
4182 tone=intensity > threshold ? (double) QuantumRange : intensity+
4183 (double) QuantumRange-threshold;
4184 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
4185 tone=intensity > (7.0*threshold/6.0) ? (double) QuantumRange :
4186 intensity+(double) QuantumRange-7.0*threshold/6.0;
4187 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4188 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
4189 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
4191 if ((double) GetPixelGreen(image,q) < tone)
4192 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4193 if ((double) GetPixelBlue(image,q) < tone)
4194 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
4195 SetPixelAlpha(sepia_image,GetPixelAlpha(image,p),q);
4196 p+=GetPixelChannels(image);
4197 q+=GetPixelChannels(sepia_image);
4199 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4201 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4206 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4207 #pragma omp critical (MagickCore_SepiaToneImage)
4209 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4211 if (proceed == MagickFalse)
4215 sepia_view=DestroyCacheView(sepia_view);
4216 image_view=DestroyCacheView(image_view);
4217 (void) NormalizeImage(sepia_image,exception);
4218 (void) ContrastImage(sepia_image,MagickTrue,exception);
4219 if (status == MagickFalse)
4220 sepia_image=DestroyImage(sepia_image);
4221 return(sepia_image);
4225 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4229 % S h a d o w I m a g e %
4233 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4235 % ShadowImage() simulates a shadow from the specified image and returns it.
4237 % The format of the ShadowImage method is:
4239 % Image *ShadowImage(const Image *image,const double alpha,
4240 % const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4241 % ExceptionInfo *exception)
4243 % A description of each parameter follows:
4245 % o image: the image.
4247 % o alpha: percentage transparency.
4249 % o sigma: the standard deviation of the Gaussian, in pixels.
4251 % o x_offset: the shadow x-offset.
4253 % o y_offset: the shadow y-offset.
4255 % o exception: return any errors or warnings in this structure.
4258 MagickExport Image *ShadowImage(const Image *image,const double alpha,
4259 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4260 ExceptionInfo *exception)
4262 #define ShadowImageTag "Shadow/Image"
4284 assert(image != (Image *) NULL);
4285 assert(image->signature == MagickSignature);
4286 if (image->debug != MagickFalse)
4287 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4288 assert(exception != (ExceptionInfo *) NULL);
4289 assert(exception->signature == MagickSignature);
4290 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4291 if (clone_image == (Image *) NULL)
4292 return((Image *) NULL);
4293 if (IsGrayColorspace(image->colorspace) != MagickFalse)
4294 (void) SetImageColorspace(clone_image,sRGBColorspace,exception);
4295 (void) SetImageVirtualPixelMethod(clone_image,TransparentVirtualPixelMethod,
4297 border_info.width=(size_t) floor(2.0*sigma+0.5);
4298 border_info.height=(size_t) floor(2.0*sigma+0.5);
4301 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4303 clone_image->alpha_trait=BlendPixelTrait;
4304 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
4305 clone_image=DestroyImage(clone_image);
4306 if (border_image == (Image *) NULL)
4307 return((Image *) NULL);
4308 if (border_image->alpha_trait == UndefinedPixelTrait)
4309 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
4314 image_view=AcquireAuthenticCacheView(border_image,exception);
4315 for (y=0; y < (ssize_t) border_image->rows; y++)
4326 if (status == MagickFalse)
4328 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
4330 if (q == (Quantum *) NULL)
4335 background_color=border_image->background_color;
4336 background_color.alpha_trait=BlendPixelTrait;
4337 for (x=0; x < (ssize_t) border_image->columns; x++)
4339 if (border_image->alpha_trait != UndefinedPixelTrait)
4340 background_color.alpha=GetPixelAlpha(border_image,q)*alpha/100.0;
4341 SetPixelViaPixelInfo(border_image,&background_color,q);
4342 q+=GetPixelChannels(border_image);
4344 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4347 image_view=DestroyCacheView(image_view);
4348 if (status == MagickFalse)
4350 border_image=DestroyImage(border_image);
4351 return((Image *) NULL);
4353 channel_mask=SetImageChannelMask(border_image,AlphaChannel);
4354 shadow_image=BlurImage(border_image,0.0,sigma,exception);
4355 border_image=DestroyImage(border_image);
4356 if (shadow_image == (Image *) NULL)
4357 return((Image *) NULL);
4358 (void) SetPixelChannelMask(shadow_image,channel_mask);
4359 if (shadow_image->page.width == 0)
4360 shadow_image->page.width=shadow_image->columns;
4361 if (shadow_image->page.height == 0)
4362 shadow_image->page.height=shadow_image->rows;
4363 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4364 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4365 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4366 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
4367 return(shadow_image);
4371 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4375 % S k e t c h I m a g e %
4379 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4381 % SketchImage() simulates a pencil sketch. We convolve the image with a
4382 % Gaussian operator of the given radius and standard deviation (sigma). For
4383 % reasonable results, radius should be larger than sigma. Use a radius of 0
4384 % and SketchImage() selects a suitable radius for you. Angle gives the angle
4387 % The format of the SketchImage method is:
4389 % Image *SketchImage(const Image *image,const double radius,
4390 % const double sigma,const double angle,ExceptionInfo *exception)
4392 % A description of each parameter follows:
4394 % o image: the image.
4396 % o radius: the radius of the Gaussian, in pixels, not counting the
4399 % o sigma: the standard deviation of the Gaussian, in pixels.
4401 % o angle: apply the effect along this angle.
4403 % o exception: return any errors or warnings in this structure.
4406 MagickExport Image *SketchImage(const Image *image,const double radius,
4407 const double sigma,const double angle,ExceptionInfo *exception)
4423 **restrict random_info;
4428 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4436 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4437 MagickTrue,exception);
4438 if (random_image == (Image *) NULL)
4439 return((Image *) NULL);
4441 random_info=AcquireRandomInfoThreadSet();
4442 random_view=AcquireAuthenticCacheView(random_image,exception);
4443 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4444 key=GetRandomSecretKey(random_info[0]);
4445 #pragma omp parallel for schedule(static,4) shared(status) \
4446 magick_threads(random_image,random_image,random_image->rows,key == ~0UL)
4448 for (y=0; y < (ssize_t) random_image->rows; y++)
4451 id = GetOpenMPThreadId();
4459 if (status == MagickFalse)
4461 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4463 if (q == (Quantum *) NULL)
4468 for (x=0; x < (ssize_t) random_image->columns; x++)
4476 if (GetPixelReadMask(random_image,q) == 0)
4478 q+=GetPixelChannels(random_image);
4481 value=GetPseudoRandomValue(random_info[id]);
4482 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4484 PixelChannel channel=GetPixelChannelChannel(image,i);
4485 PixelTrait traits=GetPixelChannelTraits(image,channel);
4486 if (traits == UndefinedPixelTrait)
4488 q[i]=ClampToQuantum(QuantumRange*value);
4490 q+=GetPixelChannels(random_image);
4492 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4495 random_view=DestroyCacheView(random_view);
4496 random_info=DestroyRandomInfoThreadSet(random_info);
4497 if (status == MagickFalse)
4499 random_image=DestroyImage(random_image);
4500 return(random_image);
4502 blur_image=MotionBlurImage(random_image,radius,sigma,angle,exception);
4503 random_image=DestroyImage(random_image);
4504 if (blur_image == (Image *) NULL)
4505 return((Image *) NULL);
4506 dodge_image=EdgeImage(blur_image,radius,exception);
4507 blur_image=DestroyImage(blur_image);
4508 if (dodge_image == (Image *) NULL)
4509 return((Image *) NULL);
4510 (void) NormalizeImage(dodge_image,exception);
4511 (void) NegateImage(dodge_image,MagickFalse,exception);
4512 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
4513 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4514 if (sketch_image == (Image *) NULL)
4516 dodge_image=DestroyImage(dodge_image);
4517 return((Image *) NULL);
4519 (void) CompositeImage(sketch_image,dodge_image,ColorDodgeCompositeOp,
4520 MagickTrue,0,0,exception);
4521 dodge_image=DestroyImage(dodge_image);
4522 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4523 if (blend_image == (Image *) NULL)
4525 sketch_image=DestroyImage(sketch_image);
4526 return((Image *) NULL);
4528 (void) SetImageArtifact(blend_image,"compose:args","20x80");
4529 (void) CompositeImage(sketch_image,blend_image,BlendCompositeOp,MagickTrue,
4531 blend_image=DestroyImage(blend_image);
4532 return(sketch_image);
4536 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4540 % S o l a r i z e I m a g e %
4544 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4546 % SolarizeImage() applies a special effect to the image, similar to the effect
4547 % achieved in a photo darkroom by selectively exposing areas of photo
4548 % sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4549 % measure of the extent of the solarization.
4551 % The format of the SolarizeImage method is:
4553 % MagickBooleanType SolarizeImage(Image *image,const double threshold,
4554 % ExceptionInfo *exception)
4556 % A description of each parameter follows:
4558 % o image: the image.
4560 % o threshold: Define the extent of the solarization.
4562 % o exception: return any errors or warnings in this structure.
4565 MagickExport MagickBooleanType SolarizeImage(Image *image,
4566 const double threshold,ExceptionInfo *exception)
4568 #define SolarizeImageTag "Solarize/Image"
4582 assert(image != (Image *) NULL);
4583 assert(image->signature == MagickSignature);
4584 if (image->debug != MagickFalse)
4585 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4586 if (IsGrayColorspace(image->colorspace) != MagickFalse)
4587 (void) SetImageColorspace(image,sRGBColorspace,exception);
4588 if (image->storage_class == PseudoClass)
4596 for (i=0; i < (ssize_t) image->colors; i++)
4598 if ((double) image->colormap[i].red > threshold)
4599 image->colormap[i].red=QuantumRange-image->colormap[i].red;
4600 if ((double) image->colormap[i].green > threshold)
4601 image->colormap[i].green=QuantumRange-image->colormap[i].green;
4602 if ((double) image->colormap[i].blue > threshold)
4603 image->colormap[i].blue=QuantumRange-image->colormap[i].blue;
4611 image_view=AcquireAuthenticCacheView(image,exception);
4612 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4613 #pragma omp parallel for schedule(static,4) shared(progress,status) \
4614 magick_threads(image,image,image->rows,1)
4616 for (y=0; y < (ssize_t) image->rows; y++)
4624 if (status == MagickFalse)
4626 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
4627 if (q == (Quantum *) NULL)
4632 for (x=0; x < (ssize_t) image->columns; x++)
4637 if (GetPixelReadMask(image,q) == 0)
4639 q+=GetPixelChannels(image);
4642 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4644 PixelChannel channel=GetPixelChannelChannel(image,i);
4645 PixelTrait traits=GetPixelChannelTraits(image,channel);
4646 if ((traits & UpdatePixelTrait) == 0)
4648 if ((double) q[i] > threshold)
4649 q[i]=QuantumRange-q[i];
4651 q+=GetPixelChannels(image);
4653 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4655 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4660 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4661 #pragma omp critical (MagickCore_SolarizeImage)
4663 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4664 if (proceed == MagickFalse)
4668 image_view=DestroyCacheView(image_view);
4673 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4677 % S t e g a n o I m a g e %
4681 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4683 % SteganoImage() hides a digital watermark within the image. Recover
4684 % the hidden watermark later to prove that the authenticity of an image.
4685 % Offset defines the start position within the image to hide the watermark.
4687 % The format of the SteganoImage method is:
4689 % Image *SteganoImage(const Image *image,Image *watermark,
4690 % ExceptionInfo *exception)
4692 % A description of each parameter follows:
4694 % o image: the image.
4696 % o watermark: the watermark image.
4698 % o exception: return any errors or warnings in this structure.
4701 MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4702 ExceptionInfo *exception)
4704 #define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
4705 #define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
4706 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
4707 #define SteganoImageTag "Stegano/Image"
4742 Initialize steganographic image attributes.
4744 assert(image != (const Image *) NULL);
4745 assert(image->signature == MagickSignature);
4746 if (image->debug != MagickFalse)
4747 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4748 assert(watermark != (const Image *) NULL);
4749 assert(watermark->signature == MagickSignature);
4750 assert(exception != (ExceptionInfo *) NULL);
4751 assert(exception->signature == MagickSignature);
4753 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4754 if (stegano_image == (Image *) NULL)
4755 return((Image *) NULL);
4756 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
4757 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
4759 stegano_image=DestroyImage(stegano_image);
4760 return((Image *) NULL);
4763 Hide watermark in low-order bits of image.
4768 depth=stegano_image->depth;
4769 k=stegano_image->offset;
4771 watermark_view=AcquireVirtualCacheView(watermark,exception);
4772 stegano_view=AcquireAuthenticCacheView(stegano_image,exception);
4773 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
4775 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
4777 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
4782 (void) GetOneCacheViewVirtualPixelInfo(watermark_view,x,y,&pixel,
4784 offset=k/(ssize_t) stegano_image->columns;
4785 if (offset >= (ssize_t) stegano_image->rows)
4787 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4788 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4790 if (q == (Quantum *) NULL)
4796 SetPixelRed(stegano_image,SetBit(GetPixelRed(stegano_image,q),j,
4797 GetBit(GetPixelInfoIntensity(stegano_image,&pixel),i)),q);
4802 SetPixelGreen(stegano_image,SetBit(GetPixelGreen(stegano_image,q),j,
4803 GetBit(GetPixelInfoIntensity(stegano_image,&pixel),i)),q);
4808 SetPixelBlue(stegano_image,SetBit(GetPixelBlue(stegano_image,q),j,
4809 GetBit(GetPixelInfoIntensity(stegano_image,&pixel),i)),q);
4813 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
4819 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
4821 if (k == stegano_image->offset)
4825 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4830 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4832 if (proceed == MagickFalse)
4836 stegano_view=DestroyCacheView(stegano_view);
4837 watermark_view=DestroyCacheView(watermark_view);
4838 if (status == MagickFalse)
4839 stegano_image=DestroyImage(stegano_image);
4840 return(stegano_image);
4844 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4848 % S t e r e o A n a g l y p h I m a g e %
4852 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4854 % StereoAnaglyphImage() combines two images and produces a single image that
4855 % is the composite of a left and right image of a stereo pair. Special
4856 % red-green stereo glasses are required to view this effect.
4858 % The format of the StereoAnaglyphImage method is:
4860 % Image *StereoImage(const Image *left_image,const Image *right_image,
4861 % ExceptionInfo *exception)
4862 % Image *StereoAnaglyphImage(const Image *left_image,
4863 % const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
4864 % ExceptionInfo *exception)
4866 % A description of each parameter follows:
4868 % o left_image: the left image.
4870 % o right_image: the right image.
4872 % o exception: return any errors or warnings in this structure.
4874 % o x_offset: amount, in pixels, by which the left image is offset to the
4875 % right of the right image.
4877 % o y_offset: amount, in pixels, by which the left image is offset to the
4878 % bottom of the right image.
4882 MagickExport Image *StereoImage(const Image *left_image,
4883 const Image *right_image,ExceptionInfo *exception)
4885 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4888 MagickExport Image *StereoAnaglyphImage(const Image *left_image,
4889 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
4890 ExceptionInfo *exception)
4892 #define StereoImageTag "Stereo/Image"
4906 assert(left_image != (const Image *) NULL);
4907 assert(left_image->signature == MagickSignature);
4908 if (left_image->debug != MagickFalse)
4909 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4910 left_image->filename);
4911 assert(right_image != (const Image *) NULL);
4912 assert(right_image->signature == MagickSignature);
4913 assert(exception != (ExceptionInfo *) NULL);
4914 assert(exception->signature == MagickSignature);
4915 assert(right_image != (const Image *) NULL);
4917 if ((left_image->columns != right_image->columns) ||
4918 (left_image->rows != right_image->rows))
4919 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4921 Initialize stereo image attributes.
4923 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4924 MagickTrue,exception);
4925 if (stereo_image == (Image *) NULL)
4926 return((Image *) NULL);
4927 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
4929 stereo_image=DestroyImage(stereo_image);
4930 return((Image *) NULL);
4932 (void) SetImageColorspace(stereo_image,sRGBColorspace,exception);
4934 Copy left image to red channel and right image to blue channel.
4937 for (y=0; y < (ssize_t) stereo_image->rows; y++)
4939 register const Quantum
4949 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4951 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4952 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
4953 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4954 (r == (Quantum *) NULL))
4956 for (x=0; x < (ssize_t) stereo_image->columns; x++)
4958 SetPixelRed(image,GetPixelRed(left_image,p),r);
4959 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4960 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4961 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4962 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4963 GetPixelAlpha(right_image,q))/2,r);
4964 p+=GetPixelChannels(left_image);
4965 q+=GetPixelChannels(right_image);
4966 r+=GetPixelChannels(stereo_image);
4968 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4970 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4975 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4976 stereo_image->rows);
4977 if (proceed == MagickFalse)
4981 if (status == MagickFalse)
4982 stereo_image=DestroyImage(stereo_image);
4983 return(stereo_image);
4987 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4991 % S w i r l I m a g e %
4995 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4997 % SwirlImage() swirls the pixels about the center of the image, where
4998 % degrees indicates the sweep of the arc through which each pixel is moved.
4999 % You get a more dramatic effect as the degrees move from 1 to 360.
5001 % The format of the SwirlImage method is:
5003 % Image *SwirlImage(const Image *image,double degrees,
5004 % const PixelInterpolateMethod method,ExceptionInfo *exception)
5006 % A description of each parameter follows:
5008 % o image: the image.
5010 % o degrees: Define the tightness of the swirling effect.
5012 % o method: the pixel interpolation method.
5014 % o exception: return any errors or warnings in this structure.
5017 MagickExport Image *SwirlImage(const Image *image,double degrees,
5018 const PixelInterpolateMethod method,ExceptionInfo *exception)
5020 #define SwirlImageTag "Swirl/Image"
5047 Initialize swirl image attributes.
5049 assert(image != (const Image *) NULL);
5050 assert(image->signature == MagickSignature);
5051 if (image->debug != MagickFalse)
5052 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5053 assert(exception != (ExceptionInfo *) NULL);
5054 assert(exception->signature == MagickSignature);
5055 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5056 if (swirl_image == (Image *) NULL)
5057 return((Image *) NULL);
5058 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
5060 swirl_image=DestroyImage(swirl_image);
5061 return((Image *) NULL);
5063 if (swirl_image->background_color.alpha != OpaqueAlpha)
5064 swirl_image->alpha_trait=BlendPixelTrait;
5066 Compute scaling factor.
5068 center.x=(double) image->columns/2.0;
5069 center.y=(double) image->rows/2.0;
5070 radius=MagickMax(center.x,center.y);
5073 if (image->columns > image->rows)
5074 scale.y=(double) image->columns/(double) image->rows;
5076 if (image->columns < image->rows)
5077 scale.x=(double) image->rows/(double) image->columns;
5078 degrees=(double) DegreesToRadians(degrees);
5084 image_view=AcquireVirtualCacheView(image,exception);
5085 interpolate_view=AcquireVirtualCacheView(image,exception);
5086 swirl_view=AcquireAuthenticCacheView(swirl_image,exception);
5087 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5088 #pragma omp parallel for schedule(static,4) shared(progress,status) \
5089 magick_threads(image,swirl_image,image->rows,1)
5091 for (y=0; y < (ssize_t) image->rows; y++)
5099 register const Quantum
5108 if (status == MagickFalse)
5110 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5111 q=QueueCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
5113 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
5118 delta.y=scale.y*(double) (y-center.y);
5119 for (x=0; x < (ssize_t) image->columns; x++)
5122 Determine if the pixel is within an ellipse.
5124 if (GetPixelReadMask(image,p) == 0)
5126 SetPixelBackgoundColor(swirl_image,q);
5127 p+=GetPixelChannels(image);
5128 q+=GetPixelChannels(swirl_image);
5131 delta.x=scale.x*(double) (x-center.x);
5132 distance=delta.x*delta.x+delta.y*delta.y;
5133 if (distance >= (radius*radius))
5138 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5140 PixelChannel channel=GetPixelChannelChannel(image,i);
5141 PixelTrait traits=GetPixelChannelTraits(image,channel);
5142 PixelTrait swirl_traits=GetPixelChannelTraits(swirl_image,channel);
5143 if ((traits == UndefinedPixelTrait) ||
5144 (swirl_traits == UndefinedPixelTrait))
5146 SetPixelChannel(swirl_image,channel,p[i],q);
5159 factor=1.0-sqrt((double) distance)/radius;
5160 sine=sin((double) (degrees*factor*factor));
5161 cosine=cos((double) (degrees*factor*factor));
5162 status=InterpolatePixelChannels(image,interpolate_view,swirl_image,
5163 method,((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5164 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
5166 p+=GetPixelChannels(image);
5167 q+=GetPixelChannels(swirl_image);
5169 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5171 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5176 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5177 #pragma omp critical (MagickCore_SwirlImage)
5179 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5180 if (proceed == MagickFalse)
5184 swirl_view=DestroyCacheView(swirl_view);
5185 interpolate_view=DestroyCacheView(interpolate_view);
5186 image_view=DestroyCacheView(image_view);
5187 if (status == MagickFalse)
5188 swirl_image=DestroyImage(swirl_image);
5189 return(swirl_image);
5193 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5197 % T i n t I m a g e %
5201 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5203 % TintImage() applies a color vector to each pixel in the image. The length
5204 % of the vector is 0 for black and white and at its maximum for the midtones.
5205 % The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5207 % The format of the TintImage method is:
5209 % Image *TintImage(const Image *image,const char *blend,
5210 % const PixelInfo *tint,ExceptionInfo *exception)
5212 % A description of each parameter follows:
5214 % o image: the image.
5216 % o blend: A color value used for tinting.
5218 % o tint: A color value used for tinting.
5220 % o exception: return any errors or warnings in this structure.
5223 MagickExport Image *TintImage(const Image *image,const char *blend,
5224 const PixelInfo *tint,ExceptionInfo *exception)
5226 #define TintImageTag "Tint/Image"
5257 Allocate tint image.
5259 assert(image != (const Image *) NULL);
5260 assert(image->signature == MagickSignature);
5261 if (image->debug != MagickFalse)
5262 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5263 assert(exception != (ExceptionInfo *) NULL);
5264 assert(exception->signature == MagickSignature);
5265 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5266 if (tint_image == (Image *) NULL)
5267 return((Image *) NULL);
5268 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
5270 tint_image=DestroyImage(tint_image);
5271 return((Image *) NULL);
5273 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
5274 (IsPixelInfoGray(tint) == MagickFalse))
5275 (void) SetImageColorspace(tint_image,sRGBColorspace,exception);
5276 if (blend == (const char *) NULL)
5279 Determine RGB values of the color.
5281 GetPixelInfo(image,&color_vector);
5282 flags=ParseGeometry(blend,&geometry_info);
5283 color_vector.red=geometry_info.rho;
5284 color_vector.green=geometry_info.rho;
5285 color_vector.blue=geometry_info.rho;
5286 color_vector.alpha=(MagickRealType) OpaqueAlpha;
5287 if ((flags & SigmaValue) != 0)
5288 color_vector.green=geometry_info.sigma;
5289 if ((flags & XiValue) != 0)
5290 color_vector.blue=geometry_info.xi;
5291 if ((flags & PsiValue) != 0)
5292 color_vector.alpha=geometry_info.psi;
5293 if (image->colorspace == CMYKColorspace)
5295 color_vector.black=geometry_info.rho;
5296 if ((flags & PsiValue) != 0)
5297 color_vector.black=geometry_info.psi;
5298 if ((flags & ChiValue) != 0)
5299 color_vector.alpha=geometry_info.chi;
5301 intensity=(double) GetPixelInfoIntensity(image,tint);
5302 color_vector.red=(double) (color_vector.red*tint->red/100.0-intensity);
5303 color_vector.green=(double) (color_vector.green*tint->green/100.0-intensity);
5304 color_vector.blue=(double) (color_vector.blue*tint->blue/100.0-intensity);
5305 color_vector.black=(double) (color_vector.black*tint->black/100.0-intensity);
5306 color_vector.alpha=(double) (color_vector.alpha*tint->alpha/100.0-intensity);
5312 image_view=AcquireVirtualCacheView(image,exception);
5313 tint_view=AcquireAuthenticCacheView(tint_image,exception);
5314 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5315 #pragma omp parallel for schedule(static,4) shared(progress,status) \
5316 magick_threads(image,tint_image,image->rows,1)
5318 for (y=0; y < (ssize_t) image->rows; y++)
5320 register const Quantum
5329 if (status == MagickFalse)
5331 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5332 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5334 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
5339 for (x=0; x < (ssize_t) image->columns; x++)
5350 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5352 PixelChannel channel=GetPixelChannelChannel(image,i);
5353 PixelTrait traits=GetPixelChannelTraits(image,channel);
5354 PixelTrait tint_traits=GetPixelChannelTraits(tint_image,channel);
5355 if ((traits == UndefinedPixelTrait) ||
5356 (tint_traits == UndefinedPixelTrait))
5358 if (((tint_traits & CopyPixelTrait) != 0) ||
5359 (GetPixelReadMask(image,p) == 0))
5361 SetPixelChannel(tint_image,channel,p[i],q);
5365 GetPixelInfo(image,&pixel);
5366 weight=QuantumScale*GetPixelRed(image,p)-0.5;
5367 pixel.red=(double) GetPixelRed(image,p)+color_vector.red*(1.0-(4.0*
5369 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
5370 pixel.green=(double) GetPixelGreen(image,p)+color_vector.green*(1.0-(4.0*
5372 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
5373 pixel.blue=(double) GetPixelBlue(image,p)+color_vector.blue*(1.0-(4.0*
5375 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
5376 pixel.black=(double) GetPixelBlack(image,p)+color_vector.black*(1.0-(4.0*
5378 SetPixelViaPixelInfo(tint_image,&pixel,q);
5379 p+=GetPixelChannels(image);
5380 q+=GetPixelChannels(tint_image);
5382 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5384 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5389 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5390 #pragma omp critical (MagickCore_TintImage)
5392 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5393 if (proceed == MagickFalse)
5397 tint_view=DestroyCacheView(tint_view);
5398 image_view=DestroyCacheView(image_view);
5399 if (status == MagickFalse)
5400 tint_image=DestroyImage(tint_image);
5405 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5409 % V i g n e t t e I m a g e %
5413 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5415 % VignetteImage() softens the edges of the image in vignette style.
5417 % The format of the VignetteImage method is:
5419 % Image *VignetteImage(const Image *image,const double radius,
5420 % const double sigma,const ssize_t x,const ssize_t y,
5421 % ExceptionInfo *exception)
5423 % A description of each parameter follows:
5425 % o image: the image.
5427 % o radius: the radius of the pixel neighborhood.
5429 % o sigma: the standard deviation of the Gaussian, in pixels.
5431 % o x, y: Define the x and y ellipse offset.
5433 % o exception: return any errors or warnings in this structure.
5436 MagickExport Image *VignetteImage(const Image *image,const double radius,
5437 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
5440 ellipse[MagickPathExtent];
5451 assert(image != (Image *) NULL);
5452 assert(image->signature == MagickSignature);
5453 if (image->debug != MagickFalse)
5454 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5455 assert(exception != (ExceptionInfo *) NULL);
5456 assert(exception->signature == MagickSignature);
5457 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5458 if (canvas_image == (Image *) NULL)
5459 return((Image *) NULL);
5460 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
5462 canvas_image=DestroyImage(canvas_image);
5463 return((Image *) NULL);
5465 canvas_image->alpha_trait=BlendPixelTrait;
5466 oval_image=CloneImage(canvas_image,canvas_image->columns,canvas_image->rows,
5467 MagickTrue,exception);
5468 if (oval_image == (Image *) NULL)
5470 canvas_image=DestroyImage(canvas_image);
5471 return((Image *) NULL);
5473 (void) QueryColorCompliance("#000000",AllCompliance,
5474 &oval_image->background_color,exception);
5475 (void) SetImageBackgroundColor(oval_image,exception);
5476 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
5477 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5479 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5481 (void) FormatLocaleString(ellipse,MagickPathExtent,"ellipse %g,%g,%g,%g,"
5482 "0.0,360.0",image->columns/2.0,image->rows/2.0,image->columns/2.0-x,
5484 draw_info->primitive=AcquireString(ellipse);
5485 (void) DrawImage(oval_image,draw_info,exception);
5486 draw_info=DestroyDrawInfo(draw_info);
5487 blur_image=BlurImage(oval_image,radius,sigma,exception);
5488 oval_image=DestroyImage(oval_image);
5489 if (blur_image == (Image *) NULL)
5491 canvas_image=DestroyImage(canvas_image);
5492 return((Image *) NULL);
5494 blur_image->alpha_trait=UndefinedPixelTrait;
5495 (void) CompositeImage(canvas_image,blur_image,IntensityCompositeOp,MagickTrue,
5497 blur_image=DestroyImage(blur_image);
5498 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5499 canvas_image=DestroyImage(canvas_image);
5500 if (vignette_image != (Image *) NULL)
5501 (void) TransformImageColorspace(vignette_image,image->colorspace,exception);
5502 return(vignette_image);
5506 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5510 % W a v e I m a g e %
5514 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5516 % WaveImage() creates a "ripple" effect in the image by shifting the pixels
5517 % vertically along a sine wave whose amplitude and wavelength is specified
5518 % by the given parameters.
5520 % The format of the WaveImage method is:
5522 % Image *WaveImage(const Image *image,const double amplitude,
5523 % const double wave_length,const PixelInterpolateMethod method,
5524 % ExceptionInfo *exception)
5526 % A description of each parameter follows:
5528 % o image: the image.
5530 % o amplitude, wave_length: Define the amplitude and wave length of the
5533 % o interpolate: the pixel interpolation method.
5535 % o exception: return any errors or warnings in this structure.
5538 MagickExport Image *WaveImage(const Image *image,const double amplitude,
5539 const double wave_length,const PixelInterpolateMethod method,
5540 ExceptionInfo *exception)
5542 #define WaveImageTag "Wave/Image"
5567 Initialize wave image attributes.
5569 assert(image != (Image *) NULL);
5570 assert(image->signature == MagickSignature);
5571 if (image->debug != MagickFalse)
5572 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5573 assert(exception != (ExceptionInfo *) NULL);
5574 assert(exception->signature == MagickSignature);
5575 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
5576 fabs(amplitude)),MagickTrue,exception);
5577 if (wave_image == (Image *) NULL)
5578 return((Image *) NULL);
5579 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
5581 wave_image=DestroyImage(wave_image);
5582 return((Image *) NULL);
5584 if (wave_image->background_color.alpha != OpaqueAlpha)
5585 wave_image->alpha_trait=BlendPixelTrait;
5589 sine_map=(double *) AcquireQuantumMemory((size_t) wave_image->columns,
5591 if (sine_map == (double *) NULL)
5593 wave_image=DestroyImage(wave_image);
5594 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5596 for (i=0; i < (ssize_t) wave_image->columns; i++)
5597 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5604 image_view=AcquireVirtualCacheView(image,exception);
5605 wave_view=AcquireAuthenticCacheView(wave_image,exception);
5606 (void) SetCacheViewVirtualPixelMethod(image_view,
5607 BackgroundVirtualPixelMethod);
5608 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5609 #pragma omp parallel for schedule(static,4) shared(progress,status) \
5610 magick_threads(image,wave_image,wave_image->rows,1)
5612 for (y=0; y < (ssize_t) wave_image->rows; y++)
5620 if (status == MagickFalse)
5622 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5624 if (q == (Quantum *) NULL)
5629 for (x=0; x < (ssize_t) wave_image->columns; x++)
5631 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5632 (double) x,(double) (y-sine_map[x]),q,exception);
5633 q+=GetPixelChannels(wave_image);
5635 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5637 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5642 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5643 #pragma omp critical (MagickCore_WaveImage)
5645 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5646 if (proceed == MagickFalse)
5650 wave_view=DestroyCacheView(wave_view);
5651 image_view=DestroyCacheView(image_view);
5652 sine_map=(double *) RelinquishMagickMemory(sine_map);
5653 if (status == MagickFalse)
5654 wave_image=DestroyImage(wave_image);