2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % MagickCore Image Special Effects Methods %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/annotate.h"
45 #include "MagickCore/artifact.h"
46 #include "MagickCore/attribute.h"
47 #include "MagickCore/cache.h"
48 #include "MagickCore/cache-view.h"
49 #include "MagickCore/color.h"
50 #include "MagickCore/color-private.h"
51 #include "MagickCore/colorspace-private.h"
52 #include "MagickCore/composite.h"
53 #include "MagickCore/decorate.h"
54 #include "MagickCore/distort.h"
55 #include "MagickCore/draw.h"
56 #include "MagickCore/effect.h"
57 #include "MagickCore/enhance.h"
58 #include "MagickCore/exception.h"
59 #include "MagickCore/exception-private.h"
60 #include "MagickCore/fx.h"
61 #include "MagickCore/fx-private.h"
62 #include "MagickCore/gem.h"
63 #include "MagickCore/gem-private.h"
64 #include "MagickCore/geometry.h"
65 #include "MagickCore/layer.h"
66 #include "MagickCore/list.h"
67 #include "MagickCore/log.h"
68 #include "MagickCore/image.h"
69 #include "MagickCore/image-private.h"
70 #include "MagickCore/magick.h"
71 #include "MagickCore/memory_.h"
72 #include "MagickCore/monitor.h"
73 #include "MagickCore/monitor-private.h"
74 #include "MagickCore/option.h"
75 #include "MagickCore/pixel.h"
76 #include "MagickCore/pixel-accessor.h"
77 #include "MagickCore/property.h"
78 #include "MagickCore/quantum.h"
79 #include "MagickCore/quantum-private.h"
80 #include "MagickCore/random_.h"
81 #include "MagickCore/random-private.h"
82 #include "MagickCore/resample.h"
83 #include "MagickCore/resample-private.h"
84 #include "MagickCore/resize.h"
85 #include "MagickCore/resource_.h"
86 #include "MagickCore/splay-tree.h"
87 #include "MagickCore/statistic.h"
88 #include "MagickCore/string_.h"
89 #include "MagickCore/string-private.h"
90 #include "MagickCore/thread-private.h"
91 #include "MagickCore/transform.h"
92 #include "MagickCore/utility.h"
97 #define LeftShiftOperator 0xf5
98 #define RightShiftOperator 0xf6
99 #define LessThanEqualOperator 0xf7
100 #define GreaterThanEqualOperator 0xf8
101 #define EqualOperator 0xf9
102 #define NotEqualOperator 0xfa
103 #define LogicalAndOperator 0xfb
104 #define LogicalOrOperator 0xfc
105 #define ExponentialNotation 0xfd
133 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
137 + A c q u i r e F x I n f o %
141 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143 % AcquireFxInfo() allocates the FxInfo structure.
145 % The format of the AcquireFxInfo method is:
147 % FxInfo *AcquireFxInfo(Image *image,const char *expression,
148 % ExceptionInfo *exception)
150 % A description of each parameter follows:
152 % o image: the image.
154 % o expression: the expression.
156 % o exception: return any errors or warnings in this structure.
159 MagickPrivate FxInfo *AcquireFxInfo(const Image *image,const char *expression,
160 ExceptionInfo *exception)
174 fx_info=(FxInfo *) AcquireMagickMemory(sizeof(*fx_info));
175 if (fx_info == (FxInfo *) NULL)
176 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
177 (void) ResetMagickMemory(fx_info,0,sizeof(*fx_info));
178 fx_info->exception=AcquireExceptionInfo();
179 fx_info->images=image;
180 fx_info->colors=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
181 RelinquishMagickMemory);
182 fx_info->symbols=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
183 RelinquishMagickMemory);
184 fx_info->view=(CacheView **) AcquireQuantumMemory(GetImageListLength(
185 fx_info->images),sizeof(*fx_info->view));
186 if (fx_info->view == (CacheView **) NULL)
187 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
189 next=GetFirstImageInList(fx_info->images);
190 for ( ; next != (Image *) NULL; next=next->next)
192 fx_info->view[i]=AcquireVirtualCacheView(next,exception);
195 fx_info->random_info=AcquireRandomInfo();
196 fx_info->expression=ConstantString(expression);
197 fx_info->file=stderr;
198 (void) SubstituteString(&fx_info->expression," ",""); /* compact string */
200 Force right-to-left associativity for unary negation.
202 (void) SubstituteString(&fx_info->expression,"-","-1.0*");
203 (void) SubstituteString(&fx_info->expression,"E-1.0*","E-");
204 (void) SubstituteString(&fx_info->expression,"e-1.0*","e-");
206 Convert complex to simple operators.
209 *fx_op=(char) LeftShiftOperator;
210 (void) SubstituteString(&fx_info->expression,"<<",fx_op);
211 *fx_op=(char) RightShiftOperator;
212 (void) SubstituteString(&fx_info->expression,">>",fx_op);
213 *fx_op=(char) LessThanEqualOperator;
214 (void) SubstituteString(&fx_info->expression,"<=",fx_op);
215 *fx_op=(char) GreaterThanEqualOperator;
216 (void) SubstituteString(&fx_info->expression,">=",fx_op);
217 *fx_op=(char) EqualOperator;
218 (void) SubstituteString(&fx_info->expression,"==",fx_op);
219 *fx_op=(char) NotEqualOperator;
220 (void) SubstituteString(&fx_info->expression,"!=",fx_op);
221 *fx_op=(char) LogicalAndOperator;
222 (void) SubstituteString(&fx_info->expression,"&&",fx_op);
223 *fx_op=(char) LogicalOrOperator;
224 (void) SubstituteString(&fx_info->expression,"||",fx_op);
225 *fx_op=(char) ExponentialNotation;
226 (void) SubstituteString(&fx_info->expression,"**",fx_op);
231 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
235 % A d d N o i s e I m a g e %
239 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241 % AddNoiseImage() adds random noise to the image.
243 % The format of the AddNoiseImage method is:
245 % Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
246 % const double attenuate,ExceptionInfo *exception)
248 % A description of each parameter follows:
250 % o image: the image.
252 % o channel: the channel type.
254 % o noise_type: The type of noise: Uniform, Gaussian, Multiplicative,
255 % Impulse, Laplacian, or Poisson.
257 % o attenuate: attenuate the random distribution.
259 % o exception: return any errors or warnings in this structure.
262 MagickExport Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
263 const double attenuate,ExceptionInfo *exception)
265 #define AddNoiseImageTag "AddNoise/Image"
281 **restrict random_info;
290 Initialize noise image attributes.
292 assert(image != (const Image *) NULL);
293 assert(image->signature == MagickSignature);
294 if (image->debug != MagickFalse)
295 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
296 assert(exception != (ExceptionInfo *) NULL);
297 assert(exception->signature == MagickSignature);
298 noise_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
299 if (noise_image == (Image *) NULL)
300 return((Image *) NULL);
301 if (SetImageStorageClass(noise_image,DirectClass,exception) == MagickFalse)
303 noise_image=DestroyImage(noise_image);
304 return((Image *) NULL);
306 if (IsGrayColorspace(image->colorspace) != MagickFalse)
307 (void) TransformImageColorspace(noise_image,RGBColorspace,exception);
309 Add noise in each row.
313 random_info=AcquireRandomInfoThreadSet();
314 image_view=AcquireVirtualCacheView(image,exception);
315 noise_view=AcquireAuthenticCacheView(noise_image,exception);
316 key=GetRandomSecretKey(random_info[0]);
317 #if defined(MAGICKCORE_OPENMP_SUPPORT)
318 #pragma omp parallel for schedule(static,4) shared(progress,status) \
319 dynamic_number_threads(image,image->columns,image->rows,key == ~0UL)
321 for (y=0; y < (ssize_t) image->rows; y++)
324 id = GetOpenMPThreadId();
329 register const Quantum
338 if (status == MagickFalse)
340 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
341 q=QueueCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
343 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
348 for (x=0; x < (ssize_t) image->columns; x++)
353 if (GetPixelMask(image,p) != 0)
355 p+=GetPixelChannels(image);
356 q+=GetPixelChannels(noise_image);
359 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
368 channel=GetPixelChannelMapChannel(image,i);
369 traits=GetPixelChannelMapTraits(image,channel);
370 noise_traits=GetPixelChannelMapTraits(noise_image,channel);
371 if ((traits == UndefinedPixelTrait) ||
372 (noise_traits == UndefinedPixelTrait))
374 if ((noise_traits & CopyPixelTrait) != 0)
376 SetPixelChannel(noise_image,channel,p[i],q);
379 SetPixelChannel(noise_image,channel,ClampToQuantum(
380 GenerateDifferentialNoise(random_info[id],p[i],noise_type,attenuate)),
383 p+=GetPixelChannels(image);
384 q+=GetPixelChannels(noise_image);
386 sync=SyncCacheViewAuthenticPixels(noise_view,exception);
387 if (sync == MagickFalse)
389 if (image->progress_monitor != (MagickProgressMonitor) NULL)
394 #if defined(MAGICKCORE_OPENMP_SUPPORT)
395 #pragma omp critical (MagickCore_AddNoiseImage)
397 proceed=SetImageProgress(image,AddNoiseImageTag,progress++,
399 if (proceed == MagickFalse)
403 noise_view=DestroyCacheView(noise_view);
404 image_view=DestroyCacheView(image_view);
405 random_info=DestroyRandomInfoThreadSet(random_info);
406 if (status == MagickFalse)
407 noise_image=DestroyImage(noise_image);
412 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
416 % B l u e S h i f t I m a g e %
420 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
422 % BlueShiftImage() mutes the colors of the image to simulate a scene at
423 % nighttime in the moonlight.
425 % The format of the BlueShiftImage method is:
427 % Image *BlueShiftImage(const Image *image,const double factor,
428 % ExceptionInfo *exception)
430 % A description of each parameter follows:
432 % o image: the image.
434 % o factor: the shift factor.
436 % o exception: return any errors or warnings in this structure.
439 MagickExport Image *BlueShiftImage(const Image *image,const double factor,
440 ExceptionInfo *exception)
442 #define BlueShiftImageTag "BlueShift/Image"
461 Allocate blue shift image.
463 assert(image != (const Image *) NULL);
464 assert(image->signature == MagickSignature);
465 if (image->debug != MagickFalse)
466 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
467 assert(exception != (ExceptionInfo *) NULL);
468 assert(exception->signature == MagickSignature);
469 shift_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
470 if (shift_image == (Image *) NULL)
471 return((Image *) NULL);
472 if (SetImageStorageClass(shift_image,DirectClass,exception) == MagickFalse)
474 shift_image=DestroyImage(shift_image);
475 return((Image *) NULL);
478 Blue-shift DirectClass image.
482 image_view=AcquireVirtualCacheView(image,exception);
483 shift_view=AcquireAuthenticCacheView(shift_image,exception);
484 #if defined(MAGICKCORE_OPENMP_SUPPORT)
485 #pragma omp parallel for schedule(static,4) shared(progress,status) \
486 dynamic_number_threads(image,image->columns,image->rows,1)
488 for (y=0; y < (ssize_t) image->rows; y++)
499 register const Quantum
508 if (status == MagickFalse)
510 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
511 q=QueueCacheViewAuthenticPixels(shift_view,0,y,shift_image->columns,1,
513 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
518 for (x=0; x < (ssize_t) image->columns; x++)
520 quantum=GetPixelRed(image,p);
521 if (GetPixelGreen(image,p) < quantum)
522 quantum=GetPixelGreen(image,p);
523 if (GetPixelBlue(image,p) < quantum)
524 quantum=GetPixelBlue(image,p);
525 pixel.red=0.5*(GetPixelRed(image,p)+factor*quantum);
526 pixel.green=0.5*(GetPixelGreen(image,p)+factor*quantum);
527 pixel.blue=0.5*(GetPixelBlue(image,p)+factor*quantum);
528 quantum=GetPixelRed(image,p);
529 if (GetPixelGreen(image,p) > quantum)
530 quantum=GetPixelGreen(image,p);
531 if (GetPixelBlue(image,p) > quantum)
532 quantum=GetPixelBlue(image,p);
533 pixel.red=0.5*(pixel.red+factor*quantum);
534 pixel.green=0.5*(pixel.green+factor*quantum);
535 pixel.blue=0.5*(pixel.blue+factor*quantum);
536 SetPixelRed(shift_image,ClampToQuantum(pixel.red),q);
537 SetPixelGreen(shift_image,ClampToQuantum(pixel.green),q);
538 SetPixelBlue(shift_image,ClampToQuantum(pixel.blue),q);
539 p+=GetPixelChannels(image);
540 q+=GetPixelChannels(shift_image);
542 sync=SyncCacheViewAuthenticPixels(shift_view,exception);
543 if (sync == MagickFalse)
545 if (image->progress_monitor != (MagickProgressMonitor) NULL)
550 #if defined(MAGICKCORE_OPENMP_SUPPORT)
551 #pragma omp critical (MagickCore_BlueShiftImage)
553 proceed=SetImageProgress(image,BlueShiftImageTag,progress++,
555 if (proceed == MagickFalse)
559 image_view=DestroyCacheView(image_view);
560 shift_view=DestroyCacheView(shift_view);
561 if (status == MagickFalse)
562 shift_image=DestroyImage(shift_image);
567 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
571 % C h a r c o a l I m a g e %
575 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
577 % CharcoalImage() creates a new image that is a copy of an existing one with
578 % the edge highlighted. It allocates the memory necessary for the new Image
579 % structure and returns a pointer to the new image.
581 % The format of the CharcoalImage method is:
583 % Image *CharcoalImage(const Image *image,const double radius,
584 % const double sigma,ExceptionInfo *exception)
586 % A description of each parameter follows:
588 % o image: the image.
590 % o radius: the radius of the pixel neighborhood.
592 % o sigma: the standard deviation of the Gaussian, in pixels.
594 % o exception: return any errors or warnings in this structure.
597 MagickExport Image *CharcoalImage(const Image *image,const double radius,
598 const double sigma,ExceptionInfo *exception)
605 assert(image != (Image *) NULL);
606 assert(image->signature == MagickSignature);
607 if (image->debug != MagickFalse)
608 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
609 assert(exception != (ExceptionInfo *) NULL);
610 assert(exception->signature == MagickSignature);
611 clone_image=CloneImage(image,0,0,MagickTrue,exception);
612 if (clone_image == (Image *) NULL)
613 return((Image *) NULL);
614 (void) SetImageType(clone_image,GrayscaleType,exception);
615 edge_image=EdgeImage(clone_image,radius,sigma,exception);
616 clone_image=DestroyImage(clone_image);
617 if (edge_image == (Image *) NULL)
618 return((Image *) NULL);
619 charcoal_image=BlurImage(edge_image,radius,sigma,exception);
620 edge_image=DestroyImage(edge_image);
621 if (charcoal_image == (Image *) NULL)
622 return((Image *) NULL);
623 (void) NormalizeImage(charcoal_image,exception);
624 (void) NegateImage(charcoal_image,MagickFalse,exception);
625 (void) SetImageType(charcoal_image,GrayscaleType,exception);
626 return(charcoal_image);
630 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
634 % C o l o r i z e I m a g e %
638 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
640 % ColorizeImage() blends the fill color with each pixel in the image.
641 % A percentage blend is specified with opacity. Control the application
642 % of different color components by specifying a different percentage for
643 % each component (e.g. 90/100/10 is 90% red, 100% green, and 10% blue).
645 % The format of the ColorizeImage method is:
647 % Image *ColorizeImage(const Image *image,const char *blend,
648 % const PixelInfo *colorize,ExceptionInfo *exception)
650 % A description of each parameter follows:
652 % o image: the image.
654 % o blend: A character string indicating the level of blending as a
657 % o colorize: A color value.
659 % o exception: return any errors or warnings in this structure.
662 MagickExport Image *ColorizeImage(const Image *image,const char *blend,
663 const PixelInfo *colorize,ExceptionInfo *exception)
665 #define ColorizeImageTag "Colorize/Image"
666 #define Colorize(pixel,blend_percentage,colorize) \
667 (pixel)=((pixel)*(100.0-(blend_percentage))+(colorize)*(blend_percentage))/100.0;
695 Allocate colorized image.
697 assert(image != (const Image *) NULL);
698 assert(image->signature == MagickSignature);
699 if (image->debug != MagickFalse)
700 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
701 assert(exception != (ExceptionInfo *) NULL);
702 assert(exception->signature == MagickSignature);
703 colorize_image=CloneImage(image,image->columns,image->rows,MagickTrue,
705 if (colorize_image == (Image *) NULL)
706 return((Image *) NULL);
707 if (SetImageStorageClass(colorize_image,DirectClass,exception) == MagickFalse)
709 colorize_image=DestroyImage(colorize_image);
710 return((Image *) NULL);
712 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
713 (IsPixelInfoGray(colorize) != MagickFalse))
714 (void) SetImageColorspace(colorize_image,RGBColorspace,exception);
715 if ((colorize_image->matte == MagickFalse) &&
716 (colorize->matte != MagickFalse))
717 (void) SetImageAlpha(colorize_image,OpaqueAlpha,exception);
718 if (blend == (const char *) NULL)
719 return(colorize_image);
720 GetPixelInfo(image,&blend_percentage);
721 flags=ParseGeometry(blend,&geometry_info);
722 blend_percentage.red=geometry_info.rho;
723 blend_percentage.green=geometry_info.rho;
724 blend_percentage.blue=geometry_info.rho;
725 blend_percentage.black=geometry_info.rho;
726 blend_percentage.alpha=geometry_info.rho;
727 if ((flags & SigmaValue) != 0)
728 blend_percentage.green=geometry_info.sigma;
729 if ((flags & XiValue) != 0)
730 blend_percentage.blue=geometry_info.xi;
731 if ((flags & PsiValue) != 0)
732 blend_percentage.alpha=geometry_info.psi;
733 if (blend_percentage.colorspace == CMYKColorspace)
735 if ((flags & PsiValue) != 0)
736 blend_percentage.black=geometry_info.psi;
737 if ((flags & ChiValue) != 0)
738 blend_percentage.alpha=geometry_info.chi;
741 Colorize DirectClass image.
745 image_view=AcquireVirtualCacheView(image,exception);
746 colorize_view=AcquireAuthenticCacheView(colorize_image,exception);
747 #if defined(MAGICKCORE_OPENMP_SUPPORT)
748 #pragma omp parallel for schedule(static,4) shared(progress,status) \
749 dynamic_number_threads(image,image->columns,image->rows,1)
751 for (y=0; y < (ssize_t) image->rows; y++)
759 register const Quantum
768 if (status == MagickFalse)
770 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
771 q=QueueCacheViewAuthenticPixels(colorize_view,0,y,colorize_image->columns,1,
773 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
778 GetPixelInfo(colorize_image,&pixel);
779 for (x=0; x < (ssize_t) image->columns; x++)
781 if (GetPixelMask(colorize_image,q) != 0)
783 p+=GetPixelChannels(image);
784 q+=GetPixelChannels(colorize_image);
787 GetPixelInfoPixel(image,p,&pixel);
788 Colorize(pixel.red,blend_percentage.red,colorize->red);
789 Colorize(pixel.green,blend_percentage.green,colorize->green);
790 Colorize(pixel.blue,blend_percentage.blue,colorize->blue);
791 Colorize(pixel.black,blend_percentage.black,colorize->black);
792 Colorize(pixel.alpha,blend_percentage.alpha,colorize->alpha);
793 SetPixelInfoPixel(colorize_image,&pixel,q);
794 p+=GetPixelChannels(image);
795 q+=GetPixelChannels(colorize_image);
797 sync=SyncCacheViewAuthenticPixels(colorize_view,exception);
798 if (sync == MagickFalse)
800 if (image->progress_monitor != (MagickProgressMonitor) NULL)
805 #if defined(MAGICKCORE_OPENMP_SUPPORT)
806 #pragma omp critical (MagickCore_ColorizeImage)
808 proceed=SetImageProgress(image,ColorizeImageTag,progress++,image->rows);
809 if (proceed == MagickFalse)
813 image_view=DestroyCacheView(image_view);
814 colorize_view=DestroyCacheView(colorize_view);
815 if (status == MagickFalse)
816 colorize_image=DestroyImage(colorize_image);
817 return(colorize_image);
821 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
825 % C o l o r M a t r i x I m a g e %
829 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
831 % ColorMatrixImage() applies color transformation to an image. This method
832 % permits saturation changes, hue rotation, luminance to alpha, and various
833 % other effects. Although variable-sized transformation matrices can be used,
834 % typically one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA
835 % (or RGBA with offsets). The matrix is similar to those used by Adobe Flash
836 % except offsets are in column 6 rather than 5 (in support of CMYKA images)
837 % and offsets are normalized (divide Flash offset by 255).
839 % The format of the ColorMatrixImage method is:
841 % Image *ColorMatrixImage(const Image *image,
842 % const KernelInfo *color_matrix,ExceptionInfo *exception)
844 % A description of each parameter follows:
846 % o image: the image.
848 % o color_matrix: the color matrix.
850 % o exception: return any errors or warnings in this structure.
853 /* FUTURE: modify to make use of a MagickMatrix Mutliply function
854 That should be provided in "matrix.c"
855 (ASIDE: actually distorts should do this too but currently doesn't)
858 MagickExport Image *ColorMatrixImage(const Image *image,
859 const KernelInfo *color_matrix,ExceptionInfo *exception)
861 #define ColorMatrixImageTag "ColorMatrix/Image"
870 { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
871 { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
872 { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
873 { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
874 { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
875 { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }
896 Map given color_matrix, into a 6x6 matrix RGBKA and a constant
898 assert(image != (Image *) NULL);
899 assert(image->signature == MagickSignature);
900 if (image->debug != MagickFalse)
901 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
902 assert(exception != (ExceptionInfo *) NULL);
903 assert(exception->signature == MagickSignature);
905 for (v=0; v < (ssize_t) color_matrix->height; v++)
906 for (u=0; u < (ssize_t) color_matrix->width; u++)
908 if ((v < 6) && (u < 6))
909 ColorMatrix[v][u]=color_matrix->values[i];
913 Initialize color image.
915 color_image=CloneImage(image,0,0,MagickTrue,exception);
916 if (color_image == (Image *) NULL)
917 return((Image *) NULL);
918 if (SetImageStorageClass(color_image,DirectClass,exception) == MagickFalse)
920 color_image=DestroyImage(color_image);
921 return((Image *) NULL);
923 if (image->debug != MagickFalse)
926 format[MaxTextExtent],
929 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
930 " ColorMatrix image with color matrix:");
931 message=AcquireString("");
932 for (v=0; v < 6; v++)
935 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
936 (void) ConcatenateString(&message,format);
937 for (u=0; u < 6; u++)
939 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",
941 (void) ConcatenateString(&message,format);
943 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
945 message=DestroyString(message);
948 Apply the ColorMatrix to image.
952 image_view=AcquireVirtualCacheView(image,exception);
953 color_view=AcquireAuthenticCacheView(color_image,exception);
954 #if defined(MAGICKCORE_OPENMP_SUPPORT)
955 #pragma omp parallel for schedule(static,4) shared(progress,status) \
956 dynamic_number_threads(image,image->columns,image->rows,1)
958 for (y=0; y < (ssize_t) image->rows; y++)
963 register const Quantum
972 if (status == MagickFalse)
974 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
975 q=GetCacheViewAuthenticPixels(color_view,0,y,color_image->columns,1,
977 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
982 GetPixelInfo(image,&pixel);
983 for (x=0; x < (ssize_t) image->columns; x++)
991 GetPixelInfoPixel(image,p,&pixel);
992 height=color_matrix->height > 6 ? 6UL : color_matrix->height;
993 for (v=0; v < (ssize_t) height; v++)
998 sum=ColorMatrix[v][0]*GetPixelRed(image,p)+ColorMatrix[v][1]*
999 GetPixelGreen(image,p)+ColorMatrix[v][2]*GetPixelBlue(image,p);
1000 if (image->colorspace == CMYKColorspace)
1001 sum+=ColorMatrix[v][3]*GetPixelBlack(image,p);
1002 if (image->matte != MagickFalse)
1003 sum+=ColorMatrix[v][4]*GetPixelAlpha(image,p);
1004 sum+=QuantumRange*ColorMatrix[v][5];
1007 case 0: pixel.red=sum; break;
1008 case 1: pixel.green=sum; break;
1009 case 2: pixel.blue=sum; break;
1010 case 3: pixel.black=sum; break;
1011 case 4: pixel.alpha=sum; break;
1015 SetPixelInfoPixel(color_image,&pixel,q);
1016 p+=GetPixelChannels(image);
1017 q+=GetPixelChannels(color_image);
1019 if (SyncCacheViewAuthenticPixels(color_view,exception) == MagickFalse)
1021 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1026 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1027 #pragma omp critical (MagickCore_ColorMatrixImage)
1029 proceed=SetImageProgress(image,ColorMatrixImageTag,progress++,
1031 if (proceed == MagickFalse)
1035 color_view=DestroyCacheView(color_view);
1036 image_view=DestroyCacheView(image_view);
1037 if (status == MagickFalse)
1038 color_image=DestroyImage(color_image);
1039 return(color_image);
1043 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1047 + D e s t r o y F x I n f o %
1051 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1053 % DestroyFxInfo() deallocates memory associated with an FxInfo structure.
1055 % The format of the DestroyFxInfo method is:
1057 % ImageInfo *DestroyFxInfo(ImageInfo *fx_info)
1059 % A description of each parameter follows:
1061 % o fx_info: the fx info.
1064 MagickPrivate FxInfo *DestroyFxInfo(FxInfo *fx_info)
1069 fx_info->exception=DestroyExceptionInfo(fx_info->exception);
1070 fx_info->expression=DestroyString(fx_info->expression);
1071 fx_info->symbols=DestroySplayTree(fx_info->symbols);
1072 fx_info->colors=DestroySplayTree(fx_info->colors);
1073 for (i=(ssize_t) GetImageListLength(fx_info->images)-1; i >= 0; i--)
1074 fx_info->view[i]=DestroyCacheView(fx_info->view[i]);
1075 fx_info->view=(CacheView **) RelinquishMagickMemory(fx_info->view);
1076 fx_info->random_info=DestroyRandomInfo(fx_info->random_info);
1077 fx_info=(FxInfo *) RelinquishMagickMemory(fx_info);
1082 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1086 + 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 %
1090 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1092 % FxEvaluateChannelExpression() evaluates an expression and returns the
1095 % The format of the FxEvaluateExpression method is:
1097 % double FxEvaluateChannelExpression(FxInfo *fx_info,
1098 % const PixelChannel channel,const ssize_t x,const ssize_t y,
1099 % double *alpha,Exceptioninfo *exception)
1100 % double FxEvaluateExpression(FxInfo *fx_info,
1101 % double *alpha,Exceptioninfo *exception)
1103 % A description of each parameter follows:
1105 % o fx_info: the fx info.
1107 % o channel: the channel.
1109 % o x,y: the pixel position.
1111 % o alpha: the result.
1113 % o exception: return any errors or warnings in this structure.
1117 static inline double MagickMax(const double x,const double y)
1124 static inline double MagickMin(const double x,const double y)
1131 static double FxChannelStatistics(FxInfo *fx_info,Image *image,
1132 PixelChannel channel,const char *symbol,ExceptionInfo *exception)
1139 statistic[MaxTextExtent];
1147 channel_mask=UndefinedChannel;
1148 for (p=symbol; (*p != '.') && (*p != '\0'); p++) ;
1154 option=ParseCommandOption(MagickPixelChannelOptions,MagickTrue,p+1);
1157 channel=(PixelChannel) option;
1158 channel_mask=(ChannelType) (channel_mask | (1 << channel));
1159 SetPixelChannelMapMask(image,channel_mask);
1162 (void) FormatLocaleString(key,MaxTextExtent,"%p.%.20g.%s",(void *) image,
1163 (double) channel,symbol);
1164 value=(const char *) GetValueFromSplayTree(fx_info->symbols,key);
1165 if (value != (const char *) NULL)
1167 if (channel_mask != UndefinedChannel)
1168 SetPixelChannelMapMask(image,channel_mask);
1169 return(QuantumScale*StringToDouble(value,(char **) NULL));
1171 (void) DeleteNodeFromSplayTree(fx_info->symbols,key);
1172 if (LocaleNCompare(symbol,"depth",5) == 0)
1177 depth=GetImageDepth(image,exception);
1178 (void) FormatLocaleString(statistic,MaxTextExtent,"%.20g",(double) depth);
1180 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1186 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
1187 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",kurtosis);
1189 if (LocaleNCompare(symbol,"maxima",6) == 0)
1195 (void) GetImageRange(image,&minima,&maxima,exception);
1196 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",maxima);
1198 if (LocaleNCompare(symbol,"mean",4) == 0)
1204 (void) GetImageMean(image,&mean,&standard_deviation,exception);
1205 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",mean);
1207 if (LocaleNCompare(symbol,"minima",6) == 0)
1213 (void) GetImageRange(image,&minima,&maxima,exception);
1214 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",minima);
1216 if (LocaleNCompare(symbol,"skewness",8) == 0)
1222 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
1223 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",skewness);
1225 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1231 (void) GetImageMean(image,&mean,&standard_deviation,exception);
1232 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",
1233 standard_deviation);
1235 if (channel_mask != UndefinedChannel)
1236 SetPixelChannelMapMask(image,channel_mask);
1237 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(key),
1238 ConstantString(statistic));
1239 return(QuantumScale*StringToDouble(statistic,(char **) NULL));
1243 FxEvaluateSubexpression(FxInfo *,const PixelChannel,const ssize_t,
1244 const ssize_t,const char *,double *,ExceptionInfo *);
1246 static MagickOffsetType FxGCD(MagickOffsetType alpha,MagickOffsetType beta)
1249 return(FxGCD(beta,alpha % beta));
1253 static inline const char *FxSubexpression(const char *expression,
1254 ExceptionInfo *exception)
1263 subexpression=expression;
1264 while ((*subexpression != '\0') &&
1265 ((level != 1) || (strchr(")",(int) *subexpression) == (char *) NULL)))
1267 if (strchr("(",(int) *subexpression) != (char *) NULL)
1270 if (strchr(")",(int) *subexpression) != (char *) NULL)
1274 if (*subexpression == '\0')
1275 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1276 "UnbalancedParenthesis","'%s'",expression);
1277 return(subexpression);
1280 static double FxGetSymbol(FxInfo *fx_info,const PixelChannel channel,
1281 const ssize_t x,const ssize_t y,const char *expression,
1282 ExceptionInfo *exception)
1286 subexpression[MaxTextExtent],
1287 symbol[MaxTextExtent];
1314 i=GetImageIndexInList(fx_info->images);
1318 if (isalpha((int) *(p+1)) == 0)
1320 if (strchr("suv",(int) *p) != (char *) NULL)
1327 i=GetImageIndexInList(fx_info->images);
1330 case 'u': i=0; break;
1331 case 'v': i=1; break;
1338 for (p++; *p != '\0'; )
1352 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1354 i=(ssize_t) (alpha+0.5);
1360 if ((isalpha((int) *(p+1)) == 0) && (*p == 'p'))
1367 for (p++; *p != '\0'; )
1381 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1392 for (p++; *p != '\0'; )
1406 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1416 length=GetImageListLength(fx_info->images);
1418 i+=(ssize_t) length;
1420 image=GetImageFromList(fx_info->images,i);
1421 if (image == (Image *) NULL)
1423 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1424 "NoSuchImage","'%s'",expression);
1427 GetPixelInfo(image,&pixel);
1428 (void) InterpolatePixelInfo(image,fx_info->view[i],image->interpolate,
1429 point.x,point.y,&pixel,exception);
1430 if ((strlen(p) > 2) && (LocaleCompare(p,"intensity") != 0) &&
1431 (LocaleCompare(p,"luminance") != 0) && (LocaleCompare(p,"hue") != 0) &&
1432 (LocaleCompare(p,"saturation") != 0) &&
1433 (LocaleCompare(p,"lightness") != 0))
1436 name[MaxTextExtent];
1438 (void) CopyMagickString(name,p,MaxTextExtent);
1439 for (q=name+(strlen(name)-1); q > name; q--)
1449 if ((strlen(name) > 2) &&
1450 (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
1455 color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
1456 if (color != (PixelInfo *) NULL)
1466 status=QueryColorCompliance(name,AllCompliance,&pixel,
1467 fx_info->exception);
1468 if (status != MagickFalse)
1470 (void) AddValueToSplayTree(fx_info->colors,ConstantString(
1471 name),ClonePixelInfo(&pixel));
1477 (void) CopyMagickString(symbol,p,MaxTextExtent);
1478 StripString(symbol);
1479 if (*symbol == '\0')
1483 case RedPixelChannel: return(QuantumScale*pixel.red);
1484 case GreenPixelChannel: return(QuantumScale*pixel.green);
1485 case BluePixelChannel: return(QuantumScale*pixel.blue);
1486 case BlackPixelChannel:
1488 if (image->colorspace != CMYKColorspace)
1490 (void) ThrowMagickException(exception,GetMagickModule(),
1491 ImageError,"ColorSeparatedImageRequired","'%s'",
1495 return(QuantumScale*pixel.black);
1497 case AlphaPixelChannel:
1502 if (pixel.matte == MagickFalse)
1504 alpha=(double) (QuantumScale*pixel.alpha);
1507 case IndexPixelChannel:
1509 case IntensityPixelChannel:
1511 return(QuantumScale*GetPixelInfoIntensity(&pixel));
1516 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1517 "UnableToParseExpression","'%s'",p);
1525 if (LocaleCompare(symbol,"a") == 0)
1526 return((double) (QuantumScale*pixel.alpha));
1532 if (LocaleCompare(symbol,"b") == 0)
1533 return(QuantumScale*pixel.blue);
1539 if (LocaleNCompare(symbol,"channel",7) == 0)
1547 flags=ParseGeometry(symbol+7,&channel_info);
1548 if (image->colorspace == CMYKColorspace)
1551 case CyanPixelChannel:
1553 if ((flags & RhoValue) == 0)
1555 return(channel_info.rho);
1557 case MagentaPixelChannel:
1559 if ((flags & SigmaValue) == 0)
1561 return(channel_info.sigma);
1563 case YellowPixelChannel:
1565 if ((flags & XiValue) == 0)
1567 return(channel_info.xi);
1569 case BlackPixelChannel:
1571 if ((flags & PsiValue) == 0)
1573 return(channel_info.psi);
1575 case AlphaPixelChannel:
1577 if ((flags & ChiValue) == 0)
1579 return(channel_info.chi);
1586 case RedPixelChannel:
1588 if ((flags & RhoValue) == 0)
1590 return(channel_info.rho);
1592 case GreenPixelChannel:
1594 if ((flags & SigmaValue) == 0)
1596 return(channel_info.sigma);
1598 case BluePixelChannel:
1600 if ((flags & XiValue) == 0)
1602 return(channel_info.xi);
1604 case BlackPixelChannel:
1606 if ((flags & ChiValue) == 0)
1608 return(channel_info.chi);
1610 case AlphaPixelChannel:
1612 if ((flags & PsiValue) == 0)
1614 return(channel_info.psi);
1621 if (LocaleCompare(symbol,"c") == 0)
1622 return(QuantumScale*pixel.red);
1628 if (LocaleNCompare(symbol,"depth",5) == 0)
1629 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1635 if (LocaleCompare(symbol,"g") == 0)
1636 return(QuantumScale*pixel.green);
1642 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1643 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1644 if (LocaleCompare(symbol,"k") == 0)
1646 if (image->colorspace != CMYKColorspace)
1648 (void) ThrowMagickException(exception,GetMagickModule(),
1649 OptionError,"ColorSeparatedImageRequired","'%s'",
1653 return(QuantumScale*pixel.black);
1660 if (LocaleCompare(symbol,"h") == 0)
1661 return((double) image->rows);
1662 if (LocaleCompare(symbol,"hue") == 0)
1669 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1678 if ((LocaleCompare(symbol,"image.depth") == 0) ||
1679 (LocaleCompare(symbol,"image.minima") == 0) ||
1680 (LocaleCompare(symbol,"image.maxima") == 0) ||
1681 (LocaleCompare(symbol,"image.mean") == 0) ||
1682 (LocaleCompare(symbol,"image.kurtosis") == 0) ||
1683 (LocaleCompare(symbol,"image.skewness") == 0) ||
1684 (LocaleCompare(symbol,"image.standard_deviation") == 0))
1685 return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
1686 if (LocaleCompare(symbol,"image.resolution.x") == 0)
1687 return(image->resolution.x);
1688 if (LocaleCompare(symbol,"image.resolution.y") == 0)
1689 return(image->resolution.y);
1690 if (LocaleCompare(symbol,"intensity") == 0)
1691 return(QuantumScale*GetPixelInfoIntensity(&pixel));
1692 if (LocaleCompare(symbol,"i") == 0)
1699 if (LocaleCompare(symbol,"j") == 0)
1706 if (LocaleCompare(symbol,"lightness") == 0)
1713 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1717 if (LocaleCompare(symbol,"luminance") == 0)
1722 luminence=0.2126*pixel.red+0.7152*pixel.green+0.0722*pixel.blue;
1723 return(QuantumScale*luminence);
1730 if (LocaleNCompare(symbol,"maxima",6) == 0)
1731 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1732 if (LocaleNCompare(symbol,"mean",4) == 0)
1733 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1734 if (LocaleNCompare(symbol,"minima",6) == 0)
1735 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1736 if (LocaleCompare(symbol,"m") == 0)
1737 return(QuantumScale*pixel.blue);
1743 if (LocaleCompare(symbol,"n") == 0)
1744 return((double) GetImageListLength(fx_info->images));
1750 if (LocaleCompare(symbol,"o") == 0)
1751 return(QuantumScale*pixel.alpha);
1757 if (LocaleCompare(symbol,"page.height") == 0)
1758 return((double) image->page.height);
1759 if (LocaleCompare(symbol,"page.width") == 0)
1760 return((double) image->page.width);
1761 if (LocaleCompare(symbol,"page.x") == 0)
1762 return((double) image->page.x);
1763 if (LocaleCompare(symbol,"page.y") == 0)
1764 return((double) image->page.y);
1770 if (LocaleCompare(symbol,"resolution.x") == 0)
1771 return(image->resolution.x);
1772 if (LocaleCompare(symbol,"resolution.y") == 0)
1773 return(image->resolution.y);
1774 if (LocaleCompare(symbol,"r") == 0)
1775 return(QuantumScale*pixel.red);
1781 if (LocaleCompare(symbol,"saturation") == 0)
1788 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1792 if (LocaleNCompare(symbol,"skewness",8) == 0)
1793 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1794 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1795 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1801 if (LocaleCompare(symbol,"t") == 0)
1802 return((double) GetImageIndexInList(fx_info->images));
1808 if (LocaleCompare(symbol,"w") == 0)
1809 return((double) image->columns);
1815 if (LocaleCompare(symbol,"y") == 0)
1816 return(QuantumScale*pixel.green);
1822 if (LocaleCompare(symbol,"z") == 0)
1827 depth=(double) GetImageDepth(image,fx_info->exception);
1835 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1836 if (value != (const char *) NULL)
1837 return((double) StringToDouble(value,(char **) NULL));
1838 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1839 "UnableToParseExpression","'%s'",symbol);
1843 static const char *FxOperatorPrecedence(const char *expression,
1844 ExceptionInfo *exception)
1848 UndefinedPrecedence,
1850 BitwiseComplementPrecedence,
1852 ExponentialNotationPrecedence,
1856 RelationalPrecedence,
1857 EquivalencyPrecedence,
1858 BitwiseAndPrecedence,
1859 BitwiseOrPrecedence,
1860 LogicalAndPrecedence,
1861 LogicalOrPrecedence,
1863 AssignmentPrecedence,
1883 subexpression=(const char *) NULL;
1884 target=NullPrecedence;
1885 while (*expression != '\0')
1887 precedence=UndefinedPrecedence;
1888 if ((isspace((int) ((char) *expression)) != 0) || (c == (int) '@'))
1893 switch (*expression)
1898 #if defined(MAGICKCORE_HAVE_ACOSH)
1899 if (LocaleNCompare(expression,"acosh",5) == 0)
1905 #if defined(MAGICKCORE_HAVE_ASINH)
1906 if (LocaleNCompare(expression,"asinh",5) == 0)
1912 #if defined(MAGICKCORE_HAVE_ATANH)
1913 if (LocaleNCompare(expression,"atanh",5) == 0)
1919 if (LocaleNCompare(expression,"atan2",5) == 0)
1929 if ((LocaleNCompare(expression,"E+",2) == 0) ||
1930 (LocaleNCompare(expression,"E-",2) == 0))
1932 expression+=2; /* scientific notation */
1939 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1940 (LocaleNCompare(expression,"j1",2) == 0))
1949 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1956 if ((c == (int) '{') || (c == (int) '['))
1959 if ((c == (int) '}') || (c == (int) ']'))
1962 switch ((unsigned char) *expression)
1967 precedence=BitwiseComplementPrecedence;
1973 precedence=ExponentPrecedence;
1978 if (((c != 0) && ((isdigit((int) ((char) c)) != 0) ||
1979 (strchr(")",c) != (char *) NULL))) &&
1980 (((islower((int) ((char) *expression)) != 0) ||
1981 (strchr("(",(int) *expression) != (char *) NULL)) ||
1982 ((isdigit((int) ((char) c)) == 0) &&
1983 (isdigit((int) ((char) *expression)) != 0))) &&
1984 (strchr("xy",(int) *expression) == (char *) NULL))
1985 precedence=MultiplyPrecedence;
1992 precedence=MultiplyPrecedence;
1998 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
2000 precedence=AdditionPrecedence;
2003 case LeftShiftOperator:
2004 case RightShiftOperator:
2006 precedence=ShiftPrecedence;
2010 case LessThanEqualOperator:
2011 case GreaterThanEqualOperator:
2014 precedence=RelationalPrecedence;
2018 case NotEqualOperator:
2020 precedence=EquivalencyPrecedence;
2025 precedence=BitwiseAndPrecedence;
2030 precedence=BitwiseOrPrecedence;
2033 case LogicalAndOperator:
2035 precedence=LogicalAndPrecedence;
2038 case LogicalOrOperator:
2040 precedence=LogicalOrPrecedence;
2043 case ExponentialNotation:
2045 precedence=ExponentialNotationPrecedence;
2051 precedence=TernaryPrecedence;
2056 precedence=AssignmentPrecedence;
2061 precedence=CommaPrecedence;
2066 precedence=SeparatorPrecedence;
2070 if ((precedence == BitwiseComplementPrecedence) ||
2071 (precedence == TernaryPrecedence) ||
2072 (precedence == AssignmentPrecedence))
2074 if (precedence > target)
2077 Right-to-left associativity.
2080 subexpression=expression;
2084 if (precedence >= target)
2087 Left-to-right associativity.
2090 subexpression=expression;
2092 if (strchr("(",(int) *expression) != (char *) NULL)
2093 expression=FxSubexpression(expression,exception);
2094 c=(int) (*expression++);
2096 return(subexpression);
2099 static double FxEvaluateSubexpression(FxInfo *fx_info,
2100 const PixelChannel channel,const ssize_t x,const ssize_t y,
2101 const char *expression,double *beta,ExceptionInfo *exception)
2105 subexpression[MaxTextExtent];
2115 if (exception->severity != UndefinedException)
2117 while (isspace((int) *expression) != 0)
2119 if (*expression == '\0')
2121 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
2122 "MissingExpression","'%s'",expression);
2125 *subexpression='\0';
2126 p=FxOperatorPrecedence(expression,exception);
2127 if (p != (const char *) NULL)
2129 (void) CopyMagickString(subexpression,expression,(size_t)
2131 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2133 switch ((unsigned char) *p)
2137 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2138 *beta=(double) (~(size_t) *beta);
2143 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2144 return(*beta == 0.0 ? 1.0 : 0.0);
2148 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2149 channel,x,y,++p,beta,exception));
2153 case ExponentialNotation:
2155 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2156 return(alpha*(*beta));
2160 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2163 if (exception->severity == UndefinedException)
2164 (void) ThrowMagickException(exception,GetMagickModule(),
2165 OptionError,"DivideByZero","'%s'",expression);
2168 return(alpha/(*beta));
2172 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2173 *beta=fabs(floor(((double) *beta)+0.5));
2176 (void) ThrowMagickException(exception,GetMagickModule(),
2177 OptionError,"DivideByZero","'%s'",expression);
2180 return(fmod((double) alpha,(double) *beta));
2184 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2185 return(alpha+(*beta));
2189 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2190 return(alpha-(*beta));
2192 case LeftShiftOperator:
2194 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2195 *beta=(double) ((size_t) (alpha+0.5) << (size_t) (gamma+0.5));
2198 case RightShiftOperator:
2200 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2201 *beta=(double) ((size_t) (alpha+0.5) >> (size_t) (gamma+0.5));
2206 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2207 return(alpha < *beta ? 1.0 : 0.0);
2209 case LessThanEqualOperator:
2211 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2212 return(alpha <= *beta ? 1.0 : 0.0);
2216 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2217 return(alpha > *beta ? 1.0 : 0.0);
2219 case GreaterThanEqualOperator:
2221 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2222 return(alpha >= *beta ? 1.0 : 0.0);
2226 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2227 return(fabs(alpha-(*beta)) < MagickEpsilon ? MagickEpsilon : 0.0);
2229 case NotEqualOperator:
2231 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2232 return(fabs(alpha-(*beta)) >= MagickEpsilon ? 1.0 : 0.0);
2236 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2237 *beta=(double) ((size_t) (alpha+0.5) & (size_t) (gamma+0.5));
2242 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2243 *beta=(double) ((size_t) (alpha+0.5) | (size_t) (gamma+0.5));
2246 case LogicalAndOperator:
2248 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2249 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2252 case LogicalOrOperator:
2254 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2255 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2263 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2265 p=StringToken(":",&q);
2266 if (q == (char *) NULL)
2268 (void) ThrowMagickException(exception,GetMagickModule(),
2269 OptionError,"UnableToParseExpression","'%s'",subexpression);
2272 if (fabs((double) alpha) >= MagickEpsilon)
2273 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2275 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2281 numeric[MaxTextExtent];
2284 while (isalpha((int) ((unsigned char) *q)) != 0)
2288 (void) ThrowMagickException(exception,GetMagickModule(),
2289 OptionError,"UnableToParseExpression","'%s'",subexpression);
2292 ClearMagickException(exception);
2293 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2294 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
2296 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2297 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2298 subexpression),ConstantString(numeric));
2303 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2308 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2313 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2319 if (strchr("(",(int) *expression) != (char *) NULL)
2321 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2322 subexpression[strlen(subexpression)-1]='\0';
2323 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2327 switch (*expression)
2331 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2337 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2343 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2345 return((double) (~(size_t) (gamma+0.5)));
2350 if (LocaleNCompare(expression,"abs",3) == 0)
2352 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2354 return((double) fabs((double) alpha));
2356 #if defined(MAGICKCORE_HAVE_ACOSH)
2357 if (LocaleNCompare(expression,"acosh",5) == 0)
2359 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2361 return((double) acosh((double) alpha));
2364 if (LocaleNCompare(expression,"acos",4) == 0)
2366 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2368 return((double) acos((double) alpha));
2370 #if defined(MAGICKCORE_HAVE_J1)
2371 if (LocaleNCompare(expression,"airy",4) == 0)
2373 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2377 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
2378 return(gamma*gamma);
2381 #if defined(MAGICKCORE_HAVE_ASINH)
2382 if (LocaleNCompare(expression,"asinh",5) == 0)
2384 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2386 return((double) asinh((double) alpha));
2389 if (LocaleNCompare(expression,"asin",4) == 0)
2391 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2393 return((double) asin((double) alpha));
2395 if (LocaleNCompare(expression,"alt",3) == 0)
2397 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2399 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
2401 if (LocaleNCompare(expression,"atan2",5) == 0)
2403 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2405 return((double) atan2((double) alpha,(double) *beta));
2407 #if defined(MAGICKCORE_HAVE_ATANH)
2408 if (LocaleNCompare(expression,"atanh",5) == 0)
2410 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2412 return((double) atanh((double) alpha));
2415 if (LocaleNCompare(expression,"atan",4) == 0)
2417 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2419 return((double) atan((double) alpha));
2421 if (LocaleCompare(expression,"a") == 0)
2422 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2428 if (LocaleCompare(expression,"b") == 0)
2429 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2435 if (LocaleNCompare(expression,"ceil",4) == 0)
2437 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2439 return((double) ceil((double) alpha));
2441 if (LocaleNCompare(expression,"cosh",4) == 0)
2443 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2445 return((double) cosh((double) alpha));
2447 if (LocaleNCompare(expression,"cos",3) == 0)
2449 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2451 return((double) cos((double) alpha));
2453 if (LocaleCompare(expression,"c") == 0)
2454 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2460 if (LocaleNCompare(expression,"debug",5) == 0)
2465 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2467 if (fx_info->images->colorspace == CMYKColorspace)
2470 case CyanPixelChannel: type="cyan"; break;
2471 case MagentaPixelChannel: type="magenta"; break;
2472 case YellowPixelChannel: type="yellow"; break;
2473 case AlphaPixelChannel: type="opacity"; break;
2474 case BlackPixelChannel: type="black"; break;
2475 default: type="unknown"; break;
2480 case RedPixelChannel: type="red"; break;
2481 case GreenPixelChannel: type="green"; break;
2482 case BluePixelChannel: type="blue"; break;
2483 case AlphaPixelChannel: type="opacity"; break;
2484 default: type="unknown"; break;
2486 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2487 if (strlen(subexpression) > 1)
2488 subexpression[strlen(subexpression)-1]='\0';
2489 if (fx_info->file != (FILE *) NULL)
2490 (void) FormatLocaleFile(fx_info->file,"%s[%.20g,%.20g].%s: "
2491 "%s=%.*g\n",fx_info->images->filename,(double) x,(double) y,type,
2492 subexpression,GetMagickPrecision(),(double) alpha);
2495 if (LocaleNCompare(expression,"drc",3) == 0)
2497 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2499 return((double) (alpha/(*beta*(alpha-1.0)+1.0)));
2506 if (LocaleCompare(expression,"epsilon") == 0)
2507 return((double) MagickEpsilon);
2508 if (LocaleNCompare(expression,"exp",3) == 0)
2510 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2512 return((double) exp((double) alpha));
2514 if (LocaleCompare(expression,"e") == 0)
2515 return((double) 2.7182818284590452354);
2521 if (LocaleNCompare(expression,"floor",5) == 0)
2523 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2525 return((double) floor((double) alpha));
2532 if (LocaleNCompare(expression,"gauss",5) == 0)
2534 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2536 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
2537 return((double) gamma);
2539 if (LocaleNCompare(expression,"gcd",3) == 0)
2544 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2546 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType) (*beta+
2548 return((double) gcd);
2550 if (LocaleCompare(expression,"g") == 0)
2551 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2557 if (LocaleCompare(expression,"h") == 0)
2558 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2559 if (LocaleCompare(expression,"hue") == 0)
2560 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2561 if (LocaleNCompare(expression,"hypot",5) == 0)
2563 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2565 return((double) hypot((double) alpha,(double) *beta));
2572 if (LocaleCompare(expression,"k") == 0)
2573 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2579 if (LocaleCompare(expression,"intensity") == 0)
2580 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2581 if (LocaleNCompare(expression,"int",3) == 0)
2583 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2585 return((double) floor(alpha));
2587 #if defined(MAGICKCORE_HAVE_ISNAN)
2588 if (LocaleNCompare(expression,"isnan",5) == 0)
2590 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2592 return((double) !!isnan((double) alpha));
2595 if (LocaleCompare(expression,"i") == 0)
2596 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2602 if (LocaleCompare(expression,"j") == 0)
2603 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2604 #if defined(MAGICKCORE_HAVE_J0)
2605 if (LocaleNCompare(expression,"j0",2) == 0)
2607 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2609 return((double) j0((double) alpha));
2612 #if defined(MAGICKCORE_HAVE_J1)
2613 if (LocaleNCompare(expression,"j1",2) == 0)
2615 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2617 return((double) j1((double) alpha));
2620 #if defined(MAGICKCORE_HAVE_J1)
2621 if (LocaleNCompare(expression,"jinc",4) == 0)
2623 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2627 gamma=(double) (2.0*j1((double) (MagickPI*alpha))/(MagickPI*
2637 if (LocaleNCompare(expression,"ln",2) == 0)
2639 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2641 return((double) log((double) alpha));
2643 if (LocaleNCompare(expression,"logtwo",6) == 0)
2645 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2647 return((double) log10((double) alpha))/log10(2.0);
2649 if (LocaleNCompare(expression,"log",3) == 0)
2651 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2653 return((double) log10((double) alpha));
2655 if (LocaleCompare(expression,"lightness") == 0)
2656 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2662 if (LocaleCompare(expression,"MaxRGB") == 0)
2663 return((double) QuantumRange);
2664 if (LocaleNCompare(expression,"maxima",6) == 0)
2666 if (LocaleNCompare(expression,"max",3) == 0)
2668 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2670 return(alpha > *beta ? alpha : *beta);
2672 if (LocaleNCompare(expression,"minima",6) == 0)
2674 if (LocaleNCompare(expression,"min",3) == 0)
2676 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2678 return(alpha < *beta ? alpha : *beta);
2680 if (LocaleNCompare(expression,"mod",3) == 0)
2682 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2684 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2687 if (LocaleCompare(expression,"m") == 0)
2688 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2694 if (LocaleNCompare(expression,"not",3) == 0)
2696 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2698 return((double) (alpha < MagickEpsilon));
2700 if (LocaleCompare(expression,"n") == 0)
2701 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2707 if (LocaleCompare(expression,"Opaque") == 0)
2709 if (LocaleCompare(expression,"o") == 0)
2710 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2716 if (LocaleCompare(expression,"phi") == 0)
2717 return((double) MagickPHI);
2718 if (LocaleCompare(expression,"pi") == 0)
2719 return((double) MagickPI);
2720 if (LocaleNCompare(expression,"pow",3) == 0)
2722 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2724 return((double) pow((double) alpha,(double) *beta));
2726 if (LocaleCompare(expression,"p") == 0)
2727 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2733 if (LocaleCompare(expression,"QuantumRange") == 0)
2734 return((double) QuantumRange);
2735 if (LocaleCompare(expression,"QuantumScale") == 0)
2736 return((double) QuantumScale);
2742 if (LocaleNCompare(expression,"rand",4) == 0)
2743 return((double) GetPseudoRandomValue(fx_info->random_info));
2744 if (LocaleNCompare(expression,"round",5) == 0)
2746 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2748 return((double) floor((double) alpha+0.5));
2750 if (LocaleCompare(expression,"r") == 0)
2751 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2757 if (LocaleCompare(expression,"saturation") == 0)
2758 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2759 if (LocaleNCompare(expression,"sign",4) == 0)
2761 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2763 return(alpha < 0.0 ? -1.0 : 1.0);
2765 if (LocaleNCompare(expression,"sinc",4) == 0)
2767 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2771 gamma=(double) (sin((double) (MagickPI*alpha))/
2775 if (LocaleNCompare(expression,"sinh",4) == 0)
2777 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2779 return((double) sinh((double) alpha));
2781 if (LocaleNCompare(expression,"sin",3) == 0)
2783 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2785 return((double) sin((double) alpha));
2787 if (LocaleNCompare(expression,"sqrt",4) == 0)
2789 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2791 return((double) sqrt((double) alpha));
2793 if (LocaleNCompare(expression,"squish",6) == 0)
2795 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2797 return((double) (1.0/(1.0+exp((double) (-alpha)))));
2799 if (LocaleCompare(expression,"s") == 0)
2800 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2806 if (LocaleNCompare(expression,"tanh",4) == 0)
2808 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2810 return((double) tanh((double) alpha));
2812 if (LocaleNCompare(expression,"tan",3) == 0)
2814 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2816 return((double) tan((double) alpha));
2818 if (LocaleCompare(expression,"Transparent") == 0)
2820 if (LocaleNCompare(expression,"trunc",5) == 0)
2822 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2825 return((double) floor((double) alpha));
2826 return((double) ceil((double) alpha));
2828 if (LocaleCompare(expression,"t") == 0)
2829 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2835 if (LocaleCompare(expression,"u") == 0)
2836 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2842 if (LocaleCompare(expression,"v") == 0)
2843 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2849 if (LocaleNCompare(expression,"while",5) == 0)
2853 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2855 } while (fabs((double) alpha) >= MagickEpsilon);
2856 return((double) *beta);
2858 if (LocaleCompare(expression,"w") == 0)
2859 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2865 if (LocaleCompare(expression,"y") == 0)
2866 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2872 if (LocaleCompare(expression,"z") == 0)
2873 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2879 q=(char *) expression;
2880 alpha=InterpretSiPrefixValue(expression,&q);
2881 if (q == expression)
2882 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2886 MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
2887 double *alpha,ExceptionInfo *exception)
2892 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2897 MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
2898 double *alpha,ExceptionInfo *exception)
2907 fx_info->file=(FILE *) NULL;
2908 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2914 MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
2915 const PixelChannel channel,const ssize_t x,const ssize_t y,
2916 double *alpha,ExceptionInfo *exception)
2922 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2924 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2928 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2936 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2938 % FxImage() applies a mathematical expression to the specified image.
2940 % The format of the FxImage method is:
2942 % Image *FxImage(const Image *image,const char *expression,
2943 % ExceptionInfo *exception)
2945 % A description of each parameter follows:
2947 % o image: the image.
2949 % o expression: A mathematical expression.
2951 % o exception: return any errors or warnings in this structure.
2955 static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2960 assert(fx_info != (FxInfo **) NULL);
2961 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
2962 if (fx_info[i] != (FxInfo *) NULL)
2963 fx_info[i]=DestroyFxInfo(fx_info[i]);
2964 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
2968 static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2969 ExceptionInfo *exception)
2986 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
2987 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
2988 if (fx_info == (FxInfo **) NULL)
2989 return((FxInfo **) NULL);
2990 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2991 if (*expression != '@')
2992 fx_expression=ConstantString(expression);
2994 fx_expression=FileToString(expression+1,~0,exception);
2995 for (i=0; i < (ssize_t) number_threads; i++)
2997 fx_info[i]=AcquireFxInfo(image,fx_expression,exception);
2998 if (fx_info[i] == (FxInfo *) NULL)
2999 return(DestroyFxThreadSet(fx_info));
3000 (void) FxPreprocessExpression(fx_info[i],&alpha,fx_info[i]->exception);
3002 fx_expression=DestroyString(fx_expression);
3006 MagickExport Image *FxImage(const Image *image,const char *expression,
3007 ExceptionInfo *exception)
3009 #define FxImageTag "Fx/Image"
3033 assert(image != (Image *) NULL);
3034 assert(image->signature == MagickSignature);
3035 if (image->debug != MagickFalse)
3036 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3037 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3038 if (fx_image == (Image *) NULL)
3039 return((Image *) NULL);
3040 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
3042 fx_image=DestroyImage(fx_image);
3043 return((Image *) NULL);
3045 fx_info=AcquireFxThreadSet(image,expression,exception);
3046 if (fx_info == (FxInfo **) NULL)
3048 fx_image=DestroyImage(fx_image);
3049 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3051 status=FxPreprocessExpression(fx_info[0],&alpha,exception);
3052 if (status == MagickFalse)
3054 fx_image=DestroyImage(fx_image);
3055 fx_info=DestroyFxThreadSet(fx_info);
3056 return((Image *) NULL);
3063 image_view=AcquireVirtualCacheView(image,exception);
3064 fx_view=AcquireAuthenticCacheView(fx_image,exception);
3065 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3066 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3067 dynamic_number_threads(image,image->columns,image->rows,1)
3069 for (y=0; y < (ssize_t) fx_image->rows; y++)
3072 id = GetOpenMPThreadId();
3074 register const Quantum
3083 if (status == MagickFalse)
3085 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3086 q=QueueCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
3087 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3092 for (x=0; x < (ssize_t) fx_image->columns; x++)
3097 if (GetPixelMask(image,p) != 0)
3099 p+=GetPixelChannels(image);
3100 q+=GetPixelChannels(fx_image);
3103 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3115 channel=GetPixelChannelMapChannel(image,i);
3116 traits=GetPixelChannelMapTraits(image,channel);
3117 fx_traits=GetPixelChannelMapTraits(fx_image,channel);
3118 if ((traits == UndefinedPixelTrait) ||
3119 (fx_traits == UndefinedPixelTrait))
3121 if ((fx_traits & CopyPixelTrait) != 0)
3123 SetPixelChannel(fx_image,channel,p[i],q);
3127 (void) FxEvaluateChannelExpression(fx_info[id],channel,x,y,&alpha,
3129 q[i]=ClampToQuantum(QuantumRange*alpha);
3131 p+=GetPixelChannels(image);
3132 q+=GetPixelChannels(fx_image);
3134 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3136 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3141 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3142 #pragma omp critical (MagickCore_FxImage)
3144 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3145 if (proceed == MagickFalse)
3149 fx_view=DestroyCacheView(fx_view);
3150 image_view=DestroyCacheView(image_view);
3151 fx_info=DestroyFxThreadSet(fx_info);
3152 if (status == MagickFalse)
3153 fx_image=DestroyImage(fx_image);
3158 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3162 % I m p l o d e I m a g e %
3166 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3168 % ImplodeImage() creates a new image that is a copy of an existing
3169 % one with the image pixels "implode" by the specified percentage. It
3170 % allocates the memory necessary for the new Image structure and returns a
3171 % pointer to the new image.
3173 % The format of the ImplodeImage method is:
3175 % Image *ImplodeImage(const Image *image,const double amount,
3176 % const PixelInterpolateMethod method,ExceptionInfo *exception)
3178 % A description of each parameter follows:
3180 % o implode_image: Method ImplodeImage returns a pointer to the image
3181 % after it is implode. A null image is returned if there is a memory
3184 % o image: the image.
3186 % o amount: Define the extent of the implosion.
3188 % o method: the pixel interpolation method.
3190 % o exception: return any errors or warnings in this structure.
3193 MagickExport Image *ImplodeImage(const Image *image,const double amount,
3194 const PixelInterpolateMethod method,ExceptionInfo *exception)
3196 #define ImplodeImageTag "Implode/Image"
3222 Initialize implode image attributes.
3224 assert(image != (Image *) NULL);
3225 assert(image->signature == MagickSignature);
3226 if (image->debug != MagickFalse)
3227 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3228 assert(exception != (ExceptionInfo *) NULL);
3229 assert(exception->signature == MagickSignature);
3230 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3232 if (implode_image == (Image *) NULL)
3233 return((Image *) NULL);
3234 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
3236 implode_image=DestroyImage(implode_image);
3237 return((Image *) NULL);
3239 if (implode_image->background_color.alpha != OpaqueAlpha)
3240 implode_image->matte=MagickTrue;
3242 Compute scaling factor.
3246 center.x=0.5*image->columns;
3247 center.y=0.5*image->rows;
3249 if (image->columns > image->rows)
3250 scale.y=(double) image->columns/(double) image->rows;
3252 if (image->columns < image->rows)
3254 scale.x=(double) image->rows/(double) image->columns;
3262 image_view=AcquireVirtualCacheView(image,exception);
3263 implode_view=AcquireAuthenticCacheView(implode_image,exception);
3264 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3265 #pragma omp parallel for schedule(static,4) shared(progress,status) \
3266 dynamic_number_threads(image,image->columns,image->rows,1)
3268 for (y=0; y < (ssize_t) image->rows; y++)
3276 register const Quantum
3285 if (status == MagickFalse)
3287 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3288 q=QueueCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
3290 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3295 delta.y=scale.y*(double) (y-center.y);
3296 for (x=0; x < (ssize_t) image->columns; x++)
3302 Determine if the pixel is within an ellipse.
3304 if (GetPixelMask(image,p) != 0)
3306 p+=GetPixelChannels(image);
3307 q+=GetPixelChannels(implode_image);
3310 delta.x=scale.x*(double) (x-center.x);
3311 distance=delta.x*delta.x+delta.y*delta.y;
3312 if (distance >= (radius*radius))
3313 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3322 channel=GetPixelChannelMapChannel(image,i);
3323 traits=GetPixelChannelMapTraits(image,channel);
3324 implode_traits=GetPixelChannelMapTraits(implode_image,channel);
3325 if ((traits == UndefinedPixelTrait) ||
3326 (implode_traits == UndefinedPixelTrait))
3328 SetPixelChannel(implode_image,channel,p[i],q);
3340 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/radius/
3342 status=InterpolatePixelChannels(image,image_view,implode_image,method,
3343 (double) (factor*delta.x/scale.x+center.x),(double) (factor*delta.y/
3344 scale.y+center.y),q,exception);
3346 p+=GetPixelChannels(image);
3347 q+=GetPixelChannels(implode_image);
3349 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3351 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3356 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3357 #pragma omp critical (MagickCore_ImplodeImage)
3359 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3360 if (proceed == MagickFalse)
3364 implode_view=DestroyCacheView(implode_view);
3365 image_view=DestroyCacheView(image_view);
3366 if (status == MagickFalse)
3367 implode_image=DestroyImage(implode_image);
3368 return(implode_image);
3372 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3376 % M o r p h I m a g e s %
3380 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3382 % The MorphImages() method requires a minimum of two images. The first
3383 % image is transformed into the second by a number of intervening images
3384 % as specified by frames.
3386 % The format of the MorphImage method is:
3388 % Image *MorphImages(const Image *image,const size_t number_frames,
3389 % ExceptionInfo *exception)
3391 % A description of each parameter follows:
3393 % o image: the image.
3395 % o number_frames: Define the number of in-between image to generate.
3396 % The more in-between frames, the smoother the morph.
3398 % o exception: return any errors or warnings in this structure.
3401 MagickExport Image *MorphImages(const Image *image,
3402 const size_t number_frames,ExceptionInfo *exception)
3404 #define MorphImageTag "Morph/Image"
3420 register const Image
3430 Clone first frame in sequence.
3432 assert(image != (Image *) NULL);
3433 assert(image->signature == MagickSignature);
3434 if (image->debug != MagickFalse)
3435 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3436 assert(exception != (ExceptionInfo *) NULL);
3437 assert(exception->signature == MagickSignature);
3438 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3439 if (morph_images == (Image *) NULL)
3440 return((Image *) NULL);
3441 if (GetNextImageInList(image) == (Image *) NULL)
3446 for (i=1; i < (ssize_t) number_frames; i++)
3448 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3449 if (morph_image == (Image *) NULL)
3451 morph_images=DestroyImageList(morph_images);
3452 return((Image *) NULL);
3454 AppendImageToList(&morph_images,morph_image);
3455 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3460 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3462 if (proceed == MagickFalse)
3466 return(GetFirstImageInList(morph_images));
3469 Morph image sequence.
3474 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3476 for (i=0; i < (ssize_t) number_frames; i++)
3482 beta=(double) (i+1.0)/(double) (number_frames+1.0);
3484 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
3485 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*next->rows+beta*
3486 GetNextImageInList(next)->rows+0.5),next->filter,exception);
3487 if (morph_image == (Image *) NULL)
3489 morph_images=DestroyImageList(morph_images);
3490 return((Image *) NULL);
3492 status=SetImageStorageClass(morph_image,DirectClass,exception);
3493 if (status == MagickFalse)
3495 morph_image=DestroyImage(morph_image);
3496 return((Image *) NULL);
3498 AppendImageToList(&morph_images,morph_image);
3499 morph_images=GetLastImageInList(morph_images);
3500 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
3501 morph_images->rows,GetNextImageInList(next)->filter,exception);
3502 if (morph_image == (Image *) NULL)
3504 morph_images=DestroyImageList(morph_images);
3505 return((Image *) NULL);
3507 image_view=AcquireVirtualCacheView(morph_image,exception);
3508 morph_view=AcquireAuthenticCacheView(morph_images,exception);
3509 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3510 #pragma omp parallel for schedule(static,4) shared(status) \
3511 dynamic_number_threads(image,image->columns,image->rows,1)
3513 for (y=0; y < (ssize_t) morph_images->rows; y++)
3518 register const Quantum
3527 if (status == MagickFalse)
3529 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3531 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3533 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
3538 for (x=0; x < (ssize_t) morph_images->columns; x++)
3543 if (GetPixelMask(image,p) != 0)
3545 p+=GetPixelChannels(image);
3546 q+=GetPixelChannels(morph_image);
3549 for (i=0; i < (ssize_t) GetPixelChannels(morph_image); i++)
3558 channel=GetPixelChannelMapChannel(image,i);
3559 traits=GetPixelChannelMapTraits(image,channel);
3560 morph_traits=GetPixelChannelMapTraits(morph_image,channel);
3561 if ((traits == UndefinedPixelTrait) ||
3562 (morph_traits == UndefinedPixelTrait))
3564 if ((morph_traits & CopyPixelTrait) != 0)
3566 SetPixelChannel(morph_image,channel,p[i],q);
3569 SetPixelChannel(morph_image,channel,ClampToQuantum(alpha*
3570 GetPixelChannel(morph_images,channel,q)+beta*p[i]),q);
3572 p+=GetPixelChannels(morph_image);
3573 q+=GetPixelChannels(morph_images);
3575 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3576 if (sync == MagickFalse)
3579 morph_view=DestroyCacheView(morph_view);
3580 image_view=DestroyCacheView(image_view);
3581 morph_image=DestroyImage(morph_image);
3583 if (i < (ssize_t) number_frames)
3586 Clone last frame in sequence.
3588 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3589 if (morph_image == (Image *) NULL)
3591 morph_images=DestroyImageList(morph_images);
3592 return((Image *) NULL);
3594 AppendImageToList(&morph_images,morph_image);
3595 morph_images=GetLastImageInList(morph_images);
3596 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3601 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3602 #pragma omp critical (MagickCore_MorphImages)
3604 proceed=SetImageProgress(image,MorphImageTag,scene,
3605 GetImageListLength(image));
3606 if (proceed == MagickFalse)
3611 if (GetNextImageInList(next) != (Image *) NULL)
3613 morph_images=DestroyImageList(morph_images);
3614 return((Image *) NULL);
3616 return(GetFirstImageInList(morph_images));
3620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3624 % P l a s m a I m a g e %
3628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3630 % PlasmaImage() initializes an image with plasma fractal values. The image
3631 % must be initialized with a base color and the random number generator
3632 % seeded before this method is called.
3634 % The format of the PlasmaImage method is:
3636 % MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
3637 % size_t attenuate,size_t depth,ExceptionInfo *exception)
3639 % A description of each parameter follows:
3641 % o image: the image.
3643 % o segment: Define the region to apply plasma fractals values.
3645 % o attenuate: Define the plasma attenuation factor.
3647 % o depth: Limit the plasma recursion depth.
3649 % o exception: return any errors or warnings in this structure.
3653 static inline Quantum PlasmaPixel(RandomInfo *random_info,
3654 const double pixel,const double noise)
3659 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
3664 static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3665 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3666 const SegmentInfo *segment,size_t attenuate,size_t depth,
3667 ExceptionInfo *exception)
3678 register const Quantum
3694 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3702 Divide the area into quadrants and recurse.
3706 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3707 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
3708 local_info=(*segment);
3709 local_info.x2=(double) x_mid;
3710 local_info.y2=(double) y_mid;
3711 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3712 &local_info,attenuate,depth,exception);
3713 local_info=(*segment);
3714 local_info.y1=(double) y_mid;
3715 local_info.x2=(double) x_mid;
3716 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3717 &local_info,attenuate,depth,exception);
3718 local_info=(*segment);
3719 local_info.x1=(double) x_mid;
3720 local_info.y2=(double) y_mid;
3721 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3722 &local_info,attenuate,depth,exception);
3723 local_info=(*segment);
3724 local_info.x1=(double) x_mid;
3725 local_info.y1=(double) y_mid;
3726 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3727 &local_info,attenuate,depth,exception));
3729 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3730 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
3731 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3732 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3733 return(MagickFalse);
3735 Average pixels and apply plasma.
3737 plasma=(double) QuantumRange/(2.0*attenuate);
3738 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3743 x=(ssize_t) ceil(segment->x1-0.5);
3744 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),1,1,
3746 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),1,1,
3748 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
3749 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3750 (q == (Quantum *) NULL))
3752 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3754 channel=GetPixelChannelMapChannel(image,i);
3755 traits=GetPixelChannelMapTraits(image,channel);
3756 if (traits == UndefinedPixelTrait)
3758 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3760 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3761 if (segment->x1 != segment->x2)
3766 x=(ssize_t) ceil(segment->x2-0.5);
3767 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3769 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3771 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
3772 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3773 (q == (Quantum *) NULL))
3775 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3777 channel=GetPixelChannelMapChannel(image,i);
3778 traits=GetPixelChannelMapTraits(image,channel);
3779 if (traits == UndefinedPixelTrait)
3781 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3783 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3786 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3788 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3793 y=(ssize_t) ceil(segment->y2-0.5);
3794 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3796 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3798 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
3799 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3800 (q == (Quantum *) NULL))
3802 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3804 channel=GetPixelChannelMapChannel(image,i);
3805 traits=GetPixelChannelMapTraits(image,channel);
3806 if (traits == UndefinedPixelTrait)
3808 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3810 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3812 if (segment->y1 != segment->y2)
3817 y=(ssize_t) ceil(segment->y1-0.5);
3818 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3820 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3822 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
3823 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3824 (q == (Quantum *) NULL))
3826 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3828 channel=GetPixelChannelMapChannel(image,i);
3829 traits=GetPixelChannelMapTraits(image,channel);
3830 if (traits == UndefinedPixelTrait)
3832 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3834 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3837 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3842 x=(ssize_t) ceil(segment->x1-0.5);
3843 y=(ssize_t) ceil(segment->y1-0.5);
3844 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
3845 x=(ssize_t) ceil(segment->x2-0.5);
3846 y=(ssize_t) ceil(segment->y2-0.5);
3847 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
3848 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
3849 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3850 (q == (Quantum *) NULL))
3852 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3854 channel=GetPixelChannelMapChannel(image,i);
3855 traits=GetPixelChannelMapTraits(image,channel);
3856 if (traits == UndefinedPixelTrait)
3858 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3860 (void) SyncCacheViewAuthenticPixels(image_view,exception);
3862 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3864 return(MagickFalse);
3867 MagickExport MagickBooleanType PlasmaImage(Image *image,
3868 const SegmentInfo *segment,size_t attenuate,size_t depth,
3869 ExceptionInfo *exception)
3882 if (image->debug != MagickFalse)
3883 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3884 assert(image != (Image *) NULL);
3885 assert(image->signature == MagickSignature);
3886 if (image->debug != MagickFalse)
3887 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3888 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
3889 return(MagickFalse);
3890 image_view=AcquireAuthenticCacheView(image,exception);
3891 u_view=AcquireVirtualCacheView(image,exception);
3892 v_view=AcquireVirtualCacheView(image,exception);
3893 random_info=AcquireRandomInfo();
3894 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3895 attenuate,depth,exception);
3896 random_info=DestroyRandomInfo(random_info);
3897 v_view=DestroyCacheView(v_view);
3898 u_view=DestroyCacheView(u_view);
3899 image_view=DestroyCacheView(image_view);
3904 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3908 % P o l a r o i d I m a g e %
3912 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3914 % PolaroidImage() simulates a Polaroid picture.
3916 % The format of the AnnotateImage method is:
3918 % Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
3919 % const char *caption,const double angle,
3920 % const PixelInterpolateMethod method,ExceptionInfo exception)
3922 % A description of each parameter follows:
3924 % o image: the image.
3926 % o draw_info: the draw info.
3928 % o caption: the Polaroid caption.
3930 % o angle: Apply the effect along this angle.
3932 % o method: the pixel interpolation method.
3934 % o exception: return any errors or warnings in this structure.
3937 MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
3938 const char *caption,const double angle,const PixelInterpolateMethod method,
3939 ExceptionInfo *exception)
3957 Simulate a Polaroid picture.
3959 assert(image != (Image *) NULL);
3960 assert(image->signature == MagickSignature);
3961 if (image->debug != MagickFalse)
3962 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3963 assert(exception != (ExceptionInfo *) NULL);
3964 assert(exception->signature == MagickSignature);
3965 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
3966 image->rows)/25.0,10.0);
3967 height=image->rows+2*quantum;
3968 caption_image=(Image *) NULL;
3969 if (caption != (const char *) NULL)
3972 geometry[MaxTextExtent],
3988 Generate caption image.
3990 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3991 if (caption_image == (Image *) NULL)
3992 return((Image *) NULL);
3993 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
3994 text=InterpretImageProperties((ImageInfo *) NULL,(Image *) image,caption,
3996 (void) CloneString(&annotate_info->text,text);
3997 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
3999 status=SetImageExtent(caption_image,image->columns,(size_t) ((count+1)*
4000 (metrics.ascent-metrics.descent)+0.5),exception);
4001 if (status == MagickFalse)
4002 caption_image=DestroyImage(caption_image);
4005 caption_image->background_color=image->border_color;
4006 (void) SetImageBackgroundColor(caption_image,exception);
4007 (void) CloneString(&annotate_info->text,text);
4008 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
4010 if (annotate_info->gravity == UndefinedGravity)
4011 (void) CloneString(&annotate_info->geometry,AcquireString(
4013 (void) AnnotateImage(caption_image,annotate_info,exception);
4014 height+=caption_image->rows;
4016 annotate_info=DestroyDrawInfo(annotate_info);
4017 text=DestroyString(text);
4019 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
4021 if (picture_image == (Image *) NULL)
4023 if (caption_image != (Image *) NULL)
4024 caption_image=DestroyImage(caption_image);
4025 return((Image *) NULL);
4027 picture_image->background_color=image->border_color;
4028 (void) SetImageBackgroundColor(picture_image,exception);
4029 (void) CompositeImage(picture_image,image,OverCompositeOp,MagickTrue,quantum,
4031 if (caption_image != (Image *) NULL)
4033 (void) CompositeImage(picture_image,caption_image,OverCompositeOp,
4034 MagickTrue,quantum,(ssize_t) (image->rows+3*quantum/2),exception);
4035 caption_image=DestroyImage(caption_image);
4037 (void) QueryColorCompliance("none",AllCompliance,
4038 &picture_image->background_color,exception);
4039 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
4040 rotate_image=RotateImage(picture_image,90.0,exception);
4041 picture_image=DestroyImage(picture_image);
4042 if (rotate_image == (Image *) NULL)
4043 return((Image *) NULL);
4044 picture_image=rotate_image;
4045 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
4046 picture_image->columns,method,exception);
4047 picture_image=DestroyImage(picture_image);
4048 if (bend_image == (Image *) NULL)
4049 return((Image *) NULL);
4050 picture_image=bend_image;
4051 rotate_image=RotateImage(picture_image,-90.0,exception);
4052 picture_image=DestroyImage(picture_image);
4053 if (rotate_image == (Image *) NULL)
4054 return((Image *) NULL);
4055 picture_image=rotate_image;
4056 picture_image->background_color=image->background_color;
4057 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
4059 if (polaroid_image == (Image *) NULL)
4061 picture_image=DestroyImage(picture_image);
4062 return(picture_image);
4064 flop_image=FlopImage(polaroid_image,exception);
4065 polaroid_image=DestroyImage(polaroid_image);
4066 if (flop_image == (Image *) NULL)
4068 picture_image=DestroyImage(picture_image);
4069 return(picture_image);
4071 polaroid_image=flop_image;
4072 (void) CompositeImage(polaroid_image,picture_image,OverCompositeOp,
4073 MagickTrue,(ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
4074 picture_image=DestroyImage(picture_image);
4075 (void) QueryColorCompliance("none",AllCompliance,
4076 &polaroid_image->background_color,exception);
4077 rotate_image=RotateImage(polaroid_image,angle,exception);
4078 polaroid_image=DestroyImage(polaroid_image);
4079 if (rotate_image == (Image *) NULL)
4080 return((Image *) NULL);
4081 polaroid_image=rotate_image;
4082 trim_image=TrimImage(polaroid_image,exception);
4083 polaroid_image=DestroyImage(polaroid_image);
4084 if (trim_image == (Image *) NULL)
4085 return((Image *) NULL);
4086 polaroid_image=trim_image;
4087 return(polaroid_image);
4091 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4095 % S e p i a T o n e I m a g e %
4099 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4101 % MagickSepiaToneImage() applies a special effect to the image, similar to the
4102 % effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4103 % 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4104 % threshold of 80% is a good starting point for a reasonable tone.
4106 % The format of the SepiaToneImage method is:
4108 % Image *SepiaToneImage(const Image *image,const double threshold,
4109 % ExceptionInfo *exception)
4111 % A description of each parameter follows:
4113 % o image: the image.
4115 % o threshold: the tone threshold.
4117 % o exception: return any errors or warnings in this structure.
4120 MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4121 ExceptionInfo *exception)
4123 #define SepiaToneImageTag "SepiaTone/Image"
4142 Initialize sepia-toned image attributes.
4144 assert(image != (const Image *) NULL);
4145 assert(image->signature == MagickSignature);
4146 if (image->debug != MagickFalse)
4147 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4148 assert(exception != (ExceptionInfo *) NULL);
4149 assert(exception->signature == MagickSignature);
4150 sepia_image=CloneImage(image,0,0,MagickTrue,exception);
4151 if (sepia_image == (Image *) NULL)
4152 return((Image *) NULL);
4153 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
4155 sepia_image=DestroyImage(sepia_image);
4156 return((Image *) NULL);
4159 Tone each row of the image.
4163 image_view=AcquireVirtualCacheView(image,exception);
4164 sepia_view=AcquireAuthenticCacheView(sepia_image,exception);
4165 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4166 #pragma omp parallel for schedule(static,4) shared(progress,status) \
4167 dynamic_number_threads(image,image->columns,image->rows,1)
4169 for (y=0; y < (ssize_t) image->rows; y++)
4171 register const Quantum
4180 if (status == MagickFalse)
4182 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
4183 q=GetCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
4185 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
4190 for (x=0; x < (ssize_t) image->columns; x++)
4196 intensity=(double) GetPixelIntensity(image,p);
4197 tone=intensity > threshold ? (double) QuantumRange : intensity+
4198 (double) QuantumRange-threshold;
4199 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
4200 tone=intensity > (7.0*threshold/6.0) ? (double) QuantumRange :
4201 intensity+(double) QuantumRange-7.0*threshold/6.0;
4202 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4203 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
4204 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
4206 if ((double) GetPixelGreen(image,q) < tone)
4207 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4208 if ((double) GetPixelBlue(image,q) < tone)
4209 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
4210 p+=GetPixelChannels(image);
4211 q+=GetPixelChannels(sepia_image);
4213 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4215 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4220 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4221 #pragma omp critical (MagickCore_SepiaToneImage)
4223 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4225 if (proceed == MagickFalse)
4229 sepia_view=DestroyCacheView(sepia_view);
4230 image_view=DestroyCacheView(image_view);
4231 (void) NormalizeImage(sepia_image,exception);
4232 (void) ContrastImage(sepia_image,MagickTrue,exception);
4233 if (status == MagickFalse)
4234 sepia_image=DestroyImage(sepia_image);
4235 return(sepia_image);
4239 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4243 % S h a d o w I m a g e %
4247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4249 % ShadowImage() simulates a shadow from the specified image and returns it.
4251 % The format of the ShadowImage method is:
4253 % Image *ShadowImage(const Image *image,const double alpha,
4254 % const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4255 % ExceptionInfo *exception)
4257 % A description of each parameter follows:
4259 % o image: the image.
4261 % o alpha: percentage transparency.
4263 % o sigma: the standard deviation of the Gaussian, in pixels.
4265 % o x_offset: the shadow x-offset.
4267 % o y_offset: the shadow y-offset.
4269 % o exception: return any errors or warnings in this structure.
4272 MagickExport Image *ShadowImage(const Image *image,const double alpha,
4273 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4274 ExceptionInfo *exception)
4276 #define ShadowImageTag "Shadow/Image"
4298 assert(image != (Image *) NULL);
4299 assert(image->signature == MagickSignature);
4300 if (image->debug != MagickFalse)
4301 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4302 assert(exception != (ExceptionInfo *) NULL);
4303 assert(exception->signature == MagickSignature);
4304 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4305 if (clone_image == (Image *) NULL)
4306 return((Image *) NULL);
4307 if (IsGrayColorspace(image->colorspace) != MagickFalse)
4308 (void) TransformImageColorspace(clone_image,RGBColorspace,exception);
4309 (void) SetImageVirtualPixelMethod(clone_image,TransparentVirtualPixelMethod,
4311 border_info.width=(size_t) floor(2.0*sigma+0.5);
4312 border_info.height=(size_t) floor(2.0*sigma+0.5);
4315 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4317 clone_image->matte=MagickTrue;
4318 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
4319 clone_image=DestroyImage(clone_image);
4320 if (border_image == (Image *) NULL)
4321 return((Image *) NULL);
4322 if (border_image->matte == MagickFalse)
4323 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
4328 image_view=AcquireAuthenticCacheView(border_image,exception);
4329 for (y=0; y < (ssize_t) border_image->rows; y++)
4340 if (status == MagickFalse)
4342 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
4344 if (q == (Quantum *) NULL)
4349 background_color=border_image->background_color;
4350 background_color.matte=MagickTrue;
4351 for (x=0; x < (ssize_t) border_image->columns; x++)
4353 if (border_image->matte != MagickFalse)
4354 background_color.alpha=GetPixelAlpha(border_image,q)*alpha/100.0;
4355 SetPixelInfoPixel(border_image,&background_color,q);
4356 q+=GetPixelChannels(border_image);
4358 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4361 image_view=DestroyCacheView(image_view);
4362 if (status == MagickFalse)
4364 border_image=DestroyImage(border_image);
4365 return((Image *) NULL);
4367 channel_mask=SetPixelChannelMask(border_image,AlphaChannel);
4368 shadow_image=BlurImage(border_image,0.0,sigma,exception);
4369 border_image=DestroyImage(border_image);
4370 if (shadow_image == (Image *) NULL)
4371 return((Image *) NULL);
4372 (void) SetPixelChannelMapMask(shadow_image,channel_mask);
4373 if (shadow_image->page.width == 0)
4374 shadow_image->page.width=shadow_image->columns;
4375 if (shadow_image->page.height == 0)
4376 shadow_image->page.height=shadow_image->rows;
4377 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4378 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4379 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4380 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
4381 return(shadow_image);
4385 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4389 % S k e t c h I m a g e %
4393 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4395 % SketchImage() simulates a pencil sketch. We convolve the image with a
4396 % Gaussian operator of the given radius and standard deviation (sigma). For
4397 % reasonable results, radius should be larger than sigma. Use a radius of 0
4398 % and SketchImage() selects a suitable radius for you. Angle gives the angle
4401 % The format of the SketchImage method is:
4403 % Image *SketchImage(const Image *image,const double radius,
4404 % const double sigma,const double angle,ExceptionInfo *exception)
4406 % A description of each parameter follows:
4408 % o image: the image.
4410 % o radius: the radius of the Gaussian, in pixels, not counting the
4413 % o sigma: the standard deviation of the Gaussian, in pixels.
4415 % o angle: apply the effect along this angle.
4417 % o exception: return any errors or warnings in this structure.
4420 MagickExport Image *SketchImage(const Image *image,const double radius,
4421 const double sigma,const double angle,ExceptionInfo *exception)
4437 **restrict random_info;
4448 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4449 MagickTrue,exception);
4450 if (random_image == (Image *) NULL)
4451 return((Image *) NULL);
4453 random_info=AcquireRandomInfoThreadSet();
4454 key=GetRandomSecretKey(random_info[0]);
4455 random_view=AcquireAuthenticCacheView(random_image,exception);
4456 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4457 #pragma omp parallel for schedule(static,4) shared(status) \
4458 dynamic_number_threads(image,image->columns,image->rows,key == ~0UL)
4460 for (y=0; y < (ssize_t) random_image->rows; y++)
4463 id = GetOpenMPThreadId();
4471 if (status == MagickFalse)
4473 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4475 if (q == (Quantum *) NULL)
4480 for (x=0; x < (ssize_t) random_image->columns; x++)
4488 if (GetPixelMask(random_image,q) != 0)
4490 q+=GetPixelChannels(random_image);
4493 value=GetPseudoRandomValue(random_info[id]);
4494 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4502 channel=GetPixelChannelMapChannel(image,i);
4503 traits=GetPixelChannelMapTraits(image,channel);
4504 if (traits == UndefinedPixelTrait)
4506 q[i]=ClampToQuantum(QuantumRange*value);
4508 q+=GetPixelChannels(random_image);
4510 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4513 random_view=DestroyCacheView(random_view);
4514 random_info=DestroyRandomInfoThreadSet(random_info);
4515 if (status == MagickFalse)
4517 random_image=DestroyImage(random_image);
4518 return(random_image);
4520 blur_image=MotionBlurImage(random_image,radius,sigma,angle,exception);
4521 random_image=DestroyImage(random_image);
4522 if (blur_image == (Image *) NULL)
4523 return((Image *) NULL);
4524 dodge_image=EdgeImage(blur_image,radius,1.0,exception);
4525 blur_image=DestroyImage(blur_image);
4526 if (dodge_image == (Image *) NULL)
4527 return((Image *) NULL);
4528 (void) NormalizeImage(dodge_image,exception);
4529 (void) NegateImage(dodge_image,MagickFalse,exception);
4530 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
4531 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4532 if (sketch_image == (Image *) NULL)
4534 dodge_image=DestroyImage(dodge_image);
4535 return((Image *) NULL);
4537 (void) CompositeImage(sketch_image,dodge_image,ColorDodgeCompositeOp,
4538 MagickTrue,0,0,exception);
4539 dodge_image=DestroyImage(dodge_image);
4540 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4541 if (blend_image == (Image *) NULL)
4543 sketch_image=DestroyImage(sketch_image);
4544 return((Image *) NULL);
4546 (void) SetImageArtifact(blend_image,"compose:args","20x80");
4547 (void) CompositeImage(sketch_image,blend_image,BlendCompositeOp,MagickTrue,
4549 blend_image=DestroyImage(blend_image);
4550 return(sketch_image);
4554 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4558 % S o l a r i z e I m a g e %
4562 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4564 % SolarizeImage() applies a special effect to the image, similar to the effect
4565 % achieved in a photo darkroom by selectively exposing areas of photo
4566 % sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4567 % measure of the extent of the solarization.
4569 % The format of the SolarizeImage method is:
4571 % MagickBooleanType SolarizeImage(Image *image,const double threshold,
4572 % ExceptionInfo *exception)
4574 % A description of each parameter follows:
4576 % o image: the image.
4578 % o threshold: Define the extent of the solarization.
4580 % o exception: return any errors or warnings in this structure.
4583 MagickExport MagickBooleanType SolarizeImage(Image *image,
4584 const double threshold,ExceptionInfo *exception)
4586 #define SolarizeImageTag "Solarize/Image"
4600 assert(image != (Image *) NULL);
4601 assert(image->signature == MagickSignature);
4602 if (image->debug != MagickFalse)
4603 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4604 if (image->storage_class == PseudoClass)
4612 for (i=0; i < (ssize_t) image->colors; i++)
4614 if ((double) image->colormap[i].red > threshold)
4615 image->colormap[i].red=QuantumRange-image->colormap[i].red;
4616 if ((double) image->colormap[i].green > threshold)
4617 image->colormap[i].green=QuantumRange-
4618 image->colormap[i].green;
4619 if ((double) image->colormap[i].blue > threshold)
4620 image->colormap[i].blue=QuantumRange-
4621 image->colormap[i].blue;
4629 image_view=AcquireAuthenticCacheView(image,exception);
4630 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4631 #pragma omp parallel for schedule(static,4) shared(progress,status) \
4632 dynamic_number_threads(image,image->columns,image->rows,1)
4634 for (y=0; y < (ssize_t) image->rows; y++)
4642 if (status == MagickFalse)
4644 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
4645 if (q == (Quantum *) NULL)
4650 for (x=0; x < (ssize_t) image->columns; x++)
4655 if (GetPixelMask(image,q) != 0)
4657 q+=GetPixelChannels(image);
4660 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4668 channel=GetPixelChannelMapChannel(image,i);
4669 traits=GetPixelChannelMapTraits(image,channel);
4670 if ((traits == UndefinedPixelTrait) ||
4671 ((traits & CopyPixelTrait) != 0))
4673 if ((double) q[i] > threshold)
4674 q[i]=QuantumRange-q[i];
4676 q+=GetPixelChannels(image);
4678 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4680 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4685 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4686 #pragma omp critical (MagickCore_SolarizeImage)
4688 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4689 if (proceed == MagickFalse)
4693 image_view=DestroyCacheView(image_view);
4698 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4702 % S t e g a n o I m a g e %
4706 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4708 % SteganoImage() hides a digital watermark within the image. Recover
4709 % the hidden watermark later to prove that the authenticity of an image.
4710 % Offset defines the start position within the image to hide the watermark.
4712 % The format of the SteganoImage method is:
4714 % Image *SteganoImage(const Image *image,Image *watermark,
4715 % ExceptionInfo *exception)
4717 % A description of each parameter follows:
4719 % o image: the image.
4721 % o watermark: the watermark image.
4723 % o exception: return any errors or warnings in this structure.
4726 MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4727 ExceptionInfo *exception)
4729 #define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
4730 #define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
4731 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
4732 #define SteganoImageTag "Stegano/Image"
4767 Initialize steganographic image attributes.
4769 assert(image != (const Image *) NULL);
4770 assert(image->signature == MagickSignature);
4771 if (image->debug != MagickFalse)
4772 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4773 assert(watermark != (const Image *) NULL);
4774 assert(watermark->signature == MagickSignature);
4775 assert(exception != (ExceptionInfo *) NULL);
4776 assert(exception->signature == MagickSignature);
4778 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4779 if (stegano_image == (Image *) NULL)
4780 return((Image *) NULL);
4781 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
4782 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
4784 stegano_image=DestroyImage(stegano_image);
4785 return((Image *) NULL);
4788 Hide watermark in low-order bits of image.
4793 depth=stegano_image->depth;
4794 k=stegano_image->offset;
4796 watermark_view=AcquireVirtualCacheView(watermark,exception);
4797 stegano_view=AcquireAuthenticCacheView(stegano_image,exception);
4798 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
4800 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
4802 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
4807 (void) GetOneCacheViewVirtualPixelInfo(watermark_view,x,y,&pixel,
4809 offset=k/(ssize_t) stegano_image->columns;
4810 if (offset >= (ssize_t) stegano_image->rows)
4812 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4813 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4815 if (q == (Quantum *) NULL)
4821 SetPixelRed(stegano_image,SetBit(GetPixelRed(stegano_image,q),j,
4822 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
4827 SetPixelGreen(stegano_image,SetBit(GetPixelGreen(stegano_image,q),j,
4828 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
4833 SetPixelBlue(stegano_image,SetBit(GetPixelBlue(stegano_image,q),j,
4834 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
4838 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
4844 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
4846 if (k == stegano_image->offset)
4850 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4855 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4857 if (proceed == MagickFalse)
4861 stegano_view=DestroyCacheView(stegano_view);
4862 watermark_view=DestroyCacheView(watermark_view);
4863 if (status == MagickFalse)
4865 stegano_image=DestroyImage(stegano_image);
4866 return((Image *) NULL);
4868 return(stegano_image);
4872 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4876 % S t e r e o A n a g l y p h I m a g e %
4880 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4882 % StereoAnaglyphImage() combines two images and produces a single image that
4883 % is the composite of a left and right image of a stereo pair. Special
4884 % red-green stereo glasses are required to view this effect.
4886 % The format of the StereoAnaglyphImage method is:
4888 % Image *StereoImage(const Image *left_image,const Image *right_image,
4889 % ExceptionInfo *exception)
4890 % Image *StereoAnaglyphImage(const Image *left_image,
4891 % const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
4892 % ExceptionInfo *exception)
4894 % A description of each parameter follows:
4896 % o left_image: the left image.
4898 % o right_image: the right image.
4900 % o exception: return any errors or warnings in this structure.
4902 % o x_offset: amount, in pixels, by which the left image is offset to the
4903 % right of the right image.
4905 % o y_offset: amount, in pixels, by which the left image is offset to the
4906 % bottom of the right image.
4910 MagickExport Image *StereoImage(const Image *left_image,
4911 const Image *right_image,ExceptionInfo *exception)
4913 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4916 MagickExport Image *StereoAnaglyphImage(const Image *left_image,
4917 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
4918 ExceptionInfo *exception)
4920 #define StereoImageTag "Stereo/Image"
4934 assert(left_image != (const Image *) NULL);
4935 assert(left_image->signature == MagickSignature);
4936 if (left_image->debug != MagickFalse)
4937 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4938 left_image->filename);
4939 assert(right_image != (const Image *) NULL);
4940 assert(right_image->signature == MagickSignature);
4941 assert(exception != (ExceptionInfo *) NULL);
4942 assert(exception->signature == MagickSignature);
4943 assert(right_image != (const Image *) NULL);
4945 if ((left_image->columns != right_image->columns) ||
4946 (left_image->rows != right_image->rows))
4947 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4949 Initialize stereo image attributes.
4951 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4952 MagickTrue,exception);
4953 if (stereo_image == (Image *) NULL)
4954 return((Image *) NULL);
4955 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
4957 stereo_image=DestroyImage(stereo_image);
4958 return((Image *) NULL);
4960 (void) SetImageColorspace(stereo_image,sRGBColorspace,exception);
4962 Copy left image to red channel and right image to blue channel.
4965 for (y=0; y < (ssize_t) stereo_image->rows; y++)
4967 register const Quantum
4977 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4979 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4980 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
4981 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4982 (r == (Quantum *) NULL))
4984 for (x=0; x < (ssize_t) stereo_image->columns; x++)
4986 SetPixelRed(image,GetPixelRed(left_image,p),r);
4987 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4988 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4989 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4990 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4991 GetPixelAlpha(right_image,q))/2,r);
4992 p+=GetPixelChannels(left_image);
4993 q+=GetPixelChannels(right_image);
4994 r+=GetPixelChannels(stereo_image);
4996 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4998 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5003 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
5004 stereo_image->rows);
5005 if (proceed == MagickFalse)
5009 if (status == MagickFalse)
5011 stereo_image=DestroyImage(stereo_image);
5012 return((Image *) NULL);
5014 return(stereo_image);
5018 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5022 % S w i r l I m a g e %
5026 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5028 % SwirlImage() swirls the pixels about the center of the image, where
5029 % degrees indicates the sweep of the arc through which each pixel is moved.
5030 % You get a more dramatic effect as the degrees move from 1 to 360.
5032 % The format of the SwirlImage method is:
5034 % Image *SwirlImage(const Image *image,double degrees,
5035 % const PixelInterpolateMethod method,ExceptionInfo *exception)
5037 % A description of each parameter follows:
5039 % o image: the image.
5041 % o degrees: Define the tightness of the swirling effect.
5043 % o method: the pixel interpolation method.
5045 % o exception: return any errors or warnings in this structure.
5048 MagickExport Image *SwirlImage(const Image *image,double degrees,
5049 const PixelInterpolateMethod method,ExceptionInfo *exception)
5051 #define SwirlImageTag "Swirl/Image"
5077 Initialize swirl image attributes.
5079 assert(image != (const Image *) NULL);
5080 assert(image->signature == MagickSignature);
5081 if (image->debug != MagickFalse)
5082 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5083 assert(exception != (ExceptionInfo *) NULL);
5084 assert(exception->signature == MagickSignature);
5085 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5086 if (swirl_image == (Image *) NULL)
5087 return((Image *) NULL);
5088 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
5090 swirl_image=DestroyImage(swirl_image);
5091 return((Image *) NULL);
5093 if (swirl_image->background_color.alpha != OpaqueAlpha)
5094 swirl_image->matte=MagickTrue;
5096 Compute scaling factor.
5098 center.x=(double) image->columns/2.0;
5099 center.y=(double) image->rows/2.0;
5100 radius=MagickMax(center.x,center.y);
5103 if (image->columns > image->rows)
5104 scale.y=(double) image->columns/(double) image->rows;
5106 if (image->columns < image->rows)
5107 scale.x=(double) image->rows/(double) image->columns;
5108 degrees=(double) DegreesToRadians(degrees);
5114 image_view=AcquireVirtualCacheView(image,exception);
5115 swirl_view=AcquireAuthenticCacheView(swirl_image,exception);
5116 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5117 #pragma omp parallel for schedule(static,4) shared(progress,status) \
5118 dynamic_number_threads(image,image->columns,image->rows,1)
5120 for (y=0; y < (ssize_t) image->rows; y++)
5128 register const Quantum
5137 if (status == MagickFalse)
5139 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
5140 q=QueueCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
5142 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
5147 delta.y=scale.y*(double) (y-center.y);
5148 for (x=0; x < (ssize_t) image->columns; x++)
5151 Determine if the pixel is within an ellipse.
5153 if (GetPixelMask(image,p) != 0)
5155 p+=GetPixelChannels(image);
5156 q+=GetPixelChannels(swirl_image);
5159 delta.x=scale.x*(double) (x-center.x);
5160 distance=delta.x*delta.x+delta.y*delta.y;
5161 if (distance >= (radius*radius))
5166 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5175 channel=GetPixelChannelMapChannel(image,i);
5176 traits=GetPixelChannelMapTraits(image,channel);
5177 swirl_traits=GetPixelChannelMapTraits(swirl_image,channel);
5178 if ((traits == UndefinedPixelTrait) ||
5179 (swirl_traits == UndefinedPixelTrait))
5181 SetPixelChannel(swirl_image,channel,p[i],q);
5194 factor=1.0-sqrt((double) distance)/radius;
5195 sine=sin((double) (degrees*factor*factor));
5196 cosine=cos((double) (degrees*factor*factor));
5197 status=InterpolatePixelChannels(image,image_view,swirl_image,method,
5198 ((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5199 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
5201 p+=GetPixelChannels(image);
5202 q+=GetPixelChannels(swirl_image);
5204 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5206 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5211 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5212 #pragma omp critical (MagickCore_SwirlImage)
5214 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5215 if (proceed == MagickFalse)
5219 swirl_view=DestroyCacheView(swirl_view);
5220 image_view=DestroyCacheView(image_view);
5221 if (status == MagickFalse)
5222 swirl_image=DestroyImage(swirl_image);
5223 return(swirl_image);
5227 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5231 % T i n t I m a g e %
5235 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5237 % TintImage() applies a color vector to each pixel in the image. The length
5238 % of the vector is 0 for black and white and at its maximum for the midtones.
5239 % The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5241 % The format of the TintImage method is:
5243 % Image *TintImage(const Image *image,const char *blend,
5244 % const PixelInfo *tint,ExceptionInfo *exception)
5246 % A description of each parameter follows:
5248 % o image: the image.
5250 % o blend: A color value used for tinting.
5252 % o tint: A color value used for tinting.
5254 % o exception: return any errors or warnings in this structure.
5257 MagickExport Image *TintImage(const Image *image,const char *blend,
5258 const PixelInfo *tint,ExceptionInfo *exception)
5260 #define TintImageTag "Tint/Image"
5291 Allocate tint image.
5293 assert(image != (const Image *) NULL);
5294 assert(image->signature == MagickSignature);
5295 if (image->debug != MagickFalse)
5296 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5297 assert(exception != (ExceptionInfo *) NULL);
5298 assert(exception->signature == MagickSignature);
5299 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5300 if (tint_image == (Image *) NULL)
5301 return((Image *) NULL);
5302 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
5304 tint_image=DestroyImage(tint_image);
5305 return((Image *) NULL);
5307 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
5308 (IsPixelInfoGray(tint) == MagickFalse))
5309 (void) SetImageColorspace(tint_image,RGBColorspace,exception);
5310 if (blend == (const char *) NULL)
5313 Determine RGB values of the color.
5315 GetPixelInfo(image,&color_vector);
5316 flags=ParseGeometry(blend,&geometry_info);
5317 color_vector.red=geometry_info.rho;
5318 color_vector.green=geometry_info.rho;
5319 color_vector.blue=geometry_info.rho;
5320 color_vector.alpha=OpaqueAlpha;
5321 if ((flags & SigmaValue) != 0)
5322 color_vector.green=geometry_info.sigma;
5323 if ((flags & XiValue) != 0)
5324 color_vector.blue=geometry_info.xi;
5325 if ((flags & PsiValue) != 0)
5326 color_vector.alpha=geometry_info.psi;
5327 if (image->colorspace == CMYKColorspace)
5329 color_vector.black=geometry_info.rho;
5330 if ((flags & PsiValue) != 0)
5331 color_vector.black=geometry_info.psi;
5332 if ((flags & ChiValue) != 0)
5333 color_vector.alpha=geometry_info.chi;
5335 intensity=(double) GetPixelInfoIntensity(tint);
5336 color_vector.red=(double) (color_vector.red*tint->red/100.0-
5338 color_vector.green=(double) (color_vector.green*tint->green/100.0-
5340 color_vector.blue=(double) (color_vector.blue*tint->blue/100.0-
5342 color_vector.black=(double) (color_vector.black*tint->black/100.0-
5344 color_vector.alpha=(double) (color_vector.alpha*tint->alpha/100.0-
5351 image_view=AcquireVirtualCacheView(image,exception);
5352 tint_view=AcquireAuthenticCacheView(tint_image,exception);
5353 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5354 #pragma omp parallel for schedule(static,4) shared(progress,status) \
5355 dynamic_number_threads(image,image->columns,image->rows,1)
5357 for (y=0; y < (ssize_t) image->rows; y++)
5359 register const Quantum
5368 if (status == MagickFalse)
5370 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5371 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5373 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
5378 for (x=0; x < (ssize_t) image->columns; x++)
5389 if (GetPixelMask(image,p) != 0)
5391 p+=GetPixelChannels(image);
5392 q+=GetPixelChannels(tint_image);
5395 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5404 channel=GetPixelChannelMapChannel(image,i);
5405 traits=GetPixelChannelMapTraits(image,channel);
5406 tint_traits=GetPixelChannelMapTraits(tint_image,channel);
5407 if ((traits == UndefinedPixelTrait) ||
5408 (tint_traits == UndefinedPixelTrait))
5410 if ((tint_traits & CopyPixelTrait) != 0)
5412 SetPixelChannel(tint_image,channel,p[i],q);
5416 GetPixelInfo(image,&pixel);
5417 weight=QuantumScale*GetPixelRed(image,p)-0.5;
5418 pixel.red=(double) GetPixelRed(image,p)+color_vector.red*
5419 (1.0-(4.0*(weight*weight)));
5420 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
5421 pixel.green=(double) GetPixelGreen(image,p)+color_vector.green*
5422 (1.0-(4.0*(weight*weight)));
5423 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
5424 pixel.blue=(double) GetPixelBlue(image,p)+color_vector.blue*
5425 (1.0-(4.0*(weight*weight)));
5426 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
5427 pixel.black=(double) GetPixelBlack(image,p)+color_vector.black*
5428 (1.0-(4.0*(weight*weight)));
5429 SetPixelInfoPixel(tint_image,&pixel,q);
5430 p+=GetPixelChannels(image);
5431 q+=GetPixelChannels(tint_image);
5433 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5435 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5440 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5441 #pragma omp critical (MagickCore_TintImage)
5443 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5444 if (proceed == MagickFalse)
5448 tint_view=DestroyCacheView(tint_view);
5449 image_view=DestroyCacheView(image_view);
5450 if (status == MagickFalse)
5451 tint_image=DestroyImage(tint_image);
5456 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5460 % V i g n e t t e I m a g e %
5464 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5466 % VignetteImage() softens the edges of the image in vignette style.
5468 % The format of the VignetteImage method is:
5470 % Image *VignetteImage(const Image *image,const double radius,
5471 % const double sigma,const ssize_t x,const ssize_t y,
5472 % ExceptionInfo *exception)
5474 % A description of each parameter follows:
5476 % o image: the image.
5478 % o radius: the radius of the pixel neighborhood.
5480 % o sigma: the standard deviation of the Gaussian, in pixels.
5482 % o x, y: Define the x and y ellipse offset.
5484 % o exception: return any errors or warnings in this structure.
5487 MagickExport Image *VignetteImage(const Image *image,const double radius,
5488 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
5491 ellipse[MaxTextExtent];
5502 assert(image != (Image *) NULL);
5503 assert(image->signature == MagickSignature);
5504 if (image->debug != MagickFalse)
5505 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5506 assert(exception != (ExceptionInfo *) NULL);
5507 assert(exception->signature == MagickSignature);
5508 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5509 if (canvas_image == (Image *) NULL)
5510 return((Image *) NULL);
5511 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
5513 canvas_image=DestroyImage(canvas_image);
5514 return((Image *) NULL);
5516 canvas_image->matte=MagickTrue;
5517 oval_image=CloneImage(canvas_image,canvas_image->columns,canvas_image->rows,
5518 MagickTrue,exception);
5519 if (oval_image == (Image *) NULL)
5521 canvas_image=DestroyImage(canvas_image);
5522 return((Image *) NULL);
5524 (void) QueryColorCompliance("#000000",AllCompliance,
5525 &oval_image->background_color,exception);
5526 (void) SetImageBackgroundColor(oval_image,exception);
5527 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
5528 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5530 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5532 (void) FormatLocaleString(ellipse,MaxTextExtent,"ellipse %g,%g,%g,%g,"
5533 "0.0,360.0",image->columns/2.0,image->rows/2.0,image->columns/2.0-x,
5535 draw_info->primitive=AcquireString(ellipse);
5536 (void) DrawImage(oval_image,draw_info,exception);
5537 draw_info=DestroyDrawInfo(draw_info);
5538 blur_image=BlurImage(oval_image,radius,sigma,exception);
5539 oval_image=DestroyImage(oval_image);
5540 if (blur_image == (Image *) NULL)
5542 canvas_image=DestroyImage(canvas_image);
5543 return((Image *) NULL);
5545 blur_image->matte=MagickFalse;
5546 (void) CompositeImage(canvas_image,blur_image,IntensityCompositeOp,MagickTrue,
5548 blur_image=DestroyImage(blur_image);
5549 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5550 canvas_image=DestroyImage(canvas_image);
5551 if (vignette_image != (Image *) NULL)
5552 (void) TransformImageColorspace(vignette_image,image->colorspace,exception);
5553 return(vignette_image);
5557 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5561 % W a v e I m a g e %
5565 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5567 % WaveImage() creates a "ripple" effect in the image by shifting the pixels
5568 % vertically along a sine wave whose amplitude and wavelength is specified
5569 % by the given parameters.
5571 % The format of the WaveImage method is:
5573 % Image *WaveImage(const Image *image,const double amplitude,
5574 % const double wave_length,const PixelInterpolateMethod method,
5575 % ExceptionInfo *exception)
5577 % A description of each parameter follows:
5579 % o image: the image.
5581 % o amplitude, wave_length: Define the amplitude and wave length of the
5584 % o interpolate: the pixel interpolation method.
5586 % o exception: return any errors or warnings in this structure.
5589 MagickExport Image *WaveImage(const Image *image,const double amplitude,
5590 const double wave_length,const PixelInterpolateMethod method,
5591 ExceptionInfo *exception)
5593 #define WaveImageTag "Wave/Image"
5618 Initialize wave image attributes.
5620 assert(image != (Image *) NULL);
5621 assert(image->signature == MagickSignature);
5622 if (image->debug != MagickFalse)
5623 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5624 assert(exception != (ExceptionInfo *) NULL);
5625 assert(exception->signature == MagickSignature);
5626 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
5627 fabs(amplitude)),MagickTrue,exception);
5628 if (wave_image == (Image *) NULL)
5629 return((Image *) NULL);
5630 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
5632 wave_image=DestroyImage(wave_image);
5633 return((Image *) NULL);
5635 if (wave_image->background_color.alpha != OpaqueAlpha)
5636 wave_image->matte=MagickTrue;
5640 sine_map=(double *) AcquireQuantumMemory((size_t) wave_image->columns,
5642 if (sine_map == (double *) NULL)
5644 wave_image=DestroyImage(wave_image);
5645 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5647 for (i=0; i < (ssize_t) wave_image->columns; i++)
5648 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5655 image_view=AcquireVirtualCacheView(image,exception);
5656 wave_view=AcquireAuthenticCacheView(wave_image,exception);
5657 (void) SetCacheViewVirtualPixelMethod(image_view,
5658 BackgroundVirtualPixelMethod);
5659 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5660 #pragma omp parallel for schedule(static,4) shared(progress,status) \
5661 dynamic_number_threads(image,image->columns,image->rows,1)
5663 for (y=0; y < (ssize_t) wave_image->rows; y++)
5671 if (status == MagickFalse)
5673 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5675 if (q == (Quantum *) NULL)
5680 for (x=0; x < (ssize_t) wave_image->columns; x++)
5682 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5683 (double) x,(double) (y-sine_map[x]),q,exception);
5684 q+=GetPixelChannels(wave_image);
5686 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5688 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5693 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5694 #pragma omp critical (MagickCore_WaveImage)
5696 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5697 if (proceed == MagickFalse)
5701 wave_view=DestroyCacheView(wave_view);
5702 image_view=DestroyCacheView(image_view);
5703 sine_map=(double *) RelinquishMagickMemory(sine_map);
5704 if (status == MagickFalse)
5705 wave_image=DestroyImage(wave_image);