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,i);
765 if (traits == UndefinedPixelTrait)
767 if (((traits & CopyPixelTrait) != 0) ||
768 (GetPixelReadMask(colorize_image,q) == 0))
770 SetPixelChannel(colorize_image,i,ClampToQuantum(Colorize(q[i],
771 GetPixelInfoChannel(&blend_percentage,i),
772 GetPixelInfoChannel(colorize,i))),q);
774 q+=GetPixelChannels(colorize_image);
776 sync=SyncCacheViewAuthenticPixels(image_view,exception);
777 if (sync == MagickFalse)
779 if (image->progress_monitor != (MagickProgressMonitor) NULL)
784 #if defined(MAGICKCORE_OPENMP_SUPPORT)
785 #pragma omp critical (MagickCore_ColorizeImage)
787 proceed=SetImageProgress(image,ColorizeImageTag,progress++,
788 colorize_image->rows);
789 if (proceed == MagickFalse)
793 image_view=DestroyCacheView(image_view);
794 if (status == MagickFalse)
795 colorize_image=DestroyImage(colorize_image);
796 return(colorize_image);
800 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
804 % C o l o r M a t r i x I m a g e %
808 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
810 % ColorMatrixImage() applies color transformation to an image. This method
811 % permits saturation changes, hue rotation, luminance to alpha, and various
812 % other effects. Although variable-sized transformation matrices can be used,
813 % typically one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA
814 % (or RGBA with offsets). The matrix is similar to those used by Adobe Flash
815 % except offsets are in column 6 rather than 5 (in support of CMYKA images)
816 % and offsets are normalized (divide Flash offset by 255).
818 % The format of the ColorMatrixImage method is:
820 % Image *ColorMatrixImage(const Image *image,
821 % const KernelInfo *color_matrix,ExceptionInfo *exception)
823 % A description of each parameter follows:
825 % o image: the image.
827 % o color_matrix: the color matrix.
829 % o exception: return any errors or warnings in this structure.
832 /* FUTURE: modify to make use of a MagickMatrix Mutliply function
833 That should be provided in "matrix.c"
834 (ASIDE: actually distorts should do this too but currently doesn't)
837 MagickExport Image *ColorMatrixImage(const Image *image,
838 const KernelInfo *color_matrix,ExceptionInfo *exception)
840 #define ColorMatrixImageTag "ColorMatrix/Image"
849 { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
850 { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
851 { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
852 { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
853 { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
854 { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }
875 Map given color_matrix, into a 6x6 matrix RGBKA and a constant
877 assert(image != (Image *) NULL);
878 assert(image->signature == MagickSignature);
879 if (image->debug != MagickFalse)
880 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
881 assert(exception != (ExceptionInfo *) NULL);
882 assert(exception->signature == MagickSignature);
884 for (v=0; v < (ssize_t) color_matrix->height; v++)
885 for (u=0; u < (ssize_t) color_matrix->width; u++)
887 if ((v < 6) && (u < 6))
888 ColorMatrix[v][u]=color_matrix->values[i];
892 Initialize color image.
894 color_image=CloneImage(image,0,0,MagickTrue,exception);
895 if (color_image == (Image *) NULL)
896 return((Image *) NULL);
897 if (SetImageStorageClass(color_image,DirectClass,exception) == MagickFalse)
899 color_image=DestroyImage(color_image);
900 return((Image *) NULL);
902 if (image->debug != MagickFalse)
905 format[MaxTextExtent],
908 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
909 " ColorMatrix image with color matrix:");
910 message=AcquireString("");
911 for (v=0; v < 6; v++)
914 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
915 (void) ConcatenateString(&message,format);
916 for (u=0; u < 6; u++)
918 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",
920 (void) ConcatenateString(&message,format);
922 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
924 message=DestroyString(message);
927 Apply the ColorMatrix to image.
931 image_view=AcquireVirtualCacheView(image,exception);
932 color_view=AcquireAuthenticCacheView(color_image,exception);
933 #if defined(MAGICKCORE_OPENMP_SUPPORT)
934 #pragma omp parallel for schedule(static,4) shared(progress,status) \
935 magick_threads(image,color_image,image->rows,1)
937 for (y=0; y < (ssize_t) image->rows; y++)
942 register const Quantum
951 if (status == MagickFalse)
953 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
954 q=GetCacheViewAuthenticPixels(color_view,0,y,color_image->columns,1,
956 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
961 GetPixelInfo(image,&pixel);
962 for (x=0; x < (ssize_t) image->columns; x++)
970 GetPixelInfoPixel(image,p,&pixel);
971 height=color_matrix->height > 6 ? 6UL : color_matrix->height;
972 for (v=0; v < (ssize_t) height; v++)
977 sum=ColorMatrix[v][0]*GetPixelRed(image,p)+ColorMatrix[v][1]*
978 GetPixelGreen(image,p)+ColorMatrix[v][2]*GetPixelBlue(image,p);
979 if (image->colorspace == CMYKColorspace)
980 sum+=ColorMatrix[v][3]*GetPixelBlack(image,p);
981 if (image->alpha_trait != UndefinedPixelTrait)
982 sum+=ColorMatrix[v][4]*GetPixelAlpha(image,p);
983 sum+=QuantumRange*ColorMatrix[v][5];
986 case 0: pixel.red=sum; break;
987 case 1: pixel.green=sum; break;
988 case 2: pixel.blue=sum; break;
989 case 3: pixel.black=sum; break;
990 case 4: pixel.alpha=sum; break;
994 SetPixelViaPixelInfo(color_image,&pixel,q);
995 p+=GetPixelChannels(image);
996 q+=GetPixelChannels(color_image);
998 if (SyncCacheViewAuthenticPixels(color_view,exception) == MagickFalse)
1000 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1005 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1006 #pragma omp critical (MagickCore_ColorMatrixImage)
1008 proceed=SetImageProgress(image,ColorMatrixImageTag,progress++,
1010 if (proceed == MagickFalse)
1014 color_view=DestroyCacheView(color_view);
1015 image_view=DestroyCacheView(image_view);
1016 if (status == MagickFalse)
1017 color_image=DestroyImage(color_image);
1018 return(color_image);
1022 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1026 + D e s t r o y F x I n f o %
1030 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1032 % DestroyFxInfo() deallocates memory associated with an FxInfo structure.
1034 % The format of the DestroyFxInfo method is:
1036 % ImageInfo *DestroyFxInfo(ImageInfo *fx_info)
1038 % A description of each parameter follows:
1040 % o fx_info: the fx info.
1043 MagickPrivate FxInfo *DestroyFxInfo(FxInfo *fx_info)
1048 fx_info->exception=DestroyExceptionInfo(fx_info->exception);
1049 fx_info->expression=DestroyString(fx_info->expression);
1050 fx_info->symbols=DestroySplayTree(fx_info->symbols);
1051 fx_info->colors=DestroySplayTree(fx_info->colors);
1052 for (i=(ssize_t) GetImageListLength(fx_info->images)-1; i >= 0; i--)
1053 fx_info->view[i]=DestroyCacheView(fx_info->view[i]);
1054 fx_info->view=(CacheView **) RelinquishMagickMemory(fx_info->view);
1055 fx_info->random_info=DestroyRandomInfo(fx_info->random_info);
1056 fx_info=(FxInfo *) RelinquishMagickMemory(fx_info);
1061 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1065 + 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 %
1069 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1071 % FxEvaluateChannelExpression() evaluates an expression and returns the
1074 % The format of the FxEvaluateExpression method is:
1076 % double FxEvaluateChannelExpression(FxInfo *fx_info,
1077 % const PixelChannel channel,const ssize_t x,const ssize_t y,
1078 % double *alpha,Exceptioninfo *exception)
1079 % double FxEvaluateExpression(FxInfo *fx_info,
1080 % double *alpha,Exceptioninfo *exception)
1082 % A description of each parameter follows:
1084 % o fx_info: the fx info.
1086 % o channel: the channel.
1088 % o x,y: the pixel position.
1090 % o alpha: the result.
1092 % o exception: return any errors or warnings in this structure.
1096 static double FxChannelStatistics(FxInfo *fx_info,Image *image,
1097 PixelChannel channel,const char *symbol,ExceptionInfo *exception)
1104 statistic[MaxTextExtent];
1112 channel_mask=UndefinedChannel;
1113 for (p=symbol; (*p != '.') && (*p != '\0'); p++) ;
1119 option=ParseCommandOption(MagickPixelChannelOptions,MagickTrue,p+1);
1122 channel=(PixelChannel) option;
1123 channel_mask=(ChannelType) (channel_mask | (1 << channel));
1124 SetPixelChannelMask(image,channel_mask);
1127 (void) FormatLocaleString(key,MaxTextExtent,"%p.%.20g.%s",(void *) image,
1128 (double) channel,symbol);
1129 value=(const char *) GetValueFromSplayTree(fx_info->symbols,key);
1130 if (value != (const char *) NULL)
1132 if (channel_mask != UndefinedChannel)
1133 SetPixelChannelMask(image,channel_mask);
1134 return(QuantumScale*StringToDouble(value,(char **) NULL));
1136 (void) DeleteNodeFromSplayTree(fx_info->symbols,key);
1137 if (LocaleNCompare(symbol,"depth",5) == 0)
1142 depth=GetImageDepth(image,exception);
1143 (void) FormatLocaleString(statistic,MaxTextExtent,"%.20g",(double) depth);
1145 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1151 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
1152 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",kurtosis);
1154 if (LocaleNCompare(symbol,"maxima",6) == 0)
1160 (void) GetImageRange(image,&minima,&maxima,exception);
1161 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",maxima);
1163 if (LocaleNCompare(symbol,"mean",4) == 0)
1169 (void) GetImageMean(image,&mean,&standard_deviation,exception);
1170 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",mean);
1172 if (LocaleNCompare(symbol,"minima",6) == 0)
1178 (void) GetImageRange(image,&minima,&maxima,exception);
1179 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",minima);
1181 if (LocaleNCompare(symbol,"skewness",8) == 0)
1187 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
1188 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",skewness);
1190 if (LocaleNCompare(symbol,"standard-deviation",18) == 0)
1196 (void) GetImageMean(image,&mean,&standard_deviation,exception);
1197 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",
1198 standard_deviation);
1200 if (channel_mask != UndefinedChannel)
1201 SetPixelChannelMask(image,channel_mask);
1202 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(key),
1203 ConstantString(statistic));
1204 return(QuantumScale*StringToDouble(statistic,(char **) NULL));
1208 FxEvaluateSubexpression(FxInfo *,const PixelChannel,const ssize_t,
1209 const ssize_t,const char *,double *,ExceptionInfo *);
1211 static MagickOffsetType FxGCD(MagickOffsetType alpha,MagickOffsetType beta)
1214 return(FxGCD(beta,alpha % beta));
1218 static inline const char *FxSubexpression(const char *expression,
1219 ExceptionInfo *exception)
1228 subexpression=expression;
1229 while ((*subexpression != '\0') &&
1230 ((level != 1) || (strchr(")",(int) *subexpression) == (char *) NULL)))
1232 if (strchr("(",(int) *subexpression) != (char *) NULL)
1235 if (strchr(")",(int) *subexpression) != (char *) NULL)
1239 if (*subexpression == '\0')
1240 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1241 "UnbalancedParenthesis","`%s'",expression);
1242 return(subexpression);
1245 static double FxGetSymbol(FxInfo *fx_info,const PixelChannel channel,
1246 const ssize_t x,const ssize_t y,const char *expression,
1247 ExceptionInfo *exception)
1251 subexpression[MaxTextExtent],
1252 symbol[MaxTextExtent];
1279 i=GetImageIndexInList(fx_info->images);
1283 if (isalpha((int) ((unsigned char) *(p+1))) == 0)
1285 if (strchr("suv",(int) *p) != (char *) NULL)
1292 i=GetImageIndexInList(fx_info->images);
1295 case 'u': i=0; break;
1296 case 'v': i=1; break;
1303 for (p++; *p != '\0'; )
1317 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1319 i=(ssize_t) (alpha+0.5);
1325 if ((*p == 'p') && (isalpha((int) ((unsigned char) *(p+1))) == 0))
1332 for (p++; *p != '\0'; )
1346 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1357 for (p++; *p != '\0'; )
1371 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1381 length=GetImageListLength(fx_info->images);
1383 i+=(ssize_t) length;
1386 image=GetImageFromList(fx_info->images,i);
1387 if (image == (Image *) NULL)
1389 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1390 "NoSuchImage","`%s'",expression);
1393 GetPixelInfo(image,&pixel);
1394 (void) InterpolatePixelInfo(image,fx_info->view[i],image->interpolate,
1395 point.x,point.y,&pixel,exception);
1396 if ((strlen(p) > 2) && (LocaleCompare(p,"intensity") != 0) &&
1397 (LocaleCompare(p,"luma") != 0) && (LocaleCompare(p,"luminance") != 0) &&
1398 (LocaleCompare(p,"hue") != 0) && (LocaleCompare(p,"saturation") != 0) &&
1399 (LocaleCompare(p,"lightness") != 0))
1402 name[MaxTextExtent];
1404 (void) CopyMagickString(name,p,MaxTextExtent);
1405 for (q=name+(strlen(name)-1); q > name; q--)
1415 if ((strlen(name) > 2) &&
1416 (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
1421 color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
1422 if (color != (PixelInfo *) NULL)
1432 status=QueryColorCompliance(name,AllCompliance,&pixel,
1433 fx_info->exception);
1434 if (status != MagickFalse)
1436 (void) AddValueToSplayTree(fx_info->colors,ConstantString(
1437 name),ClonePixelInfo(&pixel));
1443 (void) CopyMagickString(symbol,p,MaxTextExtent);
1444 StripString(symbol);
1445 if (*symbol == '\0')
1449 case RedPixelChannel: return(QuantumScale*pixel.red);
1450 case GreenPixelChannel: return(QuantumScale*pixel.green);
1451 case BluePixelChannel: return(QuantumScale*pixel.blue);
1452 case BlackPixelChannel:
1454 if (image->colorspace != CMYKColorspace)
1456 (void) ThrowMagickException(exception,GetMagickModule(),
1457 ImageError,"ColorSeparatedImageRequired","`%s'",
1461 return(QuantumScale*pixel.black);
1463 case AlphaPixelChannel:
1468 if (pixel.alpha_trait == UndefinedPixelTrait)
1470 alpha=(double) (QuantumScale*pixel.alpha);
1473 case IndexPixelChannel:
1475 case IntensityPixelChannel:
1478 quantum_pixel[MaxPixelChannels];
1480 SetPixelViaPixelInfo(image,&pixel,quantum_pixel);
1481 return(QuantumScale*GetPixelIntensity(image,quantum_pixel));
1486 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1487 "UnableToParseExpression","`%s'",p);
1495 if (LocaleCompare(symbol,"a") == 0)
1496 return((double) (QuantumScale*pixel.alpha));
1502 if (LocaleCompare(symbol,"b") == 0)
1503 return(QuantumScale*pixel.blue);
1509 if (LocaleNCompare(symbol,"channel",7) == 0)
1517 flags=ParseGeometry(symbol+7,&channel_info);
1518 if (image->colorspace == CMYKColorspace)
1521 case CyanPixelChannel:
1523 if ((flags & RhoValue) == 0)
1525 return(channel_info.rho);
1527 case MagentaPixelChannel:
1529 if ((flags & SigmaValue) == 0)
1531 return(channel_info.sigma);
1533 case YellowPixelChannel:
1535 if ((flags & XiValue) == 0)
1537 return(channel_info.xi);
1539 case BlackPixelChannel:
1541 if ((flags & PsiValue) == 0)
1543 return(channel_info.psi);
1545 case AlphaPixelChannel:
1547 if ((flags & ChiValue) == 0)
1549 return(channel_info.chi);
1556 case RedPixelChannel:
1558 if ((flags & RhoValue) == 0)
1560 return(channel_info.rho);
1562 case GreenPixelChannel:
1564 if ((flags & SigmaValue) == 0)
1566 return(channel_info.sigma);
1568 case BluePixelChannel:
1570 if ((flags & XiValue) == 0)
1572 return(channel_info.xi);
1574 case BlackPixelChannel:
1576 if ((flags & ChiValue) == 0)
1578 return(channel_info.chi);
1580 case AlphaPixelChannel:
1582 if ((flags & PsiValue) == 0)
1584 return(channel_info.psi);
1590 if (LocaleCompare(symbol,"c") == 0)
1591 return(QuantumScale*pixel.red);
1597 if (LocaleNCompare(symbol,"depth",5) == 0)
1598 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1604 if (LocaleCompare(symbol,"g") == 0)
1605 return(QuantumScale*pixel.green);
1611 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1612 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1613 if (LocaleCompare(symbol,"k") == 0)
1615 if (image->colorspace != CMYKColorspace)
1617 (void) ThrowMagickException(exception,GetMagickModule(),
1618 OptionError,"ColorSeparatedImageRequired","`%s'",
1622 return(QuantumScale*pixel.black);
1629 if (LocaleCompare(symbol,"h") == 0)
1630 return((double) image->rows);
1631 if (LocaleCompare(symbol,"hue") == 0)
1638 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1647 if ((LocaleCompare(symbol,"image.depth") == 0) ||
1648 (LocaleCompare(symbol,"image.minima") == 0) ||
1649 (LocaleCompare(symbol,"image.maxima") == 0) ||
1650 (LocaleCompare(symbol,"image.mean") == 0) ||
1651 (LocaleCompare(symbol,"image.kurtosis") == 0) ||
1652 (LocaleCompare(symbol,"image.skewness") == 0) ||
1653 (LocaleCompare(symbol,"image.standard_deviation") == 0))
1654 return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
1655 if (LocaleCompare(symbol,"image.resolution.x") == 0)
1656 return(image->resolution.x);
1657 if (LocaleCompare(symbol,"image.resolution.y") == 0)
1658 return(image->resolution.y);
1659 if (LocaleCompare(symbol,"intensity") == 0)
1662 quantum_pixel[MaxPixelChannels];
1664 SetPixelViaPixelInfo(image,&pixel,quantum_pixel);
1665 return(QuantumScale*GetPixelIntensity(image,quantum_pixel));
1667 if (LocaleCompare(symbol,"i") == 0)
1674 if (LocaleCompare(symbol,"j") == 0)
1681 if (LocaleCompare(symbol,"lightness") == 0)
1688 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1692 if (LocaleCompare(symbol,"luma") == 0)
1697 luma=0.212656*pixel.red+0.715158*pixel.green+0.072186*pixel.blue;
1698 return(QuantumScale*luma);
1700 if (LocaleCompare(symbol,"luminance") == 0)
1705 luminence=0.212656*pixel.red+0.715158*pixel.green+0.072186*pixel.blue;
1706 return(QuantumScale*luminence);
1713 if (LocaleNCompare(symbol,"maxima",6) == 0)
1714 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1715 if (LocaleNCompare(symbol,"mean",4) == 0)
1716 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1717 if (LocaleNCompare(symbol,"minima",6) == 0)
1718 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1719 if (LocaleCompare(symbol,"m") == 0)
1720 return(QuantumScale*pixel.green);
1726 if (LocaleCompare(symbol,"n") == 0)
1727 return((double) GetImageListLength(fx_info->images));
1733 if (LocaleCompare(symbol,"o") == 0)
1734 return(QuantumScale*pixel.alpha);
1740 if (LocaleCompare(symbol,"page.height") == 0)
1741 return((double) image->page.height);
1742 if (LocaleCompare(symbol,"page.width") == 0)
1743 return((double) image->page.width);
1744 if (LocaleCompare(symbol,"page.x") == 0)
1745 return((double) image->page.x);
1746 if (LocaleCompare(symbol,"page.y") == 0)
1747 return((double) image->page.y);
1753 if (LocaleCompare(symbol,"resolution.x") == 0)
1754 return(image->resolution.x);
1755 if (LocaleCompare(symbol,"resolution.y") == 0)
1756 return(image->resolution.y);
1757 if (LocaleCompare(symbol,"r") == 0)
1758 return(QuantumScale*pixel.red);
1764 if (LocaleCompare(symbol,"saturation") == 0)
1771 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1775 if (LocaleNCompare(symbol,"skewness",8) == 0)
1776 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1777 if (LocaleNCompare(symbol,"standard-deviation",18) == 0)
1778 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1784 if (LocaleCompare(symbol,"t") == 0)
1785 return((double) GetImageIndexInList(fx_info->images));
1791 if (LocaleCompare(symbol,"w") == 0)
1792 return((double) image->columns);
1798 if (LocaleCompare(symbol,"y") == 0)
1799 return(QuantumScale*pixel.blue);
1805 if (LocaleCompare(symbol,"z") == 0)
1810 depth=(double) GetImageDepth(image,fx_info->exception);
1818 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1819 if (value != (const char *) NULL)
1820 return((double) StringToDouble(value,(char **) NULL));
1821 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1822 "UnableToParseExpression","`%s'",symbol);
1826 static const char *FxOperatorPrecedence(const char *expression,
1827 ExceptionInfo *exception)
1831 UndefinedPrecedence,
1833 BitwiseComplementPrecedence,
1835 ExponentialNotationPrecedence,
1839 RelationalPrecedence,
1840 EquivalencyPrecedence,
1841 BitwiseAndPrecedence,
1842 BitwiseOrPrecedence,
1843 LogicalAndPrecedence,
1844 LogicalOrPrecedence,
1846 AssignmentPrecedence,
1866 subexpression=(const char *) NULL;
1867 target=NullPrecedence;
1868 while (*expression != '\0')
1870 precedence=UndefinedPrecedence;
1871 if ((isspace((int) ((unsigned char) *expression)) != 0) || (c == (int) '@'))
1876 switch (*expression)
1881 #if defined(MAGICKCORE_HAVE_ACOSH)
1882 if (LocaleNCompare(expression,"acosh",5) == 0)
1888 #if defined(MAGICKCORE_HAVE_ASINH)
1889 if (LocaleNCompare(expression,"asinh",5) == 0)
1895 #if defined(MAGICKCORE_HAVE_ATANH)
1896 if (LocaleNCompare(expression,"atanh",5) == 0)
1902 if (LocaleNCompare(expression,"atan2",5) == 0)
1912 if ((isdigit((int) ((unsigned char) c)) != 0) &&
1913 ((LocaleNCompare(expression,"E+",2) == 0) ||
1914 (LocaleNCompare(expression,"E-",2) == 0)))
1916 expression+=2; /* scientific notation */
1923 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1924 (LocaleNCompare(expression,"j1",2) == 0))
1933 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1940 if ((c == (int) '{') || (c == (int) '['))
1943 if ((c == (int) '}') || (c == (int) ']'))
1946 switch ((unsigned char) *expression)
1951 precedence=BitwiseComplementPrecedence;
1957 precedence=ExponentPrecedence;
1962 if (((c != 0) && ((isdigit((int) ((unsigned char) c)) != 0) ||
1963 (strchr(")",(int) ((unsigned char) c)) != (char *) NULL))) &&
1964 (((islower((int) ((unsigned char) *expression)) != 0) ||
1965 (strchr("(",(int) ((unsigned char) *expression)) != (char *) NULL)) ||
1966 ((isdigit((int) ((unsigned char) c)) == 0) &&
1967 (isdigit((int) ((unsigned char) *expression)) != 0))) &&
1968 (strchr("xy",(int) ((unsigned char) *expression)) == (char *) NULL))
1969 precedence=MultiplyPrecedence;
1976 precedence=MultiplyPrecedence;
1982 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
1984 precedence=AdditionPrecedence;
1987 case LeftShiftOperator:
1988 case RightShiftOperator:
1990 precedence=ShiftPrecedence;
1994 case LessThanEqualOperator:
1995 case GreaterThanEqualOperator:
1998 precedence=RelationalPrecedence;
2002 case NotEqualOperator:
2004 precedence=EquivalencyPrecedence;
2009 precedence=BitwiseAndPrecedence;
2014 precedence=BitwiseOrPrecedence;
2017 case LogicalAndOperator:
2019 precedence=LogicalAndPrecedence;
2022 case LogicalOrOperator:
2024 precedence=LogicalOrPrecedence;
2027 case ExponentialNotation:
2029 precedence=ExponentialNotationPrecedence;
2035 precedence=TernaryPrecedence;
2040 precedence=AssignmentPrecedence;
2045 precedence=CommaPrecedence;
2050 precedence=SeparatorPrecedence;
2054 if ((precedence == BitwiseComplementPrecedence) ||
2055 (precedence == TernaryPrecedence) ||
2056 (precedence == AssignmentPrecedence))
2058 if (precedence > target)
2061 Right-to-left associativity.
2064 subexpression=expression;
2068 if (precedence >= target)
2071 Left-to-right associativity.
2074 subexpression=expression;
2076 if (strchr("(",(int) *expression) != (char *) NULL)
2077 expression=FxSubexpression(expression,exception);
2078 c=(int) (*expression++);
2080 return(subexpression);
2083 static double FxEvaluateSubexpression(FxInfo *fx_info,
2084 const PixelChannel channel,const ssize_t x,const ssize_t y,
2085 const char *expression,double *beta,ExceptionInfo *exception)
2089 subexpression[MaxTextExtent];
2099 if (exception->severity >= ErrorException)
2101 while (isspace((int) ((unsigned char) *expression)) != 0)
2103 if (*expression == '\0')
2105 *subexpression='\0';
2106 p=FxOperatorPrecedence(expression,exception);
2107 if (p != (const char *) NULL)
2109 (void) CopyMagickString(subexpression,expression,(size_t)
2111 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2113 switch ((unsigned char) *p)
2117 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2118 *beta=(double) (~(size_t) *beta);
2123 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2124 return(*beta == 0.0 ? 1.0 : 0.0);
2128 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2129 channel,x,y,++p,beta,exception));
2133 case ExponentialNotation:
2135 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2136 return(alpha*(*beta));
2140 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2143 (void) ThrowMagickException(exception,GetMagickModule(),
2144 OptionError,"DivideByZero","`%s'",expression);
2147 return(alpha/(*beta));
2151 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2152 *beta=fabs(floor(((double) *beta)+0.5));
2155 (void) ThrowMagickException(exception,GetMagickModule(),
2156 OptionError,"DivideByZero","`%s'",expression);
2159 return(fmod((double) alpha,(double) *beta));
2163 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2164 return(alpha+(*beta));
2168 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2169 return(alpha-(*beta));
2171 case LeftShiftOperator:
2173 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2174 *beta=(double) ((size_t) (alpha+0.5) << (size_t) (gamma+0.5));
2177 case RightShiftOperator:
2179 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2180 *beta=(double) ((size_t) (alpha+0.5) >> (size_t) (gamma+0.5));
2185 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2186 return(alpha < *beta ? 1.0 : 0.0);
2188 case LessThanEqualOperator:
2190 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2191 return(alpha <= *beta ? 1.0 : 0.0);
2195 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2196 return(alpha > *beta ? 1.0 : 0.0);
2198 case GreaterThanEqualOperator:
2200 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2201 return(alpha >= *beta ? 1.0 : 0.0);
2205 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2206 return(fabs(alpha-(*beta)) < MagickEpsilon ? 1.0 : 0.0);
2208 case NotEqualOperator:
2210 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2211 return(fabs(alpha-(*beta)) >= MagickEpsilon ? 1.0 : 0.0);
2215 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2216 *beta=(double) ((size_t) (alpha+0.5) & (size_t) (gamma+0.5));
2221 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2222 *beta=(double) ((size_t) (alpha+0.5) | (size_t) (gamma+0.5));
2225 case LogicalAndOperator:
2227 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2228 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2231 case LogicalOrOperator:
2233 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2234 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2242 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2244 p=StringToken(":",&q);
2245 if (q == (char *) NULL)
2247 (void) ThrowMagickException(exception,GetMagickModule(),
2248 OptionError,"UnableToParseExpression","`%s'",subexpression);
2251 if (fabs((double) alpha) >= MagickEpsilon)
2252 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2254 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2260 numeric[MaxTextExtent];
2263 while (isalpha((int) ((unsigned char) *q)) != 0)
2267 (void) ThrowMagickException(exception,GetMagickModule(),
2268 OptionError,"UnableToParseExpression","`%s'",subexpression);
2271 ClearMagickException(exception);
2272 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2273 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
2275 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2276 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2277 subexpression),ConstantString(numeric));
2282 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2287 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2292 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2298 if (strchr("(",(int) *expression) != (char *) NULL)
2300 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2301 subexpression[strlen(subexpression)-1]='\0';
2302 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2306 switch (*expression)
2310 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2316 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2322 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2324 return((double) (~(size_t) (gamma+0.5)));
2329 if (LocaleNCompare(expression,"abs",3) == 0)
2331 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2333 return((double) fabs((double) alpha));
2335 #if defined(MAGICKCORE_HAVE_ACOSH)
2336 if (LocaleNCompare(expression,"acosh",5) == 0)
2338 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2340 return((double) acosh((double) alpha));
2343 if (LocaleNCompare(expression,"acos",4) == 0)
2345 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2347 return((double) acos((double) alpha));
2349 #if defined(MAGICKCORE_HAVE_J1)
2350 if (LocaleNCompare(expression,"airy",4) == 0)
2352 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2356 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
2357 return(gamma*gamma);
2360 #if defined(MAGICKCORE_HAVE_ASINH)
2361 if (LocaleNCompare(expression,"asinh",5) == 0)
2363 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2365 return((double) asinh((double) alpha));
2368 if (LocaleNCompare(expression,"asin",4) == 0)
2370 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2372 return((double) asin((double) alpha));
2374 if (LocaleNCompare(expression,"alt",3) == 0)
2376 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2378 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
2380 if (LocaleNCompare(expression,"atan2",5) == 0)
2382 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2384 return((double) atan2((double) alpha,(double) *beta));
2386 #if defined(MAGICKCORE_HAVE_ATANH)
2387 if (LocaleNCompare(expression,"atanh",5) == 0)
2389 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2391 return((double) atanh((double) alpha));
2394 if (LocaleNCompare(expression,"atan",4) == 0)
2396 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2398 return((double) atan((double) alpha));
2400 if (LocaleCompare(expression,"a") == 0)
2401 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2407 if (LocaleCompare(expression,"b") == 0)
2408 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2414 if (LocaleNCompare(expression,"ceil",4) == 0)
2416 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2418 return((double) ceil((double) alpha));
2420 if (LocaleNCompare(expression,"clamp",5) == 0)
2422 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2430 if (LocaleNCompare(expression,"cosh",4) == 0)
2432 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2434 return((double) cosh((double) alpha));
2436 if (LocaleNCompare(expression,"cos",3) == 0)
2438 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2440 return((double) cos((double) alpha));
2442 if (LocaleCompare(expression,"c") == 0)
2443 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2449 if (LocaleNCompare(expression,"debug",5) == 0)
2454 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2456 if (fx_info->images->colorspace == CMYKColorspace)
2459 case CyanPixelChannel: type="cyan"; break;
2460 case MagentaPixelChannel: type="magenta"; break;
2461 case YellowPixelChannel: type="yellow"; break;
2462 case AlphaPixelChannel: type="opacity"; break;
2463 case BlackPixelChannel: type="black"; break;
2464 default: type="unknown"; break;
2469 case RedPixelChannel: type="red"; break;
2470 case GreenPixelChannel: type="green"; break;
2471 case BluePixelChannel: type="blue"; break;
2472 case AlphaPixelChannel: type="opacity"; break;
2473 default: type="unknown"; break;
2475 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2476 if (strlen(subexpression) > 1)
2477 subexpression[strlen(subexpression)-1]='\0';
2478 if (fx_info->file != (FILE *) NULL)
2479 (void) FormatLocaleFile(fx_info->file,"%s[%.20g,%.20g].%s: "
2480 "%s=%.*g\n",fx_info->images->filename,(double) x,(double) y,type,
2481 subexpression,GetMagickPrecision(),(double) alpha);
2484 if (LocaleNCompare(expression,"drc",3) == 0)
2486 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2488 return((double) (alpha/(*beta*(alpha-1.0)+1.0)));
2495 if (LocaleCompare(expression,"epsilon") == 0)
2496 return((double) MagickEpsilon);
2497 if (LocaleNCompare(expression,"exp",3) == 0)
2499 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2501 return((double) exp((double) alpha));
2503 if (LocaleCompare(expression,"e") == 0)
2504 return((double) 2.7182818284590452354);
2510 if (LocaleNCompare(expression,"floor",5) == 0)
2512 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2514 return((double) floor((double) alpha));
2521 if (LocaleNCompare(expression,"gauss",5) == 0)
2523 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2525 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
2526 return((double) gamma);
2528 if (LocaleNCompare(expression,"gcd",3) == 0)
2533 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2535 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType) (*beta+
2537 return((double) gcd);
2539 if (LocaleCompare(expression,"g") == 0)
2540 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2546 if (LocaleCompare(expression,"h") == 0)
2547 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2548 if (LocaleCompare(expression,"hue") == 0)
2549 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2550 if (LocaleNCompare(expression,"hypot",5) == 0)
2552 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2554 return((double) hypot((double) alpha,(double) *beta));
2561 if (LocaleCompare(expression,"k") == 0)
2562 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2568 if (LocaleCompare(expression,"intensity") == 0)
2569 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2570 if (LocaleNCompare(expression,"int",3) == 0)
2572 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2574 return((double) floor(alpha));
2576 #if defined(MAGICKCORE_HAVE_ISNAN)
2577 if (LocaleNCompare(expression,"isnan",5) == 0)
2579 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2581 return((double) !!isnan((double) alpha));
2584 if (LocaleCompare(expression,"i") == 0)
2585 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2591 if (LocaleCompare(expression,"j") == 0)
2592 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2593 #if defined(MAGICKCORE_HAVE_J0)
2594 if (LocaleNCompare(expression,"j0",2) == 0)
2596 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2598 return((double) j0((double) alpha));
2601 #if defined(MAGICKCORE_HAVE_J1)
2602 if (LocaleNCompare(expression,"j1",2) == 0)
2604 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2606 return((double) j1((double) alpha));
2609 #if defined(MAGICKCORE_HAVE_J1)
2610 if (LocaleNCompare(expression,"jinc",4) == 0)
2612 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2616 gamma=(double) (2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha));
2625 if (LocaleNCompare(expression,"ln",2) == 0)
2627 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2629 return((double) log((double) alpha));
2631 if (LocaleNCompare(expression,"logtwo",6) == 0)
2633 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2635 return((double) log10((double) alpha))/log10(2.0);
2637 if (LocaleNCompare(expression,"log",3) == 0)
2639 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2641 return((double) log10((double) alpha));
2643 if (LocaleCompare(expression,"lightness") == 0)
2644 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2650 if (LocaleCompare(expression,"MaxRGB") == 0)
2651 return((double) QuantumRange);
2652 if (LocaleNCompare(expression,"maxima",6) == 0)
2654 if (LocaleNCompare(expression,"max",3) == 0)
2656 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2658 return(alpha > *beta ? alpha : *beta);
2660 if (LocaleNCompare(expression,"minima",6) == 0)
2662 if (LocaleNCompare(expression,"min",3) == 0)
2664 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2666 return(alpha < *beta ? alpha : *beta);
2668 if (LocaleNCompare(expression,"mod",3) == 0)
2670 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2672 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2675 if (LocaleCompare(expression,"m") == 0)
2676 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2682 if (LocaleNCompare(expression,"not",3) == 0)
2684 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2686 return((double) (alpha < MagickEpsilon));
2688 if (LocaleCompare(expression,"n") == 0)
2689 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2695 if (LocaleCompare(expression,"Opaque") == 0)
2697 if (LocaleCompare(expression,"o") == 0)
2698 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2704 if (LocaleCompare(expression,"phi") == 0)
2705 return((double) MagickPHI);
2706 if (LocaleCompare(expression,"pi") == 0)
2707 return((double) MagickPI);
2708 if (LocaleNCompare(expression,"pow",3) == 0)
2710 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2712 return((double) pow((double) alpha,(double) *beta));
2714 if (LocaleCompare(expression,"p") == 0)
2715 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2721 if (LocaleCompare(expression,"QuantumRange") == 0)
2722 return((double) QuantumRange);
2723 if (LocaleCompare(expression,"QuantumScale") == 0)
2724 return((double) QuantumScale);
2730 if (LocaleNCompare(expression,"rand",4) == 0)
2735 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2736 #pragma omp critical (MagickCore_FxEvaluateSubexpression)
2738 alpha=GetPseudoRandomValue(fx_info->random_info);
2741 if (LocaleNCompare(expression,"round",5) == 0)
2743 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2745 return((double) floor((double) alpha+0.5));
2747 if (LocaleCompare(expression,"r") == 0)
2748 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2754 if (LocaleCompare(expression,"saturation") == 0)
2755 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2756 if (LocaleNCompare(expression,"sign",4) == 0)
2758 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2760 return(alpha < 0.0 ? -1.0 : 1.0);
2762 if (LocaleNCompare(expression,"sinc",4) == 0)
2764 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2768 gamma=(double) (sin((double) (MagickPI*alpha))/
2772 if (LocaleNCompare(expression,"sinh",4) == 0)
2774 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2776 return((double) sinh((double) alpha));
2778 if (LocaleNCompare(expression,"sin",3) == 0)
2780 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2782 return((double) sin((double) alpha));
2784 if (LocaleNCompare(expression,"sqrt",4) == 0)
2786 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2788 return((double) sqrt((double) alpha));
2790 if (LocaleNCompare(expression,"squish",6) == 0)
2792 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2794 return((double) (1.0/(1.0+exp((double) (-alpha)))));
2796 if (LocaleCompare(expression,"s") == 0)
2797 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2803 if (LocaleNCompare(expression,"tanh",4) == 0)
2805 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2807 return((double) tanh((double) alpha));
2809 if (LocaleNCompare(expression,"tan",3) == 0)
2811 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2813 return((double) tan((double) alpha));
2815 if (LocaleCompare(expression,"Transparent") == 0)
2817 if (LocaleNCompare(expression,"trunc",5) == 0)
2819 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2822 return((double) floor((double) alpha));
2823 return((double) ceil((double) alpha));
2825 if (LocaleCompare(expression,"t") == 0)
2826 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2832 if (LocaleCompare(expression,"u") == 0)
2833 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2839 if (LocaleCompare(expression,"v") == 0)
2840 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2846 if (LocaleNCompare(expression,"while",5) == 0)
2850 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2852 } while (fabs((double) alpha) >= MagickEpsilon);
2853 return((double) *beta);
2855 if (LocaleCompare(expression,"w") == 0)
2856 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2862 if (LocaleCompare(expression,"y") == 0)
2863 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2869 if (LocaleCompare(expression,"z") == 0)
2870 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2876 q=(char *) expression;
2877 alpha=InterpretSiPrefixValue(expression,&q);
2878 if (q == expression)
2879 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2883 MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
2884 double *alpha,ExceptionInfo *exception)
2889 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2894 MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
2895 double *alpha,ExceptionInfo *exception)
2904 fx_info->file=(FILE *) NULL;
2905 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2911 MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
2912 const PixelChannel channel,const ssize_t x,const ssize_t y,
2913 double *alpha,ExceptionInfo *exception)
2919 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2921 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2925 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2933 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2935 % FxImage() applies a mathematical expression to the specified image.
2937 % The format of the FxImage method is:
2939 % Image *FxImage(const Image *image,const char *expression,
2940 % ExceptionInfo *exception)
2942 % A description of each parameter follows:
2944 % o image: the image.
2946 % o expression: A mathematical expression.
2948 % o exception: return any errors or warnings in this structure.
2952 static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2957 assert(fx_info != (FxInfo **) NULL);
2958 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
2959 if (fx_info[i] != (FxInfo *) NULL)
2960 fx_info[i]=DestroyFxInfo(fx_info[i]);
2961 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
2965 static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2966 ExceptionInfo *exception)
2983 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
2984 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
2985 if (fx_info == (FxInfo **) NULL)
2987 (void) ThrowMagickException(exception,GetMagickModule(),
2988 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
2989 return((FxInfo **) NULL);
2991 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2992 if (*expression != '@')
2993 fx_expression=ConstantString(expression);
2995 fx_expression=FileToString(expression+1,~0UL,exception);
2996 for (i=0; i < (ssize_t) number_threads; i++)
3001 fx_info[i]=AcquireFxInfo(image,fx_expression,exception);
3002 if (fx_info[i] == (FxInfo *) NULL)
3004 status=FxPreprocessExpression(fx_info[i],&alpha,exception);
3005 if (status == MagickFalse)
3008 fx_expression=DestroyString(fx_expression);
3009 if (i < (ssize_t) number_threads)
3010 fx_info=DestroyFxThreadSet(fx_info);
3014 MagickExport Image *FxImage(const Image *image,const char *expression,
3015 ExceptionInfo *exception)
3017 #define FxImageTag "Fx/Image"
3038 assert(image != (Image *) NULL);
3039 assert(image->signature == MagickSignature);
3040 if (image->debug != MagickFalse)
3041 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3042 fx_info=AcquireFxThreadSet(image,expression,exception);
3043 if (fx_info == (FxInfo **) NULL)
3044 return((Image *) NULL);
3045 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3046 if (fx_image == (Image *) NULL)
3048 fx_info=DestroyFxThreadSet(fx_info);
3049 return((Image *) NULL);
3051 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
3053 fx_info=DestroyFxThreadSet(fx_info);
3054 fx_image=DestroyImage(fx_image);
3055 return((Image *) NULL);
3062 image_view=AcquireVirtualCacheView(image,exception);
3063 fx_view=AcquireAuthenticCacheView(fx_image,exception);
3064 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3065 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3066 magick_threads(image,fx_image,fx_image->rows,1)
3068 for (y=0; y < (ssize_t) fx_image->rows; y++)
3071 id = GetOpenMPThreadId();
3073 register const Quantum
3082 if (status == MagickFalse)
3084 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3085 q=QueueCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
3086 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3091 for (x=0; x < (ssize_t) fx_image->columns; x++)
3096 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3101 PixelChannel channel=GetPixelChannelChannel(image,i);
3102 PixelTrait traits=GetPixelChannelTraits(image,channel);
3103 PixelTrait fx_traits=GetPixelChannelTraits(fx_image,channel);
3104 if ((traits == UndefinedPixelTrait) ||
3105 (fx_traits == UndefinedPixelTrait))
3107 if (((fx_traits & CopyPixelTrait) != 0) ||
3108 (GetPixelReadMask(image,p) == 0))
3110 SetPixelChannel(fx_image,channel,p[i],q);
3114 (void) FxEvaluateChannelExpression(fx_info[id],channel,x,y,&alpha,
3116 q[i]=ClampToQuantum(QuantumRange*alpha);
3118 p+=GetPixelChannels(image);
3119 q+=GetPixelChannels(fx_image);
3121 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3123 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3128 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3129 #pragma omp critical (MagickCore_FxImage)
3131 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3132 if (proceed == MagickFalse)
3136 fx_view=DestroyCacheView(fx_view);
3137 image_view=DestroyCacheView(image_view);
3138 fx_info=DestroyFxThreadSet(fx_info);
3139 if (status == MagickFalse)
3140 fx_image=DestroyImage(fx_image);
3145 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3149 % I m p l o d e I m a g e %
3153 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3155 % ImplodeImage() creates a new image that is a copy of an existing
3156 % one with the image pixels "implode" by the specified percentage. It
3157 % allocates the memory necessary for the new Image structure and returns a
3158 % pointer to the new image.
3160 % The format of the ImplodeImage method is:
3162 % Image *ImplodeImage(const Image *image,const double amount,
3163 % const PixelInterpolateMethod method,ExceptionInfo *exception)
3165 % A description of each parameter follows:
3167 % o implode_image: Method ImplodeImage returns a pointer to the image
3168 % after it is implode. A null image is returned if there is a memory
3171 % o image: the image.
3173 % o amount: Define the extent of the implosion.
3175 % o method: the pixel interpolation method.
3177 % o exception: return any errors or warnings in this structure.
3180 MagickExport Image *ImplodeImage(const Image *image,const double amount,
3181 const PixelInterpolateMethod method,ExceptionInfo *exception)
3183 #define ImplodeImageTag "Implode/Image"
3210 Initialize implode image attributes.
3212 assert(image != (Image *) NULL);
3213 assert(image->signature == MagickSignature);
3214 if (image->debug != MagickFalse)
3215 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3216 assert(exception != (ExceptionInfo *) NULL);
3217 assert(exception->signature == MagickSignature);
3218 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3220 if (implode_image == (Image *) NULL)
3221 return((Image *) NULL);
3222 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
3224 implode_image=DestroyImage(implode_image);
3225 return((Image *) NULL);
3227 if (implode_image->background_color.alpha != OpaqueAlpha)
3228 implode_image->alpha_trait=BlendPixelTrait;
3230 Compute scaling factor.
3234 center.x=0.5*image->columns;
3235 center.y=0.5*image->rows;
3237 if (image->columns > image->rows)
3238 scale.y=(double) image->columns/(double) image->rows;
3240 if (image->columns < image->rows)
3242 scale.x=(double) image->rows/(double) image->columns;
3250 image_view=AcquireVirtualCacheView(image,exception);
3251 interpolate_view=AcquireVirtualCacheView(image,exception);
3252 implode_view=AcquireAuthenticCacheView(implode_image,exception);
3253 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3254 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3255 magick_threads(image,implode_image,image->rows,1)
3257 for (y=0; y < (ssize_t) image->rows; y++)
3265 register const Quantum
3274 if (status == MagickFalse)
3276 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3277 q=QueueCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
3279 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3284 delta.y=scale.y*(double) (y-center.y);
3285 for (x=0; x < (ssize_t) image->columns; x++)
3291 Determine if the pixel is within an ellipse.
3293 if (GetPixelReadMask(image,p) == 0)
3295 SetPixelBackgoundColor(implode_image,q);
3296 p+=GetPixelChannels(image);
3297 q+=GetPixelChannels(implode_image);
3300 delta.x=scale.x*(double) (x-center.x);
3301 distance=delta.x*delta.x+delta.y*delta.y;
3302 if (distance >= (radius*radius))
3303 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3305 PixelChannel channel=GetPixelChannelChannel(image,i);
3306 PixelTrait traits=GetPixelChannelTraits(image,channel);
3307 PixelTrait implode_traits=GetPixelChannelTraits(implode_image,
3309 if ((traits == UndefinedPixelTrait) ||
3310 (implode_traits == UndefinedPixelTrait))
3312 SetPixelChannel(implode_image,channel,p[i],q);
3324 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/radius/
3326 status=InterpolatePixelChannels(image,interpolate_view,implode_image,
3327 method,(double) (factor*delta.x/scale.x+center.x),(double) (factor*
3328 delta.y/scale.y+center.y),q,exception);
3330 p+=GetPixelChannels(image);
3331 q+=GetPixelChannels(implode_image);
3333 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3335 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3340 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3341 #pragma omp critical (MagickCore_ImplodeImage)
3343 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3344 if (proceed == MagickFalse)
3348 implode_view=DestroyCacheView(implode_view);
3349 interpolate_view=DestroyCacheView(interpolate_view);
3350 image_view=DestroyCacheView(image_view);
3351 if (status == MagickFalse)
3352 implode_image=DestroyImage(implode_image);
3353 return(implode_image);
3357 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3361 % M o r p h I m a g e s %
3365 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3367 % The MorphImages() method requires a minimum of two images. The first
3368 % image is transformed into the second by a number of intervening images
3369 % as specified by frames.
3371 % The format of the MorphImage method is:
3373 % Image *MorphImages(const Image *image,const size_t number_frames,
3374 % ExceptionInfo *exception)
3376 % A description of each parameter follows:
3378 % o image: the image.
3380 % o number_frames: Define the number of in-between image to generate.
3381 % The more in-between frames, the smoother the morph.
3383 % o exception: return any errors or warnings in this structure.
3386 MagickExport Image *MorphImages(const Image *image,const size_t number_frames,
3387 ExceptionInfo *exception)
3389 #define MorphImageTag "Morph/Image"
3405 register const Image
3415 Clone first frame in sequence.
3417 assert(image != (Image *) NULL);
3418 assert(image->signature == MagickSignature);
3419 if (image->debug != MagickFalse)
3420 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3421 assert(exception != (ExceptionInfo *) NULL);
3422 assert(exception->signature == MagickSignature);
3423 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3424 if (morph_images == (Image *) NULL)
3425 return((Image *) NULL);
3426 if (GetNextImageInList(image) == (Image *) NULL)
3431 for (i=1; i < (ssize_t) number_frames; i++)
3433 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3434 if (morph_image == (Image *) NULL)
3436 morph_images=DestroyImageList(morph_images);
3437 return((Image *) NULL);
3439 AppendImageToList(&morph_images,morph_image);
3440 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3445 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3447 if (proceed == MagickFalse)
3451 return(GetFirstImageInList(morph_images));
3454 Morph image sequence.
3459 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3461 for (i=0; i < (ssize_t) number_frames; i++)
3467 beta=(double) (i+1.0)/(double) (number_frames+1.0);
3469 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
3470 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*next->rows+beta*
3471 GetNextImageInList(next)->rows+0.5),next->filter,exception);
3472 if (morph_image == (Image *) NULL)
3474 morph_images=DestroyImageList(morph_images);
3475 return((Image *) NULL);
3477 status=SetImageStorageClass(morph_image,DirectClass,exception);
3478 if (status == MagickFalse)
3480 morph_image=DestroyImage(morph_image);
3481 return((Image *) NULL);
3483 AppendImageToList(&morph_images,morph_image);
3484 morph_images=GetLastImageInList(morph_images);
3485 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
3486 morph_images->rows,GetNextImageInList(next)->filter,exception);
3487 if (morph_image == (Image *) NULL)
3489 morph_images=DestroyImageList(morph_images);
3490 return((Image *) NULL);
3492 image_view=AcquireVirtualCacheView(morph_image,exception);
3493 morph_view=AcquireAuthenticCacheView(morph_images,exception);
3494 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3495 #pragma omp parallel for schedule(static,4) shared(status) \
3496 magick_threads(morph_image,morph_image,morph_image->rows,1)
3498 for (y=0; y < (ssize_t) morph_images->rows; y++)
3503 register const Quantum
3512 if (status == MagickFalse)
3514 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3516 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3518 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3523 for (x=0; x < (ssize_t) morph_images->columns; x++)
3528 for (i=0; i < (ssize_t) GetPixelChannels(morph_image); i++)
3530 PixelChannel channel=GetPixelChannelChannel(morph_image,i);
3531 PixelTrait traits=GetPixelChannelTraits(morph_image,channel);
3532 PixelTrait morph_traits=GetPixelChannelTraits(morph_images,channel);
3533 if ((traits == UndefinedPixelTrait) ||
3534 (morph_traits == UndefinedPixelTrait))
3536 if (((morph_traits & CopyPixelTrait) != 0) ||
3537 (GetPixelReadMask(morph_images,p) == 0))
3539 SetPixelChannel(morph_image,channel,p[i],q);
3542 SetPixelChannel(morph_image,channel,ClampToQuantum(alpha*
3543 GetPixelChannel(morph_images,channel,q)+beta*p[i]),q);
3545 p+=GetPixelChannels(morph_image);
3546 q+=GetPixelChannels(morph_images);
3548 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3549 if (sync == MagickFalse)
3552 morph_view=DestroyCacheView(morph_view);
3553 image_view=DestroyCacheView(image_view);
3554 morph_image=DestroyImage(morph_image);
3556 if (i < (ssize_t) number_frames)
3559 Clone last frame in sequence.
3561 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3562 if (morph_image == (Image *) NULL)
3564 morph_images=DestroyImageList(morph_images);
3565 return((Image *) NULL);
3567 AppendImageToList(&morph_images,morph_image);
3568 morph_images=GetLastImageInList(morph_images);
3569 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3574 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3575 #pragma omp critical (MagickCore_MorphImages)
3577 proceed=SetImageProgress(image,MorphImageTag,scene,
3578 GetImageListLength(image));
3579 if (proceed == MagickFalse)
3584 if (GetNextImageInList(next) != (Image *) NULL)
3586 morph_images=DestroyImageList(morph_images);
3587 return((Image *) NULL);
3589 return(GetFirstImageInList(morph_images));
3593 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3597 % P l a s m a I m a g e %
3601 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3603 % PlasmaImage() initializes an image with plasma fractal values. The image
3604 % must be initialized with a base color and the random number generator
3605 % seeded before this method is called.
3607 % The format of the PlasmaImage method is:
3609 % MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
3610 % size_t attenuate,size_t depth,ExceptionInfo *exception)
3612 % A description of each parameter follows:
3614 % o image: the image.
3616 % o segment: Define the region to apply plasma fractals values.
3618 % o attenuate: Define the plasma attenuation factor.
3620 % o depth: Limit the plasma recursion depth.
3622 % o exception: return any errors or warnings in this structure.
3626 static inline Quantum PlasmaPixel(RandomInfo *random_info,
3627 const double pixel,const double noise)
3632 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
3637 static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3638 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3639 const SegmentInfo *segment,size_t attenuate,size_t depth,
3640 ExceptionInfo *exception)
3645 register const Quantum
3661 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3669 Divide the area into quadrants and recurse.
3673 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3674 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
3675 local_info=(*segment);
3676 local_info.x2=(double) x_mid;
3677 local_info.y2=(double) y_mid;
3678 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3679 &local_info,attenuate,depth,exception);
3680 local_info=(*segment);
3681 local_info.y1=(double) y_mid;
3682 local_info.x2=(double) x_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.x1=(double) x_mid;
3687 local_info.y2=(double) y_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.y1=(double) y_mid;
3693 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3694 &local_info,attenuate,depth,exception));
3696 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3697 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
3698 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3699 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3700 return(MagickFalse);
3702 Average pixels and apply plasma.
3704 plasma=(double) QuantumRange/(2.0*attenuate);
3705 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3710 x=(ssize_t) ceil(segment->x1-0.5);
3711 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),1,1,
3713 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),1,1,
3715 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
3716 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3717 (q == (Quantum *) NULL))
3719 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3721 PixelChannel channel=GetPixelChannelChannel(image,i);
3722 PixelTrait traits=GetPixelChannelTraits(image,channel);
3723 if (traits == UndefinedPixelTrait)
3725 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
3727 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3728 if (segment->x1 != segment->x2)
3733 x=(ssize_t) ceil(segment->x2-0.5);
3734 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3736 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3738 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
3739 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3740 (q == (Quantum *) NULL))
3742 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3744 PixelChannel channel=GetPixelChannelChannel(image,i);
3745 PixelTrait traits=GetPixelChannelTraits(image,channel);
3746 if (traits == UndefinedPixelTrait)
3748 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
3750 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3753 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3755 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3760 y=(ssize_t) ceil(segment->y2-0.5);
3761 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3763 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3765 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
3766 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3767 (q == (Quantum *) NULL))
3769 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3771 PixelChannel channel=GetPixelChannelChannel(image,i);
3772 PixelTrait traits=GetPixelChannelTraits(image,channel);
3773 if (traits == UndefinedPixelTrait)
3775 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
3777 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3779 if (segment->y1 != segment->y2)
3784 y=(ssize_t) ceil(segment->y1-0.5);
3785 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3787 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3789 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
3790 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3791 (q == (Quantum *) NULL))
3793 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3795 PixelChannel channel=GetPixelChannelChannel(image,i);
3796 PixelTrait traits=GetPixelChannelTraits(image,channel);
3797 if (traits == UndefinedPixelTrait)
3799 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
3801 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3804 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3809 x=(ssize_t) ceil(segment->x1-0.5);
3810 y=(ssize_t) ceil(segment->y1-0.5);
3811 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
3812 x=(ssize_t) ceil(segment->x2-0.5);
3813 y=(ssize_t) ceil(segment->y2-0.5);
3814 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
3815 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
3816 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3817 (q == (Quantum *) NULL))
3819 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3821 PixelChannel channel=GetPixelChannelChannel(image,i);
3822 PixelTrait traits=GetPixelChannelTraits(image,channel);
3823 if (traits == UndefinedPixelTrait)
3825 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
3827 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3829 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3831 return(MagickFalse);
3834 MagickExport MagickBooleanType PlasmaImage(Image *image,
3835 const SegmentInfo *segment,size_t attenuate,size_t depth,
3836 ExceptionInfo *exception)
3849 if (image->debug != MagickFalse)
3850 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3851 assert(image != (Image *) NULL);
3852 assert(image->signature == MagickSignature);
3853 if (image->debug != MagickFalse)
3854 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3855 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
3856 return(MagickFalse);
3857 image_view=AcquireAuthenticCacheView(image,exception);
3858 u_view=AcquireVirtualCacheView(image,exception);
3859 v_view=AcquireVirtualCacheView(image,exception);
3860 random_info=AcquireRandomInfo();
3861 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3862 attenuate,depth,exception);
3863 random_info=DestroyRandomInfo(random_info);
3864 v_view=DestroyCacheView(v_view);
3865 u_view=DestroyCacheView(u_view);
3866 image_view=DestroyCacheView(image_view);
3871 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3875 % P o l a r o i d I m a g e %
3879 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3881 % PolaroidImage() simulates a Polaroid picture.
3883 % The format of the AnnotateImage method is:
3885 % Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
3886 % const char *caption,const double angle,
3887 % const PixelInterpolateMethod method,ExceptionInfo exception)
3889 % A description of each parameter follows:
3891 % o image: the image.
3893 % o draw_info: the draw info.
3895 % o caption: the Polaroid caption.
3897 % o angle: Apply the effect along this angle.
3899 % o method: the pixel interpolation method.
3901 % o exception: return any errors or warnings in this structure.
3904 MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
3905 const char *caption,const double angle,const PixelInterpolateMethod method,
3906 ExceptionInfo *exception)
3924 Simulate a Polaroid picture.
3926 assert(image != (Image *) NULL);
3927 assert(image->signature == MagickSignature);
3928 if (image->debug != MagickFalse)
3929 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3930 assert(exception != (ExceptionInfo *) NULL);
3931 assert(exception->signature == MagickSignature);
3932 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
3933 image->rows)/25.0,10.0);
3934 height=image->rows+2*quantum;
3935 caption_image=(Image *) NULL;
3936 if (caption != (const char *) NULL)
3939 geometry[MaxTextExtent],
3958 Generate caption image.
3960 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3961 if (caption_image == (Image *) NULL)
3962 return((Image *) NULL);
3963 image_info=AcquireImageInfo();
3964 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
3965 text=InterpretImageProperties(image_info,(Image *) image,caption,
3967 image_info=DestroyImageInfo(image_info);
3968 (void) CloneString(&annotate_info->text,text);
3969 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
3971 status=SetImageExtent(caption_image,image->columns,(size_t) ((count+1)*
3972 (metrics.ascent-metrics.descent)+0.5),exception);
3973 if (status == MagickFalse)
3974 caption_image=DestroyImage(caption_image);
3977 caption_image->background_color=image->border_color;
3978 (void) SetImageBackgroundColor(caption_image,exception);
3979 (void) CloneString(&annotate_info->text,text);
3980 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
3982 if (annotate_info->gravity == UndefinedGravity)
3983 (void) CloneString(&annotate_info->geometry,AcquireString(
3985 (void) AnnotateImage(caption_image,annotate_info,exception);
3986 height+=caption_image->rows;
3988 annotate_info=DestroyDrawInfo(annotate_info);
3989 text=DestroyString(text);
3991 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
3993 if (picture_image == (Image *) NULL)
3995 if (caption_image != (Image *) NULL)
3996 caption_image=DestroyImage(caption_image);
3997 return((Image *) NULL);
3999 picture_image->background_color=image->border_color;
4000 (void) SetImageBackgroundColor(picture_image,exception);
4001 (void) CompositeImage(picture_image,image,OverCompositeOp,MagickTrue,quantum,
4003 if (caption_image != (Image *) NULL)
4005 (void) CompositeImage(picture_image,caption_image,OverCompositeOp,
4006 MagickTrue,quantum,(ssize_t) (image->rows+3*quantum/2),exception);
4007 caption_image=DestroyImage(caption_image);
4009 (void) QueryColorCompliance("none",AllCompliance,
4010 &picture_image->background_color,exception);
4011 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
4012 rotate_image=RotateImage(picture_image,90.0,exception);
4013 picture_image=DestroyImage(picture_image);
4014 if (rotate_image == (Image *) NULL)
4015 return((Image *) NULL);
4016 picture_image=rotate_image;
4017 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
4018 picture_image->columns,method,exception);
4019 picture_image=DestroyImage(picture_image);
4020 if (bend_image == (Image *) NULL)
4021 return((Image *) NULL);
4022 picture_image=bend_image;
4023 rotate_image=RotateImage(picture_image,-90.0,exception);
4024 picture_image=DestroyImage(picture_image);
4025 if (rotate_image == (Image *) NULL)
4026 return((Image *) NULL);
4027 picture_image=rotate_image;
4028 picture_image->background_color=image->background_color;
4029 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
4031 if (polaroid_image == (Image *) NULL)
4033 picture_image=DestroyImage(picture_image);
4034 return(picture_image);
4036 flop_image=FlopImage(polaroid_image,exception);
4037 polaroid_image=DestroyImage(polaroid_image);
4038 if (flop_image == (Image *) NULL)
4040 picture_image=DestroyImage(picture_image);
4041 return(picture_image);
4043 polaroid_image=flop_image;
4044 (void) CompositeImage(polaroid_image,picture_image,OverCompositeOp,
4045 MagickTrue,(ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
4046 picture_image=DestroyImage(picture_image);
4047 (void) QueryColorCompliance("none",AllCompliance,
4048 &polaroid_image->background_color,exception);
4049 rotate_image=RotateImage(polaroid_image,angle,exception);
4050 polaroid_image=DestroyImage(polaroid_image);
4051 if (rotate_image == (Image *) NULL)
4052 return((Image *) NULL);
4053 polaroid_image=rotate_image;
4054 trim_image=TrimImage(polaroid_image,exception);
4055 polaroid_image=DestroyImage(polaroid_image);
4056 if (trim_image == (Image *) NULL)
4057 return((Image *) NULL);
4058 polaroid_image=trim_image;
4059 return(polaroid_image);
4063 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4067 % S e p i a T o n e I m a g e %
4071 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4073 % MagickSepiaToneImage() applies a special effect to the image, similar to the
4074 % effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4075 % 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4076 % threshold of 80% is a good starting point for a reasonable tone.
4078 % The format of the SepiaToneImage method is:
4080 % Image *SepiaToneImage(const Image *image,const double threshold,
4081 % ExceptionInfo *exception)
4083 % A description of each parameter follows:
4085 % o image: the image.
4087 % o threshold: the tone threshold.
4089 % o exception: return any errors or warnings in this structure.
4092 MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4093 ExceptionInfo *exception)
4095 #define SepiaToneImageTag "SepiaTone/Image"
4114 Initialize sepia-toned image attributes.
4116 assert(image != (const Image *) NULL);
4117 assert(image->signature == MagickSignature);
4118 if (image->debug != MagickFalse)
4119 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4120 assert(exception != (ExceptionInfo *) NULL);
4121 assert(exception->signature == MagickSignature);
4122 sepia_image=CloneImage(image,0,0,MagickTrue,exception);
4123 if (sepia_image == (Image *) NULL)
4124 return((Image *) NULL);
4125 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
4127 sepia_image=DestroyImage(sepia_image);
4128 return((Image *) NULL);
4131 Tone each row of the image.
4135 image_view=AcquireVirtualCacheView(image,exception);
4136 sepia_view=AcquireAuthenticCacheView(sepia_image,exception);
4137 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4138 #pragma omp parallel for schedule(static,4) shared(progress,status) \
4139 magick_threads(image,sepia_image,image->rows,1)
4141 for (y=0; y < (ssize_t) image->rows; y++)
4143 register const Quantum
4152 if (status == MagickFalse)
4154 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
4155 q=GetCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
4157 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
4162 for (x=0; x < (ssize_t) image->columns; x++)
4168 intensity=GetPixelIntensity(image,p);
4169 tone=intensity > threshold ? (double) QuantumRange : intensity+
4170 (double) QuantumRange-threshold;
4171 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
4172 tone=intensity > (7.0*threshold/6.0) ? (double) QuantumRange :
4173 intensity+(double) QuantumRange-7.0*threshold/6.0;
4174 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4175 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
4176 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
4178 if ((double) GetPixelGreen(image,q) < tone)
4179 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4180 if ((double) GetPixelBlue(image,q) < tone)
4181 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
4182 SetPixelAlpha(sepia_image,GetPixelAlpha(image,p),q);
4183 p+=GetPixelChannels(image);
4184 q+=GetPixelChannels(sepia_image);
4186 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4188 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4193 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4194 #pragma omp critical (MagickCore_SepiaToneImage)
4196 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4198 if (proceed == MagickFalse)
4202 sepia_view=DestroyCacheView(sepia_view);
4203 image_view=DestroyCacheView(image_view);
4204 (void) NormalizeImage(sepia_image,exception);
4205 (void) ContrastImage(sepia_image,MagickTrue,exception);
4206 if (status == MagickFalse)
4207 sepia_image=DestroyImage(sepia_image);
4208 return(sepia_image);
4212 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4216 % S h a d o w I m a g e %
4220 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4222 % ShadowImage() simulates a shadow from the specified image and returns it.
4224 % The format of the ShadowImage method is:
4226 % Image *ShadowImage(const Image *image,const double alpha,
4227 % const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4228 % ExceptionInfo *exception)
4230 % A description of each parameter follows:
4232 % o image: the image.
4234 % o alpha: percentage transparency.
4236 % o sigma: the standard deviation of the Gaussian, in pixels.
4238 % o x_offset: the shadow x-offset.
4240 % o y_offset: the shadow y-offset.
4242 % o exception: return any errors or warnings in this structure.
4245 MagickExport Image *ShadowImage(const Image *image,const double alpha,
4246 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4247 ExceptionInfo *exception)
4249 #define ShadowImageTag "Shadow/Image"
4271 assert(image != (Image *) NULL);
4272 assert(image->signature == MagickSignature);
4273 if (image->debug != MagickFalse)
4274 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4275 assert(exception != (ExceptionInfo *) NULL);
4276 assert(exception->signature == MagickSignature);
4277 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4278 if (clone_image == (Image *) NULL)
4279 return((Image *) NULL);
4280 if (IsGrayColorspace(image->colorspace) != MagickFalse)
4281 (void) SetImageColorspace(clone_image,sRGBColorspace,exception);
4282 (void) SetImageVirtualPixelMethod(clone_image,TransparentVirtualPixelMethod,
4284 border_info.width=(size_t) floor(2.0*sigma+0.5);
4285 border_info.height=(size_t) floor(2.0*sigma+0.5);
4288 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4290 clone_image->alpha_trait=BlendPixelTrait;
4291 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
4292 clone_image=DestroyImage(clone_image);
4293 if (border_image == (Image *) NULL)
4294 return((Image *) NULL);
4295 if (border_image->alpha_trait == UndefinedPixelTrait)
4296 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
4301 image_view=AcquireAuthenticCacheView(border_image,exception);
4302 for (y=0; y < (ssize_t) border_image->rows; y++)
4313 if (status == MagickFalse)
4315 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
4317 if (q == (Quantum *) NULL)
4322 background_color=border_image->background_color;
4323 background_color.alpha_trait=BlendPixelTrait;
4324 for (x=0; x < (ssize_t) border_image->columns; x++)
4326 if (border_image->alpha_trait != UndefinedPixelTrait)
4327 background_color.alpha=GetPixelAlpha(border_image,q)*alpha/100.0;
4328 SetPixelViaPixelInfo(border_image,&background_color,q);
4329 q+=GetPixelChannels(border_image);
4331 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4334 image_view=DestroyCacheView(image_view);
4335 if (status == MagickFalse)
4337 border_image=DestroyImage(border_image);
4338 return((Image *) NULL);
4340 channel_mask=SetImageChannelMask(border_image,AlphaChannel);
4341 shadow_image=BlurImage(border_image,0.0,sigma,exception);
4342 border_image=DestroyImage(border_image);
4343 if (shadow_image == (Image *) NULL)
4344 return((Image *) NULL);
4345 SetPixelChannelMask(shadow_image,channel_mask);
4346 if (shadow_image->page.width == 0)
4347 shadow_image->page.width=shadow_image->columns;
4348 if (shadow_image->page.height == 0)
4349 shadow_image->page.height=shadow_image->rows;
4350 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4351 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4352 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4353 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
4354 return(shadow_image);
4358 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4362 % S k e t c h I m a g e %
4366 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4368 % SketchImage() simulates a pencil sketch. We convolve the image with a
4369 % Gaussian operator of the given radius and standard deviation (sigma). For
4370 % reasonable results, radius should be larger than sigma. Use a radius of 0
4371 % and SketchImage() selects a suitable radius for you. Angle gives the angle
4374 % The format of the SketchImage method is:
4376 % Image *SketchImage(const Image *image,const double radius,
4377 % const double sigma,const double angle,ExceptionInfo *exception)
4379 % A description of each parameter follows:
4381 % o image: the image.
4383 % o radius: the radius of the Gaussian, in pixels, not counting the
4386 % o sigma: the standard deviation of the Gaussian, in pixels.
4388 % o angle: apply the effect along this angle.
4390 % o exception: return any errors or warnings in this structure.
4393 MagickExport Image *SketchImage(const Image *image,const double radius,
4394 const double sigma,const double angle,ExceptionInfo *exception)
4410 **restrict random_info;
4415 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4423 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4424 MagickTrue,exception);
4425 if (random_image == (Image *) NULL)
4426 return((Image *) NULL);
4428 random_info=AcquireRandomInfoThreadSet();
4429 random_view=AcquireAuthenticCacheView(random_image,exception);
4430 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4431 key=GetRandomSecretKey(random_info[0]);
4432 #pragma omp parallel for schedule(static,4) shared(status) \
4433 magick_threads(random_image,random_image,random_image->rows,key == ~0UL)
4435 for (y=0; y < (ssize_t) random_image->rows; y++)
4438 id = GetOpenMPThreadId();
4446 if (status == MagickFalse)
4448 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4450 if (q == (Quantum *) NULL)
4455 for (x=0; x < (ssize_t) random_image->columns; x++)
4463 if (GetPixelReadMask(random_image,q) == 0)
4465 q+=GetPixelChannels(random_image);
4468 value=GetPseudoRandomValue(random_info[id]);
4469 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4471 PixelChannel channel=GetPixelChannelChannel(image,i);
4472 PixelTrait traits=GetPixelChannelTraits(image,channel);
4473 if (traits == UndefinedPixelTrait)
4475 q[i]=ClampToQuantum(QuantumRange*value);
4477 q+=GetPixelChannels(random_image);
4479 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4482 random_view=DestroyCacheView(random_view);
4483 random_info=DestroyRandomInfoThreadSet(random_info);
4484 if (status == MagickFalse)
4486 random_image=DestroyImage(random_image);
4487 return(random_image);
4489 blur_image=MotionBlurImage(random_image,radius,sigma,angle,exception);
4490 random_image=DestroyImage(random_image);
4491 if (blur_image == (Image *) NULL)
4492 return((Image *) NULL);
4493 dodge_image=EdgeImage(blur_image,radius,exception);
4494 blur_image=DestroyImage(blur_image);
4495 if (dodge_image == (Image *) NULL)
4496 return((Image *) NULL);
4497 (void) NormalizeImage(dodge_image,exception);
4498 (void) NegateImage(dodge_image,MagickFalse,exception);
4499 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
4500 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4501 if (sketch_image == (Image *) NULL)
4503 dodge_image=DestroyImage(dodge_image);
4504 return((Image *) NULL);
4506 (void) CompositeImage(sketch_image,dodge_image,ColorDodgeCompositeOp,
4507 MagickTrue,0,0,exception);
4508 dodge_image=DestroyImage(dodge_image);
4509 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4510 if (blend_image == (Image *) NULL)
4512 sketch_image=DestroyImage(sketch_image);
4513 return((Image *) NULL);
4515 (void) SetImageArtifact(blend_image,"compose:args","20x80");
4516 (void) CompositeImage(sketch_image,blend_image,BlendCompositeOp,MagickTrue,
4518 blend_image=DestroyImage(blend_image);
4519 return(sketch_image);
4523 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4527 % S o l a r i z e I m a g e %
4531 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4533 % SolarizeImage() applies a special effect to the image, similar to the effect
4534 % achieved in a photo darkroom by selectively exposing areas of photo
4535 % sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4536 % measure of the extent of the solarization.
4538 % The format of the SolarizeImage method is:
4540 % MagickBooleanType SolarizeImage(Image *image,const double threshold,
4541 % ExceptionInfo *exception)
4543 % A description of each parameter follows:
4545 % o image: the image.
4547 % o threshold: Define the extent of the solarization.
4549 % o exception: return any errors or warnings in this structure.
4552 MagickExport MagickBooleanType SolarizeImage(Image *image,
4553 const double threshold,ExceptionInfo *exception)
4555 #define SolarizeImageTag "Solarize/Image"
4569 assert(image != (Image *) NULL);
4570 assert(image->signature == MagickSignature);
4571 if (image->debug != MagickFalse)
4572 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4573 if (IsGrayColorspace(image->colorspace) != MagickFalse)
4574 (void) SetImageColorspace(image,sRGBColorspace,exception);
4575 if (image->storage_class == PseudoClass)
4583 for (i=0; i < (ssize_t) image->colors; i++)
4585 if ((double) image->colormap[i].red > threshold)
4586 image->colormap[i].red=QuantumRange-image->colormap[i].red;
4587 if ((double) image->colormap[i].green > threshold)
4588 image->colormap[i].green=QuantumRange-image->colormap[i].green;
4589 if ((double) image->colormap[i].blue > threshold)
4590 image->colormap[i].blue=QuantumRange-image->colormap[i].blue;
4598 image_view=AcquireAuthenticCacheView(image,exception);
4599 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4600 #pragma omp parallel for schedule(static,4) shared(progress,status) \
4601 magick_threads(image,image,image->rows,1)
4603 for (y=0; y < (ssize_t) image->rows; y++)
4611 if (status == MagickFalse)
4613 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
4614 if (q == (Quantum *) NULL)
4619 for (x=0; x < (ssize_t) image->columns; x++)
4624 if (GetPixelReadMask(image,q) == 0)
4626 q+=GetPixelChannels(image);
4629 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4631 PixelChannel channel=GetPixelChannelChannel(image,i);
4632 PixelTrait traits=GetPixelChannelTraits(image,channel);
4633 if ((traits & UpdatePixelTrait) == 0)
4635 if ((double) q[i] > threshold)
4636 q[i]=QuantumRange-q[i];
4638 q+=GetPixelChannels(image);
4640 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4642 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4647 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4648 #pragma omp critical (MagickCore_SolarizeImage)
4650 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4651 if (proceed == MagickFalse)
4655 image_view=DestroyCacheView(image_view);
4660 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4664 % S t e g a n o I m a g e %
4668 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4670 % SteganoImage() hides a digital watermark within the image. Recover
4671 % the hidden watermark later to prove that the authenticity of an image.
4672 % Offset defines the start position within the image to hide the watermark.
4674 % The format of the SteganoImage method is:
4676 % Image *SteganoImage(const Image *image,Image *watermark,
4677 % ExceptionInfo *exception)
4679 % A description of each parameter follows:
4681 % o image: the image.
4683 % o watermark: the watermark image.
4685 % o exception: return any errors or warnings in this structure.
4688 MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4689 ExceptionInfo *exception)
4691 #define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
4692 #define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
4693 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
4694 #define SteganoImageTag "Stegano/Image"
4729 Initialize steganographic image attributes.
4731 assert(image != (const Image *) NULL);
4732 assert(image->signature == MagickSignature);
4733 if (image->debug != MagickFalse)
4734 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4735 assert(watermark != (const Image *) NULL);
4736 assert(watermark->signature == MagickSignature);
4737 assert(exception != (ExceptionInfo *) NULL);
4738 assert(exception->signature == MagickSignature);
4740 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4741 if (stegano_image == (Image *) NULL)
4742 return((Image *) NULL);
4743 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
4744 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
4746 stegano_image=DestroyImage(stegano_image);
4747 return((Image *) NULL);
4750 Hide watermark in low-order bits of image.
4755 depth=stegano_image->depth;
4756 k=stegano_image->offset;
4758 watermark_view=AcquireVirtualCacheView(watermark,exception);
4759 stegano_view=AcquireAuthenticCacheView(stegano_image,exception);
4760 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
4762 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
4764 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
4769 (void) GetOneCacheViewVirtualPixelInfo(watermark_view,x,y,&pixel,
4771 offset=k/(ssize_t) stegano_image->columns;
4772 if (offset >= (ssize_t) stegano_image->rows)
4774 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4775 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4777 if (q == (Quantum *) NULL)
4783 SetPixelRed(stegano_image,SetBit(GetPixelRed(stegano_image,q),j,
4784 GetBit(GetPixelInfoIntensity(stegano_image,&pixel),i)),q);
4789 SetPixelGreen(stegano_image,SetBit(GetPixelGreen(stegano_image,q),j,
4790 GetBit(GetPixelInfoIntensity(stegano_image,&pixel),i)),q);
4795 SetPixelBlue(stegano_image,SetBit(GetPixelBlue(stegano_image,q),j,
4796 GetBit(GetPixelInfoIntensity(stegano_image,&pixel),i)),q);
4800 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
4806 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
4808 if (k == stegano_image->offset)
4812 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4817 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4819 if (proceed == MagickFalse)
4823 stegano_view=DestroyCacheView(stegano_view);
4824 watermark_view=DestroyCacheView(watermark_view);
4825 if (status == MagickFalse)
4826 stegano_image=DestroyImage(stegano_image);
4827 return(stegano_image);
4831 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4835 % S t e r e o A n a g l y p h I m a g e %
4839 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4841 % StereoAnaglyphImage() combines two images and produces a single image that
4842 % is the composite of a left and right image of a stereo pair. Special
4843 % red-green stereo glasses are required to view this effect.
4845 % The format of the StereoAnaglyphImage method is:
4847 % Image *StereoImage(const Image *left_image,const Image *right_image,
4848 % ExceptionInfo *exception)
4849 % Image *StereoAnaglyphImage(const Image *left_image,
4850 % const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
4851 % ExceptionInfo *exception)
4853 % A description of each parameter follows:
4855 % o left_image: the left image.
4857 % o right_image: the right image.
4859 % o exception: return any errors or warnings in this structure.
4861 % o x_offset: amount, in pixels, by which the left image is offset to the
4862 % right of the right image.
4864 % o y_offset: amount, in pixels, by which the left image is offset to the
4865 % bottom of the right image.
4869 MagickExport Image *StereoImage(const Image *left_image,
4870 const Image *right_image,ExceptionInfo *exception)
4872 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4875 MagickExport Image *StereoAnaglyphImage(const Image *left_image,
4876 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
4877 ExceptionInfo *exception)
4879 #define StereoImageTag "Stereo/Image"
4893 assert(left_image != (const Image *) NULL);
4894 assert(left_image->signature == MagickSignature);
4895 if (left_image->debug != MagickFalse)
4896 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4897 left_image->filename);
4898 assert(right_image != (const Image *) NULL);
4899 assert(right_image->signature == MagickSignature);
4900 assert(exception != (ExceptionInfo *) NULL);
4901 assert(exception->signature == MagickSignature);
4902 assert(right_image != (const Image *) NULL);
4904 if ((left_image->columns != right_image->columns) ||
4905 (left_image->rows != right_image->rows))
4906 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4908 Initialize stereo image attributes.
4910 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4911 MagickTrue,exception);
4912 if (stereo_image == (Image *) NULL)
4913 return((Image *) NULL);
4914 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
4916 stereo_image=DestroyImage(stereo_image);
4917 return((Image *) NULL);
4919 (void) SetImageColorspace(stereo_image,sRGBColorspace,exception);
4921 Copy left image to red channel and right image to blue channel.
4924 for (y=0; y < (ssize_t) stereo_image->rows; y++)
4926 register const Quantum
4936 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4938 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4939 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
4940 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4941 (r == (Quantum *) NULL))
4943 for (x=0; x < (ssize_t) stereo_image->columns; x++)
4945 SetPixelRed(image,GetPixelRed(left_image,p),r);
4946 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4947 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4948 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4949 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4950 GetPixelAlpha(right_image,q))/2,r);
4951 p+=GetPixelChannels(left_image);
4952 q+=GetPixelChannels(right_image);
4953 r+=GetPixelChannels(stereo_image);
4955 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4957 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4962 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4963 stereo_image->rows);
4964 if (proceed == MagickFalse)
4968 if (status == MagickFalse)
4969 stereo_image=DestroyImage(stereo_image);
4970 return(stereo_image);
4974 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4978 % S w i r l I m a g e %
4982 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4984 % SwirlImage() swirls the pixels about the center of the image, where
4985 % degrees indicates the sweep of the arc through which each pixel is moved.
4986 % You get a more dramatic effect as the degrees move from 1 to 360.
4988 % The format of the SwirlImage method is:
4990 % Image *SwirlImage(const Image *image,double degrees,
4991 % const PixelInterpolateMethod method,ExceptionInfo *exception)
4993 % A description of each parameter follows:
4995 % o image: the image.
4997 % o degrees: Define the tightness of the swirling effect.
4999 % o method: the pixel interpolation method.
5001 % o exception: return any errors or warnings in this structure.
5004 MagickExport Image *SwirlImage(const Image *image,double degrees,
5005 const PixelInterpolateMethod method,ExceptionInfo *exception)
5007 #define SwirlImageTag "Swirl/Image"
5034 Initialize swirl image attributes.
5036 assert(image != (const Image *) NULL);
5037 assert(image->signature == MagickSignature);
5038 if (image->debug != MagickFalse)
5039 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5040 assert(exception != (ExceptionInfo *) NULL);
5041 assert(exception->signature == MagickSignature);
5042 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5043 if (swirl_image == (Image *) NULL)
5044 return((Image *) NULL);
5045 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
5047 swirl_image=DestroyImage(swirl_image);
5048 return((Image *) NULL);
5050 if (swirl_image->background_color.alpha != OpaqueAlpha)
5051 swirl_image->alpha_trait=BlendPixelTrait;
5053 Compute scaling factor.
5055 center.x=(double) image->columns/2.0;
5056 center.y=(double) image->rows/2.0;
5057 radius=MagickMax(center.x,center.y);
5060 if (image->columns > image->rows)
5061 scale.y=(double) image->columns/(double) image->rows;
5063 if (image->columns < image->rows)
5064 scale.x=(double) image->rows/(double) image->columns;
5065 degrees=(double) DegreesToRadians(degrees);
5071 image_view=AcquireVirtualCacheView(image,exception);
5072 interpolate_view=AcquireVirtualCacheView(image,exception);
5073 swirl_view=AcquireAuthenticCacheView(swirl_image,exception);
5074 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5075 #pragma omp parallel for schedule(static,4) shared(progress,status) \
5076 magick_threads(image,swirl_image,image->rows,1)
5078 for (y=0; y < (ssize_t) image->rows; y++)
5086 register const Quantum
5095 if (status == MagickFalse)
5097 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5098 q=QueueCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
5100 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
5105 delta.y=scale.y*(double) (y-center.y);
5106 for (x=0; x < (ssize_t) image->columns; x++)
5109 Determine if the pixel is within an ellipse.
5111 if (GetPixelReadMask(image,p) == 0)
5113 SetPixelBackgoundColor(swirl_image,q);
5114 p+=GetPixelChannels(image);
5115 q+=GetPixelChannels(swirl_image);
5118 delta.x=scale.x*(double) (x-center.x);
5119 distance=delta.x*delta.x+delta.y*delta.y;
5120 if (distance >= (radius*radius))
5125 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5127 PixelChannel channel=GetPixelChannelChannel(image,i);
5128 PixelTrait traits=GetPixelChannelTraits(image,channel);
5129 PixelTrait swirl_traits=GetPixelChannelTraits(swirl_image,channel);
5130 if ((traits == UndefinedPixelTrait) ||
5131 (swirl_traits == UndefinedPixelTrait))
5133 SetPixelChannel(swirl_image,channel,p[i],q);
5146 factor=1.0-sqrt((double) distance)/radius;
5147 sine=sin((double) (degrees*factor*factor));
5148 cosine=cos((double) (degrees*factor*factor));
5149 status=InterpolatePixelChannels(image,interpolate_view,swirl_image,
5150 method,((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5151 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
5153 p+=GetPixelChannels(image);
5154 q+=GetPixelChannels(swirl_image);
5156 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5158 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5163 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5164 #pragma omp critical (MagickCore_SwirlImage)
5166 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5167 if (proceed == MagickFalse)
5171 swirl_view=DestroyCacheView(swirl_view);
5172 interpolate_view=DestroyCacheView(interpolate_view);
5173 image_view=DestroyCacheView(image_view);
5174 if (status == MagickFalse)
5175 swirl_image=DestroyImage(swirl_image);
5176 return(swirl_image);
5180 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5184 % T i n t I m a g e %
5188 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5190 % TintImage() applies a color vector to each pixel in the image. The length
5191 % of the vector is 0 for black and white and at its maximum for the midtones.
5192 % The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5194 % The format of the TintImage method is:
5196 % Image *TintImage(const Image *image,const char *blend,
5197 % const PixelInfo *tint,ExceptionInfo *exception)
5199 % A description of each parameter follows:
5201 % o image: the image.
5203 % o blend: A color value used for tinting.
5205 % o tint: A color value used for tinting.
5207 % o exception: return any errors or warnings in this structure.
5210 MagickExport Image *TintImage(const Image *image,const char *blend,
5211 const PixelInfo *tint,ExceptionInfo *exception)
5213 #define TintImageTag "Tint/Image"
5244 Allocate tint image.
5246 assert(image != (const Image *) NULL);
5247 assert(image->signature == MagickSignature);
5248 if (image->debug != MagickFalse)
5249 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5250 assert(exception != (ExceptionInfo *) NULL);
5251 assert(exception->signature == MagickSignature);
5252 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5253 if (tint_image == (Image *) NULL)
5254 return((Image *) NULL);
5255 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
5257 tint_image=DestroyImage(tint_image);
5258 return((Image *) NULL);
5260 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
5261 (IsPixelInfoGray(tint) == MagickFalse))
5262 (void) SetImageColorspace(tint_image,sRGBColorspace,exception);
5263 if (blend == (const char *) NULL)
5266 Determine RGB values of the color.
5268 GetPixelInfo(image,&color_vector);
5269 flags=ParseGeometry(blend,&geometry_info);
5270 color_vector.red=geometry_info.rho;
5271 color_vector.green=geometry_info.rho;
5272 color_vector.blue=geometry_info.rho;
5273 color_vector.alpha=(MagickRealType) OpaqueAlpha;
5274 if ((flags & SigmaValue) != 0)
5275 color_vector.green=geometry_info.sigma;
5276 if ((flags & XiValue) != 0)
5277 color_vector.blue=geometry_info.xi;
5278 if ((flags & PsiValue) != 0)
5279 color_vector.alpha=geometry_info.psi;
5280 if (image->colorspace == CMYKColorspace)
5282 color_vector.black=geometry_info.rho;
5283 if ((flags & PsiValue) != 0)
5284 color_vector.black=geometry_info.psi;
5285 if ((flags & ChiValue) != 0)
5286 color_vector.alpha=geometry_info.chi;
5288 intensity=(double) GetPixelInfoIntensity(image,tint);
5289 color_vector.red=(double) (color_vector.red*tint->red/100.0-intensity);
5290 color_vector.green=(double) (color_vector.green*tint->green/100.0-intensity);
5291 color_vector.blue=(double) (color_vector.blue*tint->blue/100.0-intensity);
5292 color_vector.black=(double) (color_vector.black*tint->black/100.0-intensity);
5293 color_vector.alpha=(double) (color_vector.alpha*tint->alpha/100.0-intensity);
5299 image_view=AcquireVirtualCacheView(image,exception);
5300 tint_view=AcquireAuthenticCacheView(tint_image,exception);
5301 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5302 #pragma omp parallel for schedule(static,4) shared(progress,status) \
5303 magick_threads(image,tint_image,image->rows,1)
5305 for (y=0; y < (ssize_t) image->rows; y++)
5307 register const Quantum
5316 if (status == MagickFalse)
5318 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5319 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5321 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
5326 for (x=0; x < (ssize_t) image->columns; x++)
5337 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5339 PixelChannel channel=GetPixelChannelChannel(image,i);
5340 PixelTrait traits=GetPixelChannelTraits(image,channel);
5341 PixelTrait tint_traits=GetPixelChannelTraits(tint_image,channel);
5342 if ((traits == UndefinedPixelTrait) ||
5343 (tint_traits == UndefinedPixelTrait))
5345 if (((tint_traits & CopyPixelTrait) != 0) ||
5346 (GetPixelReadMask(image,p) == 0))
5348 SetPixelChannel(tint_image,channel,p[i],q);
5352 GetPixelInfo(image,&pixel);
5353 weight=QuantumScale*GetPixelRed(image,p)-0.5;
5354 pixel.red=(double) GetPixelRed(image,p)+color_vector.red*(1.0-(4.0*
5356 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
5357 pixel.green=(double) GetPixelGreen(image,p)+color_vector.green*(1.0-(4.0*
5359 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
5360 pixel.blue=(double) GetPixelBlue(image,p)+color_vector.blue*(1.0-(4.0*
5362 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
5363 pixel.black=(double) GetPixelBlack(image,p)+color_vector.black*(1.0-(4.0*
5365 SetPixelViaPixelInfo(tint_image,&pixel,q);
5366 p+=GetPixelChannels(image);
5367 q+=GetPixelChannels(tint_image);
5369 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5371 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5376 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5377 #pragma omp critical (MagickCore_TintImage)
5379 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5380 if (proceed == MagickFalse)
5384 tint_view=DestroyCacheView(tint_view);
5385 image_view=DestroyCacheView(image_view);
5386 if (status == MagickFalse)
5387 tint_image=DestroyImage(tint_image);
5392 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5396 % V i g n e t t e I m a g e %
5400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5402 % VignetteImage() softens the edges of the image in vignette style.
5404 % The format of the VignetteImage method is:
5406 % Image *VignetteImage(const Image *image,const double radius,
5407 % const double sigma,const ssize_t x,const ssize_t y,
5408 % ExceptionInfo *exception)
5410 % A description of each parameter follows:
5412 % o image: the image.
5414 % o radius: the radius of the pixel neighborhood.
5416 % o sigma: the standard deviation of the Gaussian, in pixels.
5418 % o x, y: Define the x and y ellipse offset.
5420 % o exception: return any errors or warnings in this structure.
5423 MagickExport Image *VignetteImage(const Image *image,const double radius,
5424 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
5427 ellipse[MaxTextExtent];
5438 assert(image != (Image *) NULL);
5439 assert(image->signature == MagickSignature);
5440 if (image->debug != MagickFalse)
5441 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5442 assert(exception != (ExceptionInfo *) NULL);
5443 assert(exception->signature == MagickSignature);
5444 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5445 if (canvas_image == (Image *) NULL)
5446 return((Image *) NULL);
5447 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
5449 canvas_image=DestroyImage(canvas_image);
5450 return((Image *) NULL);
5452 canvas_image->alpha_trait=BlendPixelTrait;
5453 oval_image=CloneImage(canvas_image,canvas_image->columns,canvas_image->rows,
5454 MagickTrue,exception);
5455 if (oval_image == (Image *) NULL)
5457 canvas_image=DestroyImage(canvas_image);
5458 return((Image *) NULL);
5460 (void) QueryColorCompliance("#000000",AllCompliance,
5461 &oval_image->background_color,exception);
5462 (void) SetImageBackgroundColor(oval_image,exception);
5463 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
5464 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5466 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5468 (void) FormatLocaleString(ellipse,MaxTextExtent,"ellipse %g,%g,%g,%g,"
5469 "0.0,360.0",image->columns/2.0,image->rows/2.0,image->columns/2.0-x,
5471 draw_info->primitive=AcquireString(ellipse);
5472 (void) DrawImage(oval_image,draw_info,exception);
5473 draw_info=DestroyDrawInfo(draw_info);
5474 blur_image=BlurImage(oval_image,radius,sigma,exception);
5475 oval_image=DestroyImage(oval_image);
5476 if (blur_image == (Image *) NULL)
5478 canvas_image=DestroyImage(canvas_image);
5479 return((Image *) NULL);
5481 blur_image->alpha_trait=UndefinedPixelTrait;
5482 (void) CompositeImage(canvas_image,blur_image,IntensityCompositeOp,MagickTrue,
5484 blur_image=DestroyImage(blur_image);
5485 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5486 canvas_image=DestroyImage(canvas_image);
5487 if (vignette_image != (Image *) NULL)
5488 (void) TransformImageColorspace(vignette_image,image->colorspace,exception);
5489 return(vignette_image);
5493 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5497 % W a v e I m a g e %
5501 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5503 % WaveImage() creates a "ripple" effect in the image by shifting the pixels
5504 % vertically along a sine wave whose amplitude and wavelength is specified
5505 % by the given parameters.
5507 % The format of the WaveImage method is:
5509 % Image *WaveImage(const Image *image,const double amplitude,
5510 % const double wave_length,const PixelInterpolateMethod method,
5511 % ExceptionInfo *exception)
5513 % A description of each parameter follows:
5515 % o image: the image.
5517 % o amplitude, wave_length: Define the amplitude and wave length of the
5520 % o interpolate: the pixel interpolation method.
5522 % o exception: return any errors or warnings in this structure.
5525 MagickExport Image *WaveImage(const Image *image,const double amplitude,
5526 const double wave_length,const PixelInterpolateMethod method,
5527 ExceptionInfo *exception)
5529 #define WaveImageTag "Wave/Image"
5554 Initialize wave image attributes.
5556 assert(image != (Image *) NULL);
5557 assert(image->signature == MagickSignature);
5558 if (image->debug != MagickFalse)
5559 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5560 assert(exception != (ExceptionInfo *) NULL);
5561 assert(exception->signature == MagickSignature);
5562 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
5563 fabs(amplitude)),MagickTrue,exception);
5564 if (wave_image == (Image *) NULL)
5565 return((Image *) NULL);
5566 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
5568 wave_image=DestroyImage(wave_image);
5569 return((Image *) NULL);
5571 if (wave_image->background_color.alpha != OpaqueAlpha)
5572 wave_image->alpha_trait=BlendPixelTrait;
5576 sine_map=(double *) AcquireQuantumMemory((size_t) wave_image->columns,
5578 if (sine_map == (double *) NULL)
5580 wave_image=DestroyImage(wave_image);
5581 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5583 for (i=0; i < (ssize_t) wave_image->columns; i++)
5584 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5591 image_view=AcquireVirtualCacheView(image,exception);
5592 wave_view=AcquireAuthenticCacheView(wave_image,exception);
5593 (void) SetCacheViewVirtualPixelMethod(image_view,
5594 BackgroundVirtualPixelMethod);
5595 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5596 #pragma omp parallel for schedule(static,4) shared(progress,status) \
5597 magick_threads(image,wave_image,wave_image->rows,1)
5599 for (y=0; y < (ssize_t) wave_image->rows; y++)
5607 if (status == MagickFalse)
5609 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5611 if (q == (Quantum *) NULL)
5616 for (x=0; x < (ssize_t) wave_image->columns; x++)
5618 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5619 (double) x,(double) (y-sine_map[x]),q,exception);
5620 q+=GetPixelChannels(wave_image);
5622 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5624 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5629 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5630 #pragma omp critical (MagickCore_WaveImage)
5632 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5633 if (proceed == MagickFalse)
5637 wave_view=DestroyCacheView(wave_view);
5638 image_view=DestroyCacheView(image_view);
5639 sine_map=(double *) RelinquishMagickMemory(sine_map);
5640 if (status == MagickFalse)
5641 wave_image=DestroyImage(wave_image);