blob: fc5e64dc84bfa1f30c7bb95c80fedb9064e308b6 [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 %
16% John Cristy %
17% October 1996 %
18% %
19% %
cristy45ef08f2012-12-07 13:13:34 +000020% Copyright 1999-2013 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"
49#include "MagickCore/color.h"
50#include "MagickCore/color-private.h"
cristy9b7a4fc2012-04-08 22:26:56 +000051#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000052#include "MagickCore/composite.h"
53#include "MagickCore/decorate.h"
cristyc53413d2011-11-17 13:04:26 +000054#include "MagickCore/distort.h"
cristy4c08aed2011-07-01 19:47:50 +000055#include "MagickCore/draw.h"
56#include "MagickCore/effect.h"
57#include "MagickCore/enhance.h"
58#include "MagickCore/exception.h"
59#include "MagickCore/exception-private.h"
60#include "MagickCore/fx.h"
61#include "MagickCore/fx-private.h"
62#include "MagickCore/gem.h"
cristy8ea81222011-09-04 10:33:32 +000063#include "MagickCore/gem-private.h"
cristy4c08aed2011-07-01 19:47:50 +000064#include "MagickCore/geometry.h"
65#include "MagickCore/layer.h"
66#include "MagickCore/list.h"
67#include "MagickCore/log.h"
68#include "MagickCore/image.h"
69#include "MagickCore/image-private.h"
70#include "MagickCore/magick.h"
71#include "MagickCore/memory_.h"
72#include "MagickCore/monitor.h"
73#include "MagickCore/monitor-private.h"
74#include "MagickCore/option.h"
75#include "MagickCore/pixel.h"
76#include "MagickCore/pixel-accessor.h"
77#include "MagickCore/property.h"
78#include "MagickCore/quantum.h"
79#include "MagickCore/quantum-private.h"
80#include "MagickCore/random_.h"
81#include "MagickCore/random-private.h"
82#include "MagickCore/resample.h"
83#include "MagickCore/resample-private.h"
84#include "MagickCore/resize.h"
cristy57340e02012-05-05 00:53:23 +000085#include "MagickCore/resource_.h"
cristy4c08aed2011-07-01 19:47:50 +000086#include "MagickCore/splay-tree.h"
87#include "MagickCore/statistic.h"
88#include "MagickCore/string_.h"
89#include "MagickCore/string-private.h"
90#include "MagickCore/thread-private.h"
91#include "MagickCore/transform.h"
92#include "MagickCore/utility.h"
cristy3ed852e2009-09-05 21:47:34 +000093
94/*
95 Define declarations.
96*/
97#define LeftShiftOperator 0xf5
98#define RightShiftOperator 0xf6
99#define LessThanEqualOperator 0xf7
100#define GreaterThanEqualOperator 0xf8
101#define EqualOperator 0xf9
102#define NotEqualOperator 0xfa
103#define LogicalAndOperator 0xfb
104#define LogicalOrOperator 0xfc
cristy116af162010-08-13 01:25:47 +0000105#define ExponentialNotation 0xfd
cristy3ed852e2009-09-05 21:47:34 +0000106
107struct _FxInfo
108{
109 const Image
110 *images;
111
cristy3ed852e2009-09-05 21:47:34 +0000112 char
113 *expression;
114
115 FILE
116 *file;
117
118 SplayTreeInfo
119 *colors,
120 *symbols;
121
cristyd76c51e2011-03-26 00:21:26 +0000122 CacheView
123 **view;
cristy3ed852e2009-09-05 21:47:34 +0000124
125 RandomInfo
126 *random_info;
127
128 ExceptionInfo
129 *exception;
130};
131
132/*
133%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
134% %
135% %
136% %
137+ A c q u i r e F x I n f o %
138% %
139% %
140% %
141%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142%
143% AcquireFxInfo() allocates the FxInfo structure.
144%
145% The format of the AcquireFxInfo method is:
146%
cristydb070952012-04-20 14:33:00 +0000147% FxInfo *AcquireFxInfo(Image *image,const char *expression,
148% ExceptionInfo *exception)
cristy0a9b3722010-10-23 18:45:49 +0000149%
cristy3ed852e2009-09-05 21:47:34 +0000150% A description of each parameter follows:
151%
152% o image: the image.
153%
154% o expression: the expression.
155%
cristydb070952012-04-20 14:33:00 +0000156% o exception: return any errors or warnings in this structure.
157%
cristy3ed852e2009-09-05 21:47:34 +0000158*/
cristydb070952012-04-20 14:33:00 +0000159MagickPrivate FxInfo *AcquireFxInfo(const Image *image,const char *expression,
160 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000161{
162 char
163 fx_op[2];
164
cristycb180922011-03-11 14:41:24 +0000165 const Image
166 *next;
167
cristy3ed852e2009-09-05 21:47:34 +0000168 FxInfo
169 *fx_info;
170
cristybb503372010-05-27 20:51:26 +0000171 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000172 i;
173
cristy73bd4a52010-10-05 11:24:23 +0000174 fx_info=(FxInfo *) AcquireMagickMemory(sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +0000175 if (fx_info == (FxInfo *) NULL)
176 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
177 (void) ResetMagickMemory(fx_info,0,sizeof(*fx_info));
178 fx_info->exception=AcquireExceptionInfo();
anthony7d86e172011-03-23 12:37:06 +0000179 fx_info->images=image;
cristy3ed852e2009-09-05 21:47:34 +0000180 fx_info->colors=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
181 RelinquishMagickMemory);
182 fx_info->symbols=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
183 RelinquishMagickMemory);
cristyd76c51e2011-03-26 00:21:26 +0000184 fx_info->view=(CacheView **) AcquireQuantumMemory(GetImageListLength(
185 fx_info->images),sizeof(*fx_info->view));
186 if (fx_info->view == (CacheView **) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000187 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
cristya2262262011-03-11 02:50:37 +0000188 i=0;
cristy0ea377f2011-03-24 00:54:19 +0000189 next=GetFirstImageInList(fx_info->images);
190 for ( ; next != (Image *) NULL; next=next->next)
cristy3ed852e2009-09-05 21:47:34 +0000191 {
cristy46ff2672012-12-14 15:32:26 +0000192 fx_info->view[i]=AcquireVirtualCacheView(next,exception);
cristya2262262011-03-11 02:50:37 +0000193 i++;
cristy3ed852e2009-09-05 21:47:34 +0000194 }
195 fx_info->random_info=AcquireRandomInfo();
196 fx_info->expression=ConstantString(expression);
197 fx_info->file=stderr;
198 (void) SubstituteString(&fx_info->expression," ",""); /* compact string */
cristy37af0912011-05-23 16:09:42 +0000199 /*
200 Force right-to-left associativity for unary negation.
201 */
202 (void) SubstituteString(&fx_info->expression,"-","-1.0*");
cristy422d5502012-12-22 22:20:57 +0000203 (void) SubstituteString(&fx_info->expression,"^-1.0*","^-");
cristy8fbb96d2012-07-18 01:18:52 +0000204 (void) SubstituteString(&fx_info->expression,"E-1.0*","E-");
205 (void) SubstituteString(&fx_info->expression,"e-1.0*","e-");
cristy8b8a3ae2010-10-23 18:49:46 +0000206 /*
cristy3ed852e2009-09-05 21:47:34 +0000207 Convert complex to simple operators.
208 */
209 fx_op[1]='\0';
210 *fx_op=(char) LeftShiftOperator;
211 (void) SubstituteString(&fx_info->expression,"<<",fx_op);
212 *fx_op=(char) RightShiftOperator;
213 (void) SubstituteString(&fx_info->expression,">>",fx_op);
214 *fx_op=(char) LessThanEqualOperator;
215 (void) SubstituteString(&fx_info->expression,"<=",fx_op);
216 *fx_op=(char) GreaterThanEqualOperator;
217 (void) SubstituteString(&fx_info->expression,">=",fx_op);
218 *fx_op=(char) EqualOperator;
219 (void) SubstituteString(&fx_info->expression,"==",fx_op);
220 *fx_op=(char) NotEqualOperator;
221 (void) SubstituteString(&fx_info->expression,"!=",fx_op);
222 *fx_op=(char) LogicalAndOperator;
223 (void) SubstituteString(&fx_info->expression,"&&",fx_op);
224 *fx_op=(char) LogicalOrOperator;
225 (void) SubstituteString(&fx_info->expression,"||",fx_op);
cristy116af162010-08-13 01:25:47 +0000226 *fx_op=(char) ExponentialNotation;
227 (void) SubstituteString(&fx_info->expression,"**",fx_op);
cristy3ed852e2009-09-05 21:47:34 +0000228 return(fx_info);
229}
230
231/*
232%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233% %
234% %
235% %
236% A d d N o i s e I m a g e %
237% %
238% %
239% %
240%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241%
242% AddNoiseImage() adds random noise to the image.
243%
244% The format of the AddNoiseImage method is:
245%
246% Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
cristy9ed1f812011-10-08 02:00:08 +0000247% const double attenuate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000248%
249% A description of each parameter follows:
250%
251% o image: the image.
252%
253% o channel: the channel type.
254%
255% o noise_type: The type of noise: Uniform, Gaussian, Multiplicative,
256% Impulse, Laplacian, or Poisson.
257%
cristy9ed1f812011-10-08 02:00:08 +0000258% o attenuate: attenuate the random distribution.
259%
cristy3ed852e2009-09-05 21:47:34 +0000260% o exception: return any errors or warnings in this structure.
261%
262*/
cristy9ed1f812011-10-08 02:00:08 +0000263MagickExport Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
264 const double attenuate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000265{
266#define AddNoiseImageTag "AddNoise/Image"
267
cristyfa112112010-01-04 17:48:07 +0000268 CacheView
269 *image_view,
270 *noise_view;
271
cristy3ed852e2009-09-05 21:47:34 +0000272 Image
273 *noise_image;
274
cristy3ed852e2009-09-05 21:47:34 +0000275 MagickBooleanType
276 status;
277
cristybb503372010-05-27 20:51:26 +0000278 MagickOffsetType
279 progress;
280
cristy3ed852e2009-09-05 21:47:34 +0000281 RandomInfo
cristyfa112112010-01-04 17:48:07 +0000282 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +0000283
cristybb503372010-05-27 20:51:26 +0000284 ssize_t
285 y;
286
glennrpf7659d72012-09-24 18:14:56 +0000287#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +0000288 unsigned long
289 key;
glennrpf7659d72012-09-24 18:14:56 +0000290#endif
cristy57340e02012-05-05 00:53:23 +0000291
cristy3ed852e2009-09-05 21:47:34 +0000292 /*
293 Initialize noise image attributes.
294 */
295 assert(image != (const Image *) NULL);
296 assert(image->signature == MagickSignature);
297 if (image->debug != MagickFalse)
298 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
299 assert(exception != (ExceptionInfo *) NULL);
300 assert(exception->signature == MagickSignature);
cristyb2145892011-10-10 00:55:32 +0000301 noise_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000302 if (noise_image == (Image *) NULL)
303 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000304 if (SetImageStorageClass(noise_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000305 {
cristy3ed852e2009-09-05 21:47:34 +0000306 noise_image=DestroyImage(noise_image);
307 return((Image *) NULL);
308 }
309 /*
310 Add noise in each row.
311 */
cristy3ed852e2009-09-05 21:47:34 +0000312 status=MagickTrue;
313 progress=0;
314 random_info=AcquireRandomInfoThreadSet();
cristy46ff2672012-12-14 15:32:26 +0000315 image_view=AcquireVirtualCacheView(image,exception);
316 noise_view=AcquireAuthenticCacheView(noise_image,exception);
cristy319a1e72010-02-21 15:13:11 +0000317#if defined(MAGICKCORE_OPENMP_SUPPORT)
glennrpf7659d72012-09-24 18:14:56 +0000318 key=GetRandomSecretKey(random_info[0]);
cristy57340e02012-05-05 00:53:23 +0000319 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +0000320 magick_threads(image,noise_image,image->rows,key == ~0UL)
cristy3ed852e2009-09-05 21:47:34 +0000321#endif
cristybb503372010-05-27 20:51:26 +0000322 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000323 {
cristy5c9e6f22010-09-17 17:31:01 +0000324 const int
325 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +0000326
cristy3ed852e2009-09-05 21:47:34 +0000327 MagickBooleanType
328 sync;
329
cristy4c08aed2011-07-01 19:47:50 +0000330 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000331 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000332
cristybb503372010-05-27 20:51:26 +0000333 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000334 x;
335
cristy4c08aed2011-07-01 19:47:50 +0000336 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000337 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000338
339 if (status == MagickFalse)
340 continue;
341 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +0000342 q=QueueCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +0000343 exception);
cristy4c08aed2011-07-01 19:47:50 +0000344 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000345 {
346 status=MagickFalse;
347 continue;
348 }
cristybb503372010-05-27 20:51:26 +0000349 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000350 {
cristy850b3072011-10-08 01:38:05 +0000351 register ssize_t
352 i;
353
354 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
355 {
cristy5a23c552013-02-13 14:34:28 +0000356 PixelChannel channel=GetPixelChannelChannel(image,i);
357 PixelTrait traits=GetPixelChannelTraits(image,channel);
358 PixelTrait noise_traits=GetPixelChannelTraits(noise_image,channel);
cristy850b3072011-10-08 01:38:05 +0000359 if ((traits == UndefinedPixelTrait) ||
360 (noise_traits == UndefinedPixelTrait))
361 continue;
cristy1eced092012-08-10 23:10:56 +0000362 if (((noise_traits & CopyPixelTrait) != 0) ||
cristy883fde12013-04-08 00:50:13 +0000363 (GetPixelReadMask(image,p) == 0))
cristyb2145892011-10-10 00:55:32 +0000364 {
365 SetPixelChannel(noise_image,channel,p[i],q);
366 continue;
367 }
cristy850b3072011-10-08 01:38:05 +0000368 SetPixelChannel(noise_image,channel,ClampToQuantum(
369 GenerateDifferentialNoise(random_info[id],p[i],noise_type,attenuate)),
370 q);
371 }
cristyed231572011-07-14 02:18:59 +0000372 p+=GetPixelChannels(image);
373 q+=GetPixelChannels(noise_image);
cristy3ed852e2009-09-05 21:47:34 +0000374 }
375 sync=SyncCacheViewAuthenticPixels(noise_view,exception);
376 if (sync == MagickFalse)
377 status=MagickFalse;
378 if (image->progress_monitor != (MagickProgressMonitor) NULL)
379 {
380 MagickBooleanType
381 proceed;
382
cristyb5d5f722009-11-04 03:03:49 +0000383#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy319a1e72010-02-21 15:13:11 +0000384 #pragma omp critical (MagickCore_AddNoiseImage)
cristy3ed852e2009-09-05 21:47:34 +0000385#endif
386 proceed=SetImageProgress(image,AddNoiseImageTag,progress++,
387 image->rows);
388 if (proceed == MagickFalse)
389 status=MagickFalse;
390 }
391 }
392 noise_view=DestroyCacheView(noise_view);
393 image_view=DestroyCacheView(image_view);
394 random_info=DestroyRandomInfoThreadSet(random_info);
395 if (status == MagickFalse)
396 noise_image=DestroyImage(noise_image);
397 return(noise_image);
398}
399
400/*
401%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
402% %
403% %
404% %
405% B l u e S h i f t I m a g e %
406% %
407% %
408% %
409%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
410%
411% BlueShiftImage() mutes the colors of the image to simulate a scene at
412% nighttime in the moonlight.
413%
414% The format of the BlueShiftImage method is:
415%
416% Image *BlueShiftImage(const Image *image,const double factor,
417% ExceptionInfo *exception)
418%
419% A description of each parameter follows:
420%
421% o image: the image.
422%
423% o factor: the shift factor.
424%
425% o exception: return any errors or warnings in this structure.
426%
427*/
428MagickExport Image *BlueShiftImage(const Image *image,const double factor,
429 ExceptionInfo *exception)
430{
431#define BlueShiftImageTag "BlueShift/Image"
432
cristyc4c8d132010-01-07 01:58:38 +0000433 CacheView
434 *image_view,
435 *shift_view;
436
cristy3ed852e2009-09-05 21:47:34 +0000437 Image
438 *shift_image;
439
cristy3ed852e2009-09-05 21:47:34 +0000440 MagickBooleanType
441 status;
442
cristybb503372010-05-27 20:51:26 +0000443 MagickOffsetType
444 progress;
445
446 ssize_t
447 y;
448
cristy3ed852e2009-09-05 21:47:34 +0000449 /*
450 Allocate blue shift image.
451 */
452 assert(image != (const Image *) NULL);
453 assert(image->signature == MagickSignature);
454 if (image->debug != MagickFalse)
455 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
456 assert(exception != (ExceptionInfo *) NULL);
457 assert(exception->signature == MagickSignature);
cristya6d7a9b2012-01-18 20:04:48 +0000458 shift_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000459 if (shift_image == (Image *) NULL)
460 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000461 if (SetImageStorageClass(shift_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000462 {
cristy3ed852e2009-09-05 21:47:34 +0000463 shift_image=DestroyImage(shift_image);
464 return((Image *) NULL);
465 }
466 /*
467 Blue-shift DirectClass image.
468 */
469 status=MagickTrue;
470 progress=0;
cristy46ff2672012-12-14 15:32:26 +0000471 image_view=AcquireVirtualCacheView(image,exception);
472 shift_view=AcquireAuthenticCacheView(shift_image,exception);
cristy319a1e72010-02-21 15:13:11 +0000473#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000474 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +0000475 magick_threads(image,shift_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000476#endif
cristybb503372010-05-27 20:51:26 +0000477 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000478 {
479 MagickBooleanType
480 sync;
481
cristy4c08aed2011-07-01 19:47:50 +0000482 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000483 pixel;
484
485 Quantum
486 quantum;
487
cristy4c08aed2011-07-01 19:47:50 +0000488 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000489 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000490
cristybb503372010-05-27 20:51:26 +0000491 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000492 x;
493
cristy4c08aed2011-07-01 19:47:50 +0000494 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000495 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000496
497 if (status == MagickFalse)
498 continue;
499 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
500 q=QueueCacheViewAuthenticPixels(shift_view,0,y,shift_image->columns,1,
501 exception);
cristy4c08aed2011-07-01 19:47:50 +0000502 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000503 {
504 status=MagickFalse;
505 continue;
506 }
cristybb503372010-05-27 20:51:26 +0000507 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000508 {
cristy4c08aed2011-07-01 19:47:50 +0000509 quantum=GetPixelRed(image,p);
510 if (GetPixelGreen(image,p) < quantum)
511 quantum=GetPixelGreen(image,p);
512 if (GetPixelBlue(image,p) < quantum)
513 quantum=GetPixelBlue(image,p);
514 pixel.red=0.5*(GetPixelRed(image,p)+factor*quantum);
515 pixel.green=0.5*(GetPixelGreen(image,p)+factor*quantum);
516 pixel.blue=0.5*(GetPixelBlue(image,p)+factor*quantum);
517 quantum=GetPixelRed(image,p);
518 if (GetPixelGreen(image,p) > quantum)
519 quantum=GetPixelGreen(image,p);
520 if (GetPixelBlue(image,p) > quantum)
521 quantum=GetPixelBlue(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000522 pixel.red=0.5*(pixel.red+factor*quantum);
523 pixel.green=0.5*(pixel.green+factor*quantum);
524 pixel.blue=0.5*(pixel.blue+factor*quantum);
cristy4c08aed2011-07-01 19:47:50 +0000525 SetPixelRed(shift_image,ClampToQuantum(pixel.red),q);
526 SetPixelGreen(shift_image,ClampToQuantum(pixel.green),q);
527 SetPixelBlue(shift_image,ClampToQuantum(pixel.blue),q);
cristyed231572011-07-14 02:18:59 +0000528 p+=GetPixelChannels(image);
529 q+=GetPixelChannels(shift_image);
cristy3ed852e2009-09-05 21:47:34 +0000530 }
531 sync=SyncCacheViewAuthenticPixels(shift_view,exception);
532 if (sync == MagickFalse)
533 status=MagickFalse;
534 if (image->progress_monitor != (MagickProgressMonitor) NULL)
535 {
536 MagickBooleanType
537 proceed;
538
cristy319a1e72010-02-21 15:13:11 +0000539#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +0000540 #pragma omp critical (MagickCore_BlueShiftImage)
cristy3ed852e2009-09-05 21:47:34 +0000541#endif
542 proceed=SetImageProgress(image,BlueShiftImageTag,progress++,
543 image->rows);
544 if (proceed == MagickFalse)
545 status=MagickFalse;
546 }
547 }
548 image_view=DestroyCacheView(image_view);
549 shift_view=DestroyCacheView(shift_view);
550 if (status == MagickFalse)
551 shift_image=DestroyImage(shift_image);
552 return(shift_image);
553}
554
555/*
556%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
557% %
558% %
559% %
560% C h a r c o a l I m a g e %
561% %
562% %
563% %
564%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
565%
566% CharcoalImage() creates a new image that is a copy of an existing one with
567% the edge highlighted. It allocates the memory necessary for the new Image
568% structure and returns a pointer to the new image.
569%
570% The format of the CharcoalImage method is:
571%
572% Image *CharcoalImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +0000573% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000574%
575% A description of each parameter follows:
576%
577% o image: the image.
578%
579% o radius: the radius of the pixel neighborhood.
580%
581% o sigma: the standard deviation of the Gaussian, in pixels.
582%
583% o exception: return any errors or warnings in this structure.
584%
585*/
586MagickExport Image *CharcoalImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +0000587 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000588{
589 Image
590 *charcoal_image,
591 *clone_image,
592 *edge_image;
593
594 assert(image != (Image *) NULL);
595 assert(image->signature == MagickSignature);
596 if (image->debug != MagickFalse)
597 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
598 assert(exception != (ExceptionInfo *) NULL);
599 assert(exception->signature == MagickSignature);
600 clone_image=CloneImage(image,0,0,MagickTrue,exception);
601 if (clone_image == (Image *) NULL)
602 return((Image *) NULL);
cristya31d1a02013-03-25 14:38:56 +0000603 edge_image=EdgeImage(clone_image,GetOptimalKernelWidth1D(radius,1.0),
cristy8a1278a2013-03-24 13:33:29 +0000604 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 }
cristya6400b12013-03-15 12:20:18 +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;
1312 if (isalpha((int) *(p+1)) == 0)
1313 {
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 }
1354 if ((isalpha((int) *(p+1)) == 0) && (*p == 'p'))
1355 {
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) &&
1425 (LocaleCompare(p,"luminance") != 0) && (LocaleCompare(p,"hue") != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00001426 (LocaleCompare(p,"saturation") != 0) &&
1427 (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 }
1613 return(0.0);
1614 }
1615 if (LocaleCompare(symbol,"c") == 0)
1616 return(QuantumScale*pixel.red);
1617 break;
1618 }
1619 case 'D':
1620 case 'd':
1621 {
1622 if (LocaleNCompare(symbol,"depth",5) == 0)
1623 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1624 break;
1625 }
1626 case 'G':
1627 case 'g':
1628 {
1629 if (LocaleCompare(symbol,"g") == 0)
1630 return(QuantumScale*pixel.green);
1631 break;
1632 }
1633 case 'K':
1634 case 'k':
1635 {
1636 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1637 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1638 if (LocaleCompare(symbol,"k") == 0)
1639 {
1640 if (image->colorspace != CMYKColorspace)
1641 {
1642 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001643 OptionError,"ColorSeparatedImageRequired","`%s'",
cristy3ed852e2009-09-05 21:47:34 +00001644 image->filename);
1645 return(0.0);
1646 }
cristy4c08aed2011-07-01 19:47:50 +00001647 return(QuantumScale*pixel.black);
cristy3ed852e2009-09-05 21:47:34 +00001648 }
1649 break;
1650 }
1651 case 'H':
1652 case 'h':
1653 {
1654 if (LocaleCompare(symbol,"h") == 0)
cristya19f1d72012-08-07 18:24:38 +00001655 return((double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001656 if (LocaleCompare(symbol,"hue") == 0)
1657 {
1658 double
1659 hue,
1660 lightness,
1661 saturation;
1662
cristy0a39a5c2012-06-27 12:51:45 +00001663 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
cristyda1f9c12011-10-02 21:39:49 +00001664 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001665 return(hue);
1666 }
1667 break;
1668 }
1669 case 'I':
1670 case 'i':
1671 {
1672 if ((LocaleCompare(symbol,"image.depth") == 0) ||
1673 (LocaleCompare(symbol,"image.minima") == 0) ||
1674 (LocaleCompare(symbol,"image.maxima") == 0) ||
1675 (LocaleCompare(symbol,"image.mean") == 0) ||
1676 (LocaleCompare(symbol,"image.kurtosis") == 0) ||
1677 (LocaleCompare(symbol,"image.skewness") == 0) ||
1678 (LocaleCompare(symbol,"image.standard_deviation") == 0))
1679 return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
1680 if (LocaleCompare(symbol,"image.resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001681 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001682 if (LocaleCompare(symbol,"image.resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001683 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001684 if (LocaleCompare(symbol,"intensity") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001685 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001686 if (LocaleCompare(symbol,"i") == 0)
cristya19f1d72012-08-07 18:24:38 +00001687 return((double) x);
cristy3ed852e2009-09-05 21:47:34 +00001688 break;
1689 }
1690 case 'J':
1691 case 'j':
1692 {
1693 if (LocaleCompare(symbol,"j") == 0)
cristya19f1d72012-08-07 18:24:38 +00001694 return((double) y);
cristy3ed852e2009-09-05 21:47:34 +00001695 break;
1696 }
1697 case 'L':
1698 case 'l':
1699 {
1700 if (LocaleCompare(symbol,"lightness") == 0)
1701 {
1702 double
1703 hue,
1704 lightness,
1705 saturation;
1706
cristy0a39a5c2012-06-27 12:51:45 +00001707 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
cristyda1f9c12011-10-02 21:39:49 +00001708 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001709 return(lightness);
1710 }
1711 if (LocaleCompare(symbol,"luminance") == 0)
1712 {
1713 double
1714 luminence;
1715
cristya86a5cb2012-10-14 13:40:33 +00001716 luminence=0.21267f*pixel.red+0.71516f*pixel.green+0.07217f*pixel.blue;
cristy3ed852e2009-09-05 21:47:34 +00001717 return(QuantumScale*luminence);
1718 }
1719 break;
1720 }
1721 case 'M':
1722 case 'm':
1723 {
1724 if (LocaleNCompare(symbol,"maxima",6) == 0)
1725 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1726 if (LocaleNCompare(symbol,"mean",4) == 0)
1727 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1728 if (LocaleNCompare(symbol,"minima",6) == 0)
1729 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1730 if (LocaleCompare(symbol,"m") == 0)
1731 return(QuantumScale*pixel.blue);
1732 break;
1733 }
1734 case 'N':
1735 case 'n':
1736 {
1737 if (LocaleCompare(symbol,"n") == 0)
cristya19f1d72012-08-07 18:24:38 +00001738 return((double) GetImageListLength(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001739 break;
1740 }
1741 case 'O':
1742 case 'o':
1743 {
1744 if (LocaleCompare(symbol,"o") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001745 return(QuantumScale*pixel.alpha);
cristy3ed852e2009-09-05 21:47:34 +00001746 break;
1747 }
1748 case 'P':
1749 case 'p':
1750 {
1751 if (LocaleCompare(symbol,"page.height") == 0)
cristya19f1d72012-08-07 18:24:38 +00001752 return((double) image->page.height);
cristy3ed852e2009-09-05 21:47:34 +00001753 if (LocaleCompare(symbol,"page.width") == 0)
cristya19f1d72012-08-07 18:24:38 +00001754 return((double) image->page.width);
cristy3ed852e2009-09-05 21:47:34 +00001755 if (LocaleCompare(symbol,"page.x") == 0)
cristya19f1d72012-08-07 18:24:38 +00001756 return((double) image->page.x);
cristy3ed852e2009-09-05 21:47:34 +00001757 if (LocaleCompare(symbol,"page.y") == 0)
cristya19f1d72012-08-07 18:24:38 +00001758 return((double) image->page.y);
cristy3ed852e2009-09-05 21:47:34 +00001759 break;
1760 }
1761 case 'R':
1762 case 'r':
1763 {
1764 if (LocaleCompare(symbol,"resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001765 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001766 if (LocaleCompare(symbol,"resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001767 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001768 if (LocaleCompare(symbol,"r") == 0)
1769 return(QuantumScale*pixel.red);
1770 break;
1771 }
1772 case 'S':
1773 case 's':
1774 {
1775 if (LocaleCompare(symbol,"saturation") == 0)
1776 {
1777 double
1778 hue,
1779 lightness,
1780 saturation;
1781
cristy0a39a5c2012-06-27 12:51:45 +00001782 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
cristyda1f9c12011-10-02 21:39:49 +00001783 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001784 return(saturation);
1785 }
1786 if (LocaleNCompare(symbol,"skewness",8) == 0)
1787 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1788 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1789 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1790 break;
1791 }
1792 case 'T':
1793 case 't':
1794 {
1795 if (LocaleCompare(symbol,"t") == 0)
cristya19f1d72012-08-07 18:24:38 +00001796 return((double) GetImageIndexInList(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001797 break;
1798 }
1799 case 'W':
1800 case 'w':
1801 {
1802 if (LocaleCompare(symbol,"w") == 0)
cristya19f1d72012-08-07 18:24:38 +00001803 return((double) image->columns);
cristy3ed852e2009-09-05 21:47:34 +00001804 break;
1805 }
1806 case 'Y':
1807 case 'y':
1808 {
1809 if (LocaleCompare(symbol,"y") == 0)
1810 return(QuantumScale*pixel.green);
1811 break;
1812 }
1813 case 'Z':
1814 case 'z':
1815 {
1816 if (LocaleCompare(symbol,"z") == 0)
1817 {
cristya19f1d72012-08-07 18:24:38 +00001818 double
cristy3ed852e2009-09-05 21:47:34 +00001819 depth;
1820
cristya19f1d72012-08-07 18:24:38 +00001821 depth=(double) GetImageDepth(image,fx_info->exception);
cristy3ed852e2009-09-05 21:47:34 +00001822 return(depth);
1823 }
1824 break;
1825 }
1826 default:
1827 break;
1828 }
1829 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1830 if (value != (const char *) NULL)
cristya19f1d72012-08-07 18:24:38 +00001831 return((double) StringToDouble(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001832 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
cristyefe601c2013-01-05 17:51:12 +00001833 "UnableToParseExpression","`%s'",symbol);
cristy3ed852e2009-09-05 21:47:34 +00001834 return(0.0);
1835}
1836
1837static const char *FxOperatorPrecedence(const char *expression,
1838 ExceptionInfo *exception)
1839{
1840 typedef enum
1841 {
1842 UndefinedPrecedence,
1843 NullPrecedence,
1844 BitwiseComplementPrecedence,
1845 ExponentPrecedence,
cristy116af162010-08-13 01:25:47 +00001846 ExponentialNotationPrecedence,
cristy3ed852e2009-09-05 21:47:34 +00001847 MultiplyPrecedence,
1848 AdditionPrecedence,
1849 ShiftPrecedence,
1850 RelationalPrecedence,
1851 EquivalencyPrecedence,
1852 BitwiseAndPrecedence,
1853 BitwiseOrPrecedence,
1854 LogicalAndPrecedence,
1855 LogicalOrPrecedence,
1856 TernaryPrecedence,
1857 AssignmentPrecedence,
1858 CommaPrecedence,
1859 SeparatorPrecedence
1860 } FxPrecedence;
1861
1862 FxPrecedence
1863 precedence,
1864 target;
1865
1866 register const char
1867 *subexpression;
1868
1869 register int
1870 c;
1871
cristybb503372010-05-27 20:51:26 +00001872 size_t
cristy3ed852e2009-09-05 21:47:34 +00001873 level;
1874
1875 c=0;
1876 level=0;
1877 subexpression=(const char *) NULL;
1878 target=NullPrecedence;
1879 while (*expression != '\0')
1880 {
1881 precedence=UndefinedPrecedence;
1882 if ((isspace((int) ((char) *expression)) != 0) || (c == (int) '@'))
1883 {
1884 expression++;
1885 continue;
1886 }
cristy488fa882010-03-01 22:34:24 +00001887 switch (*expression)
1888 {
1889 case 'A':
1890 case 'a':
cristy3ed852e2009-09-05 21:47:34 +00001891 {
cristyb33454f2011-08-03 02:10:45 +00001892#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00001893 if (LocaleNCompare(expression,"acosh",5) == 0)
1894 {
1895 expression+=5;
1896 break;
1897 }
cristyb33454f2011-08-03 02:10:45 +00001898#endif
1899#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00001900 if (LocaleNCompare(expression,"asinh",5) == 0)
1901 {
1902 expression+=5;
1903 break;
1904 }
cristyb33454f2011-08-03 02:10:45 +00001905#endif
1906#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00001907 if (LocaleNCompare(expression,"atanh",5) == 0)
cristy488fa882010-03-01 22:34:24 +00001908 {
1909 expression+=5;
1910 break;
1911 }
cristyb33454f2011-08-03 02:10:45 +00001912#endif
anthony62838e52012-05-24 12:41:54 +00001913 if (LocaleNCompare(expression,"atan2",5) == 0)
1914 {
1915 expression+=5;
1916 break;
1917 }
cristy488fa882010-03-01 22:34:24 +00001918 break;
cristy3ed852e2009-09-05 21:47:34 +00001919 }
cristy62d455f2011-11-03 11:42:28 +00001920 case 'E':
1921 case 'e':
1922 {
1923 if ((LocaleNCompare(expression,"E+",2) == 0) ||
1924 (LocaleNCompare(expression,"E-",2) == 0))
1925 {
1926 expression+=2; /* scientific notation */
1927 break;
1928 }
1929 }
cristy488fa882010-03-01 22:34:24 +00001930 case 'J':
1931 case 'j':
1932 {
1933 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1934 (LocaleNCompare(expression,"j1",2) == 0))
1935 {
1936 expression+=2;
1937 break;
1938 }
1939 break;
1940 }
cristy2def9322010-06-18 23:59:37 +00001941 case '#':
1942 {
1943 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1944 expression++;
1945 break;
1946 }
cristy488fa882010-03-01 22:34:24 +00001947 default:
1948 break;
1949 }
cristy3ed852e2009-09-05 21:47:34 +00001950 if ((c == (int) '{') || (c == (int) '['))
1951 level++;
1952 else
1953 if ((c == (int) '}') || (c == (int) ']'))
1954 level--;
1955 if (level == 0)
1956 switch ((unsigned char) *expression)
1957 {
1958 case '~':
1959 case '!':
1960 {
1961 precedence=BitwiseComplementPrecedence;
1962 break;
1963 }
1964 case '^':
cristy6621e252010-08-13 00:42:57 +00001965 case '@':
cristy3ed852e2009-09-05 21:47:34 +00001966 {
1967 precedence=ExponentPrecedence;
1968 break;
1969 }
1970 default:
1971 {
1972 if (((c != 0) && ((isdigit((int) ((char) c)) != 0) ||
1973 (strchr(")",c) != (char *) NULL))) &&
1974 (((islower((int) ((char) *expression)) != 0) ||
1975 (strchr("(",(int) *expression) != (char *) NULL)) ||
1976 ((isdigit((int) ((char) c)) == 0) &&
1977 (isdigit((int) ((char) *expression)) != 0))) &&
1978 (strchr("xy",(int) *expression) == (char *) NULL))
1979 precedence=MultiplyPrecedence;
1980 break;
1981 }
1982 case '*':
1983 case '/':
1984 case '%':
1985 {
1986 precedence=MultiplyPrecedence;
1987 break;
1988 }
1989 case '+':
1990 case '-':
1991 {
1992 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
1993 (isalpha(c) != 0))
1994 precedence=AdditionPrecedence;
1995 break;
1996 }
1997 case LeftShiftOperator:
1998 case RightShiftOperator:
1999 {
2000 precedence=ShiftPrecedence;
2001 break;
2002 }
2003 case '<':
2004 case LessThanEqualOperator:
2005 case GreaterThanEqualOperator:
2006 case '>':
2007 {
2008 precedence=RelationalPrecedence;
2009 break;
2010 }
2011 case EqualOperator:
2012 case NotEqualOperator:
2013 {
2014 precedence=EquivalencyPrecedence;
2015 break;
2016 }
2017 case '&':
2018 {
2019 precedence=BitwiseAndPrecedence;
2020 break;
2021 }
2022 case '|':
2023 {
2024 precedence=BitwiseOrPrecedence;
2025 break;
2026 }
2027 case LogicalAndOperator:
2028 {
2029 precedence=LogicalAndPrecedence;
2030 break;
2031 }
2032 case LogicalOrOperator:
2033 {
2034 precedence=LogicalOrPrecedence;
2035 break;
2036 }
cristy116af162010-08-13 01:25:47 +00002037 case ExponentialNotation:
2038 {
2039 precedence=ExponentialNotationPrecedence;
2040 break;
2041 }
cristy3ed852e2009-09-05 21:47:34 +00002042 case ':':
2043 case '?':
2044 {
2045 precedence=TernaryPrecedence;
2046 break;
2047 }
2048 case '=':
2049 {
2050 precedence=AssignmentPrecedence;
2051 break;
2052 }
2053 case ',':
2054 {
2055 precedence=CommaPrecedence;
2056 break;
2057 }
2058 case ';':
2059 {
2060 precedence=SeparatorPrecedence;
2061 break;
2062 }
2063 }
2064 if ((precedence == BitwiseComplementPrecedence) ||
2065 (precedence == TernaryPrecedence) ||
2066 (precedence == AssignmentPrecedence))
2067 {
2068 if (precedence > target)
2069 {
2070 /*
2071 Right-to-left associativity.
2072 */
2073 target=precedence;
2074 subexpression=expression;
2075 }
2076 }
2077 else
2078 if (precedence >= target)
2079 {
2080 /*
2081 Left-to-right associativity.
2082 */
2083 target=precedence;
2084 subexpression=expression;
2085 }
2086 if (strchr("(",(int) *expression) != (char *) NULL)
2087 expression=FxSubexpression(expression,exception);
2088 c=(int) (*expression++);
2089 }
2090 return(subexpression);
2091}
2092
cristya19f1d72012-08-07 18:24:38 +00002093static double FxEvaluateSubexpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002094 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristya19f1d72012-08-07 18:24:38 +00002095 const char *expression,double *beta,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002096{
2097 char
2098 *q,
2099 subexpression[MaxTextExtent];
2100
cristya19f1d72012-08-07 18:24:38 +00002101 double
cristy3ed852e2009-09-05 21:47:34 +00002102 alpha,
2103 gamma;
2104
2105 register const char
2106 *p;
2107
2108 *beta=0.0;
2109 if (exception->severity != UndefinedException)
2110 return(0.0);
2111 while (isspace((int) *expression) != 0)
2112 expression++;
2113 if (*expression == '\0')
2114 {
2115 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
cristyefe601c2013-01-05 17:51:12 +00002116 "MissingExpression","`%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002117 return(0.0);
2118 }
cristy66322f02010-05-17 11:40:48 +00002119 *subexpression='\0';
cristy3ed852e2009-09-05 21:47:34 +00002120 p=FxOperatorPrecedence(expression,exception);
2121 if (p != (const char *) NULL)
2122 {
2123 (void) CopyMagickString(subexpression,expression,(size_t)
2124 (p-expression+1));
2125 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2126 exception);
2127 switch ((unsigned char) *p)
2128 {
2129 case '~':
2130 {
2131 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002132 *beta=(double) (~(size_t) *beta);
cristy3ed852e2009-09-05 21:47:34 +00002133 return(*beta);
2134 }
2135 case '!':
2136 {
2137 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2138 return(*beta == 0.0 ? 1.0 : 0.0);
2139 }
2140 case '^':
2141 {
2142 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2143 channel,x,y,++p,beta,exception));
2144 return(*beta);
2145 }
2146 case '*':
cristy116af162010-08-13 01:25:47 +00002147 case ExponentialNotation:
cristy3ed852e2009-09-05 21:47:34 +00002148 {
2149 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2150 return(alpha*(*beta));
2151 }
2152 case '/':
2153 {
2154 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2155 if (*beta == 0.0)
2156 {
2157 if (exception->severity == UndefinedException)
2158 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00002159 OptionError,"DivideByZero","`%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002160 return(0.0);
2161 }
2162 return(alpha/(*beta));
2163 }
2164 case '%':
2165 {
2166 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2167 *beta=fabs(floor(((double) *beta)+0.5));
2168 if (*beta == 0.0)
2169 {
2170 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00002171 OptionError,"DivideByZero","`%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002172 return(0.0);
2173 }
2174 return(fmod((double) alpha,(double) *beta));
2175 }
2176 case '+':
2177 {
2178 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2179 return(alpha+(*beta));
2180 }
2181 case '-':
2182 {
2183 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2184 return(alpha-(*beta));
2185 }
2186 case LeftShiftOperator:
2187 {
2188 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002189 *beta=(double) ((size_t) (alpha+0.5) << (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002190 return(*beta);
2191 }
2192 case RightShiftOperator:
2193 {
2194 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002195 *beta=(double) ((size_t) (alpha+0.5) >> (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002196 return(*beta);
2197 }
2198 case '<':
2199 {
2200 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2201 return(alpha < *beta ? 1.0 : 0.0);
2202 }
2203 case LessThanEqualOperator:
2204 {
2205 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2206 return(alpha <= *beta ? 1.0 : 0.0);
2207 }
2208 case '>':
2209 {
2210 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2211 return(alpha > *beta ? 1.0 : 0.0);
2212 }
2213 case GreaterThanEqualOperator:
2214 {
2215 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2216 return(alpha >= *beta ? 1.0 : 0.0);
2217 }
2218 case EqualOperator:
2219 {
2220 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy9b528342012-06-02 00:59:20 +00002221 return(fabs(alpha-(*beta)) < MagickEpsilon ? MagickEpsilon : 0.0);
cristy3ed852e2009-09-05 21:47:34 +00002222 }
2223 case NotEqualOperator:
2224 {
2225 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy972050b2012-06-04 22:09:17 +00002226 return(fabs(alpha-(*beta)) >= MagickEpsilon ? 1.0 : 0.0);
cristy3ed852e2009-09-05 21:47:34 +00002227 }
2228 case '&':
2229 {
2230 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002231 *beta=(double) ((size_t) (alpha+0.5) & (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002232 return(*beta);
2233 }
2234 case '|':
2235 {
2236 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002237 *beta=(double) ((size_t) (alpha+0.5) | (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002238 return(*beta);
2239 }
2240 case LogicalAndOperator:
2241 {
2242 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2243 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2244 return(*beta);
2245 }
2246 case LogicalOrOperator:
2247 {
2248 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2249 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2250 return(*beta);
2251 }
2252 case '?':
2253 {
cristya19f1d72012-08-07 18:24:38 +00002254 double
cristy3ed852e2009-09-05 21:47:34 +00002255 gamma;
2256
2257 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2258 q=subexpression;
2259 p=StringToken(":",&q);
2260 if (q == (char *) NULL)
2261 {
2262 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00002263 OptionError,"UnableToParseExpression","`%s'",subexpression);
cristy3ed852e2009-09-05 21:47:34 +00002264 return(0.0);
2265 }
cristy972050b2012-06-04 22:09:17 +00002266 if (fabs((double) alpha) >= MagickEpsilon)
cristy3ed852e2009-09-05 21:47:34 +00002267 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2268 else
2269 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2270 return(gamma);
2271 }
2272 case '=':
2273 {
2274 char
2275 numeric[MaxTextExtent];
2276
2277 q=subexpression;
2278 while (isalpha((int) ((unsigned char) *q)) != 0)
2279 q++;
2280 if (*q != '\0')
2281 {
2282 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00002283 OptionError,"UnableToParseExpression","`%s'",subexpression);
cristy3ed852e2009-09-05 21:47:34 +00002284 return(0.0);
2285 }
2286 ClearMagickException(exception);
2287 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristyb51dff52011-05-19 16:55:47 +00002288 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
cristy8cd5b312010-01-07 01:10:24 +00002289 *beta);
cristy3ed852e2009-09-05 21:47:34 +00002290 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2291 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2292 subexpression),ConstantString(numeric));
2293 return(*beta);
2294 }
2295 case ',':
2296 {
2297 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2298 return(alpha);
2299 }
2300 case ';':
2301 {
2302 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2303 return(*beta);
2304 }
2305 default:
2306 {
2307 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2308 exception);
2309 return(gamma);
2310 }
2311 }
2312 }
2313 if (strchr("(",(int) *expression) != (char *) NULL)
2314 {
2315 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2316 subexpression[strlen(subexpression)-1]='\0';
2317 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2318 exception);
2319 return(gamma);
2320 }
cristy8b8a3ae2010-10-23 18:49:46 +00002321 switch (*expression)
cristy3ed852e2009-09-05 21:47:34 +00002322 {
2323 case '+':
2324 {
2325 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2326 exception);
2327 return(1.0*gamma);
2328 }
2329 case '-':
2330 {
2331 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2332 exception);
2333 return(-1.0*gamma);
2334 }
2335 case '~':
2336 {
2337 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2338 exception);
cristya19f1d72012-08-07 18:24:38 +00002339 return((double) (~(size_t) (gamma+0.5)));
cristy3ed852e2009-09-05 21:47:34 +00002340 }
2341 case 'A':
2342 case 'a':
2343 {
2344 if (LocaleNCompare(expression,"abs",3) == 0)
2345 {
2346 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2347 exception);
cristya19f1d72012-08-07 18:24:38 +00002348 return((double) fabs((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002349 }
cristyb33454f2011-08-03 02:10:45 +00002350#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00002351 if (LocaleNCompare(expression,"acosh",5) == 0)
2352 {
2353 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2354 exception);
cristya19f1d72012-08-07 18:24:38 +00002355 return((double) acosh((double) alpha));
cristy363772a2011-07-28 23:25:33 +00002356 }
cristyb33454f2011-08-03 02:10:45 +00002357#endif
cristy3ed852e2009-09-05 21:47:34 +00002358 if (LocaleNCompare(expression,"acos",4) == 0)
2359 {
2360 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2361 exception);
cristya19f1d72012-08-07 18:24:38 +00002362 return((double) acos((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002363 }
cristy43c22f42010-03-30 12:34:07 +00002364#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002365 if (LocaleNCompare(expression,"airy",4) == 0)
2366 {
2367 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2368 exception);
2369 if (alpha == 0.0)
cristy2dd03222010-03-30 22:12:11 +00002370 return(1.0);
2371 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
cristy43c22f42010-03-30 12:34:07 +00002372 return(gamma*gamma);
cristyee56cf12010-03-01 22:17:06 +00002373 }
cristy43c22f42010-03-30 12:34:07 +00002374#endif
cristyb33454f2011-08-03 02:10:45 +00002375#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00002376 if (LocaleNCompare(expression,"asinh",5) == 0)
2377 {
2378 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2379 exception);
cristya19f1d72012-08-07 18:24:38 +00002380 return((double) asinh((double) alpha));
cristy363772a2011-07-28 23:25:33 +00002381 }
cristyb33454f2011-08-03 02:10:45 +00002382#endif
cristy3ed852e2009-09-05 21:47:34 +00002383 if (LocaleNCompare(expression,"asin",4) == 0)
2384 {
2385 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2386 exception);
cristya19f1d72012-08-07 18:24:38 +00002387 return((double) asin((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002388 }
2389 if (LocaleNCompare(expression,"alt",3) == 0)
2390 {
2391 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2392 exception);
cristybb503372010-05-27 20:51:26 +00002393 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
cristy3ed852e2009-09-05 21:47:34 +00002394 }
2395 if (LocaleNCompare(expression,"atan2",5) == 0)
2396 {
2397 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2398 exception);
cristya19f1d72012-08-07 18:24:38 +00002399 return((double) atan2((double) alpha,(double) *beta));
cristy3ed852e2009-09-05 21:47:34 +00002400 }
cristyb33454f2011-08-03 02:10:45 +00002401#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00002402 if (LocaleNCompare(expression,"atanh",5) == 0)
2403 {
2404 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2405 exception);
cristya19f1d72012-08-07 18:24:38 +00002406 return((double) atanh((double) alpha));
cristy363772a2011-07-28 23:25:33 +00002407 }
cristyb33454f2011-08-03 02:10:45 +00002408#endif
cristy3ed852e2009-09-05 21:47:34 +00002409 if (LocaleNCompare(expression,"atan",4) == 0)
2410 {
2411 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2412 exception);
cristya19f1d72012-08-07 18:24:38 +00002413 return((double) atan((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002414 }
2415 if (LocaleCompare(expression,"a") == 0)
2416 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2417 break;
2418 }
2419 case 'B':
2420 case 'b':
2421 {
2422 if (LocaleCompare(expression,"b") == 0)
2423 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2424 break;
2425 }
2426 case 'C':
2427 case 'c':
2428 {
2429 if (LocaleNCompare(expression,"ceil",4) == 0)
2430 {
2431 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2432 exception);
cristya19f1d72012-08-07 18:24:38 +00002433 return((double) ceil((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002434 }
2435 if (LocaleNCompare(expression,"cosh",4) == 0)
2436 {
2437 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2438 exception);
cristya19f1d72012-08-07 18:24:38 +00002439 return((double) cosh((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002440 }
2441 if (LocaleNCompare(expression,"cos",3) == 0)
2442 {
2443 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2444 exception);
cristya19f1d72012-08-07 18:24:38 +00002445 return((double) cos((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002446 }
2447 if (LocaleCompare(expression,"c") == 0)
2448 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2449 break;
2450 }
2451 case 'D':
2452 case 'd':
2453 {
2454 if (LocaleNCompare(expression,"debug",5) == 0)
2455 {
2456 const char
2457 *type;
2458
2459 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2460 exception);
2461 if (fx_info->images->colorspace == CMYKColorspace)
2462 switch (channel)
2463 {
cristy0568ffc2011-07-25 16:54:14 +00002464 case CyanPixelChannel: type="cyan"; break;
2465 case MagentaPixelChannel: type="magenta"; break;
2466 case YellowPixelChannel: type="yellow"; break;
2467 case AlphaPixelChannel: type="opacity"; break;
2468 case BlackPixelChannel: type="black"; break;
cristy3ed852e2009-09-05 21:47:34 +00002469 default: type="unknown"; break;
2470 }
2471 else
2472 switch (channel)
2473 {
cristy0568ffc2011-07-25 16:54:14 +00002474 case RedPixelChannel: type="red"; break;
2475 case GreenPixelChannel: type="green"; break;
2476 case BluePixelChannel: type="blue"; break;
2477 case AlphaPixelChannel: type="opacity"; break;
cristy3ed852e2009-09-05 21:47:34 +00002478 default: type="unknown"; break;
2479 }
2480 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2481 if (strlen(subexpression) > 1)
2482 subexpression[strlen(subexpression)-1]='\0';
2483 if (fx_info->file != (FILE *) NULL)
cristy1707c6c2012-01-18 23:30:54 +00002484 (void) FormatLocaleFile(fx_info->file,"%s[%.20g,%.20g].%s: "
2485 "%s=%.*g\n",fx_info->images->filename,(double) x,(double) y,type,
2486 subexpression,GetMagickPrecision(),(double) alpha);
cristy3ed852e2009-09-05 21:47:34 +00002487 return(0.0);
2488 }
cristy5597a8d2011-11-04 00:25:32 +00002489 if (LocaleNCompare(expression,"drc",3) == 0)
2490 {
2491 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2492 exception);
cristya19f1d72012-08-07 18:24:38 +00002493 return((double) (alpha/(*beta*(alpha-1.0)+1.0)));
cristy5597a8d2011-11-04 00:25:32 +00002494 }
cristy3ed852e2009-09-05 21:47:34 +00002495 break;
2496 }
2497 case 'E':
2498 case 'e':
2499 {
2500 if (LocaleCompare(expression,"epsilon") == 0)
cristya19f1d72012-08-07 18:24:38 +00002501 return((double) MagickEpsilon);
cristy3ed852e2009-09-05 21:47:34 +00002502 if (LocaleNCompare(expression,"exp",3) == 0)
2503 {
2504 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2505 exception);
cristya19f1d72012-08-07 18:24:38 +00002506 return((double) exp((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002507 }
2508 if (LocaleCompare(expression,"e") == 0)
cristya19f1d72012-08-07 18:24:38 +00002509 return((double) 2.7182818284590452354);
cristy3ed852e2009-09-05 21:47:34 +00002510 break;
2511 }
2512 case 'F':
2513 case 'f':
2514 {
2515 if (LocaleNCompare(expression,"floor",5) == 0)
2516 {
2517 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2518 exception);
cristya19f1d72012-08-07 18:24:38 +00002519 return((double) floor((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002520 }
2521 break;
2522 }
2523 case 'G':
2524 case 'g':
2525 {
cristy9eeedea2011-11-02 19:04:05 +00002526 if (LocaleNCompare(expression,"gauss",5) == 0)
2527 {
2528 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2529 exception);
2530 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
cristya19f1d72012-08-07 18:24:38 +00002531 return((double) gamma);
cristy9eeedea2011-11-02 19:04:05 +00002532 }
cristyb0aad4c2011-11-02 19:30:35 +00002533 if (LocaleNCompare(expression,"gcd",3) == 0)
2534 {
2535 MagickOffsetType
2536 gcd;
2537
2538 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2539 exception);
cristy1707c6c2012-01-18 23:30:54 +00002540 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType) (*beta+
2541 0.5));
cristya19f1d72012-08-07 18:24:38 +00002542 return((double) gcd);
cristyb0aad4c2011-11-02 19:30:35 +00002543 }
cristy3ed852e2009-09-05 21:47:34 +00002544 if (LocaleCompare(expression,"g") == 0)
2545 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2546 break;
2547 }
2548 case 'H':
2549 case 'h':
2550 {
2551 if (LocaleCompare(expression,"h") == 0)
2552 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2553 if (LocaleCompare(expression,"hue") == 0)
2554 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2555 if (LocaleNCompare(expression,"hypot",5) == 0)
2556 {
2557 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2558 exception);
cristya19f1d72012-08-07 18:24:38 +00002559 return((double) hypot((double) alpha,(double) *beta));
cristy3ed852e2009-09-05 21:47:34 +00002560 }
2561 break;
2562 }
2563 case 'K':
2564 case 'k':
2565 {
2566 if (LocaleCompare(expression,"k") == 0)
2567 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2568 break;
2569 }
2570 case 'I':
2571 case 'i':
2572 {
2573 if (LocaleCompare(expression,"intensity") == 0)
2574 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2575 if (LocaleNCompare(expression,"int",3) == 0)
2576 {
2577 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2578 exception);
cristya19f1d72012-08-07 18:24:38 +00002579 return((double) floor(alpha));
cristy3ed852e2009-09-05 21:47:34 +00002580 }
cristy82b20722011-11-05 21:52:36 +00002581#if defined(MAGICKCORE_HAVE_ISNAN)
cristy639399c2011-11-02 19:16:15 +00002582 if (LocaleNCompare(expression,"isnan",5) == 0)
2583 {
2584 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2585 exception);
cristya19f1d72012-08-07 18:24:38 +00002586 return((double) !!isnan((double) alpha));
cristy639399c2011-11-02 19:16:15 +00002587 }
cristy82b20722011-11-05 21:52:36 +00002588#endif
cristy3ed852e2009-09-05 21:47:34 +00002589 if (LocaleCompare(expression,"i") == 0)
2590 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2591 break;
2592 }
2593 case 'J':
2594 case 'j':
2595 {
2596 if (LocaleCompare(expression,"j") == 0)
2597 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
cristy161b9262010-03-20 19:34:32 +00002598#if defined(MAGICKCORE_HAVE_J0)
cristyee56cf12010-03-01 22:17:06 +00002599 if (LocaleNCompare(expression,"j0",2) == 0)
2600 {
2601 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2602 exception);
cristya19f1d72012-08-07 18:24:38 +00002603 return((double) j0((double) alpha));
cristyee56cf12010-03-01 22:17:06 +00002604 }
cristy161b9262010-03-20 19:34:32 +00002605#endif
2606#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002607 if (LocaleNCompare(expression,"j1",2) == 0)
2608 {
2609 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2610 exception);
cristya19f1d72012-08-07 18:24:38 +00002611 return((double) j1((double) alpha));
cristyee56cf12010-03-01 22:17:06 +00002612 }
cristy161b9262010-03-20 19:34:32 +00002613#endif
cristyaa018fa2010-04-08 23:03:54 +00002614#if defined(MAGICKCORE_HAVE_J1)
cristya6a09e72010-03-02 14:51:02 +00002615 if (LocaleNCompare(expression,"jinc",4) == 0)
2616 {
2617 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2618 exception);
cristy0946a822010-03-12 17:14:58 +00002619 if (alpha == 0.0)
2620 return(1.0);
cristyc90d70d2012-11-03 23:53:13 +00002621 gamma=(double) (2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha));
cristyfce2f7b2010-03-12 00:29:49 +00002622 return(gamma);
cristya6a09e72010-03-02 14:51:02 +00002623 }
cristyaa018fa2010-04-08 23:03:54 +00002624#endif
cristy3ed852e2009-09-05 21:47:34 +00002625 break;
2626 }
2627 case 'L':
2628 case 'l':
2629 {
2630 if (LocaleNCompare(expression,"ln",2) == 0)
2631 {
2632 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2633 exception);
cristya19f1d72012-08-07 18:24:38 +00002634 return((double) log((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002635 }
cristyc8ed5322010-08-31 12:07:59 +00002636 if (LocaleNCompare(expression,"logtwo",6) == 0)
cristy3ed852e2009-09-05 21:47:34 +00002637 {
cristyc8ed5322010-08-31 12:07:59 +00002638 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
cristy3ed852e2009-09-05 21:47:34 +00002639 exception);
cristya19f1d72012-08-07 18:24:38 +00002640 return((double) log10((double) alpha))/log10(2.0);
cristy3ed852e2009-09-05 21:47:34 +00002641 }
2642 if (LocaleNCompare(expression,"log",3) == 0)
2643 {
2644 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2645 exception);
cristya19f1d72012-08-07 18:24:38 +00002646 return((double) log10((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002647 }
2648 if (LocaleCompare(expression,"lightness") == 0)
2649 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2650 break;
2651 }
2652 case 'M':
2653 case 'm':
2654 {
2655 if (LocaleCompare(expression,"MaxRGB") == 0)
cristya19f1d72012-08-07 18:24:38 +00002656 return((double) QuantumRange);
cristy3ed852e2009-09-05 21:47:34 +00002657 if (LocaleNCompare(expression,"maxima",6) == 0)
2658 break;
2659 if (LocaleNCompare(expression,"max",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002660 {
2661 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2662 exception);
2663 return(alpha > *beta ? alpha : *beta);
2664 }
cristy3ed852e2009-09-05 21:47:34 +00002665 if (LocaleNCompare(expression,"minima",6) == 0)
2666 break;
2667 if (LocaleNCompare(expression,"min",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002668 {
2669 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2670 exception);
2671 return(alpha < *beta ? alpha : *beta);
2672 }
cristy3ed852e2009-09-05 21:47:34 +00002673 if (LocaleNCompare(expression,"mod",3) == 0)
2674 {
2675 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2676 exception);
cristy984049c2011-11-03 18:34:58 +00002677 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2678 return(gamma);
cristy3ed852e2009-09-05 21:47:34 +00002679 }
2680 if (LocaleCompare(expression,"m") == 0)
2681 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2682 break;
2683 }
2684 case 'N':
2685 case 'n':
2686 {
cristyad3502e2011-11-02 19:10:45 +00002687 if (LocaleNCompare(expression,"not",3) == 0)
2688 {
2689 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2690 exception);
cristya19f1d72012-08-07 18:24:38 +00002691 return((double) (alpha < MagickEpsilon));
cristyad3502e2011-11-02 19:10:45 +00002692 }
cristy3ed852e2009-09-05 21:47:34 +00002693 if (LocaleCompare(expression,"n") == 0)
2694 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2695 break;
2696 }
2697 case 'O':
2698 case 'o':
2699 {
2700 if (LocaleCompare(expression,"Opaque") == 0)
2701 return(1.0);
2702 if (LocaleCompare(expression,"o") == 0)
2703 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2704 break;
2705 }
2706 case 'P':
2707 case 'p':
2708 {
cristy670aa3c2011-11-03 00:54:00 +00002709 if (LocaleCompare(expression,"phi") == 0)
cristya19f1d72012-08-07 18:24:38 +00002710 return((double) MagickPHI);
cristy3ed852e2009-09-05 21:47:34 +00002711 if (LocaleCompare(expression,"pi") == 0)
cristya19f1d72012-08-07 18:24:38 +00002712 return((double) MagickPI);
cristy3ed852e2009-09-05 21:47:34 +00002713 if (LocaleNCompare(expression,"pow",3) == 0)
2714 {
2715 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2716 exception);
cristya19f1d72012-08-07 18:24:38 +00002717 return((double) pow((double) alpha,(double) *beta));
cristy3ed852e2009-09-05 21:47:34 +00002718 }
2719 if (LocaleCompare(expression,"p") == 0)
2720 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2721 break;
2722 }
2723 case 'Q':
2724 case 'q':
2725 {
2726 if (LocaleCompare(expression,"QuantumRange") == 0)
cristya19f1d72012-08-07 18:24:38 +00002727 return((double) QuantumRange);
cristy3ed852e2009-09-05 21:47:34 +00002728 if (LocaleCompare(expression,"QuantumScale") == 0)
cristya19f1d72012-08-07 18:24:38 +00002729 return((double) QuantumScale);
cristy3ed852e2009-09-05 21:47:34 +00002730 break;
2731 }
2732 case 'R':
2733 case 'r':
2734 {
2735 if (LocaleNCompare(expression,"rand",4) == 0)
cristya19f1d72012-08-07 18:24:38 +00002736 return((double) GetPseudoRandomValue(fx_info->random_info));
cristy3ed852e2009-09-05 21:47:34 +00002737 if (LocaleNCompare(expression,"round",5) == 0)
2738 {
2739 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2740 exception);
cristya19f1d72012-08-07 18:24:38 +00002741 return((double) floor((double) alpha+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002742 }
2743 if (LocaleCompare(expression,"r") == 0)
2744 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2745 break;
2746 }
2747 case 'S':
2748 case 's':
2749 {
2750 if (LocaleCompare(expression,"saturation") == 0)
2751 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2752 if (LocaleNCompare(expression,"sign",4) == 0)
2753 {
2754 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2755 exception);
2756 return(alpha < 0.0 ? -1.0 : 1.0);
2757 }
cristya6a09e72010-03-02 14:51:02 +00002758 if (LocaleNCompare(expression,"sinc",4) == 0)
2759 {
2760 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2761 exception);
2762 if (alpha == 0)
2763 return(1.0);
cristya19f1d72012-08-07 18:24:38 +00002764 gamma=(double) (sin((double) (MagickPI*alpha))/
cristya6a09e72010-03-02 14:51:02 +00002765 (MagickPI*alpha));
2766 return(gamma);
2767 }
cristy3ed852e2009-09-05 21:47:34 +00002768 if (LocaleNCompare(expression,"sinh",4) == 0)
2769 {
2770 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2771 exception);
cristya19f1d72012-08-07 18:24:38 +00002772 return((double) sinh((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002773 }
2774 if (LocaleNCompare(expression,"sin",3) == 0)
2775 {
2776 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2777 exception);
cristya19f1d72012-08-07 18:24:38 +00002778 return((double) sin((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002779 }
2780 if (LocaleNCompare(expression,"sqrt",4) == 0)
2781 {
2782 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2783 exception);
cristya19f1d72012-08-07 18:24:38 +00002784 return((double) sqrt((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002785 }
cristy9eeedea2011-11-02 19:04:05 +00002786 if (LocaleNCompare(expression,"squish",6) == 0)
2787 {
2788 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2789 exception);
cristya19f1d72012-08-07 18:24:38 +00002790 return((double) (1.0/(1.0+exp((double) (-alpha)))));
cristy9eeedea2011-11-02 19:04:05 +00002791 }
cristy3ed852e2009-09-05 21:47:34 +00002792 if (LocaleCompare(expression,"s") == 0)
2793 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2794 break;
2795 }
2796 case 'T':
2797 case 't':
2798 {
2799 if (LocaleNCompare(expression,"tanh",4) == 0)
2800 {
2801 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2802 exception);
cristya19f1d72012-08-07 18:24:38 +00002803 return((double) tanh((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002804 }
2805 if (LocaleNCompare(expression,"tan",3) == 0)
2806 {
2807 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2808 exception);
cristya19f1d72012-08-07 18:24:38 +00002809 return((double) tan((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002810 }
2811 if (LocaleCompare(expression,"Transparent") == 0)
2812 return(0.0);
cristy16788e42010-08-13 13:44:26 +00002813 if (LocaleNCompare(expression,"trunc",5) == 0)
2814 {
2815 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2816 exception);
2817 if (alpha >= 0.0)
cristya19f1d72012-08-07 18:24:38 +00002818 return((double) floor((double) alpha));
2819 return((double) ceil((double) alpha));
cristy16788e42010-08-13 13:44:26 +00002820 }
cristy3ed852e2009-09-05 21:47:34 +00002821 if (LocaleCompare(expression,"t") == 0)
2822 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2823 break;
2824 }
2825 case 'U':
2826 case 'u':
2827 {
2828 if (LocaleCompare(expression,"u") == 0)
2829 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2830 break;
2831 }
2832 case 'V':
2833 case 'v':
2834 {
2835 if (LocaleCompare(expression,"v") == 0)
2836 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2837 break;
2838 }
2839 case 'W':
2840 case 'w':
2841 {
cristy9eeedea2011-11-02 19:04:05 +00002842 if (LocaleNCompare(expression,"while",5) == 0)
2843 {
2844 do
2845 {
2846 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2847 exception);
2848 } while (fabs((double) alpha) >= MagickEpsilon);
cristya19f1d72012-08-07 18:24:38 +00002849 return((double) *beta);
cristy9eeedea2011-11-02 19:04:05 +00002850 }
cristy3ed852e2009-09-05 21:47:34 +00002851 if (LocaleCompare(expression,"w") == 0)
2852 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2853 break;
2854 }
2855 case 'Y':
2856 case 'y':
2857 {
2858 if (LocaleCompare(expression,"y") == 0)
2859 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2860 break;
2861 }
2862 case 'Z':
2863 case 'z':
2864 {
2865 if (LocaleCompare(expression,"z") == 0)
2866 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2867 break;
2868 }
2869 default:
2870 break;
2871 }
2872 q=(char *) expression;
cristydbdd0e32011-11-04 23:29:40 +00002873 alpha=InterpretSiPrefixValue(expression,&q);
cristy3ed852e2009-09-05 21:47:34 +00002874 if (q == expression)
2875 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2876 return(alpha);
2877}
2878
cristy7832dc22011-09-05 01:21:53 +00002879MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
cristya19f1d72012-08-07 18:24:38 +00002880 double *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002881{
2882 MagickBooleanType
2883 status;
2884
cristy541ae572011-08-05 19:08:59 +00002885 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2886 exception);
cristy3ed852e2009-09-05 21:47:34 +00002887 return(status);
2888}
2889
2890MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
cristya19f1d72012-08-07 18:24:38 +00002891 double *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002892{
2893 FILE
2894 *file;
2895
2896 MagickBooleanType
2897 status;
2898
2899 file=fx_info->file;
2900 fx_info->file=(FILE *) NULL;
cristy541ae572011-08-05 19:08:59 +00002901 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2902 exception);
cristy3ed852e2009-09-05 21:47:34 +00002903 fx_info->file=file;
2904 return(status);
2905}
2906
cristy7832dc22011-09-05 01:21:53 +00002907MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002908 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristya19f1d72012-08-07 18:24:38 +00002909 double *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002910{
cristya19f1d72012-08-07 18:24:38 +00002911 double
cristy3ed852e2009-09-05 21:47:34 +00002912 beta;
2913
2914 beta=0.0;
2915 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2916 exception);
2917 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2918}
2919
2920/*
2921%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2922% %
2923% %
2924% %
2925% F x I m a g e %
2926% %
2927% %
2928% %
2929%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2930%
2931% FxImage() applies a mathematical expression to the specified image.
2932%
2933% The format of the FxImage method is:
2934%
2935% Image *FxImage(const Image *image,const char *expression,
2936% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002937%
2938% A description of each parameter follows:
2939%
2940% o image: the image.
2941%
cristy3ed852e2009-09-05 21:47:34 +00002942% o expression: A mathematical expression.
2943%
2944% o exception: return any errors or warnings in this structure.
2945%
2946*/
2947
2948static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2949{
cristybb503372010-05-27 20:51:26 +00002950 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002951 i;
2952
2953 assert(fx_info != (FxInfo **) NULL);
cristyac245f82012-05-05 17:13:57 +00002954 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
cristy3ed852e2009-09-05 21:47:34 +00002955 if (fx_info[i] != (FxInfo *) NULL)
2956 fx_info[i]=DestroyFxInfo(fx_info[i]);
cristyb41ee102010-10-04 16:46:15 +00002957 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
cristy3ed852e2009-09-05 21:47:34 +00002958 return(fx_info);
2959}
2960
2961static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2962 ExceptionInfo *exception)
2963{
2964 char
2965 *fx_expression;
2966
2967 FxInfo
2968 **fx_info;
2969
cristya19f1d72012-08-07 18:24:38 +00002970 double
cristy3ed852e2009-09-05 21:47:34 +00002971 alpha;
2972
cristybb503372010-05-27 20:51:26 +00002973 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002974 i;
2975
cristybb503372010-05-27 20:51:26 +00002976 size_t
cristy3ed852e2009-09-05 21:47:34 +00002977 number_threads;
2978
cristy9357bdd2012-07-30 12:28:34 +00002979 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
cristyb41ee102010-10-04 16:46:15 +00002980 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +00002981 if (fx_info == (FxInfo **) NULL)
cristy65d161b2012-10-07 20:39:52 +00002982 {
2983 (void) ThrowMagickException(exception,GetMagickModule(),
2984 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
2985 return((FxInfo **) NULL);
2986 }
cristy3ed852e2009-09-05 21:47:34 +00002987 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2988 if (*expression != '@')
2989 fx_expression=ConstantString(expression);
2990 else
2991 fx_expression=FileToString(expression+1,~0,exception);
cristybb503372010-05-27 20:51:26 +00002992 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +00002993 {
cristy65d161b2012-10-07 20:39:52 +00002994 MagickBooleanType
2995 status;
2996
cristydb070952012-04-20 14:33:00 +00002997 fx_info[i]=AcquireFxInfo(image,fx_expression,exception);
cristy3ed852e2009-09-05 21:47:34 +00002998 if (fx_info[i] == (FxInfo *) NULL)
cristy65d161b2012-10-07 20:39:52 +00002999 break;
3000 status=FxPreprocessExpression(fx_info[i],&alpha,exception);
3001 if (status == MagickFalse)
3002 break;
cristy3ed852e2009-09-05 21:47:34 +00003003 }
3004 fx_expression=DestroyString(fx_expression);
cristy65d161b2012-10-07 20:39:52 +00003005 if (i < (ssize_t) number_threads)
3006 fx_info=DestroyFxThreadSet(fx_info);
cristy3ed852e2009-09-05 21:47:34 +00003007 return(fx_info);
3008}
3009
3010MagickExport Image *FxImage(const Image *image,const char *expression,
3011 ExceptionInfo *exception)
3012{
cristy3ed852e2009-09-05 21:47:34 +00003013#define FxImageTag "Fx/Image"
3014
cristyfa112112010-01-04 17:48:07 +00003015 CacheView
cristy79cedc72011-07-25 00:41:15 +00003016 *fx_view,
3017 *image_view;
cristyfa112112010-01-04 17:48:07 +00003018
cristy3ed852e2009-09-05 21:47:34 +00003019 FxInfo
cristyfa112112010-01-04 17:48:07 +00003020 **restrict fx_info;
cristy3ed852e2009-09-05 21:47:34 +00003021
3022 Image
3023 *fx_image;
3024
cristy3ed852e2009-09-05 21:47:34 +00003025 MagickBooleanType
3026 status;
3027
cristybb503372010-05-27 20:51:26 +00003028 MagickOffsetType
3029 progress;
3030
cristybb503372010-05-27 20:51:26 +00003031 ssize_t
3032 y;
3033
cristy3ed852e2009-09-05 21:47:34 +00003034 assert(image != (Image *) NULL);
3035 assert(image->signature == MagickSignature);
3036 if (image->debug != MagickFalse)
3037 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00003038 fx_info=AcquireFxThreadSet(image,expression,exception);
3039 if (fx_info == (FxInfo **) NULL)
cristy65d161b2012-10-07 20:39:52 +00003040 return((Image *) NULL);
3041 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3042 if (fx_image == (Image *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003043 {
cristy3ed852e2009-09-05 21:47:34 +00003044 fx_info=DestroyFxThreadSet(fx_info);
3045 return((Image *) NULL);
3046 }
cristy65d161b2012-10-07 20:39:52 +00003047 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
3048 {
3049 fx_info=DestroyFxThreadSet(fx_info);
3050 fx_image=DestroyImage(fx_image);
3051 return((Image *) NULL);
3052 }
cristy3ed852e2009-09-05 21:47:34 +00003053 /*
3054 Fx image.
3055 */
3056 status=MagickTrue;
3057 progress=0;
cristy46ff2672012-12-14 15:32:26 +00003058 image_view=AcquireVirtualCacheView(image,exception);
3059 fx_view=AcquireAuthenticCacheView(fx_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003060#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003061 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00003062 magick_threads(image,fx_image,fx_image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003063#endif
cristybb503372010-05-27 20:51:26 +00003064 for (y=0; y < (ssize_t) fx_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003065 {
cristy5c9e6f22010-09-17 17:31:01 +00003066 const int
3067 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003068
cristy79cedc72011-07-25 00:41:15 +00003069 register const Quantum
3070 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003071
cristy4c08aed2011-07-01 19:47:50 +00003072 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003073 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003074
cristy79cedc72011-07-25 00:41:15 +00003075 register ssize_t
3076 x;
3077
cristy3ed852e2009-09-05 21:47:34 +00003078 if (status == MagickFalse)
3079 continue;
cristy79cedc72011-07-25 00:41:15 +00003080 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003081 q=QueueCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
cristy79cedc72011-07-25 00:41:15 +00003082 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003083 {
3084 status=MagickFalse;
3085 continue;
3086 }
cristybb503372010-05-27 20:51:26 +00003087 for (x=0; x < (ssize_t) fx_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003088 {
cristy79cedc72011-07-25 00:41:15 +00003089 register ssize_t
3090 i;
3091
3092 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3093 {
cristya19f1d72012-08-07 18:24:38 +00003094 double
cristy79cedc72011-07-25 00:41:15 +00003095 alpha;
3096
cristy5a23c552013-02-13 14:34:28 +00003097 PixelChannel channel=GetPixelChannelChannel(image,i);
3098 PixelTrait traits=GetPixelChannelTraits(image,channel);
3099 PixelTrait fx_traits=GetPixelChannelTraits(fx_image,channel);
cristy010d7d12011-08-31 01:02:48 +00003100 if ((traits == UndefinedPixelTrait) ||
3101 (fx_traits == UndefinedPixelTrait))
cristy79cedc72011-07-25 00:41:15 +00003102 continue;
cristy1eced092012-08-10 23:10:56 +00003103 if (((fx_traits & CopyPixelTrait) != 0) ||
cristy883fde12013-04-08 00:50:13 +00003104 (GetPixelReadMask(image,p) == 0))
cristy79cedc72011-07-25 00:41:15 +00003105 {
cristy0beccfa2011-09-25 20:47:53 +00003106 SetPixelChannel(fx_image,channel,p[i],q);
cristy79cedc72011-07-25 00:41:15 +00003107 continue;
3108 }
3109 alpha=0.0;
cristya382aca2011-12-06 18:22:48 +00003110 (void) FxEvaluateChannelExpression(fx_info[id],channel,x,y,&alpha,
3111 exception);
cristy8cd03c32012-07-07 18:57:59 +00003112 q[i]=ClampToQuantum(QuantumRange*alpha);
cristy79cedc72011-07-25 00:41:15 +00003113 }
3114 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003115 q+=GetPixelChannels(fx_image);
cristy3ed852e2009-09-05 21:47:34 +00003116 }
3117 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3118 status=MagickFalse;
3119 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3120 {
3121 MagickBooleanType
3122 proceed;
3123
cristyb5d5f722009-11-04 03:03:49 +00003124#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003125 #pragma omp critical (MagickCore_FxImage)
cristy3ed852e2009-09-05 21:47:34 +00003126#endif
3127 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3128 if (proceed == MagickFalse)
3129 status=MagickFalse;
3130 }
3131 }
cristy3ed852e2009-09-05 21:47:34 +00003132 fx_view=DestroyCacheView(fx_view);
cristy79cedc72011-07-25 00:41:15 +00003133 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003134 fx_info=DestroyFxThreadSet(fx_info);
3135 if (status == MagickFalse)
3136 fx_image=DestroyImage(fx_image);
3137 return(fx_image);
3138}
3139
3140/*
3141%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3142% %
3143% %
3144% %
3145% I m p l o d e I m a g e %
3146% %
3147% %
3148% %
3149%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3150%
3151% ImplodeImage() creates a new image that is a copy of an existing
3152% one with the image pixels "implode" by the specified percentage. It
3153% allocates the memory necessary for the new Image structure and returns a
3154% pointer to the new image.
3155%
3156% The format of the ImplodeImage method is:
3157%
3158% Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003159% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003160%
3161% A description of each parameter follows:
3162%
3163% o implode_image: Method ImplodeImage returns a pointer to the image
3164% after it is implode. A null image is returned if there is a memory
3165% shortage.
3166%
3167% o image: the image.
3168%
3169% o amount: Define the extent of the implosion.
3170%
cristy76f512e2011-09-12 01:26:56 +00003171% o method: the pixel interpolation method.
3172%
cristy3ed852e2009-09-05 21:47:34 +00003173% o exception: return any errors or warnings in this structure.
3174%
3175*/
3176MagickExport Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003177 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003178{
3179#define ImplodeImageTag "Implode/Image"
3180
cristyfa112112010-01-04 17:48:07 +00003181 CacheView
3182 *image_view,
3183 *implode_view;
3184
cristy3ed852e2009-09-05 21:47:34 +00003185 Image
3186 *implode_image;
3187
cristy3ed852e2009-09-05 21:47:34 +00003188 MagickBooleanType
3189 status;
3190
cristybb503372010-05-27 20:51:26 +00003191 MagickOffsetType
3192 progress;
3193
cristya19f1d72012-08-07 18:24:38 +00003194 double
cristy3ed852e2009-09-05 21:47:34 +00003195 radius;
3196
3197 PointInfo
3198 center,
3199 scale;
3200
cristybb503372010-05-27 20:51:26 +00003201 ssize_t
3202 y;
3203
cristy3ed852e2009-09-05 21:47:34 +00003204 /*
3205 Initialize implode image attributes.
3206 */
3207 assert(image != (Image *) NULL);
3208 assert(image->signature == MagickSignature);
3209 if (image->debug != MagickFalse)
3210 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3211 assert(exception != (ExceptionInfo *) NULL);
3212 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00003213 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3214 exception);
cristy3ed852e2009-09-05 21:47:34 +00003215 if (implode_image == (Image *) NULL)
3216 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003217 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003218 {
cristy3ed852e2009-09-05 21:47:34 +00003219 implode_image=DestroyImage(implode_image);
3220 return((Image *) NULL);
3221 }
cristy4c08aed2011-07-01 19:47:50 +00003222 if (implode_image->background_color.alpha != OpaqueAlpha)
cristy8a46d822012-08-28 23:32:39 +00003223 implode_image->alpha_trait=BlendPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +00003224 /*
3225 Compute scaling factor.
3226 */
3227 scale.x=1.0;
3228 scale.y=1.0;
3229 center.x=0.5*image->columns;
3230 center.y=0.5*image->rows;
3231 radius=center.x;
3232 if (image->columns > image->rows)
3233 scale.y=(double) image->columns/(double) image->rows;
3234 else
3235 if (image->columns < image->rows)
3236 {
3237 scale.x=(double) image->rows/(double) image->columns;
3238 radius=center.y;
3239 }
3240 /*
3241 Implode image.
3242 */
3243 status=MagickTrue;
3244 progress=0;
cristy46ff2672012-12-14 15:32:26 +00003245 image_view=AcquireVirtualCacheView(image,exception);
3246 implode_view=AcquireAuthenticCacheView(implode_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003247#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003248 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00003249 magick_threads(image,implode_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003250#endif
cristybb503372010-05-27 20:51:26 +00003251 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003252 {
cristya19f1d72012-08-07 18:24:38 +00003253 double
cristy3ed852e2009-09-05 21:47:34 +00003254 distance;
3255
3256 PointInfo
3257 delta;
3258
cristy6d188022011-09-12 13:23:33 +00003259 register const Quantum
3260 *restrict p;
3261
cristybb503372010-05-27 20:51:26 +00003262 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003263 x;
3264
cristy4c08aed2011-07-01 19:47:50 +00003265 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003266 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003267
3268 if (status == MagickFalse)
3269 continue;
cristy6d188022011-09-12 13:23:33 +00003270 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003271 q=QueueCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003272 exception);
cristy6d188022011-09-12 13:23:33 +00003273 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003274 {
3275 status=MagickFalse;
3276 continue;
3277 }
cristy3ed852e2009-09-05 21:47:34 +00003278 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00003279 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003280 {
cristy6d188022011-09-12 13:23:33 +00003281 register ssize_t
3282 i;
3283
cristy3ed852e2009-09-05 21:47:34 +00003284 /*
3285 Determine if the pixel is within an ellipse.
3286 */
cristy883fde12013-04-08 00:50:13 +00003287 if (GetPixelReadMask(image,p) == 0)
cristy10a6c612012-01-29 21:41:05 +00003288 {
3289 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);
cristy76f512e2011-09-12 01:26:56 +00003319 status=InterpolatePixelChannels(image,image_view,implode_image,method,
3320 (double) (factor*delta.x/scale.x+center.x),(double) (factor*delta.y/
3321 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);
3342 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003343 if (status == MagickFalse)
3344 implode_image=DestroyImage(implode_image);
3345 return(implode_image);
3346}
3347
3348/*
3349%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3350% %
3351% %
3352% %
3353% M o r p h I m a g e s %
3354% %
3355% %
3356% %
3357%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3358%
3359% The MorphImages() method requires a minimum of two images. The first
3360% image is transformed into the second by a number of intervening images
3361% as specified by frames.
3362%
3363% The format of the MorphImage method is:
3364%
cristybb503372010-05-27 20:51:26 +00003365% Image *MorphImages(const Image *image,const size_t number_frames,
cristy3ed852e2009-09-05 21:47:34 +00003366% ExceptionInfo *exception)
3367%
3368% A description of each parameter follows:
3369%
3370% o image: the image.
3371%
3372% o number_frames: Define the number of in-between image to generate.
3373% The more in-between frames, the smoother the morph.
3374%
3375% o exception: return any errors or warnings in this structure.
3376%
3377*/
cristy7de7f742012-12-14 00:28:27 +00003378MagickExport Image *MorphImages(const Image *image,const size_t number_frames,
3379 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003380{
3381#define MorphImageTag "Morph/Image"
3382
cristyfab83642012-12-14 00:31:38 +00003383 double
3384 alpha,
3385 beta;
3386
cristy3ed852e2009-09-05 21:47:34 +00003387 Image
3388 *morph_image,
3389 *morph_images;
3390
cristy9d314ff2011-03-09 01:30:28 +00003391 MagickBooleanType
3392 status;
cristy3ed852e2009-09-05 21:47:34 +00003393
3394 MagickOffsetType
3395 scene;
3396
cristy3ed852e2009-09-05 21:47:34 +00003397 register const Image
3398 *next;
3399
cristybb503372010-05-27 20:51:26 +00003400 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003401 i;
3402
cristy9d314ff2011-03-09 01:30:28 +00003403 ssize_t
3404 y;
cristy3ed852e2009-09-05 21:47:34 +00003405
3406 /*
3407 Clone first frame in sequence.
3408 */
3409 assert(image != (Image *) NULL);
3410 assert(image->signature == MagickSignature);
3411 if (image->debug != MagickFalse)
3412 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3413 assert(exception != (ExceptionInfo *) NULL);
3414 assert(exception->signature == MagickSignature);
3415 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3416 if (morph_images == (Image *) NULL)
3417 return((Image *) NULL);
3418 if (GetNextImageInList(image) == (Image *) NULL)
3419 {
3420 /*
3421 Morph single image.
3422 */
cristybb503372010-05-27 20:51:26 +00003423 for (i=1; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003424 {
3425 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3426 if (morph_image == (Image *) NULL)
3427 {
3428 morph_images=DestroyImageList(morph_images);
3429 return((Image *) NULL);
3430 }
3431 AppendImageToList(&morph_images,morph_image);
cristy8b27a6d2010-02-14 03:31:15 +00003432 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003433 {
cristy8b27a6d2010-02-14 03:31:15 +00003434 MagickBooleanType
3435 proceed;
3436
cristybb503372010-05-27 20:51:26 +00003437 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3438 number_frames);
cristy8b27a6d2010-02-14 03:31:15 +00003439 if (proceed == MagickFalse)
3440 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00003441 }
3442 }
3443 return(GetFirstImageInList(morph_images));
3444 }
3445 /*
3446 Morph image sequence.
3447 */
3448 status=MagickTrue;
3449 scene=0;
3450 next=image;
3451 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3452 {
cristybb503372010-05-27 20:51:26 +00003453 for (i=0; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003454 {
3455 CacheView
3456 *image_view,
3457 *morph_view;
3458
cristya19f1d72012-08-07 18:24:38 +00003459 beta=(double) (i+1.0)/(double) (number_frames+1.0);
cristy3ed852e2009-09-05 21:47:34 +00003460 alpha=1.0-beta;
cristy15b98cd2010-09-12 19:42:50 +00003461 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
cristyaa2c16c2012-03-25 22:21:35 +00003462 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*next->rows+beta*
3463 GetNextImageInList(next)->rows+0.5),next->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +00003464 if (morph_image == (Image *) NULL)
3465 {
3466 morph_images=DestroyImageList(morph_images);
3467 return((Image *) NULL);
3468 }
cristy1707c6c2012-01-18 23:30:54 +00003469 status=SetImageStorageClass(morph_image,DirectClass,exception);
3470 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003471 {
cristy3ed852e2009-09-05 21:47:34 +00003472 morph_image=DestroyImage(morph_image);
3473 return((Image *) NULL);
3474 }
3475 AppendImageToList(&morph_images,morph_image);
3476 morph_images=GetLastImageInList(morph_images);
cristy15b98cd2010-09-12 19:42:50 +00003477 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
cristyaa2c16c2012-03-25 22:21:35 +00003478 morph_images->rows,GetNextImageInList(next)->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +00003479 if (morph_image == (Image *) NULL)
3480 {
3481 morph_images=DestroyImageList(morph_images);
3482 return((Image *) NULL);
3483 }
cristy46ff2672012-12-14 15:32:26 +00003484 image_view=AcquireVirtualCacheView(morph_image,exception);
3485 morph_view=AcquireAuthenticCacheView(morph_images,exception);
cristyb5d5f722009-11-04 03:03:49 +00003486#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003487 #pragma omp parallel for schedule(static,4) shared(status) \
cristy5e6b2592012-12-19 14:08:11 +00003488 magick_threads(morph_image,morph_image,morph_image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003489#endif
cristybb503372010-05-27 20:51:26 +00003490 for (y=0; y < (ssize_t) morph_images->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003491 {
3492 MagickBooleanType
3493 sync;
3494
cristy4c08aed2011-07-01 19:47:50 +00003495 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003496 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003497
cristybb503372010-05-27 20:51:26 +00003498 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003499 x;
3500
cristy4c08aed2011-07-01 19:47:50 +00003501 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003502 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003503
3504 if (status == MagickFalse)
3505 continue;
3506 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3507 exception);
3508 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3509 exception);
cristy4c08aed2011-07-01 19:47:50 +00003510 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003511 {
3512 status=MagickFalse;
3513 continue;
3514 }
cristybb503372010-05-27 20:51:26 +00003515 for (x=0; x < (ssize_t) morph_images->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003516 {
cristy1707c6c2012-01-18 23:30:54 +00003517 register ssize_t
3518 i;
3519
cristy10a6c612012-01-29 21:41:05 +00003520 for (i=0; i < (ssize_t) GetPixelChannels(morph_image); i++)
cristy1707c6c2012-01-18 23:30:54 +00003521 {
cristy5a23c552013-02-13 14:34:28 +00003522 PixelChannel channel=GetPixelChannelChannel(image,i);
3523 PixelTrait traits=GetPixelChannelTraits(image,channel);
3524 PixelTrait morph_traits=GetPixelChannelTraits(morph_image,channel);
cristy1707c6c2012-01-18 23:30:54 +00003525 if ((traits == UndefinedPixelTrait) ||
3526 (morph_traits == UndefinedPixelTrait))
3527 continue;
cristy1eced092012-08-10 23:10:56 +00003528 if (((morph_traits & CopyPixelTrait) != 0) ||
cristy883fde12013-04-08 00:50:13 +00003529 (GetPixelReadMask(image,p) == 0))
cristy1707c6c2012-01-18 23:30:54 +00003530 {
3531 SetPixelChannel(morph_image,channel,p[i],q);
3532 continue;
3533 }
3534 SetPixelChannel(morph_image,channel,ClampToQuantum(alpha*
3535 GetPixelChannel(morph_images,channel,q)+beta*p[i]),q);
3536 }
cristyed231572011-07-14 02:18:59 +00003537 p+=GetPixelChannels(morph_image);
3538 q+=GetPixelChannels(morph_images);
cristy3ed852e2009-09-05 21:47:34 +00003539 }
3540 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3541 if (sync == MagickFalse)
3542 status=MagickFalse;
3543 }
3544 morph_view=DestroyCacheView(morph_view);
3545 image_view=DestroyCacheView(image_view);
3546 morph_image=DestroyImage(morph_image);
3547 }
cristybb503372010-05-27 20:51:26 +00003548 if (i < (ssize_t) number_frames)
cristy3ed852e2009-09-05 21:47:34 +00003549 break;
3550 /*
3551 Clone last frame in sequence.
3552 */
3553 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3554 if (morph_image == (Image *) NULL)
3555 {
3556 morph_images=DestroyImageList(morph_images);
3557 return((Image *) NULL);
3558 }
3559 AppendImageToList(&morph_images,morph_image);
3560 morph_images=GetLastImageInList(morph_images);
3561 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3562 {
3563 MagickBooleanType
3564 proceed;
3565
cristyb5d5f722009-11-04 03:03:49 +00003566#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003567 #pragma omp critical (MagickCore_MorphImages)
cristy3ed852e2009-09-05 21:47:34 +00003568#endif
3569 proceed=SetImageProgress(image,MorphImageTag,scene,
3570 GetImageListLength(image));
3571 if (proceed == MagickFalse)
3572 status=MagickFalse;
3573 }
3574 scene++;
3575 }
3576 if (GetNextImageInList(next) != (Image *) NULL)
3577 {
3578 morph_images=DestroyImageList(morph_images);
3579 return((Image *) NULL);
3580 }
3581 return(GetFirstImageInList(morph_images));
3582}
3583
3584/*
3585%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3586% %
3587% %
3588% %
3589% P l a s m a I m a g e %
3590% %
3591% %
3592% %
3593%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3594%
3595% PlasmaImage() initializes an image with plasma fractal values. The image
3596% must be initialized with a base color and the random number generator
3597% seeded before this method is called.
3598%
3599% The format of the PlasmaImage method is:
3600%
3601% MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
cristy5cbc0162011-08-29 00:36:28 +00003602% size_t attenuate,size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003603%
3604% A description of each parameter follows:
3605%
3606% o image: the image.
3607%
3608% o segment: Define the region to apply plasma fractals values.
3609%
glennrp7dae1ca2010-09-16 12:17:35 +00003610% o attenuate: Define the plasma attenuation factor.
cristy3ed852e2009-09-05 21:47:34 +00003611%
3612% o depth: Limit the plasma recursion depth.
3613%
cristy5cbc0162011-08-29 00:36:28 +00003614% o exception: return any errors or warnings in this structure.
3615%
cristy3ed852e2009-09-05 21:47:34 +00003616*/
3617
3618static inline Quantum PlasmaPixel(RandomInfo *random_info,
cristya19f1d72012-08-07 18:24:38 +00003619 const double pixel,const double noise)
cristy3ed852e2009-09-05 21:47:34 +00003620{
3621 Quantum
3622 plasma;
3623
cristyce70c172010-01-07 17:15:30 +00003624 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
cristy3ed852e2009-09-05 21:47:34 +00003625 noise/2.0);
3626 return(plasma);
3627}
3628
cristyda1f9c12011-10-02 21:39:49 +00003629static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3630 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3631 const SegmentInfo *segment,size_t attenuate,size_t depth,
3632 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003633{
cristya19f1d72012-08-07 18:24:38 +00003634 double
cristy3ed852e2009-09-05 21:47:34 +00003635 plasma;
3636
cristyda1f9c12011-10-02 21:39:49 +00003637 register const Quantum
3638 *restrict u,
3639 *restrict v;
3640
3641 register Quantum
3642 *restrict q;
3643
3644 register ssize_t
3645 i;
cristy3ed852e2009-09-05 21:47:34 +00003646
cristy9d314ff2011-03-09 01:30:28 +00003647 ssize_t
3648 x,
3649 x_mid,
3650 y,
3651 y_mid;
3652
cristy3ed852e2009-09-05 21:47:34 +00003653 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3654 return(MagickTrue);
3655 if (depth != 0)
3656 {
3657 SegmentInfo
3658 local_info;
3659
3660 /*
3661 Divide the area into quadrants and recurse.
3662 */
3663 depth--;
3664 attenuate++;
cristybb503372010-05-27 20:51:26 +00003665 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3666 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003667 local_info=(*segment);
3668 local_info.x2=(double) x_mid;
3669 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003670 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3671 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003672 local_info=(*segment);
3673 local_info.y1=(double) y_mid;
3674 local_info.x2=(double) x_mid;
cristyda1f9c12011-10-02 21:39:49 +00003675 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3676 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003677 local_info=(*segment);
3678 local_info.x1=(double) x_mid;
3679 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003680 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3681 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003682 local_info=(*segment);
3683 local_info.x1=(double) x_mid;
3684 local_info.y1=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003685 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3686 &local_info,attenuate,depth,exception));
cristy3ed852e2009-09-05 21:47:34 +00003687 }
cristybb503372010-05-27 20:51:26 +00003688 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3689 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003690 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3691 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3692 return(MagickFalse);
3693 /*
3694 Average pixels and apply plasma.
3695 */
cristya19f1d72012-08-07 18:24:38 +00003696 plasma=(double) QuantumRange/(2.0*attenuate);
cristy3ed852e2009-09-05 21:47:34 +00003697 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3698 {
cristy3ed852e2009-09-05 21:47:34 +00003699 /*
3700 Left pixel.
3701 */
cristybb503372010-05-27 20:51:26 +00003702 x=(ssize_t) ceil(segment->x1-0.5);
cristy1707c6c2012-01-18 23:30:54 +00003703 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),1,1,
3704 exception);
3705 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),1,1,
3706 exception);
cristyc5c6f662010-09-22 14:23:02 +00003707 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003708 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3709 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003710 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003711 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3712 {
cristy5a23c552013-02-13 14:34:28 +00003713 PixelChannel channel=GetPixelChannelChannel(image,i);
3714 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003715 if (traits == UndefinedPixelTrait)
3716 continue;
3717 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3718 }
cristyc5c6f662010-09-22 14:23:02 +00003719 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003720 if (segment->x1 != segment->x2)
3721 {
3722 /*
3723 Right pixel.
3724 */
cristybb503372010-05-27 20:51:26 +00003725 x=(ssize_t) ceil(segment->x2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003726 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3727 1,1,exception);
3728 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3729 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003730 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003731 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3732 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003733 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003734 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3735 {
cristy5a23c552013-02-13 14:34:28 +00003736 PixelChannel channel=GetPixelChannelChannel(image,i);
3737 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003738 if (traits == UndefinedPixelTrait)
3739 continue;
3740 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3741 }
cristyc5c6f662010-09-22 14:23:02 +00003742 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003743 }
3744 }
3745 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3746 {
3747 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3748 {
cristy3ed852e2009-09-05 21:47:34 +00003749 /*
3750 Bottom pixel.
3751 */
cristybb503372010-05-27 20:51:26 +00003752 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003753 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3754 1,1,exception);
3755 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3756 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003757 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003758 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3759 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003760 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003761 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3762 {
cristy5a23c552013-02-13 14:34:28 +00003763 PixelChannel channel=GetPixelChannelChannel(image,i);
3764 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003765 if (traits == UndefinedPixelTrait)
3766 continue;
3767 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3768 }
cristyc5c6f662010-09-22 14:23:02 +00003769 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003770 }
3771 if (segment->y1 != segment->y2)
3772 {
cristy3ed852e2009-09-05 21:47:34 +00003773 /*
3774 Top pixel.
3775 */
cristybb503372010-05-27 20:51:26 +00003776 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003777 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3778 1,1,exception);
3779 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3780 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003781 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003782 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3783 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003784 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003785 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3786 {
cristy5a23c552013-02-13 14:34:28 +00003787 PixelChannel channel=GetPixelChannelChannel(image,i);
3788 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003789 if (traits == UndefinedPixelTrait)
3790 continue;
3791 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3792 }
cristyc5c6f662010-09-22 14:23:02 +00003793 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003794 }
3795 }
3796 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3797 {
cristy3ed852e2009-09-05 21:47:34 +00003798 /*
3799 Middle pixel.
3800 */
cristybb503372010-05-27 20:51:26 +00003801 x=(ssize_t) ceil(segment->x1-0.5);
3802 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003803 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
cristybb503372010-05-27 20:51:26 +00003804 x=(ssize_t) ceil(segment->x2-0.5);
3805 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003806 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003807 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003808 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3809 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003810 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003811 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3812 {
cristy5a23c552013-02-13 14:34:28 +00003813 PixelChannel channel=GetPixelChannelChannel(image,i);
3814 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003815 if (traits == UndefinedPixelTrait)
3816 continue;
3817 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3818 }
cristyc5c6f662010-09-22 14:23:02 +00003819 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003820 }
3821 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3822 return(MagickTrue);
3823 return(MagickFalse);
3824}
cristyda1f9c12011-10-02 21:39:49 +00003825
cristy3ed852e2009-09-05 21:47:34 +00003826MagickExport MagickBooleanType PlasmaImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00003827 const SegmentInfo *segment,size_t attenuate,size_t depth,
3828 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003829{
cristyc5c6f662010-09-22 14:23:02 +00003830 CacheView
cristyda1f9c12011-10-02 21:39:49 +00003831 *image_view,
3832 *u_view,
3833 *v_view;
cristyc5c6f662010-09-22 14:23:02 +00003834
cristy3ed852e2009-09-05 21:47:34 +00003835 MagickBooleanType
3836 status;
3837
3838 RandomInfo
3839 *random_info;
3840
3841 if (image->debug != MagickFalse)
3842 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3843 assert(image != (Image *) NULL);
3844 assert(image->signature == MagickSignature);
3845 if (image->debug != MagickFalse)
3846 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy5cbc0162011-08-29 00:36:28 +00003847 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristyc5c6f662010-09-22 14:23:02 +00003848 return(MagickFalse);
cristy46ff2672012-12-14 15:32:26 +00003849 image_view=AcquireAuthenticCacheView(image,exception);
3850 u_view=AcquireVirtualCacheView(image,exception);
3851 v_view=AcquireVirtualCacheView(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003852 random_info=AcquireRandomInfo();
cristyda1f9c12011-10-02 21:39:49 +00003853 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3854 attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003855 random_info=DestroyRandomInfo(random_info);
cristyda1f9c12011-10-02 21:39:49 +00003856 v_view=DestroyCacheView(v_view);
3857 u_view=DestroyCacheView(u_view);
cristyc5c6f662010-09-22 14:23:02 +00003858 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003859 return(status);
3860}
3861
3862/*
3863%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3864% %
3865% %
3866% %
3867% P o l a r o i d I m a g e %
3868% %
3869% %
3870% %
3871%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3872%
3873% PolaroidImage() simulates a Polaroid picture.
3874%
3875% The format of the AnnotateImage method is:
3876%
3877% Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003878% const char *caption,const double angle,
3879% const PixelInterpolateMethod method,ExceptionInfo exception)
cristy3ed852e2009-09-05 21:47:34 +00003880%
3881% A description of each parameter follows:
3882%
3883% o image: the image.
3884%
3885% o draw_info: the draw info.
3886%
cristye9e3d382011-12-14 01:50:13 +00003887% o caption: the Polaroid caption.
3888%
cristycee97112010-05-28 00:44:52 +00003889% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00003890%
cristy5c4e2582011-09-11 19:21:03 +00003891% o method: the pixel interpolation method.
3892%
cristy3ed852e2009-09-05 21:47:34 +00003893% o exception: return any errors or warnings in this structure.
3894%
3895*/
3896MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003897 const char *caption,const double angle,const PixelInterpolateMethod method,
cristy5c4e2582011-09-11 19:21:03 +00003898 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003899{
cristy3ed852e2009-09-05 21:47:34 +00003900 Image
3901 *bend_image,
3902 *caption_image,
3903 *flop_image,
3904 *picture_image,
3905 *polaroid_image,
3906 *rotate_image,
3907 *trim_image;
3908
cristybb503372010-05-27 20:51:26 +00003909 size_t
cristy3ed852e2009-09-05 21:47:34 +00003910 height;
3911
cristy9d314ff2011-03-09 01:30:28 +00003912 ssize_t
3913 quantum;
3914
cristy3ed852e2009-09-05 21:47:34 +00003915 /*
3916 Simulate a Polaroid picture.
3917 */
3918 assert(image != (Image *) NULL);
3919 assert(image->signature == MagickSignature);
3920 if (image->debug != MagickFalse)
3921 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3922 assert(exception != (ExceptionInfo *) NULL);
3923 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00003924 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
cristy3ed852e2009-09-05 21:47:34 +00003925 image->rows)/25.0,10.0);
3926 height=image->rows+2*quantum;
3927 caption_image=(Image *) NULL;
cristye9e3d382011-12-14 01:50:13 +00003928 if (caption != (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003929 {
3930 char
cristye9e3d382011-12-14 01:50:13 +00003931 geometry[MaxTextExtent],
3932 *text;
cristy3ed852e2009-09-05 21:47:34 +00003933
3934 DrawInfo
3935 *annotate_info;
3936
cristy3ed852e2009-09-05 21:47:34 +00003937 MagickBooleanType
3938 status;
3939
cristy9d314ff2011-03-09 01:30:28 +00003940 ssize_t
3941 count;
3942
cristy3ed852e2009-09-05 21:47:34 +00003943 TypeMetric
3944 metrics;
3945
3946 /*
3947 Generate caption image.
3948 */
3949 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3950 if (caption_image == (Image *) NULL)
3951 return((Image *) NULL);
3952 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
cristye9e3d382011-12-14 01:50:13 +00003953 text=InterpretImageProperties((ImageInfo *) NULL,(Image *) image,caption,
3954 exception);
3955 (void) CloneString(&annotate_info->text,text);
cristy6b1d05e2010-09-22 19:17:27 +00003956 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
cristye9e3d382011-12-14 01:50:13 +00003957 &text,exception);
3958 status=SetImageExtent(caption_image,image->columns,(size_t) ((count+1)*
3959 (metrics.ascent-metrics.descent)+0.5),exception);
cristy3ed852e2009-09-05 21:47:34 +00003960 if (status == MagickFalse)
3961 caption_image=DestroyImage(caption_image);
3962 else
3963 {
3964 caption_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003965 (void) SetImageBackgroundColor(caption_image,exception);
cristye9e3d382011-12-14 01:50:13 +00003966 (void) CloneString(&annotate_info->text,text);
cristyb51dff52011-05-19 16:55:47 +00003967 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
cristy3ed852e2009-09-05 21:47:34 +00003968 metrics.ascent);
3969 if (annotate_info->gravity == UndefinedGravity)
3970 (void) CloneString(&annotate_info->geometry,AcquireString(
3971 geometry));
cristy5cbc0162011-08-29 00:36:28 +00003972 (void) AnnotateImage(caption_image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003973 height+=caption_image->rows;
3974 }
3975 annotate_info=DestroyDrawInfo(annotate_info);
cristye9e3d382011-12-14 01:50:13 +00003976 text=DestroyString(text);
cristy3ed852e2009-09-05 21:47:34 +00003977 }
3978 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
3979 exception);
3980 if (picture_image == (Image *) NULL)
3981 {
3982 if (caption_image != (Image *) NULL)
3983 caption_image=DestroyImage(caption_image);
3984 return((Image *) NULL);
3985 }
3986 picture_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003987 (void) SetImageBackgroundColor(picture_image,exception);
cristy39172402012-03-30 13:04:39 +00003988 (void) CompositeImage(picture_image,image,OverCompositeOp,MagickTrue,quantum,
cristyfeb3e962012-03-29 17:25:55 +00003989 quantum,exception);
cristy3ed852e2009-09-05 21:47:34 +00003990 if (caption_image != (Image *) NULL)
3991 {
cristyfeb3e962012-03-29 17:25:55 +00003992 (void) CompositeImage(picture_image,caption_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00003993 MagickTrue,quantum,(ssize_t) (image->rows+3*quantum/2),exception);
cristy3ed852e2009-09-05 21:47:34 +00003994 caption_image=DestroyImage(caption_image);
3995 }
cristy9950d572011-10-01 18:22:35 +00003996 (void) QueryColorCompliance("none",AllCompliance,
3997 &picture_image->background_color,exception);
cristy63240882011-08-05 19:05:27 +00003998 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00003999 rotate_image=RotateImage(picture_image,90.0,exception);
4000 picture_image=DestroyImage(picture_image);
4001 if (rotate_image == (Image *) NULL)
4002 return((Image *) NULL);
4003 picture_image=rotate_image;
4004 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
cristy5c4e2582011-09-11 19:21:03 +00004005 picture_image->columns,method,exception);
cristy3ed852e2009-09-05 21:47:34 +00004006 picture_image=DestroyImage(picture_image);
4007 if (bend_image == (Image *) NULL)
4008 return((Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00004009 picture_image=bend_image;
4010 rotate_image=RotateImage(picture_image,-90.0,exception);
4011 picture_image=DestroyImage(picture_image);
4012 if (rotate_image == (Image *) NULL)
4013 return((Image *) NULL);
4014 picture_image=rotate_image;
4015 picture_image->background_color=image->background_color;
anthonyf46d4262012-03-26 03:30:34 +00004016 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
cristy3ed852e2009-09-05 21:47:34 +00004017 exception);
4018 if (polaroid_image == (Image *) NULL)
4019 {
4020 picture_image=DestroyImage(picture_image);
4021 return(picture_image);
4022 }
4023 flop_image=FlopImage(polaroid_image,exception);
4024 polaroid_image=DestroyImage(polaroid_image);
4025 if (flop_image == (Image *) NULL)
4026 {
4027 picture_image=DestroyImage(picture_image);
4028 return(picture_image);
4029 }
4030 polaroid_image=flop_image;
cristyfeb3e962012-03-29 17:25:55 +00004031 (void) CompositeImage(polaroid_image,picture_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00004032 MagickTrue,(ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
cristy3ed852e2009-09-05 21:47:34 +00004033 picture_image=DestroyImage(picture_image);
cristy9950d572011-10-01 18:22:35 +00004034 (void) QueryColorCompliance("none",AllCompliance,
4035 &polaroid_image->background_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00004036 rotate_image=RotateImage(polaroid_image,angle,exception);
4037 polaroid_image=DestroyImage(polaroid_image);
4038 if (rotate_image == (Image *) NULL)
4039 return((Image *) NULL);
4040 polaroid_image=rotate_image;
4041 trim_image=TrimImage(polaroid_image,exception);
4042 polaroid_image=DestroyImage(polaroid_image);
4043 if (trim_image == (Image *) NULL)
4044 return((Image *) NULL);
4045 polaroid_image=trim_image;
4046 return(polaroid_image);
4047}
4048
4049/*
4050%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4051% %
4052% %
4053% %
cristy3ed852e2009-09-05 21:47:34 +00004054% S e p i a T o n e I m a g e %
4055% %
4056% %
4057% %
4058%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4059%
4060% MagickSepiaToneImage() applies a special effect to the image, similar to the
4061% effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4062% 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4063% threshold of 80% is a good starting point for a reasonable tone.
4064%
4065% The format of the SepiaToneImage method is:
4066%
4067% Image *SepiaToneImage(const Image *image,const double threshold,
4068% ExceptionInfo *exception)
4069%
4070% A description of each parameter follows:
4071%
4072% o image: the image.
4073%
4074% o threshold: the tone threshold.
4075%
4076% o exception: return any errors or warnings in this structure.
4077%
4078*/
4079MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4080 ExceptionInfo *exception)
4081{
4082#define SepiaToneImageTag "SepiaTone/Image"
4083
cristyc4c8d132010-01-07 01:58:38 +00004084 CacheView
4085 *image_view,
4086 *sepia_view;
4087
cristy3ed852e2009-09-05 21:47:34 +00004088 Image
4089 *sepia_image;
4090
cristy3ed852e2009-09-05 21:47:34 +00004091 MagickBooleanType
4092 status;
4093
cristybb503372010-05-27 20:51:26 +00004094 MagickOffsetType
4095 progress;
4096
4097 ssize_t
4098 y;
4099
cristy3ed852e2009-09-05 21:47:34 +00004100 /*
4101 Initialize sepia-toned image attributes.
4102 */
4103 assert(image != (const Image *) NULL);
4104 assert(image->signature == MagickSignature);
4105 if (image->debug != MagickFalse)
4106 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4107 assert(exception != (ExceptionInfo *) NULL);
4108 assert(exception->signature == MagickSignature);
cristy1707c6c2012-01-18 23:30:54 +00004109 sepia_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004110 if (sepia_image == (Image *) NULL)
4111 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004112 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004113 {
cristy3ed852e2009-09-05 21:47:34 +00004114 sepia_image=DestroyImage(sepia_image);
4115 return((Image *) NULL);
4116 }
4117 /*
4118 Tone each row of the image.
4119 */
4120 status=MagickTrue;
4121 progress=0;
cristy46ff2672012-12-14 15:32:26 +00004122 image_view=AcquireVirtualCacheView(image,exception);
4123 sepia_view=AcquireAuthenticCacheView(sepia_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00004124#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00004125 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00004126 magick_threads(image,sepia_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00004127#endif
cristybb503372010-05-27 20:51:26 +00004128 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004129 {
cristy4c08aed2011-07-01 19:47:50 +00004130 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004131 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004132
cristybb503372010-05-27 20:51:26 +00004133 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004134 x;
4135
cristy4c08aed2011-07-01 19:47:50 +00004136 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004137 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004138
4139 if (status == MagickFalse)
4140 continue;
4141 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00004142 q=GetCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00004143 exception);
cristy4c08aed2011-07-01 19:47:50 +00004144 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004145 {
4146 status=MagickFalse;
4147 continue;
4148 }
cristybb503372010-05-27 20:51:26 +00004149 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004150 {
cristya19f1d72012-08-07 18:24:38 +00004151 double
cristy3ed852e2009-09-05 21:47:34 +00004152 intensity,
4153 tone;
4154
cristyf13c5942012-08-08 23:50:11 +00004155 intensity=GetPixelIntensity(image,p);
cristya19f1d72012-08-07 18:24:38 +00004156 tone=intensity > threshold ? (double) QuantumRange : intensity+
4157 (double) QuantumRange-threshold;
cristy4c08aed2011-07-01 19:47:50 +00004158 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
cristya19f1d72012-08-07 18:24:38 +00004159 tone=intensity > (7.0*threshold/6.0) ? (double) QuantumRange :
4160 intensity+(double) QuantumRange-7.0*threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004161 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004162 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004163 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004164 tone=threshold/7.0;
cristya19f1d72012-08-07 18:24:38 +00004165 if ((double) GetPixelGreen(image,q) < tone)
cristy4c08aed2011-07-01 19:47:50 +00004166 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
cristya19f1d72012-08-07 18:24:38 +00004167 if ((double) GetPixelBlue(image,q) < tone)
cristy4c08aed2011-07-01 19:47:50 +00004168 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristyed231572011-07-14 02:18:59 +00004169 p+=GetPixelChannels(image);
4170 q+=GetPixelChannels(sepia_image);
cristy3ed852e2009-09-05 21:47:34 +00004171 }
4172 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4173 status=MagickFalse;
4174 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4175 {
4176 MagickBooleanType
4177 proceed;
4178
cristyb5d5f722009-11-04 03:03:49 +00004179#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004180 #pragma omp critical (MagickCore_SepiaToneImage)
cristy3ed852e2009-09-05 21:47:34 +00004181#endif
4182 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4183 image->rows);
4184 if (proceed == MagickFalse)
4185 status=MagickFalse;
4186 }
4187 }
4188 sepia_view=DestroyCacheView(sepia_view);
4189 image_view=DestroyCacheView(image_view);
cristye23ec9d2011-08-16 18:15:40 +00004190 (void) NormalizeImage(sepia_image,exception);
4191 (void) ContrastImage(sepia_image,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004192 if (status == MagickFalse)
4193 sepia_image=DestroyImage(sepia_image);
4194 return(sepia_image);
4195}
4196
4197/*
4198%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4199% %
4200% %
4201% %
4202% S h a d o w I m a g e %
4203% %
4204% %
4205% %
4206%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4207%
4208% ShadowImage() simulates a shadow from the specified image and returns it.
4209%
4210% The format of the ShadowImage method is:
4211%
cristy70cddf72011-12-10 22:42:42 +00004212% Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004213% const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4214% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004215%
4216% A description of each parameter follows:
4217%
4218% o image: the image.
4219%
cristy70cddf72011-12-10 22:42:42 +00004220% o alpha: percentage transparency.
cristy3ed852e2009-09-05 21:47:34 +00004221%
4222% o sigma: the standard deviation of the Gaussian, in pixels.
4223%
4224% o x_offset: the shadow x-offset.
4225%
4226% o y_offset: the shadow y-offset.
4227%
4228% o exception: return any errors or warnings in this structure.
4229%
4230*/
cristy70cddf72011-12-10 22:42:42 +00004231MagickExport Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004232 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4233 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004234{
4235#define ShadowImageTag "Shadow/Image"
4236
cristy70cddf72011-12-10 22:42:42 +00004237 CacheView
4238 *image_view;
4239
cristybd5a96c2011-08-21 00:04:26 +00004240 ChannelType
4241 channel_mask;
4242
cristy3ed852e2009-09-05 21:47:34 +00004243 Image
4244 *border_image,
4245 *clone_image,
4246 *shadow_image;
4247
cristy70cddf72011-12-10 22:42:42 +00004248 MagickBooleanType
4249 status;
4250
cristy3ed852e2009-09-05 21:47:34 +00004251 RectangleInfo
4252 border_info;
4253
cristy70cddf72011-12-10 22:42:42 +00004254 ssize_t
4255 y;
4256
cristy3ed852e2009-09-05 21:47:34 +00004257 assert(image != (Image *) NULL);
4258 assert(image->signature == MagickSignature);
4259 if (image->debug != MagickFalse)
4260 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4261 assert(exception != (ExceptionInfo *) NULL);
4262 assert(exception->signature == MagickSignature);
4263 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4264 if (clone_image == (Image *) NULL)
4265 return((Image *) NULL);
cristya6400b12013-03-15 12:20:18 +00004266 if (IsGrayColorspace(image->colorspace) != MagickFalse)
4267 (void) TransformImageColorspace(clone_image,sRGBColorspace,exception);
cristy0ce08762012-06-30 01:33:18 +00004268 (void) SetImageVirtualPixelMethod(clone_image,TransparentVirtualPixelMethod,
cristy387430f2012-02-07 13:09:46 +00004269 exception);
cristybb503372010-05-27 20:51:26 +00004270 border_info.width=(size_t) floor(2.0*sigma+0.5);
4271 border_info.height=(size_t) floor(2.0*sigma+0.5);
cristy3ed852e2009-09-05 21:47:34 +00004272 border_info.x=0;
4273 border_info.y=0;
cristy9950d572011-10-01 18:22:35 +00004274 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4275 exception);
cristy8a46d822012-08-28 23:32:39 +00004276 clone_image->alpha_trait=BlendPixelTrait;
cristy70cddf72011-12-10 22:42:42 +00004277 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
cristy3ed852e2009-09-05 21:47:34 +00004278 clone_image=DestroyImage(clone_image);
4279 if (border_image == (Image *) NULL)
4280 return((Image *) NULL);
cristy8a46d822012-08-28 23:32:39 +00004281 if (border_image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00004282 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004283 /*
4284 Shadow image.
4285 */
cristy70cddf72011-12-10 22:42:42 +00004286 status=MagickTrue;
cristy46ff2672012-12-14 15:32:26 +00004287 image_view=AcquireAuthenticCacheView(border_image,exception);
cristy70cddf72011-12-10 22:42:42 +00004288 for (y=0; y < (ssize_t) border_image->rows; y++)
4289 {
4290 PixelInfo
4291 background_color;
4292
4293 register Quantum
4294 *restrict q;
4295
4296 register ssize_t
4297 x;
4298
4299 if (status == MagickFalse)
4300 continue;
4301 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
4302 exception);
4303 if (q == (Quantum *) NULL)
4304 {
4305 status=MagickFalse;
4306 continue;
4307 }
4308 background_color=border_image->background_color;
cristy8a46d822012-08-28 23:32:39 +00004309 background_color.alpha_trait=BlendPixelTrait;
cristy70cddf72011-12-10 22:42:42 +00004310 for (x=0; x < (ssize_t) border_image->columns; x++)
4311 {
cristy8a46d822012-08-28 23:32:39 +00004312 if (border_image->alpha_trait == BlendPixelTrait)
cristy70cddf72011-12-10 22:42:42 +00004313 background_color.alpha=GetPixelAlpha(border_image,q)*alpha/100.0;
4314 SetPixelInfoPixel(border_image,&background_color,q);
4315 q+=GetPixelChannels(border_image);
4316 }
4317 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4318 status=MagickFalse;
4319 }
4320 image_view=DestroyCacheView(image_view);
4321 if (status == MagickFalse)
4322 {
4323 border_image=DestroyImage(border_image);
4324 return((Image *) NULL);
4325 }
cristycf1296e2012-08-26 23:40:49 +00004326 channel_mask=SetImageChannelMask(border_image,AlphaChannel);
cristyaa2c16c2012-03-25 22:21:35 +00004327 shadow_image=BlurImage(border_image,0.0,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00004328 border_image=DestroyImage(border_image);
4329 if (shadow_image == (Image *) NULL)
4330 return((Image *) NULL);
cristycf1296e2012-08-26 23:40:49 +00004331 (void) SetPixelChannelMask(shadow_image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00004332 if (shadow_image->page.width == 0)
4333 shadow_image->page.width=shadow_image->columns;
4334 if (shadow_image->page.height == 0)
4335 shadow_image->page.height=shadow_image->rows;
cristybb503372010-05-27 20:51:26 +00004336 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4337 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4338 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4339 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
cristy3ed852e2009-09-05 21:47:34 +00004340 return(shadow_image);
4341}
4342
4343/*
4344%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4345% %
4346% %
4347% %
4348% S k e t c h I m a g e %
4349% %
4350% %
4351% %
4352%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4353%
4354% SketchImage() simulates a pencil sketch. We convolve the image with a
4355% Gaussian operator of the given radius and standard deviation (sigma). For
4356% reasonable results, radius should be larger than sigma. Use a radius of 0
4357% and SketchImage() selects a suitable radius for you. Angle gives the angle
4358% of the sketch.
4359%
4360% The format of the SketchImage method is:
4361%
4362% Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004363% const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004364%
4365% A description of each parameter follows:
4366%
4367% o image: the image.
4368%
cristy574cc262011-08-05 01:23:58 +00004369% o radius: the radius of the Gaussian, in pixels, not counting the
4370% center pixel.
cristy3ed852e2009-09-05 21:47:34 +00004371%
4372% o sigma: the standard deviation of the Gaussian, in pixels.
4373%
cristyf7ef0252011-09-09 14:50:06 +00004374% o angle: apply the effect along this angle.
4375%
cristy3ed852e2009-09-05 21:47:34 +00004376% o exception: return any errors or warnings in this structure.
4377%
4378*/
4379MagickExport Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004380 const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004381{
cristyfa112112010-01-04 17:48:07 +00004382 CacheView
4383 *random_view;
4384
cristy3ed852e2009-09-05 21:47:34 +00004385 Image
4386 *blend_image,
4387 *blur_image,
4388 *dodge_image,
4389 *random_image,
4390 *sketch_image;
4391
cristy3ed852e2009-09-05 21:47:34 +00004392 MagickBooleanType
4393 status;
4394
cristy3ed852e2009-09-05 21:47:34 +00004395 RandomInfo
cristyfa112112010-01-04 17:48:07 +00004396 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00004397
cristy9d314ff2011-03-09 01:30:28 +00004398 ssize_t
4399 y;
4400
glennrpf7659d72012-09-24 18:14:56 +00004401#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +00004402 unsigned long
4403 key;
glennrpf7659d72012-09-24 18:14:56 +00004404#endif
cristy57340e02012-05-05 00:53:23 +00004405
cristy3ed852e2009-09-05 21:47:34 +00004406 /*
4407 Sketch image.
4408 */
4409 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4410 MagickTrue,exception);
4411 if (random_image == (Image *) NULL)
4412 return((Image *) NULL);
4413 status=MagickTrue;
cristy1b784432009-12-19 02:20:40 +00004414 random_info=AcquireRandomInfoThreadSet();
glennrpf7659d72012-09-24 18:14:56 +00004415#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +00004416 key=GetRandomSecretKey(random_info[0]);
glennrpf7659d72012-09-24 18:14:56 +00004417#endif
cristy46ff2672012-12-14 15:32:26 +00004418 random_view=AcquireAuthenticCacheView(random_image,exception);
cristy1b784432009-12-19 02:20:40 +00004419#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +00004420 #pragma omp parallel for schedule(static,4) shared(status) \
cristy5e6b2592012-12-19 14:08:11 +00004421 magick_threads(random_image,random_image,random_image->rows,key == ~0UL)
cristy1b784432009-12-19 02:20:40 +00004422#endif
cristybb503372010-05-27 20:51:26 +00004423 for (y=0; y < (ssize_t) random_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004424 {
cristy5c9e6f22010-09-17 17:31:01 +00004425 const int
4426 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00004427
cristybb503372010-05-27 20:51:26 +00004428 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004429 x;
4430
cristy4c08aed2011-07-01 19:47:50 +00004431 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004432 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004433
cristy1b784432009-12-19 02:20:40 +00004434 if (status == MagickFalse)
4435 continue;
cristy3ed852e2009-09-05 21:47:34 +00004436 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4437 exception);
cristyacd2ed22011-08-30 01:44:23 +00004438 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004439 {
4440 status=MagickFalse;
4441 continue;
4442 }
cristybb503372010-05-27 20:51:26 +00004443 for (x=0; x < (ssize_t) random_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004444 {
cristya19f1d72012-08-07 18:24:38 +00004445 double
cristy76f512e2011-09-12 01:26:56 +00004446 value;
4447
4448 register ssize_t
4449 i;
4450
cristy883fde12013-04-08 00:50:13 +00004451 if (GetPixelReadMask(random_image,q) == 0)
cristy10a6c612012-01-29 21:41:05 +00004452 {
4453 q+=GetPixelChannels(random_image);
4454 continue;
4455 }
cristy76f512e2011-09-12 01:26:56 +00004456 value=GetPseudoRandomValue(random_info[id]);
4457 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4458 {
cristy5a23c552013-02-13 14:34:28 +00004459 PixelChannel channel=GetPixelChannelChannel(image,i);
4460 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004461 if (traits == UndefinedPixelTrait)
4462 continue;
4463 q[i]=ClampToQuantum(QuantumRange*value);
4464 }
cristyed231572011-07-14 02:18:59 +00004465 q+=GetPixelChannels(random_image);
cristy3ed852e2009-09-05 21:47:34 +00004466 }
4467 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4468 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004469 }
4470 random_view=DestroyCacheView(random_view);
cristy1b784432009-12-19 02:20:40 +00004471 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00004472 if (status == MagickFalse)
4473 {
4474 random_image=DestroyImage(random_image);
4475 return(random_image);
4476 }
cristyaa2c16c2012-03-25 22:21:35 +00004477 blur_image=MotionBlurImage(random_image,radius,sigma,angle,exception);
cristy3ed852e2009-09-05 21:47:34 +00004478 random_image=DestroyImage(random_image);
4479 if (blur_image == (Image *) NULL)
4480 return((Image *) NULL);
cristy32d746f2013-03-25 18:33:17 +00004481 dodge_image=EdgeImage(blur_image,GetOptimalKernelWidth1D(radius,0.5),
cristyfe249712013-03-25 18:28:48 +00004482 exception);
cristy3ed852e2009-09-05 21:47:34 +00004483 blur_image=DestroyImage(blur_image);
4484 if (dodge_image == (Image *) NULL)
4485 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +00004486 (void) NormalizeImage(dodge_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +00004487 (void) NegateImage(dodge_image,MagickFalse,exception);
cristye941a752011-10-15 01:52:48 +00004488 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
cristy3ed852e2009-09-05 21:47:34 +00004489 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4490 if (sketch_image == (Image *) NULL)
4491 {
4492 dodge_image=DestroyImage(dodge_image);
4493 return((Image *) NULL);
4494 }
cristyfeb3e962012-03-29 17:25:55 +00004495 (void) CompositeImage(sketch_image,dodge_image,ColorDodgeCompositeOp,
cristy39172402012-03-30 13:04:39 +00004496 MagickTrue,0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004497 dodge_image=DestroyImage(dodge_image);
4498 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4499 if (blend_image == (Image *) NULL)
4500 {
4501 sketch_image=DestroyImage(sketch_image);
4502 return((Image *) NULL);
4503 }
4504 (void) SetImageArtifact(blend_image,"compose:args","20x80");
cristy39172402012-03-30 13:04:39 +00004505 (void) CompositeImage(sketch_image,blend_image,BlendCompositeOp,MagickTrue,
cristyfeb3e962012-03-29 17:25:55 +00004506 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004507 blend_image=DestroyImage(blend_image);
4508 return(sketch_image);
4509}
4510
4511/*
4512%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4513% %
4514% %
4515% %
4516% S o l a r i z e I m a g e %
4517% %
4518% %
4519% %
4520%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4521%
4522% SolarizeImage() applies a special effect to the image, similar to the effect
4523% achieved in a photo darkroom by selectively exposing areas of photo
4524% sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4525% measure of the extent of the solarization.
4526%
4527% The format of the SolarizeImage method is:
4528%
cristy5cbc0162011-08-29 00:36:28 +00004529% MagickBooleanType SolarizeImage(Image *image,const double threshold,
4530% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004531%
4532% A description of each parameter follows:
4533%
4534% o image: the image.
4535%
4536% o threshold: Define the extent of the solarization.
4537%
cristy5cbc0162011-08-29 00:36:28 +00004538% o exception: return any errors or warnings in this structure.
4539%
cristy3ed852e2009-09-05 21:47:34 +00004540*/
4541MagickExport MagickBooleanType SolarizeImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00004542 const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004543{
4544#define SolarizeImageTag "Solarize/Image"
4545
cristyc4c8d132010-01-07 01:58:38 +00004546 CacheView
4547 *image_view;
4548
cristy3ed852e2009-09-05 21:47:34 +00004549 MagickBooleanType
4550 status;
4551
cristybb503372010-05-27 20:51:26 +00004552 MagickOffsetType
4553 progress;
4554
4555 ssize_t
4556 y;
4557
cristy3ed852e2009-09-05 21:47:34 +00004558 assert(image != (Image *) NULL);
4559 assert(image->signature == MagickSignature);
4560 if (image->debug != MagickFalse)
4561 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristya6400b12013-03-15 12:20:18 +00004562 if (IsGrayColorspace(image->colorspace) != MagickFalse)
4563 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00004564 if (image->storage_class == PseudoClass)
4565 {
cristybb503372010-05-27 20:51:26 +00004566 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004567 i;
4568
4569 /*
4570 Solarize colormap.
4571 */
cristybb503372010-05-27 20:51:26 +00004572 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00004573 {
cristya19f1d72012-08-07 18:24:38 +00004574 if ((double) image->colormap[i].red > threshold)
cristy6e963d82012-06-19 15:23:24 +00004575 image->colormap[i].red=QuantumRange-image->colormap[i].red;
cristya19f1d72012-08-07 18:24:38 +00004576 if ((double) image->colormap[i].green > threshold)
cristy6e963d82012-06-19 15:23:24 +00004577 image->colormap[i].green=QuantumRange-
cristy3ed852e2009-09-05 21:47:34 +00004578 image->colormap[i].green;
cristya19f1d72012-08-07 18:24:38 +00004579 if ((double) image->colormap[i].blue > threshold)
cristy6e963d82012-06-19 15:23:24 +00004580 image->colormap[i].blue=QuantumRange-
cristy3ed852e2009-09-05 21:47:34 +00004581 image->colormap[i].blue;
4582 }
4583 }
4584 /*
4585 Solarize image.
4586 */
4587 status=MagickTrue;
4588 progress=0;
cristy46ff2672012-12-14 15:32:26 +00004589 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00004590#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00004591 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00004592 magick_threads(image,image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00004593#endif
cristybb503372010-05-27 20:51:26 +00004594 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004595 {
cristybb503372010-05-27 20:51:26 +00004596 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004597 x;
4598
cristy4c08aed2011-07-01 19:47:50 +00004599 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004600 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004601
4602 if (status == MagickFalse)
4603 continue;
cristy5cbc0162011-08-29 00:36:28 +00004604 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00004605 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004606 {
4607 status=MagickFalse;
4608 continue;
4609 }
cristybb503372010-05-27 20:51:26 +00004610 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004611 {
cristy76f512e2011-09-12 01:26:56 +00004612 register ssize_t
4613 i;
4614
cristy883fde12013-04-08 00:50:13 +00004615 if (GetPixelReadMask(image,q) == 0)
cristy10a6c612012-01-29 21:41:05 +00004616 {
4617 q+=GetPixelChannels(image);
4618 continue;
4619 }
cristy76f512e2011-09-12 01:26:56 +00004620 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4621 {
cristy5a23c552013-02-13 14:34:28 +00004622 PixelChannel channel=GetPixelChannelChannel(image,i);
4623 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004624 if ((traits == UndefinedPixelTrait) ||
4625 ((traits & CopyPixelTrait) != 0))
4626 continue;
cristya19f1d72012-08-07 18:24:38 +00004627 if ((double) q[i] > threshold)
cristy76f512e2011-09-12 01:26:56 +00004628 q[i]=QuantumRange-q[i];
4629 }
cristyed231572011-07-14 02:18:59 +00004630 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004631 }
4632 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4633 status=MagickFalse;
4634 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4635 {
4636 MagickBooleanType
4637 proceed;
4638
cristyb5d5f722009-11-04 03:03:49 +00004639#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004640 #pragma omp critical (MagickCore_SolarizeImage)
cristy3ed852e2009-09-05 21:47:34 +00004641#endif
4642 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4643 if (proceed == MagickFalse)
4644 status=MagickFalse;
4645 }
4646 }
4647 image_view=DestroyCacheView(image_view);
4648 return(status);
4649}
4650
4651/*
4652%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4653% %
4654% %
4655% %
4656% S t e g a n o I m a g e %
4657% %
4658% %
4659% %
4660%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4661%
4662% SteganoImage() hides a digital watermark within the image. Recover
4663% the hidden watermark later to prove that the authenticity of an image.
4664% Offset defines the start position within the image to hide the watermark.
4665%
4666% The format of the SteganoImage method is:
4667%
4668% Image *SteganoImage(const Image *image,Image *watermark,
4669% ExceptionInfo *exception)
4670%
4671% A description of each parameter follows:
4672%
4673% o image: the image.
4674%
4675% o watermark: the watermark image.
4676%
4677% o exception: return any errors or warnings in this structure.
4678%
4679*/
4680MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4681 ExceptionInfo *exception)
4682{
cristye1bf8ad2010-09-19 17:07:03 +00004683#define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
cristy4c08aed2011-07-01 19:47:50 +00004684#define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
cristyeaedf062010-05-29 22:36:02 +00004685 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
cristy3ed852e2009-09-05 21:47:34 +00004686#define SteganoImageTag "Stegano/Image"
4687
cristyb0d3bb92010-09-22 14:37:58 +00004688 CacheView
4689 *stegano_view,
4690 *watermark_view;
4691
cristy3ed852e2009-09-05 21:47:34 +00004692 Image
4693 *stegano_image;
4694
4695 int
4696 c;
4697
cristy3ed852e2009-09-05 21:47:34 +00004698 MagickBooleanType
4699 status;
4700
cristy101ab702011-10-13 13:06:32 +00004701 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004702 pixel;
4703
cristy4c08aed2011-07-01 19:47:50 +00004704 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004705 *q;
4706
cristye1bf8ad2010-09-19 17:07:03 +00004707 register ssize_t
4708 x;
4709
cristybb503372010-05-27 20:51:26 +00004710 size_t
cristyeaedf062010-05-29 22:36:02 +00004711 depth,
4712 one;
cristy3ed852e2009-09-05 21:47:34 +00004713
cristye1bf8ad2010-09-19 17:07:03 +00004714 ssize_t
4715 i,
4716 j,
4717 k,
4718 y;
4719
cristy3ed852e2009-09-05 21:47:34 +00004720 /*
4721 Initialize steganographic image attributes.
4722 */
4723 assert(image != (const Image *) NULL);
4724 assert(image->signature == MagickSignature);
4725 if (image->debug != MagickFalse)
4726 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4727 assert(watermark != (const Image *) NULL);
4728 assert(watermark->signature == MagickSignature);
4729 assert(exception != (ExceptionInfo *) NULL);
4730 assert(exception->signature == MagickSignature);
cristyeaedf062010-05-29 22:36:02 +00004731 one=1UL;
cristy3ed852e2009-09-05 21:47:34 +00004732 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4733 if (stegano_image == (Image *) NULL)
4734 return((Image *) NULL);
cristyf61b1832012-04-01 01:38:19 +00004735 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
cristy574cc262011-08-05 01:23:58 +00004736 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004737 {
cristy3ed852e2009-09-05 21:47:34 +00004738 stegano_image=DestroyImage(stegano_image);
4739 return((Image *) NULL);
4740 }
cristy3ed852e2009-09-05 21:47:34 +00004741 /*
4742 Hide watermark in low-order bits of image.
4743 */
4744 c=0;
4745 i=0;
4746 j=0;
4747 depth=stegano_image->depth;
cristyf61b1832012-04-01 01:38:19 +00004748 k=stegano_image->offset;
cristyda16f162011-02-19 23:52:17 +00004749 status=MagickTrue;
cristy46ff2672012-12-14 15:32:26 +00004750 watermark_view=AcquireVirtualCacheView(watermark,exception);
4751 stegano_view=AcquireAuthenticCacheView(stegano_image,exception);
cristybb503372010-05-27 20:51:26 +00004752 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
cristy3ed852e2009-09-05 21:47:34 +00004753 {
cristybb503372010-05-27 20:51:26 +00004754 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
cristy3ed852e2009-09-05 21:47:34 +00004755 {
cristybb503372010-05-27 20:51:26 +00004756 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
cristy3ed852e2009-09-05 21:47:34 +00004757 {
cristy1707c6c2012-01-18 23:30:54 +00004758 ssize_t
4759 offset;
4760
cristyf05d4942012-03-17 16:26:09 +00004761 (void) GetOneCacheViewVirtualPixelInfo(watermark_view,x,y,&pixel,
cristyda1f9c12011-10-02 21:39:49 +00004762 exception);
cristy1707c6c2012-01-18 23:30:54 +00004763 offset=k/(ssize_t) stegano_image->columns;
4764 if (offset >= (ssize_t) stegano_image->rows)
cristy3ed852e2009-09-05 21:47:34 +00004765 break;
cristyb0d3bb92010-09-22 14:37:58 +00004766 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4767 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4768 exception);
cristyacd2ed22011-08-30 01:44:23 +00004769 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004770 break;
4771 switch (c)
4772 {
4773 case 0:
4774 {
cristyf61b1832012-04-01 01:38:19 +00004775 SetPixelRed(stegano_image,SetBit(GetPixelRed(stegano_image,q),j,
4776 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004777 break;
4778 }
4779 case 1:
4780 {
cristyf61b1832012-04-01 01:38:19 +00004781 SetPixelGreen(stegano_image,SetBit(GetPixelGreen(stegano_image,q),j,
4782 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004783 break;
4784 }
4785 case 2:
4786 {
cristyf61b1832012-04-01 01:38:19 +00004787 SetPixelBlue(stegano_image,SetBit(GetPixelBlue(stegano_image,q),j,
4788 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004789 break;
4790 }
4791 }
cristyb0d3bb92010-09-22 14:37:58 +00004792 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004793 break;
4794 c++;
4795 if (c == 3)
4796 c=0;
4797 k++;
cristybb503372010-05-27 20:51:26 +00004798 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
cristy3ed852e2009-09-05 21:47:34 +00004799 k=0;
cristyf61b1832012-04-01 01:38:19 +00004800 if (k == stegano_image->offset)
cristy3ed852e2009-09-05 21:47:34 +00004801 j++;
4802 }
4803 }
cristy8b27a6d2010-02-14 03:31:15 +00004804 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004805 {
cristy8b27a6d2010-02-14 03:31:15 +00004806 MagickBooleanType
4807 proceed;
4808
4809 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4810 (depth-i),depth);
4811 if (proceed == MagickFalse)
4812 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004813 }
4814 }
cristyb0d3bb92010-09-22 14:37:58 +00004815 stegano_view=DestroyCacheView(stegano_view);
4816 watermark_view=DestroyCacheView(watermark_view);
cristyda16f162011-02-19 23:52:17 +00004817 if (status == MagickFalse)
cristyfab83642012-12-14 00:31:38 +00004818 stegano_image=DestroyImage(stegano_image);
cristy3ed852e2009-09-05 21:47:34 +00004819 return(stegano_image);
4820}
4821
4822/*
4823%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4824% %
4825% %
4826% %
4827% S t e r e o A n a g l y p h I m a g e %
4828% %
4829% %
4830% %
4831%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4832%
4833% StereoAnaglyphImage() combines two images and produces a single image that
4834% is the composite of a left and right image of a stereo pair. Special
4835% red-green stereo glasses are required to view this effect.
4836%
4837% The format of the StereoAnaglyphImage method is:
4838%
4839% Image *StereoImage(const Image *left_image,const Image *right_image,
4840% ExceptionInfo *exception)
4841% Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004842% const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004843% ExceptionInfo *exception)
4844%
4845% A description of each parameter follows:
4846%
4847% o left_image: the left image.
4848%
4849% o right_image: the right image.
4850%
4851% o exception: return any errors or warnings in this structure.
4852%
4853% o x_offset: amount, in pixels, by which the left image is offset to the
4854% right of the right image.
4855%
4856% o y_offset: amount, in pixels, by which the left image is offset to the
4857% bottom of the right image.
4858%
4859%
4860*/
4861MagickExport Image *StereoImage(const Image *left_image,
4862 const Image *right_image,ExceptionInfo *exception)
4863{
4864 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4865}
4866
4867MagickExport Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004868 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004869 ExceptionInfo *exception)
4870{
4871#define StereoImageTag "Stereo/Image"
4872
4873 const Image
4874 *image;
4875
4876 Image
4877 *stereo_image;
4878
cristy3ed852e2009-09-05 21:47:34 +00004879 MagickBooleanType
4880 status;
4881
cristy9d314ff2011-03-09 01:30:28 +00004882 ssize_t
4883 y;
4884
cristy3ed852e2009-09-05 21:47:34 +00004885 assert(left_image != (const Image *) NULL);
4886 assert(left_image->signature == MagickSignature);
4887 if (left_image->debug != MagickFalse)
4888 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4889 left_image->filename);
4890 assert(right_image != (const Image *) NULL);
4891 assert(right_image->signature == MagickSignature);
4892 assert(exception != (ExceptionInfo *) NULL);
4893 assert(exception->signature == MagickSignature);
4894 assert(right_image != (const Image *) NULL);
4895 image=left_image;
4896 if ((left_image->columns != right_image->columns) ||
4897 (left_image->rows != right_image->rows))
4898 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4899 /*
4900 Initialize stereo image attributes.
4901 */
4902 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4903 MagickTrue,exception);
4904 if (stereo_image == (Image *) NULL)
4905 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004906 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004907 {
cristy3ed852e2009-09-05 21:47:34 +00004908 stereo_image=DestroyImage(stereo_image);
4909 return((Image *) NULL);
4910 }
cristy079c78d2012-07-03 11:53:48 +00004911 (void) SetImageColorspace(stereo_image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00004912 /*
4913 Copy left image to red channel and right image to blue channel.
4914 */
cristyda16f162011-02-19 23:52:17 +00004915 status=MagickTrue;
cristybb503372010-05-27 20:51:26 +00004916 for (y=0; y < (ssize_t) stereo_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004917 {
cristy4c08aed2011-07-01 19:47:50 +00004918 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004919 *restrict p,
4920 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004921
cristybb503372010-05-27 20:51:26 +00004922 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004923 x;
4924
cristy4c08aed2011-07-01 19:47:50 +00004925 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004926 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +00004927
4928 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4929 exception);
4930 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4931 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
cristy76f512e2011-09-12 01:26:56 +00004932 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4933 (r == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004934 break;
cristybb503372010-05-27 20:51:26 +00004935 for (x=0; x < (ssize_t) stereo_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004936 {
cristy4c08aed2011-07-01 19:47:50 +00004937 SetPixelRed(image,GetPixelRed(left_image,p),r);
cristy76f512e2011-09-12 01:26:56 +00004938 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4939 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4940 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4941 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4942 GetPixelAlpha(right_image,q))/2,r);
cristyed231572011-07-14 02:18:59 +00004943 p+=GetPixelChannels(left_image);
cristy76f512e2011-09-12 01:26:56 +00004944 q+=GetPixelChannels(right_image);
4945 r+=GetPixelChannels(stereo_image);
cristy3ed852e2009-09-05 21:47:34 +00004946 }
4947 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4948 break;
cristy8b27a6d2010-02-14 03:31:15 +00004949 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004950 {
cristy8b27a6d2010-02-14 03:31:15 +00004951 MagickBooleanType
4952 proceed;
4953
cristybb503372010-05-27 20:51:26 +00004954 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4955 stereo_image->rows);
cristy8b27a6d2010-02-14 03:31:15 +00004956 if (proceed == MagickFalse)
4957 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004958 }
4959 }
cristyda16f162011-02-19 23:52:17 +00004960 if (status == MagickFalse)
cristyfab83642012-12-14 00:31:38 +00004961 stereo_image=DestroyImage(stereo_image);
cristy3ed852e2009-09-05 21:47:34 +00004962 return(stereo_image);
4963}
4964
4965/*
4966%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4967% %
4968% %
4969% %
4970% S w i r l I m a g e %
4971% %
4972% %
4973% %
4974%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4975%
4976% SwirlImage() swirls the pixels about the center of the image, where
4977% degrees indicates the sweep of the arc through which each pixel is moved.
4978% You get a more dramatic effect as the degrees move from 1 to 360.
4979%
4980% The format of the SwirlImage method is:
4981%
4982% Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00004983% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004984%
4985% A description of each parameter follows:
4986%
4987% o image: the image.
4988%
4989% o degrees: Define the tightness of the swirling effect.
4990%
cristy76f512e2011-09-12 01:26:56 +00004991% o method: the pixel interpolation method.
4992%
cristy3ed852e2009-09-05 21:47:34 +00004993% o exception: return any errors or warnings in this structure.
4994%
4995*/
4996MagickExport Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00004997 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004998{
4999#define SwirlImageTag "Swirl/Image"
5000
cristyfa112112010-01-04 17:48:07 +00005001 CacheView
5002 *image_view,
5003 *swirl_view;
5004
cristy3ed852e2009-09-05 21:47:34 +00005005 Image
5006 *swirl_image;
5007
cristy3ed852e2009-09-05 21:47:34 +00005008 MagickBooleanType
5009 status;
5010
cristybb503372010-05-27 20:51:26 +00005011 MagickOffsetType
5012 progress;
5013
cristya19f1d72012-08-07 18:24:38 +00005014 double
cristy3ed852e2009-09-05 21:47:34 +00005015 radius;
5016
5017 PointInfo
5018 center,
5019 scale;
5020
cristybb503372010-05-27 20:51:26 +00005021 ssize_t
5022 y;
5023
cristy3ed852e2009-09-05 21:47:34 +00005024 /*
5025 Initialize swirl image attributes.
5026 */
5027 assert(image != (const Image *) NULL);
5028 assert(image->signature == MagickSignature);
5029 if (image->debug != MagickFalse)
5030 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5031 assert(exception != (ExceptionInfo *) NULL);
5032 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00005033 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005034 if (swirl_image == (Image *) NULL)
5035 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005036 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005037 {
cristy3ed852e2009-09-05 21:47:34 +00005038 swirl_image=DestroyImage(swirl_image);
5039 return((Image *) NULL);
5040 }
cristy4c08aed2011-07-01 19:47:50 +00005041 if (swirl_image->background_color.alpha != OpaqueAlpha)
cristy8a46d822012-08-28 23:32:39 +00005042 swirl_image->alpha_trait=BlendPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +00005043 /*
5044 Compute scaling factor.
5045 */
5046 center.x=(double) image->columns/2.0;
5047 center.y=(double) image->rows/2.0;
5048 radius=MagickMax(center.x,center.y);
5049 scale.x=1.0;
5050 scale.y=1.0;
5051 if (image->columns > image->rows)
5052 scale.y=(double) image->columns/(double) image->rows;
5053 else
5054 if (image->columns < image->rows)
5055 scale.x=(double) image->rows/(double) image->columns;
5056 degrees=(double) DegreesToRadians(degrees);
5057 /*
5058 Swirl image.
5059 */
5060 status=MagickTrue;
5061 progress=0;
cristy46ff2672012-12-14 15:32:26 +00005062 image_view=AcquireVirtualCacheView(image,exception);
5063 swirl_view=AcquireAuthenticCacheView(swirl_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00005064#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005065 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00005066 magick_threads(image,swirl_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005067#endif
cristybb503372010-05-27 20:51:26 +00005068 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005069 {
cristya19f1d72012-08-07 18:24:38 +00005070 double
cristy3ed852e2009-09-05 21:47:34 +00005071 distance;
5072
5073 PointInfo
5074 delta;
5075
cristy6d188022011-09-12 13:23:33 +00005076 register const Quantum
5077 *restrict p;
5078
cristybb503372010-05-27 20:51:26 +00005079 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005080 x;
5081
cristy4c08aed2011-07-01 19:47:50 +00005082 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005083 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005084
5085 if (status == MagickFalse)
5086 continue;
cristy6d188022011-09-12 13:23:33 +00005087 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00005088 q=QueueCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00005089 exception);
cristy6d188022011-09-12 13:23:33 +00005090 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005091 {
5092 status=MagickFalse;
5093 continue;
5094 }
cristy3ed852e2009-09-05 21:47:34 +00005095 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00005096 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005097 {
5098 /*
5099 Determine if the pixel is within an ellipse.
5100 */
cristy883fde12013-04-08 00:50:13 +00005101 if (GetPixelReadMask(image,p) == 0)
cristy10a6c612012-01-29 21:41:05 +00005102 {
5103 p+=GetPixelChannels(image);
5104 q+=GetPixelChannels(swirl_image);
5105 continue;
5106 }
cristy3ed852e2009-09-05 21:47:34 +00005107 delta.x=scale.x*(double) (x-center.x);
5108 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00005109 if (distance >= (radius*radius))
5110 {
cristy1707c6c2012-01-18 23:30:54 +00005111 register ssize_t
5112 i;
5113
cristy6d188022011-09-12 13:23:33 +00005114 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy1707c6c2012-01-18 23:30:54 +00005115 {
cristy5a23c552013-02-13 14:34:28 +00005116 PixelChannel channel=GetPixelChannelChannel(image,i);
5117 PixelTrait traits=GetPixelChannelTraits(image,channel);
5118 PixelTrait swirl_traits=GetPixelChannelTraits(swirl_image,channel);
cristy1707c6c2012-01-18 23:30:54 +00005119 if ((traits == UndefinedPixelTrait) ||
5120 (swirl_traits == UndefinedPixelTrait))
5121 continue;
5122 SetPixelChannel(swirl_image,channel,p[i],q);
5123 }
cristy6d188022011-09-12 13:23:33 +00005124 }
5125 else
cristy3ed852e2009-09-05 21:47:34 +00005126 {
cristya19f1d72012-08-07 18:24:38 +00005127 double
cristy3ed852e2009-09-05 21:47:34 +00005128 cosine,
5129 factor,
5130 sine;
5131
5132 /*
5133 Swirl the pixel.
5134 */
5135 factor=1.0-sqrt((double) distance)/radius;
5136 sine=sin((double) (degrees*factor*factor));
5137 cosine=cos((double) (degrees*factor*factor));
cristy76f512e2011-09-12 01:26:56 +00005138 status=InterpolatePixelChannels(image,image_view,swirl_image,method,
5139 ((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5140 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00005141 }
cristy6d188022011-09-12 13:23:33 +00005142 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00005143 q+=GetPixelChannels(swirl_image);
cristy3ed852e2009-09-05 21:47:34 +00005144 }
5145 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5146 status=MagickFalse;
5147 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5148 {
5149 MagickBooleanType
5150 proceed;
5151
cristyb5d5f722009-11-04 03:03:49 +00005152#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005153 #pragma omp critical (MagickCore_SwirlImage)
cristy3ed852e2009-09-05 21:47:34 +00005154#endif
5155 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5156 if (proceed == MagickFalse)
5157 status=MagickFalse;
5158 }
5159 }
5160 swirl_view=DestroyCacheView(swirl_view);
5161 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005162 if (status == MagickFalse)
5163 swirl_image=DestroyImage(swirl_image);
5164 return(swirl_image);
5165}
5166
5167/*
5168%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5169% %
5170% %
5171% %
5172% T i n t I m a g e %
5173% %
5174% %
5175% %
5176%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5177%
5178% TintImage() applies a color vector to each pixel in the image. The length
5179% of the vector is 0 for black and white and at its maximum for the midtones.
5180% The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5181%
5182% The format of the TintImage method is:
5183%
cristyb817c3f2011-10-03 14:00:35 +00005184% Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005185% const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005186%
5187% A description of each parameter follows:
5188%
5189% o image: the image.
5190%
cristyb817c3f2011-10-03 14:00:35 +00005191% o blend: A color value used for tinting.
cristy3ed852e2009-09-05 21:47:34 +00005192%
5193% o tint: A color value used for tinting.
5194%
5195% o exception: return any errors or warnings in this structure.
5196%
5197*/
cristyb817c3f2011-10-03 14:00:35 +00005198MagickExport Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005199 const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005200{
5201#define TintImageTag "Tint/Image"
5202
cristyc4c8d132010-01-07 01:58:38 +00005203 CacheView
5204 *image_view,
5205 *tint_view;
5206
cristyf54150e2013-04-11 01:25:47 +00005207 double
5208 intensity;
5209
cristy3ed852e2009-09-05 21:47:34 +00005210 GeometryInfo
5211 geometry_info;
5212
5213 Image
5214 *tint_image;
5215
cristy3ed852e2009-09-05 21:47:34 +00005216 MagickBooleanType
5217 status;
5218
cristybb503372010-05-27 20:51:26 +00005219 MagickOffsetType
5220 progress;
cristy3ed852e2009-09-05 21:47:34 +00005221
cristy4c08aed2011-07-01 19:47:50 +00005222 PixelInfo
cristy1707c6c2012-01-18 23:30:54 +00005223 color_vector;
cristy3ed852e2009-09-05 21:47:34 +00005224
cristybb503372010-05-27 20:51:26 +00005225 MagickStatusType
5226 flags;
5227
5228 ssize_t
5229 y;
5230
cristy3ed852e2009-09-05 21:47:34 +00005231 /*
5232 Allocate tint image.
5233 */
5234 assert(image != (const Image *) NULL);
5235 assert(image->signature == MagickSignature);
5236 if (image->debug != MagickFalse)
5237 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5238 assert(exception != (ExceptionInfo *) NULL);
5239 assert(exception->signature == MagickSignature);
5240 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5241 if (tint_image == (Image *) NULL)
5242 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005243 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005244 {
cristy3ed852e2009-09-05 21:47:34 +00005245 tint_image=DestroyImage(tint_image);
5246 return((Image *) NULL);
5247 }
cristya6400b12013-03-15 12:20:18 +00005248 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
cristy4dab3802013-03-15 22:08:15 +00005249 (IsPixelInfoGray(tint) == MagickFalse))
cristya6400b12013-03-15 12:20:18 +00005250 (void) SetImageColorspace(tint_image,sRGBColorspace,exception);
cristyaed9c382011-10-03 17:54:21 +00005251 if (blend == (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005252 return(tint_image);
5253 /*
5254 Determine RGB values of the color.
5255 */
cristy1707c6c2012-01-18 23:30:54 +00005256 GetPixelInfo(image,&color_vector);
cristyb817c3f2011-10-03 14:00:35 +00005257 flags=ParseGeometry(blend,&geometry_info);
cristy1707c6c2012-01-18 23:30:54 +00005258 color_vector.red=geometry_info.rho;
5259 color_vector.green=geometry_info.rho;
5260 color_vector.blue=geometry_info.rho;
cristy92ac5722013-03-09 15:09:38 +00005261 color_vector.alpha=(MagickRealType) OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00005262 if ((flags & SigmaValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005263 color_vector.green=geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +00005264 if ((flags & XiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005265 color_vector.blue=geometry_info.xi;
cristyb817c3f2011-10-03 14:00:35 +00005266 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005267 color_vector.alpha=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005268 if (image->colorspace == CMYKColorspace)
5269 {
cristy1707c6c2012-01-18 23:30:54 +00005270 color_vector.black=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005271 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005272 color_vector.black=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005273 if ((flags & ChiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005274 color_vector.alpha=geometry_info.chi;
cristy76f512e2011-09-12 01:26:56 +00005275 }
cristya19f1d72012-08-07 18:24:38 +00005276 intensity=(double) GetPixelInfoIntensity(tint);
cristyf54150e2013-04-11 01:25:47 +00005277 color_vector.red=(double) (color_vector.red*tint->red/100.0-intensity);
5278 color_vector.green=(double) (color_vector.green*tint->green/100.0-intensity);
5279 color_vector.blue=(double) (color_vector.blue*tint->blue/100.0-intensity);
5280 color_vector.black=(double) (color_vector.black*tint->black/100.0-intensity);
5281 color_vector.alpha=(double) (color_vector.alpha*tint->alpha/100.0-intensity);
cristy3ed852e2009-09-05 21:47:34 +00005282 /*
5283 Tint image.
5284 */
5285 status=MagickTrue;
5286 progress=0;
cristy46ff2672012-12-14 15:32:26 +00005287 image_view=AcquireVirtualCacheView(image,exception);
5288 tint_view=AcquireAuthenticCacheView(tint_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00005289#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005290 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00005291 magick_threads(image,tint_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005292#endif
cristybb503372010-05-27 20:51:26 +00005293 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005294 {
cristy4c08aed2011-07-01 19:47:50 +00005295 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00005296 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00005297
cristy4c08aed2011-07-01 19:47:50 +00005298 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005299 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005300
cristy6b91acb2011-04-19 12:23:54 +00005301 register ssize_t
5302 x;
5303
cristy3ed852e2009-09-05 21:47:34 +00005304 if (status == MagickFalse)
5305 continue;
5306 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5307 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5308 exception);
cristy4c08aed2011-07-01 19:47:50 +00005309 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005310 {
5311 status=MagickFalse;
5312 continue;
5313 }
cristybb503372010-05-27 20:51:26 +00005314 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005315 {
cristy4c08aed2011-07-01 19:47:50 +00005316 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00005317 pixel;
5318
cristya19f1d72012-08-07 18:24:38 +00005319 double
cristy3ed852e2009-09-05 21:47:34 +00005320 weight;
5321
cristy1707c6c2012-01-18 23:30:54 +00005322 register ssize_t
5323 i;
5324
5325 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5326 {
cristy5a23c552013-02-13 14:34:28 +00005327 PixelChannel channel=GetPixelChannelChannel(image,i);
5328 PixelTrait traits=GetPixelChannelTraits(image,channel);
5329 PixelTrait tint_traits=GetPixelChannelTraits(tint_image,channel);
cristy1707c6c2012-01-18 23:30:54 +00005330 if ((traits == UndefinedPixelTrait) ||
5331 (tint_traits == UndefinedPixelTrait))
5332 continue;
cristy1eced092012-08-10 23:10:56 +00005333 if (((tint_traits & CopyPixelTrait) != 0) ||
cristy883fde12013-04-08 00:50:13 +00005334 (GetPixelReadMask(image,p) == 0))
cristy1707c6c2012-01-18 23:30:54 +00005335 {
5336 SetPixelChannel(tint_image,channel,p[i],q);
5337 continue;
5338 }
5339 }
5340 GetPixelInfo(image,&pixel);
5341 weight=QuantumScale*GetPixelRed(image,p)-0.5;
cristy1eced092012-08-10 23:10:56 +00005342 pixel.red=(double) GetPixelRed(image,p)+color_vector.red*(1.0-(4.0*
5343 (weight*weight)));
cristy1707c6c2012-01-18 23:30:54 +00005344 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
cristy1eced092012-08-10 23:10:56 +00005345 pixel.green=(double) GetPixelGreen(image,p)+color_vector.green*(1.0-(4.0*
5346 (weight*weight)));
cristy1707c6c2012-01-18 23:30:54 +00005347 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
cristy1eced092012-08-10 23:10:56 +00005348 pixel.blue=(double) GetPixelBlue(image,p)+color_vector.blue*(1.0-(4.0*
5349 (weight*weight)));
cristy1707c6c2012-01-18 23:30:54 +00005350 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
cristy1eced092012-08-10 23:10:56 +00005351 pixel.black=(double) GetPixelBlack(image,p)+color_vector.black*(1.0-(4.0*
5352 (weight*weight)));
cristy1707c6c2012-01-18 23:30:54 +00005353 SetPixelInfoPixel(tint_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00005354 p+=GetPixelChannels(image);
5355 q+=GetPixelChannels(tint_image);
cristy3ed852e2009-09-05 21:47:34 +00005356 }
5357 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5358 status=MagickFalse;
5359 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5360 {
5361 MagickBooleanType
5362 proceed;
5363
cristyb5d5f722009-11-04 03:03:49 +00005364#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005365 #pragma omp critical (MagickCore_TintImage)
cristy3ed852e2009-09-05 21:47:34 +00005366#endif
5367 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5368 if (proceed == MagickFalse)
5369 status=MagickFalse;
5370 }
5371 }
5372 tint_view=DestroyCacheView(tint_view);
5373 image_view=DestroyCacheView(image_view);
5374 if (status == MagickFalse)
5375 tint_image=DestroyImage(tint_image);
5376 return(tint_image);
5377}
5378
5379/*
5380%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5381% %
5382% %
5383% %
5384% V i g n e t t e I m a g e %
5385% %
5386% %
5387% %
5388%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5389%
5390% VignetteImage() softens the edges of the image in vignette style.
5391%
5392% The format of the VignetteImage method is:
5393%
5394% Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005395% const double sigma,const ssize_t x,const ssize_t y,
cristy05c0c9a2011-09-05 23:16:13 +00005396% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005397%
5398% A description of each parameter follows:
5399%
5400% o image: the image.
5401%
5402% o radius: the radius of the pixel neighborhood.
5403%
5404% o sigma: the standard deviation of the Gaussian, in pixels.
5405%
5406% o x, y: Define the x and y ellipse offset.
5407%
5408% o exception: return any errors or warnings in this structure.
5409%
5410*/
5411MagickExport Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005412 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005413{
5414 char
5415 ellipse[MaxTextExtent];
5416
5417 DrawInfo
5418 *draw_info;
5419
5420 Image
5421 *canvas_image,
5422 *blur_image,
5423 *oval_image,
5424 *vignette_image;
5425
5426 assert(image != (Image *) NULL);
5427 assert(image->signature == MagickSignature);
5428 if (image->debug != MagickFalse)
5429 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5430 assert(exception != (ExceptionInfo *) NULL);
5431 assert(exception->signature == MagickSignature);
5432 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5433 if (canvas_image == (Image *) NULL)
5434 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005435 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005436 {
cristy3ed852e2009-09-05 21:47:34 +00005437 canvas_image=DestroyImage(canvas_image);
5438 return((Image *) NULL);
5439 }
cristy8a46d822012-08-28 23:32:39 +00005440 canvas_image->alpha_trait=BlendPixelTrait;
cristy98621462011-12-31 22:31:11 +00005441 oval_image=CloneImage(canvas_image,canvas_image->columns,canvas_image->rows,
5442 MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005443 if (oval_image == (Image *) NULL)
5444 {
5445 canvas_image=DestroyImage(canvas_image);
5446 return((Image *) NULL);
5447 }
cristy9950d572011-10-01 18:22:35 +00005448 (void) QueryColorCompliance("#000000",AllCompliance,
5449 &oval_image->background_color,exception);
cristyea1a8aa2011-10-20 13:24:06 +00005450 (void) SetImageBackgroundColor(oval_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00005451 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
cristy9950d572011-10-01 18:22:35 +00005452 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5453 exception);
5454 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5455 exception);
cristy1707c6c2012-01-18 23:30:54 +00005456 (void) FormatLocaleString(ellipse,MaxTextExtent,"ellipse %g,%g,%g,%g,"
5457 "0.0,360.0",image->columns/2.0,image->rows/2.0,image->columns/2.0-x,
5458 image->rows/2.0-y);
cristy3ed852e2009-09-05 21:47:34 +00005459 draw_info->primitive=AcquireString(ellipse);
cristy018f07f2011-09-04 21:15:19 +00005460 (void) DrawImage(oval_image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00005461 draw_info=DestroyDrawInfo(draw_info);
cristyaa2c16c2012-03-25 22:21:35 +00005462 blur_image=BlurImage(oval_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00005463 oval_image=DestroyImage(oval_image);
5464 if (blur_image == (Image *) NULL)
5465 {
5466 canvas_image=DestroyImage(canvas_image);
5467 return((Image *) NULL);
5468 }
cristy8a46d822012-08-28 23:32:39 +00005469 blur_image->alpha_trait=UndefinedPixelTrait;
cristy39172402012-03-30 13:04:39 +00005470 (void) CompositeImage(canvas_image,blur_image,IntensityCompositeOp,MagickTrue,
5471 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00005472 blur_image=DestroyImage(blur_image);
5473 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5474 canvas_image=DestroyImage(canvas_image);
cristy66d26122012-06-23 21:56:40 +00005475 if (vignette_image != (Image *) NULL)
5476 (void) TransformImageColorspace(vignette_image,image->colorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00005477 return(vignette_image);
5478}
5479
5480/*
5481%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5482% %
5483% %
5484% %
5485% W a v e I m a g e %
5486% %
5487% %
5488% %
5489%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5490%
5491% WaveImage() creates a "ripple" effect in the image by shifting the pixels
cristycee97112010-05-28 00:44:52 +00005492% vertically along a sine wave whose amplitude and wavelength is specified
cristy3ed852e2009-09-05 21:47:34 +00005493% by the given parameters.
5494%
5495% The format of the WaveImage method is:
5496%
5497% Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005498% const double wave_length,const PixelInterpolateMethod method,
5499% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005500%
5501% A description of each parameter follows:
5502%
5503% o image: the image.
5504%
5505% o amplitude, wave_length: Define the amplitude and wave length of the
5506% sine wave.
5507%
cristy5c4e2582011-09-11 19:21:03 +00005508% o interpolate: the pixel interpolation method.
5509%
cristy3ed852e2009-09-05 21:47:34 +00005510% o exception: return any errors or warnings in this structure.
5511%
5512*/
5513MagickExport Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005514 const double wave_length,const PixelInterpolateMethod method,
5515 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005516{
5517#define WaveImageTag "Wave/Image"
5518
cristyfa112112010-01-04 17:48:07 +00005519 CacheView
cristyd76c51e2011-03-26 00:21:26 +00005520 *image_view,
cristyfa112112010-01-04 17:48:07 +00005521 *wave_view;
5522
cristy3ed852e2009-09-05 21:47:34 +00005523 Image
5524 *wave_image;
5525
cristy3ed852e2009-09-05 21:47:34 +00005526 MagickBooleanType
5527 status;
5528
cristybb503372010-05-27 20:51:26 +00005529 MagickOffsetType
5530 progress;
5531
cristya19f1d72012-08-07 18:24:38 +00005532 double
cristy3ed852e2009-09-05 21:47:34 +00005533 *sine_map;
5534
cristybb503372010-05-27 20:51:26 +00005535 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005536 i;
5537
cristybb503372010-05-27 20:51:26 +00005538 ssize_t
5539 y;
5540
cristy3ed852e2009-09-05 21:47:34 +00005541 /*
5542 Initialize wave image attributes.
5543 */
5544 assert(image != (Image *) NULL);
5545 assert(image->signature == MagickSignature);
5546 if (image->debug != MagickFalse)
5547 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5548 assert(exception != (ExceptionInfo *) NULL);
5549 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00005550 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
cristy3ed852e2009-09-05 21:47:34 +00005551 fabs(amplitude)),MagickTrue,exception);
5552 if (wave_image == (Image *) NULL)
5553 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005554 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005555 {
cristy3ed852e2009-09-05 21:47:34 +00005556 wave_image=DestroyImage(wave_image);
5557 return((Image *) NULL);
5558 }
cristy4c08aed2011-07-01 19:47:50 +00005559 if (wave_image->background_color.alpha != OpaqueAlpha)
cristy8a46d822012-08-28 23:32:39 +00005560 wave_image->alpha_trait=BlendPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +00005561 /*
5562 Allocate sine map.
5563 */
cristya19f1d72012-08-07 18:24:38 +00005564 sine_map=(double *) AcquireQuantumMemory((size_t) wave_image->columns,
cristy3ed852e2009-09-05 21:47:34 +00005565 sizeof(*sine_map));
cristya19f1d72012-08-07 18:24:38 +00005566 if (sine_map == (double *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005567 {
5568 wave_image=DestroyImage(wave_image);
5569 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5570 }
cristybb503372010-05-27 20:51:26 +00005571 for (i=0; i < (ssize_t) wave_image->columns; i++)
cristy4205a3c2010-09-12 20:19:59 +00005572 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5573 wave_length));
cristy3ed852e2009-09-05 21:47:34 +00005574 /*
5575 Wave image.
5576 */
5577 status=MagickTrue;
5578 progress=0;
cristy46ff2672012-12-14 15:32:26 +00005579 image_view=AcquireVirtualCacheView(image,exception);
5580 wave_view=AcquireAuthenticCacheView(wave_image,exception);
cristyd76c51e2011-03-26 00:21:26 +00005581 (void) SetCacheViewVirtualPixelMethod(image_view,
5582 BackgroundVirtualPixelMethod);
cristyb5d5f722009-11-04 03:03:49 +00005583#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005584 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00005585 magick_threads(image,wave_image,wave_image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005586#endif
cristybb503372010-05-27 20:51:26 +00005587 for (y=0; y < (ssize_t) wave_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005588 {
cristy4c08aed2011-07-01 19:47:50 +00005589 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005590 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005591
cristye97bb922011-04-03 01:36:52 +00005592 register ssize_t
5593 x;
5594
cristy3ed852e2009-09-05 21:47:34 +00005595 if (status == MagickFalse)
5596 continue;
5597 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5598 exception);
cristyacd2ed22011-08-30 01:44:23 +00005599 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005600 {
5601 status=MagickFalse;
5602 continue;
5603 }
cristybb503372010-05-27 20:51:26 +00005604 for (x=0; x < (ssize_t) wave_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005605 {
cristy5c4e2582011-09-11 19:21:03 +00005606 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5607 (double) x,(double) (y-sine_map[x]),q,exception);
cristyed231572011-07-14 02:18:59 +00005608 q+=GetPixelChannels(wave_image);
cristy3ed852e2009-09-05 21:47:34 +00005609 }
5610 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5611 status=MagickFalse;
5612 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5613 {
5614 MagickBooleanType
5615 proceed;
5616
cristyb5d5f722009-11-04 03:03:49 +00005617#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005618 #pragma omp critical (MagickCore_WaveImage)
cristy3ed852e2009-09-05 21:47:34 +00005619#endif
5620 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5621 if (proceed == MagickFalse)
5622 status=MagickFalse;
5623 }
5624 }
5625 wave_view=DestroyCacheView(wave_view);
cristyd76c51e2011-03-26 00:21:26 +00005626 image_view=DestroyCacheView(image_view);
cristya19f1d72012-08-07 18:24:38 +00005627 sine_map=(double *) RelinquishMagickMemory(sine_map);
cristy3ed852e2009-09-05 21:47:34 +00005628 if (status == MagickFalse)
5629 wave_image=DestroyImage(wave_image);
5630 return(wave_image);
5631}