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,"j0",2) == 0) ||
1931 (LocaleNCompare(expression,"j1",2) == 0))
1940 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1947 if ((c == (int) '{') || (c == (int) '['))
1950 if ((c == (int) '}') || (c == (int) ']'))
1953 switch ((unsigned char) *expression)
1958 precedence=BitwiseComplementPrecedence;
1964 precedence=ExponentPrecedence;
1969 if (((c != 0) && ((isdigit((int) ((unsigned char) c)) != 0) ||
1970 (strchr(")",(int) ((unsigned char) c)) != (char *) NULL))) &&
1971 (((islower((int) ((unsigned char) *expression)) != 0) ||
1972 (strchr("(",(int) ((unsigned char) *expression)) != (char *) NULL)) ||
1973 ((isdigit((int) ((unsigned char) c)) == 0) &&
1974 (isdigit((int) ((unsigned char) *expression)) != 0))) &&
1975 (strchr("xy",(int) ((unsigned char) *expression)) == (char *) NULL))
1976 precedence=MultiplyPrecedence;
1983 precedence=MultiplyPrecedence;
1989 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
1991 precedence=AdditionPrecedence;
1994 case LeftShiftOperator:
1995 case RightShiftOperator:
1997 precedence=ShiftPrecedence;
2001 case LessThanEqualOperator:
2002 case GreaterThanEqualOperator:
2005 precedence=RelationalPrecedence;
2009 case NotEqualOperator:
2011 precedence=EquivalencyPrecedence;
2016 precedence=BitwiseAndPrecedence;
2021 precedence=BitwiseOrPrecedence;
2024 case LogicalAndOperator:
2026 precedence=LogicalAndPrecedence;
2029 case LogicalOrOperator:
2031 precedence=LogicalOrPrecedence;
2034 case ExponentialNotation:
2036 precedence=ExponentialNotationPrecedence;
2042 precedence=TernaryPrecedence;
2047 precedence=AssignmentPrecedence;
2052 precedence=CommaPrecedence;
2057 precedence=SeparatorPrecedence;
2061 if ((precedence == BitwiseComplementPrecedence) ||
2062 (precedence == TernaryPrecedence) ||
2063 (precedence == AssignmentPrecedence))
2065 if (precedence > target)
2068 Right-to-left associativity.
2071 subexpression=expression;
2075 if (precedence >= target)
2078 Left-to-right associativity.
2081 subexpression=expression;
2083 if (strchr("(",(int) *expression) != (char *) NULL)
2084 expression=FxSubexpression(expression,exception);
2085 c=(int) (*expression++);
2087 return(subexpression);
2090 static double FxEvaluateSubexpression(FxInfo *fx_info,
2091 const PixelChannel channel,const ssize_t x,const ssize_t y,
2092 const char *expression,double *beta,ExceptionInfo *exception)
2096 subexpression[MaxTextExtent];
2106 if (exception->severity != UndefinedException)
2108 while (isspace((int) ((unsigned char) *expression)) != 0)
2110 if (*expression == '\0')
2112 *subexpression='\0';
2113 p=FxOperatorPrecedence(expression,exception);
2114 if (p != (const char *) NULL)
2116 (void) CopyMagickString(subexpression,expression,(size_t)
2118 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2120 switch ((unsigned char) *p)
2124 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2125 *beta=(double) (~(size_t) *beta);
2130 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2131 return(*beta == 0.0 ? 1.0 : 0.0);
2135 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2136 channel,x,y,++p,beta,exception));
2140 case ExponentialNotation:
2142 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2143 return(alpha*(*beta));
2147 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2150 if (exception->severity == UndefinedException)
2151 (void) ThrowMagickException(exception,GetMagickModule(),
2152 OptionError,"DivideByZero","`%s'",expression);
2155 return(alpha/(*beta));
2159 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2160 *beta=fabs(floor(((double) *beta)+0.5));
2163 (void) ThrowMagickException(exception,GetMagickModule(),
2164 OptionError,"DivideByZero","`%s'",expression);
2167 return(fmod((double) alpha,(double) *beta));
2171 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2172 return(alpha+(*beta));
2176 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2177 return(alpha-(*beta));
2179 case LeftShiftOperator:
2181 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2182 *beta=(double) ((size_t) (alpha+0.5) << (size_t) (gamma+0.5));
2185 case RightShiftOperator:
2187 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2188 *beta=(double) ((size_t) (alpha+0.5) >> (size_t) (gamma+0.5));
2193 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2194 return(alpha < *beta ? 1.0 : 0.0);
2196 case LessThanEqualOperator:
2198 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2199 return(alpha <= *beta ? 1.0 : 0.0);
2203 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2204 return(alpha > *beta ? 1.0 : 0.0);
2206 case GreaterThanEqualOperator:
2208 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2209 return(alpha >= *beta ? 1.0 : 0.0);
2213 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2214 return(fabs(alpha-(*beta)) < MagickEpsilon ? 1.0 : 0.0);
2216 case NotEqualOperator:
2218 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2219 return(fabs(alpha-(*beta)) >= MagickEpsilon ? 1.0 : 0.0);
2223 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2224 *beta=(double) ((size_t) (alpha+0.5) & (size_t) (gamma+0.5));
2229 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2230 *beta=(double) ((size_t) (alpha+0.5) | (size_t) (gamma+0.5));
2233 case LogicalAndOperator:
2235 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2236 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2239 case LogicalOrOperator:
2241 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2242 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2250 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2252 p=StringToken(":",&q);
2253 if (q == (char *) NULL)
2255 (void) ThrowMagickException(exception,GetMagickModule(),
2256 OptionError,"UnableToParseExpression","`%s'",subexpression);
2259 if (fabs((double) alpha) >= MagickEpsilon)
2260 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2262 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2268 numeric[MaxTextExtent];
2271 while (isalpha((int) ((unsigned char) *q)) != 0)
2275 (void) ThrowMagickException(exception,GetMagickModule(),
2276 OptionError,"UnableToParseExpression","`%s'",subexpression);
2279 ClearMagickException(exception);
2280 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2281 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
2283 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2284 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2285 subexpression),ConstantString(numeric));
2290 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2295 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2300 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2306 if (strchr("(",(int) *expression) != (char *) NULL)
2308 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2309 subexpression[strlen(subexpression)-1]='\0';
2310 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2314 switch (*expression)
2318 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2324 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2330 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2332 return((double) (~(size_t) (gamma+0.5)));
2337 if (LocaleNCompare(expression,"abs",3) == 0)
2339 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2341 return((double) fabs((double) alpha));
2343 #if defined(MAGICKCORE_HAVE_ACOSH)
2344 if (LocaleNCompare(expression,"acosh",5) == 0)
2346 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2348 return((double) acosh((double) alpha));
2351 if (LocaleNCompare(expression,"acos",4) == 0)
2353 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2355 return((double) acos((double) alpha));
2357 #if defined(MAGICKCORE_HAVE_J1)
2358 if (LocaleNCompare(expression,"airy",4) == 0)
2360 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2364 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
2365 return(gamma*gamma);
2368 #if defined(MAGICKCORE_HAVE_ASINH)
2369 if (LocaleNCompare(expression,"asinh",5) == 0)
2371 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2373 return((double) asinh((double) alpha));
2376 if (LocaleNCompare(expression,"asin",4) == 0)
2378 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2380 return((double) asin((double) alpha));
2382 if (LocaleNCompare(expression,"alt",3) == 0)
2384 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2386 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
2388 if (LocaleNCompare(expression,"atan2",5) == 0)
2390 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2392 return((double) atan2((double) alpha,(double) *beta));
2394 #if defined(MAGICKCORE_HAVE_ATANH)
2395 if (LocaleNCompare(expression,"atanh",5) == 0)
2397 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2399 return((double) atanh((double) alpha));
2402 if (LocaleNCompare(expression,"atan",4) == 0)
2404 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2406 return((double) atan((double) alpha));
2408 if (LocaleCompare(expression,"a") == 0)
2409 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2415 if (LocaleCompare(expression,"b") == 0)
2416 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2422 if (LocaleNCompare(expression,"ceil",4) == 0)
2424 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2426 return((double) ceil((double) alpha));
2428 if (LocaleNCompare(expression,"cosh",4) == 0)
2430 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2432 return((double) cosh((double) alpha));
2434 if (LocaleNCompare(expression,"cos",3) == 0)
2436 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2438 return((double) cos((double) alpha));
2440 if (LocaleCompare(expression,"c") == 0)
2441 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2447 if (LocaleNCompare(expression,"debug",5) == 0)
2452 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2454 if (fx_info->images->colorspace == CMYKColorspace)
2457 case CyanPixelChannel: type="cyan"; break;
2458 case MagentaPixelChannel: type="magenta"; break;
2459 case YellowPixelChannel: type="yellow"; break;
2460 case AlphaPixelChannel: type="opacity"; break;
2461 case BlackPixelChannel: type="black"; break;
2462 default: type="unknown"; break;
2467 case RedPixelChannel: type="red"; break;
2468 case GreenPixelChannel: type="green"; break;
2469 case BluePixelChannel: type="blue"; break;
2470 case AlphaPixelChannel: type="opacity"; break;
2471 default: type="unknown"; break;
2473 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2474 if (strlen(subexpression) > 1)
2475 subexpression[strlen(subexpression)-1]='\0';
2476 if (fx_info->file != (FILE *) NULL)
2477 (void) FormatLocaleFile(fx_info->file,"%s[%.20g,%.20g].%s: "
2478 "%s=%.*g\n",fx_info->images->filename,(double) x,(double) y,type,
2479 subexpression,GetMagickPrecision(),(double) alpha);
2482 if (LocaleNCompare(expression,"drc",3) == 0)
2484 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2486 return((double) (alpha/(*beta*(alpha-1.0)+1.0)));
2493 if (LocaleCompare(expression,"epsilon") == 0)
2494 return((double) MagickEpsilon);
2495 if (LocaleNCompare(expression,"exp",3) == 0)
2497 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2499 return((double) exp((double) alpha));
2501 if (LocaleCompare(expression,"e") == 0)
2502 return((double) 2.7182818284590452354);
2508 if (LocaleNCompare(expression,"floor",5) == 0)
2510 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2512 return((double) floor((double) alpha));
2519 if (LocaleNCompare(expression,"gauss",5) == 0)
2521 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2523 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
2524 return((double) gamma);
2526 if (LocaleNCompare(expression,"gcd",3) == 0)
2531 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2533 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType) (*beta+
2535 return((double) gcd);
2537 if (LocaleCompare(expression,"g") == 0)
2538 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2544 if (LocaleCompare(expression,"h") == 0)
2545 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2546 if (LocaleCompare(expression,"hue") == 0)
2547 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2548 if (LocaleNCompare(expression,"hypot",5) == 0)
2550 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2552 return((double) hypot((double) alpha,(double) *beta));
2559 if (LocaleCompare(expression,"k") == 0)
2560 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2566 if (LocaleCompare(expression,"intensity") == 0)
2567 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2568 if (LocaleNCompare(expression,"int",3) == 0)
2570 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2572 return((double) floor(alpha));
2574 #if defined(MAGICKCORE_HAVE_ISNAN)
2575 if (LocaleNCompare(expression,"isnan",5) == 0)
2577 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2579 return((double) !!isnan((double) alpha));
2582 if (LocaleCompare(expression,"i") == 0)
2583 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2589 if (LocaleCompare(expression,"j") == 0)
2590 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2591 #if defined(MAGICKCORE_HAVE_J0)
2592 if (LocaleNCompare(expression,"j0",2) == 0)
2594 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2596 return((double) j0((double) alpha));
2599 #if defined(MAGICKCORE_HAVE_J1)
2600 if (LocaleNCompare(expression,"j1",2) == 0)
2602 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2604 return((double) j1((double) alpha));
2607 #if defined(MAGICKCORE_HAVE_J1)
2608 if (LocaleNCompare(expression,"jinc",4) == 0)
2610 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2614 gamma=(double) (2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha));
2623 if (LocaleNCompare(expression,"ln",2) == 0)
2625 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2627 return((double) log((double) alpha));
2629 if (LocaleNCompare(expression,"logtwo",6) == 0)
2631 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2633 return((double) log10((double) alpha))/log10(2.0);
2635 if (LocaleNCompare(expression,"log",3) == 0)
2637 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2639 return((double) log10((double) alpha));
2641 if (LocaleCompare(expression,"lightness") == 0)
2642 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2648 if (LocaleCompare(expression,"MaxRGB") == 0)
2649 return((double) QuantumRange);
2650 if (LocaleNCompare(expression,"maxima",6) == 0)
2652 if (LocaleNCompare(expression,"max",3) == 0)
2654 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2656 return(alpha > *beta ? alpha : *beta);
2658 if (LocaleNCompare(expression,"minima",6) == 0)
2660 if (LocaleNCompare(expression,"min",3) == 0)
2662 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2664 return(alpha < *beta ? alpha : *beta);
2666 if (LocaleNCompare(expression,"mod",3) == 0)
2668 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2670 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2673 if (LocaleCompare(expression,"m") == 0)
2674 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2680 if (LocaleNCompare(expression,"not",3) == 0)
2682 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2684 return((double) (alpha < MagickEpsilon));
2686 if (LocaleCompare(expression,"n") == 0)
2687 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2693 if (LocaleCompare(expression,"Opaque") == 0)
2695 if (LocaleCompare(expression,"o") == 0)
2696 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2702 if (LocaleCompare(expression,"phi") == 0)
2703 return((double) MagickPHI);
2704 if (LocaleCompare(expression,"pi") == 0)
2705 return((double) MagickPI);
2706 if (LocaleNCompare(expression,"pow",3) == 0)
2708 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2710 return((double) pow((double) alpha,(double) *beta));
2712 if (LocaleCompare(expression,"p") == 0)
2713 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2719 if (LocaleCompare(expression,"QuantumRange") == 0)
2720 return((double) QuantumRange);
2721 if (LocaleCompare(expression,"QuantumScale") == 0)
2722 return((double) QuantumScale);
2728 if (LocaleNCompare(expression,"rand",4) == 0)
2729 return((double) GetPseudoRandomValue(fx_info->random_info));
2730 if (LocaleNCompare(expression,"round",5) == 0)
2732 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2734 return((double) floor((double) alpha+0.5));
2736 if (LocaleCompare(expression,"r") == 0)
2737 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2743 if (LocaleCompare(expression,"saturation") == 0)
2744 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2745 if (LocaleNCompare(expression,"sign",4) == 0)
2747 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2749 return(alpha < 0.0 ? -1.0 : 1.0);
2751 if (LocaleNCompare(expression,"sinc",4) == 0)
2753 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2757 gamma=(double) (sin((double) (MagickPI*alpha))/
2761 if (LocaleNCompare(expression,"sinh",4) == 0)
2763 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2765 return((double) sinh((double) alpha));
2767 if (LocaleNCompare(expression,"sin",3) == 0)
2769 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2771 return((double) sin((double) alpha));
2773 if (LocaleNCompare(expression,"sqrt",4) == 0)
2775 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2777 return((double) sqrt((double) alpha));
2779 if (LocaleNCompare(expression,"squish",6) == 0)
2781 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2783 return((double) (1.0/(1.0+exp((double) (-alpha)))));
2785 if (LocaleCompare(expression,"s") == 0)
2786 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2792 if (LocaleNCompare(expression,"tanh",4) == 0)
2794 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2796 return((double) tanh((double) alpha));
2798 if (LocaleNCompare(expression,"tan",3) == 0)
2800 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2802 return((double) tan((double) alpha));
2804 if (LocaleCompare(expression,"Transparent") == 0)
2806 if (LocaleNCompare(expression,"trunc",5) == 0)
2808 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2811 return((double) floor((double) alpha));
2812 return((double) ceil((double) alpha));
2814 if (LocaleCompare(expression,"t") == 0)
2815 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2821 if (LocaleCompare(expression,"u") == 0)
2822 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2828 if (LocaleCompare(expression,"v") == 0)
2829 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2835 if (LocaleNCompare(expression,"while",5) == 0)
2839 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2841 } while (fabs((double) alpha) >= MagickEpsilon);
2842 return((double) *beta);
2844 if (LocaleCompare(expression,"w") == 0)
2845 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2851 if (LocaleCompare(expression,"y") == 0)
2852 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2858 if (LocaleCompare(expression,"z") == 0)
2859 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2865 q=(char *) expression;
2866 alpha=InterpretSiPrefixValue(expression,&q);
2867 if (q == expression)
2868 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2872 MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
2873 double *alpha,ExceptionInfo *exception)
2878 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2883 MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
2884 double *alpha,ExceptionInfo *exception)
2893 fx_info->file=(FILE *) NULL;
2894 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2900 MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
2901 const PixelChannel channel,const ssize_t x,const ssize_t y,
2902 double *alpha,ExceptionInfo *exception)
2908 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2910 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2914 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2922 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2924 % FxImage() applies a mathematical expression to the specified image.
2926 % The format of the FxImage method is:
2928 % Image *FxImage(const Image *image,const char *expression,
2929 % ExceptionInfo *exception)
2931 % A description of each parameter follows:
2933 % o image: the image.
2935 % o expression: A mathematical expression.
2937 % o exception: return any errors or warnings in this structure.
2941 static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2946 assert(fx_info != (FxInfo **) NULL);
2947 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
2948 if (fx_info[i] != (FxInfo *) NULL)
2949 fx_info[i]=DestroyFxInfo(fx_info[i]);
2950 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
2954 static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2955 ExceptionInfo *exception)
2972 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
2973 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
2974 if (fx_info == (FxInfo **) NULL)
2976 (void) ThrowMagickException(exception,GetMagickModule(),
2977 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
2978 return((FxInfo **) NULL);
2980 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2981 if (*expression != '@')
2982 fx_expression=ConstantString(expression);
2984 fx_expression=FileToString(expression+1,~0UL,exception);
2985 for (i=0; i < (ssize_t) number_threads; i++)
2990 fx_info[i]=AcquireFxInfo(image,fx_expression,exception);
2991 if (fx_info[i] == (FxInfo *) NULL)
2993 status=FxPreprocessExpression(fx_info[i],&alpha,exception);
2994 if (status == MagickFalse)
2997 fx_expression=DestroyString(fx_expression);
2998 if (i < (ssize_t) number_threads)
2999 fx_info=DestroyFxThreadSet(fx_info);
3003 MagickExport Image *FxImage(const Image *image,const char *expression,
3004 ExceptionInfo *exception)
3006 #define FxImageTag "Fx/Image"
3027 assert(image != (Image *) NULL);
3028 assert(image->signature == MagickSignature);
3029 if (image->debug != MagickFalse)
3030 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3031 fx_info=AcquireFxThreadSet(image,expression,exception);
3032 if (fx_info == (FxInfo **) NULL)
3033 return((Image *) NULL);
3034 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3035 if (fx_image == (Image *) NULL)
3037 fx_info=DestroyFxThreadSet(fx_info);
3038 return((Image *) NULL);
3040 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
3042 fx_info=DestroyFxThreadSet(fx_info);
3043 fx_image=DestroyImage(fx_image);
3044 return((Image *) NULL);
3051 image_view=AcquireVirtualCacheView(image,exception);
3052 fx_view=AcquireAuthenticCacheView(fx_image,exception);
3053 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3054 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3055 magick_threads(image,fx_image,fx_image->rows,1)
3057 for (y=0; y < (ssize_t) fx_image->rows; y++)
3060 id = GetOpenMPThreadId();
3062 register const Quantum
3071 if (status == MagickFalse)
3073 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3074 q=QueueCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
3075 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3080 for (x=0; x < (ssize_t) fx_image->columns; x++)
3085 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3090 PixelChannel channel=GetPixelChannelChannel(image,i);
3091 PixelTrait traits=GetPixelChannelTraits(image,channel);
3092 PixelTrait fx_traits=GetPixelChannelTraits(fx_image,channel);
3093 if ((traits == UndefinedPixelTrait) ||
3094 (fx_traits == UndefinedPixelTrait))
3096 if (((fx_traits & CopyPixelTrait) != 0) ||
3097 (GetPixelReadMask(image,p) == 0))
3099 SetPixelChannel(fx_image,channel,p[i],q);
3103 (void) FxEvaluateChannelExpression(fx_info[id],channel,x,y,&alpha,
3105 q[i]=ClampToQuantum(QuantumRange*alpha);
3107 p+=GetPixelChannels(image);
3108 q+=GetPixelChannels(fx_image);
3110 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3112 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3117 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3118 #pragma omp critical (MagickCore_FxImage)
3120 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3121 if (proceed == MagickFalse)
3125 fx_view=DestroyCacheView(fx_view);
3126 image_view=DestroyCacheView(image_view);
3127 fx_info=DestroyFxThreadSet(fx_info);
3128 if (status == MagickFalse)
3129 fx_image=DestroyImage(fx_image);
3134 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3138 % I m p l o d e I m a g e %
3142 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3144 % ImplodeImage() creates a new image that is a copy of an existing
3145 % one with the image pixels "implode" by the specified percentage. It
3146 % allocates the memory necessary for the new Image structure and returns a
3147 % pointer to the new image.
3149 % The format of the ImplodeImage method is:
3151 % Image *ImplodeImage(const Image *image,const double amount,
3152 % const PixelInterpolateMethod method,ExceptionInfo *exception)
3154 % A description of each parameter follows:
3156 % o implode_image: Method ImplodeImage returns a pointer to the image
3157 % after it is implode. A null image is returned if there is a memory
3160 % o image: the image.
3162 % o amount: Define the extent of the implosion.
3164 % o method: the pixel interpolation method.
3166 % o exception: return any errors or warnings in this structure.
3169 MagickExport Image *ImplodeImage(const Image *image,const double amount,
3170 const PixelInterpolateMethod method,ExceptionInfo *exception)
3172 #define ImplodeImageTag "Implode/Image"
3199 Initialize implode image attributes.
3201 assert(image != (Image *) NULL);
3202 assert(image->signature == MagickSignature);
3203 if (image->debug != MagickFalse)
3204 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3205 assert(exception != (ExceptionInfo *) NULL);
3206 assert(exception->signature == MagickSignature);
3207 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3209 if (implode_image == (Image *) NULL)
3210 return((Image *) NULL);
3211 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
3213 implode_image=DestroyImage(implode_image);
3214 return((Image *) NULL);
3216 if (implode_image->background_color.alpha != OpaqueAlpha)
3217 implode_image->alpha_trait=BlendPixelTrait;
3219 Compute scaling factor.
3223 center.x=0.5*image->columns;
3224 center.y=0.5*image->rows;
3226 if (image->columns > image->rows)
3227 scale.y=(double) image->columns/(double) image->rows;
3229 if (image->columns < image->rows)
3231 scale.x=(double) image->rows/(double) image->columns;
3239 image_view=AcquireVirtualCacheView(image,exception);
3240 interpolate_view=AcquireVirtualCacheView(image,exception);
3241 implode_view=AcquireAuthenticCacheView(implode_image,exception);
3242 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3243 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3244 magick_threads(image,implode_image,image->rows,1)
3246 for (y=0; y < (ssize_t) image->rows; y++)
3254 register const Quantum
3263 if (status == MagickFalse)
3265 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3266 q=QueueCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
3268 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3273 delta.y=scale.y*(double) (y-center.y);
3274 for (x=0; x < (ssize_t) image->columns; x++)
3280 Determine if the pixel is within an ellipse.
3282 if (GetPixelReadMask(image,p) == 0)
3284 SetPixelBackgoundColor(implode_image,q);
3285 p+=GetPixelChannels(image);
3286 q+=GetPixelChannels(implode_image);
3289 delta.x=scale.x*(double) (x-center.x);
3290 distance=delta.x*delta.x+delta.y*delta.y;
3291 if (distance >= (radius*radius))
3292 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3294 PixelChannel channel=GetPixelChannelChannel(image,i);
3295 PixelTrait traits=GetPixelChannelTraits(image,channel);
3296 PixelTrait implode_traits=GetPixelChannelTraits(implode_image,
3298 if ((traits == UndefinedPixelTrait) ||
3299 (implode_traits == UndefinedPixelTrait))
3301 SetPixelChannel(implode_image,channel,p[i],q);
3313 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/radius/
3315 status=InterpolatePixelChannels(image,interpolate_view,implode_image,
3316 method,(double) (factor*delta.x/scale.x+center.x),(double) (factor*
3317 delta.y/scale.y+center.y),q,exception);
3319 p+=GetPixelChannels(image);
3320 q+=GetPixelChannels(implode_image);
3322 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3324 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3329 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3330 #pragma omp critical (MagickCore_ImplodeImage)
3332 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3333 if (proceed == MagickFalse)
3337 implode_view=DestroyCacheView(implode_view);
3338 interpolate_view=DestroyCacheView(interpolate_view);
3339 image_view=DestroyCacheView(image_view);
3340 if (status == MagickFalse)
3341 implode_image=DestroyImage(implode_image);
3342 return(implode_image);
3346 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3350 % M o r p h I m a g e s %
3354 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3356 % The MorphImages() method requires a minimum of two images. The first
3357 % image is transformed into the second by a number of intervening images
3358 % as specified by frames.
3360 % The format of the MorphImage method is:
3362 % Image *MorphImages(const Image *image,const size_t number_frames,
3363 % ExceptionInfo *exception)
3365 % A description of each parameter follows:
3367 % o image: the image.
3369 % o number_frames: Define the number of in-between image to generate.
3370 % The more in-between frames, the smoother the morph.
3372 % o exception: return any errors or warnings in this structure.
3375 MagickExport Image *MorphImages(const Image *image,const size_t number_frames,
3376 ExceptionInfo *exception)
3378 #define MorphImageTag "Morph/Image"
3394 register const Image
3404 Clone first frame in sequence.
3406 assert(image != (Image *) NULL);
3407 assert(image->signature == MagickSignature);
3408 if (image->debug != MagickFalse)
3409 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3410 assert(exception != (ExceptionInfo *) NULL);
3411 assert(exception->signature == MagickSignature);
3412 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3413 if (morph_images == (Image *) NULL)
3414 return((Image *) NULL);
3415 if (GetNextImageInList(image) == (Image *) NULL)
3420 for (i=1; i < (ssize_t) number_frames; i++)
3422 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3423 if (morph_image == (Image *) NULL)
3425 morph_images=DestroyImageList(morph_images);
3426 return((Image *) NULL);
3428 AppendImageToList(&morph_images,morph_image);
3429 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3434 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3436 if (proceed == MagickFalse)
3440 return(GetFirstImageInList(morph_images));
3443 Morph image sequence.
3448 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3450 for (i=0; i < (ssize_t) number_frames; i++)
3456 beta=(double) (i+1.0)/(double) (number_frames+1.0);
3458 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
3459 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*next->rows+beta*
3460 GetNextImageInList(next)->rows+0.5),next->filter,exception);
3461 if (morph_image == (Image *) NULL)
3463 morph_images=DestroyImageList(morph_images);
3464 return((Image *) NULL);
3466 status=SetImageStorageClass(morph_image,DirectClass,exception);
3467 if (status == MagickFalse)
3469 morph_image=DestroyImage(morph_image);
3470 return((Image *) NULL);
3472 AppendImageToList(&morph_images,morph_image);
3473 morph_images=GetLastImageInList(morph_images);
3474 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
3475 morph_images->rows,GetNextImageInList(next)->filter,exception);
3476 if (morph_image == (Image *) NULL)
3478 morph_images=DestroyImageList(morph_images);
3479 return((Image *) NULL);
3481 image_view=AcquireVirtualCacheView(morph_image,exception);
3482 morph_view=AcquireAuthenticCacheView(morph_images,exception);
3483 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3484 #pragma omp parallel for schedule(static,4) shared(status) \
3485 magick_threads(morph_image,morph_image,morph_image->rows,1)
3487 for (y=0; y < (ssize_t) morph_images->rows; y++)
3492 register const Quantum
3501 if (status == MagickFalse)
3503 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3505 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3507 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3512 for (x=0; x < (ssize_t) morph_images->columns; x++)
3517 for (i=0; i < (ssize_t) GetPixelChannels(morph_image); i++)
3519 PixelChannel channel=GetPixelChannelChannel(morph_image,i);
3520 PixelTrait traits=GetPixelChannelTraits(morph_image,channel);
3521 PixelTrait morph_traits=GetPixelChannelTraits(morph_images,channel);
3522 if ((traits == UndefinedPixelTrait) ||
3523 (morph_traits == UndefinedPixelTrait))
3525 if (((morph_traits & CopyPixelTrait) != 0) ||
3526 (GetPixelReadMask(morph_images,p) == 0))
3528 SetPixelChannel(morph_image,channel,p[i],q);
3531 SetPixelChannel(morph_image,channel,ClampToQuantum(alpha*
3532 GetPixelChannel(morph_images,channel,q)+beta*p[i]),q);
3534 p+=GetPixelChannels(morph_image);
3535 q+=GetPixelChannels(morph_images);
3537 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3538 if (sync == MagickFalse)
3541 morph_view=DestroyCacheView(morph_view);
3542 image_view=DestroyCacheView(image_view);
3543 morph_image=DestroyImage(morph_image);
3545 if (i < (ssize_t) number_frames)
3548 Clone last frame in sequence.
3550 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3551 if (morph_image == (Image *) NULL)
3553 morph_images=DestroyImageList(morph_images);
3554 return((Image *) NULL);
3556 AppendImageToList(&morph_images,morph_image);
3557 morph_images=GetLastImageInList(morph_images);
3558 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3563 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3564 #pragma omp critical (MagickCore_MorphImages)
3566 proceed=SetImageProgress(image,MorphImageTag,scene,
3567 GetImageListLength(image));
3568 if (proceed == MagickFalse)
3573 if (GetNextImageInList(next) != (Image *) NULL)
3575 morph_images=DestroyImageList(morph_images);
3576 return((Image *) NULL);
3578 return(GetFirstImageInList(morph_images));
3582 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3586 % P l a s m a I m a g e %
3590 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3592 % PlasmaImage() initializes an image with plasma fractal values. The image
3593 % must be initialized with a base color and the random number generator
3594 % seeded before this method is called.
3596 % The format of the PlasmaImage method is:
3598 % MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
3599 % size_t attenuate,size_t depth,ExceptionInfo *exception)
3601 % A description of each parameter follows:
3603 % o image: the image.
3605 % o segment: Define the region to apply plasma fractals values.
3607 % o attenuate: Define the plasma attenuation factor.
3609 % o depth: Limit the plasma recursion depth.
3611 % o exception: return any errors or warnings in this structure.
3615 static inline Quantum PlasmaPixel(RandomInfo *random_info,
3616 const double pixel,const double noise)
3621 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
3626 static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3627 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3628 const SegmentInfo *segment,size_t attenuate,size_t depth,
3629 ExceptionInfo *exception)
3634 register const Quantum
3650 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3658 Divide the area into quadrants and recurse.
3662 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3663 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
3664 local_info=(*segment);
3665 local_info.x2=(double) x_mid;
3666 local_info.y2=(double) y_mid;
3667 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3668 &local_info,attenuate,depth,exception);
3669 local_info=(*segment);
3670 local_info.y1=(double) y_mid;
3671 local_info.x2=(double) x_mid;
3672 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3673 &local_info,attenuate,depth,exception);
3674 local_info=(*segment);
3675 local_info.x1=(double) x_mid;
3676 local_info.y2=(double) y_mid;
3677 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3678 &local_info,attenuate,depth,exception);
3679 local_info=(*segment);
3680 local_info.x1=(double) x_mid;
3681 local_info.y1=(double) y_mid;
3682 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3683 &local_info,attenuate,depth,exception));
3685 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3686 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
3687 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3688 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3689 return(MagickFalse);
3691 Average pixels and apply plasma.
3693 plasma=(double) QuantumRange/(2.0*attenuate);
3694 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3699 x=(ssize_t) ceil(segment->x1-0.5);
3700 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),1,1,
3702 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),1,1,
3704 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
3705 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3706 (q == (Quantum *) NULL))
3708 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3710 PixelChannel channel=GetPixelChannelChannel(image,i);
3711 PixelTrait traits=GetPixelChannelTraits(image,channel);
3712 if (traits == UndefinedPixelTrait)
3714 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
3716 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3717 if (segment->x1 != segment->x2)
3722 x=(ssize_t) ceil(segment->x2-0.5);
3723 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3725 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3727 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
3728 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3729 (q == (Quantum *) NULL))
3731 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3733 PixelChannel channel=GetPixelChannelChannel(image,i);
3734 PixelTrait traits=GetPixelChannelTraits(image,channel);
3735 if (traits == UndefinedPixelTrait)
3737 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
3739 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3742 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3744 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3749 y=(ssize_t) ceil(segment->y2-0.5);
3750 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3752 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3754 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
3755 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3756 (q == (Quantum *) NULL))
3758 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3760 PixelChannel channel=GetPixelChannelChannel(image,i);
3761 PixelTrait traits=GetPixelChannelTraits(image,channel);
3762 if (traits == UndefinedPixelTrait)
3764 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
3766 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3768 if (segment->y1 != segment->y2)
3773 y=(ssize_t) ceil(segment->y1-0.5);
3774 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3776 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3778 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
3779 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3780 (q == (Quantum *) NULL))
3782 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3784 PixelChannel channel=GetPixelChannelChannel(image,i);
3785 PixelTrait traits=GetPixelChannelTraits(image,channel);
3786 if (traits == UndefinedPixelTrait)
3788 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
3790 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3793 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3798 x=(ssize_t) ceil(segment->x1-0.5);
3799 y=(ssize_t) ceil(segment->y1-0.5);
3800 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
3801 x=(ssize_t) ceil(segment->x2-0.5);
3802 y=(ssize_t) ceil(segment->y2-0.5);
3803 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
3804 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
3805 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3806 (q == (Quantum *) NULL))
3808 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3810 PixelChannel channel=GetPixelChannelChannel(image,i);
3811 PixelTrait traits=GetPixelChannelTraits(image,channel);
3812 if (traits == UndefinedPixelTrait)
3814 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
3816 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3818 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3820 return(MagickFalse);
3823 MagickExport MagickBooleanType PlasmaImage(Image *image,
3824 const SegmentInfo *segment,size_t attenuate,size_t depth,
3825 ExceptionInfo *exception)
3838 if (image->debug != MagickFalse)
3839 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3840 assert(image != (Image *) NULL);
3841 assert(image->signature == MagickSignature);
3842 if (image->debug != MagickFalse)
3843 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3844 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
3845 return(MagickFalse);
3846 image_view=AcquireAuthenticCacheView(image,exception);
3847 u_view=AcquireVirtualCacheView(image,exception);
3848 v_view=AcquireVirtualCacheView(image,exception);
3849 random_info=AcquireRandomInfo();
3850 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3851 attenuate,depth,exception);
3852 random_info=DestroyRandomInfo(random_info);
3853 v_view=DestroyCacheView(v_view);
3854 u_view=DestroyCacheView(u_view);
3855 image_view=DestroyCacheView(image_view);
3860 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3864 % P o l a r o i d I m a g e %
3868 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3870 % PolaroidImage() simulates a Polaroid picture.
3872 % The format of the AnnotateImage method is:
3874 % Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
3875 % const char *caption,const double angle,
3876 % const PixelInterpolateMethod method,ExceptionInfo exception)
3878 % A description of each parameter follows:
3880 % o image: the image.
3882 % o draw_info: the draw info.
3884 % o caption: the Polaroid caption.
3886 % o angle: Apply the effect along this angle.
3888 % o method: the pixel interpolation method.
3890 % o exception: return any errors or warnings in this structure.
3893 MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
3894 const char *caption,const double angle,const PixelInterpolateMethod method,
3895 ExceptionInfo *exception)
3913 Simulate a Polaroid picture.
3915 assert(image != (Image *) NULL);
3916 assert(image->signature == MagickSignature);
3917 if (image->debug != MagickFalse)
3918 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3919 assert(exception != (ExceptionInfo *) NULL);
3920 assert(exception->signature == MagickSignature);
3921 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
3922 image->rows)/25.0,10.0);
3923 height=image->rows+2*quantum;
3924 caption_image=(Image *) NULL;
3925 if (caption != (const char *) NULL)
3928 geometry[MaxTextExtent],
3947 Generate caption image.
3949 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3950 if (caption_image == (Image *) NULL)
3951 return((Image *) NULL);
3952 image_info=AcquireImageInfo();
3953 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
3954 text=InterpretImageProperties(image_info,(Image *) image,caption,
3956 image_info=DestroyImageInfo(image_info);
3957 (void) CloneString(&annotate_info->text,text);
3958 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
3960 status=SetImageExtent(caption_image,image->columns,(size_t) ((count+1)*
3961 (metrics.ascent-metrics.descent)+0.5),exception);
3962 if (status == MagickFalse)
3963 caption_image=DestroyImage(caption_image);
3966 caption_image->background_color=image->border_color;
3967 (void) SetImageBackgroundColor(caption_image,exception);
3968 (void) CloneString(&annotate_info->text,text);
3969 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
3971 if (annotate_info->gravity == UndefinedGravity)
3972 (void) CloneString(&annotate_info->geometry,AcquireString(
3974 (void) AnnotateImage(caption_image,annotate_info,exception);
3975 height+=caption_image->rows;
3977 annotate_info=DestroyDrawInfo(annotate_info);
3978 text=DestroyString(text);
3980 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
3982 if (picture_image == (Image *) NULL)
3984 if (caption_image != (Image *) NULL)
3985 caption_image=DestroyImage(caption_image);
3986 return((Image *) NULL);
3988 picture_image->background_color=image->border_color;
3989 (void) SetImageBackgroundColor(picture_image,exception);
3990 (void) CompositeImage(picture_image,image,OverCompositeOp,MagickTrue,quantum,
3992 if (caption_image != (Image *) NULL)
3994 (void) CompositeImage(picture_image,caption_image,OverCompositeOp,
3995 MagickTrue,quantum,(ssize_t) (image->rows+3*quantum/2),exception);
3996 caption_image=DestroyImage(caption_image);
3998 (void) QueryColorCompliance("none",AllCompliance,
3999 &picture_image->background_color,exception);
4000 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
4001 rotate_image=RotateImage(picture_image,90.0,exception);
4002 picture_image=DestroyImage(picture_image);
4003 if (rotate_image == (Image *) NULL)
4004 return((Image *) NULL);
4005 picture_image=rotate_image;
4006 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
4007 picture_image->columns,method,exception);
4008 picture_image=DestroyImage(picture_image);
4009 if (bend_image == (Image *) NULL)
4010 return((Image *) NULL);
4011 picture_image=bend_image;
4012 rotate_image=RotateImage(picture_image,-90.0,exception);
4013 picture_image=DestroyImage(picture_image);
4014 if (rotate_image == (Image *) NULL)
4015 return((Image *) NULL);
4016 picture_image=rotate_image;
4017 picture_image->background_color=image->background_color;
4018 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
4020 if (polaroid_image == (Image *) NULL)
4022 picture_image=DestroyImage(picture_image);
4023 return(picture_image);
4025 flop_image=FlopImage(polaroid_image,exception);
4026 polaroid_image=DestroyImage(polaroid_image);
4027 if (flop_image == (Image *) NULL)
4029 picture_image=DestroyImage(picture_image);
4030 return(picture_image);
4032 polaroid_image=flop_image;
4033 (void) CompositeImage(polaroid_image,picture_image,OverCompositeOp,
4034 MagickTrue,(ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
4035 picture_image=DestroyImage(picture_image);
4036 (void) QueryColorCompliance("none",AllCompliance,
4037 &polaroid_image->background_color,exception);
4038 rotate_image=RotateImage(polaroid_image,angle,exception);
4039 polaroid_image=DestroyImage(polaroid_image);
4040 if (rotate_image == (Image *) NULL)
4041 return((Image *) NULL);
4042 polaroid_image=rotate_image;
4043 trim_image=TrimImage(polaroid_image,exception);
4044 polaroid_image=DestroyImage(polaroid_image);
4045 if (trim_image == (Image *) NULL)
4046 return((Image *) NULL);
4047 polaroid_image=trim_image;
4048 return(polaroid_image);
4052 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4056 % S e p i a T o n e I m a g e %
4060 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4062 % MagickSepiaToneImage() applies a special effect to the image, similar to the
4063 % effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4064 % 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4065 % threshold of 80% is a good starting point for a reasonable tone.
4067 % The format of the SepiaToneImage method is:
4069 % Image *SepiaToneImage(const Image *image,const double threshold,
4070 % ExceptionInfo *exception)
4072 % A description of each parameter follows:
4074 % o image: the image.
4076 % o threshold: the tone threshold.
4078 % o exception: return any errors or warnings in this structure.
4081 MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4082 ExceptionInfo *exception)
4084 #define SepiaToneImageTag "SepiaTone/Image"
4103 Initialize sepia-toned image attributes.
4105 assert(image != (const Image *) NULL);
4106 assert(image->signature == MagickSignature);
4107 if (image->debug != MagickFalse)
4108 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4109 assert(exception != (ExceptionInfo *) NULL);
4110 assert(exception->signature == MagickSignature);
4111 sepia_image=CloneImage(image,0,0,MagickTrue,exception);
4112 if (sepia_image == (Image *) NULL)
4113 return((Image *) NULL);
4114 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
4116 sepia_image=DestroyImage(sepia_image);
4117 return((Image *) NULL);
4120 Tone each row of the image.
4124 image_view=AcquireVirtualCacheView(image,exception);
4125 sepia_view=AcquireAuthenticCacheView(sepia_image,exception);
4126 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4127 #pragma omp parallel for schedule(static,4) shared(progress,status) \
4128 magick_threads(image,sepia_image,image->rows,1)
4130 for (y=0; y < (ssize_t) image->rows; y++)
4132 register const Quantum
4141 if (status == MagickFalse)
4143 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
4144 q=GetCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
4146 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
4151 for (x=0; x < (ssize_t) image->columns; x++)
4157 intensity=GetPixelIntensity(image,p);
4158 tone=intensity > threshold ? (double) QuantumRange : intensity+
4159 (double) QuantumRange-threshold;
4160 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
4161 tone=intensity > (7.0*threshold/6.0) ? (double) QuantumRange :
4162 intensity+(double) QuantumRange-7.0*threshold/6.0;
4163 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4164 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
4165 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
4167 if ((double) GetPixelGreen(image,q) < tone)
4168 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4169 if ((double) GetPixelBlue(image,q) < tone)
4170 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
4171 SetPixelAlpha(sepia_image,GetPixelAlpha(image,p),q);
4172 p+=GetPixelChannels(image);
4173 q+=GetPixelChannels(sepia_image);
4175 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4177 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4182 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4183 #pragma omp critical (MagickCore_SepiaToneImage)
4185 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4187 if (proceed == MagickFalse)
4191 sepia_view=DestroyCacheView(sepia_view);
4192 image_view=DestroyCacheView(image_view);
4193 (void) NormalizeImage(sepia_image,exception);
4194 (void) ContrastImage(sepia_image,MagickTrue,exception);
4195 if (status == MagickFalse)
4196 sepia_image=DestroyImage(sepia_image);
4197 return(sepia_image);
4201 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4205 % S h a d o w I m a g e %
4209 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4211 % ShadowImage() simulates a shadow from the specified image and returns it.
4213 % The format of the ShadowImage method is:
4215 % Image *ShadowImage(const Image *image,const double alpha,
4216 % const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4217 % ExceptionInfo *exception)
4219 % A description of each parameter follows:
4221 % o image: the image.
4223 % o alpha: percentage transparency.
4225 % o sigma: the standard deviation of the Gaussian, in pixels.
4227 % o x_offset: the shadow x-offset.
4229 % o y_offset: the shadow y-offset.
4231 % o exception: return any errors or warnings in this structure.
4234 MagickExport Image *ShadowImage(const Image *image,const double alpha,
4235 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4236 ExceptionInfo *exception)
4238 #define ShadowImageTag "Shadow/Image"
4260 assert(image != (Image *) NULL);
4261 assert(image->signature == MagickSignature);
4262 if (image->debug != MagickFalse)
4263 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4264 assert(exception != (ExceptionInfo *) NULL);
4265 assert(exception->signature == MagickSignature);
4266 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4267 if (clone_image == (Image *) NULL)
4268 return((Image *) NULL);
4269 if (IsGrayColorspace(image->colorspace) != MagickFalse)
4270 (void) SetImageColorspace(clone_image,sRGBColorspace,exception);
4271 (void) SetImageVirtualPixelMethod(clone_image,TransparentVirtualPixelMethod,
4273 border_info.width=(size_t) floor(2.0*sigma+0.5);
4274 border_info.height=(size_t) floor(2.0*sigma+0.5);
4277 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4279 clone_image->alpha_trait=BlendPixelTrait;
4280 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
4281 clone_image=DestroyImage(clone_image);
4282 if (border_image == (Image *) NULL)
4283 return((Image *) NULL);
4284 if (border_image->alpha_trait != BlendPixelTrait)
4285 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
4290 image_view=AcquireAuthenticCacheView(border_image,exception);
4291 for (y=0; y < (ssize_t) border_image->rows; y++)
4302 if (status == MagickFalse)
4304 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
4306 if (q == (Quantum *) NULL)
4311 background_color=border_image->background_color;
4312 background_color.alpha_trait=BlendPixelTrait;
4313 for (x=0; x < (ssize_t) border_image->columns; x++)
4315 if (border_image->alpha_trait == BlendPixelTrait)
4316 background_color.alpha=GetPixelAlpha(border_image,q)*alpha/100.0;
4317 SetPixelInfoPixel(border_image,&background_color,q);
4318 q+=GetPixelChannels(border_image);
4320 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4323 image_view=DestroyCacheView(image_view);
4324 if (status == MagickFalse)
4326 border_image=DestroyImage(border_image);
4327 return((Image *) NULL);
4329 channel_mask=SetImageChannelMask(border_image,AlphaChannel);
4330 shadow_image=BlurImage(border_image,0.0,sigma,exception);
4331 border_image=DestroyImage(border_image);
4332 if (shadow_image == (Image *) NULL)
4333 return((Image *) NULL);
4334 (void) SetPixelChannelMask(shadow_image,channel_mask);
4335 if (shadow_image->page.width == 0)
4336 shadow_image->page.width=shadow_image->columns;
4337 if (shadow_image->page.height == 0)
4338 shadow_image->page.height=shadow_image->rows;
4339 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4340 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4341 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4342 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
4343 return(shadow_image);
4347 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4351 % S k e t c h I m a g e %
4355 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4357 % SketchImage() simulates a pencil sketch. We convolve the image with a
4358 % Gaussian operator of the given radius and standard deviation (sigma). For
4359 % reasonable results, radius should be larger than sigma. Use a radius of 0
4360 % and SketchImage() selects a suitable radius for you. Angle gives the angle
4363 % The format of the SketchImage method is:
4365 % Image *SketchImage(const Image *image,const double radius,
4366 % const double sigma,const double angle,ExceptionInfo *exception)
4368 % A description of each parameter follows:
4370 % o image: the image.
4372 % o radius: the radius of the Gaussian, in pixels, not counting the
4375 % o sigma: the standard deviation of the Gaussian, in pixels.
4377 % o angle: apply the effect along this angle.
4379 % o exception: return any errors or warnings in this structure.
4382 MagickExport Image *SketchImage(const Image *image,const double radius,
4383 const double sigma,const double angle,ExceptionInfo *exception)
4399 **restrict random_info;
4404 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4412 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4413 MagickTrue,exception);
4414 if (random_image == (Image *) NULL)
4415 return((Image *) NULL);
4417 random_info=AcquireRandomInfoThreadSet();
4418 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4419 key=GetRandomSecretKey(random_info[0]);
4421 random_view=AcquireAuthenticCacheView(random_image,exception);
4422 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4423 #pragma omp parallel for schedule(static,4) shared(status) \
4424 magick_threads(random_image,random_image,random_image->rows,key == ~0UL)
4426 for (y=0; y < (ssize_t) random_image->rows; y++)
4429 id = GetOpenMPThreadId();
4437 if (status == MagickFalse)
4439 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4441 if (q == (Quantum *) NULL)
4446 for (x=0; x < (ssize_t) random_image->columns; x++)
4454 if (GetPixelReadMask(random_image,q) == 0)
4456 q+=GetPixelChannels(random_image);
4459 value=GetPseudoRandomValue(random_info[id]);
4460 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4462 PixelChannel channel=GetPixelChannelChannel(image,i);
4463 PixelTrait traits=GetPixelChannelTraits(image,channel);
4464 if (traits == UndefinedPixelTrait)
4466 q[i]=ClampToQuantum(QuantumRange*value);
4468 q+=GetPixelChannels(random_image);
4470 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4473 random_view=DestroyCacheView(random_view);
4474 random_info=DestroyRandomInfoThreadSet(random_info);
4475 if (status == MagickFalse)
4477 random_image=DestroyImage(random_image);
4478 return(random_image);
4480 blur_image=MotionBlurImage(random_image,radius,sigma,angle,exception);
4481 random_image=DestroyImage(random_image);
4482 if (blur_image == (Image *) NULL)
4483 return((Image *) NULL);
4484 dodge_image=EdgeImage(blur_image,radius,exception);
4485 blur_image=DestroyImage(blur_image);
4486 if (dodge_image == (Image *) NULL)
4487 return((Image *) NULL);
4488 (void) NormalizeImage(dodge_image,exception);
4489 (void) NegateImage(dodge_image,MagickFalse,exception);
4490 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
4491 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4492 if (sketch_image == (Image *) NULL)
4494 dodge_image=DestroyImage(dodge_image);
4495 return((Image *) NULL);
4497 (void) CompositeImage(sketch_image,dodge_image,ColorDodgeCompositeOp,
4498 MagickTrue,0,0,exception);
4499 dodge_image=DestroyImage(dodge_image);
4500 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4501 if (blend_image == (Image *) NULL)
4503 sketch_image=DestroyImage(sketch_image);
4504 return((Image *) NULL);
4506 (void) SetImageArtifact(blend_image,"compose:args","20x80");
4507 (void) CompositeImage(sketch_image,blend_image,BlendCompositeOp,MagickTrue,
4509 blend_image=DestroyImage(blend_image);
4510 return(sketch_image);
4514 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4518 % S o l a r i z e I m a g e %
4522 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4524 % SolarizeImage() applies a special effect to the image, similar to the effect
4525 % achieved in a photo darkroom by selectively exposing areas of photo
4526 % sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4527 % measure of the extent of the solarization.
4529 % The format of the SolarizeImage method is:
4531 % MagickBooleanType SolarizeImage(Image *image,const double threshold,
4532 % ExceptionInfo *exception)
4534 % A description of each parameter follows:
4536 % o image: the image.
4538 % o threshold: Define the extent of the solarization.
4540 % o exception: return any errors or warnings in this structure.
4543 MagickExport MagickBooleanType SolarizeImage(Image *image,
4544 const double threshold,ExceptionInfo *exception)
4546 #define SolarizeImageTag "Solarize/Image"
4560 assert(image != (Image *) NULL);
4561 assert(image->signature == MagickSignature);
4562 if (image->debug != MagickFalse)
4563 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4564 if (IsGrayColorspace(image->colorspace) != MagickFalse)
4565 (void) SetImageColorspace(image,sRGBColorspace,exception);
4566 if (image->storage_class == PseudoClass)
4574 for (i=0; i < (ssize_t) image->colors; i++)
4576 if ((double) image->colormap[i].red > threshold)
4577 image->colormap[i].red=QuantumRange-image->colormap[i].red;
4578 if ((double) image->colormap[i].green > threshold)
4579 image->colormap[i].green=QuantumRange-image->colormap[i].green;
4580 if ((double) image->colormap[i].blue > threshold)
4581 image->colormap[i].blue=QuantumRange-image->colormap[i].blue;
4589 image_view=AcquireAuthenticCacheView(image,exception);
4590 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4591 #pragma omp parallel for schedule(static,4) shared(progress,status) \
4592 magick_threads(image,image,image->rows,1)
4594 for (y=0; y < (ssize_t) image->rows; y++)
4602 if (status == MagickFalse)
4604 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
4605 if (q == (Quantum *) NULL)
4610 for (x=0; x < (ssize_t) image->columns; x++)
4615 if (GetPixelReadMask(image,q) == 0)
4617 q+=GetPixelChannels(image);
4620 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4622 PixelChannel channel=GetPixelChannelChannel(image,i);
4623 PixelTrait traits=GetPixelChannelTraits(image,channel);
4624 if ((traits == UndefinedPixelTrait) ||
4625 ((traits & CopyPixelTrait) != 0))
4627 if ((double) q[i] > threshold)
4628 q[i]=QuantumRange-q[i];
4630 q+=GetPixelChannels(image);
4632 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4634 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4639 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4640 #pragma omp critical (MagickCore_SolarizeImage)
4642 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4643 if (proceed == MagickFalse)
4647 image_view=DestroyCacheView(image_view);
4652 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4656 % S t e g a n o I m a g e %
4660 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4662 % SteganoImage() hides a digital watermark within the image. Recover
4663 % the hidden watermark later to prove that the authenticity of an image.
4664 % Offset defines the start position within the image to hide the watermark.
4666 % The format of the SteganoImage method is:
4668 % Image *SteganoImage(const Image *image,Image *watermark,
4669 % ExceptionInfo *exception)
4671 % A description of each parameter follows:
4673 % o image: the image.
4675 % o watermark: the watermark image.
4677 % o exception: return any errors or warnings in this structure.
4680 MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4681 ExceptionInfo *exception)
4683 #define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
4684 #define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
4685 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
4686 #define SteganoImageTag "Stegano/Image"
4721 Initialize steganographic image attributes.
4723 assert(image != (const Image *) NULL);
4724 assert(image->signature == MagickSignature);
4725 if (image->debug != MagickFalse)
4726 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4727 assert(watermark != (const Image *) NULL);
4728 assert(watermark->signature == MagickSignature);
4729 assert(exception != (ExceptionInfo *) NULL);
4730 assert(exception->signature == MagickSignature);
4732 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4733 if (stegano_image == (Image *) NULL)
4734 return((Image *) NULL);
4735 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
4736 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
4738 stegano_image=DestroyImage(stegano_image);
4739 return((Image *) NULL);
4742 Hide watermark in low-order bits of image.
4747 depth=stegano_image->depth;
4748 k=stegano_image->offset;
4750 watermark_view=AcquireVirtualCacheView(watermark,exception);
4751 stegano_view=AcquireAuthenticCacheView(stegano_image,exception);
4752 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
4754 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
4756 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
4761 (void) GetOneCacheViewVirtualPixelInfo(watermark_view,x,y,&pixel,
4763 offset=k/(ssize_t) stegano_image->columns;
4764 if (offset >= (ssize_t) stegano_image->rows)
4766 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4767 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4769 if (q == (Quantum *) NULL)
4775 SetPixelRed(stegano_image,SetBit(GetPixelRed(stegano_image,q),j,
4776 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
4781 SetPixelGreen(stegano_image,SetBit(GetPixelGreen(stegano_image,q),j,
4782 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
4787 SetPixelBlue(stegano_image,SetBit(GetPixelBlue(stegano_image,q),j,
4788 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
4792 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
4798 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
4800 if (k == stegano_image->offset)
4804 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4809 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4811 if (proceed == MagickFalse)
4815 stegano_view=DestroyCacheView(stegano_view);
4816 watermark_view=DestroyCacheView(watermark_view);
4817 if (status == MagickFalse)
4818 stegano_image=DestroyImage(stegano_image);
4819 return(stegano_image);
4823 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4827 % S t e r e o A n a g l y p h I m a g e %
4831 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4833 % StereoAnaglyphImage() combines two images and produces a single image that
4834 % is the composite of a left and right image of a stereo pair. Special
4835 % red-green stereo glasses are required to view this effect.
4837 % The format of the StereoAnaglyphImage method is:
4839 % Image *StereoImage(const Image *left_image,const Image *right_image,
4840 % ExceptionInfo *exception)
4841 % Image *StereoAnaglyphImage(const Image *left_image,
4842 % const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
4843 % ExceptionInfo *exception)
4845 % A description of each parameter follows:
4847 % o left_image: the left image.
4849 % o right_image: the right image.
4851 % o exception: return any errors or warnings in this structure.
4853 % o x_offset: amount, in pixels, by which the left image is offset to the
4854 % right of the right image.
4856 % o y_offset: amount, in pixels, by which the left image is offset to the
4857 % bottom of the right image.
4861 MagickExport Image *StereoImage(const Image *left_image,
4862 const Image *right_image,ExceptionInfo *exception)
4864 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4867 MagickExport Image *StereoAnaglyphImage(const Image *left_image,
4868 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
4869 ExceptionInfo *exception)
4871 #define StereoImageTag "Stereo/Image"
4885 assert(left_image != (const Image *) NULL);
4886 assert(left_image->signature == MagickSignature);
4887 if (left_image->debug != MagickFalse)
4888 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4889 left_image->filename);
4890 assert(right_image != (const Image *) NULL);
4891 assert(right_image->signature == MagickSignature);
4892 assert(exception != (ExceptionInfo *) NULL);
4893 assert(exception->signature == MagickSignature);
4894 assert(right_image != (const Image *) NULL);
4896 if ((left_image->columns != right_image->columns) ||
4897 (left_image->rows != right_image->rows))
4898 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4900 Initialize stereo image attributes.
4902 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4903 MagickTrue,exception);
4904 if (stereo_image == (Image *) NULL)
4905 return((Image *) NULL);
4906 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
4908 stereo_image=DestroyImage(stereo_image);
4909 return((Image *) NULL);
4911 (void) SetImageColorspace(stereo_image,sRGBColorspace,exception);
4913 Copy left image to red channel and right image to blue channel.
4916 for (y=0; y < (ssize_t) stereo_image->rows; y++)
4918 register const Quantum
4928 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4930 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4931 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
4932 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4933 (r == (Quantum *) NULL))
4935 for (x=0; x < (ssize_t) stereo_image->columns; x++)
4937 SetPixelRed(image,GetPixelRed(left_image,p),r);
4938 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4939 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4940 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4941 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4942 GetPixelAlpha(right_image,q))/2,r);
4943 p+=GetPixelChannels(left_image);
4944 q+=GetPixelChannels(right_image);
4945 r+=GetPixelChannels(stereo_image);
4947 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4949 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4954 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4955 stereo_image->rows);
4956 if (proceed == MagickFalse)
4960 if (status == MagickFalse)
4961 stereo_image=DestroyImage(stereo_image);
4962 return(stereo_image);
4966 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4970 % S w i r l I m a g e %
4974 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4976 % SwirlImage() swirls the pixels about the center of the image, where
4977 % degrees indicates the sweep of the arc through which each pixel is moved.
4978 % You get a more dramatic effect as the degrees move from 1 to 360.
4980 % The format of the SwirlImage method is:
4982 % Image *SwirlImage(const Image *image,double degrees,
4983 % const PixelInterpolateMethod method,ExceptionInfo *exception)
4985 % A description of each parameter follows:
4987 % o image: the image.
4989 % o degrees: Define the tightness of the swirling effect.
4991 % o method: the pixel interpolation method.
4993 % o exception: return any errors or warnings in this structure.
4996 MagickExport Image *SwirlImage(const Image *image,double degrees,
4997 const PixelInterpolateMethod method,ExceptionInfo *exception)
4999 #define SwirlImageTag "Swirl/Image"
5026 Initialize swirl image attributes.
5028 assert(image != (const Image *) NULL);
5029 assert(image->signature == MagickSignature);
5030 if (image->debug != MagickFalse)
5031 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5032 assert(exception != (ExceptionInfo *) NULL);
5033 assert(exception->signature == MagickSignature);
5034 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5035 if (swirl_image == (Image *) NULL)
5036 return((Image *) NULL);
5037 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
5039 swirl_image=DestroyImage(swirl_image);
5040 return((Image *) NULL);
5042 if (swirl_image->background_color.alpha != OpaqueAlpha)
5043 swirl_image->alpha_trait=BlendPixelTrait;
5045 Compute scaling factor.
5047 center.x=(double) image->columns/2.0;
5048 center.y=(double) image->rows/2.0;
5049 radius=MagickMax(center.x,center.y);
5052 if (image->columns > image->rows)
5053 scale.y=(double) image->columns/(double) image->rows;
5055 if (image->columns < image->rows)
5056 scale.x=(double) image->rows/(double) image->columns;
5057 degrees=(double) DegreesToRadians(degrees);
5063 image_view=AcquireVirtualCacheView(image,exception);
5064 interpolate_view=AcquireVirtualCacheView(image,exception);
5065 swirl_view=AcquireAuthenticCacheView(swirl_image,exception);
5066 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5067 #pragma omp parallel for schedule(static,4) shared(progress,status) \
5068 magick_threads(image,swirl_image,image->rows,1)
5070 for (y=0; y < (ssize_t) image->rows; y++)
5078 register const Quantum
5087 if (status == MagickFalse)
5089 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5090 q=QueueCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
5092 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
5097 delta.y=scale.y*(double) (y-center.y);
5098 for (x=0; x < (ssize_t) image->columns; x++)
5101 Determine if the pixel is within an ellipse.
5103 if (GetPixelReadMask(image,p) == 0)
5105 SetPixelBackgoundColor(swirl_image,q);
5106 p+=GetPixelChannels(image);
5107 q+=GetPixelChannels(swirl_image);
5110 delta.x=scale.x*(double) (x-center.x);
5111 distance=delta.x*delta.x+delta.y*delta.y;
5112 if (distance >= (radius*radius))
5117 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5119 PixelChannel channel=GetPixelChannelChannel(image,i);
5120 PixelTrait traits=GetPixelChannelTraits(image,channel);
5121 PixelTrait swirl_traits=GetPixelChannelTraits(swirl_image,channel);
5122 if ((traits == UndefinedPixelTrait) ||
5123 (swirl_traits == UndefinedPixelTrait))
5125 SetPixelChannel(swirl_image,channel,p[i],q);
5138 factor=1.0-sqrt((double) distance)/radius;
5139 sine=sin((double) (degrees*factor*factor));
5140 cosine=cos((double) (degrees*factor*factor));
5141 status=InterpolatePixelChannels(image,interpolate_view,swirl_image,
5142 method,((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5143 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
5145 p+=GetPixelChannels(image);
5146 q+=GetPixelChannels(swirl_image);
5148 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5150 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5155 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5156 #pragma omp critical (MagickCore_SwirlImage)
5158 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5159 if (proceed == MagickFalse)
5163 swirl_view=DestroyCacheView(swirl_view);
5164 interpolate_view=DestroyCacheView(interpolate_view);
5165 image_view=DestroyCacheView(image_view);
5166 if (status == MagickFalse)
5167 swirl_image=DestroyImage(swirl_image);
5168 return(swirl_image);
5172 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5176 % T i n t I m a g e %
5180 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5182 % TintImage() applies a color vector to each pixel in the image. The length
5183 % of the vector is 0 for black and white and at its maximum for the midtones.
5184 % The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5186 % The format of the TintImage method is:
5188 % Image *TintImage(const Image *image,const char *blend,
5189 % const PixelInfo *tint,ExceptionInfo *exception)
5191 % A description of each parameter follows:
5193 % o image: the image.
5195 % o blend: A color value used for tinting.
5197 % o tint: A color value used for tinting.
5199 % o exception: return any errors or warnings in this structure.
5202 MagickExport Image *TintImage(const Image *image,const char *blend,
5203 const PixelInfo *tint,ExceptionInfo *exception)
5205 #define TintImageTag "Tint/Image"
5236 Allocate tint image.
5238 assert(image != (const Image *) NULL);
5239 assert(image->signature == MagickSignature);
5240 if (image->debug != MagickFalse)
5241 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5242 assert(exception != (ExceptionInfo *) NULL);
5243 assert(exception->signature == MagickSignature);
5244 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5245 if (tint_image == (Image *) NULL)
5246 return((Image *) NULL);
5247 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
5249 tint_image=DestroyImage(tint_image);
5250 return((Image *) NULL);
5252 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
5253 (IsPixelInfoGray(tint) == MagickFalse))
5254 (void) SetImageColorspace(tint_image,sRGBColorspace,exception);
5255 if (blend == (const char *) NULL)
5258 Determine RGB values of the color.
5260 GetPixelInfo(image,&color_vector);
5261 flags=ParseGeometry(blend,&geometry_info);
5262 color_vector.red=geometry_info.rho;
5263 color_vector.green=geometry_info.rho;
5264 color_vector.blue=geometry_info.rho;
5265 color_vector.alpha=(MagickRealType) OpaqueAlpha;
5266 if ((flags & SigmaValue) != 0)
5267 color_vector.green=geometry_info.sigma;
5268 if ((flags & XiValue) != 0)
5269 color_vector.blue=geometry_info.xi;
5270 if ((flags & PsiValue) != 0)
5271 color_vector.alpha=geometry_info.psi;
5272 if (image->colorspace == CMYKColorspace)
5274 color_vector.black=geometry_info.rho;
5275 if ((flags & PsiValue) != 0)
5276 color_vector.black=geometry_info.psi;
5277 if ((flags & ChiValue) != 0)
5278 color_vector.alpha=geometry_info.chi;
5280 intensity=(double) GetPixelInfoIntensity(tint);
5281 color_vector.red=(double) (color_vector.red*tint->red/100.0-intensity);
5282 color_vector.green=(double) (color_vector.green*tint->green/100.0-intensity);
5283 color_vector.blue=(double) (color_vector.blue*tint->blue/100.0-intensity);
5284 color_vector.black=(double) (color_vector.black*tint->black/100.0-intensity);
5285 color_vector.alpha=(double) (color_vector.alpha*tint->alpha/100.0-intensity);
5291 image_view=AcquireVirtualCacheView(image,exception);
5292 tint_view=AcquireAuthenticCacheView(tint_image,exception);
5293 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5294 #pragma omp parallel for schedule(static,4) shared(progress,status) \
5295 magick_threads(image,tint_image,image->rows,1)
5297 for (y=0; y < (ssize_t) image->rows; y++)
5299 register const Quantum
5308 if (status == MagickFalse)
5310 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5311 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5313 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
5318 for (x=0; x < (ssize_t) image->columns; x++)
5329 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5331 PixelChannel channel=GetPixelChannelChannel(image,i);
5332 PixelTrait traits=GetPixelChannelTraits(image,channel);
5333 PixelTrait tint_traits=GetPixelChannelTraits(tint_image,channel);
5334 if ((traits == UndefinedPixelTrait) ||
5335 (tint_traits == UndefinedPixelTrait))
5337 if (((tint_traits & CopyPixelTrait) != 0) ||
5338 (GetPixelReadMask(image,p) == 0))
5340 SetPixelChannel(tint_image,channel,p[i],q);
5344 GetPixelInfo(image,&pixel);
5345 weight=QuantumScale*GetPixelRed(image,p)-0.5;
5346 pixel.red=(double) GetPixelRed(image,p)+color_vector.red*(1.0-(4.0*
5348 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
5349 pixel.green=(double) GetPixelGreen(image,p)+color_vector.green*(1.0-(4.0*
5351 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
5352 pixel.blue=(double) GetPixelBlue(image,p)+color_vector.blue*(1.0-(4.0*
5354 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
5355 pixel.black=(double) GetPixelBlack(image,p)+color_vector.black*(1.0-(4.0*
5357 SetPixelInfoPixel(tint_image,&pixel,q);
5358 p+=GetPixelChannels(image);
5359 q+=GetPixelChannels(tint_image);
5361 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5363 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5368 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5369 #pragma omp critical (MagickCore_TintImage)
5371 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5372 if (proceed == MagickFalse)
5376 tint_view=DestroyCacheView(tint_view);
5377 image_view=DestroyCacheView(image_view);
5378 if (status == MagickFalse)
5379 tint_image=DestroyImage(tint_image);
5384 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5388 % V i g n e t t e I m a g e %
5392 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5394 % VignetteImage() softens the edges of the image in vignette style.
5396 % The format of the VignetteImage method is:
5398 % Image *VignetteImage(const Image *image,const double radius,
5399 % const double sigma,const ssize_t x,const ssize_t y,
5400 % ExceptionInfo *exception)
5402 % A description of each parameter follows:
5404 % o image: the image.
5406 % o radius: the radius of the pixel neighborhood.
5408 % o sigma: the standard deviation of the Gaussian, in pixels.
5410 % o x, y: Define the x and y ellipse offset.
5412 % o exception: return any errors or warnings in this structure.
5415 MagickExport Image *VignetteImage(const Image *image,const double radius,
5416 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
5419 ellipse[MaxTextExtent];
5430 assert(image != (Image *) NULL);
5431 assert(image->signature == MagickSignature);
5432 if (image->debug != MagickFalse)
5433 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5434 assert(exception != (ExceptionInfo *) NULL);
5435 assert(exception->signature == MagickSignature);
5436 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5437 if (canvas_image == (Image *) NULL)
5438 return((Image *) NULL);
5439 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
5441 canvas_image=DestroyImage(canvas_image);
5442 return((Image *) NULL);
5444 canvas_image->alpha_trait=BlendPixelTrait;
5445 oval_image=CloneImage(canvas_image,canvas_image->columns,canvas_image->rows,
5446 MagickTrue,exception);
5447 if (oval_image == (Image *) NULL)
5449 canvas_image=DestroyImage(canvas_image);
5450 return((Image *) NULL);
5452 (void) QueryColorCompliance("#000000",AllCompliance,
5453 &oval_image->background_color,exception);
5454 (void) SetImageBackgroundColor(oval_image,exception);
5455 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
5456 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5458 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5460 (void) FormatLocaleString(ellipse,MaxTextExtent,"ellipse %g,%g,%g,%g,"
5461 "0.0,360.0",image->columns/2.0,image->rows/2.0,image->columns/2.0-x,
5463 draw_info->primitive=AcquireString(ellipse);
5464 (void) DrawImage(oval_image,draw_info,exception);
5465 draw_info=DestroyDrawInfo(draw_info);
5466 blur_image=BlurImage(oval_image,radius,sigma,exception);
5467 oval_image=DestroyImage(oval_image);
5468 if (blur_image == (Image *) NULL)
5470 canvas_image=DestroyImage(canvas_image);
5471 return((Image *) NULL);
5473 blur_image->alpha_trait=UndefinedPixelTrait;
5474 (void) CompositeImage(canvas_image,blur_image,IntensityCompositeOp,MagickTrue,
5476 blur_image=DestroyImage(blur_image);
5477 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5478 canvas_image=DestroyImage(canvas_image);
5479 if (vignette_image != (Image *) NULL)
5480 (void) TransformImageColorspace(vignette_image,image->colorspace,exception);
5481 return(vignette_image);
5485 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5489 % W a v e I m a g e %
5493 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5495 % WaveImage() creates a "ripple" effect in the image by shifting the pixels
5496 % vertically along a sine wave whose amplitude and wavelength is specified
5497 % by the given parameters.
5499 % The format of the WaveImage method is:
5501 % Image *WaveImage(const Image *image,const double amplitude,
5502 % const double wave_length,const PixelInterpolateMethod method,
5503 % ExceptionInfo *exception)
5505 % A description of each parameter follows:
5507 % o image: the image.
5509 % o amplitude, wave_length: Define the amplitude and wave length of the
5512 % o interpolate: the pixel interpolation method.
5514 % o exception: return any errors or warnings in this structure.
5517 MagickExport Image *WaveImage(const Image *image,const double amplitude,
5518 const double wave_length,const PixelInterpolateMethod method,
5519 ExceptionInfo *exception)
5521 #define WaveImageTag "Wave/Image"
5546 Initialize wave image attributes.
5548 assert(image != (Image *) NULL);
5549 assert(image->signature == MagickSignature);
5550 if (image->debug != MagickFalse)
5551 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5552 assert(exception != (ExceptionInfo *) NULL);
5553 assert(exception->signature == MagickSignature);
5554 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
5555 fabs(amplitude)),MagickTrue,exception);
5556 if (wave_image == (Image *) NULL)
5557 return((Image *) NULL);
5558 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
5560 wave_image=DestroyImage(wave_image);
5561 return((Image *) NULL);
5563 if (wave_image->background_color.alpha != OpaqueAlpha)
5564 wave_image->alpha_trait=BlendPixelTrait;
5568 sine_map=(double *) AcquireQuantumMemory((size_t) wave_image->columns,
5570 if (sine_map == (double *) NULL)
5572 wave_image=DestroyImage(wave_image);
5573 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5575 for (i=0; i < (ssize_t) wave_image->columns; i++)
5576 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5583 image_view=AcquireVirtualCacheView(image,exception);
5584 wave_view=AcquireAuthenticCacheView(wave_image,exception);
5585 (void) SetCacheViewVirtualPixelMethod(image_view,
5586 BackgroundVirtualPixelMethod);
5587 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5588 #pragma omp parallel for schedule(static,4) shared(progress,status) \
5589 magick_threads(image,wave_image,wave_image->rows,1)
5591 for (y=0; y < (ssize_t) wave_image->rows; y++)
5599 if (status == MagickFalse)
5601 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5603 if (q == (Quantum *) NULL)
5608 for (x=0; x < (ssize_t) wave_image->columns; x++)
5610 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5611 (double) x,(double) (y-sine_map[x]),q,exception);
5612 q+=GetPixelChannels(wave_image);
5614 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5616 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5621 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5622 #pragma omp critical (MagickCore_WaveImage)
5624 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5625 if (proceed == MagickFalse)
5629 wave_view=DestroyCacheView(wave_view);
5630 image_view=DestroyCacheView(image_view);
5631 sine_map=(double *) RelinquishMagickMemory(sine_map);
5632 if (status == MagickFalse)
5633 wave_image=DestroyImage(wave_image);