blob: a304342a2caf3d9885b0ac21727ecfc2677550f3 [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;
cristy3ed852e2009-09-05 21:47:34 +00001413 i%=length;
1414 image=GetImageFromList(fx_info->images,i);
1415 if (image == (Image *) NULL)
1416 {
1417 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
cristyefe601c2013-01-05 17:51:12 +00001418 "NoSuchImage","`%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00001419 return(0.0);
1420 }
cristy4c08aed2011-07-01 19:47:50 +00001421 GetPixelInfo(image,&pixel);
1422 (void) InterpolatePixelInfo(image,fx_info->view[i],image->interpolate,
cristy4f820712011-04-01 12:35:43 +00001423 point.x,point.y,&pixel,exception);
cristy1707c6c2012-01-18 23:30:54 +00001424 if ((strlen(p) > 2) && (LocaleCompare(p,"intensity") != 0) &&
cristyba213402013-06-28 14:05:12 +00001425 (LocaleCompare(p,"luma") != 0) && (LocaleCompare(p,"luminance") != 0) &&
1426 (LocaleCompare(p,"hue") != 0) && (LocaleCompare(p,"saturation") != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00001427 (LocaleCompare(p,"lightness") != 0))
1428 {
1429 char
1430 name[MaxTextExtent];
1431
1432 (void) CopyMagickString(name,p,MaxTextExtent);
1433 for (q=name+(strlen(name)-1); q > name; q--)
1434 {
1435 if (*q == ')')
1436 break;
1437 if (*q == '.')
1438 {
1439 *q='\0';
1440 break;
1441 }
1442 }
1443 if ((strlen(name) > 2) &&
1444 (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
1445 {
cristy4c08aed2011-07-01 19:47:50 +00001446 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001447 *color;
1448
cristy4c08aed2011-07-01 19:47:50 +00001449 color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
1450 if (color != (PixelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001451 {
1452 pixel=(*color);
1453 p+=strlen(name);
1454 }
1455 else
cristy1707c6c2012-01-18 23:30:54 +00001456 {
1457 MagickBooleanType
1458 status;
1459
1460 status=QueryColorCompliance(name,AllCompliance,&pixel,
1461 fx_info->exception);
1462 if (status != MagickFalse)
1463 {
1464 (void) AddValueToSplayTree(fx_info->colors,ConstantString(
1465 name),ClonePixelInfo(&pixel));
1466 p+=strlen(name);
1467 }
1468 }
cristy3ed852e2009-09-05 21:47:34 +00001469 }
1470 }
1471 (void) CopyMagickString(symbol,p,MaxTextExtent);
1472 StripString(symbol);
1473 if (*symbol == '\0')
1474 {
1475 switch (channel)
1476 {
cristy0568ffc2011-07-25 16:54:14 +00001477 case RedPixelChannel: return(QuantumScale*pixel.red);
1478 case GreenPixelChannel: return(QuantumScale*pixel.green);
1479 case BluePixelChannel: return(QuantumScale*pixel.blue);
1480 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001481 {
1482 if (image->colorspace != CMYKColorspace)
1483 {
1484 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001485 ImageError,"ColorSeparatedImageRequired","`%s'",
cristy3ed852e2009-09-05 21:47:34 +00001486 image->filename);
1487 return(0.0);
1488 }
cristy4c08aed2011-07-01 19:47:50 +00001489 return(QuantumScale*pixel.black);
1490 }
cristy0568ffc2011-07-25 16:54:14 +00001491 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001492 {
cristya19f1d72012-08-07 18:24:38 +00001493 double
cristy4c08aed2011-07-01 19:47:50 +00001494 alpha;
1495
cristy8a46d822012-08-28 23:32:39 +00001496 if (pixel.alpha_trait != BlendPixelTrait)
cristy4c08aed2011-07-01 19:47:50 +00001497 return(1.0);
cristya19f1d72012-08-07 18:24:38 +00001498 alpha=(double) (QuantumScale*pixel.alpha);
cristy4c08aed2011-07-01 19:47:50 +00001499 return(alpha);
cristy3ed852e2009-09-05 21:47:34 +00001500 }
cristya382aca2011-12-06 18:22:48 +00001501 case IndexPixelChannel:
1502 return(0.0);
cristyb3a73b52011-07-26 01:34:43 +00001503 case IntensityPixelChannel:
cristyf364ed42010-12-15 01:54:43 +00001504 {
cristy4c08aed2011-07-01 19:47:50 +00001505 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristyf364ed42010-12-15 01:54:43 +00001506 }
cristy3ed852e2009-09-05 21:47:34 +00001507 default:
1508 break;
1509 }
1510 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
cristyefe601c2013-01-05 17:51:12 +00001511 "UnableToParseExpression","`%s'",p);
cristy3ed852e2009-09-05 21:47:34 +00001512 return(0.0);
1513 }
1514 switch (*symbol)
1515 {
1516 case 'A':
1517 case 'a':
1518 {
1519 if (LocaleCompare(symbol,"a") == 0)
cristya19f1d72012-08-07 18:24:38 +00001520 return((double) (QuantumScale*pixel.alpha));
cristy3ed852e2009-09-05 21:47:34 +00001521 break;
1522 }
1523 case 'B':
1524 case 'b':
1525 {
1526 if (LocaleCompare(symbol,"b") == 0)
1527 return(QuantumScale*pixel.blue);
1528 break;
1529 }
1530 case 'C':
1531 case 'c':
1532 {
1533 if (LocaleNCompare(symbol,"channel",7) == 0)
1534 {
1535 GeometryInfo
1536 channel_info;
1537
1538 MagickStatusType
1539 flags;
1540
1541 flags=ParseGeometry(symbol+7,&channel_info);
1542 if (image->colorspace == CMYKColorspace)
1543 switch (channel)
1544 {
cristy0568ffc2011-07-25 16:54:14 +00001545 case CyanPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001546 {
1547 if ((flags & RhoValue) == 0)
1548 return(0.0);
1549 return(channel_info.rho);
1550 }
cristy0568ffc2011-07-25 16:54:14 +00001551 case MagentaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001552 {
1553 if ((flags & SigmaValue) == 0)
1554 return(0.0);
1555 return(channel_info.sigma);
1556 }
cristy0568ffc2011-07-25 16:54:14 +00001557 case YellowPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001558 {
1559 if ((flags & XiValue) == 0)
1560 return(0.0);
1561 return(channel_info.xi);
1562 }
cristy0568ffc2011-07-25 16:54:14 +00001563 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001564 {
1565 if ((flags & PsiValue) == 0)
1566 return(0.0);
1567 return(channel_info.psi);
1568 }
cristy0568ffc2011-07-25 16:54:14 +00001569 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001570 {
1571 if ((flags & ChiValue) == 0)
1572 return(0.0);
1573 return(channel_info.chi);
1574 }
1575 default:
1576 return(0.0);
1577 }
1578 switch (channel)
1579 {
cristy0568ffc2011-07-25 16:54:14 +00001580 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001581 {
1582 if ((flags & RhoValue) == 0)
1583 return(0.0);
1584 return(channel_info.rho);
1585 }
cristy0568ffc2011-07-25 16:54:14 +00001586 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001587 {
1588 if ((flags & SigmaValue) == 0)
1589 return(0.0);
1590 return(channel_info.sigma);
1591 }
cristy0568ffc2011-07-25 16:54:14 +00001592 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001593 {
1594 if ((flags & XiValue) == 0)
1595 return(0.0);
1596 return(channel_info.xi);
1597 }
cristy0568ffc2011-07-25 16:54:14 +00001598 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001599 {
1600 if ((flags & ChiValue) == 0)
1601 return(0.0);
1602 return(channel_info.chi);
1603 }
cristy0568ffc2011-07-25 16:54:14 +00001604 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001605 {
1606 if ((flags & PsiValue) == 0)
1607 return(0.0);
1608 return(channel_info.psi);
1609 }
cristy3ed852e2009-09-05 21:47:34 +00001610 default:
1611 return(0.0);
1612 }
cristy3ed852e2009-09-05 21:47:34 +00001613 }
1614 if (LocaleCompare(symbol,"c") == 0)
1615 return(QuantumScale*pixel.red);
1616 break;
1617 }
1618 case 'D':
1619 case 'd':
1620 {
1621 if (LocaleNCompare(symbol,"depth",5) == 0)
1622 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1623 break;
1624 }
1625 case 'G':
1626 case 'g':
1627 {
1628 if (LocaleCompare(symbol,"g") == 0)
1629 return(QuantumScale*pixel.green);
1630 break;
1631 }
1632 case 'K':
1633 case 'k':
1634 {
1635 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1636 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1637 if (LocaleCompare(symbol,"k") == 0)
1638 {
1639 if (image->colorspace != CMYKColorspace)
1640 {
1641 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001642 OptionError,"ColorSeparatedImageRequired","`%s'",
cristy3ed852e2009-09-05 21:47:34 +00001643 image->filename);
1644 return(0.0);
1645 }
cristy4c08aed2011-07-01 19:47:50 +00001646 return(QuantumScale*pixel.black);
cristy3ed852e2009-09-05 21:47:34 +00001647 }
1648 break;
1649 }
1650 case 'H':
1651 case 'h':
1652 {
1653 if (LocaleCompare(symbol,"h") == 0)
cristya19f1d72012-08-07 18:24:38 +00001654 return((double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001655 if (LocaleCompare(symbol,"hue") == 0)
1656 {
1657 double
1658 hue,
1659 lightness,
1660 saturation;
1661
cristy0a39a5c2012-06-27 12:51:45 +00001662 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
cristyda1f9c12011-10-02 21:39:49 +00001663 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001664 return(hue);
1665 }
1666 break;
1667 }
1668 case 'I':
1669 case 'i':
1670 {
1671 if ((LocaleCompare(symbol,"image.depth") == 0) ||
1672 (LocaleCompare(symbol,"image.minima") == 0) ||
1673 (LocaleCompare(symbol,"image.maxima") == 0) ||
1674 (LocaleCompare(symbol,"image.mean") == 0) ||
1675 (LocaleCompare(symbol,"image.kurtosis") == 0) ||
1676 (LocaleCompare(symbol,"image.skewness") == 0) ||
1677 (LocaleCompare(symbol,"image.standard_deviation") == 0))
1678 return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
1679 if (LocaleCompare(symbol,"image.resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001680 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001681 if (LocaleCompare(symbol,"image.resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001682 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001683 if (LocaleCompare(symbol,"intensity") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001684 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001685 if (LocaleCompare(symbol,"i") == 0)
cristya19f1d72012-08-07 18:24:38 +00001686 return((double) x);
cristy3ed852e2009-09-05 21:47:34 +00001687 break;
1688 }
1689 case 'J':
1690 case 'j':
1691 {
1692 if (LocaleCompare(symbol,"j") == 0)
cristya19f1d72012-08-07 18:24:38 +00001693 return((double) y);
cristy3ed852e2009-09-05 21:47:34 +00001694 break;
1695 }
1696 case 'L':
1697 case 'l':
1698 {
1699 if (LocaleCompare(symbol,"lightness") == 0)
1700 {
1701 double
1702 hue,
1703 lightness,
1704 saturation;
1705
cristy0a39a5c2012-06-27 12:51:45 +00001706 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
cristyda1f9c12011-10-02 21:39:49 +00001707 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001708 return(lightness);
1709 }
cristyd3d2a272013-06-24 16:09:41 +00001710 if (LocaleCompare(symbol,"luma") == 0)
1711 {
1712 double
1713 luma;
1714
1715 luma=0.212656*pixel.red+0.715158*pixel.green+0.072186*pixel.blue;
1716 return(QuantumScale*luma);
1717 }
cristy3ed852e2009-09-05 21:47:34 +00001718 if (LocaleCompare(symbol,"luminance") == 0)
1719 {
1720 double
1721 luminence;
1722
cristyd3d2a272013-06-24 16:09:41 +00001723 luminence=0.212656*pixel.red+0.715158*pixel.green+0.072186*pixel.blue;
cristy3ed852e2009-09-05 21:47:34 +00001724 return(QuantumScale*luminence);
1725 }
1726 break;
1727 }
1728 case 'M':
1729 case 'm':
1730 {
1731 if (LocaleNCompare(symbol,"maxima",6) == 0)
1732 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1733 if (LocaleNCompare(symbol,"mean",4) == 0)
1734 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1735 if (LocaleNCompare(symbol,"minima",6) == 0)
1736 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1737 if (LocaleCompare(symbol,"m") == 0)
1738 return(QuantumScale*pixel.blue);
1739 break;
1740 }
1741 case 'N':
1742 case 'n':
1743 {
1744 if (LocaleCompare(symbol,"n") == 0)
cristya19f1d72012-08-07 18:24:38 +00001745 return((double) GetImageListLength(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001746 break;
1747 }
1748 case 'O':
1749 case 'o':
1750 {
1751 if (LocaleCompare(symbol,"o") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001752 return(QuantumScale*pixel.alpha);
cristy3ed852e2009-09-05 21:47:34 +00001753 break;
1754 }
1755 case 'P':
1756 case 'p':
1757 {
1758 if (LocaleCompare(symbol,"page.height") == 0)
cristya19f1d72012-08-07 18:24:38 +00001759 return((double) image->page.height);
cristy3ed852e2009-09-05 21:47:34 +00001760 if (LocaleCompare(symbol,"page.width") == 0)
cristya19f1d72012-08-07 18:24:38 +00001761 return((double) image->page.width);
cristy3ed852e2009-09-05 21:47:34 +00001762 if (LocaleCompare(symbol,"page.x") == 0)
cristya19f1d72012-08-07 18:24:38 +00001763 return((double) image->page.x);
cristy3ed852e2009-09-05 21:47:34 +00001764 if (LocaleCompare(symbol,"page.y") == 0)
cristya19f1d72012-08-07 18:24:38 +00001765 return((double) image->page.y);
cristy3ed852e2009-09-05 21:47:34 +00001766 break;
1767 }
1768 case 'R':
1769 case 'r':
1770 {
1771 if (LocaleCompare(symbol,"resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001772 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001773 if (LocaleCompare(symbol,"resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001774 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001775 if (LocaleCompare(symbol,"r") == 0)
1776 return(QuantumScale*pixel.red);
1777 break;
1778 }
1779 case 'S':
1780 case 's':
1781 {
1782 if (LocaleCompare(symbol,"saturation") == 0)
1783 {
1784 double
1785 hue,
1786 lightness,
1787 saturation;
1788
cristy0a39a5c2012-06-27 12:51:45 +00001789 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
cristyda1f9c12011-10-02 21:39:49 +00001790 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001791 return(saturation);
1792 }
1793 if (LocaleNCompare(symbol,"skewness",8) == 0)
1794 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1795 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1796 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1797 break;
1798 }
1799 case 'T':
1800 case 't':
1801 {
1802 if (LocaleCompare(symbol,"t") == 0)
cristya19f1d72012-08-07 18:24:38 +00001803 return((double) GetImageIndexInList(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001804 break;
1805 }
1806 case 'W':
1807 case 'w':
1808 {
1809 if (LocaleCompare(symbol,"w") == 0)
cristya19f1d72012-08-07 18:24:38 +00001810 return((double) image->columns);
cristy3ed852e2009-09-05 21:47:34 +00001811 break;
1812 }
1813 case 'Y':
1814 case 'y':
1815 {
1816 if (LocaleCompare(symbol,"y") == 0)
1817 return(QuantumScale*pixel.green);
1818 break;
1819 }
1820 case 'Z':
1821 case 'z':
1822 {
1823 if (LocaleCompare(symbol,"z") == 0)
1824 {
cristya19f1d72012-08-07 18:24:38 +00001825 double
cristy3ed852e2009-09-05 21:47:34 +00001826 depth;
1827
cristya19f1d72012-08-07 18:24:38 +00001828 depth=(double) GetImageDepth(image,fx_info->exception);
cristy3ed852e2009-09-05 21:47:34 +00001829 return(depth);
1830 }
1831 break;
1832 }
1833 default:
1834 break;
1835 }
1836 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1837 if (value != (const char *) NULL)
cristya19f1d72012-08-07 18:24:38 +00001838 return((double) StringToDouble(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001839 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
cristyefe601c2013-01-05 17:51:12 +00001840 "UnableToParseExpression","`%s'",symbol);
cristy3ed852e2009-09-05 21:47:34 +00001841 return(0.0);
1842}
1843
1844static const char *FxOperatorPrecedence(const char *expression,
1845 ExceptionInfo *exception)
1846{
1847 typedef enum
1848 {
1849 UndefinedPrecedence,
1850 NullPrecedence,
1851 BitwiseComplementPrecedence,
1852 ExponentPrecedence,
cristy116af162010-08-13 01:25:47 +00001853 ExponentialNotationPrecedence,
cristy3ed852e2009-09-05 21:47:34 +00001854 MultiplyPrecedence,
1855 AdditionPrecedence,
1856 ShiftPrecedence,
1857 RelationalPrecedence,
1858 EquivalencyPrecedence,
1859 BitwiseAndPrecedence,
1860 BitwiseOrPrecedence,
1861 LogicalAndPrecedence,
1862 LogicalOrPrecedence,
1863 TernaryPrecedence,
1864 AssignmentPrecedence,
1865 CommaPrecedence,
1866 SeparatorPrecedence
1867 } FxPrecedence;
1868
1869 FxPrecedence
1870 precedence,
1871 target;
1872
1873 register const char
1874 *subexpression;
1875
1876 register int
1877 c;
1878
cristybb503372010-05-27 20:51:26 +00001879 size_t
cristy3ed852e2009-09-05 21:47:34 +00001880 level;
1881
1882 c=0;
1883 level=0;
1884 subexpression=(const char *) NULL;
1885 target=NullPrecedence;
1886 while (*expression != '\0')
1887 {
1888 precedence=UndefinedPrecedence;
cristy248a3ec2013-08-17 16:25:26 +00001889 if ((isspace((int) ((unsigned char) *expression)) != 0) || (c == (int) '@'))
cristy3ed852e2009-09-05 21:47:34 +00001890 {
1891 expression++;
1892 continue;
1893 }
cristy488fa882010-03-01 22:34:24 +00001894 switch (*expression)
1895 {
1896 case 'A':
1897 case 'a':
cristy3ed852e2009-09-05 21:47:34 +00001898 {
cristyb33454f2011-08-03 02:10:45 +00001899#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00001900 if (LocaleNCompare(expression,"acosh",5) == 0)
1901 {
1902 expression+=5;
1903 break;
1904 }
cristyb33454f2011-08-03 02:10:45 +00001905#endif
1906#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00001907 if (LocaleNCompare(expression,"asinh",5) == 0)
1908 {
1909 expression+=5;
1910 break;
1911 }
cristyb33454f2011-08-03 02:10:45 +00001912#endif
1913#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00001914 if (LocaleNCompare(expression,"atanh",5) == 0)
cristy488fa882010-03-01 22:34:24 +00001915 {
1916 expression+=5;
1917 break;
1918 }
cristyb33454f2011-08-03 02:10:45 +00001919#endif
anthony62838e52012-05-24 12:41:54 +00001920 if (LocaleNCompare(expression,"atan2",5) == 0)
1921 {
1922 expression+=5;
1923 break;
1924 }
cristy488fa882010-03-01 22:34:24 +00001925 break;
cristy3ed852e2009-09-05 21:47:34 +00001926 }
cristy488fa882010-03-01 22:34:24 +00001927 case 'J':
1928 case 'j':
1929 {
1930 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1931 (LocaleNCompare(expression,"j1",2) == 0))
1932 {
1933 expression+=2;
1934 break;
1935 }
1936 break;
1937 }
cristy2def9322010-06-18 23:59:37 +00001938 case '#':
1939 {
1940 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1941 expression++;
1942 break;
1943 }
cristy488fa882010-03-01 22:34:24 +00001944 default:
1945 break;
1946 }
cristy3ed852e2009-09-05 21:47:34 +00001947 if ((c == (int) '{') || (c == (int) '['))
1948 level++;
1949 else
1950 if ((c == (int) '}') || (c == (int) ']'))
1951 level--;
1952 if (level == 0)
1953 switch ((unsigned char) *expression)
1954 {
1955 case '~':
1956 case '!':
1957 {
1958 precedence=BitwiseComplementPrecedence;
1959 break;
1960 }
1961 case '^':
cristy6621e252010-08-13 00:42:57 +00001962 case '@':
cristy3ed852e2009-09-05 21:47:34 +00001963 {
1964 precedence=ExponentPrecedence;
1965 break;
1966 }
1967 default:
1968 {
cristy37466eb2013-08-17 17:04:59 +00001969 if (((c != 0) && ((isdigit((int) ((unsigned char) c)) != 0) ||
1970 (strchr(")",(int) ((unsigned char) c)) != (char *) NULL))) &&
1971 (((islower((int) ((unsigned char) *expression)) != 0) ||
1972 (strchr("(",(int) ((unsigned char) *expression)) != (char *) NULL)) ||
1973 ((isdigit((int) ((unsigned char) c)) == 0) &&
1974 (isdigit((int) ((unsigned char) *expression)) != 0))) &&
1975 (strchr("xy",(int) ((unsigned char) *expression)) == (char *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001976 precedence=MultiplyPrecedence;
1977 break;
1978 }
1979 case '*':
1980 case '/':
1981 case '%':
1982 {
1983 precedence=MultiplyPrecedence;
1984 break;
1985 }
1986 case '+':
1987 case '-':
1988 {
1989 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
1990 (isalpha(c) != 0))
1991 precedence=AdditionPrecedence;
1992 break;
1993 }
1994 case LeftShiftOperator:
1995 case RightShiftOperator:
1996 {
1997 precedence=ShiftPrecedence;
1998 break;
1999 }
2000 case '<':
2001 case LessThanEqualOperator:
2002 case GreaterThanEqualOperator:
2003 case '>':
2004 {
2005 precedence=RelationalPrecedence;
2006 break;
2007 }
2008 case EqualOperator:
2009 case NotEqualOperator:
2010 {
2011 precedence=EquivalencyPrecedence;
2012 break;
2013 }
2014 case '&':
2015 {
2016 precedence=BitwiseAndPrecedence;
2017 break;
2018 }
2019 case '|':
2020 {
2021 precedence=BitwiseOrPrecedence;
2022 break;
2023 }
2024 case LogicalAndOperator:
2025 {
2026 precedence=LogicalAndPrecedence;
2027 break;
2028 }
2029 case LogicalOrOperator:
2030 {
2031 precedence=LogicalOrPrecedence;
2032 break;
2033 }
cristy116af162010-08-13 01:25:47 +00002034 case ExponentialNotation:
2035 {
2036 precedence=ExponentialNotationPrecedence;
2037 break;
2038 }
cristy3ed852e2009-09-05 21:47:34 +00002039 case ':':
2040 case '?':
2041 {
2042 precedence=TernaryPrecedence;
2043 break;
2044 }
2045 case '=':
2046 {
2047 precedence=AssignmentPrecedence;
2048 break;
2049 }
2050 case ',':
2051 {
2052 precedence=CommaPrecedence;
2053 break;
2054 }
2055 case ';':
2056 {
2057 precedence=SeparatorPrecedence;
2058 break;
2059 }
2060 }
2061 if ((precedence == BitwiseComplementPrecedence) ||
2062 (precedence == TernaryPrecedence) ||
2063 (precedence == AssignmentPrecedence))
2064 {
2065 if (precedence > target)
2066 {
2067 /*
2068 Right-to-left associativity.
2069 */
2070 target=precedence;
2071 subexpression=expression;
2072 }
2073 }
2074 else
2075 if (precedence >= target)
2076 {
2077 /*
2078 Left-to-right associativity.
2079 */
2080 target=precedence;
2081 subexpression=expression;
2082 }
2083 if (strchr("(",(int) *expression) != (char *) NULL)
2084 expression=FxSubexpression(expression,exception);
2085 c=(int) (*expression++);
2086 }
2087 return(subexpression);
2088}
2089
cristya19f1d72012-08-07 18:24:38 +00002090static double FxEvaluateSubexpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002091 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristya19f1d72012-08-07 18:24:38 +00002092 const char *expression,double *beta,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002093{
2094 char
2095 *q,
2096 subexpression[MaxTextExtent];
2097
cristya19f1d72012-08-07 18:24:38 +00002098 double
cristy3ed852e2009-09-05 21:47:34 +00002099 alpha,
2100 gamma;
2101
2102 register const char
2103 *p;
2104
2105 *beta=0.0;
2106 if (exception->severity != UndefinedException)
2107 return(0.0);
cristy248a3ec2013-08-17 16:25:26 +00002108 while (isspace((int) ((unsigned char) *expression)) != 0)
cristy3ed852e2009-09-05 21:47:34 +00002109 expression++;
2110 if (*expression == '\0')
2111 {
2112 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
cristyefe601c2013-01-05 17:51:12 +00002113 "MissingExpression","`%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002114 return(0.0);
2115 }
cristy66322f02010-05-17 11:40:48 +00002116 *subexpression='\0';
cristy3ed852e2009-09-05 21:47:34 +00002117 p=FxOperatorPrecedence(expression,exception);
2118 if (p != (const char *) NULL)
2119 {
2120 (void) CopyMagickString(subexpression,expression,(size_t)
2121 (p-expression+1));
2122 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2123 exception);
2124 switch ((unsigned char) *p)
2125 {
2126 case '~':
2127 {
2128 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002129 *beta=(double) (~(size_t) *beta);
cristy3ed852e2009-09-05 21:47:34 +00002130 return(*beta);
2131 }
2132 case '!':
2133 {
2134 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2135 return(*beta == 0.0 ? 1.0 : 0.0);
2136 }
2137 case '^':
2138 {
2139 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2140 channel,x,y,++p,beta,exception));
2141 return(*beta);
2142 }
2143 case '*':
cristy116af162010-08-13 01:25:47 +00002144 case ExponentialNotation:
cristy3ed852e2009-09-05 21:47:34 +00002145 {
2146 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2147 return(alpha*(*beta));
2148 }
2149 case '/':
2150 {
2151 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2152 if (*beta == 0.0)
2153 {
2154 if (exception->severity == UndefinedException)
2155 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00002156 OptionError,"DivideByZero","`%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002157 return(0.0);
2158 }
2159 return(alpha/(*beta));
2160 }
2161 case '%':
2162 {
2163 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2164 *beta=fabs(floor(((double) *beta)+0.5));
2165 if (*beta == 0.0)
2166 {
2167 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00002168 OptionError,"DivideByZero","`%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002169 return(0.0);
2170 }
2171 return(fmod((double) alpha,(double) *beta));
2172 }
2173 case '+':
2174 {
2175 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2176 return(alpha+(*beta));
2177 }
2178 case '-':
2179 {
2180 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2181 return(alpha-(*beta));
2182 }
2183 case LeftShiftOperator:
2184 {
2185 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002186 *beta=(double) ((size_t) (alpha+0.5) << (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002187 return(*beta);
2188 }
2189 case RightShiftOperator:
2190 {
2191 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002192 *beta=(double) ((size_t) (alpha+0.5) >> (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002193 return(*beta);
2194 }
2195 case '<':
2196 {
2197 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2198 return(alpha < *beta ? 1.0 : 0.0);
2199 }
2200 case LessThanEqualOperator:
2201 {
2202 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2203 return(alpha <= *beta ? 1.0 : 0.0);
2204 }
2205 case '>':
2206 {
2207 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2208 return(alpha > *beta ? 1.0 : 0.0);
2209 }
2210 case GreaterThanEqualOperator:
2211 {
2212 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2213 return(alpha >= *beta ? 1.0 : 0.0);
2214 }
2215 case EqualOperator:
2216 {
2217 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy9b528342012-06-02 00:59:20 +00002218 return(fabs(alpha-(*beta)) < MagickEpsilon ? MagickEpsilon : 0.0);
cristy3ed852e2009-09-05 21:47:34 +00002219 }
2220 case NotEqualOperator:
2221 {
2222 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy972050b2012-06-04 22:09:17 +00002223 return(fabs(alpha-(*beta)) >= MagickEpsilon ? 1.0 : 0.0);
cristy3ed852e2009-09-05 21:47:34 +00002224 }
2225 case '&':
2226 {
2227 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002228 *beta=(double) ((size_t) (alpha+0.5) & (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002229 return(*beta);
2230 }
2231 case '|':
2232 {
2233 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002234 *beta=(double) ((size_t) (alpha+0.5) | (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002235 return(*beta);
2236 }
2237 case LogicalAndOperator:
2238 {
2239 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2240 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2241 return(*beta);
2242 }
2243 case LogicalOrOperator:
2244 {
2245 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2246 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2247 return(*beta);
2248 }
2249 case '?':
2250 {
cristya19f1d72012-08-07 18:24:38 +00002251 double
cristy3ed852e2009-09-05 21:47:34 +00002252 gamma;
2253
2254 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2255 q=subexpression;
2256 p=StringToken(":",&q);
2257 if (q == (char *) NULL)
2258 {
2259 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00002260 OptionError,"UnableToParseExpression","`%s'",subexpression);
cristy3ed852e2009-09-05 21:47:34 +00002261 return(0.0);
2262 }
cristy972050b2012-06-04 22:09:17 +00002263 if (fabs((double) alpha) >= MagickEpsilon)
cristy3ed852e2009-09-05 21:47:34 +00002264 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2265 else
2266 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2267 return(gamma);
2268 }
2269 case '=':
2270 {
2271 char
2272 numeric[MaxTextExtent];
2273
2274 q=subexpression;
2275 while (isalpha((int) ((unsigned char) *q)) != 0)
2276 q++;
2277 if (*q != '\0')
2278 {
2279 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00002280 OptionError,"UnableToParseExpression","`%s'",subexpression);
cristy3ed852e2009-09-05 21:47:34 +00002281 return(0.0);
2282 }
2283 ClearMagickException(exception);
2284 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristyb51dff52011-05-19 16:55:47 +00002285 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
cristy8cd5b312010-01-07 01:10:24 +00002286 *beta);
cristy3ed852e2009-09-05 21:47:34 +00002287 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2288 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2289 subexpression),ConstantString(numeric));
2290 return(*beta);
2291 }
2292 case ',':
2293 {
2294 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2295 return(alpha);
2296 }
2297 case ';':
2298 {
2299 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2300 return(*beta);
2301 }
2302 default:
2303 {
2304 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2305 exception);
2306 return(gamma);
2307 }
2308 }
2309 }
2310 if (strchr("(",(int) *expression) != (char *) NULL)
2311 {
2312 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2313 subexpression[strlen(subexpression)-1]='\0';
2314 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2315 exception);
2316 return(gamma);
2317 }
cristy8b8a3ae2010-10-23 18:49:46 +00002318 switch (*expression)
cristy3ed852e2009-09-05 21:47:34 +00002319 {
2320 case '+':
2321 {
2322 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2323 exception);
2324 return(1.0*gamma);
2325 }
2326 case '-':
2327 {
2328 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2329 exception);
2330 return(-1.0*gamma);
2331 }
2332 case '~':
2333 {
2334 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2335 exception);
cristya19f1d72012-08-07 18:24:38 +00002336 return((double) (~(size_t) (gamma+0.5)));
cristy3ed852e2009-09-05 21:47:34 +00002337 }
2338 case 'A':
2339 case 'a':
2340 {
2341 if (LocaleNCompare(expression,"abs",3) == 0)
2342 {
2343 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2344 exception);
cristya19f1d72012-08-07 18:24:38 +00002345 return((double) fabs((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002346 }
cristyb33454f2011-08-03 02:10:45 +00002347#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00002348 if (LocaleNCompare(expression,"acosh",5) == 0)
2349 {
2350 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2351 exception);
cristya19f1d72012-08-07 18:24:38 +00002352 return((double) acosh((double) alpha));
cristy363772a2011-07-28 23:25:33 +00002353 }
cristyb33454f2011-08-03 02:10:45 +00002354#endif
cristy3ed852e2009-09-05 21:47:34 +00002355 if (LocaleNCompare(expression,"acos",4) == 0)
2356 {
2357 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2358 exception);
cristya19f1d72012-08-07 18:24:38 +00002359 return((double) acos((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002360 }
cristy43c22f42010-03-30 12:34:07 +00002361#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002362 if (LocaleNCompare(expression,"airy",4) == 0)
2363 {
2364 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2365 exception);
2366 if (alpha == 0.0)
cristy2dd03222010-03-30 22:12:11 +00002367 return(1.0);
2368 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
cristy43c22f42010-03-30 12:34:07 +00002369 return(gamma*gamma);
cristyee56cf12010-03-01 22:17:06 +00002370 }
cristy43c22f42010-03-30 12:34:07 +00002371#endif
cristyb33454f2011-08-03 02:10:45 +00002372#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00002373 if (LocaleNCompare(expression,"asinh",5) == 0)
2374 {
2375 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2376 exception);
cristya19f1d72012-08-07 18:24:38 +00002377 return((double) asinh((double) alpha));
cristy363772a2011-07-28 23:25:33 +00002378 }
cristyb33454f2011-08-03 02:10:45 +00002379#endif
cristy3ed852e2009-09-05 21:47:34 +00002380 if (LocaleNCompare(expression,"asin",4) == 0)
2381 {
2382 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2383 exception);
cristya19f1d72012-08-07 18:24:38 +00002384 return((double) asin((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002385 }
2386 if (LocaleNCompare(expression,"alt",3) == 0)
2387 {
2388 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2389 exception);
cristybb503372010-05-27 20:51:26 +00002390 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
cristy3ed852e2009-09-05 21:47:34 +00002391 }
2392 if (LocaleNCompare(expression,"atan2",5) == 0)
2393 {
2394 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2395 exception);
cristya19f1d72012-08-07 18:24:38 +00002396 return((double) atan2((double) alpha,(double) *beta));
cristy3ed852e2009-09-05 21:47:34 +00002397 }
cristyb33454f2011-08-03 02:10:45 +00002398#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00002399 if (LocaleNCompare(expression,"atanh",5) == 0)
2400 {
2401 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2402 exception);
cristya19f1d72012-08-07 18:24:38 +00002403 return((double) atanh((double) alpha));
cristy363772a2011-07-28 23:25:33 +00002404 }
cristyb33454f2011-08-03 02:10:45 +00002405#endif
cristy3ed852e2009-09-05 21:47:34 +00002406 if (LocaleNCompare(expression,"atan",4) == 0)
2407 {
2408 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2409 exception);
cristya19f1d72012-08-07 18:24:38 +00002410 return((double) atan((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002411 }
2412 if (LocaleCompare(expression,"a") == 0)
2413 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2414 break;
2415 }
2416 case 'B':
2417 case 'b':
2418 {
2419 if (LocaleCompare(expression,"b") == 0)
2420 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2421 break;
2422 }
2423 case 'C':
2424 case 'c':
2425 {
2426 if (LocaleNCompare(expression,"ceil",4) == 0)
2427 {
2428 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2429 exception);
cristya19f1d72012-08-07 18:24:38 +00002430 return((double) ceil((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002431 }
2432 if (LocaleNCompare(expression,"cosh",4) == 0)
2433 {
2434 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2435 exception);
cristya19f1d72012-08-07 18:24:38 +00002436 return((double) cosh((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002437 }
2438 if (LocaleNCompare(expression,"cos",3) == 0)
2439 {
2440 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2441 exception);
cristya19f1d72012-08-07 18:24:38 +00002442 return((double) cos((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002443 }
2444 if (LocaleCompare(expression,"c") == 0)
2445 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2446 break;
2447 }
2448 case 'D':
2449 case 'd':
2450 {
2451 if (LocaleNCompare(expression,"debug",5) == 0)
2452 {
2453 const char
2454 *type;
2455
2456 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2457 exception);
2458 if (fx_info->images->colorspace == CMYKColorspace)
2459 switch (channel)
2460 {
cristy0568ffc2011-07-25 16:54:14 +00002461 case CyanPixelChannel: type="cyan"; break;
2462 case MagentaPixelChannel: type="magenta"; break;
2463 case YellowPixelChannel: type="yellow"; break;
2464 case AlphaPixelChannel: type="opacity"; break;
2465 case BlackPixelChannel: type="black"; break;
cristy3ed852e2009-09-05 21:47:34 +00002466 default: type="unknown"; break;
2467 }
2468 else
2469 switch (channel)
2470 {
cristy0568ffc2011-07-25 16:54:14 +00002471 case RedPixelChannel: type="red"; break;
2472 case GreenPixelChannel: type="green"; break;
2473 case BluePixelChannel: type="blue"; break;
2474 case AlphaPixelChannel: type="opacity"; break;
cristy3ed852e2009-09-05 21:47:34 +00002475 default: type="unknown"; break;
2476 }
2477 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2478 if (strlen(subexpression) > 1)
2479 subexpression[strlen(subexpression)-1]='\0';
2480 if (fx_info->file != (FILE *) NULL)
cristy1707c6c2012-01-18 23:30:54 +00002481 (void) FormatLocaleFile(fx_info->file,"%s[%.20g,%.20g].%s: "
2482 "%s=%.*g\n",fx_info->images->filename,(double) x,(double) y,type,
2483 subexpression,GetMagickPrecision(),(double) alpha);
cristy3ed852e2009-09-05 21:47:34 +00002484 return(0.0);
2485 }
cristy5597a8d2011-11-04 00:25:32 +00002486 if (LocaleNCompare(expression,"drc",3) == 0)
2487 {
2488 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2489 exception);
cristya19f1d72012-08-07 18:24:38 +00002490 return((double) (alpha/(*beta*(alpha-1.0)+1.0)));
cristy5597a8d2011-11-04 00:25:32 +00002491 }
cristy3ed852e2009-09-05 21:47:34 +00002492 break;
2493 }
2494 case 'E':
2495 case 'e':
2496 {
2497 if (LocaleCompare(expression,"epsilon") == 0)
cristya19f1d72012-08-07 18:24:38 +00002498 return((double) MagickEpsilon);
cristy3ed852e2009-09-05 21:47:34 +00002499 if (LocaleNCompare(expression,"exp",3) == 0)
2500 {
2501 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2502 exception);
cristya19f1d72012-08-07 18:24:38 +00002503 return((double) exp((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002504 }
2505 if (LocaleCompare(expression,"e") == 0)
cristya19f1d72012-08-07 18:24:38 +00002506 return((double) 2.7182818284590452354);
cristy3ed852e2009-09-05 21:47:34 +00002507 break;
2508 }
2509 case 'F':
2510 case 'f':
2511 {
2512 if (LocaleNCompare(expression,"floor",5) == 0)
2513 {
2514 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2515 exception);
cristya19f1d72012-08-07 18:24:38 +00002516 return((double) floor((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002517 }
2518 break;
2519 }
2520 case 'G':
2521 case 'g':
2522 {
cristy9eeedea2011-11-02 19:04:05 +00002523 if (LocaleNCompare(expression,"gauss",5) == 0)
2524 {
2525 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2526 exception);
2527 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
cristya19f1d72012-08-07 18:24:38 +00002528 return((double) gamma);
cristy9eeedea2011-11-02 19:04:05 +00002529 }
cristyb0aad4c2011-11-02 19:30:35 +00002530 if (LocaleNCompare(expression,"gcd",3) == 0)
2531 {
2532 MagickOffsetType
2533 gcd;
2534
2535 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2536 exception);
cristy1707c6c2012-01-18 23:30:54 +00002537 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType) (*beta+
2538 0.5));
cristya19f1d72012-08-07 18:24:38 +00002539 return((double) gcd);
cristyb0aad4c2011-11-02 19:30:35 +00002540 }
cristy3ed852e2009-09-05 21:47:34 +00002541 if (LocaleCompare(expression,"g") == 0)
2542 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2543 break;
2544 }
2545 case 'H':
2546 case 'h':
2547 {
2548 if (LocaleCompare(expression,"h") == 0)
2549 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2550 if (LocaleCompare(expression,"hue") == 0)
2551 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2552 if (LocaleNCompare(expression,"hypot",5) == 0)
2553 {
2554 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2555 exception);
cristya19f1d72012-08-07 18:24:38 +00002556 return((double) hypot((double) alpha,(double) *beta));
cristy3ed852e2009-09-05 21:47:34 +00002557 }
2558 break;
2559 }
2560 case 'K':
2561 case 'k':
2562 {
2563 if (LocaleCompare(expression,"k") == 0)
2564 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2565 break;
2566 }
2567 case 'I':
2568 case 'i':
2569 {
2570 if (LocaleCompare(expression,"intensity") == 0)
2571 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2572 if (LocaleNCompare(expression,"int",3) == 0)
2573 {
2574 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2575 exception);
cristya19f1d72012-08-07 18:24:38 +00002576 return((double) floor(alpha));
cristy3ed852e2009-09-05 21:47:34 +00002577 }
cristy82b20722011-11-05 21:52:36 +00002578#if defined(MAGICKCORE_HAVE_ISNAN)
cristy639399c2011-11-02 19:16:15 +00002579 if (LocaleNCompare(expression,"isnan",5) == 0)
2580 {
2581 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2582 exception);
cristya19f1d72012-08-07 18:24:38 +00002583 return((double) !!isnan((double) alpha));
cristy639399c2011-11-02 19:16:15 +00002584 }
cristy82b20722011-11-05 21:52:36 +00002585#endif
cristy3ed852e2009-09-05 21:47:34 +00002586 if (LocaleCompare(expression,"i") == 0)
2587 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2588 break;
2589 }
2590 case 'J':
2591 case 'j':
2592 {
2593 if (LocaleCompare(expression,"j") == 0)
2594 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
cristy161b9262010-03-20 19:34:32 +00002595#if defined(MAGICKCORE_HAVE_J0)
cristyee56cf12010-03-01 22:17:06 +00002596 if (LocaleNCompare(expression,"j0",2) == 0)
2597 {
2598 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2599 exception);
cristya19f1d72012-08-07 18:24:38 +00002600 return((double) j0((double) alpha));
cristyee56cf12010-03-01 22:17:06 +00002601 }
cristy161b9262010-03-20 19:34:32 +00002602#endif
2603#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002604 if (LocaleNCompare(expression,"j1",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) j1((double) alpha));
cristyee56cf12010-03-01 22:17:06 +00002609 }
cristy161b9262010-03-20 19:34:32 +00002610#endif
cristyaa018fa2010-04-08 23:03:54 +00002611#if defined(MAGICKCORE_HAVE_J1)
cristya6a09e72010-03-02 14:51:02 +00002612 if (LocaleNCompare(expression,"jinc",4) == 0)
2613 {
2614 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2615 exception);
cristy0946a822010-03-12 17:14:58 +00002616 if (alpha == 0.0)
2617 return(1.0);
cristyc90d70d2012-11-03 23:53:13 +00002618 gamma=(double) (2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha));
cristyfce2f7b2010-03-12 00:29:49 +00002619 return(gamma);
cristya6a09e72010-03-02 14:51:02 +00002620 }
cristyaa018fa2010-04-08 23:03:54 +00002621#endif
cristy3ed852e2009-09-05 21:47:34 +00002622 break;
2623 }
2624 case 'L':
2625 case 'l':
2626 {
2627 if (LocaleNCompare(expression,"ln",2) == 0)
2628 {
2629 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2630 exception);
cristya19f1d72012-08-07 18:24:38 +00002631 return((double) log((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002632 }
cristyc8ed5322010-08-31 12:07:59 +00002633 if (LocaleNCompare(expression,"logtwo",6) == 0)
cristy3ed852e2009-09-05 21:47:34 +00002634 {
cristyc8ed5322010-08-31 12:07:59 +00002635 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
cristy3ed852e2009-09-05 21:47:34 +00002636 exception);
cristya19f1d72012-08-07 18:24:38 +00002637 return((double) log10((double) alpha))/log10(2.0);
cristy3ed852e2009-09-05 21:47:34 +00002638 }
2639 if (LocaleNCompare(expression,"log",3) == 0)
2640 {
2641 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2642 exception);
cristya19f1d72012-08-07 18:24:38 +00002643 return((double) log10((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002644 }
2645 if (LocaleCompare(expression,"lightness") == 0)
2646 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2647 break;
2648 }
2649 case 'M':
2650 case 'm':
2651 {
2652 if (LocaleCompare(expression,"MaxRGB") == 0)
cristya19f1d72012-08-07 18:24:38 +00002653 return((double) QuantumRange);
cristy3ed852e2009-09-05 21:47:34 +00002654 if (LocaleNCompare(expression,"maxima",6) == 0)
2655 break;
2656 if (LocaleNCompare(expression,"max",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002657 {
2658 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2659 exception);
2660 return(alpha > *beta ? alpha : *beta);
2661 }
cristy3ed852e2009-09-05 21:47:34 +00002662 if (LocaleNCompare(expression,"minima",6) == 0)
2663 break;
2664 if (LocaleNCompare(expression,"min",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,"mod",3) == 0)
2671 {
2672 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2673 exception);
cristy984049c2011-11-03 18:34:58 +00002674 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2675 return(gamma);
cristy3ed852e2009-09-05 21:47:34 +00002676 }
2677 if (LocaleCompare(expression,"m") == 0)
2678 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2679 break;
2680 }
2681 case 'N':
2682 case 'n':
2683 {
cristyad3502e2011-11-02 19:10:45 +00002684 if (LocaleNCompare(expression,"not",3) == 0)
2685 {
2686 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2687 exception);
cristya19f1d72012-08-07 18:24:38 +00002688 return((double) (alpha < MagickEpsilon));
cristyad3502e2011-11-02 19:10:45 +00002689 }
cristy3ed852e2009-09-05 21:47:34 +00002690 if (LocaleCompare(expression,"n") == 0)
2691 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2692 break;
2693 }
2694 case 'O':
2695 case 'o':
2696 {
2697 if (LocaleCompare(expression,"Opaque") == 0)
2698 return(1.0);
2699 if (LocaleCompare(expression,"o") == 0)
2700 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2701 break;
2702 }
2703 case 'P':
2704 case 'p':
2705 {
cristy670aa3c2011-11-03 00:54:00 +00002706 if (LocaleCompare(expression,"phi") == 0)
cristya19f1d72012-08-07 18:24:38 +00002707 return((double) MagickPHI);
cristy3ed852e2009-09-05 21:47:34 +00002708 if (LocaleCompare(expression,"pi") == 0)
cristya19f1d72012-08-07 18:24:38 +00002709 return((double) MagickPI);
cristy3ed852e2009-09-05 21:47:34 +00002710 if (LocaleNCompare(expression,"pow",3) == 0)
2711 {
2712 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2713 exception);
cristya19f1d72012-08-07 18:24:38 +00002714 return((double) pow((double) alpha,(double) *beta));
cristy3ed852e2009-09-05 21:47:34 +00002715 }
2716 if (LocaleCompare(expression,"p") == 0)
2717 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2718 break;
2719 }
2720 case 'Q':
2721 case 'q':
2722 {
2723 if (LocaleCompare(expression,"QuantumRange") == 0)
cristya19f1d72012-08-07 18:24:38 +00002724 return((double) QuantumRange);
cristy3ed852e2009-09-05 21:47:34 +00002725 if (LocaleCompare(expression,"QuantumScale") == 0)
cristya19f1d72012-08-07 18:24:38 +00002726 return((double) QuantumScale);
cristy3ed852e2009-09-05 21:47:34 +00002727 break;
2728 }
2729 case 'R':
2730 case 'r':
2731 {
2732 if (LocaleNCompare(expression,"rand",4) == 0)
cristya19f1d72012-08-07 18:24:38 +00002733 return((double) GetPseudoRandomValue(fx_info->random_info));
cristy3ed852e2009-09-05 21:47:34 +00002734 if (LocaleNCompare(expression,"round",5) == 0)
2735 {
2736 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2737 exception);
cristya19f1d72012-08-07 18:24:38 +00002738 return((double) floor((double) alpha+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002739 }
2740 if (LocaleCompare(expression,"r") == 0)
2741 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2742 break;
2743 }
2744 case 'S':
2745 case 's':
2746 {
2747 if (LocaleCompare(expression,"saturation") == 0)
2748 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2749 if (LocaleNCompare(expression,"sign",4) == 0)
2750 {
2751 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2752 exception);
2753 return(alpha < 0.0 ? -1.0 : 1.0);
2754 }
cristya6a09e72010-03-02 14:51:02 +00002755 if (LocaleNCompare(expression,"sinc",4) == 0)
2756 {
2757 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2758 exception);
2759 if (alpha == 0)
2760 return(1.0);
cristya19f1d72012-08-07 18:24:38 +00002761 gamma=(double) (sin((double) (MagickPI*alpha))/
cristya6a09e72010-03-02 14:51:02 +00002762 (MagickPI*alpha));
2763 return(gamma);
2764 }
cristy3ed852e2009-09-05 21:47:34 +00002765 if (LocaleNCompare(expression,"sinh",4) == 0)
2766 {
2767 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2768 exception);
cristya19f1d72012-08-07 18:24:38 +00002769 return((double) sinh((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002770 }
2771 if (LocaleNCompare(expression,"sin",3) == 0)
2772 {
2773 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2774 exception);
cristya19f1d72012-08-07 18:24:38 +00002775 return((double) sin((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002776 }
2777 if (LocaleNCompare(expression,"sqrt",4) == 0)
2778 {
2779 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2780 exception);
cristya19f1d72012-08-07 18:24:38 +00002781 return((double) sqrt((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002782 }
cristy9eeedea2011-11-02 19:04:05 +00002783 if (LocaleNCompare(expression,"squish",6) == 0)
2784 {
2785 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2786 exception);
cristya19f1d72012-08-07 18:24:38 +00002787 return((double) (1.0/(1.0+exp((double) (-alpha)))));
cristy9eeedea2011-11-02 19:04:05 +00002788 }
cristy3ed852e2009-09-05 21:47:34 +00002789 if (LocaleCompare(expression,"s") == 0)
2790 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2791 break;
2792 }
2793 case 'T':
2794 case 't':
2795 {
2796 if (LocaleNCompare(expression,"tanh",4) == 0)
2797 {
2798 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2799 exception);
cristya19f1d72012-08-07 18:24:38 +00002800 return((double) tanh((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002801 }
2802 if (LocaleNCompare(expression,"tan",3) == 0)
2803 {
2804 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2805 exception);
cristya19f1d72012-08-07 18:24:38 +00002806 return((double) tan((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002807 }
2808 if (LocaleCompare(expression,"Transparent") == 0)
2809 return(0.0);
cristy16788e42010-08-13 13:44:26 +00002810 if (LocaleNCompare(expression,"trunc",5) == 0)
2811 {
2812 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2813 exception);
2814 if (alpha >= 0.0)
cristya19f1d72012-08-07 18:24:38 +00002815 return((double) floor((double) alpha));
2816 return((double) ceil((double) alpha));
cristy16788e42010-08-13 13:44:26 +00002817 }
cristy3ed852e2009-09-05 21:47:34 +00002818 if (LocaleCompare(expression,"t") == 0)
2819 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2820 break;
2821 }
2822 case 'U':
2823 case 'u':
2824 {
2825 if (LocaleCompare(expression,"u") == 0)
2826 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2827 break;
2828 }
2829 case 'V':
2830 case 'v':
2831 {
2832 if (LocaleCompare(expression,"v") == 0)
2833 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2834 break;
2835 }
2836 case 'W':
2837 case 'w':
2838 {
cristy9eeedea2011-11-02 19:04:05 +00002839 if (LocaleNCompare(expression,"while",5) == 0)
2840 {
2841 do
2842 {
2843 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2844 exception);
2845 } while (fabs((double) alpha) >= MagickEpsilon);
cristya19f1d72012-08-07 18:24:38 +00002846 return((double) *beta);
cristy9eeedea2011-11-02 19:04:05 +00002847 }
cristy3ed852e2009-09-05 21:47:34 +00002848 if (LocaleCompare(expression,"w") == 0)
2849 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2850 break;
2851 }
2852 case 'Y':
2853 case 'y':
2854 {
2855 if (LocaleCompare(expression,"y") == 0)
2856 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2857 break;
2858 }
2859 case 'Z':
2860 case 'z':
2861 {
2862 if (LocaleCompare(expression,"z") == 0)
2863 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2864 break;
2865 }
2866 default:
2867 break;
2868 }
2869 q=(char *) expression;
cristydbdd0e32011-11-04 23:29:40 +00002870 alpha=InterpretSiPrefixValue(expression,&q);
cristy3ed852e2009-09-05 21:47:34 +00002871 if (q == expression)
2872 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2873 return(alpha);
2874}
2875
cristy7832dc22011-09-05 01:21:53 +00002876MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
cristya19f1d72012-08-07 18:24:38 +00002877 double *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002878{
2879 MagickBooleanType
2880 status;
2881
cristy541ae572011-08-05 19:08:59 +00002882 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2883 exception);
cristy3ed852e2009-09-05 21:47:34 +00002884 return(status);
2885}
2886
2887MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
cristya19f1d72012-08-07 18:24:38 +00002888 double *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002889{
2890 FILE
2891 *file;
2892
2893 MagickBooleanType
2894 status;
2895
2896 file=fx_info->file;
2897 fx_info->file=(FILE *) NULL;
cristy541ae572011-08-05 19:08:59 +00002898 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2899 exception);
cristy3ed852e2009-09-05 21:47:34 +00002900 fx_info->file=file;
2901 return(status);
2902}
2903
cristy7832dc22011-09-05 01:21:53 +00002904MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002905 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristya19f1d72012-08-07 18:24:38 +00002906 double *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002907{
cristya19f1d72012-08-07 18:24:38 +00002908 double
cristy3ed852e2009-09-05 21:47:34 +00002909 beta;
2910
2911 beta=0.0;
2912 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2913 exception);
2914 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2915}
2916
2917/*
2918%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2919% %
2920% %
2921% %
2922% F x I m a g e %
2923% %
2924% %
2925% %
2926%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2927%
2928% FxImage() applies a mathematical expression to the specified image.
2929%
2930% The format of the FxImage method is:
2931%
2932% Image *FxImage(const Image *image,const char *expression,
2933% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002934%
2935% A description of each parameter follows:
2936%
2937% o image: the image.
2938%
cristy3ed852e2009-09-05 21:47:34 +00002939% o expression: A mathematical expression.
2940%
2941% o exception: return any errors or warnings in this structure.
2942%
2943*/
2944
2945static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2946{
cristybb503372010-05-27 20:51:26 +00002947 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002948 i;
2949
2950 assert(fx_info != (FxInfo **) NULL);
cristyac245f82012-05-05 17:13:57 +00002951 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
cristy3ed852e2009-09-05 21:47:34 +00002952 if (fx_info[i] != (FxInfo *) NULL)
2953 fx_info[i]=DestroyFxInfo(fx_info[i]);
cristyb41ee102010-10-04 16:46:15 +00002954 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
cristy3ed852e2009-09-05 21:47:34 +00002955 return(fx_info);
2956}
2957
2958static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2959 ExceptionInfo *exception)
2960{
2961 char
2962 *fx_expression;
2963
2964 FxInfo
2965 **fx_info;
2966
cristya19f1d72012-08-07 18:24:38 +00002967 double
cristy3ed852e2009-09-05 21:47:34 +00002968 alpha;
2969
cristybb503372010-05-27 20:51:26 +00002970 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002971 i;
2972
cristybb503372010-05-27 20:51:26 +00002973 size_t
cristy3ed852e2009-09-05 21:47:34 +00002974 number_threads;
2975
cristy9357bdd2012-07-30 12:28:34 +00002976 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
cristyb41ee102010-10-04 16:46:15 +00002977 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +00002978 if (fx_info == (FxInfo **) NULL)
cristy65d161b2012-10-07 20:39:52 +00002979 {
2980 (void) ThrowMagickException(exception,GetMagickModule(),
2981 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
2982 return((FxInfo **) NULL);
2983 }
cristy3ed852e2009-09-05 21:47:34 +00002984 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2985 if (*expression != '@')
2986 fx_expression=ConstantString(expression);
2987 else
cristy3a5987c2013-11-07 14:18:46 +00002988 fx_expression=FileToString(expression+1,~0UL,exception);
cristybb503372010-05-27 20:51:26 +00002989 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +00002990 {
cristy65d161b2012-10-07 20:39:52 +00002991 MagickBooleanType
2992 status;
2993
cristydb070952012-04-20 14:33:00 +00002994 fx_info[i]=AcquireFxInfo(image,fx_expression,exception);
cristy3ed852e2009-09-05 21:47:34 +00002995 if (fx_info[i] == (FxInfo *) NULL)
cristy65d161b2012-10-07 20:39:52 +00002996 break;
2997 status=FxPreprocessExpression(fx_info[i],&alpha,exception);
2998 if (status == MagickFalse)
2999 break;
cristy3ed852e2009-09-05 21:47:34 +00003000 }
3001 fx_expression=DestroyString(fx_expression);
cristy65d161b2012-10-07 20:39:52 +00003002 if (i < (ssize_t) number_threads)
3003 fx_info=DestroyFxThreadSet(fx_info);
cristy3ed852e2009-09-05 21:47:34 +00003004 return(fx_info);
3005}
3006
3007MagickExport Image *FxImage(const Image *image,const char *expression,
3008 ExceptionInfo *exception)
3009{
cristy3ed852e2009-09-05 21:47:34 +00003010#define FxImageTag "Fx/Image"
3011
cristyfa112112010-01-04 17:48:07 +00003012 CacheView
cristy79cedc72011-07-25 00:41:15 +00003013 *fx_view,
3014 *image_view;
cristyfa112112010-01-04 17:48:07 +00003015
cristy3ed852e2009-09-05 21:47:34 +00003016 FxInfo
cristyfa112112010-01-04 17:48:07 +00003017 **restrict fx_info;
cristy3ed852e2009-09-05 21:47:34 +00003018
3019 Image
3020 *fx_image;
3021
cristy3ed852e2009-09-05 21:47:34 +00003022 MagickBooleanType
3023 status;
3024
cristybb503372010-05-27 20:51:26 +00003025 MagickOffsetType
3026 progress;
3027
cristybb503372010-05-27 20:51:26 +00003028 ssize_t
3029 y;
3030
cristy3ed852e2009-09-05 21:47:34 +00003031 assert(image != (Image *) NULL);
3032 assert(image->signature == MagickSignature);
3033 if (image->debug != MagickFalse)
3034 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00003035 fx_info=AcquireFxThreadSet(image,expression,exception);
3036 if (fx_info == (FxInfo **) NULL)
cristy65d161b2012-10-07 20:39:52 +00003037 return((Image *) NULL);
3038 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3039 if (fx_image == (Image *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003040 {
cristy3ed852e2009-09-05 21:47:34 +00003041 fx_info=DestroyFxThreadSet(fx_info);
3042 return((Image *) NULL);
3043 }
cristy65d161b2012-10-07 20:39:52 +00003044 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
3045 {
3046 fx_info=DestroyFxThreadSet(fx_info);
3047 fx_image=DestroyImage(fx_image);
3048 return((Image *) NULL);
3049 }
cristy3ed852e2009-09-05 21:47:34 +00003050 /*
3051 Fx image.
3052 */
3053 status=MagickTrue;
3054 progress=0;
cristy46ff2672012-12-14 15:32:26 +00003055 image_view=AcquireVirtualCacheView(image,exception);
3056 fx_view=AcquireAuthenticCacheView(fx_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003057#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003058 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00003059 magick_threads(image,fx_image,fx_image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003060#endif
cristybb503372010-05-27 20:51:26 +00003061 for (y=0; y < (ssize_t) fx_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003062 {
cristy5c9e6f22010-09-17 17:31:01 +00003063 const int
3064 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003065
cristy79cedc72011-07-25 00:41:15 +00003066 register const Quantum
3067 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003068
cristy4c08aed2011-07-01 19:47:50 +00003069 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003070 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003071
cristy79cedc72011-07-25 00:41:15 +00003072 register ssize_t
3073 x;
3074
cristy3ed852e2009-09-05 21:47:34 +00003075 if (status == MagickFalse)
3076 continue;
cristy79cedc72011-07-25 00:41:15 +00003077 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003078 q=QueueCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
cristy79cedc72011-07-25 00:41:15 +00003079 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003080 {
3081 status=MagickFalse;
3082 continue;
3083 }
cristybb503372010-05-27 20:51:26 +00003084 for (x=0; x < (ssize_t) fx_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003085 {
cristy79cedc72011-07-25 00:41:15 +00003086 register ssize_t
3087 i;
3088
3089 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3090 {
cristya19f1d72012-08-07 18:24:38 +00003091 double
cristy79cedc72011-07-25 00:41:15 +00003092 alpha;
3093
cristy5a23c552013-02-13 14:34:28 +00003094 PixelChannel channel=GetPixelChannelChannel(image,i);
3095 PixelTrait traits=GetPixelChannelTraits(image,channel);
3096 PixelTrait fx_traits=GetPixelChannelTraits(fx_image,channel);
cristy010d7d12011-08-31 01:02:48 +00003097 if ((traits == UndefinedPixelTrait) ||
3098 (fx_traits == UndefinedPixelTrait))
cristy79cedc72011-07-25 00:41:15 +00003099 continue;
cristy1eced092012-08-10 23:10:56 +00003100 if (((fx_traits & CopyPixelTrait) != 0) ||
cristy883fde12013-04-08 00:50:13 +00003101 (GetPixelReadMask(image,p) == 0))
cristy79cedc72011-07-25 00:41:15 +00003102 {
cristy0beccfa2011-09-25 20:47:53 +00003103 SetPixelChannel(fx_image,channel,p[i],q);
cristy79cedc72011-07-25 00:41:15 +00003104 continue;
3105 }
3106 alpha=0.0;
cristya382aca2011-12-06 18:22:48 +00003107 (void) FxEvaluateChannelExpression(fx_info[id],channel,x,y,&alpha,
3108 exception);
cristy8cd03c32012-07-07 18:57:59 +00003109 q[i]=ClampToQuantum(QuantumRange*alpha);
cristy79cedc72011-07-25 00:41:15 +00003110 }
3111 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003112 q+=GetPixelChannels(fx_image);
cristy3ed852e2009-09-05 21:47:34 +00003113 }
3114 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3115 status=MagickFalse;
3116 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3117 {
3118 MagickBooleanType
3119 proceed;
3120
cristyb5d5f722009-11-04 03:03:49 +00003121#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003122 #pragma omp critical (MagickCore_FxImage)
cristy3ed852e2009-09-05 21:47:34 +00003123#endif
3124 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3125 if (proceed == MagickFalse)
3126 status=MagickFalse;
3127 }
3128 }
cristy3ed852e2009-09-05 21:47:34 +00003129 fx_view=DestroyCacheView(fx_view);
cristy79cedc72011-07-25 00:41:15 +00003130 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003131 fx_info=DestroyFxThreadSet(fx_info);
3132 if (status == MagickFalse)
3133 fx_image=DestroyImage(fx_image);
3134 return(fx_image);
3135}
3136
3137/*
3138%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3139% %
3140% %
3141% %
3142% I m p l o d e I m a g e %
3143% %
3144% %
3145% %
3146%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3147%
3148% ImplodeImage() creates a new image that is a copy of an existing
3149% one with the image pixels "implode" by the specified percentage. It
3150% allocates the memory necessary for the new Image structure and returns a
3151% pointer to the new image.
3152%
3153% The format of the ImplodeImage method is:
3154%
3155% Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003156% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003157%
3158% A description of each parameter follows:
3159%
3160% o implode_image: Method ImplodeImage returns a pointer to the image
3161% after it is implode. A null image is returned if there is a memory
3162% shortage.
3163%
3164% o image: the image.
3165%
3166% o amount: Define the extent of the implosion.
3167%
cristy76f512e2011-09-12 01:26:56 +00003168% o method: the pixel interpolation method.
3169%
cristy3ed852e2009-09-05 21:47:34 +00003170% o exception: return any errors or warnings in this structure.
3171%
3172*/
3173MagickExport Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003174 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003175{
3176#define ImplodeImageTag "Implode/Image"
3177
cristyfa112112010-01-04 17:48:07 +00003178 CacheView
3179 *image_view,
cristy5ee937c2013-04-19 12:55:16 +00003180 *implode_view,
3181 *interpolate_view;
cristyfa112112010-01-04 17:48:07 +00003182
cristy3ed852e2009-09-05 21:47:34 +00003183 Image
3184 *implode_image;
3185
cristy3ed852e2009-09-05 21:47:34 +00003186 MagickBooleanType
3187 status;
3188
cristybb503372010-05-27 20:51:26 +00003189 MagickOffsetType
3190 progress;
3191
cristya19f1d72012-08-07 18:24:38 +00003192 double
cristy3ed852e2009-09-05 21:47:34 +00003193 radius;
3194
3195 PointInfo
3196 center,
3197 scale;
3198
cristybb503372010-05-27 20:51:26 +00003199 ssize_t
3200 y;
3201
cristy3ed852e2009-09-05 21:47:34 +00003202 /*
3203 Initialize implode image attributes.
3204 */
3205 assert(image != (Image *) NULL);
3206 assert(image->signature == MagickSignature);
3207 if (image->debug != MagickFalse)
3208 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3209 assert(exception != (ExceptionInfo *) NULL);
3210 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00003211 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3212 exception);
cristy3ed852e2009-09-05 21:47:34 +00003213 if (implode_image == (Image *) NULL)
3214 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003215 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003216 {
cristy3ed852e2009-09-05 21:47:34 +00003217 implode_image=DestroyImage(implode_image);
3218 return((Image *) NULL);
3219 }
cristy4c08aed2011-07-01 19:47:50 +00003220 if (implode_image->background_color.alpha != OpaqueAlpha)
cristy8a46d822012-08-28 23:32:39 +00003221 implode_image->alpha_trait=BlendPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +00003222 /*
3223 Compute scaling factor.
3224 */
3225 scale.x=1.0;
3226 scale.y=1.0;
3227 center.x=0.5*image->columns;
3228 center.y=0.5*image->rows;
3229 radius=center.x;
3230 if (image->columns > image->rows)
3231 scale.y=(double) image->columns/(double) image->rows;
3232 else
3233 if (image->columns < image->rows)
3234 {
3235 scale.x=(double) image->rows/(double) image->columns;
3236 radius=center.y;
3237 }
3238 /*
3239 Implode image.
3240 */
3241 status=MagickTrue;
3242 progress=0;
cristy46ff2672012-12-14 15:32:26 +00003243 image_view=AcquireVirtualCacheView(image,exception);
cristy5ee937c2013-04-19 12:55:16 +00003244 interpolate_view=AcquireVirtualCacheView(image,exception);
cristy46ff2672012-12-14 15:32:26 +00003245 implode_view=AcquireAuthenticCacheView(implode_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003246#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003247 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00003248 magick_threads(image,implode_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003249#endif
cristybb503372010-05-27 20:51:26 +00003250 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003251 {
cristya19f1d72012-08-07 18:24:38 +00003252 double
cristy3ed852e2009-09-05 21:47:34 +00003253 distance;
3254
3255 PointInfo
3256 delta;
3257
cristy6d188022011-09-12 13:23:33 +00003258 register const Quantum
3259 *restrict p;
3260
cristybb503372010-05-27 20:51:26 +00003261 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003262 x;
3263
cristy4c08aed2011-07-01 19:47:50 +00003264 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003265 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003266
3267 if (status == MagickFalse)
3268 continue;
cristy1665ce12013-04-19 12:18:43 +00003269 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003270 q=QueueCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003271 exception);
cristy6d188022011-09-12 13:23:33 +00003272 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003273 {
3274 status=MagickFalse;
3275 continue;
3276 }
cristy3ed852e2009-09-05 21:47:34 +00003277 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00003278 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003279 {
cristy6d188022011-09-12 13:23:33 +00003280 register ssize_t
3281 i;
3282
cristy3ed852e2009-09-05 21:47:34 +00003283 /*
3284 Determine if the pixel is within an ellipse.
3285 */
cristy883fde12013-04-08 00:50:13 +00003286 if (GetPixelReadMask(image,p) == 0)
cristy10a6c612012-01-29 21:41:05 +00003287 {
cristyc3a58022013-10-09 23:22:42 +00003288 SetPixelBackgoundColor(implode_image,q);
cristy10a6c612012-01-29 21:41:05 +00003289 p+=GetPixelChannels(image);
3290 q+=GetPixelChannels(implode_image);
3291 continue;
3292 }
cristy3ed852e2009-09-05 21:47:34 +00003293 delta.x=scale.x*(double) (x-center.x);
3294 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00003295 if (distance >= (radius*radius))
cristya6d7a9b2012-01-18 20:04:48 +00003296 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy6d188022011-09-12 13:23:33 +00003297 {
cristy5a23c552013-02-13 14:34:28 +00003298 PixelChannel channel=GetPixelChannelChannel(image,i);
3299 PixelTrait traits=GetPixelChannelTraits(image,channel);
3300 PixelTrait implode_traits=GetPixelChannelTraits(implode_image,
3301 channel);
cristy1707c6c2012-01-18 23:30:54 +00003302 if ((traits == UndefinedPixelTrait) ||
3303 (implode_traits == UndefinedPixelTrait))
3304 continue;
cristya6d7a9b2012-01-18 20:04:48 +00003305 SetPixelChannel(implode_image,channel,p[i],q);
cristy6d188022011-09-12 13:23:33 +00003306 }
3307 else
cristy3ed852e2009-09-05 21:47:34 +00003308 {
3309 double
3310 factor;
3311
3312 /*
3313 Implode the pixel.
3314 */
3315 factor=1.0;
3316 if (distance > 0.0)
cristy1707c6c2012-01-18 23:30:54 +00003317 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/radius/
3318 2)),-amount);
cristy5ee937c2013-04-19 12:55:16 +00003319 status=InterpolatePixelChannels(image,interpolate_view,implode_image,
3320 method,(double) (factor*delta.x/scale.x+center.x),(double) (factor*
3321 delta.y/scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00003322 }
cristy6d188022011-09-12 13:23:33 +00003323 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003324 q+=GetPixelChannels(implode_image);
cristy3ed852e2009-09-05 21:47:34 +00003325 }
3326 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3327 status=MagickFalse;
3328 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3329 {
3330 MagickBooleanType
3331 proceed;
3332
cristyb5d5f722009-11-04 03:03:49 +00003333#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003334 #pragma omp critical (MagickCore_ImplodeImage)
cristy3ed852e2009-09-05 21:47:34 +00003335#endif
3336 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3337 if (proceed == MagickFalse)
3338 status=MagickFalse;
3339 }
3340 }
3341 implode_view=DestroyCacheView(implode_view);
cristy5ee937c2013-04-19 12:55:16 +00003342 interpolate_view=DestroyCacheView(interpolate_view);
cristy3ed852e2009-09-05 21:47:34 +00003343 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003344 if (status == MagickFalse)
3345 implode_image=DestroyImage(implode_image);
3346 return(implode_image);
3347}
3348
3349/*
3350%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3351% %
3352% %
3353% %
3354% M o r p h I m a g e s %
3355% %
3356% %
3357% %
3358%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3359%
3360% The MorphImages() method requires a minimum of two images. The first
3361% image is transformed into the second by a number of intervening images
3362% as specified by frames.
3363%
3364% The format of the MorphImage method is:
3365%
cristybb503372010-05-27 20:51:26 +00003366% Image *MorphImages(const Image *image,const size_t number_frames,
cristy3ed852e2009-09-05 21:47:34 +00003367% ExceptionInfo *exception)
3368%
3369% A description of each parameter follows:
3370%
3371% o image: the image.
3372%
3373% o number_frames: Define the number of in-between image to generate.
3374% The more in-between frames, the smoother the morph.
3375%
3376% o exception: return any errors or warnings in this structure.
3377%
3378*/
cristy7de7f742012-12-14 00:28:27 +00003379MagickExport Image *MorphImages(const Image *image,const size_t number_frames,
3380 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003381{
3382#define MorphImageTag "Morph/Image"
3383
cristyfab83642012-12-14 00:31:38 +00003384 double
3385 alpha,
3386 beta;
3387
cristy3ed852e2009-09-05 21:47:34 +00003388 Image
3389 *morph_image,
3390 *morph_images;
3391
cristy9d314ff2011-03-09 01:30:28 +00003392 MagickBooleanType
3393 status;
cristy3ed852e2009-09-05 21:47:34 +00003394
3395 MagickOffsetType
3396 scene;
3397
cristy3ed852e2009-09-05 21:47:34 +00003398 register const Image
3399 *next;
3400
cristybb503372010-05-27 20:51:26 +00003401 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003402 i;
3403
cristy9d314ff2011-03-09 01:30:28 +00003404 ssize_t
3405 y;
cristy3ed852e2009-09-05 21:47:34 +00003406
3407 /*
3408 Clone first frame in sequence.
3409 */
3410 assert(image != (Image *) NULL);
3411 assert(image->signature == MagickSignature);
3412 if (image->debug != MagickFalse)
3413 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3414 assert(exception != (ExceptionInfo *) NULL);
3415 assert(exception->signature == MagickSignature);
3416 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3417 if (morph_images == (Image *) NULL)
3418 return((Image *) NULL);
3419 if (GetNextImageInList(image) == (Image *) NULL)
3420 {
3421 /*
3422 Morph single image.
3423 */
cristybb503372010-05-27 20:51:26 +00003424 for (i=1; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003425 {
3426 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3427 if (morph_image == (Image *) NULL)
3428 {
3429 morph_images=DestroyImageList(morph_images);
3430 return((Image *) NULL);
3431 }
3432 AppendImageToList(&morph_images,morph_image);
cristy8b27a6d2010-02-14 03:31:15 +00003433 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003434 {
cristy8b27a6d2010-02-14 03:31:15 +00003435 MagickBooleanType
3436 proceed;
3437
cristybb503372010-05-27 20:51:26 +00003438 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3439 number_frames);
cristy8b27a6d2010-02-14 03:31:15 +00003440 if (proceed == MagickFalse)
3441 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00003442 }
3443 }
3444 return(GetFirstImageInList(morph_images));
3445 }
3446 /*
3447 Morph image sequence.
3448 */
3449 status=MagickTrue;
3450 scene=0;
3451 next=image;
3452 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3453 {
cristybb503372010-05-27 20:51:26 +00003454 for (i=0; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003455 {
3456 CacheView
3457 *image_view,
3458 *morph_view;
3459
cristya19f1d72012-08-07 18:24:38 +00003460 beta=(double) (i+1.0)/(double) (number_frames+1.0);
cristy3ed852e2009-09-05 21:47:34 +00003461 alpha=1.0-beta;
cristy15b98cd2010-09-12 19:42:50 +00003462 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
cristyaa2c16c2012-03-25 22:21:35 +00003463 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*next->rows+beta*
3464 GetNextImageInList(next)->rows+0.5),next->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +00003465 if (morph_image == (Image *) NULL)
3466 {
3467 morph_images=DestroyImageList(morph_images);
3468 return((Image *) NULL);
3469 }
cristy1707c6c2012-01-18 23:30:54 +00003470 status=SetImageStorageClass(morph_image,DirectClass,exception);
3471 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003472 {
cristy3ed852e2009-09-05 21:47:34 +00003473 morph_image=DestroyImage(morph_image);
3474 return((Image *) NULL);
3475 }
3476 AppendImageToList(&morph_images,morph_image);
3477 morph_images=GetLastImageInList(morph_images);
cristy15b98cd2010-09-12 19:42:50 +00003478 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
cristyaa2c16c2012-03-25 22:21:35 +00003479 morph_images->rows,GetNextImageInList(next)->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +00003480 if (morph_image == (Image *) NULL)
3481 {
3482 morph_images=DestroyImageList(morph_images);
3483 return((Image *) NULL);
3484 }
cristy46ff2672012-12-14 15:32:26 +00003485 image_view=AcquireVirtualCacheView(morph_image,exception);
3486 morph_view=AcquireAuthenticCacheView(morph_images,exception);
cristyb5d5f722009-11-04 03:03:49 +00003487#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003488 #pragma omp parallel for schedule(static,4) shared(status) \
cristy5e6b2592012-12-19 14:08:11 +00003489 magick_threads(morph_image,morph_image,morph_image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003490#endif
cristybb503372010-05-27 20:51:26 +00003491 for (y=0; y < (ssize_t) morph_images->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003492 {
3493 MagickBooleanType
3494 sync;
3495
cristy4c08aed2011-07-01 19:47:50 +00003496 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003497 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003498
cristybb503372010-05-27 20:51:26 +00003499 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003500 x;
3501
cristy4c08aed2011-07-01 19:47:50 +00003502 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003503 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003504
3505 if (status == MagickFalse)
3506 continue;
3507 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3508 exception);
3509 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3510 exception);
cristy4c08aed2011-07-01 19:47:50 +00003511 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003512 {
3513 status=MagickFalse;
3514 continue;
3515 }
cristybb503372010-05-27 20:51:26 +00003516 for (x=0; x < (ssize_t) morph_images->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003517 {
cristy1707c6c2012-01-18 23:30:54 +00003518 register ssize_t
3519 i;
3520
cristy10a6c612012-01-29 21:41:05 +00003521 for (i=0; i < (ssize_t) GetPixelChannels(morph_image); i++)
cristy1707c6c2012-01-18 23:30:54 +00003522 {
cristy031a2462013-05-24 13:29:27 +00003523 PixelChannel channel=GetPixelChannelChannel(morph_image,i);
3524 PixelTrait traits=GetPixelChannelTraits(morph_image,channel);
3525 PixelTrait morph_traits=GetPixelChannelTraits(morph_images,channel);
cristy1707c6c2012-01-18 23:30:54 +00003526 if ((traits == UndefinedPixelTrait) ||
3527 (morph_traits == UndefinedPixelTrait))
3528 continue;
cristy1eced092012-08-10 23:10:56 +00003529 if (((morph_traits & CopyPixelTrait) != 0) ||
cristy031a2462013-05-24 13:29:27 +00003530 (GetPixelReadMask(morph_images,p) == 0))
cristy1707c6c2012-01-18 23:30:54 +00003531 {
3532 SetPixelChannel(morph_image,channel,p[i],q);
3533 continue;
3534 }
3535 SetPixelChannel(morph_image,channel,ClampToQuantum(alpha*
3536 GetPixelChannel(morph_images,channel,q)+beta*p[i]),q);
3537 }
cristyed231572011-07-14 02:18:59 +00003538 p+=GetPixelChannels(morph_image);
3539 q+=GetPixelChannels(morph_images);
cristy3ed852e2009-09-05 21:47:34 +00003540 }
3541 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3542 if (sync == MagickFalse)
3543 status=MagickFalse;
3544 }
3545 morph_view=DestroyCacheView(morph_view);
3546 image_view=DestroyCacheView(image_view);
3547 morph_image=DestroyImage(morph_image);
3548 }
cristybb503372010-05-27 20:51:26 +00003549 if (i < (ssize_t) number_frames)
cristy3ed852e2009-09-05 21:47:34 +00003550 break;
3551 /*
3552 Clone last frame in sequence.
3553 */
3554 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3555 if (morph_image == (Image *) NULL)
3556 {
3557 morph_images=DestroyImageList(morph_images);
3558 return((Image *) NULL);
3559 }
3560 AppendImageToList(&morph_images,morph_image);
3561 morph_images=GetLastImageInList(morph_images);
3562 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3563 {
3564 MagickBooleanType
3565 proceed;
3566
cristyb5d5f722009-11-04 03:03:49 +00003567#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003568 #pragma omp critical (MagickCore_MorphImages)
cristy3ed852e2009-09-05 21:47:34 +00003569#endif
3570 proceed=SetImageProgress(image,MorphImageTag,scene,
3571 GetImageListLength(image));
3572 if (proceed == MagickFalse)
3573 status=MagickFalse;
3574 }
3575 scene++;
3576 }
3577 if (GetNextImageInList(next) != (Image *) NULL)
3578 {
3579 morph_images=DestroyImageList(morph_images);
3580 return((Image *) NULL);
3581 }
3582 return(GetFirstImageInList(morph_images));
3583}
3584
3585/*
3586%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3587% %
3588% %
3589% %
3590% P l a s m a I m a g e %
3591% %
3592% %
3593% %
3594%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3595%
3596% PlasmaImage() initializes an image with plasma fractal values. The image
3597% must be initialized with a base color and the random number generator
3598% seeded before this method is called.
3599%
3600% The format of the PlasmaImage method is:
3601%
3602% MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
cristy5cbc0162011-08-29 00:36:28 +00003603% size_t attenuate,size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003604%
3605% A description of each parameter follows:
3606%
3607% o image: the image.
3608%
3609% o segment: Define the region to apply plasma fractals values.
3610%
glennrp7dae1ca2010-09-16 12:17:35 +00003611% o attenuate: Define the plasma attenuation factor.
cristy3ed852e2009-09-05 21:47:34 +00003612%
3613% o depth: Limit the plasma recursion depth.
3614%
cristy5cbc0162011-08-29 00:36:28 +00003615% o exception: return any errors or warnings in this structure.
3616%
cristy3ed852e2009-09-05 21:47:34 +00003617*/
3618
3619static inline Quantum PlasmaPixel(RandomInfo *random_info,
cristya19f1d72012-08-07 18:24:38 +00003620 const double pixel,const double noise)
cristy3ed852e2009-09-05 21:47:34 +00003621{
3622 Quantum
3623 plasma;
3624
cristyce70c172010-01-07 17:15:30 +00003625 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
cristy3ed852e2009-09-05 21:47:34 +00003626 noise/2.0);
3627 return(plasma);
3628}
3629
cristyda1f9c12011-10-02 21:39:49 +00003630static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3631 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3632 const SegmentInfo *segment,size_t attenuate,size_t depth,
3633 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003634{
cristya19f1d72012-08-07 18:24:38 +00003635 double
cristy3ed852e2009-09-05 21:47:34 +00003636 plasma;
3637
cristyda1f9c12011-10-02 21:39:49 +00003638 register const Quantum
3639 *restrict u,
3640 *restrict v;
3641
3642 register Quantum
3643 *restrict q;
3644
3645 register ssize_t
3646 i;
cristy3ed852e2009-09-05 21:47:34 +00003647
cristy9d314ff2011-03-09 01:30:28 +00003648 ssize_t
3649 x,
3650 x_mid,
3651 y,
3652 y_mid;
3653
cristy3ed852e2009-09-05 21:47:34 +00003654 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3655 return(MagickTrue);
3656 if (depth != 0)
3657 {
3658 SegmentInfo
3659 local_info;
3660
3661 /*
3662 Divide the area into quadrants and recurse.
3663 */
3664 depth--;
3665 attenuate++;
cristybb503372010-05-27 20:51:26 +00003666 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3667 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003668 local_info=(*segment);
3669 local_info.x2=(double) x_mid;
3670 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003671 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3672 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003673 local_info=(*segment);
3674 local_info.y1=(double) y_mid;
3675 local_info.x2=(double) x_mid;
cristyda1f9c12011-10-02 21:39:49 +00003676 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3677 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003678 local_info=(*segment);
3679 local_info.x1=(double) x_mid;
3680 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003681 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3682 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003683 local_info=(*segment);
3684 local_info.x1=(double) x_mid;
3685 local_info.y1=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003686 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3687 &local_info,attenuate,depth,exception));
cristy3ed852e2009-09-05 21:47:34 +00003688 }
cristybb503372010-05-27 20:51:26 +00003689 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3690 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003691 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3692 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3693 return(MagickFalse);
3694 /*
3695 Average pixels and apply plasma.
3696 */
cristya19f1d72012-08-07 18:24:38 +00003697 plasma=(double) QuantumRange/(2.0*attenuate);
cristy3ed852e2009-09-05 21:47:34 +00003698 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3699 {
cristy3ed852e2009-09-05 21:47:34 +00003700 /*
3701 Left pixel.
3702 */
cristybb503372010-05-27 20:51:26 +00003703 x=(ssize_t) ceil(segment->x1-0.5);
cristy1707c6c2012-01-18 23:30:54 +00003704 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),1,1,
3705 exception);
3706 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),1,1,
3707 exception);
cristyc5c6f662010-09-22 14:23:02 +00003708 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003709 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3710 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003711 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003712 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3713 {
cristy5a23c552013-02-13 14:34:28 +00003714 PixelChannel channel=GetPixelChannelChannel(image,i);
3715 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003716 if (traits == UndefinedPixelTrait)
3717 continue;
cristy6e564992013-05-24 01:21:04 +00003718 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
cristyda1f9c12011-10-02 21:39:49 +00003719 }
cristyc5c6f662010-09-22 14:23:02 +00003720 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003721 if (segment->x1 != segment->x2)
3722 {
3723 /*
3724 Right pixel.
3725 */
cristybb503372010-05-27 20:51:26 +00003726 x=(ssize_t) ceil(segment->x2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003727 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3728 1,1,exception);
3729 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3730 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003731 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003732 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3733 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003734 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003735 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3736 {
cristy5a23c552013-02-13 14:34:28 +00003737 PixelChannel channel=GetPixelChannelChannel(image,i);
3738 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003739 if (traits == UndefinedPixelTrait)
3740 continue;
cristy6e564992013-05-24 01:21:04 +00003741 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
cristyda1f9c12011-10-02 21:39:49 +00003742 }
cristyc5c6f662010-09-22 14:23:02 +00003743 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003744 }
3745 }
3746 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3747 {
3748 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3749 {
cristy3ed852e2009-09-05 21:47:34 +00003750 /*
3751 Bottom pixel.
3752 */
cristybb503372010-05-27 20:51:26 +00003753 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003754 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3755 1,1,exception);
3756 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3757 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003758 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003759 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3760 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003761 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003762 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3763 {
cristy5a23c552013-02-13 14:34:28 +00003764 PixelChannel channel=GetPixelChannelChannel(image,i);
3765 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003766 if (traits == UndefinedPixelTrait)
3767 continue;
cristy6e564992013-05-24 01:21:04 +00003768 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
cristyda1f9c12011-10-02 21:39:49 +00003769 }
cristyc5c6f662010-09-22 14:23:02 +00003770 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003771 }
3772 if (segment->y1 != segment->y2)
3773 {
cristy3ed852e2009-09-05 21:47:34 +00003774 /*
3775 Top pixel.
3776 */
cristybb503372010-05-27 20:51:26 +00003777 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003778 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3779 1,1,exception);
3780 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3781 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003782 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003783 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3784 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003785 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003786 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3787 {
cristy5a23c552013-02-13 14:34:28 +00003788 PixelChannel channel=GetPixelChannelChannel(image,i);
3789 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003790 if (traits == UndefinedPixelTrait)
3791 continue;
cristy6e564992013-05-24 01:21:04 +00003792 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
cristyda1f9c12011-10-02 21:39:49 +00003793 }
cristyc5c6f662010-09-22 14:23:02 +00003794 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003795 }
3796 }
3797 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3798 {
cristy3ed852e2009-09-05 21:47:34 +00003799 /*
3800 Middle pixel.
3801 */
cristybb503372010-05-27 20:51:26 +00003802 x=(ssize_t) ceil(segment->x1-0.5);
3803 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003804 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
cristybb503372010-05-27 20:51:26 +00003805 x=(ssize_t) ceil(segment->x2-0.5);
3806 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003807 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003808 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003809 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3810 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003811 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003812 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3813 {
cristy5a23c552013-02-13 14:34:28 +00003814 PixelChannel channel=GetPixelChannelChannel(image,i);
3815 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003816 if (traits == UndefinedPixelTrait)
3817 continue;
cristy6e564992013-05-24 01:21:04 +00003818 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
cristyda1f9c12011-10-02 21:39:49 +00003819 }
cristyc5c6f662010-09-22 14:23:02 +00003820 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003821 }
3822 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3823 return(MagickTrue);
3824 return(MagickFalse);
3825}
cristyda1f9c12011-10-02 21:39:49 +00003826
cristy3ed852e2009-09-05 21:47:34 +00003827MagickExport MagickBooleanType PlasmaImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00003828 const SegmentInfo *segment,size_t attenuate,size_t depth,
3829 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003830{
cristyc5c6f662010-09-22 14:23:02 +00003831 CacheView
cristyda1f9c12011-10-02 21:39:49 +00003832 *image_view,
3833 *u_view,
3834 *v_view;
cristyc5c6f662010-09-22 14:23:02 +00003835
cristy3ed852e2009-09-05 21:47:34 +00003836 MagickBooleanType
3837 status;
3838
3839 RandomInfo
3840 *random_info;
3841
3842 if (image->debug != MagickFalse)
3843 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3844 assert(image != (Image *) NULL);
3845 assert(image->signature == MagickSignature);
3846 if (image->debug != MagickFalse)
3847 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy5cbc0162011-08-29 00:36:28 +00003848 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristyc5c6f662010-09-22 14:23:02 +00003849 return(MagickFalse);
cristy46ff2672012-12-14 15:32:26 +00003850 image_view=AcquireAuthenticCacheView(image,exception);
3851 u_view=AcquireVirtualCacheView(image,exception);
3852 v_view=AcquireVirtualCacheView(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003853 random_info=AcquireRandomInfo();
cristyda1f9c12011-10-02 21:39:49 +00003854 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3855 attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003856 random_info=DestroyRandomInfo(random_info);
cristyda1f9c12011-10-02 21:39:49 +00003857 v_view=DestroyCacheView(v_view);
3858 u_view=DestroyCacheView(u_view);
cristyc5c6f662010-09-22 14:23:02 +00003859 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003860 return(status);
3861}
3862
3863/*
3864%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3865% %
3866% %
3867% %
3868% P o l a r o i d I m a g e %
3869% %
3870% %
3871% %
3872%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3873%
3874% PolaroidImage() simulates a Polaroid picture.
3875%
3876% The format of the AnnotateImage method is:
3877%
3878% Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003879% const char *caption,const double angle,
3880% const PixelInterpolateMethod method,ExceptionInfo exception)
cristy3ed852e2009-09-05 21:47:34 +00003881%
3882% A description of each parameter follows:
3883%
3884% o image: the image.
3885%
3886% o draw_info: the draw info.
3887%
cristye9e3d382011-12-14 01:50:13 +00003888% o caption: the Polaroid caption.
3889%
cristycee97112010-05-28 00:44:52 +00003890% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00003891%
cristy5c4e2582011-09-11 19:21:03 +00003892% o method: the pixel interpolation method.
3893%
cristy3ed852e2009-09-05 21:47:34 +00003894% o exception: return any errors or warnings in this structure.
3895%
3896*/
3897MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003898 const char *caption,const double angle,const PixelInterpolateMethod method,
cristy5c4e2582011-09-11 19:21:03 +00003899 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003900{
cristy3ed852e2009-09-05 21:47:34 +00003901 Image
3902 *bend_image,
3903 *caption_image,
3904 *flop_image,
3905 *picture_image,
3906 *polaroid_image,
3907 *rotate_image,
3908 *trim_image;
3909
cristybb503372010-05-27 20:51:26 +00003910 size_t
cristy3ed852e2009-09-05 21:47:34 +00003911 height;
3912
cristy9d314ff2011-03-09 01:30:28 +00003913 ssize_t
3914 quantum;
3915
cristy3ed852e2009-09-05 21:47:34 +00003916 /*
3917 Simulate a Polaroid picture.
3918 */
3919 assert(image != (Image *) NULL);
3920 assert(image->signature == MagickSignature);
3921 if (image->debug != MagickFalse)
3922 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3923 assert(exception != (ExceptionInfo *) NULL);
3924 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00003925 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
cristy3ed852e2009-09-05 21:47:34 +00003926 image->rows)/25.0,10.0);
3927 height=image->rows+2*quantum;
3928 caption_image=(Image *) NULL;
cristye9e3d382011-12-14 01:50:13 +00003929 if (caption != (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003930 {
3931 char
cristye9e3d382011-12-14 01:50:13 +00003932 geometry[MaxTextExtent],
3933 *text;
cristy3ed852e2009-09-05 21:47:34 +00003934
3935 DrawInfo
3936 *annotate_info;
3937
cristy72e0b772013-04-28 14:29:45 +00003938 ImageInfo
3939 *image_info;
3940
cristy3ed852e2009-09-05 21:47:34 +00003941 MagickBooleanType
3942 status;
3943
cristy9d314ff2011-03-09 01:30:28 +00003944 ssize_t
3945 count;
3946
cristy3ed852e2009-09-05 21:47:34 +00003947 TypeMetric
3948 metrics;
3949
3950 /*
3951 Generate caption image.
3952 */
3953 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3954 if (caption_image == (Image *) NULL)
3955 return((Image *) NULL);
cristy72e0b772013-04-28 14:29:45 +00003956 image_info=AcquireImageInfo();
cristy3ed852e2009-09-05 21:47:34 +00003957 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
cristy72e0b772013-04-28 14:29:45 +00003958 text=InterpretImageProperties(image_info,(Image *) image,caption,
cristye9e3d382011-12-14 01:50:13 +00003959 exception);
cristy72e0b772013-04-28 14:29:45 +00003960 image_info=DestroyImageInfo(image_info);
cristye9e3d382011-12-14 01:50:13 +00003961 (void) CloneString(&annotate_info->text,text);
cristy6b1d05e2010-09-22 19:17:27 +00003962 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
cristye9e3d382011-12-14 01:50:13 +00003963 &text,exception);
3964 status=SetImageExtent(caption_image,image->columns,(size_t) ((count+1)*
3965 (metrics.ascent-metrics.descent)+0.5),exception);
cristy3ed852e2009-09-05 21:47:34 +00003966 if (status == MagickFalse)
3967 caption_image=DestroyImage(caption_image);
3968 else
3969 {
3970 caption_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003971 (void) SetImageBackgroundColor(caption_image,exception);
cristye9e3d382011-12-14 01:50:13 +00003972 (void) CloneString(&annotate_info->text,text);
cristyb51dff52011-05-19 16:55:47 +00003973 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
cristy3ed852e2009-09-05 21:47:34 +00003974 metrics.ascent);
3975 if (annotate_info->gravity == UndefinedGravity)
3976 (void) CloneString(&annotate_info->geometry,AcquireString(
3977 geometry));
cristy5cbc0162011-08-29 00:36:28 +00003978 (void) AnnotateImage(caption_image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003979 height+=caption_image->rows;
3980 }
3981 annotate_info=DestroyDrawInfo(annotate_info);
cristye9e3d382011-12-14 01:50:13 +00003982 text=DestroyString(text);
cristy3ed852e2009-09-05 21:47:34 +00003983 }
3984 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
3985 exception);
3986 if (picture_image == (Image *) NULL)
3987 {
3988 if (caption_image != (Image *) NULL)
3989 caption_image=DestroyImage(caption_image);
3990 return((Image *) NULL);
3991 }
3992 picture_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003993 (void) SetImageBackgroundColor(picture_image,exception);
cristy39172402012-03-30 13:04:39 +00003994 (void) CompositeImage(picture_image,image,OverCompositeOp,MagickTrue,quantum,
cristyfeb3e962012-03-29 17:25:55 +00003995 quantum,exception);
cristy3ed852e2009-09-05 21:47:34 +00003996 if (caption_image != (Image *) NULL)
3997 {
cristyfeb3e962012-03-29 17:25:55 +00003998 (void) CompositeImage(picture_image,caption_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00003999 MagickTrue,quantum,(ssize_t) (image->rows+3*quantum/2),exception);
cristy3ed852e2009-09-05 21:47:34 +00004000 caption_image=DestroyImage(caption_image);
4001 }
cristy9950d572011-10-01 18:22:35 +00004002 (void) QueryColorCompliance("none",AllCompliance,
4003 &picture_image->background_color,exception);
cristy63240882011-08-05 19:05:27 +00004004 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004005 rotate_image=RotateImage(picture_image,90.0,exception);
4006 picture_image=DestroyImage(picture_image);
4007 if (rotate_image == (Image *) NULL)
4008 return((Image *) NULL);
4009 picture_image=rotate_image;
4010 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
cristy5c4e2582011-09-11 19:21:03 +00004011 picture_image->columns,method,exception);
cristy3ed852e2009-09-05 21:47:34 +00004012 picture_image=DestroyImage(picture_image);
4013 if (bend_image == (Image *) NULL)
4014 return((Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00004015 picture_image=bend_image;
4016 rotate_image=RotateImage(picture_image,-90.0,exception);
4017 picture_image=DestroyImage(picture_image);
4018 if (rotate_image == (Image *) NULL)
4019 return((Image *) NULL);
4020 picture_image=rotate_image;
4021 picture_image->background_color=image->background_color;
anthonyf46d4262012-03-26 03:30:34 +00004022 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
cristy3ed852e2009-09-05 21:47:34 +00004023 exception);
4024 if (polaroid_image == (Image *) NULL)
4025 {
4026 picture_image=DestroyImage(picture_image);
4027 return(picture_image);
4028 }
4029 flop_image=FlopImage(polaroid_image,exception);
4030 polaroid_image=DestroyImage(polaroid_image);
4031 if (flop_image == (Image *) NULL)
4032 {
4033 picture_image=DestroyImage(picture_image);
4034 return(picture_image);
4035 }
4036 polaroid_image=flop_image;
cristyfeb3e962012-03-29 17:25:55 +00004037 (void) CompositeImage(polaroid_image,picture_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00004038 MagickTrue,(ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
cristy3ed852e2009-09-05 21:47:34 +00004039 picture_image=DestroyImage(picture_image);
cristy9950d572011-10-01 18:22:35 +00004040 (void) QueryColorCompliance("none",AllCompliance,
4041 &polaroid_image->background_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00004042 rotate_image=RotateImage(polaroid_image,angle,exception);
4043 polaroid_image=DestroyImage(polaroid_image);
4044 if (rotate_image == (Image *) NULL)
4045 return((Image *) NULL);
4046 polaroid_image=rotate_image;
4047 trim_image=TrimImage(polaroid_image,exception);
4048 polaroid_image=DestroyImage(polaroid_image);
4049 if (trim_image == (Image *) NULL)
4050 return((Image *) NULL);
4051 polaroid_image=trim_image;
4052 return(polaroid_image);
4053}
4054
4055/*
4056%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4057% %
4058% %
4059% %
cristy3ed852e2009-09-05 21:47:34 +00004060% S e p i a T o n e I m a g e %
4061% %
4062% %
4063% %
4064%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4065%
4066% MagickSepiaToneImage() applies a special effect to the image, similar to the
4067% effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4068% 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4069% threshold of 80% is a good starting point for a reasonable tone.
4070%
4071% The format of the SepiaToneImage method is:
4072%
4073% Image *SepiaToneImage(const Image *image,const double threshold,
4074% ExceptionInfo *exception)
4075%
4076% A description of each parameter follows:
4077%
4078% o image: the image.
4079%
4080% o threshold: the tone threshold.
4081%
4082% o exception: return any errors or warnings in this structure.
4083%
4084*/
4085MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4086 ExceptionInfo *exception)
4087{
4088#define SepiaToneImageTag "SepiaTone/Image"
4089
cristyc4c8d132010-01-07 01:58:38 +00004090 CacheView
4091 *image_view,
4092 *sepia_view;
4093
cristy3ed852e2009-09-05 21:47:34 +00004094 Image
4095 *sepia_image;
4096
cristy3ed852e2009-09-05 21:47:34 +00004097 MagickBooleanType
4098 status;
4099
cristybb503372010-05-27 20:51:26 +00004100 MagickOffsetType
4101 progress;
4102
4103 ssize_t
4104 y;
4105
cristy3ed852e2009-09-05 21:47:34 +00004106 /*
4107 Initialize sepia-toned image attributes.
4108 */
4109 assert(image != (const Image *) NULL);
4110 assert(image->signature == MagickSignature);
4111 if (image->debug != MagickFalse)
4112 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4113 assert(exception != (ExceptionInfo *) NULL);
4114 assert(exception->signature == MagickSignature);
cristy1707c6c2012-01-18 23:30:54 +00004115 sepia_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004116 if (sepia_image == (Image *) NULL)
4117 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004118 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004119 {
cristy3ed852e2009-09-05 21:47:34 +00004120 sepia_image=DestroyImage(sepia_image);
4121 return((Image *) NULL);
4122 }
4123 /*
4124 Tone each row of the image.
4125 */
4126 status=MagickTrue;
4127 progress=0;
cristy46ff2672012-12-14 15:32:26 +00004128 image_view=AcquireVirtualCacheView(image,exception);
4129 sepia_view=AcquireAuthenticCacheView(sepia_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00004130#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00004131 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00004132 magick_threads(image,sepia_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00004133#endif
cristybb503372010-05-27 20:51:26 +00004134 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004135 {
cristy4c08aed2011-07-01 19:47:50 +00004136 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004137 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004138
cristybb503372010-05-27 20:51:26 +00004139 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004140 x;
4141
cristy4c08aed2011-07-01 19:47:50 +00004142 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004143 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004144
4145 if (status == MagickFalse)
4146 continue;
4147 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00004148 q=GetCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00004149 exception);
cristy4c08aed2011-07-01 19:47:50 +00004150 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004151 {
4152 status=MagickFalse;
4153 continue;
4154 }
cristybb503372010-05-27 20:51:26 +00004155 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004156 {
cristya19f1d72012-08-07 18:24:38 +00004157 double
cristy3ed852e2009-09-05 21:47:34 +00004158 intensity,
4159 tone;
4160
cristyf13c5942012-08-08 23:50:11 +00004161 intensity=GetPixelIntensity(image,p);
cristya19f1d72012-08-07 18:24:38 +00004162 tone=intensity > threshold ? (double) QuantumRange : intensity+
4163 (double) QuantumRange-threshold;
cristy4c08aed2011-07-01 19:47:50 +00004164 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
cristya19f1d72012-08-07 18:24:38 +00004165 tone=intensity > (7.0*threshold/6.0) ? (double) QuantumRange :
4166 intensity+(double) QuantumRange-7.0*threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004167 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004168 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004169 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004170 tone=threshold/7.0;
cristya19f1d72012-08-07 18:24:38 +00004171 if ((double) GetPixelGreen(image,q) < tone)
cristy4c08aed2011-07-01 19:47:50 +00004172 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
cristya19f1d72012-08-07 18:24:38 +00004173 if ((double) GetPixelBlue(image,q) < tone)
cristy4c08aed2011-07-01 19:47:50 +00004174 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristyc8a5f0e2014-04-04 11:36:37 +00004175 SetPixelAlpha(sepia_image,GetPixelAlpha(image,p),q);
cristyed231572011-07-14 02:18:59 +00004176 p+=GetPixelChannels(image);
4177 q+=GetPixelChannels(sepia_image);
cristy3ed852e2009-09-05 21:47:34 +00004178 }
4179 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4180 status=MagickFalse;
4181 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4182 {
4183 MagickBooleanType
4184 proceed;
4185
cristyb5d5f722009-11-04 03:03:49 +00004186#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004187 #pragma omp critical (MagickCore_SepiaToneImage)
cristy3ed852e2009-09-05 21:47:34 +00004188#endif
4189 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4190 image->rows);
4191 if (proceed == MagickFalse)
4192 status=MagickFalse;
4193 }
4194 }
4195 sepia_view=DestroyCacheView(sepia_view);
4196 image_view=DestroyCacheView(image_view);
cristye23ec9d2011-08-16 18:15:40 +00004197 (void) NormalizeImage(sepia_image,exception);
4198 (void) ContrastImage(sepia_image,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004199 if (status == MagickFalse)
4200 sepia_image=DestroyImage(sepia_image);
4201 return(sepia_image);
4202}
4203
4204/*
4205%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4206% %
4207% %
4208% %
4209% S h a d o w I m a g e %
4210% %
4211% %
4212% %
4213%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4214%
4215% ShadowImage() simulates a shadow from the specified image and returns it.
4216%
4217% The format of the ShadowImage method is:
4218%
cristy70cddf72011-12-10 22:42:42 +00004219% Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004220% const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4221% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004222%
4223% A description of each parameter follows:
4224%
4225% o image: the image.
4226%
cristy70cddf72011-12-10 22:42:42 +00004227% o alpha: percentage transparency.
cristy3ed852e2009-09-05 21:47:34 +00004228%
4229% o sigma: the standard deviation of the Gaussian, in pixels.
4230%
4231% o x_offset: the shadow x-offset.
4232%
4233% o y_offset: the shadow y-offset.
4234%
4235% o exception: return any errors or warnings in this structure.
4236%
4237*/
cristy70cddf72011-12-10 22:42:42 +00004238MagickExport Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004239 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4240 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004241{
4242#define ShadowImageTag "Shadow/Image"
4243
cristy70cddf72011-12-10 22:42:42 +00004244 CacheView
4245 *image_view;
4246
cristybd5a96c2011-08-21 00:04:26 +00004247 ChannelType
4248 channel_mask;
4249
cristy3ed852e2009-09-05 21:47:34 +00004250 Image
4251 *border_image,
4252 *clone_image,
4253 *shadow_image;
4254
cristy70cddf72011-12-10 22:42:42 +00004255 MagickBooleanType
4256 status;
4257
cristy3ed852e2009-09-05 21:47:34 +00004258 RectangleInfo
4259 border_info;
4260
cristy70cddf72011-12-10 22:42:42 +00004261 ssize_t
4262 y;
4263
cristy3ed852e2009-09-05 21:47:34 +00004264 assert(image != (Image *) NULL);
4265 assert(image->signature == MagickSignature);
4266 if (image->debug != MagickFalse)
4267 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4268 assert(exception != (ExceptionInfo *) NULL);
4269 assert(exception->signature == MagickSignature);
4270 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4271 if (clone_image == (Image *) NULL)
4272 return((Image *) NULL);
cristya6400b12013-03-15 12:20:18 +00004273 if (IsGrayColorspace(image->colorspace) != MagickFalse)
cristy0c81d062013-04-21 15:22:02 +00004274 (void) SetImageColorspace(clone_image,sRGBColorspace,exception);
cristy0ce08762012-06-30 01:33:18 +00004275 (void) SetImageVirtualPixelMethod(clone_image,TransparentVirtualPixelMethod,
cristy387430f2012-02-07 13:09:46 +00004276 exception);
cristybb503372010-05-27 20:51:26 +00004277 border_info.width=(size_t) floor(2.0*sigma+0.5);
4278 border_info.height=(size_t) floor(2.0*sigma+0.5);
cristy3ed852e2009-09-05 21:47:34 +00004279 border_info.x=0;
4280 border_info.y=0;
cristy9950d572011-10-01 18:22:35 +00004281 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4282 exception);
cristy8a46d822012-08-28 23:32:39 +00004283 clone_image->alpha_trait=BlendPixelTrait;
cristy70cddf72011-12-10 22:42:42 +00004284 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
cristy3ed852e2009-09-05 21:47:34 +00004285 clone_image=DestroyImage(clone_image);
4286 if (border_image == (Image *) NULL)
4287 return((Image *) NULL);
cristy8a46d822012-08-28 23:32:39 +00004288 if (border_image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00004289 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004290 /*
4291 Shadow image.
4292 */
cristy70cddf72011-12-10 22:42:42 +00004293 status=MagickTrue;
cristy46ff2672012-12-14 15:32:26 +00004294 image_view=AcquireAuthenticCacheView(border_image,exception);
cristy70cddf72011-12-10 22:42:42 +00004295 for (y=0; y < (ssize_t) border_image->rows; y++)
4296 {
4297 PixelInfo
4298 background_color;
4299
4300 register Quantum
4301 *restrict q;
4302
4303 register ssize_t
4304 x;
4305
4306 if (status == MagickFalse)
4307 continue;
4308 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
4309 exception);
4310 if (q == (Quantum *) NULL)
4311 {
4312 status=MagickFalse;
4313 continue;
4314 }
4315 background_color=border_image->background_color;
cristy8a46d822012-08-28 23:32:39 +00004316 background_color.alpha_trait=BlendPixelTrait;
cristy70cddf72011-12-10 22:42:42 +00004317 for (x=0; x < (ssize_t) border_image->columns; x++)
4318 {
cristy8a46d822012-08-28 23:32:39 +00004319 if (border_image->alpha_trait == BlendPixelTrait)
cristy70cddf72011-12-10 22:42:42 +00004320 background_color.alpha=GetPixelAlpha(border_image,q)*alpha/100.0;
4321 SetPixelInfoPixel(border_image,&background_color,q);
4322 q+=GetPixelChannels(border_image);
4323 }
4324 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4325 status=MagickFalse;
4326 }
4327 image_view=DestroyCacheView(image_view);
4328 if (status == MagickFalse)
4329 {
4330 border_image=DestroyImage(border_image);
4331 return((Image *) NULL);
4332 }
cristycf1296e2012-08-26 23:40:49 +00004333 channel_mask=SetImageChannelMask(border_image,AlphaChannel);
cristyaa2c16c2012-03-25 22:21:35 +00004334 shadow_image=BlurImage(border_image,0.0,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00004335 border_image=DestroyImage(border_image);
4336 if (shadow_image == (Image *) NULL)
4337 return((Image *) NULL);
cristycf1296e2012-08-26 23:40:49 +00004338 (void) SetPixelChannelMask(shadow_image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00004339 if (shadow_image->page.width == 0)
4340 shadow_image->page.width=shadow_image->columns;
4341 if (shadow_image->page.height == 0)
4342 shadow_image->page.height=shadow_image->rows;
cristybb503372010-05-27 20:51:26 +00004343 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4344 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4345 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4346 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
cristy3ed852e2009-09-05 21:47:34 +00004347 return(shadow_image);
4348}
4349
4350/*
4351%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4352% %
4353% %
4354% %
4355% S k e t c h I m a g e %
4356% %
4357% %
4358% %
4359%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4360%
4361% SketchImage() simulates a pencil sketch. We convolve the image with a
4362% Gaussian operator of the given radius and standard deviation (sigma). For
4363% reasonable results, radius should be larger than sigma. Use a radius of 0
4364% and SketchImage() selects a suitable radius for you. Angle gives the angle
4365% of the sketch.
4366%
4367% The format of the SketchImage method is:
4368%
4369% Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004370% const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004371%
4372% A description of each parameter follows:
4373%
4374% o image: the image.
4375%
cristy574cc262011-08-05 01:23:58 +00004376% o radius: the radius of the Gaussian, in pixels, not counting the
4377% center pixel.
cristy3ed852e2009-09-05 21:47:34 +00004378%
4379% o sigma: the standard deviation of the Gaussian, in pixels.
4380%
cristyf7ef0252011-09-09 14:50:06 +00004381% o angle: apply the effect along this angle.
4382%
cristy3ed852e2009-09-05 21:47:34 +00004383% o exception: return any errors or warnings in this structure.
4384%
4385*/
4386MagickExport Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004387 const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004388{
cristyfa112112010-01-04 17:48:07 +00004389 CacheView
4390 *random_view;
4391
cristy3ed852e2009-09-05 21:47:34 +00004392 Image
4393 *blend_image,
4394 *blur_image,
4395 *dodge_image,
4396 *random_image,
4397 *sketch_image;
4398
cristy3ed852e2009-09-05 21:47:34 +00004399 MagickBooleanType
4400 status;
4401
cristy3ed852e2009-09-05 21:47:34 +00004402 RandomInfo
cristyfa112112010-01-04 17:48:07 +00004403 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00004404
cristy9d314ff2011-03-09 01:30:28 +00004405 ssize_t
4406 y;
4407
glennrpf7659d72012-09-24 18:14:56 +00004408#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +00004409 unsigned long
4410 key;
glennrpf7659d72012-09-24 18:14:56 +00004411#endif
cristy57340e02012-05-05 00:53:23 +00004412
cristy3ed852e2009-09-05 21:47:34 +00004413 /*
4414 Sketch image.
4415 */
4416 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4417 MagickTrue,exception);
4418 if (random_image == (Image *) NULL)
4419 return((Image *) NULL);
4420 status=MagickTrue;
cristy1b784432009-12-19 02:20:40 +00004421 random_info=AcquireRandomInfoThreadSet();
glennrpf7659d72012-09-24 18:14:56 +00004422#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +00004423 key=GetRandomSecretKey(random_info[0]);
glennrpf7659d72012-09-24 18:14:56 +00004424#endif
cristy46ff2672012-12-14 15:32:26 +00004425 random_view=AcquireAuthenticCacheView(random_image,exception);
cristy1b784432009-12-19 02:20:40 +00004426#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +00004427 #pragma omp parallel for schedule(static,4) shared(status) \
cristy5e6b2592012-12-19 14:08:11 +00004428 magick_threads(random_image,random_image,random_image->rows,key == ~0UL)
cristy1b784432009-12-19 02:20:40 +00004429#endif
cristybb503372010-05-27 20:51:26 +00004430 for (y=0; y < (ssize_t) random_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004431 {
cristy5c9e6f22010-09-17 17:31:01 +00004432 const int
4433 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00004434
cristybb503372010-05-27 20:51:26 +00004435 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004436 x;
4437
cristy4c08aed2011-07-01 19:47:50 +00004438 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004439 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004440
cristy1b784432009-12-19 02:20:40 +00004441 if (status == MagickFalse)
4442 continue;
cristy3ed852e2009-09-05 21:47:34 +00004443 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4444 exception);
cristyacd2ed22011-08-30 01:44:23 +00004445 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004446 {
4447 status=MagickFalse;
4448 continue;
4449 }
cristybb503372010-05-27 20:51:26 +00004450 for (x=0; x < (ssize_t) random_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004451 {
cristya19f1d72012-08-07 18:24:38 +00004452 double
cristy76f512e2011-09-12 01:26:56 +00004453 value;
4454
4455 register ssize_t
4456 i;
4457
cristy883fde12013-04-08 00:50:13 +00004458 if (GetPixelReadMask(random_image,q) == 0)
cristy10a6c612012-01-29 21:41:05 +00004459 {
4460 q+=GetPixelChannels(random_image);
4461 continue;
4462 }
cristy76f512e2011-09-12 01:26:56 +00004463 value=GetPseudoRandomValue(random_info[id]);
4464 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4465 {
cristy5a23c552013-02-13 14:34:28 +00004466 PixelChannel channel=GetPixelChannelChannel(image,i);
4467 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004468 if (traits == UndefinedPixelTrait)
4469 continue;
4470 q[i]=ClampToQuantum(QuantumRange*value);
4471 }
cristyed231572011-07-14 02:18:59 +00004472 q+=GetPixelChannels(random_image);
cristy3ed852e2009-09-05 21:47:34 +00004473 }
4474 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4475 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004476 }
4477 random_view=DestroyCacheView(random_view);
cristy1b784432009-12-19 02:20:40 +00004478 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00004479 if (status == MagickFalse)
4480 {
4481 random_image=DestroyImage(random_image);
4482 return(random_image);
4483 }
cristyaa2c16c2012-03-25 22:21:35 +00004484 blur_image=MotionBlurImage(random_image,radius,sigma,angle,exception);
cristy3ed852e2009-09-05 21:47:34 +00004485 random_image=DestroyImage(random_image);
4486 if (blur_image == (Image *) NULL)
4487 return((Image *) NULL);
cristy800446a2013-05-20 20:15:38 +00004488 dodge_image=EdgeImage(blur_image,radius,exception);
cristy3ed852e2009-09-05 21:47:34 +00004489 blur_image=DestroyImage(blur_image);
4490 if (dodge_image == (Image *) NULL)
4491 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +00004492 (void) NormalizeImage(dodge_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +00004493 (void) NegateImage(dodge_image,MagickFalse,exception);
cristye941a752011-10-15 01:52:48 +00004494 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
cristy3ed852e2009-09-05 21:47:34 +00004495 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4496 if (sketch_image == (Image *) NULL)
4497 {
4498 dodge_image=DestroyImage(dodge_image);
4499 return((Image *) NULL);
4500 }
cristyfeb3e962012-03-29 17:25:55 +00004501 (void) CompositeImage(sketch_image,dodge_image,ColorDodgeCompositeOp,
cristy39172402012-03-30 13:04:39 +00004502 MagickTrue,0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004503 dodge_image=DestroyImage(dodge_image);
4504 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4505 if (blend_image == (Image *) NULL)
4506 {
4507 sketch_image=DestroyImage(sketch_image);
4508 return((Image *) NULL);
4509 }
4510 (void) SetImageArtifact(blend_image,"compose:args","20x80");
cristy39172402012-03-30 13:04:39 +00004511 (void) CompositeImage(sketch_image,blend_image,BlendCompositeOp,MagickTrue,
cristyfeb3e962012-03-29 17:25:55 +00004512 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004513 blend_image=DestroyImage(blend_image);
4514 return(sketch_image);
4515}
4516
4517/*
4518%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4519% %
4520% %
4521% %
4522% S o l a r i z e I m a g e %
4523% %
4524% %
4525% %
4526%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4527%
4528% SolarizeImage() applies a special effect to the image, similar to the effect
4529% achieved in a photo darkroom by selectively exposing areas of photo
4530% sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4531% measure of the extent of the solarization.
4532%
4533% The format of the SolarizeImage method is:
4534%
cristy5cbc0162011-08-29 00:36:28 +00004535% MagickBooleanType SolarizeImage(Image *image,const double threshold,
4536% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004537%
4538% A description of each parameter follows:
4539%
4540% o image: the image.
4541%
4542% o threshold: Define the extent of the solarization.
4543%
cristy5cbc0162011-08-29 00:36:28 +00004544% o exception: return any errors or warnings in this structure.
4545%
cristy3ed852e2009-09-05 21:47:34 +00004546*/
4547MagickExport MagickBooleanType SolarizeImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00004548 const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004549{
4550#define SolarizeImageTag "Solarize/Image"
4551
cristyc4c8d132010-01-07 01:58:38 +00004552 CacheView
4553 *image_view;
4554
cristy3ed852e2009-09-05 21:47:34 +00004555 MagickBooleanType
4556 status;
4557
cristybb503372010-05-27 20:51:26 +00004558 MagickOffsetType
4559 progress;
4560
4561 ssize_t
4562 y;
4563
cristy3ed852e2009-09-05 21:47:34 +00004564 assert(image != (Image *) NULL);
4565 assert(image->signature == MagickSignature);
4566 if (image->debug != MagickFalse)
4567 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristya6400b12013-03-15 12:20:18 +00004568 if (IsGrayColorspace(image->colorspace) != MagickFalse)
cristy0c81d062013-04-21 15:22:02 +00004569 (void) SetImageColorspace(image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00004570 if (image->storage_class == PseudoClass)
4571 {
cristybb503372010-05-27 20:51:26 +00004572 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004573 i;
4574
4575 /*
4576 Solarize colormap.
4577 */
cristybb503372010-05-27 20:51:26 +00004578 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00004579 {
cristya19f1d72012-08-07 18:24:38 +00004580 if ((double) image->colormap[i].red > threshold)
cristy6e963d82012-06-19 15:23:24 +00004581 image->colormap[i].red=QuantumRange-image->colormap[i].red;
cristya19f1d72012-08-07 18:24:38 +00004582 if ((double) image->colormap[i].green > threshold)
cristy13ceedd2013-12-02 17:57:23 +00004583 image->colormap[i].green=QuantumRange-image->colormap[i].green;
cristya19f1d72012-08-07 18:24:38 +00004584 if ((double) image->colormap[i].blue > threshold)
cristy13ceedd2013-12-02 17:57:23 +00004585 image->colormap[i].blue=QuantumRange-image->colormap[i].blue;
cristy3ed852e2009-09-05 21:47:34 +00004586 }
4587 }
4588 /*
4589 Solarize image.
4590 */
4591 status=MagickTrue;
4592 progress=0;
cristy46ff2672012-12-14 15:32:26 +00004593 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00004594#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00004595 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00004596 magick_threads(image,image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00004597#endif
cristybb503372010-05-27 20:51:26 +00004598 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004599 {
cristybb503372010-05-27 20:51:26 +00004600 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004601 x;
4602
cristy4c08aed2011-07-01 19:47:50 +00004603 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004604 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004605
4606 if (status == MagickFalse)
4607 continue;
cristy5cbc0162011-08-29 00:36:28 +00004608 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00004609 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004610 {
4611 status=MagickFalse;
4612 continue;
4613 }
cristybb503372010-05-27 20:51:26 +00004614 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004615 {
cristy76f512e2011-09-12 01:26:56 +00004616 register ssize_t
4617 i;
4618
cristy883fde12013-04-08 00:50:13 +00004619 if (GetPixelReadMask(image,q) == 0)
cristy10a6c612012-01-29 21:41:05 +00004620 {
4621 q+=GetPixelChannels(image);
4622 continue;
4623 }
cristy76f512e2011-09-12 01:26:56 +00004624 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4625 {
cristy5a23c552013-02-13 14:34:28 +00004626 PixelChannel channel=GetPixelChannelChannel(image,i);
4627 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004628 if ((traits == UndefinedPixelTrait) ||
4629 ((traits & CopyPixelTrait) != 0))
4630 continue;
cristya19f1d72012-08-07 18:24:38 +00004631 if ((double) q[i] > threshold)
cristy76f512e2011-09-12 01:26:56 +00004632 q[i]=QuantumRange-q[i];
4633 }
cristyed231572011-07-14 02:18:59 +00004634 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004635 }
4636 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4637 status=MagickFalse;
4638 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4639 {
4640 MagickBooleanType
4641 proceed;
4642
cristyb5d5f722009-11-04 03:03:49 +00004643#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004644 #pragma omp critical (MagickCore_SolarizeImage)
cristy3ed852e2009-09-05 21:47:34 +00004645#endif
4646 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4647 if (proceed == MagickFalse)
4648 status=MagickFalse;
4649 }
4650 }
4651 image_view=DestroyCacheView(image_view);
4652 return(status);
4653}
4654
4655/*
4656%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4657% %
4658% %
4659% %
4660% S t e g a n o I m a g e %
4661% %
4662% %
4663% %
4664%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4665%
4666% SteganoImage() hides a digital watermark within the image. Recover
4667% the hidden watermark later to prove that the authenticity of an image.
4668% Offset defines the start position within the image to hide the watermark.
4669%
4670% The format of the SteganoImage method is:
4671%
4672% Image *SteganoImage(const Image *image,Image *watermark,
4673% ExceptionInfo *exception)
4674%
4675% A description of each parameter follows:
4676%
4677% o image: the image.
4678%
4679% o watermark: the watermark image.
4680%
4681% o exception: return any errors or warnings in this structure.
4682%
4683*/
4684MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4685 ExceptionInfo *exception)
4686{
cristye1bf8ad2010-09-19 17:07:03 +00004687#define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
cristy4c08aed2011-07-01 19:47:50 +00004688#define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
cristyeaedf062010-05-29 22:36:02 +00004689 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
cristy3ed852e2009-09-05 21:47:34 +00004690#define SteganoImageTag "Stegano/Image"
4691
cristyb0d3bb92010-09-22 14:37:58 +00004692 CacheView
4693 *stegano_view,
4694 *watermark_view;
4695
cristy3ed852e2009-09-05 21:47:34 +00004696 Image
4697 *stegano_image;
4698
4699 int
4700 c;
4701
cristy3ed852e2009-09-05 21:47:34 +00004702 MagickBooleanType
4703 status;
4704
cristy101ab702011-10-13 13:06:32 +00004705 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004706 pixel;
4707
cristy4c08aed2011-07-01 19:47:50 +00004708 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004709 *q;
4710
cristye1bf8ad2010-09-19 17:07:03 +00004711 register ssize_t
4712 x;
4713
cristybb503372010-05-27 20:51:26 +00004714 size_t
cristyeaedf062010-05-29 22:36:02 +00004715 depth,
4716 one;
cristy3ed852e2009-09-05 21:47:34 +00004717
cristye1bf8ad2010-09-19 17:07:03 +00004718 ssize_t
4719 i,
4720 j,
4721 k,
4722 y;
4723
cristy3ed852e2009-09-05 21:47:34 +00004724 /*
4725 Initialize steganographic image attributes.
4726 */
4727 assert(image != (const Image *) NULL);
4728 assert(image->signature == MagickSignature);
4729 if (image->debug != MagickFalse)
4730 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4731 assert(watermark != (const Image *) NULL);
4732 assert(watermark->signature == MagickSignature);
4733 assert(exception != (ExceptionInfo *) NULL);
4734 assert(exception->signature == MagickSignature);
cristyeaedf062010-05-29 22:36:02 +00004735 one=1UL;
cristy3ed852e2009-09-05 21:47:34 +00004736 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4737 if (stegano_image == (Image *) NULL)
4738 return((Image *) NULL);
cristyf61b1832012-04-01 01:38:19 +00004739 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
cristy574cc262011-08-05 01:23:58 +00004740 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004741 {
cristy3ed852e2009-09-05 21:47:34 +00004742 stegano_image=DestroyImage(stegano_image);
4743 return((Image *) NULL);
4744 }
cristy3ed852e2009-09-05 21:47:34 +00004745 /*
4746 Hide watermark in low-order bits of image.
4747 */
4748 c=0;
4749 i=0;
4750 j=0;
4751 depth=stegano_image->depth;
cristyf61b1832012-04-01 01:38:19 +00004752 k=stegano_image->offset;
cristyda16f162011-02-19 23:52:17 +00004753 status=MagickTrue;
cristy46ff2672012-12-14 15:32:26 +00004754 watermark_view=AcquireVirtualCacheView(watermark,exception);
4755 stegano_view=AcquireAuthenticCacheView(stegano_image,exception);
cristybb503372010-05-27 20:51:26 +00004756 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
cristy3ed852e2009-09-05 21:47:34 +00004757 {
cristybb503372010-05-27 20:51:26 +00004758 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
cristy3ed852e2009-09-05 21:47:34 +00004759 {
cristybb503372010-05-27 20:51:26 +00004760 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
cristy3ed852e2009-09-05 21:47:34 +00004761 {
cristy1707c6c2012-01-18 23:30:54 +00004762 ssize_t
4763 offset;
4764
cristyf05d4942012-03-17 16:26:09 +00004765 (void) GetOneCacheViewVirtualPixelInfo(watermark_view,x,y,&pixel,
cristyda1f9c12011-10-02 21:39:49 +00004766 exception);
cristy1707c6c2012-01-18 23:30:54 +00004767 offset=k/(ssize_t) stegano_image->columns;
4768 if (offset >= (ssize_t) stegano_image->rows)
cristy3ed852e2009-09-05 21:47:34 +00004769 break;
cristyb0d3bb92010-09-22 14:37:58 +00004770 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4771 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4772 exception);
cristyacd2ed22011-08-30 01:44:23 +00004773 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004774 break;
4775 switch (c)
4776 {
4777 case 0:
4778 {
cristyf61b1832012-04-01 01:38:19 +00004779 SetPixelRed(stegano_image,SetBit(GetPixelRed(stegano_image,q),j,
4780 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004781 break;
4782 }
4783 case 1:
4784 {
cristyf61b1832012-04-01 01:38:19 +00004785 SetPixelGreen(stegano_image,SetBit(GetPixelGreen(stegano_image,q),j,
4786 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004787 break;
4788 }
4789 case 2:
4790 {
cristyf61b1832012-04-01 01:38:19 +00004791 SetPixelBlue(stegano_image,SetBit(GetPixelBlue(stegano_image,q),j,
4792 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004793 break;
4794 }
4795 }
cristyb0d3bb92010-09-22 14:37:58 +00004796 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004797 break;
4798 c++;
4799 if (c == 3)
4800 c=0;
4801 k++;
cristybb503372010-05-27 20:51:26 +00004802 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
cristy3ed852e2009-09-05 21:47:34 +00004803 k=0;
cristyf61b1832012-04-01 01:38:19 +00004804 if (k == stegano_image->offset)
cristy3ed852e2009-09-05 21:47:34 +00004805 j++;
4806 }
4807 }
cristy8b27a6d2010-02-14 03:31:15 +00004808 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004809 {
cristy8b27a6d2010-02-14 03:31:15 +00004810 MagickBooleanType
4811 proceed;
4812
4813 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4814 (depth-i),depth);
4815 if (proceed == MagickFalse)
4816 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004817 }
4818 }
cristyb0d3bb92010-09-22 14:37:58 +00004819 stegano_view=DestroyCacheView(stegano_view);
4820 watermark_view=DestroyCacheView(watermark_view);
cristyda16f162011-02-19 23:52:17 +00004821 if (status == MagickFalse)
cristyfab83642012-12-14 00:31:38 +00004822 stegano_image=DestroyImage(stegano_image);
cristy3ed852e2009-09-05 21:47:34 +00004823 return(stegano_image);
4824}
4825
4826/*
4827%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4828% %
4829% %
4830% %
4831% S t e r e o A n a g l y p h I m a g e %
4832% %
4833% %
4834% %
4835%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4836%
4837% StereoAnaglyphImage() combines two images and produces a single image that
4838% is the composite of a left and right image of a stereo pair. Special
4839% red-green stereo glasses are required to view this effect.
4840%
4841% The format of the StereoAnaglyphImage method is:
4842%
4843% Image *StereoImage(const Image *left_image,const Image *right_image,
4844% ExceptionInfo *exception)
4845% Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004846% const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004847% ExceptionInfo *exception)
4848%
4849% A description of each parameter follows:
4850%
4851% o left_image: the left image.
4852%
4853% o right_image: the right image.
4854%
4855% o exception: return any errors or warnings in this structure.
4856%
4857% o x_offset: amount, in pixels, by which the left image is offset to the
4858% right of the right image.
4859%
4860% o y_offset: amount, in pixels, by which the left image is offset to the
4861% bottom of the right image.
4862%
4863%
4864*/
4865MagickExport Image *StereoImage(const Image *left_image,
4866 const Image *right_image,ExceptionInfo *exception)
4867{
4868 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4869}
4870
4871MagickExport Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004872 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004873 ExceptionInfo *exception)
4874{
4875#define StereoImageTag "Stereo/Image"
4876
4877 const Image
4878 *image;
4879
4880 Image
4881 *stereo_image;
4882
cristy3ed852e2009-09-05 21:47:34 +00004883 MagickBooleanType
4884 status;
4885
cristy9d314ff2011-03-09 01:30:28 +00004886 ssize_t
4887 y;
4888
cristy3ed852e2009-09-05 21:47:34 +00004889 assert(left_image != (const Image *) NULL);
4890 assert(left_image->signature == MagickSignature);
4891 if (left_image->debug != MagickFalse)
4892 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4893 left_image->filename);
4894 assert(right_image != (const Image *) NULL);
4895 assert(right_image->signature == MagickSignature);
4896 assert(exception != (ExceptionInfo *) NULL);
4897 assert(exception->signature == MagickSignature);
4898 assert(right_image != (const Image *) NULL);
4899 image=left_image;
4900 if ((left_image->columns != right_image->columns) ||
4901 (left_image->rows != right_image->rows))
4902 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4903 /*
4904 Initialize stereo image attributes.
4905 */
4906 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4907 MagickTrue,exception);
4908 if (stereo_image == (Image *) NULL)
4909 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004910 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004911 {
cristy3ed852e2009-09-05 21:47:34 +00004912 stereo_image=DestroyImage(stereo_image);
4913 return((Image *) NULL);
4914 }
cristy079c78d2012-07-03 11:53:48 +00004915 (void) SetImageColorspace(stereo_image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00004916 /*
4917 Copy left image to red channel and right image to blue channel.
4918 */
cristyda16f162011-02-19 23:52:17 +00004919 status=MagickTrue;
cristybb503372010-05-27 20:51:26 +00004920 for (y=0; y < (ssize_t) stereo_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004921 {
cristy4c08aed2011-07-01 19:47:50 +00004922 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004923 *restrict p,
4924 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004925
cristybb503372010-05-27 20:51:26 +00004926 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004927 x;
4928
cristy4c08aed2011-07-01 19:47:50 +00004929 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004930 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +00004931
4932 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4933 exception);
4934 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4935 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
cristy76f512e2011-09-12 01:26:56 +00004936 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4937 (r == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004938 break;
cristybb503372010-05-27 20:51:26 +00004939 for (x=0; x < (ssize_t) stereo_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004940 {
cristy4c08aed2011-07-01 19:47:50 +00004941 SetPixelRed(image,GetPixelRed(left_image,p),r);
cristy76f512e2011-09-12 01:26:56 +00004942 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4943 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4944 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4945 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4946 GetPixelAlpha(right_image,q))/2,r);
cristyed231572011-07-14 02:18:59 +00004947 p+=GetPixelChannels(left_image);
cristy76f512e2011-09-12 01:26:56 +00004948 q+=GetPixelChannels(right_image);
4949 r+=GetPixelChannels(stereo_image);
cristy3ed852e2009-09-05 21:47:34 +00004950 }
4951 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4952 break;
cristy8b27a6d2010-02-14 03:31:15 +00004953 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004954 {
cristy8b27a6d2010-02-14 03:31:15 +00004955 MagickBooleanType
4956 proceed;
4957
cristybb503372010-05-27 20:51:26 +00004958 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4959 stereo_image->rows);
cristy8b27a6d2010-02-14 03:31:15 +00004960 if (proceed == MagickFalse)
4961 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004962 }
4963 }
cristyda16f162011-02-19 23:52:17 +00004964 if (status == MagickFalse)
cristyfab83642012-12-14 00:31:38 +00004965 stereo_image=DestroyImage(stereo_image);
cristy3ed852e2009-09-05 21:47:34 +00004966 return(stereo_image);
4967}
4968
4969/*
4970%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4971% %
4972% %
4973% %
4974% S w i r l I m a g e %
4975% %
4976% %
4977% %
4978%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4979%
4980% SwirlImage() swirls the pixels about the center of the image, where
4981% degrees indicates the sweep of the arc through which each pixel is moved.
4982% You get a more dramatic effect as the degrees move from 1 to 360.
4983%
4984% The format of the SwirlImage method is:
4985%
4986% Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00004987% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004988%
4989% A description of each parameter follows:
4990%
4991% o image: the image.
4992%
4993% o degrees: Define the tightness of the swirling effect.
4994%
cristy76f512e2011-09-12 01:26:56 +00004995% o method: the pixel interpolation method.
4996%
cristy3ed852e2009-09-05 21:47:34 +00004997% o exception: return any errors or warnings in this structure.
4998%
4999*/
5000MagickExport Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00005001 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005002{
5003#define SwirlImageTag "Swirl/Image"
5004
cristyfa112112010-01-04 17:48:07 +00005005 CacheView
5006 *image_view,
cristy5ee937c2013-04-19 12:55:16 +00005007 *interpolate_view,
cristyfa112112010-01-04 17:48:07 +00005008 *swirl_view;
5009
cristy3ed852e2009-09-05 21:47:34 +00005010 Image
5011 *swirl_image;
5012
cristy3ed852e2009-09-05 21:47:34 +00005013 MagickBooleanType
5014 status;
5015
cristybb503372010-05-27 20:51:26 +00005016 MagickOffsetType
5017 progress;
5018
cristya19f1d72012-08-07 18:24:38 +00005019 double
cristy3ed852e2009-09-05 21:47:34 +00005020 radius;
5021
5022 PointInfo
5023 center,
5024 scale;
5025
cristybb503372010-05-27 20:51:26 +00005026 ssize_t
5027 y;
5028
cristy3ed852e2009-09-05 21:47:34 +00005029 /*
5030 Initialize swirl image attributes.
5031 */
5032 assert(image != (const Image *) NULL);
5033 assert(image->signature == MagickSignature);
5034 if (image->debug != MagickFalse)
5035 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5036 assert(exception != (ExceptionInfo *) NULL);
5037 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00005038 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005039 if (swirl_image == (Image *) NULL)
5040 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005041 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005042 {
cristy3ed852e2009-09-05 21:47:34 +00005043 swirl_image=DestroyImage(swirl_image);
5044 return((Image *) NULL);
5045 }
cristy4c08aed2011-07-01 19:47:50 +00005046 if (swirl_image->background_color.alpha != OpaqueAlpha)
cristy8a46d822012-08-28 23:32:39 +00005047 swirl_image->alpha_trait=BlendPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +00005048 /*
5049 Compute scaling factor.
5050 */
5051 center.x=(double) image->columns/2.0;
5052 center.y=(double) image->rows/2.0;
5053 radius=MagickMax(center.x,center.y);
5054 scale.x=1.0;
5055 scale.y=1.0;
5056 if (image->columns > image->rows)
5057 scale.y=(double) image->columns/(double) image->rows;
5058 else
5059 if (image->columns < image->rows)
5060 scale.x=(double) image->rows/(double) image->columns;
5061 degrees=(double) DegreesToRadians(degrees);
5062 /*
5063 Swirl image.
5064 */
5065 status=MagickTrue;
5066 progress=0;
cristy46ff2672012-12-14 15:32:26 +00005067 image_view=AcquireVirtualCacheView(image,exception);
cristy5ee937c2013-04-19 12:55:16 +00005068 interpolate_view=AcquireVirtualCacheView(image,exception);
cristy46ff2672012-12-14 15:32:26 +00005069 swirl_view=AcquireAuthenticCacheView(swirl_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00005070#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005071 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00005072 magick_threads(image,swirl_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005073#endif
cristybb503372010-05-27 20:51:26 +00005074 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005075 {
cristya19f1d72012-08-07 18:24:38 +00005076 double
cristy3ed852e2009-09-05 21:47:34 +00005077 distance;
5078
5079 PointInfo
5080 delta;
5081
cristy6d188022011-09-12 13:23:33 +00005082 register const Quantum
5083 *restrict p;
5084
cristybb503372010-05-27 20:51:26 +00005085 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005086 x;
5087
cristy4c08aed2011-07-01 19:47:50 +00005088 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005089 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005090
5091 if (status == MagickFalse)
5092 continue;
cristy1665ce12013-04-19 12:18:43 +00005093 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00005094 q=QueueCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00005095 exception);
cristy6d188022011-09-12 13:23:33 +00005096 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005097 {
5098 status=MagickFalse;
5099 continue;
5100 }
cristy3ed852e2009-09-05 21:47:34 +00005101 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00005102 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005103 {
5104 /*
5105 Determine if the pixel is within an ellipse.
5106 */
cristy883fde12013-04-08 00:50:13 +00005107 if (GetPixelReadMask(image,p) == 0)
cristy10a6c612012-01-29 21:41:05 +00005108 {
cristyc3a58022013-10-09 23:22:42 +00005109 SetPixelBackgoundColor(swirl_image,q);
cristy10a6c612012-01-29 21:41:05 +00005110 p+=GetPixelChannels(image);
5111 q+=GetPixelChannels(swirl_image);
5112 continue;
5113 }
cristy3ed852e2009-09-05 21:47:34 +00005114 delta.x=scale.x*(double) (x-center.x);
5115 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00005116 if (distance >= (radius*radius))
5117 {
cristy1707c6c2012-01-18 23:30:54 +00005118 register ssize_t
5119 i;
5120
cristy6d188022011-09-12 13:23:33 +00005121 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy1707c6c2012-01-18 23:30:54 +00005122 {
cristy5a23c552013-02-13 14:34:28 +00005123 PixelChannel channel=GetPixelChannelChannel(image,i);
5124 PixelTrait traits=GetPixelChannelTraits(image,channel);
5125 PixelTrait swirl_traits=GetPixelChannelTraits(swirl_image,channel);
cristy1707c6c2012-01-18 23:30:54 +00005126 if ((traits == UndefinedPixelTrait) ||
5127 (swirl_traits == UndefinedPixelTrait))
5128 continue;
5129 SetPixelChannel(swirl_image,channel,p[i],q);
5130 }
cristy6d188022011-09-12 13:23:33 +00005131 }
5132 else
cristy3ed852e2009-09-05 21:47:34 +00005133 {
cristya19f1d72012-08-07 18:24:38 +00005134 double
cristy3ed852e2009-09-05 21:47:34 +00005135 cosine,
5136 factor,
5137 sine;
5138
5139 /*
5140 Swirl the pixel.
5141 */
5142 factor=1.0-sqrt((double) distance)/radius;
5143 sine=sin((double) (degrees*factor*factor));
5144 cosine=cos((double) (degrees*factor*factor));
cristy5ee937c2013-04-19 12:55:16 +00005145 status=InterpolatePixelChannels(image,interpolate_view,swirl_image,
5146 method,((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
cristy76f512e2011-09-12 01:26:56 +00005147 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00005148 }
cristy6d188022011-09-12 13:23:33 +00005149 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00005150 q+=GetPixelChannels(swirl_image);
cristy3ed852e2009-09-05 21:47:34 +00005151 }
5152 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5153 status=MagickFalse;
5154 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5155 {
5156 MagickBooleanType
5157 proceed;
5158
cristyb5d5f722009-11-04 03:03:49 +00005159#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005160 #pragma omp critical (MagickCore_SwirlImage)
cristy3ed852e2009-09-05 21:47:34 +00005161#endif
5162 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5163 if (proceed == MagickFalse)
5164 status=MagickFalse;
5165 }
5166 }
5167 swirl_view=DestroyCacheView(swirl_view);
cristy5ee937c2013-04-19 12:55:16 +00005168 interpolate_view=DestroyCacheView(interpolate_view);
cristy3ed852e2009-09-05 21:47:34 +00005169 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005170 if (status == MagickFalse)
5171 swirl_image=DestroyImage(swirl_image);
5172 return(swirl_image);
5173}
5174
5175/*
5176%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5177% %
5178% %
5179% %
5180% T i n t I m a g e %
5181% %
5182% %
5183% %
5184%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5185%
5186% TintImage() applies a color vector to each pixel in the image. The length
5187% of the vector is 0 for black and white and at its maximum for the midtones.
5188% The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5189%
5190% The format of the TintImage method is:
5191%
cristyb817c3f2011-10-03 14:00:35 +00005192% Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005193% const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005194%
5195% A description of each parameter follows:
5196%
5197% o image: the image.
5198%
cristyb817c3f2011-10-03 14:00:35 +00005199% o blend: A color value used for tinting.
cristy3ed852e2009-09-05 21:47:34 +00005200%
5201% o tint: A color value used for tinting.
5202%
5203% o exception: return any errors or warnings in this structure.
5204%
5205*/
cristyb817c3f2011-10-03 14:00:35 +00005206MagickExport Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005207 const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005208{
5209#define TintImageTag "Tint/Image"
5210
cristyc4c8d132010-01-07 01:58:38 +00005211 CacheView
5212 *image_view,
5213 *tint_view;
5214
cristyf54150e2013-04-11 01:25:47 +00005215 double
5216 intensity;
5217
cristy3ed852e2009-09-05 21:47:34 +00005218 GeometryInfo
5219 geometry_info;
5220
5221 Image
5222 *tint_image;
5223
cristy3ed852e2009-09-05 21:47:34 +00005224 MagickBooleanType
5225 status;
5226
cristybb503372010-05-27 20:51:26 +00005227 MagickOffsetType
5228 progress;
cristy3ed852e2009-09-05 21:47:34 +00005229
cristy4c08aed2011-07-01 19:47:50 +00005230 PixelInfo
cristy1707c6c2012-01-18 23:30:54 +00005231 color_vector;
cristy3ed852e2009-09-05 21:47:34 +00005232
cristybb503372010-05-27 20:51:26 +00005233 MagickStatusType
5234 flags;
5235
5236 ssize_t
5237 y;
5238
cristy3ed852e2009-09-05 21:47:34 +00005239 /*
5240 Allocate tint image.
5241 */
5242 assert(image != (const Image *) NULL);
5243 assert(image->signature == MagickSignature);
5244 if (image->debug != MagickFalse)
5245 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5246 assert(exception != (ExceptionInfo *) NULL);
5247 assert(exception->signature == MagickSignature);
5248 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5249 if (tint_image == (Image *) NULL)
5250 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005251 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005252 {
cristy3ed852e2009-09-05 21:47:34 +00005253 tint_image=DestroyImage(tint_image);
5254 return((Image *) NULL);
5255 }
cristya6400b12013-03-15 12:20:18 +00005256 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
cristy4dab3802013-03-15 22:08:15 +00005257 (IsPixelInfoGray(tint) == MagickFalse))
cristya6400b12013-03-15 12:20:18 +00005258 (void) SetImageColorspace(tint_image,sRGBColorspace,exception);
cristyaed9c382011-10-03 17:54:21 +00005259 if (blend == (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005260 return(tint_image);
5261 /*
5262 Determine RGB values of the color.
5263 */
cristy1707c6c2012-01-18 23:30:54 +00005264 GetPixelInfo(image,&color_vector);
cristyb817c3f2011-10-03 14:00:35 +00005265 flags=ParseGeometry(blend,&geometry_info);
cristy1707c6c2012-01-18 23:30:54 +00005266 color_vector.red=geometry_info.rho;
5267 color_vector.green=geometry_info.rho;
5268 color_vector.blue=geometry_info.rho;
cristy92ac5722013-03-09 15:09:38 +00005269 color_vector.alpha=(MagickRealType) OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00005270 if ((flags & SigmaValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005271 color_vector.green=geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +00005272 if ((flags & XiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005273 color_vector.blue=geometry_info.xi;
cristyb817c3f2011-10-03 14:00:35 +00005274 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005275 color_vector.alpha=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005276 if (image->colorspace == CMYKColorspace)
5277 {
cristy1707c6c2012-01-18 23:30:54 +00005278 color_vector.black=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005279 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005280 color_vector.black=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005281 if ((flags & ChiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005282 color_vector.alpha=geometry_info.chi;
cristy76f512e2011-09-12 01:26:56 +00005283 }
cristya19f1d72012-08-07 18:24:38 +00005284 intensity=(double) GetPixelInfoIntensity(tint);
cristyf54150e2013-04-11 01:25:47 +00005285 color_vector.red=(double) (color_vector.red*tint->red/100.0-intensity);
5286 color_vector.green=(double) (color_vector.green*tint->green/100.0-intensity);
5287 color_vector.blue=(double) (color_vector.blue*tint->blue/100.0-intensity);
5288 color_vector.black=(double) (color_vector.black*tint->black/100.0-intensity);
5289 color_vector.alpha=(double) (color_vector.alpha*tint->alpha/100.0-intensity);
cristy3ed852e2009-09-05 21:47:34 +00005290 /*
5291 Tint image.
5292 */
5293 status=MagickTrue;
5294 progress=0;
cristy46ff2672012-12-14 15:32:26 +00005295 image_view=AcquireVirtualCacheView(image,exception);
5296 tint_view=AcquireAuthenticCacheView(tint_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00005297#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005298 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00005299 magick_threads(image,tint_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005300#endif
cristybb503372010-05-27 20:51:26 +00005301 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005302 {
cristy4c08aed2011-07-01 19:47:50 +00005303 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00005304 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00005305
cristy4c08aed2011-07-01 19:47:50 +00005306 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005307 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005308
cristy6b91acb2011-04-19 12:23:54 +00005309 register ssize_t
5310 x;
5311
cristy3ed852e2009-09-05 21:47:34 +00005312 if (status == MagickFalse)
5313 continue;
5314 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5315 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5316 exception);
cristy4c08aed2011-07-01 19:47:50 +00005317 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005318 {
5319 status=MagickFalse;
5320 continue;
5321 }
cristybb503372010-05-27 20:51:26 +00005322 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005323 {
cristy4c08aed2011-07-01 19:47:50 +00005324 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00005325 pixel;
5326
cristya19f1d72012-08-07 18:24:38 +00005327 double
cristy3ed852e2009-09-05 21:47:34 +00005328 weight;
5329
cristy1707c6c2012-01-18 23:30:54 +00005330 register ssize_t
5331 i;
5332
5333 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5334 {
cristy5a23c552013-02-13 14:34:28 +00005335 PixelChannel channel=GetPixelChannelChannel(image,i);
5336 PixelTrait traits=GetPixelChannelTraits(image,channel);
5337 PixelTrait tint_traits=GetPixelChannelTraits(tint_image,channel);
cristy1707c6c2012-01-18 23:30:54 +00005338 if ((traits == UndefinedPixelTrait) ||
5339 (tint_traits == UndefinedPixelTrait))
5340 continue;
cristy1eced092012-08-10 23:10:56 +00005341 if (((tint_traits & CopyPixelTrait) != 0) ||
cristy883fde12013-04-08 00:50:13 +00005342 (GetPixelReadMask(image,p) == 0))
cristy1707c6c2012-01-18 23:30:54 +00005343 {
5344 SetPixelChannel(tint_image,channel,p[i],q);
5345 continue;
5346 }
5347 }
5348 GetPixelInfo(image,&pixel);
5349 weight=QuantumScale*GetPixelRed(image,p)-0.5;
cristy1eced092012-08-10 23:10:56 +00005350 pixel.red=(double) GetPixelRed(image,p)+color_vector.red*(1.0-(4.0*
5351 (weight*weight)));
cristy1707c6c2012-01-18 23:30:54 +00005352 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
cristy1eced092012-08-10 23:10:56 +00005353 pixel.green=(double) GetPixelGreen(image,p)+color_vector.green*(1.0-(4.0*
5354 (weight*weight)));
cristy1707c6c2012-01-18 23:30:54 +00005355 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
cristy1eced092012-08-10 23:10:56 +00005356 pixel.blue=(double) GetPixelBlue(image,p)+color_vector.blue*(1.0-(4.0*
5357 (weight*weight)));
cristy1707c6c2012-01-18 23:30:54 +00005358 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
cristy1eced092012-08-10 23:10:56 +00005359 pixel.black=(double) GetPixelBlack(image,p)+color_vector.black*(1.0-(4.0*
5360 (weight*weight)));
cristy1707c6c2012-01-18 23:30:54 +00005361 SetPixelInfoPixel(tint_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00005362 p+=GetPixelChannels(image);
5363 q+=GetPixelChannels(tint_image);
cristy3ed852e2009-09-05 21:47:34 +00005364 }
5365 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5366 status=MagickFalse;
5367 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5368 {
5369 MagickBooleanType
5370 proceed;
5371
cristyb5d5f722009-11-04 03:03:49 +00005372#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005373 #pragma omp critical (MagickCore_TintImage)
cristy3ed852e2009-09-05 21:47:34 +00005374#endif
5375 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5376 if (proceed == MagickFalse)
5377 status=MagickFalse;
5378 }
5379 }
5380 tint_view=DestroyCacheView(tint_view);
5381 image_view=DestroyCacheView(image_view);
5382 if (status == MagickFalse)
5383 tint_image=DestroyImage(tint_image);
5384 return(tint_image);
5385}
5386
5387/*
5388%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5389% %
5390% %
5391% %
5392% V i g n e t t e I m a g e %
5393% %
5394% %
5395% %
5396%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5397%
5398% VignetteImage() softens the edges of the image in vignette style.
5399%
5400% The format of the VignetteImage method is:
5401%
5402% Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005403% const double sigma,const ssize_t x,const ssize_t y,
cristy05c0c9a2011-09-05 23:16:13 +00005404% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005405%
5406% A description of each parameter follows:
5407%
5408% o image: the image.
5409%
5410% o radius: the radius of the pixel neighborhood.
5411%
5412% o sigma: the standard deviation of the Gaussian, in pixels.
5413%
5414% o x, y: Define the x and y ellipse offset.
5415%
5416% o exception: return any errors or warnings in this structure.
5417%
5418*/
5419MagickExport Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005420 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005421{
5422 char
5423 ellipse[MaxTextExtent];
5424
5425 DrawInfo
5426 *draw_info;
5427
5428 Image
5429 *canvas_image,
5430 *blur_image,
5431 *oval_image,
5432 *vignette_image;
5433
5434 assert(image != (Image *) NULL);
5435 assert(image->signature == MagickSignature);
5436 if (image->debug != MagickFalse)
5437 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5438 assert(exception != (ExceptionInfo *) NULL);
5439 assert(exception->signature == MagickSignature);
5440 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5441 if (canvas_image == (Image *) NULL)
5442 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005443 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005444 {
cristy3ed852e2009-09-05 21:47:34 +00005445 canvas_image=DestroyImage(canvas_image);
5446 return((Image *) NULL);
5447 }
cristy8a46d822012-08-28 23:32:39 +00005448 canvas_image->alpha_trait=BlendPixelTrait;
cristy98621462011-12-31 22:31:11 +00005449 oval_image=CloneImage(canvas_image,canvas_image->columns,canvas_image->rows,
5450 MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005451 if (oval_image == (Image *) NULL)
5452 {
5453 canvas_image=DestroyImage(canvas_image);
5454 return((Image *) NULL);
5455 }
cristy9950d572011-10-01 18:22:35 +00005456 (void) QueryColorCompliance("#000000",AllCompliance,
5457 &oval_image->background_color,exception);
cristyea1a8aa2011-10-20 13:24:06 +00005458 (void) SetImageBackgroundColor(oval_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00005459 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
cristy9950d572011-10-01 18:22:35 +00005460 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5461 exception);
5462 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5463 exception);
cristy1707c6c2012-01-18 23:30:54 +00005464 (void) FormatLocaleString(ellipse,MaxTextExtent,"ellipse %g,%g,%g,%g,"
5465 "0.0,360.0",image->columns/2.0,image->rows/2.0,image->columns/2.0-x,
5466 image->rows/2.0-y);
cristy3ed852e2009-09-05 21:47:34 +00005467 draw_info->primitive=AcquireString(ellipse);
cristy018f07f2011-09-04 21:15:19 +00005468 (void) DrawImage(oval_image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00005469 draw_info=DestroyDrawInfo(draw_info);
cristyaa2c16c2012-03-25 22:21:35 +00005470 blur_image=BlurImage(oval_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00005471 oval_image=DestroyImage(oval_image);
5472 if (blur_image == (Image *) NULL)
5473 {
5474 canvas_image=DestroyImage(canvas_image);
5475 return((Image *) NULL);
5476 }
cristy8a46d822012-08-28 23:32:39 +00005477 blur_image->alpha_trait=UndefinedPixelTrait;
cristy39172402012-03-30 13:04:39 +00005478 (void) CompositeImage(canvas_image,blur_image,IntensityCompositeOp,MagickTrue,
5479 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00005480 blur_image=DestroyImage(blur_image);
5481 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5482 canvas_image=DestroyImage(canvas_image);
cristy66d26122012-06-23 21:56:40 +00005483 if (vignette_image != (Image *) NULL)
5484 (void) TransformImageColorspace(vignette_image,image->colorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00005485 return(vignette_image);
5486}
5487
5488/*
5489%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5490% %
5491% %
5492% %
5493% W a v e I m a g e %
5494% %
5495% %
5496% %
5497%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5498%
5499% WaveImage() creates a "ripple" effect in the image by shifting the pixels
cristycee97112010-05-28 00:44:52 +00005500% vertically along a sine wave whose amplitude and wavelength is specified
cristy3ed852e2009-09-05 21:47:34 +00005501% by the given parameters.
5502%
5503% The format of the WaveImage method is:
5504%
5505% Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005506% const double wave_length,const PixelInterpolateMethod method,
5507% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005508%
5509% A description of each parameter follows:
5510%
5511% o image: the image.
5512%
5513% o amplitude, wave_length: Define the amplitude and wave length of the
5514% sine wave.
5515%
cristy5c4e2582011-09-11 19:21:03 +00005516% o interpolate: the pixel interpolation method.
5517%
cristy3ed852e2009-09-05 21:47:34 +00005518% o exception: return any errors or warnings in this structure.
5519%
5520*/
5521MagickExport Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005522 const double wave_length,const PixelInterpolateMethod method,
5523 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005524{
5525#define WaveImageTag "Wave/Image"
5526
cristyfa112112010-01-04 17:48:07 +00005527 CacheView
cristyd76c51e2011-03-26 00:21:26 +00005528 *image_view,
cristyfa112112010-01-04 17:48:07 +00005529 *wave_view;
5530
cristy3ed852e2009-09-05 21:47:34 +00005531 Image
5532 *wave_image;
5533
cristy3ed852e2009-09-05 21:47:34 +00005534 MagickBooleanType
5535 status;
5536
cristybb503372010-05-27 20:51:26 +00005537 MagickOffsetType
5538 progress;
5539
cristya19f1d72012-08-07 18:24:38 +00005540 double
cristy3ed852e2009-09-05 21:47:34 +00005541 *sine_map;
5542
cristybb503372010-05-27 20:51:26 +00005543 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005544 i;
5545
cristybb503372010-05-27 20:51:26 +00005546 ssize_t
5547 y;
5548
cristy3ed852e2009-09-05 21:47:34 +00005549 /*
5550 Initialize wave image attributes.
5551 */
5552 assert(image != (Image *) NULL);
5553 assert(image->signature == MagickSignature);
5554 if (image->debug != MagickFalse)
5555 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5556 assert(exception != (ExceptionInfo *) NULL);
5557 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00005558 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
cristy3ed852e2009-09-05 21:47:34 +00005559 fabs(amplitude)),MagickTrue,exception);
5560 if (wave_image == (Image *) NULL)
5561 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005562 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005563 {
cristy3ed852e2009-09-05 21:47:34 +00005564 wave_image=DestroyImage(wave_image);
5565 return((Image *) NULL);
5566 }
cristy4c08aed2011-07-01 19:47:50 +00005567 if (wave_image->background_color.alpha != OpaqueAlpha)
cristy8a46d822012-08-28 23:32:39 +00005568 wave_image->alpha_trait=BlendPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +00005569 /*
5570 Allocate sine map.
5571 */
cristya19f1d72012-08-07 18:24:38 +00005572 sine_map=(double *) AcquireQuantumMemory((size_t) wave_image->columns,
cristy3ed852e2009-09-05 21:47:34 +00005573 sizeof(*sine_map));
cristya19f1d72012-08-07 18:24:38 +00005574 if (sine_map == (double *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005575 {
5576 wave_image=DestroyImage(wave_image);
5577 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5578 }
cristybb503372010-05-27 20:51:26 +00005579 for (i=0; i < (ssize_t) wave_image->columns; i++)
cristy4205a3c2010-09-12 20:19:59 +00005580 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5581 wave_length));
cristy3ed852e2009-09-05 21:47:34 +00005582 /*
5583 Wave image.
5584 */
5585 status=MagickTrue;
5586 progress=0;
cristy46ff2672012-12-14 15:32:26 +00005587 image_view=AcquireVirtualCacheView(image,exception);
5588 wave_view=AcquireAuthenticCacheView(wave_image,exception);
cristyd76c51e2011-03-26 00:21:26 +00005589 (void) SetCacheViewVirtualPixelMethod(image_view,
5590 BackgroundVirtualPixelMethod);
cristyb5d5f722009-11-04 03:03:49 +00005591#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005592 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00005593 magick_threads(image,wave_image,wave_image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005594#endif
cristybb503372010-05-27 20:51:26 +00005595 for (y=0; y < (ssize_t) wave_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005596 {
cristy4c08aed2011-07-01 19:47:50 +00005597 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005598 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005599
cristye97bb922011-04-03 01:36:52 +00005600 register ssize_t
5601 x;
5602
cristy3ed852e2009-09-05 21:47:34 +00005603 if (status == MagickFalse)
5604 continue;
5605 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5606 exception);
cristyacd2ed22011-08-30 01:44:23 +00005607 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005608 {
5609 status=MagickFalse;
5610 continue;
5611 }
cristybb503372010-05-27 20:51:26 +00005612 for (x=0; x < (ssize_t) wave_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005613 {
cristy5c4e2582011-09-11 19:21:03 +00005614 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5615 (double) x,(double) (y-sine_map[x]),q,exception);
cristyed231572011-07-14 02:18:59 +00005616 q+=GetPixelChannels(wave_image);
cristy3ed852e2009-09-05 21:47:34 +00005617 }
5618 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5619 status=MagickFalse;
5620 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5621 {
5622 MagickBooleanType
5623 proceed;
5624
cristyb5d5f722009-11-04 03:03:49 +00005625#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005626 #pragma omp critical (MagickCore_WaveImage)
cristy3ed852e2009-09-05 21:47:34 +00005627#endif
5628 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5629 if (proceed == MagickFalse)
5630 status=MagickFalse;
5631 }
5632 }
5633 wave_view=DestroyCacheView(wave_view);
cristyd76c51e2011-03-26 00:21:26 +00005634 image_view=DestroyCacheView(image_view);
cristya19f1d72012-08-07 18:24:38 +00005635 sine_map=(double *) RelinquishMagickMemory(sine_map);
cristy3ed852e2009-09-05 21:47:34 +00005636 if (status == MagickFalse)
5637 wave_image=DestroyImage(wave_image);
5638 return(wave_image);
5639}