2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % MagickCore Image Special Effects Methods %
20 % Copyright 1999-2014 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)
684 Allocate colorized image.
686 assert(image != (const Image *) NULL);
687 assert(image->signature == MagickSignature);
688 if (image->debug != MagickFalse)
689 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
690 assert(exception != (ExceptionInfo *) NULL);
691 assert(exception->signature == MagickSignature);
692 colorize_image=CloneImage(image,image->columns,image->rows,MagickTrue,
694 if (colorize_image == (Image *) NULL)
695 return((Image *) NULL);
696 if (SetImageStorageClass(colorize_image,DirectClass,exception) == MagickFalse)
698 colorize_image=DestroyImage(colorize_image);
699 return((Image *) NULL);
701 if ((IsGrayColorspace(image->colorspace) != MagickFalse) ||
702 (IsPixelInfoGray(colorize) != MagickFalse))
703 (void) SetImageColorspace(colorize_image,sRGBColorspace,exception);
704 if ((colorize_image->alpha_trait != BlendPixelTrait) &&
705 (colorize->alpha_trait == BlendPixelTrait))
706 (void) SetImageAlpha(colorize_image,OpaqueAlpha,exception);
707 if (blend == (const char *) NULL)
708 return(colorize_image);
709 GetPixelInfo(image,&blend_percentage);
710 flags=ParseGeometry(blend,&geometry_info);
711 blend_percentage.red=geometry_info.rho;
712 blend_percentage.green=geometry_info.rho;
713 blend_percentage.blue=geometry_info.rho;
714 blend_percentage.black=geometry_info.rho;
715 blend_percentage.alpha=geometry_info.rho;
716 if ((flags & SigmaValue) != 0)
717 blend_percentage.green=geometry_info.sigma;
718 if ((flags & XiValue) != 0)
719 blend_percentage.blue=geometry_info.xi;
720 if ((flags & PsiValue) != 0)
721 blend_percentage.alpha=geometry_info.psi;
722 if (blend_percentage.colorspace == CMYKColorspace)
724 if ((flags & PsiValue) != 0)
725 blend_percentage.black=geometry_info.psi;
726 if ((flags & ChiValue) != 0)
727 blend_percentage.alpha=geometry_info.chi;
730 Colorize DirectClass image.
734 image_view=AcquireVirtualCacheView(image,exception);
735 colorize_view=AcquireAuthenticCacheView(colorize_image,exception);
736 #if defined(MAGICKCORE_OPENMP_SUPPORT)
737 #pragma omp parallel for schedule(static,4) shared(progress,status) \
738 magick_threads(image,colorize_image,image->rows,1)
740 for (y=0; y < (ssize_t) image->rows; y++)
745 register const Quantum
754 if (status == MagickFalse)
756 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
757 q=QueueCacheViewAuthenticPixels(colorize_view,0,y,colorize_image->columns,1,
759 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
764 for (x=0; x < (ssize_t) image->columns; x++)
769 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
771 PixelChannel channel=GetPixelChannelChannel(image,i);
772 PixelTrait traits=GetPixelChannelTraits(image,channel);
773 PixelTrait colorize_traits=GetPixelChannelTraits(colorize_image,
775 if ((traits == UndefinedPixelTrait) ||
776 (colorize_traits == UndefinedPixelTrait))
778 if (((colorize_traits & CopyPixelTrait) != 0) ||
779 (GetPixelReadMask(image,p) == 0))
781 SetPixelChannel(colorize_image,channel,p[i],q);
784 SetPixelChannel(colorize_image,channel,ClampToQuantum(Colorize(p[i],
785 GetPixelInfoChannel(&blend_percentage,channel),GetPixelInfoChannel(
786 colorize,channel))),q);
788 p+=GetPixelChannels(image);
789 q+=GetPixelChannels(colorize_image);
791 sync=SyncCacheViewAuthenticPixels(colorize_view,exception);
792 if (sync == MagickFalse)
794 if (image->progress_monitor != (MagickProgressMonitor) NULL)
799 #if defined(MAGICKCORE_OPENMP_SUPPORT)
800 #pragma omp critical (MagickCore_ColorizeImage)
802 proceed=SetImageProgress(image,ColorizeImageTag,progress++,image->rows);
803 if (proceed == MagickFalse)
807 image_view=DestroyCacheView(image_view);
808 colorize_view=DestroyCacheView(colorize_view);
809 if (status == MagickFalse)
810 colorize_image=DestroyImage(colorize_image);
811 return(colorize_image);
815 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
819 % C o l o r M a t r i x I m a g e %
823 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
825 % ColorMatrixImage() applies color transformation to an image. This method
826 % permits saturation changes, hue rotation, luminance to alpha, and various
827 % other effects. Although variable-sized transformation matrices can be used,
828 % typically one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA
829 % (or RGBA with offsets). The matrix is similar to those used by Adobe Flash
830 % except offsets are in column 6 rather than 5 (in support of CMYKA images)
831 % and offsets are normalized (divide Flash offset by 255).
833 % The format of the ColorMatrixImage method is:
835 % Image *ColorMatrixImage(const Image *image,
836 % const KernelInfo *color_matrix,ExceptionInfo *exception)
838 % A description of each parameter follows:
840 % o image: the image.
842 % o color_matrix: the color matrix.
844 % o exception: return any errors or warnings in this structure.
847 /* FUTURE: modify to make use of a MagickMatrix Mutliply function
848 That should be provided in "matrix.c"
849 (ASIDE: actually distorts should do this too but currently doesn't)
852 MagickExport Image *ColorMatrixImage(const Image *image,
853 const KernelInfo *color_matrix,ExceptionInfo *exception)
855 #define ColorMatrixImageTag "ColorMatrix/Image"
864 { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
865 { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
866 { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
867 { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
868 { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
869 { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }
890 Map given color_matrix, into a 6x6 matrix RGBKA and a constant
892 assert(image != (Image *) NULL);
893 assert(image->signature == MagickSignature);
894 if (image->debug != MagickFalse)
895 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
896 assert(exception != (ExceptionInfo *) NULL);
897 assert(exception->signature == MagickSignature);
899 for (v=0; v < (ssize_t) color_matrix->height; v++)
900 for (u=0; u < (ssize_t) color_matrix->width; u++)
902 if ((v < 6) && (u < 6))
903 ColorMatrix[v][u]=color_matrix->values[i];
907 Initialize color image.
909 color_image=CloneImage(image,0,0,MagickTrue,exception);
910 if (color_image == (Image *) NULL)
911 return((Image *) NULL);
912 if (SetImageStorageClass(color_image,DirectClass,exception) == MagickFalse)
914 color_image=DestroyImage(color_image);
915 return((Image *) NULL);
917 if (image->debug != MagickFalse)
920 format[MaxTextExtent],
923 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
924 " ColorMatrix image with color matrix:");
925 message=AcquireString("");
926 for (v=0; v < 6; v++)
929 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
930 (void) ConcatenateString(&message,format);
931 for (u=0; u < 6; u++)
933 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",
935 (void) ConcatenateString(&message,format);
937 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
939 message=DestroyString(message);
942 Apply the ColorMatrix to image.
946 image_view=AcquireVirtualCacheView(image,exception);
947 color_view=AcquireAuthenticCacheView(color_image,exception);
948 #if defined(MAGICKCORE_OPENMP_SUPPORT)
949 #pragma omp parallel for schedule(static,4) shared(progress,status) \
950 magick_threads(image,color_image,image->rows,1)
952 for (y=0; y < (ssize_t) image->rows; y++)
957 register const Quantum
966 if (status == MagickFalse)
968 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
969 q=GetCacheViewAuthenticPixels(color_view,0,y,color_image->columns,1,
971 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
976 GetPixelInfo(image,&pixel);
977 for (x=0; x < (ssize_t) image->columns; x++)
985 GetPixelInfoPixel(image,p,&pixel);
986 height=color_matrix->height > 6 ? 6UL : color_matrix->height;
987 for (v=0; v < (ssize_t) height; v++)
992 sum=ColorMatrix[v][0]*GetPixelRed(image,p)+ColorMatrix[v][1]*
993 GetPixelGreen(image,p)+ColorMatrix[v][2]*GetPixelBlue(image,p);
994 if (image->colorspace == CMYKColorspace)
995 sum+=ColorMatrix[v][3]*GetPixelBlack(image,p);
996 if (image->alpha_trait == BlendPixelTrait)
997 sum+=ColorMatrix[v][4]*GetPixelAlpha(image,p);
998 sum+=QuantumRange*ColorMatrix[v][5];
1001 case 0: pixel.red=sum; break;
1002 case 1: pixel.green=sum; break;
1003 case 2: pixel.blue=sum; break;
1004 case 3: pixel.black=sum; break;
1005 case 4: pixel.alpha=sum; break;
1009 SetPixelInfoPixel(color_image,&pixel,q);
1010 p+=GetPixelChannels(image);
1011 q+=GetPixelChannels(color_image);
1013 if (SyncCacheViewAuthenticPixels(color_view,exception) == MagickFalse)
1015 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1020 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1021 #pragma omp critical (MagickCore_ColorMatrixImage)
1023 proceed=SetImageProgress(image,ColorMatrixImageTag,progress++,
1025 if (proceed == MagickFalse)
1029 color_view=DestroyCacheView(color_view);
1030 image_view=DestroyCacheView(image_view);
1031 if (status == MagickFalse)
1032 color_image=DestroyImage(color_image);
1033 return(color_image);
1037 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1041 + D e s t r o y F x I n f o %
1045 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1047 % DestroyFxInfo() deallocates memory associated with an FxInfo structure.
1049 % The format of the DestroyFxInfo method is:
1051 % ImageInfo *DestroyFxInfo(ImageInfo *fx_info)
1053 % A description of each parameter follows:
1055 % o fx_info: the fx info.
1058 MagickPrivate FxInfo *DestroyFxInfo(FxInfo *fx_info)
1063 fx_info->exception=DestroyExceptionInfo(fx_info->exception);
1064 fx_info->expression=DestroyString(fx_info->expression);
1065 fx_info->symbols=DestroySplayTree(fx_info->symbols);
1066 fx_info->colors=DestroySplayTree(fx_info->colors);
1067 for (i=(ssize_t) GetImageListLength(fx_info->images)-1; i >= 0; i--)
1068 fx_info->view[i]=DestroyCacheView(fx_info->view[i]);
1069 fx_info->view=(CacheView **) RelinquishMagickMemory(fx_info->view);
1070 fx_info->random_info=DestroyRandomInfo(fx_info->random_info);
1071 fx_info=(FxInfo *) RelinquishMagickMemory(fx_info);
1076 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1080 + 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 %
1084 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1086 % FxEvaluateChannelExpression() evaluates an expression and returns the
1089 % The format of the FxEvaluateExpression method is:
1091 % double FxEvaluateChannelExpression(FxInfo *fx_info,
1092 % const PixelChannel channel,const ssize_t x,const ssize_t y,
1093 % double *alpha,Exceptioninfo *exception)
1094 % double FxEvaluateExpression(FxInfo *fx_info,
1095 % double *alpha,Exceptioninfo *exception)
1097 % A description of each parameter follows:
1099 % o fx_info: the fx info.
1101 % o channel: the channel.
1103 % o x,y: the pixel position.
1105 % o alpha: the result.
1107 % o exception: return any errors or warnings in this structure.
1111 static inline double MagickMax(const double x,const double y)
1118 static inline double MagickMin(const double x,const double y)
1125 static double FxChannelStatistics(FxInfo *fx_info,Image *image,
1126 PixelChannel channel,const char *symbol,ExceptionInfo *exception)
1133 statistic[MaxTextExtent];
1141 channel_mask=UndefinedChannel;
1142 for (p=symbol; (*p != '.') && (*p != '\0'); p++) ;
1148 option=ParseCommandOption(MagickPixelChannelOptions,MagickTrue,p+1);
1151 channel=(PixelChannel) option;
1152 channel_mask=(ChannelType) (channel_mask | (1 << channel));
1153 SetPixelChannelMask(image,channel_mask);
1156 (void) FormatLocaleString(key,MaxTextExtent,"%p.%.20g.%s",(void *) image,
1157 (double) channel,symbol);
1158 value=(const char *) GetValueFromSplayTree(fx_info->symbols,key);
1159 if (value != (const char *) NULL)
1161 if (channel_mask != UndefinedChannel)
1162 SetPixelChannelMask(image,channel_mask);
1163 return(QuantumScale*StringToDouble(value,(char **) NULL));
1165 (void) DeleteNodeFromSplayTree(fx_info->symbols,key);
1166 if (LocaleNCompare(symbol,"depth",5) == 0)
1171 depth=GetImageDepth(image,exception);
1172 (void) FormatLocaleString(statistic,MaxTextExtent,"%.20g",(double) depth);
1174 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1180 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
1181 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",kurtosis);
1183 if (LocaleNCompare(symbol,"maxima",6) == 0)
1189 (void) GetImageRange(image,&minima,&maxima,exception);
1190 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",maxima);
1192 if (LocaleNCompare(symbol,"mean",4) == 0)
1198 (void) GetImageMean(image,&mean,&standard_deviation,exception);
1199 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",mean);
1201 if (LocaleNCompare(symbol,"minima",6) == 0)
1207 (void) GetImageRange(image,&minima,&maxima,exception);
1208 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",minima);
1210 if (LocaleNCompare(symbol,"skewness",8) == 0)
1216 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
1217 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",skewness);
1219 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1225 (void) GetImageMean(image,&mean,&standard_deviation,exception);
1226 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",
1227 standard_deviation);
1229 if (channel_mask != UndefinedChannel)
1230 SetPixelChannelMask(image,channel_mask);
1231 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(key),
1232 ConstantString(statistic));
1233 return(QuantumScale*StringToDouble(statistic,(char **) NULL));
1237 FxEvaluateSubexpression(FxInfo *,const PixelChannel,const ssize_t,
1238 const ssize_t,const char *,double *,ExceptionInfo *);
1240 static MagickOffsetType FxGCD(MagickOffsetType alpha,MagickOffsetType beta)
1243 return(FxGCD(beta,alpha % beta));
1247 static inline const char *FxSubexpression(const char *expression,
1248 ExceptionInfo *exception)
1257 subexpression=expression;
1258 while ((*subexpression != '\0') &&
1259 ((level != 1) || (strchr(")",(int) *subexpression) == (char *) NULL)))
1261 if (strchr("(",(int) *subexpression) != (char *) NULL)
1264 if (strchr(")",(int) *subexpression) != (char *) NULL)
1268 if (*subexpression == '\0')
1269 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1270 "UnbalancedParenthesis","`%s'",expression);
1271 return(subexpression);
1274 static double FxGetSymbol(FxInfo *fx_info,const PixelChannel channel,
1275 const ssize_t x,const ssize_t y,const char *expression,
1276 ExceptionInfo *exception)
1280 subexpression[MaxTextExtent],
1281 symbol[MaxTextExtent];
1308 i=GetImageIndexInList(fx_info->images);
1312 if (isalpha((int) ((unsigned char) *(p+1))) == 0)
1314 if (strchr("suv",(int) *p) != (char *) NULL)
1321 i=GetImageIndexInList(fx_info->images);
1324 case 'u': i=0; break;
1325 case 'v': i=1; break;
1332 for (p++; *p != '\0'; )
1346 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1348 i=(ssize_t) (alpha+0.5);
1354 if ((*p == 'p') && (isalpha((int) ((unsigned char) *(p+1))) == 0))
1361 for (p++; *p != '\0'; )
1375 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1386 for (p++; *p != '\0'; )
1400 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1410 length=GetImageListLength(fx_info->images);
1412 i+=(ssize_t) length;
1414 image=GetImageFromList(fx_info->images,i);
1415 if (image == (Image *) NULL)
1417 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1418 "NoSuchImage","`%s'",expression);
1421 GetPixelInfo(image,&pixel);
1422 (void) InterpolatePixelInfo(image,fx_info->view[i],image->interpolate,
1423 point.x,point.y,&pixel,exception);
1424 if ((strlen(p) > 2) && (LocaleCompare(p,"intensity") != 0) &&
1425 (LocaleCompare(p,"luma") != 0) && (LocaleCompare(p,"luminance") != 0) &&
1426 (LocaleCompare(p,"hue") != 0) && (LocaleCompare(p,"saturation") != 0) &&
1427 (LocaleCompare(p,"lightness") != 0))
1430 name[MaxTextExtent];
1432 (void) CopyMagickString(name,p,MaxTextExtent);
1433 for (q=name+(strlen(name)-1); q > name; q--)
1443 if ((strlen(name) > 2) &&
1444 (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
1449 color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
1450 if (color != (PixelInfo *) NULL)
1460 status=QueryColorCompliance(name,AllCompliance,&pixel,
1461 fx_info->exception);
1462 if (status != MagickFalse)
1464 (void) AddValueToSplayTree(fx_info->colors,ConstantString(
1465 name),ClonePixelInfo(&pixel));
1471 (void) CopyMagickString(symbol,p,MaxTextExtent);
1472 StripString(symbol);
1473 if (*symbol == '\0')
1477 case RedPixelChannel: return(QuantumScale*pixel.red);
1478 case GreenPixelChannel: return(QuantumScale*pixel.green);
1479 case BluePixelChannel: return(QuantumScale*pixel.blue);
1480 case BlackPixelChannel:
1482 if (image->colorspace != CMYKColorspace)
1484 (void) ThrowMagickException(exception,GetMagickModule(),
1485 ImageError,"ColorSeparatedImageRequired","`%s'",
1489 return(QuantumScale*pixel.black);
1491 case AlphaPixelChannel:
1496 if (pixel.alpha_trait != BlendPixelTrait)
1498 alpha=(double) (QuantumScale*pixel.alpha);
1501 case IndexPixelChannel:
1503 case IntensityPixelChannel:
1505 return(QuantumScale*GetPixelInfoIntensity(&pixel));
1510 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1511 "UnableToParseExpression","`%s'",p);
1519 if (LocaleCompare(symbol,"a") == 0)
1520 return((double) (QuantumScale*pixel.alpha));
1526 if (LocaleCompare(symbol,"b") == 0)
1527 return(QuantumScale*pixel.blue);
1533 if (LocaleNCompare(symbol,"channel",7) == 0)
1541 flags=ParseGeometry(symbol+7,&channel_info);
1542 if (image->colorspace == CMYKColorspace)
1545 case CyanPixelChannel:
1547 if ((flags & RhoValue) == 0)
1549 return(channel_info.rho);
1551 case MagentaPixelChannel:
1553 if ((flags & SigmaValue) == 0)
1555 return(channel_info.sigma);
1557 case YellowPixelChannel:
1559 if ((flags & XiValue) == 0)
1561 return(channel_info.xi);
1563 case BlackPixelChannel:
1565 if ((flags & PsiValue) == 0)
1567 return(channel_info.psi);
1569 case AlphaPixelChannel:
1571 if ((flags & ChiValue) == 0)
1573 return(channel_info.chi);
1580 case RedPixelChannel:
1582 if ((flags & RhoValue) == 0)
1584 return(channel_info.rho);
1586 case GreenPixelChannel:
1588 if ((flags & SigmaValue) == 0)
1590 return(channel_info.sigma);
1592 case BluePixelChannel:
1594 if ((flags & XiValue) == 0)
1596 return(channel_info.xi);
1598 case BlackPixelChannel:
1600 if ((flags & ChiValue) == 0)
1602 return(channel_info.chi);
1604 case AlphaPixelChannel:
1606 if ((flags & PsiValue) == 0)
1608 return(channel_info.psi);
1614 if (LocaleCompare(symbol,"c") == 0)
1615 return(QuantumScale*pixel.red);
1621 if (LocaleNCompare(symbol,"depth",5) == 0)
1622 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1628 if (LocaleCompare(symbol,"g") == 0)
1629 return(QuantumScale*pixel.green);
1635 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1636 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1637 if (LocaleCompare(symbol,"k") == 0)
1639 if (image->colorspace != CMYKColorspace)
1641 (void) ThrowMagickException(exception,GetMagickModule(),
1642 OptionError,"ColorSeparatedImageRequired","`%s'",
1646 return(QuantumScale*pixel.black);
1653 if (LocaleCompare(symbol,"h") == 0)
1654 return((double) image->rows);
1655 if (LocaleCompare(symbol,"hue") == 0)
1662 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1671 if ((LocaleCompare(symbol,"image.depth") == 0) ||
1672 (LocaleCompare(symbol,"image.minima") == 0) ||
1673 (LocaleCompare(symbol,"image.maxima") == 0) ||
1674 (LocaleCompare(symbol,"image.mean") == 0) ||
1675 (LocaleCompare(symbol,"image.kurtosis") == 0) ||
1676 (LocaleCompare(symbol,"image.skewness") == 0) ||
1677 (LocaleCompare(symbol,"image.standard_deviation") == 0))
1678 return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
1679 if (LocaleCompare(symbol,"image.resolution.x") == 0)
1680 return(image->resolution.x);
1681 if (LocaleCompare(symbol,"image.resolution.y") == 0)
1682 return(image->resolution.y);
1683 if (LocaleCompare(symbol,"intensity") == 0)
1684 return(QuantumScale*GetPixelInfoIntensity(&pixel));
1685 if (LocaleCompare(symbol,"i") == 0)
1692 if (LocaleCompare(symbol,"j") == 0)
1699 if (LocaleCompare(symbol,"lightness") == 0)
1706 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1710 if (LocaleCompare(symbol,"luma") == 0)
1715 luma=0.212656*pixel.red+0.715158*pixel.green+0.072186*pixel.blue;
1716 return(QuantumScale*luma);
1718 if (LocaleCompare(symbol,"luminance") == 0)
1723 luminence=0.212656*pixel.red+0.715158*pixel.green+0.072186*pixel.blue;
1724 return(QuantumScale*luminence);
1731 if (LocaleNCompare(symbol,"maxima",6) == 0)
1732 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1733 if (LocaleNCompare(symbol,"mean",4) == 0)
1734 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1735 if (LocaleNCompare(symbol,"minima",6) == 0)
1736 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1737 if (LocaleCompare(symbol,"m") == 0)
1738 return(QuantumScale*pixel.blue);
1744 if (LocaleCompare(symbol,"n") == 0)
1745 return((double) GetImageListLength(fx_info->images));
1751 if (LocaleCompare(symbol,"o") == 0)
1752 return(QuantumScale*pixel.alpha);
1758 if (LocaleCompare(symbol,"page.height") == 0)
1759 return((double) image->page.height);
1760 if (LocaleCompare(symbol,"page.width") == 0)
1761 return((double) image->page.width);
1762 if (LocaleCompare(symbol,"page.x") == 0)
1763 return((double) image->page.x);
1764 if (LocaleCompare(symbol,"page.y") == 0)
1765 return((double) image->page.y);
1771 if (LocaleCompare(symbol,"resolution.x") == 0)
1772 return(image->resolution.x);
1773 if (LocaleCompare(symbol,"resolution.y") == 0)
1774 return(image->resolution.y);
1775 if (LocaleCompare(symbol,"r") == 0)
1776 return(QuantumScale*pixel.red);
1782 if (LocaleCompare(symbol,"saturation") == 0)
1789 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1793 if (LocaleNCompare(symbol,"skewness",8) == 0)
1794 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1795 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1796 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1802 if (LocaleCompare(symbol,"t") == 0)
1803 return((double) GetImageIndexInList(fx_info->images));
1809 if (LocaleCompare(symbol,"w") == 0)
1810 return((double) image->columns);
1816 if (LocaleCompare(symbol,"y") == 0)
1817 return(QuantumScale*pixel.green);
1823 if (LocaleCompare(symbol,"z") == 0)
1828 depth=(double) GetImageDepth(image,fx_info->exception);
1836 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1837 if (value != (const char *) NULL)
1838 return((double) StringToDouble(value,(char **) NULL));
1839 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1840 "UnableToParseExpression","`%s'",symbol);
1844 static const char *FxOperatorPrecedence(const char *expression,
1845 ExceptionInfo *exception)
1849 UndefinedPrecedence,
1851 BitwiseComplementPrecedence,
1853 ExponentialNotationPrecedence,
1857 RelationalPrecedence,
1858 EquivalencyPrecedence,
1859 BitwiseAndPrecedence,
1860 BitwiseOrPrecedence,
1861 LogicalAndPrecedence,
1862 LogicalOrPrecedence,
1864 AssignmentPrecedence,
1884 subexpression=(const char *) NULL;
1885 target=NullPrecedence;
1886 while (*expression != '\0')
1888 precedence=UndefinedPrecedence;
1889 if ((isspace((int) ((unsigned char) *expression)) != 0) || (c == (int) '@'))
1894 switch (*expression)
1899 #if defined(MAGICKCORE_HAVE_ACOSH)
1900 if (LocaleNCompare(expression,"acosh",5) == 0)
1906 #if defined(MAGICKCORE_HAVE_ASINH)
1907 if (LocaleNCompare(expression,"asinh",5) == 0)
1913 #if defined(MAGICKCORE_HAVE_ATANH)
1914 if (LocaleNCompare(expression,"atanh",5) == 0)
1920 if (LocaleNCompare(expression,"atan2",5) == 0)
1930 if ((LocaleNCompare(expression,"E+",2) == 0) ||
1931 (LocaleNCompare(expression,"E-",2) == 0))
1933 expression+=2; /* scientific notation */
1940 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1941 (LocaleNCompare(expression,"j1",2) == 0))
1950 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1957 if ((c == (int) '{') || (c == (int) '['))
1960 if ((c == (int) '}') || (c == (int) ']'))
1963 switch ((unsigned char) *expression)
1968 precedence=BitwiseComplementPrecedence;
1974 precedence=ExponentPrecedence;
1979 if (((c != 0) && ((isdigit((int) ((unsigned char) c)) != 0) ||
1980 (strchr(")",(int) ((unsigned char) c)) != (char *) NULL))) &&
1981 (((islower((int) ((unsigned char) *expression)) != 0) ||
1982 (strchr("(",(int) ((unsigned char) *expression)) != (char *) NULL)) ||
1983 ((isdigit((int) ((unsigned char) c)) == 0) &&
1984 (isdigit((int) ((unsigned char) *expression)) != 0))) &&
1985 (strchr("xy",(int) ((unsigned char) *expression)) == (char *) NULL))
1986 precedence=MultiplyPrecedence;
1993 precedence=MultiplyPrecedence;
1999 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
2001 precedence=AdditionPrecedence;
2004 case LeftShiftOperator:
2005 case RightShiftOperator:
2007 precedence=ShiftPrecedence;
2011 case LessThanEqualOperator:
2012 case GreaterThanEqualOperator:
2015 precedence=RelationalPrecedence;
2019 case NotEqualOperator:
2021 precedence=EquivalencyPrecedence;
2026 precedence=BitwiseAndPrecedence;
2031 precedence=BitwiseOrPrecedence;
2034 case LogicalAndOperator:
2036 precedence=LogicalAndPrecedence;
2039 case LogicalOrOperator:
2041 precedence=LogicalOrPrecedence;
2044 case ExponentialNotation:
2046 precedence=ExponentialNotationPrecedence;
2052 precedence=TernaryPrecedence;
2057 precedence=AssignmentPrecedence;
2062 precedence=CommaPrecedence;
2067 precedence=SeparatorPrecedence;
2071 if ((precedence == BitwiseComplementPrecedence) ||
2072 (precedence == TernaryPrecedence) ||
2073 (precedence == AssignmentPrecedence))
2075 if (precedence > target)
2078 Right-to-left associativity.
2081 subexpression=expression;
2085 if (precedence >= target)
2088 Left-to-right associativity.
2091 subexpression=expression;
2093 if (strchr("(",(int) *expression) != (char *) NULL)
2094 expression=FxSubexpression(expression,exception);
2095 c=(int) (*expression++);
2097 return(subexpression);
2100 static double FxEvaluateSubexpression(FxInfo *fx_info,
2101 const PixelChannel channel,const ssize_t x,const ssize_t y,
2102 const char *expression,double *beta,ExceptionInfo *exception)
2106 subexpression[MaxTextExtent];
2116 if (exception->severity != UndefinedException)
2118 while (isspace((int) ((unsigned char) *expression)) != 0)
2120 if (*expression == '\0')
2122 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
2123 "MissingExpression","`%s'",expression);
2126 *subexpression='\0';
2127 p=FxOperatorPrecedence(expression,exception);
2128 if (p != (const char *) NULL)
2130 (void) CopyMagickString(subexpression,expression,(size_t)
2132 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2134 switch ((unsigned char) *p)
2138 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2139 *beta=(double) (~(size_t) *beta);
2144 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2145 return(*beta == 0.0 ? 1.0 : 0.0);
2149 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2150 channel,x,y,++p,beta,exception));
2154 case ExponentialNotation:
2156 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2157 return(alpha*(*beta));
2161 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2164 if (exception->severity == UndefinedException)
2165 (void) ThrowMagickException(exception,GetMagickModule(),
2166 OptionError,"DivideByZero","`%s'",expression);
2169 return(alpha/(*beta));
2173 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2174 *beta=fabs(floor(((double) *beta)+0.5));
2177 (void) ThrowMagickException(exception,GetMagickModule(),
2178 OptionError,"DivideByZero","`%s'",expression);
2181 return(fmod((double) alpha,(double) *beta));
2185 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2186 return(alpha+(*beta));
2190 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2191 return(alpha-(*beta));
2193 case LeftShiftOperator:
2195 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2196 *beta=(double) ((size_t) (alpha+0.5) << (size_t) (gamma+0.5));
2199 case RightShiftOperator:
2201 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2202 *beta=(double) ((size_t) (alpha+0.5) >> (size_t) (gamma+0.5));
2207 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2208 return(alpha < *beta ? 1.0 : 0.0);
2210 case LessThanEqualOperator:
2212 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2213 return(alpha <= *beta ? 1.0 : 0.0);
2217 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2218 return(alpha > *beta ? 1.0 : 0.0);
2220 case GreaterThanEqualOperator:
2222 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2223 return(alpha >= *beta ? 1.0 : 0.0);
2227 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2228 return(fabs(alpha-(*beta)) < MagickEpsilon ? MagickEpsilon : 0.0);
2230 case NotEqualOperator:
2232 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2233 return(fabs(alpha-(*beta)) >= MagickEpsilon ? 1.0 : 0.0);
2237 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2238 *beta=(double) ((size_t) (alpha+0.5) & (size_t) (gamma+0.5));
2243 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2244 *beta=(double) ((size_t) (alpha+0.5) | (size_t) (gamma+0.5));
2247 case LogicalAndOperator:
2249 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2250 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2253 case LogicalOrOperator:
2255 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2256 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2264 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2266 p=StringToken(":",&q);
2267 if (q == (char *) NULL)
2269 (void) ThrowMagickException(exception,GetMagickModule(),
2270 OptionError,"UnableToParseExpression","`%s'",subexpression);
2273 if (fabs((double) alpha) >= MagickEpsilon)
2274 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2276 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2282 numeric[MaxTextExtent];
2285 while (isalpha((int) ((unsigned char) *q)) != 0)
2289 (void) ThrowMagickException(exception,GetMagickModule(),
2290 OptionError,"UnableToParseExpression","`%s'",subexpression);
2293 ClearMagickException(exception);
2294 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2295 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
2297 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2298 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2299 subexpression),ConstantString(numeric));
2304 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2309 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2314 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2320 if (strchr("(",(int) *expression) != (char *) NULL)
2322 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2323 subexpression[strlen(subexpression)-1]='\0';
2324 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2328 switch (*expression)
2332 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2338 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2344 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2346 return((double) (~(size_t) (gamma+0.5)));
2351 if (LocaleNCompare(expression,"abs",3) == 0)
2353 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2355 return((double) fabs((double) alpha));
2357 #if defined(MAGICKCORE_HAVE_ACOSH)
2358 if (LocaleNCompare(expression,"acosh",5) == 0)
2360 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2362 return((double) acosh((double) alpha));
2365 if (LocaleNCompare(expression,"acos",4) == 0)
2367 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2369 return((double) acos((double) alpha));
2371 #if defined(MAGICKCORE_HAVE_J1)
2372 if (LocaleNCompare(expression,"airy",4) == 0)
2374 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2378 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
2379 return(gamma*gamma);
2382 #if defined(MAGICKCORE_HAVE_ASINH)
2383 if (LocaleNCompare(expression,"asinh",5) == 0)
2385 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2387 return((double) asinh((double) alpha));
2390 if (LocaleNCompare(expression,"asin",4) == 0)
2392 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2394 return((double) asin((double) alpha));
2396 if (LocaleNCompare(expression,"alt",3) == 0)
2398 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2400 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
2402 if (LocaleNCompare(expression,"atan2",5) == 0)
2404 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2406 return((double) atan2((double) alpha,(double) *beta));
2408 #if defined(MAGICKCORE_HAVE_ATANH)
2409 if (LocaleNCompare(expression,"atanh",5) == 0)
2411 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2413 return((double) atanh((double) alpha));
2416 if (LocaleNCompare(expression,"atan",4) == 0)
2418 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2420 return((double) atan((double) alpha));
2422 if (LocaleCompare(expression,"a") == 0)
2423 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2429 if (LocaleCompare(expression,"b") == 0)
2430 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2436 if (LocaleNCompare(expression,"ceil",4) == 0)
2438 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2440 return((double) ceil((double) alpha));
2442 if (LocaleNCompare(expression,"cosh",4) == 0)
2444 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2446 return((double) cosh((double) alpha));
2448 if (LocaleNCompare(expression,"cos",3) == 0)
2450 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2452 return((double) cos((double) alpha));
2454 if (LocaleCompare(expression,"c") == 0)
2455 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2461 if (LocaleNCompare(expression,"debug",5) == 0)
2466 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2468 if (fx_info->images->colorspace == CMYKColorspace)
2471 case CyanPixelChannel: type="cyan"; break;
2472 case MagentaPixelChannel: type="magenta"; break;
2473 case YellowPixelChannel: type="yellow"; break;
2474 case AlphaPixelChannel: type="opacity"; break;
2475 case BlackPixelChannel: type="black"; break;
2476 default: type="unknown"; break;
2481 case RedPixelChannel: type="red"; break;
2482 case GreenPixelChannel: type="green"; break;
2483 case BluePixelChannel: type="blue"; break;
2484 case AlphaPixelChannel: type="opacity"; break;
2485 default: type="unknown"; break;
2487 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2488 if (strlen(subexpression) > 1)
2489 subexpression[strlen(subexpression)-1]='\0';
2490 if (fx_info->file != (FILE *) NULL)
2491 (void) FormatLocaleFile(fx_info->file,"%s[%.20g,%.20g].%s: "
2492 "%s=%.*g\n",fx_info->images->filename,(double) x,(double) y,type,
2493 subexpression,GetMagickPrecision(),(double) alpha);
2496 if (LocaleNCompare(expression,"drc",3) == 0)
2498 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2500 return((double) (alpha/(*beta*(alpha-1.0)+1.0)));
2507 if (LocaleCompare(expression,"epsilon") == 0)
2508 return((double) MagickEpsilon);
2509 if (LocaleNCompare(expression,"exp",3) == 0)
2511 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2513 return((double) exp((double) alpha));
2515 if (LocaleCompare(expression,"e") == 0)
2516 return((double) 2.7182818284590452354);
2522 if (LocaleNCompare(expression,"floor",5) == 0)
2524 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2526 return((double) floor((double) alpha));
2533 if (LocaleNCompare(expression,"gauss",5) == 0)
2535 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2537 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
2538 return((double) gamma);
2540 if (LocaleNCompare(expression,"gcd",3) == 0)
2545 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2547 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType) (*beta+
2549 return((double) gcd);
2551 if (LocaleCompare(expression,"g") == 0)
2552 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2558 if (LocaleCompare(expression,"h") == 0)
2559 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2560 if (LocaleCompare(expression,"hue") == 0)
2561 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2562 if (LocaleNCompare(expression,"hypot",5) == 0)
2564 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2566 return((double) hypot((double) alpha,(double) *beta));
2573 if (LocaleCompare(expression,"k") == 0)
2574 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2580 if (LocaleCompare(expression,"intensity") == 0)
2581 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2582 if (LocaleNCompare(expression,"int",3) == 0)
2584 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2586 return((double) floor(alpha));
2588 #if defined(MAGICKCORE_HAVE_ISNAN)
2589 if (LocaleNCompare(expression,"isnan",5) == 0)
2591 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2593 return((double) !!isnan((double) alpha));
2596 if (LocaleCompare(expression,"i") == 0)
2597 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2603 if (LocaleCompare(expression,"j") == 0)
2604 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2605 #if defined(MAGICKCORE_HAVE_J0)
2606 if (LocaleNCompare(expression,"j0",2) == 0)
2608 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2610 return((double) j0((double) alpha));
2613 #if defined(MAGICKCORE_HAVE_J1)
2614 if (LocaleNCompare(expression,"j1",2) == 0)
2616 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2618 return((double) j1((double) alpha));
2621 #if defined(MAGICKCORE_HAVE_J1)
2622 if (LocaleNCompare(expression,"jinc",4) == 0)
2624 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2628 gamma=(double) (2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha));
2637 if (LocaleNCompare(expression,"ln",2) == 0)
2639 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2641 return((double) log((double) alpha));
2643 if (LocaleNCompare(expression,"logtwo",6) == 0)
2645 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2647 return((double) log10((double) alpha))/log10(2.0);
2649 if (LocaleNCompare(expression,"log",3) == 0)
2651 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2653 return((double) log10((double) alpha));
2655 if (LocaleCompare(expression,"lightness") == 0)
2656 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2662 if (LocaleCompare(expression,"MaxRGB") == 0)
2663 return((double) QuantumRange);
2664 if (LocaleNCompare(expression,"maxima",6) == 0)
2666 if (LocaleNCompare(expression,"max",3) == 0)
2668 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2670 return(alpha > *beta ? alpha : *beta);
2672 if (LocaleNCompare(expression,"minima",6) == 0)
2674 if (LocaleNCompare(expression,"min",3) == 0)
2676 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2678 return(alpha < *beta ? alpha : *beta);
2680 if (LocaleNCompare(expression,"mod",3) == 0)
2682 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2684 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2687 if (LocaleCompare(expression,"m") == 0)
2688 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2694 if (LocaleNCompare(expression,"not",3) == 0)
2696 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2698 return((double) (alpha < MagickEpsilon));
2700 if (LocaleCompare(expression,"n") == 0)
2701 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2707 if (LocaleCompare(expression,"Opaque") == 0)
2709 if (LocaleCompare(expression,"o") == 0)
2710 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2716 if (LocaleCompare(expression,"phi") == 0)
2717 return((double) MagickPHI);
2718 if (LocaleCompare(expression,"pi") == 0)
2719 return((double) MagickPI);
2720 if (LocaleNCompare(expression,"pow",3) == 0)
2722 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2724 return((double) pow((double) alpha,(double) *beta));
2726 if (LocaleCompare(expression,"p") == 0)
2727 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2733 if (LocaleCompare(expression,"QuantumRange") == 0)
2734 return((double) QuantumRange);
2735 if (LocaleCompare(expression,"QuantumScale") == 0)
2736 return((double) QuantumScale);
2742 if (LocaleNCompare(expression,"rand",4) == 0)
2743 return((double) GetPseudoRandomValue(fx_info->random_info));
2744 if (LocaleNCompare(expression,"round",5) == 0)
2746 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2748 return((double) floor((double) alpha+0.5));
2750 if (LocaleCompare(expression,"r") == 0)
2751 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2757 if (LocaleCompare(expression,"saturation") == 0)
2758 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2759 if (LocaleNCompare(expression,"sign",4) == 0)
2761 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2763 return(alpha < 0.0 ? -1.0 : 1.0);
2765 if (LocaleNCompare(expression,"sinc",4) == 0)
2767 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2771 gamma=(double) (sin((double) (MagickPI*alpha))/
2775 if (LocaleNCompare(expression,"sinh",4) == 0)
2777 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2779 return((double) sinh((double) alpha));
2781 if (LocaleNCompare(expression,"sin",3) == 0)
2783 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2785 return((double) sin((double) alpha));
2787 if (LocaleNCompare(expression,"sqrt",4) == 0)
2789 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2791 return((double) sqrt((double) alpha));
2793 if (LocaleNCompare(expression,"squish",6) == 0)
2795 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2797 return((double) (1.0/(1.0+exp((double) (-alpha)))));
2799 if (LocaleCompare(expression,"s") == 0)
2800 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2806 if (LocaleNCompare(expression,"tanh",4) == 0)
2808 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2810 return((double) tanh((double) alpha));
2812 if (LocaleNCompare(expression,"tan",3) == 0)
2814 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2816 return((double) tan((double) alpha));
2818 if (LocaleCompare(expression,"Transparent") == 0)
2820 if (LocaleNCompare(expression,"trunc",5) == 0)
2822 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2825 return((double) floor((double) alpha));
2826 return((double) ceil((double) alpha));
2828 if (LocaleCompare(expression,"t") == 0)
2829 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2835 if (LocaleCompare(expression,"u") == 0)
2836 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2842 if (LocaleCompare(expression,"v") == 0)
2843 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2849 if (LocaleNCompare(expression,"while",5) == 0)
2853 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2855 } while (fabs((double) alpha) >= MagickEpsilon);
2856 return((double) *beta);
2858 if (LocaleCompare(expression,"w") == 0)
2859 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2865 if (LocaleCompare(expression,"y") == 0)
2866 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2872 if (LocaleCompare(expression,"z") == 0)
2873 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2879 q=(char *) expression;
2880 alpha=InterpretSiPrefixValue(expression,&q);
2881 if (q == expression)
2882 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2886 MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
2887 double *alpha,ExceptionInfo *exception)
2892 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2897 MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
2898 double *alpha,ExceptionInfo *exception)
2907 fx_info->file=(FILE *) NULL;
2908 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2914 MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
2915 const PixelChannel channel,const ssize_t x,const ssize_t y,
2916 double *alpha,ExceptionInfo *exception)
2922 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2924 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2928 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2936 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2938 % FxImage() applies a mathematical expression to the specified image.
2940 % The format of the FxImage method is:
2942 % Image *FxImage(const Image *image,const char *expression,
2943 % ExceptionInfo *exception)
2945 % A description of each parameter follows:
2947 % o image: the image.
2949 % o expression: A mathematical expression.
2951 % o exception: return any errors or warnings in this structure.
2955 static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2960 assert(fx_info != (FxInfo **) NULL);
2961 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
2962 if (fx_info[i] != (FxInfo *) NULL)
2963 fx_info[i]=DestroyFxInfo(fx_info[i]);
2964 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
2968 static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2969 ExceptionInfo *exception)
2986 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
2987 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
2988 if (fx_info == (FxInfo **) NULL)
2990 (void) ThrowMagickException(exception,GetMagickModule(),
2991 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
2992 return((FxInfo **) NULL);
2994 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2995 if (*expression != '@')
2996 fx_expression=ConstantString(expression);
2998 fx_expression=FileToString(expression+1,~0UL,exception);
2999 for (i=0; i < (ssize_t) number_threads; i++)
3004 fx_info[i]=AcquireFxInfo(image,fx_expression,exception);
3005 if (fx_info[i] == (FxInfo *) NULL)
3007 status=FxPreprocessExpression(fx_info[i],&alpha,exception);
3008 if (status == MagickFalse)
3011 fx_expression=DestroyString(fx_expression);
3012 if (i < (ssize_t) number_threads)
3013 fx_info=DestroyFxThreadSet(fx_info);
3017 MagickExport Image *FxImage(const Image *image,const char *expression,
3018 ExceptionInfo *exception)
3020 #define FxImageTag "Fx/Image"
3041 assert(image != (Image *) NULL);
3042 assert(image->signature == MagickSignature);
3043 if (image->debug != MagickFalse)
3044 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3045 fx_info=AcquireFxThreadSet(image,expression,exception);
3046 if (fx_info == (FxInfo **) NULL)
3047 return((Image *) NULL);
3048 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3049 if (fx_image == (Image *) NULL)
3051 fx_info=DestroyFxThreadSet(fx_info);
3052 return((Image *) NULL);
3054 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
3056 fx_info=DestroyFxThreadSet(fx_info);
3057 fx_image=DestroyImage(fx_image);
3058 return((Image *) NULL);
3065 image_view=AcquireVirtualCacheView(image,exception);
3066 fx_view=AcquireAuthenticCacheView(fx_image,exception);
3067 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3068 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3069 magick_threads(image,fx_image,fx_image->rows,1)
3071 for (y=0; y < (ssize_t) fx_image->rows; y++)
3074 id = GetOpenMPThreadId();
3076 register const Quantum
3085 if (status == MagickFalse)
3087 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3088 q=QueueCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
3089 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3094 for (x=0; x < (ssize_t) fx_image->columns; x++)
3099 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3104 PixelChannel channel=GetPixelChannelChannel(image,i);
3105 PixelTrait traits=GetPixelChannelTraits(image,channel);
3106 PixelTrait fx_traits=GetPixelChannelTraits(fx_image,channel);
3107 if ((traits == UndefinedPixelTrait) ||
3108 (fx_traits == UndefinedPixelTrait))
3110 if (((fx_traits & CopyPixelTrait) != 0) ||
3111 (GetPixelReadMask(image,p) == 0))
3113 SetPixelChannel(fx_image,channel,p[i],q);
3117 (void) FxEvaluateChannelExpression(fx_info[id],channel,x,y,&alpha,
3119 q[i]=ClampToQuantum(QuantumRange*alpha);
3121 p+=GetPixelChannels(image);
3122 q+=GetPixelChannels(fx_image);
3124 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3126 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3131 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3132 #pragma omp critical (MagickCore_FxImage)
3134 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3135 if (proceed == MagickFalse)
3139 fx_view=DestroyCacheView(fx_view);
3140 image_view=DestroyCacheView(image_view);
3141 fx_info=DestroyFxThreadSet(fx_info);
3142 if (status == MagickFalse)
3143 fx_image=DestroyImage(fx_image);
3148 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3152 % I m p l o d e I m a g e %
3156 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3158 % ImplodeImage() creates a new image that is a copy of an existing
3159 % one with the image pixels "implode" by the specified percentage. It
3160 % allocates the memory necessary for the new Image structure and returns a
3161 % pointer to the new image.
3163 % The format of the ImplodeImage method is:
3165 % Image *ImplodeImage(const Image *image,const double amount,
3166 % const PixelInterpolateMethod method,ExceptionInfo *exception)
3168 % A description of each parameter follows:
3170 % o implode_image: Method ImplodeImage returns a pointer to the image
3171 % after it is implode. A null image is returned if there is a memory
3174 % o image: the image.
3176 % o amount: Define the extent of the implosion.
3178 % o method: the pixel interpolation method.
3180 % o exception: return any errors or warnings in this structure.
3183 MagickExport Image *ImplodeImage(const Image *image,const double amount,
3184 const PixelInterpolateMethod method,ExceptionInfo *exception)
3186 #define ImplodeImageTag "Implode/Image"
3213 Initialize implode image attributes.
3215 assert(image != (Image *) NULL);
3216 assert(image->signature == MagickSignature);
3217 if (image->debug != MagickFalse)
3218 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3219 assert(exception != (ExceptionInfo *) NULL);
3220 assert(exception->signature == MagickSignature);
3221 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3223 if (implode_image == (Image *) NULL)
3224 return((Image *) NULL);
3225 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
3227 implode_image=DestroyImage(implode_image);
3228 return((Image *) NULL);
3230 if (implode_image->background_color.alpha != OpaqueAlpha)
3231 implode_image->alpha_trait=BlendPixelTrait;
3233 Compute scaling factor.
3237 center.x=0.5*image->columns;
3238 center.y=0.5*image->rows;
3240 if (image->columns > image->rows)
3241 scale.y=(double) image->columns/(double) image->rows;
3243 if (image->columns < image->rows)
3245 scale.x=(double) image->rows/(double) image->columns;
3253 image_view=AcquireVirtualCacheView(image,exception);
3254 interpolate_view=AcquireVirtualCacheView(image,exception);
3255 implode_view=AcquireAuthenticCacheView(implode_image,exception);
3256 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3257 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3258 magick_threads(image,implode_image,image->rows,1)
3260 for (y=0; y < (ssize_t) image->rows; y++)
3268 register const Quantum
3277 if (status == MagickFalse)
3279 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3280 q=QueueCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
3282 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3287 delta.y=scale.y*(double) (y-center.y);
3288 for (x=0; x < (ssize_t) image->columns; x++)
3294 Determine if the pixel is within an ellipse.
3296 if (GetPixelReadMask(image,p) == 0)
3298 SetPixelBackgoundColor(implode_image,q);
3299 p+=GetPixelChannels(image);
3300 q+=GetPixelChannels(implode_image);
3303 delta.x=scale.x*(double) (x-center.x);
3304 distance=delta.x*delta.x+delta.y*delta.y;
3305 if (distance >= (radius*radius))
3306 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3308 PixelChannel channel=GetPixelChannelChannel(image,i);
3309 PixelTrait traits=GetPixelChannelTraits(image,channel);
3310 PixelTrait implode_traits=GetPixelChannelTraits(implode_image,
3312 if ((traits == UndefinedPixelTrait) ||
3313 (implode_traits == UndefinedPixelTrait))
3315 SetPixelChannel(implode_image,channel,p[i],q);
3327 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/radius/
3329 status=InterpolatePixelChannels(image,interpolate_view,implode_image,
3330 method,(double) (factor*delta.x/scale.x+center.x),(double) (factor*
3331 delta.y/scale.y+center.y),q,exception);
3333 p+=GetPixelChannels(image);
3334 q+=GetPixelChannels(implode_image);
3336 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3338 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3343 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3344 #pragma omp critical (MagickCore_ImplodeImage)
3346 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3347 if (proceed == MagickFalse)
3351 implode_view=DestroyCacheView(implode_view);
3352 interpolate_view=DestroyCacheView(interpolate_view);
3353 image_view=DestroyCacheView(image_view);
3354 if (status == MagickFalse)
3355 implode_image=DestroyImage(implode_image);
3356 return(implode_image);
3360 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3364 % M o r p h I m a g e s %
3368 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3370 % The MorphImages() method requires a minimum of two images. The first
3371 % image is transformed into the second by a number of intervening images
3372 % as specified by frames.
3374 % The format of the MorphImage method is:
3376 % Image *MorphImages(const Image *image,const size_t number_frames,
3377 % ExceptionInfo *exception)
3379 % A description of each parameter follows:
3381 % o image: the image.
3383 % o number_frames: Define the number of in-between image to generate.
3384 % The more in-between frames, the smoother the morph.
3386 % o exception: return any errors or warnings in this structure.
3389 MagickExport Image *MorphImages(const Image *image,const size_t number_frames,
3390 ExceptionInfo *exception)
3392 #define MorphImageTag "Morph/Image"
3408 register const Image
3418 Clone first frame in sequence.
3420 assert(image != (Image *) NULL);
3421 assert(image->signature == MagickSignature);
3422 if (image->debug != MagickFalse)
3423 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3424 assert(exception != (ExceptionInfo *) NULL);
3425 assert(exception->signature == MagickSignature);
3426 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3427 if (morph_images == (Image *) NULL)
3428 return((Image *) NULL);
3429 if (GetNextImageInList(image) == (Image *) NULL)
3434 for (i=1; i < (ssize_t) number_frames; i++)
3436 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3437 if (morph_image == (Image *) NULL)
3439 morph_images=DestroyImageList(morph_images);
3440 return((Image *) NULL);
3442 AppendImageToList(&morph_images,morph_image);
3443 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3448 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3450 if (proceed == MagickFalse)
3454 return(GetFirstImageInList(morph_images));
3457 Morph image sequence.
3462 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3464 for (i=0; i < (ssize_t) number_frames; i++)
3470 beta=(double) (i+1.0)/(double) (number_frames+1.0);
3472 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
3473 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*next->rows+beta*
3474 GetNextImageInList(next)->rows+0.5),next->filter,exception);
3475 if (morph_image == (Image *) NULL)
3477 morph_images=DestroyImageList(morph_images);
3478 return((Image *) NULL);
3480 status=SetImageStorageClass(morph_image,DirectClass,exception);
3481 if (status == MagickFalse)
3483 morph_image=DestroyImage(morph_image);
3484 return((Image *) NULL);
3486 AppendImageToList(&morph_images,morph_image);
3487 morph_images=GetLastImageInList(morph_images);
3488 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
3489 morph_images->rows,GetNextImageInList(next)->filter,exception);
3490 if (morph_image == (Image *) NULL)
3492 morph_images=DestroyImageList(morph_images);
3493 return((Image *) NULL);
3495 image_view=AcquireVirtualCacheView(morph_image,exception);
3496 morph_view=AcquireAuthenticCacheView(morph_images,exception);
3497 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3498 #pragma omp parallel for schedule(static,4) shared(status) \
3499 magick_threads(morph_image,morph_image,morph_image->rows,1)
3501 for (y=0; y < (ssize_t) morph_images->rows; y++)
3506 register const Quantum
3515 if (status == MagickFalse)
3517 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3519 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3521 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3526 for (x=0; x < (ssize_t) morph_images->columns; x++)
3531 for (i=0; i < (ssize_t) GetPixelChannels(morph_image); i++)
3533 PixelChannel channel=GetPixelChannelChannel(morph_image,i);
3534 PixelTrait traits=GetPixelChannelTraits(morph_image,channel);
3535 PixelTrait morph_traits=GetPixelChannelTraits(morph_images,channel);
3536 if ((traits == UndefinedPixelTrait) ||
3537 (morph_traits == UndefinedPixelTrait))
3539 if (((morph_traits & CopyPixelTrait) != 0) ||
3540 (GetPixelReadMask(morph_images,p) == 0))
3542 SetPixelChannel(morph_image,channel,p[i],q);
3545 SetPixelChannel(morph_image,channel,ClampToQuantum(alpha*
3546 GetPixelChannel(morph_images,channel,q)+beta*p[i]),q);
3548 p+=GetPixelChannels(morph_image);
3549 q+=GetPixelChannels(morph_images);
3551 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3552 if (sync == MagickFalse)
3555 morph_view=DestroyCacheView(morph_view);
3556 image_view=DestroyCacheView(image_view);
3557 morph_image=DestroyImage(morph_image);
3559 if (i < (ssize_t) number_frames)
3562 Clone last frame in sequence.
3564 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3565 if (morph_image == (Image *) NULL)
3567 morph_images=DestroyImageList(morph_images);
3568 return((Image *) NULL);
3570 AppendImageToList(&morph_images,morph_image);
3571 morph_images=GetLastImageInList(morph_images);
3572 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3577 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3578 #pragma omp critical (MagickCore_MorphImages)
3580 proceed=SetImageProgress(image,MorphImageTag,scene,
3581 GetImageListLength(image));
3582 if (proceed == MagickFalse)
3587 if (GetNextImageInList(next) != (Image *) NULL)
3589 morph_images=DestroyImageList(morph_images);
3590 return((Image *) NULL);
3592 return(GetFirstImageInList(morph_images));
3596 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3600 % P l a s m a I m a g e %
3604 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3606 % PlasmaImage() initializes an image with plasma fractal values. The image
3607 % must be initialized with a base color and the random number generator
3608 % seeded before this method is called.
3610 % The format of the PlasmaImage method is:
3612 % MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
3613 % size_t attenuate,size_t depth,ExceptionInfo *exception)
3615 % A description of each parameter follows:
3617 % o image: the image.
3619 % o segment: Define the region to apply plasma fractals values.
3621 % o attenuate: Define the plasma attenuation factor.
3623 % o depth: Limit the plasma recursion depth.
3625 % o exception: return any errors or warnings in this structure.
3629 static inline Quantum PlasmaPixel(RandomInfo *random_info,
3630 const double pixel,const double noise)
3635 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
3640 static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3641 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3642 const SegmentInfo *segment,size_t attenuate,size_t depth,
3643 ExceptionInfo *exception)
3648 register const Quantum
3664 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3672 Divide the area into quadrants and recurse.
3676 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3677 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
3678 local_info=(*segment);
3679 local_info.x2=(double) x_mid;
3680 local_info.y2=(double) y_mid;
3681 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3682 &local_info,attenuate,depth,exception);
3683 local_info=(*segment);
3684 local_info.y1=(double) y_mid;
3685 local_info.x2=(double) x_mid;
3686 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3687 &local_info,attenuate,depth,exception);
3688 local_info=(*segment);
3689 local_info.x1=(double) x_mid;
3690 local_info.y2=(double) y_mid;
3691 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3692 &local_info,attenuate,depth,exception);
3693 local_info=(*segment);
3694 local_info.x1=(double) x_mid;
3695 local_info.y1=(double) y_mid;
3696 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3697 &local_info,attenuate,depth,exception));
3699 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3700 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
3701 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3702 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3703 return(MagickFalse);
3705 Average pixels and apply plasma.
3707 plasma=(double) QuantumRange/(2.0*attenuate);
3708 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3713 x=(ssize_t) ceil(segment->x1-0.5);
3714 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),1,1,
3716 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),1,1,
3718 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
3719 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3720 (q == (Quantum *) NULL))
3722 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3724 PixelChannel channel=GetPixelChannelChannel(image,i);
3725 PixelTrait traits=GetPixelChannelTraits(image,channel);
3726 if (traits == UndefinedPixelTrait)
3728 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
3730 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3731 if (segment->x1 != segment->x2)
3736 x=(ssize_t) ceil(segment->x2-0.5);
3737 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3739 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3741 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
3742 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3743 (q == (Quantum *) NULL))
3745 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3747 PixelChannel channel=GetPixelChannelChannel(image,i);
3748 PixelTrait traits=GetPixelChannelTraits(image,channel);
3749 if (traits == UndefinedPixelTrait)
3751 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
3753 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3756 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3758 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3763 y=(ssize_t) ceil(segment->y2-0.5);
3764 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3766 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3768 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
3769 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3770 (q == (Quantum *) NULL))
3772 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3774 PixelChannel channel=GetPixelChannelChannel(image,i);
3775 PixelTrait traits=GetPixelChannelTraits(image,channel);
3776 if (traits == UndefinedPixelTrait)
3778 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
3780 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3782 if (segment->y1 != segment->y2)
3787 y=(ssize_t) ceil(segment->y1-0.5);
3788 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3790 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3792 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
3793 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3794 (q == (Quantum *) NULL))
3796 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3798 PixelChannel channel=GetPixelChannelChannel(image,i);
3799 PixelTrait traits=GetPixelChannelTraits(image,channel);
3800 if (traits == UndefinedPixelTrait)
3802 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
3804 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3807 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3812 x=(ssize_t) ceil(segment->x1-0.5);
3813 y=(ssize_t) ceil(segment->y1-0.5);
3814 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
3815 x=(ssize_t) ceil(segment->x2-0.5);
3816 y=(ssize_t) ceil(segment->y2-0.5);
3817 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
3818 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
3819 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3820 (q == (Quantum *) NULL))
3822 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3824 PixelChannel channel=GetPixelChannelChannel(image,i);
3825 PixelTrait traits=GetPixelChannelTraits(image,channel);
3826 if (traits == UndefinedPixelTrait)
3828 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
3830 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3832 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3834 return(MagickFalse);
3837 MagickExport MagickBooleanType PlasmaImage(Image *image,
3838 const SegmentInfo *segment,size_t attenuate,size_t depth,
3839 ExceptionInfo *exception)
3852 if (image->debug != MagickFalse)
3853 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3854 assert(image != (Image *) NULL);
3855 assert(image->signature == MagickSignature);
3856 if (image->debug != MagickFalse)
3857 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3858 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
3859 return(MagickFalse);
3860 image_view=AcquireAuthenticCacheView(image,exception);
3861 u_view=AcquireVirtualCacheView(image,exception);
3862 v_view=AcquireVirtualCacheView(image,exception);
3863 random_info=AcquireRandomInfo();
3864 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3865 attenuate,depth,exception);
3866 random_info=DestroyRandomInfo(random_info);
3867 v_view=DestroyCacheView(v_view);
3868 u_view=DestroyCacheView(u_view);
3869 image_view=DestroyCacheView(image_view);
3874 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3878 % P o l a r o i d I m a g e %
3882 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3884 % PolaroidImage() simulates a Polaroid picture.
3886 % The format of the AnnotateImage method is:
3888 % Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
3889 % const char *caption,const double angle,
3890 % const PixelInterpolateMethod method,ExceptionInfo exception)
3892 % A description of each parameter follows:
3894 % o image: the image.
3896 % o draw_info: the draw info.
3898 % o caption: the Polaroid caption.
3900 % o angle: Apply the effect along this angle.
3902 % o method: the pixel interpolation method.
3904 % o exception: return any errors or warnings in this structure.
3907 MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
3908 const char *caption,const double angle,const PixelInterpolateMethod method,
3909 ExceptionInfo *exception)
3927 Simulate a Polaroid picture.
3929 assert(image != (Image *) NULL);
3930 assert(image->signature == MagickSignature);
3931 if (image->debug != MagickFalse)
3932 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3933 assert(exception != (ExceptionInfo *) NULL);
3934 assert(exception->signature == MagickSignature);
3935 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
3936 image->rows)/25.0,10.0);
3937 height=image->rows+2*quantum;
3938 caption_image=(Image *) NULL;
3939 if (caption != (const char *) NULL)
3942 geometry[MaxTextExtent],
3961 Generate caption image.
3963 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3964 if (caption_image == (Image *) NULL)
3965 return((Image *) NULL);
3966 image_info=AcquireImageInfo();
3967 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
3968 text=InterpretImageProperties(image_info,(Image *) image,caption,
3970 image_info=DestroyImageInfo(image_info);
3971 (void) CloneString(&annotate_info->text,text);
3972 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
3974 status=SetImageExtent(caption_image,image->columns,(size_t) ((count+1)*
3975 (metrics.ascent-metrics.descent)+0.5),exception);
3976 if (status == MagickFalse)
3977 caption_image=DestroyImage(caption_image);
3980 caption_image->background_color=image->border_color;
3981 (void) SetImageBackgroundColor(caption_image,exception);
3982 (void) CloneString(&annotate_info->text,text);
3983 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
3985 if (annotate_info->gravity == UndefinedGravity)
3986 (void) CloneString(&annotate_info->geometry,AcquireString(
3988 (void) AnnotateImage(caption_image,annotate_info,exception);
3989 height+=caption_image->rows;
3991 annotate_info=DestroyDrawInfo(annotate_info);
3992 text=DestroyString(text);
3994 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
3996 if (picture_image == (Image *) NULL)
3998 if (caption_image != (Image *) NULL)
3999 caption_image=DestroyImage(caption_image);
4000 return((Image *) NULL);
4002 picture_image->background_color=image->border_color;
4003 (void) SetImageBackgroundColor(picture_image,exception);
4004 (void) CompositeImage(picture_image,image,OverCompositeOp,MagickTrue,quantum,
4006 if (caption_image != (Image *) NULL)
4008 (void) CompositeImage(picture_image,caption_image,OverCompositeOp,
4009 MagickTrue,quantum,(ssize_t) (image->rows+3*quantum/2),exception);
4010 caption_image=DestroyImage(caption_image);
4012 (void) QueryColorCompliance("none",AllCompliance,
4013 &picture_image->background_color,exception);
4014 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
4015 rotate_image=RotateImage(picture_image,90.0,exception);
4016 picture_image=DestroyImage(picture_image);
4017 if (rotate_image == (Image *) NULL)
4018 return((Image *) NULL);
4019 picture_image=rotate_image;
4020 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
4021 picture_image->columns,method,exception);
4022 picture_image=DestroyImage(picture_image);
4023 if (bend_image == (Image *) NULL)
4024 return((Image *) NULL);
4025 picture_image=bend_image;
4026 rotate_image=RotateImage(picture_image,-90.0,exception);
4027 picture_image=DestroyImage(picture_image);
4028 if (rotate_image == (Image *) NULL)
4029 return((Image *) NULL);
4030 picture_image=rotate_image;
4031 picture_image->background_color=image->background_color;
4032 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
4034 if (polaroid_image == (Image *) NULL)
4036 picture_image=DestroyImage(picture_image);
4037 return(picture_image);
4039 flop_image=FlopImage(polaroid_image,exception);
4040 polaroid_image=DestroyImage(polaroid_image);
4041 if (flop_image == (Image *) NULL)
4043 picture_image=DestroyImage(picture_image);
4044 return(picture_image);
4046 polaroid_image=flop_image;
4047 (void) CompositeImage(polaroid_image,picture_image,OverCompositeOp,
4048 MagickTrue,(ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
4049 picture_image=DestroyImage(picture_image);
4050 (void) QueryColorCompliance("none",AllCompliance,
4051 &polaroid_image->background_color,exception);
4052 rotate_image=RotateImage(polaroid_image,angle,exception);
4053 polaroid_image=DestroyImage(polaroid_image);
4054 if (rotate_image == (Image *) NULL)
4055 return((Image *) NULL);
4056 polaroid_image=rotate_image;
4057 trim_image=TrimImage(polaroid_image,exception);
4058 polaroid_image=DestroyImage(polaroid_image);
4059 if (trim_image == (Image *) NULL)
4060 return((Image *) NULL);
4061 polaroid_image=trim_image;
4062 return(polaroid_image);
4066 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4070 % S e p i a T o n e I m a g e %
4074 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4076 % MagickSepiaToneImage() applies a special effect to the image, similar to the
4077 % effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4078 % 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4079 % threshold of 80% is a good starting point for a reasonable tone.
4081 % The format of the SepiaToneImage method is:
4083 % Image *SepiaToneImage(const Image *image,const double threshold,
4084 % ExceptionInfo *exception)
4086 % A description of each parameter follows:
4088 % o image: the image.
4090 % o threshold: the tone threshold.
4092 % o exception: return any errors or warnings in this structure.
4095 MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4096 ExceptionInfo *exception)
4098 #define SepiaToneImageTag "SepiaTone/Image"
4117 Initialize sepia-toned image attributes.
4119 assert(image != (const Image *) NULL);
4120 assert(image->signature == MagickSignature);
4121 if (image->debug != MagickFalse)
4122 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4123 assert(exception != (ExceptionInfo *) NULL);
4124 assert(exception->signature == MagickSignature);
4125 sepia_image=CloneImage(image,0,0,MagickTrue,exception);
4126 if (sepia_image == (Image *) NULL)
4127 return((Image *) NULL);
4128 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
4130 sepia_image=DestroyImage(sepia_image);
4131 return((Image *) NULL);
4134 Tone each row of the image.
4138 image_view=AcquireVirtualCacheView(image,exception);
4139 sepia_view=AcquireAuthenticCacheView(sepia_image,exception);
4140 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4141 #pragma omp parallel for schedule(static,4) shared(progress,status) \
4142 magick_threads(image,sepia_image,image->rows,1)
4144 for (y=0; y < (ssize_t) image->rows; y++)
4146 register const Quantum
4155 if (status == MagickFalse)
4157 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
4158 q=GetCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
4160 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
4165 for (x=0; x < (ssize_t) image->columns; x++)
4171 intensity=GetPixelIntensity(image,p);
4172 tone=intensity > threshold ? (double) QuantumRange : intensity+
4173 (double) QuantumRange-threshold;
4174 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
4175 tone=intensity > (7.0*threshold/6.0) ? (double) QuantumRange :
4176 intensity+(double) QuantumRange-7.0*threshold/6.0;
4177 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4178 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
4179 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
4181 if ((double) GetPixelGreen(image,q) < tone)
4182 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4183 if ((double) GetPixelBlue(image,q) < tone)
4184 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
4185 p+=GetPixelChannels(image);
4186 q+=GetPixelChannels(sepia_image);
4188 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4190 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4195 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4196 #pragma omp critical (MagickCore_SepiaToneImage)
4198 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4200 if (proceed == MagickFalse)
4204 sepia_view=DestroyCacheView(sepia_view);
4205 image_view=DestroyCacheView(image_view);
4206 (void) NormalizeImage(sepia_image,exception);
4207 (void) ContrastImage(sepia_image,MagickTrue,exception);
4208 if (status == MagickFalse)
4209 sepia_image=DestroyImage(sepia_image);
4210 return(sepia_image);
4214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4218 % S h a d o w I m a g e %
4222 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4224 % ShadowImage() simulates a shadow from the specified image and returns it.
4226 % The format of the ShadowImage method is:
4228 % Image *ShadowImage(const Image *image,const double alpha,
4229 % const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4230 % ExceptionInfo *exception)
4232 % A description of each parameter follows:
4234 % o image: the image.
4236 % o alpha: percentage transparency.
4238 % o sigma: the standard deviation of the Gaussian, in pixels.
4240 % o x_offset: the shadow x-offset.
4242 % o y_offset: the shadow y-offset.
4244 % o exception: return any errors or warnings in this structure.
4247 MagickExport Image *ShadowImage(const Image *image,const double alpha,
4248 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4249 ExceptionInfo *exception)
4251 #define ShadowImageTag "Shadow/Image"
4273 assert(image != (Image *) NULL);
4274 assert(image->signature == MagickSignature);
4275 if (image->debug != MagickFalse)
4276 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4277 assert(exception != (ExceptionInfo *) NULL);
4278 assert(exception->signature == MagickSignature);
4279 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4280 if (clone_image == (Image *) NULL)
4281 return((Image *) NULL);
4282 if (IsGrayColorspace(image->colorspace) != MagickFalse)
4283 (void) SetImageColorspace(clone_image,sRGBColorspace,exception);
4284 (void) SetImageVirtualPixelMethod(clone_image,TransparentVirtualPixelMethod,
4286 border_info.width=(size_t) floor(2.0*sigma+0.5);
4287 border_info.height=(size_t) floor(2.0*sigma+0.5);
4290 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4292 clone_image->alpha_trait=BlendPixelTrait;
4293 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
4294 clone_image=DestroyImage(clone_image);
4295 if (border_image == (Image *) NULL)
4296 return((Image *) NULL);
4297 if (border_image->alpha_trait != BlendPixelTrait)
4298 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
4303 image_view=AcquireAuthenticCacheView(border_image,exception);
4304 for (y=0; y < (ssize_t) border_image->rows; y++)
4315 if (status == MagickFalse)
4317 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
4319 if (q == (Quantum *) NULL)
4324 background_color=border_image->background_color;
4325 background_color.alpha_trait=BlendPixelTrait;
4326 for (x=0; x < (ssize_t) border_image->columns; x++)
4328 if (border_image->alpha_trait == BlendPixelTrait)
4329 background_color.alpha=GetPixelAlpha(border_image,q)*alpha/100.0;
4330 SetPixelInfoPixel(border_image,&background_color,q);
4331 q+=GetPixelChannels(border_image);
4333 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4336 image_view=DestroyCacheView(image_view);
4337 if (status == MagickFalse)
4339 border_image=DestroyImage(border_image);
4340 return((Image *) NULL);
4342 channel_mask=SetImageChannelMask(border_image,AlphaChannel);
4343 shadow_image=BlurImage(border_image,0.0,sigma,exception);
4344 border_image=DestroyImage(border_image);
4345 if (shadow_image == (Image *) NULL)
4346 return((Image *) NULL);
4347 (void) SetPixelChannelMask(shadow_image,channel_mask);
4348 if (shadow_image->page.width == 0)
4349 shadow_image->page.width=shadow_image->columns;
4350 if (shadow_image->page.height == 0)
4351 shadow_image->page.height=shadow_image->rows;
4352 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4353 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4354 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4355 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
4356 return(shadow_image);
4360 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4364 % S k e t c h I m a g e %
4368 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4370 % SketchImage() simulates a pencil sketch. We convolve the image with a
4371 % Gaussian operator of the given radius and standard deviation (sigma). For
4372 % reasonable results, radius should be larger than sigma. Use a radius of 0
4373 % and SketchImage() selects a suitable radius for you. Angle gives the angle
4376 % The format of the SketchImage method is:
4378 % Image *SketchImage(const Image *image,const double radius,
4379 % const double sigma,const double angle,ExceptionInfo *exception)
4381 % A description of each parameter follows:
4383 % o image: the image.
4385 % o radius: the radius of the Gaussian, in pixels, not counting the
4388 % o sigma: the standard deviation of the Gaussian, in pixels.
4390 % o angle: apply the effect along this angle.
4392 % o exception: return any errors or warnings in this structure.
4395 MagickExport Image *SketchImage(const Image *image,const double radius,
4396 const double sigma,const double angle,ExceptionInfo *exception)
4412 **restrict random_info;
4417 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4425 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4426 MagickTrue,exception);
4427 if (random_image == (Image *) NULL)
4428 return((Image *) NULL);
4430 random_info=AcquireRandomInfoThreadSet();
4431 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4432 key=GetRandomSecretKey(random_info[0]);
4434 random_view=AcquireAuthenticCacheView(random_image,exception);
4435 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4436 #pragma omp parallel for schedule(static,4) shared(status) \
4437 magick_threads(random_image,random_image,random_image->rows,key == ~0UL)
4439 for (y=0; y < (ssize_t) random_image->rows; y++)
4442 id = GetOpenMPThreadId();
4450 if (status == MagickFalse)
4452 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4454 if (q == (Quantum *) NULL)
4459 for (x=0; x < (ssize_t) random_image->columns; x++)
4467 if (GetPixelReadMask(random_image,q) == 0)
4469 q+=GetPixelChannels(random_image);
4472 value=GetPseudoRandomValue(random_info[id]);
4473 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4475 PixelChannel channel=GetPixelChannelChannel(image,i);
4476 PixelTrait traits=GetPixelChannelTraits(image,channel);
4477 if (traits == UndefinedPixelTrait)
4479 q[i]=ClampToQuantum(QuantumRange*value);
4481 q+=GetPixelChannels(random_image);
4483 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4486 random_view=DestroyCacheView(random_view);
4487 random_info=DestroyRandomInfoThreadSet(random_info);
4488 if (status == MagickFalse)
4490 random_image=DestroyImage(random_image);
4491 return(random_image);
4493 blur_image=MotionBlurImage(random_image,radius,sigma,angle,exception);
4494 random_image=DestroyImage(random_image);
4495 if (blur_image == (Image *) NULL)
4496 return((Image *) NULL);
4497 dodge_image=EdgeImage(blur_image,radius,exception);
4498 blur_image=DestroyImage(blur_image);
4499 if (dodge_image == (Image *) NULL)
4500 return((Image *) NULL);
4501 (void) NormalizeImage(dodge_image,exception);
4502 (void) NegateImage(dodge_image,MagickFalse,exception);
4503 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
4504 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4505 if (sketch_image == (Image *) NULL)
4507 dodge_image=DestroyImage(dodge_image);
4508 return((Image *) NULL);
4510 (void) CompositeImage(sketch_image,dodge_image,ColorDodgeCompositeOp,
4511 MagickTrue,0,0,exception);
4512 dodge_image=DestroyImage(dodge_image);
4513 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4514 if (blend_image == (Image *) NULL)
4516 sketch_image=DestroyImage(sketch_image);
4517 return((Image *) NULL);
4519 (void) SetImageArtifact(blend_image,"compose:args","20x80");
4520 (void) CompositeImage(sketch_image,blend_image,BlendCompositeOp,MagickTrue,
4522 blend_image=DestroyImage(blend_image);
4523 return(sketch_image);
4527 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4531 % S o l a r i z e I m a g e %
4535 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4537 % SolarizeImage() applies a special effect to the image, similar to the effect
4538 % achieved in a photo darkroom by selectively exposing areas of photo
4539 % sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4540 % measure of the extent of the solarization.
4542 % The format of the SolarizeImage method is:
4544 % MagickBooleanType SolarizeImage(Image *image,const double threshold,
4545 % ExceptionInfo *exception)
4547 % A description of each parameter follows:
4549 % o image: the image.
4551 % o threshold: Define the extent of the solarization.
4553 % o exception: return any errors or warnings in this structure.
4556 MagickExport MagickBooleanType SolarizeImage(Image *image,
4557 const double threshold,ExceptionInfo *exception)
4559 #define SolarizeImageTag "Solarize/Image"
4573 assert(image != (Image *) NULL);
4574 assert(image->signature == MagickSignature);
4575 if (image->debug != MagickFalse)
4576 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4577 if (IsGrayColorspace(image->colorspace) != MagickFalse)
4578 (void) SetImageColorspace(image,sRGBColorspace,exception);
4579 if (image->storage_class == PseudoClass)
4587 for (i=0; i < (ssize_t) image->colors; i++)
4589 if ((double) image->colormap[i].red > threshold)
4590 image->colormap[i].red=QuantumRange-image->colormap[i].red;
4591 if ((double) image->colormap[i].green > threshold)
4592 image->colormap[i].green=QuantumRange-image->colormap[i].green;
4593 if ((double) image->colormap[i].blue > threshold)
4594 image->colormap[i].blue=QuantumRange-image->colormap[i].blue;
4602 image_view=AcquireAuthenticCacheView(image,exception);
4603 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4604 #pragma omp parallel for schedule(static,4) shared(progress,status) \
4605 magick_threads(image,image,image->rows,1)
4607 for (y=0; y < (ssize_t) image->rows; y++)
4615 if (status == MagickFalse)
4617 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
4618 if (q == (Quantum *) NULL)
4623 for (x=0; x < (ssize_t) image->columns; x++)
4628 if (GetPixelReadMask(image,q) == 0)
4630 q+=GetPixelChannels(image);
4633 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4635 PixelChannel channel=GetPixelChannelChannel(image,i);
4636 PixelTrait traits=GetPixelChannelTraits(image,channel);
4637 if ((traits == UndefinedPixelTrait) ||
4638 ((traits & CopyPixelTrait) != 0))
4640 if ((double) q[i] > threshold)
4641 q[i]=QuantumRange-q[i];
4643 q+=GetPixelChannels(image);
4645 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4647 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4652 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4653 #pragma omp critical (MagickCore_SolarizeImage)
4655 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4656 if (proceed == MagickFalse)
4660 image_view=DestroyCacheView(image_view);
4665 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4669 % S t e g a n o I m a g e %
4673 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4675 % SteganoImage() hides a digital watermark within the image. Recover
4676 % the hidden watermark later to prove that the authenticity of an image.
4677 % Offset defines the start position within the image to hide the watermark.
4679 % The format of the SteganoImage method is:
4681 % Image *SteganoImage(const Image *image,Image *watermark,
4682 % ExceptionInfo *exception)
4684 % A description of each parameter follows:
4686 % o image: the image.
4688 % o watermark: the watermark image.
4690 % o exception: return any errors or warnings in this structure.
4693 MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4694 ExceptionInfo *exception)
4696 #define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
4697 #define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
4698 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
4699 #define SteganoImageTag "Stegano/Image"
4734 Initialize steganographic image attributes.
4736 assert(image != (const Image *) NULL);
4737 assert(image->signature == MagickSignature);
4738 if (image->debug != MagickFalse)
4739 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4740 assert(watermark != (const Image *) NULL);
4741 assert(watermark->signature == MagickSignature);
4742 assert(exception != (ExceptionInfo *) NULL);
4743 assert(exception->signature == MagickSignature);
4745 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4746 if (stegano_image == (Image *) NULL)
4747 return((Image *) NULL);
4748 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
4749 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
4751 stegano_image=DestroyImage(stegano_image);
4752 return((Image *) NULL);
4755 Hide watermark in low-order bits of image.
4760 depth=stegano_image->depth;
4761 k=stegano_image->offset;
4763 watermark_view=AcquireVirtualCacheView(watermark,exception);
4764 stegano_view=AcquireAuthenticCacheView(stegano_image,exception);
4765 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
4767 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
4769 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
4774 (void) GetOneCacheViewVirtualPixelInfo(watermark_view,x,y,&pixel,
4776 offset=k/(ssize_t) stegano_image->columns;
4777 if (offset >= (ssize_t) stegano_image->rows)
4779 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4780 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4782 if (q == (Quantum *) NULL)
4788 SetPixelRed(stegano_image,SetBit(GetPixelRed(stegano_image,q),j,
4789 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
4794 SetPixelGreen(stegano_image,SetBit(GetPixelGreen(stegano_image,q),j,
4795 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
4800 SetPixelBlue(stegano_image,SetBit(GetPixelBlue(stegano_image,q),j,
4801 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
4805 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
4811 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
4813 if (k == stegano_image->offset)
4817 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4822 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4824 if (proceed == MagickFalse)
4828 stegano_view=DestroyCacheView(stegano_view);
4829 watermark_view=DestroyCacheView(watermark_view);
4830 if (status == MagickFalse)
4831 stegano_image=DestroyImage(stegano_image);
4832 return(stegano_image);
4836 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4840 % S t e r e o A n a g l y p h I m a g e %
4844 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4846 % StereoAnaglyphImage() combines two images and produces a single image that
4847 % is the composite of a left and right image of a stereo pair. Special
4848 % red-green stereo glasses are required to view this effect.
4850 % The format of the StereoAnaglyphImage method is:
4852 % Image *StereoImage(const Image *left_image,const Image *right_image,
4853 % ExceptionInfo *exception)
4854 % Image *StereoAnaglyphImage(const Image *left_image,
4855 % const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
4856 % ExceptionInfo *exception)
4858 % A description of each parameter follows:
4860 % o left_image: the left image.
4862 % o right_image: the right image.
4864 % o exception: return any errors or warnings in this structure.
4866 % o x_offset: amount, in pixels, by which the left image is offset to the
4867 % right of the right image.
4869 % o y_offset: amount, in pixels, by which the left image is offset to the
4870 % bottom of the right image.
4874 MagickExport Image *StereoImage(const Image *left_image,
4875 const Image *right_image,ExceptionInfo *exception)
4877 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4880 MagickExport Image *StereoAnaglyphImage(const Image *left_image,
4881 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
4882 ExceptionInfo *exception)
4884 #define StereoImageTag "Stereo/Image"
4898 assert(left_image != (const Image *) NULL);
4899 assert(left_image->signature == MagickSignature);
4900 if (left_image->debug != MagickFalse)
4901 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4902 left_image->filename);
4903 assert(right_image != (const Image *) NULL);
4904 assert(right_image->signature == MagickSignature);
4905 assert(exception != (ExceptionInfo *) NULL);
4906 assert(exception->signature == MagickSignature);
4907 assert(right_image != (const Image *) NULL);
4909 if ((left_image->columns != right_image->columns) ||
4910 (left_image->rows != right_image->rows))
4911 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4913 Initialize stereo image attributes.
4915 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4916 MagickTrue,exception);
4917 if (stereo_image == (Image *) NULL)
4918 return((Image *) NULL);
4919 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
4921 stereo_image=DestroyImage(stereo_image);
4922 return((Image *) NULL);
4924 (void) SetImageColorspace(stereo_image,sRGBColorspace,exception);
4926 Copy left image to red channel and right image to blue channel.
4929 for (y=0; y < (ssize_t) stereo_image->rows; y++)
4931 register const Quantum
4941 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4943 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4944 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
4945 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4946 (r == (Quantum *) NULL))
4948 for (x=0; x < (ssize_t) stereo_image->columns; x++)
4950 SetPixelRed(image,GetPixelRed(left_image,p),r);
4951 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4952 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4953 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4954 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4955 GetPixelAlpha(right_image,q))/2,r);
4956 p+=GetPixelChannels(left_image);
4957 q+=GetPixelChannels(right_image);
4958 r+=GetPixelChannels(stereo_image);
4960 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4962 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4967 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4968 stereo_image->rows);
4969 if (proceed == MagickFalse)
4973 if (status == MagickFalse)
4974 stereo_image=DestroyImage(stereo_image);
4975 return(stereo_image);
4979 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4983 % S w i r l I m a g e %
4987 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4989 % SwirlImage() swirls the pixels about the center of the image, where
4990 % degrees indicates the sweep of the arc through which each pixel is moved.
4991 % You get a more dramatic effect as the degrees move from 1 to 360.
4993 % The format of the SwirlImage method is:
4995 % Image *SwirlImage(const Image *image,double degrees,
4996 % const PixelInterpolateMethod method,ExceptionInfo *exception)
4998 % A description of each parameter follows:
5000 % o image: the image.
5002 % o degrees: Define the tightness of the swirling effect.
5004 % o method: the pixel interpolation method.
5006 % o exception: return any errors or warnings in this structure.
5009 MagickExport Image *SwirlImage(const Image *image,double degrees,
5010 const PixelInterpolateMethod method,ExceptionInfo *exception)
5012 #define SwirlImageTag "Swirl/Image"
5039 Initialize swirl image attributes.
5041 assert(image != (const Image *) NULL);
5042 assert(image->signature == MagickSignature);
5043 if (image->debug != MagickFalse)
5044 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5045 assert(exception != (ExceptionInfo *) NULL);
5046 assert(exception->signature == MagickSignature);
5047 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5048 if (swirl_image == (Image *) NULL)
5049 return((Image *) NULL);
5050 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
5052 swirl_image=DestroyImage(swirl_image);
5053 return((Image *) NULL);
5055 if (swirl_image->background_color.alpha != OpaqueAlpha)
5056 swirl_image->alpha_trait=BlendPixelTrait;
5058 Compute scaling factor.
5060 center.x=(double) image->columns/2.0;
5061 center.y=(double) image->rows/2.0;
5062 radius=MagickMax(center.x,center.y);
5065 if (image->columns > image->rows)
5066 scale.y=(double) image->columns/(double) image->rows;
5068 if (image->columns < image->rows)
5069 scale.x=(double) image->rows/(double) image->columns;
5070 degrees=(double) DegreesToRadians(degrees);
5076 image_view=AcquireVirtualCacheView(image,exception);
5077 interpolate_view=AcquireVirtualCacheView(image,exception);
5078 swirl_view=AcquireAuthenticCacheView(swirl_image,exception);
5079 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5080 #pragma omp parallel for schedule(static,4) shared(progress,status) \
5081 magick_threads(image,swirl_image,image->rows,1)
5083 for (y=0; y < (ssize_t) image->rows; y++)
5091 register const Quantum
5100 if (status == MagickFalse)
5102 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5103 q=QueueCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
5105 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
5110 delta.y=scale.y*(double) (y-center.y);
5111 for (x=0; x < (ssize_t) image->columns; x++)
5114 Determine if the pixel is within an ellipse.
5116 if (GetPixelReadMask(image,p) == 0)
5118 SetPixelBackgoundColor(swirl_image,q);
5119 p+=GetPixelChannels(image);
5120 q+=GetPixelChannels(swirl_image);
5123 delta.x=scale.x*(double) (x-center.x);
5124 distance=delta.x*delta.x+delta.y*delta.y;
5125 if (distance >= (radius*radius))
5130 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5132 PixelChannel channel=GetPixelChannelChannel(image,i);
5133 PixelTrait traits=GetPixelChannelTraits(image,channel);
5134 PixelTrait swirl_traits=GetPixelChannelTraits(swirl_image,channel);
5135 if ((traits == UndefinedPixelTrait) ||
5136 (swirl_traits == UndefinedPixelTrait))
5138 SetPixelChannel(swirl_image,channel,p[i],q);
5151 factor=1.0-sqrt((double) distance)/radius;
5152 sine=sin((double) (degrees*factor*factor));
5153 cosine=cos((double) (degrees*factor*factor));
5154 status=InterpolatePixelChannels(image,interpolate_view,swirl_image,
5155 method,((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5156 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
5158 p+=GetPixelChannels(image);
5159 q+=GetPixelChannels(swirl_image);
5161 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5163 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5168 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5169 #pragma omp critical (MagickCore_SwirlImage)
5171 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5172 if (proceed == MagickFalse)
5176 swirl_view=DestroyCacheView(swirl_view);
5177 interpolate_view=DestroyCacheView(interpolate_view);
5178 image_view=DestroyCacheView(image_view);
5179 if (status == MagickFalse)
5180 swirl_image=DestroyImage(swirl_image);
5181 return(swirl_image);
5185 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5189 % T i n t I m a g e %
5193 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5195 % TintImage() applies a color vector to each pixel in the image. The length
5196 % of the vector is 0 for black and white and at its maximum for the midtones.
5197 % The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5199 % The format of the TintImage method is:
5201 % Image *TintImage(const Image *image,const char *blend,
5202 % const PixelInfo *tint,ExceptionInfo *exception)
5204 % A description of each parameter follows:
5206 % o image: the image.
5208 % o blend: A color value used for tinting.
5210 % o tint: A color value used for tinting.
5212 % o exception: return any errors or warnings in this structure.
5215 MagickExport Image *TintImage(const Image *image,const char *blend,
5216 const PixelInfo *tint,ExceptionInfo *exception)
5218 #define TintImageTag "Tint/Image"
5249 Allocate tint image.
5251 assert(image != (const Image *) NULL);
5252 assert(image->signature == MagickSignature);
5253 if (image->debug != MagickFalse)
5254 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5255 assert(exception != (ExceptionInfo *) NULL);
5256 assert(exception->signature == MagickSignature);
5257 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5258 if (tint_image == (Image *) NULL)
5259 return((Image *) NULL);
5260 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
5262 tint_image=DestroyImage(tint_image);
5263 return((Image *) NULL);
5265 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
5266 (IsPixelInfoGray(tint) == MagickFalse))
5267 (void) SetImageColorspace(tint_image,sRGBColorspace,exception);
5268 if (blend == (const char *) NULL)
5271 Determine RGB values of the color.
5273 GetPixelInfo(image,&color_vector);
5274 flags=ParseGeometry(blend,&geometry_info);
5275 color_vector.red=geometry_info.rho;
5276 color_vector.green=geometry_info.rho;
5277 color_vector.blue=geometry_info.rho;
5278 color_vector.alpha=(MagickRealType) OpaqueAlpha;
5279 if ((flags & SigmaValue) != 0)
5280 color_vector.green=geometry_info.sigma;
5281 if ((flags & XiValue) != 0)
5282 color_vector.blue=geometry_info.xi;
5283 if ((flags & PsiValue) != 0)
5284 color_vector.alpha=geometry_info.psi;
5285 if (image->colorspace == CMYKColorspace)
5287 color_vector.black=geometry_info.rho;
5288 if ((flags & PsiValue) != 0)
5289 color_vector.black=geometry_info.psi;
5290 if ((flags & ChiValue) != 0)
5291 color_vector.alpha=geometry_info.chi;
5293 intensity=(double) GetPixelInfoIntensity(tint);
5294 color_vector.red=(double) (color_vector.red*tint->red/100.0-intensity);
5295 color_vector.green=(double) (color_vector.green*tint->green/100.0-intensity);
5296 color_vector.blue=(double) (color_vector.blue*tint->blue/100.0-intensity);
5297 color_vector.black=(double) (color_vector.black*tint->black/100.0-intensity);
5298 color_vector.alpha=(double) (color_vector.alpha*tint->alpha/100.0-intensity);
5304 image_view=AcquireVirtualCacheView(image,exception);
5305 tint_view=AcquireAuthenticCacheView(tint_image,exception);
5306 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5307 #pragma omp parallel for schedule(static,4) shared(progress,status) \
5308 magick_threads(image,tint_image,image->rows,1)
5310 for (y=0; y < (ssize_t) image->rows; y++)
5312 register const Quantum
5321 if (status == MagickFalse)
5323 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5324 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5326 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
5331 for (x=0; x < (ssize_t) image->columns; x++)
5342 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5344 PixelChannel channel=GetPixelChannelChannel(image,i);
5345 PixelTrait traits=GetPixelChannelTraits(image,channel);
5346 PixelTrait tint_traits=GetPixelChannelTraits(tint_image,channel);
5347 if ((traits == UndefinedPixelTrait) ||
5348 (tint_traits == UndefinedPixelTrait))
5350 if (((tint_traits & CopyPixelTrait) != 0) ||
5351 (GetPixelReadMask(image,p) == 0))
5353 SetPixelChannel(tint_image,channel,p[i],q);
5357 GetPixelInfo(image,&pixel);
5358 weight=QuantumScale*GetPixelRed(image,p)-0.5;
5359 pixel.red=(double) GetPixelRed(image,p)+color_vector.red*(1.0-(4.0*
5361 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
5362 pixel.green=(double) GetPixelGreen(image,p)+color_vector.green*(1.0-(4.0*
5364 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
5365 pixel.blue=(double) GetPixelBlue(image,p)+color_vector.blue*(1.0-(4.0*
5367 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
5368 pixel.black=(double) GetPixelBlack(image,p)+color_vector.black*(1.0-(4.0*
5370 SetPixelInfoPixel(tint_image,&pixel,q);
5371 p+=GetPixelChannels(image);
5372 q+=GetPixelChannels(tint_image);
5374 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5376 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5381 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5382 #pragma omp critical (MagickCore_TintImage)
5384 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5385 if (proceed == MagickFalse)
5389 tint_view=DestroyCacheView(tint_view);
5390 image_view=DestroyCacheView(image_view);
5391 if (status == MagickFalse)
5392 tint_image=DestroyImage(tint_image);
5397 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5401 % V i g n e t t e I m a g e %
5405 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5407 % VignetteImage() softens the edges of the image in vignette style.
5409 % The format of the VignetteImage method is:
5411 % Image *VignetteImage(const Image *image,const double radius,
5412 % const double sigma,const ssize_t x,const ssize_t y,
5413 % ExceptionInfo *exception)
5415 % A description of each parameter follows:
5417 % o image: the image.
5419 % o radius: the radius of the pixel neighborhood.
5421 % o sigma: the standard deviation of the Gaussian, in pixels.
5423 % o x, y: Define the x and y ellipse offset.
5425 % o exception: return any errors or warnings in this structure.
5428 MagickExport Image *VignetteImage(const Image *image,const double radius,
5429 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
5432 ellipse[MaxTextExtent];
5443 assert(image != (Image *) NULL);
5444 assert(image->signature == MagickSignature);
5445 if (image->debug != MagickFalse)
5446 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5447 assert(exception != (ExceptionInfo *) NULL);
5448 assert(exception->signature == MagickSignature);
5449 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5450 if (canvas_image == (Image *) NULL)
5451 return((Image *) NULL);
5452 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
5454 canvas_image=DestroyImage(canvas_image);
5455 return((Image *) NULL);
5457 canvas_image->alpha_trait=BlendPixelTrait;
5458 oval_image=CloneImage(canvas_image,canvas_image->columns,canvas_image->rows,
5459 MagickTrue,exception);
5460 if (oval_image == (Image *) NULL)
5462 canvas_image=DestroyImage(canvas_image);
5463 return((Image *) NULL);
5465 (void) QueryColorCompliance("#000000",AllCompliance,
5466 &oval_image->background_color,exception);
5467 (void) SetImageBackgroundColor(oval_image,exception);
5468 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
5469 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5471 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5473 (void) FormatLocaleString(ellipse,MaxTextExtent,"ellipse %g,%g,%g,%g,"
5474 "0.0,360.0",image->columns/2.0,image->rows/2.0,image->columns/2.0-x,
5476 draw_info->primitive=AcquireString(ellipse);
5477 (void) DrawImage(oval_image,draw_info,exception);
5478 draw_info=DestroyDrawInfo(draw_info);
5479 blur_image=BlurImage(oval_image,radius,sigma,exception);
5480 oval_image=DestroyImage(oval_image);
5481 if (blur_image == (Image *) NULL)
5483 canvas_image=DestroyImage(canvas_image);
5484 return((Image *) NULL);
5486 blur_image->alpha_trait=UndefinedPixelTrait;
5487 (void) CompositeImage(canvas_image,blur_image,IntensityCompositeOp,MagickTrue,
5489 blur_image=DestroyImage(blur_image);
5490 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5491 canvas_image=DestroyImage(canvas_image);
5492 if (vignette_image != (Image *) NULL)
5493 (void) TransformImageColorspace(vignette_image,image->colorspace,exception);
5494 return(vignette_image);
5498 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5502 % W a v e I m a g e %
5506 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5508 % WaveImage() creates a "ripple" effect in the image by shifting the pixels
5509 % vertically along a sine wave whose amplitude and wavelength is specified
5510 % by the given parameters.
5512 % The format of the WaveImage method is:
5514 % Image *WaveImage(const Image *image,const double amplitude,
5515 % const double wave_length,const PixelInterpolateMethod method,
5516 % ExceptionInfo *exception)
5518 % A description of each parameter follows:
5520 % o image: the image.
5522 % o amplitude, wave_length: Define the amplitude and wave length of the
5525 % o interpolate: the pixel interpolation method.
5527 % o exception: return any errors or warnings in this structure.
5530 MagickExport Image *WaveImage(const Image *image,const double amplitude,
5531 const double wave_length,const PixelInterpolateMethod method,
5532 ExceptionInfo *exception)
5534 #define WaveImageTag "Wave/Image"
5559 Initialize wave image attributes.
5561 assert(image != (Image *) NULL);
5562 assert(image->signature == MagickSignature);
5563 if (image->debug != MagickFalse)
5564 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5565 assert(exception != (ExceptionInfo *) NULL);
5566 assert(exception->signature == MagickSignature);
5567 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
5568 fabs(amplitude)),MagickTrue,exception);
5569 if (wave_image == (Image *) NULL)
5570 return((Image *) NULL);
5571 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
5573 wave_image=DestroyImage(wave_image);
5574 return((Image *) NULL);
5576 if (wave_image->background_color.alpha != OpaqueAlpha)
5577 wave_image->alpha_trait=BlendPixelTrait;
5581 sine_map=(double *) AcquireQuantumMemory((size_t) wave_image->columns,
5583 if (sine_map == (double *) NULL)
5585 wave_image=DestroyImage(wave_image);
5586 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5588 for (i=0; i < (ssize_t) wave_image->columns; i++)
5589 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5596 image_view=AcquireVirtualCacheView(image,exception);
5597 wave_view=AcquireAuthenticCacheView(wave_image,exception);
5598 (void) SetCacheViewVirtualPixelMethod(image_view,
5599 BackgroundVirtualPixelMethod);
5600 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5601 #pragma omp parallel for schedule(static,4) shared(progress,status) \
5602 magick_threads(image,wave_image,wave_image->rows,1)
5604 for (y=0; y < (ssize_t) wave_image->rows; y++)
5612 if (status == MagickFalse)
5614 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5616 if (q == (Quantum *) NULL)
5621 for (x=0; x < (ssize_t) wave_image->columns; x++)
5623 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5624 (double) x,(double) (y-sine_map[x]),q,exception);
5625 q+=GetPixelChannels(wave_image);
5627 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5629 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5634 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5635 #pragma omp critical (MagickCore_WaveImage)
5637 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5638 if (proceed == MagickFalse)
5642 wave_view=DestroyCacheView(wave_view);
5643 image_view=DestroyCacheView(image_view);
5644 sine_map=(double *) RelinquishMagickMemory(sine_map);
5645 if (status == MagickFalse)
5646 wave_image=DestroyImage(wave_image);