blob: ce918687af3dc9f7ef6ac8dbedee3f435740e470 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% FFFFF X X %
7% F X X %
8% FFF X %
9% F X X %
10% F X X %
11% %
12% %
13% MagickCore Image Special Effects Methods %
14% %
15% Software Design %
16% John Cristy %
17% October 1996 %
18% %
19% %
cristy45ef08f2012-12-07 13:13:34 +000020% Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37%
38*/
39
40/*
41 Include declarations.
42*/
cristy4c08aed2011-07-01 19:47:50 +000043#include "MagickCore/studio.h"
44#include "MagickCore/annotate.h"
45#include "MagickCore/artifact.h"
46#include "MagickCore/attribute.h"
47#include "MagickCore/cache.h"
48#include "MagickCore/cache-view.h"
49#include "MagickCore/color.h"
50#include "MagickCore/color-private.h"
cristy9b7a4fc2012-04-08 22:26:56 +000051#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000052#include "MagickCore/composite.h"
53#include "MagickCore/decorate.h"
cristyc53413d2011-11-17 13:04:26 +000054#include "MagickCore/distort.h"
cristy4c08aed2011-07-01 19:47:50 +000055#include "MagickCore/draw.h"
56#include "MagickCore/effect.h"
57#include "MagickCore/enhance.h"
58#include "MagickCore/exception.h"
59#include "MagickCore/exception-private.h"
60#include "MagickCore/fx.h"
61#include "MagickCore/fx-private.h"
62#include "MagickCore/gem.h"
cristy8ea81222011-09-04 10:33:32 +000063#include "MagickCore/gem-private.h"
cristy4c08aed2011-07-01 19:47:50 +000064#include "MagickCore/geometry.h"
65#include "MagickCore/layer.h"
66#include "MagickCore/list.h"
67#include "MagickCore/log.h"
68#include "MagickCore/image.h"
69#include "MagickCore/image-private.h"
70#include "MagickCore/magick.h"
71#include "MagickCore/memory_.h"
72#include "MagickCore/monitor.h"
73#include "MagickCore/monitor-private.h"
74#include "MagickCore/option.h"
75#include "MagickCore/pixel.h"
76#include "MagickCore/pixel-accessor.h"
77#include "MagickCore/property.h"
78#include "MagickCore/quantum.h"
79#include "MagickCore/quantum-private.h"
80#include "MagickCore/random_.h"
81#include "MagickCore/random-private.h"
82#include "MagickCore/resample.h"
83#include "MagickCore/resample-private.h"
84#include "MagickCore/resize.h"
cristy57340e02012-05-05 00:53:23 +000085#include "MagickCore/resource_.h"
cristy4c08aed2011-07-01 19:47:50 +000086#include "MagickCore/splay-tree.h"
87#include "MagickCore/statistic.h"
88#include "MagickCore/string_.h"
89#include "MagickCore/string-private.h"
90#include "MagickCore/thread-private.h"
91#include "MagickCore/transform.h"
92#include "MagickCore/utility.h"
cristy3ed852e2009-09-05 21:47:34 +000093
94/*
95 Define declarations.
96*/
97#define LeftShiftOperator 0xf5
98#define RightShiftOperator 0xf6
99#define LessThanEqualOperator 0xf7
100#define GreaterThanEqualOperator 0xf8
101#define EqualOperator 0xf9
102#define NotEqualOperator 0xfa
103#define LogicalAndOperator 0xfb
104#define LogicalOrOperator 0xfc
cristy116af162010-08-13 01:25:47 +0000105#define ExponentialNotation 0xfd
cristy3ed852e2009-09-05 21:47:34 +0000106
107struct _FxInfo
108{
109 const Image
110 *images;
111
cristy3ed852e2009-09-05 21:47:34 +0000112 char
113 *expression;
114
115 FILE
116 *file;
117
118 SplayTreeInfo
119 *colors,
120 *symbols;
121
cristyd76c51e2011-03-26 00:21:26 +0000122 CacheView
123 **view;
cristy3ed852e2009-09-05 21:47:34 +0000124
125 RandomInfo
126 *random_info;
127
128 ExceptionInfo
129 *exception;
130};
131
132/*
133%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
134% %
135% %
136% %
137+ A c q u i r e F x I n f o %
138% %
139% %
140% %
141%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142%
143% AcquireFxInfo() allocates the FxInfo structure.
144%
145% The format of the AcquireFxInfo method is:
146%
cristydb070952012-04-20 14:33:00 +0000147% FxInfo *AcquireFxInfo(Image *image,const char *expression,
148% ExceptionInfo *exception)
cristy0a9b3722010-10-23 18:45:49 +0000149%
cristy3ed852e2009-09-05 21:47:34 +0000150% A description of each parameter follows:
151%
152% o image: the image.
153%
154% o expression: the expression.
155%
cristydb070952012-04-20 14:33:00 +0000156% o exception: return any errors or warnings in this structure.
157%
cristy3ed852e2009-09-05 21:47:34 +0000158*/
cristydb070952012-04-20 14:33:00 +0000159MagickPrivate FxInfo *AcquireFxInfo(const Image *image,const char *expression,
160 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000161{
162 char
163 fx_op[2];
164
cristycb180922011-03-11 14:41:24 +0000165 const Image
166 *next;
167
cristy3ed852e2009-09-05 21:47:34 +0000168 FxInfo
169 *fx_info;
170
cristybb503372010-05-27 20:51:26 +0000171 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000172 i;
173
cristy73bd4a52010-10-05 11:24:23 +0000174 fx_info=(FxInfo *) AcquireMagickMemory(sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +0000175 if (fx_info == (FxInfo *) NULL)
176 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
177 (void) ResetMagickMemory(fx_info,0,sizeof(*fx_info));
178 fx_info->exception=AcquireExceptionInfo();
anthony7d86e172011-03-23 12:37:06 +0000179 fx_info->images=image;
cristy3ed852e2009-09-05 21:47:34 +0000180 fx_info->colors=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
181 RelinquishMagickMemory);
182 fx_info->symbols=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
183 RelinquishMagickMemory);
cristyd76c51e2011-03-26 00:21:26 +0000184 fx_info->view=(CacheView **) AcquireQuantumMemory(GetImageListLength(
185 fx_info->images),sizeof(*fx_info->view));
186 if (fx_info->view == (CacheView **) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000187 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
cristya2262262011-03-11 02:50:37 +0000188 i=0;
cristy0ea377f2011-03-24 00:54:19 +0000189 next=GetFirstImageInList(fx_info->images);
190 for ( ; next != (Image *) NULL; next=next->next)
cristy3ed852e2009-09-05 21:47:34 +0000191 {
cristy46ff2672012-12-14 15:32:26 +0000192 fx_info->view[i]=AcquireVirtualCacheView(next,exception);
cristya2262262011-03-11 02:50:37 +0000193 i++;
cristy3ed852e2009-09-05 21:47:34 +0000194 }
195 fx_info->random_info=AcquireRandomInfo();
196 fx_info->expression=ConstantString(expression);
197 fx_info->file=stderr;
198 (void) SubstituteString(&fx_info->expression," ",""); /* compact string */
cristy37af0912011-05-23 16:09:42 +0000199 /*
200 Force right-to-left associativity for unary negation.
201 */
202 (void) SubstituteString(&fx_info->expression,"-","-1.0*");
cristy422d5502012-12-22 22:20:57 +0000203 (void) SubstituteString(&fx_info->expression,"^-1.0*","^-");
cristy8fbb96d2012-07-18 01:18:52 +0000204 (void) SubstituteString(&fx_info->expression,"E-1.0*","E-");
205 (void) SubstituteString(&fx_info->expression,"e-1.0*","e-");
cristy8b8a3ae2010-10-23 18:49:46 +0000206 /*
cristy3ed852e2009-09-05 21:47:34 +0000207 Convert complex to simple operators.
208 */
209 fx_op[1]='\0';
210 *fx_op=(char) LeftShiftOperator;
211 (void) SubstituteString(&fx_info->expression,"<<",fx_op);
212 *fx_op=(char) RightShiftOperator;
213 (void) SubstituteString(&fx_info->expression,">>",fx_op);
214 *fx_op=(char) LessThanEqualOperator;
215 (void) SubstituteString(&fx_info->expression,"<=",fx_op);
216 *fx_op=(char) GreaterThanEqualOperator;
217 (void) SubstituteString(&fx_info->expression,">=",fx_op);
218 *fx_op=(char) EqualOperator;
219 (void) SubstituteString(&fx_info->expression,"==",fx_op);
220 *fx_op=(char) NotEqualOperator;
221 (void) SubstituteString(&fx_info->expression,"!=",fx_op);
222 *fx_op=(char) LogicalAndOperator;
223 (void) SubstituteString(&fx_info->expression,"&&",fx_op);
224 *fx_op=(char) LogicalOrOperator;
225 (void) SubstituteString(&fx_info->expression,"||",fx_op);
cristy116af162010-08-13 01:25:47 +0000226 *fx_op=(char) ExponentialNotation;
227 (void) SubstituteString(&fx_info->expression,"**",fx_op);
cristy3ed852e2009-09-05 21:47:34 +0000228 return(fx_info);
229}
230
231/*
232%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233% %
234% %
235% %
236% A d d N o i s e I m a g e %
237% %
238% %
239% %
240%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241%
242% AddNoiseImage() adds random noise to the image.
243%
244% The format of the AddNoiseImage method is:
245%
246% Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
cristy9ed1f812011-10-08 02:00:08 +0000247% const double attenuate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000248%
249% A description of each parameter follows:
250%
251% o image: the image.
252%
253% o channel: the channel type.
254%
255% o noise_type: The type of noise: Uniform, Gaussian, Multiplicative,
256% Impulse, Laplacian, or Poisson.
257%
cristy9ed1f812011-10-08 02:00:08 +0000258% o attenuate: attenuate the random distribution.
259%
cristy3ed852e2009-09-05 21:47:34 +0000260% o exception: return any errors or warnings in this structure.
261%
262*/
cristy9ed1f812011-10-08 02:00:08 +0000263MagickExport Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
264 const double attenuate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000265{
266#define AddNoiseImageTag "AddNoise/Image"
267
cristyfa112112010-01-04 17:48:07 +0000268 CacheView
269 *image_view,
270 *noise_view;
271
cristy3ed852e2009-09-05 21:47:34 +0000272 Image
273 *noise_image;
274
cristy3ed852e2009-09-05 21:47:34 +0000275 MagickBooleanType
276 status;
277
cristybb503372010-05-27 20:51:26 +0000278 MagickOffsetType
279 progress;
280
cristy3ed852e2009-09-05 21:47:34 +0000281 RandomInfo
cristyfa112112010-01-04 17:48:07 +0000282 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +0000283
cristybb503372010-05-27 20:51:26 +0000284 ssize_t
285 y;
286
glennrpf7659d72012-09-24 18:14:56 +0000287#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +0000288 unsigned long
289 key;
glennrpf7659d72012-09-24 18:14:56 +0000290#endif
cristy57340e02012-05-05 00:53:23 +0000291
cristy3ed852e2009-09-05 21:47:34 +0000292 /*
293 Initialize noise image attributes.
294 */
295 assert(image != (const Image *) NULL);
296 assert(image->signature == MagickSignature);
297 if (image->debug != MagickFalse)
298 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
299 assert(exception != (ExceptionInfo *) NULL);
300 assert(exception->signature == MagickSignature);
cristyb2145892011-10-10 00:55:32 +0000301 noise_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000302 if (noise_image == (Image *) NULL)
303 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000304 if (SetImageStorageClass(noise_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000305 {
cristy3ed852e2009-09-05 21:47:34 +0000306 noise_image=DestroyImage(noise_image);
307 return((Image *) NULL);
308 }
309 /*
310 Add noise in each row.
311 */
cristy3ed852e2009-09-05 21:47:34 +0000312 status=MagickTrue;
313 progress=0;
314 random_info=AcquireRandomInfoThreadSet();
cristy46ff2672012-12-14 15:32:26 +0000315 image_view=AcquireVirtualCacheView(image,exception);
316 noise_view=AcquireAuthenticCacheView(noise_image,exception);
cristy319a1e72010-02-21 15:13:11 +0000317#if defined(MAGICKCORE_OPENMP_SUPPORT)
glennrpf7659d72012-09-24 18:14:56 +0000318 key=GetRandomSecretKey(random_info[0]);
cristy57340e02012-05-05 00:53:23 +0000319 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +0000320 magick_threads(image,noise_image,image->rows,key == ~0UL)
cristy3ed852e2009-09-05 21:47:34 +0000321#endif
cristybb503372010-05-27 20:51:26 +0000322 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000323 {
cristy5c9e6f22010-09-17 17:31:01 +0000324 const int
325 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +0000326
cristy3ed852e2009-09-05 21:47:34 +0000327 MagickBooleanType
328 sync;
329
cristy4c08aed2011-07-01 19:47:50 +0000330 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000331 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000332
cristybb503372010-05-27 20:51:26 +0000333 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000334 x;
335
cristy4c08aed2011-07-01 19:47:50 +0000336 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000337 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000338
339 if (status == MagickFalse)
340 continue;
341 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +0000342 q=QueueCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +0000343 exception);
cristy4c08aed2011-07-01 19:47:50 +0000344 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000345 {
346 status=MagickFalse;
347 continue;
348 }
cristybb503372010-05-27 20:51:26 +0000349 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000350 {
cristy850b3072011-10-08 01:38:05 +0000351 register ssize_t
352 i;
353
354 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
355 {
cristy5a23c552013-02-13 14:34:28 +0000356 PixelChannel channel=GetPixelChannelChannel(image,i);
357 PixelTrait traits=GetPixelChannelTraits(image,channel);
358 PixelTrait noise_traits=GetPixelChannelTraits(noise_image,channel);
cristy850b3072011-10-08 01:38:05 +0000359 if ((traits == UndefinedPixelTrait) ||
360 (noise_traits == UndefinedPixelTrait))
361 continue;
cristy1eced092012-08-10 23:10:56 +0000362 if (((noise_traits & CopyPixelTrait) != 0) ||
cristy883fde12013-04-08 00:50:13 +0000363 (GetPixelReadMask(image,p) == 0))
cristyb2145892011-10-10 00:55:32 +0000364 {
365 SetPixelChannel(noise_image,channel,p[i],q);
366 continue;
367 }
cristy850b3072011-10-08 01:38:05 +0000368 SetPixelChannel(noise_image,channel,ClampToQuantum(
369 GenerateDifferentialNoise(random_info[id],p[i],noise_type,attenuate)),
370 q);
371 }
cristyed231572011-07-14 02:18:59 +0000372 p+=GetPixelChannels(image);
373 q+=GetPixelChannels(noise_image);
cristy3ed852e2009-09-05 21:47:34 +0000374 }
375 sync=SyncCacheViewAuthenticPixels(noise_view,exception);
376 if (sync == MagickFalse)
377 status=MagickFalse;
378 if (image->progress_monitor != (MagickProgressMonitor) NULL)
379 {
380 MagickBooleanType
381 proceed;
382
cristyb5d5f722009-11-04 03:03:49 +0000383#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy319a1e72010-02-21 15:13:11 +0000384 #pragma omp critical (MagickCore_AddNoiseImage)
cristy3ed852e2009-09-05 21:47:34 +0000385#endif
386 proceed=SetImageProgress(image,AddNoiseImageTag,progress++,
387 image->rows);
388 if (proceed == MagickFalse)
389 status=MagickFalse;
390 }
391 }
392 noise_view=DestroyCacheView(noise_view);
393 image_view=DestroyCacheView(image_view);
394 random_info=DestroyRandomInfoThreadSet(random_info);
395 if (status == MagickFalse)
396 noise_image=DestroyImage(noise_image);
397 return(noise_image);
398}
399
400/*
401%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
402% %
403% %
404% %
405% B l u e S h i f t I m a g e %
406% %
407% %
408% %
409%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
410%
411% BlueShiftImage() mutes the colors of the image to simulate a scene at
412% nighttime in the moonlight.
413%
414% The format of the BlueShiftImage method is:
415%
416% Image *BlueShiftImage(const Image *image,const double factor,
417% ExceptionInfo *exception)
418%
419% A description of each parameter follows:
420%
421% o image: the image.
422%
423% o factor: the shift factor.
424%
425% o exception: return any errors or warnings in this structure.
426%
427*/
428MagickExport Image *BlueShiftImage(const Image *image,const double factor,
429 ExceptionInfo *exception)
430{
431#define BlueShiftImageTag "BlueShift/Image"
432
cristyc4c8d132010-01-07 01:58:38 +0000433 CacheView
434 *image_view,
435 *shift_view;
436
cristy3ed852e2009-09-05 21:47:34 +0000437 Image
438 *shift_image;
439
cristy3ed852e2009-09-05 21:47:34 +0000440 MagickBooleanType
441 status;
442
cristybb503372010-05-27 20:51:26 +0000443 MagickOffsetType
444 progress;
445
446 ssize_t
447 y;
448
cristy3ed852e2009-09-05 21:47:34 +0000449 /*
450 Allocate blue shift image.
451 */
452 assert(image != (const Image *) NULL);
453 assert(image->signature == MagickSignature);
454 if (image->debug != MagickFalse)
455 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
456 assert(exception != (ExceptionInfo *) NULL);
457 assert(exception->signature == MagickSignature);
cristya6d7a9b2012-01-18 20:04:48 +0000458 shift_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000459 if (shift_image == (Image *) NULL)
460 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000461 if (SetImageStorageClass(shift_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000462 {
cristy3ed852e2009-09-05 21:47:34 +0000463 shift_image=DestroyImage(shift_image);
464 return((Image *) NULL);
465 }
466 /*
467 Blue-shift DirectClass image.
468 */
469 status=MagickTrue;
470 progress=0;
cristy46ff2672012-12-14 15:32:26 +0000471 image_view=AcquireVirtualCacheView(image,exception);
472 shift_view=AcquireAuthenticCacheView(shift_image,exception);
cristy319a1e72010-02-21 15:13:11 +0000473#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000474 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +0000475 magick_threads(image,shift_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000476#endif
cristybb503372010-05-27 20:51:26 +0000477 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000478 {
479 MagickBooleanType
480 sync;
481
cristy4c08aed2011-07-01 19:47:50 +0000482 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000483 pixel;
484
485 Quantum
486 quantum;
487
cristy4c08aed2011-07-01 19:47:50 +0000488 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000489 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000490
cristybb503372010-05-27 20:51:26 +0000491 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000492 x;
493
cristy4c08aed2011-07-01 19:47:50 +0000494 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000495 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000496
497 if (status == MagickFalse)
498 continue;
499 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
500 q=QueueCacheViewAuthenticPixels(shift_view,0,y,shift_image->columns,1,
501 exception);
cristy4c08aed2011-07-01 19:47:50 +0000502 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000503 {
504 status=MagickFalse;
505 continue;
506 }
cristybb503372010-05-27 20:51:26 +0000507 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000508 {
cristy4c08aed2011-07-01 19:47:50 +0000509 quantum=GetPixelRed(image,p);
510 if (GetPixelGreen(image,p) < quantum)
511 quantum=GetPixelGreen(image,p);
512 if (GetPixelBlue(image,p) < quantum)
513 quantum=GetPixelBlue(image,p);
514 pixel.red=0.5*(GetPixelRed(image,p)+factor*quantum);
515 pixel.green=0.5*(GetPixelGreen(image,p)+factor*quantum);
516 pixel.blue=0.5*(GetPixelBlue(image,p)+factor*quantum);
517 quantum=GetPixelRed(image,p);
518 if (GetPixelGreen(image,p) > quantum)
519 quantum=GetPixelGreen(image,p);
520 if (GetPixelBlue(image,p) > quantum)
521 quantum=GetPixelBlue(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000522 pixel.red=0.5*(pixel.red+factor*quantum);
523 pixel.green=0.5*(pixel.green+factor*quantum);
524 pixel.blue=0.5*(pixel.blue+factor*quantum);
cristy4c08aed2011-07-01 19:47:50 +0000525 SetPixelRed(shift_image,ClampToQuantum(pixel.red),q);
526 SetPixelGreen(shift_image,ClampToQuantum(pixel.green),q);
527 SetPixelBlue(shift_image,ClampToQuantum(pixel.blue),q);
cristyed231572011-07-14 02:18:59 +0000528 p+=GetPixelChannels(image);
529 q+=GetPixelChannels(shift_image);
cristy3ed852e2009-09-05 21:47:34 +0000530 }
531 sync=SyncCacheViewAuthenticPixels(shift_view,exception);
532 if (sync == MagickFalse)
533 status=MagickFalse;
534 if (image->progress_monitor != (MagickProgressMonitor) NULL)
535 {
536 MagickBooleanType
537 proceed;
538
cristy319a1e72010-02-21 15:13:11 +0000539#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +0000540 #pragma omp critical (MagickCore_BlueShiftImage)
cristy3ed852e2009-09-05 21:47:34 +0000541#endif
542 proceed=SetImageProgress(image,BlueShiftImageTag,progress++,
543 image->rows);
544 if (proceed == MagickFalse)
545 status=MagickFalse;
546 }
547 }
548 image_view=DestroyCacheView(image_view);
549 shift_view=DestroyCacheView(shift_view);
550 if (status == MagickFalse)
551 shift_image=DestroyImage(shift_image);
552 return(shift_image);
553}
554
555/*
556%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
557% %
558% %
559% %
560% C h a r c o a l I m a g e %
561% %
562% %
563% %
564%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
565%
566% CharcoalImage() creates a new image that is a copy of an existing one with
567% the edge highlighted. It allocates the memory necessary for the new Image
568% structure and returns a pointer to the new image.
569%
570% The format of the CharcoalImage method is:
571%
572% Image *CharcoalImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +0000573% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000574%
575% A description of each parameter follows:
576%
577% o image: the image.
578%
579% o radius: the radius of the pixel neighborhood.
580%
581% o sigma: the standard deviation of the Gaussian, in pixels.
582%
583% o exception: return any errors or warnings in this structure.
584%
585*/
586MagickExport Image *CharcoalImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +0000587 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000588{
589 Image
590 *charcoal_image,
591 *clone_image,
592 *edge_image;
593
594 assert(image != (Image *) NULL);
595 assert(image->signature == MagickSignature);
596 if (image->debug != MagickFalse)
597 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
598 assert(exception != (ExceptionInfo *) NULL);
599 assert(exception->signature == MagickSignature);
600 clone_image=CloneImage(image,0,0,MagickTrue,exception);
601 if (clone_image == (Image *) NULL)
602 return((Image *) NULL);
cristyb2f4d5d2013-05-20 20:10:33 +0000603 edge_image=EdgeImage(clone_image,radius,exception);
cristy3ed852e2009-09-05 21:47:34 +0000604 clone_image=DestroyImage(clone_image);
605 if (edge_image == (Image *) NULL)
606 return((Image *) NULL);
cristyaa2c16c2012-03-25 22:21:35 +0000607 charcoal_image=BlurImage(edge_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000608 edge_image=DestroyImage(edge_image);
609 if (charcoal_image == (Image *) NULL)
610 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +0000611 (void) NormalizeImage(charcoal_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +0000612 (void) NegateImage(charcoal_image,MagickFalse,exception);
cristye247eff2013-04-15 11:50:40 +0000613 (void) GrayscaleImage(charcoal_image,image->intensity,exception);
cristy3ed852e2009-09-05 21:47:34 +0000614 return(charcoal_image);
615}
616
617/*
618%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
619% %
620% %
621% %
622% C o l o r i z e I m a g e %
623% %
624% %
625% %
626%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
627%
628% ColorizeImage() blends the fill color with each pixel in the image.
629% A percentage blend is specified with opacity. Control the application
630% of different color components by specifying a different percentage for
631% each component (e.g. 90/100/10 is 90% red, 100% green, and 10% blue).
632%
633% The format of the ColorizeImage method is:
634%
cristyc7e6ff62011-10-03 13:46:11 +0000635% Image *ColorizeImage(const Image *image,const char *blend,
636% const PixelInfo *colorize,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000637%
638% A description of each parameter follows:
639%
640% o image: the image.
641%
cristyc7e6ff62011-10-03 13:46:11 +0000642% o blend: A character string indicating the level of blending as a
cristy3ed852e2009-09-05 21:47:34 +0000643% percentage.
644%
645% o colorize: A color value.
646%
647% o exception: return any errors or warnings in this structure.
648%
649*/
cristyc7e6ff62011-10-03 13:46:11 +0000650MagickExport Image *ColorizeImage(const Image *image,const char *blend,
651 const PixelInfo *colorize,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000652{
653#define ColorizeImageTag "Colorize/Image"
cristy0cf6da52012-08-25 00:35:24 +0000654#define Colorize(pixel,blend_percentage,colorize) \
655 (((pixel)*(100.0-(blend_percentage))+(colorize)*(blend_percentage))/100.0)
cristy3ed852e2009-09-05 21:47:34 +0000656
cristyc4c8d132010-01-07 01:58:38 +0000657 CacheView
658 *colorize_view,
659 *image_view;
660
cristy3ed852e2009-09-05 21:47:34 +0000661 GeometryInfo
662 geometry_info;
663
664 Image
665 *colorize_image;
666
cristy3ed852e2009-09-05 21:47:34 +0000667 MagickBooleanType
668 status;
669
cristybb503372010-05-27 20:51:26 +0000670 MagickOffsetType
671 progress;
672
cristy3ed852e2009-09-05 21:47:34 +0000673 MagickStatusType
674 flags;
675
cristyc7e6ff62011-10-03 13:46:11 +0000676 PixelInfo
cristy20c3aed2012-04-10 01:06:21 +0000677 blend_percentage;
cristyc7e6ff62011-10-03 13:46:11 +0000678
cristybb503372010-05-27 20:51:26 +0000679 ssize_t
680 y;
681
cristy3ed852e2009-09-05 21:47:34 +0000682 /*
683 Allocate colorized image.
684 */
685 assert(image != (const Image *) NULL);
686 assert(image->signature == MagickSignature);
687 if (image->debug != MagickFalse)
688 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
689 assert(exception != (ExceptionInfo *) NULL);
690 assert(exception->signature == MagickSignature);
cristy768165d2012-04-09 15:01:35 +0000691 colorize_image=CloneImage(image,image->columns,image->rows,MagickTrue,
692 exception);
693 if (colorize_image == (Image *) NULL)
694 return((Image *) NULL);
695 if (SetImageStorageClass(colorize_image,DirectClass,exception) == MagickFalse)
696 {
697 colorize_image=DestroyImage(colorize_image);
698 return((Image *) NULL);
699 }
cristyf8da9f92013-04-16 19:52:28 +0000700 if ((IsGrayColorspace(image->colorspace) != MagickFalse) ||
cristy4dab3802013-03-15 22:08:15 +0000701 (IsPixelInfoGray(colorize) != MagickFalse))
cristya6400b12013-03-15 12:20:18 +0000702 (void) SetImageColorspace(colorize_image,sRGBColorspace,exception);
cristy8a46d822012-08-28 23:32:39 +0000703 if ((colorize_image->alpha_trait != BlendPixelTrait) &&
704 (colorize->alpha_trait == BlendPixelTrait))
cristy768165d2012-04-09 15:01:35 +0000705 (void) SetImageAlpha(colorize_image,OpaqueAlpha,exception);
706 if (blend == (const char *) NULL)
707 return(colorize_image);
cristy20c3aed2012-04-10 01:06:21 +0000708 GetPixelInfo(image,&blend_percentage);
709 flags=ParseGeometry(blend,&geometry_info);
710 blend_percentage.red=geometry_info.rho;
711 blend_percentage.green=geometry_info.rho;
712 blend_percentage.blue=geometry_info.rho;
713 blend_percentage.black=geometry_info.rho;
cristy3ee7de52012-04-14 23:40:47 +0000714 blend_percentage.alpha=geometry_info.rho;
cristy20c3aed2012-04-10 01:06:21 +0000715 if ((flags & SigmaValue) != 0)
716 blend_percentage.green=geometry_info.sigma;
717 if ((flags & XiValue) != 0)
718 blend_percentage.blue=geometry_info.xi;
719 if ((flags & PsiValue) != 0)
720 blend_percentage.alpha=geometry_info.psi;
721 if (blend_percentage.colorspace == CMYKColorspace)
722 {
723 if ((flags & PsiValue) != 0)
724 blend_percentage.black=geometry_info.psi;
725 if ((flags & ChiValue) != 0)
726 blend_percentage.alpha=geometry_info.chi;
727 }
cristy3ed852e2009-09-05 21:47:34 +0000728 /*
729 Colorize DirectClass image.
730 */
731 status=MagickTrue;
732 progress=0;
cristy46ff2672012-12-14 15:32:26 +0000733 image_view=AcquireVirtualCacheView(image,exception);
734 colorize_view=AcquireAuthenticCacheView(colorize_image,exception);
cristy319a1e72010-02-21 15:13:11 +0000735#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000736 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +0000737 magick_threads(image,colorize_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000738#endif
cristybb503372010-05-27 20:51:26 +0000739 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000740 {
741 MagickBooleanType
742 sync;
743
cristy4c08aed2011-07-01 19:47:50 +0000744 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000745 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000746
cristy4c08aed2011-07-01 19:47:50 +0000747 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000748 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000749
cristy4a37c622012-08-23 18:47:56 +0000750 register ssize_t
751 x;
752
cristy3ed852e2009-09-05 21:47:34 +0000753 if (status == MagickFalse)
754 continue;
755 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
756 q=QueueCacheViewAuthenticPixels(colorize_view,0,y,colorize_image->columns,1,
757 exception);
cristy4c08aed2011-07-01 19:47:50 +0000758 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000759 {
760 status=MagickFalse;
761 continue;
762 }
cristybb503372010-05-27 20:51:26 +0000763 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000764 {
cristy0cf6da52012-08-25 00:35:24 +0000765 register ssize_t
766 i;
767
768 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
769 {
cristy5a23c552013-02-13 14:34:28 +0000770 PixelChannel channel=GetPixelChannelChannel(image,i);
771 PixelTrait traits=GetPixelChannelTraits(image,channel);
772 PixelTrait colorize_traits=GetPixelChannelTraits(colorize_image,
773 channel);
cristy0cf6da52012-08-25 00:35:24 +0000774 if ((traits == UndefinedPixelTrait) ||
775 (colorize_traits == UndefinedPixelTrait))
cristyd6803382012-04-10 01:41:25 +0000776 continue;
cristy0cf6da52012-08-25 00:35:24 +0000777 if (((colorize_traits & CopyPixelTrait) != 0) ||
cristy883fde12013-04-08 00:50:13 +0000778 (GetPixelReadMask(image,p) == 0))
cristy0cf6da52012-08-25 00:35:24 +0000779 {
780 SetPixelChannel(colorize_image,channel,p[i],q);
781 continue;
782 }
cristyb7113232013-02-15 00:35:19 +0000783 SetPixelChannel(colorize_image,channel,ClampToQuantum(Colorize(p[i],
784 GetPixelInfoChannel(&blend_percentage,channel),GetPixelInfoChannel(
785 colorize,channel))),q);
cristy0cf6da52012-08-25 00:35:24 +0000786 }
cristyed231572011-07-14 02:18:59 +0000787 p+=GetPixelChannels(image);
788 q+=GetPixelChannels(colorize_image);
cristy3ed852e2009-09-05 21:47:34 +0000789 }
790 sync=SyncCacheViewAuthenticPixels(colorize_view,exception);
791 if (sync == MagickFalse)
792 status=MagickFalse;
793 if (image->progress_monitor != (MagickProgressMonitor) NULL)
794 {
795 MagickBooleanType
796 proceed;
797
cristy319a1e72010-02-21 15:13:11 +0000798#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +0000799 #pragma omp critical (MagickCore_ColorizeImage)
cristy3ed852e2009-09-05 21:47:34 +0000800#endif
801 proceed=SetImageProgress(image,ColorizeImageTag,progress++,image->rows);
802 if (proceed == MagickFalse)
803 status=MagickFalse;
804 }
805 }
806 image_view=DestroyCacheView(image_view);
807 colorize_view=DestroyCacheView(colorize_view);
808 if (status == MagickFalse)
809 colorize_image=DestroyImage(colorize_image);
810 return(colorize_image);
811}
812
813/*
814%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
815% %
816% %
817% %
cristye6365592010-04-02 17:31:23 +0000818% C o l o r M a t r i x I m a g e %
819% %
820% %
821% %
822%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
823%
824% ColorMatrixImage() applies color transformation to an image. This method
825% permits saturation changes, hue rotation, luminance to alpha, and various
826% other effects. Although variable-sized transformation matrices can be used,
827% typically one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA
828% (or RGBA with offsets). The matrix is similar to those used by Adobe Flash
829% except offsets are in column 6 rather than 5 (in support of CMYKA images)
830% and offsets are normalized (divide Flash offset by 255).
831%
832% The format of the ColorMatrixImage method is:
833%
834% Image *ColorMatrixImage(const Image *image,
835% const KernelInfo *color_matrix,ExceptionInfo *exception)
836%
837% A description of each parameter follows:
838%
839% o image: the image.
840%
841% o color_matrix: the color matrix.
842%
843% o exception: return any errors or warnings in this structure.
844%
845*/
anthonyfd706f92012-01-19 04:22:02 +0000846/* FUTURE: modify to make use of a MagickMatrix Mutliply function
847 That should be provided in "matrix.c"
848 (ASIDE: actually distorts should do this too but currently doesn't)
849*/
850
cristye6365592010-04-02 17:31:23 +0000851MagickExport Image *ColorMatrixImage(const Image *image,
852 const KernelInfo *color_matrix,ExceptionInfo *exception)
853{
854#define ColorMatrixImageTag "ColorMatrix/Image"
855
856 CacheView
857 *color_view,
858 *image_view;
859
860 double
861 ColorMatrix[6][6] =
862 {
863 { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
864 { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
865 { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
866 { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
867 { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
868 { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }
869 };
870
871 Image
872 *color_image;
873
cristye6365592010-04-02 17:31:23 +0000874 MagickBooleanType
875 status;
876
cristybb503372010-05-27 20:51:26 +0000877 MagickOffsetType
878 progress;
879
880 register ssize_t
cristye6365592010-04-02 17:31:23 +0000881 i;
882
cristybb503372010-05-27 20:51:26 +0000883 ssize_t
884 u,
885 v,
886 y;
887
cristye6365592010-04-02 17:31:23 +0000888 /*
anthonyfd706f92012-01-19 04:22:02 +0000889 Map given color_matrix, into a 6x6 matrix RGBKA and a constant
cristye6365592010-04-02 17:31:23 +0000890 */
891 assert(image != (Image *) NULL);
892 assert(image->signature == MagickSignature);
893 if (image->debug != MagickFalse)
894 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
895 assert(exception != (ExceptionInfo *) NULL);
896 assert(exception->signature == MagickSignature);
897 i=0;
cristybb503372010-05-27 20:51:26 +0000898 for (v=0; v < (ssize_t) color_matrix->height; v++)
899 for (u=0; u < (ssize_t) color_matrix->width; u++)
cristye6365592010-04-02 17:31:23 +0000900 {
901 if ((v < 6) && (u < 6))
902 ColorMatrix[v][u]=color_matrix->values[i];
903 i++;
904 }
905 /*
906 Initialize color image.
907 */
cristy12550e62010-06-07 12:46:40 +0000908 color_image=CloneImage(image,0,0,MagickTrue,exception);
cristye6365592010-04-02 17:31:23 +0000909 if (color_image == (Image *) NULL)
910 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000911 if (SetImageStorageClass(color_image,DirectClass,exception) == MagickFalse)
cristye6365592010-04-02 17:31:23 +0000912 {
cristye6365592010-04-02 17:31:23 +0000913 color_image=DestroyImage(color_image);
914 return((Image *) NULL);
915 }
916 if (image->debug != MagickFalse)
917 {
918 char
919 format[MaxTextExtent],
920 *message;
921
922 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
923 " ColorMatrix image with color matrix:");
924 message=AcquireString("");
925 for (v=0; v < 6; v++)
926 {
927 *message='\0';
cristyb51dff52011-05-19 16:55:47 +0000928 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristye6365592010-04-02 17:31:23 +0000929 (void) ConcatenateString(&message,format);
930 for (u=0; u < 6; u++)
931 {
cristyb51dff52011-05-19 16:55:47 +0000932 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",
cristye6365592010-04-02 17:31:23 +0000933 ColorMatrix[v][u]);
934 (void) ConcatenateString(&message,format);
935 }
936 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
937 }
938 message=DestroyString(message);
939 }
940 /*
anthonyfd706f92012-01-19 04:22:02 +0000941 Apply the ColorMatrix to image.
cristye6365592010-04-02 17:31:23 +0000942 */
943 status=MagickTrue;
944 progress=0;
cristy46ff2672012-12-14 15:32:26 +0000945 image_view=AcquireVirtualCacheView(image,exception);
946 color_view=AcquireAuthenticCacheView(color_image,exception);
cristye6365592010-04-02 17:31:23 +0000947#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000948 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +0000949 magick_threads(image,color_image,image->rows,1)
cristye6365592010-04-02 17:31:23 +0000950#endif
cristybb503372010-05-27 20:51:26 +0000951 for (y=0; y < (ssize_t) image->rows; y++)
cristye6365592010-04-02 17:31:23 +0000952 {
cristyfcc25d92012-02-19 23:06:48 +0000953 PixelInfo
cristye6365592010-04-02 17:31:23 +0000954 pixel;
955
cristy4c08aed2011-07-01 19:47:50 +0000956 register const Quantum
cristye6365592010-04-02 17:31:23 +0000957 *restrict p;
958
cristy4c08aed2011-07-01 19:47:50 +0000959 register Quantum
960 *restrict q;
961
cristybb503372010-05-27 20:51:26 +0000962 register ssize_t
cristye6365592010-04-02 17:31:23 +0000963 x;
964
cristye6365592010-04-02 17:31:23 +0000965 if (status == MagickFalse)
966 continue;
967 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
968 q=GetCacheViewAuthenticPixels(color_view,0,y,color_image->columns,1,
969 exception);
cristy4c08aed2011-07-01 19:47:50 +0000970 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristye6365592010-04-02 17:31:23 +0000971 {
972 status=MagickFalse;
973 continue;
974 }
cristyfcc25d92012-02-19 23:06:48 +0000975 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +0000976 for (x=0; x < (ssize_t) image->columns; x++)
cristye6365592010-04-02 17:31:23 +0000977 {
cristybb503372010-05-27 20:51:26 +0000978 register ssize_t
cristye6365592010-04-02 17:31:23 +0000979 v;
980
cristybb503372010-05-27 20:51:26 +0000981 size_t
cristye6365592010-04-02 17:31:23 +0000982 height;
983
cristyfcc25d92012-02-19 23:06:48 +0000984 GetPixelInfoPixel(image,p,&pixel);
cristye6365592010-04-02 17:31:23 +0000985 height=color_matrix->height > 6 ? 6UL : color_matrix->height;
cristybb503372010-05-27 20:51:26 +0000986 for (v=0; v < (ssize_t) height; v++)
cristye6365592010-04-02 17:31:23 +0000987 {
cristya19f1d72012-08-07 18:24:38 +0000988 double
cristyfcc25d92012-02-19 23:06:48 +0000989 sum;
990
991 sum=ColorMatrix[v][0]*GetPixelRed(image,p)+ColorMatrix[v][1]*
cristy4c08aed2011-07-01 19:47:50 +0000992 GetPixelGreen(image,p)+ColorMatrix[v][2]*GetPixelBlue(image,p);
cristye6365592010-04-02 17:31:23 +0000993 if (image->colorspace == CMYKColorspace)
cristyfcc25d92012-02-19 23:06:48 +0000994 sum+=ColorMatrix[v][3]*GetPixelBlack(image,p);
cristy8a46d822012-08-28 23:32:39 +0000995 if (image->alpha_trait == BlendPixelTrait)
cristyfcc25d92012-02-19 23:06:48 +0000996 sum+=ColorMatrix[v][4]*GetPixelAlpha(image,p);
997 sum+=QuantumRange*ColorMatrix[v][5];
cristye6365592010-04-02 17:31:23 +0000998 switch (v)
999 {
cristyfcc25d92012-02-19 23:06:48 +00001000 case 0: pixel.red=sum; break;
1001 case 1: pixel.green=sum; break;
1002 case 2: pixel.blue=sum; break;
1003 case 3: pixel.black=sum; break;
1004 case 4: pixel.alpha=sum; break;
1005 default: break;
cristye6365592010-04-02 17:31:23 +00001006 }
1007 }
cristyfcc25d92012-02-19 23:06:48 +00001008 SetPixelInfoPixel(color_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00001009 p+=GetPixelChannels(image);
1010 q+=GetPixelChannels(color_image);
cristye6365592010-04-02 17:31:23 +00001011 }
1012 if (SyncCacheViewAuthenticPixels(color_view,exception) == MagickFalse)
1013 status=MagickFalse;
1014 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1015 {
1016 MagickBooleanType
1017 proceed;
1018
1019#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00001020 #pragma omp critical (MagickCore_ColorMatrixImage)
cristye6365592010-04-02 17:31:23 +00001021#endif
1022 proceed=SetImageProgress(image,ColorMatrixImageTag,progress++,
1023 image->rows);
1024 if (proceed == MagickFalse)
1025 status=MagickFalse;
1026 }
1027 }
1028 color_view=DestroyCacheView(color_view);
1029 image_view=DestroyCacheView(image_view);
1030 if (status == MagickFalse)
1031 color_image=DestroyImage(color_image);
1032 return(color_image);
1033}
1034
1035/*
1036%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1037% %
1038% %
1039% %
cristy3ed852e2009-09-05 21:47:34 +00001040+ D e s t r o y F x I n f o %
1041% %
1042% %
1043% %
1044%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1045%
1046% DestroyFxInfo() deallocates memory associated with an FxInfo structure.
1047%
1048% The format of the DestroyFxInfo method is:
1049%
1050% ImageInfo *DestroyFxInfo(ImageInfo *fx_info)
1051%
1052% A description of each parameter follows:
1053%
1054% o fx_info: the fx info.
1055%
1056*/
cristy7832dc22011-09-05 01:21:53 +00001057MagickPrivate FxInfo *DestroyFxInfo(FxInfo *fx_info)
cristy3ed852e2009-09-05 21:47:34 +00001058{
cristybb503372010-05-27 20:51:26 +00001059 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001060 i;
1061
1062 fx_info->exception=DestroyExceptionInfo(fx_info->exception);
1063 fx_info->expression=DestroyString(fx_info->expression);
1064 fx_info->symbols=DestroySplayTree(fx_info->symbols);
1065 fx_info->colors=DestroySplayTree(fx_info->colors);
cristy0ea377f2011-03-24 00:54:19 +00001066 for (i=(ssize_t) GetImageListLength(fx_info->images)-1; i >= 0; i--)
cristyd76c51e2011-03-26 00:21:26 +00001067 fx_info->view[i]=DestroyCacheView(fx_info->view[i]);
1068 fx_info->view=(CacheView **) RelinquishMagickMemory(fx_info->view);
cristy3ed852e2009-09-05 21:47:34 +00001069 fx_info->random_info=DestroyRandomInfo(fx_info->random_info);
1070 fx_info=(FxInfo *) RelinquishMagickMemory(fx_info);
1071 return(fx_info);
1072}
1073
1074/*
1075%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1076% %
1077% %
1078% %
cristy3ed852e2009-09-05 21:47:34 +00001079+ 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 %
1080% %
1081% %
1082% %
1083%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1084%
1085% FxEvaluateChannelExpression() evaluates an expression and returns the
1086% results.
1087%
1088% The format of the FxEvaluateExpression method is:
1089%
cristya19f1d72012-08-07 18:24:38 +00001090% double FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00001091% const PixelChannel channel,const ssize_t x,const ssize_t y,
cristya19f1d72012-08-07 18:24:38 +00001092% double *alpha,Exceptioninfo *exception)
1093% double FxEvaluateExpression(FxInfo *fx_info,
1094% double *alpha,Exceptioninfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001095%
1096% A description of each parameter follows:
1097%
1098% o fx_info: the fx info.
1099%
1100% o channel: the channel.
1101%
1102% o x,y: the pixel position.
1103%
1104% o alpha: the result.
1105%
1106% o exception: return any errors or warnings in this structure.
1107%
1108*/
1109
cristy351842f2010-03-07 15:27:38 +00001110static inline double MagickMax(const double x,const double y)
1111{
1112 if (x > y)
1113 return(x);
1114 return(y);
1115}
1116
1117static inline double MagickMin(const double x,const double y)
1118{
1119 if (x < y)
1120 return(x);
1121 return(y);
1122}
1123
cristya19f1d72012-08-07 18:24:38 +00001124static double FxChannelStatistics(FxInfo *fx_info,Image *image,
cristy0568ffc2011-07-25 16:54:14 +00001125 PixelChannel channel,const char *symbol,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001126{
cristy5048d302012-08-07 01:05:16 +00001127 ChannelType
1128 channel_mask;
1129
cristy3ed852e2009-09-05 21:47:34 +00001130 char
1131 key[MaxTextExtent],
1132 statistic[MaxTextExtent];
1133
1134 const char
1135 *value;
1136
1137 register const char
1138 *p;
1139
cristy5048d302012-08-07 01:05:16 +00001140 channel_mask=UndefinedChannel;
cristy3ed852e2009-09-05 21:47:34 +00001141 for (p=symbol; (*p != '.') && (*p != '\0'); p++) ;
1142 if (*p == '.')
cristy3ed852e2009-09-05 21:47:34 +00001143 {
cristy5048d302012-08-07 01:05:16 +00001144 ssize_t
1145 option;
1146
1147 option=ParseCommandOption(MagickPixelChannelOptions,MagickTrue,p+1);
1148 if (option >= 0)
1149 {
1150 channel=(PixelChannel) option;
1151 channel_mask=(ChannelType) (channel_mask | (1 << channel));
cristycf1296e2012-08-26 23:40:49 +00001152 SetPixelChannelMask(image,channel_mask);
cristy5048d302012-08-07 01:05:16 +00001153 }
cristy3ed852e2009-09-05 21:47:34 +00001154 }
cristyb51dff52011-05-19 16:55:47 +00001155 (void) FormatLocaleString(key,MaxTextExtent,"%p.%.20g.%s",(void *) image,
cristye8c25f92010-06-03 00:53:06 +00001156 (double) channel,symbol);
cristy3ed852e2009-09-05 21:47:34 +00001157 value=(const char *) GetValueFromSplayTree(fx_info->symbols,key);
1158 if (value != (const char *) NULL)
cristy5048d302012-08-07 01:05:16 +00001159 {
1160 if (channel_mask != UndefinedChannel)
cristycf1296e2012-08-26 23:40:49 +00001161 SetPixelChannelMask(image,channel_mask);
cristy5048d302012-08-07 01:05:16 +00001162 return(QuantumScale*StringToDouble(value,(char **) NULL));
1163 }
cristy3ed852e2009-09-05 21:47:34 +00001164 (void) DeleteNodeFromSplayTree(fx_info->symbols,key);
1165 if (LocaleNCompare(symbol,"depth",5) == 0)
1166 {
cristybb503372010-05-27 20:51:26 +00001167 size_t
cristy3ed852e2009-09-05 21:47:34 +00001168 depth;
1169
cristyfefab1b2011-07-05 00:33:22 +00001170 depth=GetImageDepth(image,exception);
1171 (void) FormatLocaleString(statistic,MaxTextExtent,"%.20g",(double) depth);
cristy3ed852e2009-09-05 21:47:34 +00001172 }
1173 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1174 {
1175 double
1176 kurtosis,
1177 skewness;
1178
cristyd42d9952011-07-08 14:21:50 +00001179 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001180 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",kurtosis);
cristy3ed852e2009-09-05 21:47:34 +00001181 }
1182 if (LocaleNCompare(symbol,"maxima",6) == 0)
1183 {
1184 double
1185 maxima,
1186 minima;
1187
cristyd42d9952011-07-08 14:21:50 +00001188 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001189 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",maxima);
cristy3ed852e2009-09-05 21:47:34 +00001190 }
1191 if (LocaleNCompare(symbol,"mean",4) == 0)
1192 {
1193 double
1194 mean,
1195 standard_deviation;
1196
cristyd42d9952011-07-08 14:21:50 +00001197 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001198 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",mean);
cristy3ed852e2009-09-05 21:47:34 +00001199 }
1200 if (LocaleNCompare(symbol,"minima",6) == 0)
1201 {
1202 double
1203 maxima,
1204 minima;
1205
cristyd42d9952011-07-08 14:21:50 +00001206 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001207 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",minima);
cristy3ed852e2009-09-05 21:47:34 +00001208 }
1209 if (LocaleNCompare(symbol,"skewness",8) == 0)
1210 {
1211 double
1212 kurtosis,
1213 skewness;
1214
cristyd42d9952011-07-08 14:21:50 +00001215 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001216 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",skewness);
cristy3ed852e2009-09-05 21:47:34 +00001217 }
1218 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1219 {
1220 double
1221 mean,
1222 standard_deviation;
1223
cristyd42d9952011-07-08 14:21:50 +00001224 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001225 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",
cristy3ed852e2009-09-05 21:47:34 +00001226 standard_deviation);
1227 }
cristy5048d302012-08-07 01:05:16 +00001228 if (channel_mask != UndefinedChannel)
cristycf1296e2012-08-26 23:40:49 +00001229 SetPixelChannelMask(image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00001230 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(key),
1231 ConstantString(statistic));
cristydbdd0e32011-11-04 23:29:40 +00001232 return(QuantumScale*StringToDouble(statistic,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001233}
1234
cristya19f1d72012-08-07 18:24:38 +00001235static double
cristy0568ffc2011-07-25 16:54:14 +00001236 FxEvaluateSubexpression(FxInfo *,const PixelChannel,const ssize_t,
cristya19f1d72012-08-07 18:24:38 +00001237 const ssize_t,const char *,double *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +00001238
cristyb0aad4c2011-11-02 19:30:35 +00001239static MagickOffsetType FxGCD(MagickOffsetType alpha,MagickOffsetType beta)
1240{
1241 if (beta != 0)
1242 return(FxGCD(beta,alpha % beta));
1243 return(alpha);
1244}
1245
cristy3ed852e2009-09-05 21:47:34 +00001246static inline const char *FxSubexpression(const char *expression,
1247 ExceptionInfo *exception)
1248{
1249 const char
1250 *subexpression;
1251
cristybb503372010-05-27 20:51:26 +00001252 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001253 level;
1254
1255 level=0;
1256 subexpression=expression;
1257 while ((*subexpression != '\0') &&
1258 ((level != 1) || (strchr(")",(int) *subexpression) == (char *) NULL)))
1259 {
1260 if (strchr("(",(int) *subexpression) != (char *) NULL)
1261 level++;
1262 else
1263 if (strchr(")",(int) *subexpression) != (char *) NULL)
1264 level--;
1265 subexpression++;
1266 }
1267 if (*subexpression == '\0')
1268 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
cristyefe601c2013-01-05 17:51:12 +00001269 "UnbalancedParenthesis","`%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00001270 return(subexpression);
1271}
1272
cristya19f1d72012-08-07 18:24:38 +00001273static double FxGetSymbol(FxInfo *fx_info,const PixelChannel channel,
cristye85007d2010-06-06 22:51:36 +00001274 const ssize_t x,const ssize_t y,const char *expression,
1275 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001276{
1277 char
1278 *q,
1279 subexpression[MaxTextExtent],
1280 symbol[MaxTextExtent];
1281
1282 const char
1283 *p,
1284 *value;
1285
1286 Image
1287 *image;
1288
cristy4c08aed2011-07-01 19:47:50 +00001289 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001290 pixel;
1291
cristya19f1d72012-08-07 18:24:38 +00001292 double
cristy3ed852e2009-09-05 21:47:34 +00001293 alpha,
1294 beta;
1295
1296 PointInfo
1297 point;
1298
cristybb503372010-05-27 20:51:26 +00001299 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001300 i;
1301
1302 size_t
cristy1707c6c2012-01-18 23:30:54 +00001303 length,
cristy3ed852e2009-09-05 21:47:34 +00001304 level;
1305
1306 p=expression;
1307 i=GetImageIndexInList(fx_info->images);
1308 level=0;
1309 point.x=(double) x;
1310 point.y=(double) y;
1311 if (isalpha((int) *(p+1)) == 0)
1312 {
1313 if (strchr("suv",(int) *p) != (char *) NULL)
1314 {
1315 switch (*p)
1316 {
1317 case 's':
1318 default:
1319 {
1320 i=GetImageIndexInList(fx_info->images);
1321 break;
1322 }
1323 case 'u': i=0; break;
1324 case 'v': i=1; break;
1325 }
1326 p++;
1327 if (*p == '[')
1328 {
1329 level++;
1330 q=subexpression;
1331 for (p++; *p != '\0'; )
1332 {
1333 if (*p == '[')
1334 level++;
1335 else
1336 if (*p == ']')
1337 {
1338 level--;
1339 if (level == 0)
1340 break;
1341 }
1342 *q++=(*p++);
1343 }
1344 *q='\0';
1345 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1346 &beta,exception);
cristybb503372010-05-27 20:51:26 +00001347 i=(ssize_t) (alpha+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001348 p++;
1349 }
1350 if (*p == '.')
1351 p++;
1352 }
1353 if ((isalpha((int) *(p+1)) == 0) && (*p == 'p'))
1354 {
1355 p++;
1356 if (*p == '{')
1357 {
1358 level++;
1359 q=subexpression;
1360 for (p++; *p != '\0'; )
1361 {
1362 if (*p == '{')
1363 level++;
1364 else
1365 if (*p == '}')
1366 {
1367 level--;
1368 if (level == 0)
1369 break;
1370 }
1371 *q++=(*p++);
1372 }
1373 *q='\0';
1374 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1375 &beta,exception);
1376 point.x=alpha;
1377 point.y=beta;
1378 p++;
1379 }
1380 else
1381 if (*p == '[')
1382 {
1383 level++;
1384 q=subexpression;
1385 for (p++; *p != '\0'; )
1386 {
1387 if (*p == '[')
1388 level++;
1389 else
1390 if (*p == ']')
1391 {
1392 level--;
1393 if (level == 0)
1394 break;
1395 }
1396 *q++=(*p++);
1397 }
1398 *q='\0';
1399 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1400 &beta,exception);
1401 point.x+=alpha;
1402 point.y+=beta;
1403 p++;
1404 }
1405 if (*p == '.')
1406 p++;
1407 }
1408 }
1409 length=GetImageListLength(fx_info->images);
1410 while (i < 0)
cristybb503372010-05-27 20:51:26 +00001411 i+=(ssize_t) length;
cristy3ed852e2009-09-05 21:47:34 +00001412 i%=length;
1413 image=GetImageFromList(fx_info->images,i);
1414 if (image == (Image *) NULL)
1415 {
1416 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
cristyefe601c2013-01-05 17:51:12 +00001417 "NoSuchImage","`%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00001418 return(0.0);
1419 }
cristy4c08aed2011-07-01 19:47:50 +00001420 GetPixelInfo(image,&pixel);
1421 (void) InterpolatePixelInfo(image,fx_info->view[i],image->interpolate,
cristy4f820712011-04-01 12:35:43 +00001422 point.x,point.y,&pixel,exception);
cristy1707c6c2012-01-18 23:30:54 +00001423 if ((strlen(p) > 2) && (LocaleCompare(p,"intensity") != 0) &&
1424 (LocaleCompare(p,"luminance") != 0) && (LocaleCompare(p,"hue") != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00001425 (LocaleCompare(p,"saturation") != 0) &&
1426 (LocaleCompare(p,"lightness") != 0))
1427 {
1428 char
1429 name[MaxTextExtent];
1430
1431 (void) CopyMagickString(name,p,MaxTextExtent);
1432 for (q=name+(strlen(name)-1); q > name; q--)
1433 {
1434 if (*q == ')')
1435 break;
1436 if (*q == '.')
1437 {
1438 *q='\0';
1439 break;
1440 }
1441 }
1442 if ((strlen(name) > 2) &&
1443 (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
1444 {
cristy4c08aed2011-07-01 19:47:50 +00001445 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001446 *color;
1447
cristy4c08aed2011-07-01 19:47:50 +00001448 color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
1449 if (color != (PixelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001450 {
1451 pixel=(*color);
1452 p+=strlen(name);
1453 }
1454 else
cristy1707c6c2012-01-18 23:30:54 +00001455 {
1456 MagickBooleanType
1457 status;
1458
1459 status=QueryColorCompliance(name,AllCompliance,&pixel,
1460 fx_info->exception);
1461 if (status != MagickFalse)
1462 {
1463 (void) AddValueToSplayTree(fx_info->colors,ConstantString(
1464 name),ClonePixelInfo(&pixel));
1465 p+=strlen(name);
1466 }
1467 }
cristy3ed852e2009-09-05 21:47:34 +00001468 }
1469 }
1470 (void) CopyMagickString(symbol,p,MaxTextExtent);
1471 StripString(symbol);
1472 if (*symbol == '\0')
1473 {
1474 switch (channel)
1475 {
cristy0568ffc2011-07-25 16:54:14 +00001476 case RedPixelChannel: return(QuantumScale*pixel.red);
1477 case GreenPixelChannel: return(QuantumScale*pixel.green);
1478 case BluePixelChannel: return(QuantumScale*pixel.blue);
1479 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001480 {
1481 if (image->colorspace != CMYKColorspace)
1482 {
1483 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001484 ImageError,"ColorSeparatedImageRequired","`%s'",
cristy3ed852e2009-09-05 21:47:34 +00001485 image->filename);
1486 return(0.0);
1487 }
cristy4c08aed2011-07-01 19:47:50 +00001488 return(QuantumScale*pixel.black);
1489 }
cristy0568ffc2011-07-25 16:54:14 +00001490 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001491 {
cristya19f1d72012-08-07 18:24:38 +00001492 double
cristy4c08aed2011-07-01 19:47:50 +00001493 alpha;
1494
cristy8a46d822012-08-28 23:32:39 +00001495 if (pixel.alpha_trait != BlendPixelTrait)
cristy4c08aed2011-07-01 19:47:50 +00001496 return(1.0);
cristya19f1d72012-08-07 18:24:38 +00001497 alpha=(double) (QuantumScale*pixel.alpha);
cristy4c08aed2011-07-01 19:47:50 +00001498 return(alpha);
cristy3ed852e2009-09-05 21:47:34 +00001499 }
cristya382aca2011-12-06 18:22:48 +00001500 case IndexPixelChannel:
1501 return(0.0);
cristyb3a73b52011-07-26 01:34:43 +00001502 case IntensityPixelChannel:
cristyf364ed42010-12-15 01:54:43 +00001503 {
cristy4c08aed2011-07-01 19:47:50 +00001504 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristyf364ed42010-12-15 01:54:43 +00001505 }
cristy3ed852e2009-09-05 21:47:34 +00001506 default:
1507 break;
1508 }
1509 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
cristyefe601c2013-01-05 17:51:12 +00001510 "UnableToParseExpression","`%s'",p);
cristy3ed852e2009-09-05 21:47:34 +00001511 return(0.0);
1512 }
1513 switch (*symbol)
1514 {
1515 case 'A':
1516 case 'a':
1517 {
1518 if (LocaleCompare(symbol,"a") == 0)
cristya19f1d72012-08-07 18:24:38 +00001519 return((double) (QuantumScale*pixel.alpha));
cristy3ed852e2009-09-05 21:47:34 +00001520 break;
1521 }
1522 case 'B':
1523 case 'b':
1524 {
1525 if (LocaleCompare(symbol,"b") == 0)
1526 return(QuantumScale*pixel.blue);
1527 break;
1528 }
1529 case 'C':
1530 case 'c':
1531 {
1532 if (LocaleNCompare(symbol,"channel",7) == 0)
1533 {
1534 GeometryInfo
1535 channel_info;
1536
1537 MagickStatusType
1538 flags;
1539
1540 flags=ParseGeometry(symbol+7,&channel_info);
1541 if (image->colorspace == CMYKColorspace)
1542 switch (channel)
1543 {
cristy0568ffc2011-07-25 16:54:14 +00001544 case CyanPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001545 {
1546 if ((flags & RhoValue) == 0)
1547 return(0.0);
1548 return(channel_info.rho);
1549 }
cristy0568ffc2011-07-25 16:54:14 +00001550 case MagentaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001551 {
1552 if ((flags & SigmaValue) == 0)
1553 return(0.0);
1554 return(channel_info.sigma);
1555 }
cristy0568ffc2011-07-25 16:54:14 +00001556 case YellowPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001557 {
1558 if ((flags & XiValue) == 0)
1559 return(0.0);
1560 return(channel_info.xi);
1561 }
cristy0568ffc2011-07-25 16:54:14 +00001562 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001563 {
1564 if ((flags & PsiValue) == 0)
1565 return(0.0);
1566 return(channel_info.psi);
1567 }
cristy0568ffc2011-07-25 16:54:14 +00001568 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001569 {
1570 if ((flags & ChiValue) == 0)
1571 return(0.0);
1572 return(channel_info.chi);
1573 }
1574 default:
1575 return(0.0);
1576 }
1577 switch (channel)
1578 {
cristy0568ffc2011-07-25 16:54:14 +00001579 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001580 {
1581 if ((flags & RhoValue) == 0)
1582 return(0.0);
1583 return(channel_info.rho);
1584 }
cristy0568ffc2011-07-25 16:54:14 +00001585 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001586 {
1587 if ((flags & SigmaValue) == 0)
1588 return(0.0);
1589 return(channel_info.sigma);
1590 }
cristy0568ffc2011-07-25 16:54:14 +00001591 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001592 {
1593 if ((flags & XiValue) == 0)
1594 return(0.0);
1595 return(channel_info.xi);
1596 }
cristy0568ffc2011-07-25 16:54:14 +00001597 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001598 {
1599 if ((flags & ChiValue) == 0)
1600 return(0.0);
1601 return(channel_info.chi);
1602 }
cristy0568ffc2011-07-25 16:54:14 +00001603 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001604 {
1605 if ((flags & PsiValue) == 0)
1606 return(0.0);
1607 return(channel_info.psi);
1608 }
cristy3ed852e2009-09-05 21:47:34 +00001609 default:
1610 return(0.0);
1611 }
1612 return(0.0);
1613 }
1614 if (LocaleCompare(symbol,"c") == 0)
1615 return(QuantumScale*pixel.red);
1616 break;
1617 }
1618 case 'D':
1619 case 'd':
1620 {
1621 if (LocaleNCompare(symbol,"depth",5) == 0)
1622 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1623 break;
1624 }
1625 case 'G':
1626 case 'g':
1627 {
1628 if (LocaleCompare(symbol,"g") == 0)
1629 return(QuantumScale*pixel.green);
1630 break;
1631 }
1632 case 'K':
1633 case 'k':
1634 {
1635 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1636 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1637 if (LocaleCompare(symbol,"k") == 0)
1638 {
1639 if (image->colorspace != CMYKColorspace)
1640 {
1641 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001642 OptionError,"ColorSeparatedImageRequired","`%s'",
cristy3ed852e2009-09-05 21:47:34 +00001643 image->filename);
1644 return(0.0);
1645 }
cristy4c08aed2011-07-01 19:47:50 +00001646 return(QuantumScale*pixel.black);
cristy3ed852e2009-09-05 21:47:34 +00001647 }
1648 break;
1649 }
1650 case 'H':
1651 case 'h':
1652 {
1653 if (LocaleCompare(symbol,"h") == 0)
cristya19f1d72012-08-07 18:24:38 +00001654 return((double) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001655 if (LocaleCompare(symbol,"hue") == 0)
1656 {
1657 double
1658 hue,
1659 lightness,
1660 saturation;
1661
cristy0a39a5c2012-06-27 12:51:45 +00001662 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
cristyda1f9c12011-10-02 21:39:49 +00001663 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001664 return(hue);
1665 }
1666 break;
1667 }
1668 case 'I':
1669 case 'i':
1670 {
1671 if ((LocaleCompare(symbol,"image.depth") == 0) ||
1672 (LocaleCompare(symbol,"image.minima") == 0) ||
1673 (LocaleCompare(symbol,"image.maxima") == 0) ||
1674 (LocaleCompare(symbol,"image.mean") == 0) ||
1675 (LocaleCompare(symbol,"image.kurtosis") == 0) ||
1676 (LocaleCompare(symbol,"image.skewness") == 0) ||
1677 (LocaleCompare(symbol,"image.standard_deviation") == 0))
1678 return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
1679 if (LocaleCompare(symbol,"image.resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001680 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001681 if (LocaleCompare(symbol,"image.resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001682 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001683 if (LocaleCompare(symbol,"intensity") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001684 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001685 if (LocaleCompare(symbol,"i") == 0)
cristya19f1d72012-08-07 18:24:38 +00001686 return((double) x);
cristy3ed852e2009-09-05 21:47:34 +00001687 break;
1688 }
1689 case 'J':
1690 case 'j':
1691 {
1692 if (LocaleCompare(symbol,"j") == 0)
cristya19f1d72012-08-07 18:24:38 +00001693 return((double) y);
cristy3ed852e2009-09-05 21:47:34 +00001694 break;
1695 }
1696 case 'L':
1697 case 'l':
1698 {
1699 if (LocaleCompare(symbol,"lightness") == 0)
1700 {
1701 double
1702 hue,
1703 lightness,
1704 saturation;
1705
cristy0a39a5c2012-06-27 12:51:45 +00001706 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
cristyda1f9c12011-10-02 21:39:49 +00001707 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001708 return(lightness);
1709 }
1710 if (LocaleCompare(symbol,"luminance") == 0)
1711 {
1712 double
1713 luminence;
1714
cristya86a5cb2012-10-14 13:40:33 +00001715 luminence=0.21267f*pixel.red+0.71516f*pixel.green+0.07217f*pixel.blue;
cristy3ed852e2009-09-05 21:47:34 +00001716 return(QuantumScale*luminence);
1717 }
1718 break;
1719 }
1720 case 'M':
1721 case 'm':
1722 {
1723 if (LocaleNCompare(symbol,"maxima",6) == 0)
1724 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1725 if (LocaleNCompare(symbol,"mean",4) == 0)
1726 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1727 if (LocaleNCompare(symbol,"minima",6) == 0)
1728 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1729 if (LocaleCompare(symbol,"m") == 0)
1730 return(QuantumScale*pixel.blue);
1731 break;
1732 }
1733 case 'N':
1734 case 'n':
1735 {
1736 if (LocaleCompare(symbol,"n") == 0)
cristya19f1d72012-08-07 18:24:38 +00001737 return((double) GetImageListLength(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001738 break;
1739 }
1740 case 'O':
1741 case 'o':
1742 {
1743 if (LocaleCompare(symbol,"o") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001744 return(QuantumScale*pixel.alpha);
cristy3ed852e2009-09-05 21:47:34 +00001745 break;
1746 }
1747 case 'P':
1748 case 'p':
1749 {
1750 if (LocaleCompare(symbol,"page.height") == 0)
cristya19f1d72012-08-07 18:24:38 +00001751 return((double) image->page.height);
cristy3ed852e2009-09-05 21:47:34 +00001752 if (LocaleCompare(symbol,"page.width") == 0)
cristya19f1d72012-08-07 18:24:38 +00001753 return((double) image->page.width);
cristy3ed852e2009-09-05 21:47:34 +00001754 if (LocaleCompare(symbol,"page.x") == 0)
cristya19f1d72012-08-07 18:24:38 +00001755 return((double) image->page.x);
cristy3ed852e2009-09-05 21:47:34 +00001756 if (LocaleCompare(symbol,"page.y") == 0)
cristya19f1d72012-08-07 18:24:38 +00001757 return((double) image->page.y);
cristy3ed852e2009-09-05 21:47:34 +00001758 break;
1759 }
1760 case 'R':
1761 case 'r':
1762 {
1763 if (LocaleCompare(symbol,"resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001764 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001765 if (LocaleCompare(symbol,"resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001766 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001767 if (LocaleCompare(symbol,"r") == 0)
1768 return(QuantumScale*pixel.red);
1769 break;
1770 }
1771 case 'S':
1772 case 's':
1773 {
1774 if (LocaleCompare(symbol,"saturation") == 0)
1775 {
1776 double
1777 hue,
1778 lightness,
1779 saturation;
1780
cristy0a39a5c2012-06-27 12:51:45 +00001781 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
cristyda1f9c12011-10-02 21:39:49 +00001782 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001783 return(saturation);
1784 }
1785 if (LocaleNCompare(symbol,"skewness",8) == 0)
1786 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1787 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1788 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1789 break;
1790 }
1791 case 'T':
1792 case 't':
1793 {
1794 if (LocaleCompare(symbol,"t") == 0)
cristya19f1d72012-08-07 18:24:38 +00001795 return((double) GetImageIndexInList(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001796 break;
1797 }
1798 case 'W':
1799 case 'w':
1800 {
1801 if (LocaleCompare(symbol,"w") == 0)
cristya19f1d72012-08-07 18:24:38 +00001802 return((double) image->columns);
cristy3ed852e2009-09-05 21:47:34 +00001803 break;
1804 }
1805 case 'Y':
1806 case 'y':
1807 {
1808 if (LocaleCompare(symbol,"y") == 0)
1809 return(QuantumScale*pixel.green);
1810 break;
1811 }
1812 case 'Z':
1813 case 'z':
1814 {
1815 if (LocaleCompare(symbol,"z") == 0)
1816 {
cristya19f1d72012-08-07 18:24:38 +00001817 double
cristy3ed852e2009-09-05 21:47:34 +00001818 depth;
1819
cristya19f1d72012-08-07 18:24:38 +00001820 depth=(double) GetImageDepth(image,fx_info->exception);
cristy3ed852e2009-09-05 21:47:34 +00001821 return(depth);
1822 }
1823 break;
1824 }
1825 default:
1826 break;
1827 }
1828 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1829 if (value != (const char *) NULL)
cristya19f1d72012-08-07 18:24:38 +00001830 return((double) StringToDouble(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001831 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
cristyefe601c2013-01-05 17:51:12 +00001832 "UnableToParseExpression","`%s'",symbol);
cristy3ed852e2009-09-05 21:47:34 +00001833 return(0.0);
1834}
1835
1836static const char *FxOperatorPrecedence(const char *expression,
1837 ExceptionInfo *exception)
1838{
1839 typedef enum
1840 {
1841 UndefinedPrecedence,
1842 NullPrecedence,
1843 BitwiseComplementPrecedence,
1844 ExponentPrecedence,
cristy116af162010-08-13 01:25:47 +00001845 ExponentialNotationPrecedence,
cristy3ed852e2009-09-05 21:47:34 +00001846 MultiplyPrecedence,
1847 AdditionPrecedence,
1848 ShiftPrecedence,
1849 RelationalPrecedence,
1850 EquivalencyPrecedence,
1851 BitwiseAndPrecedence,
1852 BitwiseOrPrecedence,
1853 LogicalAndPrecedence,
1854 LogicalOrPrecedence,
1855 TernaryPrecedence,
1856 AssignmentPrecedence,
1857 CommaPrecedence,
1858 SeparatorPrecedence
1859 } FxPrecedence;
1860
1861 FxPrecedence
1862 precedence,
1863 target;
1864
1865 register const char
1866 *subexpression;
1867
1868 register int
1869 c;
1870
cristybb503372010-05-27 20:51:26 +00001871 size_t
cristy3ed852e2009-09-05 21:47:34 +00001872 level;
1873
1874 c=0;
1875 level=0;
1876 subexpression=(const char *) NULL;
1877 target=NullPrecedence;
1878 while (*expression != '\0')
1879 {
1880 precedence=UndefinedPrecedence;
1881 if ((isspace((int) ((char) *expression)) != 0) || (c == (int) '@'))
1882 {
1883 expression++;
1884 continue;
1885 }
cristy488fa882010-03-01 22:34:24 +00001886 switch (*expression)
1887 {
1888 case 'A':
1889 case 'a':
cristy3ed852e2009-09-05 21:47:34 +00001890 {
cristyb33454f2011-08-03 02:10:45 +00001891#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00001892 if (LocaleNCompare(expression,"acosh",5) == 0)
1893 {
1894 expression+=5;
1895 break;
1896 }
cristyb33454f2011-08-03 02:10:45 +00001897#endif
1898#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00001899 if (LocaleNCompare(expression,"asinh",5) == 0)
1900 {
1901 expression+=5;
1902 break;
1903 }
cristyb33454f2011-08-03 02:10:45 +00001904#endif
1905#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00001906 if (LocaleNCompare(expression,"atanh",5) == 0)
cristy488fa882010-03-01 22:34:24 +00001907 {
1908 expression+=5;
1909 break;
1910 }
cristyb33454f2011-08-03 02:10:45 +00001911#endif
anthony62838e52012-05-24 12:41:54 +00001912 if (LocaleNCompare(expression,"atan2",5) == 0)
1913 {
1914 expression+=5;
1915 break;
1916 }
cristy488fa882010-03-01 22:34:24 +00001917 break;
cristy3ed852e2009-09-05 21:47:34 +00001918 }
cristy62d455f2011-11-03 11:42:28 +00001919 case 'E':
1920 case 'e':
1921 {
1922 if ((LocaleNCompare(expression,"E+",2) == 0) ||
1923 (LocaleNCompare(expression,"E-",2) == 0))
1924 {
1925 expression+=2; /* scientific notation */
1926 break;
1927 }
1928 }
cristy488fa882010-03-01 22:34:24 +00001929 case 'J':
1930 case 'j':
1931 {
1932 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1933 (LocaleNCompare(expression,"j1",2) == 0))
1934 {
1935 expression+=2;
1936 break;
1937 }
1938 break;
1939 }
cristy2def9322010-06-18 23:59:37 +00001940 case '#':
1941 {
1942 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1943 expression++;
1944 break;
1945 }
cristy488fa882010-03-01 22:34:24 +00001946 default:
1947 break;
1948 }
cristy3ed852e2009-09-05 21:47:34 +00001949 if ((c == (int) '{') || (c == (int) '['))
1950 level++;
1951 else
1952 if ((c == (int) '}') || (c == (int) ']'))
1953 level--;
1954 if (level == 0)
1955 switch ((unsigned char) *expression)
1956 {
1957 case '~':
1958 case '!':
1959 {
1960 precedence=BitwiseComplementPrecedence;
1961 break;
1962 }
1963 case '^':
cristy6621e252010-08-13 00:42:57 +00001964 case '@':
cristy3ed852e2009-09-05 21:47:34 +00001965 {
1966 precedence=ExponentPrecedence;
1967 break;
1968 }
1969 default:
1970 {
1971 if (((c != 0) && ((isdigit((int) ((char) c)) != 0) ||
1972 (strchr(")",c) != (char *) NULL))) &&
1973 (((islower((int) ((char) *expression)) != 0) ||
1974 (strchr("(",(int) *expression) != (char *) NULL)) ||
1975 ((isdigit((int) ((char) c)) == 0) &&
1976 (isdigit((int) ((char) *expression)) != 0))) &&
1977 (strchr("xy",(int) *expression) == (char *) NULL))
1978 precedence=MultiplyPrecedence;
1979 break;
1980 }
1981 case '*':
1982 case '/':
1983 case '%':
1984 {
1985 precedence=MultiplyPrecedence;
1986 break;
1987 }
1988 case '+':
1989 case '-':
1990 {
1991 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
1992 (isalpha(c) != 0))
1993 precedence=AdditionPrecedence;
1994 break;
1995 }
1996 case LeftShiftOperator:
1997 case RightShiftOperator:
1998 {
1999 precedence=ShiftPrecedence;
2000 break;
2001 }
2002 case '<':
2003 case LessThanEqualOperator:
2004 case GreaterThanEqualOperator:
2005 case '>':
2006 {
2007 precedence=RelationalPrecedence;
2008 break;
2009 }
2010 case EqualOperator:
2011 case NotEqualOperator:
2012 {
2013 precedence=EquivalencyPrecedence;
2014 break;
2015 }
2016 case '&':
2017 {
2018 precedence=BitwiseAndPrecedence;
2019 break;
2020 }
2021 case '|':
2022 {
2023 precedence=BitwiseOrPrecedence;
2024 break;
2025 }
2026 case LogicalAndOperator:
2027 {
2028 precedence=LogicalAndPrecedence;
2029 break;
2030 }
2031 case LogicalOrOperator:
2032 {
2033 precedence=LogicalOrPrecedence;
2034 break;
2035 }
cristy116af162010-08-13 01:25:47 +00002036 case ExponentialNotation:
2037 {
2038 precedence=ExponentialNotationPrecedence;
2039 break;
2040 }
cristy3ed852e2009-09-05 21:47:34 +00002041 case ':':
2042 case '?':
2043 {
2044 precedence=TernaryPrecedence;
2045 break;
2046 }
2047 case '=':
2048 {
2049 precedence=AssignmentPrecedence;
2050 break;
2051 }
2052 case ',':
2053 {
2054 precedence=CommaPrecedence;
2055 break;
2056 }
2057 case ';':
2058 {
2059 precedence=SeparatorPrecedence;
2060 break;
2061 }
2062 }
2063 if ((precedence == BitwiseComplementPrecedence) ||
2064 (precedence == TernaryPrecedence) ||
2065 (precedence == AssignmentPrecedence))
2066 {
2067 if (precedence > target)
2068 {
2069 /*
2070 Right-to-left associativity.
2071 */
2072 target=precedence;
2073 subexpression=expression;
2074 }
2075 }
2076 else
2077 if (precedence >= target)
2078 {
2079 /*
2080 Left-to-right associativity.
2081 */
2082 target=precedence;
2083 subexpression=expression;
2084 }
2085 if (strchr("(",(int) *expression) != (char *) NULL)
2086 expression=FxSubexpression(expression,exception);
2087 c=(int) (*expression++);
2088 }
2089 return(subexpression);
2090}
2091
cristya19f1d72012-08-07 18:24:38 +00002092static double FxEvaluateSubexpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002093 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristya19f1d72012-08-07 18:24:38 +00002094 const char *expression,double *beta,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002095{
2096 char
2097 *q,
2098 subexpression[MaxTextExtent];
2099
cristya19f1d72012-08-07 18:24:38 +00002100 double
cristy3ed852e2009-09-05 21:47:34 +00002101 alpha,
2102 gamma;
2103
2104 register const char
2105 *p;
2106
2107 *beta=0.0;
2108 if (exception->severity != UndefinedException)
2109 return(0.0);
2110 while (isspace((int) *expression) != 0)
2111 expression++;
2112 if (*expression == '\0')
2113 {
2114 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
cristyefe601c2013-01-05 17:51:12 +00002115 "MissingExpression","`%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002116 return(0.0);
2117 }
cristy66322f02010-05-17 11:40:48 +00002118 *subexpression='\0';
cristy3ed852e2009-09-05 21:47:34 +00002119 p=FxOperatorPrecedence(expression,exception);
2120 if (p != (const char *) NULL)
2121 {
2122 (void) CopyMagickString(subexpression,expression,(size_t)
2123 (p-expression+1));
2124 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2125 exception);
2126 switch ((unsigned char) *p)
2127 {
2128 case '~':
2129 {
2130 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002131 *beta=(double) (~(size_t) *beta);
cristy3ed852e2009-09-05 21:47:34 +00002132 return(*beta);
2133 }
2134 case '!':
2135 {
2136 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2137 return(*beta == 0.0 ? 1.0 : 0.0);
2138 }
2139 case '^':
2140 {
2141 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2142 channel,x,y,++p,beta,exception));
2143 return(*beta);
2144 }
2145 case '*':
cristy116af162010-08-13 01:25:47 +00002146 case ExponentialNotation:
cristy3ed852e2009-09-05 21:47:34 +00002147 {
2148 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2149 return(alpha*(*beta));
2150 }
2151 case '/':
2152 {
2153 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2154 if (*beta == 0.0)
2155 {
2156 if (exception->severity == UndefinedException)
2157 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00002158 OptionError,"DivideByZero","`%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002159 return(0.0);
2160 }
2161 return(alpha/(*beta));
2162 }
2163 case '%':
2164 {
2165 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2166 *beta=fabs(floor(((double) *beta)+0.5));
2167 if (*beta == 0.0)
2168 {
2169 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00002170 OptionError,"DivideByZero","`%s'",expression);
cristy3ed852e2009-09-05 21:47:34 +00002171 return(0.0);
2172 }
2173 return(fmod((double) alpha,(double) *beta));
2174 }
2175 case '+':
2176 {
2177 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2178 return(alpha+(*beta));
2179 }
2180 case '-':
2181 {
2182 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2183 return(alpha-(*beta));
2184 }
2185 case LeftShiftOperator:
2186 {
2187 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002188 *beta=(double) ((size_t) (alpha+0.5) << (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002189 return(*beta);
2190 }
2191 case RightShiftOperator:
2192 {
2193 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002194 *beta=(double) ((size_t) (alpha+0.5) >> (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002195 return(*beta);
2196 }
2197 case '<':
2198 {
2199 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2200 return(alpha < *beta ? 1.0 : 0.0);
2201 }
2202 case LessThanEqualOperator:
2203 {
2204 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2205 return(alpha <= *beta ? 1.0 : 0.0);
2206 }
2207 case '>':
2208 {
2209 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2210 return(alpha > *beta ? 1.0 : 0.0);
2211 }
2212 case GreaterThanEqualOperator:
2213 {
2214 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2215 return(alpha >= *beta ? 1.0 : 0.0);
2216 }
2217 case EqualOperator:
2218 {
2219 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy9b528342012-06-02 00:59:20 +00002220 return(fabs(alpha-(*beta)) < MagickEpsilon ? MagickEpsilon : 0.0);
cristy3ed852e2009-09-05 21:47:34 +00002221 }
2222 case NotEqualOperator:
2223 {
2224 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy972050b2012-06-04 22:09:17 +00002225 return(fabs(alpha-(*beta)) >= MagickEpsilon ? 1.0 : 0.0);
cristy3ed852e2009-09-05 21:47:34 +00002226 }
2227 case '&':
2228 {
2229 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002230 *beta=(double) ((size_t) (alpha+0.5) & (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002231 return(*beta);
2232 }
2233 case '|':
2234 {
2235 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristya19f1d72012-08-07 18:24:38 +00002236 *beta=(double) ((size_t) (alpha+0.5) | (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002237 return(*beta);
2238 }
2239 case LogicalAndOperator:
2240 {
2241 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2242 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2243 return(*beta);
2244 }
2245 case LogicalOrOperator:
2246 {
2247 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2248 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2249 return(*beta);
2250 }
2251 case '?':
2252 {
cristya19f1d72012-08-07 18:24:38 +00002253 double
cristy3ed852e2009-09-05 21:47:34 +00002254 gamma;
2255
2256 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2257 q=subexpression;
2258 p=StringToken(":",&q);
2259 if (q == (char *) NULL)
2260 {
2261 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00002262 OptionError,"UnableToParseExpression","`%s'",subexpression);
cristy3ed852e2009-09-05 21:47:34 +00002263 return(0.0);
2264 }
cristy972050b2012-06-04 22:09:17 +00002265 if (fabs((double) alpha) >= MagickEpsilon)
cristy3ed852e2009-09-05 21:47:34 +00002266 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2267 else
2268 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2269 return(gamma);
2270 }
2271 case '=':
2272 {
2273 char
2274 numeric[MaxTextExtent];
2275
2276 q=subexpression;
2277 while (isalpha((int) ((unsigned char) *q)) != 0)
2278 q++;
2279 if (*q != '\0')
2280 {
2281 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00002282 OptionError,"UnableToParseExpression","`%s'",subexpression);
cristy3ed852e2009-09-05 21:47:34 +00002283 return(0.0);
2284 }
2285 ClearMagickException(exception);
2286 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristyb51dff52011-05-19 16:55:47 +00002287 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
cristy8cd5b312010-01-07 01:10:24 +00002288 *beta);
cristy3ed852e2009-09-05 21:47:34 +00002289 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2290 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2291 subexpression),ConstantString(numeric));
2292 return(*beta);
2293 }
2294 case ',':
2295 {
2296 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2297 return(alpha);
2298 }
2299 case ';':
2300 {
2301 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2302 return(*beta);
2303 }
2304 default:
2305 {
2306 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2307 exception);
2308 return(gamma);
2309 }
2310 }
2311 }
2312 if (strchr("(",(int) *expression) != (char *) NULL)
2313 {
2314 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2315 subexpression[strlen(subexpression)-1]='\0';
2316 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2317 exception);
2318 return(gamma);
2319 }
cristy8b8a3ae2010-10-23 18:49:46 +00002320 switch (*expression)
cristy3ed852e2009-09-05 21:47:34 +00002321 {
2322 case '+':
2323 {
2324 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2325 exception);
2326 return(1.0*gamma);
2327 }
2328 case '-':
2329 {
2330 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2331 exception);
2332 return(-1.0*gamma);
2333 }
2334 case '~':
2335 {
2336 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2337 exception);
cristya19f1d72012-08-07 18:24:38 +00002338 return((double) (~(size_t) (gamma+0.5)));
cristy3ed852e2009-09-05 21:47:34 +00002339 }
2340 case 'A':
2341 case 'a':
2342 {
2343 if (LocaleNCompare(expression,"abs",3) == 0)
2344 {
2345 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2346 exception);
cristya19f1d72012-08-07 18:24:38 +00002347 return((double) fabs((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002348 }
cristyb33454f2011-08-03 02:10:45 +00002349#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00002350 if (LocaleNCompare(expression,"acosh",5) == 0)
2351 {
2352 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2353 exception);
cristya19f1d72012-08-07 18:24:38 +00002354 return((double) acosh((double) alpha));
cristy363772a2011-07-28 23:25:33 +00002355 }
cristyb33454f2011-08-03 02:10:45 +00002356#endif
cristy3ed852e2009-09-05 21:47:34 +00002357 if (LocaleNCompare(expression,"acos",4) == 0)
2358 {
2359 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2360 exception);
cristya19f1d72012-08-07 18:24:38 +00002361 return((double) acos((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002362 }
cristy43c22f42010-03-30 12:34:07 +00002363#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002364 if (LocaleNCompare(expression,"airy",4) == 0)
2365 {
2366 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2367 exception);
2368 if (alpha == 0.0)
cristy2dd03222010-03-30 22:12:11 +00002369 return(1.0);
2370 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
cristy43c22f42010-03-30 12:34:07 +00002371 return(gamma*gamma);
cristyee56cf12010-03-01 22:17:06 +00002372 }
cristy43c22f42010-03-30 12:34:07 +00002373#endif
cristyb33454f2011-08-03 02:10:45 +00002374#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00002375 if (LocaleNCompare(expression,"asinh",5) == 0)
2376 {
2377 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2378 exception);
cristya19f1d72012-08-07 18:24:38 +00002379 return((double) asinh((double) alpha));
cristy363772a2011-07-28 23:25:33 +00002380 }
cristyb33454f2011-08-03 02:10:45 +00002381#endif
cristy3ed852e2009-09-05 21:47:34 +00002382 if (LocaleNCompare(expression,"asin",4) == 0)
2383 {
2384 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2385 exception);
cristya19f1d72012-08-07 18:24:38 +00002386 return((double) asin((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002387 }
2388 if (LocaleNCompare(expression,"alt",3) == 0)
2389 {
2390 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2391 exception);
cristybb503372010-05-27 20:51:26 +00002392 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
cristy3ed852e2009-09-05 21:47:34 +00002393 }
2394 if (LocaleNCompare(expression,"atan2",5) == 0)
2395 {
2396 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2397 exception);
cristya19f1d72012-08-07 18:24:38 +00002398 return((double) atan2((double) alpha,(double) *beta));
cristy3ed852e2009-09-05 21:47:34 +00002399 }
cristyb33454f2011-08-03 02:10:45 +00002400#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00002401 if (LocaleNCompare(expression,"atanh",5) == 0)
2402 {
2403 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2404 exception);
cristya19f1d72012-08-07 18:24:38 +00002405 return((double) atanh((double) alpha));
cristy363772a2011-07-28 23:25:33 +00002406 }
cristyb33454f2011-08-03 02:10:45 +00002407#endif
cristy3ed852e2009-09-05 21:47:34 +00002408 if (LocaleNCompare(expression,"atan",4) == 0)
2409 {
2410 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2411 exception);
cristya19f1d72012-08-07 18:24:38 +00002412 return((double) atan((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002413 }
2414 if (LocaleCompare(expression,"a") == 0)
2415 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2416 break;
2417 }
2418 case 'B':
2419 case 'b':
2420 {
2421 if (LocaleCompare(expression,"b") == 0)
2422 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2423 break;
2424 }
2425 case 'C':
2426 case 'c':
2427 {
2428 if (LocaleNCompare(expression,"ceil",4) == 0)
2429 {
2430 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2431 exception);
cristya19f1d72012-08-07 18:24:38 +00002432 return((double) ceil((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002433 }
2434 if (LocaleNCompare(expression,"cosh",4) == 0)
2435 {
2436 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2437 exception);
cristya19f1d72012-08-07 18:24:38 +00002438 return((double) cosh((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002439 }
2440 if (LocaleNCompare(expression,"cos",3) == 0)
2441 {
2442 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2443 exception);
cristya19f1d72012-08-07 18:24:38 +00002444 return((double) cos((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002445 }
2446 if (LocaleCompare(expression,"c") == 0)
2447 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2448 break;
2449 }
2450 case 'D':
2451 case 'd':
2452 {
2453 if (LocaleNCompare(expression,"debug",5) == 0)
2454 {
2455 const char
2456 *type;
2457
2458 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2459 exception);
2460 if (fx_info->images->colorspace == CMYKColorspace)
2461 switch (channel)
2462 {
cristy0568ffc2011-07-25 16:54:14 +00002463 case CyanPixelChannel: type="cyan"; break;
2464 case MagentaPixelChannel: type="magenta"; break;
2465 case YellowPixelChannel: type="yellow"; break;
2466 case AlphaPixelChannel: type="opacity"; break;
2467 case BlackPixelChannel: type="black"; break;
cristy3ed852e2009-09-05 21:47:34 +00002468 default: type="unknown"; break;
2469 }
2470 else
2471 switch (channel)
2472 {
cristy0568ffc2011-07-25 16:54:14 +00002473 case RedPixelChannel: type="red"; break;
2474 case GreenPixelChannel: type="green"; break;
2475 case BluePixelChannel: type="blue"; break;
2476 case AlphaPixelChannel: type="opacity"; break;
cristy3ed852e2009-09-05 21:47:34 +00002477 default: type="unknown"; break;
2478 }
2479 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2480 if (strlen(subexpression) > 1)
2481 subexpression[strlen(subexpression)-1]='\0';
2482 if (fx_info->file != (FILE *) NULL)
cristy1707c6c2012-01-18 23:30:54 +00002483 (void) FormatLocaleFile(fx_info->file,"%s[%.20g,%.20g].%s: "
2484 "%s=%.*g\n",fx_info->images->filename,(double) x,(double) y,type,
2485 subexpression,GetMagickPrecision(),(double) alpha);
cristy3ed852e2009-09-05 21:47:34 +00002486 return(0.0);
2487 }
cristy5597a8d2011-11-04 00:25:32 +00002488 if (LocaleNCompare(expression,"drc",3) == 0)
2489 {
2490 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2491 exception);
cristya19f1d72012-08-07 18:24:38 +00002492 return((double) (alpha/(*beta*(alpha-1.0)+1.0)));
cristy5597a8d2011-11-04 00:25:32 +00002493 }
cristy3ed852e2009-09-05 21:47:34 +00002494 break;
2495 }
2496 case 'E':
2497 case 'e':
2498 {
2499 if (LocaleCompare(expression,"epsilon") == 0)
cristya19f1d72012-08-07 18:24:38 +00002500 return((double) MagickEpsilon);
cristy3ed852e2009-09-05 21:47:34 +00002501 if (LocaleNCompare(expression,"exp",3) == 0)
2502 {
2503 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2504 exception);
cristya19f1d72012-08-07 18:24:38 +00002505 return((double) exp((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002506 }
2507 if (LocaleCompare(expression,"e") == 0)
cristya19f1d72012-08-07 18:24:38 +00002508 return((double) 2.7182818284590452354);
cristy3ed852e2009-09-05 21:47:34 +00002509 break;
2510 }
2511 case 'F':
2512 case 'f':
2513 {
2514 if (LocaleNCompare(expression,"floor",5) == 0)
2515 {
2516 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2517 exception);
cristya19f1d72012-08-07 18:24:38 +00002518 return((double) floor((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002519 }
2520 break;
2521 }
2522 case 'G':
2523 case 'g':
2524 {
cristy9eeedea2011-11-02 19:04:05 +00002525 if (LocaleNCompare(expression,"gauss",5) == 0)
2526 {
2527 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2528 exception);
2529 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
cristya19f1d72012-08-07 18:24:38 +00002530 return((double) gamma);
cristy9eeedea2011-11-02 19:04:05 +00002531 }
cristyb0aad4c2011-11-02 19:30:35 +00002532 if (LocaleNCompare(expression,"gcd",3) == 0)
2533 {
2534 MagickOffsetType
2535 gcd;
2536
2537 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2538 exception);
cristy1707c6c2012-01-18 23:30:54 +00002539 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType) (*beta+
2540 0.5));
cristya19f1d72012-08-07 18:24:38 +00002541 return((double) gcd);
cristyb0aad4c2011-11-02 19:30:35 +00002542 }
cristy3ed852e2009-09-05 21:47:34 +00002543 if (LocaleCompare(expression,"g") == 0)
2544 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2545 break;
2546 }
2547 case 'H':
2548 case 'h':
2549 {
2550 if (LocaleCompare(expression,"h") == 0)
2551 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2552 if (LocaleCompare(expression,"hue") == 0)
2553 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2554 if (LocaleNCompare(expression,"hypot",5) == 0)
2555 {
2556 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2557 exception);
cristya19f1d72012-08-07 18:24:38 +00002558 return((double) hypot((double) alpha,(double) *beta));
cristy3ed852e2009-09-05 21:47:34 +00002559 }
2560 break;
2561 }
2562 case 'K':
2563 case 'k':
2564 {
2565 if (LocaleCompare(expression,"k") == 0)
2566 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2567 break;
2568 }
2569 case 'I':
2570 case 'i':
2571 {
2572 if (LocaleCompare(expression,"intensity") == 0)
2573 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2574 if (LocaleNCompare(expression,"int",3) == 0)
2575 {
2576 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2577 exception);
cristya19f1d72012-08-07 18:24:38 +00002578 return((double) floor(alpha));
cristy3ed852e2009-09-05 21:47:34 +00002579 }
cristy82b20722011-11-05 21:52:36 +00002580#if defined(MAGICKCORE_HAVE_ISNAN)
cristy639399c2011-11-02 19:16:15 +00002581 if (LocaleNCompare(expression,"isnan",5) == 0)
2582 {
2583 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2584 exception);
cristya19f1d72012-08-07 18:24:38 +00002585 return((double) !!isnan((double) alpha));
cristy639399c2011-11-02 19:16:15 +00002586 }
cristy82b20722011-11-05 21:52:36 +00002587#endif
cristy3ed852e2009-09-05 21:47:34 +00002588 if (LocaleCompare(expression,"i") == 0)
2589 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2590 break;
2591 }
2592 case 'J':
2593 case 'j':
2594 {
2595 if (LocaleCompare(expression,"j") == 0)
2596 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
cristy161b9262010-03-20 19:34:32 +00002597#if defined(MAGICKCORE_HAVE_J0)
cristyee56cf12010-03-01 22:17:06 +00002598 if (LocaleNCompare(expression,"j0",2) == 0)
2599 {
2600 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2601 exception);
cristya19f1d72012-08-07 18:24:38 +00002602 return((double) j0((double) alpha));
cristyee56cf12010-03-01 22:17:06 +00002603 }
cristy161b9262010-03-20 19:34:32 +00002604#endif
2605#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002606 if (LocaleNCompare(expression,"j1",2) == 0)
2607 {
2608 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2609 exception);
cristya19f1d72012-08-07 18:24:38 +00002610 return((double) j1((double) alpha));
cristyee56cf12010-03-01 22:17:06 +00002611 }
cristy161b9262010-03-20 19:34:32 +00002612#endif
cristyaa018fa2010-04-08 23:03:54 +00002613#if defined(MAGICKCORE_HAVE_J1)
cristya6a09e72010-03-02 14:51:02 +00002614 if (LocaleNCompare(expression,"jinc",4) == 0)
2615 {
2616 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2617 exception);
cristy0946a822010-03-12 17:14:58 +00002618 if (alpha == 0.0)
2619 return(1.0);
cristyc90d70d2012-11-03 23:53:13 +00002620 gamma=(double) (2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha));
cristyfce2f7b2010-03-12 00:29:49 +00002621 return(gamma);
cristya6a09e72010-03-02 14:51:02 +00002622 }
cristyaa018fa2010-04-08 23:03:54 +00002623#endif
cristy3ed852e2009-09-05 21:47:34 +00002624 break;
2625 }
2626 case 'L':
2627 case 'l':
2628 {
2629 if (LocaleNCompare(expression,"ln",2) == 0)
2630 {
2631 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2632 exception);
cristya19f1d72012-08-07 18:24:38 +00002633 return((double) log((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002634 }
cristyc8ed5322010-08-31 12:07:59 +00002635 if (LocaleNCompare(expression,"logtwo",6) == 0)
cristy3ed852e2009-09-05 21:47:34 +00002636 {
cristyc8ed5322010-08-31 12:07:59 +00002637 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
cristy3ed852e2009-09-05 21:47:34 +00002638 exception);
cristya19f1d72012-08-07 18:24:38 +00002639 return((double) log10((double) alpha))/log10(2.0);
cristy3ed852e2009-09-05 21:47:34 +00002640 }
2641 if (LocaleNCompare(expression,"log",3) == 0)
2642 {
2643 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2644 exception);
cristya19f1d72012-08-07 18:24:38 +00002645 return((double) log10((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002646 }
2647 if (LocaleCompare(expression,"lightness") == 0)
2648 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2649 break;
2650 }
2651 case 'M':
2652 case 'm':
2653 {
2654 if (LocaleCompare(expression,"MaxRGB") == 0)
cristya19f1d72012-08-07 18:24:38 +00002655 return((double) QuantumRange);
cristy3ed852e2009-09-05 21:47:34 +00002656 if (LocaleNCompare(expression,"maxima",6) == 0)
2657 break;
2658 if (LocaleNCompare(expression,"max",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002659 {
2660 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2661 exception);
2662 return(alpha > *beta ? alpha : *beta);
2663 }
cristy3ed852e2009-09-05 21:47:34 +00002664 if (LocaleNCompare(expression,"minima",6) == 0)
2665 break;
2666 if (LocaleNCompare(expression,"min",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002667 {
2668 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2669 exception);
2670 return(alpha < *beta ? alpha : *beta);
2671 }
cristy3ed852e2009-09-05 21:47:34 +00002672 if (LocaleNCompare(expression,"mod",3) == 0)
2673 {
2674 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2675 exception);
cristy984049c2011-11-03 18:34:58 +00002676 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2677 return(gamma);
cristy3ed852e2009-09-05 21:47:34 +00002678 }
2679 if (LocaleCompare(expression,"m") == 0)
2680 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2681 break;
2682 }
2683 case 'N':
2684 case 'n':
2685 {
cristyad3502e2011-11-02 19:10:45 +00002686 if (LocaleNCompare(expression,"not",3) == 0)
2687 {
2688 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2689 exception);
cristya19f1d72012-08-07 18:24:38 +00002690 return((double) (alpha < MagickEpsilon));
cristyad3502e2011-11-02 19:10:45 +00002691 }
cristy3ed852e2009-09-05 21:47:34 +00002692 if (LocaleCompare(expression,"n") == 0)
2693 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2694 break;
2695 }
2696 case 'O':
2697 case 'o':
2698 {
2699 if (LocaleCompare(expression,"Opaque") == 0)
2700 return(1.0);
2701 if (LocaleCompare(expression,"o") == 0)
2702 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2703 break;
2704 }
2705 case 'P':
2706 case 'p':
2707 {
cristy670aa3c2011-11-03 00:54:00 +00002708 if (LocaleCompare(expression,"phi") == 0)
cristya19f1d72012-08-07 18:24:38 +00002709 return((double) MagickPHI);
cristy3ed852e2009-09-05 21:47:34 +00002710 if (LocaleCompare(expression,"pi") == 0)
cristya19f1d72012-08-07 18:24:38 +00002711 return((double) MagickPI);
cristy3ed852e2009-09-05 21:47:34 +00002712 if (LocaleNCompare(expression,"pow",3) == 0)
2713 {
2714 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2715 exception);
cristya19f1d72012-08-07 18:24:38 +00002716 return((double) pow((double) alpha,(double) *beta));
cristy3ed852e2009-09-05 21:47:34 +00002717 }
2718 if (LocaleCompare(expression,"p") == 0)
2719 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2720 break;
2721 }
2722 case 'Q':
2723 case 'q':
2724 {
2725 if (LocaleCompare(expression,"QuantumRange") == 0)
cristya19f1d72012-08-07 18:24:38 +00002726 return((double) QuantumRange);
cristy3ed852e2009-09-05 21:47:34 +00002727 if (LocaleCompare(expression,"QuantumScale") == 0)
cristya19f1d72012-08-07 18:24:38 +00002728 return((double) QuantumScale);
cristy3ed852e2009-09-05 21:47:34 +00002729 break;
2730 }
2731 case 'R':
2732 case 'r':
2733 {
2734 if (LocaleNCompare(expression,"rand",4) == 0)
cristya19f1d72012-08-07 18:24:38 +00002735 return((double) GetPseudoRandomValue(fx_info->random_info));
cristy3ed852e2009-09-05 21:47:34 +00002736 if (LocaleNCompare(expression,"round",5) == 0)
2737 {
2738 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2739 exception);
cristya19f1d72012-08-07 18:24:38 +00002740 return((double) floor((double) alpha+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002741 }
2742 if (LocaleCompare(expression,"r") == 0)
2743 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2744 break;
2745 }
2746 case 'S':
2747 case 's':
2748 {
2749 if (LocaleCompare(expression,"saturation") == 0)
2750 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2751 if (LocaleNCompare(expression,"sign",4) == 0)
2752 {
2753 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2754 exception);
2755 return(alpha < 0.0 ? -1.0 : 1.0);
2756 }
cristya6a09e72010-03-02 14:51:02 +00002757 if (LocaleNCompare(expression,"sinc",4) == 0)
2758 {
2759 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2760 exception);
2761 if (alpha == 0)
2762 return(1.0);
cristya19f1d72012-08-07 18:24:38 +00002763 gamma=(double) (sin((double) (MagickPI*alpha))/
cristya6a09e72010-03-02 14:51:02 +00002764 (MagickPI*alpha));
2765 return(gamma);
2766 }
cristy3ed852e2009-09-05 21:47:34 +00002767 if (LocaleNCompare(expression,"sinh",4) == 0)
2768 {
2769 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2770 exception);
cristya19f1d72012-08-07 18:24:38 +00002771 return((double) sinh((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002772 }
2773 if (LocaleNCompare(expression,"sin",3) == 0)
2774 {
2775 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2776 exception);
cristya19f1d72012-08-07 18:24:38 +00002777 return((double) sin((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002778 }
2779 if (LocaleNCompare(expression,"sqrt",4) == 0)
2780 {
2781 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2782 exception);
cristya19f1d72012-08-07 18:24:38 +00002783 return((double) sqrt((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002784 }
cristy9eeedea2011-11-02 19:04:05 +00002785 if (LocaleNCompare(expression,"squish",6) == 0)
2786 {
2787 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2788 exception);
cristya19f1d72012-08-07 18:24:38 +00002789 return((double) (1.0/(1.0+exp((double) (-alpha)))));
cristy9eeedea2011-11-02 19:04:05 +00002790 }
cristy3ed852e2009-09-05 21:47:34 +00002791 if (LocaleCompare(expression,"s") == 0)
2792 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2793 break;
2794 }
2795 case 'T':
2796 case 't':
2797 {
2798 if (LocaleNCompare(expression,"tanh",4) == 0)
2799 {
2800 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2801 exception);
cristya19f1d72012-08-07 18:24:38 +00002802 return((double) tanh((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002803 }
2804 if (LocaleNCompare(expression,"tan",3) == 0)
2805 {
2806 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2807 exception);
cristya19f1d72012-08-07 18:24:38 +00002808 return((double) tan((double) alpha));
cristy3ed852e2009-09-05 21:47:34 +00002809 }
2810 if (LocaleCompare(expression,"Transparent") == 0)
2811 return(0.0);
cristy16788e42010-08-13 13:44:26 +00002812 if (LocaleNCompare(expression,"trunc",5) == 0)
2813 {
2814 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2815 exception);
2816 if (alpha >= 0.0)
cristya19f1d72012-08-07 18:24:38 +00002817 return((double) floor((double) alpha));
2818 return((double) ceil((double) alpha));
cristy16788e42010-08-13 13:44:26 +00002819 }
cristy3ed852e2009-09-05 21:47:34 +00002820 if (LocaleCompare(expression,"t") == 0)
2821 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2822 break;
2823 }
2824 case 'U':
2825 case 'u':
2826 {
2827 if (LocaleCompare(expression,"u") == 0)
2828 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2829 break;
2830 }
2831 case 'V':
2832 case 'v':
2833 {
2834 if (LocaleCompare(expression,"v") == 0)
2835 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2836 break;
2837 }
2838 case 'W':
2839 case 'w':
2840 {
cristy9eeedea2011-11-02 19:04:05 +00002841 if (LocaleNCompare(expression,"while",5) == 0)
2842 {
2843 do
2844 {
2845 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2846 exception);
2847 } while (fabs((double) alpha) >= MagickEpsilon);
cristya19f1d72012-08-07 18:24:38 +00002848 return((double) *beta);
cristy9eeedea2011-11-02 19:04:05 +00002849 }
cristy3ed852e2009-09-05 21:47:34 +00002850 if (LocaleCompare(expression,"w") == 0)
2851 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2852 break;
2853 }
2854 case 'Y':
2855 case 'y':
2856 {
2857 if (LocaleCompare(expression,"y") == 0)
2858 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2859 break;
2860 }
2861 case 'Z':
2862 case 'z':
2863 {
2864 if (LocaleCompare(expression,"z") == 0)
2865 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2866 break;
2867 }
2868 default:
2869 break;
2870 }
2871 q=(char *) expression;
cristydbdd0e32011-11-04 23:29:40 +00002872 alpha=InterpretSiPrefixValue(expression,&q);
cristy3ed852e2009-09-05 21:47:34 +00002873 if (q == expression)
2874 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2875 return(alpha);
2876}
2877
cristy7832dc22011-09-05 01:21:53 +00002878MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
cristya19f1d72012-08-07 18:24:38 +00002879 double *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002880{
2881 MagickBooleanType
2882 status;
2883
cristy541ae572011-08-05 19:08:59 +00002884 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2885 exception);
cristy3ed852e2009-09-05 21:47:34 +00002886 return(status);
2887}
2888
2889MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
cristya19f1d72012-08-07 18:24:38 +00002890 double *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002891{
2892 FILE
2893 *file;
2894
2895 MagickBooleanType
2896 status;
2897
2898 file=fx_info->file;
2899 fx_info->file=(FILE *) NULL;
cristy541ae572011-08-05 19:08:59 +00002900 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2901 exception);
cristy3ed852e2009-09-05 21:47:34 +00002902 fx_info->file=file;
2903 return(status);
2904}
2905
cristy7832dc22011-09-05 01:21:53 +00002906MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002907 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristya19f1d72012-08-07 18:24:38 +00002908 double *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002909{
cristya19f1d72012-08-07 18:24:38 +00002910 double
cristy3ed852e2009-09-05 21:47:34 +00002911 beta;
2912
2913 beta=0.0;
2914 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2915 exception);
2916 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2917}
2918
2919/*
2920%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2921% %
2922% %
2923% %
2924% F x I m a g e %
2925% %
2926% %
2927% %
2928%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2929%
2930% FxImage() applies a mathematical expression to the specified image.
2931%
2932% The format of the FxImage method is:
2933%
2934% Image *FxImage(const Image *image,const char *expression,
2935% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002936%
2937% A description of each parameter follows:
2938%
2939% o image: the image.
2940%
cristy3ed852e2009-09-05 21:47:34 +00002941% o expression: A mathematical expression.
2942%
2943% o exception: return any errors or warnings in this structure.
2944%
2945*/
2946
2947static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2948{
cristybb503372010-05-27 20:51:26 +00002949 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002950 i;
2951
2952 assert(fx_info != (FxInfo **) NULL);
cristyac245f82012-05-05 17:13:57 +00002953 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
cristy3ed852e2009-09-05 21:47:34 +00002954 if (fx_info[i] != (FxInfo *) NULL)
2955 fx_info[i]=DestroyFxInfo(fx_info[i]);
cristyb41ee102010-10-04 16:46:15 +00002956 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
cristy3ed852e2009-09-05 21:47:34 +00002957 return(fx_info);
2958}
2959
2960static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2961 ExceptionInfo *exception)
2962{
2963 char
2964 *fx_expression;
2965
2966 FxInfo
2967 **fx_info;
2968
cristya19f1d72012-08-07 18:24:38 +00002969 double
cristy3ed852e2009-09-05 21:47:34 +00002970 alpha;
2971
cristybb503372010-05-27 20:51:26 +00002972 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002973 i;
2974
cristybb503372010-05-27 20:51:26 +00002975 size_t
cristy3ed852e2009-09-05 21:47:34 +00002976 number_threads;
2977
cristy9357bdd2012-07-30 12:28:34 +00002978 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
cristyb41ee102010-10-04 16:46:15 +00002979 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +00002980 if (fx_info == (FxInfo **) NULL)
cristy65d161b2012-10-07 20:39:52 +00002981 {
2982 (void) ThrowMagickException(exception,GetMagickModule(),
2983 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
2984 return((FxInfo **) NULL);
2985 }
cristy3ed852e2009-09-05 21:47:34 +00002986 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2987 if (*expression != '@')
2988 fx_expression=ConstantString(expression);
2989 else
2990 fx_expression=FileToString(expression+1,~0,exception);
cristybb503372010-05-27 20:51:26 +00002991 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +00002992 {
cristy65d161b2012-10-07 20:39:52 +00002993 MagickBooleanType
2994 status;
2995
cristydb070952012-04-20 14:33:00 +00002996 fx_info[i]=AcquireFxInfo(image,fx_expression,exception);
cristy3ed852e2009-09-05 21:47:34 +00002997 if (fx_info[i] == (FxInfo *) NULL)
cristy65d161b2012-10-07 20:39:52 +00002998 break;
2999 status=FxPreprocessExpression(fx_info[i],&alpha,exception);
3000 if (status == MagickFalse)
3001 break;
cristy3ed852e2009-09-05 21:47:34 +00003002 }
3003 fx_expression=DestroyString(fx_expression);
cristy65d161b2012-10-07 20:39:52 +00003004 if (i < (ssize_t) number_threads)
3005 fx_info=DestroyFxThreadSet(fx_info);
cristy3ed852e2009-09-05 21:47:34 +00003006 return(fx_info);
3007}
3008
3009MagickExport Image *FxImage(const Image *image,const char *expression,
3010 ExceptionInfo *exception)
3011{
cristy3ed852e2009-09-05 21:47:34 +00003012#define FxImageTag "Fx/Image"
3013
cristyfa112112010-01-04 17:48:07 +00003014 CacheView
cristy79cedc72011-07-25 00:41:15 +00003015 *fx_view,
3016 *image_view;
cristyfa112112010-01-04 17:48:07 +00003017
cristy3ed852e2009-09-05 21:47:34 +00003018 FxInfo
cristyfa112112010-01-04 17:48:07 +00003019 **restrict fx_info;
cristy3ed852e2009-09-05 21:47:34 +00003020
3021 Image
3022 *fx_image;
3023
cristy3ed852e2009-09-05 21:47:34 +00003024 MagickBooleanType
3025 status;
3026
cristybb503372010-05-27 20:51:26 +00003027 MagickOffsetType
3028 progress;
3029
cristybb503372010-05-27 20:51:26 +00003030 ssize_t
3031 y;
3032
cristy3ed852e2009-09-05 21:47:34 +00003033 assert(image != (Image *) NULL);
3034 assert(image->signature == MagickSignature);
3035 if (image->debug != MagickFalse)
3036 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00003037 fx_info=AcquireFxThreadSet(image,expression,exception);
3038 if (fx_info == (FxInfo **) NULL)
cristy65d161b2012-10-07 20:39:52 +00003039 return((Image *) NULL);
3040 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
3041 if (fx_image == (Image *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003042 {
cristy3ed852e2009-09-05 21:47:34 +00003043 fx_info=DestroyFxThreadSet(fx_info);
3044 return((Image *) NULL);
3045 }
cristy65d161b2012-10-07 20:39:52 +00003046 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
3047 {
3048 fx_info=DestroyFxThreadSet(fx_info);
3049 fx_image=DestroyImage(fx_image);
3050 return((Image *) NULL);
3051 }
cristy3ed852e2009-09-05 21:47:34 +00003052 /*
3053 Fx image.
3054 */
3055 status=MagickTrue;
3056 progress=0;
cristy46ff2672012-12-14 15:32:26 +00003057 image_view=AcquireVirtualCacheView(image,exception);
3058 fx_view=AcquireAuthenticCacheView(fx_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003059#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003060 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00003061 magick_threads(image,fx_image,fx_image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003062#endif
cristybb503372010-05-27 20:51:26 +00003063 for (y=0; y < (ssize_t) fx_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003064 {
cristy5c9e6f22010-09-17 17:31:01 +00003065 const int
3066 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003067
cristy79cedc72011-07-25 00:41:15 +00003068 register const Quantum
3069 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003070
cristy4c08aed2011-07-01 19:47:50 +00003071 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003072 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003073
cristy79cedc72011-07-25 00:41:15 +00003074 register ssize_t
3075 x;
3076
cristy3ed852e2009-09-05 21:47:34 +00003077 if (status == MagickFalse)
3078 continue;
cristy79cedc72011-07-25 00:41:15 +00003079 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003080 q=QueueCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
cristy79cedc72011-07-25 00:41:15 +00003081 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003082 {
3083 status=MagickFalse;
3084 continue;
3085 }
cristybb503372010-05-27 20:51:26 +00003086 for (x=0; x < (ssize_t) fx_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003087 {
cristy79cedc72011-07-25 00:41:15 +00003088 register ssize_t
3089 i;
3090
3091 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3092 {
cristya19f1d72012-08-07 18:24:38 +00003093 double
cristy79cedc72011-07-25 00:41:15 +00003094 alpha;
3095
cristy5a23c552013-02-13 14:34:28 +00003096 PixelChannel channel=GetPixelChannelChannel(image,i);
3097 PixelTrait traits=GetPixelChannelTraits(image,channel);
3098 PixelTrait fx_traits=GetPixelChannelTraits(fx_image,channel);
cristy010d7d12011-08-31 01:02:48 +00003099 if ((traits == UndefinedPixelTrait) ||
3100 (fx_traits == UndefinedPixelTrait))
cristy79cedc72011-07-25 00:41:15 +00003101 continue;
cristy1eced092012-08-10 23:10:56 +00003102 if (((fx_traits & CopyPixelTrait) != 0) ||
cristy883fde12013-04-08 00:50:13 +00003103 (GetPixelReadMask(image,p) == 0))
cristy79cedc72011-07-25 00:41:15 +00003104 {
cristy0beccfa2011-09-25 20:47:53 +00003105 SetPixelChannel(fx_image,channel,p[i],q);
cristy79cedc72011-07-25 00:41:15 +00003106 continue;
3107 }
3108 alpha=0.0;
cristya382aca2011-12-06 18:22:48 +00003109 (void) FxEvaluateChannelExpression(fx_info[id],channel,x,y,&alpha,
3110 exception);
cristy8cd03c32012-07-07 18:57:59 +00003111 q[i]=ClampToQuantum(QuantumRange*alpha);
cristy79cedc72011-07-25 00:41:15 +00003112 }
3113 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003114 q+=GetPixelChannels(fx_image);
cristy3ed852e2009-09-05 21:47:34 +00003115 }
3116 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3117 status=MagickFalse;
3118 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3119 {
3120 MagickBooleanType
3121 proceed;
3122
cristyb5d5f722009-11-04 03:03:49 +00003123#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003124 #pragma omp critical (MagickCore_FxImage)
cristy3ed852e2009-09-05 21:47:34 +00003125#endif
3126 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3127 if (proceed == MagickFalse)
3128 status=MagickFalse;
3129 }
3130 }
cristy3ed852e2009-09-05 21:47:34 +00003131 fx_view=DestroyCacheView(fx_view);
cristy79cedc72011-07-25 00:41:15 +00003132 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003133 fx_info=DestroyFxThreadSet(fx_info);
3134 if (status == MagickFalse)
3135 fx_image=DestroyImage(fx_image);
3136 return(fx_image);
3137}
3138
3139/*
3140%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3141% %
3142% %
3143% %
3144% I m p l o d e I m a g e %
3145% %
3146% %
3147% %
3148%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3149%
3150% ImplodeImage() creates a new image that is a copy of an existing
3151% one with the image pixels "implode" by the specified percentage. It
3152% allocates the memory necessary for the new Image structure and returns a
3153% pointer to the new image.
3154%
3155% The format of the ImplodeImage method is:
3156%
3157% Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003158% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003159%
3160% A description of each parameter follows:
3161%
3162% o implode_image: Method ImplodeImage returns a pointer to the image
3163% after it is implode. A null image is returned if there is a memory
3164% shortage.
3165%
3166% o image: the image.
3167%
3168% o amount: Define the extent of the implosion.
3169%
cristy76f512e2011-09-12 01:26:56 +00003170% o method: the pixel interpolation method.
3171%
cristy3ed852e2009-09-05 21:47:34 +00003172% o exception: return any errors or warnings in this structure.
3173%
3174*/
3175MagickExport Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003176 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003177{
3178#define ImplodeImageTag "Implode/Image"
3179
cristyfa112112010-01-04 17:48:07 +00003180 CacheView
3181 *image_view,
cristy5ee937c2013-04-19 12:55:16 +00003182 *implode_view,
3183 *interpolate_view;
cristyfa112112010-01-04 17:48:07 +00003184
cristy3ed852e2009-09-05 21:47:34 +00003185 Image
3186 *implode_image;
3187
cristy3ed852e2009-09-05 21:47:34 +00003188 MagickBooleanType
3189 status;
3190
cristybb503372010-05-27 20:51:26 +00003191 MagickOffsetType
3192 progress;
3193
cristya19f1d72012-08-07 18:24:38 +00003194 double
cristy3ed852e2009-09-05 21:47:34 +00003195 radius;
3196
3197 PointInfo
3198 center,
3199 scale;
3200
cristybb503372010-05-27 20:51:26 +00003201 ssize_t
3202 y;
3203
cristy3ed852e2009-09-05 21:47:34 +00003204 /*
3205 Initialize implode image attributes.
3206 */
3207 assert(image != (Image *) NULL);
3208 assert(image->signature == MagickSignature);
3209 if (image->debug != MagickFalse)
3210 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3211 assert(exception != (ExceptionInfo *) NULL);
3212 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00003213 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3214 exception);
cristy3ed852e2009-09-05 21:47:34 +00003215 if (implode_image == (Image *) NULL)
3216 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003217 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003218 {
cristy3ed852e2009-09-05 21:47:34 +00003219 implode_image=DestroyImage(implode_image);
3220 return((Image *) NULL);
3221 }
cristy4c08aed2011-07-01 19:47:50 +00003222 if (implode_image->background_color.alpha != OpaqueAlpha)
cristy8a46d822012-08-28 23:32:39 +00003223 implode_image->alpha_trait=BlendPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +00003224 /*
3225 Compute scaling factor.
3226 */
3227 scale.x=1.0;
3228 scale.y=1.0;
3229 center.x=0.5*image->columns;
3230 center.y=0.5*image->rows;
3231 radius=center.x;
3232 if (image->columns > image->rows)
3233 scale.y=(double) image->columns/(double) image->rows;
3234 else
3235 if (image->columns < image->rows)
3236 {
3237 scale.x=(double) image->rows/(double) image->columns;
3238 radius=center.y;
3239 }
3240 /*
3241 Implode image.
3242 */
3243 status=MagickTrue;
3244 progress=0;
cristy46ff2672012-12-14 15:32:26 +00003245 image_view=AcquireVirtualCacheView(image,exception);
cristy5ee937c2013-04-19 12:55:16 +00003246 interpolate_view=AcquireVirtualCacheView(image,exception);
cristy46ff2672012-12-14 15:32:26 +00003247 implode_view=AcquireAuthenticCacheView(implode_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003248#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003249 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00003250 magick_threads(image,implode_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003251#endif
cristybb503372010-05-27 20:51:26 +00003252 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003253 {
cristya19f1d72012-08-07 18:24:38 +00003254 double
cristy3ed852e2009-09-05 21:47:34 +00003255 distance;
3256
3257 PointInfo
3258 delta;
3259
cristy6d188022011-09-12 13:23:33 +00003260 register const Quantum
3261 *restrict p;
3262
cristybb503372010-05-27 20:51:26 +00003263 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003264 x;
3265
cristy4c08aed2011-07-01 19:47:50 +00003266 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003267 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003268
3269 if (status == MagickFalse)
3270 continue;
cristy1665ce12013-04-19 12:18:43 +00003271 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003272 q=QueueCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003273 exception);
cristy6d188022011-09-12 13:23:33 +00003274 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003275 {
3276 status=MagickFalse;
3277 continue;
3278 }
cristy3ed852e2009-09-05 21:47:34 +00003279 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00003280 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003281 {
cristy6d188022011-09-12 13:23:33 +00003282 register ssize_t
3283 i;
3284
cristy3ed852e2009-09-05 21:47:34 +00003285 /*
3286 Determine if the pixel is within an ellipse.
3287 */
cristy883fde12013-04-08 00:50:13 +00003288 if (GetPixelReadMask(image,p) == 0)
cristy10a6c612012-01-29 21:41:05 +00003289 {
3290 p+=GetPixelChannels(image);
3291 q+=GetPixelChannels(implode_image);
3292 continue;
3293 }
cristy3ed852e2009-09-05 21:47:34 +00003294 delta.x=scale.x*(double) (x-center.x);
3295 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00003296 if (distance >= (radius*radius))
cristya6d7a9b2012-01-18 20:04:48 +00003297 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy6d188022011-09-12 13:23:33 +00003298 {
cristy5a23c552013-02-13 14:34:28 +00003299 PixelChannel channel=GetPixelChannelChannel(image,i);
3300 PixelTrait traits=GetPixelChannelTraits(image,channel);
3301 PixelTrait implode_traits=GetPixelChannelTraits(implode_image,
3302 channel);
cristy1707c6c2012-01-18 23:30:54 +00003303 if ((traits == UndefinedPixelTrait) ||
3304 (implode_traits == UndefinedPixelTrait))
3305 continue;
cristya6d7a9b2012-01-18 20:04:48 +00003306 SetPixelChannel(implode_image,channel,p[i],q);
cristy6d188022011-09-12 13:23:33 +00003307 }
3308 else
cristy3ed852e2009-09-05 21:47:34 +00003309 {
3310 double
3311 factor;
3312
3313 /*
3314 Implode the pixel.
3315 */
3316 factor=1.0;
3317 if (distance > 0.0)
cristy1707c6c2012-01-18 23:30:54 +00003318 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/radius/
3319 2)),-amount);
cristy5ee937c2013-04-19 12:55:16 +00003320 status=InterpolatePixelChannels(image,interpolate_view,implode_image,
3321 method,(double) (factor*delta.x/scale.x+center.x),(double) (factor*
3322 delta.y/scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00003323 }
cristy6d188022011-09-12 13:23:33 +00003324 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003325 q+=GetPixelChannels(implode_image);
cristy3ed852e2009-09-05 21:47:34 +00003326 }
3327 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3328 status=MagickFalse;
3329 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3330 {
3331 MagickBooleanType
3332 proceed;
3333
cristyb5d5f722009-11-04 03:03:49 +00003334#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003335 #pragma omp critical (MagickCore_ImplodeImage)
cristy3ed852e2009-09-05 21:47:34 +00003336#endif
3337 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3338 if (proceed == MagickFalse)
3339 status=MagickFalse;
3340 }
3341 }
3342 implode_view=DestroyCacheView(implode_view);
cristy5ee937c2013-04-19 12:55:16 +00003343 interpolate_view=DestroyCacheView(interpolate_view);
cristy3ed852e2009-09-05 21:47:34 +00003344 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003345 if (status == MagickFalse)
3346 implode_image=DestroyImage(implode_image);
3347 return(implode_image);
3348}
3349
3350/*
3351%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3352% %
3353% %
3354% %
3355% M o r p h I m a g e s %
3356% %
3357% %
3358% %
3359%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3360%
3361% The MorphImages() method requires a minimum of two images. The first
3362% image is transformed into the second by a number of intervening images
3363% as specified by frames.
3364%
3365% The format of the MorphImage method is:
3366%
cristybb503372010-05-27 20:51:26 +00003367% Image *MorphImages(const Image *image,const size_t number_frames,
cristy3ed852e2009-09-05 21:47:34 +00003368% ExceptionInfo *exception)
3369%
3370% A description of each parameter follows:
3371%
3372% o image: the image.
3373%
3374% o number_frames: Define the number of in-between image to generate.
3375% The more in-between frames, the smoother the morph.
3376%
3377% o exception: return any errors or warnings in this structure.
3378%
3379*/
cristy7de7f742012-12-14 00:28:27 +00003380MagickExport Image *MorphImages(const Image *image,const size_t number_frames,
3381 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003382{
3383#define MorphImageTag "Morph/Image"
3384
cristyfab83642012-12-14 00:31:38 +00003385 double
3386 alpha,
3387 beta;
3388
cristy3ed852e2009-09-05 21:47:34 +00003389 Image
3390 *morph_image,
3391 *morph_images;
3392
cristy9d314ff2011-03-09 01:30:28 +00003393 MagickBooleanType
3394 status;
cristy3ed852e2009-09-05 21:47:34 +00003395
3396 MagickOffsetType
3397 scene;
3398
cristy3ed852e2009-09-05 21:47:34 +00003399 register const Image
3400 *next;
3401
cristybb503372010-05-27 20:51:26 +00003402 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003403 i;
3404
cristy9d314ff2011-03-09 01:30:28 +00003405 ssize_t
3406 y;
cristy3ed852e2009-09-05 21:47:34 +00003407
3408 /*
3409 Clone first frame in sequence.
3410 */
3411 assert(image != (Image *) NULL);
3412 assert(image->signature == MagickSignature);
3413 if (image->debug != MagickFalse)
3414 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3415 assert(exception != (ExceptionInfo *) NULL);
3416 assert(exception->signature == MagickSignature);
3417 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3418 if (morph_images == (Image *) NULL)
3419 return((Image *) NULL);
3420 if (GetNextImageInList(image) == (Image *) NULL)
3421 {
3422 /*
3423 Morph single image.
3424 */
cristybb503372010-05-27 20:51:26 +00003425 for (i=1; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003426 {
3427 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3428 if (morph_image == (Image *) NULL)
3429 {
3430 morph_images=DestroyImageList(morph_images);
3431 return((Image *) NULL);
3432 }
3433 AppendImageToList(&morph_images,morph_image);
cristy8b27a6d2010-02-14 03:31:15 +00003434 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003435 {
cristy8b27a6d2010-02-14 03:31:15 +00003436 MagickBooleanType
3437 proceed;
3438
cristybb503372010-05-27 20:51:26 +00003439 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3440 number_frames);
cristy8b27a6d2010-02-14 03:31:15 +00003441 if (proceed == MagickFalse)
3442 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00003443 }
3444 }
3445 return(GetFirstImageInList(morph_images));
3446 }
3447 /*
3448 Morph image sequence.
3449 */
3450 status=MagickTrue;
3451 scene=0;
3452 next=image;
3453 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3454 {
cristybb503372010-05-27 20:51:26 +00003455 for (i=0; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003456 {
3457 CacheView
3458 *image_view,
3459 *morph_view;
3460
cristya19f1d72012-08-07 18:24:38 +00003461 beta=(double) (i+1.0)/(double) (number_frames+1.0);
cristy3ed852e2009-09-05 21:47:34 +00003462 alpha=1.0-beta;
cristy15b98cd2010-09-12 19:42:50 +00003463 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
cristyaa2c16c2012-03-25 22:21:35 +00003464 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*next->rows+beta*
3465 GetNextImageInList(next)->rows+0.5),next->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +00003466 if (morph_image == (Image *) NULL)
3467 {
3468 morph_images=DestroyImageList(morph_images);
3469 return((Image *) NULL);
3470 }
cristy1707c6c2012-01-18 23:30:54 +00003471 status=SetImageStorageClass(morph_image,DirectClass,exception);
3472 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003473 {
cristy3ed852e2009-09-05 21:47:34 +00003474 morph_image=DestroyImage(morph_image);
3475 return((Image *) NULL);
3476 }
3477 AppendImageToList(&morph_images,morph_image);
3478 morph_images=GetLastImageInList(morph_images);
cristy15b98cd2010-09-12 19:42:50 +00003479 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
cristyaa2c16c2012-03-25 22:21:35 +00003480 morph_images->rows,GetNextImageInList(next)->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +00003481 if (morph_image == (Image *) NULL)
3482 {
3483 morph_images=DestroyImageList(morph_images);
3484 return((Image *) NULL);
3485 }
cristy46ff2672012-12-14 15:32:26 +00003486 image_view=AcquireVirtualCacheView(morph_image,exception);
3487 morph_view=AcquireAuthenticCacheView(morph_images,exception);
cristyb5d5f722009-11-04 03:03:49 +00003488#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003489 #pragma omp parallel for schedule(static,4) shared(status) \
cristy5e6b2592012-12-19 14:08:11 +00003490 magick_threads(morph_image,morph_image,morph_image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003491#endif
cristybb503372010-05-27 20:51:26 +00003492 for (y=0; y < (ssize_t) morph_images->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003493 {
3494 MagickBooleanType
3495 sync;
3496
cristy4c08aed2011-07-01 19:47:50 +00003497 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003498 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003499
cristybb503372010-05-27 20:51:26 +00003500 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003501 x;
3502
cristy4c08aed2011-07-01 19:47:50 +00003503 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003504 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003505
3506 if (status == MagickFalse)
3507 continue;
3508 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3509 exception);
3510 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3511 exception);
cristy4c08aed2011-07-01 19:47:50 +00003512 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003513 {
3514 status=MagickFalse;
3515 continue;
3516 }
cristybb503372010-05-27 20:51:26 +00003517 for (x=0; x < (ssize_t) morph_images->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003518 {
cristy1707c6c2012-01-18 23:30:54 +00003519 register ssize_t
3520 i;
3521
cristy10a6c612012-01-29 21:41:05 +00003522 for (i=0; i < (ssize_t) GetPixelChannels(morph_image); i++)
cristy1707c6c2012-01-18 23:30:54 +00003523 {
cristy5a23c552013-02-13 14:34:28 +00003524 PixelChannel channel=GetPixelChannelChannel(image,i);
3525 PixelTrait traits=GetPixelChannelTraits(image,channel);
3526 PixelTrait morph_traits=GetPixelChannelTraits(morph_image,channel);
cristy1707c6c2012-01-18 23:30:54 +00003527 if ((traits == UndefinedPixelTrait) ||
3528 (morph_traits == UndefinedPixelTrait))
3529 continue;
cristy1eced092012-08-10 23:10:56 +00003530 if (((morph_traits & CopyPixelTrait) != 0) ||
cristy883fde12013-04-08 00:50:13 +00003531 (GetPixelReadMask(image,p) == 0))
cristy1707c6c2012-01-18 23:30:54 +00003532 {
3533 SetPixelChannel(morph_image,channel,p[i],q);
3534 continue;
3535 }
3536 SetPixelChannel(morph_image,channel,ClampToQuantum(alpha*
3537 GetPixelChannel(morph_images,channel,q)+beta*p[i]),q);
3538 }
cristyed231572011-07-14 02:18:59 +00003539 p+=GetPixelChannels(morph_image);
3540 q+=GetPixelChannels(morph_images);
cristy3ed852e2009-09-05 21:47:34 +00003541 }
3542 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3543 if (sync == MagickFalse)
3544 status=MagickFalse;
3545 }
3546 morph_view=DestroyCacheView(morph_view);
3547 image_view=DestroyCacheView(image_view);
3548 morph_image=DestroyImage(morph_image);
3549 }
cristybb503372010-05-27 20:51:26 +00003550 if (i < (ssize_t) number_frames)
cristy3ed852e2009-09-05 21:47:34 +00003551 break;
3552 /*
3553 Clone last frame in sequence.
3554 */
3555 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3556 if (morph_image == (Image *) NULL)
3557 {
3558 morph_images=DestroyImageList(morph_images);
3559 return((Image *) NULL);
3560 }
3561 AppendImageToList(&morph_images,morph_image);
3562 morph_images=GetLastImageInList(morph_images);
3563 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3564 {
3565 MagickBooleanType
3566 proceed;
3567
cristyb5d5f722009-11-04 03:03:49 +00003568#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00003569 #pragma omp critical (MagickCore_MorphImages)
cristy3ed852e2009-09-05 21:47:34 +00003570#endif
3571 proceed=SetImageProgress(image,MorphImageTag,scene,
3572 GetImageListLength(image));
3573 if (proceed == MagickFalse)
3574 status=MagickFalse;
3575 }
3576 scene++;
3577 }
3578 if (GetNextImageInList(next) != (Image *) NULL)
3579 {
3580 morph_images=DestroyImageList(morph_images);
3581 return((Image *) NULL);
3582 }
3583 return(GetFirstImageInList(morph_images));
3584}
3585
3586/*
3587%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3588% %
3589% %
3590% %
3591% P l a s m a I m a g e %
3592% %
3593% %
3594% %
3595%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3596%
3597% PlasmaImage() initializes an image with plasma fractal values. The image
3598% must be initialized with a base color and the random number generator
3599% seeded before this method is called.
3600%
3601% The format of the PlasmaImage method is:
3602%
3603% MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
cristy5cbc0162011-08-29 00:36:28 +00003604% size_t attenuate,size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003605%
3606% A description of each parameter follows:
3607%
3608% o image: the image.
3609%
3610% o segment: Define the region to apply plasma fractals values.
3611%
glennrp7dae1ca2010-09-16 12:17:35 +00003612% o attenuate: Define the plasma attenuation factor.
cristy3ed852e2009-09-05 21:47:34 +00003613%
3614% o depth: Limit the plasma recursion depth.
3615%
cristy5cbc0162011-08-29 00:36:28 +00003616% o exception: return any errors or warnings in this structure.
3617%
cristy3ed852e2009-09-05 21:47:34 +00003618*/
3619
3620static inline Quantum PlasmaPixel(RandomInfo *random_info,
cristya19f1d72012-08-07 18:24:38 +00003621 const double pixel,const double noise)
cristy3ed852e2009-09-05 21:47:34 +00003622{
3623 Quantum
3624 plasma;
3625
cristyce70c172010-01-07 17:15:30 +00003626 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
cristy3ed852e2009-09-05 21:47:34 +00003627 noise/2.0);
3628 return(plasma);
3629}
3630
cristyda1f9c12011-10-02 21:39:49 +00003631static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3632 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3633 const SegmentInfo *segment,size_t attenuate,size_t depth,
3634 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003635{
cristya19f1d72012-08-07 18:24:38 +00003636 double
cristy3ed852e2009-09-05 21:47:34 +00003637 plasma;
3638
cristyda1f9c12011-10-02 21:39:49 +00003639 register const Quantum
3640 *restrict u,
3641 *restrict v;
3642
3643 register Quantum
3644 *restrict q;
3645
3646 register ssize_t
3647 i;
cristy3ed852e2009-09-05 21:47:34 +00003648
cristy9d314ff2011-03-09 01:30:28 +00003649 ssize_t
3650 x,
3651 x_mid,
3652 y,
3653 y_mid;
3654
cristy3ed852e2009-09-05 21:47:34 +00003655 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3656 return(MagickTrue);
3657 if (depth != 0)
3658 {
3659 SegmentInfo
3660 local_info;
3661
3662 /*
3663 Divide the area into quadrants and recurse.
3664 */
3665 depth--;
3666 attenuate++;
cristybb503372010-05-27 20:51:26 +00003667 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3668 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003669 local_info=(*segment);
3670 local_info.x2=(double) x_mid;
3671 local_info.y2=(double) y_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.y1=(double) y_mid;
3676 local_info.x2=(double) x_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.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003682 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3683 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003684 local_info=(*segment);
3685 local_info.x1=(double) x_mid;
3686 local_info.y1=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003687 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3688 &local_info,attenuate,depth,exception));
cristy3ed852e2009-09-05 21:47:34 +00003689 }
cristybb503372010-05-27 20:51:26 +00003690 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3691 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003692 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3693 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3694 return(MagickFalse);
3695 /*
3696 Average pixels and apply plasma.
3697 */
cristya19f1d72012-08-07 18:24:38 +00003698 plasma=(double) QuantumRange/(2.0*attenuate);
cristy3ed852e2009-09-05 21:47:34 +00003699 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3700 {
cristy3ed852e2009-09-05 21:47:34 +00003701 /*
3702 Left pixel.
3703 */
cristybb503372010-05-27 20:51:26 +00003704 x=(ssize_t) ceil(segment->x1-0.5);
cristy1707c6c2012-01-18 23:30:54 +00003705 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),1,1,
3706 exception);
3707 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),1,1,
3708 exception);
cristyc5c6f662010-09-22 14:23:02 +00003709 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003710 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3711 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003712 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003713 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3714 {
cristy5a23c552013-02-13 14:34:28 +00003715 PixelChannel channel=GetPixelChannelChannel(image,i);
3716 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003717 if (traits == UndefinedPixelTrait)
3718 continue;
cristy6e564992013-05-24 01:21:04 +00003719 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
cristyda1f9c12011-10-02 21:39:49 +00003720 }
cristyc5c6f662010-09-22 14:23:02 +00003721 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003722 if (segment->x1 != segment->x2)
3723 {
3724 /*
3725 Right pixel.
3726 */
cristybb503372010-05-27 20:51:26 +00003727 x=(ssize_t) ceil(segment->x2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003728 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3729 1,1,exception);
3730 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3731 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003732 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003733 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3734 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003735 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003736 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3737 {
cristy5a23c552013-02-13 14:34:28 +00003738 PixelChannel channel=GetPixelChannelChannel(image,i);
3739 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003740 if (traits == UndefinedPixelTrait)
3741 continue;
cristy6e564992013-05-24 01:21:04 +00003742 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
cristyda1f9c12011-10-02 21:39:49 +00003743 }
cristyc5c6f662010-09-22 14:23:02 +00003744 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003745 }
3746 }
3747 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3748 {
3749 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3750 {
cristy3ed852e2009-09-05 21:47:34 +00003751 /*
3752 Bottom pixel.
3753 */
cristybb503372010-05-27 20:51:26 +00003754 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003755 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3756 1,1,exception);
3757 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3758 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003759 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003760 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3761 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003762 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003763 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3764 {
cristy5a23c552013-02-13 14:34:28 +00003765 PixelChannel channel=GetPixelChannelChannel(image,i);
3766 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003767 if (traits == UndefinedPixelTrait)
3768 continue;
cristy6e564992013-05-24 01:21:04 +00003769 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
cristyda1f9c12011-10-02 21:39:49 +00003770 }
cristyc5c6f662010-09-22 14:23:02 +00003771 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003772 }
3773 if (segment->y1 != segment->y2)
3774 {
cristy3ed852e2009-09-05 21:47:34 +00003775 /*
3776 Top pixel.
3777 */
cristybb503372010-05-27 20:51:26 +00003778 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003779 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3780 1,1,exception);
3781 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3782 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003783 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003784 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3785 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003786 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003787 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3788 {
cristy5a23c552013-02-13 14:34:28 +00003789 PixelChannel channel=GetPixelChannelChannel(image,i);
3790 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003791 if (traits == UndefinedPixelTrait)
3792 continue;
cristy6e564992013-05-24 01:21:04 +00003793 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
cristyda1f9c12011-10-02 21:39:49 +00003794 }
cristyc5c6f662010-09-22 14:23:02 +00003795 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003796 }
3797 }
3798 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3799 {
cristy3ed852e2009-09-05 21:47:34 +00003800 /*
3801 Middle pixel.
3802 */
cristybb503372010-05-27 20:51:26 +00003803 x=(ssize_t) ceil(segment->x1-0.5);
3804 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003805 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
cristybb503372010-05-27 20:51:26 +00003806 x=(ssize_t) ceil(segment->x2-0.5);
3807 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003808 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003809 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003810 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3811 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003812 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003813 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3814 {
cristy5a23c552013-02-13 14:34:28 +00003815 PixelChannel channel=GetPixelChannelChannel(image,i);
3816 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003817 if (traits == UndefinedPixelTrait)
3818 continue;
cristy6e564992013-05-24 01:21:04 +00003819 q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
cristyda1f9c12011-10-02 21:39:49 +00003820 }
cristyc5c6f662010-09-22 14:23:02 +00003821 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003822 }
3823 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3824 return(MagickTrue);
3825 return(MagickFalse);
3826}
cristyda1f9c12011-10-02 21:39:49 +00003827
cristy3ed852e2009-09-05 21:47:34 +00003828MagickExport MagickBooleanType PlasmaImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00003829 const SegmentInfo *segment,size_t attenuate,size_t depth,
3830 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003831{
cristyc5c6f662010-09-22 14:23:02 +00003832 CacheView
cristyda1f9c12011-10-02 21:39:49 +00003833 *image_view,
3834 *u_view,
3835 *v_view;
cristyc5c6f662010-09-22 14:23:02 +00003836
cristy3ed852e2009-09-05 21:47:34 +00003837 MagickBooleanType
3838 status;
3839
3840 RandomInfo
3841 *random_info;
3842
3843 if (image->debug != MagickFalse)
3844 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3845 assert(image != (Image *) NULL);
3846 assert(image->signature == MagickSignature);
3847 if (image->debug != MagickFalse)
3848 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy5cbc0162011-08-29 00:36:28 +00003849 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristyc5c6f662010-09-22 14:23:02 +00003850 return(MagickFalse);
cristy46ff2672012-12-14 15:32:26 +00003851 image_view=AcquireAuthenticCacheView(image,exception);
3852 u_view=AcquireVirtualCacheView(image,exception);
3853 v_view=AcquireVirtualCacheView(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003854 random_info=AcquireRandomInfo();
cristyda1f9c12011-10-02 21:39:49 +00003855 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3856 attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003857 random_info=DestroyRandomInfo(random_info);
cristyda1f9c12011-10-02 21:39:49 +00003858 v_view=DestroyCacheView(v_view);
3859 u_view=DestroyCacheView(u_view);
cristyc5c6f662010-09-22 14:23:02 +00003860 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003861 return(status);
3862}
3863
3864/*
3865%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3866% %
3867% %
3868% %
3869% P o l a r o i d I m a g e %
3870% %
3871% %
3872% %
3873%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3874%
3875% PolaroidImage() simulates a Polaroid picture.
3876%
3877% The format of the AnnotateImage method is:
3878%
3879% Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003880% const char *caption,const double angle,
3881% const PixelInterpolateMethod method,ExceptionInfo exception)
cristy3ed852e2009-09-05 21:47:34 +00003882%
3883% A description of each parameter follows:
3884%
3885% o image: the image.
3886%
3887% o draw_info: the draw info.
3888%
cristye9e3d382011-12-14 01:50:13 +00003889% o caption: the Polaroid caption.
3890%
cristycee97112010-05-28 00:44:52 +00003891% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00003892%
cristy5c4e2582011-09-11 19:21:03 +00003893% o method: the pixel interpolation method.
3894%
cristy3ed852e2009-09-05 21:47:34 +00003895% o exception: return any errors or warnings in this structure.
3896%
3897*/
3898MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003899 const char *caption,const double angle,const PixelInterpolateMethod method,
cristy5c4e2582011-09-11 19:21:03 +00003900 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003901{
cristy3ed852e2009-09-05 21:47:34 +00003902 Image
3903 *bend_image,
3904 *caption_image,
3905 *flop_image,
3906 *picture_image,
3907 *polaroid_image,
3908 *rotate_image,
3909 *trim_image;
3910
cristybb503372010-05-27 20:51:26 +00003911 size_t
cristy3ed852e2009-09-05 21:47:34 +00003912 height;
3913
cristy9d314ff2011-03-09 01:30:28 +00003914 ssize_t
3915 quantum;
3916
cristy3ed852e2009-09-05 21:47:34 +00003917 /*
3918 Simulate a Polaroid picture.
3919 */
3920 assert(image != (Image *) NULL);
3921 assert(image->signature == MagickSignature);
3922 if (image->debug != MagickFalse)
3923 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3924 assert(exception != (ExceptionInfo *) NULL);
3925 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00003926 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
cristy3ed852e2009-09-05 21:47:34 +00003927 image->rows)/25.0,10.0);
3928 height=image->rows+2*quantum;
3929 caption_image=(Image *) NULL;
cristye9e3d382011-12-14 01:50:13 +00003930 if (caption != (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003931 {
3932 char
cristye9e3d382011-12-14 01:50:13 +00003933 geometry[MaxTextExtent],
3934 *text;
cristy3ed852e2009-09-05 21:47:34 +00003935
3936 DrawInfo
3937 *annotate_info;
3938
cristy72e0b772013-04-28 14:29:45 +00003939 ImageInfo
3940 *image_info;
3941
cristy3ed852e2009-09-05 21:47:34 +00003942 MagickBooleanType
3943 status;
3944
cristy9d314ff2011-03-09 01:30:28 +00003945 ssize_t
3946 count;
3947
cristy3ed852e2009-09-05 21:47:34 +00003948 TypeMetric
3949 metrics;
3950
3951 /*
3952 Generate caption image.
3953 */
3954 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3955 if (caption_image == (Image *) NULL)
3956 return((Image *) NULL);
cristy72e0b772013-04-28 14:29:45 +00003957 image_info=AcquireImageInfo();
cristy3ed852e2009-09-05 21:47:34 +00003958 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
cristy72e0b772013-04-28 14:29:45 +00003959 text=InterpretImageProperties(image_info,(Image *) image,caption,
cristye9e3d382011-12-14 01:50:13 +00003960 exception);
cristy72e0b772013-04-28 14:29:45 +00003961 image_info=DestroyImageInfo(image_info);
cristye9e3d382011-12-14 01:50:13 +00003962 (void) CloneString(&annotate_info->text,text);
cristy6b1d05e2010-09-22 19:17:27 +00003963 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
cristye9e3d382011-12-14 01:50:13 +00003964 &text,exception);
3965 status=SetImageExtent(caption_image,image->columns,(size_t) ((count+1)*
3966 (metrics.ascent-metrics.descent)+0.5),exception);
cristy3ed852e2009-09-05 21:47:34 +00003967 if (status == MagickFalse)
3968 caption_image=DestroyImage(caption_image);
3969 else
3970 {
3971 caption_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003972 (void) SetImageBackgroundColor(caption_image,exception);
cristye9e3d382011-12-14 01:50:13 +00003973 (void) CloneString(&annotate_info->text,text);
cristyb51dff52011-05-19 16:55:47 +00003974 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
cristy3ed852e2009-09-05 21:47:34 +00003975 metrics.ascent);
3976 if (annotate_info->gravity == UndefinedGravity)
3977 (void) CloneString(&annotate_info->geometry,AcquireString(
3978 geometry));
cristy5cbc0162011-08-29 00:36:28 +00003979 (void) AnnotateImage(caption_image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003980 height+=caption_image->rows;
3981 }
3982 annotate_info=DestroyDrawInfo(annotate_info);
cristye9e3d382011-12-14 01:50:13 +00003983 text=DestroyString(text);
cristy3ed852e2009-09-05 21:47:34 +00003984 }
3985 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
3986 exception);
3987 if (picture_image == (Image *) NULL)
3988 {
3989 if (caption_image != (Image *) NULL)
3990 caption_image=DestroyImage(caption_image);
3991 return((Image *) NULL);
3992 }
3993 picture_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003994 (void) SetImageBackgroundColor(picture_image,exception);
cristy39172402012-03-30 13:04:39 +00003995 (void) CompositeImage(picture_image,image,OverCompositeOp,MagickTrue,quantum,
cristyfeb3e962012-03-29 17:25:55 +00003996 quantum,exception);
cristy3ed852e2009-09-05 21:47:34 +00003997 if (caption_image != (Image *) NULL)
3998 {
cristyfeb3e962012-03-29 17:25:55 +00003999 (void) CompositeImage(picture_image,caption_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00004000 MagickTrue,quantum,(ssize_t) (image->rows+3*quantum/2),exception);
cristy3ed852e2009-09-05 21:47:34 +00004001 caption_image=DestroyImage(caption_image);
4002 }
cristy9950d572011-10-01 18:22:35 +00004003 (void) QueryColorCompliance("none",AllCompliance,
4004 &picture_image->background_color,exception);
cristy63240882011-08-05 19:05:27 +00004005 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004006 rotate_image=RotateImage(picture_image,90.0,exception);
4007 picture_image=DestroyImage(picture_image);
4008 if (rotate_image == (Image *) NULL)
4009 return((Image *) NULL);
4010 picture_image=rotate_image;
4011 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
cristy5c4e2582011-09-11 19:21:03 +00004012 picture_image->columns,method,exception);
cristy3ed852e2009-09-05 21:47:34 +00004013 picture_image=DestroyImage(picture_image);
4014 if (bend_image == (Image *) NULL)
4015 return((Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00004016 picture_image=bend_image;
4017 rotate_image=RotateImage(picture_image,-90.0,exception);
4018 picture_image=DestroyImage(picture_image);
4019 if (rotate_image == (Image *) NULL)
4020 return((Image *) NULL);
4021 picture_image=rotate_image;
4022 picture_image->background_color=image->background_color;
anthonyf46d4262012-03-26 03:30:34 +00004023 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
cristy3ed852e2009-09-05 21:47:34 +00004024 exception);
4025 if (polaroid_image == (Image *) NULL)
4026 {
4027 picture_image=DestroyImage(picture_image);
4028 return(picture_image);
4029 }
4030 flop_image=FlopImage(polaroid_image,exception);
4031 polaroid_image=DestroyImage(polaroid_image);
4032 if (flop_image == (Image *) NULL)
4033 {
4034 picture_image=DestroyImage(picture_image);
4035 return(picture_image);
4036 }
4037 polaroid_image=flop_image;
cristyfeb3e962012-03-29 17:25:55 +00004038 (void) CompositeImage(polaroid_image,picture_image,OverCompositeOp,
cristy39172402012-03-30 13:04:39 +00004039 MagickTrue,(ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
cristy3ed852e2009-09-05 21:47:34 +00004040 picture_image=DestroyImage(picture_image);
cristy9950d572011-10-01 18:22:35 +00004041 (void) QueryColorCompliance("none",AllCompliance,
4042 &polaroid_image->background_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00004043 rotate_image=RotateImage(polaroid_image,angle,exception);
4044 polaroid_image=DestroyImage(polaroid_image);
4045 if (rotate_image == (Image *) NULL)
4046 return((Image *) NULL);
4047 polaroid_image=rotate_image;
4048 trim_image=TrimImage(polaroid_image,exception);
4049 polaroid_image=DestroyImage(polaroid_image);
4050 if (trim_image == (Image *) NULL)
4051 return((Image *) NULL);
4052 polaroid_image=trim_image;
4053 return(polaroid_image);
4054}
4055
4056/*
4057%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4058% %
4059% %
4060% %
cristy3ed852e2009-09-05 21:47:34 +00004061% S e p i a T o n e I m a g e %
4062% %
4063% %
4064% %
4065%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4066%
4067% MagickSepiaToneImage() applies a special effect to the image, similar to the
4068% effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4069% 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4070% threshold of 80% is a good starting point for a reasonable tone.
4071%
4072% The format of the SepiaToneImage method is:
4073%
4074% Image *SepiaToneImage(const Image *image,const double threshold,
4075% ExceptionInfo *exception)
4076%
4077% A description of each parameter follows:
4078%
4079% o image: the image.
4080%
4081% o threshold: the tone threshold.
4082%
4083% o exception: return any errors or warnings in this structure.
4084%
4085*/
4086MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4087 ExceptionInfo *exception)
4088{
4089#define SepiaToneImageTag "SepiaTone/Image"
4090
cristyc4c8d132010-01-07 01:58:38 +00004091 CacheView
4092 *image_view,
4093 *sepia_view;
4094
cristy3ed852e2009-09-05 21:47:34 +00004095 Image
4096 *sepia_image;
4097
cristy3ed852e2009-09-05 21:47:34 +00004098 MagickBooleanType
4099 status;
4100
cristybb503372010-05-27 20:51:26 +00004101 MagickOffsetType
4102 progress;
4103
4104 ssize_t
4105 y;
4106
cristy3ed852e2009-09-05 21:47:34 +00004107 /*
4108 Initialize sepia-toned image attributes.
4109 */
4110 assert(image != (const Image *) NULL);
4111 assert(image->signature == MagickSignature);
4112 if (image->debug != MagickFalse)
4113 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4114 assert(exception != (ExceptionInfo *) NULL);
4115 assert(exception->signature == MagickSignature);
cristy1707c6c2012-01-18 23:30:54 +00004116 sepia_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004117 if (sepia_image == (Image *) NULL)
4118 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004119 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004120 {
cristy3ed852e2009-09-05 21:47:34 +00004121 sepia_image=DestroyImage(sepia_image);
4122 return((Image *) NULL);
4123 }
4124 /*
4125 Tone each row of the image.
4126 */
4127 status=MagickTrue;
4128 progress=0;
cristy46ff2672012-12-14 15:32:26 +00004129 image_view=AcquireVirtualCacheView(image,exception);
4130 sepia_view=AcquireAuthenticCacheView(sepia_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00004131#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00004132 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00004133 magick_threads(image,sepia_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00004134#endif
cristybb503372010-05-27 20:51:26 +00004135 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004136 {
cristy4c08aed2011-07-01 19:47:50 +00004137 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004138 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004139
cristybb503372010-05-27 20:51:26 +00004140 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004141 x;
4142
cristy4c08aed2011-07-01 19:47:50 +00004143 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004144 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004145
4146 if (status == MagickFalse)
4147 continue;
4148 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00004149 q=GetCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00004150 exception);
cristy4c08aed2011-07-01 19:47:50 +00004151 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004152 {
4153 status=MagickFalse;
4154 continue;
4155 }
cristybb503372010-05-27 20:51:26 +00004156 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004157 {
cristya19f1d72012-08-07 18:24:38 +00004158 double
cristy3ed852e2009-09-05 21:47:34 +00004159 intensity,
4160 tone;
4161
cristyf13c5942012-08-08 23:50:11 +00004162 intensity=GetPixelIntensity(image,p);
cristya19f1d72012-08-07 18:24:38 +00004163 tone=intensity > threshold ? (double) QuantumRange : intensity+
4164 (double) QuantumRange-threshold;
cristy4c08aed2011-07-01 19:47:50 +00004165 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
cristya19f1d72012-08-07 18:24:38 +00004166 tone=intensity > (7.0*threshold/6.0) ? (double) QuantumRange :
4167 intensity+(double) QuantumRange-7.0*threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004168 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004169 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004170 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004171 tone=threshold/7.0;
cristya19f1d72012-08-07 18:24:38 +00004172 if ((double) GetPixelGreen(image,q) < tone)
cristy4c08aed2011-07-01 19:47:50 +00004173 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
cristya19f1d72012-08-07 18:24:38 +00004174 if ((double) GetPixelBlue(image,q) < tone)
cristy4c08aed2011-07-01 19:47:50 +00004175 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristyed231572011-07-14 02:18:59 +00004176 p+=GetPixelChannels(image);
4177 q+=GetPixelChannels(sepia_image);
cristy3ed852e2009-09-05 21:47:34 +00004178 }
4179 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4180 status=MagickFalse;
4181 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4182 {
4183 MagickBooleanType
4184 proceed;
4185
cristyb5d5f722009-11-04 03:03:49 +00004186#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004187 #pragma omp critical (MagickCore_SepiaToneImage)
cristy3ed852e2009-09-05 21:47:34 +00004188#endif
4189 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4190 image->rows);
4191 if (proceed == MagickFalse)
4192 status=MagickFalse;
4193 }
4194 }
4195 sepia_view=DestroyCacheView(sepia_view);
4196 image_view=DestroyCacheView(image_view);
cristye23ec9d2011-08-16 18:15:40 +00004197 (void) NormalizeImage(sepia_image,exception);
4198 (void) ContrastImage(sepia_image,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004199 if (status == MagickFalse)
4200 sepia_image=DestroyImage(sepia_image);
4201 return(sepia_image);
4202}
4203
4204/*
4205%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4206% %
4207% %
4208% %
4209% S h a d o w I m a g e %
4210% %
4211% %
4212% %
4213%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4214%
4215% ShadowImage() simulates a shadow from the specified image and returns it.
4216%
4217% The format of the ShadowImage method is:
4218%
cristy70cddf72011-12-10 22:42:42 +00004219% Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004220% const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4221% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004222%
4223% A description of each parameter follows:
4224%
4225% o image: the image.
4226%
cristy70cddf72011-12-10 22:42:42 +00004227% o alpha: percentage transparency.
cristy3ed852e2009-09-05 21:47:34 +00004228%
4229% o sigma: the standard deviation of the Gaussian, in pixels.
4230%
4231% o x_offset: the shadow x-offset.
4232%
4233% o y_offset: the shadow y-offset.
4234%
4235% o exception: return any errors or warnings in this structure.
4236%
4237*/
cristy70cddf72011-12-10 22:42:42 +00004238MagickExport Image *ShadowImage(const Image *image,const double alpha,
cristyaa2c16c2012-03-25 22:21:35 +00004239 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
4240 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004241{
4242#define ShadowImageTag "Shadow/Image"
4243
cristy70cddf72011-12-10 22:42:42 +00004244 CacheView
4245 *image_view;
4246
cristybd5a96c2011-08-21 00:04:26 +00004247 ChannelType
4248 channel_mask;
4249
cristy3ed852e2009-09-05 21:47:34 +00004250 Image
4251 *border_image,
4252 *clone_image,
4253 *shadow_image;
4254
cristy70cddf72011-12-10 22:42:42 +00004255 MagickBooleanType
4256 status;
4257
cristy3ed852e2009-09-05 21:47:34 +00004258 RectangleInfo
4259 border_info;
4260
cristy70cddf72011-12-10 22:42:42 +00004261 ssize_t
4262 y;
4263
cristy3ed852e2009-09-05 21:47:34 +00004264 assert(image != (Image *) NULL);
4265 assert(image->signature == MagickSignature);
4266 if (image->debug != MagickFalse)
4267 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4268 assert(exception != (ExceptionInfo *) NULL);
4269 assert(exception->signature == MagickSignature);
4270 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4271 if (clone_image == (Image *) NULL)
4272 return((Image *) NULL);
cristya6400b12013-03-15 12:20:18 +00004273 if (IsGrayColorspace(image->colorspace) != MagickFalse)
cristy0c81d062013-04-21 15:22:02 +00004274 (void) SetImageColorspace(clone_image,sRGBColorspace,exception);
cristy0ce08762012-06-30 01:33:18 +00004275 (void) SetImageVirtualPixelMethod(clone_image,TransparentVirtualPixelMethod,
cristy387430f2012-02-07 13:09:46 +00004276 exception);
cristybb503372010-05-27 20:51:26 +00004277 border_info.width=(size_t) floor(2.0*sigma+0.5);
4278 border_info.height=(size_t) floor(2.0*sigma+0.5);
cristy3ed852e2009-09-05 21:47:34 +00004279 border_info.x=0;
4280 border_info.y=0;
cristy9950d572011-10-01 18:22:35 +00004281 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4282 exception);
cristy8a46d822012-08-28 23:32:39 +00004283 clone_image->alpha_trait=BlendPixelTrait;
cristy70cddf72011-12-10 22:42:42 +00004284 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
cristy3ed852e2009-09-05 21:47:34 +00004285 clone_image=DestroyImage(clone_image);
4286 if (border_image == (Image *) NULL)
4287 return((Image *) NULL);
cristy8a46d822012-08-28 23:32:39 +00004288 if (border_image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00004289 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004290 /*
4291 Shadow image.
4292 */
cristy70cddf72011-12-10 22:42:42 +00004293 status=MagickTrue;
cristy46ff2672012-12-14 15:32:26 +00004294 image_view=AcquireAuthenticCacheView(border_image,exception);
cristy70cddf72011-12-10 22:42:42 +00004295 for (y=0; y < (ssize_t) border_image->rows; y++)
4296 {
4297 PixelInfo
4298 background_color;
4299
4300 register Quantum
4301 *restrict q;
4302
4303 register ssize_t
4304 x;
4305
4306 if (status == MagickFalse)
4307 continue;
4308 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
4309 exception);
4310 if (q == (Quantum *) NULL)
4311 {
4312 status=MagickFalse;
4313 continue;
4314 }
4315 background_color=border_image->background_color;
cristy8a46d822012-08-28 23:32:39 +00004316 background_color.alpha_trait=BlendPixelTrait;
cristy70cddf72011-12-10 22:42:42 +00004317 for (x=0; x < (ssize_t) border_image->columns; x++)
4318 {
cristy8a46d822012-08-28 23:32:39 +00004319 if (border_image->alpha_trait == BlendPixelTrait)
cristy70cddf72011-12-10 22:42:42 +00004320 background_color.alpha=GetPixelAlpha(border_image,q)*alpha/100.0;
4321 SetPixelInfoPixel(border_image,&background_color,q);
4322 q+=GetPixelChannels(border_image);
4323 }
4324 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4325 status=MagickFalse;
4326 }
4327 image_view=DestroyCacheView(image_view);
4328 if (status == MagickFalse)
4329 {
4330 border_image=DestroyImage(border_image);
4331 return((Image *) NULL);
4332 }
cristycf1296e2012-08-26 23:40:49 +00004333 channel_mask=SetImageChannelMask(border_image,AlphaChannel);
cristyaa2c16c2012-03-25 22:21:35 +00004334 shadow_image=BlurImage(border_image,0.0,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00004335 border_image=DestroyImage(border_image);
4336 if (shadow_image == (Image *) NULL)
4337 return((Image *) NULL);
cristycf1296e2012-08-26 23:40:49 +00004338 (void) SetPixelChannelMask(shadow_image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00004339 if (shadow_image->page.width == 0)
4340 shadow_image->page.width=shadow_image->columns;
4341 if (shadow_image->page.height == 0)
4342 shadow_image->page.height=shadow_image->rows;
cristybb503372010-05-27 20:51:26 +00004343 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4344 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4345 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4346 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
cristy3ed852e2009-09-05 21:47:34 +00004347 return(shadow_image);
4348}
4349
4350/*
4351%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4352% %
4353% %
4354% %
4355% S k e t c h I m a g e %
4356% %
4357% %
4358% %
4359%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4360%
4361% SketchImage() simulates a pencil sketch. We convolve the image with a
4362% Gaussian operator of the given radius and standard deviation (sigma). For
4363% reasonable results, radius should be larger than sigma. Use a radius of 0
4364% and SketchImage() selects a suitable radius for you. Angle gives the angle
4365% of the sketch.
4366%
4367% The format of the SketchImage method is:
4368%
4369% Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004370% const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004371%
4372% A description of each parameter follows:
4373%
4374% o image: the image.
4375%
cristy574cc262011-08-05 01:23:58 +00004376% o radius: the radius of the Gaussian, in pixels, not counting the
4377% center pixel.
cristy3ed852e2009-09-05 21:47:34 +00004378%
4379% o sigma: the standard deviation of the Gaussian, in pixels.
4380%
cristyf7ef0252011-09-09 14:50:06 +00004381% o angle: apply the effect along this angle.
4382%
cristy3ed852e2009-09-05 21:47:34 +00004383% o exception: return any errors or warnings in this structure.
4384%
4385*/
4386MagickExport Image *SketchImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00004387 const double sigma,const double angle,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004388{
cristyfa112112010-01-04 17:48:07 +00004389 CacheView
4390 *random_view;
4391
cristy3ed852e2009-09-05 21:47:34 +00004392 Image
4393 *blend_image,
4394 *blur_image,
4395 *dodge_image,
4396 *random_image,
4397 *sketch_image;
4398
cristy3ed852e2009-09-05 21:47:34 +00004399 MagickBooleanType
4400 status;
4401
cristy3ed852e2009-09-05 21:47:34 +00004402 RandomInfo
cristyfa112112010-01-04 17:48:07 +00004403 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00004404
cristy9d314ff2011-03-09 01:30:28 +00004405 ssize_t
4406 y;
4407
glennrpf7659d72012-09-24 18:14:56 +00004408#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +00004409 unsigned long
4410 key;
glennrpf7659d72012-09-24 18:14:56 +00004411#endif
cristy57340e02012-05-05 00:53:23 +00004412
cristy3ed852e2009-09-05 21:47:34 +00004413 /*
4414 Sketch image.
4415 */
4416 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4417 MagickTrue,exception);
4418 if (random_image == (Image *) NULL)
4419 return((Image *) NULL);
4420 status=MagickTrue;
cristy1b784432009-12-19 02:20:40 +00004421 random_info=AcquireRandomInfoThreadSet();
glennrpf7659d72012-09-24 18:14:56 +00004422#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +00004423 key=GetRandomSecretKey(random_info[0]);
glennrpf7659d72012-09-24 18:14:56 +00004424#endif
cristy46ff2672012-12-14 15:32:26 +00004425 random_view=AcquireAuthenticCacheView(random_image,exception);
cristy1b784432009-12-19 02:20:40 +00004426#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +00004427 #pragma omp parallel for schedule(static,4) shared(status) \
cristy5e6b2592012-12-19 14:08:11 +00004428 magick_threads(random_image,random_image,random_image->rows,key == ~0UL)
cristy1b784432009-12-19 02:20:40 +00004429#endif
cristybb503372010-05-27 20:51:26 +00004430 for (y=0; y < (ssize_t) random_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004431 {
cristy5c9e6f22010-09-17 17:31:01 +00004432 const int
4433 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00004434
cristybb503372010-05-27 20:51:26 +00004435 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004436 x;
4437
cristy4c08aed2011-07-01 19:47:50 +00004438 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004439 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004440
cristy1b784432009-12-19 02:20:40 +00004441 if (status == MagickFalse)
4442 continue;
cristy3ed852e2009-09-05 21:47:34 +00004443 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4444 exception);
cristyacd2ed22011-08-30 01:44:23 +00004445 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004446 {
4447 status=MagickFalse;
4448 continue;
4449 }
cristybb503372010-05-27 20:51:26 +00004450 for (x=0; x < (ssize_t) random_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004451 {
cristya19f1d72012-08-07 18:24:38 +00004452 double
cristy76f512e2011-09-12 01:26:56 +00004453 value;
4454
4455 register ssize_t
4456 i;
4457
cristy883fde12013-04-08 00:50:13 +00004458 if (GetPixelReadMask(random_image,q) == 0)
cristy10a6c612012-01-29 21:41:05 +00004459 {
4460 q+=GetPixelChannels(random_image);
4461 continue;
4462 }
cristy76f512e2011-09-12 01:26:56 +00004463 value=GetPseudoRandomValue(random_info[id]);
4464 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4465 {
cristy5a23c552013-02-13 14:34:28 +00004466 PixelChannel channel=GetPixelChannelChannel(image,i);
4467 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004468 if (traits == UndefinedPixelTrait)
4469 continue;
4470 q[i]=ClampToQuantum(QuantumRange*value);
4471 }
cristyed231572011-07-14 02:18:59 +00004472 q+=GetPixelChannels(random_image);
cristy3ed852e2009-09-05 21:47:34 +00004473 }
4474 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4475 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004476 }
4477 random_view=DestroyCacheView(random_view);
cristy1b784432009-12-19 02:20:40 +00004478 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00004479 if (status == MagickFalse)
4480 {
4481 random_image=DestroyImage(random_image);
4482 return(random_image);
4483 }
cristyaa2c16c2012-03-25 22:21:35 +00004484 blur_image=MotionBlurImage(random_image,radius,sigma,angle,exception);
cristy3ed852e2009-09-05 21:47:34 +00004485 random_image=DestroyImage(random_image);
4486 if (blur_image == (Image *) NULL)
4487 return((Image *) NULL);
cristy800446a2013-05-20 20:15:38 +00004488 dodge_image=EdgeImage(blur_image,radius,exception);
cristy3ed852e2009-09-05 21:47:34 +00004489 blur_image=DestroyImage(blur_image);
4490 if (dodge_image == (Image *) NULL)
4491 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +00004492 (void) NormalizeImage(dodge_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +00004493 (void) NegateImage(dodge_image,MagickFalse,exception);
cristye941a752011-10-15 01:52:48 +00004494 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
cristy3ed852e2009-09-05 21:47:34 +00004495 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4496 if (sketch_image == (Image *) NULL)
4497 {
4498 dodge_image=DestroyImage(dodge_image);
4499 return((Image *) NULL);
4500 }
cristyfeb3e962012-03-29 17:25:55 +00004501 (void) CompositeImage(sketch_image,dodge_image,ColorDodgeCompositeOp,
cristy39172402012-03-30 13:04:39 +00004502 MagickTrue,0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004503 dodge_image=DestroyImage(dodge_image);
4504 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4505 if (blend_image == (Image *) NULL)
4506 {
4507 sketch_image=DestroyImage(sketch_image);
4508 return((Image *) NULL);
4509 }
4510 (void) SetImageArtifact(blend_image,"compose:args","20x80");
cristy39172402012-03-30 13:04:39 +00004511 (void) CompositeImage(sketch_image,blend_image,BlendCompositeOp,MagickTrue,
cristyfeb3e962012-03-29 17:25:55 +00004512 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004513 blend_image=DestroyImage(blend_image);
4514 return(sketch_image);
4515}
4516
4517/*
4518%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4519% %
4520% %
4521% %
4522% S o l a r i z e I m a g e %
4523% %
4524% %
4525% %
4526%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4527%
4528% SolarizeImage() applies a special effect to the image, similar to the effect
4529% achieved in a photo darkroom by selectively exposing areas of photo
4530% sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4531% measure of the extent of the solarization.
4532%
4533% The format of the SolarizeImage method is:
4534%
cristy5cbc0162011-08-29 00:36:28 +00004535% MagickBooleanType SolarizeImage(Image *image,const double threshold,
4536% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004537%
4538% A description of each parameter follows:
4539%
4540% o image: the image.
4541%
4542% o threshold: Define the extent of the solarization.
4543%
cristy5cbc0162011-08-29 00:36:28 +00004544% o exception: return any errors or warnings in this structure.
4545%
cristy3ed852e2009-09-05 21:47:34 +00004546*/
4547MagickExport MagickBooleanType SolarizeImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00004548 const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004549{
4550#define SolarizeImageTag "Solarize/Image"
4551
cristyc4c8d132010-01-07 01:58:38 +00004552 CacheView
4553 *image_view;
4554
cristy3ed852e2009-09-05 21:47:34 +00004555 MagickBooleanType
4556 status;
4557
cristybb503372010-05-27 20:51:26 +00004558 MagickOffsetType
4559 progress;
4560
4561 ssize_t
4562 y;
4563
cristy3ed852e2009-09-05 21:47:34 +00004564 assert(image != (Image *) NULL);
4565 assert(image->signature == MagickSignature);
4566 if (image->debug != MagickFalse)
4567 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristya6400b12013-03-15 12:20:18 +00004568 if (IsGrayColorspace(image->colorspace) != MagickFalse)
cristy0c81d062013-04-21 15:22:02 +00004569 (void) SetImageColorspace(image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00004570 if (image->storage_class == PseudoClass)
4571 {
cristybb503372010-05-27 20:51:26 +00004572 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004573 i;
4574
4575 /*
4576 Solarize colormap.
4577 */
cristybb503372010-05-27 20:51:26 +00004578 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00004579 {
cristya19f1d72012-08-07 18:24:38 +00004580 if ((double) image->colormap[i].red > threshold)
cristy6e963d82012-06-19 15:23:24 +00004581 image->colormap[i].red=QuantumRange-image->colormap[i].red;
cristya19f1d72012-08-07 18:24:38 +00004582 if ((double) image->colormap[i].green > threshold)
cristy6e963d82012-06-19 15:23:24 +00004583 image->colormap[i].green=QuantumRange-
cristy3ed852e2009-09-05 21:47:34 +00004584 image->colormap[i].green;
cristya19f1d72012-08-07 18:24:38 +00004585 if ((double) image->colormap[i].blue > threshold)
cristy6e963d82012-06-19 15:23:24 +00004586 image->colormap[i].blue=QuantumRange-
cristy3ed852e2009-09-05 21:47:34 +00004587 image->colormap[i].blue;
4588 }
4589 }
4590 /*
4591 Solarize image.
4592 */
4593 status=MagickTrue;
4594 progress=0;
cristy46ff2672012-12-14 15:32:26 +00004595 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00004596#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00004597 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00004598 magick_threads(image,image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00004599#endif
cristybb503372010-05-27 20:51:26 +00004600 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004601 {
cristybb503372010-05-27 20:51:26 +00004602 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004603 x;
4604
cristy4c08aed2011-07-01 19:47:50 +00004605 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004606 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004607
4608 if (status == MagickFalse)
4609 continue;
cristy5cbc0162011-08-29 00:36:28 +00004610 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00004611 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004612 {
4613 status=MagickFalse;
4614 continue;
4615 }
cristybb503372010-05-27 20:51:26 +00004616 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004617 {
cristy76f512e2011-09-12 01:26:56 +00004618 register ssize_t
4619 i;
4620
cristy883fde12013-04-08 00:50:13 +00004621 if (GetPixelReadMask(image,q) == 0)
cristy10a6c612012-01-29 21:41:05 +00004622 {
4623 q+=GetPixelChannels(image);
4624 continue;
4625 }
cristy76f512e2011-09-12 01:26:56 +00004626 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4627 {
cristy5a23c552013-02-13 14:34:28 +00004628 PixelChannel channel=GetPixelChannelChannel(image,i);
4629 PixelTrait traits=GetPixelChannelTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004630 if ((traits == UndefinedPixelTrait) ||
4631 ((traits & CopyPixelTrait) != 0))
4632 continue;
cristya19f1d72012-08-07 18:24:38 +00004633 if ((double) q[i] > threshold)
cristy76f512e2011-09-12 01:26:56 +00004634 q[i]=QuantumRange-q[i];
4635 }
cristyed231572011-07-14 02:18:59 +00004636 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004637 }
4638 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4639 status=MagickFalse;
4640 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4641 {
4642 MagickBooleanType
4643 proceed;
4644
cristyb5d5f722009-11-04 03:03:49 +00004645#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00004646 #pragma omp critical (MagickCore_SolarizeImage)
cristy3ed852e2009-09-05 21:47:34 +00004647#endif
4648 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4649 if (proceed == MagickFalse)
4650 status=MagickFalse;
4651 }
4652 }
4653 image_view=DestroyCacheView(image_view);
4654 return(status);
4655}
4656
4657/*
4658%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4659% %
4660% %
4661% %
4662% S t e g a n o I m a g e %
4663% %
4664% %
4665% %
4666%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4667%
4668% SteganoImage() hides a digital watermark within the image. Recover
4669% the hidden watermark later to prove that the authenticity of an image.
4670% Offset defines the start position within the image to hide the watermark.
4671%
4672% The format of the SteganoImage method is:
4673%
4674% Image *SteganoImage(const Image *image,Image *watermark,
4675% ExceptionInfo *exception)
4676%
4677% A description of each parameter follows:
4678%
4679% o image: the image.
4680%
4681% o watermark: the watermark image.
4682%
4683% o exception: return any errors or warnings in this structure.
4684%
4685*/
4686MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4687 ExceptionInfo *exception)
4688{
cristye1bf8ad2010-09-19 17:07:03 +00004689#define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
cristy4c08aed2011-07-01 19:47:50 +00004690#define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
cristyeaedf062010-05-29 22:36:02 +00004691 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
cristy3ed852e2009-09-05 21:47:34 +00004692#define SteganoImageTag "Stegano/Image"
4693
cristyb0d3bb92010-09-22 14:37:58 +00004694 CacheView
4695 *stegano_view,
4696 *watermark_view;
4697
cristy3ed852e2009-09-05 21:47:34 +00004698 Image
4699 *stegano_image;
4700
4701 int
4702 c;
4703
cristy3ed852e2009-09-05 21:47:34 +00004704 MagickBooleanType
4705 status;
4706
cristy101ab702011-10-13 13:06:32 +00004707 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004708 pixel;
4709
cristy4c08aed2011-07-01 19:47:50 +00004710 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004711 *q;
4712
cristye1bf8ad2010-09-19 17:07:03 +00004713 register ssize_t
4714 x;
4715
cristybb503372010-05-27 20:51:26 +00004716 size_t
cristyeaedf062010-05-29 22:36:02 +00004717 depth,
4718 one;
cristy3ed852e2009-09-05 21:47:34 +00004719
cristye1bf8ad2010-09-19 17:07:03 +00004720 ssize_t
4721 i,
4722 j,
4723 k,
4724 y;
4725
cristy3ed852e2009-09-05 21:47:34 +00004726 /*
4727 Initialize steganographic image attributes.
4728 */
4729 assert(image != (const Image *) NULL);
4730 assert(image->signature == MagickSignature);
4731 if (image->debug != MagickFalse)
4732 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4733 assert(watermark != (const Image *) NULL);
4734 assert(watermark->signature == MagickSignature);
4735 assert(exception != (ExceptionInfo *) NULL);
4736 assert(exception->signature == MagickSignature);
cristyeaedf062010-05-29 22:36:02 +00004737 one=1UL;
cristy3ed852e2009-09-05 21:47:34 +00004738 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4739 if (stegano_image == (Image *) NULL)
4740 return((Image *) NULL);
cristyf61b1832012-04-01 01:38:19 +00004741 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
cristy574cc262011-08-05 01:23:58 +00004742 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004743 {
cristy3ed852e2009-09-05 21:47:34 +00004744 stegano_image=DestroyImage(stegano_image);
4745 return((Image *) NULL);
4746 }
cristy3ed852e2009-09-05 21:47:34 +00004747 /*
4748 Hide watermark in low-order bits of image.
4749 */
4750 c=0;
4751 i=0;
4752 j=0;
4753 depth=stegano_image->depth;
cristyf61b1832012-04-01 01:38:19 +00004754 k=stegano_image->offset;
cristyda16f162011-02-19 23:52:17 +00004755 status=MagickTrue;
cristy46ff2672012-12-14 15:32:26 +00004756 watermark_view=AcquireVirtualCacheView(watermark,exception);
4757 stegano_view=AcquireAuthenticCacheView(stegano_image,exception);
cristybb503372010-05-27 20:51:26 +00004758 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
cristy3ed852e2009-09-05 21:47:34 +00004759 {
cristybb503372010-05-27 20:51:26 +00004760 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
cristy3ed852e2009-09-05 21:47:34 +00004761 {
cristybb503372010-05-27 20:51:26 +00004762 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
cristy3ed852e2009-09-05 21:47:34 +00004763 {
cristy1707c6c2012-01-18 23:30:54 +00004764 ssize_t
4765 offset;
4766
cristyf05d4942012-03-17 16:26:09 +00004767 (void) GetOneCacheViewVirtualPixelInfo(watermark_view,x,y,&pixel,
cristyda1f9c12011-10-02 21:39:49 +00004768 exception);
cristy1707c6c2012-01-18 23:30:54 +00004769 offset=k/(ssize_t) stegano_image->columns;
4770 if (offset >= (ssize_t) stegano_image->rows)
cristy3ed852e2009-09-05 21:47:34 +00004771 break;
cristyb0d3bb92010-09-22 14:37:58 +00004772 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4773 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4774 exception);
cristyacd2ed22011-08-30 01:44:23 +00004775 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004776 break;
4777 switch (c)
4778 {
4779 case 0:
4780 {
cristyf61b1832012-04-01 01:38:19 +00004781 SetPixelRed(stegano_image,SetBit(GetPixelRed(stegano_image,q),j,
4782 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004783 break;
4784 }
4785 case 1:
4786 {
cristyf61b1832012-04-01 01:38:19 +00004787 SetPixelGreen(stegano_image,SetBit(GetPixelGreen(stegano_image,q),j,
4788 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004789 break;
4790 }
4791 case 2:
4792 {
cristyf61b1832012-04-01 01:38:19 +00004793 SetPixelBlue(stegano_image,SetBit(GetPixelBlue(stegano_image,q),j,
4794 GetBit(GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004795 break;
4796 }
4797 }
cristyb0d3bb92010-09-22 14:37:58 +00004798 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004799 break;
4800 c++;
4801 if (c == 3)
4802 c=0;
4803 k++;
cristybb503372010-05-27 20:51:26 +00004804 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
cristy3ed852e2009-09-05 21:47:34 +00004805 k=0;
cristyf61b1832012-04-01 01:38:19 +00004806 if (k == stegano_image->offset)
cristy3ed852e2009-09-05 21:47:34 +00004807 j++;
4808 }
4809 }
cristy8b27a6d2010-02-14 03:31:15 +00004810 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004811 {
cristy8b27a6d2010-02-14 03:31:15 +00004812 MagickBooleanType
4813 proceed;
4814
4815 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4816 (depth-i),depth);
4817 if (proceed == MagickFalse)
4818 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004819 }
4820 }
cristyb0d3bb92010-09-22 14:37:58 +00004821 stegano_view=DestroyCacheView(stegano_view);
4822 watermark_view=DestroyCacheView(watermark_view);
cristyda16f162011-02-19 23:52:17 +00004823 if (status == MagickFalse)
cristyfab83642012-12-14 00:31:38 +00004824 stegano_image=DestroyImage(stegano_image);
cristy3ed852e2009-09-05 21:47:34 +00004825 return(stegano_image);
4826}
4827
4828/*
4829%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4830% %
4831% %
4832% %
4833% S t e r e o A n a g l y p h I m a g e %
4834% %
4835% %
4836% %
4837%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4838%
4839% StereoAnaglyphImage() combines two images and produces a single image that
4840% is the composite of a left and right image of a stereo pair. Special
4841% red-green stereo glasses are required to view this effect.
4842%
4843% The format of the StereoAnaglyphImage method is:
4844%
4845% Image *StereoImage(const Image *left_image,const Image *right_image,
4846% ExceptionInfo *exception)
4847% Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004848% const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004849% ExceptionInfo *exception)
4850%
4851% A description of each parameter follows:
4852%
4853% o left_image: the left image.
4854%
4855% o right_image: the right image.
4856%
4857% o exception: return any errors or warnings in this structure.
4858%
4859% o x_offset: amount, in pixels, by which the left image is offset to the
4860% right of the right image.
4861%
4862% o y_offset: amount, in pixels, by which the left image is offset to the
4863% bottom of the right image.
4864%
4865%
4866*/
4867MagickExport Image *StereoImage(const Image *left_image,
4868 const Image *right_image,ExceptionInfo *exception)
4869{
4870 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4871}
4872
4873MagickExport Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004874 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004875 ExceptionInfo *exception)
4876{
4877#define StereoImageTag "Stereo/Image"
4878
4879 const Image
4880 *image;
4881
4882 Image
4883 *stereo_image;
4884
cristy3ed852e2009-09-05 21:47:34 +00004885 MagickBooleanType
4886 status;
4887
cristy9d314ff2011-03-09 01:30:28 +00004888 ssize_t
4889 y;
4890
cristy3ed852e2009-09-05 21:47:34 +00004891 assert(left_image != (const Image *) NULL);
4892 assert(left_image->signature == MagickSignature);
4893 if (left_image->debug != MagickFalse)
4894 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4895 left_image->filename);
4896 assert(right_image != (const Image *) NULL);
4897 assert(right_image->signature == MagickSignature);
4898 assert(exception != (ExceptionInfo *) NULL);
4899 assert(exception->signature == MagickSignature);
4900 assert(right_image != (const Image *) NULL);
4901 image=left_image;
4902 if ((left_image->columns != right_image->columns) ||
4903 (left_image->rows != right_image->rows))
4904 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4905 /*
4906 Initialize stereo image attributes.
4907 */
4908 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4909 MagickTrue,exception);
4910 if (stereo_image == (Image *) NULL)
4911 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004912 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004913 {
cristy3ed852e2009-09-05 21:47:34 +00004914 stereo_image=DestroyImage(stereo_image);
4915 return((Image *) NULL);
4916 }
cristy079c78d2012-07-03 11:53:48 +00004917 (void) SetImageColorspace(stereo_image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00004918 /*
4919 Copy left image to red channel and right image to blue channel.
4920 */
cristyda16f162011-02-19 23:52:17 +00004921 status=MagickTrue;
cristybb503372010-05-27 20:51:26 +00004922 for (y=0; y < (ssize_t) stereo_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004923 {
cristy4c08aed2011-07-01 19:47:50 +00004924 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004925 *restrict p,
4926 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004927
cristybb503372010-05-27 20:51:26 +00004928 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004929 x;
4930
cristy4c08aed2011-07-01 19:47:50 +00004931 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004932 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +00004933
4934 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4935 exception);
4936 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4937 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
cristy76f512e2011-09-12 01:26:56 +00004938 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4939 (r == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004940 break;
cristybb503372010-05-27 20:51:26 +00004941 for (x=0; x < (ssize_t) stereo_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004942 {
cristy4c08aed2011-07-01 19:47:50 +00004943 SetPixelRed(image,GetPixelRed(left_image,p),r);
cristy76f512e2011-09-12 01:26:56 +00004944 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4945 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4946 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4947 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4948 GetPixelAlpha(right_image,q))/2,r);
cristyed231572011-07-14 02:18:59 +00004949 p+=GetPixelChannels(left_image);
cristy76f512e2011-09-12 01:26:56 +00004950 q+=GetPixelChannels(right_image);
4951 r+=GetPixelChannels(stereo_image);
cristy3ed852e2009-09-05 21:47:34 +00004952 }
4953 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4954 break;
cristy8b27a6d2010-02-14 03:31:15 +00004955 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004956 {
cristy8b27a6d2010-02-14 03:31:15 +00004957 MagickBooleanType
4958 proceed;
4959
cristybb503372010-05-27 20:51:26 +00004960 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4961 stereo_image->rows);
cristy8b27a6d2010-02-14 03:31:15 +00004962 if (proceed == MagickFalse)
4963 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004964 }
4965 }
cristyda16f162011-02-19 23:52:17 +00004966 if (status == MagickFalse)
cristyfab83642012-12-14 00:31:38 +00004967 stereo_image=DestroyImage(stereo_image);
cristy3ed852e2009-09-05 21:47:34 +00004968 return(stereo_image);
4969}
4970
4971/*
4972%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4973% %
4974% %
4975% %
4976% S w i r l I m a g e %
4977% %
4978% %
4979% %
4980%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4981%
4982% SwirlImage() swirls the pixels about the center of the image, where
4983% degrees indicates the sweep of the arc through which each pixel is moved.
4984% You get a more dramatic effect as the degrees move from 1 to 360.
4985%
4986% The format of the SwirlImage method is:
4987%
4988% 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% A description of each parameter follows:
4992%
4993% o image: the image.
4994%
4995% o degrees: Define the tightness of the swirling effect.
4996%
cristy76f512e2011-09-12 01:26:56 +00004997% o method: the pixel interpolation method.
4998%
cristy3ed852e2009-09-05 21:47:34 +00004999% o exception: return any errors or warnings in this structure.
5000%
5001*/
5002MagickExport Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00005003 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005004{
5005#define SwirlImageTag "Swirl/Image"
5006
cristyfa112112010-01-04 17:48:07 +00005007 CacheView
5008 *image_view,
cristy5ee937c2013-04-19 12:55:16 +00005009 *interpolate_view,
cristyfa112112010-01-04 17:48:07 +00005010 *swirl_view;
5011
cristy3ed852e2009-09-05 21:47:34 +00005012 Image
5013 *swirl_image;
5014
cristy3ed852e2009-09-05 21:47:34 +00005015 MagickBooleanType
5016 status;
5017
cristybb503372010-05-27 20:51:26 +00005018 MagickOffsetType
5019 progress;
5020
cristya19f1d72012-08-07 18:24:38 +00005021 double
cristy3ed852e2009-09-05 21:47:34 +00005022 radius;
5023
5024 PointInfo
5025 center,
5026 scale;
5027
cristybb503372010-05-27 20:51:26 +00005028 ssize_t
5029 y;
5030
cristy3ed852e2009-09-05 21:47:34 +00005031 /*
5032 Initialize swirl image attributes.
5033 */
5034 assert(image != (const Image *) NULL);
5035 assert(image->signature == MagickSignature);
5036 if (image->debug != MagickFalse)
5037 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5038 assert(exception != (ExceptionInfo *) NULL);
5039 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00005040 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005041 if (swirl_image == (Image *) NULL)
5042 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005043 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005044 {
cristy3ed852e2009-09-05 21:47:34 +00005045 swirl_image=DestroyImage(swirl_image);
5046 return((Image *) NULL);
5047 }
cristy4c08aed2011-07-01 19:47:50 +00005048 if (swirl_image->background_color.alpha != OpaqueAlpha)
cristy8a46d822012-08-28 23:32:39 +00005049 swirl_image->alpha_trait=BlendPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +00005050 /*
5051 Compute scaling factor.
5052 */
5053 center.x=(double) image->columns/2.0;
5054 center.y=(double) image->rows/2.0;
5055 radius=MagickMax(center.x,center.y);
5056 scale.x=1.0;
5057 scale.y=1.0;
5058 if (image->columns > image->rows)
5059 scale.y=(double) image->columns/(double) image->rows;
5060 else
5061 if (image->columns < image->rows)
5062 scale.x=(double) image->rows/(double) image->columns;
5063 degrees=(double) DegreesToRadians(degrees);
5064 /*
5065 Swirl image.
5066 */
5067 status=MagickTrue;
5068 progress=0;
cristy46ff2672012-12-14 15:32:26 +00005069 image_view=AcquireVirtualCacheView(image,exception);
cristy5ee937c2013-04-19 12:55:16 +00005070 interpolate_view=AcquireVirtualCacheView(image,exception);
cristy46ff2672012-12-14 15:32:26 +00005071 swirl_view=AcquireAuthenticCacheView(swirl_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00005072#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005073 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00005074 magick_threads(image,swirl_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005075#endif
cristybb503372010-05-27 20:51:26 +00005076 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005077 {
cristya19f1d72012-08-07 18:24:38 +00005078 double
cristy3ed852e2009-09-05 21:47:34 +00005079 distance;
5080
5081 PointInfo
5082 delta;
5083
cristy6d188022011-09-12 13:23:33 +00005084 register const Quantum
5085 *restrict p;
5086
cristybb503372010-05-27 20:51:26 +00005087 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005088 x;
5089
cristy4c08aed2011-07-01 19:47:50 +00005090 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005091 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005092
5093 if (status == MagickFalse)
5094 continue;
cristy1665ce12013-04-19 12:18:43 +00005095 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00005096 q=QueueCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00005097 exception);
cristy6d188022011-09-12 13:23:33 +00005098 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005099 {
5100 status=MagickFalse;
5101 continue;
5102 }
cristy3ed852e2009-09-05 21:47:34 +00005103 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00005104 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005105 {
5106 /*
5107 Determine if the pixel is within an ellipse.
5108 */
cristy883fde12013-04-08 00:50:13 +00005109 if (GetPixelReadMask(image,p) == 0)
cristy10a6c612012-01-29 21:41:05 +00005110 {
5111 p+=GetPixelChannels(image);
5112 q+=GetPixelChannels(swirl_image);
5113 continue;
5114 }
cristy3ed852e2009-09-05 21:47:34 +00005115 delta.x=scale.x*(double) (x-center.x);
5116 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00005117 if (distance >= (radius*radius))
5118 {
cristy1707c6c2012-01-18 23:30:54 +00005119 register ssize_t
5120 i;
5121
cristy6d188022011-09-12 13:23:33 +00005122 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy1707c6c2012-01-18 23:30:54 +00005123 {
cristy5a23c552013-02-13 14:34:28 +00005124 PixelChannel channel=GetPixelChannelChannel(image,i);
5125 PixelTrait traits=GetPixelChannelTraits(image,channel);
5126 PixelTrait swirl_traits=GetPixelChannelTraits(swirl_image,channel);
cristy1707c6c2012-01-18 23:30:54 +00005127 if ((traits == UndefinedPixelTrait) ||
5128 (swirl_traits == UndefinedPixelTrait))
5129 continue;
5130 SetPixelChannel(swirl_image,channel,p[i],q);
5131 }
cristy6d188022011-09-12 13:23:33 +00005132 }
5133 else
cristy3ed852e2009-09-05 21:47:34 +00005134 {
cristya19f1d72012-08-07 18:24:38 +00005135 double
cristy3ed852e2009-09-05 21:47:34 +00005136 cosine,
5137 factor,
5138 sine;
5139
5140 /*
5141 Swirl the pixel.
5142 */
5143 factor=1.0-sqrt((double) distance)/radius;
5144 sine=sin((double) (degrees*factor*factor));
5145 cosine=cos((double) (degrees*factor*factor));
cristy5ee937c2013-04-19 12:55:16 +00005146 status=InterpolatePixelChannels(image,interpolate_view,swirl_image,
5147 method,((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
cristy76f512e2011-09-12 01:26:56 +00005148 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00005149 }
cristy6d188022011-09-12 13:23:33 +00005150 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00005151 q+=GetPixelChannels(swirl_image);
cristy3ed852e2009-09-05 21:47:34 +00005152 }
5153 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5154 status=MagickFalse;
5155 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5156 {
5157 MagickBooleanType
5158 proceed;
5159
cristyb5d5f722009-11-04 03:03:49 +00005160#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005161 #pragma omp critical (MagickCore_SwirlImage)
cristy3ed852e2009-09-05 21:47:34 +00005162#endif
5163 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5164 if (proceed == MagickFalse)
5165 status=MagickFalse;
5166 }
5167 }
5168 swirl_view=DestroyCacheView(swirl_view);
cristy5ee937c2013-04-19 12:55:16 +00005169 interpolate_view=DestroyCacheView(interpolate_view);
cristy3ed852e2009-09-05 21:47:34 +00005170 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005171 if (status == MagickFalse)
5172 swirl_image=DestroyImage(swirl_image);
5173 return(swirl_image);
5174}
5175
5176/*
5177%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5178% %
5179% %
5180% %
5181% T i n t I m a g e %
5182% %
5183% %
5184% %
5185%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5186%
5187% TintImage() applies a color vector to each pixel in the image. The length
5188% of the vector is 0 for black and white and at its maximum for the midtones.
5189% The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5190%
5191% The format of the TintImage method is:
5192%
cristyb817c3f2011-10-03 14:00:35 +00005193% Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005194% const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005195%
5196% A description of each parameter follows:
5197%
5198% o image: the image.
5199%
cristyb817c3f2011-10-03 14:00:35 +00005200% o blend: A color value used for tinting.
cristy3ed852e2009-09-05 21:47:34 +00005201%
5202% o tint: A color value used for tinting.
5203%
5204% o exception: return any errors or warnings in this structure.
5205%
5206*/
cristyb817c3f2011-10-03 14:00:35 +00005207MagickExport Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005208 const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005209{
5210#define TintImageTag "Tint/Image"
5211
cristyc4c8d132010-01-07 01:58:38 +00005212 CacheView
5213 *image_view,
5214 *tint_view;
5215
cristyf54150e2013-04-11 01:25:47 +00005216 double
5217 intensity;
5218
cristy3ed852e2009-09-05 21:47:34 +00005219 GeometryInfo
5220 geometry_info;
5221
5222 Image
5223 *tint_image;
5224
cristy3ed852e2009-09-05 21:47:34 +00005225 MagickBooleanType
5226 status;
5227
cristybb503372010-05-27 20:51:26 +00005228 MagickOffsetType
5229 progress;
cristy3ed852e2009-09-05 21:47:34 +00005230
cristy4c08aed2011-07-01 19:47:50 +00005231 PixelInfo
cristy1707c6c2012-01-18 23:30:54 +00005232 color_vector;
cristy3ed852e2009-09-05 21:47:34 +00005233
cristybb503372010-05-27 20:51:26 +00005234 MagickStatusType
5235 flags;
5236
5237 ssize_t
5238 y;
5239
cristy3ed852e2009-09-05 21:47:34 +00005240 /*
5241 Allocate tint image.
5242 */
5243 assert(image != (const Image *) NULL);
5244 assert(image->signature == MagickSignature);
5245 if (image->debug != MagickFalse)
5246 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5247 assert(exception != (ExceptionInfo *) NULL);
5248 assert(exception->signature == MagickSignature);
5249 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5250 if (tint_image == (Image *) NULL)
5251 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005252 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005253 {
cristy3ed852e2009-09-05 21:47:34 +00005254 tint_image=DestroyImage(tint_image);
5255 return((Image *) NULL);
5256 }
cristya6400b12013-03-15 12:20:18 +00005257 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
cristy4dab3802013-03-15 22:08:15 +00005258 (IsPixelInfoGray(tint) == MagickFalse))
cristya6400b12013-03-15 12:20:18 +00005259 (void) SetImageColorspace(tint_image,sRGBColorspace,exception);
cristyaed9c382011-10-03 17:54:21 +00005260 if (blend == (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005261 return(tint_image);
5262 /*
5263 Determine RGB values of the color.
5264 */
cristy1707c6c2012-01-18 23:30:54 +00005265 GetPixelInfo(image,&color_vector);
cristyb817c3f2011-10-03 14:00:35 +00005266 flags=ParseGeometry(blend,&geometry_info);
cristy1707c6c2012-01-18 23:30:54 +00005267 color_vector.red=geometry_info.rho;
5268 color_vector.green=geometry_info.rho;
5269 color_vector.blue=geometry_info.rho;
cristy92ac5722013-03-09 15:09:38 +00005270 color_vector.alpha=(MagickRealType) OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00005271 if ((flags & SigmaValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005272 color_vector.green=geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +00005273 if ((flags & XiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005274 color_vector.blue=geometry_info.xi;
cristyb817c3f2011-10-03 14:00:35 +00005275 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005276 color_vector.alpha=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005277 if (image->colorspace == CMYKColorspace)
5278 {
cristy1707c6c2012-01-18 23:30:54 +00005279 color_vector.black=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005280 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005281 color_vector.black=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005282 if ((flags & ChiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005283 color_vector.alpha=geometry_info.chi;
cristy76f512e2011-09-12 01:26:56 +00005284 }
cristya19f1d72012-08-07 18:24:38 +00005285 intensity=(double) GetPixelInfoIntensity(tint);
cristyf54150e2013-04-11 01:25:47 +00005286 color_vector.red=(double) (color_vector.red*tint->red/100.0-intensity);
5287 color_vector.green=(double) (color_vector.green*tint->green/100.0-intensity);
5288 color_vector.blue=(double) (color_vector.blue*tint->blue/100.0-intensity);
5289 color_vector.black=(double) (color_vector.black*tint->black/100.0-intensity);
5290 color_vector.alpha=(double) (color_vector.alpha*tint->alpha/100.0-intensity);
cristy3ed852e2009-09-05 21:47:34 +00005291 /*
5292 Tint image.
5293 */
5294 status=MagickTrue;
5295 progress=0;
cristy46ff2672012-12-14 15:32:26 +00005296 image_view=AcquireVirtualCacheView(image,exception);
5297 tint_view=AcquireAuthenticCacheView(tint_image,exception);
cristyb5d5f722009-11-04 03:03:49 +00005298#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005299 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00005300 magick_threads(image,tint_image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005301#endif
cristybb503372010-05-27 20:51:26 +00005302 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005303 {
cristy4c08aed2011-07-01 19:47:50 +00005304 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00005305 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00005306
cristy4c08aed2011-07-01 19:47:50 +00005307 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005308 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005309
cristy6b91acb2011-04-19 12:23:54 +00005310 register ssize_t
5311 x;
5312
cristy3ed852e2009-09-05 21:47:34 +00005313 if (status == MagickFalse)
5314 continue;
5315 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5316 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5317 exception);
cristy4c08aed2011-07-01 19:47:50 +00005318 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005319 {
5320 status=MagickFalse;
5321 continue;
5322 }
cristybb503372010-05-27 20:51:26 +00005323 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005324 {
cristy4c08aed2011-07-01 19:47:50 +00005325 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00005326 pixel;
5327
cristya19f1d72012-08-07 18:24:38 +00005328 double
cristy3ed852e2009-09-05 21:47:34 +00005329 weight;
5330
cristy1707c6c2012-01-18 23:30:54 +00005331 register ssize_t
5332 i;
5333
5334 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5335 {
cristy5a23c552013-02-13 14:34:28 +00005336 PixelChannel channel=GetPixelChannelChannel(image,i);
5337 PixelTrait traits=GetPixelChannelTraits(image,channel);
5338 PixelTrait tint_traits=GetPixelChannelTraits(tint_image,channel);
cristy1707c6c2012-01-18 23:30:54 +00005339 if ((traits == UndefinedPixelTrait) ||
5340 (tint_traits == UndefinedPixelTrait))
5341 continue;
cristy1eced092012-08-10 23:10:56 +00005342 if (((tint_traits & CopyPixelTrait) != 0) ||
cristy883fde12013-04-08 00:50:13 +00005343 (GetPixelReadMask(image,p) == 0))
cristy1707c6c2012-01-18 23:30:54 +00005344 {
5345 SetPixelChannel(tint_image,channel,p[i],q);
5346 continue;
5347 }
5348 }
5349 GetPixelInfo(image,&pixel);
5350 weight=QuantumScale*GetPixelRed(image,p)-0.5;
cristy1eced092012-08-10 23:10:56 +00005351 pixel.red=(double) GetPixelRed(image,p)+color_vector.red*(1.0-(4.0*
5352 (weight*weight)));
cristy1707c6c2012-01-18 23:30:54 +00005353 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
cristy1eced092012-08-10 23:10:56 +00005354 pixel.green=(double) GetPixelGreen(image,p)+color_vector.green*(1.0-(4.0*
5355 (weight*weight)));
cristy1707c6c2012-01-18 23:30:54 +00005356 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
cristy1eced092012-08-10 23:10:56 +00005357 pixel.blue=(double) GetPixelBlue(image,p)+color_vector.blue*(1.0-(4.0*
5358 (weight*weight)));
cristy1707c6c2012-01-18 23:30:54 +00005359 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
cristy1eced092012-08-10 23:10:56 +00005360 pixel.black=(double) GetPixelBlack(image,p)+color_vector.black*(1.0-(4.0*
5361 (weight*weight)));
cristy1707c6c2012-01-18 23:30:54 +00005362 SetPixelInfoPixel(tint_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00005363 p+=GetPixelChannels(image);
5364 q+=GetPixelChannels(tint_image);
cristy3ed852e2009-09-05 21:47:34 +00005365 }
5366 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5367 status=MagickFalse;
5368 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5369 {
5370 MagickBooleanType
5371 proceed;
5372
cristyb5d5f722009-11-04 03:03:49 +00005373#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005374 #pragma omp critical (MagickCore_TintImage)
cristy3ed852e2009-09-05 21:47:34 +00005375#endif
5376 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5377 if (proceed == MagickFalse)
5378 status=MagickFalse;
5379 }
5380 }
5381 tint_view=DestroyCacheView(tint_view);
5382 image_view=DestroyCacheView(image_view);
5383 if (status == MagickFalse)
5384 tint_image=DestroyImage(tint_image);
5385 return(tint_image);
5386}
5387
5388/*
5389%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5390% %
5391% %
5392% %
5393% V i g n e t t e I m a g e %
5394% %
5395% %
5396% %
5397%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5398%
5399% VignetteImage() softens the edges of the image in vignette style.
5400%
5401% The format of the VignetteImage method is:
5402%
5403% Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005404% const double sigma,const ssize_t x,const ssize_t y,
cristy05c0c9a2011-09-05 23:16:13 +00005405% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005406%
5407% A description of each parameter follows:
5408%
5409% o image: the image.
5410%
5411% o radius: the radius of the pixel neighborhood.
5412%
5413% o sigma: the standard deviation of the Gaussian, in pixels.
5414%
5415% o x, y: Define the x and y ellipse offset.
5416%
5417% o exception: return any errors or warnings in this structure.
5418%
5419*/
5420MagickExport Image *VignetteImage(const Image *image,const double radius,
cristyaa2c16c2012-03-25 22:21:35 +00005421 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005422{
5423 char
5424 ellipse[MaxTextExtent];
5425
5426 DrawInfo
5427 *draw_info;
5428
5429 Image
5430 *canvas_image,
5431 *blur_image,
5432 *oval_image,
5433 *vignette_image;
5434
5435 assert(image != (Image *) NULL);
5436 assert(image->signature == MagickSignature);
5437 if (image->debug != MagickFalse)
5438 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5439 assert(exception != (ExceptionInfo *) NULL);
5440 assert(exception->signature == MagickSignature);
5441 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5442 if (canvas_image == (Image *) NULL)
5443 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005444 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005445 {
cristy3ed852e2009-09-05 21:47:34 +00005446 canvas_image=DestroyImage(canvas_image);
5447 return((Image *) NULL);
5448 }
cristy8a46d822012-08-28 23:32:39 +00005449 canvas_image->alpha_trait=BlendPixelTrait;
cristy98621462011-12-31 22:31:11 +00005450 oval_image=CloneImage(canvas_image,canvas_image->columns,canvas_image->rows,
5451 MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005452 if (oval_image == (Image *) NULL)
5453 {
5454 canvas_image=DestroyImage(canvas_image);
5455 return((Image *) NULL);
5456 }
cristy9950d572011-10-01 18:22:35 +00005457 (void) QueryColorCompliance("#000000",AllCompliance,
5458 &oval_image->background_color,exception);
cristyea1a8aa2011-10-20 13:24:06 +00005459 (void) SetImageBackgroundColor(oval_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00005460 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
cristy9950d572011-10-01 18:22:35 +00005461 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5462 exception);
5463 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5464 exception);
cristy1707c6c2012-01-18 23:30:54 +00005465 (void) FormatLocaleString(ellipse,MaxTextExtent,"ellipse %g,%g,%g,%g,"
5466 "0.0,360.0",image->columns/2.0,image->rows/2.0,image->columns/2.0-x,
5467 image->rows/2.0-y);
cristy3ed852e2009-09-05 21:47:34 +00005468 draw_info->primitive=AcquireString(ellipse);
cristy018f07f2011-09-04 21:15:19 +00005469 (void) DrawImage(oval_image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00005470 draw_info=DestroyDrawInfo(draw_info);
cristyaa2c16c2012-03-25 22:21:35 +00005471 blur_image=BlurImage(oval_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00005472 oval_image=DestroyImage(oval_image);
5473 if (blur_image == (Image *) NULL)
5474 {
5475 canvas_image=DestroyImage(canvas_image);
5476 return((Image *) NULL);
5477 }
cristy8a46d822012-08-28 23:32:39 +00005478 blur_image->alpha_trait=UndefinedPixelTrait;
cristy39172402012-03-30 13:04:39 +00005479 (void) CompositeImage(canvas_image,blur_image,IntensityCompositeOp,MagickTrue,
5480 0,0,exception);
cristy3ed852e2009-09-05 21:47:34 +00005481 blur_image=DestroyImage(blur_image);
5482 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5483 canvas_image=DestroyImage(canvas_image);
cristy66d26122012-06-23 21:56:40 +00005484 if (vignette_image != (Image *) NULL)
5485 (void) TransformImageColorspace(vignette_image,image->colorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00005486 return(vignette_image);
5487}
5488
5489/*
5490%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5491% %
5492% %
5493% %
5494% W a v e I m a g e %
5495% %
5496% %
5497% %
5498%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5499%
5500% WaveImage() creates a "ripple" effect in the image by shifting the pixels
cristycee97112010-05-28 00:44:52 +00005501% vertically along a sine wave whose amplitude and wavelength is specified
cristy3ed852e2009-09-05 21:47:34 +00005502% by the given parameters.
5503%
5504% The format of the WaveImage method is:
5505%
5506% Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005507% const double wave_length,const PixelInterpolateMethod method,
5508% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005509%
5510% A description of each parameter follows:
5511%
5512% o image: the image.
5513%
5514% o amplitude, wave_length: Define the amplitude and wave length of the
5515% sine wave.
5516%
cristy5c4e2582011-09-11 19:21:03 +00005517% o interpolate: the pixel interpolation method.
5518%
cristy3ed852e2009-09-05 21:47:34 +00005519% o exception: return any errors or warnings in this structure.
5520%
5521*/
5522MagickExport Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005523 const double wave_length,const PixelInterpolateMethod method,
5524 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005525{
5526#define WaveImageTag "Wave/Image"
5527
cristyfa112112010-01-04 17:48:07 +00005528 CacheView
cristyd76c51e2011-03-26 00:21:26 +00005529 *image_view,
cristyfa112112010-01-04 17:48:07 +00005530 *wave_view;
5531
cristy3ed852e2009-09-05 21:47:34 +00005532 Image
5533 *wave_image;
5534
cristy3ed852e2009-09-05 21:47:34 +00005535 MagickBooleanType
5536 status;
5537
cristybb503372010-05-27 20:51:26 +00005538 MagickOffsetType
5539 progress;
5540
cristya19f1d72012-08-07 18:24:38 +00005541 double
cristy3ed852e2009-09-05 21:47:34 +00005542 *sine_map;
5543
cristybb503372010-05-27 20:51:26 +00005544 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005545 i;
5546
cristybb503372010-05-27 20:51:26 +00005547 ssize_t
5548 y;
5549
cristy3ed852e2009-09-05 21:47:34 +00005550 /*
5551 Initialize wave image attributes.
5552 */
5553 assert(image != (Image *) NULL);
5554 assert(image->signature == MagickSignature);
5555 if (image->debug != MagickFalse)
5556 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5557 assert(exception != (ExceptionInfo *) NULL);
5558 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00005559 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
cristy3ed852e2009-09-05 21:47:34 +00005560 fabs(amplitude)),MagickTrue,exception);
5561 if (wave_image == (Image *) NULL)
5562 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005563 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005564 {
cristy3ed852e2009-09-05 21:47:34 +00005565 wave_image=DestroyImage(wave_image);
5566 return((Image *) NULL);
5567 }
cristy4c08aed2011-07-01 19:47:50 +00005568 if (wave_image->background_color.alpha != OpaqueAlpha)
cristy8a46d822012-08-28 23:32:39 +00005569 wave_image->alpha_trait=BlendPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +00005570 /*
5571 Allocate sine map.
5572 */
cristya19f1d72012-08-07 18:24:38 +00005573 sine_map=(double *) AcquireQuantumMemory((size_t) wave_image->columns,
cristy3ed852e2009-09-05 21:47:34 +00005574 sizeof(*sine_map));
cristya19f1d72012-08-07 18:24:38 +00005575 if (sine_map == (double *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005576 {
5577 wave_image=DestroyImage(wave_image);
5578 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5579 }
cristybb503372010-05-27 20:51:26 +00005580 for (i=0; i < (ssize_t) wave_image->columns; i++)
cristy4205a3c2010-09-12 20:19:59 +00005581 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5582 wave_length));
cristy3ed852e2009-09-05 21:47:34 +00005583 /*
5584 Wave image.
5585 */
5586 status=MagickTrue;
5587 progress=0;
cristy46ff2672012-12-14 15:32:26 +00005588 image_view=AcquireVirtualCacheView(image,exception);
5589 wave_view=AcquireAuthenticCacheView(wave_image,exception);
cristyd76c51e2011-03-26 00:21:26 +00005590 (void) SetCacheViewVirtualPixelMethod(image_view,
5591 BackgroundVirtualPixelMethod);
cristyb5d5f722009-11-04 03:03:49 +00005592#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00005593 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00005594 magick_threads(image,wave_image,wave_image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00005595#endif
cristybb503372010-05-27 20:51:26 +00005596 for (y=0; y < (ssize_t) wave_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005597 {
cristy4c08aed2011-07-01 19:47:50 +00005598 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005599 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005600
cristye97bb922011-04-03 01:36:52 +00005601 register ssize_t
5602 x;
5603
cristy3ed852e2009-09-05 21:47:34 +00005604 if (status == MagickFalse)
5605 continue;
5606 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5607 exception);
cristyacd2ed22011-08-30 01:44:23 +00005608 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005609 {
5610 status=MagickFalse;
5611 continue;
5612 }
cristybb503372010-05-27 20:51:26 +00005613 for (x=0; x < (ssize_t) wave_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005614 {
cristy5c4e2582011-09-11 19:21:03 +00005615 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5616 (double) x,(double) (y-sine_map[x]),q,exception);
cristyed231572011-07-14 02:18:59 +00005617 q+=GetPixelChannels(wave_image);
cristy3ed852e2009-09-05 21:47:34 +00005618 }
5619 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5620 status=MagickFalse;
5621 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5622 {
5623 MagickBooleanType
5624 proceed;
5625
cristyb5d5f722009-11-04 03:03:49 +00005626#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy69cfa022012-01-23 12:47:29 +00005627 #pragma omp critical (MagickCore_WaveImage)
cristy3ed852e2009-09-05 21:47:34 +00005628#endif
5629 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5630 if (proceed == MagickFalse)
5631 status=MagickFalse;
5632 }
5633 }
5634 wave_view=DestroyCacheView(wave_view);
cristyd76c51e2011-03-26 00:21:26 +00005635 image_view=DestroyCacheView(image_view);
cristya19f1d72012-08-07 18:24:38 +00005636 sine_map=(double *) RelinquishMagickMemory(sine_map);
cristy3ed852e2009-09-05 21:47:34 +00005637 if (status == MagickFalse)
5638 wave_image=DestroyImage(wave_image);
5639 return(wave_image);
5640}