blob: eca389a1798d64e29389529366906893014d4297 [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) ||
363 (GetPixelMask(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);
cristy018f07f2011-09-04 21:15:19 +0000603 (void) SetImageType(clone_image,GrayscaleType,exception);
cristy8ae632d2011-09-05 17:29:53 +0000604 edge_image=EdgeImage(clone_image,radius,sigma,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);
cristy018f07f2011-09-04 21:15:19 +0000614 (void) SetImageType(charcoal_image,GrayscaleType,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 }
cristy8a46d822012-08-28 23:32:39 +0000701 if ((colorize_image->alpha_trait != BlendPixelTrait) &&
702 (colorize->alpha_trait == BlendPixelTrait))
cristy768165d2012-04-09 15:01:35 +0000703 (void) SetImageAlpha(colorize_image,OpaqueAlpha,exception);
704 if (blend == (const char *) NULL)
705 return(colorize_image);
cristy20c3aed2012-04-10 01:06:21 +0000706 GetPixelInfo(image,&blend_percentage);
707 flags=ParseGeometry(blend,&geometry_info);
708 blend_percentage.red=geometry_info.rho;
709 blend_percentage.green=geometry_info.rho;
710 blend_percentage.blue=geometry_info.rho;
711 blend_percentage.black=geometry_info.rho;
cristy3ee7de52012-04-14 23:40:47 +0000712 blend_percentage.alpha=geometry_info.rho;
cristy20c3aed2012-04-10 01:06:21 +0000713 if ((flags & SigmaValue) != 0)
714 blend_percentage.green=geometry_info.sigma;
715 if ((flags & XiValue) != 0)
716 blend_percentage.blue=geometry_info.xi;
717 if ((flags & PsiValue) != 0)
718 blend_percentage.alpha=geometry_info.psi;
719 if (blend_percentage.colorspace == CMYKColorspace)
720 {
721 if ((flags & PsiValue) != 0)
722 blend_percentage.black=geometry_info.psi;
723 if ((flags & ChiValue) != 0)
724 blend_percentage.alpha=geometry_info.chi;
725 }
cristy3ed852e2009-09-05 21:47:34 +0000726 /*
727 Colorize DirectClass image.
728 */
729 status=MagickTrue;
730 progress=0;
cristy46ff2672012-12-14 15:32:26 +0000731 image_view=AcquireVirtualCacheView(image,exception);
732 colorize_view=AcquireAuthenticCacheView(colorize_image,exception);
cristy319a1e72010-02-21 15:13:11 +0000733#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000734 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +0000735 magick_threads(image,colorize_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000736#endif
cristybb503372010-05-27 20:51:26 +0000737 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000738 {
739 MagickBooleanType
740 sync;
741
cristy4c08aed2011-07-01 19:47:50 +0000742 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000743 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000744
cristy4c08aed2011-07-01 19:47:50 +0000745 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000746 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000747
cristy4a37c622012-08-23 18:47:56 +0000748 register ssize_t
749 x;
750
cristy3ed852e2009-09-05 21:47:34 +0000751 if (status == MagickFalse)
752 continue;
753 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
754 q=QueueCacheViewAuthenticPixels(colorize_view,0,y,colorize_image->columns,1,
755 exception);
cristy4c08aed2011-07-01 19:47:50 +0000756 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000757 {
758 status=MagickFalse;
759 continue;
760 }
cristybb503372010-05-27 20:51:26 +0000761 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000762 {
cristy0cf6da52012-08-25 00:35:24 +0000763 register ssize_t
764 i;
765
766 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
767 {
cristy5a23c552013-02-13 14:34:28 +0000768 PixelChannel channel=GetPixelChannelChannel(image,i);
769 PixelTrait traits=GetPixelChannelTraits(image,channel);
770 PixelTrait colorize_traits=GetPixelChannelTraits(colorize_image,
771 channel);
cristy0cf6da52012-08-25 00:35:24 +0000772 if ((traits == UndefinedPixelTrait) ||
773 (colorize_traits == UndefinedPixelTrait))
cristyd6803382012-04-10 01:41:25 +0000774 continue;
cristy0cf6da52012-08-25 00:35:24 +0000775 if (((colorize_traits & CopyPixelTrait) != 0) ||
776 (GetPixelMask(image,p) != 0))
777 {
778 SetPixelChannel(colorize_image,channel,p[i],q);
779 continue;
780 }
cristyb7113232013-02-15 00:35:19 +0000781 SetPixelChannel(colorize_image,channel,ClampToQuantum(Colorize(p[i],
782 GetPixelInfoChannel(&blend_percentage,channel),GetPixelInfoChannel(
783 colorize,channel))),q);
cristy0cf6da52012-08-25 00:35:24 +0000784 }
cristyed231572011-07-14 02:18:59 +0000785 p+=GetPixelChannels(image);
786 q+=GetPixelChannels(colorize_image);
cristy3ed852e2009-09-05 21:47:34 +0000787 }
788 sync=SyncCacheViewAuthenticPixels(colorize_view,exception);
789 if (sync == MagickFalse)
790 status=MagickFalse;
791 if (image->progress_monitor != (MagickProgressMonitor) NULL)
792 {
793 MagickBooleanType
794 proceed;
795
cristy319a1e72010-02-21 15:13:11 +0000796#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +0000797 #pragma omp critical (MagickCore_ColorizeImage)
cristy3ed852e2009-09-05 21:47:34 +0000798#endif
799 proceed=SetImageProgress(image,ColorizeImageTag,progress++,image->rows);
800 if (proceed == MagickFalse)
801 status=MagickFalse;
802 }
803 }
804 image_view=DestroyCacheView(image_view);
805 colorize_view=DestroyCacheView(colorize_view);
806 if (status == MagickFalse)
807 colorize_image=DestroyImage(colorize_image);
808 return(colorize_image);
809}
810
811/*
812%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
813% %
814% %
815% %
cristye6365592010-04-02 17:31:23 +0000816% C o l o r M a t r i x I m a g e %
817% %
818% %
819% %
820%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
821%
822% ColorMatrixImage() applies color transformation to an image. This method
823% permits saturation changes, hue rotation, luminance to alpha, and various
824% other effects. Although variable-sized transformation matrices can be used,
825% typically one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA
826% (or RGBA with offsets). The matrix is similar to those used by Adobe Flash
827% except offsets are in column 6 rather than 5 (in support of CMYKA images)
828% and offsets are normalized (divide Flash offset by 255).
829%
830% The format of the ColorMatrixImage method is:
831%
832% Image *ColorMatrixImage(const Image *image,
833% const KernelInfo *color_matrix,ExceptionInfo *exception)
834%
835% A description of each parameter follows:
836%
837% o image: the image.
838%
839% o color_matrix: the color matrix.
840%
841% o exception: return any errors or warnings in this structure.
842%
843*/
anthonyfd706f92012-01-19 04:22:02 +0000844/* FUTURE: modify to make use of a MagickMatrix Mutliply function
845 That should be provided in "matrix.c"
846 (ASIDE: actually distorts should do this too but currently doesn't)
847*/
848
cristye6365592010-04-02 17:31:23 +0000849MagickExport Image *ColorMatrixImage(const Image *image,
850 const KernelInfo *color_matrix,ExceptionInfo *exception)
851{
852#define ColorMatrixImageTag "ColorMatrix/Image"
853
854 CacheView
855 *color_view,
856 *image_view;
857
858 double
859 ColorMatrix[6][6] =
860 {
861 { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
862 { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
863 { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
864 { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
865 { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
866 { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }
867 };
868
869 Image
870 *color_image;
871
cristye6365592010-04-02 17:31:23 +0000872 MagickBooleanType
873 status;
874
cristybb503372010-05-27 20:51:26 +0000875 MagickOffsetType
876 progress;
877
878 register ssize_t
cristye6365592010-04-02 17:31:23 +0000879 i;
880
cristybb503372010-05-27 20:51:26 +0000881 ssize_t
882 u,
883 v,
884 y;
885
cristye6365592010-04-02 17:31:23 +0000886 /*
anthonyfd706f92012-01-19 04:22:02 +0000887 Map given color_matrix, into a 6x6 matrix RGBKA and a constant
cristye6365592010-04-02 17:31:23 +0000888 */
889 assert(image != (Image *) NULL);
890 assert(image->signature == MagickSignature);
891 if (image->debug != MagickFalse)
892 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
893 assert(exception != (ExceptionInfo *) NULL);
894 assert(exception->signature == MagickSignature);
895 i=0;
cristybb503372010-05-27 20:51:26 +0000896 for (v=0; v < (ssize_t) color_matrix->height; v++)
897 for (u=0; u < (ssize_t) color_matrix->width; u++)
cristye6365592010-04-02 17:31:23 +0000898 {
899 if ((v < 6) && (u < 6))
900 ColorMatrix[v][u]=color_matrix->values[i];
901 i++;
902 }
903 /*
904 Initialize color image.
905 */
cristy12550e62010-06-07 12:46:40 +0000906 color_image=CloneImage(image,0,0,MagickTrue,exception);
cristye6365592010-04-02 17:31:23 +0000907 if (color_image == (Image *) NULL)
908 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000909 if (SetImageStorageClass(color_image,DirectClass,exception) == MagickFalse)
cristye6365592010-04-02 17:31:23 +0000910 {
cristye6365592010-04-02 17:31:23 +0000911 color_image=DestroyImage(color_image);
912 return((Image *) NULL);
913 }
914 if (image->debug != MagickFalse)
915 {
916 char
917 format[MaxTextExtent],
918 *message;
919
920 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
921 " ColorMatrix image with color matrix:");
922 message=AcquireString("");
923 for (v=0; v < 6; v++)
924 {
925 *message='\0';
cristyb51dff52011-05-19 16:55:47 +0000926 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristye6365592010-04-02 17:31:23 +0000927 (void) ConcatenateString(&message,format);
928 for (u=0; u < 6; u++)
929 {
cristyb51dff52011-05-19 16:55:47 +0000930 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",
cristye6365592010-04-02 17:31:23 +0000931 ColorMatrix[v][u]);
932 (void) ConcatenateString(&message,format);
933 }
934 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
935 }
936 message=DestroyString(message);
937 }
938 /*
anthonyfd706f92012-01-19 04:22:02 +0000939 Apply the ColorMatrix to image.
cristye6365592010-04-02 17:31:23 +0000940 */
941 status=MagickTrue;
942 progress=0;
cristy46ff2672012-12-14 15:32:26 +0000943 image_view=AcquireVirtualCacheView(image,exception);
944 color_view=AcquireAuthenticCacheView(color_image,exception);
cristye6365592010-04-02 17:31:23 +0000945#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000946 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +0000947 magick_threads(image,color_image,image->rows,1)
cristye6365592010-04-02 17:31:23 +0000948#endif
cristybb503372010-05-27 20:51:26 +0000949 for (y=0; y < (ssize_t) image->rows; y++)
cristye6365592010-04-02 17:31:23 +0000950 {
cristyfcc25d92012-02-19 23:06:48 +0000951 PixelInfo
cristye6365592010-04-02 17:31:23 +0000952 pixel;
953
cristy4c08aed2011-07-01 19:47:50 +0000954 register const Quantum
cristye6365592010-04-02 17:31:23 +0000955 *restrict p;
956
cristy4c08aed2011-07-01 19:47:50 +0000957 register Quantum
958 *restrict q;
959
cristybb503372010-05-27 20:51:26 +0000960 register ssize_t
cristye6365592010-04-02 17:31:23 +0000961 x;
962
cristye6365592010-04-02 17:31:23 +0000963 if (status == MagickFalse)
964 continue;
965 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
966 q=GetCacheViewAuthenticPixels(color_view,0,y,color_image->columns,1,
967 exception);
cristy4c08aed2011-07-01 19:47:50 +0000968 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristye6365592010-04-02 17:31:23 +0000969 {
970 status=MagickFalse;
971 continue;
972 }
cristyfcc25d92012-02-19 23:06:48 +0000973 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +0000974 for (x=0; x < (ssize_t) image->columns; x++)
cristye6365592010-04-02 17:31:23 +0000975 {
cristybb503372010-05-27 20:51:26 +0000976 register ssize_t
cristye6365592010-04-02 17:31:23 +0000977 v;
978
cristybb503372010-05-27 20:51:26 +0000979 size_t
cristye6365592010-04-02 17:31:23 +0000980 height;
981
cristyfcc25d92012-02-19 23:06:48 +0000982 GetPixelInfoPixel(image,p,&pixel);
cristye6365592010-04-02 17:31:23 +0000983 height=color_matrix->height > 6 ? 6UL : color_matrix->height;
cristybb503372010-05-27 20:51:26 +0000984 for (v=0; v < (ssize_t) height; v++)
cristye6365592010-04-02 17:31:23 +0000985 {
cristya19f1d72012-08-07 18:24:38 +0000986 double
cristyfcc25d92012-02-19 23:06:48 +0000987 sum;
988
989 sum=ColorMatrix[v][0]*GetPixelRed(image,p)+ColorMatrix[v][1]*
cristy4c08aed2011-07-01 19:47:50 +0000990 GetPixelGreen(image,p)+ColorMatrix[v][2]*GetPixelBlue(image,p);
cristye6365592010-04-02 17:31:23 +0000991 if (image->colorspace == CMYKColorspace)
cristyfcc25d92012-02-19 23:06:48 +0000992 sum+=ColorMatrix[v][3]*GetPixelBlack(image,p);
cristy8a46d822012-08-28 23:32:39 +0000993 if (image->alpha_trait == BlendPixelTrait)
cristyfcc25d92012-02-19 23:06:48 +0000994 sum+=ColorMatrix[v][4]*GetPixelAlpha(image,p);
995 sum+=QuantumRange*ColorMatrix[v][5];
cristye6365592010-04-02 17:31:23 +0000996 switch (v)
997 {
cristyfcc25d92012-02-19 23:06:48 +0000998 case 0: pixel.red=sum; break;
999 case 1: pixel.green=sum; break;
1000 case 2: pixel.blue=sum; break;
1001 case 3: pixel.black=sum; break;
1002 case 4: pixel.alpha=sum; break;
1003 default: break;
cristye6365592010-04-02 17:31:23 +00001004 }
1005 }
cristyfcc25d92012-02-19 23:06:48 +00001006 SetPixelInfoPixel(color_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00001007 p+=GetPixelChannels(image);
1008 q+=GetPixelChannels(color_image);
cristye6365592010-04-02 17:31:23 +00001009 }
1010 if (SyncCacheViewAuthenticPixels(color_view,exception) == MagickFalse)
1011 status=MagickFalse;
1012 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1013 {
1014 MagickBooleanType
1015 proceed;
1016
1017#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00001018 #pragma omp critical (MagickCore_ColorMatrixImage)
cristye6365592010-04-02 17:31:23 +00001019#endif
1020 proceed=SetImageProgress(image,ColorMatrixImageTag,progress++,
1021 image->rows);
1022 if (proceed == MagickFalse)
1023 status=MagickFalse;
1024 }
1025 }
1026 color_view=DestroyCacheView(color_view);
1027 image_view=DestroyCacheView(image_view);
1028 if (status == MagickFalse)
1029 color_image=DestroyImage(color_image);
1030 return(color_image);
1031}
1032
1033/*
1034%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1035% %
1036% %
1037% %
cristy3ed852e2009-09-05 21:47:34 +00001038+ D e s t r o y F x I n f o %
1039% %
1040% %
1041% %
1042%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1043%
1044% DestroyFxInfo() deallocates memory associated with an FxInfo structure.
1045%
1046% The format of the DestroyFxInfo method is:
1047%
1048% ImageInfo *DestroyFxInfo(ImageInfo *fx_info)
1049%
1050% A description of each parameter follows:
1051%
1052% o fx_info: the fx info.
1053%
1054*/
cristy7832dc22011-09-05 01:21:53 +00001055MagickPrivate FxInfo *DestroyFxInfo(FxInfo *fx_info)
cristy3ed852e2009-09-05 21:47:34 +00001056{
cristybb503372010-05-27 20:51:26 +00001057 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001058 i;
1059
1060 fx_info->exception=DestroyExceptionInfo(fx_info->exception);
1061 fx_info->expression=DestroyString(fx_info->expression);
1062 fx_info->symbols=DestroySplayTree(fx_info->symbols);
1063 fx_info->colors=DestroySplayTree(fx_info->colors);
cristy0ea377f2011-03-24 00:54:19 +00001064 for (i=(ssize_t) GetImageListLength(fx_info->images)-1; i >= 0; i--)
cristyd76c51e2011-03-26 00:21:26 +00001065 fx_info->view[i]=DestroyCacheView(fx_info->view[i]);
1066 fx_info->view=(CacheView **) RelinquishMagickMemory(fx_info->view);
cristy3ed852e2009-09-05 21:47:34 +00001067 fx_info->random_info=DestroyRandomInfo(fx_info->random_info);
1068 fx_info=(FxInfo *) RelinquishMagickMemory(fx_info);
1069 return(fx_info);
1070}
1071
1072/*
1073%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1074% %
1075% %
1076% %
cristy3ed852e2009-09-05 21:47:34 +00001077+ 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 %
1078% %
1079% %
1080% %
1081%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1082%
1083% FxEvaluateChannelExpression() evaluates an expression and returns the
1084% results.
1085%
1086% The format of the FxEvaluateExpression method is:
1087%
cristya19f1d72012-08-07 18:24:38 +00001088% double FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00001089% const PixelChannel channel,const ssize_t x,const ssize_t y,
cristya19f1d72012-08-07 18:24:38 +00001090% double *alpha,Exceptioninfo *exception)
1091% double FxEvaluateExpression(FxInfo *fx_info,
1092% double *alpha,Exceptioninfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001093%
1094% A description of each parameter follows:
1095%
1096% o fx_info: the fx info.
1097%
1098% o channel: the channel.
1099%
1100% o x,y: the pixel position.
1101%
1102% o alpha: the result.
1103%
1104% o exception: return any errors or warnings in this structure.
1105%
1106*/
1107
cristy351842f2010-03-07 15:27:38 +00001108static inline double MagickMax(const double x,const double y)
1109{
1110 if (x > y)
1111 return(x);
1112 return(y);
1113}
1114
1115static inline double MagickMin(const double x,const double y)
1116{
1117 if (x < y)
1118 return(x);
1119 return(y);
1120}
1121
cristya19f1d72012-08-07 18:24:38 +00001122static double FxChannelStatistics(FxInfo *fx_info,Image *image,
cristy0568ffc2011-07-25 16:54:14 +00001123 PixelChannel channel,const char *symbol,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001124{
cristy5048d302012-08-07 01:05:16 +00001125 ChannelType
1126 channel_mask;
1127
cristy3ed852e2009-09-05 21:47:34 +00001128 char
1129 key[MaxTextExtent],
1130 statistic[MaxTextExtent];
1131
1132 const char
1133 *value;
1134
1135 register const char
1136 *p;
1137
cristy5048d302012-08-07 01:05:16 +00001138 channel_mask=UndefinedChannel;
cristy3ed852e2009-09-05 21:47:34 +00001139 for (p=symbol; (*p != '.') && (*p != '\0'); p++) ;
1140 if (*p == '.')
cristy3ed852e2009-09-05 21:47:34 +00001141 {
cristy5048d302012-08-07 01:05:16 +00001142 ssize_t
1143 option;
1144
1145 option=ParseCommandOption(MagickPixelChannelOptions,MagickTrue,p+1);
1146 if (option >= 0)
1147 {
1148 channel=(PixelChannel) option;
1149 channel_mask=(ChannelType) (channel_mask | (1 << channel));
cristycf1296e2012-08-26 23:40:49 +00001150 SetPixelChannelMask(image,channel_mask);
cristy5048d302012-08-07 01:05:16 +00001151 }
cristy3ed852e2009-09-05 21:47:34 +00001152 }
cristyb51dff52011-05-19 16:55:47 +00001153 (void) FormatLocaleString(key,MaxTextExtent,"%p.%.20g.%s",(void *) image,
cristye8c25f92010-06-03 00:53:06 +00001154 (double) channel,symbol);
cristy3ed852e2009-09-05 21:47:34 +00001155 value=(const char *) GetValueFromSplayTree(fx_info->symbols,key);
1156 if (value != (const char *) NULL)
cristy5048d302012-08-07 01:05:16 +00001157 {
1158 if (channel_mask != UndefinedChannel)
cristycf1296e2012-08-26 23:40:49 +00001159 SetPixelChannelMask(image,channel_mask);
cristy5048d302012-08-07 01:05:16 +00001160 return(QuantumScale*StringToDouble(value,(char **) NULL));
1161 }
cristy3ed852e2009-09-05 21:47:34 +00001162 (void) DeleteNodeFromSplayTree(fx_info->symbols,key);
1163 if (LocaleNCompare(symbol,"depth",5) == 0)
1164 {
cristybb503372010-05-27 20:51:26 +00001165 size_t
cristy3ed852e2009-09-05 21:47:34 +00001166 depth;
1167
cristyfefab1b2011-07-05 00:33:22 +00001168 depth=GetImageDepth(image,exception);
1169 (void) FormatLocaleString(statistic,MaxTextExtent,"%.20g",(double) depth);
cristy3ed852e2009-09-05 21:47:34 +00001170 }
1171 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1172 {
1173 double
1174 kurtosis,
1175 skewness;
1176
cristyd42d9952011-07-08 14:21:50 +00001177 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001178 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",kurtosis);
cristy3ed852e2009-09-05 21:47:34 +00001179 }
1180 if (LocaleNCompare(symbol,"maxima",6) == 0)
1181 {
1182 double
1183 maxima,
1184 minima;
1185
cristyd42d9952011-07-08 14:21:50 +00001186 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001187 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",maxima);
cristy3ed852e2009-09-05 21:47:34 +00001188 }
1189 if (LocaleNCompare(symbol,"mean",4) == 0)
1190 {
1191 double
1192 mean,
1193 standard_deviation;
1194
cristyd42d9952011-07-08 14:21:50 +00001195 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001196 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",mean);
cristy3ed852e2009-09-05 21:47:34 +00001197 }
1198 if (LocaleNCompare(symbol,"minima",6) == 0)
1199 {
1200 double
1201 maxima,
1202 minima;
1203
cristyd42d9952011-07-08 14:21:50 +00001204 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001205 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",minima);
cristy3ed852e2009-09-05 21:47:34 +00001206 }
1207 if (LocaleNCompare(symbol,"skewness",8) == 0)
1208 {
1209 double
1210 kurtosis,
1211 skewness;
1212
cristyd42d9952011-07-08 14:21:50 +00001213 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001214 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",skewness);
cristy3ed852e2009-09-05 21:47:34 +00001215 }
1216 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1217 {
1218 double
1219 mean,
1220 standard_deviation;
1221
cristyd42d9952011-07-08 14:21:50 +00001222 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001223 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",
cristy3ed852e2009-09-05 21:47:34 +00001224 standard_deviation);
1225 }
cristy5048d302012-08-07 01:05:16 +00001226 if (channel_mask != UndefinedChannel)
cristycf1296e2012-08-26 23:40:49 +00001227 SetPixelChannelMask(image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00001228 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(key),
1229 ConstantString(statistic));
cristydbdd0e32011-11-04 23:29:40 +00001230 return(QuantumScale*StringToDouble(statistic,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001231}
1232
cristya19f1d72012-08-07 18:24:38 +00001233static double
cristy0568ffc2011-07-25 16:54:14 +00001234 FxEvaluateSubexpression(FxInfo *,const PixelChannel,const ssize_t,
cristya19f1d72012-08-07 18:24:38 +00001235 const ssize_t,const char *,double *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +00001236
cristyb0aad4c2011-11-02 19:30:35 +00001237static MagickOffsetType FxGCD(MagickOffsetType alpha,MagickOffsetType beta)
1238{
1239 if (beta != 0)
1240 return(FxGCD(beta,alpha % beta));
1241 return(alpha);
1242}
1243
cristy3ed852e2009-09-05 21:47:34 +00001244static inline const char *FxSubexpression(const char *expression,
1245 ExceptionInfo *exception)
1246{
1247 const char
1248 *subexpression;
1249
cristybb503372010-05-27 20:51:26 +00001250 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001251 level;
1252
1253 level=0;
1254 subexpression=expression;
1255 while ((*subexpression != '\0') &&
1256 ((level != 1) || (strchr(")",(int) *subexpression) == (char *) NULL)))
1257 {
1258 if (strchr("(",(int) *subexpression) != (char *) NULL)
1259 level++;
1260 else
1261 if (strchr(")",(int) *subexpression) != (char *) NULL)
1262 level--;
1263 subexpression++;
1264 }
1265 if (*subexpression == '\0')
1266 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
cristyefe601c2013-01-05 17:51:12 +00001267 "UnbalancedParenthesis","`%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00001268 return(subexpression);
1269}
1270
cristya19f1d72012-08-07 18:24:38 +00001271static double FxGetSymbol(FxInfo *fx_info,const PixelChannel channel,
cristye85007d2010-06-06 22:51:36 +00001272 const ssize_t x,const ssize_t y,const char *expression,
1273 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001274{
1275 char
1276 *q,
1277 subexpression[MaxTextExtent],
1278 symbol[MaxTextExtent];
1279
1280 const char
1281 *p,
1282 *value;
1283
1284 Image
1285 *image;
1286
cristy4c08aed2011-07-01 19:47:50 +00001287 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001288 pixel;
1289
cristya19f1d72012-08-07 18:24:38 +00001290 double
cristy3ed852e2009-09-05 21:47:34 +00001291 alpha,
1292 beta;
1293
1294 PointInfo
1295 point;
1296
cristybb503372010-05-27 20:51:26 +00001297 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001298 i;
1299
1300 size_t
cristy1707c6c2012-01-18 23:30:54 +00001301 length,
cristy3ed852e2009-09-05 21:47:34 +00001302 level;
1303
1304 p=expression;
1305 i=GetImageIndexInList(fx_info->images);
1306 level=0;
1307 point.x=(double) x;
1308 point.y=(double) y;
1309 if (isalpha((int) *(p+1)) == 0)
1310 {
1311 if (strchr("suv",(int) *p) != (char *) NULL)
1312 {
1313 switch (*p)
1314 {
1315 case 's':
1316 default:
1317 {
1318 i=GetImageIndexInList(fx_info->images);
1319 break;
1320 }
1321 case 'u': i=0; break;
1322 case 'v': i=1; break;
1323 }
1324 p++;
1325 if (*p == '[')
1326 {
1327 level++;
1328 q=subexpression;
1329 for (p++; *p != '\0'; )
1330 {
1331 if (*p == '[')
1332 level++;
1333 else
1334 if (*p == ']')
1335 {
1336 level--;
1337 if (level == 0)
1338 break;
1339 }
1340 *q++=(*p++);
1341 }
1342 *q='\0';
1343 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1344 &beta,exception);
cristybb503372010-05-27 20:51:26 +00001345 i=(ssize_t) (alpha+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001346 p++;
1347 }
1348 if (*p == '.')
1349 p++;
1350 }
1351 if ((isalpha((int) *(p+1)) == 0) && (*p == 'p'))
1352 {
1353 p++;
1354 if (*p == '{')
1355 {
1356 level++;
1357 q=subexpression;
1358 for (p++; *p != '\0'; )
1359 {
1360 if (*p == '{')
1361 level++;
1362 else
1363 if (*p == '}')
1364 {
1365 level--;
1366 if (level == 0)
1367 break;
1368 }
1369 *q++=(*p++);
1370 }
1371 *q='\0';
1372 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1373 &beta,exception);
1374 point.x=alpha;
1375 point.y=beta;
1376 p++;
1377 }
1378 else
1379 if (*p == '[')
1380 {
1381 level++;
1382 q=subexpression;
1383 for (p++; *p != '\0'; )
1384 {
1385 if (*p == '[')
1386 level++;
1387 else
1388 if (*p == ']')
1389 {
1390 level--;
1391 if (level == 0)
1392 break;
1393 }
1394 *q++=(*p++);
1395 }
1396 *q='\0';
1397 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1398 &beta,exception);
1399 point.x+=alpha;
1400 point.y+=beta;
1401 p++;
1402 }
1403 if (*p == '.')
1404 p++;
1405 }
1406 }
1407 length=GetImageListLength(fx_info->images);
1408 while (i < 0)
cristybb503372010-05-27 20:51:26 +00001409 i+=(ssize_t) length;
cristy3ed852e2009-09-05 21:47:34 +00001410 i%=length;
1411 image=GetImageFromList(fx_info->images,i);
1412 if (image == (Image *) NULL)
1413 {
1414 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
cristyefe601c2013-01-05 17:51:12 +00001415 "NoSuchImage","`%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00001416 return(0.0);
1417 }
cristy4c08aed2011-07-01 19:47:50 +00001418 GetPixelInfo(image,&pixel);
1419 (void) InterpolatePixelInfo(image,fx_info->view[i],image->interpolate,
cristy4f820712011-04-01 12:35:43 +00001420 point.x,point.y,&pixel,exception);
cristy1707c6c2012-01-18 23:30:54 +00001421 if ((strlen(p) > 2) && (LocaleCompare(p,"intensity") != 0) &&
1422 (LocaleCompare(p,"luminance") != 0) && (LocaleCompare(p,"hue") != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00001423 (LocaleCompare(p,"saturation") != 0) &&
1424 (LocaleCompare(p,"lightness") != 0))
1425 {
1426 char
1427 name[MaxTextExtent];
1428
1429 (void) CopyMagickString(name,p,MaxTextExtent);
1430 for (q=name+(strlen(name)-1); q > name; q--)
1431 {
1432 if (*q == ')')
1433 break;
1434 if (*q == '.')
1435 {
1436 *q='\0';
1437 break;
1438 }
1439 }
1440 if ((strlen(name) > 2) &&
1441 (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
1442 {
cristy4c08aed2011-07-01 19:47:50 +00001443 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001444 *color;
1445
cristy4c08aed2011-07-01 19:47:50 +00001446 color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
1447 if (color != (PixelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001448 {
1449 pixel=(*color);
1450 p+=strlen(name);
1451 }
1452 else
cristy1707c6c2012-01-18 23:30:54 +00001453 {
1454 MagickBooleanType
1455 status;
1456
1457 status=QueryColorCompliance(name,AllCompliance,&pixel,
1458 fx_info->exception);
1459 if (status != MagickFalse)
1460 {
1461 (void) AddValueToSplayTree(fx_info->colors,ConstantString(
1462 name),ClonePixelInfo(&pixel));
1463 p+=strlen(name);
1464 }
1465 }
cristy3ed852e2009-09-05 21:47:34 +00001466 }
1467 }
1468 (void) CopyMagickString(symbol,p,MaxTextExtent);
1469 StripString(symbol);
1470 if (*symbol == '\0')
1471 {
1472 switch (channel)
1473 {
cristy0568ffc2011-07-25 16:54:14 +00001474 case RedPixelChannel: return(QuantumScale*pixel.red);
1475 case GreenPixelChannel: return(QuantumScale*pixel.green);
1476 case BluePixelChannel: return(QuantumScale*pixel.blue);
1477 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001478 {
1479 if (image->colorspace != CMYKColorspace)
1480 {
1481 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001482 ImageError,"ColorSeparatedImageRequired","`%s'",
cristy3ed852e2009-09-05 21:47:34 +00001483 image->filename);
1484 return(0.0);
1485 }
cristy4c08aed2011-07-01 19:47:50 +00001486 return(QuantumScale*pixel.black);
1487 }
cristy0568ffc2011-07-25 16:54:14 +00001488 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001489 {
cristya19f1d72012-08-07 18:24:38 +00001490 double
cristy4c08aed2011-07-01 19:47:50 +00001491 alpha;
1492
cristy8a46d822012-08-28 23:32:39 +00001493 if (pixel.alpha_trait != BlendPixelTrait)
cristy4c08aed2011-07-01 19:47:50 +00001494 return(1.0);
cristya19f1d72012-08-07 18:24:38 +00001495 alpha=(double) (QuantumScale*pixel.alpha);
cristy4c08aed2011-07-01 19:47:50 +00001496 return(alpha);
cristy3ed852e2009-09-05 21:47:34 +00001497 }
cristya382aca2011-12-06 18:22:48 +00001498 case IndexPixelChannel:
1499 return(0.0);
cristyb3a73b52011-07-26 01:34:43 +00001500 case IntensityPixelChannel:
cristyf364ed42010-12-15 01:54:43 +00001501 {
cristy4c08aed2011-07-01 19:47:50 +00001502 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristyf364ed42010-12-15 01:54:43 +00001503 }
cristy3ed852e2009-09-05 21:47:34 +00001504 default:
1505 break;
1506 }
1507 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
cristyefe601c2013-01-05 17:51:12 +00001508 "UnableToParseExpression","`%s'",p);
cristy3ed852e2009-09-05 21:47:34 +00001509 return(0.0);
1510 }
1511 switch (*symbol)
1512 {
1513 case 'A':
1514 case 'a':
1515 {
1516 if (LocaleCompare(symbol,"a") == 0)
cristya19f1d72012-08-07 18:24:38 +00001517 return((double) (QuantumScale*pixel.alpha));
cristy3ed852e2009-09-05 21:47:34 +00001518 break;
1519 }
1520 case 'B':
1521 case 'b':
1522 {
1523 if (LocaleCompare(symbol,"b") == 0)
1524 return(QuantumScale*pixel.blue);
1525 break;
1526 }
1527 case 'C':
1528 case 'c':
1529 {
1530 if (LocaleNCompare(symbol,"channel",7) == 0)
1531 {
1532 GeometryInfo
1533 channel_info;
1534
1535 MagickStatusType
1536 flags;
1537
1538 flags=ParseGeometry(symbol+7,&channel_info);
1539 if (image->colorspace == CMYKColorspace)
1540 switch (channel)
1541 {
cristy0568ffc2011-07-25 16:54:14 +00001542 case CyanPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001543 {
1544 if ((flags & RhoValue) == 0)
1545 return(0.0);
1546 return(channel_info.rho);
1547 }
cristy0568ffc2011-07-25 16:54:14 +00001548 case MagentaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001549 {
1550 if ((flags & SigmaValue) == 0)
1551 return(0.0);
1552 return(channel_info.sigma);
1553 }
cristy0568ffc2011-07-25 16:54:14 +00001554 case YellowPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001555 {
1556 if ((flags & XiValue) == 0)
1557 return(0.0);
1558 return(channel_info.xi);
1559 }
cristy0568ffc2011-07-25 16:54:14 +00001560 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001561 {
1562 if ((flags & PsiValue) == 0)
1563 return(0.0);
1564 return(channel_info.psi);
1565 }
cristy0568ffc2011-07-25 16:54:14 +00001566 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001567 {
1568 if ((flags & ChiValue) == 0)
1569 return(0.0);
1570 return(channel_info.chi);
1571 }
1572 default:
1573 return(0.0);
1574 }
1575 switch (channel)
1576 {
cristy0568ffc2011-07-25 16:54:14 +00001577 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001578 {
1579 if ((flags & RhoValue) == 0)
1580 return(0.0);
1581 return(channel_info.rho);
1582 }
cristy0568ffc2011-07-25 16:54:14 +00001583 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001584 {
1585 if ((flags & SigmaValue) == 0)
1586 return(0.0);
1587 return(channel_info.sigma);
1588 }
cristy0568ffc2011-07-25 16:54:14 +00001589 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001590 {
1591 if ((flags & XiValue) == 0)
1592 return(0.0);
1593 return(channel_info.xi);
1594 }
cristy0568ffc2011-07-25 16:54:14 +00001595 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001596 {
1597 if ((flags & ChiValue) == 0)
1598 return(0.0);
1599 return(channel_info.chi);
1600 }
cristy0568ffc2011-07-25 16:54:14 +00001601 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001602 {
1603 if ((flags & PsiValue) == 0)
1604 return(0.0);
1605 return(channel_info.psi);
1606 }
cristy3ed852e2009-09-05 21:47:34 +00001607 default:
1608 return(0.0);
1609 }
1610 return(0.0);
1611 }
1612 if (LocaleCompare(symbol,"c") == 0)
1613 return(QuantumScale*pixel.red);
1614 break;
1615 }
1616 case 'D':
1617 case 'd':
1618 {
1619 if (LocaleNCompare(symbol,"depth",5) == 0)
1620 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1621 break;
1622 }
1623 case 'G':
1624 case 'g':
1625 {
1626 if (LocaleCompare(symbol,"g") == 0)
1627 return(QuantumScale*pixel.green);
1628 break;
1629 }
1630 case 'K':
1631 case 'k':
1632 {
1633 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1634 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1635 if (LocaleCompare(symbol,"k") == 0)
1636 {
1637 if (image->colorspace != CMYKColorspace)
1638 {
1639 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001640 OptionError,"ColorSeparatedImageRequired","`%s'",
cristy3ed852e2009-09-05 21:47:34 +00001641 image->filename);
1642 return(0.0);
1643 }
cristy4c08aed2011-07-01 19:47:50 +00001644 return(QuantumScale*pixel.black);
cristy3ed852e2009-09-05 21:47:34 +00001645 }
1646 break;
1647 }
1648 case 'H':
1649 case 'h':
1650 {
1651 if (LocaleCompare(symbol,"h") == 0)
cristya19f1d72012-08-07 18:24:38 +00001652 return((double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001653 if (LocaleCompare(symbol,"hue") == 0)
1654 {
1655 double
1656 hue,
1657 lightness,
1658 saturation;
1659
cristy0a39a5c2012-06-27 12:51:45 +00001660 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
cristyda1f9c12011-10-02 21:39:49 +00001661 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001662 return(hue);
1663 }
1664 break;
1665 }
1666 case 'I':
1667 case 'i':
1668 {
1669 if ((LocaleCompare(symbol,"image.depth") == 0) ||
1670 (LocaleCompare(symbol,"image.minima") == 0) ||
1671 (LocaleCompare(symbol,"image.maxima") == 0) ||
1672 (LocaleCompare(symbol,"image.mean") == 0) ||
1673 (LocaleCompare(symbol,"image.kurtosis") == 0) ||
1674 (LocaleCompare(symbol,"image.skewness") == 0) ||
1675 (LocaleCompare(symbol,"image.standard_deviation") == 0))
1676 return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
1677 if (LocaleCompare(symbol,"image.resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001678 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001679 if (LocaleCompare(symbol,"image.resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001680 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001681 if (LocaleCompare(symbol,"intensity") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001682 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001683 if (LocaleCompare(symbol,"i") == 0)
cristya19f1d72012-08-07 18:24:38 +00001684 return((double) x);
cristy3ed852e2009-09-05 21:47:34 +00001685 break;
1686 }
1687 case 'J':
1688 case 'j':
1689 {
1690 if (LocaleCompare(symbol,"j") == 0)
cristya19f1d72012-08-07 18:24:38 +00001691 return((double) y);
cristy3ed852e2009-09-05 21:47:34 +00001692 break;
1693 }
1694 case 'L':
1695 case 'l':
1696 {
1697 if (LocaleCompare(symbol,"lightness") == 0)
1698 {
1699 double
1700 hue,
1701 lightness,
1702 saturation;
1703
cristy0a39a5c2012-06-27 12:51:45 +00001704 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
cristyda1f9c12011-10-02 21:39:49 +00001705 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001706 return(lightness);
1707 }
1708 if (LocaleCompare(symbol,"luminance") == 0)
1709 {
1710 double
1711 luminence;
1712
cristya86a5cb2012-10-14 13:40:33 +00001713 luminence=0.21267f*pixel.red+0.71516f*pixel.green+0.07217f*pixel.blue;
cristy3ed852e2009-09-05 21:47:34 +00001714 return(QuantumScale*luminence);
1715 }
1716 break;
1717 }
1718 case 'M':
1719 case 'm':
1720 {
1721 if (LocaleNCompare(symbol,"maxima",6) == 0)
1722 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1723 if (LocaleNCompare(symbol,"mean",4) == 0)
1724 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1725 if (LocaleNCompare(symbol,"minima",6) == 0)
1726 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1727 if (LocaleCompare(symbol,"m") == 0)
1728 return(QuantumScale*pixel.blue);
1729 break;
1730 }
1731 case 'N':
1732 case 'n':
1733 {
1734 if (LocaleCompare(symbol,"n") == 0)
cristya19f1d72012-08-07 18:24:38 +00001735 return((double) GetImageListLength(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001736 break;
1737 }
1738 case 'O':
1739 case 'o':
1740 {
1741 if (LocaleCompare(symbol,"o") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001742 return(QuantumScale*pixel.alpha);
cristy3ed852e2009-09-05 21:47:34 +00001743 break;
1744 }
1745 case 'P':
1746 case 'p':
1747 {
1748 if (LocaleCompare(symbol,"page.height") == 0)
cristya19f1d72012-08-07 18:24:38 +00001749 return((double) image->page.height);
cristy3ed852e2009-09-05 21:47:34 +00001750 if (LocaleCompare(symbol,"page.width") == 0)
cristya19f1d72012-08-07 18:24:38 +00001751 return((double) image->page.width);
cristy3ed852e2009-09-05 21:47:34 +00001752 if (LocaleCompare(symbol,"page.x") == 0)
cristya19f1d72012-08-07 18:24:38 +00001753 return((double) image->page.x);
cristy3ed852e2009-09-05 21:47:34 +00001754 if (LocaleCompare(symbol,"page.y") == 0)
cristya19f1d72012-08-07 18:24:38 +00001755 return((double) image->page.y);
cristy3ed852e2009-09-05 21:47:34 +00001756 break;
1757 }
1758 case 'R':
1759 case 'r':
1760 {
1761 if (LocaleCompare(symbol,"resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001762 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001763 if (LocaleCompare(symbol,"resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001764 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001765 if (LocaleCompare(symbol,"r") == 0)
1766 return(QuantumScale*pixel.red);
1767 break;
1768 }
1769 case 'S':
1770 case 's':
1771 {
1772 if (LocaleCompare(symbol,"saturation") == 0)
1773 {
1774 double
1775 hue,
1776 lightness,
1777 saturation;
1778
cristy0a39a5c2012-06-27 12:51:45 +00001779 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
cristyda1f9c12011-10-02 21:39:49 +00001780 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001781 return(saturation);
1782 }
1783 if (LocaleNCompare(symbol,"skewness",8) == 0)
1784 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1785 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1786 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1787 break;
1788 }
1789 case 'T':
1790 case 't':
1791 {
1792 if (LocaleCompare(symbol,"t") == 0)
cristya19f1d72012-08-07 18:24:38 +00001793 return((double) GetImageIndexInList(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001794 break;
1795 }
1796 case 'W':
1797 case 'w':
1798 {
1799 if (LocaleCompare(symbol,"w") == 0)
cristya19f1d72012-08-07 18:24:38 +00001800 return((double) image->columns);
cristy3ed852e2009-09-05 21:47:34 +00001801 break;
1802 }
1803 case 'Y':
1804 case 'y':
1805 {
1806 if (LocaleCompare(symbol,"y") == 0)
1807 return(QuantumScale*pixel.green);
1808 break;
1809 }
1810 case 'Z':
1811 case 'z':
1812 {
1813 if (LocaleCompare(symbol,"z") == 0)
1814 {
cristya19f1d72012-08-07 18:24:38 +00001815 double
cristy3ed852e2009-09-05 21:47:34 +00001816 depth;
1817
cristya19f1d72012-08-07 18:24:38 +00001818 depth=(double) GetImageDepth(image,fx_info->exception);
cristy3ed852e2009-09-05 21:47:34 +00001819 return(depth);
1820 }
1821 break;
1822 }
1823 default:
1824 break;
1825 }
1826 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1827 if (value != (const char *) NULL)
cristya19f1d72012-08-07 18:24:38 +00001828 return((double) StringToDouble(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001829 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
cristyefe601c2013-01-05 17:51:12 +00001830 "UnableToParseExpression","`%s'",symbol);
cristy3ed852e2009-09-05 21:47:34 +00001831 return(0.0);
1832}
1833
1834static const char *FxOperatorPrecedence(const char *expression,
1835 ExceptionInfo *exception)
1836{
1837 typedef enum
1838 {
1839 UndefinedPrecedence,
1840 NullPrecedence,
1841 BitwiseComplementPrecedence,
1842 ExponentPrecedence,
cristy116af162010-08-13 01:25:47 +00001843 ExponentialNotationPrecedence,
cristy3ed852e2009-09-05 21:47:34 +00001844 MultiplyPrecedence,
1845 AdditionPrecedence,
1846 ShiftPrecedence,
1847 RelationalPrecedence,
1848 EquivalencyPrecedence,
1849 BitwiseAndPrecedence,
1850 BitwiseOrPrecedence,
1851 LogicalAndPrecedence,
1852 LogicalOrPrecedence,
1853 TernaryPrecedence,
1854 AssignmentPrecedence,
1855 CommaPrecedence,
1856 SeparatorPrecedence
1857 } FxPrecedence;
1858
1859 FxPrecedence
1860 precedence,
1861 target;
1862
1863 register const char
1864 *subexpression;
1865
1866 register int
1867 c;
1868
cristybb503372010-05-27 20:51:26 +00001869 size_t
cristy3ed852e2009-09-05 21:47:34 +00001870 level;
1871
1872 c=0;
1873 level=0;
1874 subexpression=(const char *) NULL;
1875 target=NullPrecedence;
1876 while (*expression != '\0')
1877 {
1878 precedence=UndefinedPrecedence;
1879 if ((isspace((int) ((char) *expression)) != 0) || (c == (int) '@'))
1880 {
1881 expression++;
1882 continue;
1883 }
cristy488fa882010-03-01 22:34:24 +00001884 switch (*expression)
1885 {
1886 case 'A':
1887 case 'a':
cristy3ed852e2009-09-05 21:47:34 +00001888 {
cristyb33454f2011-08-03 02:10:45 +00001889#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00001890 if (LocaleNCompare(expression,"acosh",5) == 0)
1891 {
1892 expression+=5;
1893 break;
1894 }
cristyb33454f2011-08-03 02:10:45 +00001895#endif
1896#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00001897 if (LocaleNCompare(expression,"asinh",5) == 0)
1898 {
1899 expression+=5;
1900 break;
1901 }
cristyb33454f2011-08-03 02:10:45 +00001902#endif
1903#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00001904 if (LocaleNCompare(expression,"atanh",5) == 0)
cristy488fa882010-03-01 22:34:24 +00001905 {
1906 expression+=5;
1907 break;
1908 }
cristyb33454f2011-08-03 02:10:45 +00001909#endif
anthony62838e52012-05-24 12:41:54 +00001910 if (LocaleNCompare(expression,"atan2",5) == 0)
1911 {
1912 expression+=5;
1913 break;
1914 }
cristy488fa882010-03-01 22:34:24 +00001915 break;
cristy3ed852e2009-09-05 21:47:34 +00001916 }
cristy62d455f2011-11-03 11:42:28 +00001917 case 'E':
1918 case 'e':
1919 {
1920 if ((LocaleNCompare(expression,"E+",2) == 0) ||
1921 (LocaleNCompare(expression,"E-",2) == 0))
1922 {
1923 expression+=2; /* scientific notation */
1924 break;
1925 }
1926 }
cristy488fa882010-03-01 22:34:24 +00001927 case 'J':
1928 case 'j':
1929 {
1930 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1931 (LocaleNCompare(expression,"j1",2) == 0))
1932 {
1933 expression+=2;
1934 break;
1935 }
1936 break;
1937 }
cristy2def9322010-06-18 23:59:37 +00001938 case '#':
1939 {
1940 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1941 expression++;
1942 break;
1943 }
cristy488fa882010-03-01 22:34:24 +00001944 default:
1945 break;
1946 }
cristy3ed852e2009-09-05 21:47:34 +00001947 if ((c == (int) '{') || (c == (int) '['))
1948 level++;
1949 else
1950 if ((c == (int) '}') || (c == (int) ']'))
1951 level--;
1952 if (level == 0)
1953 switch ((unsigned char) *expression)
1954 {
1955 case '~':
1956 case '!':
1957 {
1958 precedence=BitwiseComplementPrecedence;
1959 break;
1960 }
1961 case '^':
cristy6621e252010-08-13 00:42:57 +00001962 case '@':
cristy3ed852e2009-09-05 21:47:34 +00001963 {
1964 precedence=ExponentPrecedence;
1965 break;
1966 }
1967 default:
1968 {
1969 if (((c != 0) && ((isdigit((int) ((char) c)) != 0) ||
1970 (strchr(")",c) != (char *) NULL))) &&
1971 (((islower((int) ((char) *expression)) != 0) ||
1972 (strchr("(",(int) *expression) != (char *) NULL)) ||
1973 ((isdigit((int) ((char) c)) == 0) &&
1974 (isdigit((int) ((char) *expression)) != 0))) &&
1975 (strchr("xy",(int) *expression) == (char *) NULL))
1976 precedence=MultiplyPrecedence;
1977 break;
1978 }
1979 case '*':
1980 case '/':
1981 case '%':
1982 {
1983 precedence=MultiplyPrecedence;
1984 break;
1985 }
1986 case '+':
1987 case '-':
1988 {
1989 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
1990 (isalpha(c) != 0))
1991 precedence=AdditionPrecedence;
1992 break;
1993 }
1994 case LeftShiftOperator:
1995 case RightShiftOperator:
1996 {
1997 precedence=ShiftPrecedence;
1998 break;
1999 }
2000 case '<':
2001 case LessThanEqualOperator:
2002 case GreaterThanEqualOperator:
2003 case '>':
2004 {
2005 precedence=RelationalPrecedence;
2006 break;
2007 }
2008 case EqualOperator:
2009 case NotEqualOperator:
2010 {
2011 precedence=EquivalencyPrecedence;
2012 break;
2013 }
2014 case '&':
2015 {
2016 precedence=BitwiseAndPrecedence;
2017 break;
2018 }
2019 case '|':
2020 {
2021 precedence=BitwiseOrPrecedence;
2022 break;
2023 }
2024 case LogicalAndOperator:
2025 {
2026 precedence=LogicalAndPrecedence;
2027 break;
2028 }
2029 case LogicalOrOperator:
2030 {
2031 precedence=LogicalOrPrecedence;
2032 break;
2033 }
cristy116af162010-08-13 01:25:47 +00002034 case ExponentialNotation:
2035 {
2036 precedence=ExponentialNotationPrecedence;
2037 break;
2038 }
cristy3ed852e2009-09-05 21:47:34 +00002039 case ':':
2040 case '?':
2041 {
2042 precedence=TernaryPrecedence;
2043 break;
2044 }
2045 case '=':
2046 {
2047 precedence=AssignmentPrecedence;
2048 break;
2049 }
2050 case ',':
2051 {
2052 precedence=CommaPrecedence;
2053 break;
2054 }
2055 case ';':
2056 {
2057 precedence=SeparatorPrecedence;
2058 break;
2059 }
2060 }
2061 if ((precedence == BitwiseComplementPrecedence) ||
2062 (precedence == TernaryPrecedence) ||
2063 (precedence == AssignmentPrecedence))
2064 {
2065 if (precedence > target)
2066 {
2067 /*
2068 Right-to-left associativity.
2069 */
2070 target=precedence;
2071 subexpression=expression;
2072 }
2073 }
2074 else
2075 if (precedence >= target)
2076 {
2077 /*
2078 Left-to-right associativity.
2079 */
2080 target=precedence;
2081 subexpression=expression;
2082 }
2083 if (strchr("(",(int) *expression) != (char *) NULL)
2084 expression=FxSubexpression(expression,exception);
2085 c=(int) (*expression++);
2086 }
2087 return(subexpression);
2088}
2089
cristya19f1d72012-08-07 18:24:38 +00002090static double FxEvaluateSubexpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002091 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristya19f1d72012-08-07 18:24:38 +00002092 const char *expression,double *beta,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002093{
2094 char
2095 *q,
2096 subexpression[MaxTextExtent];
2097
cristya19f1d72012-08-07 18:24:38 +00002098 double
cristy3ed852e2009-09-05 21:47:34 +00002099 alpha,
2100 gamma;
2101
2102 register const char
2103 *p;
2104
2105 *beta=0.0;
2106 if (exception->severity != UndefinedException)
2107 return(0.0);
2108 while (isspace((int) *expression) != 0)
2109 expression++;
2110 if (*expression == '\0')
2111 {
2112 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
cristyefe601c2013-01-05 17:51:12 +00002113 "MissingExpression","`%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002114 return(0.0);
2115 }
cristy66322f02010-05-17 11:40:48 +00002116 *subexpression='\0';
cristy3ed852e2009-09-05 21:47:34 +00002117 p=FxOperatorPrecedence(expression,exception);
2118 if (p != (const char *) NULL)
2119 {
2120 (void) CopyMagickString(subexpression,expression,(size_t)
2121 (p-expression+1));
2122 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2123 exception);
2124 switch ((unsigned char) *p)
2125 {
2126 case '~':
2127 {
2128 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002129 *beta=(double) (~(size_t) *beta);
cristy3ed852e2009-09-05 21:47:34 +00002130 return(*beta);
2131 }
2132 case '!':
2133 {
2134 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2135 return(*beta == 0.0 ? 1.0 : 0.0);
2136 }
2137 case '^':
2138 {
2139 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2140 channel,x,y,++p,beta,exception));
2141 return(*beta);
2142 }
2143 case '*':
cristy116af162010-08-13 01:25:47 +00002144 case ExponentialNotation:
cristy3ed852e2009-09-05 21:47:34 +00002145 {
2146 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2147 return(alpha*(*beta));
2148 }
2149 case '/':
2150 {
2151 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2152 if (*beta == 0.0)
2153 {
2154 if (exception->severity == UndefinedException)
2155 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00002156 OptionError,"DivideByZero","`%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002157 return(0.0);
2158 }
2159 return(alpha/(*beta));
2160 }
2161 case '%':
2162 {
2163 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2164 *beta=fabs(floor(((double) *beta)+0.5));
2165 if (*beta == 0.0)
2166 {
2167 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00002168 OptionError,"DivideByZero","`%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002169 return(0.0);
2170 }
2171 return(fmod((double) alpha,(double) *beta));
2172 }
2173 case '+':
2174 {
2175 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2176 return(alpha+(*beta));
2177 }
2178 case '-':
2179 {
2180 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2181 return(alpha-(*beta));
2182 }
2183 case LeftShiftOperator:
2184 {
2185 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002186 *beta=(double) ((size_t) (alpha+0.5) << (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002187 return(*beta);
2188 }
2189 case RightShiftOperator:
2190 {
2191 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002192 *beta=(double) ((size_t) (alpha+0.5) >> (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002193 return(*beta);
2194 }
2195 case '<':
2196 {
2197 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2198 return(alpha < *beta ? 1.0 : 0.0);
2199 }
2200 case LessThanEqualOperator:
2201 {
2202 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2203 return(alpha <= *beta ? 1.0 : 0.0);
2204 }
2205 case '>':
2206 {
2207 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2208 return(alpha > *beta ? 1.0 : 0.0);
2209 }
2210 case GreaterThanEqualOperator:
2211 {
2212 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2213 return(alpha >= *beta ? 1.0 : 0.0);
2214 }
2215 case EqualOperator:
2216 {
2217 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy9b528342012-06-02 00:59:20 +00002218 return(fabs(alpha-(*beta)) < MagickEpsilon ? MagickEpsilon : 0.0);
cristy3ed852e2009-09-05 21:47:34 +00002219 }
2220 case NotEqualOperator:
2221 {
2222 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy972050b2012-06-04 22:09:17 +00002223 return(fabs(alpha-(*beta)) >= MagickEpsilon ? 1.0 : 0.0);
cristy3ed852e2009-09-05 21:47:34 +00002224 }
2225 case '&':
2226 {
2227 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002228 *beta=(double) ((size_t) (alpha+0.5) & (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002229 return(*beta);
2230 }
2231 case '|':
2232 {
2233 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002234 *beta=(double) ((size_t) (alpha+0.5) | (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002235 return(*beta);
2236 }
2237 case LogicalAndOperator:
2238 {
2239 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2240 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2241 return(*beta);
2242 }
2243 case LogicalOrOperator:
2244 {
2245 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2246 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2247 return(*beta);
2248 }
2249 case '?':
2250 {
cristya19f1d72012-08-07 18:24:38 +00002251 double
cristy3ed852e2009-09-05 21:47:34 +00002252 gamma;
2253
2254 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2255 q=subexpression;
2256 p=StringToken(":",&q);
2257 if (q == (char *) NULL)
2258 {
2259 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00002260 OptionError,"UnableToParseExpression","`%s'",subexpression);
cristy3ed852e2009-09-05 21:47:34 +00002261 return(0.0);
2262 }
cristy972050b2012-06-04 22:09:17 +00002263 if (fabs((double) alpha) >= MagickEpsilon)
cristy3ed852e2009-09-05 21:47:34 +00002264 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2265 else
2266 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2267 return(gamma);
2268 }
2269 case '=':
2270 {
2271 char
2272 numeric[MaxTextExtent];
2273
2274 q=subexpression;
2275 while (isalpha((int) ((unsigned char) *q)) != 0)
2276 q++;
2277 if (*q != '\0')
2278 {
2279 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00002280 OptionError,"UnableToParseExpression","`%s'",subexpression);
cristy3ed852e2009-09-05 21:47:34 +00002281 return(0.0);
2282 }
2283 ClearMagickException(exception);
2284 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristyb51dff52011-05-19 16:55:47 +00002285 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
cristy8cd5b312010-01-07 01:10:24 +00002286 *beta);
cristy3ed852e2009-09-05 21:47:34 +00002287 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2288 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2289 subexpression),ConstantString(numeric));
2290 return(*beta);
2291 }
2292 case ',':
2293 {
2294 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2295 return(alpha);
2296 }
2297 case ';':
2298 {
2299 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2300 return(*beta);
2301 }
2302 default:
2303 {
2304 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2305 exception);
2306 return(gamma);
2307 }
2308 }
2309 }
2310 if (strchr("(",(int) *expression) != (char *) NULL)
2311 {
2312 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2313 subexpression[strlen(subexpression)-1]='\0';
2314 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2315 exception);
2316 return(gamma);
2317 }
cristy8b8a3ae2010-10-23 18:49:46 +00002318 switch (*expression)
cristy3ed852e2009-09-05 21:47:34 +00002319 {
2320 case '+':
2321 {
2322 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2323 exception);
2324 return(1.0*gamma);
2325 }
2326 case '-':
2327 {
2328 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2329 exception);
2330 return(-1.0*gamma);
2331 }
2332 case '~':
2333 {
2334 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2335 exception);
cristya19f1d72012-08-07 18:24:38 +00002336 return((double) (~(size_t) (gamma+0.5)));
cristy3ed852e2009-09-05 21:47:34 +00002337 }
2338 case 'A':
2339 case 'a':
2340 {
2341 if (LocaleNCompare(expression,"abs",3) == 0)
2342 {
2343 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2344 exception);
cristya19f1d72012-08-07 18:24:38 +00002345 return((double) fabs((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002346 }
cristyb33454f2011-08-03 02:10:45 +00002347#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00002348 if (LocaleNCompare(expression,"acosh",5) == 0)
2349 {
2350 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2351 exception);
cristya19f1d72012-08-07 18:24:38 +00002352 return((double) acosh((double) alpha));
cristy363772a2011-07-28 23:25:33 +00002353 }
cristyb33454f2011-08-03 02:10:45 +00002354#endif
cristy3ed852e2009-09-05 21:47:34 +00002355 if (LocaleNCompare(expression,"acos",4) == 0)
2356 {
2357 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2358 exception);
cristya19f1d72012-08-07 18:24:38 +00002359 return((double) acos((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002360 }
cristy43c22f42010-03-30 12:34:07 +00002361#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002362 if (LocaleNCompare(expression,"airy",4) == 0)
2363 {
2364 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2365 exception);
2366 if (alpha == 0.0)
cristy2dd03222010-03-30 22:12:11 +00002367 return(1.0);
2368 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
cristy43c22f42010-03-30 12:34:07 +00002369 return(gamma*gamma);
cristyee56cf12010-03-01 22:17:06 +00002370 }
cristy43c22f42010-03-30 12:34:07 +00002371#endif
cristyb33454f2011-08-03 02:10:45 +00002372#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00002373 if (LocaleNCompare(expression,"asinh",5) == 0)
2374 {
2375 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2376 exception);
cristya19f1d72012-08-07 18:24:38 +00002377 return((double) asinh((double) alpha));
cristy363772a2011-07-28 23:25:33 +00002378 }
cristyb33454f2011-08-03 02:10:45 +00002379#endif
cristy3ed852e2009-09-05 21:47:34 +00002380 if (LocaleNCompare(expression,"asin",4) == 0)
2381 {
2382 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2383 exception);
cristya19f1d72012-08-07 18:24:38 +00002384 return((double) asin((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002385 }
2386 if (LocaleNCompare(expression,"alt",3) == 0)
2387 {
2388 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2389 exception);
cristybb503372010-05-27 20:51:26 +00002390 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
cristy3ed852e2009-09-05 21:47:34 +00002391 }
2392 if (LocaleNCompare(expression,"atan2",5) == 0)
2393 {
2394 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2395 exception);
cristya19f1d72012-08-07 18:24:38 +00002396 return((double) atan2((double) alpha,(double) *beta));
cristy3ed852e2009-09-05 21:47:34 +00002397 }
cristyb33454f2011-08-03 02:10:45 +00002398#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00002399 if (LocaleNCompare(expression,"atanh",5) == 0)
2400 {
2401 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2402 exception);
cristya19f1d72012-08-07 18:24:38 +00002403 return((double) atanh((double) alpha));
cristy363772a2011-07-28 23:25:33 +00002404 }
cristyb33454f2011-08-03 02:10:45 +00002405#endif
cristy3ed852e2009-09-05 21:47:34 +00002406 if (LocaleNCompare(expression,"atan",4) == 0)
2407 {
2408 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2409 exception);
cristya19f1d72012-08-07 18:24:38 +00002410 return((double) atan((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002411 }
2412 if (LocaleCompare(expression,"a") == 0)
2413 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2414 break;
2415 }
2416 case 'B':
2417 case 'b':
2418 {
2419 if (LocaleCompare(expression,"b") == 0)
2420 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2421 break;
2422 }
2423 case 'C':
2424 case 'c':
2425 {
2426 if (LocaleNCompare(expression,"ceil",4) == 0)
2427 {
2428 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2429 exception);
cristya19f1d72012-08-07 18:24:38 +00002430 return((double) ceil((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002431 }
2432 if (LocaleNCompare(expression,"cosh",4) == 0)
2433 {
2434 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2435 exception);
cristya19f1d72012-08-07 18:24:38 +00002436 return((double) cosh((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002437 }
2438 if (LocaleNCompare(expression,"cos",3) == 0)
2439 {
2440 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2441 exception);
cristya19f1d72012-08-07 18:24:38 +00002442 return((double) cos((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002443 }
2444 if (LocaleCompare(expression,"c") == 0)
2445 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2446 break;
2447 }
2448 case 'D':
2449 case 'd':
2450 {
2451 if (LocaleNCompare(expression,"debug",5) == 0)
2452 {
2453 const char
2454 *type;
2455
2456 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2457 exception);
2458 if (fx_info->images->colorspace == CMYKColorspace)
2459 switch (channel)
2460 {
cristy0568ffc2011-07-25 16:54:14 +00002461 case CyanPixelChannel: type="cyan"; break;
2462 case MagentaPixelChannel: type="magenta"; break;
2463 case YellowPixelChannel: type="yellow"; break;
2464 case AlphaPixelChannel: type="opacity"; break;
2465 case BlackPixelChannel: type="black"; break;
cristy3ed852e2009-09-05 21:47:34 +00002466 default: type="unknown"; break;
2467 }
2468 else
2469 switch (channel)
2470 {
cristy0568ffc2011-07-25 16:54:14 +00002471 case RedPixelChannel: type="red"; break;
2472 case GreenPixelChannel: type="green"; break;
2473 case BluePixelChannel: type="blue"; break;
2474 case AlphaPixelChannel: type="opacity"; break;
cristy3ed852e2009-09-05 21:47:34 +00002475 default: type="unknown"; break;
2476 }
2477 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2478 if (strlen(subexpression) > 1)
2479 subexpression[strlen(subexpression)-1]='\0';
2480 if (fx_info->file != (FILE *) NULL)
cristy1707c6c2012-01-18 23:30:54 +00002481 (void) FormatLocaleFile(fx_info->file,"%s[%.20g,%.20g].%s: "
2482 "%s=%.*g\n",fx_info->images->filename,(double) x,(double) y,type,
2483 subexpression,GetMagickPrecision(),(double) alpha);
cristy3ed852e2009-09-05 21:47:34 +00002484 return(0.0);
2485 }
cristy5597a8d2011-11-04 00:25:32 +00002486 if (LocaleNCompare(expression,"drc",3) == 0)
2487 {
2488 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2489 exception);
cristya19f1d72012-08-07 18:24:38 +00002490 return((double) (alpha/(*beta*(alpha-1.0)+1.0)));
cristy5597a8d2011-11-04 00:25:32 +00002491 }
cristy3ed852e2009-09-05 21:47:34 +00002492 break;
2493 }
2494 case 'E':
2495 case 'e':
2496 {
2497 if (LocaleCompare(expression,"epsilon") == 0)
cristya19f1d72012-08-07 18:24:38 +00002498 return((double) MagickEpsilon);
cristy3ed852e2009-09-05 21:47:34 +00002499 if (LocaleNCompare(expression,"exp",3) == 0)
2500 {
2501 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2502 exception);
cristya19f1d72012-08-07 18:24:38 +00002503 return((double) exp((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002504 }
2505 if (LocaleCompare(expression,"e") == 0)
cristya19f1d72012-08-07 18:24:38 +00002506 return((double) 2.7182818284590452354);
cristy3ed852e2009-09-05 21:47:34 +00002507 break;
2508 }
2509 case 'F':
2510 case 'f':
2511 {
2512 if (LocaleNCompare(expression,"floor",5) == 0)
2513 {
2514 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2515 exception);
cristya19f1d72012-08-07 18:24:38 +00002516 return((double) floor((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002517 }
2518 break;
2519 }
2520 case 'G':
2521 case 'g':
2522 {
cristy9eeedea2011-11-02 19:04:05 +00002523 if (LocaleNCompare(expression,"gauss",5) == 0)
2524 {
2525 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2526 exception);
2527 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
cristya19f1d72012-08-07 18:24:38 +00002528 return((double) gamma);
cristy9eeedea2011-11-02 19:04:05 +00002529 }
cristyb0aad4c2011-11-02 19:30:35 +00002530 if (LocaleNCompare(expression,"gcd",3) == 0)
2531 {
2532 MagickOffsetType
2533 gcd;
2534
2535 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2536 exception);
cristy1707c6c2012-01-18 23:30:54 +00002537 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType) (*beta+
2538 0.5));
cristya19f1d72012-08-07 18:24:38 +00002539 return((double) gcd);
cristyb0aad4c2011-11-02 19:30:35 +00002540 }
cristy3ed852e2009-09-05 21:47:34 +00002541 if (LocaleCompare(expression,"g") == 0)
2542 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2543 break;
2544 }
2545 case 'H':
2546 case 'h':
2547 {
2548 if (LocaleCompare(expression,"h") == 0)
2549 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2550 if (LocaleCompare(expression,"hue") == 0)
2551 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2552 if (LocaleNCompare(expression,"hypot",5) == 0)
2553 {
2554 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2555 exception);
cristya19f1d72012-08-07 18:24:38 +00002556 return((double) hypot((double) alpha,(double) *beta));
cristy3ed852e2009-09-05 21:47:34 +00002557 }
2558 break;
2559 }
2560 case 'K':
2561 case 'k':
2562 {
2563 if (LocaleCompare(expression,"k") == 0)
2564 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2565 break;
2566 }
2567 case 'I':
2568 case 'i':
2569 {
2570 if (LocaleCompare(expression,"intensity") == 0)
2571 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2572 if (LocaleNCompare(expression,"int",3) == 0)
2573 {
2574 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2575 exception);
cristya19f1d72012-08-07 18:24:38 +00002576 return((double) floor(alpha));
cristy3ed852e2009-09-05 21:47:34 +00002577 }
cristy82b20722011-11-05 21:52:36 +00002578#if defined(MAGICKCORE_HAVE_ISNAN)
cristy639399c2011-11-02 19:16:15 +00002579 if (LocaleNCompare(expression,"isnan",5) == 0)
2580 {
2581 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2582 exception);
cristya19f1d72012-08-07 18:24:38 +00002583 return((double) !!isnan((double) alpha));
cristy639399c2011-11-02 19:16:15 +00002584 }
cristy82b20722011-11-05 21:52:36 +00002585#endif
cristy3ed852e2009-09-05 21:47:34 +00002586 if (LocaleCompare(expression,"i") == 0)
2587 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2588 break;
2589 }
2590 case 'J':
2591 case 'j':
2592 {
2593 if (LocaleCompare(expression,"j") == 0)
2594 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
cristy161b9262010-03-20 19:34:32 +00002595#if defined(MAGICKCORE_HAVE_J0)
cristyee56cf12010-03-01 22:17:06 +00002596 if (LocaleNCompare(expression,"j0",2) == 0)
2597 {
2598 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2599 exception);
cristya19f1d72012-08-07 18:24:38 +00002600 return((double) j0((double) alpha));
cristyee56cf12010-03-01 22:17:06 +00002601 }
cristy161b9262010-03-20 19:34:32 +00002602#endif
2603#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002604 if (LocaleNCompare(expression,"j1",2) == 0)
2605 {
2606 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2607 exception);
cristya19f1d72012-08-07 18:24:38 +00002608 return((double) j1((double) alpha));
cristyee56cf12010-03-01 22:17:06 +00002609 }
cristy161b9262010-03-20 19:34:32 +00002610#endif
cristyaa018fa2010-04-08 23:03:54 +00002611#if defined(MAGICKCORE_HAVE_J1)
cristya6a09e72010-03-02 14:51:02 +00002612 if (LocaleNCompare(expression,"jinc",4) == 0)
2613 {
2614 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2615 exception);
cristy0946a822010-03-12 17:14:58 +00002616 if (alpha == 0.0)
2617 return(1.0);
cristyc90d70d2012-11-03 23:53:13 +00002618 gamma=(double) (2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha));
cristyfce2f7b2010-03-12 00:29:49 +00002619 return(gamma);
cristya6a09e72010-03-02 14:51:02 +00002620 }
cristyaa018fa2010-04-08 23:03:54 +00002621#endif
cristy3ed852e2009-09-05 21:47:34 +00002622 break;
2623 }
2624 case 'L':
2625 case 'l':
2626 {
2627 if (LocaleNCompare(expression,"ln",2) == 0)
2628 {
2629 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2630 exception);
cristya19f1d72012-08-07 18:24:38 +00002631 return((double) log((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002632 }
cristyc8ed5322010-08-31 12:07:59 +00002633 if (LocaleNCompare(expression,"logtwo",6) == 0)
cristy3ed852e2009-09-05 21:47:34 +00002634 {
cristyc8ed5322010-08-31 12:07:59 +00002635 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
cristy3ed852e2009-09-05 21:47:34 +00002636 exception);
cristya19f1d72012-08-07 18:24:38 +00002637 return((double) log10((double) alpha))/log10(2.0);
cristy3ed852e2009-09-05 21:47:34 +00002638 }
2639 if (LocaleNCompare(expression,"log",3) == 0)
2640 {
2641 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2642 exception);
cristya19f1d72012-08-07 18:24:38 +00002643 return((double) log10((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002644 }
2645 if (LocaleCompare(expression,"lightness") == 0)
2646 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2647 break;
2648 }
2649 case 'M':
2650 case 'm':
2651 {
2652 if (LocaleCompare(expression,"MaxRGB") == 0)
cristya19f1d72012-08-07 18:24:38 +00002653 return((double) QuantumRange);
cristy3ed852e2009-09-05 21:47:34 +00002654 if (LocaleNCompare(expression,"maxima",6) == 0)
2655 break;
2656 if (LocaleNCompare(expression,"max",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002657 {
2658 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2659 exception);
2660 return(alpha > *beta ? alpha : *beta);
2661 }
cristy3ed852e2009-09-05 21:47:34 +00002662 if (LocaleNCompare(expression,"minima",6) == 0)
2663 break;
2664 if (LocaleNCompare(expression,"min",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002665 {
2666 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2667 exception);
2668 return(alpha < *beta ? alpha : *beta);
2669 }
cristy3ed852e2009-09-05 21:47:34 +00002670 if (LocaleNCompare(expression,"mod",3) == 0)
2671 {
2672 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2673 exception);
cristy984049c2011-11-03 18:34:58 +00002674 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2675 return(gamma);
cristy3ed852e2009-09-05 21:47:34 +00002676 }
2677 if (LocaleCompare(expression,"m") == 0)
2678 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2679 break;
2680 }
2681 case 'N':
2682 case 'n':
2683 {
cristyad3502e2011-11-02 19:10:45 +00002684 if (LocaleNCompare(expression,"not",3) == 0)
2685 {
2686 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2687 exception);
cristya19f1d72012-08-07 18:24:38 +00002688 return((double) (alpha < MagickEpsilon));
cristyad3502e2011-11-02 19:10:45 +00002689 }
cristy3ed852e2009-09-05 21:47:34 +00002690 if (LocaleCompare(expression,"n") == 0)
2691 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2692 break;
2693 }
2694 case 'O':
2695 case 'o':
2696 {
2697 if (LocaleCompare(expression,"Opaque") == 0)
2698 return(1.0);
2699 if (LocaleCompare(expression,"o") == 0)
2700 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2701 break;
2702 }
2703 case 'P':
2704 case 'p':
2705 {
cristy670aa3c2011-11-03 00:54:00 +00002706 if (LocaleCompare(expression,"phi") == 0)
cristya19f1d72012-08-07 18:24:38 +00002707 return((double) MagickPHI);
cristy3ed852e2009-09-05 21:47:34 +00002708 if (LocaleCompare(expression,"pi") == 0)
cristya19f1d72012-08-07 18:24:38 +00002709 return((double) MagickPI);
cristy3ed852e2009-09-05 21:47:34 +00002710 if (LocaleNCompare(expression,"pow",3) == 0)
2711 {
2712 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2713 exception);
cristya19f1d72012-08-07 18:24:38 +00002714 return((double) pow((double) alpha,(double) *beta));
cristy3ed852e2009-09-05 21:47:34 +00002715 }
2716 if (LocaleCompare(expression,"p") == 0)
2717 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2718 break;
2719 }
2720 case 'Q':
2721 case 'q':
2722 {
2723 if (LocaleCompare(expression,"QuantumRange") == 0)
cristya19f1d72012-08-07 18:24:38 +00002724 return((double) QuantumRange);
cristy3ed852e2009-09-05 21:47:34 +00002725 if (LocaleCompare(expression,"QuantumScale") == 0)
cristya19f1d72012-08-07 18:24:38 +00002726 return((double) QuantumScale);
cristy3ed852e2009-09-05 21:47:34 +00002727 break;
2728 }
2729 case 'R':
2730 case 'r':
2731 {
2732 if (LocaleNCompare(expression,"rand",4) == 0)
cristya19f1d72012-08-07 18:24:38 +00002733 return((double) GetPseudoRandomValue(fx_info->random_info));
cristy3ed852e2009-09-05 21:47:34 +00002734 if (LocaleNCompare(expression,"round",5) == 0)
2735 {
2736 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2737 exception);
cristya19f1d72012-08-07 18:24:38 +00002738 return((double) floor((double) alpha+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002739 }
2740 if (LocaleCompare(expression,"r") == 0)
2741 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2742 break;
2743 }
2744 case 'S':
2745 case 's':
2746 {
2747 if (LocaleCompare(expression,"saturation") == 0)
2748 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2749 if (LocaleNCompare(expression,"sign",4) == 0)
2750 {
2751 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2752 exception);
2753 return(alpha < 0.0 ? -1.0 : 1.0);
2754 }
cristya6a09e72010-03-02 14:51:02 +00002755 if (LocaleNCompare(expression,"sinc",4) == 0)
2756 {
2757 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2758 exception);
2759 if (alpha == 0)
2760 return(1.0);
cristya19f1d72012-08-07 18:24:38 +00002761 gamma=(double) (sin((double) (MagickPI*alpha))/
cristya6a09e72010-03-02 14:51:02 +00002762 (MagickPI*alpha));
2763 return(gamma);
2764 }
cristy3ed852e2009-09-05 21:47:34 +00002765 if (LocaleNCompare(expression,"sinh",4) == 0)
2766 {
2767 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2768 exception);
cristya19f1d72012-08-07 18:24:38 +00002769 return((double) sinh((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002770 }
2771 if (LocaleNCompare(expression,"sin",3) == 0)
2772 {
2773 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2774 exception);
cristya19f1d72012-08-07 18:24:38 +00002775 return((double) sin((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002776 }
2777 if (LocaleNCompare(expression,"sqrt",4) == 0)
2778 {
2779 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2780 exception);
cristya19f1d72012-08-07 18:24:38 +00002781 return((double) sqrt((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002782 }
cristy9eeedea2011-11-02 19:04:05 +00002783 if (LocaleNCompare(expression,"squish",6) == 0)
2784 {
2785 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2786 exception);
cristya19f1d72012-08-07 18:24:38 +00002787 return((double) (1.0/(1.0+exp((double) (-alpha)))));
cristy9eeedea2011-11-02 19:04:05 +00002788 }
cristy3ed852e2009-09-05 21:47:34 +00002789 if (LocaleCompare(expression,"s") == 0)
2790 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2791 break;
2792 }
2793 case 'T':
2794 case 't':
2795 {
2796 if (LocaleNCompare(expression,"tanh",4) == 0)
2797 {
2798 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2799 exception);
cristya19f1d72012-08-07 18:24:38 +00002800 return((double) tanh((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002801 }
2802 if (LocaleNCompare(expression,"tan",3) == 0)
2803 {
2804 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2805 exception);
cristya19f1d72012-08-07 18:24:38 +00002806 return((double) tan((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002807 }
2808 if (LocaleCompare(expression,"Transparent") == 0)
2809 return(0.0);
cristy16788e42010-08-13 13:44:26 +00002810 if (LocaleNCompare(expression,"trunc",5) == 0)
2811 {
2812 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2813 exception);
2814 if (alpha >= 0.0)
cristya19f1d72012-08-07 18:24:38 +00002815 return((double) floor((double) alpha));
2816 return((double) ceil((double) alpha));
cristy16788e42010-08-13 13:44:26 +00002817 }
cristy3ed852e2009-09-05 21:47:34 +00002818 if (LocaleCompare(expression,"t") == 0)
2819 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2820 break;
2821 }
2822 case 'U':
2823 case 'u':
2824 {
2825 if (LocaleCompare(expression,"u") == 0)
2826 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2827 break;
2828 }
2829 case 'V':
2830 case 'v':
2831 {
2832 if (LocaleCompare(expression,"v") == 0)
2833 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2834 break;
2835 }
2836 case 'W':
2837 case 'w':
2838 {
cristy9eeedea2011-11-02 19:04:05 +00002839 if (LocaleNCompare(expression,"while",5) == 0)
2840 {
2841 do
2842 {
2843 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2844 exception);
2845 } while (fabs((double) alpha) >= MagickEpsilon);
cristya19f1d72012-08-07 18:24:38 +00002846 return((double) *beta);
cristy9eeedea2011-11-02 19:04:05 +00002847 }
cristy3ed852e2009-09-05 21:47:34 +00002848 if (LocaleCompare(expression,"w") == 0)
2849 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2850 break;
2851 }
2852 case 'Y':
2853 case 'y':
2854 {
2855 if (LocaleCompare(expression,"y") == 0)
2856 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2857 break;
2858 }
2859 case 'Z':
2860 case 'z':
2861 {
2862 if (LocaleCompare(expression,"z") == 0)
2863 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2864 break;
2865 }
2866 default:
2867 break;
2868 }
2869 q=(char *) expression;
cristydbdd0e32011-11-04 23:29:40 +00002870 alpha=InterpretSiPrefixValue(expression,&q);
cristy3ed852e2009-09-05 21:47:34 +00002871 if (q == expression)
2872 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2873 return(alpha);
2874}
2875
cristy7832dc22011-09-05 01:21:53 +00002876MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
cristya19f1d72012-08-07 18:24:38 +00002877 double *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002878{
2879 MagickBooleanType
2880 status;
2881
cristy541ae572011-08-05 19:08:59 +00002882 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2883 exception);
cristy3ed852e2009-09-05 21:47:34 +00002884 return(status);
2885}
2886
2887MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
cristya19f1d72012-08-07 18:24:38 +00002888 double *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002889{
2890 FILE
2891 *file;
2892
2893 MagickBooleanType
2894 status;
2895
2896 file=fx_info->file;
2897 fx_info->file=(FILE *) NULL;
cristy541ae572011-08-05 19:08:59 +00002898 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2899 exception);
cristy3ed852e2009-09-05 21:47:34 +00002900 fx_info->file=file;
2901 return(status);
2902}
2903
cristy7832dc22011-09-05 01:21:53 +00002904MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002905 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristya19f1d72012-08-07 18:24:38 +00002906 double *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002907{
cristya19f1d72012-08-07 18:24:38 +00002908 double
cristy3ed852e2009-09-05 21:47:34 +00002909 beta;
2910
2911 beta=0.0;
2912 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2913 exception);
2914 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2915}
2916
2917/*
2918%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2919% %
2920% %
2921% %
2922% F x I m a g e %
2923% %
2924% %
2925% %
2926%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2927%
2928% FxImage() applies a mathematical expression to the specified image.
2929%
2930% The format of the FxImage method is:
2931%
2932% Image *FxImage(const Image *image,const char *expression,
2933% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002934%
2935% A description of each parameter follows:
2936%
2937% o image: the image.
2938%
cristy3ed852e2009-09-05 21:47:34 +00002939% o expression: A mathematical expression.
2940%
2941% o exception: return any errors or warnings in this structure.
2942%
2943*/
2944
2945static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2946{
cristybb503372010-05-27 20:51:26 +00002947 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002948 i;
2949
2950 assert(fx_info != (FxInfo **) NULL);
cristyac245f82012-05-05 17:13:57 +00002951 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
cristy3ed852e2009-09-05 21:47:34 +00002952 if (fx_info[i] != (FxInfo *) NULL)
2953 fx_info[i]=DestroyFxInfo(fx_info[i]);
cristyb41ee102010-10-04 16:46:15 +00002954 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
cristy3ed852e2009-09-05 21:47:34 +00002955 return(fx_info);
2956}
2957
2958static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2959 ExceptionInfo *exception)
2960{
2961 char
2962 *fx_expression;
2963
2964 FxInfo
2965 **fx_info;
2966
cristya19f1d72012-08-07 18:24:38 +00002967 double
cristy3ed852e2009-09-05 21:47:34 +00002968 alpha;
2969
cristybb503372010-05-27 20:51:26 +00002970 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002971 i;
2972
cristybb503372010-05-27 20:51:26 +00002973 size_t
cristy3ed852e2009-09-05 21:47:34 +00002974 number_threads;
2975
cristy9357bdd2012-07-30 12:28:34 +00002976 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
cristyb41ee102010-10-04 16:46:15 +00002977 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +00002978 if (fx_info == (FxInfo **) NULL)
cristy65d161b2012-10-07 20:39:52 +00002979 {
2980 (void) ThrowMagickException(exception,GetMagickModule(),
2981 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
2982 return((FxInfo **) NULL);
2983 }
cristy3ed852e2009-09-05 21:47:34 +00002984 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2985 if (*expression != '@')
2986 fx_expression=ConstantString(expression);
2987 else
2988 fx_expression=FileToString(expression+1,~0,exception);
cristybb503372010-05-27 20:51:26 +00002989 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +00002990 {
cristy65d161b2012-10-07 20:39:52 +00002991 MagickBooleanType
2992 status;
2993
cristydb070952012-04-20 14:33:00 +00002994 fx_info[i]=AcquireFxInfo(image,fx_expression,exception);
cristy3ed852e2009-09-05 21:47:34 +00002995 if (fx_info[i] == (FxInfo *) NULL)
cristy65d161b2012-10-07 20:39:52 +00002996 break;
2997 status=FxPreprocessExpression(fx_info[i],&alpha,exception);
2998 if (status == MagickFalse)
2999 break;
cristy3ed852e2009-09-05 21:47:34 +00003000 }
3001 fx_expression=DestroyString(fx_expression);
cristy65d161b2012-10-07 20:39:52 +00003002 if (i < (ssize_t) number_threads)
3003 fx_info=DestroyFxThreadSet(fx_info);
cristy3ed852e2009-09-05 21:47:34 +00003004 return(fx_info);
3005}
3006
3007MagickExport Image *FxImage(const Image *image,const char *expression,
3008 ExceptionInfo *exception)
3009{
cristy3ed852e2009-09-05 21:47:34 +00003010#define FxImageTag "Fx/Image"
3011
cristyfa112112010-01-04 17:48:07 +00003012 CacheView
cristy79cedc72011-07-25 00:41:15 +00003013 *fx_view,
3014 *image_view;
cristyfa112112010-01-04 17:48:07 +00003015
cristy3ed852e2009-09-05 21:47:34 +00003016 FxInfo
cristyfa112112010-01-04 17:48:07 +00003017 **restrict fx_info;
cristy3ed852e2009-09-05 21:47:34 +00003018
3019 Image
3020 *fx_image;
3021
cristy3ed852e2009-09-05 21:47:34 +00003022 MagickBooleanType
3023 status;
3024
cristybb503372010-05-27 20:51:26 +00003025 MagickOffsetType
3026 progress;
3027
cristybb503372010-05-27 20:51:26 +00003028 ssize_t
3029 y;
3030
cristy3ed852e2009-09-05 21:47:34 +00003031 assert(image != (Image *) NULL);
3032 assert(image->signature == MagickSignature);
3033 if (image->debug != MagickFalse)
3034 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00003035 fx_info=AcquireFxThreadSet(image,expression,exception);
3036 if (fx_info == (FxInfo **) NULL)
cristy65d161b2012-10-07 20:39:52 +00003037 return((Image *) NULL);
3038 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3039 if (fx_image == (Image *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003040 {
cristy3ed852e2009-09-05 21:47:34 +00003041 fx_info=DestroyFxThreadSet(fx_info);
3042 return((Image *) NULL);
3043 }
cristy65d161b2012-10-07 20:39:52 +00003044 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
3045 {
3046 fx_info=DestroyFxThreadSet(fx_info);
3047 fx_image=DestroyImage(fx_image);
3048 return((Image *) NULL);
3049 }
cristy3ed852e2009-09-05 21:47:34 +00003050 /*
3051 Fx image.
3052 */
3053 status=MagickTrue;
3054 progress=0;
cristy46ff2672012-12-14 15:32:26 +00003055 image_view=AcquireVirtualCacheView(image,exception);
3056 fx_view=AcquireAuthenticCacheView(fx_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003057#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003058 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00003059 magick_threads(image,fx_image,fx_image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003060#endif
cristybb503372010-05-27 20:51:26 +00003061 for (y=0; y < (ssize_t) fx_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003062 {
cristy5c9e6f22010-09-17 17:31:01 +00003063 const int
3064 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003065
cristy79cedc72011-07-25 00:41:15 +00003066 register const Quantum
3067 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003068
cristy4c08aed2011-07-01 19:47:50 +00003069 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003070 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003071
cristy79cedc72011-07-25 00:41:15 +00003072 register ssize_t
3073 x;
3074
cristy3ed852e2009-09-05 21:47:34 +00003075 if (status == MagickFalse)
3076 continue;
cristy79cedc72011-07-25 00:41:15 +00003077 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003078 q=QueueCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
cristy79cedc72011-07-25 00:41:15 +00003079 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003080 {
3081 status=MagickFalse;
3082 continue;
3083 }
cristybb503372010-05-27 20:51:26 +00003084 for (x=0; x < (ssize_t) fx_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003085 {
cristy79cedc72011-07-25 00:41:15 +00003086 register ssize_t
3087 i;
3088
3089 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3090 {
cristya19f1d72012-08-07 18:24:38 +00003091 double
cristy79cedc72011-07-25 00:41:15 +00003092 alpha;
3093
cristy5a23c552013-02-13 14:34:28 +00003094 PixelChannel channel=GetPixelChannelChannel(image,i);
3095 PixelTrait traits=GetPixelChannelTraits(image,channel);
3096 PixelTrait fx_traits=GetPixelChannelTraits(fx_image,channel);
cristy010d7d12011-08-31 01:02:48 +00003097 if ((traits == UndefinedPixelTrait) ||
3098 (fx_traits == UndefinedPixelTrait))
cristy79cedc72011-07-25 00:41:15 +00003099 continue;
cristy1eced092012-08-10 23:10:56 +00003100 if (((fx_traits & CopyPixelTrait) != 0) ||
3101 (GetPixelMask(image,p) != 0))
cristy79cedc72011-07-25 00:41:15 +00003102 {
cristy0beccfa2011-09-25 20:47:53 +00003103 SetPixelChannel(fx_image,channel,p[i],q);
cristy79cedc72011-07-25 00:41:15 +00003104 continue;
3105 }
3106 alpha=0.0;
cristya382aca2011-12-06 18:22:48 +00003107 (void) FxEvaluateChannelExpression(fx_info[id],channel,x,y,&alpha,
3108 exception);
cristy8cd03c32012-07-07 18:57:59 +00003109 q[i]=ClampToQuantum(QuantumRange*alpha);
cristy79cedc72011-07-25 00:41:15 +00003110 }
3111 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003112 q+=GetPixelChannels(fx_image);
cristy3ed852e2009-09-05 21:47:34 +00003113 }
3114 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3115 status=MagickFalse;
3116 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3117 {
3118 MagickBooleanType
3119 proceed;
3120
cristyb5d5f722009-11-04 03:03:49 +00003121#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003122 #pragma omp critical (MagickCore_FxImage)
cristy3ed852e2009-09-05 21:47:34 +00003123#endif
3124 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3125 if (proceed == MagickFalse)
3126 status=MagickFalse;
3127 }
3128 }
cristy3ed852e2009-09-05 21:47:34 +00003129 fx_view=DestroyCacheView(fx_view);
cristy79cedc72011-07-25 00:41:15 +00003130 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003131 fx_info=DestroyFxThreadSet(fx_info);
3132 if (status == MagickFalse)
3133 fx_image=DestroyImage(fx_image);
3134 return(fx_image);
3135}
3136
3137/*
3138%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3139% %
3140% %
3141% %
3142% I m p l o d e I m a g e %
3143% %
3144% %
3145% %
3146%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3147%
3148% ImplodeImage() creates a new image that is a copy of an existing
3149% one with the image pixels "implode" by the specified percentage. It
3150% allocates the memory necessary for the new Image structure and returns a
3151% pointer to the new image.
3152%
3153% The format of the ImplodeImage method is:
3154%
3155% Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003156% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003157%
3158% A description of each parameter follows:
3159%
3160% o implode_image: Method ImplodeImage returns a pointer to the image
3161% after it is implode. A null image is returned if there is a memory
3162% shortage.
3163%
3164% o image: the image.
3165%
3166% o amount: Define the extent of the implosion.
3167%
cristy76f512e2011-09-12 01:26:56 +00003168% o method: the pixel interpolation method.
3169%
cristy3ed852e2009-09-05 21:47:34 +00003170% o exception: return any errors or warnings in this structure.
3171%
3172*/
3173MagickExport Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003174 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003175{
3176#define ImplodeImageTag "Implode/Image"
3177
cristyfa112112010-01-04 17:48:07 +00003178 CacheView
3179 *image_view,
3180 *implode_view;
3181
cristy3ed852e2009-09-05 21:47:34 +00003182 Image
3183 *implode_image;
3184
cristy3ed852e2009-09-05 21:47:34 +00003185 MagickBooleanType
3186 status;
3187
cristybb503372010-05-27 20:51:26 +00003188 MagickOffsetType
3189 progress;
3190
cristya19f1d72012-08-07 18:24:38 +00003191 double
cristy3ed852e2009-09-05 21:47:34 +00003192 radius;
3193
3194 PointInfo
3195 center,
3196 scale;
3197
cristybb503372010-05-27 20:51:26 +00003198 ssize_t
3199 y;
3200
cristy3ed852e2009-09-05 21:47:34 +00003201 /*
3202 Initialize implode image attributes.
3203 */
3204 assert(image != (Image *) NULL);
3205 assert(image->signature == MagickSignature);
3206 if (image->debug != MagickFalse)
3207 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3208 assert(exception != (ExceptionInfo *) NULL);
3209 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00003210 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3211 exception);
cristy3ed852e2009-09-05 21:47:34 +00003212 if (implode_image == (Image *) NULL)
3213 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003214 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003215 {
cristy3ed852e2009-09-05 21:47:34 +00003216 implode_image=DestroyImage(implode_image);
3217 return((Image *) NULL);
3218 }
cristy4c08aed2011-07-01 19:47:50 +00003219 if (implode_image->background_color.alpha != OpaqueAlpha)
cristy8a46d822012-08-28 23:32:39 +00003220 implode_image->alpha_trait=BlendPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +00003221 /*
3222 Compute scaling factor.
3223 */
3224 scale.x=1.0;
3225 scale.y=1.0;
3226 center.x=0.5*image->columns;
3227 center.y=0.5*image->rows;
3228 radius=center.x;
3229 if (image->columns > image->rows)
3230 scale.y=(double) image->columns/(double) image->rows;
3231 else
3232 if (image->columns < image->rows)
3233 {
3234 scale.x=(double) image->rows/(double) image->columns;
3235 radius=center.y;
3236 }
3237 /*
3238 Implode image.
3239 */
3240 status=MagickTrue;
3241 progress=0;
cristy46ff2672012-12-14 15:32:26 +00003242 image_view=AcquireVirtualCacheView(image,exception);
3243 implode_view=AcquireAuthenticCacheView(implode_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003244#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003245 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00003246 magick_threads(image,implode_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003247#endif
cristybb503372010-05-27 20:51:26 +00003248 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003249 {
cristya19f1d72012-08-07 18:24:38 +00003250 double
cristy3ed852e2009-09-05 21:47:34 +00003251 distance;
3252
3253 PointInfo
3254 delta;
3255
cristy6d188022011-09-12 13:23:33 +00003256 register const Quantum
3257 *restrict p;
3258
cristybb503372010-05-27 20:51:26 +00003259 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003260 x;
3261
cristy4c08aed2011-07-01 19:47:50 +00003262 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003263 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003264
3265 if (status == MagickFalse)
3266 continue;
cristy6d188022011-09-12 13:23:33 +00003267 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003268 q=QueueCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003269 exception);
cristy6d188022011-09-12 13:23:33 +00003270 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003271 {
3272 status=MagickFalse;
3273 continue;
3274 }
cristy3ed852e2009-09-05 21:47:34 +00003275 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00003276 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003277 {
cristy6d188022011-09-12 13:23:33 +00003278 register ssize_t
3279 i;
3280
cristy3ed852e2009-09-05 21:47:34 +00003281 /*
3282 Determine if the pixel is within an ellipse.
3283 */
cristy10a6c612012-01-29 21:41:05 +00003284 if (GetPixelMask(image,p) != 0)
3285 {
3286 p+=GetPixelChannels(image);
3287 q+=GetPixelChannels(implode_image);
3288 continue;
3289 }
cristy3ed852e2009-09-05 21:47:34 +00003290 delta.x=scale.x*(double) (x-center.x);
3291 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00003292 if (distance >= (radius*radius))
cristya6d7a9b2012-01-18 20:04:48 +00003293 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy6d188022011-09-12 13:23:33 +00003294 {
cristy5a23c552013-02-13 14:34:28 +00003295 PixelChannel channel=GetPixelChannelChannel(image,i);
3296 PixelTrait traits=GetPixelChannelTraits(image,channel);
3297 PixelTrait implode_traits=GetPixelChannelTraits(implode_image,
3298 channel);
cristy1707c6c2012-01-18 23:30:54 +00003299 if ((traits == UndefinedPixelTrait) ||
3300 (implode_traits == UndefinedPixelTrait))
3301 continue;
cristya6d7a9b2012-01-18 20:04:48 +00003302 SetPixelChannel(implode_image,channel,p[i],q);
cristy6d188022011-09-12 13:23:33 +00003303 }
3304 else
cristy3ed852e2009-09-05 21:47:34 +00003305 {
3306 double
3307 factor;
3308
3309 /*
3310 Implode the pixel.
3311 */
3312 factor=1.0;
3313 if (distance > 0.0)
cristy1707c6c2012-01-18 23:30:54 +00003314 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/radius/
3315 2)),-amount);
cristy76f512e2011-09-12 01:26:56 +00003316 status=InterpolatePixelChannels(image,image_view,implode_image,method,
3317 (double) (factor*delta.x/scale.x+center.x),(double) (factor*delta.y/
3318 scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00003319 }
cristy6d188022011-09-12 13:23:33 +00003320 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003321 q+=GetPixelChannels(implode_image);
cristy3ed852e2009-09-05 21:47:34 +00003322 }
3323 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3324 status=MagickFalse;
3325 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3326 {
3327 MagickBooleanType
3328 proceed;
3329
cristyb5d5f722009-11-04 03:03:49 +00003330#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003331 #pragma omp critical (MagickCore_ImplodeImage)
cristy3ed852e2009-09-05 21:47:34 +00003332#endif
3333 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3334 if (proceed == MagickFalse)
3335 status=MagickFalse;
3336 }
3337 }
3338 implode_view=DestroyCacheView(implode_view);
3339 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003340 if (status == MagickFalse)
3341 implode_image=DestroyImage(implode_image);
3342 return(implode_image);
3343}
3344
3345/*
3346%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3347% %
3348% %
3349% %
3350% M o r p h I m a g e s %
3351% %
3352% %
3353% %
3354%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3355%
3356% The MorphImages() method requires a minimum of two images. The first
3357% image is transformed into the second by a number of intervening images
3358% as specified by frames.
3359%
3360% The format of the MorphImage method is:
3361%
cristybb503372010-05-27 20:51:26 +00003362% Image *MorphImages(const Image *image,const size_t number_frames,
cristy3ed852e2009-09-05 21:47:34 +00003363% ExceptionInfo *exception)
3364%
3365% A description of each parameter follows:
3366%
3367% o image: the image.
3368%
3369% o number_frames: Define the number of in-between image to generate.
3370% The more in-between frames, the smoother the morph.
3371%
3372% o exception: return any errors or warnings in this structure.
3373%
3374*/
cristy7de7f742012-12-14 00:28:27 +00003375MagickExport Image *MorphImages(const Image *image,const size_t number_frames,
3376 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003377{
3378#define MorphImageTag "Morph/Image"
3379
cristyfab83642012-12-14 00:31:38 +00003380 double
3381 alpha,
3382 beta;
3383
cristy3ed852e2009-09-05 21:47:34 +00003384 Image
3385 *morph_image,
3386 *morph_images;
3387
cristy9d314ff2011-03-09 01:30:28 +00003388 MagickBooleanType
3389 status;
cristy3ed852e2009-09-05 21:47:34 +00003390
3391 MagickOffsetType
3392 scene;
3393
cristy3ed852e2009-09-05 21:47:34 +00003394 register const Image
3395 *next;
3396
cristybb503372010-05-27 20:51:26 +00003397 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003398 i;
3399
cristy9d314ff2011-03-09 01:30:28 +00003400 ssize_t
3401 y;
cristy3ed852e2009-09-05 21:47:34 +00003402
3403 /*
3404 Clone first frame in sequence.
3405 */
3406 assert(image != (Image *) NULL);
3407 assert(image->signature == MagickSignature);
3408 if (image->debug != MagickFalse)
3409 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3410 assert(exception != (ExceptionInfo *) NULL);
3411 assert(exception->signature == MagickSignature);
3412 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3413 if (morph_images == (Image *) NULL)
3414 return((Image *) NULL);
3415 if (GetNextImageInList(image) == (Image *) NULL)
3416 {
3417 /*
3418 Morph single image.
3419 */
cristybb503372010-05-27 20:51:26 +00003420 for (i=1; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003421 {
3422 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3423 if (morph_image == (Image *) NULL)
3424 {
3425 morph_images=DestroyImageList(morph_images);
3426 return((Image *) NULL);
3427 }
3428 AppendImageToList(&morph_images,morph_image);
cristy8b27a6d2010-02-14 03:31:15 +00003429 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003430 {
cristy8b27a6d2010-02-14 03:31:15 +00003431 MagickBooleanType
3432 proceed;
3433
cristybb503372010-05-27 20:51:26 +00003434 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3435 number_frames);
cristy8b27a6d2010-02-14 03:31:15 +00003436 if (proceed == MagickFalse)
3437 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00003438 }
3439 }
3440 return(GetFirstImageInList(morph_images));
3441 }
3442 /*
3443 Morph image sequence.
3444 */
3445 status=MagickTrue;
3446 scene=0;
3447 next=image;
3448 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3449 {
cristybb503372010-05-27 20:51:26 +00003450 for (i=0; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003451 {
3452 CacheView
3453 *image_view,
3454 *morph_view;
3455
cristya19f1d72012-08-07 18:24:38 +00003456 beta=(double) (i+1.0)/(double) (number_frames+1.0);
cristy3ed852e2009-09-05 21:47:34 +00003457 alpha=1.0-beta;
cristy15b98cd2010-09-12 19:42:50 +00003458 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
cristyaa2c16c2012-03-25 22:21:35 +00003459 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*next->rows+beta*
3460 GetNextImageInList(next)->rows+0.5),next->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +00003461 if (morph_image == (Image *) NULL)
3462 {
3463 morph_images=DestroyImageList(morph_images);
3464 return((Image *) NULL);
3465 }
cristy1707c6c2012-01-18 23:30:54 +00003466 status=SetImageStorageClass(morph_image,DirectClass,exception);
3467 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003468 {
cristy3ed852e2009-09-05 21:47:34 +00003469 morph_image=DestroyImage(morph_image);
3470 return((Image *) NULL);
3471 }
3472 AppendImageToList(&morph_images,morph_image);
3473 morph_images=GetLastImageInList(morph_images);
cristy15b98cd2010-09-12 19:42:50 +00003474 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
cristyaa2c16c2012-03-25 22:21:35 +00003475 morph_images->rows,GetNextImageInList(next)->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +00003476 if (morph_image == (Image *) NULL)
3477 {
3478 morph_images=DestroyImageList(morph_images);
3479 return((Image *) NULL);
3480 }
cristy46ff2672012-12-14 15:32:26 +00003481 image_view=AcquireVirtualCacheView(morph_image,exception);
3482 morph_view=AcquireAuthenticCacheView(morph_images,exception);
cristyb5d5f722009-11-04 03:03:49 +00003483#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003484 #pragma omp parallel for schedule(static,4) shared(status) \
cristy5e6b2592012-12-19 14:08:11 +00003485 magick_threads(morph_image,morph_image,morph_image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003486#endif
cristybb503372010-05-27 20:51:26 +00003487 for (y=0; y < (ssize_t) morph_images->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003488 {
3489 MagickBooleanType
3490 sync;
3491
cristy4c08aed2011-07-01 19:47:50 +00003492 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003493 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003494
cristybb503372010-05-27 20:51:26 +00003495 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003496 x;
3497
cristy4c08aed2011-07-01 19:47:50 +00003498 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003499 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003500
3501 if (status == MagickFalse)
3502 continue;
3503 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3504 exception);
3505 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3506 exception);
cristy4c08aed2011-07-01 19:47:50 +00003507 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003508 {
3509 status=MagickFalse;
3510 continue;
3511 }
cristybb503372010-05-27 20:51:26 +00003512 for (x=0; x < (ssize_t) morph_images->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003513 {
cristy1707c6c2012-01-18 23:30:54 +00003514 register ssize_t
3515 i;
3516
cristy10a6c612012-01-29 21:41:05 +00003517 for (i=0; i < (ssize_t) GetPixelChannels(morph_image); i++)
cristy1707c6c2012-01-18 23:30:54 +00003518 {
cristy5a23c552013-02-13 14:34:28 +00003519 PixelChannel channel=GetPixelChannelChannel(image,i);
3520 PixelTrait traits=GetPixelChannelTraits(image,channel);
3521 PixelTrait morph_traits=GetPixelChannelTraits(morph_image,channel);
cristy1707c6c2012-01-18 23:30:54 +00003522 if ((traits == UndefinedPixelTrait) ||
3523 (morph_traits == UndefinedPixelTrait))
3524 continue;
cristy1eced092012-08-10 23:10:56 +00003525 if (((morph_traits & CopyPixelTrait) != 0) ||
3526 (GetPixelMask(image,p) != 0))
cristy1707c6c2012-01-18 23:30:54 +00003527 {
3528 SetPixelChannel(morph_image,channel,p[i],q);
3529 continue;
3530 }
3531 SetPixelChannel(morph_image,channel,ClampToQuantum(alpha*
3532 GetPixelChannel(morph_images,channel,q)+beta*p[i]),q);
3533 }
cristyed231572011-07-14 02:18:59 +00003534 p+=GetPixelChannels(morph_image);
3535 q+=GetPixelChannels(morph_images);
cristy3ed852e2009-09-05 21:47:34 +00003536 }
3537 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3538 if (sync == MagickFalse)
3539 status=MagickFalse;
3540 }
3541 morph_view=DestroyCacheView(morph_view);
3542 image_view=DestroyCacheView(image_view);
3543 morph_image=DestroyImage(morph_image);
3544 }
cristybb503372010-05-27 20:51:26 +00003545 if (i < (ssize_t) number_frames)
cristy3ed852e2009-09-05 21:47:34 +00003546 break;
3547 /*
3548 Clone last frame in sequence.
3549 */
3550 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3551 if (morph_image == (Image *) NULL)
3552 {
3553 morph_images=DestroyImageList(morph_images);
3554 return((Image *) NULL);
3555 }
3556 AppendImageToList(&morph_images,morph_image);
3557 morph_images=GetLastImageInList(morph_images);
3558 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3559 {
3560 MagickBooleanType
3561 proceed;
3562
cristyb5d5f722009-11-04 03:03:49 +00003563#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003564 #pragma omp critical (MagickCore_MorphImages)
cristy3ed852e2009-09-05 21:47:34 +00003565#endif
3566 proceed=SetImageProgress(image,MorphImageTag,scene,
3567 GetImageListLength(image));
3568 if (proceed == MagickFalse)
3569 status=MagickFalse;
3570 }
3571 scene++;
3572 }
3573 if (GetNextImageInList(next) != (Image *) NULL)
3574 {
3575 morph_images=DestroyImageList(morph_images);
3576 return((Image *) NULL);
3577 }
3578 return(GetFirstImageInList(morph_images));
3579}
3580
3581/*
3582%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3583% %
3584% %
3585% %
3586% P l a s m a I m a g e %
3587% %
3588% %
3589% %
3590%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3591%
3592% PlasmaImage() initializes an image with plasma fractal values. The image
3593% must be initialized with a base color and the random number generator
3594% seeded before this method is called.
3595%
3596% The format of the PlasmaImage method is:
3597%
3598% MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
cristy5cbc0162011-08-29 00:36:28 +00003599% size_t attenuate,size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003600%
3601% A description of each parameter follows:
3602%
3603% o image: the image.
3604%
3605% o segment: Define the region to apply plasma fractals values.
3606%
glennrp7dae1ca2010-09-16 12:17:35 +00003607% o attenuate: Define the plasma attenuation factor.
cristy3ed852e2009-09-05 21:47:34 +00003608%
3609% o depth: Limit the plasma recursion depth.
3610%
cristy5cbc0162011-08-29 00:36:28 +00003611% o exception: return any errors or warnings in this structure.
3612%
cristy3ed852e2009-09-05 21:47:34 +00003613*/
3614
3615static inline Quantum PlasmaPixel(RandomInfo *random_info,
cristya19f1d72012-08-07 18:24:38 +00003616 const double pixel,const double noise)
cristy3ed852e2009-09-05 21:47:34 +00003617{
3618 Quantum
3619 plasma;
3620
cristyce70c172010-01-07 17:15:30 +00003621 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
cristy3ed852e2009-09-05 21:47:34 +00003622 noise/2.0);
3623 return(plasma);
3624}
3625
cristyda1f9c12011-10-02 21:39:49 +00003626static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3627 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3628 const SegmentInfo *segment,size_t attenuate,size_t depth,
3629 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003630{
cristya19f1d72012-08-07 18:24:38 +00003631 double
cristy3ed852e2009-09-05 21:47:34 +00003632 plasma;
3633
cristyda1f9c12011-10-02 21:39:49 +00003634 register const Quantum
3635 *restrict u,
3636 *restrict v;
3637
3638 register Quantum
3639 *restrict q;
3640
3641 register ssize_t
3642 i;
cristy3ed852e2009-09-05 21:47:34 +00003643
cristy9d314ff2011-03-09 01:30:28 +00003644 ssize_t
3645 x,
3646 x_mid,
3647 y,
3648 y_mid;
3649
cristy3ed852e2009-09-05 21:47:34 +00003650 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3651 return(MagickTrue);
3652 if (depth != 0)
3653 {
3654 SegmentInfo
3655 local_info;
3656
3657 /*
3658 Divide the area into quadrants and recurse.
3659 */
3660 depth--;
3661 attenuate++;
cristybb503372010-05-27 20:51:26 +00003662 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3663 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003664 local_info=(*segment);
3665 local_info.x2=(double) x_mid;
3666 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003667 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3668 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003669 local_info=(*segment);
3670 local_info.y1=(double) y_mid;
3671 local_info.x2=(double) x_mid;
cristyda1f9c12011-10-02 21:39:49 +00003672 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3673 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003674 local_info=(*segment);
3675 local_info.x1=(double) x_mid;
3676 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003677 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3678 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003679 local_info=(*segment);
3680 local_info.x1=(double) x_mid;
3681 local_info.y1=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003682 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3683 &local_info,attenuate,depth,exception));
cristy3ed852e2009-09-05 21:47:34 +00003684 }
cristybb503372010-05-27 20:51:26 +00003685 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3686 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003687 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3688 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3689 return(MagickFalse);
3690 /*
3691 Average pixels and apply plasma.
3692 */
cristya19f1d72012-08-07 18:24:38 +00003693 plasma=(double) QuantumRange/(2.0*attenuate);
cristy3ed852e2009-09-05 21:47:34 +00003694 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3695 {
cristy3ed852e2009-09-05 21:47:34 +00003696 /*
3697 Left pixel.
3698 */
cristybb503372010-05-27 20:51:26 +00003699 x=(ssize_t) ceil(segment->x1-0.5);
cristy1707c6c2012-01-18 23:30:54 +00003700 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),1,1,
3701 exception);
3702 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),1,1,
3703 exception);
cristyc5c6f662010-09-22 14:23:02 +00003704 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003705 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3706 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003707 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003708 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3709 {
cristy5a23c552013-02-13 14:34:28 +00003710 PixelChannel channel=GetPixelChannelChannel(image,i);
3711 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003712 if (traits == UndefinedPixelTrait)
3713 continue;
3714 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3715 }
cristyc5c6f662010-09-22 14:23:02 +00003716 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003717 if (segment->x1 != segment->x2)
3718 {
3719 /*
3720 Right pixel.
3721 */
cristybb503372010-05-27 20:51:26 +00003722 x=(ssize_t) ceil(segment->x2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003723 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3724 1,1,exception);
3725 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3726 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003727 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003728 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3729 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003730 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003731 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3732 {
cristy5a23c552013-02-13 14:34:28 +00003733 PixelChannel channel=GetPixelChannelChannel(image,i);
3734 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003735 if (traits == UndefinedPixelTrait)
3736 continue;
3737 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3738 }
cristyc5c6f662010-09-22 14:23:02 +00003739 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003740 }
3741 }
3742 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3743 {
3744 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3745 {
cristy3ed852e2009-09-05 21:47:34 +00003746 /*
3747 Bottom pixel.
3748 */
cristybb503372010-05-27 20:51:26 +00003749 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003750 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3751 1,1,exception);
3752 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3753 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003754 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003755 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3756 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003757 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003758 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3759 {
cristy5a23c552013-02-13 14:34:28 +00003760 PixelChannel channel=GetPixelChannelChannel(image,i);
3761 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003762 if (traits == UndefinedPixelTrait)
3763 continue;
3764 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3765 }
cristyc5c6f662010-09-22 14:23:02 +00003766 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003767 }
3768 if (segment->y1 != segment->y2)
3769 {
cristy3ed852e2009-09-05 21:47:34 +00003770 /*
3771 Top pixel.
3772 */
cristybb503372010-05-27 20:51:26 +00003773 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003774 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3775 1,1,exception);
3776 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3777 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003778 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003779 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3780 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003781 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003782 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3783 {
cristy5a23c552013-02-13 14:34:28 +00003784 PixelChannel channel=GetPixelChannelChannel(image,i);
3785 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003786 if (traits == UndefinedPixelTrait)
3787 continue;
3788 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3789 }
cristyc5c6f662010-09-22 14:23:02 +00003790 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003791 }
3792 }
3793 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3794 {
cristy3ed852e2009-09-05 21:47:34 +00003795 /*
3796 Middle pixel.
3797 */
cristybb503372010-05-27 20:51:26 +00003798 x=(ssize_t) ceil(segment->x1-0.5);
3799 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003800 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
cristybb503372010-05-27 20:51:26 +00003801 x=(ssize_t) ceil(segment->x2-0.5);
3802 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003803 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003804 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003805 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3806 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003807 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003808 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3809 {
cristy5a23c552013-02-13 14:34:28 +00003810 PixelChannel channel=GetPixelChannelChannel(image,i);
3811 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003812 if (traits == UndefinedPixelTrait)
3813 continue;
3814 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3815 }
cristyc5c6f662010-09-22 14:23:02 +00003816 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003817 }
3818 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3819 return(MagickTrue);
3820 return(MagickFalse);
3821}
cristyda1f9c12011-10-02 21:39:49 +00003822
cristy3ed852e2009-09-05 21:47:34 +00003823MagickExport MagickBooleanType PlasmaImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00003824 const SegmentInfo *segment,size_t attenuate,size_t depth,
3825 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003826{
cristyc5c6f662010-09-22 14:23:02 +00003827 CacheView
cristyda1f9c12011-10-02 21:39:49 +00003828 *image_view,
3829 *u_view,
3830 *v_view;
cristyc5c6f662010-09-22 14:23:02 +00003831
cristy3ed852e2009-09-05 21:47:34 +00003832 MagickBooleanType
3833 status;
3834
3835 RandomInfo
3836 *random_info;
3837
3838 if (image->debug != MagickFalse)
3839 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3840 assert(image != (Image *) NULL);
3841 assert(image->signature == MagickSignature);
3842 if (image->debug != MagickFalse)
3843 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy5cbc0162011-08-29 00:36:28 +00003844 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristyc5c6f662010-09-22 14:23:02 +00003845 return(MagickFalse);
cristy46ff2672012-12-14 15:32:26 +00003846 image_view=AcquireAuthenticCacheView(image,exception);
3847 u_view=AcquireVirtualCacheView(image,exception);
3848 v_view=AcquireVirtualCacheView(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003849 random_info=AcquireRandomInfo();
cristyda1f9c12011-10-02 21:39:49 +00003850 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3851 attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003852 random_info=DestroyRandomInfo(random_info);
cristyda1f9c12011-10-02 21:39:49 +00003853 v_view=DestroyCacheView(v_view);
3854 u_view=DestroyCacheView(u_view);
cristyc5c6f662010-09-22 14:23:02 +00003855 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003856 return(status);
3857}
3858
3859/*
3860%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3861% %
3862% %
3863% %
3864% P o l a r o i d I m a g e %
3865% %
3866% %
3867% %
3868%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3869%
3870% PolaroidImage() simulates a Polaroid picture.
3871%
3872% The format of the AnnotateImage method is:
3873%
3874% Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003875% const char *caption,const double angle,
3876% const PixelInterpolateMethod method,ExceptionInfo exception)
cristy3ed852e2009-09-05 21:47:34 +00003877%
3878% A description of each parameter follows:
3879%
3880% o image: the image.
3881%
3882% o draw_info: the draw info.
3883%
cristye9e3d382011-12-14 01:50:13 +00003884% o caption: the Polaroid caption.
3885%
cristycee97112010-05-28 00:44:52 +00003886% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00003887%
cristy5c4e2582011-09-11 19:21:03 +00003888% o method: the pixel interpolation method.
3889%
cristy3ed852e2009-09-05 21:47:34 +00003890% o exception: return any errors or warnings in this structure.
3891%
3892*/
3893MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003894 const char *caption,const double angle,const PixelInterpolateMethod method,
cristy5c4e2582011-09-11 19:21:03 +00003895 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003896{
cristy3ed852e2009-09-05 21:47:34 +00003897 Image
3898 *bend_image,
3899 *caption_image,
3900 *flop_image,
3901 *picture_image,
3902 *polaroid_image,
3903 *rotate_image,
3904 *trim_image;
3905
cristybb503372010-05-27 20:51:26 +00003906 size_t
cristy3ed852e2009-09-05 21:47:34 +00003907 height;
3908
cristy9d314ff2011-03-09 01:30:28 +00003909 ssize_t
3910 quantum;
3911
cristy3ed852e2009-09-05 21:47:34 +00003912 /*
3913 Simulate a Polaroid picture.
3914 */
3915 assert(image != (Image *) NULL);
3916 assert(image->signature == MagickSignature);
3917 if (image->debug != MagickFalse)
3918 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3919 assert(exception != (ExceptionInfo *) NULL);
3920 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00003921 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
cristy3ed852e2009-09-05 21:47:34 +00003922 image->rows)/25.0,10.0);
3923 height=image->rows+2*quantum;
3924 caption_image=(Image *) NULL;
cristye9e3d382011-12-14 01:50:13 +00003925 if (caption != (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003926 {
3927 char
cristye9e3d382011-12-14 01:50:13 +00003928 geometry[MaxTextExtent],
3929 *text;
cristy3ed852e2009-09-05 21:47:34 +00003930
3931 DrawInfo
3932 *annotate_info;
3933
cristy3ed852e2009-09-05 21:47:34 +00003934 MagickBooleanType
3935 status;
3936
cristy9d314ff2011-03-09 01:30:28 +00003937 ssize_t
3938 count;
3939
cristy3ed852e2009-09-05 21:47:34 +00003940 TypeMetric
3941 metrics;
3942
3943 /*
3944 Generate caption image.
3945 */
3946 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3947 if (caption_image == (Image *) NULL)
3948 return((Image *) NULL);
3949 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
cristye9e3d382011-12-14 01:50:13 +00003950 text=InterpretImageProperties((ImageInfo *) NULL,(Image *) image,caption,
3951 exception);
3952 (void) CloneString(&annotate_info->text,text);
cristy6b1d05e2010-09-22 19:17:27 +00003953 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
cristye9e3d382011-12-14 01:50:13 +00003954 &text,exception);
3955 status=SetImageExtent(caption_image,image->columns,(size_t) ((count+1)*
3956 (metrics.ascent-metrics.descent)+0.5),exception);
cristy3ed852e2009-09-05 21:47:34 +00003957 if (status == MagickFalse)
3958 caption_image=DestroyImage(caption_image);
3959 else
3960 {
3961 caption_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003962 (void) SetImageBackgroundColor(caption_image,exception);
cristye9e3d382011-12-14 01:50:13 +00003963 (void) CloneString(&annotate_info->text,text);
cristyb51dff52011-05-19 16:55:47 +00003964 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
cristy3ed852e2009-09-05 21:47:34 +00003965 metrics.ascent);
3966 if (annotate_info->gravity == UndefinedGravity)
3967 (void) CloneString(&annotate_info->geometry,AcquireString(
3968 geometry));
cristy5cbc0162011-08-29 00:36:28 +00003969 (void) AnnotateImage(caption_image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003970 height+=caption_image->rows;
3971 }
3972 annotate_info=DestroyDrawInfo(annotate_info);
cristye9e3d382011-12-14 01:50:13 +00003973 text=DestroyString(text);
cristy3ed852e2009-09-05 21:47:34 +00003974 }
3975 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
3976 exception);
3977 if (picture_image == (Image *) NULL)
3978 {
3979 if (caption_image != (Image *) NULL)
3980 caption_image=DestroyImage(caption_image);
3981 return((Image *) NULL);
3982 }
3983 picture_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003984 (void) SetImageBackgroundColor(picture_image,exception);
cristy39172402012-03-30 13:04:39 +00003985 (void) CompositeImage(picture_image,image,OverCompositeOp,MagickTrue,quantum,
cristyfeb3e962012-03-29 17:25:55 +00003986 quantum,exception);
cristy3ed852e2009-09-05 21:47:34 +00003987 if (caption_image != (Image *) NULL)
3988 {
cristyfeb3e962012-03-29 17:25:55 +00003989 (void) CompositeImage(picture_image,caption_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00003990 MagickTrue,quantum,(ssize_t) (image->rows+3*quantum/2),exception);
cristy3ed852e2009-09-05 21:47:34 +00003991 caption_image=DestroyImage(caption_image);
3992 }
cristy9950d572011-10-01 18:22:35 +00003993 (void) QueryColorCompliance("none",AllCompliance,
3994 &picture_image->background_color,exception);
cristy63240882011-08-05 19:05:27 +00003995 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00003996 rotate_image=RotateImage(picture_image,90.0,exception);
3997 picture_image=DestroyImage(picture_image);
3998 if (rotate_image == (Image *) NULL)
3999 return((Image *) NULL);
4000 picture_image=rotate_image;
4001 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
cristy5c4e2582011-09-11 19:21:03 +00004002 picture_image->columns,method,exception);
cristy3ed852e2009-09-05 21:47:34 +00004003 picture_image=DestroyImage(picture_image);
4004 if (bend_image == (Image *) NULL)
4005 return((Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00004006 picture_image=bend_image;
4007 rotate_image=RotateImage(picture_image,-90.0,exception);
4008 picture_image=DestroyImage(picture_image);
4009 if (rotate_image == (Image *) NULL)
4010 return((Image *) NULL);
4011 picture_image=rotate_image;
4012 picture_image->background_color=image->background_color;
anthonyf46d4262012-03-26 03:30:34 +00004013 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
cristy3ed852e2009-09-05 21:47:34 +00004014 exception);
4015 if (polaroid_image == (Image *) NULL)
4016 {
4017 picture_image=DestroyImage(picture_image);
4018 return(picture_image);
4019 }
4020 flop_image=FlopImage(polaroid_image,exception);
4021 polaroid_image=DestroyImage(polaroid_image);
4022 if (flop_image == (Image *) NULL)
4023 {
4024 picture_image=DestroyImage(picture_image);
4025 return(picture_image);
4026 }
4027 polaroid_image=flop_image;
cristyfeb3e962012-03-29 17:25:55 +00004028 (void) CompositeImage(polaroid_image,picture_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00004029 MagickTrue,(ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
cristy3ed852e2009-09-05 21:47:34 +00004030 picture_image=DestroyImage(picture_image);
cristy9950d572011-10-01 18:22:35 +00004031 (void) QueryColorCompliance("none",AllCompliance,
4032 &polaroid_image->background_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00004033 rotate_image=RotateImage(polaroid_image,angle,exception);
4034 polaroid_image=DestroyImage(polaroid_image);
4035 if (rotate_image == (Image *) NULL)
4036 return((Image *) NULL);
4037 polaroid_image=rotate_image;
4038 trim_image=TrimImage(polaroid_image,exception);
4039 polaroid_image=DestroyImage(polaroid_image);
4040 if (trim_image == (Image *) NULL)
4041 return((Image *) NULL);
4042 polaroid_image=trim_image;
4043 return(polaroid_image);
4044}
4045
4046/*
4047%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4048% %
4049% %
4050% %
cristy3ed852e2009-09-05 21:47:34 +00004051% S e p i a T o n e I m a g e %
4052% %
4053% %
4054% %
4055%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4056%
4057% MagickSepiaToneImage() applies a special effect to the image, similar to the
4058% effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4059% 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4060% threshold of 80% is a good starting point for a reasonable tone.
4061%
4062% The format of the SepiaToneImage method is:
4063%
4064% Image *SepiaToneImage(const Image *image,const double threshold,
4065% ExceptionInfo *exception)
4066%
4067% A description of each parameter follows:
4068%
4069% o image: the image.
4070%
4071% o threshold: the tone threshold.
4072%
4073% o exception: return any errors or warnings in this structure.
4074%
4075*/
4076MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4077 ExceptionInfo *exception)
4078{
4079#define SepiaToneImageTag "SepiaTone/Image"
4080
cristyc4c8d132010-01-07 01:58:38 +00004081 CacheView
4082 *image_view,
4083 *sepia_view;
4084
cristy3ed852e2009-09-05 21:47:34 +00004085 Image
4086 *sepia_image;
4087
cristy3ed852e2009-09-05 21:47:34 +00004088 MagickBooleanType
4089 status;
4090
cristybb503372010-05-27 20:51:26 +00004091 MagickOffsetType
4092 progress;
4093
4094 ssize_t
4095 y;
4096
cristy3ed852e2009-09-05 21:47:34 +00004097 /*
4098 Initialize sepia-toned image attributes.
4099 */
4100 assert(image != (const Image *) NULL);
4101 assert(image->signature == MagickSignature);
4102 if (image->debug != MagickFalse)
4103 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4104 assert(exception != (ExceptionInfo *) NULL);
4105 assert(exception->signature == MagickSignature);
cristy1707c6c2012-01-18 23:30:54 +00004106 sepia_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004107 if (sepia_image == (Image *) NULL)
4108 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004109 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004110 {
cristy3ed852e2009-09-05 21:47:34 +00004111 sepia_image=DestroyImage(sepia_image);
4112 return((Image *) NULL);
4113 }
4114 /*
4115 Tone each row of the image.
4116 */
4117 status=MagickTrue;
4118 progress=0;
cristy46ff2672012-12-14 15:32:26 +00004119 image_view=AcquireVirtualCacheView(image,exception);
4120 sepia_view=AcquireAuthenticCacheView(sepia_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00004121#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00004122 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00004123 magick_threads(image,sepia_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00004124#endif
cristybb503372010-05-27 20:51:26 +00004125 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004126 {
cristy4c08aed2011-07-01 19:47:50 +00004127 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004128 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004129
cristybb503372010-05-27 20:51:26 +00004130 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004131 x;
4132
cristy4c08aed2011-07-01 19:47:50 +00004133 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004134 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004135
4136 if (status == MagickFalse)
4137 continue;
4138 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00004139 q=GetCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00004140 exception);
cristy4c08aed2011-07-01 19:47:50 +00004141 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004142 {
4143 status=MagickFalse;
4144 continue;
4145 }
cristybb503372010-05-27 20:51:26 +00004146 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004147 {
cristya19f1d72012-08-07 18:24:38 +00004148 double
cristy3ed852e2009-09-05 21:47:34 +00004149 intensity,
4150 tone;
4151
cristyf13c5942012-08-08 23:50:11 +00004152 intensity=GetPixelIntensity(image,p);
cristya19f1d72012-08-07 18:24:38 +00004153 tone=intensity > threshold ? (double) QuantumRange : intensity+
4154 (double) QuantumRange-threshold;
cristy4c08aed2011-07-01 19:47:50 +00004155 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
cristya19f1d72012-08-07 18:24:38 +00004156 tone=intensity > (7.0*threshold/6.0) ? (double) QuantumRange :
4157 intensity+(double) QuantumRange-7.0*threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004158 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004159 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004160 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004161 tone=threshold/7.0;
cristya19f1d72012-08-07 18:24:38 +00004162 if ((double) GetPixelGreen(image,q) < tone)
cristy4c08aed2011-07-01 19:47:50 +00004163 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
cristya19f1d72012-08-07 18:24:38 +00004164 if ((double) GetPixelBlue(image,q) < tone)
cristy4c08aed2011-07-01 19:47:50 +00004165 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristyed231572011-07-14 02:18:59 +00004166 p+=GetPixelChannels(image);
4167 q+=GetPixelChannels(sepia_image);
cristy3ed852e2009-09-05 21:47:34 +00004168 }
4169 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4170 status=MagickFalse;
4171 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4172 {
4173 MagickBooleanType
4174 proceed;
4175
cristyb5d5f722009-11-04 03:03:49 +00004176#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004177 #pragma omp critical (MagickCore_SepiaToneImage)
cristy3ed852e2009-09-05 21:47:34 +00004178#endif
4179 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4180 image->rows);
4181 if (proceed == MagickFalse)
4182 status=MagickFalse;
4183 }
4184 }
4185 sepia_view=DestroyCacheView(sepia_view);
4186 image_view=DestroyCacheView(image_view);
cristye23ec9d2011-08-16 18:15:40 +00004187 (void) NormalizeImage(sepia_image,exception);
4188 (void) ContrastImage(sepia_image,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004189 if (status == MagickFalse)
4190 sepia_image=DestroyImage(sepia_image);
4191 return(sepia_image);
4192}
4193
4194/*
4195%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4196% %
4197% %
4198% %
4199% S h a d o w I m a g e %
4200% %
4201% %
4202% %
4203%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4204%
4205% ShadowImage() simulates a shadow from the specified image and returns it.
4206%
4207% The format of the ShadowImage method is:
4208%
cristy70cddf72011-12-10 22:42:42 +00004209% Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004210% const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4211% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004212%
4213% A description of each parameter follows:
4214%
4215% o image: the image.
4216%
cristy70cddf72011-12-10 22:42:42 +00004217% o alpha: percentage transparency.
cristy3ed852e2009-09-05 21:47:34 +00004218%
4219% o sigma: the standard deviation of the Gaussian, in pixels.
4220%
4221% o x_offset: the shadow x-offset.
4222%
4223% o y_offset: the shadow y-offset.
4224%
4225% o exception: return any errors or warnings in this structure.
4226%
4227*/
cristy70cddf72011-12-10 22:42:42 +00004228MagickExport Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004229 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4230 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004231{
4232#define ShadowImageTag "Shadow/Image"
4233
cristy70cddf72011-12-10 22:42:42 +00004234 CacheView
4235 *image_view;
4236
cristybd5a96c2011-08-21 00:04:26 +00004237 ChannelType
4238 channel_mask;
4239
cristy3ed852e2009-09-05 21:47:34 +00004240 Image
4241 *border_image,
4242 *clone_image,
4243 *shadow_image;
4244
cristy70cddf72011-12-10 22:42:42 +00004245 MagickBooleanType
4246 status;
4247
cristy3ed852e2009-09-05 21:47:34 +00004248 RectangleInfo
4249 border_info;
4250
cristy70cddf72011-12-10 22:42:42 +00004251 ssize_t
4252 y;
4253
cristy3ed852e2009-09-05 21:47:34 +00004254 assert(image != (Image *) NULL);
4255 assert(image->signature == MagickSignature);
4256 if (image->debug != MagickFalse)
4257 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4258 assert(exception != (ExceptionInfo *) NULL);
4259 assert(exception->signature == MagickSignature);
4260 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4261 if (clone_image == (Image *) NULL)
4262 return((Image *) NULL);
cristy0ce08762012-06-30 01:33:18 +00004263 (void) SetImageVirtualPixelMethod(clone_image,TransparentVirtualPixelMethod,
cristy387430f2012-02-07 13:09:46 +00004264 exception);
cristybb503372010-05-27 20:51:26 +00004265 border_info.width=(size_t) floor(2.0*sigma+0.5);
4266 border_info.height=(size_t) floor(2.0*sigma+0.5);
cristy3ed852e2009-09-05 21:47:34 +00004267 border_info.x=0;
4268 border_info.y=0;
cristy9950d572011-10-01 18:22:35 +00004269 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4270 exception);
cristy8a46d822012-08-28 23:32:39 +00004271 clone_image->alpha_trait=BlendPixelTrait;
cristy70cddf72011-12-10 22:42:42 +00004272 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
cristy3ed852e2009-09-05 21:47:34 +00004273 clone_image=DestroyImage(clone_image);
4274 if (border_image == (Image *) NULL)
4275 return((Image *) NULL);
cristy8a46d822012-08-28 23:32:39 +00004276 if (border_image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00004277 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004278 /*
4279 Shadow image.
4280 */
cristy70cddf72011-12-10 22:42:42 +00004281 status=MagickTrue;
cristy46ff2672012-12-14 15:32:26 +00004282 image_view=AcquireAuthenticCacheView(border_image,exception);
cristy70cddf72011-12-10 22:42:42 +00004283 for (y=0; y < (ssize_t) border_image->rows; y++)
4284 {
4285 PixelInfo
4286 background_color;
4287
4288 register Quantum
4289 *restrict q;
4290
4291 register ssize_t
4292 x;
4293
4294 if (status == MagickFalse)
4295 continue;
4296 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
4297 exception);
4298 if (q == (Quantum *) NULL)
4299 {
4300 status=MagickFalse;
4301 continue;
4302 }
4303 background_color=border_image->background_color;
cristy8a46d822012-08-28 23:32:39 +00004304 background_color.alpha_trait=BlendPixelTrait;
cristy70cddf72011-12-10 22:42:42 +00004305 for (x=0; x < (ssize_t) border_image->columns; x++)
4306 {
cristy8a46d822012-08-28 23:32:39 +00004307 if (border_image->alpha_trait == BlendPixelTrait)
cristy70cddf72011-12-10 22:42:42 +00004308 background_color.alpha=GetPixelAlpha(border_image,q)*alpha/100.0;
4309 SetPixelInfoPixel(border_image,&background_color,q);
4310 q+=GetPixelChannels(border_image);
4311 }
4312 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4313 status=MagickFalse;
4314 }
4315 image_view=DestroyCacheView(image_view);
4316 if (status == MagickFalse)
4317 {
4318 border_image=DestroyImage(border_image);
4319 return((Image *) NULL);
4320 }
cristycf1296e2012-08-26 23:40:49 +00004321 channel_mask=SetImageChannelMask(border_image,AlphaChannel);
cristyaa2c16c2012-03-25 22:21:35 +00004322 shadow_image=BlurImage(border_image,0.0,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00004323 border_image=DestroyImage(border_image);
4324 if (shadow_image == (Image *) NULL)
4325 return((Image *) NULL);
cristycf1296e2012-08-26 23:40:49 +00004326 (void) SetPixelChannelMask(shadow_image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00004327 if (shadow_image->page.width == 0)
4328 shadow_image->page.width=shadow_image->columns;
4329 if (shadow_image->page.height == 0)
4330 shadow_image->page.height=shadow_image->rows;
cristybb503372010-05-27 20:51:26 +00004331 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4332 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4333 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4334 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
cristy3ed852e2009-09-05 21:47:34 +00004335 return(shadow_image);
4336}
4337
4338/*
4339%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4340% %
4341% %
4342% %
4343% S k e t c h I m a g e %
4344% %
4345% %
4346% %
4347%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4348%
4349% SketchImage() simulates a pencil sketch. We convolve the image with a
4350% Gaussian operator of the given radius and standard deviation (sigma). For
4351% reasonable results, radius should be larger than sigma. Use a radius of 0
4352% and SketchImage() selects a suitable radius for you. Angle gives the angle
4353% of the sketch.
4354%
4355% The format of the SketchImage method is:
4356%
4357% Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004358% const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004359%
4360% A description of each parameter follows:
4361%
4362% o image: the image.
4363%
cristy574cc262011-08-05 01:23:58 +00004364% o radius: the radius of the Gaussian, in pixels, not counting the
4365% center pixel.
cristy3ed852e2009-09-05 21:47:34 +00004366%
4367% o sigma: the standard deviation of the Gaussian, in pixels.
4368%
cristyf7ef0252011-09-09 14:50:06 +00004369% o angle: apply the effect along this angle.
4370%
cristy3ed852e2009-09-05 21:47:34 +00004371% o exception: return any errors or warnings in this structure.
4372%
4373*/
4374MagickExport Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004375 const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004376{
cristyfa112112010-01-04 17:48:07 +00004377 CacheView
4378 *random_view;
4379
cristy3ed852e2009-09-05 21:47:34 +00004380 Image
4381 *blend_image,
4382 *blur_image,
4383 *dodge_image,
4384 *random_image,
4385 *sketch_image;
4386
cristy3ed852e2009-09-05 21:47:34 +00004387 MagickBooleanType
4388 status;
4389
cristy3ed852e2009-09-05 21:47:34 +00004390 RandomInfo
cristyfa112112010-01-04 17:48:07 +00004391 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00004392
cristy9d314ff2011-03-09 01:30:28 +00004393 ssize_t
4394 y;
4395
glennrpf7659d72012-09-24 18:14:56 +00004396#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +00004397 unsigned long
4398 key;
glennrpf7659d72012-09-24 18:14:56 +00004399#endif
cristy57340e02012-05-05 00:53:23 +00004400
cristy3ed852e2009-09-05 21:47:34 +00004401 /*
4402 Sketch image.
4403 */
4404 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4405 MagickTrue,exception);
4406 if (random_image == (Image *) NULL)
4407 return((Image *) NULL);
4408 status=MagickTrue;
cristy1b784432009-12-19 02:20:40 +00004409 random_info=AcquireRandomInfoThreadSet();
glennrpf7659d72012-09-24 18:14:56 +00004410#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +00004411 key=GetRandomSecretKey(random_info[0]);
glennrpf7659d72012-09-24 18:14:56 +00004412#endif
cristy46ff2672012-12-14 15:32:26 +00004413 random_view=AcquireAuthenticCacheView(random_image,exception);
cristy1b784432009-12-19 02:20:40 +00004414#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +00004415 #pragma omp parallel for schedule(static,4) shared(status) \
cristy5e6b2592012-12-19 14:08:11 +00004416 magick_threads(random_image,random_image,random_image->rows,key == ~0UL)
cristy1b784432009-12-19 02:20:40 +00004417#endif
cristybb503372010-05-27 20:51:26 +00004418 for (y=0; y < (ssize_t) random_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004419 {
cristy5c9e6f22010-09-17 17:31:01 +00004420 const int
4421 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00004422
cristybb503372010-05-27 20:51:26 +00004423 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004424 x;
4425
cristy4c08aed2011-07-01 19:47:50 +00004426 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004427 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004428
cristy1b784432009-12-19 02:20:40 +00004429 if (status == MagickFalse)
4430 continue;
cristy3ed852e2009-09-05 21:47:34 +00004431 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4432 exception);
cristyacd2ed22011-08-30 01:44:23 +00004433 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004434 {
4435 status=MagickFalse;
4436 continue;
4437 }
cristybb503372010-05-27 20:51:26 +00004438 for (x=0; x < (ssize_t) random_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004439 {
cristya19f1d72012-08-07 18:24:38 +00004440 double
cristy76f512e2011-09-12 01:26:56 +00004441 value;
4442
4443 register ssize_t
4444 i;
4445
cristy10a6c612012-01-29 21:41:05 +00004446 if (GetPixelMask(random_image,q) != 0)
4447 {
4448 q+=GetPixelChannels(random_image);
4449 continue;
4450 }
cristy76f512e2011-09-12 01:26:56 +00004451 value=GetPseudoRandomValue(random_info[id]);
4452 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4453 {
cristy5a23c552013-02-13 14:34:28 +00004454 PixelChannel channel=GetPixelChannelChannel(image,i);
4455 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004456 if (traits == UndefinedPixelTrait)
4457 continue;
4458 q[i]=ClampToQuantum(QuantumRange*value);
4459 }
cristyed231572011-07-14 02:18:59 +00004460 q+=GetPixelChannels(random_image);
cristy3ed852e2009-09-05 21:47:34 +00004461 }
4462 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4463 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004464 }
4465 random_view=DestroyCacheView(random_view);
cristy1b784432009-12-19 02:20:40 +00004466 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00004467 if (status == MagickFalse)
4468 {
4469 random_image=DestroyImage(random_image);
4470 return(random_image);
4471 }
cristyaa2c16c2012-03-25 22:21:35 +00004472 blur_image=MotionBlurImage(random_image,radius,sigma,angle,exception);
cristy3ed852e2009-09-05 21:47:34 +00004473 random_image=DestroyImage(random_image);
4474 if (blur_image == (Image *) NULL)
4475 return((Image *) NULL);
cristy6bfd6902011-12-09 01:33:45 +00004476 dodge_image=EdgeImage(blur_image,radius,1.0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004477 blur_image=DestroyImage(blur_image);
4478 if (dodge_image == (Image *) NULL)
4479 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +00004480 (void) NormalizeImage(dodge_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +00004481 (void) NegateImage(dodge_image,MagickFalse,exception);
cristye941a752011-10-15 01:52:48 +00004482 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
cristy3ed852e2009-09-05 21:47:34 +00004483 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4484 if (sketch_image == (Image *) NULL)
4485 {
4486 dodge_image=DestroyImage(dodge_image);
4487 return((Image *) NULL);
4488 }
cristyfeb3e962012-03-29 17:25:55 +00004489 (void) CompositeImage(sketch_image,dodge_image,ColorDodgeCompositeOp,
cristy39172402012-03-30 13:04:39 +00004490 MagickTrue,0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004491 dodge_image=DestroyImage(dodge_image);
4492 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4493 if (blend_image == (Image *) NULL)
4494 {
4495 sketch_image=DestroyImage(sketch_image);
4496 return((Image *) NULL);
4497 }
4498 (void) SetImageArtifact(blend_image,"compose:args","20x80");
cristy39172402012-03-30 13:04:39 +00004499 (void) CompositeImage(sketch_image,blend_image,BlendCompositeOp,MagickTrue,
cristyfeb3e962012-03-29 17:25:55 +00004500 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004501 blend_image=DestroyImage(blend_image);
4502 return(sketch_image);
4503}
4504
4505/*
4506%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4507% %
4508% %
4509% %
4510% S o l a r i z e I m a g e %
4511% %
4512% %
4513% %
4514%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4515%
4516% SolarizeImage() applies a special effect to the image, similar to the effect
4517% achieved in a photo darkroom by selectively exposing areas of photo
4518% sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4519% measure of the extent of the solarization.
4520%
4521% The format of the SolarizeImage method is:
4522%
cristy5cbc0162011-08-29 00:36:28 +00004523% MagickBooleanType SolarizeImage(Image *image,const double threshold,
4524% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004525%
4526% A description of each parameter follows:
4527%
4528% o image: the image.
4529%
4530% o threshold: Define the extent of the solarization.
4531%
cristy5cbc0162011-08-29 00:36:28 +00004532% o exception: return any errors or warnings in this structure.
4533%
cristy3ed852e2009-09-05 21:47:34 +00004534*/
4535MagickExport MagickBooleanType SolarizeImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00004536 const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004537{
4538#define SolarizeImageTag "Solarize/Image"
4539
cristyc4c8d132010-01-07 01:58:38 +00004540 CacheView
4541 *image_view;
4542
cristy3ed852e2009-09-05 21:47:34 +00004543 MagickBooleanType
4544 status;
4545
cristybb503372010-05-27 20:51:26 +00004546 MagickOffsetType
4547 progress;
4548
4549 ssize_t
4550 y;
4551
cristy3ed852e2009-09-05 21:47:34 +00004552 assert(image != (Image *) NULL);
4553 assert(image->signature == MagickSignature);
4554 if (image->debug != MagickFalse)
4555 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4556 if (image->storage_class == PseudoClass)
4557 {
cristybb503372010-05-27 20:51:26 +00004558 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004559 i;
4560
4561 /*
4562 Solarize colormap.
4563 */
cristybb503372010-05-27 20:51:26 +00004564 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00004565 {
cristya19f1d72012-08-07 18:24:38 +00004566 if ((double) image->colormap[i].red > threshold)
cristy6e963d82012-06-19 15:23:24 +00004567 image->colormap[i].red=QuantumRange-image->colormap[i].red;
cristya19f1d72012-08-07 18:24:38 +00004568 if ((double) image->colormap[i].green > threshold)
cristy6e963d82012-06-19 15:23:24 +00004569 image->colormap[i].green=QuantumRange-
cristy3ed852e2009-09-05 21:47:34 +00004570 image->colormap[i].green;
cristya19f1d72012-08-07 18:24:38 +00004571 if ((double) image->colormap[i].blue > threshold)
cristy6e963d82012-06-19 15:23:24 +00004572 image->colormap[i].blue=QuantumRange-
cristy3ed852e2009-09-05 21:47:34 +00004573 image->colormap[i].blue;
4574 }
4575 }
4576 /*
4577 Solarize image.
4578 */
4579 status=MagickTrue;
4580 progress=0;
cristy46ff2672012-12-14 15:32:26 +00004581 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00004582#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00004583 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00004584 magick_threads(image,image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00004585#endif
cristybb503372010-05-27 20:51:26 +00004586 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004587 {
cristybb503372010-05-27 20:51:26 +00004588 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004589 x;
4590
cristy4c08aed2011-07-01 19:47:50 +00004591 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004592 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004593
4594 if (status == MagickFalse)
4595 continue;
cristy5cbc0162011-08-29 00:36:28 +00004596 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00004597 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004598 {
4599 status=MagickFalse;
4600 continue;
4601 }
cristybb503372010-05-27 20:51:26 +00004602 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004603 {
cristy76f512e2011-09-12 01:26:56 +00004604 register ssize_t
4605 i;
4606
cristy10a6c612012-01-29 21:41:05 +00004607 if (GetPixelMask(image,q) != 0)
4608 {
4609 q+=GetPixelChannels(image);
4610 continue;
4611 }
cristy76f512e2011-09-12 01:26:56 +00004612 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4613 {
cristy5a23c552013-02-13 14:34:28 +00004614 PixelChannel channel=GetPixelChannelChannel(image,i);
4615 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004616 if ((traits == UndefinedPixelTrait) ||
4617 ((traits & CopyPixelTrait) != 0))
4618 continue;
cristya19f1d72012-08-07 18:24:38 +00004619 if ((double) q[i] > threshold)
cristy76f512e2011-09-12 01:26:56 +00004620 q[i]=QuantumRange-q[i];
4621 }
cristyed231572011-07-14 02:18:59 +00004622 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004623 }
4624 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4625 status=MagickFalse;
4626 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4627 {
4628 MagickBooleanType
4629 proceed;
4630
cristyb5d5f722009-11-04 03:03:49 +00004631#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004632 #pragma omp critical (MagickCore_SolarizeImage)
cristy3ed852e2009-09-05 21:47:34 +00004633#endif
4634 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4635 if (proceed == MagickFalse)
4636 status=MagickFalse;
4637 }
4638 }
4639 image_view=DestroyCacheView(image_view);
4640 return(status);
4641}
4642
4643/*
4644%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4645% %
4646% %
4647% %
4648% S t e g a n o I m a g e %
4649% %
4650% %
4651% %
4652%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4653%
4654% SteganoImage() hides a digital watermark within the image. Recover
4655% the hidden watermark later to prove that the authenticity of an image.
4656% Offset defines the start position within the image to hide the watermark.
4657%
4658% The format of the SteganoImage method is:
4659%
4660% Image *SteganoImage(const Image *image,Image *watermark,
4661% ExceptionInfo *exception)
4662%
4663% A description of each parameter follows:
4664%
4665% o image: the image.
4666%
4667% o watermark: the watermark image.
4668%
4669% o exception: return any errors or warnings in this structure.
4670%
4671*/
4672MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4673 ExceptionInfo *exception)
4674{
cristye1bf8ad2010-09-19 17:07:03 +00004675#define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
cristy4c08aed2011-07-01 19:47:50 +00004676#define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
cristyeaedf062010-05-29 22:36:02 +00004677 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
cristy3ed852e2009-09-05 21:47:34 +00004678#define SteganoImageTag "Stegano/Image"
4679
cristyb0d3bb92010-09-22 14:37:58 +00004680 CacheView
4681 *stegano_view,
4682 *watermark_view;
4683
cristy3ed852e2009-09-05 21:47:34 +00004684 Image
4685 *stegano_image;
4686
4687 int
4688 c;
4689
cristy3ed852e2009-09-05 21:47:34 +00004690 MagickBooleanType
4691 status;
4692
cristy101ab702011-10-13 13:06:32 +00004693 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004694 pixel;
4695
cristy4c08aed2011-07-01 19:47:50 +00004696 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004697 *q;
4698
cristye1bf8ad2010-09-19 17:07:03 +00004699 register ssize_t
4700 x;
4701
cristybb503372010-05-27 20:51:26 +00004702 size_t
cristyeaedf062010-05-29 22:36:02 +00004703 depth,
4704 one;
cristy3ed852e2009-09-05 21:47:34 +00004705
cristye1bf8ad2010-09-19 17:07:03 +00004706 ssize_t
4707 i,
4708 j,
4709 k,
4710 y;
4711
cristy3ed852e2009-09-05 21:47:34 +00004712 /*
4713 Initialize steganographic image attributes.
4714 */
4715 assert(image != (const Image *) NULL);
4716 assert(image->signature == MagickSignature);
4717 if (image->debug != MagickFalse)
4718 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4719 assert(watermark != (const Image *) NULL);
4720 assert(watermark->signature == MagickSignature);
4721 assert(exception != (ExceptionInfo *) NULL);
4722 assert(exception->signature == MagickSignature);
cristyeaedf062010-05-29 22:36:02 +00004723 one=1UL;
cristy3ed852e2009-09-05 21:47:34 +00004724 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4725 if (stegano_image == (Image *) NULL)
4726 return((Image *) NULL);
cristyf61b1832012-04-01 01:38:19 +00004727 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
cristy574cc262011-08-05 01:23:58 +00004728 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004729 {
cristy3ed852e2009-09-05 21:47:34 +00004730 stegano_image=DestroyImage(stegano_image);
4731 return((Image *) NULL);
4732 }
cristy3ed852e2009-09-05 21:47:34 +00004733 /*
4734 Hide watermark in low-order bits of image.
4735 */
4736 c=0;
4737 i=0;
4738 j=0;
4739 depth=stegano_image->depth;
cristyf61b1832012-04-01 01:38:19 +00004740 k=stegano_image->offset;
cristyda16f162011-02-19 23:52:17 +00004741 status=MagickTrue;
cristy46ff2672012-12-14 15:32:26 +00004742 watermark_view=AcquireVirtualCacheView(watermark,exception);
4743 stegano_view=AcquireAuthenticCacheView(stegano_image,exception);
cristybb503372010-05-27 20:51:26 +00004744 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
cristy3ed852e2009-09-05 21:47:34 +00004745 {
cristybb503372010-05-27 20:51:26 +00004746 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
cristy3ed852e2009-09-05 21:47:34 +00004747 {
cristybb503372010-05-27 20:51:26 +00004748 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
cristy3ed852e2009-09-05 21:47:34 +00004749 {
cristy1707c6c2012-01-18 23:30:54 +00004750 ssize_t
4751 offset;
4752
cristyf05d4942012-03-17 16:26:09 +00004753 (void) GetOneCacheViewVirtualPixelInfo(watermark_view,x,y,&pixel,
cristyda1f9c12011-10-02 21:39:49 +00004754 exception);
cristy1707c6c2012-01-18 23:30:54 +00004755 offset=k/(ssize_t) stegano_image->columns;
4756 if (offset >= (ssize_t) stegano_image->rows)
cristy3ed852e2009-09-05 21:47:34 +00004757 break;
cristyb0d3bb92010-09-22 14:37:58 +00004758 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4759 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4760 exception);
cristyacd2ed22011-08-30 01:44:23 +00004761 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004762 break;
4763 switch (c)
4764 {
4765 case 0:
4766 {
cristyf61b1832012-04-01 01:38:19 +00004767 SetPixelRed(stegano_image,SetBit(GetPixelRed(stegano_image,q),j,
4768 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004769 break;
4770 }
4771 case 1:
4772 {
cristyf61b1832012-04-01 01:38:19 +00004773 SetPixelGreen(stegano_image,SetBit(GetPixelGreen(stegano_image,q),j,
4774 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004775 break;
4776 }
4777 case 2:
4778 {
cristyf61b1832012-04-01 01:38:19 +00004779 SetPixelBlue(stegano_image,SetBit(GetPixelBlue(stegano_image,q),j,
4780 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004781 break;
4782 }
4783 }
cristyb0d3bb92010-09-22 14:37:58 +00004784 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004785 break;
4786 c++;
4787 if (c == 3)
4788 c=0;
4789 k++;
cristybb503372010-05-27 20:51:26 +00004790 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
cristy3ed852e2009-09-05 21:47:34 +00004791 k=0;
cristyf61b1832012-04-01 01:38:19 +00004792 if (k == stegano_image->offset)
cristy3ed852e2009-09-05 21:47:34 +00004793 j++;
4794 }
4795 }
cristy8b27a6d2010-02-14 03:31:15 +00004796 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004797 {
cristy8b27a6d2010-02-14 03:31:15 +00004798 MagickBooleanType
4799 proceed;
4800
4801 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4802 (depth-i),depth);
4803 if (proceed == MagickFalse)
4804 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004805 }
4806 }
cristyb0d3bb92010-09-22 14:37:58 +00004807 stegano_view=DestroyCacheView(stegano_view);
4808 watermark_view=DestroyCacheView(watermark_view);
cristyda16f162011-02-19 23:52:17 +00004809 if (status == MagickFalse)
cristyfab83642012-12-14 00:31:38 +00004810 stegano_image=DestroyImage(stegano_image);
cristy3ed852e2009-09-05 21:47:34 +00004811 return(stegano_image);
4812}
4813
4814/*
4815%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4816% %
4817% %
4818% %
4819% S t e r e o A n a g l y p h I m a g e %
4820% %
4821% %
4822% %
4823%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4824%
4825% StereoAnaglyphImage() combines two images and produces a single image that
4826% is the composite of a left and right image of a stereo pair. Special
4827% red-green stereo glasses are required to view this effect.
4828%
4829% The format of the StereoAnaglyphImage method is:
4830%
4831% Image *StereoImage(const Image *left_image,const Image *right_image,
4832% ExceptionInfo *exception)
4833% Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004834% const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004835% ExceptionInfo *exception)
4836%
4837% A description of each parameter follows:
4838%
4839% o left_image: the left image.
4840%
4841% o right_image: the right image.
4842%
4843% o exception: return any errors or warnings in this structure.
4844%
4845% o x_offset: amount, in pixels, by which the left image is offset to the
4846% right of the right image.
4847%
4848% o y_offset: amount, in pixels, by which the left image is offset to the
4849% bottom of the right image.
4850%
4851%
4852*/
4853MagickExport Image *StereoImage(const Image *left_image,
4854 const Image *right_image,ExceptionInfo *exception)
4855{
4856 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4857}
4858
4859MagickExport Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004860 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004861 ExceptionInfo *exception)
4862{
4863#define StereoImageTag "Stereo/Image"
4864
4865 const Image
4866 *image;
4867
4868 Image
4869 *stereo_image;
4870
cristy3ed852e2009-09-05 21:47:34 +00004871 MagickBooleanType
4872 status;
4873
cristy9d314ff2011-03-09 01:30:28 +00004874 ssize_t
4875 y;
4876
cristy3ed852e2009-09-05 21:47:34 +00004877 assert(left_image != (const Image *) NULL);
4878 assert(left_image->signature == MagickSignature);
4879 if (left_image->debug != MagickFalse)
4880 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4881 left_image->filename);
4882 assert(right_image != (const Image *) NULL);
4883 assert(right_image->signature == MagickSignature);
4884 assert(exception != (ExceptionInfo *) NULL);
4885 assert(exception->signature == MagickSignature);
4886 assert(right_image != (const Image *) NULL);
4887 image=left_image;
4888 if ((left_image->columns != right_image->columns) ||
4889 (left_image->rows != right_image->rows))
4890 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4891 /*
4892 Initialize stereo image attributes.
4893 */
4894 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4895 MagickTrue,exception);
4896 if (stereo_image == (Image *) NULL)
4897 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004898 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004899 {
cristy3ed852e2009-09-05 21:47:34 +00004900 stereo_image=DestroyImage(stereo_image);
4901 return((Image *) NULL);
4902 }
cristy079c78d2012-07-03 11:53:48 +00004903 (void) SetImageColorspace(stereo_image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00004904 /*
4905 Copy left image to red channel and right image to blue channel.
4906 */
cristyda16f162011-02-19 23:52:17 +00004907 status=MagickTrue;
cristybb503372010-05-27 20:51:26 +00004908 for (y=0; y < (ssize_t) stereo_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004909 {
cristy4c08aed2011-07-01 19:47:50 +00004910 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004911 *restrict p,
4912 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004913
cristybb503372010-05-27 20:51:26 +00004914 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004915 x;
4916
cristy4c08aed2011-07-01 19:47:50 +00004917 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004918 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +00004919
4920 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4921 exception);
4922 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4923 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
cristy76f512e2011-09-12 01:26:56 +00004924 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4925 (r == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004926 break;
cristybb503372010-05-27 20:51:26 +00004927 for (x=0; x < (ssize_t) stereo_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004928 {
cristy4c08aed2011-07-01 19:47:50 +00004929 SetPixelRed(image,GetPixelRed(left_image,p),r);
cristy76f512e2011-09-12 01:26:56 +00004930 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4931 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4932 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4933 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4934 GetPixelAlpha(right_image,q))/2,r);
cristyed231572011-07-14 02:18:59 +00004935 p+=GetPixelChannels(left_image);
cristy76f512e2011-09-12 01:26:56 +00004936 q+=GetPixelChannels(right_image);
4937 r+=GetPixelChannels(stereo_image);
cristy3ed852e2009-09-05 21:47:34 +00004938 }
4939 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4940 break;
cristy8b27a6d2010-02-14 03:31:15 +00004941 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004942 {
cristy8b27a6d2010-02-14 03:31:15 +00004943 MagickBooleanType
4944 proceed;
4945
cristybb503372010-05-27 20:51:26 +00004946 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4947 stereo_image->rows);
cristy8b27a6d2010-02-14 03:31:15 +00004948 if (proceed == MagickFalse)
4949 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004950 }
4951 }
cristyda16f162011-02-19 23:52:17 +00004952 if (status == MagickFalse)
cristyfab83642012-12-14 00:31:38 +00004953 stereo_image=DestroyImage(stereo_image);
cristy3ed852e2009-09-05 21:47:34 +00004954 return(stereo_image);
4955}
4956
4957/*
4958%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4959% %
4960% %
4961% %
4962% S w i r l I m a g e %
4963% %
4964% %
4965% %
4966%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4967%
4968% SwirlImage() swirls the pixels about the center of the image, where
4969% degrees indicates the sweep of the arc through which each pixel is moved.
4970% You get a more dramatic effect as the degrees move from 1 to 360.
4971%
4972% The format of the SwirlImage method is:
4973%
4974% Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00004975% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004976%
4977% A description of each parameter follows:
4978%
4979% o image: the image.
4980%
4981% o degrees: Define the tightness of the swirling effect.
4982%
cristy76f512e2011-09-12 01:26:56 +00004983% o method: the pixel interpolation method.
4984%
cristy3ed852e2009-09-05 21:47:34 +00004985% o exception: return any errors or warnings in this structure.
4986%
4987*/
4988MagickExport Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00004989 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004990{
4991#define SwirlImageTag "Swirl/Image"
4992
cristyfa112112010-01-04 17:48:07 +00004993 CacheView
4994 *image_view,
4995 *swirl_view;
4996
cristy3ed852e2009-09-05 21:47:34 +00004997 Image
4998 *swirl_image;
4999
cristy3ed852e2009-09-05 21:47:34 +00005000 MagickBooleanType
5001 status;
5002
cristybb503372010-05-27 20:51:26 +00005003 MagickOffsetType
5004 progress;
5005
cristya19f1d72012-08-07 18:24:38 +00005006 double
cristy3ed852e2009-09-05 21:47:34 +00005007 radius;
5008
5009 PointInfo
5010 center,
5011 scale;
5012
cristybb503372010-05-27 20:51:26 +00005013 ssize_t
5014 y;
5015
cristy3ed852e2009-09-05 21:47:34 +00005016 /*
5017 Initialize swirl image attributes.
5018 */
5019 assert(image != (const Image *) NULL);
5020 assert(image->signature == MagickSignature);
5021 if (image->debug != MagickFalse)
5022 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5023 assert(exception != (ExceptionInfo *) NULL);
5024 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00005025 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005026 if (swirl_image == (Image *) NULL)
5027 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005028 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005029 {
cristy3ed852e2009-09-05 21:47:34 +00005030 swirl_image=DestroyImage(swirl_image);
5031 return((Image *) NULL);
5032 }
cristy4c08aed2011-07-01 19:47:50 +00005033 if (swirl_image->background_color.alpha != OpaqueAlpha)
cristy8a46d822012-08-28 23:32:39 +00005034 swirl_image->alpha_trait=BlendPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +00005035 /*
5036 Compute scaling factor.
5037 */
5038 center.x=(double) image->columns/2.0;
5039 center.y=(double) image->rows/2.0;
5040 radius=MagickMax(center.x,center.y);
5041 scale.x=1.0;
5042 scale.y=1.0;
5043 if (image->columns > image->rows)
5044 scale.y=(double) image->columns/(double) image->rows;
5045 else
5046 if (image->columns < image->rows)
5047 scale.x=(double) image->rows/(double) image->columns;
5048 degrees=(double) DegreesToRadians(degrees);
5049 /*
5050 Swirl image.
5051 */
5052 status=MagickTrue;
5053 progress=0;
cristy46ff2672012-12-14 15:32:26 +00005054 image_view=AcquireVirtualCacheView(image,exception);
5055 swirl_view=AcquireAuthenticCacheView(swirl_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00005056#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005057 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00005058 magick_threads(image,swirl_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005059#endif
cristybb503372010-05-27 20:51:26 +00005060 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005061 {
cristya19f1d72012-08-07 18:24:38 +00005062 double
cristy3ed852e2009-09-05 21:47:34 +00005063 distance;
5064
5065 PointInfo
5066 delta;
5067
cristy6d188022011-09-12 13:23:33 +00005068 register const Quantum
5069 *restrict p;
5070
cristybb503372010-05-27 20:51:26 +00005071 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005072 x;
5073
cristy4c08aed2011-07-01 19:47:50 +00005074 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005075 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005076
5077 if (status == MagickFalse)
5078 continue;
cristy6d188022011-09-12 13:23:33 +00005079 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00005080 q=QueueCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00005081 exception);
cristy6d188022011-09-12 13:23:33 +00005082 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005083 {
5084 status=MagickFalse;
5085 continue;
5086 }
cristy3ed852e2009-09-05 21:47:34 +00005087 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00005088 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005089 {
5090 /*
5091 Determine if the pixel is within an ellipse.
5092 */
cristy10a6c612012-01-29 21:41:05 +00005093 if (GetPixelMask(image,p) != 0)
5094 {
5095 p+=GetPixelChannels(image);
5096 q+=GetPixelChannels(swirl_image);
5097 continue;
5098 }
cristy3ed852e2009-09-05 21:47:34 +00005099 delta.x=scale.x*(double) (x-center.x);
5100 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00005101 if (distance >= (radius*radius))
5102 {
cristy1707c6c2012-01-18 23:30:54 +00005103 register ssize_t
5104 i;
5105
cristy6d188022011-09-12 13:23:33 +00005106 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy1707c6c2012-01-18 23:30:54 +00005107 {
cristy5a23c552013-02-13 14:34:28 +00005108 PixelChannel channel=GetPixelChannelChannel(image,i);
5109 PixelTrait traits=GetPixelChannelTraits(image,channel);
5110 PixelTrait swirl_traits=GetPixelChannelTraits(swirl_image,channel);
cristy1707c6c2012-01-18 23:30:54 +00005111 if ((traits == UndefinedPixelTrait) ||
5112 (swirl_traits == UndefinedPixelTrait))
5113 continue;
5114 SetPixelChannel(swirl_image,channel,p[i],q);
5115 }
cristy6d188022011-09-12 13:23:33 +00005116 }
5117 else
cristy3ed852e2009-09-05 21:47:34 +00005118 {
cristya19f1d72012-08-07 18:24:38 +00005119 double
cristy3ed852e2009-09-05 21:47:34 +00005120 cosine,
5121 factor,
5122 sine;
5123
5124 /*
5125 Swirl the pixel.
5126 */
5127 factor=1.0-sqrt((double) distance)/radius;
5128 sine=sin((double) (degrees*factor*factor));
5129 cosine=cos((double) (degrees*factor*factor));
cristy76f512e2011-09-12 01:26:56 +00005130 status=InterpolatePixelChannels(image,image_view,swirl_image,method,
5131 ((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5132 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00005133 }
cristy6d188022011-09-12 13:23:33 +00005134 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00005135 q+=GetPixelChannels(swirl_image);
cristy3ed852e2009-09-05 21:47:34 +00005136 }
5137 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5138 status=MagickFalse;
5139 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5140 {
5141 MagickBooleanType
5142 proceed;
5143
cristyb5d5f722009-11-04 03:03:49 +00005144#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005145 #pragma omp critical (MagickCore_SwirlImage)
cristy3ed852e2009-09-05 21:47:34 +00005146#endif
5147 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5148 if (proceed == MagickFalse)
5149 status=MagickFalse;
5150 }
5151 }
5152 swirl_view=DestroyCacheView(swirl_view);
5153 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005154 if (status == MagickFalse)
5155 swirl_image=DestroyImage(swirl_image);
5156 return(swirl_image);
5157}
5158
5159/*
5160%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5161% %
5162% %
5163% %
5164% T i n t I m a g e %
5165% %
5166% %
5167% %
5168%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5169%
5170% TintImage() applies a color vector to each pixel in the image. The length
5171% of the vector is 0 for black and white and at its maximum for the midtones.
5172% The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5173%
5174% The format of the TintImage method is:
5175%
cristyb817c3f2011-10-03 14:00:35 +00005176% Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005177% const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005178%
5179% A description of each parameter follows:
5180%
5181% o image: the image.
5182%
cristyb817c3f2011-10-03 14:00:35 +00005183% o blend: A color value used for tinting.
cristy3ed852e2009-09-05 21:47:34 +00005184%
5185% o tint: A color value used for tinting.
5186%
5187% o exception: return any errors or warnings in this structure.
5188%
5189*/
cristyb817c3f2011-10-03 14:00:35 +00005190MagickExport Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005191 const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005192{
5193#define TintImageTag "Tint/Image"
5194
cristyc4c8d132010-01-07 01:58:38 +00005195 CacheView
5196 *image_view,
5197 *tint_view;
5198
cristy3ed852e2009-09-05 21:47:34 +00005199 GeometryInfo
5200 geometry_info;
5201
5202 Image
5203 *tint_image;
5204
cristy3ed852e2009-09-05 21:47:34 +00005205 MagickBooleanType
5206 status;
5207
cristybb503372010-05-27 20:51:26 +00005208 MagickOffsetType
5209 progress;
cristy3ed852e2009-09-05 21:47:34 +00005210
cristya19f1d72012-08-07 18:24:38 +00005211 double
cristy28474bf2011-09-11 23:32:52 +00005212 intensity;
5213
cristy4c08aed2011-07-01 19:47:50 +00005214 PixelInfo
cristy1707c6c2012-01-18 23:30:54 +00005215 color_vector;
cristy3ed852e2009-09-05 21:47:34 +00005216
cristybb503372010-05-27 20:51:26 +00005217 MagickStatusType
5218 flags;
5219
5220 ssize_t
5221 y;
5222
cristy3ed852e2009-09-05 21:47:34 +00005223 /*
5224 Allocate tint image.
5225 */
5226 assert(image != (const Image *) NULL);
5227 assert(image->signature == MagickSignature);
5228 if (image->debug != MagickFalse)
5229 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5230 assert(exception != (ExceptionInfo *) NULL);
5231 assert(exception->signature == MagickSignature);
5232 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5233 if (tint_image == (Image *) NULL)
5234 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005235 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005236 {
cristy3ed852e2009-09-05 21:47:34 +00005237 tint_image=DestroyImage(tint_image);
5238 return((Image *) NULL);
5239 }
cristyaed9c382011-10-03 17:54:21 +00005240 if (blend == (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005241 return(tint_image);
5242 /*
5243 Determine RGB values of the color.
5244 */
cristy1707c6c2012-01-18 23:30:54 +00005245 GetPixelInfo(image,&color_vector);
cristyb817c3f2011-10-03 14:00:35 +00005246 flags=ParseGeometry(blend,&geometry_info);
cristy1707c6c2012-01-18 23:30:54 +00005247 color_vector.red=geometry_info.rho;
5248 color_vector.green=geometry_info.rho;
5249 color_vector.blue=geometry_info.rho;
cristy92ac5722013-03-09 15:09:38 +00005250 color_vector.alpha=(MagickRealType) OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00005251 if ((flags & SigmaValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005252 color_vector.green=geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +00005253 if ((flags & XiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005254 color_vector.blue=geometry_info.xi;
cristyb817c3f2011-10-03 14:00:35 +00005255 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005256 color_vector.alpha=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005257 if (image->colorspace == CMYKColorspace)
5258 {
cristy1707c6c2012-01-18 23:30:54 +00005259 color_vector.black=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005260 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005261 color_vector.black=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005262 if ((flags & ChiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005263 color_vector.alpha=geometry_info.chi;
cristy76f512e2011-09-12 01:26:56 +00005264 }
cristya19f1d72012-08-07 18:24:38 +00005265 intensity=(double) GetPixelInfoIntensity(tint);
5266 color_vector.red=(double) (color_vector.red*tint->red/100.0-
cristy1707c6c2012-01-18 23:30:54 +00005267 intensity);
cristya19f1d72012-08-07 18:24:38 +00005268 color_vector.green=(double) (color_vector.green*tint->green/100.0-
cristy1707c6c2012-01-18 23:30:54 +00005269 intensity);
cristya19f1d72012-08-07 18:24:38 +00005270 color_vector.blue=(double) (color_vector.blue*tint->blue/100.0-
cristy1707c6c2012-01-18 23:30:54 +00005271 intensity);
cristya19f1d72012-08-07 18:24:38 +00005272 color_vector.black=(double) (color_vector.black*tint->black/100.0-
cristy1707c6c2012-01-18 23:30:54 +00005273 intensity);
cristya19f1d72012-08-07 18:24:38 +00005274 color_vector.alpha=(double) (color_vector.alpha*tint->alpha/100.0-
cristy1707c6c2012-01-18 23:30:54 +00005275 intensity);
cristy3ed852e2009-09-05 21:47:34 +00005276 /*
5277 Tint image.
5278 */
5279 status=MagickTrue;
5280 progress=0;
cristy46ff2672012-12-14 15:32:26 +00005281 image_view=AcquireVirtualCacheView(image,exception);
5282 tint_view=AcquireAuthenticCacheView(tint_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00005283#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005284 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00005285 magick_threads(image,tint_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005286#endif
cristybb503372010-05-27 20:51:26 +00005287 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005288 {
cristy4c08aed2011-07-01 19:47:50 +00005289 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00005290 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00005291
cristy4c08aed2011-07-01 19:47:50 +00005292 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005293 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005294
cristy6b91acb2011-04-19 12:23:54 +00005295 register ssize_t
5296 x;
5297
cristy3ed852e2009-09-05 21:47:34 +00005298 if (status == MagickFalse)
5299 continue;
5300 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5301 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5302 exception);
cristy4c08aed2011-07-01 19:47:50 +00005303 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005304 {
5305 status=MagickFalse;
5306 continue;
5307 }
cristybb503372010-05-27 20:51:26 +00005308 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005309 {
cristy4c08aed2011-07-01 19:47:50 +00005310 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00005311 pixel;
5312
cristya19f1d72012-08-07 18:24:38 +00005313 double
cristy3ed852e2009-09-05 21:47:34 +00005314 weight;
5315
cristy1707c6c2012-01-18 23:30:54 +00005316 register ssize_t
5317 i;
5318
5319 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5320 {
cristy5a23c552013-02-13 14:34:28 +00005321 PixelChannel channel=GetPixelChannelChannel(image,i);
5322 PixelTrait traits=GetPixelChannelTraits(image,channel);
5323 PixelTrait tint_traits=GetPixelChannelTraits(tint_image,channel);
cristy1707c6c2012-01-18 23:30:54 +00005324 if ((traits == UndefinedPixelTrait) ||
5325 (tint_traits == UndefinedPixelTrait))
5326 continue;
cristy1eced092012-08-10 23:10:56 +00005327 if (((tint_traits & CopyPixelTrait) != 0) ||
5328 (GetPixelMask(image,p) != 0))
cristy1707c6c2012-01-18 23:30:54 +00005329 {
5330 SetPixelChannel(tint_image,channel,p[i],q);
5331 continue;
5332 }
5333 }
5334 GetPixelInfo(image,&pixel);
5335 weight=QuantumScale*GetPixelRed(image,p)-0.5;
cristy1eced092012-08-10 23:10:56 +00005336 pixel.red=(double) GetPixelRed(image,p)+color_vector.red*(1.0-(4.0*
5337 (weight*weight)));
cristy1707c6c2012-01-18 23:30:54 +00005338 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
cristy1eced092012-08-10 23:10:56 +00005339 pixel.green=(double) GetPixelGreen(image,p)+color_vector.green*(1.0-(4.0*
5340 (weight*weight)));
cristy1707c6c2012-01-18 23:30:54 +00005341 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
cristy1eced092012-08-10 23:10:56 +00005342 pixel.blue=(double) GetPixelBlue(image,p)+color_vector.blue*(1.0-(4.0*
5343 (weight*weight)));
cristy1707c6c2012-01-18 23:30:54 +00005344 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
cristy1eced092012-08-10 23:10:56 +00005345 pixel.black=(double) GetPixelBlack(image,p)+color_vector.black*(1.0-(4.0*
5346 (weight*weight)));
cristy1707c6c2012-01-18 23:30:54 +00005347 SetPixelInfoPixel(tint_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00005348 p+=GetPixelChannels(image);
5349 q+=GetPixelChannels(tint_image);
cristy3ed852e2009-09-05 21:47:34 +00005350 }
5351 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5352 status=MagickFalse;
5353 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5354 {
5355 MagickBooleanType
5356 proceed;
5357
cristyb5d5f722009-11-04 03:03:49 +00005358#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005359 #pragma omp critical (MagickCore_TintImage)
cristy3ed852e2009-09-05 21:47:34 +00005360#endif
5361 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5362 if (proceed == MagickFalse)
5363 status=MagickFalse;
5364 }
5365 }
5366 tint_view=DestroyCacheView(tint_view);
5367 image_view=DestroyCacheView(image_view);
5368 if (status == MagickFalse)
5369 tint_image=DestroyImage(tint_image);
5370 return(tint_image);
5371}
5372
5373/*
5374%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5375% %
5376% %
5377% %
5378% V i g n e t t e I m a g e %
5379% %
5380% %
5381% %
5382%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5383%
5384% VignetteImage() softens the edges of the image in vignette style.
5385%
5386% The format of the VignetteImage method is:
5387%
5388% Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005389% const double sigma,const ssize_t x,const ssize_t y,
cristy05c0c9a2011-09-05 23:16:13 +00005390% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005391%
5392% A description of each parameter follows:
5393%
5394% o image: the image.
5395%
5396% o radius: the radius of the pixel neighborhood.
5397%
5398% o sigma: the standard deviation of the Gaussian, in pixels.
5399%
5400% o x, y: Define the x and y ellipse offset.
5401%
5402% o exception: return any errors or warnings in this structure.
5403%
5404*/
5405MagickExport Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005406 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005407{
5408 char
5409 ellipse[MaxTextExtent];
5410
5411 DrawInfo
5412 *draw_info;
5413
5414 Image
5415 *canvas_image,
5416 *blur_image,
5417 *oval_image,
5418 *vignette_image;
5419
5420 assert(image != (Image *) NULL);
5421 assert(image->signature == MagickSignature);
5422 if (image->debug != MagickFalse)
5423 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5424 assert(exception != (ExceptionInfo *) NULL);
5425 assert(exception->signature == MagickSignature);
5426 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5427 if (canvas_image == (Image *) NULL)
5428 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005429 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005430 {
cristy3ed852e2009-09-05 21:47:34 +00005431 canvas_image=DestroyImage(canvas_image);
5432 return((Image *) NULL);
5433 }
cristy8a46d822012-08-28 23:32:39 +00005434 canvas_image->alpha_trait=BlendPixelTrait;
cristy98621462011-12-31 22:31:11 +00005435 oval_image=CloneImage(canvas_image,canvas_image->columns,canvas_image->rows,
5436 MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005437 if (oval_image == (Image *) NULL)
5438 {
5439 canvas_image=DestroyImage(canvas_image);
5440 return((Image *) NULL);
5441 }
cristy9950d572011-10-01 18:22:35 +00005442 (void) QueryColorCompliance("#000000",AllCompliance,
5443 &oval_image->background_color,exception);
cristyea1a8aa2011-10-20 13:24:06 +00005444 (void) SetImageBackgroundColor(oval_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00005445 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
cristy9950d572011-10-01 18:22:35 +00005446 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5447 exception);
5448 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5449 exception);
cristy1707c6c2012-01-18 23:30:54 +00005450 (void) FormatLocaleString(ellipse,MaxTextExtent,"ellipse %g,%g,%g,%g,"
5451 "0.0,360.0",image->columns/2.0,image->rows/2.0,image->columns/2.0-x,
5452 image->rows/2.0-y);
cristy3ed852e2009-09-05 21:47:34 +00005453 draw_info->primitive=AcquireString(ellipse);
cristy018f07f2011-09-04 21:15:19 +00005454 (void) DrawImage(oval_image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00005455 draw_info=DestroyDrawInfo(draw_info);
cristyaa2c16c2012-03-25 22:21:35 +00005456 blur_image=BlurImage(oval_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00005457 oval_image=DestroyImage(oval_image);
5458 if (blur_image == (Image *) NULL)
5459 {
5460 canvas_image=DestroyImage(canvas_image);
5461 return((Image *) NULL);
5462 }
cristy8a46d822012-08-28 23:32:39 +00005463 blur_image->alpha_trait=UndefinedPixelTrait;
cristy39172402012-03-30 13:04:39 +00005464 (void) CompositeImage(canvas_image,blur_image,IntensityCompositeOp,MagickTrue,
5465 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00005466 blur_image=DestroyImage(blur_image);
5467 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5468 canvas_image=DestroyImage(canvas_image);
cristy66d26122012-06-23 21:56:40 +00005469 if (vignette_image != (Image *) NULL)
5470 (void) TransformImageColorspace(vignette_image,image->colorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00005471 return(vignette_image);
5472}
5473
5474/*
5475%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5476% %
5477% %
5478% %
5479% W a v e I m a g e %
5480% %
5481% %
5482% %
5483%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5484%
5485% WaveImage() creates a "ripple" effect in the image by shifting the pixels
cristycee97112010-05-28 00:44:52 +00005486% vertically along a sine wave whose amplitude and wavelength is specified
cristy3ed852e2009-09-05 21:47:34 +00005487% by the given parameters.
5488%
5489% The format of the WaveImage method is:
5490%
5491% Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005492% const double wave_length,const PixelInterpolateMethod method,
5493% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005494%
5495% A description of each parameter follows:
5496%
5497% o image: the image.
5498%
5499% o amplitude, wave_length: Define the amplitude and wave length of the
5500% sine wave.
5501%
cristy5c4e2582011-09-11 19:21:03 +00005502% o interpolate: the pixel interpolation method.
5503%
cristy3ed852e2009-09-05 21:47:34 +00005504% o exception: return any errors or warnings in this structure.
5505%
5506*/
5507MagickExport Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005508 const double wave_length,const PixelInterpolateMethod method,
5509 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005510{
5511#define WaveImageTag "Wave/Image"
5512
cristyfa112112010-01-04 17:48:07 +00005513 CacheView
cristyd76c51e2011-03-26 00:21:26 +00005514 *image_view,
cristyfa112112010-01-04 17:48:07 +00005515 *wave_view;
5516
cristy3ed852e2009-09-05 21:47:34 +00005517 Image
5518 *wave_image;
5519
cristy3ed852e2009-09-05 21:47:34 +00005520 MagickBooleanType
5521 status;
5522
cristybb503372010-05-27 20:51:26 +00005523 MagickOffsetType
5524 progress;
5525
cristya19f1d72012-08-07 18:24:38 +00005526 double
cristy3ed852e2009-09-05 21:47:34 +00005527 *sine_map;
5528
cristybb503372010-05-27 20:51:26 +00005529 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005530 i;
5531
cristybb503372010-05-27 20:51:26 +00005532 ssize_t
5533 y;
5534
cristy3ed852e2009-09-05 21:47:34 +00005535 /*
5536 Initialize wave image attributes.
5537 */
5538 assert(image != (Image *) NULL);
5539 assert(image->signature == MagickSignature);
5540 if (image->debug != MagickFalse)
5541 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5542 assert(exception != (ExceptionInfo *) NULL);
5543 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00005544 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
cristy3ed852e2009-09-05 21:47:34 +00005545 fabs(amplitude)),MagickTrue,exception);
5546 if (wave_image == (Image *) NULL)
5547 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005548 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005549 {
cristy3ed852e2009-09-05 21:47:34 +00005550 wave_image=DestroyImage(wave_image);
5551 return((Image *) NULL);
5552 }
cristy4c08aed2011-07-01 19:47:50 +00005553 if (wave_image->background_color.alpha != OpaqueAlpha)
cristy8a46d822012-08-28 23:32:39 +00005554 wave_image->alpha_trait=BlendPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +00005555 /*
5556 Allocate sine map.
5557 */
cristya19f1d72012-08-07 18:24:38 +00005558 sine_map=(double *) AcquireQuantumMemory((size_t) wave_image->columns,
cristy3ed852e2009-09-05 21:47:34 +00005559 sizeof(*sine_map));
cristya19f1d72012-08-07 18:24:38 +00005560 if (sine_map == (double *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005561 {
5562 wave_image=DestroyImage(wave_image);
5563 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5564 }
cristybb503372010-05-27 20:51:26 +00005565 for (i=0; i < (ssize_t) wave_image->columns; i++)
cristy4205a3c2010-09-12 20:19:59 +00005566 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5567 wave_length));
cristy3ed852e2009-09-05 21:47:34 +00005568 /*
5569 Wave image.
5570 */
5571 status=MagickTrue;
5572 progress=0;
cristy46ff2672012-12-14 15:32:26 +00005573 image_view=AcquireVirtualCacheView(image,exception);
5574 wave_view=AcquireAuthenticCacheView(wave_image,exception);
cristyd76c51e2011-03-26 00:21:26 +00005575 (void) SetCacheViewVirtualPixelMethod(image_view,
5576 BackgroundVirtualPixelMethod);
cristyb5d5f722009-11-04 03:03:49 +00005577#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005578 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00005579 magick_threads(image,wave_image,wave_image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005580#endif
cristybb503372010-05-27 20:51:26 +00005581 for (y=0; y < (ssize_t) wave_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005582 {
cristy4c08aed2011-07-01 19:47:50 +00005583 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005584 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005585
cristye97bb922011-04-03 01:36:52 +00005586 register ssize_t
5587 x;
5588
cristy3ed852e2009-09-05 21:47:34 +00005589 if (status == MagickFalse)
5590 continue;
5591 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5592 exception);
cristyacd2ed22011-08-30 01:44:23 +00005593 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005594 {
5595 status=MagickFalse;
5596 continue;
5597 }
cristybb503372010-05-27 20:51:26 +00005598 for (x=0; x < (ssize_t) wave_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005599 {
cristy5c4e2582011-09-11 19:21:03 +00005600 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5601 (double) x,(double) (y-sine_map[x]),q,exception);
cristyed231572011-07-14 02:18:59 +00005602 q+=GetPixelChannels(wave_image);
cristy3ed852e2009-09-05 21:47:34 +00005603 }
5604 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5605 status=MagickFalse;
5606 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5607 {
5608 MagickBooleanType
5609 proceed;
5610
cristyb5d5f722009-11-04 03:03:49 +00005611#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005612 #pragma omp critical (MagickCore_WaveImage)
cristy3ed852e2009-09-05 21:47:34 +00005613#endif
5614 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5615 if (proceed == MagickFalse)
5616 status=MagickFalse;
5617 }
5618 }
5619 wave_view=DestroyCacheView(wave_view);
cristyd76c51e2011-03-26 00:21:26 +00005620 image_view=DestroyCacheView(image_view);
cristya19f1d72012-08-07 18:24:38 +00005621 sine_map=(double *) RelinquishMagickMemory(sine_map);
cristy3ed852e2009-09-05 21:47:34 +00005622 if (status == MagickFalse)
5623 wave_image=DestroyImage(wave_image);
5624 return(wave_image);
5625}