blob: 320d9102a7ba9823dff7b530659d8af00838b8ef [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% FFFFF X X %
7% F X X %
8% FFF X %
9% F X X %
10% F X X %
11% %
12% %
13% MagickCore Image Special Effects Methods %
14% %
15% Software Design %
cristyde984cd2013-12-01 14:49:27 +000016% Cristy %
cristy3ed852e2009-09-05 21:47:34 +000017% October 1996 %
18% %
19% %
cristyfe676ee2013-11-18 13:03:38 +000020% Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
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. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37%
38*/
39
40/*
41 Include declarations.
42*/
cristy4c08aed2011-07-01 19:47:50 +000043#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"
cristy6a2180c2013-05-27 10:28:36 +000049#include "MagickCore/channel.h"
cristy4c08aed2011-07-01 19:47:50 +000050#include "MagickCore/color.h"
51#include "MagickCore/color-private.h"
cristy9b7a4fc2012-04-08 22:26:56 +000052#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000053#include "MagickCore/composite.h"
54#include "MagickCore/decorate.h"
cristyc53413d2011-11-17 13:04:26 +000055#include "MagickCore/distort.h"
cristy4c08aed2011-07-01 19:47:50 +000056#include "MagickCore/draw.h"
57#include "MagickCore/effect.h"
58#include "MagickCore/enhance.h"
59#include "MagickCore/exception.h"
60#include "MagickCore/exception-private.h"
61#include "MagickCore/fx.h"
62#include "MagickCore/fx-private.h"
63#include "MagickCore/gem.h"
cristy8ea81222011-09-04 10:33:32 +000064#include "MagickCore/gem-private.h"
cristy4c08aed2011-07-01 19:47:50 +000065#include "MagickCore/geometry.h"
66#include "MagickCore/layer.h"
67#include "MagickCore/list.h"
68#include "MagickCore/log.h"
69#include "MagickCore/image.h"
70#include "MagickCore/image-private.h"
71#include "MagickCore/magick.h"
72#include "MagickCore/memory_.h"
73#include "MagickCore/monitor.h"
74#include "MagickCore/monitor-private.h"
75#include "MagickCore/option.h"
76#include "MagickCore/pixel.h"
77#include "MagickCore/pixel-accessor.h"
78#include "MagickCore/property.h"
79#include "MagickCore/quantum.h"
80#include "MagickCore/quantum-private.h"
81#include "MagickCore/random_.h"
82#include "MagickCore/random-private.h"
83#include "MagickCore/resample.h"
84#include "MagickCore/resample-private.h"
85#include "MagickCore/resize.h"
cristy57340e02012-05-05 00:53:23 +000086#include "MagickCore/resource_.h"
cristy4c08aed2011-07-01 19:47:50 +000087#include "MagickCore/splay-tree.h"
88#include "MagickCore/statistic.h"
89#include "MagickCore/string_.h"
90#include "MagickCore/string-private.h"
91#include "MagickCore/thread-private.h"
92#include "MagickCore/transform.h"
93#include "MagickCore/utility.h"
cristy3ed852e2009-09-05 21:47:34 +000094
95/*
96 Define declarations.
97*/
cristy41acfc72013-11-14 16:47:53 +000098#define LeftShiftOperator 0xf5U
99#define RightShiftOperator 0xf6U
100#define LessThanEqualOperator 0xf7U
101#define GreaterThanEqualOperator 0xf8U
102#define EqualOperator 0xf9U
103#define NotEqualOperator 0xfaU
104#define LogicalAndOperator 0xfbU
105#define LogicalOrOperator 0xfcU
106#define ExponentialNotation 0xfdU
107
cristy3ed852e2009-09-05 21:47:34 +0000108struct _FxInfo
109{
110 const Image
111 *images;
112
cristy3ed852e2009-09-05 21:47:34 +0000113 char
114 *expression;
115
116 FILE
117 *file;
118
119 SplayTreeInfo
120 *colors,
121 *symbols;
122
cristyd76c51e2011-03-26 00:21:26 +0000123 CacheView
124 **view;
cristy3ed852e2009-09-05 21:47:34 +0000125
126 RandomInfo
127 *random_info;
128
129 ExceptionInfo
130 *exception;
131};
132
133/*
134%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
135% %
136% %
137% %
138+ A c q u i r e F x I n f o %
139% %
140% %
141% %
142%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143%
144% AcquireFxInfo() allocates the FxInfo structure.
145%
146% The format of the AcquireFxInfo method is:
147%
cristydb070952012-04-20 14:33:00 +0000148% FxInfo *AcquireFxInfo(Image *image,const char *expression,
149% ExceptionInfo *exception)
cristy0a9b3722010-10-23 18:45:49 +0000150%
cristy3ed852e2009-09-05 21:47:34 +0000151% A description of each parameter follows:
152%
153% o image: the image.
154%
155% o expression: the expression.
156%
cristydb070952012-04-20 14:33:00 +0000157% o exception: return any errors or warnings in this structure.
158%
cristy3ed852e2009-09-05 21:47:34 +0000159*/
cristydb070952012-04-20 14:33:00 +0000160MagickPrivate FxInfo *AcquireFxInfo(const Image *image,const char *expression,
161 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000162{
163 char
164 fx_op[2];
165
cristycb180922011-03-11 14:41:24 +0000166 const Image
167 *next;
168
cristy3ed852e2009-09-05 21:47:34 +0000169 FxInfo
170 *fx_info;
171
cristybb503372010-05-27 20:51:26 +0000172 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000173 i;
174
cristy73bd4a52010-10-05 11:24:23 +0000175 fx_info=(FxInfo *) AcquireMagickMemory(sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +0000176 if (fx_info == (FxInfo *) NULL)
177 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
178 (void) ResetMagickMemory(fx_info,0,sizeof(*fx_info));
179 fx_info->exception=AcquireExceptionInfo();
anthony7d86e172011-03-23 12:37:06 +0000180 fx_info->images=image;
cristy3ed852e2009-09-05 21:47:34 +0000181 fx_info->colors=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
cristy1e12eb42013-08-17 16:19:01 +0000182 RelinquishAlignedMemory);
cristy3ed852e2009-09-05 21:47:34 +0000183 fx_info->symbols=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
184 RelinquishMagickMemory);
cristyd76c51e2011-03-26 00:21:26 +0000185 fx_info->view=(CacheView **) AcquireQuantumMemory(GetImageListLength(
186 fx_info->images),sizeof(*fx_info->view));
187 if (fx_info->view == (CacheView **) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000188 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
cristya2262262011-03-11 02:50:37 +0000189 i=0;
cristy0ea377f2011-03-24 00:54:19 +0000190 next=GetFirstImageInList(fx_info->images);
191 for ( ; next != (Image *) NULL; next=next->next)
cristy3ed852e2009-09-05 21:47:34 +0000192 {
cristy46ff2672012-12-14 15:32:26 +0000193 fx_info->view[i]=AcquireVirtualCacheView(next,exception);
cristya2262262011-03-11 02:50:37 +0000194 i++;
cristy3ed852e2009-09-05 21:47:34 +0000195 }
196 fx_info->random_info=AcquireRandomInfo();
197 fx_info->expression=ConstantString(expression);
198 fx_info->file=stderr;
199 (void) SubstituteString(&fx_info->expression," ",""); /* compact string */
cristy37af0912011-05-23 16:09:42 +0000200 /*
201 Force right-to-left associativity for unary negation.
202 */
203 (void) SubstituteString(&fx_info->expression,"-","-1.0*");
cristy422d5502012-12-22 22:20:57 +0000204 (void) SubstituteString(&fx_info->expression,"^-1.0*","^-");
cristy8fbb96d2012-07-18 01:18:52 +0000205 (void) SubstituteString(&fx_info->expression,"E-1.0*","E-");
206 (void) SubstituteString(&fx_info->expression,"e-1.0*","e-");
cristy8b8a3ae2010-10-23 18:49:46 +0000207 /*
cristydf751512013-08-17 14:19:12 +0000208 Convert compound to simple operators.
cristy3ed852e2009-09-05 21:47:34 +0000209 */
210 fx_op[1]='\0';
cristy41acfc72013-11-14 16:47:53 +0000211 *fx_op=(char) LeftShiftOperator;
cristy3ed852e2009-09-05 21:47:34 +0000212 (void) SubstituteString(&fx_info->expression,"<<",fx_op);
cristy41acfc72013-11-14 16:47:53 +0000213 *fx_op=(char) RightShiftOperator;
cristy3ed852e2009-09-05 21:47:34 +0000214 (void) SubstituteString(&fx_info->expression,">>",fx_op);
cristy41acfc72013-11-14 16:47:53 +0000215 *fx_op=(char) LessThanEqualOperator;
cristy3ed852e2009-09-05 21:47:34 +0000216 (void) SubstituteString(&fx_info->expression,"<=",fx_op);
cristy41acfc72013-11-14 16:47:53 +0000217 *fx_op=(char) GreaterThanEqualOperator;
cristy3ed852e2009-09-05 21:47:34 +0000218 (void) SubstituteString(&fx_info->expression,">=",fx_op);
cristy41acfc72013-11-14 16:47:53 +0000219 *fx_op=(char) EqualOperator;
cristy3ed852e2009-09-05 21:47:34 +0000220 (void) SubstituteString(&fx_info->expression,"==",fx_op);
cristy41acfc72013-11-14 16:47:53 +0000221 *fx_op=(char) NotEqualOperator;
cristy3ed852e2009-09-05 21:47:34 +0000222 (void) SubstituteString(&fx_info->expression,"!=",fx_op);
cristy41acfc72013-11-14 16:47:53 +0000223 *fx_op=(char) LogicalAndOperator;
cristy3ed852e2009-09-05 21:47:34 +0000224 (void) SubstituteString(&fx_info->expression,"&&",fx_op);
cristy41acfc72013-11-14 16:47:53 +0000225 *fx_op=(char) LogicalOrOperator;
cristy3ed852e2009-09-05 21:47:34 +0000226 (void) SubstituteString(&fx_info->expression,"||",fx_op);
cristy41acfc72013-11-14 16:47:53 +0000227 *fx_op=(char) ExponentialNotation;
cristy116af162010-08-13 01:25:47 +0000228 (void) SubstituteString(&fx_info->expression,"**",fx_op);
cristy3ed852e2009-09-05 21:47:34 +0000229 return(fx_info);
230}
231
232/*
233%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234% %
235% %
236% %
237% A d d N o i s e I m a g e %
238% %
239% %
240% %
241%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
242%
243% AddNoiseImage() adds random noise to the image.
244%
245% The format of the AddNoiseImage method is:
246%
247% Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
cristy9ed1f812011-10-08 02:00:08 +0000248% const double attenuate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000249%
250% A description of each parameter follows:
251%
252% o image: the image.
253%
254% o channel: the channel type.
255%
256% o noise_type: The type of noise: Uniform, Gaussian, Multiplicative,
257% Impulse, Laplacian, or Poisson.
258%
cristy9ed1f812011-10-08 02:00:08 +0000259% o attenuate: attenuate the random distribution.
260%
cristy3ed852e2009-09-05 21:47:34 +0000261% o exception: return any errors or warnings in this structure.
262%
263*/
cristy9ed1f812011-10-08 02:00:08 +0000264MagickExport Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
265 const double attenuate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000266{
267#define AddNoiseImageTag "AddNoise/Image"
268
cristyfa112112010-01-04 17:48:07 +0000269 CacheView
270 *image_view,
271 *noise_view;
272
cristy3ed852e2009-09-05 21:47:34 +0000273 Image
274 *noise_image;
275
cristy3ed852e2009-09-05 21:47:34 +0000276 MagickBooleanType
277 status;
278
cristybb503372010-05-27 20:51:26 +0000279 MagickOffsetType
280 progress;
281
cristy3ed852e2009-09-05 21:47:34 +0000282 RandomInfo
cristyfa112112010-01-04 17:48:07 +0000283 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +0000284
cristybb503372010-05-27 20:51:26 +0000285 ssize_t
286 y;
287
glennrpf7659d72012-09-24 18:14:56 +0000288#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +0000289 unsigned long
290 key;
glennrpf7659d72012-09-24 18:14:56 +0000291#endif
cristy57340e02012-05-05 00:53:23 +0000292
cristy3ed852e2009-09-05 21:47:34 +0000293 /*
294 Initialize noise image attributes.
295 */
296 assert(image != (const Image *) NULL);
297 assert(image->signature == MagickSignature);
298 if (image->debug != MagickFalse)
299 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
300 assert(exception != (ExceptionInfo *) NULL);
301 assert(exception->signature == MagickSignature);
cristyb2145892011-10-10 00:55:32 +0000302 noise_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000303 if (noise_image == (Image *) NULL)
304 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000305 if (SetImageStorageClass(noise_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000306 {
cristy3ed852e2009-09-05 21:47:34 +0000307 noise_image=DestroyImage(noise_image);
308 return((Image *) NULL);
309 }
310 /*
311 Add noise in each row.
312 */
cristy3ed852e2009-09-05 21:47:34 +0000313 status=MagickTrue;
314 progress=0;
315 random_info=AcquireRandomInfoThreadSet();
cristy46ff2672012-12-14 15:32:26 +0000316 image_view=AcquireVirtualCacheView(image,exception);
317 noise_view=AcquireAuthenticCacheView(noise_image,exception);
cristy319a1e72010-02-21 15:13:11 +0000318#if defined(MAGICKCORE_OPENMP_SUPPORT)
glennrpf7659d72012-09-24 18:14:56 +0000319 key=GetRandomSecretKey(random_info[0]);
cristy57340e02012-05-05 00:53:23 +0000320 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +0000321 magick_threads(image,noise_image,image->rows,key == ~0UL)
cristy3ed852e2009-09-05 21:47:34 +0000322#endif
cristybb503372010-05-27 20:51:26 +0000323 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000324 {
cristy5c9e6f22010-09-17 17:31:01 +0000325 const int
326 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +0000327
cristy3ed852e2009-09-05 21:47:34 +0000328 MagickBooleanType
329 sync;
330
cristy4c08aed2011-07-01 19:47:50 +0000331 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000332 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000333
cristybb503372010-05-27 20:51:26 +0000334 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000335 x;
336
cristy4c08aed2011-07-01 19:47:50 +0000337 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000338 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000339
340 if (status == MagickFalse)
341 continue;
342 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +0000343 q=QueueCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +0000344 exception);
cristy4c08aed2011-07-01 19:47:50 +0000345 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000346 {
347 status=MagickFalse;
348 continue;
349 }
cristybb503372010-05-27 20:51:26 +0000350 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000351 {
cristy850b3072011-10-08 01:38:05 +0000352 register ssize_t
353 i;
354
355 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
356 {
cristy5a23c552013-02-13 14:34:28 +0000357 PixelChannel channel=GetPixelChannelChannel(image,i);
358 PixelTrait traits=GetPixelChannelTraits(image,channel);
359 PixelTrait noise_traits=GetPixelChannelTraits(noise_image,channel);
cristy850b3072011-10-08 01:38:05 +0000360 if ((traits == UndefinedPixelTrait) ||
361 (noise_traits == UndefinedPixelTrait))
362 continue;
cristy1eced092012-08-10 23:10:56 +0000363 if (((noise_traits & CopyPixelTrait) != 0) ||
cristy883fde12013-04-08 00:50:13 +0000364 (GetPixelReadMask(image,p) == 0))
cristyb2145892011-10-10 00:55:32 +0000365 {
366 SetPixelChannel(noise_image,channel,p[i],q);
367 continue;
368 }
cristy850b3072011-10-08 01:38:05 +0000369 SetPixelChannel(noise_image,channel,ClampToQuantum(
370 GenerateDifferentialNoise(random_info[id],p[i],noise_type,attenuate)),
371 q);
372 }
cristyed231572011-07-14 02:18:59 +0000373 p+=GetPixelChannels(image);
374 q+=GetPixelChannels(noise_image);
cristy3ed852e2009-09-05 21:47:34 +0000375 }
376 sync=SyncCacheViewAuthenticPixels(noise_view,exception);
377 if (sync == MagickFalse)
378 status=MagickFalse;
379 if (image->progress_monitor != (MagickProgressMonitor) NULL)
380 {
381 MagickBooleanType
382 proceed;
383
cristyb5d5f722009-11-04 03:03:49 +0000384#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy319a1e72010-02-21 15:13:11 +0000385 #pragma omp critical (MagickCore_AddNoiseImage)
cristy3ed852e2009-09-05 21:47:34 +0000386#endif
387 proceed=SetImageProgress(image,AddNoiseImageTag,progress++,
388 image->rows);
389 if (proceed == MagickFalse)
390 status=MagickFalse;
391 }
392 }
393 noise_view=DestroyCacheView(noise_view);
394 image_view=DestroyCacheView(image_view);
395 random_info=DestroyRandomInfoThreadSet(random_info);
396 if (status == MagickFalse)
397 noise_image=DestroyImage(noise_image);
398 return(noise_image);
399}
400
401/*
402%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
403% %
404% %
405% %
406% B l u e S h i f t I m a g e %
407% %
408% %
409% %
410%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
411%
412% BlueShiftImage() mutes the colors of the image to simulate a scene at
413% nighttime in the moonlight.
414%
415% The format of the BlueShiftImage method is:
416%
417% Image *BlueShiftImage(const Image *image,const double factor,
418% ExceptionInfo *exception)
419%
420% A description of each parameter follows:
421%
422% o image: the image.
423%
424% o factor: the shift factor.
425%
426% o exception: return any errors or warnings in this structure.
427%
428*/
429MagickExport Image *BlueShiftImage(const Image *image,const double factor,
430 ExceptionInfo *exception)
431{
432#define BlueShiftImageTag "BlueShift/Image"
433
cristyc4c8d132010-01-07 01:58:38 +0000434 CacheView
435 *image_view,
436 *shift_view;
437
cristy3ed852e2009-09-05 21:47:34 +0000438 Image
439 *shift_image;
440
cristy3ed852e2009-09-05 21:47:34 +0000441 MagickBooleanType
442 status;
443
cristybb503372010-05-27 20:51:26 +0000444 MagickOffsetType
445 progress;
446
447 ssize_t
448 y;
449
cristy3ed852e2009-09-05 21:47:34 +0000450 /*
451 Allocate blue shift image.
452 */
453 assert(image != (const Image *) NULL);
454 assert(image->signature == MagickSignature);
455 if (image->debug != MagickFalse)
456 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
457 assert(exception != (ExceptionInfo *) NULL);
458 assert(exception->signature == MagickSignature);
cristya6d7a9b2012-01-18 20:04:48 +0000459 shift_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000460 if (shift_image == (Image *) NULL)
461 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000462 if (SetImageStorageClass(shift_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000463 {
cristy3ed852e2009-09-05 21:47:34 +0000464 shift_image=DestroyImage(shift_image);
465 return((Image *) NULL);
466 }
467 /*
468 Blue-shift DirectClass image.
469 */
470 status=MagickTrue;
471 progress=0;
cristy46ff2672012-12-14 15:32:26 +0000472 image_view=AcquireVirtualCacheView(image,exception);
473 shift_view=AcquireAuthenticCacheView(shift_image,exception);
cristy319a1e72010-02-21 15:13:11 +0000474#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000475 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +0000476 magick_threads(image,shift_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000477#endif
cristybb503372010-05-27 20:51:26 +0000478 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000479 {
480 MagickBooleanType
481 sync;
482
cristy4c08aed2011-07-01 19:47:50 +0000483 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000484 pixel;
485
486 Quantum
487 quantum;
488
cristy4c08aed2011-07-01 19:47:50 +0000489 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000490 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000491
cristybb503372010-05-27 20:51:26 +0000492 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000493 x;
494
cristy4c08aed2011-07-01 19:47:50 +0000495 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000496 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000497
498 if (status == MagickFalse)
499 continue;
500 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
501 q=QueueCacheViewAuthenticPixels(shift_view,0,y,shift_image->columns,1,
502 exception);
cristy4c08aed2011-07-01 19:47:50 +0000503 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000504 {
505 status=MagickFalse;
506 continue;
507 }
cristybb503372010-05-27 20:51:26 +0000508 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000509 {
cristy4c08aed2011-07-01 19:47:50 +0000510 quantum=GetPixelRed(image,p);
511 if (GetPixelGreen(image,p) < quantum)
512 quantum=GetPixelGreen(image,p);
513 if (GetPixelBlue(image,p) < quantum)
514 quantum=GetPixelBlue(image,p);
515 pixel.red=0.5*(GetPixelRed(image,p)+factor*quantum);
516 pixel.green=0.5*(GetPixelGreen(image,p)+factor*quantum);
517 pixel.blue=0.5*(GetPixelBlue(image,p)+factor*quantum);
518 quantum=GetPixelRed(image,p);
519 if (GetPixelGreen(image,p) > quantum)
520 quantum=GetPixelGreen(image,p);
521 if (GetPixelBlue(image,p) > quantum)
522 quantum=GetPixelBlue(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000523 pixel.red=0.5*(pixel.red+factor*quantum);
524 pixel.green=0.5*(pixel.green+factor*quantum);
525 pixel.blue=0.5*(pixel.blue+factor*quantum);
cristy4c08aed2011-07-01 19:47:50 +0000526 SetPixelRed(shift_image,ClampToQuantum(pixel.red),q);
527 SetPixelGreen(shift_image,ClampToQuantum(pixel.green),q);
528 SetPixelBlue(shift_image,ClampToQuantum(pixel.blue),q);
cristyed231572011-07-14 02:18:59 +0000529 p+=GetPixelChannels(image);
530 q+=GetPixelChannels(shift_image);
cristy3ed852e2009-09-05 21:47:34 +0000531 }
532 sync=SyncCacheViewAuthenticPixels(shift_view,exception);
533 if (sync == MagickFalse)
534 status=MagickFalse;
535 if (image->progress_monitor != (MagickProgressMonitor) NULL)
536 {
537 MagickBooleanType
538 proceed;
539
cristy319a1e72010-02-21 15:13:11 +0000540#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +0000541 #pragma omp critical (MagickCore_BlueShiftImage)
cristy3ed852e2009-09-05 21:47:34 +0000542#endif
543 proceed=SetImageProgress(image,BlueShiftImageTag,progress++,
544 image->rows);
545 if (proceed == MagickFalse)
546 status=MagickFalse;
547 }
548 }
549 image_view=DestroyCacheView(image_view);
550 shift_view=DestroyCacheView(shift_view);
551 if (status == MagickFalse)
552 shift_image=DestroyImage(shift_image);
553 return(shift_image);
554}
555
556/*
557%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
558% %
559% %
560% %
561% C h a r c o a l I m a g e %
562% %
563% %
564% %
565%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
566%
567% CharcoalImage() creates a new image that is a copy of an existing one with
568% the edge highlighted. It allocates the memory necessary for the new Image
569% structure and returns a pointer to the new image.
570%
571% The format of the CharcoalImage method is:
572%
573% Image *CharcoalImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +0000574% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000575%
576% A description of each parameter follows:
577%
578% o image: the image.
579%
580% o radius: the radius of the pixel neighborhood.
581%
582% o sigma: the standard deviation of the Gaussian, in pixels.
583%
584% o exception: return any errors or warnings in this structure.
585%
586*/
587MagickExport Image *CharcoalImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +0000588 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000589{
590 Image
591 *charcoal_image,
592 *clone_image,
593 *edge_image;
594
595 assert(image != (Image *) NULL);
596 assert(image->signature == MagickSignature);
597 if (image->debug != MagickFalse)
598 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
599 assert(exception != (ExceptionInfo *) NULL);
600 assert(exception->signature == MagickSignature);
601 clone_image=CloneImage(image,0,0,MagickTrue,exception);
602 if (clone_image == (Image *) NULL)
603 return((Image *) NULL);
cristyb2f4d5d2013-05-20 20:10:33 +0000604 edge_image=EdgeImage(clone_image,radius,exception);
cristy3ed852e2009-09-05 21:47:34 +0000605 clone_image=DestroyImage(clone_image);
606 if (edge_image == (Image *) NULL)
607 return((Image *) NULL);
cristyaa2c16c2012-03-25 22:21:35 +0000608 charcoal_image=BlurImage(edge_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000609 edge_image=DestroyImage(edge_image);
610 if (charcoal_image == (Image *) NULL)
611 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +0000612 (void) NormalizeImage(charcoal_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +0000613 (void) NegateImage(charcoal_image,MagickFalse,exception);
cristye247eff2013-04-15 11:50:40 +0000614 (void) GrayscaleImage(charcoal_image,image->intensity,exception);
cristy3ed852e2009-09-05 21:47:34 +0000615 return(charcoal_image);
616}
617
618/*
619%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
620% %
621% %
622% %
623% C o l o r i z e I m a g e %
624% %
625% %
626% %
627%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
628%
629% ColorizeImage() blends the fill color with each pixel in the image.
630% A percentage blend is specified with opacity. Control the application
631% of different color components by specifying a different percentage for
632% each component (e.g. 90/100/10 is 90% red, 100% green, and 10% blue).
633%
634% The format of the ColorizeImage method is:
635%
cristyc7e6ff62011-10-03 13:46:11 +0000636% Image *ColorizeImage(const Image *image,const char *blend,
637% const PixelInfo *colorize,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000638%
639% A description of each parameter follows:
640%
641% o image: the image.
642%
cristyc7e6ff62011-10-03 13:46:11 +0000643% o blend: A character string indicating the level of blending as a
cristy3ed852e2009-09-05 21:47:34 +0000644% percentage.
645%
646% o colorize: A color value.
647%
648% o exception: return any errors or warnings in this structure.
649%
650*/
cristyc7e6ff62011-10-03 13:46:11 +0000651MagickExport Image *ColorizeImage(const Image *image,const char *blend,
652 const PixelInfo *colorize,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000653{
654#define ColorizeImageTag "Colorize/Image"
cristy0cf6da52012-08-25 00:35:24 +0000655#define Colorize(pixel,blend_percentage,colorize) \
656 (((pixel)*(100.0-(blend_percentage))+(colorize)*(blend_percentage))/100.0)
cristy3ed852e2009-09-05 21:47:34 +0000657
cristyc4c8d132010-01-07 01:58:38 +0000658 CacheView
659 *colorize_view,
660 *image_view;
661
cristy3ed852e2009-09-05 21:47:34 +0000662 GeometryInfo
663 geometry_info;
664
665 Image
666 *colorize_image;
667
cristy3ed852e2009-09-05 21:47:34 +0000668 MagickBooleanType
669 status;
670
cristybb503372010-05-27 20:51:26 +0000671 MagickOffsetType
672 progress;
673
cristy3ed852e2009-09-05 21:47:34 +0000674 MagickStatusType
675 flags;
676
cristyc7e6ff62011-10-03 13:46:11 +0000677 PixelInfo
cristy20c3aed2012-04-10 01:06:21 +0000678 blend_percentage;
cristyc7e6ff62011-10-03 13:46:11 +0000679
cristybb503372010-05-27 20:51:26 +0000680 ssize_t
681 y;
682
cristy3ed852e2009-09-05 21:47:34 +0000683 /*
684 Allocate colorized image.
685 */
686 assert(image != (const Image *) NULL);
687 assert(image->signature == MagickSignature);
688 if (image->debug != MagickFalse)
689 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
690 assert(exception != (ExceptionInfo *) NULL);
691 assert(exception->signature == MagickSignature);
cristy768165d2012-04-09 15:01:35 +0000692 colorize_image=CloneImage(image,image->columns,image->rows,MagickTrue,
693 exception);
694 if (colorize_image == (Image *) NULL)
695 return((Image *) NULL);
696 if (SetImageStorageClass(colorize_image,DirectClass,exception) == MagickFalse)
697 {
698 colorize_image=DestroyImage(colorize_image);
699 return((Image *) NULL);
700 }
cristyf8da9f92013-04-16 19:52:28 +0000701 if ((IsGrayColorspace(image->colorspace) != MagickFalse) ||
cristy4dab3802013-03-15 22:08:15 +0000702 (IsPixelInfoGray(colorize) != MagickFalse))
cristya6400b12013-03-15 12:20:18 +0000703 (void) SetImageColorspace(colorize_image,sRGBColorspace,exception);
cristy8a46d822012-08-28 23:32:39 +0000704 if ((colorize_image->alpha_trait != BlendPixelTrait) &&
705 (colorize->alpha_trait == BlendPixelTrait))
cristy768165d2012-04-09 15:01:35 +0000706 (void) SetImageAlpha(colorize_image,OpaqueAlpha,exception);
707 if (blend == (const char *) NULL)
708 return(colorize_image);
cristy20c3aed2012-04-10 01:06:21 +0000709 GetPixelInfo(image,&blend_percentage);
710 flags=ParseGeometry(blend,&geometry_info);
711 blend_percentage.red=geometry_info.rho;
712 blend_percentage.green=geometry_info.rho;
713 blend_percentage.blue=geometry_info.rho;
714 blend_percentage.black=geometry_info.rho;
cristy3ee7de52012-04-14 23:40:47 +0000715 blend_percentage.alpha=geometry_info.rho;
cristy20c3aed2012-04-10 01:06:21 +0000716 if ((flags & SigmaValue) != 0)
717 blend_percentage.green=geometry_info.sigma;
718 if ((flags & XiValue) != 0)
719 blend_percentage.blue=geometry_info.xi;
720 if ((flags & PsiValue) != 0)
721 blend_percentage.alpha=geometry_info.psi;
722 if (blend_percentage.colorspace == CMYKColorspace)
723 {
724 if ((flags & PsiValue) != 0)
725 blend_percentage.black=geometry_info.psi;
726 if ((flags & ChiValue) != 0)
727 blend_percentage.alpha=geometry_info.chi;
728 }
cristy3ed852e2009-09-05 21:47:34 +0000729 /*
730 Colorize DirectClass image.
731 */
732 status=MagickTrue;
733 progress=0;
cristy46ff2672012-12-14 15:32:26 +0000734 image_view=AcquireVirtualCacheView(image,exception);
735 colorize_view=AcquireAuthenticCacheView(colorize_image,exception);
cristy319a1e72010-02-21 15:13:11 +0000736#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000737 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +0000738 magick_threads(image,colorize_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000739#endif
cristybb503372010-05-27 20:51:26 +0000740 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000741 {
742 MagickBooleanType
743 sync;
744
cristy4c08aed2011-07-01 19:47:50 +0000745 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000746 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000747
cristy4c08aed2011-07-01 19:47:50 +0000748 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000749 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000750
cristy4a37c622012-08-23 18:47:56 +0000751 register ssize_t
752 x;
753
cristy3ed852e2009-09-05 21:47:34 +0000754 if (status == MagickFalse)
755 continue;
756 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
757 q=QueueCacheViewAuthenticPixels(colorize_view,0,y,colorize_image->columns,1,
758 exception);
cristy4c08aed2011-07-01 19:47:50 +0000759 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000760 {
761 status=MagickFalse;
762 continue;
763 }
cristybb503372010-05-27 20:51:26 +0000764 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000765 {
cristy0cf6da52012-08-25 00:35:24 +0000766 register ssize_t
767 i;
768
769 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
770 {
cristy5a23c552013-02-13 14:34:28 +0000771 PixelChannel channel=GetPixelChannelChannel(image,i);
772 PixelTrait traits=GetPixelChannelTraits(image,channel);
773 PixelTrait colorize_traits=GetPixelChannelTraits(colorize_image,
774 channel);
cristy0cf6da52012-08-25 00:35:24 +0000775 if ((traits == UndefinedPixelTrait) ||
776 (colorize_traits == UndefinedPixelTrait))
cristyd6803382012-04-10 01:41:25 +0000777 continue;
cristy0cf6da52012-08-25 00:35:24 +0000778 if (((colorize_traits & CopyPixelTrait) != 0) ||
cristy883fde12013-04-08 00:50:13 +0000779 (GetPixelReadMask(image,p) == 0))
cristy0cf6da52012-08-25 00:35:24 +0000780 {
781 SetPixelChannel(colorize_image,channel,p[i],q);
782 continue;
783 }
cristyb7113232013-02-15 00:35:19 +0000784 SetPixelChannel(colorize_image,channel,ClampToQuantum(Colorize(p[i],
785 GetPixelInfoChannel(&blend_percentage,channel),GetPixelInfoChannel(
786 colorize,channel))),q);
cristy0cf6da52012-08-25 00:35:24 +0000787 }
cristyed231572011-07-14 02:18:59 +0000788 p+=GetPixelChannels(image);
789 q+=GetPixelChannels(colorize_image);
cristy3ed852e2009-09-05 21:47:34 +0000790 }
791 sync=SyncCacheViewAuthenticPixels(colorize_view,exception);
792 if (sync == MagickFalse)
793 status=MagickFalse;
794 if (image->progress_monitor != (MagickProgressMonitor) NULL)
795 {
796 MagickBooleanType
797 proceed;
798
cristy319a1e72010-02-21 15:13:11 +0000799#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +0000800 #pragma omp critical (MagickCore_ColorizeImage)
cristy3ed852e2009-09-05 21:47:34 +0000801#endif
802 proceed=SetImageProgress(image,ColorizeImageTag,progress++,image->rows);
803 if (proceed == MagickFalse)
804 status=MagickFalse;
805 }
806 }
807 image_view=DestroyCacheView(image_view);
808 colorize_view=DestroyCacheView(colorize_view);
809 if (status == MagickFalse)
810 colorize_image=DestroyImage(colorize_image);
811 return(colorize_image);
812}
813
814/*
815%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
816% %
817% %
818% %
cristye6365592010-04-02 17:31:23 +0000819% C o l o r M a t r i x I m a g e %
820% %
821% %
822% %
823%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
824%
825% ColorMatrixImage() applies color transformation to an image. This method
826% permits saturation changes, hue rotation, luminance to alpha, and various
827% other effects. Although variable-sized transformation matrices can be used,
828% typically one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA
829% (or RGBA with offsets). The matrix is similar to those used by Adobe Flash
830% except offsets are in column 6 rather than 5 (in support of CMYKA images)
831% and offsets are normalized (divide Flash offset by 255).
832%
833% The format of the ColorMatrixImage method is:
834%
835% Image *ColorMatrixImage(const Image *image,
836% const KernelInfo *color_matrix,ExceptionInfo *exception)
837%
838% A description of each parameter follows:
839%
840% o image: the image.
841%
842% o color_matrix: the color matrix.
843%
844% o exception: return any errors or warnings in this structure.
845%
846*/
anthonyfd706f92012-01-19 04:22:02 +0000847/* FUTURE: modify to make use of a MagickMatrix Mutliply function
848 That should be provided in "matrix.c"
849 (ASIDE: actually distorts should do this too but currently doesn't)
850*/
851
cristye6365592010-04-02 17:31:23 +0000852MagickExport Image *ColorMatrixImage(const Image *image,
853 const KernelInfo *color_matrix,ExceptionInfo *exception)
854{
855#define ColorMatrixImageTag "ColorMatrix/Image"
856
857 CacheView
858 *color_view,
859 *image_view;
860
861 double
862 ColorMatrix[6][6] =
863 {
864 { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
865 { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
866 { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
867 { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
868 { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
869 { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }
870 };
871
872 Image
873 *color_image;
874
cristye6365592010-04-02 17:31:23 +0000875 MagickBooleanType
876 status;
877
cristybb503372010-05-27 20:51:26 +0000878 MagickOffsetType
879 progress;
880
881 register ssize_t
cristye6365592010-04-02 17:31:23 +0000882 i;
883
cristybb503372010-05-27 20:51:26 +0000884 ssize_t
885 u,
886 v,
887 y;
888
cristye6365592010-04-02 17:31:23 +0000889 /*
anthonyfd706f92012-01-19 04:22:02 +0000890 Map given color_matrix, into a 6x6 matrix RGBKA and a constant
cristye6365592010-04-02 17:31:23 +0000891 */
892 assert(image != (Image *) NULL);
893 assert(image->signature == MagickSignature);
894 if (image->debug != MagickFalse)
895 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
896 assert(exception != (ExceptionInfo *) NULL);
897 assert(exception->signature == MagickSignature);
898 i=0;
cristybb503372010-05-27 20:51:26 +0000899 for (v=0; v < (ssize_t) color_matrix->height; v++)
900 for (u=0; u < (ssize_t) color_matrix->width; u++)
cristye6365592010-04-02 17:31:23 +0000901 {
902 if ((v < 6) && (u < 6))
903 ColorMatrix[v][u]=color_matrix->values[i];
904 i++;
905 }
906 /*
907 Initialize color image.
908 */
cristy12550e62010-06-07 12:46:40 +0000909 color_image=CloneImage(image,0,0,MagickTrue,exception);
cristye6365592010-04-02 17:31:23 +0000910 if (color_image == (Image *) NULL)
911 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000912 if (SetImageStorageClass(color_image,DirectClass,exception) == MagickFalse)
cristye6365592010-04-02 17:31:23 +0000913 {
cristye6365592010-04-02 17:31:23 +0000914 color_image=DestroyImage(color_image);
915 return((Image *) NULL);
916 }
917 if (image->debug != MagickFalse)
918 {
919 char
920 format[MaxTextExtent],
921 *message;
922
923 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
924 " ColorMatrix image with color matrix:");
925 message=AcquireString("");
926 for (v=0; v < 6; v++)
927 {
928 *message='\0';
cristyb51dff52011-05-19 16:55:47 +0000929 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristye6365592010-04-02 17:31:23 +0000930 (void) ConcatenateString(&message,format);
931 for (u=0; u < 6; u++)
932 {
cristyb51dff52011-05-19 16:55:47 +0000933 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",
cristye6365592010-04-02 17:31:23 +0000934 ColorMatrix[v][u]);
935 (void) ConcatenateString(&message,format);
936 }
937 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
938 }
939 message=DestroyString(message);
940 }
941 /*
anthonyfd706f92012-01-19 04:22:02 +0000942 Apply the ColorMatrix to image.
cristye6365592010-04-02 17:31:23 +0000943 */
944 status=MagickTrue;
945 progress=0;
cristy46ff2672012-12-14 15:32:26 +0000946 image_view=AcquireVirtualCacheView(image,exception);
947 color_view=AcquireAuthenticCacheView(color_image,exception);
cristye6365592010-04-02 17:31:23 +0000948#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000949 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +0000950 magick_threads(image,color_image,image->rows,1)
cristye6365592010-04-02 17:31:23 +0000951#endif
cristybb503372010-05-27 20:51:26 +0000952 for (y=0; y < (ssize_t) image->rows; y++)
cristye6365592010-04-02 17:31:23 +0000953 {
cristyfcc25d92012-02-19 23:06:48 +0000954 PixelInfo
cristye6365592010-04-02 17:31:23 +0000955 pixel;
956
cristy4c08aed2011-07-01 19:47:50 +0000957 register const Quantum
cristye6365592010-04-02 17:31:23 +0000958 *restrict p;
959
cristy4c08aed2011-07-01 19:47:50 +0000960 register Quantum
961 *restrict q;
962
cristybb503372010-05-27 20:51:26 +0000963 register ssize_t
cristye6365592010-04-02 17:31:23 +0000964 x;
965
cristye6365592010-04-02 17:31:23 +0000966 if (status == MagickFalse)
967 continue;
968 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
969 q=GetCacheViewAuthenticPixels(color_view,0,y,color_image->columns,1,
970 exception);
cristy4c08aed2011-07-01 19:47:50 +0000971 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristye6365592010-04-02 17:31:23 +0000972 {
973 status=MagickFalse;
974 continue;
975 }
cristyfcc25d92012-02-19 23:06:48 +0000976 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +0000977 for (x=0; x < (ssize_t) image->columns; x++)
cristye6365592010-04-02 17:31:23 +0000978 {
cristybb503372010-05-27 20:51:26 +0000979 register ssize_t
cristye6365592010-04-02 17:31:23 +0000980 v;
981
cristybb503372010-05-27 20:51:26 +0000982 size_t
cristye6365592010-04-02 17:31:23 +0000983 height;
984
cristyfcc25d92012-02-19 23:06:48 +0000985 GetPixelInfoPixel(image,p,&pixel);
cristye6365592010-04-02 17:31:23 +0000986 height=color_matrix->height > 6 ? 6UL : color_matrix->height;
cristybb503372010-05-27 20:51:26 +0000987 for (v=0; v < (ssize_t) height; v++)
cristye6365592010-04-02 17:31:23 +0000988 {
cristya19f1d72012-08-07 18:24:38 +0000989 double
cristyfcc25d92012-02-19 23:06:48 +0000990 sum;
991
992 sum=ColorMatrix[v][0]*GetPixelRed(image,p)+ColorMatrix[v][1]*
cristy4c08aed2011-07-01 19:47:50 +0000993 GetPixelGreen(image,p)+ColorMatrix[v][2]*GetPixelBlue(image,p);
cristye6365592010-04-02 17:31:23 +0000994 if (image->colorspace == CMYKColorspace)
cristyfcc25d92012-02-19 23:06:48 +0000995 sum+=ColorMatrix[v][3]*GetPixelBlack(image,p);
cristy8a46d822012-08-28 23:32:39 +0000996 if (image->alpha_trait == BlendPixelTrait)
cristyfcc25d92012-02-19 23:06:48 +0000997 sum+=ColorMatrix[v][4]*GetPixelAlpha(image,p);
998 sum+=QuantumRange*ColorMatrix[v][5];
cristye6365592010-04-02 17:31:23 +0000999 switch (v)
1000 {
cristyfcc25d92012-02-19 23:06:48 +00001001 case 0: pixel.red=sum; break;
1002 case 1: pixel.green=sum; break;
1003 case 2: pixel.blue=sum; break;
1004 case 3: pixel.black=sum; break;
1005 case 4: pixel.alpha=sum; break;
1006 default: break;
cristye6365592010-04-02 17:31:23 +00001007 }
1008 }
cristyfcc25d92012-02-19 23:06:48 +00001009 SetPixelInfoPixel(color_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00001010 p+=GetPixelChannels(image);
1011 q+=GetPixelChannels(color_image);
cristye6365592010-04-02 17:31:23 +00001012 }
1013 if (SyncCacheViewAuthenticPixels(color_view,exception) == MagickFalse)
1014 status=MagickFalse;
1015 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1016 {
1017 MagickBooleanType
1018 proceed;
1019
1020#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00001021 #pragma omp critical (MagickCore_ColorMatrixImage)
cristye6365592010-04-02 17:31:23 +00001022#endif
1023 proceed=SetImageProgress(image,ColorMatrixImageTag,progress++,
1024 image->rows);
1025 if (proceed == MagickFalse)
1026 status=MagickFalse;
1027 }
1028 }
1029 color_view=DestroyCacheView(color_view);
1030 image_view=DestroyCacheView(image_view);
1031 if (status == MagickFalse)
1032 color_image=DestroyImage(color_image);
1033 return(color_image);
1034}
1035
1036/*
1037%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1038% %
1039% %
1040% %
cristy3ed852e2009-09-05 21:47:34 +00001041+ D e s t r o y F x I n f o %
1042% %
1043% %
1044% %
1045%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1046%
1047% DestroyFxInfo() deallocates memory associated with an FxInfo structure.
1048%
1049% The format of the DestroyFxInfo method is:
1050%
1051% ImageInfo *DestroyFxInfo(ImageInfo *fx_info)
1052%
1053% A description of each parameter follows:
1054%
1055% o fx_info: the fx info.
1056%
1057*/
cristy7832dc22011-09-05 01:21:53 +00001058MagickPrivate FxInfo *DestroyFxInfo(FxInfo *fx_info)
cristy3ed852e2009-09-05 21:47:34 +00001059{
cristybb503372010-05-27 20:51:26 +00001060 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001061 i;
1062
1063 fx_info->exception=DestroyExceptionInfo(fx_info->exception);
1064 fx_info->expression=DestroyString(fx_info->expression);
1065 fx_info->symbols=DestroySplayTree(fx_info->symbols);
1066 fx_info->colors=DestroySplayTree(fx_info->colors);
cristy0ea377f2011-03-24 00:54:19 +00001067 for (i=(ssize_t) GetImageListLength(fx_info->images)-1; i >= 0; i--)
cristyd76c51e2011-03-26 00:21:26 +00001068 fx_info->view[i]=DestroyCacheView(fx_info->view[i]);
1069 fx_info->view=(CacheView **) RelinquishMagickMemory(fx_info->view);
cristy3ed852e2009-09-05 21:47:34 +00001070 fx_info->random_info=DestroyRandomInfo(fx_info->random_info);
1071 fx_info=(FxInfo *) RelinquishMagickMemory(fx_info);
1072 return(fx_info);
1073}
1074
1075/*
1076%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1077% %
1078% %
1079% %
cristy3ed852e2009-09-05 21:47:34 +00001080+ 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 %
1081% %
1082% %
1083% %
1084%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1085%
1086% FxEvaluateChannelExpression() evaluates an expression and returns the
1087% results.
1088%
1089% The format of the FxEvaluateExpression method is:
1090%
cristya19f1d72012-08-07 18:24:38 +00001091% double FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00001092% const PixelChannel channel,const ssize_t x,const ssize_t y,
cristya19f1d72012-08-07 18:24:38 +00001093% double *alpha,Exceptioninfo *exception)
1094% double FxEvaluateExpression(FxInfo *fx_info,
1095% double *alpha,Exceptioninfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001096%
1097% A description of each parameter follows:
1098%
1099% o fx_info: the fx info.
1100%
1101% o channel: the channel.
1102%
1103% o x,y: the pixel position.
1104%
1105% o alpha: the result.
1106%
1107% o exception: return any errors or warnings in this structure.
1108%
1109*/
1110
cristy351842f2010-03-07 15:27:38 +00001111static inline double MagickMax(const double x,const double y)
1112{
1113 if (x > y)
1114 return(x);
1115 return(y);
1116}
1117
1118static inline double MagickMin(const double x,const double y)
1119{
1120 if (x < y)
1121 return(x);
1122 return(y);
1123}
1124
cristya19f1d72012-08-07 18:24:38 +00001125static double FxChannelStatistics(FxInfo *fx_info,Image *image,
cristy0568ffc2011-07-25 16:54:14 +00001126 PixelChannel channel,const char *symbol,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001127{
cristy5048d302012-08-07 01:05:16 +00001128 ChannelType
1129 channel_mask;
1130
cristy3ed852e2009-09-05 21:47:34 +00001131 char
1132 key[MaxTextExtent],
1133 statistic[MaxTextExtent];
1134
1135 const char
1136 *value;
1137
1138 register const char
1139 *p;
1140
cristy5048d302012-08-07 01:05:16 +00001141 channel_mask=UndefinedChannel;
cristy3ed852e2009-09-05 21:47:34 +00001142 for (p=symbol; (*p != '.') && (*p != '\0'); p++) ;
1143 if (*p == '.')
cristy3ed852e2009-09-05 21:47:34 +00001144 {
cristy5048d302012-08-07 01:05:16 +00001145 ssize_t
1146 option;
1147
1148 option=ParseCommandOption(MagickPixelChannelOptions,MagickTrue,p+1);
1149 if (option >= 0)
1150 {
1151 channel=(PixelChannel) option;
1152 channel_mask=(ChannelType) (channel_mask | (1 << channel));
cristycf1296e2012-08-26 23:40:49 +00001153 SetPixelChannelMask(image,channel_mask);
cristy5048d302012-08-07 01:05:16 +00001154 }
cristy3ed852e2009-09-05 21:47:34 +00001155 }
cristyb51dff52011-05-19 16:55:47 +00001156 (void) FormatLocaleString(key,MaxTextExtent,"%p.%.20g.%s",(void *) image,
cristye8c25f92010-06-03 00:53:06 +00001157 (double) channel,symbol);
cristy3ed852e2009-09-05 21:47:34 +00001158 value=(const char *) GetValueFromSplayTree(fx_info->symbols,key);
1159 if (value != (const char *) NULL)
cristy5048d302012-08-07 01:05:16 +00001160 {
1161 if (channel_mask != UndefinedChannel)
cristycf1296e2012-08-26 23:40:49 +00001162 SetPixelChannelMask(image,channel_mask);
cristy5048d302012-08-07 01:05:16 +00001163 return(QuantumScale*StringToDouble(value,(char **) NULL));
1164 }
cristy3ed852e2009-09-05 21:47:34 +00001165 (void) DeleteNodeFromSplayTree(fx_info->symbols,key);
1166 if (LocaleNCompare(symbol,"depth",5) == 0)
1167 {
cristybb503372010-05-27 20:51:26 +00001168 size_t
cristy3ed852e2009-09-05 21:47:34 +00001169 depth;
1170
cristyfefab1b2011-07-05 00:33:22 +00001171 depth=GetImageDepth(image,exception);
1172 (void) FormatLocaleString(statistic,MaxTextExtent,"%.20g",(double) depth);
cristy3ed852e2009-09-05 21:47:34 +00001173 }
1174 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1175 {
1176 double
1177 kurtosis,
1178 skewness;
1179
cristyd42d9952011-07-08 14:21:50 +00001180 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001181 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",kurtosis);
cristy3ed852e2009-09-05 21:47:34 +00001182 }
1183 if (LocaleNCompare(symbol,"maxima",6) == 0)
1184 {
1185 double
1186 maxima,
1187 minima;
1188
cristyd42d9952011-07-08 14:21:50 +00001189 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001190 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",maxima);
cristy3ed852e2009-09-05 21:47:34 +00001191 }
1192 if (LocaleNCompare(symbol,"mean",4) == 0)
1193 {
1194 double
1195 mean,
1196 standard_deviation;
1197
cristyd42d9952011-07-08 14:21:50 +00001198 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001199 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",mean);
cristy3ed852e2009-09-05 21:47:34 +00001200 }
1201 if (LocaleNCompare(symbol,"minima",6) == 0)
1202 {
1203 double
1204 maxima,
1205 minima;
1206
cristyd42d9952011-07-08 14:21:50 +00001207 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001208 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",minima);
cristy3ed852e2009-09-05 21:47:34 +00001209 }
1210 if (LocaleNCompare(symbol,"skewness",8) == 0)
1211 {
1212 double
1213 kurtosis,
1214 skewness;
1215
cristyd42d9952011-07-08 14:21:50 +00001216 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001217 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",skewness);
cristy3ed852e2009-09-05 21:47:34 +00001218 }
1219 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1220 {
1221 double
1222 mean,
1223 standard_deviation;
1224
cristyd42d9952011-07-08 14:21:50 +00001225 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001226 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",
cristy3ed852e2009-09-05 21:47:34 +00001227 standard_deviation);
1228 }
cristy5048d302012-08-07 01:05:16 +00001229 if (channel_mask != UndefinedChannel)
cristycf1296e2012-08-26 23:40:49 +00001230 SetPixelChannelMask(image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00001231 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(key),
1232 ConstantString(statistic));
cristydbdd0e32011-11-04 23:29:40 +00001233 return(QuantumScale*StringToDouble(statistic,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001234}
1235
cristya19f1d72012-08-07 18:24:38 +00001236static double
cristy0568ffc2011-07-25 16:54:14 +00001237 FxEvaluateSubexpression(FxInfo *,const PixelChannel,const ssize_t,
cristya19f1d72012-08-07 18:24:38 +00001238 const ssize_t,const char *,double *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +00001239
cristyb0aad4c2011-11-02 19:30:35 +00001240static MagickOffsetType FxGCD(MagickOffsetType alpha,MagickOffsetType beta)
1241{
1242 if (beta != 0)
1243 return(FxGCD(beta,alpha % beta));
1244 return(alpha);
1245}
1246
cristy3ed852e2009-09-05 21:47:34 +00001247static inline const char *FxSubexpression(const char *expression,
1248 ExceptionInfo *exception)
1249{
1250 const char
1251 *subexpression;
1252
cristybb503372010-05-27 20:51:26 +00001253 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001254 level;
1255
1256 level=0;
1257 subexpression=expression;
1258 while ((*subexpression != '\0') &&
1259 ((level != 1) || (strchr(")",(int) *subexpression) == (char *) NULL)))
1260 {
1261 if (strchr("(",(int) *subexpression) != (char *) NULL)
1262 level++;
1263 else
1264 if (strchr(")",(int) *subexpression) != (char *) NULL)
1265 level--;
1266 subexpression++;
1267 }
1268 if (*subexpression == '\0')
1269 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
cristyefe601c2013-01-05 17:51:12 +00001270 "UnbalancedParenthesis","`%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00001271 return(subexpression);
1272}
1273
cristya19f1d72012-08-07 18:24:38 +00001274static double FxGetSymbol(FxInfo *fx_info,const PixelChannel channel,
cristye85007d2010-06-06 22:51:36 +00001275 const ssize_t x,const ssize_t y,const char *expression,
1276 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001277{
1278 char
1279 *q,
1280 subexpression[MaxTextExtent],
1281 symbol[MaxTextExtent];
1282
1283 const char
1284 *p,
1285 *value;
1286
1287 Image
1288 *image;
1289
cristy4c08aed2011-07-01 19:47:50 +00001290 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001291 pixel;
1292
cristya19f1d72012-08-07 18:24:38 +00001293 double
cristy3ed852e2009-09-05 21:47:34 +00001294 alpha,
1295 beta;
1296
1297 PointInfo
1298 point;
1299
cristybb503372010-05-27 20:51:26 +00001300 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001301 i;
1302
1303 size_t
cristy1707c6c2012-01-18 23:30:54 +00001304 length,
cristy3ed852e2009-09-05 21:47:34 +00001305 level;
1306
1307 p=expression;
1308 i=GetImageIndexInList(fx_info->images);
1309 level=0;
1310 point.x=(double) x;
1311 point.y=(double) y;
cristy02e64f82013-11-14 16:20:04 +00001312 if (isalpha((int) ((unsigned char) *(p+1))) == 0)
cristy3ed852e2009-09-05 21:47:34 +00001313 {
1314 if (strchr("suv",(int) *p) != (char *) NULL)
1315 {
1316 switch (*p)
1317 {
1318 case 's':
1319 default:
1320 {
1321 i=GetImageIndexInList(fx_info->images);
1322 break;
1323 }
1324 case 'u': i=0; break;
1325 case 'v': i=1; break;
1326 }
1327 p++;
1328 if (*p == '[')
1329 {
1330 level++;
1331 q=subexpression;
1332 for (p++; *p != '\0'; )
1333 {
1334 if (*p == '[')
1335 level++;
1336 else
1337 if (*p == ']')
1338 {
1339 level--;
1340 if (level == 0)
1341 break;
1342 }
1343 *q++=(*p++);
1344 }
1345 *q='\0';
1346 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1347 &beta,exception);
cristybb503372010-05-27 20:51:26 +00001348 i=(ssize_t) (alpha+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001349 p++;
1350 }
1351 if (*p == '.')
1352 p++;
1353 }
cristy02e64f82013-11-14 16:20:04 +00001354 if ((*p == 'p') && (isalpha((int) ((unsigned char) *(p+1))) == 0))
cristy3ed852e2009-09-05 21:47:34 +00001355 {
1356 p++;
1357 if (*p == '{')
1358 {
1359 level++;
1360 q=subexpression;
1361 for (p++; *p != '\0'; )
1362 {
1363 if (*p == '{')
1364 level++;
1365 else
1366 if (*p == '}')
1367 {
1368 level--;
1369 if (level == 0)
1370 break;
1371 }
1372 *q++=(*p++);
1373 }
1374 *q='\0';
1375 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1376 &beta,exception);
1377 point.x=alpha;
1378 point.y=beta;
1379 p++;
1380 }
1381 else
1382 if (*p == '[')
1383 {
1384 level++;
1385 q=subexpression;
1386 for (p++; *p != '\0'; )
1387 {
1388 if (*p == '[')
1389 level++;
1390 else
1391 if (*p == ']')
1392 {
1393 level--;
1394 if (level == 0)
1395 break;
1396 }
1397 *q++=(*p++);
1398 }
1399 *q='\0';
1400 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1401 &beta,exception);
1402 point.x+=alpha;
1403 point.y+=beta;
1404 p++;
1405 }
1406 if (*p == '.')
1407 p++;
1408 }
1409 }
1410 length=GetImageListLength(fx_info->images);
1411 while (i < 0)
cristybb503372010-05-27 20:51:26 +00001412 i+=(ssize_t) length;
cristy58a749e2014-05-25 17:36:53 +00001413 if (length != 0)
1414 i%=length;
cristy3ed852e2009-09-05 21:47:34 +00001415 image=GetImageFromList(fx_info->images,i);
1416 if (image == (Image *) NULL)
1417 {
1418 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
cristyefe601c2013-01-05 17:51:12 +00001419 "NoSuchImage","`%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00001420 return(0.0);
1421 }
cristy4c08aed2011-07-01 19:47:50 +00001422 GetPixelInfo(image,&pixel);
1423 (void) InterpolatePixelInfo(image,fx_info->view[i],image->interpolate,
cristy4f820712011-04-01 12:35:43 +00001424 point.x,point.y,&pixel,exception);
cristy1707c6c2012-01-18 23:30:54 +00001425 if ((strlen(p) > 2) && (LocaleCompare(p,"intensity") != 0) &&
cristyba213402013-06-28 14:05:12 +00001426 (LocaleCompare(p,"luma") != 0) && (LocaleCompare(p,"luminance") != 0) &&
1427 (LocaleCompare(p,"hue") != 0) && (LocaleCompare(p,"saturation") != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00001428 (LocaleCompare(p,"lightness") != 0))
1429 {
1430 char
1431 name[MaxTextExtent];
1432
1433 (void) CopyMagickString(name,p,MaxTextExtent);
1434 for (q=name+(strlen(name)-1); q > name; q--)
1435 {
1436 if (*q == ')')
1437 break;
1438 if (*q == '.')
1439 {
1440 *q='\0';
1441 break;
1442 }
1443 }
1444 if ((strlen(name) > 2) &&
1445 (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
1446 {
cristy4c08aed2011-07-01 19:47:50 +00001447 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001448 *color;
1449
cristy4c08aed2011-07-01 19:47:50 +00001450 color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
1451 if (color != (PixelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001452 {
1453 pixel=(*color);
1454 p+=strlen(name);
1455 }
1456 else
cristy1707c6c2012-01-18 23:30:54 +00001457 {
1458 MagickBooleanType
1459 status;
1460
1461 status=QueryColorCompliance(name,AllCompliance,&pixel,
1462 fx_info->exception);
1463 if (status != MagickFalse)
1464 {
1465 (void) AddValueToSplayTree(fx_info->colors,ConstantString(
1466 name),ClonePixelInfo(&pixel));
1467 p+=strlen(name);
1468 }
1469 }
cristy3ed852e2009-09-05 21:47:34 +00001470 }
1471 }
1472 (void) CopyMagickString(symbol,p,MaxTextExtent);
1473 StripString(symbol);
1474 if (*symbol == '\0')
1475 {
1476 switch (channel)
1477 {
cristy0568ffc2011-07-25 16:54:14 +00001478 case RedPixelChannel: return(QuantumScale*pixel.red);
1479 case GreenPixelChannel: return(QuantumScale*pixel.green);
1480 case BluePixelChannel: return(QuantumScale*pixel.blue);
1481 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001482 {
1483 if (image->colorspace != CMYKColorspace)
1484 {
1485 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001486 ImageError,"ColorSeparatedImageRequired","`%s'",
cristy3ed852e2009-09-05 21:47:34 +00001487 image->filename);
1488 return(0.0);
1489 }
cristy4c08aed2011-07-01 19:47:50 +00001490 return(QuantumScale*pixel.black);
1491 }
cristy0568ffc2011-07-25 16:54:14 +00001492 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001493 {
cristya19f1d72012-08-07 18:24:38 +00001494 double
cristy4c08aed2011-07-01 19:47:50 +00001495 alpha;
1496
cristy8a46d822012-08-28 23:32:39 +00001497 if (pixel.alpha_trait != BlendPixelTrait)
cristy4c08aed2011-07-01 19:47:50 +00001498 return(1.0);
cristya19f1d72012-08-07 18:24:38 +00001499 alpha=(double) (QuantumScale*pixel.alpha);
cristy4c08aed2011-07-01 19:47:50 +00001500 return(alpha);
cristy3ed852e2009-09-05 21:47:34 +00001501 }
cristya382aca2011-12-06 18:22:48 +00001502 case IndexPixelChannel:
1503 return(0.0);
cristyb3a73b52011-07-26 01:34:43 +00001504 case IntensityPixelChannel:
cristyf364ed42010-12-15 01:54:43 +00001505 {
cristy4c08aed2011-07-01 19:47:50 +00001506 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristyf364ed42010-12-15 01:54:43 +00001507 }
cristy3ed852e2009-09-05 21:47:34 +00001508 default:
1509 break;
1510 }
1511 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
cristyefe601c2013-01-05 17:51:12 +00001512 "UnableToParseExpression","`%s'",p);
cristy3ed852e2009-09-05 21:47:34 +00001513 return(0.0);
1514 }
1515 switch (*symbol)
1516 {
1517 case 'A':
1518 case 'a':
1519 {
1520 if (LocaleCompare(symbol,"a") == 0)
cristya19f1d72012-08-07 18:24:38 +00001521 return((double) (QuantumScale*pixel.alpha));
cristy3ed852e2009-09-05 21:47:34 +00001522 break;
1523 }
1524 case 'B':
1525 case 'b':
1526 {
1527 if (LocaleCompare(symbol,"b") == 0)
1528 return(QuantumScale*pixel.blue);
1529 break;
1530 }
1531 case 'C':
1532 case 'c':
1533 {
1534 if (LocaleNCompare(symbol,"channel",7) == 0)
1535 {
1536 GeometryInfo
1537 channel_info;
1538
1539 MagickStatusType
1540 flags;
1541
1542 flags=ParseGeometry(symbol+7,&channel_info);
1543 if (image->colorspace == CMYKColorspace)
1544 switch (channel)
1545 {
cristy0568ffc2011-07-25 16:54:14 +00001546 case CyanPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001547 {
1548 if ((flags & RhoValue) == 0)
1549 return(0.0);
1550 return(channel_info.rho);
1551 }
cristy0568ffc2011-07-25 16:54:14 +00001552 case MagentaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001553 {
1554 if ((flags & SigmaValue) == 0)
1555 return(0.0);
1556 return(channel_info.sigma);
1557 }
cristy0568ffc2011-07-25 16:54:14 +00001558 case YellowPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001559 {
1560 if ((flags & XiValue) == 0)
1561 return(0.0);
1562 return(channel_info.xi);
1563 }
cristy0568ffc2011-07-25 16:54:14 +00001564 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001565 {
1566 if ((flags & PsiValue) == 0)
1567 return(0.0);
1568 return(channel_info.psi);
1569 }
cristy0568ffc2011-07-25 16:54:14 +00001570 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001571 {
1572 if ((flags & ChiValue) == 0)
1573 return(0.0);
1574 return(channel_info.chi);
1575 }
1576 default:
1577 return(0.0);
1578 }
1579 switch (channel)
1580 {
cristy0568ffc2011-07-25 16:54:14 +00001581 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001582 {
1583 if ((flags & RhoValue) == 0)
1584 return(0.0);
1585 return(channel_info.rho);
1586 }
cristy0568ffc2011-07-25 16:54:14 +00001587 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001588 {
1589 if ((flags & SigmaValue) == 0)
1590 return(0.0);
1591 return(channel_info.sigma);
1592 }
cristy0568ffc2011-07-25 16:54:14 +00001593 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001594 {
1595 if ((flags & XiValue) == 0)
1596 return(0.0);
1597 return(channel_info.xi);
1598 }
cristy0568ffc2011-07-25 16:54:14 +00001599 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001600 {
1601 if ((flags & ChiValue) == 0)
1602 return(0.0);
1603 return(channel_info.chi);
1604 }
cristy0568ffc2011-07-25 16:54:14 +00001605 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001606 {
1607 if ((flags & PsiValue) == 0)
1608 return(0.0);
1609 return(channel_info.psi);
1610 }
cristy3ed852e2009-09-05 21:47:34 +00001611 default:
1612 return(0.0);
1613 }
cristy3ed852e2009-09-05 21:47:34 +00001614 }
1615 if (LocaleCompare(symbol,"c") == 0)
1616 return(QuantumScale*pixel.red);
1617 break;
1618 }
1619 case 'D':
1620 case 'd':
1621 {
1622 if (LocaleNCompare(symbol,"depth",5) == 0)
1623 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1624 break;
1625 }
1626 case 'G':
1627 case 'g':
1628 {
1629 if (LocaleCompare(symbol,"g") == 0)
1630 return(QuantumScale*pixel.green);
1631 break;
1632 }
1633 case 'K':
1634 case 'k':
1635 {
1636 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1637 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1638 if (LocaleCompare(symbol,"k") == 0)
1639 {
1640 if (image->colorspace != CMYKColorspace)
1641 {
1642 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001643 OptionError,"ColorSeparatedImageRequired","`%s'",
cristy3ed852e2009-09-05 21:47:34 +00001644 image->filename);
1645 return(0.0);
1646 }
cristy4c08aed2011-07-01 19:47:50 +00001647 return(QuantumScale*pixel.black);
cristy3ed852e2009-09-05 21:47:34 +00001648 }
1649 break;
1650 }
1651 case 'H':
1652 case 'h':
1653 {
1654 if (LocaleCompare(symbol,"h") == 0)
cristya19f1d72012-08-07 18:24:38 +00001655 return((double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001656 if (LocaleCompare(symbol,"hue") == 0)
1657 {
1658 double
1659 hue,
1660 lightness,
1661 saturation;
1662
cristy0a39a5c2012-06-27 12:51:45 +00001663 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
cristyda1f9c12011-10-02 21:39:49 +00001664 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001665 return(hue);
1666 }
1667 break;
1668 }
1669 case 'I':
1670 case 'i':
1671 {
1672 if ((LocaleCompare(symbol,"image.depth") == 0) ||
1673 (LocaleCompare(symbol,"image.minima") == 0) ||
1674 (LocaleCompare(symbol,"image.maxima") == 0) ||
1675 (LocaleCompare(symbol,"image.mean") == 0) ||
1676 (LocaleCompare(symbol,"image.kurtosis") == 0) ||
1677 (LocaleCompare(symbol,"image.skewness") == 0) ||
1678 (LocaleCompare(symbol,"image.standard_deviation") == 0))
1679 return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
1680 if (LocaleCompare(symbol,"image.resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001681 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001682 if (LocaleCompare(symbol,"image.resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001683 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001684 if (LocaleCompare(symbol,"intensity") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001685 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001686 if (LocaleCompare(symbol,"i") == 0)
cristya19f1d72012-08-07 18:24:38 +00001687 return((double) x);
cristy3ed852e2009-09-05 21:47:34 +00001688 break;
1689 }
1690 case 'J':
1691 case 'j':
1692 {
1693 if (LocaleCompare(symbol,"j") == 0)
cristya19f1d72012-08-07 18:24:38 +00001694 return((double) y);
cristy3ed852e2009-09-05 21:47:34 +00001695 break;
1696 }
1697 case 'L':
1698 case 'l':
1699 {
1700 if (LocaleCompare(symbol,"lightness") == 0)
1701 {
1702 double
1703 hue,
1704 lightness,
1705 saturation;
1706
cristy0a39a5c2012-06-27 12:51:45 +00001707 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
cristyda1f9c12011-10-02 21:39:49 +00001708 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001709 return(lightness);
1710 }
cristyd3d2a272013-06-24 16:09:41 +00001711 if (LocaleCompare(symbol,"luma") == 0)
1712 {
1713 double
1714 luma;
1715
1716 luma=0.212656*pixel.red+0.715158*pixel.green+0.072186*pixel.blue;
1717 return(QuantumScale*luma);
1718 }
cristy3ed852e2009-09-05 21:47:34 +00001719 if (LocaleCompare(symbol,"luminance") == 0)
1720 {
1721 double
1722 luminence;
1723
cristyd3d2a272013-06-24 16:09:41 +00001724 luminence=0.212656*pixel.red+0.715158*pixel.green+0.072186*pixel.blue;
cristy3ed852e2009-09-05 21:47:34 +00001725 return(QuantumScale*luminence);
1726 }
1727 break;
1728 }
1729 case 'M':
1730 case 'm':
1731 {
1732 if (LocaleNCompare(symbol,"maxima",6) == 0)
1733 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1734 if (LocaleNCompare(symbol,"mean",4) == 0)
1735 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1736 if (LocaleNCompare(symbol,"minima",6) == 0)
1737 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1738 if (LocaleCompare(symbol,"m") == 0)
cristy1c8e4f62014-05-26 16:17:53 +00001739 return(QuantumScale*pixel.green);
cristy3ed852e2009-09-05 21:47:34 +00001740 break;
1741 }
1742 case 'N':
1743 case 'n':
1744 {
1745 if (LocaleCompare(symbol,"n") == 0)
cristya19f1d72012-08-07 18:24:38 +00001746 return((double) GetImageListLength(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001747 break;
1748 }
1749 case 'O':
1750 case 'o':
1751 {
1752 if (LocaleCompare(symbol,"o") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001753 return(QuantumScale*pixel.alpha);
cristy3ed852e2009-09-05 21:47:34 +00001754 break;
1755 }
1756 case 'P':
1757 case 'p':
1758 {
1759 if (LocaleCompare(symbol,"page.height") == 0)
cristya19f1d72012-08-07 18:24:38 +00001760 return((double) image->page.height);
cristy3ed852e2009-09-05 21:47:34 +00001761 if (LocaleCompare(symbol,"page.width") == 0)
cristya19f1d72012-08-07 18:24:38 +00001762 return((double) image->page.width);
cristy3ed852e2009-09-05 21:47:34 +00001763 if (LocaleCompare(symbol,"page.x") == 0)
cristya19f1d72012-08-07 18:24:38 +00001764 return((double) image->page.x);
cristy3ed852e2009-09-05 21:47:34 +00001765 if (LocaleCompare(symbol,"page.y") == 0)
cristya19f1d72012-08-07 18:24:38 +00001766 return((double) image->page.y);
cristy3ed852e2009-09-05 21:47:34 +00001767 break;
1768 }
1769 case 'R':
1770 case 'r':
1771 {
1772 if (LocaleCompare(symbol,"resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001773 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001774 if (LocaleCompare(symbol,"resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001775 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001776 if (LocaleCompare(symbol,"r") == 0)
1777 return(QuantumScale*pixel.red);
1778 break;
1779 }
1780 case 'S':
1781 case 's':
1782 {
1783 if (LocaleCompare(symbol,"saturation") == 0)
1784 {
1785 double
1786 hue,
1787 lightness,
1788 saturation;
1789
cristy0a39a5c2012-06-27 12:51:45 +00001790 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
cristyda1f9c12011-10-02 21:39:49 +00001791 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001792 return(saturation);
1793 }
1794 if (LocaleNCompare(symbol,"skewness",8) == 0)
1795 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1796 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1797 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1798 break;
1799 }
1800 case 'T':
1801 case 't':
1802 {
1803 if (LocaleCompare(symbol,"t") == 0)
cristya19f1d72012-08-07 18:24:38 +00001804 return((double) GetImageIndexInList(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001805 break;
1806 }
1807 case 'W':
1808 case 'w':
1809 {
1810 if (LocaleCompare(symbol,"w") == 0)
cristya19f1d72012-08-07 18:24:38 +00001811 return((double) image->columns);
cristy3ed852e2009-09-05 21:47:34 +00001812 break;
1813 }
1814 case 'Y':
1815 case 'y':
1816 {
1817 if (LocaleCompare(symbol,"y") == 0)
cristy1c8e4f62014-05-26 16:17:53 +00001818 return(QuantumScale*pixel.blue);
cristy3ed852e2009-09-05 21:47:34 +00001819 break;
1820 }
1821 case 'Z':
1822 case 'z':
1823 {
1824 if (LocaleCompare(symbol,"z") == 0)
1825 {
cristya19f1d72012-08-07 18:24:38 +00001826 double
cristy3ed852e2009-09-05 21:47:34 +00001827 depth;
1828
cristya19f1d72012-08-07 18:24:38 +00001829 depth=(double) GetImageDepth(image,fx_info->exception);
cristy3ed852e2009-09-05 21:47:34 +00001830 return(depth);
1831 }
1832 break;
1833 }
1834 default:
1835 break;
1836 }
1837 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1838 if (value != (const char *) NULL)
cristya19f1d72012-08-07 18:24:38 +00001839 return((double) StringToDouble(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001840 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
cristyefe601c2013-01-05 17:51:12 +00001841 "UnableToParseExpression","`%s'",symbol);
cristy3ed852e2009-09-05 21:47:34 +00001842 return(0.0);
1843}
1844
1845static const char *FxOperatorPrecedence(const char *expression,
1846 ExceptionInfo *exception)
1847{
1848 typedef enum
1849 {
1850 UndefinedPrecedence,
1851 NullPrecedence,
1852 BitwiseComplementPrecedence,
1853 ExponentPrecedence,
cristy116af162010-08-13 01:25:47 +00001854 ExponentialNotationPrecedence,
cristy3ed852e2009-09-05 21:47:34 +00001855 MultiplyPrecedence,
1856 AdditionPrecedence,
1857 ShiftPrecedence,
1858 RelationalPrecedence,
1859 EquivalencyPrecedence,
1860 BitwiseAndPrecedence,
1861 BitwiseOrPrecedence,
1862 LogicalAndPrecedence,
1863 LogicalOrPrecedence,
1864 TernaryPrecedence,
1865 AssignmentPrecedence,
1866 CommaPrecedence,
1867 SeparatorPrecedence
1868 } FxPrecedence;
1869
1870 FxPrecedence
1871 precedence,
1872 target;
1873
1874 register const char
1875 *subexpression;
1876
1877 register int
1878 c;
1879
cristybb503372010-05-27 20:51:26 +00001880 size_t
cristy3ed852e2009-09-05 21:47:34 +00001881 level;
1882
1883 c=0;
1884 level=0;
1885 subexpression=(const char *) NULL;
1886 target=NullPrecedence;
1887 while (*expression != '\0')
1888 {
1889 precedence=UndefinedPrecedence;
cristy248a3ec2013-08-17 16:25:26 +00001890 if ((isspace((int) ((unsigned char) *expression)) != 0) || (c == (int) '@'))
cristy3ed852e2009-09-05 21:47:34 +00001891 {
1892 expression++;
1893 continue;
1894 }
cristy488fa882010-03-01 22:34:24 +00001895 switch (*expression)
1896 {
1897 case 'A':
1898 case 'a':
cristy3ed852e2009-09-05 21:47:34 +00001899 {
cristyb33454f2011-08-03 02:10:45 +00001900#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00001901 if (LocaleNCompare(expression,"acosh",5) == 0)
1902 {
1903 expression+=5;
1904 break;
1905 }
cristyb33454f2011-08-03 02:10:45 +00001906#endif
1907#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00001908 if (LocaleNCompare(expression,"asinh",5) == 0)
1909 {
1910 expression+=5;
1911 break;
1912 }
cristyb33454f2011-08-03 02:10:45 +00001913#endif
1914#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00001915 if (LocaleNCompare(expression,"atanh",5) == 0)
cristy488fa882010-03-01 22:34:24 +00001916 {
1917 expression+=5;
1918 break;
1919 }
cristyb33454f2011-08-03 02:10:45 +00001920#endif
anthony62838e52012-05-24 12:41:54 +00001921 if (LocaleNCompare(expression,"atan2",5) == 0)
1922 {
1923 expression+=5;
1924 break;
1925 }
cristy488fa882010-03-01 22:34:24 +00001926 break;
cristy3ed852e2009-09-05 21:47:34 +00001927 }
cristyf06a5612014-05-27 22:00:22 +00001928 case 'E':
1929 case 'e':
1930 {
1931 if ((isdigit((int) ((unsigned char) c)) != 0) &&
1932 ((LocaleNCompare(expression,"E+",2) == 0) ||
1933 (LocaleNCompare(expression,"E-",2) == 0)))
1934 {
1935 expression+=2; /* scientific notation */
1936 break;
1937 }
1938 }
cristy488fa882010-03-01 22:34:24 +00001939 case 'J':
1940 case 'j':
1941 {
1942 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1943 (LocaleNCompare(expression,"j1",2) == 0))
1944 {
1945 expression+=2;
1946 break;
1947 }
1948 break;
1949 }
cristy2def9322010-06-18 23:59:37 +00001950 case '#':
1951 {
1952 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1953 expression++;
1954 break;
1955 }
cristy488fa882010-03-01 22:34:24 +00001956 default:
1957 break;
1958 }
cristy3ed852e2009-09-05 21:47:34 +00001959 if ((c == (int) '{') || (c == (int) '['))
1960 level++;
1961 else
1962 if ((c == (int) '}') || (c == (int) ']'))
1963 level--;
1964 if (level == 0)
1965 switch ((unsigned char) *expression)
1966 {
1967 case '~':
1968 case '!':
1969 {
1970 precedence=BitwiseComplementPrecedence;
1971 break;
1972 }
1973 case '^':
cristy6621e252010-08-13 00:42:57 +00001974 case '@':
cristy3ed852e2009-09-05 21:47:34 +00001975 {
1976 precedence=ExponentPrecedence;
1977 break;
1978 }
1979 default:
1980 {
cristy37466eb2013-08-17 17:04:59 +00001981 if (((c != 0) && ((isdigit((int) ((unsigned char) c)) != 0) ||
1982 (strchr(")",(int) ((unsigned char) c)) != (char *) NULL))) &&
1983 (((islower((int) ((unsigned char) *expression)) != 0) ||
1984 (strchr("(",(int) ((unsigned char) *expression)) != (char *) NULL)) ||
1985 ((isdigit((int) ((unsigned char) c)) == 0) &&
1986 (isdigit((int) ((unsigned char) *expression)) != 0))) &&
1987 (strchr("xy",(int) ((unsigned char) *expression)) == (char *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001988 precedence=MultiplyPrecedence;
1989 break;
1990 }
1991 case '*':
1992 case '/':
1993 case '%':
1994 {
1995 precedence=MultiplyPrecedence;
1996 break;
1997 }
1998 case '+':
1999 case '-':
2000 {
2001 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
2002 (isalpha(c) != 0))
2003 precedence=AdditionPrecedence;
2004 break;
2005 }
2006 case LeftShiftOperator:
2007 case RightShiftOperator:
2008 {
2009 precedence=ShiftPrecedence;
2010 break;
2011 }
2012 case '<':
2013 case LessThanEqualOperator:
2014 case GreaterThanEqualOperator:
2015 case '>':
2016 {
2017 precedence=RelationalPrecedence;
2018 break;
2019 }
2020 case EqualOperator:
2021 case NotEqualOperator:
2022 {
2023 precedence=EquivalencyPrecedence;
2024 break;
2025 }
2026 case '&':
2027 {
2028 precedence=BitwiseAndPrecedence;
2029 break;
2030 }
2031 case '|':
2032 {
2033 precedence=BitwiseOrPrecedence;
2034 break;
2035 }
2036 case LogicalAndOperator:
2037 {
2038 precedence=LogicalAndPrecedence;
2039 break;
2040 }
2041 case LogicalOrOperator:
2042 {
2043 precedence=LogicalOrPrecedence;
2044 break;
2045 }
cristy116af162010-08-13 01:25:47 +00002046 case ExponentialNotation:
2047 {
2048 precedence=ExponentialNotationPrecedence;
2049 break;
2050 }
cristy3ed852e2009-09-05 21:47:34 +00002051 case ':':
2052 case '?':
2053 {
2054 precedence=TernaryPrecedence;
2055 break;
2056 }
2057 case '=':
2058 {
2059 precedence=AssignmentPrecedence;
2060 break;
2061 }
2062 case ',':
2063 {
2064 precedence=CommaPrecedence;
2065 break;
2066 }
2067 case ';':
2068 {
2069 precedence=SeparatorPrecedence;
2070 break;
2071 }
2072 }
2073 if ((precedence == BitwiseComplementPrecedence) ||
2074 (precedence == TernaryPrecedence) ||
2075 (precedence == AssignmentPrecedence))
2076 {
2077 if (precedence > target)
2078 {
2079 /*
2080 Right-to-left associativity.
2081 */
2082 target=precedence;
2083 subexpression=expression;
2084 }
2085 }
2086 else
2087 if (precedence >= target)
2088 {
2089 /*
2090 Left-to-right associativity.
2091 */
2092 target=precedence;
2093 subexpression=expression;
2094 }
2095 if (strchr("(",(int) *expression) != (char *) NULL)
2096 expression=FxSubexpression(expression,exception);
2097 c=(int) (*expression++);
2098 }
2099 return(subexpression);
2100}
2101
cristya19f1d72012-08-07 18:24:38 +00002102static double FxEvaluateSubexpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002103 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristya19f1d72012-08-07 18:24:38 +00002104 const char *expression,double *beta,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002105{
2106 char
2107 *q,
2108 subexpression[MaxTextExtent];
2109
cristya19f1d72012-08-07 18:24:38 +00002110 double
cristy3ed852e2009-09-05 21:47:34 +00002111 alpha,
2112 gamma;
2113
2114 register const char
2115 *p;
2116
2117 *beta=0.0;
2118 if (exception->severity != UndefinedException)
2119 return(0.0);
cristy248a3ec2013-08-17 16:25:26 +00002120 while (isspace((int) ((unsigned char) *expression)) != 0)
cristy3ed852e2009-09-05 21:47:34 +00002121 expression++;
2122 if (*expression == '\0')
cristydc569d82014-04-20 15:54:56 +00002123 return(0.0);
cristy66322f02010-05-17 11:40:48 +00002124 *subexpression='\0';
cristy3ed852e2009-09-05 21:47:34 +00002125 p=FxOperatorPrecedence(expression,exception);
2126 if (p != (const char *) NULL)
2127 {
2128 (void) CopyMagickString(subexpression,expression,(size_t)
2129 (p-expression+1));
2130 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2131 exception);
2132 switch ((unsigned char) *p)
2133 {
2134 case '~':
2135 {
2136 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002137 *beta=(double) (~(size_t) *beta);
cristy3ed852e2009-09-05 21:47:34 +00002138 return(*beta);
2139 }
2140 case '!':
2141 {
2142 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2143 return(*beta == 0.0 ? 1.0 : 0.0);
2144 }
2145 case '^':
2146 {
2147 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2148 channel,x,y,++p,beta,exception));
2149 return(*beta);
2150 }
2151 case '*':
cristy116af162010-08-13 01:25:47 +00002152 case ExponentialNotation:
cristy3ed852e2009-09-05 21:47:34 +00002153 {
2154 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2155 return(alpha*(*beta));
2156 }
2157 case '/':
2158 {
2159 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2160 if (*beta == 0.0)
2161 {
2162 if (exception->severity == UndefinedException)
2163 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00002164 OptionError,"DivideByZero","`%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002165 return(0.0);
2166 }
2167 return(alpha/(*beta));
2168 }
2169 case '%':
2170 {
2171 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2172 *beta=fabs(floor(((double) *beta)+0.5));
2173 if (*beta == 0.0)
2174 {
2175 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00002176 OptionError,"DivideByZero","`%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002177 return(0.0);
2178 }
2179 return(fmod((double) alpha,(double) *beta));
2180 }
2181 case '+':
2182 {
2183 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2184 return(alpha+(*beta));
2185 }
2186 case '-':
2187 {
2188 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2189 return(alpha-(*beta));
2190 }
2191 case LeftShiftOperator:
2192 {
2193 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002194 *beta=(double) ((size_t) (alpha+0.5) << (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002195 return(*beta);
2196 }
2197 case RightShiftOperator:
2198 {
2199 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002200 *beta=(double) ((size_t) (alpha+0.5) >> (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002201 return(*beta);
2202 }
2203 case '<':
2204 {
2205 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2206 return(alpha < *beta ? 1.0 : 0.0);
2207 }
2208 case LessThanEqualOperator:
2209 {
2210 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2211 return(alpha <= *beta ? 1.0 : 0.0);
2212 }
2213 case '>':
2214 {
2215 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2216 return(alpha > *beta ? 1.0 : 0.0);
2217 }
2218 case GreaterThanEqualOperator:
2219 {
2220 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2221 return(alpha >= *beta ? 1.0 : 0.0);
2222 }
2223 case EqualOperator:
2224 {
2225 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristyc06e20e2014-05-15 15:31:50 +00002226 return(fabs(alpha-(*beta)) < MagickEpsilon ? 1.0 : 0.0);
cristy3ed852e2009-09-05 21:47:34 +00002227 }
2228 case NotEqualOperator:
2229 {
2230 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy972050b2012-06-04 22:09:17 +00002231 return(fabs(alpha-(*beta)) >= MagickEpsilon ? 1.0 : 0.0);
cristy3ed852e2009-09-05 21:47:34 +00002232 }
2233 case '&':
2234 {
2235 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002236 *beta=(double) ((size_t) (alpha+0.5) & (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002237 return(*beta);
2238 }
2239 case '|':
2240 {
2241 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002242 *beta=(double) ((size_t) (alpha+0.5) | (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002243 return(*beta);
2244 }
2245 case LogicalAndOperator:
2246 {
2247 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2248 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2249 return(*beta);
2250 }
2251 case LogicalOrOperator:
2252 {
2253 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2254 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2255 return(*beta);
2256 }
2257 case '?':
2258 {
cristya19f1d72012-08-07 18:24:38 +00002259 double
cristy3ed852e2009-09-05 21:47:34 +00002260 gamma;
2261
2262 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2263 q=subexpression;
2264 p=StringToken(":",&q);
2265 if (q == (char *) NULL)
2266 {
2267 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00002268 OptionError,"UnableToParseExpression","`%s'",subexpression);
cristy3ed852e2009-09-05 21:47:34 +00002269 return(0.0);
2270 }
cristy972050b2012-06-04 22:09:17 +00002271 if (fabs((double) alpha) >= MagickEpsilon)
cristy3ed852e2009-09-05 21:47:34 +00002272 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2273 else
2274 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2275 return(gamma);
2276 }
2277 case '=':
2278 {
2279 char
2280 numeric[MaxTextExtent];
2281
2282 q=subexpression;
2283 while (isalpha((int) ((unsigned char) *q)) != 0)
2284 q++;
2285 if (*q != '\0')
2286 {
2287 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00002288 OptionError,"UnableToParseExpression","`%s'",subexpression);
cristy3ed852e2009-09-05 21:47:34 +00002289 return(0.0);
2290 }
2291 ClearMagickException(exception);
2292 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristyb51dff52011-05-19 16:55:47 +00002293 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
cristy8cd5b312010-01-07 01:10:24 +00002294 *beta);
cristy3ed852e2009-09-05 21:47:34 +00002295 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2296 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2297 subexpression),ConstantString(numeric));
2298 return(*beta);
2299 }
2300 case ',':
2301 {
2302 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2303 return(alpha);
2304 }
2305 case ';':
2306 {
2307 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2308 return(*beta);
2309 }
2310 default:
2311 {
2312 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2313 exception);
2314 return(gamma);
2315 }
2316 }
2317 }
2318 if (strchr("(",(int) *expression) != (char *) NULL)
2319 {
2320 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2321 subexpression[strlen(subexpression)-1]='\0';
2322 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2323 exception);
2324 return(gamma);
2325 }
cristy8b8a3ae2010-10-23 18:49:46 +00002326 switch (*expression)
cristy3ed852e2009-09-05 21:47:34 +00002327 {
2328 case '+':
2329 {
2330 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2331 exception);
2332 return(1.0*gamma);
2333 }
2334 case '-':
2335 {
2336 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2337 exception);
2338 return(-1.0*gamma);
2339 }
2340 case '~':
2341 {
2342 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2343 exception);
cristya19f1d72012-08-07 18:24:38 +00002344 return((double) (~(size_t) (gamma+0.5)));
cristy3ed852e2009-09-05 21:47:34 +00002345 }
2346 case 'A':
2347 case 'a':
2348 {
2349 if (LocaleNCompare(expression,"abs",3) == 0)
2350 {
2351 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2352 exception);
cristya19f1d72012-08-07 18:24:38 +00002353 return((double) fabs((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002354 }
cristyb33454f2011-08-03 02:10:45 +00002355#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00002356 if (LocaleNCompare(expression,"acosh",5) == 0)
2357 {
2358 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2359 exception);
cristya19f1d72012-08-07 18:24:38 +00002360 return((double) acosh((double) alpha));
cristy363772a2011-07-28 23:25:33 +00002361 }
cristyb33454f2011-08-03 02:10:45 +00002362#endif
cristy3ed852e2009-09-05 21:47:34 +00002363 if (LocaleNCompare(expression,"acos",4) == 0)
2364 {
2365 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2366 exception);
cristya19f1d72012-08-07 18:24:38 +00002367 return((double) acos((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002368 }
cristy43c22f42010-03-30 12:34:07 +00002369#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002370 if (LocaleNCompare(expression,"airy",4) == 0)
2371 {
2372 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2373 exception);
2374 if (alpha == 0.0)
cristy2dd03222010-03-30 22:12:11 +00002375 return(1.0);
2376 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
cristy43c22f42010-03-30 12:34:07 +00002377 return(gamma*gamma);
cristyee56cf12010-03-01 22:17:06 +00002378 }
cristy43c22f42010-03-30 12:34:07 +00002379#endif
cristyb33454f2011-08-03 02:10:45 +00002380#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00002381 if (LocaleNCompare(expression,"asinh",5) == 0)
2382 {
2383 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2384 exception);
cristya19f1d72012-08-07 18:24:38 +00002385 return((double) asinh((double) alpha));
cristy363772a2011-07-28 23:25:33 +00002386 }
cristyb33454f2011-08-03 02:10:45 +00002387#endif
cristy3ed852e2009-09-05 21:47:34 +00002388 if (LocaleNCompare(expression,"asin",4) == 0)
2389 {
2390 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2391 exception);
cristya19f1d72012-08-07 18:24:38 +00002392 return((double) asin((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002393 }
2394 if (LocaleNCompare(expression,"alt",3) == 0)
2395 {
2396 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2397 exception);
cristybb503372010-05-27 20:51:26 +00002398 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
cristy3ed852e2009-09-05 21:47:34 +00002399 }
2400 if (LocaleNCompare(expression,"atan2",5) == 0)
2401 {
2402 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2403 exception);
cristya19f1d72012-08-07 18:24:38 +00002404 return((double) atan2((double) alpha,(double) *beta));
cristy3ed852e2009-09-05 21:47:34 +00002405 }
cristyb33454f2011-08-03 02:10:45 +00002406#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00002407 if (LocaleNCompare(expression,"atanh",5) == 0)
2408 {
2409 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2410 exception);
cristya19f1d72012-08-07 18:24:38 +00002411 return((double) atanh((double) alpha));
cristy363772a2011-07-28 23:25:33 +00002412 }
cristyb33454f2011-08-03 02:10:45 +00002413#endif
cristy3ed852e2009-09-05 21:47:34 +00002414 if (LocaleNCompare(expression,"atan",4) == 0)
2415 {
2416 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2417 exception);
cristya19f1d72012-08-07 18:24:38 +00002418 return((double) atan((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002419 }
2420 if (LocaleCompare(expression,"a") == 0)
2421 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2422 break;
2423 }
2424 case 'B':
2425 case 'b':
2426 {
2427 if (LocaleCompare(expression,"b") == 0)
2428 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2429 break;
2430 }
2431 case 'C':
2432 case 'c':
2433 {
2434 if (LocaleNCompare(expression,"ceil",4) == 0)
2435 {
2436 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2437 exception);
cristya19f1d72012-08-07 18:24:38 +00002438 return((double) ceil((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002439 }
2440 if (LocaleNCompare(expression,"cosh",4) == 0)
2441 {
2442 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2443 exception);
cristya19f1d72012-08-07 18:24:38 +00002444 return((double) cosh((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002445 }
2446 if (LocaleNCompare(expression,"cos",3) == 0)
2447 {
2448 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2449 exception);
cristya19f1d72012-08-07 18:24:38 +00002450 return((double) cos((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002451 }
2452 if (LocaleCompare(expression,"c") == 0)
2453 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2454 break;
2455 }
2456 case 'D':
2457 case 'd':
2458 {
2459 if (LocaleNCompare(expression,"debug",5) == 0)
2460 {
2461 const char
2462 *type;
2463
2464 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2465 exception);
2466 if (fx_info->images->colorspace == CMYKColorspace)
2467 switch (channel)
2468 {
cristy0568ffc2011-07-25 16:54:14 +00002469 case CyanPixelChannel: type="cyan"; break;
2470 case MagentaPixelChannel: type="magenta"; break;
2471 case YellowPixelChannel: type="yellow"; break;
2472 case AlphaPixelChannel: type="opacity"; break;
2473 case BlackPixelChannel: type="black"; break;
cristy3ed852e2009-09-05 21:47:34 +00002474 default: type="unknown"; break;
2475 }
2476 else
2477 switch (channel)
2478 {
cristy0568ffc2011-07-25 16:54:14 +00002479 case RedPixelChannel: type="red"; break;
2480 case GreenPixelChannel: type="green"; break;
2481 case BluePixelChannel: type="blue"; break;
2482 case AlphaPixelChannel: type="opacity"; break;
cristy3ed852e2009-09-05 21:47:34 +00002483 default: type="unknown"; break;
2484 }
2485 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2486 if (strlen(subexpression) > 1)
2487 subexpression[strlen(subexpression)-1]='\0';
2488 if (fx_info->file != (FILE *) NULL)
cristy1707c6c2012-01-18 23:30:54 +00002489 (void) FormatLocaleFile(fx_info->file,"%s[%.20g,%.20g].%s: "
2490 "%s=%.*g\n",fx_info->images->filename,(double) x,(double) y,type,
2491 subexpression,GetMagickPrecision(),(double) alpha);
cristy3ed852e2009-09-05 21:47:34 +00002492 return(0.0);
2493 }
cristy5597a8d2011-11-04 00:25:32 +00002494 if (LocaleNCompare(expression,"drc",3) == 0)
2495 {
2496 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2497 exception);
cristya19f1d72012-08-07 18:24:38 +00002498 return((double) (alpha/(*beta*(alpha-1.0)+1.0)));
cristy5597a8d2011-11-04 00:25:32 +00002499 }
cristy3ed852e2009-09-05 21:47:34 +00002500 break;
2501 }
2502 case 'E':
2503 case 'e':
2504 {
2505 if (LocaleCompare(expression,"epsilon") == 0)
cristya19f1d72012-08-07 18:24:38 +00002506 return((double) MagickEpsilon);
cristy3ed852e2009-09-05 21:47:34 +00002507 if (LocaleNCompare(expression,"exp",3) == 0)
2508 {
2509 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2510 exception);
cristya19f1d72012-08-07 18:24:38 +00002511 return((double) exp((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002512 }
2513 if (LocaleCompare(expression,"e") == 0)
cristya19f1d72012-08-07 18:24:38 +00002514 return((double) 2.7182818284590452354);
cristy3ed852e2009-09-05 21:47:34 +00002515 break;
2516 }
2517 case 'F':
2518 case 'f':
2519 {
2520 if (LocaleNCompare(expression,"floor",5) == 0)
2521 {
2522 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2523 exception);
cristya19f1d72012-08-07 18:24:38 +00002524 return((double) floor((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002525 }
2526 break;
2527 }
2528 case 'G':
2529 case 'g':
2530 {
cristy9eeedea2011-11-02 19:04:05 +00002531 if (LocaleNCompare(expression,"gauss",5) == 0)
2532 {
2533 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2534 exception);
2535 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
cristya19f1d72012-08-07 18:24:38 +00002536 return((double) gamma);
cristy9eeedea2011-11-02 19:04:05 +00002537 }
cristyb0aad4c2011-11-02 19:30:35 +00002538 if (LocaleNCompare(expression,"gcd",3) == 0)
2539 {
2540 MagickOffsetType
2541 gcd;
2542
2543 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2544 exception);
cristy1707c6c2012-01-18 23:30:54 +00002545 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType) (*beta+
2546 0.5));
cristya19f1d72012-08-07 18:24:38 +00002547 return((double) gcd);
cristyb0aad4c2011-11-02 19:30:35 +00002548 }
cristy3ed852e2009-09-05 21:47:34 +00002549 if (LocaleCompare(expression,"g") == 0)
2550 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2551 break;
2552 }
2553 case 'H':
2554 case 'h':
2555 {
2556 if (LocaleCompare(expression,"h") == 0)
2557 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2558 if (LocaleCompare(expression,"hue") == 0)
2559 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2560 if (LocaleNCompare(expression,"hypot",5) == 0)
2561 {
2562 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2563 exception);
cristya19f1d72012-08-07 18:24:38 +00002564 return((double) hypot((double) alpha,(double) *beta));
cristy3ed852e2009-09-05 21:47:34 +00002565 }
2566 break;
2567 }
2568 case 'K':
2569 case 'k':
2570 {
2571 if (LocaleCompare(expression,"k") == 0)
2572 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2573 break;
2574 }
2575 case 'I':
2576 case 'i':
2577 {
2578 if (LocaleCompare(expression,"intensity") == 0)
2579 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2580 if (LocaleNCompare(expression,"int",3) == 0)
2581 {
2582 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2583 exception);
cristya19f1d72012-08-07 18:24:38 +00002584 return((double) floor(alpha));
cristy3ed852e2009-09-05 21:47:34 +00002585 }
cristy82b20722011-11-05 21:52:36 +00002586#if defined(MAGICKCORE_HAVE_ISNAN)
cristy639399c2011-11-02 19:16:15 +00002587 if (LocaleNCompare(expression,"isnan",5) == 0)
2588 {
2589 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2590 exception);
cristya19f1d72012-08-07 18:24:38 +00002591 return((double) !!isnan((double) alpha));
cristy639399c2011-11-02 19:16:15 +00002592 }
cristy82b20722011-11-05 21:52:36 +00002593#endif
cristy3ed852e2009-09-05 21:47:34 +00002594 if (LocaleCompare(expression,"i") == 0)
2595 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2596 break;
2597 }
2598 case 'J':
2599 case 'j':
2600 {
2601 if (LocaleCompare(expression,"j") == 0)
2602 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
cristy161b9262010-03-20 19:34:32 +00002603#if defined(MAGICKCORE_HAVE_J0)
cristyee56cf12010-03-01 22:17:06 +00002604 if (LocaleNCompare(expression,"j0",2) == 0)
2605 {
2606 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2607 exception);
cristya19f1d72012-08-07 18:24:38 +00002608 return((double) j0((double) alpha));
cristyee56cf12010-03-01 22:17:06 +00002609 }
cristy161b9262010-03-20 19:34:32 +00002610#endif
2611#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002612 if (LocaleNCompare(expression,"j1",2) == 0)
2613 {
2614 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2615 exception);
cristya19f1d72012-08-07 18:24:38 +00002616 return((double) j1((double) alpha));
cristyee56cf12010-03-01 22:17:06 +00002617 }
cristy161b9262010-03-20 19:34:32 +00002618#endif
cristyaa018fa2010-04-08 23:03:54 +00002619#if defined(MAGICKCORE_HAVE_J1)
cristya6a09e72010-03-02 14:51:02 +00002620 if (LocaleNCompare(expression,"jinc",4) == 0)
2621 {
2622 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2623 exception);
cristy0946a822010-03-12 17:14:58 +00002624 if (alpha == 0.0)
2625 return(1.0);
cristyc90d70d2012-11-03 23:53:13 +00002626 gamma=(double) (2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha));
cristyfce2f7b2010-03-12 00:29:49 +00002627 return(gamma);
cristya6a09e72010-03-02 14:51:02 +00002628 }
cristyaa018fa2010-04-08 23:03:54 +00002629#endif
cristy3ed852e2009-09-05 21:47:34 +00002630 break;
2631 }
2632 case 'L':
2633 case 'l':
2634 {
2635 if (LocaleNCompare(expression,"ln",2) == 0)
2636 {
2637 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2638 exception);
cristya19f1d72012-08-07 18:24:38 +00002639 return((double) log((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002640 }
cristyc8ed5322010-08-31 12:07:59 +00002641 if (LocaleNCompare(expression,"logtwo",6) == 0)
cristy3ed852e2009-09-05 21:47:34 +00002642 {
cristyc8ed5322010-08-31 12:07:59 +00002643 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
cristy3ed852e2009-09-05 21:47:34 +00002644 exception);
cristya19f1d72012-08-07 18:24:38 +00002645 return((double) log10((double) alpha))/log10(2.0);
cristy3ed852e2009-09-05 21:47:34 +00002646 }
2647 if (LocaleNCompare(expression,"log",3) == 0)
2648 {
2649 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2650 exception);
cristya19f1d72012-08-07 18:24:38 +00002651 return((double) log10((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002652 }
2653 if (LocaleCompare(expression,"lightness") == 0)
2654 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2655 break;
2656 }
2657 case 'M':
2658 case 'm':
2659 {
2660 if (LocaleCompare(expression,"MaxRGB") == 0)
cristya19f1d72012-08-07 18:24:38 +00002661 return((double) QuantumRange);
cristy3ed852e2009-09-05 21:47:34 +00002662 if (LocaleNCompare(expression,"maxima",6) == 0)
2663 break;
2664 if (LocaleNCompare(expression,"max",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002665 {
2666 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2667 exception);
2668 return(alpha > *beta ? alpha : *beta);
2669 }
cristy3ed852e2009-09-05 21:47:34 +00002670 if (LocaleNCompare(expression,"minima",6) == 0)
2671 break;
2672 if (LocaleNCompare(expression,"min",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002673 {
2674 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2675 exception);
2676 return(alpha < *beta ? alpha : *beta);
2677 }
cristy3ed852e2009-09-05 21:47:34 +00002678 if (LocaleNCompare(expression,"mod",3) == 0)
2679 {
2680 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2681 exception);
cristy984049c2011-11-03 18:34:58 +00002682 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2683 return(gamma);
cristy3ed852e2009-09-05 21:47:34 +00002684 }
2685 if (LocaleCompare(expression,"m") == 0)
2686 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2687 break;
2688 }
2689 case 'N':
2690 case 'n':
2691 {
cristyad3502e2011-11-02 19:10:45 +00002692 if (LocaleNCompare(expression,"not",3) == 0)
2693 {
2694 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2695 exception);
cristya19f1d72012-08-07 18:24:38 +00002696 return((double) (alpha < MagickEpsilon));
cristyad3502e2011-11-02 19:10:45 +00002697 }
cristy3ed852e2009-09-05 21:47:34 +00002698 if (LocaleCompare(expression,"n") == 0)
2699 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2700 break;
2701 }
2702 case 'O':
2703 case 'o':
2704 {
2705 if (LocaleCompare(expression,"Opaque") == 0)
2706 return(1.0);
2707 if (LocaleCompare(expression,"o") == 0)
2708 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2709 break;
2710 }
2711 case 'P':
2712 case 'p':
2713 {
cristy670aa3c2011-11-03 00:54:00 +00002714 if (LocaleCompare(expression,"phi") == 0)
cristya19f1d72012-08-07 18:24:38 +00002715 return((double) MagickPHI);
cristy3ed852e2009-09-05 21:47:34 +00002716 if (LocaleCompare(expression,"pi") == 0)
cristya19f1d72012-08-07 18:24:38 +00002717 return((double) MagickPI);
cristy3ed852e2009-09-05 21:47:34 +00002718 if (LocaleNCompare(expression,"pow",3) == 0)
2719 {
2720 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2721 exception);
cristya19f1d72012-08-07 18:24:38 +00002722 return((double) pow((double) alpha,(double) *beta));
cristy3ed852e2009-09-05 21:47:34 +00002723 }
2724 if (LocaleCompare(expression,"p") == 0)
2725 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2726 break;
2727 }
2728 case 'Q':
2729 case 'q':
2730 {
2731 if (LocaleCompare(expression,"QuantumRange") == 0)
cristya19f1d72012-08-07 18:24:38 +00002732 return((double) QuantumRange);
cristy3ed852e2009-09-05 21:47:34 +00002733 if (LocaleCompare(expression,"QuantumScale") == 0)
cristya19f1d72012-08-07 18:24:38 +00002734 return((double) QuantumScale);
cristy3ed852e2009-09-05 21:47:34 +00002735 break;
2736 }
2737 case 'R':
2738 case 'r':
2739 {
2740 if (LocaleNCompare(expression,"rand",4) == 0)
cristya19f1d72012-08-07 18:24:38 +00002741 return((double) GetPseudoRandomValue(fx_info->random_info));
cristy3ed852e2009-09-05 21:47:34 +00002742 if (LocaleNCompare(expression,"round",5) == 0)
2743 {
2744 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2745 exception);
cristya19f1d72012-08-07 18:24:38 +00002746 return((double) floor((double) alpha+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002747 }
2748 if (LocaleCompare(expression,"r") == 0)
2749 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2750 break;
2751 }
2752 case 'S':
2753 case 's':
2754 {
2755 if (LocaleCompare(expression,"saturation") == 0)
2756 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2757 if (LocaleNCompare(expression,"sign",4) == 0)
2758 {
2759 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2760 exception);
2761 return(alpha < 0.0 ? -1.0 : 1.0);
2762 }
cristya6a09e72010-03-02 14:51:02 +00002763 if (LocaleNCompare(expression,"sinc",4) == 0)
2764 {
2765 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2766 exception);
2767 if (alpha == 0)
2768 return(1.0);
cristya19f1d72012-08-07 18:24:38 +00002769 gamma=(double) (sin((double) (MagickPI*alpha))/
cristya6a09e72010-03-02 14:51:02 +00002770 (MagickPI*alpha));
2771 return(gamma);
2772 }
cristy3ed852e2009-09-05 21:47:34 +00002773 if (LocaleNCompare(expression,"sinh",4) == 0)
2774 {
2775 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2776 exception);
cristya19f1d72012-08-07 18:24:38 +00002777 return((double) sinh((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002778 }
2779 if (LocaleNCompare(expression,"sin",3) == 0)
2780 {
2781 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2782 exception);
cristya19f1d72012-08-07 18:24:38 +00002783 return((double) sin((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002784 }
2785 if (LocaleNCompare(expression,"sqrt",4) == 0)
2786 {
2787 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2788 exception);
cristya19f1d72012-08-07 18:24:38 +00002789 return((double) sqrt((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002790 }
cristy9eeedea2011-11-02 19:04:05 +00002791 if (LocaleNCompare(expression,"squish",6) == 0)
2792 {
2793 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2794 exception);
cristya19f1d72012-08-07 18:24:38 +00002795 return((double) (1.0/(1.0+exp((double) (-alpha)))));
cristy9eeedea2011-11-02 19:04:05 +00002796 }
cristy3ed852e2009-09-05 21:47:34 +00002797 if (LocaleCompare(expression,"s") == 0)
2798 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2799 break;
2800 }
2801 case 'T':
2802 case 't':
2803 {
2804 if (LocaleNCompare(expression,"tanh",4) == 0)
2805 {
2806 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2807 exception);
cristya19f1d72012-08-07 18:24:38 +00002808 return((double) tanh((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002809 }
2810 if (LocaleNCompare(expression,"tan",3) == 0)
2811 {
2812 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2813 exception);
cristya19f1d72012-08-07 18:24:38 +00002814 return((double) tan((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002815 }
2816 if (LocaleCompare(expression,"Transparent") == 0)
2817 return(0.0);
cristy16788e42010-08-13 13:44:26 +00002818 if (LocaleNCompare(expression,"trunc",5) == 0)
2819 {
2820 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2821 exception);
2822 if (alpha >= 0.0)
cristya19f1d72012-08-07 18:24:38 +00002823 return((double) floor((double) alpha));
2824 return((double) ceil((double) alpha));
cristy16788e42010-08-13 13:44:26 +00002825 }
cristy3ed852e2009-09-05 21:47:34 +00002826 if (LocaleCompare(expression,"t") == 0)
2827 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2828 break;
2829 }
2830 case 'U':
2831 case 'u':
2832 {
2833 if (LocaleCompare(expression,"u") == 0)
2834 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2835 break;
2836 }
2837 case 'V':
2838 case 'v':
2839 {
2840 if (LocaleCompare(expression,"v") == 0)
2841 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2842 break;
2843 }
2844 case 'W':
2845 case 'w':
2846 {
cristy9eeedea2011-11-02 19:04:05 +00002847 if (LocaleNCompare(expression,"while",5) == 0)
2848 {
2849 do
2850 {
2851 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2852 exception);
2853 } while (fabs((double) alpha) >= MagickEpsilon);
cristya19f1d72012-08-07 18:24:38 +00002854 return((double) *beta);
cristy9eeedea2011-11-02 19:04:05 +00002855 }
cristy3ed852e2009-09-05 21:47:34 +00002856 if (LocaleCompare(expression,"w") == 0)
2857 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2858 break;
2859 }
2860 case 'Y':
2861 case 'y':
2862 {
2863 if (LocaleCompare(expression,"y") == 0)
2864 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2865 break;
2866 }
2867 case 'Z':
2868 case 'z':
2869 {
2870 if (LocaleCompare(expression,"z") == 0)
2871 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2872 break;
2873 }
2874 default:
2875 break;
2876 }
2877 q=(char *) expression;
cristydbdd0e32011-11-04 23:29:40 +00002878 alpha=InterpretSiPrefixValue(expression,&q);
cristy3ed852e2009-09-05 21:47:34 +00002879 if (q == expression)
2880 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2881 return(alpha);
2882}
2883
cristy7832dc22011-09-05 01:21:53 +00002884MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
cristya19f1d72012-08-07 18:24:38 +00002885 double *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002886{
2887 MagickBooleanType
2888 status;
2889
cristy541ae572011-08-05 19:08:59 +00002890 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2891 exception);
cristy3ed852e2009-09-05 21:47:34 +00002892 return(status);
2893}
2894
2895MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
cristya19f1d72012-08-07 18:24:38 +00002896 double *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002897{
2898 FILE
2899 *file;
2900
2901 MagickBooleanType
2902 status;
2903
2904 file=fx_info->file;
2905 fx_info->file=(FILE *) NULL;
cristy541ae572011-08-05 19:08:59 +00002906 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2907 exception);
cristy3ed852e2009-09-05 21:47:34 +00002908 fx_info->file=file;
2909 return(status);
2910}
2911
cristy7832dc22011-09-05 01:21:53 +00002912MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002913 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristya19f1d72012-08-07 18:24:38 +00002914 double *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002915{
cristya19f1d72012-08-07 18:24:38 +00002916 double
cristy3ed852e2009-09-05 21:47:34 +00002917 beta;
2918
2919 beta=0.0;
2920 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2921 exception);
2922 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2923}
2924
2925/*
2926%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2927% %
2928% %
2929% %
2930% F x I m a g e %
2931% %
2932% %
2933% %
2934%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2935%
2936% FxImage() applies a mathematical expression to the specified image.
2937%
2938% The format of the FxImage method is:
2939%
2940% Image *FxImage(const Image *image,const char *expression,
2941% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002942%
2943% A description of each parameter follows:
2944%
2945% o image: the image.
2946%
cristy3ed852e2009-09-05 21:47:34 +00002947% o expression: A mathematical expression.
2948%
2949% o exception: return any errors or warnings in this structure.
2950%
2951*/
2952
2953static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2954{
cristybb503372010-05-27 20:51:26 +00002955 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002956 i;
2957
2958 assert(fx_info != (FxInfo **) NULL);
cristyac245f82012-05-05 17:13:57 +00002959 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
cristy3ed852e2009-09-05 21:47:34 +00002960 if (fx_info[i] != (FxInfo *) NULL)
2961 fx_info[i]=DestroyFxInfo(fx_info[i]);
cristyb41ee102010-10-04 16:46:15 +00002962 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
cristy3ed852e2009-09-05 21:47:34 +00002963 return(fx_info);
2964}
2965
2966static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2967 ExceptionInfo *exception)
2968{
2969 char
2970 *fx_expression;
2971
2972 FxInfo
2973 **fx_info;
2974
cristya19f1d72012-08-07 18:24:38 +00002975 double
cristy3ed852e2009-09-05 21:47:34 +00002976 alpha;
2977
cristybb503372010-05-27 20:51:26 +00002978 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002979 i;
2980
cristybb503372010-05-27 20:51:26 +00002981 size_t
cristy3ed852e2009-09-05 21:47:34 +00002982 number_threads;
2983
cristy9357bdd2012-07-30 12:28:34 +00002984 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
cristyb41ee102010-10-04 16:46:15 +00002985 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +00002986 if (fx_info == (FxInfo **) NULL)
cristy65d161b2012-10-07 20:39:52 +00002987 {
2988 (void) ThrowMagickException(exception,GetMagickModule(),
2989 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
2990 return((FxInfo **) NULL);
2991 }
cristy3ed852e2009-09-05 21:47:34 +00002992 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2993 if (*expression != '@')
2994 fx_expression=ConstantString(expression);
2995 else
cristy3a5987c2013-11-07 14:18:46 +00002996 fx_expression=FileToString(expression+1,~0UL,exception);
cristybb503372010-05-27 20:51:26 +00002997 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +00002998 {
cristy65d161b2012-10-07 20:39:52 +00002999 MagickBooleanType
3000 status;
3001
cristydb070952012-04-20 14:33:00 +00003002 fx_info[i]=AcquireFxInfo(image,fx_expression,exception);
cristy3ed852e2009-09-05 21:47:34 +00003003 if (fx_info[i] == (FxInfo *) NULL)
cristy65d161b2012-10-07 20:39:52 +00003004 break;
3005 status=FxPreprocessExpression(fx_info[i],&alpha,exception);
3006 if (status == MagickFalse)
3007 break;
cristy3ed852e2009-09-05 21:47:34 +00003008 }
3009 fx_expression=DestroyString(fx_expression);
cristy65d161b2012-10-07 20:39:52 +00003010 if (i < (ssize_t) number_threads)
3011 fx_info=DestroyFxThreadSet(fx_info);
cristy3ed852e2009-09-05 21:47:34 +00003012 return(fx_info);
3013}
3014
3015MagickExport Image *FxImage(const Image *image,const char *expression,
3016 ExceptionInfo *exception)
3017{
cristy3ed852e2009-09-05 21:47:34 +00003018#define FxImageTag "Fx/Image"
3019
cristyfa112112010-01-04 17:48:07 +00003020 CacheView
cristy79cedc72011-07-25 00:41:15 +00003021 *fx_view,
3022 *image_view;
cristyfa112112010-01-04 17:48:07 +00003023
cristy3ed852e2009-09-05 21:47:34 +00003024 FxInfo
cristyfa112112010-01-04 17:48:07 +00003025 **restrict fx_info;
cristy3ed852e2009-09-05 21:47:34 +00003026
3027 Image
3028 *fx_image;
3029
cristy3ed852e2009-09-05 21:47:34 +00003030 MagickBooleanType
3031 status;
3032
cristybb503372010-05-27 20:51:26 +00003033 MagickOffsetType
3034 progress;
3035
cristybb503372010-05-27 20:51:26 +00003036 ssize_t
3037 y;
3038
cristy3ed852e2009-09-05 21:47:34 +00003039 assert(image != (Image *) NULL);
3040 assert(image->signature == MagickSignature);
3041 if (image->debug != MagickFalse)
3042 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00003043 fx_info=AcquireFxThreadSet(image,expression,exception);
3044 if (fx_info == (FxInfo **) NULL)
cristy65d161b2012-10-07 20:39:52 +00003045 return((Image *) NULL);
3046 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3047 if (fx_image == (Image *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003048 {
cristy3ed852e2009-09-05 21:47:34 +00003049 fx_info=DestroyFxThreadSet(fx_info);
3050 return((Image *) NULL);
3051 }
cristy65d161b2012-10-07 20:39:52 +00003052 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
3053 {
3054 fx_info=DestroyFxThreadSet(fx_info);
3055 fx_image=DestroyImage(fx_image);
3056 return((Image *) NULL);
3057 }
cristy3ed852e2009-09-05 21:47:34 +00003058 /*
3059 Fx image.
3060 */
3061 status=MagickTrue;
3062 progress=0;
cristy46ff2672012-12-14 15:32:26 +00003063 image_view=AcquireVirtualCacheView(image,exception);
3064 fx_view=AcquireAuthenticCacheView(fx_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003065#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003066 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00003067 magick_threads(image,fx_image,fx_image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003068#endif
cristybb503372010-05-27 20:51:26 +00003069 for (y=0; y < (ssize_t) fx_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003070 {
cristy5c9e6f22010-09-17 17:31:01 +00003071 const int
3072 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003073
cristy79cedc72011-07-25 00:41:15 +00003074 register const Quantum
3075 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003076
cristy4c08aed2011-07-01 19:47:50 +00003077 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003078 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003079
cristy79cedc72011-07-25 00:41:15 +00003080 register ssize_t
3081 x;
3082
cristy3ed852e2009-09-05 21:47:34 +00003083 if (status == MagickFalse)
3084 continue;
cristy79cedc72011-07-25 00:41:15 +00003085 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003086 q=QueueCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
cristy79cedc72011-07-25 00:41:15 +00003087 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003088 {
3089 status=MagickFalse;
3090 continue;
3091 }
cristybb503372010-05-27 20:51:26 +00003092 for (x=0; x < (ssize_t) fx_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003093 {
cristy79cedc72011-07-25 00:41:15 +00003094 register ssize_t
3095 i;
3096
3097 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3098 {
cristya19f1d72012-08-07 18:24:38 +00003099 double
cristy79cedc72011-07-25 00:41:15 +00003100 alpha;
3101
cristy5a23c552013-02-13 14:34:28 +00003102 PixelChannel channel=GetPixelChannelChannel(image,i);
3103 PixelTrait traits=GetPixelChannelTraits(image,channel);
3104 PixelTrait fx_traits=GetPixelChannelTraits(fx_image,channel);
cristy010d7d12011-08-31 01:02:48 +00003105 if ((traits == UndefinedPixelTrait) ||
3106 (fx_traits == UndefinedPixelTrait))
cristy79cedc72011-07-25 00:41:15 +00003107 continue;
cristy1eced092012-08-10 23:10:56 +00003108 if (((fx_traits & CopyPixelTrait) != 0) ||
cristy883fde12013-04-08 00:50:13 +00003109 (GetPixelReadMask(image,p) == 0))
cristy79cedc72011-07-25 00:41:15 +00003110 {
cristy0beccfa2011-09-25 20:47:53 +00003111 SetPixelChannel(fx_image,channel,p[i],q);
cristy79cedc72011-07-25 00:41:15 +00003112 continue;
3113 }
3114 alpha=0.0;
cristya382aca2011-12-06 18:22:48 +00003115 (void) FxEvaluateChannelExpression(fx_info[id],channel,x,y,&alpha,
3116 exception);
cristy8cd03c32012-07-07 18:57:59 +00003117 q[i]=ClampToQuantum(QuantumRange*alpha);
cristy79cedc72011-07-25 00:41:15 +00003118 }
3119 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003120 q+=GetPixelChannels(fx_image);
cristy3ed852e2009-09-05 21:47:34 +00003121 }
3122 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3123 status=MagickFalse;
3124 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3125 {
3126 MagickBooleanType
3127 proceed;
3128
cristyb5d5f722009-11-04 03:03:49 +00003129#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003130 #pragma omp critical (MagickCore_FxImage)
cristy3ed852e2009-09-05 21:47:34 +00003131#endif
3132 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3133 if (proceed == MagickFalse)
3134 status=MagickFalse;
3135 }
3136 }
cristy3ed852e2009-09-05 21:47:34 +00003137 fx_view=DestroyCacheView(fx_view);
cristy79cedc72011-07-25 00:41:15 +00003138 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003139 fx_info=DestroyFxThreadSet(fx_info);
3140 if (status == MagickFalse)
3141 fx_image=DestroyImage(fx_image);
3142 return(fx_image);
3143}
3144
3145/*
3146%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3147% %
3148% %
3149% %
3150% I m p l o d e I m a g e %
3151% %
3152% %
3153% %
3154%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3155%
3156% ImplodeImage() creates a new image that is a copy of an existing
3157% one with the image pixels "implode" by the specified percentage. It
3158% allocates the memory necessary for the new Image structure and returns a
3159% pointer to the new image.
3160%
3161% The format of the ImplodeImage method is:
3162%
3163% Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003164% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003165%
3166% A description of each parameter follows:
3167%
3168% o implode_image: Method ImplodeImage returns a pointer to the image
3169% after it is implode. A null image is returned if there is a memory
3170% shortage.
3171%
3172% o image: the image.
3173%
3174% o amount: Define the extent of the implosion.
3175%
cristy76f512e2011-09-12 01:26:56 +00003176% o method: the pixel interpolation method.
3177%
cristy3ed852e2009-09-05 21:47:34 +00003178% o exception: return any errors or warnings in this structure.
3179%
3180*/
3181MagickExport Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003182 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003183{
3184#define ImplodeImageTag "Implode/Image"
3185
cristyfa112112010-01-04 17:48:07 +00003186 CacheView
3187 *image_view,
cristy5ee937c2013-04-19 12:55:16 +00003188 *implode_view,
3189 *interpolate_view;
cristyfa112112010-01-04 17:48:07 +00003190
cristy3ed852e2009-09-05 21:47:34 +00003191 Image
3192 *implode_image;
3193
cristy3ed852e2009-09-05 21:47:34 +00003194 MagickBooleanType
3195 status;
3196
cristybb503372010-05-27 20:51:26 +00003197 MagickOffsetType
3198 progress;
3199
cristya19f1d72012-08-07 18:24:38 +00003200 double
cristy3ed852e2009-09-05 21:47:34 +00003201 radius;
3202
3203 PointInfo
3204 center,
3205 scale;
3206
cristybb503372010-05-27 20:51:26 +00003207 ssize_t
3208 y;
3209
cristy3ed852e2009-09-05 21:47:34 +00003210 /*
3211 Initialize implode image attributes.
3212 */
3213 assert(image != (Image *) NULL);
3214 assert(image->signature == MagickSignature);
3215 if (image->debug != MagickFalse)
3216 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3217 assert(exception != (ExceptionInfo *) NULL);
3218 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00003219 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3220 exception);
cristy3ed852e2009-09-05 21:47:34 +00003221 if (implode_image == (Image *) NULL)
3222 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003223 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003224 {
cristy3ed852e2009-09-05 21:47:34 +00003225 implode_image=DestroyImage(implode_image);
3226 return((Image *) NULL);
3227 }
cristy4c08aed2011-07-01 19:47:50 +00003228 if (implode_image->background_color.alpha != OpaqueAlpha)
cristy8a46d822012-08-28 23:32:39 +00003229 implode_image->alpha_trait=BlendPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +00003230 /*
3231 Compute scaling factor.
3232 */
3233 scale.x=1.0;
3234 scale.y=1.0;
3235 center.x=0.5*image->columns;
3236 center.y=0.5*image->rows;
3237 radius=center.x;
3238 if (image->columns > image->rows)
3239 scale.y=(double) image->columns/(double) image->rows;
3240 else
3241 if (image->columns < image->rows)
3242 {
3243 scale.x=(double) image->rows/(double) image->columns;
3244 radius=center.y;
3245 }
3246 /*
3247 Implode image.
3248 */
3249 status=MagickTrue;
3250 progress=0;
cristy46ff2672012-12-14 15:32:26 +00003251 image_view=AcquireVirtualCacheView(image,exception);
cristy5ee937c2013-04-19 12:55:16 +00003252 interpolate_view=AcquireVirtualCacheView(image,exception);
cristy46ff2672012-12-14 15:32:26 +00003253 implode_view=AcquireAuthenticCacheView(implode_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003254#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003255 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00003256 magick_threads(image,implode_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003257#endif
cristybb503372010-05-27 20:51:26 +00003258 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003259 {
cristya19f1d72012-08-07 18:24:38 +00003260 double
cristy3ed852e2009-09-05 21:47:34 +00003261 distance;
3262
3263 PointInfo
3264 delta;
3265
cristy6d188022011-09-12 13:23:33 +00003266 register const Quantum
3267 *restrict p;
3268
cristybb503372010-05-27 20:51:26 +00003269 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003270 x;
3271
cristy4c08aed2011-07-01 19:47:50 +00003272 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003273 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003274
3275 if (status == MagickFalse)
3276 continue;
cristy1665ce12013-04-19 12:18:43 +00003277 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003278 q=QueueCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003279 exception);
cristy6d188022011-09-12 13:23:33 +00003280 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003281 {
3282 status=MagickFalse;
3283 continue;
3284 }
cristy3ed852e2009-09-05 21:47:34 +00003285 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00003286 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003287 {
cristy6d188022011-09-12 13:23:33 +00003288 register ssize_t
3289 i;
3290
cristy3ed852e2009-09-05 21:47:34 +00003291 /*
3292 Determine if the pixel is within an ellipse.
3293 */
cristy883fde12013-04-08 00:50:13 +00003294 if (GetPixelReadMask(image,p) == 0)
cristy10a6c612012-01-29 21:41:05 +00003295 {
cristyc3a58022013-10-09 23:22:42 +00003296 SetPixelBackgoundColor(implode_image,q);
cristy10a6c612012-01-29 21:41:05 +00003297 p+=GetPixelChannels(image);
3298 q+=GetPixelChannels(implode_image);
3299 continue;
3300 }
cristy3ed852e2009-09-05 21:47:34 +00003301 delta.x=scale.x*(double) (x-center.x);
3302 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00003303 if (distance >= (radius*radius))
cristya6d7a9b2012-01-18 20:04:48 +00003304 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy6d188022011-09-12 13:23:33 +00003305 {
cristy5a23c552013-02-13 14:34:28 +00003306 PixelChannel channel=GetPixelChannelChannel(image,i);
3307 PixelTrait traits=GetPixelChannelTraits(image,channel);
3308 PixelTrait implode_traits=GetPixelChannelTraits(implode_image,
3309 channel);
cristy1707c6c2012-01-18 23:30:54 +00003310 if ((traits == UndefinedPixelTrait) ||
3311 (implode_traits == UndefinedPixelTrait))
3312 continue;
cristya6d7a9b2012-01-18 20:04:48 +00003313 SetPixelChannel(implode_image,channel,p[i],q);
cristy6d188022011-09-12 13:23:33 +00003314 }
3315 else
cristy3ed852e2009-09-05 21:47:34 +00003316 {
3317 double
3318 factor;
3319
3320 /*
3321 Implode the pixel.
3322 */
3323 factor=1.0;
3324 if (distance > 0.0)
cristy1707c6c2012-01-18 23:30:54 +00003325 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/radius/
3326 2)),-amount);
cristy5ee937c2013-04-19 12:55:16 +00003327 status=InterpolatePixelChannels(image,interpolate_view,implode_image,
3328 method,(double) (factor*delta.x/scale.x+center.x),(double) (factor*
3329 delta.y/scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00003330 }
cristy6d188022011-09-12 13:23:33 +00003331 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003332 q+=GetPixelChannels(implode_image);
cristy3ed852e2009-09-05 21:47:34 +00003333 }
3334 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3335 status=MagickFalse;
3336 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3337 {
3338 MagickBooleanType
3339 proceed;
3340
cristyb5d5f722009-11-04 03:03:49 +00003341#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003342 #pragma omp critical (MagickCore_ImplodeImage)
cristy3ed852e2009-09-05 21:47:34 +00003343#endif
3344 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3345 if (proceed == MagickFalse)
3346 status=MagickFalse;
3347 }
3348 }
3349 implode_view=DestroyCacheView(implode_view);
cristy5ee937c2013-04-19 12:55:16 +00003350 interpolate_view=DestroyCacheView(interpolate_view);
cristy3ed852e2009-09-05 21:47:34 +00003351 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003352 if (status == MagickFalse)
3353 implode_image=DestroyImage(implode_image);
3354 return(implode_image);
3355}
3356
3357/*
3358%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3359% %
3360% %
3361% %
3362% M o r p h I m a g e s %
3363% %
3364% %
3365% %
3366%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3367%
3368% The MorphImages() method requires a minimum of two images. The first
3369% image is transformed into the second by a number of intervening images
3370% as specified by frames.
3371%
3372% The format of the MorphImage method is:
3373%
cristybb503372010-05-27 20:51:26 +00003374% Image *MorphImages(const Image *image,const size_t number_frames,
cristy3ed852e2009-09-05 21:47:34 +00003375% ExceptionInfo *exception)
3376%
3377% A description of each parameter follows:
3378%
3379% o image: the image.
3380%
3381% o number_frames: Define the number of in-between image to generate.
3382% The more in-between frames, the smoother the morph.
3383%
3384% o exception: return any errors or warnings in this structure.
3385%
3386*/
cristy7de7f742012-12-14 00:28:27 +00003387MagickExport Image *MorphImages(const Image *image,const size_t number_frames,
3388 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003389{
3390#define MorphImageTag "Morph/Image"
3391
cristyfab83642012-12-14 00:31:38 +00003392 double
3393 alpha,
3394 beta;
3395
cristy3ed852e2009-09-05 21:47:34 +00003396 Image
3397 *morph_image,
3398 *morph_images;
3399
cristy9d314ff2011-03-09 01:30:28 +00003400 MagickBooleanType
3401 status;
cristy3ed852e2009-09-05 21:47:34 +00003402
3403 MagickOffsetType
3404 scene;
3405
cristy3ed852e2009-09-05 21:47:34 +00003406 register const Image
3407 *next;
3408
cristybb503372010-05-27 20:51:26 +00003409 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003410 i;
3411
cristy9d314ff2011-03-09 01:30:28 +00003412 ssize_t
3413 y;
cristy3ed852e2009-09-05 21:47:34 +00003414
3415 /*
3416 Clone first frame in sequence.
3417 */
3418 assert(image != (Image *) NULL);
3419 assert(image->signature == MagickSignature);
3420 if (image->debug != MagickFalse)
3421 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3422 assert(exception != (ExceptionInfo *) NULL);
3423 assert(exception->signature == MagickSignature);
3424 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3425 if (morph_images == (Image *) NULL)
3426 return((Image *) NULL);
3427 if (GetNextImageInList(image) == (Image *) NULL)
3428 {
3429 /*
3430 Morph single image.
3431 */
cristybb503372010-05-27 20:51:26 +00003432 for (i=1; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003433 {
3434 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3435 if (morph_image == (Image *) NULL)
3436 {
3437 morph_images=DestroyImageList(morph_images);
3438 return((Image *) NULL);
3439 }
3440 AppendImageToList(&morph_images,morph_image);
cristy8b27a6d2010-02-14 03:31:15 +00003441 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003442 {
cristy8b27a6d2010-02-14 03:31:15 +00003443 MagickBooleanType
3444 proceed;
3445
cristybb503372010-05-27 20:51:26 +00003446 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3447 number_frames);
cristy8b27a6d2010-02-14 03:31:15 +00003448 if (proceed == MagickFalse)
3449 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00003450 }
3451 }
3452 return(GetFirstImageInList(morph_images));
3453 }
3454 /*
3455 Morph image sequence.
3456 */
3457 status=MagickTrue;
3458 scene=0;
3459 next=image;
3460 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3461 {
cristybb503372010-05-27 20:51:26 +00003462 for (i=0; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003463 {
3464 CacheView
3465 *image_view,
3466 *morph_view;
3467
cristya19f1d72012-08-07 18:24:38 +00003468 beta=(double) (i+1.0)/(double) (number_frames+1.0);
cristy3ed852e2009-09-05 21:47:34 +00003469 alpha=1.0-beta;
cristy15b98cd2010-09-12 19:42:50 +00003470 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
cristyaa2c16c2012-03-25 22:21:35 +00003471 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*next->rows+beta*
3472 GetNextImageInList(next)->rows+0.5),next->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +00003473 if (morph_image == (Image *) NULL)
3474 {
3475 morph_images=DestroyImageList(morph_images);
3476 return((Image *) NULL);
3477 }
cristy1707c6c2012-01-18 23:30:54 +00003478 status=SetImageStorageClass(morph_image,DirectClass,exception);
3479 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003480 {
cristy3ed852e2009-09-05 21:47:34 +00003481 morph_image=DestroyImage(morph_image);
3482 return((Image *) NULL);
3483 }
3484 AppendImageToList(&morph_images,morph_image);
3485 morph_images=GetLastImageInList(morph_images);
cristy15b98cd2010-09-12 19:42:50 +00003486 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
cristyaa2c16c2012-03-25 22:21:35 +00003487 morph_images->rows,GetNextImageInList(next)->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +00003488 if (morph_image == (Image *) NULL)
3489 {
3490 morph_images=DestroyImageList(morph_images);
3491 return((Image *) NULL);
3492 }
cristy46ff2672012-12-14 15:32:26 +00003493 image_view=AcquireVirtualCacheView(morph_image,exception);
3494 morph_view=AcquireAuthenticCacheView(morph_images,exception);
cristyb5d5f722009-11-04 03:03:49 +00003495#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003496 #pragma omp parallel for schedule(static,4) shared(status) \
cristy5e6b2592012-12-19 14:08:11 +00003497 magick_threads(morph_image,morph_image,morph_image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003498#endif
cristybb503372010-05-27 20:51:26 +00003499 for (y=0; y < (ssize_t) morph_images->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003500 {
3501 MagickBooleanType
3502 sync;
3503
cristy4c08aed2011-07-01 19:47:50 +00003504 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003505 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003506
cristybb503372010-05-27 20:51:26 +00003507 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003508 x;
3509
cristy4c08aed2011-07-01 19:47:50 +00003510 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003511 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003512
3513 if (status == MagickFalse)
3514 continue;
3515 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3516 exception);
3517 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3518 exception);
cristy4c08aed2011-07-01 19:47:50 +00003519 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003520 {
3521 status=MagickFalse;
3522 continue;
3523 }
cristybb503372010-05-27 20:51:26 +00003524 for (x=0; x < (ssize_t) morph_images->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003525 {
cristy1707c6c2012-01-18 23:30:54 +00003526 register ssize_t
3527 i;
3528
cristy10a6c612012-01-29 21:41:05 +00003529 for (i=0; i < (ssize_t) GetPixelChannels(morph_image); i++)
cristy1707c6c2012-01-18 23:30:54 +00003530 {
cristy031a2462013-05-24 13:29:27 +00003531 PixelChannel channel=GetPixelChannelChannel(morph_image,i);
3532 PixelTrait traits=GetPixelChannelTraits(morph_image,channel);
3533 PixelTrait morph_traits=GetPixelChannelTraits(morph_images,channel);
cristy1707c6c2012-01-18 23:30:54 +00003534 if ((traits == UndefinedPixelTrait) ||
3535 (morph_traits == UndefinedPixelTrait))
3536 continue;
cristy1eced092012-08-10 23:10:56 +00003537 if (((morph_traits & CopyPixelTrait) != 0) ||
cristy031a2462013-05-24 13:29:27 +00003538 (GetPixelReadMask(morph_images,p) == 0))
cristy1707c6c2012-01-18 23:30:54 +00003539 {
3540 SetPixelChannel(morph_image,channel,p[i],q);
3541 continue;
3542 }
3543 SetPixelChannel(morph_image,channel,ClampToQuantum(alpha*
3544 GetPixelChannel(morph_images,channel,q)+beta*p[i]),q);
3545 }
cristyed231572011-07-14 02:18:59 +00003546 p+=GetPixelChannels(morph_image);
3547 q+=GetPixelChannels(morph_images);
cristy3ed852e2009-09-05 21:47:34 +00003548 }
3549 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3550 if (sync == MagickFalse)
3551 status=MagickFalse;
3552 }
3553 morph_view=DestroyCacheView(morph_view);
3554 image_view=DestroyCacheView(image_view);
3555 morph_image=DestroyImage(morph_image);
3556 }
cristybb503372010-05-27 20:51:26 +00003557 if (i < (ssize_t) number_frames)
cristy3ed852e2009-09-05 21:47:34 +00003558 break;
3559 /*
3560 Clone last frame in sequence.
3561 */
3562 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3563 if (morph_image == (Image *) NULL)
3564 {
3565 morph_images=DestroyImageList(morph_images);
3566 return((Image *) NULL);
3567 }
3568 AppendImageToList(&morph_images,morph_image);
3569 morph_images=GetLastImageInList(morph_images);
3570 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3571 {
3572 MagickBooleanType
3573 proceed;
3574
cristyb5d5f722009-11-04 03:03:49 +00003575#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003576 #pragma omp critical (MagickCore_MorphImages)
cristy3ed852e2009-09-05 21:47:34 +00003577#endif
3578 proceed=SetImageProgress(image,MorphImageTag,scene,
3579 GetImageListLength(image));
3580 if (proceed == MagickFalse)
3581 status=MagickFalse;
3582 }
3583 scene++;
3584 }
3585 if (GetNextImageInList(next) != (Image *) NULL)
3586 {
3587 morph_images=DestroyImageList(morph_images);
3588 return((Image *) NULL);
3589 }
3590 return(GetFirstImageInList(morph_images));
3591}
3592
3593/*
3594%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3595% %
3596% %
3597% %
3598% P l a s m a I m a g e %
3599% %
3600% %
3601% %
3602%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3603%
3604% PlasmaImage() initializes an image with plasma fractal values. The image
3605% must be initialized with a base color and the random number generator
3606% seeded before this method is called.
3607%
3608% The format of the PlasmaImage method is:
3609%
3610% MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
cristy5cbc0162011-08-29 00:36:28 +00003611% size_t attenuate,size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003612%
3613% A description of each parameter follows:
3614%
3615% o image: the image.
3616%
3617% o segment: Define the region to apply plasma fractals values.
3618%
glennrp7dae1ca2010-09-16 12:17:35 +00003619% o attenuate: Define the plasma attenuation factor.
cristy3ed852e2009-09-05 21:47:34 +00003620%
3621% o depth: Limit the plasma recursion depth.
3622%
cristy5cbc0162011-08-29 00:36:28 +00003623% o exception: return any errors or warnings in this structure.
3624%
cristy3ed852e2009-09-05 21:47:34 +00003625*/
3626
3627static inline Quantum PlasmaPixel(RandomInfo *random_info,
cristya19f1d72012-08-07 18:24:38 +00003628 const double pixel,const double noise)
cristy3ed852e2009-09-05 21:47:34 +00003629{
3630 Quantum
3631 plasma;
3632
cristyce70c172010-01-07 17:15:30 +00003633 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
cristy3ed852e2009-09-05 21:47:34 +00003634 noise/2.0);
3635 return(plasma);
3636}
3637
cristyda1f9c12011-10-02 21:39:49 +00003638static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3639 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3640 const SegmentInfo *segment,size_t attenuate,size_t depth,
3641 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003642{
cristya19f1d72012-08-07 18:24:38 +00003643 double
cristy3ed852e2009-09-05 21:47:34 +00003644 plasma;
3645
cristyda1f9c12011-10-02 21:39:49 +00003646 register const Quantum
3647 *restrict u,
3648 *restrict v;
3649
3650 register Quantum
3651 *restrict q;
3652
3653 register ssize_t
3654 i;
cristy3ed852e2009-09-05 21:47:34 +00003655
cristy9d314ff2011-03-09 01:30:28 +00003656 ssize_t
3657 x,
3658 x_mid,
3659 y,
3660 y_mid;
3661
cristy3ed852e2009-09-05 21:47:34 +00003662 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3663 return(MagickTrue);
3664 if (depth != 0)
3665 {
3666 SegmentInfo
3667 local_info;
3668
3669 /*
3670 Divide the area into quadrants and recurse.
3671 */
3672 depth--;
3673 attenuate++;
cristybb503372010-05-27 20:51:26 +00003674 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3675 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003676 local_info=(*segment);
3677 local_info.x2=(double) x_mid;
3678 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003679 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3680 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003681 local_info=(*segment);
3682 local_info.y1=(double) y_mid;
3683 local_info.x2=(double) x_mid;
cristyda1f9c12011-10-02 21:39:49 +00003684 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3685 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003686 local_info=(*segment);
3687 local_info.x1=(double) x_mid;
3688 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003689 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3690 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003691 local_info=(*segment);
3692 local_info.x1=(double) x_mid;
3693 local_info.y1=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003694 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3695 &local_info,attenuate,depth,exception));
cristy3ed852e2009-09-05 21:47:34 +00003696 }
cristybb503372010-05-27 20:51:26 +00003697 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3698 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003699 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3700 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3701 return(MagickFalse);
3702 /*
3703 Average pixels and apply plasma.
3704 */
cristya19f1d72012-08-07 18:24:38 +00003705 plasma=(double) QuantumRange/(2.0*attenuate);
cristy3ed852e2009-09-05 21:47:34 +00003706 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3707 {
cristy3ed852e2009-09-05 21:47:34 +00003708 /*
3709 Left pixel.
3710 */
cristybb503372010-05-27 20:51:26 +00003711 x=(ssize_t) ceil(segment->x1-0.5);
cristy1707c6c2012-01-18 23:30:54 +00003712 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),1,1,
3713 exception);
3714 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),1,1,
3715 exception);
cristyc5c6f662010-09-22 14:23:02 +00003716 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003717 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3718 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003719 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003720 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3721 {
cristy5a23c552013-02-13 14:34:28 +00003722 PixelChannel channel=GetPixelChannelChannel(image,i);
3723 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003724 if (traits == UndefinedPixelTrait)
3725 continue;
cristy6e564992013-05-24 01:21:04 +00003726 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
cristyda1f9c12011-10-02 21:39:49 +00003727 }
cristyc5c6f662010-09-22 14:23:02 +00003728 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003729 if (segment->x1 != segment->x2)
3730 {
3731 /*
3732 Right pixel.
3733 */
cristybb503372010-05-27 20:51:26 +00003734 x=(ssize_t) ceil(segment->x2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003735 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3736 1,1,exception);
3737 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3738 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003739 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003740 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3741 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003742 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003743 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3744 {
cristy5a23c552013-02-13 14:34:28 +00003745 PixelChannel channel=GetPixelChannelChannel(image,i);
3746 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003747 if (traits == UndefinedPixelTrait)
3748 continue;
cristy6e564992013-05-24 01:21:04 +00003749 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
cristyda1f9c12011-10-02 21:39:49 +00003750 }
cristyc5c6f662010-09-22 14:23:02 +00003751 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003752 }
3753 }
3754 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3755 {
3756 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3757 {
cristy3ed852e2009-09-05 21:47:34 +00003758 /*
3759 Bottom pixel.
3760 */
cristybb503372010-05-27 20:51:26 +00003761 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003762 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3763 1,1,exception);
3764 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3765 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003766 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003767 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3768 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003769 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003770 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3771 {
cristy5a23c552013-02-13 14:34:28 +00003772 PixelChannel channel=GetPixelChannelChannel(image,i);
3773 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003774 if (traits == UndefinedPixelTrait)
3775 continue;
cristy6e564992013-05-24 01:21:04 +00003776 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
cristyda1f9c12011-10-02 21:39:49 +00003777 }
cristyc5c6f662010-09-22 14:23:02 +00003778 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003779 }
3780 if (segment->y1 != segment->y2)
3781 {
cristy3ed852e2009-09-05 21:47:34 +00003782 /*
3783 Top pixel.
3784 */
cristybb503372010-05-27 20:51:26 +00003785 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003786 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3787 1,1,exception);
3788 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3789 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003790 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003791 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3792 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003793 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003794 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3795 {
cristy5a23c552013-02-13 14:34:28 +00003796 PixelChannel channel=GetPixelChannelChannel(image,i);
3797 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003798 if (traits == UndefinedPixelTrait)
3799 continue;
cristy6e564992013-05-24 01:21:04 +00003800 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
cristyda1f9c12011-10-02 21:39:49 +00003801 }
cristyc5c6f662010-09-22 14:23:02 +00003802 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003803 }
3804 }
3805 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3806 {
cristy3ed852e2009-09-05 21:47:34 +00003807 /*
3808 Middle pixel.
3809 */
cristybb503372010-05-27 20:51:26 +00003810 x=(ssize_t) ceil(segment->x1-0.5);
3811 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003812 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
cristybb503372010-05-27 20:51:26 +00003813 x=(ssize_t) ceil(segment->x2-0.5);
3814 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003815 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003816 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003817 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3818 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003819 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003820 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3821 {
cristy5a23c552013-02-13 14:34:28 +00003822 PixelChannel channel=GetPixelChannelChannel(image,i);
3823 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003824 if (traits == UndefinedPixelTrait)
3825 continue;
cristy6e564992013-05-24 01:21:04 +00003826 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
cristyda1f9c12011-10-02 21:39:49 +00003827 }
cristyc5c6f662010-09-22 14:23:02 +00003828 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003829 }
3830 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3831 return(MagickTrue);
3832 return(MagickFalse);
3833}
cristyda1f9c12011-10-02 21:39:49 +00003834
cristy3ed852e2009-09-05 21:47:34 +00003835MagickExport MagickBooleanType PlasmaImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00003836 const SegmentInfo *segment,size_t attenuate,size_t depth,
3837 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003838{
cristyc5c6f662010-09-22 14:23:02 +00003839 CacheView
cristyda1f9c12011-10-02 21:39:49 +00003840 *image_view,
3841 *u_view,
3842 *v_view;
cristyc5c6f662010-09-22 14:23:02 +00003843
cristy3ed852e2009-09-05 21:47:34 +00003844 MagickBooleanType
3845 status;
3846
3847 RandomInfo
3848 *random_info;
3849
3850 if (image->debug != MagickFalse)
3851 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3852 assert(image != (Image *) NULL);
3853 assert(image->signature == MagickSignature);
3854 if (image->debug != MagickFalse)
3855 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy5cbc0162011-08-29 00:36:28 +00003856 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristyc5c6f662010-09-22 14:23:02 +00003857 return(MagickFalse);
cristy46ff2672012-12-14 15:32:26 +00003858 image_view=AcquireAuthenticCacheView(image,exception);
3859 u_view=AcquireVirtualCacheView(image,exception);
3860 v_view=AcquireVirtualCacheView(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003861 random_info=AcquireRandomInfo();
cristyda1f9c12011-10-02 21:39:49 +00003862 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3863 attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003864 random_info=DestroyRandomInfo(random_info);
cristyda1f9c12011-10-02 21:39:49 +00003865 v_view=DestroyCacheView(v_view);
3866 u_view=DestroyCacheView(u_view);
cristyc5c6f662010-09-22 14:23:02 +00003867 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003868 return(status);
3869}
3870
3871/*
3872%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3873% %
3874% %
3875% %
3876% P o l a r o i d I m a g e %
3877% %
3878% %
3879% %
3880%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3881%
3882% PolaroidImage() simulates a Polaroid picture.
3883%
3884% The format of the AnnotateImage method is:
3885%
3886% Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003887% const char *caption,const double angle,
3888% const PixelInterpolateMethod method,ExceptionInfo exception)
cristy3ed852e2009-09-05 21:47:34 +00003889%
3890% A description of each parameter follows:
3891%
3892% o image: the image.
3893%
3894% o draw_info: the draw info.
3895%
cristye9e3d382011-12-14 01:50:13 +00003896% o caption: the Polaroid caption.
3897%
cristycee97112010-05-28 00:44:52 +00003898% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00003899%
cristy5c4e2582011-09-11 19:21:03 +00003900% o method: the pixel interpolation method.
3901%
cristy3ed852e2009-09-05 21:47:34 +00003902% o exception: return any errors or warnings in this structure.
3903%
3904*/
3905MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003906 const char *caption,const double angle,const PixelInterpolateMethod method,
cristy5c4e2582011-09-11 19:21:03 +00003907 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003908{
cristy3ed852e2009-09-05 21:47:34 +00003909 Image
3910 *bend_image,
3911 *caption_image,
3912 *flop_image,
3913 *picture_image,
3914 *polaroid_image,
3915 *rotate_image,
3916 *trim_image;
3917
cristybb503372010-05-27 20:51:26 +00003918 size_t
cristy3ed852e2009-09-05 21:47:34 +00003919 height;
3920
cristy9d314ff2011-03-09 01:30:28 +00003921 ssize_t
3922 quantum;
3923
cristy3ed852e2009-09-05 21:47:34 +00003924 /*
3925 Simulate a Polaroid picture.
3926 */
3927 assert(image != (Image *) NULL);
3928 assert(image->signature == MagickSignature);
3929 if (image->debug != MagickFalse)
3930 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3931 assert(exception != (ExceptionInfo *) NULL);
3932 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00003933 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
cristy3ed852e2009-09-05 21:47:34 +00003934 image->rows)/25.0,10.0);
3935 height=image->rows+2*quantum;
3936 caption_image=(Image *) NULL;
cristye9e3d382011-12-14 01:50:13 +00003937 if (caption != (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003938 {
3939 char
cristye9e3d382011-12-14 01:50:13 +00003940 geometry[MaxTextExtent],
3941 *text;
cristy3ed852e2009-09-05 21:47:34 +00003942
3943 DrawInfo
3944 *annotate_info;
3945
cristy72e0b772013-04-28 14:29:45 +00003946 ImageInfo
3947 *image_info;
3948
cristy3ed852e2009-09-05 21:47:34 +00003949 MagickBooleanType
3950 status;
3951
cristy9d314ff2011-03-09 01:30:28 +00003952 ssize_t
3953 count;
3954
cristy3ed852e2009-09-05 21:47:34 +00003955 TypeMetric
3956 metrics;
3957
3958 /*
3959 Generate caption image.
3960 */
3961 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3962 if (caption_image == (Image *) NULL)
3963 return((Image *) NULL);
cristy72e0b772013-04-28 14:29:45 +00003964 image_info=AcquireImageInfo();
cristy3ed852e2009-09-05 21:47:34 +00003965 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
cristy72e0b772013-04-28 14:29:45 +00003966 text=InterpretImageProperties(image_info,(Image *) image,caption,
cristye9e3d382011-12-14 01:50:13 +00003967 exception);
cristy72e0b772013-04-28 14:29:45 +00003968 image_info=DestroyImageInfo(image_info);
cristye9e3d382011-12-14 01:50:13 +00003969 (void) CloneString(&annotate_info->text,text);
cristy6b1d05e2010-09-22 19:17:27 +00003970 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
cristye9e3d382011-12-14 01:50:13 +00003971 &text,exception);
3972 status=SetImageExtent(caption_image,image->columns,(size_t) ((count+1)*
3973 (metrics.ascent-metrics.descent)+0.5),exception);
cristy3ed852e2009-09-05 21:47:34 +00003974 if (status == MagickFalse)
3975 caption_image=DestroyImage(caption_image);
3976 else
3977 {
3978 caption_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003979 (void) SetImageBackgroundColor(caption_image,exception);
cristye9e3d382011-12-14 01:50:13 +00003980 (void) CloneString(&annotate_info->text,text);
cristyb51dff52011-05-19 16:55:47 +00003981 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
cristy3ed852e2009-09-05 21:47:34 +00003982 metrics.ascent);
3983 if (annotate_info->gravity == UndefinedGravity)
3984 (void) CloneString(&annotate_info->geometry,AcquireString(
3985 geometry));
cristy5cbc0162011-08-29 00:36:28 +00003986 (void) AnnotateImage(caption_image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003987 height+=caption_image->rows;
3988 }
3989 annotate_info=DestroyDrawInfo(annotate_info);
cristye9e3d382011-12-14 01:50:13 +00003990 text=DestroyString(text);
cristy3ed852e2009-09-05 21:47:34 +00003991 }
3992 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
3993 exception);
3994 if (picture_image == (Image *) NULL)
3995 {
3996 if (caption_image != (Image *) NULL)
3997 caption_image=DestroyImage(caption_image);
3998 return((Image *) NULL);
3999 }
4000 picture_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00004001 (void) SetImageBackgroundColor(picture_image,exception);
cristy39172402012-03-30 13:04:39 +00004002 (void) CompositeImage(picture_image,image,OverCompositeOp,MagickTrue,quantum,
cristyfeb3e962012-03-29 17:25:55 +00004003 quantum,exception);
cristy3ed852e2009-09-05 21:47:34 +00004004 if (caption_image != (Image *) NULL)
4005 {
cristyfeb3e962012-03-29 17:25:55 +00004006 (void) CompositeImage(picture_image,caption_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00004007 MagickTrue,quantum,(ssize_t) (image->rows+3*quantum/2),exception);
cristy3ed852e2009-09-05 21:47:34 +00004008 caption_image=DestroyImage(caption_image);
4009 }
cristy9950d572011-10-01 18:22:35 +00004010 (void) QueryColorCompliance("none",AllCompliance,
4011 &picture_image->background_color,exception);
cristy63240882011-08-05 19:05:27 +00004012 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004013 rotate_image=RotateImage(picture_image,90.0,exception);
4014 picture_image=DestroyImage(picture_image);
4015 if (rotate_image == (Image *) NULL)
4016 return((Image *) NULL);
4017 picture_image=rotate_image;
4018 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
cristy5c4e2582011-09-11 19:21:03 +00004019 picture_image->columns,method,exception);
cristy3ed852e2009-09-05 21:47:34 +00004020 picture_image=DestroyImage(picture_image);
4021 if (bend_image == (Image *) NULL)
4022 return((Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00004023 picture_image=bend_image;
4024 rotate_image=RotateImage(picture_image,-90.0,exception);
4025 picture_image=DestroyImage(picture_image);
4026 if (rotate_image == (Image *) NULL)
4027 return((Image *) NULL);
4028 picture_image=rotate_image;
4029 picture_image->background_color=image->background_color;
anthonyf46d4262012-03-26 03:30:34 +00004030 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
cristy3ed852e2009-09-05 21:47:34 +00004031 exception);
4032 if (polaroid_image == (Image *) NULL)
4033 {
4034 picture_image=DestroyImage(picture_image);
4035 return(picture_image);
4036 }
4037 flop_image=FlopImage(polaroid_image,exception);
4038 polaroid_image=DestroyImage(polaroid_image);
4039 if (flop_image == (Image *) NULL)
4040 {
4041 picture_image=DestroyImage(picture_image);
4042 return(picture_image);
4043 }
4044 polaroid_image=flop_image;
cristyfeb3e962012-03-29 17:25:55 +00004045 (void) CompositeImage(polaroid_image,picture_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00004046 MagickTrue,(ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
cristy3ed852e2009-09-05 21:47:34 +00004047 picture_image=DestroyImage(picture_image);
cristy9950d572011-10-01 18:22:35 +00004048 (void) QueryColorCompliance("none",AllCompliance,
4049 &polaroid_image->background_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00004050 rotate_image=RotateImage(polaroid_image,angle,exception);
4051 polaroid_image=DestroyImage(polaroid_image);
4052 if (rotate_image == (Image *) NULL)
4053 return((Image *) NULL);
4054 polaroid_image=rotate_image;
4055 trim_image=TrimImage(polaroid_image,exception);
4056 polaroid_image=DestroyImage(polaroid_image);
4057 if (trim_image == (Image *) NULL)
4058 return((Image *) NULL);
4059 polaroid_image=trim_image;
4060 return(polaroid_image);
4061}
4062
4063/*
4064%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4065% %
4066% %
4067% %
cristy3ed852e2009-09-05 21:47:34 +00004068% S e p i a T o n e I m a g e %
4069% %
4070% %
4071% %
4072%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4073%
4074% MagickSepiaToneImage() applies a special effect to the image, similar to the
4075% effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4076% 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4077% threshold of 80% is a good starting point for a reasonable tone.
4078%
4079% The format of the SepiaToneImage method is:
4080%
4081% Image *SepiaToneImage(const Image *image,const double threshold,
4082% ExceptionInfo *exception)
4083%
4084% A description of each parameter follows:
4085%
4086% o image: the image.
4087%
4088% o threshold: the tone threshold.
4089%
4090% o exception: return any errors or warnings in this structure.
4091%
4092*/
4093MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4094 ExceptionInfo *exception)
4095{
4096#define SepiaToneImageTag "SepiaTone/Image"
4097
cristyc4c8d132010-01-07 01:58:38 +00004098 CacheView
4099 *image_view,
4100 *sepia_view;
4101
cristy3ed852e2009-09-05 21:47:34 +00004102 Image
4103 *sepia_image;
4104
cristy3ed852e2009-09-05 21:47:34 +00004105 MagickBooleanType
4106 status;
4107
cristybb503372010-05-27 20:51:26 +00004108 MagickOffsetType
4109 progress;
4110
4111 ssize_t
4112 y;
4113
cristy3ed852e2009-09-05 21:47:34 +00004114 /*
4115 Initialize sepia-toned image attributes.
4116 */
4117 assert(image != (const Image *) NULL);
4118 assert(image->signature == MagickSignature);
4119 if (image->debug != MagickFalse)
4120 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4121 assert(exception != (ExceptionInfo *) NULL);
4122 assert(exception->signature == MagickSignature);
cristy1707c6c2012-01-18 23:30:54 +00004123 sepia_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004124 if (sepia_image == (Image *) NULL)
4125 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004126 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004127 {
cristy3ed852e2009-09-05 21:47:34 +00004128 sepia_image=DestroyImage(sepia_image);
4129 return((Image *) NULL);
4130 }
4131 /*
4132 Tone each row of the image.
4133 */
4134 status=MagickTrue;
4135 progress=0;
cristy46ff2672012-12-14 15:32:26 +00004136 image_view=AcquireVirtualCacheView(image,exception);
4137 sepia_view=AcquireAuthenticCacheView(sepia_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00004138#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00004139 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00004140 magick_threads(image,sepia_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00004141#endif
cristybb503372010-05-27 20:51:26 +00004142 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004143 {
cristy4c08aed2011-07-01 19:47:50 +00004144 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004145 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004146
cristybb503372010-05-27 20:51:26 +00004147 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004148 x;
4149
cristy4c08aed2011-07-01 19:47:50 +00004150 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004151 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004152
4153 if (status == MagickFalse)
4154 continue;
4155 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00004156 q=GetCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00004157 exception);
cristy4c08aed2011-07-01 19:47:50 +00004158 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004159 {
4160 status=MagickFalse;
4161 continue;
4162 }
cristybb503372010-05-27 20:51:26 +00004163 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004164 {
cristya19f1d72012-08-07 18:24:38 +00004165 double
cristy3ed852e2009-09-05 21:47:34 +00004166 intensity,
4167 tone;
4168
cristyf13c5942012-08-08 23:50:11 +00004169 intensity=GetPixelIntensity(image,p);
cristya19f1d72012-08-07 18:24:38 +00004170 tone=intensity > threshold ? (double) QuantumRange : intensity+
4171 (double) QuantumRange-threshold;
cristy4c08aed2011-07-01 19:47:50 +00004172 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
cristya19f1d72012-08-07 18:24:38 +00004173 tone=intensity > (7.0*threshold/6.0) ? (double) QuantumRange :
4174 intensity+(double) QuantumRange-7.0*threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004175 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004176 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004177 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004178 tone=threshold/7.0;
cristya19f1d72012-08-07 18:24:38 +00004179 if ((double) GetPixelGreen(image,q) < tone)
cristy4c08aed2011-07-01 19:47:50 +00004180 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
cristya19f1d72012-08-07 18:24:38 +00004181 if ((double) GetPixelBlue(image,q) < tone)
cristy4c08aed2011-07-01 19:47:50 +00004182 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristyc8a5f0e2014-04-04 11:36:37 +00004183 SetPixelAlpha(sepia_image,GetPixelAlpha(image,p),q);
cristyed231572011-07-14 02:18:59 +00004184 p+=GetPixelChannels(image);
4185 q+=GetPixelChannels(sepia_image);
cristy3ed852e2009-09-05 21:47:34 +00004186 }
4187 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4188 status=MagickFalse;
4189 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4190 {
4191 MagickBooleanType
4192 proceed;
4193
cristyb5d5f722009-11-04 03:03:49 +00004194#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004195 #pragma omp critical (MagickCore_SepiaToneImage)
cristy3ed852e2009-09-05 21:47:34 +00004196#endif
4197 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4198 image->rows);
4199 if (proceed == MagickFalse)
4200 status=MagickFalse;
4201 }
4202 }
4203 sepia_view=DestroyCacheView(sepia_view);
4204 image_view=DestroyCacheView(image_view);
cristye23ec9d2011-08-16 18:15:40 +00004205 (void) NormalizeImage(sepia_image,exception);
4206 (void) ContrastImage(sepia_image,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004207 if (status == MagickFalse)
4208 sepia_image=DestroyImage(sepia_image);
4209 return(sepia_image);
4210}
4211
4212/*
4213%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4214% %
4215% %
4216% %
4217% S h a d o w I m a g e %
4218% %
4219% %
4220% %
4221%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4222%
4223% ShadowImage() simulates a shadow from the specified image and returns it.
4224%
4225% The format of the ShadowImage method is:
4226%
cristy70cddf72011-12-10 22:42:42 +00004227% Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004228% const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4229% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004230%
4231% A description of each parameter follows:
4232%
4233% o image: the image.
4234%
cristy70cddf72011-12-10 22:42:42 +00004235% o alpha: percentage transparency.
cristy3ed852e2009-09-05 21:47:34 +00004236%
4237% o sigma: the standard deviation of the Gaussian, in pixels.
4238%
4239% o x_offset: the shadow x-offset.
4240%
4241% o y_offset: the shadow y-offset.
4242%
4243% o exception: return any errors or warnings in this structure.
4244%
4245*/
cristy70cddf72011-12-10 22:42:42 +00004246MagickExport Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004247 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4248 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004249{
4250#define ShadowImageTag "Shadow/Image"
4251
cristy70cddf72011-12-10 22:42:42 +00004252 CacheView
4253 *image_view;
4254
cristybd5a96c2011-08-21 00:04:26 +00004255 ChannelType
4256 channel_mask;
4257
cristy3ed852e2009-09-05 21:47:34 +00004258 Image
4259 *border_image,
4260 *clone_image,
4261 *shadow_image;
4262
cristy70cddf72011-12-10 22:42:42 +00004263 MagickBooleanType
4264 status;
4265
cristy3ed852e2009-09-05 21:47:34 +00004266 RectangleInfo
4267 border_info;
4268
cristy70cddf72011-12-10 22:42:42 +00004269 ssize_t
4270 y;
4271
cristy3ed852e2009-09-05 21:47:34 +00004272 assert(image != (Image *) NULL);
4273 assert(image->signature == MagickSignature);
4274 if (image->debug != MagickFalse)
4275 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4276 assert(exception != (ExceptionInfo *) NULL);
4277 assert(exception->signature == MagickSignature);
4278 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4279 if (clone_image == (Image *) NULL)
4280 return((Image *) NULL);
cristya6400b12013-03-15 12:20:18 +00004281 if (IsGrayColorspace(image->colorspace) != MagickFalse)
cristy0c81d062013-04-21 15:22:02 +00004282 (void) SetImageColorspace(clone_image,sRGBColorspace,exception);
cristy0ce08762012-06-30 01:33:18 +00004283 (void) SetImageVirtualPixelMethod(clone_image,TransparentVirtualPixelMethod,
cristy387430f2012-02-07 13:09:46 +00004284 exception);
cristybb503372010-05-27 20:51:26 +00004285 border_info.width=(size_t) floor(2.0*sigma+0.5);
4286 border_info.height=(size_t) floor(2.0*sigma+0.5);
cristy3ed852e2009-09-05 21:47:34 +00004287 border_info.x=0;
4288 border_info.y=0;
cristy9950d572011-10-01 18:22:35 +00004289 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4290 exception);
cristy8a46d822012-08-28 23:32:39 +00004291 clone_image->alpha_trait=BlendPixelTrait;
cristy70cddf72011-12-10 22:42:42 +00004292 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
cristy3ed852e2009-09-05 21:47:34 +00004293 clone_image=DestroyImage(clone_image);
4294 if (border_image == (Image *) NULL)
4295 return((Image *) NULL);
cristy8a46d822012-08-28 23:32:39 +00004296 if (border_image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00004297 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004298 /*
4299 Shadow image.
4300 */
cristy70cddf72011-12-10 22:42:42 +00004301 status=MagickTrue;
cristy46ff2672012-12-14 15:32:26 +00004302 image_view=AcquireAuthenticCacheView(border_image,exception);
cristy70cddf72011-12-10 22:42:42 +00004303 for (y=0; y < (ssize_t) border_image->rows; y++)
4304 {
4305 PixelInfo
4306 background_color;
4307
4308 register Quantum
4309 *restrict q;
4310
4311 register ssize_t
4312 x;
4313
4314 if (status == MagickFalse)
4315 continue;
4316 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
4317 exception);
4318 if (q == (Quantum *) NULL)
4319 {
4320 status=MagickFalse;
4321 continue;
4322 }
4323 background_color=border_image->background_color;
cristy8a46d822012-08-28 23:32:39 +00004324 background_color.alpha_trait=BlendPixelTrait;
cristy70cddf72011-12-10 22:42:42 +00004325 for (x=0; x < (ssize_t) border_image->columns; x++)
4326 {
cristy8a46d822012-08-28 23:32:39 +00004327 if (border_image->alpha_trait == BlendPixelTrait)
cristy70cddf72011-12-10 22:42:42 +00004328 background_color.alpha=GetPixelAlpha(border_image,q)*alpha/100.0;
4329 SetPixelInfoPixel(border_image,&background_color,q);
4330 q+=GetPixelChannels(border_image);
4331 }
4332 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4333 status=MagickFalse;
4334 }
4335 image_view=DestroyCacheView(image_view);
4336 if (status == MagickFalse)
4337 {
4338 border_image=DestroyImage(border_image);
4339 return((Image *) NULL);
4340 }
cristycf1296e2012-08-26 23:40:49 +00004341 channel_mask=SetImageChannelMask(border_image,AlphaChannel);
cristyaa2c16c2012-03-25 22:21:35 +00004342 shadow_image=BlurImage(border_image,0.0,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00004343 border_image=DestroyImage(border_image);
4344 if (shadow_image == (Image *) NULL)
4345 return((Image *) NULL);
cristycf1296e2012-08-26 23:40:49 +00004346 (void) SetPixelChannelMask(shadow_image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00004347 if (shadow_image->page.width == 0)
4348 shadow_image->page.width=shadow_image->columns;
4349 if (shadow_image->page.height == 0)
4350 shadow_image->page.height=shadow_image->rows;
cristybb503372010-05-27 20:51:26 +00004351 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4352 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4353 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4354 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
cristy3ed852e2009-09-05 21:47:34 +00004355 return(shadow_image);
4356}
4357
4358/*
4359%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4360% %
4361% %
4362% %
4363% S k e t c h I m a g e %
4364% %
4365% %
4366% %
4367%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4368%
4369% SketchImage() simulates a pencil sketch. We convolve the image with a
4370% Gaussian operator of the given radius and standard deviation (sigma). For
4371% reasonable results, radius should be larger than sigma. Use a radius of 0
4372% and SketchImage() selects a suitable radius for you. Angle gives the angle
4373% of the sketch.
4374%
4375% The format of the SketchImage method is:
4376%
4377% Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004378% const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004379%
4380% A description of each parameter follows:
4381%
4382% o image: the image.
4383%
cristy574cc262011-08-05 01:23:58 +00004384% o radius: the radius of the Gaussian, in pixels, not counting the
4385% center pixel.
cristy3ed852e2009-09-05 21:47:34 +00004386%
4387% o sigma: the standard deviation of the Gaussian, in pixels.
4388%
cristyf7ef0252011-09-09 14:50:06 +00004389% o angle: apply the effect along this angle.
4390%
cristy3ed852e2009-09-05 21:47:34 +00004391% o exception: return any errors or warnings in this structure.
4392%
4393*/
4394MagickExport Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004395 const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004396{
cristyfa112112010-01-04 17:48:07 +00004397 CacheView
4398 *random_view;
4399
cristy3ed852e2009-09-05 21:47:34 +00004400 Image
4401 *blend_image,
4402 *blur_image,
4403 *dodge_image,
4404 *random_image,
4405 *sketch_image;
4406
cristy3ed852e2009-09-05 21:47:34 +00004407 MagickBooleanType
4408 status;
4409
cristy3ed852e2009-09-05 21:47:34 +00004410 RandomInfo
cristyfa112112010-01-04 17:48:07 +00004411 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00004412
cristy9d314ff2011-03-09 01:30:28 +00004413 ssize_t
4414 y;
4415
glennrpf7659d72012-09-24 18:14:56 +00004416#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +00004417 unsigned long
4418 key;
glennrpf7659d72012-09-24 18:14:56 +00004419#endif
cristy57340e02012-05-05 00:53:23 +00004420
cristy3ed852e2009-09-05 21:47:34 +00004421 /*
4422 Sketch image.
4423 */
4424 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4425 MagickTrue,exception);
4426 if (random_image == (Image *) NULL)
4427 return((Image *) NULL);
4428 status=MagickTrue;
cristy1b784432009-12-19 02:20:40 +00004429 random_info=AcquireRandomInfoThreadSet();
glennrpf7659d72012-09-24 18:14:56 +00004430#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +00004431 key=GetRandomSecretKey(random_info[0]);
glennrpf7659d72012-09-24 18:14:56 +00004432#endif
cristy46ff2672012-12-14 15:32:26 +00004433 random_view=AcquireAuthenticCacheView(random_image,exception);
cristy1b784432009-12-19 02:20:40 +00004434#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +00004435 #pragma omp parallel for schedule(static,4) shared(status) \
cristy5e6b2592012-12-19 14:08:11 +00004436 magick_threads(random_image,random_image,random_image->rows,key == ~0UL)
cristy1b784432009-12-19 02:20:40 +00004437#endif
cristybb503372010-05-27 20:51:26 +00004438 for (y=0; y < (ssize_t) random_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004439 {
cristy5c9e6f22010-09-17 17:31:01 +00004440 const int
4441 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00004442
cristybb503372010-05-27 20:51:26 +00004443 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004444 x;
4445
cristy4c08aed2011-07-01 19:47:50 +00004446 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004447 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004448
cristy1b784432009-12-19 02:20:40 +00004449 if (status == MagickFalse)
4450 continue;
cristy3ed852e2009-09-05 21:47:34 +00004451 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4452 exception);
cristyacd2ed22011-08-30 01:44:23 +00004453 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004454 {
4455 status=MagickFalse;
4456 continue;
4457 }
cristybb503372010-05-27 20:51:26 +00004458 for (x=0; x < (ssize_t) random_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004459 {
cristya19f1d72012-08-07 18:24:38 +00004460 double
cristy76f512e2011-09-12 01:26:56 +00004461 value;
4462
4463 register ssize_t
4464 i;
4465
cristy883fde12013-04-08 00:50:13 +00004466 if (GetPixelReadMask(random_image,q) == 0)
cristy10a6c612012-01-29 21:41:05 +00004467 {
4468 q+=GetPixelChannels(random_image);
4469 continue;
4470 }
cristy76f512e2011-09-12 01:26:56 +00004471 value=GetPseudoRandomValue(random_info[id]);
4472 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4473 {
cristy5a23c552013-02-13 14:34:28 +00004474 PixelChannel channel=GetPixelChannelChannel(image,i);
4475 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004476 if (traits == UndefinedPixelTrait)
4477 continue;
4478 q[i]=ClampToQuantum(QuantumRange*value);
4479 }
cristyed231572011-07-14 02:18:59 +00004480 q+=GetPixelChannels(random_image);
cristy3ed852e2009-09-05 21:47:34 +00004481 }
4482 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4483 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004484 }
4485 random_view=DestroyCacheView(random_view);
cristy1b784432009-12-19 02:20:40 +00004486 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00004487 if (status == MagickFalse)
4488 {
4489 random_image=DestroyImage(random_image);
4490 return(random_image);
4491 }
cristyaa2c16c2012-03-25 22:21:35 +00004492 blur_image=MotionBlurImage(random_image,radius,sigma,angle,exception);
cristy3ed852e2009-09-05 21:47:34 +00004493 random_image=DestroyImage(random_image);
4494 if (blur_image == (Image *) NULL)
4495 return((Image *) NULL);
cristy800446a2013-05-20 20:15:38 +00004496 dodge_image=EdgeImage(blur_image,radius,exception);
cristy3ed852e2009-09-05 21:47:34 +00004497 blur_image=DestroyImage(blur_image);
4498 if (dodge_image == (Image *) NULL)
4499 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +00004500 (void) NormalizeImage(dodge_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +00004501 (void) NegateImage(dodge_image,MagickFalse,exception);
cristye941a752011-10-15 01:52:48 +00004502 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
cristy3ed852e2009-09-05 21:47:34 +00004503 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4504 if (sketch_image == (Image *) NULL)
4505 {
4506 dodge_image=DestroyImage(dodge_image);
4507 return((Image *) NULL);
4508 }
cristyfeb3e962012-03-29 17:25:55 +00004509 (void) CompositeImage(sketch_image,dodge_image,ColorDodgeCompositeOp,
cristy39172402012-03-30 13:04:39 +00004510 MagickTrue,0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004511 dodge_image=DestroyImage(dodge_image);
4512 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4513 if (blend_image == (Image *) NULL)
4514 {
4515 sketch_image=DestroyImage(sketch_image);
4516 return((Image *) NULL);
4517 }
4518 (void) SetImageArtifact(blend_image,"compose:args","20x80");
cristy39172402012-03-30 13:04:39 +00004519 (void) CompositeImage(sketch_image,blend_image,BlendCompositeOp,MagickTrue,
cristyfeb3e962012-03-29 17:25:55 +00004520 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004521 blend_image=DestroyImage(blend_image);
4522 return(sketch_image);
4523}
4524
4525/*
4526%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4527% %
4528% %
4529% %
4530% S o l a r i z e I m a g e %
4531% %
4532% %
4533% %
4534%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4535%
4536% SolarizeImage() applies a special effect to the image, similar to the effect
4537% achieved in a photo darkroom by selectively exposing areas of photo
4538% sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4539% measure of the extent of the solarization.
4540%
4541% The format of the SolarizeImage method is:
4542%
cristy5cbc0162011-08-29 00:36:28 +00004543% MagickBooleanType SolarizeImage(Image *image,const double threshold,
4544% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004545%
4546% A description of each parameter follows:
4547%
4548% o image: the image.
4549%
4550% o threshold: Define the extent of the solarization.
4551%
cristy5cbc0162011-08-29 00:36:28 +00004552% o exception: return any errors or warnings in this structure.
4553%
cristy3ed852e2009-09-05 21:47:34 +00004554*/
4555MagickExport MagickBooleanType SolarizeImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00004556 const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004557{
4558#define SolarizeImageTag "Solarize/Image"
4559
cristyc4c8d132010-01-07 01:58:38 +00004560 CacheView
4561 *image_view;
4562
cristy3ed852e2009-09-05 21:47:34 +00004563 MagickBooleanType
4564 status;
4565
cristybb503372010-05-27 20:51:26 +00004566 MagickOffsetType
4567 progress;
4568
4569 ssize_t
4570 y;
4571
cristy3ed852e2009-09-05 21:47:34 +00004572 assert(image != (Image *) NULL);
4573 assert(image->signature == MagickSignature);
4574 if (image->debug != MagickFalse)
4575 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristya6400b12013-03-15 12:20:18 +00004576 if (IsGrayColorspace(image->colorspace) != MagickFalse)
cristy0c81d062013-04-21 15:22:02 +00004577 (void) SetImageColorspace(image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00004578 if (image->storage_class == PseudoClass)
4579 {
cristybb503372010-05-27 20:51:26 +00004580 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004581 i;
4582
4583 /*
4584 Solarize colormap.
4585 */
cristybb503372010-05-27 20:51:26 +00004586 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00004587 {
cristya19f1d72012-08-07 18:24:38 +00004588 if ((double) image->colormap[i].red > threshold)
cristy6e963d82012-06-19 15:23:24 +00004589 image->colormap[i].red=QuantumRange-image->colormap[i].red;
cristya19f1d72012-08-07 18:24:38 +00004590 if ((double) image->colormap[i].green > threshold)
cristy13ceedd2013-12-02 17:57:23 +00004591 image->colormap[i].green=QuantumRange-image->colormap[i].green;
cristya19f1d72012-08-07 18:24:38 +00004592 if ((double) image->colormap[i].blue > threshold)
cristy13ceedd2013-12-02 17:57:23 +00004593 image->colormap[i].blue=QuantumRange-image->colormap[i].blue;
cristy3ed852e2009-09-05 21:47:34 +00004594 }
4595 }
4596 /*
4597 Solarize image.
4598 */
4599 status=MagickTrue;
4600 progress=0;
cristy46ff2672012-12-14 15:32:26 +00004601 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00004602#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00004603 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00004604 magick_threads(image,image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00004605#endif
cristybb503372010-05-27 20:51:26 +00004606 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004607 {
cristybb503372010-05-27 20:51:26 +00004608 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004609 x;
4610
cristy4c08aed2011-07-01 19:47:50 +00004611 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004612 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004613
4614 if (status == MagickFalse)
4615 continue;
cristy5cbc0162011-08-29 00:36:28 +00004616 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00004617 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004618 {
4619 status=MagickFalse;
4620 continue;
4621 }
cristybb503372010-05-27 20:51:26 +00004622 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004623 {
cristy76f512e2011-09-12 01:26:56 +00004624 register ssize_t
4625 i;
4626
cristy883fde12013-04-08 00:50:13 +00004627 if (GetPixelReadMask(image,q) == 0)
cristy10a6c612012-01-29 21:41:05 +00004628 {
4629 q+=GetPixelChannels(image);
4630 continue;
4631 }
cristy76f512e2011-09-12 01:26:56 +00004632 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4633 {
cristy5a23c552013-02-13 14:34:28 +00004634 PixelChannel channel=GetPixelChannelChannel(image,i);
4635 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004636 if ((traits == UndefinedPixelTrait) ||
4637 ((traits & CopyPixelTrait) != 0))
4638 continue;
cristya19f1d72012-08-07 18:24:38 +00004639 if ((double) q[i] > threshold)
cristy76f512e2011-09-12 01:26:56 +00004640 q[i]=QuantumRange-q[i];
4641 }
cristyed231572011-07-14 02:18:59 +00004642 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004643 }
4644 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4645 status=MagickFalse;
4646 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4647 {
4648 MagickBooleanType
4649 proceed;
4650
cristyb5d5f722009-11-04 03:03:49 +00004651#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004652 #pragma omp critical (MagickCore_SolarizeImage)
cristy3ed852e2009-09-05 21:47:34 +00004653#endif
4654 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4655 if (proceed == MagickFalse)
4656 status=MagickFalse;
4657 }
4658 }
4659 image_view=DestroyCacheView(image_view);
4660 return(status);
4661}
4662
4663/*
4664%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4665% %
4666% %
4667% %
4668% S t e g a n o I m a g e %
4669% %
4670% %
4671% %
4672%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4673%
4674% SteganoImage() hides a digital watermark within the image. Recover
4675% the hidden watermark later to prove that the authenticity of an image.
4676% Offset defines the start position within the image to hide the watermark.
4677%
4678% The format of the SteganoImage method is:
4679%
4680% Image *SteganoImage(const Image *image,Image *watermark,
4681% ExceptionInfo *exception)
4682%
4683% A description of each parameter follows:
4684%
4685% o image: the image.
4686%
4687% o watermark: the watermark image.
4688%
4689% o exception: return any errors or warnings in this structure.
4690%
4691*/
4692MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4693 ExceptionInfo *exception)
4694{
cristye1bf8ad2010-09-19 17:07:03 +00004695#define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
cristy4c08aed2011-07-01 19:47:50 +00004696#define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
cristyeaedf062010-05-29 22:36:02 +00004697 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
cristy3ed852e2009-09-05 21:47:34 +00004698#define SteganoImageTag "Stegano/Image"
4699
cristyb0d3bb92010-09-22 14:37:58 +00004700 CacheView
4701 *stegano_view,
4702 *watermark_view;
4703
cristy3ed852e2009-09-05 21:47:34 +00004704 Image
4705 *stegano_image;
4706
4707 int
4708 c;
4709
cristy3ed852e2009-09-05 21:47:34 +00004710 MagickBooleanType
4711 status;
4712
cristy101ab702011-10-13 13:06:32 +00004713 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004714 pixel;
4715
cristy4c08aed2011-07-01 19:47:50 +00004716 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004717 *q;
4718
cristye1bf8ad2010-09-19 17:07:03 +00004719 register ssize_t
4720 x;
4721
cristybb503372010-05-27 20:51:26 +00004722 size_t
cristyeaedf062010-05-29 22:36:02 +00004723 depth,
4724 one;
cristy3ed852e2009-09-05 21:47:34 +00004725
cristye1bf8ad2010-09-19 17:07:03 +00004726 ssize_t
4727 i,
4728 j,
4729 k,
4730 y;
4731
cristy3ed852e2009-09-05 21:47:34 +00004732 /*
4733 Initialize steganographic image attributes.
4734 */
4735 assert(image != (const Image *) NULL);
4736 assert(image->signature == MagickSignature);
4737 if (image->debug != MagickFalse)
4738 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4739 assert(watermark != (const Image *) NULL);
4740 assert(watermark->signature == MagickSignature);
4741 assert(exception != (ExceptionInfo *) NULL);
4742 assert(exception->signature == MagickSignature);
cristyeaedf062010-05-29 22:36:02 +00004743 one=1UL;
cristy3ed852e2009-09-05 21:47:34 +00004744 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4745 if (stegano_image == (Image *) NULL)
4746 return((Image *) NULL);
cristyf61b1832012-04-01 01:38:19 +00004747 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
cristy574cc262011-08-05 01:23:58 +00004748 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004749 {
cristy3ed852e2009-09-05 21:47:34 +00004750 stegano_image=DestroyImage(stegano_image);
4751 return((Image *) NULL);
4752 }
cristy3ed852e2009-09-05 21:47:34 +00004753 /*
4754 Hide watermark in low-order bits of image.
4755 */
4756 c=0;
4757 i=0;
4758 j=0;
4759 depth=stegano_image->depth;
cristyf61b1832012-04-01 01:38:19 +00004760 k=stegano_image->offset;
cristyda16f162011-02-19 23:52:17 +00004761 status=MagickTrue;
cristy46ff2672012-12-14 15:32:26 +00004762 watermark_view=AcquireVirtualCacheView(watermark,exception);
4763 stegano_view=AcquireAuthenticCacheView(stegano_image,exception);
cristybb503372010-05-27 20:51:26 +00004764 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
cristy3ed852e2009-09-05 21:47:34 +00004765 {
cristybb503372010-05-27 20:51:26 +00004766 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
cristy3ed852e2009-09-05 21:47:34 +00004767 {
cristybb503372010-05-27 20:51:26 +00004768 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
cristy3ed852e2009-09-05 21:47:34 +00004769 {
cristy1707c6c2012-01-18 23:30:54 +00004770 ssize_t
4771 offset;
4772
cristyf05d4942012-03-17 16:26:09 +00004773 (void) GetOneCacheViewVirtualPixelInfo(watermark_view,x,y,&pixel,
cristyda1f9c12011-10-02 21:39:49 +00004774 exception);
cristy1707c6c2012-01-18 23:30:54 +00004775 offset=k/(ssize_t) stegano_image->columns;
4776 if (offset >= (ssize_t) stegano_image->rows)
cristy3ed852e2009-09-05 21:47:34 +00004777 break;
cristyb0d3bb92010-09-22 14:37:58 +00004778 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4779 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4780 exception);
cristyacd2ed22011-08-30 01:44:23 +00004781 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004782 break;
4783 switch (c)
4784 {
4785 case 0:
4786 {
cristyf61b1832012-04-01 01:38:19 +00004787 SetPixelRed(stegano_image,SetBit(GetPixelRed(stegano_image,q),j,
4788 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004789 break;
4790 }
4791 case 1:
4792 {
cristyf61b1832012-04-01 01:38:19 +00004793 SetPixelGreen(stegano_image,SetBit(GetPixelGreen(stegano_image,q),j,
4794 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004795 break;
4796 }
4797 case 2:
4798 {
cristyf61b1832012-04-01 01:38:19 +00004799 SetPixelBlue(stegano_image,SetBit(GetPixelBlue(stegano_image,q),j,
4800 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004801 break;
4802 }
4803 }
cristyb0d3bb92010-09-22 14:37:58 +00004804 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004805 break;
4806 c++;
4807 if (c == 3)
4808 c=0;
4809 k++;
cristybb503372010-05-27 20:51:26 +00004810 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
cristy3ed852e2009-09-05 21:47:34 +00004811 k=0;
cristyf61b1832012-04-01 01:38:19 +00004812 if (k == stegano_image->offset)
cristy3ed852e2009-09-05 21:47:34 +00004813 j++;
4814 }
4815 }
cristy8b27a6d2010-02-14 03:31:15 +00004816 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004817 {
cristy8b27a6d2010-02-14 03:31:15 +00004818 MagickBooleanType
4819 proceed;
4820
4821 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4822 (depth-i),depth);
4823 if (proceed == MagickFalse)
4824 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004825 }
4826 }
cristyb0d3bb92010-09-22 14:37:58 +00004827 stegano_view=DestroyCacheView(stegano_view);
4828 watermark_view=DestroyCacheView(watermark_view);
cristyda16f162011-02-19 23:52:17 +00004829 if (status == MagickFalse)
cristyfab83642012-12-14 00:31:38 +00004830 stegano_image=DestroyImage(stegano_image);
cristy3ed852e2009-09-05 21:47:34 +00004831 return(stegano_image);
4832}
4833
4834/*
4835%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4836% %
4837% %
4838% %
4839% S t e r e o A n a g l y p h I m a g e %
4840% %
4841% %
4842% %
4843%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4844%
4845% StereoAnaglyphImage() combines two images and produces a single image that
4846% is the composite of a left and right image of a stereo pair. Special
4847% red-green stereo glasses are required to view this effect.
4848%
4849% The format of the StereoAnaglyphImage method is:
4850%
4851% Image *StereoImage(const Image *left_image,const Image *right_image,
4852% ExceptionInfo *exception)
4853% Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004854% const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004855% ExceptionInfo *exception)
4856%
4857% A description of each parameter follows:
4858%
4859% o left_image: the left image.
4860%
4861% o right_image: the right image.
4862%
4863% o exception: return any errors or warnings in this structure.
4864%
4865% o x_offset: amount, in pixels, by which the left image is offset to the
4866% right of the right image.
4867%
4868% o y_offset: amount, in pixels, by which the left image is offset to the
4869% bottom of the right image.
4870%
4871%
4872*/
4873MagickExport Image *StereoImage(const Image *left_image,
4874 const Image *right_image,ExceptionInfo *exception)
4875{
4876 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4877}
4878
4879MagickExport Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004880 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004881 ExceptionInfo *exception)
4882{
4883#define StereoImageTag "Stereo/Image"
4884
4885 const Image
4886 *image;
4887
4888 Image
4889 *stereo_image;
4890
cristy3ed852e2009-09-05 21:47:34 +00004891 MagickBooleanType
4892 status;
4893
cristy9d314ff2011-03-09 01:30:28 +00004894 ssize_t
4895 y;
4896
cristy3ed852e2009-09-05 21:47:34 +00004897 assert(left_image != (const Image *) NULL);
4898 assert(left_image->signature == MagickSignature);
4899 if (left_image->debug != MagickFalse)
4900 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4901 left_image->filename);
4902 assert(right_image != (const Image *) NULL);
4903 assert(right_image->signature == MagickSignature);
4904 assert(exception != (ExceptionInfo *) NULL);
4905 assert(exception->signature == MagickSignature);
4906 assert(right_image != (const Image *) NULL);
4907 image=left_image;
4908 if ((left_image->columns != right_image->columns) ||
4909 (left_image->rows != right_image->rows))
4910 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4911 /*
4912 Initialize stereo image attributes.
4913 */
4914 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4915 MagickTrue,exception);
4916 if (stereo_image == (Image *) NULL)
4917 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004918 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004919 {
cristy3ed852e2009-09-05 21:47:34 +00004920 stereo_image=DestroyImage(stereo_image);
4921 return((Image *) NULL);
4922 }
cristy079c78d2012-07-03 11:53:48 +00004923 (void) SetImageColorspace(stereo_image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00004924 /*
4925 Copy left image to red channel and right image to blue channel.
4926 */
cristyda16f162011-02-19 23:52:17 +00004927 status=MagickTrue;
cristybb503372010-05-27 20:51:26 +00004928 for (y=0; y < (ssize_t) stereo_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004929 {
cristy4c08aed2011-07-01 19:47:50 +00004930 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004931 *restrict p,
4932 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004933
cristybb503372010-05-27 20:51:26 +00004934 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004935 x;
4936
cristy4c08aed2011-07-01 19:47:50 +00004937 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004938 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +00004939
4940 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4941 exception);
4942 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4943 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
cristy76f512e2011-09-12 01:26:56 +00004944 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4945 (r == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004946 break;
cristybb503372010-05-27 20:51:26 +00004947 for (x=0; x < (ssize_t) stereo_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004948 {
cristy4c08aed2011-07-01 19:47:50 +00004949 SetPixelRed(image,GetPixelRed(left_image,p),r);
cristy76f512e2011-09-12 01:26:56 +00004950 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4951 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4952 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4953 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4954 GetPixelAlpha(right_image,q))/2,r);
cristyed231572011-07-14 02:18:59 +00004955 p+=GetPixelChannels(left_image);
cristy76f512e2011-09-12 01:26:56 +00004956 q+=GetPixelChannels(right_image);
4957 r+=GetPixelChannels(stereo_image);
cristy3ed852e2009-09-05 21:47:34 +00004958 }
4959 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4960 break;
cristy8b27a6d2010-02-14 03:31:15 +00004961 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004962 {
cristy8b27a6d2010-02-14 03:31:15 +00004963 MagickBooleanType
4964 proceed;
4965
cristybb503372010-05-27 20:51:26 +00004966 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4967 stereo_image->rows);
cristy8b27a6d2010-02-14 03:31:15 +00004968 if (proceed == MagickFalse)
4969 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004970 }
4971 }
cristyda16f162011-02-19 23:52:17 +00004972 if (status == MagickFalse)
cristyfab83642012-12-14 00:31:38 +00004973 stereo_image=DestroyImage(stereo_image);
cristy3ed852e2009-09-05 21:47:34 +00004974 return(stereo_image);
4975}
4976
4977/*
4978%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4979% %
4980% %
4981% %
4982% S w i r l I m a g e %
4983% %
4984% %
4985% %
4986%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4987%
4988% SwirlImage() swirls the pixels about the center of the image, where
4989% degrees indicates the sweep of the arc through which each pixel is moved.
4990% You get a more dramatic effect as the degrees move from 1 to 360.
4991%
4992% The format of the SwirlImage method is:
4993%
4994% Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00004995% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004996%
4997% A description of each parameter follows:
4998%
4999% o image: the image.
5000%
5001% o degrees: Define the tightness of the swirling effect.
5002%
cristy76f512e2011-09-12 01:26:56 +00005003% o method: the pixel interpolation method.
5004%
cristy3ed852e2009-09-05 21:47:34 +00005005% o exception: return any errors or warnings in this structure.
5006%
5007*/
5008MagickExport Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00005009 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005010{
5011#define SwirlImageTag "Swirl/Image"
5012
cristyfa112112010-01-04 17:48:07 +00005013 CacheView
5014 *image_view,
cristy5ee937c2013-04-19 12:55:16 +00005015 *interpolate_view,
cristyfa112112010-01-04 17:48:07 +00005016 *swirl_view;
5017
cristy3ed852e2009-09-05 21:47:34 +00005018 Image
5019 *swirl_image;
5020
cristy3ed852e2009-09-05 21:47:34 +00005021 MagickBooleanType
5022 status;
5023
cristybb503372010-05-27 20:51:26 +00005024 MagickOffsetType
5025 progress;
5026
cristya19f1d72012-08-07 18:24:38 +00005027 double
cristy3ed852e2009-09-05 21:47:34 +00005028 radius;
5029
5030 PointInfo
5031 center,
5032 scale;
5033
cristybb503372010-05-27 20:51:26 +00005034 ssize_t
5035 y;
5036
cristy3ed852e2009-09-05 21:47:34 +00005037 /*
5038 Initialize swirl image attributes.
5039 */
5040 assert(image != (const Image *) NULL);
5041 assert(image->signature == MagickSignature);
5042 if (image->debug != MagickFalse)
5043 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5044 assert(exception != (ExceptionInfo *) NULL);
5045 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00005046 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005047 if (swirl_image == (Image *) NULL)
5048 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005049 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005050 {
cristy3ed852e2009-09-05 21:47:34 +00005051 swirl_image=DestroyImage(swirl_image);
5052 return((Image *) NULL);
5053 }
cristy4c08aed2011-07-01 19:47:50 +00005054 if (swirl_image->background_color.alpha != OpaqueAlpha)
cristy8a46d822012-08-28 23:32:39 +00005055 swirl_image->alpha_trait=BlendPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +00005056 /*
5057 Compute scaling factor.
5058 */
5059 center.x=(double) image->columns/2.0;
5060 center.y=(double) image->rows/2.0;
5061 radius=MagickMax(center.x,center.y);
5062 scale.x=1.0;
5063 scale.y=1.0;
5064 if (image->columns > image->rows)
5065 scale.y=(double) image->columns/(double) image->rows;
5066 else
5067 if (image->columns < image->rows)
5068 scale.x=(double) image->rows/(double) image->columns;
5069 degrees=(double) DegreesToRadians(degrees);
5070 /*
5071 Swirl image.
5072 */
5073 status=MagickTrue;
5074 progress=0;
cristy46ff2672012-12-14 15:32:26 +00005075 image_view=AcquireVirtualCacheView(image,exception);
cristy5ee937c2013-04-19 12:55:16 +00005076 interpolate_view=AcquireVirtualCacheView(image,exception);
cristy46ff2672012-12-14 15:32:26 +00005077 swirl_view=AcquireAuthenticCacheView(swirl_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00005078#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005079 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00005080 magick_threads(image,swirl_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005081#endif
cristybb503372010-05-27 20:51:26 +00005082 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005083 {
cristya19f1d72012-08-07 18:24:38 +00005084 double
cristy3ed852e2009-09-05 21:47:34 +00005085 distance;
5086
5087 PointInfo
5088 delta;
5089
cristy6d188022011-09-12 13:23:33 +00005090 register const Quantum
5091 *restrict p;
5092
cristybb503372010-05-27 20:51:26 +00005093 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005094 x;
5095
cristy4c08aed2011-07-01 19:47:50 +00005096 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005097 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005098
5099 if (status == MagickFalse)
5100 continue;
cristy1665ce12013-04-19 12:18:43 +00005101 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00005102 q=QueueCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00005103 exception);
cristy6d188022011-09-12 13:23:33 +00005104 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005105 {
5106 status=MagickFalse;
5107 continue;
5108 }
cristy3ed852e2009-09-05 21:47:34 +00005109 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00005110 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005111 {
5112 /*
5113 Determine if the pixel is within an ellipse.
5114 */
cristy883fde12013-04-08 00:50:13 +00005115 if (GetPixelReadMask(image,p) == 0)
cristy10a6c612012-01-29 21:41:05 +00005116 {
cristyc3a58022013-10-09 23:22:42 +00005117 SetPixelBackgoundColor(swirl_image,q);
cristy10a6c612012-01-29 21:41:05 +00005118 p+=GetPixelChannels(image);
5119 q+=GetPixelChannels(swirl_image);
5120 continue;
5121 }
cristy3ed852e2009-09-05 21:47:34 +00005122 delta.x=scale.x*(double) (x-center.x);
5123 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00005124 if (distance >= (radius*radius))
5125 {
cristy1707c6c2012-01-18 23:30:54 +00005126 register ssize_t
5127 i;
5128
cristy6d188022011-09-12 13:23:33 +00005129 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy1707c6c2012-01-18 23:30:54 +00005130 {
cristy5a23c552013-02-13 14:34:28 +00005131 PixelChannel channel=GetPixelChannelChannel(image,i);
5132 PixelTrait traits=GetPixelChannelTraits(image,channel);
5133 PixelTrait swirl_traits=GetPixelChannelTraits(swirl_image,channel);
cristy1707c6c2012-01-18 23:30:54 +00005134 if ((traits == UndefinedPixelTrait) ||
5135 (swirl_traits == UndefinedPixelTrait))
5136 continue;
5137 SetPixelChannel(swirl_image,channel,p[i],q);
5138 }
cristy6d188022011-09-12 13:23:33 +00005139 }
5140 else
cristy3ed852e2009-09-05 21:47:34 +00005141 {
cristya19f1d72012-08-07 18:24:38 +00005142 double
cristy3ed852e2009-09-05 21:47:34 +00005143 cosine,
5144 factor,
5145 sine;
5146
5147 /*
5148 Swirl the pixel.
5149 */
5150 factor=1.0-sqrt((double) distance)/radius;
5151 sine=sin((double) (degrees*factor*factor));
5152 cosine=cos((double) (degrees*factor*factor));
cristy5ee937c2013-04-19 12:55:16 +00005153 status=InterpolatePixelChannels(image,interpolate_view,swirl_image,
5154 method,((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
cristy76f512e2011-09-12 01:26:56 +00005155 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00005156 }
cristy6d188022011-09-12 13:23:33 +00005157 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00005158 q+=GetPixelChannels(swirl_image);
cristy3ed852e2009-09-05 21:47:34 +00005159 }
5160 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5161 status=MagickFalse;
5162 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5163 {
5164 MagickBooleanType
5165 proceed;
5166
cristyb5d5f722009-11-04 03:03:49 +00005167#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005168 #pragma omp critical (MagickCore_SwirlImage)
cristy3ed852e2009-09-05 21:47:34 +00005169#endif
5170 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5171 if (proceed == MagickFalse)
5172 status=MagickFalse;
5173 }
5174 }
5175 swirl_view=DestroyCacheView(swirl_view);
cristy5ee937c2013-04-19 12:55:16 +00005176 interpolate_view=DestroyCacheView(interpolate_view);
cristy3ed852e2009-09-05 21:47:34 +00005177 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005178 if (status == MagickFalse)
5179 swirl_image=DestroyImage(swirl_image);
5180 return(swirl_image);
5181}
5182
5183/*
5184%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5185% %
5186% %
5187% %
5188% T i n t I m a g e %
5189% %
5190% %
5191% %
5192%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5193%
5194% TintImage() applies a color vector to each pixel in the image. The length
5195% of the vector is 0 for black and white and at its maximum for the midtones.
5196% The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5197%
5198% The format of the TintImage method is:
5199%
cristyb817c3f2011-10-03 14:00:35 +00005200% Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005201% const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005202%
5203% A description of each parameter follows:
5204%
5205% o image: the image.
5206%
cristyb817c3f2011-10-03 14:00:35 +00005207% o blend: A color value used for tinting.
cristy3ed852e2009-09-05 21:47:34 +00005208%
5209% o tint: A color value used for tinting.
5210%
5211% o exception: return any errors or warnings in this structure.
5212%
5213*/
cristyb817c3f2011-10-03 14:00:35 +00005214MagickExport Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005215 const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005216{
5217#define TintImageTag "Tint/Image"
5218
cristyc4c8d132010-01-07 01:58:38 +00005219 CacheView
5220 *image_view,
5221 *tint_view;
5222
cristyf54150e2013-04-11 01:25:47 +00005223 double
5224 intensity;
5225
cristy3ed852e2009-09-05 21:47:34 +00005226 GeometryInfo
5227 geometry_info;
5228
5229 Image
5230 *tint_image;
5231
cristy3ed852e2009-09-05 21:47:34 +00005232 MagickBooleanType
5233 status;
5234
cristybb503372010-05-27 20:51:26 +00005235 MagickOffsetType
5236 progress;
cristy3ed852e2009-09-05 21:47:34 +00005237
cristy4c08aed2011-07-01 19:47:50 +00005238 PixelInfo
cristy1707c6c2012-01-18 23:30:54 +00005239 color_vector;
cristy3ed852e2009-09-05 21:47:34 +00005240
cristybb503372010-05-27 20:51:26 +00005241 MagickStatusType
5242 flags;
5243
5244 ssize_t
5245 y;
5246
cristy3ed852e2009-09-05 21:47:34 +00005247 /*
5248 Allocate tint image.
5249 */
5250 assert(image != (const Image *) NULL);
5251 assert(image->signature == MagickSignature);
5252 if (image->debug != MagickFalse)
5253 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5254 assert(exception != (ExceptionInfo *) NULL);
5255 assert(exception->signature == MagickSignature);
5256 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5257 if (tint_image == (Image *) NULL)
5258 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005259 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005260 {
cristy3ed852e2009-09-05 21:47:34 +00005261 tint_image=DestroyImage(tint_image);
5262 return((Image *) NULL);
5263 }
cristya6400b12013-03-15 12:20:18 +00005264 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
cristy4dab3802013-03-15 22:08:15 +00005265 (IsPixelInfoGray(tint) == MagickFalse))
cristya6400b12013-03-15 12:20:18 +00005266 (void) SetImageColorspace(tint_image,sRGBColorspace,exception);
cristyaed9c382011-10-03 17:54:21 +00005267 if (blend == (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005268 return(tint_image);
5269 /*
5270 Determine RGB values of the color.
5271 */
cristy1707c6c2012-01-18 23:30:54 +00005272 GetPixelInfo(image,&color_vector);
cristyb817c3f2011-10-03 14:00:35 +00005273 flags=ParseGeometry(blend,&geometry_info);
cristy1707c6c2012-01-18 23:30:54 +00005274 color_vector.red=geometry_info.rho;
5275 color_vector.green=geometry_info.rho;
5276 color_vector.blue=geometry_info.rho;
cristy92ac5722013-03-09 15:09:38 +00005277 color_vector.alpha=(MagickRealType) OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00005278 if ((flags & SigmaValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005279 color_vector.green=geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +00005280 if ((flags & XiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005281 color_vector.blue=geometry_info.xi;
cristyb817c3f2011-10-03 14:00:35 +00005282 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005283 color_vector.alpha=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005284 if (image->colorspace == CMYKColorspace)
5285 {
cristy1707c6c2012-01-18 23:30:54 +00005286 color_vector.black=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005287 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005288 color_vector.black=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005289 if ((flags & ChiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005290 color_vector.alpha=geometry_info.chi;
cristy76f512e2011-09-12 01:26:56 +00005291 }
cristya19f1d72012-08-07 18:24:38 +00005292 intensity=(double) GetPixelInfoIntensity(tint);
cristyf54150e2013-04-11 01:25:47 +00005293 color_vector.red=(double) (color_vector.red*tint->red/100.0-intensity);
5294 color_vector.green=(double) (color_vector.green*tint->green/100.0-intensity);
5295 color_vector.blue=(double) (color_vector.blue*tint->blue/100.0-intensity);
5296 color_vector.black=(double) (color_vector.black*tint->black/100.0-intensity);
5297 color_vector.alpha=(double) (color_vector.alpha*tint->alpha/100.0-intensity);
cristy3ed852e2009-09-05 21:47:34 +00005298 /*
5299 Tint image.
5300 */
5301 status=MagickTrue;
5302 progress=0;
cristy46ff2672012-12-14 15:32:26 +00005303 image_view=AcquireVirtualCacheView(image,exception);
5304 tint_view=AcquireAuthenticCacheView(tint_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00005305#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005306 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00005307 magick_threads(image,tint_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005308#endif
cristybb503372010-05-27 20:51:26 +00005309 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005310 {
cristy4c08aed2011-07-01 19:47:50 +00005311 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00005312 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00005313
cristy4c08aed2011-07-01 19:47:50 +00005314 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005315 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005316
cristy6b91acb2011-04-19 12:23:54 +00005317 register ssize_t
5318 x;
5319
cristy3ed852e2009-09-05 21:47:34 +00005320 if (status == MagickFalse)
5321 continue;
5322 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5323 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5324 exception);
cristy4c08aed2011-07-01 19:47:50 +00005325 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005326 {
5327 status=MagickFalse;
5328 continue;
5329 }
cristybb503372010-05-27 20:51:26 +00005330 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005331 {
cristy4c08aed2011-07-01 19:47:50 +00005332 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00005333 pixel;
5334
cristya19f1d72012-08-07 18:24:38 +00005335 double
cristy3ed852e2009-09-05 21:47:34 +00005336 weight;
5337
cristy1707c6c2012-01-18 23:30:54 +00005338 register ssize_t
5339 i;
5340
5341 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5342 {
cristy5a23c552013-02-13 14:34:28 +00005343 PixelChannel channel=GetPixelChannelChannel(image,i);
5344 PixelTrait traits=GetPixelChannelTraits(image,channel);
5345 PixelTrait tint_traits=GetPixelChannelTraits(tint_image,channel);
cristy1707c6c2012-01-18 23:30:54 +00005346 if ((traits == UndefinedPixelTrait) ||
5347 (tint_traits == UndefinedPixelTrait))
5348 continue;
cristy1eced092012-08-10 23:10:56 +00005349 if (((tint_traits & CopyPixelTrait) != 0) ||
cristy883fde12013-04-08 00:50:13 +00005350 (GetPixelReadMask(image,p) == 0))
cristy1707c6c2012-01-18 23:30:54 +00005351 {
5352 SetPixelChannel(tint_image,channel,p[i],q);
5353 continue;
5354 }
5355 }
5356 GetPixelInfo(image,&pixel);
5357 weight=QuantumScale*GetPixelRed(image,p)-0.5;
cristy1eced092012-08-10 23:10:56 +00005358 pixel.red=(double) GetPixelRed(image,p)+color_vector.red*(1.0-(4.0*
5359 (weight*weight)));
cristy1707c6c2012-01-18 23:30:54 +00005360 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
cristy1eced092012-08-10 23:10:56 +00005361 pixel.green=(double) GetPixelGreen(image,p)+color_vector.green*(1.0-(4.0*
5362 (weight*weight)));
cristy1707c6c2012-01-18 23:30:54 +00005363 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
cristy1eced092012-08-10 23:10:56 +00005364 pixel.blue=(double) GetPixelBlue(image,p)+color_vector.blue*(1.0-(4.0*
5365 (weight*weight)));
cristy1707c6c2012-01-18 23:30:54 +00005366 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
cristy1eced092012-08-10 23:10:56 +00005367 pixel.black=(double) GetPixelBlack(image,p)+color_vector.black*(1.0-(4.0*
5368 (weight*weight)));
cristy1707c6c2012-01-18 23:30:54 +00005369 SetPixelInfoPixel(tint_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00005370 p+=GetPixelChannels(image);
5371 q+=GetPixelChannels(tint_image);
cristy3ed852e2009-09-05 21:47:34 +00005372 }
5373 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5374 status=MagickFalse;
5375 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5376 {
5377 MagickBooleanType
5378 proceed;
5379
cristyb5d5f722009-11-04 03:03:49 +00005380#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005381 #pragma omp critical (MagickCore_TintImage)
cristy3ed852e2009-09-05 21:47:34 +00005382#endif
5383 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5384 if (proceed == MagickFalse)
5385 status=MagickFalse;
5386 }
5387 }
5388 tint_view=DestroyCacheView(tint_view);
5389 image_view=DestroyCacheView(image_view);
5390 if (status == MagickFalse)
5391 tint_image=DestroyImage(tint_image);
5392 return(tint_image);
5393}
5394
5395/*
5396%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5397% %
5398% %
5399% %
5400% V i g n e t t e I m a g e %
5401% %
5402% %
5403% %
5404%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5405%
5406% VignetteImage() softens the edges of the image in vignette style.
5407%
5408% The format of the VignetteImage method is:
5409%
5410% Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005411% const double sigma,const ssize_t x,const ssize_t y,
cristy05c0c9a2011-09-05 23:16:13 +00005412% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005413%
5414% A description of each parameter follows:
5415%
5416% o image: the image.
5417%
5418% o radius: the radius of the pixel neighborhood.
5419%
5420% o sigma: the standard deviation of the Gaussian, in pixels.
5421%
5422% o x, y: Define the x and y ellipse offset.
5423%
5424% o exception: return any errors or warnings in this structure.
5425%
5426*/
5427MagickExport Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005428 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005429{
5430 char
5431 ellipse[MaxTextExtent];
5432
5433 DrawInfo
5434 *draw_info;
5435
5436 Image
5437 *canvas_image,
5438 *blur_image,
5439 *oval_image,
5440 *vignette_image;
5441
5442 assert(image != (Image *) NULL);
5443 assert(image->signature == MagickSignature);
5444 if (image->debug != MagickFalse)
5445 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5446 assert(exception != (ExceptionInfo *) NULL);
5447 assert(exception->signature == MagickSignature);
5448 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5449 if (canvas_image == (Image *) NULL)
5450 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005451 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005452 {
cristy3ed852e2009-09-05 21:47:34 +00005453 canvas_image=DestroyImage(canvas_image);
5454 return((Image *) NULL);
5455 }
cristy8a46d822012-08-28 23:32:39 +00005456 canvas_image->alpha_trait=BlendPixelTrait;
cristy98621462011-12-31 22:31:11 +00005457 oval_image=CloneImage(canvas_image,canvas_image->columns,canvas_image->rows,
5458 MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005459 if (oval_image == (Image *) NULL)
5460 {
5461 canvas_image=DestroyImage(canvas_image);
5462 return((Image *) NULL);
5463 }
cristy9950d572011-10-01 18:22:35 +00005464 (void) QueryColorCompliance("#000000",AllCompliance,
5465 &oval_image->background_color,exception);
cristyea1a8aa2011-10-20 13:24:06 +00005466 (void) SetImageBackgroundColor(oval_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00005467 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
cristy9950d572011-10-01 18:22:35 +00005468 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5469 exception);
5470 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5471 exception);
cristy1707c6c2012-01-18 23:30:54 +00005472 (void) FormatLocaleString(ellipse,MaxTextExtent,"ellipse %g,%g,%g,%g,"
5473 "0.0,360.0",image->columns/2.0,image->rows/2.0,image->columns/2.0-x,
5474 image->rows/2.0-y);
cristy3ed852e2009-09-05 21:47:34 +00005475 draw_info->primitive=AcquireString(ellipse);
cristy018f07f2011-09-04 21:15:19 +00005476 (void) DrawImage(oval_image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00005477 draw_info=DestroyDrawInfo(draw_info);
cristyaa2c16c2012-03-25 22:21:35 +00005478 blur_image=BlurImage(oval_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00005479 oval_image=DestroyImage(oval_image);
5480 if (blur_image == (Image *) NULL)
5481 {
5482 canvas_image=DestroyImage(canvas_image);
5483 return((Image *) NULL);
5484 }
cristy8a46d822012-08-28 23:32:39 +00005485 blur_image->alpha_trait=UndefinedPixelTrait;
cristy39172402012-03-30 13:04:39 +00005486 (void) CompositeImage(canvas_image,blur_image,IntensityCompositeOp,MagickTrue,
5487 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00005488 blur_image=DestroyImage(blur_image);
5489 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5490 canvas_image=DestroyImage(canvas_image);
cristy66d26122012-06-23 21:56:40 +00005491 if (vignette_image != (Image *) NULL)
5492 (void) TransformImageColorspace(vignette_image,image->colorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00005493 return(vignette_image);
5494}
5495
5496/*
5497%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5498% %
5499% %
5500% %
5501% W a v e I m a g e %
5502% %
5503% %
5504% %
5505%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5506%
5507% WaveImage() creates a "ripple" effect in the image by shifting the pixels
cristycee97112010-05-28 00:44:52 +00005508% vertically along a sine wave whose amplitude and wavelength is specified
cristy3ed852e2009-09-05 21:47:34 +00005509% by the given parameters.
5510%
5511% The format of the WaveImage method is:
5512%
5513% Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005514% const double wave_length,const PixelInterpolateMethod method,
5515% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005516%
5517% A description of each parameter follows:
5518%
5519% o image: the image.
5520%
5521% o amplitude, wave_length: Define the amplitude and wave length of the
5522% sine wave.
5523%
cristy5c4e2582011-09-11 19:21:03 +00005524% o interpolate: the pixel interpolation method.
5525%
cristy3ed852e2009-09-05 21:47:34 +00005526% o exception: return any errors or warnings in this structure.
5527%
5528*/
5529MagickExport Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005530 const double wave_length,const PixelInterpolateMethod method,
5531 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005532{
5533#define WaveImageTag "Wave/Image"
5534
cristyfa112112010-01-04 17:48:07 +00005535 CacheView
cristyd76c51e2011-03-26 00:21:26 +00005536 *image_view,
cristyfa112112010-01-04 17:48:07 +00005537 *wave_view;
5538
cristy3ed852e2009-09-05 21:47:34 +00005539 Image
5540 *wave_image;
5541
cristy3ed852e2009-09-05 21:47:34 +00005542 MagickBooleanType
5543 status;
5544
cristybb503372010-05-27 20:51:26 +00005545 MagickOffsetType
5546 progress;
5547
cristya19f1d72012-08-07 18:24:38 +00005548 double
cristy3ed852e2009-09-05 21:47:34 +00005549 *sine_map;
5550
cristybb503372010-05-27 20:51:26 +00005551 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005552 i;
5553
cristybb503372010-05-27 20:51:26 +00005554 ssize_t
5555 y;
5556
cristy3ed852e2009-09-05 21:47:34 +00005557 /*
5558 Initialize wave image attributes.
5559 */
5560 assert(image != (Image *) NULL);
5561 assert(image->signature == MagickSignature);
5562 if (image->debug != MagickFalse)
5563 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5564 assert(exception != (ExceptionInfo *) NULL);
5565 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00005566 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
cristy3ed852e2009-09-05 21:47:34 +00005567 fabs(amplitude)),MagickTrue,exception);
5568 if (wave_image == (Image *) NULL)
5569 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005570 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005571 {
cristy3ed852e2009-09-05 21:47:34 +00005572 wave_image=DestroyImage(wave_image);
5573 return((Image *) NULL);
5574 }
cristy4c08aed2011-07-01 19:47:50 +00005575 if (wave_image->background_color.alpha != OpaqueAlpha)
cristy8a46d822012-08-28 23:32:39 +00005576 wave_image->alpha_trait=BlendPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +00005577 /*
5578 Allocate sine map.
5579 */
cristya19f1d72012-08-07 18:24:38 +00005580 sine_map=(double *) AcquireQuantumMemory((size_t) wave_image->columns,
cristy3ed852e2009-09-05 21:47:34 +00005581 sizeof(*sine_map));
cristya19f1d72012-08-07 18:24:38 +00005582 if (sine_map == (double *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005583 {
5584 wave_image=DestroyImage(wave_image);
5585 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5586 }
cristybb503372010-05-27 20:51:26 +00005587 for (i=0; i < (ssize_t) wave_image->columns; i++)
cristy4205a3c2010-09-12 20:19:59 +00005588 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5589 wave_length));
cristy3ed852e2009-09-05 21:47:34 +00005590 /*
5591 Wave image.
5592 */
5593 status=MagickTrue;
5594 progress=0;
cristy46ff2672012-12-14 15:32:26 +00005595 image_view=AcquireVirtualCacheView(image,exception);
5596 wave_view=AcquireAuthenticCacheView(wave_image,exception);
cristyd76c51e2011-03-26 00:21:26 +00005597 (void) SetCacheViewVirtualPixelMethod(image_view,
5598 BackgroundVirtualPixelMethod);
cristyb5d5f722009-11-04 03:03:49 +00005599#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005600 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00005601 magick_threads(image,wave_image,wave_image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005602#endif
cristybb503372010-05-27 20:51:26 +00005603 for (y=0; y < (ssize_t) wave_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005604 {
cristy4c08aed2011-07-01 19:47:50 +00005605 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005606 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005607
cristye97bb922011-04-03 01:36:52 +00005608 register ssize_t
5609 x;
5610
cristy3ed852e2009-09-05 21:47:34 +00005611 if (status == MagickFalse)
5612 continue;
5613 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5614 exception);
cristyacd2ed22011-08-30 01:44:23 +00005615 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005616 {
5617 status=MagickFalse;
5618 continue;
5619 }
cristybb503372010-05-27 20:51:26 +00005620 for (x=0; x < (ssize_t) wave_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005621 {
cristy5c4e2582011-09-11 19:21:03 +00005622 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5623 (double) x,(double) (y-sine_map[x]),q,exception);
cristyed231572011-07-14 02:18:59 +00005624 q+=GetPixelChannels(wave_image);
cristy3ed852e2009-09-05 21:47:34 +00005625 }
5626 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5627 status=MagickFalse;
5628 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5629 {
5630 MagickBooleanType
5631 proceed;
5632
cristyb5d5f722009-11-04 03:03:49 +00005633#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005634 #pragma omp critical (MagickCore_WaveImage)
cristy3ed852e2009-09-05 21:47:34 +00005635#endif
5636 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5637 if (proceed == MagickFalse)
5638 status=MagickFalse;
5639 }
5640 }
5641 wave_view=DestroyCacheView(wave_view);
cristyd76c51e2011-03-26 00:21:26 +00005642 image_view=DestroyCacheView(image_view);
cristya19f1d72012-08-07 18:24:38 +00005643 sine_map=(double *) RelinquishMagickMemory(sine_map);
cristy3ed852e2009-09-05 21:47:34 +00005644 if (status == MagickFalse)
5645 wave_image=DestroyImage(wave_image);
5646 return(wave_image);
5647}