blob: bab0cf5ebb7dec5a0fa545a5bc2ed2d1e5d24b56 [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% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 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"
51#include "MagickCore/composite.h"
52#include "MagickCore/decorate.h"
53#include "MagickCore/draw.h"
54#include "MagickCore/effect.h"
55#include "MagickCore/enhance.h"
56#include "MagickCore/exception.h"
57#include "MagickCore/exception-private.h"
58#include "MagickCore/fx.h"
59#include "MagickCore/fx-private.h"
60#include "MagickCore/gem.h"
cristy8ea81222011-09-04 10:33:32 +000061#include "MagickCore/gem-private.h"
cristy4c08aed2011-07-01 19:47:50 +000062#include "MagickCore/geometry.h"
63#include "MagickCore/layer.h"
64#include "MagickCore/list.h"
65#include "MagickCore/log.h"
66#include "MagickCore/image.h"
67#include "MagickCore/image-private.h"
68#include "MagickCore/magick.h"
69#include "MagickCore/memory_.h"
70#include "MagickCore/monitor.h"
71#include "MagickCore/monitor-private.h"
72#include "MagickCore/option.h"
73#include "MagickCore/pixel.h"
74#include "MagickCore/pixel-accessor.h"
75#include "MagickCore/property.h"
76#include "MagickCore/quantum.h"
77#include "MagickCore/quantum-private.h"
78#include "MagickCore/random_.h"
79#include "MagickCore/random-private.h"
80#include "MagickCore/resample.h"
81#include "MagickCore/resample-private.h"
82#include "MagickCore/resize.h"
83#include "MagickCore/shear.h"
84#include "MagickCore/splay-tree.h"
85#include "MagickCore/statistic.h"
86#include "MagickCore/string_.h"
87#include "MagickCore/string-private.h"
88#include "MagickCore/thread-private.h"
89#include "MagickCore/transform.h"
90#include "MagickCore/utility.h"
cristy3ed852e2009-09-05 21:47:34 +000091
92/*
93 Define declarations.
94*/
95#define LeftShiftOperator 0xf5
96#define RightShiftOperator 0xf6
97#define LessThanEqualOperator 0xf7
98#define GreaterThanEqualOperator 0xf8
99#define EqualOperator 0xf9
100#define NotEqualOperator 0xfa
101#define LogicalAndOperator 0xfb
102#define LogicalOrOperator 0xfc
cristy116af162010-08-13 01:25:47 +0000103#define ExponentialNotation 0xfd
cristy3ed852e2009-09-05 21:47:34 +0000104
105struct _FxInfo
106{
107 const Image
108 *images;
109
cristy3ed852e2009-09-05 21:47:34 +0000110 char
111 *expression;
112
113 FILE
114 *file;
115
116 SplayTreeInfo
117 *colors,
118 *symbols;
119
cristyd76c51e2011-03-26 00:21:26 +0000120 CacheView
121 **view;
cristy3ed852e2009-09-05 21:47:34 +0000122
123 RandomInfo
124 *random_info;
125
126 ExceptionInfo
127 *exception;
128};
129
130/*
131%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
132% %
133% %
134% %
135+ A c q u i r e F x I n f o %
136% %
137% %
138% %
139%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140%
141% AcquireFxInfo() allocates the FxInfo structure.
142%
143% The format of the AcquireFxInfo method is:
144%
145% FxInfo *AcquireFxInfo(Image *image,const char *expression)
cristy0a9b3722010-10-23 18:45:49 +0000146%
cristy3ed852e2009-09-05 21:47:34 +0000147% A description of each parameter follows:
148%
149% o image: the image.
150%
151% o expression: the expression.
152%
153*/
cristy7832dc22011-09-05 01:21:53 +0000154MagickPrivate FxInfo *AcquireFxInfo(const Image *image,const char *expression)
cristy3ed852e2009-09-05 21:47:34 +0000155{
156 char
157 fx_op[2];
158
cristycb180922011-03-11 14:41:24 +0000159 const Image
160 *next;
161
cristy3ed852e2009-09-05 21:47:34 +0000162 FxInfo
163 *fx_info;
164
cristybb503372010-05-27 20:51:26 +0000165 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000166 i;
167
cristy73bd4a52010-10-05 11:24:23 +0000168 fx_info=(FxInfo *) AcquireMagickMemory(sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +0000169 if (fx_info == (FxInfo *) NULL)
170 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
171 (void) ResetMagickMemory(fx_info,0,sizeof(*fx_info));
172 fx_info->exception=AcquireExceptionInfo();
anthony7d86e172011-03-23 12:37:06 +0000173 fx_info->images=image;
cristy3ed852e2009-09-05 21:47:34 +0000174 fx_info->colors=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
175 RelinquishMagickMemory);
176 fx_info->symbols=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
177 RelinquishMagickMemory);
cristyd76c51e2011-03-26 00:21:26 +0000178 fx_info->view=(CacheView **) AcquireQuantumMemory(GetImageListLength(
179 fx_info->images),sizeof(*fx_info->view));
180 if (fx_info->view == (CacheView **) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000181 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
cristya2262262011-03-11 02:50:37 +0000182 i=0;
cristy0ea377f2011-03-24 00:54:19 +0000183 next=GetFirstImageInList(fx_info->images);
184 for ( ; next != (Image *) NULL; next=next->next)
cristy3ed852e2009-09-05 21:47:34 +0000185 {
cristyd76c51e2011-03-26 00:21:26 +0000186 fx_info->view[i]=AcquireCacheView(next);
cristya2262262011-03-11 02:50:37 +0000187 i++;
cristy3ed852e2009-09-05 21:47:34 +0000188 }
189 fx_info->random_info=AcquireRandomInfo();
190 fx_info->expression=ConstantString(expression);
191 fx_info->file=stderr;
192 (void) SubstituteString(&fx_info->expression," ",""); /* compact string */
cristy37af0912011-05-23 16:09:42 +0000193 /*
194 Force right-to-left associativity for unary negation.
195 */
196 (void) SubstituteString(&fx_info->expression,"-","-1.0*");
cristy8b8a3ae2010-10-23 18:49:46 +0000197 /*
cristy3ed852e2009-09-05 21:47:34 +0000198 Convert complex to simple operators.
199 */
200 fx_op[1]='\0';
201 *fx_op=(char) LeftShiftOperator;
202 (void) SubstituteString(&fx_info->expression,"<<",fx_op);
203 *fx_op=(char) RightShiftOperator;
204 (void) SubstituteString(&fx_info->expression,">>",fx_op);
205 *fx_op=(char) LessThanEqualOperator;
206 (void) SubstituteString(&fx_info->expression,"<=",fx_op);
207 *fx_op=(char) GreaterThanEqualOperator;
208 (void) SubstituteString(&fx_info->expression,">=",fx_op);
209 *fx_op=(char) EqualOperator;
210 (void) SubstituteString(&fx_info->expression,"==",fx_op);
211 *fx_op=(char) NotEqualOperator;
212 (void) SubstituteString(&fx_info->expression,"!=",fx_op);
213 *fx_op=(char) LogicalAndOperator;
214 (void) SubstituteString(&fx_info->expression,"&&",fx_op);
215 *fx_op=(char) LogicalOrOperator;
216 (void) SubstituteString(&fx_info->expression,"||",fx_op);
cristy116af162010-08-13 01:25:47 +0000217 *fx_op=(char) ExponentialNotation;
218 (void) SubstituteString(&fx_info->expression,"**",fx_op);
cristy3ed852e2009-09-05 21:47:34 +0000219 return(fx_info);
220}
221
222/*
223%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224% %
225% %
226% %
227% A d d N o i s e I m a g e %
228% %
229% %
230% %
231%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
232%
233% AddNoiseImage() adds random noise to the image.
234%
235% The format of the AddNoiseImage method is:
236%
237% Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
cristy9ed1f812011-10-08 02:00:08 +0000238% const double attenuate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000239%
240% A description of each parameter follows:
241%
242% o image: the image.
243%
244% o channel: the channel type.
245%
246% o noise_type: The type of noise: Uniform, Gaussian, Multiplicative,
247% Impulse, Laplacian, or Poisson.
248%
cristy9ed1f812011-10-08 02:00:08 +0000249% o attenuate: attenuate the random distribution.
250%
cristy3ed852e2009-09-05 21:47:34 +0000251% o exception: return any errors or warnings in this structure.
252%
253*/
cristy9ed1f812011-10-08 02:00:08 +0000254MagickExport Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
255 const double attenuate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000256{
257#define AddNoiseImageTag "AddNoise/Image"
258
cristyfa112112010-01-04 17:48:07 +0000259 CacheView
260 *image_view,
261 *noise_view;
262
cristy3ed852e2009-09-05 21:47:34 +0000263 Image
264 *noise_image;
265
cristy3ed852e2009-09-05 21:47:34 +0000266 MagickBooleanType
267 status;
268
cristybb503372010-05-27 20:51:26 +0000269 MagickOffsetType
270 progress;
271
cristy3ed852e2009-09-05 21:47:34 +0000272 RandomInfo
cristyfa112112010-01-04 17:48:07 +0000273 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +0000274
cristybb503372010-05-27 20:51:26 +0000275 ssize_t
276 y;
277
cristy3ed852e2009-09-05 21:47:34 +0000278 /*
279 Initialize noise image attributes.
280 */
281 assert(image != (const Image *) NULL);
282 assert(image->signature == MagickSignature);
283 if (image->debug != MagickFalse)
284 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
285 assert(exception != (ExceptionInfo *) NULL);
286 assert(exception->signature == MagickSignature);
cristyb2145892011-10-10 00:55:32 +0000287 noise_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000288 if (noise_image == (Image *) NULL)
289 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000290 if (SetImageStorageClass(noise_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000291 {
cristy3ed852e2009-09-05 21:47:34 +0000292 noise_image=DestroyImage(noise_image);
293 return((Image *) NULL);
294 }
295 /*
296 Add noise in each row.
297 */
cristy3ed852e2009-09-05 21:47:34 +0000298 status=MagickTrue;
299 progress=0;
300 random_info=AcquireRandomInfoThreadSet();
301 image_view=AcquireCacheView(image);
302 noise_view=AcquireCacheView(noise_image);
cristy319a1e72010-02-21 15:13:11 +0000303#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyb5d5f722009-11-04 03:03:49 +0000304 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000305#endif
cristybb503372010-05-27 20:51:26 +0000306 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000307 {
cristy5c9e6f22010-09-17 17:31:01 +0000308 const int
309 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +0000310
cristy3ed852e2009-09-05 21:47:34 +0000311 MagickBooleanType
312 sync;
313
cristy4c08aed2011-07-01 19:47:50 +0000314 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000315 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000316
cristybb503372010-05-27 20:51:26 +0000317 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000318 x;
319
cristy4c08aed2011-07-01 19:47:50 +0000320 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000321 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000322
323 if (status == MagickFalse)
324 continue;
325 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
326 q=GetCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
327 exception);
cristy4c08aed2011-07-01 19:47:50 +0000328 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000329 {
330 status=MagickFalse;
331 continue;
332 }
cristybb503372010-05-27 20:51:26 +0000333 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000334 {
cristy850b3072011-10-08 01:38:05 +0000335 register ssize_t
336 i;
337
338 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
339 {
340 PixelChannel
341 channel;
342
343 PixelTrait
344 noise_traits,
345 traits;
346
347 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
348 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
349 noise_traits=GetPixelChannelMapTraits(noise_image,channel);
350 if ((traits == UndefinedPixelTrait) ||
351 (noise_traits == UndefinedPixelTrait))
352 continue;
cristy9eeedea2011-11-02 19:04:05 +0000353 if ((noise_traits & CopyPixelTrait) != 0)
cristyb2145892011-10-10 00:55:32 +0000354 {
355 SetPixelChannel(noise_image,channel,p[i],q);
356 continue;
357 }
cristy850b3072011-10-08 01:38:05 +0000358 SetPixelChannel(noise_image,channel,ClampToQuantum(
359 GenerateDifferentialNoise(random_info[id],p[i],noise_type,attenuate)),
360 q);
361 }
cristyed231572011-07-14 02:18:59 +0000362 p+=GetPixelChannels(image);
363 q+=GetPixelChannels(noise_image);
cristy3ed852e2009-09-05 21:47:34 +0000364 }
365 sync=SyncCacheViewAuthenticPixels(noise_view,exception);
366 if (sync == MagickFalse)
367 status=MagickFalse;
368 if (image->progress_monitor != (MagickProgressMonitor) NULL)
369 {
370 MagickBooleanType
371 proceed;
372
cristyb5d5f722009-11-04 03:03:49 +0000373#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy319a1e72010-02-21 15:13:11 +0000374 #pragma omp critical (MagickCore_AddNoiseImage)
cristy3ed852e2009-09-05 21:47:34 +0000375#endif
376 proceed=SetImageProgress(image,AddNoiseImageTag,progress++,
377 image->rows);
378 if (proceed == MagickFalse)
379 status=MagickFalse;
380 }
381 }
382 noise_view=DestroyCacheView(noise_view);
383 image_view=DestroyCacheView(image_view);
384 random_info=DestroyRandomInfoThreadSet(random_info);
385 if (status == MagickFalse)
386 noise_image=DestroyImage(noise_image);
387 return(noise_image);
388}
389
390/*
391%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
392% %
393% %
394% %
395% B l u e S h i f t I m a g e %
396% %
397% %
398% %
399%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
400%
401% BlueShiftImage() mutes the colors of the image to simulate a scene at
402% nighttime in the moonlight.
403%
404% The format of the BlueShiftImage method is:
405%
406% Image *BlueShiftImage(const Image *image,const double factor,
407% ExceptionInfo *exception)
408%
409% A description of each parameter follows:
410%
411% o image: the image.
412%
413% o factor: the shift factor.
414%
415% o exception: return any errors or warnings in this structure.
416%
417*/
418MagickExport Image *BlueShiftImage(const Image *image,const double factor,
419 ExceptionInfo *exception)
420{
421#define BlueShiftImageTag "BlueShift/Image"
422
cristyc4c8d132010-01-07 01:58:38 +0000423 CacheView
424 *image_view,
425 *shift_view;
426
cristy3ed852e2009-09-05 21:47:34 +0000427 Image
428 *shift_image;
429
cristy3ed852e2009-09-05 21:47:34 +0000430 MagickBooleanType
431 status;
432
cristybb503372010-05-27 20:51:26 +0000433 MagickOffsetType
434 progress;
435
436 ssize_t
437 y;
438
cristy3ed852e2009-09-05 21:47:34 +0000439 /*
440 Allocate blue shift image.
441 */
442 assert(image != (const Image *) NULL);
443 assert(image->signature == MagickSignature);
444 if (image->debug != MagickFalse)
445 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
446 assert(exception != (ExceptionInfo *) NULL);
447 assert(exception->signature == MagickSignature);
448 shift_image=CloneImage(image,image->columns,image->rows,MagickTrue,
449 exception);
450 if (shift_image == (Image *) NULL)
451 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000452 if (SetImageStorageClass(shift_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000453 {
cristy3ed852e2009-09-05 21:47:34 +0000454 shift_image=DestroyImage(shift_image);
455 return((Image *) NULL);
456 }
457 /*
458 Blue-shift DirectClass image.
459 */
460 status=MagickTrue;
461 progress=0;
462 image_view=AcquireCacheView(image);
463 shift_view=AcquireCacheView(shift_image);
cristy319a1e72010-02-21 15:13:11 +0000464#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyb5d5f722009-11-04 03:03:49 +0000465 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000466#endif
cristybb503372010-05-27 20:51:26 +0000467 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000468 {
469 MagickBooleanType
470 sync;
471
cristy4c08aed2011-07-01 19:47:50 +0000472 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000473 pixel;
474
475 Quantum
476 quantum;
477
cristy4c08aed2011-07-01 19:47:50 +0000478 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000479 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000480
cristybb503372010-05-27 20:51:26 +0000481 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000482 x;
483
cristy4c08aed2011-07-01 19:47:50 +0000484 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000485 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000486
487 if (status == MagickFalse)
488 continue;
489 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
490 q=QueueCacheViewAuthenticPixels(shift_view,0,y,shift_image->columns,1,
491 exception);
cristy4c08aed2011-07-01 19:47:50 +0000492 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000493 {
494 status=MagickFalse;
495 continue;
496 }
cristybb503372010-05-27 20:51:26 +0000497 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000498 {
cristy4c08aed2011-07-01 19:47:50 +0000499 quantum=GetPixelRed(image,p);
500 if (GetPixelGreen(image,p) < quantum)
501 quantum=GetPixelGreen(image,p);
502 if (GetPixelBlue(image,p) < quantum)
503 quantum=GetPixelBlue(image,p);
504 pixel.red=0.5*(GetPixelRed(image,p)+factor*quantum);
505 pixel.green=0.5*(GetPixelGreen(image,p)+factor*quantum);
506 pixel.blue=0.5*(GetPixelBlue(image,p)+factor*quantum);
507 quantum=GetPixelRed(image,p);
508 if (GetPixelGreen(image,p) > quantum)
509 quantum=GetPixelGreen(image,p);
510 if (GetPixelBlue(image,p) > quantum)
511 quantum=GetPixelBlue(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000512 pixel.red=0.5*(pixel.red+factor*quantum);
513 pixel.green=0.5*(pixel.green+factor*quantum);
514 pixel.blue=0.5*(pixel.blue+factor*quantum);
cristy4c08aed2011-07-01 19:47:50 +0000515 SetPixelRed(shift_image,ClampToQuantum(pixel.red),q);
516 SetPixelGreen(shift_image,ClampToQuantum(pixel.green),q);
517 SetPixelBlue(shift_image,ClampToQuantum(pixel.blue),q);
cristyed231572011-07-14 02:18:59 +0000518 p+=GetPixelChannels(image);
519 q+=GetPixelChannels(shift_image);
cristy3ed852e2009-09-05 21:47:34 +0000520 }
521 sync=SyncCacheViewAuthenticPixels(shift_view,exception);
522 if (sync == MagickFalse)
523 status=MagickFalse;
524 if (image->progress_monitor != (MagickProgressMonitor) NULL)
525 {
526 MagickBooleanType
527 proceed;
528
cristy319a1e72010-02-21 15:13:11 +0000529#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000530 #pragma omp critical (MagickCore_BlueShiftImage)
531#endif
532 proceed=SetImageProgress(image,BlueShiftImageTag,progress++,
533 image->rows);
534 if (proceed == MagickFalse)
535 status=MagickFalse;
536 }
537 }
538 image_view=DestroyCacheView(image_view);
539 shift_view=DestroyCacheView(shift_view);
540 if (status == MagickFalse)
541 shift_image=DestroyImage(shift_image);
542 return(shift_image);
543}
544
545/*
546%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
547% %
548% %
549% %
550% C h a r c o a l I m a g e %
551% %
552% %
553% %
554%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
555%
556% CharcoalImage() creates a new image that is a copy of an existing one with
557% the edge highlighted. It allocates the memory necessary for the new Image
558% structure and returns a pointer to the new image.
559%
560% The format of the CharcoalImage method is:
561%
562% Image *CharcoalImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +0000563% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000564%
565% A description of each parameter follows:
566%
567% o image: the image.
568%
569% o radius: the radius of the pixel neighborhood.
570%
571% o sigma: the standard deviation of the Gaussian, in pixels.
572%
cristy05c0c9a2011-09-05 23:16:13 +0000573% o bias: the bias.
574%
cristy3ed852e2009-09-05 21:47:34 +0000575% o exception: return any errors or warnings in this structure.
576%
577*/
578MagickExport Image *CharcoalImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +0000579 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000580{
581 Image
582 *charcoal_image,
583 *clone_image,
584 *edge_image;
585
586 assert(image != (Image *) NULL);
587 assert(image->signature == MagickSignature);
588 if (image->debug != MagickFalse)
589 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
590 assert(exception != (ExceptionInfo *) NULL);
591 assert(exception->signature == MagickSignature);
592 clone_image=CloneImage(image,0,0,MagickTrue,exception);
593 if (clone_image == (Image *) NULL)
594 return((Image *) NULL);
cristy018f07f2011-09-04 21:15:19 +0000595 (void) SetImageType(clone_image,GrayscaleType,exception);
cristy8ae632d2011-09-05 17:29:53 +0000596 edge_image=EdgeImage(clone_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000597 clone_image=DestroyImage(clone_image);
598 if (edge_image == (Image *) NULL)
599 return((Image *) NULL);
cristy05c0c9a2011-09-05 23:16:13 +0000600 charcoal_image=BlurImage(edge_image,radius,sigma,bias,exception);
cristy3ed852e2009-09-05 21:47:34 +0000601 edge_image=DestroyImage(edge_image);
602 if (charcoal_image == (Image *) NULL)
603 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +0000604 (void) NormalizeImage(charcoal_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +0000605 (void) NegateImage(charcoal_image,MagickFalse,exception);
cristy018f07f2011-09-04 21:15:19 +0000606 (void) SetImageType(charcoal_image,GrayscaleType,exception);
cristy3ed852e2009-09-05 21:47:34 +0000607 return(charcoal_image);
608}
609
610/*
611%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
612% %
613% %
614% %
615% C o l o r i z e I m a g e %
616% %
617% %
618% %
619%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
620%
621% ColorizeImage() blends the fill color with each pixel in the image.
622% A percentage blend is specified with opacity. Control the application
623% of different color components by specifying a different percentage for
624% each component (e.g. 90/100/10 is 90% red, 100% green, and 10% blue).
625%
626% The format of the ColorizeImage method is:
627%
cristyc7e6ff62011-10-03 13:46:11 +0000628% Image *ColorizeImage(const Image *image,const char *blend,
629% const PixelInfo *colorize,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000630%
631% A description of each parameter follows:
632%
633% o image: the image.
634%
cristyc7e6ff62011-10-03 13:46:11 +0000635% o blend: A character string indicating the level of blending as a
cristy3ed852e2009-09-05 21:47:34 +0000636% percentage.
637%
638% o colorize: A color value.
639%
640% o exception: return any errors or warnings in this structure.
641%
642*/
cristyc7e6ff62011-10-03 13:46:11 +0000643MagickExport Image *ColorizeImage(const Image *image,const char *blend,
644 const PixelInfo *colorize,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000645{
646#define ColorizeImageTag "Colorize/Image"
647
cristyc4c8d132010-01-07 01:58:38 +0000648 CacheView
649 *colorize_view,
650 *image_view;
651
cristy3ed852e2009-09-05 21:47:34 +0000652 GeometryInfo
653 geometry_info;
654
655 Image
656 *colorize_image;
657
cristy3ed852e2009-09-05 21:47:34 +0000658 MagickBooleanType
659 status;
660
cristybb503372010-05-27 20:51:26 +0000661 MagickOffsetType
662 progress;
663
cristy3ed852e2009-09-05 21:47:34 +0000664 MagickStatusType
665 flags;
666
cristyc7e6ff62011-10-03 13:46:11 +0000667 PixelInfo
668 pixel;
669
cristybb503372010-05-27 20:51:26 +0000670 ssize_t
671 y;
672
cristy3ed852e2009-09-05 21:47:34 +0000673 /*
674 Allocate colorized image.
675 */
676 assert(image != (const Image *) NULL);
677 assert(image->signature == MagickSignature);
678 if (image->debug != MagickFalse)
679 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
680 assert(exception != (ExceptionInfo *) NULL);
681 assert(exception->signature == MagickSignature);
682 colorize_image=CloneImage(image,image->columns,image->rows,MagickTrue,
683 exception);
684 if (colorize_image == (Image *) NULL)
685 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000686 if (SetImageStorageClass(colorize_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000687 {
cristy3ed852e2009-09-05 21:47:34 +0000688 colorize_image=DestroyImage(colorize_image);
689 return((Image *) NULL);
690 }
cristyc7e6ff62011-10-03 13:46:11 +0000691 if (blend == (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000692 return(colorize_image);
693 /*
694 Determine RGB values of the pen color.
695 */
cristyc7e6ff62011-10-03 13:46:11 +0000696 GetPixelInfo(image,&pixel);
cristyb817c3f2011-10-03 14:00:35 +0000697 flags=ParseGeometry(blend,&geometry_info);
cristy3ed852e2009-09-05 21:47:34 +0000698 pixel.red=geometry_info.rho;
699 pixel.green=geometry_info.rho;
700 pixel.blue=geometry_info.rho;
cristyc7e6ff62011-10-03 13:46:11 +0000701 pixel.alpha=100.0;
cristy3ed852e2009-09-05 21:47:34 +0000702 if ((flags & SigmaValue) != 0)
703 pixel.green=geometry_info.sigma;
704 if ((flags & XiValue) != 0)
705 pixel.blue=geometry_info.xi;
706 if ((flags & PsiValue) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000707 pixel.alpha=geometry_info.psi;
cristyc7e6ff62011-10-03 13:46:11 +0000708 if (pixel.colorspace == CMYKColorspace)
709 {
710 pixel.black=geometry_info.rho;
711 if ((flags & PsiValue) != 0)
712 pixel.black=geometry_info.psi;
713 if ((flags & ChiValue) != 0)
714 pixel.alpha=geometry_info.chi;
715 }
cristy3ed852e2009-09-05 21:47:34 +0000716 /*
717 Colorize DirectClass image.
718 */
719 status=MagickTrue;
720 progress=0;
721 image_view=AcquireCacheView(image);
722 colorize_view=AcquireCacheView(colorize_image);
cristy319a1e72010-02-21 15:13:11 +0000723#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyb5d5f722009-11-04 03:03:49 +0000724 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000725#endif
cristybb503372010-05-27 20:51:26 +0000726 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000727 {
728 MagickBooleanType
729 sync;
730
cristy4c08aed2011-07-01 19:47:50 +0000731 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000732 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000733
cristybb503372010-05-27 20:51:26 +0000734 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000735 x;
736
cristy4c08aed2011-07-01 19:47:50 +0000737 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000738 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000739
740 if (status == MagickFalse)
741 continue;
742 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
743 q=QueueCacheViewAuthenticPixels(colorize_view,0,y,colorize_image->columns,1,
744 exception);
cristy4c08aed2011-07-01 19:47:50 +0000745 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000746 {
747 status=MagickFalse;
748 continue;
749 }
cristybb503372010-05-27 20:51:26 +0000750 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000751 {
cristyc7e6ff62011-10-03 13:46:11 +0000752 register ssize_t
753 i;
754
755 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
756 {
757 PixelChannel
758 channel;
759
760 PixelTrait
761 colorize_traits,
762 traits;
763
764 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
765 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
766 colorize_traits=GetPixelChannelMapTraits(colorize_image,channel);
767 if ((traits == UndefinedPixelTrait) ||
768 (colorize_traits == UndefinedPixelTrait))
769 continue;
770 if ((colorize_traits & CopyPixelTrait) != 0)
771 {
772 SetPixelChannel(colorize_image,channel,p[i],q);
773 continue;
774 }
775 switch (channel)
776 {
777 case RedPixelChannel:
778 {
779 SetPixelChannel(colorize_image,channel,ClampToQuantum((p[i]*
780 (100.0-pixel.red)+colorize->red*pixel.red)/100.0),q);
781 break;
782 }
783 case GreenPixelChannel:
784 {
785 SetPixelChannel(colorize_image,channel,ClampToQuantum((p[i]*
786 (100.0-pixel.green)+colorize->green*pixel.green)/100.0),q);
787 break;
788 }
789 case BluePixelChannel:
790 {
791 SetPixelChannel(colorize_image,channel,ClampToQuantum((p[i]*
792 (100.0-pixel.blue)+colorize->blue*pixel.blue)/100.0),q);
793 break;
794 }
795 case BlackPixelChannel:
796 {
797 SetPixelChannel(colorize_image,channel,ClampToQuantum((p[i]*
798 (100.0-pixel.black)+colorize->black*pixel.black)/100.0),q);
799 break;
800 }
801 case AlphaPixelChannel:
802 {
803 SetPixelChannel(colorize_image,channel,ClampToQuantum((p[i]*
804 (100.0-pixel.alpha)+colorize->alpha*pixel.alpha)/100.0),q);
805 break;
806 }
807 default:
808 {
809 SetPixelChannel(colorize_image,channel,p[i],q);
810 break;
811 }
812 }
813 }
cristyed231572011-07-14 02:18:59 +0000814 p+=GetPixelChannels(image);
815 q+=GetPixelChannels(colorize_image);
cristy3ed852e2009-09-05 21:47:34 +0000816 }
817 sync=SyncCacheViewAuthenticPixels(colorize_view,exception);
818 if (sync == MagickFalse)
819 status=MagickFalse;
820 if (image->progress_monitor != (MagickProgressMonitor) NULL)
821 {
822 MagickBooleanType
823 proceed;
824
cristy319a1e72010-02-21 15:13:11 +0000825#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000826 #pragma omp critical (MagickCore_ColorizeImage)
827#endif
828 proceed=SetImageProgress(image,ColorizeImageTag,progress++,image->rows);
829 if (proceed == MagickFalse)
830 status=MagickFalse;
831 }
832 }
833 image_view=DestroyCacheView(image_view);
834 colorize_view=DestroyCacheView(colorize_view);
835 if (status == MagickFalse)
836 colorize_image=DestroyImage(colorize_image);
837 return(colorize_image);
838}
839
840/*
841%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
842% %
843% %
844% %
cristye6365592010-04-02 17:31:23 +0000845% C o l o r M a t r i x I m a g e %
846% %
847% %
848% %
849%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
850%
851% ColorMatrixImage() applies color transformation to an image. This method
852% permits saturation changes, hue rotation, luminance to alpha, and various
853% other effects. Although variable-sized transformation matrices can be used,
854% typically one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA
855% (or RGBA with offsets). The matrix is similar to those used by Adobe Flash
856% except offsets are in column 6 rather than 5 (in support of CMYKA images)
857% and offsets are normalized (divide Flash offset by 255).
858%
859% The format of the ColorMatrixImage method is:
860%
861% Image *ColorMatrixImage(const Image *image,
862% const KernelInfo *color_matrix,ExceptionInfo *exception)
863%
864% A description of each parameter follows:
865%
866% o image: the image.
867%
868% o color_matrix: the color matrix.
869%
870% o exception: return any errors or warnings in this structure.
871%
872*/
873MagickExport Image *ColorMatrixImage(const Image *image,
874 const KernelInfo *color_matrix,ExceptionInfo *exception)
875{
876#define ColorMatrixImageTag "ColorMatrix/Image"
877
878 CacheView
879 *color_view,
880 *image_view;
881
882 double
883 ColorMatrix[6][6] =
884 {
885 { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
886 { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
887 { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
888 { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
889 { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
890 { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }
891 };
892
893 Image
894 *color_image;
895
cristye6365592010-04-02 17:31:23 +0000896 MagickBooleanType
897 status;
898
cristybb503372010-05-27 20:51:26 +0000899 MagickOffsetType
900 progress;
901
902 register ssize_t
cristye6365592010-04-02 17:31:23 +0000903 i;
904
cristybb503372010-05-27 20:51:26 +0000905 ssize_t
906 u,
907 v,
908 y;
909
cristye6365592010-04-02 17:31:23 +0000910 /*
911 Create color matrix.
912 */
913 assert(image != (Image *) NULL);
914 assert(image->signature == MagickSignature);
915 if (image->debug != MagickFalse)
916 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
917 assert(exception != (ExceptionInfo *) NULL);
918 assert(exception->signature == MagickSignature);
919 i=0;
cristybb503372010-05-27 20:51:26 +0000920 for (v=0; v < (ssize_t) color_matrix->height; v++)
921 for (u=0; u < (ssize_t) color_matrix->width; u++)
cristye6365592010-04-02 17:31:23 +0000922 {
923 if ((v < 6) && (u < 6))
924 ColorMatrix[v][u]=color_matrix->values[i];
925 i++;
926 }
927 /*
928 Initialize color image.
929 */
cristy12550e62010-06-07 12:46:40 +0000930 color_image=CloneImage(image,0,0,MagickTrue,exception);
cristye6365592010-04-02 17:31:23 +0000931 if (color_image == (Image *) NULL)
932 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000933 if (SetImageStorageClass(color_image,DirectClass,exception) == MagickFalse)
cristye6365592010-04-02 17:31:23 +0000934 {
cristye6365592010-04-02 17:31:23 +0000935 color_image=DestroyImage(color_image);
936 return((Image *) NULL);
937 }
938 if (image->debug != MagickFalse)
939 {
940 char
941 format[MaxTextExtent],
942 *message;
943
944 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
945 " ColorMatrix image with color matrix:");
946 message=AcquireString("");
947 for (v=0; v < 6; v++)
948 {
949 *message='\0';
cristyb51dff52011-05-19 16:55:47 +0000950 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristye6365592010-04-02 17:31:23 +0000951 (void) ConcatenateString(&message,format);
952 for (u=0; u < 6; u++)
953 {
cristyb51dff52011-05-19 16:55:47 +0000954 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",
cristye6365592010-04-02 17:31:23 +0000955 ColorMatrix[v][u]);
956 (void) ConcatenateString(&message,format);
957 }
958 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
959 }
960 message=DestroyString(message);
961 }
962 /*
963 ColorMatrix image.
964 */
965 status=MagickTrue;
966 progress=0;
967 image_view=AcquireCacheView(image);
968 color_view=AcquireCacheView(color_image);
969#if defined(MAGICKCORE_OPENMP_SUPPORT)
970 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
971#endif
cristybb503372010-05-27 20:51:26 +0000972 for (y=0; y < (ssize_t) image->rows; y++)
cristye6365592010-04-02 17:31:23 +0000973 {
974 MagickRealType
975 pixel;
976
cristy4c08aed2011-07-01 19:47:50 +0000977 register const Quantum
cristye6365592010-04-02 17:31:23 +0000978 *restrict p;
979
cristy4c08aed2011-07-01 19:47:50 +0000980 register Quantum
981 *restrict q;
982
cristybb503372010-05-27 20:51:26 +0000983 register ssize_t
cristye6365592010-04-02 17:31:23 +0000984 x;
985
cristye6365592010-04-02 17:31:23 +0000986 if (status == MagickFalse)
987 continue;
988 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
989 q=GetCacheViewAuthenticPixels(color_view,0,y,color_image->columns,1,
990 exception);
cristy4c08aed2011-07-01 19:47:50 +0000991 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristye6365592010-04-02 17:31:23 +0000992 {
993 status=MagickFalse;
994 continue;
995 }
cristybb503372010-05-27 20:51:26 +0000996 for (x=0; x < (ssize_t) image->columns; x++)
cristye6365592010-04-02 17:31:23 +0000997 {
cristybb503372010-05-27 20:51:26 +0000998 register ssize_t
cristye6365592010-04-02 17:31:23 +0000999 v;
1000
cristybb503372010-05-27 20:51:26 +00001001 size_t
cristye6365592010-04-02 17:31:23 +00001002 height;
1003
1004 height=color_matrix->height > 6 ? 6UL : color_matrix->height;
cristybb503372010-05-27 20:51:26 +00001005 for (v=0; v < (ssize_t) height; v++)
cristye6365592010-04-02 17:31:23 +00001006 {
cristy4c08aed2011-07-01 19:47:50 +00001007 pixel=ColorMatrix[v][0]*GetPixelRed(image,p)+ColorMatrix[v][1]*
1008 GetPixelGreen(image,p)+ColorMatrix[v][2]*GetPixelBlue(image,p);
cristye6365592010-04-02 17:31:23 +00001009 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00001010 pixel+=ColorMatrix[v][3]*GetPixelBlack(image,p);
1011 if (image->matte != MagickFalse)
1012 pixel+=ColorMatrix[v][4]*GetPixelAlpha(image,p);
cristye6365592010-04-02 17:31:23 +00001013 pixel+=QuantumRange*ColorMatrix[v][5];
1014 switch (v)
1015 {
cristy4c08aed2011-07-01 19:47:50 +00001016 case 0: SetPixelRed(color_image,ClampToQuantum(pixel),q); break;
1017 case 1: SetPixelGreen(color_image,ClampToQuantum(pixel),q); break;
1018 case 2: SetPixelBlue(color_image,ClampToQuantum(pixel),q); break;
cristye6365592010-04-02 17:31:23 +00001019 case 3:
1020 {
cristy4c08aed2011-07-01 19:47:50 +00001021 if (image->colorspace == CMYKColorspace)
1022 SetPixelBlack(color_image,ClampToQuantum(pixel),q);
cristye6365592010-04-02 17:31:23 +00001023 break;
1024 }
1025 case 4:
1026 {
cristy4c08aed2011-07-01 19:47:50 +00001027 if (image->matte != MagickFalse)
1028 SetPixelAlpha(color_image,ClampToQuantum(pixel),q);
cristye6365592010-04-02 17:31:23 +00001029 break;
1030 }
1031 }
1032 }
cristyed231572011-07-14 02:18:59 +00001033 p+=GetPixelChannels(image);
1034 q+=GetPixelChannels(color_image);
cristye6365592010-04-02 17:31:23 +00001035 }
1036 if (SyncCacheViewAuthenticPixels(color_view,exception) == MagickFalse)
1037 status=MagickFalse;
1038 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1039 {
1040 MagickBooleanType
1041 proceed;
1042
1043#if defined(MAGICKCORE_OPENMP_SUPPORT)
1044 #pragma omp critical (MagickCore_ColorMatrixImage)
1045#endif
1046 proceed=SetImageProgress(image,ColorMatrixImageTag,progress++,
1047 image->rows);
1048 if (proceed == MagickFalse)
1049 status=MagickFalse;
1050 }
1051 }
1052 color_view=DestroyCacheView(color_view);
1053 image_view=DestroyCacheView(image_view);
1054 if (status == MagickFalse)
1055 color_image=DestroyImage(color_image);
1056 return(color_image);
1057}
1058
1059/*
1060%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1061% %
1062% %
1063% %
cristy3ed852e2009-09-05 21:47:34 +00001064+ D e s t r o y F x I n f o %
1065% %
1066% %
1067% %
1068%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1069%
1070% DestroyFxInfo() deallocates memory associated with an FxInfo structure.
1071%
1072% The format of the DestroyFxInfo method is:
1073%
1074% ImageInfo *DestroyFxInfo(ImageInfo *fx_info)
1075%
1076% A description of each parameter follows:
1077%
1078% o fx_info: the fx info.
1079%
1080*/
cristy7832dc22011-09-05 01:21:53 +00001081MagickPrivate FxInfo *DestroyFxInfo(FxInfo *fx_info)
cristy3ed852e2009-09-05 21:47:34 +00001082{
cristybb503372010-05-27 20:51:26 +00001083 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001084 i;
1085
1086 fx_info->exception=DestroyExceptionInfo(fx_info->exception);
1087 fx_info->expression=DestroyString(fx_info->expression);
1088 fx_info->symbols=DestroySplayTree(fx_info->symbols);
1089 fx_info->colors=DestroySplayTree(fx_info->colors);
cristy0ea377f2011-03-24 00:54:19 +00001090 for (i=(ssize_t) GetImageListLength(fx_info->images)-1; i >= 0; i--)
cristyd76c51e2011-03-26 00:21:26 +00001091 fx_info->view[i]=DestroyCacheView(fx_info->view[i]);
1092 fx_info->view=(CacheView **) RelinquishMagickMemory(fx_info->view);
cristy3ed852e2009-09-05 21:47:34 +00001093 fx_info->random_info=DestroyRandomInfo(fx_info->random_info);
1094 fx_info=(FxInfo *) RelinquishMagickMemory(fx_info);
1095 return(fx_info);
1096}
1097
1098/*
1099%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1100% %
1101% %
1102% %
cristy3ed852e2009-09-05 21:47:34 +00001103+ 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 %
1104% %
1105% %
1106% %
1107%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1108%
1109% FxEvaluateChannelExpression() evaluates an expression and returns the
1110% results.
1111%
1112% The format of the FxEvaluateExpression method is:
1113%
1114% MagickRealType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00001115% const PixelChannel channel,const ssize_t x,const ssize_t y,
cristy3ed852e2009-09-05 21:47:34 +00001116% MagickRealType *alpha,Exceptioninfo *exception)
1117% MagickRealType FxEvaluateExpression(FxInfo *fx_info,
1118% MagickRealType *alpha,Exceptioninfo *exception)
1119%
1120% A description of each parameter follows:
1121%
1122% o fx_info: the fx info.
1123%
1124% o channel: the channel.
1125%
1126% o x,y: the pixel position.
1127%
1128% o alpha: the result.
1129%
1130% o exception: return any errors or warnings in this structure.
1131%
1132*/
1133
cristy351842f2010-03-07 15:27:38 +00001134static inline double MagickMax(const double x,const double y)
1135{
1136 if (x > y)
1137 return(x);
1138 return(y);
1139}
1140
1141static inline double MagickMin(const double x,const double y)
1142{
1143 if (x < y)
1144 return(x);
1145 return(y);
1146}
1147
cristy3ed852e2009-09-05 21:47:34 +00001148static MagickRealType FxChannelStatistics(FxInfo *fx_info,const Image *image,
cristy0568ffc2011-07-25 16:54:14 +00001149 PixelChannel channel,const char *symbol,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001150{
1151 char
1152 key[MaxTextExtent],
1153 statistic[MaxTextExtent];
1154
1155 const char
1156 *value;
1157
1158 register const char
1159 *p;
1160
1161 for (p=symbol; (*p != '.') && (*p != '\0'); p++) ;
1162 if (*p == '.')
1163 switch (*++p) /* e.g. depth.r */
1164 {
cristy541ae572011-08-05 19:08:59 +00001165 case 'r': channel=RedPixelChannel; break;
1166 case 'g': channel=GreenPixelChannel; break;
1167 case 'b': channel=BluePixelChannel; break;
1168 case 'c': channel=CyanPixelChannel; break;
1169 case 'm': channel=MagentaPixelChannel; break;
1170 case 'y': channel=YellowPixelChannel; break;
1171 case 'k': channel=BlackPixelChannel; break;
cristy3ed852e2009-09-05 21:47:34 +00001172 default: break;
1173 }
cristyb51dff52011-05-19 16:55:47 +00001174 (void) FormatLocaleString(key,MaxTextExtent,"%p.%.20g.%s",(void *) image,
cristye8c25f92010-06-03 00:53:06 +00001175 (double) channel,symbol);
cristy3ed852e2009-09-05 21:47:34 +00001176 value=(const char *) GetValueFromSplayTree(fx_info->symbols,key);
1177 if (value != (const char *) NULL)
cristyc1acd842011-05-19 23:05:47 +00001178 return(QuantumScale*InterpretLocaleValue(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001179 (void) DeleteNodeFromSplayTree(fx_info->symbols,key);
1180 if (LocaleNCompare(symbol,"depth",5) == 0)
1181 {
cristybb503372010-05-27 20:51:26 +00001182 size_t
cristy3ed852e2009-09-05 21:47:34 +00001183 depth;
1184
cristyfefab1b2011-07-05 00:33:22 +00001185 depth=GetImageDepth(image,exception);
1186 (void) FormatLocaleString(statistic,MaxTextExtent,"%.20g",(double) depth);
cristy3ed852e2009-09-05 21:47:34 +00001187 }
1188 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1189 {
1190 double
1191 kurtosis,
1192 skewness;
1193
cristyd42d9952011-07-08 14:21:50 +00001194 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001195 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",kurtosis);
cristy3ed852e2009-09-05 21:47:34 +00001196 }
1197 if (LocaleNCompare(symbol,"maxima",6) == 0)
1198 {
1199 double
1200 maxima,
1201 minima;
1202
cristyd42d9952011-07-08 14:21:50 +00001203 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001204 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",maxima);
cristy3ed852e2009-09-05 21:47:34 +00001205 }
1206 if (LocaleNCompare(symbol,"mean",4) == 0)
1207 {
1208 double
1209 mean,
1210 standard_deviation;
1211
cristyd42d9952011-07-08 14:21:50 +00001212 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001213 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",mean);
cristy3ed852e2009-09-05 21:47:34 +00001214 }
1215 if (LocaleNCompare(symbol,"minima",6) == 0)
1216 {
1217 double
1218 maxima,
1219 minima;
1220
cristyd42d9952011-07-08 14:21:50 +00001221 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001222 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",minima);
cristy3ed852e2009-09-05 21:47:34 +00001223 }
1224 if (LocaleNCompare(symbol,"skewness",8) == 0)
1225 {
1226 double
1227 kurtosis,
1228 skewness;
1229
cristyd42d9952011-07-08 14:21:50 +00001230 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001231 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",skewness);
cristy3ed852e2009-09-05 21:47:34 +00001232 }
1233 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1234 {
1235 double
1236 mean,
1237 standard_deviation;
1238
cristyd42d9952011-07-08 14:21:50 +00001239 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001240 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",
cristy3ed852e2009-09-05 21:47:34 +00001241 standard_deviation);
1242 }
1243 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(key),
1244 ConstantString(statistic));
cristyc1acd842011-05-19 23:05:47 +00001245 return(QuantumScale*InterpretLocaleValue(statistic,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001246}
1247
1248static MagickRealType
cristy0568ffc2011-07-25 16:54:14 +00001249 FxEvaluateSubexpression(FxInfo *,const PixelChannel,const ssize_t,
cristye85007d2010-06-06 22:51:36 +00001250 const ssize_t,const char *,MagickRealType *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +00001251
cristyb0aad4c2011-11-02 19:30:35 +00001252static MagickOffsetType FxGCD(MagickOffsetType alpha,MagickOffsetType beta)
1253{
1254 if (beta != 0)
1255 return(FxGCD(beta,alpha % beta));
1256 return(alpha);
1257}
1258
cristy3ed852e2009-09-05 21:47:34 +00001259static inline const char *FxSubexpression(const char *expression,
1260 ExceptionInfo *exception)
1261{
1262 const char
1263 *subexpression;
1264
cristybb503372010-05-27 20:51:26 +00001265 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001266 level;
1267
1268 level=0;
1269 subexpression=expression;
1270 while ((*subexpression != '\0') &&
1271 ((level != 1) || (strchr(")",(int) *subexpression) == (char *) NULL)))
1272 {
1273 if (strchr("(",(int) *subexpression) != (char *) NULL)
1274 level++;
1275 else
1276 if (strchr(")",(int) *subexpression) != (char *) NULL)
1277 level--;
1278 subexpression++;
1279 }
1280 if (*subexpression == '\0')
1281 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1282 "UnbalancedParenthesis","`%s'",expression);
1283 return(subexpression);
1284}
1285
cristy0568ffc2011-07-25 16:54:14 +00001286static MagickRealType FxGetSymbol(FxInfo *fx_info,const PixelChannel channel,
cristye85007d2010-06-06 22:51:36 +00001287 const ssize_t x,const ssize_t y,const char *expression,
1288 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001289{
1290 char
1291 *q,
1292 subexpression[MaxTextExtent],
1293 symbol[MaxTextExtent];
1294
1295 const char
1296 *p,
1297 *value;
1298
1299 Image
1300 *image;
1301
cristy4c08aed2011-07-01 19:47:50 +00001302 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001303 pixel;
1304
1305 MagickRealType
1306 alpha,
1307 beta;
1308
1309 PointInfo
1310 point;
1311
cristybb503372010-05-27 20:51:26 +00001312 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001313 i;
1314
1315 size_t
1316 length;
1317
cristybb503372010-05-27 20:51:26 +00001318 size_t
cristy3ed852e2009-09-05 21:47:34 +00001319 level;
1320
1321 p=expression;
1322 i=GetImageIndexInList(fx_info->images);
1323 level=0;
1324 point.x=(double) x;
1325 point.y=(double) y;
1326 if (isalpha((int) *(p+1)) == 0)
1327 {
1328 if (strchr("suv",(int) *p) != (char *) NULL)
1329 {
1330 switch (*p)
1331 {
1332 case 's':
1333 default:
1334 {
1335 i=GetImageIndexInList(fx_info->images);
1336 break;
1337 }
1338 case 'u': i=0; break;
1339 case 'v': i=1; break;
1340 }
1341 p++;
1342 if (*p == '[')
1343 {
1344 level++;
1345 q=subexpression;
1346 for (p++; *p != '\0'; )
1347 {
1348 if (*p == '[')
1349 level++;
1350 else
1351 if (*p == ']')
1352 {
1353 level--;
1354 if (level == 0)
1355 break;
1356 }
1357 *q++=(*p++);
1358 }
1359 *q='\0';
1360 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1361 &beta,exception);
cristybb503372010-05-27 20:51:26 +00001362 i=(ssize_t) (alpha+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001363 p++;
1364 }
1365 if (*p == '.')
1366 p++;
1367 }
1368 if ((isalpha((int) *(p+1)) == 0) && (*p == 'p'))
1369 {
1370 p++;
1371 if (*p == '{')
1372 {
1373 level++;
1374 q=subexpression;
1375 for (p++; *p != '\0'; )
1376 {
1377 if (*p == '{')
1378 level++;
1379 else
1380 if (*p == '}')
1381 {
1382 level--;
1383 if (level == 0)
1384 break;
1385 }
1386 *q++=(*p++);
1387 }
1388 *q='\0';
1389 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1390 &beta,exception);
1391 point.x=alpha;
1392 point.y=beta;
1393 p++;
1394 }
1395 else
1396 if (*p == '[')
1397 {
1398 level++;
1399 q=subexpression;
1400 for (p++; *p != '\0'; )
1401 {
1402 if (*p == '[')
1403 level++;
1404 else
1405 if (*p == ']')
1406 {
1407 level--;
1408 if (level == 0)
1409 break;
1410 }
1411 *q++=(*p++);
1412 }
1413 *q='\0';
1414 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1415 &beta,exception);
1416 point.x+=alpha;
1417 point.y+=beta;
1418 p++;
1419 }
1420 if (*p == '.')
1421 p++;
1422 }
1423 }
1424 length=GetImageListLength(fx_info->images);
1425 while (i < 0)
cristybb503372010-05-27 20:51:26 +00001426 i+=(ssize_t) length;
cristy3ed852e2009-09-05 21:47:34 +00001427 i%=length;
1428 image=GetImageFromList(fx_info->images,i);
1429 if (image == (Image *) NULL)
1430 {
1431 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1432 "NoSuchImage","`%s'",expression);
1433 return(0.0);
1434 }
cristy4c08aed2011-07-01 19:47:50 +00001435 GetPixelInfo(image,&pixel);
1436 (void) InterpolatePixelInfo(image,fx_info->view[i],image->interpolate,
cristy4f820712011-04-01 12:35:43 +00001437 point.x,point.y,&pixel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001438 if ((strlen(p) > 2) &&
1439 (LocaleCompare(p,"intensity") != 0) &&
1440 (LocaleCompare(p,"luminance") != 0) &&
1441 (LocaleCompare(p,"hue") != 0) &&
1442 (LocaleCompare(p,"saturation") != 0) &&
1443 (LocaleCompare(p,"lightness") != 0))
1444 {
1445 char
1446 name[MaxTextExtent];
1447
1448 (void) CopyMagickString(name,p,MaxTextExtent);
1449 for (q=name+(strlen(name)-1); q > name; q--)
1450 {
1451 if (*q == ')')
1452 break;
1453 if (*q == '.')
1454 {
1455 *q='\0';
1456 break;
1457 }
1458 }
1459 if ((strlen(name) > 2) &&
1460 (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
1461 {
cristy4c08aed2011-07-01 19:47:50 +00001462 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001463 *color;
1464
cristy4c08aed2011-07-01 19:47:50 +00001465 color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
1466 if (color != (PixelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001467 {
1468 pixel=(*color);
1469 p+=strlen(name);
1470 }
1471 else
cristy269c9412011-10-13 23:41:15 +00001472 if (QueryColorCompliance(name,AllCompliance,&pixel,fx_info->exception) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001473 {
1474 (void) AddValueToSplayTree(fx_info->colors,ConstantString(name),
cristy4c08aed2011-07-01 19:47:50 +00001475 ClonePixelInfo(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001476 p+=strlen(name);
1477 }
1478 }
1479 }
1480 (void) CopyMagickString(symbol,p,MaxTextExtent);
1481 StripString(symbol);
1482 if (*symbol == '\0')
1483 {
1484 switch (channel)
1485 {
cristy0568ffc2011-07-25 16:54:14 +00001486 case RedPixelChannel: return(QuantumScale*pixel.red);
1487 case GreenPixelChannel: return(QuantumScale*pixel.green);
1488 case BluePixelChannel: return(QuantumScale*pixel.blue);
1489 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001490 {
1491 if (image->colorspace != CMYKColorspace)
1492 {
1493 (void) ThrowMagickException(exception,GetMagickModule(),
1494 OptionError,"ColorSeparatedImageRequired","`%s'",
1495 image->filename);
1496 return(0.0);
1497 }
cristy4c08aed2011-07-01 19:47:50 +00001498 return(QuantumScale*pixel.black);
1499 }
cristy0568ffc2011-07-25 16:54:14 +00001500 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001501 {
1502 MagickRealType
1503 alpha;
1504
1505 if (pixel.matte == MagickFalse)
1506 return(1.0);
1507 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
1508 return(alpha);
cristy3ed852e2009-09-05 21:47:34 +00001509 }
cristyb3a73b52011-07-26 01:34:43 +00001510 case IntensityPixelChannel:
cristyf364ed42010-12-15 01:54:43 +00001511 {
cristy4c08aed2011-07-01 19:47:50 +00001512 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristyf364ed42010-12-15 01:54:43 +00001513 }
cristy3ed852e2009-09-05 21:47:34 +00001514 default:
1515 break;
1516 }
1517 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1518 "UnableToParseExpression","`%s'",p);
1519 return(0.0);
1520 }
1521 switch (*symbol)
1522 {
1523 case 'A':
1524 case 'a':
1525 {
1526 if (LocaleCompare(symbol,"a") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001527 return((MagickRealType) (QuantumScale*pixel.alpha));
cristy3ed852e2009-09-05 21:47:34 +00001528 break;
1529 }
1530 case 'B':
1531 case 'b':
1532 {
1533 if (LocaleCompare(symbol,"b") == 0)
1534 return(QuantumScale*pixel.blue);
1535 break;
1536 }
1537 case 'C':
1538 case 'c':
1539 {
1540 if (LocaleNCompare(symbol,"channel",7) == 0)
1541 {
1542 GeometryInfo
1543 channel_info;
1544
1545 MagickStatusType
1546 flags;
1547
1548 flags=ParseGeometry(symbol+7,&channel_info);
1549 if (image->colorspace == CMYKColorspace)
1550 switch (channel)
1551 {
cristy0568ffc2011-07-25 16:54:14 +00001552 case CyanPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001553 {
1554 if ((flags & RhoValue) == 0)
1555 return(0.0);
1556 return(channel_info.rho);
1557 }
cristy0568ffc2011-07-25 16:54:14 +00001558 case MagentaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001559 {
1560 if ((flags & SigmaValue) == 0)
1561 return(0.0);
1562 return(channel_info.sigma);
1563 }
cristy0568ffc2011-07-25 16:54:14 +00001564 case YellowPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001565 {
1566 if ((flags & XiValue) == 0)
1567 return(0.0);
1568 return(channel_info.xi);
1569 }
cristy0568ffc2011-07-25 16:54:14 +00001570 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001571 {
1572 if ((flags & PsiValue) == 0)
1573 return(0.0);
1574 return(channel_info.psi);
1575 }
cristy0568ffc2011-07-25 16:54:14 +00001576 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001577 {
1578 if ((flags & ChiValue) == 0)
1579 return(0.0);
1580 return(channel_info.chi);
1581 }
1582 default:
1583 return(0.0);
1584 }
1585 switch (channel)
1586 {
cristy0568ffc2011-07-25 16:54:14 +00001587 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001588 {
1589 if ((flags & RhoValue) == 0)
1590 return(0.0);
1591 return(channel_info.rho);
1592 }
cristy0568ffc2011-07-25 16:54:14 +00001593 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001594 {
1595 if ((flags & SigmaValue) == 0)
1596 return(0.0);
1597 return(channel_info.sigma);
1598 }
cristy0568ffc2011-07-25 16:54:14 +00001599 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001600 {
1601 if ((flags & XiValue) == 0)
1602 return(0.0);
1603 return(channel_info.xi);
1604 }
cristy0568ffc2011-07-25 16:54:14 +00001605 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001606 {
1607 if ((flags & ChiValue) == 0)
1608 return(0.0);
1609 return(channel_info.chi);
1610 }
cristy0568ffc2011-07-25 16:54:14 +00001611 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001612 {
1613 if ((flags & PsiValue) == 0)
1614 return(0.0);
1615 return(channel_info.psi);
1616 }
cristy3ed852e2009-09-05 21:47:34 +00001617 default:
1618 return(0.0);
1619 }
1620 return(0.0);
1621 }
1622 if (LocaleCompare(symbol,"c") == 0)
1623 return(QuantumScale*pixel.red);
1624 break;
1625 }
1626 case 'D':
1627 case 'd':
1628 {
1629 if (LocaleNCompare(symbol,"depth",5) == 0)
1630 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1631 break;
1632 }
1633 case 'G':
1634 case 'g':
1635 {
1636 if (LocaleCompare(symbol,"g") == 0)
1637 return(QuantumScale*pixel.green);
1638 break;
1639 }
1640 case 'K':
1641 case 'k':
1642 {
1643 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1644 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1645 if (LocaleCompare(symbol,"k") == 0)
1646 {
1647 if (image->colorspace != CMYKColorspace)
1648 {
1649 (void) ThrowMagickException(exception,GetMagickModule(),
1650 OptionError,"ColorSeparatedImageRequired","`%s'",
1651 image->filename);
1652 return(0.0);
1653 }
cristy4c08aed2011-07-01 19:47:50 +00001654 return(QuantumScale*pixel.black);
cristy3ed852e2009-09-05 21:47:34 +00001655 }
1656 break;
1657 }
1658 case 'H':
1659 case 'h':
1660 {
1661 if (LocaleCompare(symbol,"h") == 0)
1662 return((MagickRealType) image->rows);
1663 if (LocaleCompare(symbol,"hue") == 0)
1664 {
1665 double
1666 hue,
1667 lightness,
1668 saturation;
1669
cristyda1f9c12011-10-02 21:39:49 +00001670 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1671 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001672 return(hue);
1673 }
1674 break;
1675 }
1676 case 'I':
1677 case 'i':
1678 {
1679 if ((LocaleCompare(symbol,"image.depth") == 0) ||
1680 (LocaleCompare(symbol,"image.minima") == 0) ||
1681 (LocaleCompare(symbol,"image.maxima") == 0) ||
1682 (LocaleCompare(symbol,"image.mean") == 0) ||
1683 (LocaleCompare(symbol,"image.kurtosis") == 0) ||
1684 (LocaleCompare(symbol,"image.skewness") == 0) ||
1685 (LocaleCompare(symbol,"image.standard_deviation") == 0))
1686 return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
1687 if (LocaleCompare(symbol,"image.resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001688 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001689 if (LocaleCompare(symbol,"image.resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001690 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001691 if (LocaleCompare(symbol,"intensity") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001692 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001693 if (LocaleCompare(symbol,"i") == 0)
1694 return((MagickRealType) x);
1695 break;
1696 }
1697 case 'J':
1698 case 'j':
1699 {
1700 if (LocaleCompare(symbol,"j") == 0)
1701 return((MagickRealType) y);
1702 break;
1703 }
1704 case 'L':
1705 case 'l':
1706 {
1707 if (LocaleCompare(symbol,"lightness") == 0)
1708 {
1709 double
1710 hue,
1711 lightness,
1712 saturation;
1713
cristyda1f9c12011-10-02 21:39:49 +00001714 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1715 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001716 return(lightness);
1717 }
1718 if (LocaleCompare(symbol,"luminance") == 0)
1719 {
1720 double
1721 luminence;
1722
1723 luminence=0.2126*pixel.red+0.7152*pixel.green+0.0722*pixel.blue;
1724 return(QuantumScale*luminence);
1725 }
1726 break;
1727 }
1728 case 'M':
1729 case 'm':
1730 {
1731 if (LocaleNCompare(symbol,"maxima",6) == 0)
1732 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1733 if (LocaleNCompare(symbol,"mean",4) == 0)
1734 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1735 if (LocaleNCompare(symbol,"minima",6) == 0)
1736 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1737 if (LocaleCompare(symbol,"m") == 0)
1738 return(QuantumScale*pixel.blue);
1739 break;
1740 }
1741 case 'N':
1742 case 'n':
1743 {
1744 if (LocaleCompare(symbol,"n") == 0)
anthony374f5dd2011-03-25 10:08:53 +00001745 return((MagickRealType) GetImageListLength(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001746 break;
1747 }
1748 case 'O':
1749 case 'o':
1750 {
1751 if (LocaleCompare(symbol,"o") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001752 return(QuantumScale*pixel.alpha);
cristy3ed852e2009-09-05 21:47:34 +00001753 break;
1754 }
1755 case 'P':
1756 case 'p':
1757 {
1758 if (LocaleCompare(symbol,"page.height") == 0)
1759 return((MagickRealType) image->page.height);
1760 if (LocaleCompare(symbol,"page.width") == 0)
1761 return((MagickRealType) image->page.width);
1762 if (LocaleCompare(symbol,"page.x") == 0)
1763 return((MagickRealType) image->page.x);
1764 if (LocaleCompare(symbol,"page.y") == 0)
1765 return((MagickRealType) image->page.y);
1766 break;
1767 }
1768 case 'R':
1769 case 'r':
1770 {
1771 if (LocaleCompare(symbol,"resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001772 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001773 if (LocaleCompare(symbol,"resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001774 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001775 if (LocaleCompare(symbol,"r") == 0)
1776 return(QuantumScale*pixel.red);
1777 break;
1778 }
1779 case 'S':
1780 case 's':
1781 {
1782 if (LocaleCompare(symbol,"saturation") == 0)
1783 {
1784 double
1785 hue,
1786 lightness,
1787 saturation;
1788
cristyda1f9c12011-10-02 21:39:49 +00001789 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1790 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001791 return(saturation);
1792 }
1793 if (LocaleNCompare(symbol,"skewness",8) == 0)
1794 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1795 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1796 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1797 break;
1798 }
1799 case 'T':
1800 case 't':
1801 {
1802 if (LocaleCompare(symbol,"t") == 0)
cristy5a15b932011-03-26 12:50:33 +00001803 return((MagickRealType) GetImageIndexInList(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001804 break;
1805 }
1806 case 'W':
1807 case 'w':
1808 {
1809 if (LocaleCompare(symbol,"w") == 0)
1810 return((MagickRealType) image->columns);
1811 break;
1812 }
1813 case 'Y':
1814 case 'y':
1815 {
1816 if (LocaleCompare(symbol,"y") == 0)
1817 return(QuantumScale*pixel.green);
1818 break;
1819 }
1820 case 'Z':
1821 case 'z':
1822 {
1823 if (LocaleCompare(symbol,"z") == 0)
1824 {
1825 MagickRealType
1826 depth;
1827
cristyfefab1b2011-07-05 00:33:22 +00001828 depth=(MagickRealType) GetImageDepth(image,fx_info->exception);
cristy3ed852e2009-09-05 21:47:34 +00001829 return(depth);
1830 }
1831 break;
1832 }
1833 default:
1834 break;
1835 }
1836 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1837 if (value != (const char *) NULL)
cristyc1acd842011-05-19 23:05:47 +00001838 return((MagickRealType) InterpretLocaleValue(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001839 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1840 "UnableToParseExpression","`%s'",symbol);
1841 return(0.0);
1842}
1843
1844static const char *FxOperatorPrecedence(const char *expression,
1845 ExceptionInfo *exception)
1846{
1847 typedef enum
1848 {
1849 UndefinedPrecedence,
1850 NullPrecedence,
1851 BitwiseComplementPrecedence,
1852 ExponentPrecedence,
cristy116af162010-08-13 01:25:47 +00001853 ExponentialNotationPrecedence,
cristy3ed852e2009-09-05 21:47:34 +00001854 MultiplyPrecedence,
1855 AdditionPrecedence,
1856 ShiftPrecedence,
1857 RelationalPrecedence,
1858 EquivalencyPrecedence,
1859 BitwiseAndPrecedence,
1860 BitwiseOrPrecedence,
1861 LogicalAndPrecedence,
1862 LogicalOrPrecedence,
1863 TernaryPrecedence,
1864 AssignmentPrecedence,
1865 CommaPrecedence,
1866 SeparatorPrecedence
1867 } FxPrecedence;
1868
1869 FxPrecedence
1870 precedence,
1871 target;
1872
1873 register const char
1874 *subexpression;
1875
1876 register int
1877 c;
1878
cristybb503372010-05-27 20:51:26 +00001879 size_t
cristy3ed852e2009-09-05 21:47:34 +00001880 level;
1881
1882 c=0;
1883 level=0;
1884 subexpression=(const char *) NULL;
1885 target=NullPrecedence;
1886 while (*expression != '\0')
1887 {
1888 precedence=UndefinedPrecedence;
1889 if ((isspace((int) ((char) *expression)) != 0) || (c == (int) '@'))
1890 {
1891 expression++;
1892 continue;
1893 }
cristy488fa882010-03-01 22:34:24 +00001894 switch (*expression)
1895 {
1896 case 'A':
1897 case 'a':
cristy3ed852e2009-09-05 21:47:34 +00001898 {
cristyb33454f2011-08-03 02:10:45 +00001899#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00001900 if (LocaleNCompare(expression,"acosh",5) == 0)
1901 {
1902 expression+=5;
1903 break;
1904 }
cristyb33454f2011-08-03 02:10:45 +00001905#endif
1906#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00001907 if (LocaleNCompare(expression,"asinh",5) == 0)
1908 {
1909 expression+=5;
1910 break;
1911 }
cristyb33454f2011-08-03 02:10:45 +00001912#endif
1913#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00001914 if (LocaleNCompare(expression,"atanh",5) == 0)
cristy488fa882010-03-01 22:34:24 +00001915 {
1916 expression+=5;
1917 break;
1918 }
cristyb33454f2011-08-03 02:10:45 +00001919#endif
cristy488fa882010-03-01 22:34:24 +00001920 break;
cristy3ed852e2009-09-05 21:47:34 +00001921 }
cristy62d455f2011-11-03 11:42:28 +00001922 case 'E':
1923 case 'e':
1924 {
1925 if ((LocaleNCompare(expression,"E+",2) == 0) ||
1926 (LocaleNCompare(expression,"E-",2) == 0))
1927 {
1928 expression+=2; /* scientific notation */
1929 break;
1930 }
1931 }
cristy488fa882010-03-01 22:34:24 +00001932 case 'J':
1933 case 'j':
1934 {
1935 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1936 (LocaleNCompare(expression,"j1",2) == 0))
1937 {
1938 expression+=2;
1939 break;
1940 }
1941 break;
1942 }
cristy2def9322010-06-18 23:59:37 +00001943 case '#':
1944 {
1945 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1946 expression++;
1947 break;
1948 }
cristy488fa882010-03-01 22:34:24 +00001949 default:
1950 break;
1951 }
cristy3ed852e2009-09-05 21:47:34 +00001952 if ((c == (int) '{') || (c == (int) '['))
1953 level++;
1954 else
1955 if ((c == (int) '}') || (c == (int) ']'))
1956 level--;
1957 if (level == 0)
1958 switch ((unsigned char) *expression)
1959 {
1960 case '~':
1961 case '!':
1962 {
1963 precedence=BitwiseComplementPrecedence;
1964 break;
1965 }
1966 case '^':
cristy6621e252010-08-13 00:42:57 +00001967 case '@':
cristy3ed852e2009-09-05 21:47:34 +00001968 {
1969 precedence=ExponentPrecedence;
1970 break;
1971 }
1972 default:
1973 {
1974 if (((c != 0) && ((isdigit((int) ((char) c)) != 0) ||
1975 (strchr(")",c) != (char *) NULL))) &&
1976 (((islower((int) ((char) *expression)) != 0) ||
1977 (strchr("(",(int) *expression) != (char *) NULL)) ||
1978 ((isdigit((int) ((char) c)) == 0) &&
1979 (isdigit((int) ((char) *expression)) != 0))) &&
1980 (strchr("xy",(int) *expression) == (char *) NULL))
1981 precedence=MultiplyPrecedence;
1982 break;
1983 }
1984 case '*':
1985 case '/':
1986 case '%':
1987 {
1988 precedence=MultiplyPrecedence;
1989 break;
1990 }
1991 case '+':
1992 case '-':
1993 {
1994 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
1995 (isalpha(c) != 0))
1996 precedence=AdditionPrecedence;
1997 break;
1998 }
1999 case LeftShiftOperator:
2000 case RightShiftOperator:
2001 {
2002 precedence=ShiftPrecedence;
2003 break;
2004 }
2005 case '<':
2006 case LessThanEqualOperator:
2007 case GreaterThanEqualOperator:
2008 case '>':
2009 {
2010 precedence=RelationalPrecedence;
2011 break;
2012 }
2013 case EqualOperator:
2014 case NotEqualOperator:
2015 {
2016 precedence=EquivalencyPrecedence;
2017 break;
2018 }
2019 case '&':
2020 {
2021 precedence=BitwiseAndPrecedence;
2022 break;
2023 }
2024 case '|':
2025 {
2026 precedence=BitwiseOrPrecedence;
2027 break;
2028 }
2029 case LogicalAndOperator:
2030 {
2031 precedence=LogicalAndPrecedence;
2032 break;
2033 }
2034 case LogicalOrOperator:
2035 {
2036 precedence=LogicalOrPrecedence;
2037 break;
2038 }
cristy116af162010-08-13 01:25:47 +00002039 case ExponentialNotation:
2040 {
2041 precedence=ExponentialNotationPrecedence;
2042 break;
2043 }
cristy3ed852e2009-09-05 21:47:34 +00002044 case ':':
2045 case '?':
2046 {
2047 precedence=TernaryPrecedence;
2048 break;
2049 }
2050 case '=':
2051 {
2052 precedence=AssignmentPrecedence;
2053 break;
2054 }
2055 case ',':
2056 {
2057 precedence=CommaPrecedence;
2058 break;
2059 }
2060 case ';':
2061 {
2062 precedence=SeparatorPrecedence;
2063 break;
2064 }
2065 }
2066 if ((precedence == BitwiseComplementPrecedence) ||
2067 (precedence == TernaryPrecedence) ||
2068 (precedence == AssignmentPrecedence))
2069 {
2070 if (precedence > target)
2071 {
2072 /*
2073 Right-to-left associativity.
2074 */
2075 target=precedence;
2076 subexpression=expression;
2077 }
2078 }
2079 else
2080 if (precedence >= target)
2081 {
2082 /*
2083 Left-to-right associativity.
2084 */
2085 target=precedence;
2086 subexpression=expression;
2087 }
2088 if (strchr("(",(int) *expression) != (char *) NULL)
2089 expression=FxSubexpression(expression,exception);
2090 c=(int) (*expression++);
2091 }
2092 return(subexpression);
2093}
2094
2095static MagickRealType FxEvaluateSubexpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002096 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002097 const char *expression,MagickRealType *beta,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002098{
2099 char
2100 *q,
2101 subexpression[MaxTextExtent];
2102
2103 MagickRealType
2104 alpha,
2105 gamma;
2106
2107 register const char
2108 *p;
2109
2110 *beta=0.0;
2111 if (exception->severity != UndefinedException)
2112 return(0.0);
2113 while (isspace((int) *expression) != 0)
2114 expression++;
2115 if (*expression == '\0')
2116 {
2117 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
2118 "MissingExpression","`%s'",expression);
2119 return(0.0);
2120 }
cristy66322f02010-05-17 11:40:48 +00002121 *subexpression='\0';
cristy3ed852e2009-09-05 21:47:34 +00002122 p=FxOperatorPrecedence(expression,exception);
2123 if (p != (const char *) NULL)
2124 {
2125 (void) CopyMagickString(subexpression,expression,(size_t)
2126 (p-expression+1));
2127 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2128 exception);
2129 switch ((unsigned char) *p)
2130 {
2131 case '~':
2132 {
2133 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002134 *beta=(MagickRealType) (~(size_t) *beta);
cristy3ed852e2009-09-05 21:47:34 +00002135 return(*beta);
2136 }
2137 case '!':
2138 {
2139 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2140 return(*beta == 0.0 ? 1.0 : 0.0);
2141 }
2142 case '^':
2143 {
2144 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2145 channel,x,y,++p,beta,exception));
2146 return(*beta);
2147 }
2148 case '*':
cristy116af162010-08-13 01:25:47 +00002149 case ExponentialNotation:
cristy3ed852e2009-09-05 21:47:34 +00002150 {
2151 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2152 return(alpha*(*beta));
2153 }
2154 case '/':
2155 {
2156 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2157 if (*beta == 0.0)
2158 {
2159 if (exception->severity == UndefinedException)
2160 (void) ThrowMagickException(exception,GetMagickModule(),
2161 OptionError,"DivideByZero","`%s'",expression);
2162 return(0.0);
2163 }
2164 return(alpha/(*beta));
2165 }
2166 case '%':
2167 {
2168 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2169 *beta=fabs(floor(((double) *beta)+0.5));
2170 if (*beta == 0.0)
2171 {
2172 (void) ThrowMagickException(exception,GetMagickModule(),
2173 OptionError,"DivideByZero","`%s'",expression);
2174 return(0.0);
2175 }
2176 return(fmod((double) alpha,(double) *beta));
2177 }
2178 case '+':
2179 {
2180 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2181 return(alpha+(*beta));
2182 }
2183 case '-':
2184 {
2185 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2186 return(alpha-(*beta));
2187 }
2188 case LeftShiftOperator:
2189 {
2190 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002191 *beta=(MagickRealType) ((size_t) (alpha+0.5) << (size_t)
cristy3ed852e2009-09-05 21:47:34 +00002192 (gamma+0.5));
2193 return(*beta);
2194 }
2195 case RightShiftOperator:
2196 {
2197 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002198 *beta=(MagickRealType) ((size_t) (alpha+0.5) >> (size_t)
cristy3ed852e2009-09-05 21:47:34 +00002199 (gamma+0.5));
2200 return(*beta);
2201 }
2202 case '<':
2203 {
2204 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2205 return(alpha < *beta ? 1.0 : 0.0);
2206 }
2207 case LessThanEqualOperator:
2208 {
2209 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2210 return(alpha <= *beta ? 1.0 : 0.0);
2211 }
2212 case '>':
2213 {
2214 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2215 return(alpha > *beta ? 1.0 : 0.0);
2216 }
2217 case GreaterThanEqualOperator:
2218 {
2219 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2220 return(alpha >= *beta ? 1.0 : 0.0);
2221 }
2222 case EqualOperator:
2223 {
2224 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2225 return(fabs(alpha-(*beta)) <= MagickEpsilon ? 1.0 : 0.0);
2226 }
2227 case NotEqualOperator:
2228 {
2229 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2230 return(fabs(alpha-(*beta)) > MagickEpsilon ? 1.0 : 0.0);
2231 }
2232 case '&':
2233 {
2234 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002235 *beta=(MagickRealType) ((size_t) (alpha+0.5) & (size_t)
cristy3ed852e2009-09-05 21:47:34 +00002236 (gamma+0.5));
2237 return(*beta);
2238 }
2239 case '|':
2240 {
2241 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002242 *beta=(MagickRealType) ((size_t) (alpha+0.5) | (size_t)
cristy3ed852e2009-09-05 21:47:34 +00002243 (gamma+0.5));
2244 return(*beta);
2245 }
2246 case LogicalAndOperator:
2247 {
2248 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2249 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2250 return(*beta);
2251 }
2252 case LogicalOrOperator:
2253 {
2254 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2255 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2256 return(*beta);
2257 }
2258 case '?':
2259 {
2260 MagickRealType
2261 gamma;
2262
2263 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2264 q=subexpression;
2265 p=StringToken(":",&q);
2266 if (q == (char *) NULL)
2267 {
2268 (void) ThrowMagickException(exception,GetMagickModule(),
2269 OptionError,"UnableToParseExpression","`%s'",subexpression);
2270 return(0.0);
2271 }
2272 if (fabs((double) alpha) > MagickEpsilon)
2273 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2274 else
2275 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2276 return(gamma);
2277 }
2278 case '=':
2279 {
2280 char
2281 numeric[MaxTextExtent];
2282
2283 q=subexpression;
2284 while (isalpha((int) ((unsigned char) *q)) != 0)
2285 q++;
2286 if (*q != '\0')
2287 {
2288 (void) ThrowMagickException(exception,GetMagickModule(),
2289 OptionError,"UnableToParseExpression","`%s'",subexpression);
2290 return(0.0);
2291 }
2292 ClearMagickException(exception);
2293 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristyb51dff52011-05-19 16:55:47 +00002294 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
cristy8cd5b312010-01-07 01:10:24 +00002295 *beta);
cristy3ed852e2009-09-05 21:47:34 +00002296 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2297 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2298 subexpression),ConstantString(numeric));
2299 return(*beta);
2300 }
2301 case ',':
2302 {
2303 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2304 return(alpha);
2305 }
2306 case ';':
2307 {
2308 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2309 return(*beta);
2310 }
2311 default:
2312 {
2313 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2314 exception);
2315 return(gamma);
2316 }
2317 }
2318 }
2319 if (strchr("(",(int) *expression) != (char *) NULL)
2320 {
2321 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2322 subexpression[strlen(subexpression)-1]='\0';
2323 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2324 exception);
2325 return(gamma);
2326 }
cristy8b8a3ae2010-10-23 18:49:46 +00002327 switch (*expression)
cristy3ed852e2009-09-05 21:47:34 +00002328 {
2329 case '+':
2330 {
2331 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2332 exception);
2333 return(1.0*gamma);
2334 }
2335 case '-':
2336 {
2337 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2338 exception);
2339 return(-1.0*gamma);
2340 }
2341 case '~':
2342 {
2343 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2344 exception);
cristybb503372010-05-27 20:51:26 +00002345 return((MagickRealType) (~(size_t) (gamma+0.5)));
cristy3ed852e2009-09-05 21:47:34 +00002346 }
2347 case 'A':
2348 case 'a':
2349 {
2350 if (LocaleNCompare(expression,"abs",3) == 0)
2351 {
2352 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2353 exception);
2354 return((MagickRealType) fabs((double) alpha));
2355 }
cristyb33454f2011-08-03 02:10:45 +00002356#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00002357 if (LocaleNCompare(expression,"acosh",5) == 0)
2358 {
2359 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2360 exception);
2361 return((MagickRealType) acosh((double) alpha));
2362 }
cristyb33454f2011-08-03 02:10:45 +00002363#endif
cristy3ed852e2009-09-05 21:47:34 +00002364 if (LocaleNCompare(expression,"acos",4) == 0)
2365 {
2366 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2367 exception);
2368 return((MagickRealType) acos((double) alpha));
2369 }
cristy43c22f42010-03-30 12:34:07 +00002370#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002371 if (LocaleNCompare(expression,"airy",4) == 0)
2372 {
2373 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2374 exception);
2375 if (alpha == 0.0)
cristy2dd03222010-03-30 22:12:11 +00002376 return(1.0);
2377 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
cristy43c22f42010-03-30 12:34:07 +00002378 return(gamma*gamma);
cristyee56cf12010-03-01 22:17:06 +00002379 }
cristy43c22f42010-03-30 12:34:07 +00002380#endif
cristyb33454f2011-08-03 02:10:45 +00002381#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00002382 if (LocaleNCompare(expression,"asinh",5) == 0)
2383 {
2384 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2385 exception);
2386 return((MagickRealType) asinh((double) alpha));
2387 }
cristyb33454f2011-08-03 02:10:45 +00002388#endif
cristy3ed852e2009-09-05 21:47:34 +00002389 if (LocaleNCompare(expression,"asin",4) == 0)
2390 {
2391 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2392 exception);
2393 return((MagickRealType) asin((double) alpha));
2394 }
2395 if (LocaleNCompare(expression,"alt",3) == 0)
2396 {
2397 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2398 exception);
cristybb503372010-05-27 20:51:26 +00002399 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
cristy3ed852e2009-09-05 21:47:34 +00002400 }
2401 if (LocaleNCompare(expression,"atan2",5) == 0)
2402 {
2403 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2404 exception);
2405 return((MagickRealType) atan2((double) alpha,(double) *beta));
2406 }
cristyb33454f2011-08-03 02:10:45 +00002407#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00002408 if (LocaleNCompare(expression,"atanh",5) == 0)
2409 {
2410 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2411 exception);
2412 return((MagickRealType) atanh((double) alpha));
2413 }
cristyb33454f2011-08-03 02:10:45 +00002414#endif
cristy3ed852e2009-09-05 21:47:34 +00002415 if (LocaleNCompare(expression,"atan",4) == 0)
2416 {
2417 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2418 exception);
2419 return((MagickRealType) atan((double) alpha));
2420 }
2421 if (LocaleCompare(expression,"a") == 0)
2422 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2423 break;
2424 }
2425 case 'B':
2426 case 'b':
2427 {
2428 if (LocaleCompare(expression,"b") == 0)
2429 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2430 break;
2431 }
2432 case 'C':
2433 case 'c':
2434 {
2435 if (LocaleNCompare(expression,"ceil",4) == 0)
2436 {
2437 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2438 exception);
2439 return((MagickRealType) ceil((double) alpha));
2440 }
2441 if (LocaleNCompare(expression,"cosh",4) == 0)
2442 {
2443 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2444 exception);
2445 return((MagickRealType) cosh((double) alpha));
2446 }
2447 if (LocaleNCompare(expression,"cos",3) == 0)
2448 {
2449 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2450 exception);
2451 return((MagickRealType) cos((double) alpha));
2452 }
2453 if (LocaleCompare(expression,"c") == 0)
2454 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2455 break;
2456 }
2457 case 'D':
2458 case 'd':
2459 {
2460 if (LocaleNCompare(expression,"debug",5) == 0)
2461 {
2462 const char
2463 *type;
2464
2465 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2466 exception);
2467 if (fx_info->images->colorspace == CMYKColorspace)
2468 switch (channel)
2469 {
cristy0568ffc2011-07-25 16:54:14 +00002470 case CyanPixelChannel: type="cyan"; break;
2471 case MagentaPixelChannel: type="magenta"; break;
2472 case YellowPixelChannel: type="yellow"; break;
2473 case AlphaPixelChannel: type="opacity"; break;
2474 case BlackPixelChannel: type="black"; break;
cristy3ed852e2009-09-05 21:47:34 +00002475 default: type="unknown"; break;
2476 }
2477 else
2478 switch (channel)
2479 {
cristy0568ffc2011-07-25 16:54:14 +00002480 case RedPixelChannel: type="red"; break;
2481 case GreenPixelChannel: type="green"; break;
2482 case BluePixelChannel: type="blue"; break;
2483 case AlphaPixelChannel: type="opacity"; break;
cristy3ed852e2009-09-05 21:47:34 +00002484 default: type="unknown"; break;
2485 }
2486 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2487 if (strlen(subexpression) > 1)
2488 subexpression[strlen(subexpression)-1]='\0';
2489 if (fx_info->file != (FILE *) NULL)
cristy1e604812011-05-19 18:07:50 +00002490 (void) FormatLocaleFile(fx_info->file,
2491 "%s[%.20g,%.20g].%s: %s=%.*g\n",fx_info->images->filename,
2492 (double) x,(double) y,type,subexpression,GetMagickPrecision(),
2493 (double) alpha);
cristy3ed852e2009-09-05 21:47:34 +00002494 return(0.0);
2495 }
2496 break;
2497 }
2498 case 'E':
2499 case 'e':
2500 {
2501 if (LocaleCompare(expression,"epsilon") == 0)
2502 return((MagickRealType) MagickEpsilon);
2503 if (LocaleNCompare(expression,"exp",3) == 0)
2504 {
2505 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2506 exception);
2507 return((MagickRealType) exp((double) alpha));
2508 }
2509 if (LocaleCompare(expression,"e") == 0)
2510 return((MagickRealType) 2.7182818284590452354);
2511 break;
2512 }
2513 case 'F':
2514 case 'f':
2515 {
2516 if (LocaleNCompare(expression,"floor",5) == 0)
2517 {
2518 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2519 exception);
2520 return((MagickRealType) floor((double) alpha));
2521 }
2522 break;
2523 }
2524 case 'G':
2525 case 'g':
2526 {
cristy9eeedea2011-11-02 19:04:05 +00002527 if (LocaleNCompare(expression,"gauss",5) == 0)
2528 {
2529 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2530 exception);
2531 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
2532 return((MagickRealType) gamma);
2533 }
cristyb0aad4c2011-11-02 19:30:35 +00002534 if (LocaleNCompare(expression,"gcd",3) == 0)
2535 {
2536 MagickOffsetType
2537 gcd;
2538
2539 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2540 exception);
cristy76461162011-11-02 19:32:19 +00002541 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType)
2542 (*beta+0.5));
cristyb0aad4c2011-11-02 19:30:35 +00002543 return((MagickRealType) gcd);
2544 }
cristy3ed852e2009-09-05 21:47:34 +00002545 if (LocaleCompare(expression,"g") == 0)
2546 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2547 break;
2548 }
2549 case 'H':
2550 case 'h':
2551 {
2552 if (LocaleCompare(expression,"h") == 0)
2553 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2554 if (LocaleCompare(expression,"hue") == 0)
2555 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2556 if (LocaleNCompare(expression,"hypot",5) == 0)
2557 {
2558 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2559 exception);
2560 return((MagickRealType) hypot((double) alpha,(double) *beta));
2561 }
2562 break;
2563 }
2564 case 'K':
2565 case 'k':
2566 {
2567 if (LocaleCompare(expression,"k") == 0)
2568 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2569 break;
2570 }
2571 case 'I':
2572 case 'i':
2573 {
2574 if (LocaleCompare(expression,"intensity") == 0)
2575 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2576 if (LocaleNCompare(expression,"int",3) == 0)
2577 {
2578 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2579 exception);
cristy16788e42010-08-13 13:44:26 +00002580 return((MagickRealType) floor(alpha));
cristy3ed852e2009-09-05 21:47:34 +00002581 }
cristy639399c2011-11-02 19:16:15 +00002582 if (LocaleNCompare(expression,"isnan",5) == 0)
2583 {
2584 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2585 exception);
cristy17a10202011-11-02 19:17:04 +00002586 return((MagickRealType) !!isnan((double) alpha));
cristy639399c2011-11-02 19:16:15 +00002587 }
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);
2602 return((MagickRealType) j0((double) alpha));
2603 }
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);
2610 return((MagickRealType) j1((double) alpha));
2611 }
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);
cristy69928f92010-03-12 13:27:09 +00002620 gamma=(MagickRealType) (2.0*j1((double) (MagickPI*alpha))/
cristyfce2f7b2010-03-12 00:29:49 +00002621 (MagickPI*alpha));
2622 return(gamma);
cristya6a09e72010-03-02 14:51:02 +00002623 }
cristyaa018fa2010-04-08 23:03:54 +00002624#endif
cristy3ed852e2009-09-05 21:47:34 +00002625 break;
2626 }
2627 case 'L':
2628 case 'l':
2629 {
2630 if (LocaleNCompare(expression,"ln",2) == 0)
2631 {
2632 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2633 exception);
2634 return((MagickRealType) log((double) alpha));
2635 }
cristyc8ed5322010-08-31 12:07:59 +00002636 if (LocaleNCompare(expression,"logtwo",6) == 0)
cristy3ed852e2009-09-05 21:47:34 +00002637 {
cristyc8ed5322010-08-31 12:07:59 +00002638 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
cristy3ed852e2009-09-05 21:47:34 +00002639 exception);
2640 return((MagickRealType) log10((double) alpha))/log10(2.0);
2641 }
2642 if (LocaleNCompare(expression,"log",3) == 0)
2643 {
2644 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2645 exception);
2646 return((MagickRealType) log10((double) alpha));
2647 }
2648 if (LocaleCompare(expression,"lightness") == 0)
2649 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2650 break;
2651 }
2652 case 'M':
2653 case 'm':
2654 {
2655 if (LocaleCompare(expression,"MaxRGB") == 0)
2656 return((MagickRealType) QuantumRange);
2657 if (LocaleNCompare(expression,"maxima",6) == 0)
2658 break;
2659 if (LocaleNCompare(expression,"max",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002660 {
2661 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2662 exception);
2663 return(alpha > *beta ? alpha : *beta);
2664 }
cristy3ed852e2009-09-05 21:47:34 +00002665 if (LocaleNCompare(expression,"minima",6) == 0)
2666 break;
2667 if (LocaleNCompare(expression,"min",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002668 {
2669 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2670 exception);
2671 return(alpha < *beta ? alpha : *beta);
2672 }
cristy3ed852e2009-09-05 21:47:34 +00002673 if (LocaleNCompare(expression,"mod",3) == 0)
2674 {
2675 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2676 exception);
cristy984049c2011-11-03 18:34:58 +00002677 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2678 return(gamma);
cristy3ed852e2009-09-05 21:47:34 +00002679 }
2680 if (LocaleCompare(expression,"m") == 0)
2681 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2682 break;
2683 }
2684 case 'N':
2685 case 'n':
2686 {
cristyad3502e2011-11-02 19:10:45 +00002687 if (LocaleNCompare(expression,"not",3) == 0)
2688 {
2689 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2690 exception);
2691 return((MagickRealType) (alpha < MagickEpsilon));
2692 }
cristy3ed852e2009-09-05 21:47:34 +00002693 if (LocaleCompare(expression,"n") == 0)
2694 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2695 break;
2696 }
2697 case 'O':
2698 case 'o':
2699 {
2700 if (LocaleCompare(expression,"Opaque") == 0)
2701 return(1.0);
2702 if (LocaleCompare(expression,"o") == 0)
2703 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2704 break;
2705 }
2706 case 'P':
2707 case 'p':
2708 {
cristy670aa3c2011-11-03 00:54:00 +00002709 if (LocaleCompare(expression,"phi") == 0)
2710 return((MagickRealType) MagickPHI);
cristy3ed852e2009-09-05 21:47:34 +00002711 if (LocaleCompare(expression,"pi") == 0)
2712 return((MagickRealType) MagickPI);
2713 if (LocaleNCompare(expression,"pow",3) == 0)
2714 {
2715 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2716 exception);
2717 return((MagickRealType) pow((double) alpha,(double) *beta));
2718 }
2719 if (LocaleCompare(expression,"p") == 0)
2720 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2721 break;
2722 }
2723 case 'Q':
2724 case 'q':
2725 {
2726 if (LocaleCompare(expression,"QuantumRange") == 0)
2727 return((MagickRealType) QuantumRange);
2728 if (LocaleCompare(expression,"QuantumScale") == 0)
2729 return((MagickRealType) QuantumScale);
2730 break;
2731 }
2732 case 'R':
2733 case 'r':
2734 {
2735 if (LocaleNCompare(expression,"rand",4) == 0)
2736 return((MagickRealType) GetPseudoRandomValue(fx_info->random_info));
2737 if (LocaleNCompare(expression,"round",5) == 0)
2738 {
2739 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2740 exception);
cristy16788e42010-08-13 13:44:26 +00002741 return((MagickRealType) floor((double) alpha+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002742 }
2743 if (LocaleCompare(expression,"r") == 0)
2744 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2745 break;
2746 }
2747 case 'S':
2748 case 's':
2749 {
2750 if (LocaleCompare(expression,"saturation") == 0)
2751 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2752 if (LocaleNCompare(expression,"sign",4) == 0)
2753 {
2754 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2755 exception);
2756 return(alpha < 0.0 ? -1.0 : 1.0);
2757 }
cristya6a09e72010-03-02 14:51:02 +00002758 if (LocaleNCompare(expression,"sinc",4) == 0)
2759 {
2760 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2761 exception);
2762 if (alpha == 0)
2763 return(1.0);
2764 gamma=(MagickRealType) (sin((double) (MagickPI*alpha))/
2765 (MagickPI*alpha));
2766 return(gamma);
2767 }
cristy3ed852e2009-09-05 21:47:34 +00002768 if (LocaleNCompare(expression,"sinh",4) == 0)
2769 {
2770 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2771 exception);
2772 return((MagickRealType) sinh((double) alpha));
2773 }
2774 if (LocaleNCompare(expression,"sin",3) == 0)
2775 {
2776 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2777 exception);
2778 return((MagickRealType) sin((double) alpha));
2779 }
2780 if (LocaleNCompare(expression,"sqrt",4) == 0)
2781 {
2782 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2783 exception);
2784 return((MagickRealType) sqrt((double) alpha));
2785 }
cristy9eeedea2011-11-02 19:04:05 +00002786 if (LocaleNCompare(expression,"squish",6) == 0)
2787 {
2788 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2789 exception);
2790 return((MagickRealType) (1.0/(1.0+exp((double) (4.0*alpha)))));
2791 }
cristy3ed852e2009-09-05 21:47:34 +00002792 if (LocaleCompare(expression,"s") == 0)
2793 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2794 break;
2795 }
2796 case 'T':
2797 case 't':
2798 {
2799 if (LocaleNCompare(expression,"tanh",4) == 0)
2800 {
2801 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2802 exception);
2803 return((MagickRealType) tanh((double) alpha));
2804 }
2805 if (LocaleNCompare(expression,"tan",3) == 0)
2806 {
2807 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2808 exception);
2809 return((MagickRealType) tan((double) alpha));
2810 }
2811 if (LocaleCompare(expression,"Transparent") == 0)
2812 return(0.0);
cristy16788e42010-08-13 13:44:26 +00002813 if (LocaleNCompare(expression,"trunc",5) == 0)
2814 {
2815 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2816 exception);
2817 if (alpha >= 0.0)
2818 return((MagickRealType) floor((double) alpha));
2819 return((MagickRealType) ceil((double) alpha));
2820 }
cristy3ed852e2009-09-05 21:47:34 +00002821 if (LocaleCompare(expression,"t") == 0)
2822 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2823 break;
2824 }
2825 case 'U':
2826 case 'u':
2827 {
2828 if (LocaleCompare(expression,"u") == 0)
2829 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2830 break;
2831 }
2832 case 'V':
2833 case 'v':
2834 {
2835 if (LocaleCompare(expression,"v") == 0)
2836 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2837 break;
2838 }
2839 case 'W':
2840 case 'w':
2841 {
cristy9eeedea2011-11-02 19:04:05 +00002842 if (LocaleNCompare(expression,"while",5) == 0)
2843 {
2844 do
2845 {
2846 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2847 exception);
2848 } while (fabs((double) alpha) >= MagickEpsilon);
2849 return((MagickRealType) *beta);
2850 }
cristy3ed852e2009-09-05 21:47:34 +00002851 if (LocaleCompare(expression,"w") == 0)
2852 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2853 break;
2854 }
2855 case 'Y':
2856 case 'y':
2857 {
2858 if (LocaleCompare(expression,"y") == 0)
2859 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2860 break;
2861 }
2862 case 'Z':
2863 case 'z':
2864 {
2865 if (LocaleCompare(expression,"z") == 0)
2866 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2867 break;
2868 }
2869 default:
2870 break;
2871 }
2872 q=(char *) expression;
cristyc1acd842011-05-19 23:05:47 +00002873 alpha=InterpretLocaleValue(expression,&q);
cristy3ed852e2009-09-05 21:47:34 +00002874 if (q == expression)
2875 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2876 return(alpha);
2877}
2878
cristy7832dc22011-09-05 01:21:53 +00002879MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
cristy3ed852e2009-09-05 21:47:34 +00002880 MagickRealType *alpha,ExceptionInfo *exception)
2881{
2882 MagickBooleanType
2883 status;
2884
cristy541ae572011-08-05 19:08:59 +00002885 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2886 exception);
cristy3ed852e2009-09-05 21:47:34 +00002887 return(status);
2888}
2889
2890MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
2891 MagickRealType *alpha,ExceptionInfo *exception)
2892{
2893 FILE
2894 *file;
2895
2896 MagickBooleanType
2897 status;
2898
2899 file=fx_info->file;
2900 fx_info->file=(FILE *) NULL;
cristy541ae572011-08-05 19:08:59 +00002901 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2902 exception);
cristy3ed852e2009-09-05 21:47:34 +00002903 fx_info->file=file;
2904 return(status);
2905}
2906
cristy7832dc22011-09-05 01:21:53 +00002907MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002908 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002909 MagickRealType *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002910{
2911 MagickRealType
2912 beta;
2913
2914 beta=0.0;
2915 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2916 exception);
2917 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2918}
2919
2920/*
2921%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2922% %
2923% %
2924% %
2925% F x I m a g e %
2926% %
2927% %
2928% %
2929%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2930%
2931% FxImage() applies a mathematical expression to the specified image.
2932%
2933% The format of the FxImage method is:
2934%
2935% Image *FxImage(const Image *image,const char *expression,
2936% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002937%
2938% A description of each parameter follows:
2939%
2940% o image: the image.
2941%
cristy3ed852e2009-09-05 21:47:34 +00002942% o expression: A mathematical expression.
2943%
2944% o exception: return any errors or warnings in this structure.
2945%
2946*/
2947
2948static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2949{
cristybb503372010-05-27 20:51:26 +00002950 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002951 i;
2952
2953 assert(fx_info != (FxInfo **) NULL);
cristybb503372010-05-27 20:51:26 +00002954 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
cristy3ed852e2009-09-05 21:47:34 +00002955 if (fx_info[i] != (FxInfo *) NULL)
2956 fx_info[i]=DestroyFxInfo(fx_info[i]);
cristyb41ee102010-10-04 16:46:15 +00002957 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
cristy3ed852e2009-09-05 21:47:34 +00002958 return(fx_info);
2959}
2960
2961static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2962 ExceptionInfo *exception)
2963{
2964 char
2965 *fx_expression;
2966
2967 FxInfo
2968 **fx_info;
2969
2970 MagickRealType
2971 alpha;
2972
cristybb503372010-05-27 20:51:26 +00002973 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002974 i;
2975
cristybb503372010-05-27 20:51:26 +00002976 size_t
cristy3ed852e2009-09-05 21:47:34 +00002977 number_threads;
2978
2979 number_threads=GetOpenMPMaximumThreads();
cristyb41ee102010-10-04 16:46:15 +00002980 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +00002981 if (fx_info == (FxInfo **) NULL)
2982 return((FxInfo **) NULL);
2983 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2984 if (*expression != '@')
2985 fx_expression=ConstantString(expression);
2986 else
2987 fx_expression=FileToString(expression+1,~0,exception);
cristybb503372010-05-27 20:51:26 +00002988 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +00002989 {
2990 fx_info[i]=AcquireFxInfo(image,fx_expression);
2991 if (fx_info[i] == (FxInfo *) NULL)
2992 return(DestroyFxThreadSet(fx_info));
2993 (void) FxPreprocessExpression(fx_info[i],&alpha,fx_info[i]->exception);
2994 }
2995 fx_expression=DestroyString(fx_expression);
2996 return(fx_info);
2997}
2998
2999MagickExport Image *FxImage(const Image *image,const char *expression,
3000 ExceptionInfo *exception)
3001{
cristy3ed852e2009-09-05 21:47:34 +00003002#define FxImageTag "Fx/Image"
3003
cristyfa112112010-01-04 17:48:07 +00003004 CacheView
cristy79cedc72011-07-25 00:41:15 +00003005 *fx_view,
3006 *image_view;
cristyfa112112010-01-04 17:48:07 +00003007
cristy3ed852e2009-09-05 21:47:34 +00003008 FxInfo
cristyfa112112010-01-04 17:48:07 +00003009 **restrict fx_info;
cristy3ed852e2009-09-05 21:47:34 +00003010
3011 Image
3012 *fx_image;
3013
cristy3ed852e2009-09-05 21:47:34 +00003014 MagickBooleanType
3015 status;
3016
cristybb503372010-05-27 20:51:26 +00003017 MagickOffsetType
3018 progress;
3019
cristy3ed852e2009-09-05 21:47:34 +00003020 MagickRealType
3021 alpha;
3022
cristybb503372010-05-27 20:51:26 +00003023 ssize_t
3024 y;
3025
cristy3ed852e2009-09-05 21:47:34 +00003026 assert(image != (Image *) NULL);
3027 assert(image->signature == MagickSignature);
3028 if (image->debug != MagickFalse)
3029 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy79cedc72011-07-25 00:41:15 +00003030 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00003031 if (fx_image == (Image *) NULL)
3032 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003033 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003034 {
cristy3ed852e2009-09-05 21:47:34 +00003035 fx_image=DestroyImage(fx_image);
3036 return((Image *) NULL);
3037 }
3038 fx_info=AcquireFxThreadSet(image,expression,exception);
3039 if (fx_info == (FxInfo **) NULL)
3040 {
3041 fx_image=DestroyImage(fx_image);
3042 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3043 }
3044 status=FxPreprocessExpression(fx_info[0],&alpha,exception);
3045 if (status == MagickFalse)
3046 {
3047 fx_image=DestroyImage(fx_image);
3048 fx_info=DestroyFxThreadSet(fx_info);
3049 return((Image *) NULL);
3050 }
3051 /*
3052 Fx image.
3053 */
3054 status=MagickTrue;
3055 progress=0;
cristy79cedc72011-07-25 00:41:15 +00003056 image_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00003057 fx_view=AcquireCacheView(fx_image);
cristyb5d5f722009-11-04 03:03:49 +00003058#if defined(MAGICKCORE_OPENMP_SUPPORT)
3059 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003060#endif
cristybb503372010-05-27 20:51:26 +00003061 for (y=0; y < (ssize_t) fx_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003062 {
cristy5c9e6f22010-09-17 17:31:01 +00003063 const int
3064 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003065
cristy79cedc72011-07-25 00:41:15 +00003066 register const Quantum
3067 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003068
cristy4c08aed2011-07-01 19:47:50 +00003069 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003070 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003071
cristy79cedc72011-07-25 00:41:15 +00003072 register ssize_t
3073 x;
3074
cristy3ed852e2009-09-05 21:47:34 +00003075 if (status == MagickFalse)
3076 continue;
cristy79cedc72011-07-25 00:41:15 +00003077 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy3ed852e2009-09-05 21:47:34 +00003078 q=GetCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
cristy79cedc72011-07-25 00:41:15 +00003079 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003080 {
3081 status=MagickFalse;
3082 continue;
3083 }
cristybb503372010-05-27 20:51:26 +00003084 for (x=0; x < (ssize_t) fx_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003085 {
cristy79cedc72011-07-25 00:41:15 +00003086 register ssize_t
3087 i;
3088
3089 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3090 {
3091 MagickRealType
3092 alpha;
3093
3094 PixelChannel
3095 channel;
3096
3097 PixelTrait
3098 fx_traits,
3099 traits;
3100
3101 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
cristy79cedc72011-07-25 00:41:15 +00003102 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3103 fx_traits=GetPixelChannelMapTraits(fx_image,channel);
cristy010d7d12011-08-31 01:02:48 +00003104 if ((traits == UndefinedPixelTrait) ||
3105 (fx_traits == UndefinedPixelTrait))
cristy79cedc72011-07-25 00:41:15 +00003106 continue;
3107 if ((fx_traits & CopyPixelTrait) != 0)
3108 {
cristy0beccfa2011-09-25 20:47:53 +00003109 SetPixelChannel(fx_image,channel,p[i],q);
cristy79cedc72011-07-25 00:41:15 +00003110 continue;
3111 }
3112 alpha=0.0;
cristy0568ffc2011-07-25 16:54:14 +00003113 (void) FxEvaluateChannelExpression(fx_info[id],(PixelChannel) i,x,y,
3114 &alpha,exception);
cristyb3a73b52011-07-26 01:34:43 +00003115 q[i]=ClampToQuantum((MagickRealType) QuantumRange*alpha);
cristy79cedc72011-07-25 00:41:15 +00003116 }
3117 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003118 q+=GetPixelChannels(fx_image);
cristy3ed852e2009-09-05 21:47:34 +00003119 }
3120 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3121 status=MagickFalse;
3122 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3123 {
3124 MagickBooleanType
3125 proceed;
3126
cristyb5d5f722009-11-04 03:03:49 +00003127#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy490408a2011-07-07 14:42:05 +00003128 #pragma omp critical (MagickCore_FxImage)
cristy3ed852e2009-09-05 21:47:34 +00003129#endif
3130 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3131 if (proceed == MagickFalse)
3132 status=MagickFalse;
3133 }
3134 }
cristy3ed852e2009-09-05 21:47:34 +00003135 fx_view=DestroyCacheView(fx_view);
cristy79cedc72011-07-25 00:41:15 +00003136 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003137 fx_info=DestroyFxThreadSet(fx_info);
3138 if (status == MagickFalse)
3139 fx_image=DestroyImage(fx_image);
3140 return(fx_image);
3141}
3142
3143/*
3144%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3145% %
3146% %
3147% %
3148% I m p l o d e I m a g e %
3149% %
3150% %
3151% %
3152%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3153%
3154% ImplodeImage() creates a new image that is a copy of an existing
3155% one with the image pixels "implode" by the specified percentage. It
3156% allocates the memory necessary for the new Image structure and returns a
3157% pointer to the new image.
3158%
3159% The format of the ImplodeImage method is:
3160%
3161% Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003162% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003163%
3164% A description of each parameter follows:
3165%
3166% o implode_image: Method ImplodeImage returns a pointer to the image
3167% after it is implode. A null image is returned if there is a memory
3168% shortage.
3169%
3170% o image: the image.
3171%
3172% o amount: Define the extent of the implosion.
3173%
cristy76f512e2011-09-12 01:26:56 +00003174% o method: the pixel interpolation method.
3175%
cristy3ed852e2009-09-05 21:47:34 +00003176% o exception: return any errors or warnings in this structure.
3177%
3178*/
3179MagickExport Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003180 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003181{
3182#define ImplodeImageTag "Implode/Image"
3183
cristyfa112112010-01-04 17:48:07 +00003184 CacheView
3185 *image_view,
3186 *implode_view;
3187
cristy3ed852e2009-09-05 21:47:34 +00003188 Image
3189 *implode_image;
3190
cristy3ed852e2009-09-05 21:47:34 +00003191 MagickBooleanType
3192 status;
3193
cristybb503372010-05-27 20:51:26 +00003194 MagickOffsetType
3195 progress;
3196
cristy3ed852e2009-09-05 21:47:34 +00003197 MagickRealType
3198 radius;
3199
3200 PointInfo
3201 center,
3202 scale;
3203
cristybb503372010-05-27 20:51:26 +00003204 ssize_t
3205 y;
3206
cristy3ed852e2009-09-05 21:47:34 +00003207 /*
3208 Initialize implode image attributes.
3209 */
3210 assert(image != (Image *) NULL);
3211 assert(image->signature == MagickSignature);
3212 if (image->debug != MagickFalse)
3213 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3214 assert(exception != (ExceptionInfo *) NULL);
3215 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00003216 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3217 exception);
cristy3ed852e2009-09-05 21:47:34 +00003218 if (implode_image == (Image *) NULL)
3219 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003220 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003221 {
cristy3ed852e2009-09-05 21:47:34 +00003222 implode_image=DestroyImage(implode_image);
3223 return((Image *) NULL);
3224 }
cristy4c08aed2011-07-01 19:47:50 +00003225 if (implode_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00003226 implode_image->matte=MagickTrue;
3227 /*
3228 Compute scaling factor.
3229 */
3230 scale.x=1.0;
3231 scale.y=1.0;
3232 center.x=0.5*image->columns;
3233 center.y=0.5*image->rows;
3234 radius=center.x;
3235 if (image->columns > image->rows)
3236 scale.y=(double) image->columns/(double) image->rows;
3237 else
3238 if (image->columns < image->rows)
3239 {
3240 scale.x=(double) image->rows/(double) image->columns;
3241 radius=center.y;
3242 }
3243 /*
3244 Implode image.
3245 */
3246 status=MagickTrue;
3247 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00003248 image_view=AcquireCacheView(image);
3249 implode_view=AcquireCacheView(implode_image);
cristyb5d5f722009-11-04 03:03:49 +00003250#if defined(MAGICKCORE_OPENMP_SUPPORT)
3251 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003252#endif
cristybb503372010-05-27 20:51:26 +00003253 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003254 {
cristy3ed852e2009-09-05 21:47:34 +00003255 MagickRealType
3256 distance;
3257
3258 PointInfo
3259 delta;
3260
cristy6d188022011-09-12 13:23:33 +00003261 register const Quantum
3262 *restrict p;
3263
cristybb503372010-05-27 20:51:26 +00003264 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003265 x;
3266
cristy4c08aed2011-07-01 19:47:50 +00003267 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003268 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003269
3270 if (status == MagickFalse)
3271 continue;
cristy6d188022011-09-12 13:23:33 +00003272 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy3ed852e2009-09-05 21:47:34 +00003273 q=GetCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
3274 exception);
cristy6d188022011-09-12 13:23:33 +00003275 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003276 {
3277 status=MagickFalse;
3278 continue;
3279 }
cristy3ed852e2009-09-05 21:47:34 +00003280 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00003281 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003282 {
cristy6d188022011-09-12 13:23:33 +00003283 register ssize_t
3284 i;
3285
cristy3ed852e2009-09-05 21:47:34 +00003286 /*
3287 Determine if the pixel is within an ellipse.
3288 */
3289 delta.x=scale.x*(double) (x-center.x);
3290 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00003291 if (distance >= (radius*radius))
3292 {
3293 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3294 q[i]=p[i];
3295 }
3296 else
cristy3ed852e2009-09-05 21:47:34 +00003297 {
3298 double
3299 factor;
3300
3301 /*
3302 Implode the pixel.
3303 */
3304 factor=1.0;
3305 if (distance > 0.0)
3306 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/
3307 radius/2)),-amount);
cristy76f512e2011-09-12 01:26:56 +00003308 status=InterpolatePixelChannels(image,image_view,implode_image,method,
3309 (double) (factor*delta.x/scale.x+center.x),(double) (factor*delta.y/
3310 scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00003311 }
cristy6d188022011-09-12 13:23:33 +00003312 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003313 q+=GetPixelChannels(implode_image);
cristy3ed852e2009-09-05 21:47:34 +00003314 }
3315 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3316 status=MagickFalse;
3317 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3318 {
3319 MagickBooleanType
3320 proceed;
3321
cristyb5d5f722009-11-04 03:03:49 +00003322#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003323 #pragma omp critical (MagickCore_ImplodeImage)
3324#endif
3325 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3326 if (proceed == MagickFalse)
3327 status=MagickFalse;
3328 }
3329 }
3330 implode_view=DestroyCacheView(implode_view);
3331 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003332 if (status == MagickFalse)
3333 implode_image=DestroyImage(implode_image);
3334 return(implode_image);
3335}
3336
3337/*
3338%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3339% %
3340% %
3341% %
3342% M o r p h I m a g e s %
3343% %
3344% %
3345% %
3346%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3347%
3348% The MorphImages() method requires a minimum of two images. The first
3349% image is transformed into the second by a number of intervening images
3350% as specified by frames.
3351%
3352% The format of the MorphImage method is:
3353%
cristybb503372010-05-27 20:51:26 +00003354% Image *MorphImages(const Image *image,const size_t number_frames,
cristy3ed852e2009-09-05 21:47:34 +00003355% ExceptionInfo *exception)
3356%
3357% A description of each parameter follows:
3358%
3359% o image: the image.
3360%
3361% o number_frames: Define the number of in-between image to generate.
3362% The more in-between frames, the smoother the morph.
3363%
3364% o exception: return any errors or warnings in this structure.
3365%
3366*/
3367MagickExport Image *MorphImages(const Image *image,
cristybb503372010-05-27 20:51:26 +00003368 const size_t number_frames,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003369{
3370#define MorphImageTag "Morph/Image"
3371
3372 Image
3373 *morph_image,
3374 *morph_images;
3375
cristy9d314ff2011-03-09 01:30:28 +00003376 MagickBooleanType
3377 status;
cristy3ed852e2009-09-05 21:47:34 +00003378
3379 MagickOffsetType
3380 scene;
3381
3382 MagickRealType
3383 alpha,
3384 beta;
3385
3386 register const Image
3387 *next;
3388
cristybb503372010-05-27 20:51:26 +00003389 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003390 i;
3391
cristy9d314ff2011-03-09 01:30:28 +00003392 ssize_t
3393 y;
cristy3ed852e2009-09-05 21:47:34 +00003394
3395 /*
3396 Clone first frame in sequence.
3397 */
3398 assert(image != (Image *) NULL);
3399 assert(image->signature == MagickSignature);
3400 if (image->debug != MagickFalse)
3401 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3402 assert(exception != (ExceptionInfo *) NULL);
3403 assert(exception->signature == MagickSignature);
3404 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3405 if (morph_images == (Image *) NULL)
3406 return((Image *) NULL);
3407 if (GetNextImageInList(image) == (Image *) NULL)
3408 {
3409 /*
3410 Morph single image.
3411 */
cristybb503372010-05-27 20:51:26 +00003412 for (i=1; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003413 {
3414 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3415 if (morph_image == (Image *) NULL)
3416 {
3417 morph_images=DestroyImageList(morph_images);
3418 return((Image *) NULL);
3419 }
3420 AppendImageToList(&morph_images,morph_image);
cristy8b27a6d2010-02-14 03:31:15 +00003421 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003422 {
cristy8b27a6d2010-02-14 03:31:15 +00003423 MagickBooleanType
3424 proceed;
3425
cristybb503372010-05-27 20:51:26 +00003426 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3427 number_frames);
cristy8b27a6d2010-02-14 03:31:15 +00003428 if (proceed == MagickFalse)
3429 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00003430 }
3431 }
3432 return(GetFirstImageInList(morph_images));
3433 }
3434 /*
3435 Morph image sequence.
3436 */
3437 status=MagickTrue;
3438 scene=0;
3439 next=image;
3440 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3441 {
cristybb503372010-05-27 20:51:26 +00003442 for (i=0; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003443 {
3444 CacheView
3445 *image_view,
3446 *morph_view;
3447
3448 beta=(MagickRealType) (i+1.0)/(MagickRealType) (number_frames+1.0);
3449 alpha=1.0-beta;
cristy15b98cd2010-09-12 19:42:50 +00003450 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
cristybb503372010-05-27 20:51:26 +00003451 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*
cristy15b98cd2010-09-12 19:42:50 +00003452 next->rows+beta*GetNextImageInList(next)->rows+0.5),
3453 next->filter,next->blur,exception);
cristy3ed852e2009-09-05 21:47:34 +00003454 if (morph_image == (Image *) NULL)
3455 {
3456 morph_images=DestroyImageList(morph_images);
3457 return((Image *) NULL);
3458 }
cristy574cc262011-08-05 01:23:58 +00003459 if (SetImageStorageClass(morph_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003460 {
cristy3ed852e2009-09-05 21:47:34 +00003461 morph_image=DestroyImage(morph_image);
3462 return((Image *) NULL);
3463 }
3464 AppendImageToList(&morph_images,morph_image);
3465 morph_images=GetLastImageInList(morph_images);
cristy15b98cd2010-09-12 19:42:50 +00003466 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
3467 morph_images->rows,GetNextImageInList(next)->filter,
3468 GetNextImageInList(next)->blur,exception);
cristy3ed852e2009-09-05 21:47:34 +00003469 if (morph_image == (Image *) NULL)
3470 {
3471 morph_images=DestroyImageList(morph_images);
3472 return((Image *) NULL);
3473 }
3474 image_view=AcquireCacheView(morph_image);
3475 morph_view=AcquireCacheView(morph_images);
cristyb5d5f722009-11-04 03:03:49 +00003476#if defined(MAGICKCORE_OPENMP_SUPPORT)
3477 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +00003478#endif
cristybb503372010-05-27 20:51:26 +00003479 for (y=0; y < (ssize_t) morph_images->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003480 {
3481 MagickBooleanType
3482 sync;
3483
cristy4c08aed2011-07-01 19:47:50 +00003484 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003485 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003486
cristybb503372010-05-27 20:51:26 +00003487 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003488 x;
3489
cristy4c08aed2011-07-01 19:47:50 +00003490 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003491 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003492
3493 if (status == MagickFalse)
3494 continue;
3495 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3496 exception);
3497 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3498 exception);
cristy4c08aed2011-07-01 19:47:50 +00003499 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003500 {
3501 status=MagickFalse;
3502 continue;
3503 }
cristybb503372010-05-27 20:51:26 +00003504 for (x=0; x < (ssize_t) morph_images->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003505 {
cristy4c08aed2011-07-01 19:47:50 +00003506 SetPixelRed(morph_images,ClampToQuantum(alpha*
3507 GetPixelRed(morph_images,q)+beta*GetPixelRed(morph_image,p)),q);
3508 SetPixelGreen(morph_images,ClampToQuantum(alpha*
3509 GetPixelGreen(morph_images,q)+beta*GetPixelGreen(morph_image,p)),q);
3510 SetPixelBlue(morph_images,ClampToQuantum(alpha*
3511 GetPixelBlue(morph_images,q)+beta*GetPixelBlue(morph_image,p)),q);
3512 SetPixelAlpha(morph_images,ClampToQuantum(alpha*
3513 GetPixelAlpha(morph_images,q)+beta*GetPixelAlpha(morph_image,p)),q);
3514 if ((morph_image->colorspace == CMYKColorspace) &&
3515 (morph_images->colorspace == CMYKColorspace))
3516 SetPixelBlack(morph_images,ClampToQuantum(alpha*
3517 GetPixelBlack(morph_images,q)+beta*GetPixelBlack(morph_image,p)),
3518 q);
cristyed231572011-07-14 02:18:59 +00003519 p+=GetPixelChannels(morph_image);
3520 q+=GetPixelChannels(morph_images);
cristy3ed852e2009-09-05 21:47:34 +00003521 }
3522 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3523 if (sync == MagickFalse)
3524 status=MagickFalse;
3525 }
3526 morph_view=DestroyCacheView(morph_view);
3527 image_view=DestroyCacheView(image_view);
3528 morph_image=DestroyImage(morph_image);
3529 }
cristybb503372010-05-27 20:51:26 +00003530 if (i < (ssize_t) number_frames)
cristy3ed852e2009-09-05 21:47:34 +00003531 break;
3532 /*
3533 Clone last frame in sequence.
3534 */
3535 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3536 if (morph_image == (Image *) NULL)
3537 {
3538 morph_images=DestroyImageList(morph_images);
3539 return((Image *) NULL);
3540 }
3541 AppendImageToList(&morph_images,morph_image);
3542 morph_images=GetLastImageInList(morph_images);
3543 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3544 {
3545 MagickBooleanType
3546 proceed;
3547
cristyb5d5f722009-11-04 03:03:49 +00003548#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003549 #pragma omp critical (MagickCore_MorphImages)
3550#endif
3551 proceed=SetImageProgress(image,MorphImageTag,scene,
3552 GetImageListLength(image));
3553 if (proceed == MagickFalse)
3554 status=MagickFalse;
3555 }
3556 scene++;
3557 }
3558 if (GetNextImageInList(next) != (Image *) NULL)
3559 {
3560 morph_images=DestroyImageList(morph_images);
3561 return((Image *) NULL);
3562 }
3563 return(GetFirstImageInList(morph_images));
3564}
3565
3566/*
3567%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3568% %
3569% %
3570% %
3571% P l a s m a I m a g e %
3572% %
3573% %
3574% %
3575%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3576%
3577% PlasmaImage() initializes an image with plasma fractal values. The image
3578% must be initialized with a base color and the random number generator
3579% seeded before this method is called.
3580%
3581% The format of the PlasmaImage method is:
3582%
3583% MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
cristy5cbc0162011-08-29 00:36:28 +00003584% size_t attenuate,size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003585%
3586% A description of each parameter follows:
3587%
3588% o image: the image.
3589%
3590% o segment: Define the region to apply plasma fractals values.
3591%
glennrp7dae1ca2010-09-16 12:17:35 +00003592% o attenuate: Define the plasma attenuation factor.
cristy3ed852e2009-09-05 21:47:34 +00003593%
3594% o depth: Limit the plasma recursion depth.
3595%
cristy5cbc0162011-08-29 00:36:28 +00003596% o exception: return any errors or warnings in this structure.
3597%
cristy3ed852e2009-09-05 21:47:34 +00003598*/
3599
3600static inline Quantum PlasmaPixel(RandomInfo *random_info,
3601 const MagickRealType pixel,const MagickRealType noise)
3602{
3603 Quantum
3604 plasma;
3605
cristyce70c172010-01-07 17:15:30 +00003606 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
cristy3ed852e2009-09-05 21:47:34 +00003607 noise/2.0);
3608 return(plasma);
3609}
3610
cristyda1f9c12011-10-02 21:39:49 +00003611static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3612 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3613 const SegmentInfo *segment,size_t attenuate,size_t depth,
3614 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003615{
cristy3ed852e2009-09-05 21:47:34 +00003616 MagickRealType
3617 plasma;
3618
cristyda1f9c12011-10-02 21:39:49 +00003619 PixelChannel
3620 channel;
3621
3622 PixelTrait
3623 traits;
3624
3625 register const Quantum
3626 *restrict u,
3627 *restrict v;
3628
3629 register Quantum
3630 *restrict q;
3631
3632 register ssize_t
3633 i;
cristy3ed852e2009-09-05 21:47:34 +00003634
cristy9d314ff2011-03-09 01:30:28 +00003635 ssize_t
3636 x,
3637 x_mid,
3638 y,
3639 y_mid;
3640
cristy3ed852e2009-09-05 21:47:34 +00003641 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3642 return(MagickTrue);
3643 if (depth != 0)
3644 {
3645 SegmentInfo
3646 local_info;
3647
3648 /*
3649 Divide the area into quadrants and recurse.
3650 */
3651 depth--;
3652 attenuate++;
cristybb503372010-05-27 20:51:26 +00003653 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3654 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003655 local_info=(*segment);
3656 local_info.x2=(double) x_mid;
3657 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003658 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3659 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003660 local_info=(*segment);
3661 local_info.y1=(double) y_mid;
3662 local_info.x2=(double) x_mid;
cristyda1f9c12011-10-02 21:39:49 +00003663 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3664 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003665 local_info=(*segment);
3666 local_info.x1=(double) x_mid;
3667 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003668 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3669 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003670 local_info=(*segment);
3671 local_info.x1=(double) x_mid;
3672 local_info.y1=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003673 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3674 &local_info,attenuate,depth,exception));
cristy3ed852e2009-09-05 21:47:34 +00003675 }
cristybb503372010-05-27 20:51:26 +00003676 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3677 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003678 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3679 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3680 return(MagickFalse);
3681 /*
3682 Average pixels and apply plasma.
3683 */
cristy3ed852e2009-09-05 21:47:34 +00003684 plasma=(MagickRealType) QuantumRange/(2.0*attenuate);
3685 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3686 {
cristy3ed852e2009-09-05 21:47:34 +00003687 /*
3688 Left pixel.
3689 */
cristybb503372010-05-27 20:51:26 +00003690 x=(ssize_t) ceil(segment->x1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003691 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3692 1,1,exception);
3693 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3694 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003695 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003696 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3697 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003698 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003699 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3700 {
3701 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3702 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3703 if (traits == UndefinedPixelTrait)
3704 continue;
3705 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3706 }
cristyc5c6f662010-09-22 14:23:02 +00003707 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003708 if (segment->x1 != segment->x2)
3709 {
3710 /*
3711 Right pixel.
3712 */
cristybb503372010-05-27 20:51:26 +00003713 x=(ssize_t) ceil(segment->x2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003714 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3715 1,1,exception);
3716 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3717 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003718 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003719 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3720 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003721 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003722 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3723 {
3724 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3725 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3726 if (traits == UndefinedPixelTrait)
3727 continue;
3728 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3729 }
cristyc5c6f662010-09-22 14:23:02 +00003730 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003731 }
3732 }
3733 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3734 {
3735 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3736 {
cristy3ed852e2009-09-05 21:47:34 +00003737 /*
3738 Bottom pixel.
3739 */
cristybb503372010-05-27 20:51:26 +00003740 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003741 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3742 1,1,exception);
3743 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3744 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003745 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003746 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3747 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003748 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003749 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3750 {
3751 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3752 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3753 if (traits == UndefinedPixelTrait)
3754 continue;
3755 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3756 }
cristyc5c6f662010-09-22 14:23:02 +00003757 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003758 }
3759 if (segment->y1 != segment->y2)
3760 {
cristy3ed852e2009-09-05 21:47:34 +00003761 /*
3762 Top pixel.
3763 */
cristybb503372010-05-27 20:51:26 +00003764 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003765 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3766 1,1,exception);
3767 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3768 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003769 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003770 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3771 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003772 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003773 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3774 {
3775 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3776 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3777 if (traits == UndefinedPixelTrait)
3778 continue;
3779 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3780 }
cristyc5c6f662010-09-22 14:23:02 +00003781 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003782 }
3783 }
3784 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3785 {
cristy3ed852e2009-09-05 21:47:34 +00003786 /*
3787 Middle pixel.
3788 */
cristybb503372010-05-27 20:51:26 +00003789 x=(ssize_t) ceil(segment->x1-0.5);
3790 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003791 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
cristybb503372010-05-27 20:51:26 +00003792 x=(ssize_t) ceil(segment->x2-0.5);
3793 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003794 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003795 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003796 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3797 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003798 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003799 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3800 {
3801 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3802 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3803 if (traits == UndefinedPixelTrait)
3804 continue;
3805 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3806 }
cristyc5c6f662010-09-22 14:23:02 +00003807 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003808 }
3809 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3810 return(MagickTrue);
3811 return(MagickFalse);
3812}
cristyda1f9c12011-10-02 21:39:49 +00003813
cristy3ed852e2009-09-05 21:47:34 +00003814MagickExport MagickBooleanType PlasmaImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00003815 const SegmentInfo *segment,size_t attenuate,size_t depth,
3816 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003817{
cristyc5c6f662010-09-22 14:23:02 +00003818 CacheView
cristyda1f9c12011-10-02 21:39:49 +00003819 *image_view,
3820 *u_view,
3821 *v_view;
cristyc5c6f662010-09-22 14:23:02 +00003822
cristy3ed852e2009-09-05 21:47:34 +00003823 MagickBooleanType
3824 status;
3825
3826 RandomInfo
3827 *random_info;
3828
3829 if (image->debug != MagickFalse)
3830 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3831 assert(image != (Image *) NULL);
3832 assert(image->signature == MagickSignature);
3833 if (image->debug != MagickFalse)
3834 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy5cbc0162011-08-29 00:36:28 +00003835 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristyc5c6f662010-09-22 14:23:02 +00003836 return(MagickFalse);
3837 image_view=AcquireCacheView(image);
cristyda1f9c12011-10-02 21:39:49 +00003838 u_view=AcquireCacheView(image);
3839 v_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00003840 random_info=AcquireRandomInfo();
cristyda1f9c12011-10-02 21:39:49 +00003841 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3842 attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003843 random_info=DestroyRandomInfo(random_info);
cristyda1f9c12011-10-02 21:39:49 +00003844 v_view=DestroyCacheView(v_view);
3845 u_view=DestroyCacheView(u_view);
cristyc5c6f662010-09-22 14:23:02 +00003846 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003847 return(status);
3848}
3849
3850/*
3851%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3852% %
3853% %
3854% %
3855% P o l a r o i d I m a g e %
3856% %
3857% %
3858% %
3859%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3860%
3861% PolaroidImage() simulates a Polaroid picture.
3862%
3863% The format of the AnnotateImage method is:
3864%
3865% Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristy5c4e2582011-09-11 19:21:03 +00003866% const double angle,const PixelInterpolateMethod method,
3867% ExceptionInfo exception)
cristy3ed852e2009-09-05 21:47:34 +00003868%
3869% A description of each parameter follows:
3870%
3871% o image: the image.
3872%
3873% o draw_info: the draw info.
3874%
cristycee97112010-05-28 00:44:52 +00003875% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00003876%
cristy5c4e2582011-09-11 19:21:03 +00003877% o method: the pixel interpolation method.
3878%
cristy3ed852e2009-09-05 21:47:34 +00003879% o exception: return any errors or warnings in this structure.
3880%
3881*/
3882MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristy5c4e2582011-09-11 19:21:03 +00003883 const double angle,const PixelInterpolateMethod method,
3884 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003885{
3886 const char
3887 *value;
3888
cristy3ed852e2009-09-05 21:47:34 +00003889 Image
3890 *bend_image,
3891 *caption_image,
3892 *flop_image,
3893 *picture_image,
3894 *polaroid_image,
3895 *rotate_image,
3896 *trim_image;
3897
cristybb503372010-05-27 20:51:26 +00003898 size_t
cristy3ed852e2009-09-05 21:47:34 +00003899 height;
3900
cristy9d314ff2011-03-09 01:30:28 +00003901 ssize_t
3902 quantum;
3903
cristy3ed852e2009-09-05 21:47:34 +00003904 /*
3905 Simulate a Polaroid picture.
3906 */
3907 assert(image != (Image *) NULL);
3908 assert(image->signature == MagickSignature);
3909 if (image->debug != MagickFalse)
3910 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3911 assert(exception != (ExceptionInfo *) NULL);
3912 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00003913 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
cristy3ed852e2009-09-05 21:47:34 +00003914 image->rows)/25.0,10.0);
3915 height=image->rows+2*quantum;
3916 caption_image=(Image *) NULL;
cristyd15e6592011-10-15 00:13:06 +00003917 value=GetImageProperty(image,"caption",exception);
cristy3ed852e2009-09-05 21:47:34 +00003918 if (value != (const char *) NULL)
3919 {
3920 char
3921 *caption,
3922 geometry[MaxTextExtent];
3923
3924 DrawInfo
3925 *annotate_info;
3926
cristy3ed852e2009-09-05 21:47:34 +00003927 MagickBooleanType
3928 status;
3929
cristy9d314ff2011-03-09 01:30:28 +00003930 ssize_t
3931 count;
3932
cristy3ed852e2009-09-05 21:47:34 +00003933 TypeMetric
3934 metrics;
3935
3936 /*
3937 Generate caption image.
3938 */
3939 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3940 if (caption_image == (Image *) NULL)
3941 return((Image *) NULL);
3942 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
3943 caption=InterpretImageProperties((ImageInfo *) NULL,(Image *) image,
cristy018f07f2011-09-04 21:15:19 +00003944 value,exception);
cristy3ed852e2009-09-05 21:47:34 +00003945 (void) CloneString(&annotate_info->text,caption);
cristy6b1d05e2010-09-22 19:17:27 +00003946 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
cristy5cbc0162011-08-29 00:36:28 +00003947 &caption,exception);
cristybb503372010-05-27 20:51:26 +00003948 status=SetImageExtent(caption_image,image->columns,(size_t)
cristy63240882011-08-05 19:05:27 +00003949 ((count+1)*(metrics.ascent-metrics.descent)+0.5),exception);
cristy3ed852e2009-09-05 21:47:34 +00003950 if (status == MagickFalse)
3951 caption_image=DestroyImage(caption_image);
3952 else
3953 {
3954 caption_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003955 (void) SetImageBackgroundColor(caption_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003956 (void) CloneString(&annotate_info->text,caption);
cristyb51dff52011-05-19 16:55:47 +00003957 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
cristy3ed852e2009-09-05 21:47:34 +00003958 metrics.ascent);
3959 if (annotate_info->gravity == UndefinedGravity)
3960 (void) CloneString(&annotate_info->geometry,AcquireString(
3961 geometry));
cristy5cbc0162011-08-29 00:36:28 +00003962 (void) AnnotateImage(caption_image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003963 height+=caption_image->rows;
3964 }
3965 annotate_info=DestroyDrawInfo(annotate_info);
3966 caption=DestroyString(caption);
3967 }
3968 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
3969 exception);
3970 if (picture_image == (Image *) NULL)
3971 {
3972 if (caption_image != (Image *) NULL)
3973 caption_image=DestroyImage(caption_image);
3974 return((Image *) NULL);
3975 }
3976 picture_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003977 (void) SetImageBackgroundColor(picture_image,exception);
cristye941a752011-10-15 01:52:48 +00003978 (void) CompositeImage(picture_image,OverCompositeOp,image,quantum,quantum,
3979 exception);
cristy3ed852e2009-09-05 21:47:34 +00003980 if (caption_image != (Image *) NULL)
3981 {
3982 (void) CompositeImage(picture_image,OverCompositeOp,caption_image,
cristye941a752011-10-15 01:52:48 +00003983 quantum,(ssize_t) (image->rows+3*quantum/2),exception);
cristy3ed852e2009-09-05 21:47:34 +00003984 caption_image=DestroyImage(caption_image);
3985 }
cristy9950d572011-10-01 18:22:35 +00003986 (void) QueryColorCompliance("none",AllCompliance,
3987 &picture_image->background_color,exception);
cristy63240882011-08-05 19:05:27 +00003988 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00003989 rotate_image=RotateImage(picture_image,90.0,exception);
3990 picture_image=DestroyImage(picture_image);
3991 if (rotate_image == (Image *) NULL)
3992 return((Image *) NULL);
3993 picture_image=rotate_image;
3994 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
cristy5c4e2582011-09-11 19:21:03 +00003995 picture_image->columns,method,exception);
cristy3ed852e2009-09-05 21:47:34 +00003996 picture_image=DestroyImage(picture_image);
3997 if (bend_image == (Image *) NULL)
3998 return((Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00003999 picture_image=bend_image;
4000 rotate_image=RotateImage(picture_image,-90.0,exception);
4001 picture_image=DestroyImage(picture_image);
4002 if (rotate_image == (Image *) NULL)
4003 return((Image *) NULL);
4004 picture_image=rotate_image;
4005 picture_image->background_color=image->background_color;
4006 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
4007 exception);
4008 if (polaroid_image == (Image *) NULL)
4009 {
4010 picture_image=DestroyImage(picture_image);
4011 return(picture_image);
4012 }
4013 flop_image=FlopImage(polaroid_image,exception);
4014 polaroid_image=DestroyImage(polaroid_image);
4015 if (flop_image == (Image *) NULL)
4016 {
4017 picture_image=DestroyImage(picture_image);
4018 return(picture_image);
4019 }
4020 polaroid_image=flop_image;
4021 (void) CompositeImage(polaroid_image,OverCompositeOp,picture_image,
cristye941a752011-10-15 01:52:48 +00004022 (ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
cristy3ed852e2009-09-05 21:47:34 +00004023 picture_image=DestroyImage(picture_image);
cristy9950d572011-10-01 18:22:35 +00004024 (void) QueryColorCompliance("none",AllCompliance,
4025 &polaroid_image->background_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00004026 rotate_image=RotateImage(polaroid_image,angle,exception);
4027 polaroid_image=DestroyImage(polaroid_image);
4028 if (rotate_image == (Image *) NULL)
4029 return((Image *) NULL);
4030 polaroid_image=rotate_image;
4031 trim_image=TrimImage(polaroid_image,exception);
4032 polaroid_image=DestroyImage(polaroid_image);
4033 if (trim_image == (Image *) NULL)
4034 return((Image *) NULL);
4035 polaroid_image=trim_image;
4036 return(polaroid_image);
4037}
4038
4039/*
4040%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4041% %
4042% %
4043% %
cristy3ed852e2009-09-05 21:47:34 +00004044% S e p i a T o n e I m a g e %
4045% %
4046% %
4047% %
4048%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4049%
4050% MagickSepiaToneImage() applies a special effect to the image, similar to the
4051% effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4052% 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4053% threshold of 80% is a good starting point for a reasonable tone.
4054%
4055% The format of the SepiaToneImage method is:
4056%
4057% Image *SepiaToneImage(const Image *image,const double threshold,
4058% ExceptionInfo *exception)
4059%
4060% A description of each parameter follows:
4061%
4062% o image: the image.
4063%
4064% o threshold: the tone threshold.
4065%
4066% o exception: return any errors or warnings in this structure.
4067%
4068*/
4069MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4070 ExceptionInfo *exception)
4071{
4072#define SepiaToneImageTag "SepiaTone/Image"
4073
cristyc4c8d132010-01-07 01:58:38 +00004074 CacheView
4075 *image_view,
4076 *sepia_view;
4077
cristy3ed852e2009-09-05 21:47:34 +00004078 Image
4079 *sepia_image;
4080
cristy3ed852e2009-09-05 21:47:34 +00004081 MagickBooleanType
4082 status;
4083
cristybb503372010-05-27 20:51:26 +00004084 MagickOffsetType
4085 progress;
4086
4087 ssize_t
4088 y;
4089
cristy3ed852e2009-09-05 21:47:34 +00004090 /*
4091 Initialize sepia-toned image attributes.
4092 */
4093 assert(image != (const Image *) NULL);
4094 assert(image->signature == MagickSignature);
4095 if (image->debug != MagickFalse)
4096 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4097 assert(exception != (ExceptionInfo *) NULL);
4098 assert(exception->signature == MagickSignature);
4099 sepia_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
4100 if (sepia_image == (Image *) NULL)
4101 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004102 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004103 {
cristy3ed852e2009-09-05 21:47:34 +00004104 sepia_image=DestroyImage(sepia_image);
4105 return((Image *) NULL);
4106 }
4107 /*
4108 Tone each row of the image.
4109 */
4110 status=MagickTrue;
4111 progress=0;
4112 image_view=AcquireCacheView(image);
4113 sepia_view=AcquireCacheView(sepia_image);
cristyb5d5f722009-11-04 03:03:49 +00004114#if defined(MAGICKCORE_OPENMP_SUPPORT)
4115 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004116#endif
cristybb503372010-05-27 20:51:26 +00004117 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004118 {
cristy4c08aed2011-07-01 19:47:50 +00004119 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004120 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004121
cristybb503372010-05-27 20:51:26 +00004122 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004123 x;
4124
cristy4c08aed2011-07-01 19:47:50 +00004125 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004126 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004127
4128 if (status == MagickFalse)
4129 continue;
4130 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
4131 q=QueueCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
4132 exception);
cristy4c08aed2011-07-01 19:47:50 +00004133 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004134 {
4135 status=MagickFalse;
4136 continue;
4137 }
cristybb503372010-05-27 20:51:26 +00004138 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004139 {
4140 MagickRealType
4141 intensity,
4142 tone;
4143
cristy4c08aed2011-07-01 19:47:50 +00004144 intensity=(MagickRealType) GetPixelIntensity(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004145 tone=intensity > threshold ? (MagickRealType) QuantumRange : intensity+
4146 (MagickRealType) QuantumRange-threshold;
cristy4c08aed2011-07-01 19:47:50 +00004147 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004148 tone=intensity > (7.0*threshold/6.0) ? (MagickRealType) QuantumRange :
4149 intensity+(MagickRealType) QuantumRange-7.0*threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004150 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004151 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004152 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004153 tone=threshold/7.0;
cristy4c08aed2011-07-01 19:47:50 +00004154 if ((MagickRealType) GetPixelGreen(image,q) < tone)
4155 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4156 if ((MagickRealType) GetPixelBlue(image,q) < tone)
4157 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristyed231572011-07-14 02:18:59 +00004158 p+=GetPixelChannels(image);
4159 q+=GetPixelChannels(sepia_image);
cristy3ed852e2009-09-05 21:47:34 +00004160 }
4161 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4162 status=MagickFalse;
4163 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4164 {
4165 MagickBooleanType
4166 proceed;
4167
cristyb5d5f722009-11-04 03:03:49 +00004168#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00004169 #pragma omp critical (MagickCore_SepiaToneImage)
4170#endif
4171 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4172 image->rows);
4173 if (proceed == MagickFalse)
4174 status=MagickFalse;
4175 }
4176 }
4177 sepia_view=DestroyCacheView(sepia_view);
4178 image_view=DestroyCacheView(image_view);
cristye23ec9d2011-08-16 18:15:40 +00004179 (void) NormalizeImage(sepia_image,exception);
4180 (void) ContrastImage(sepia_image,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004181 if (status == MagickFalse)
4182 sepia_image=DestroyImage(sepia_image);
4183 return(sepia_image);
4184}
4185
4186/*
4187%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4188% %
4189% %
4190% %
4191% S h a d o w I m a g e %
4192% %
4193% %
4194% %
4195%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4196%
4197% ShadowImage() simulates a shadow from the specified image and returns it.
4198%
4199% The format of the ShadowImage method is:
4200%
4201% Image *ShadowImage(const Image *image,const double opacity,
cristybb503372010-05-27 20:51:26 +00004202% const double sigma,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004203% ExceptionInfo *exception)
4204%
4205% A description of each parameter follows:
4206%
4207% o image: the image.
4208%
4209% o opacity: percentage transparency.
4210%
4211% o sigma: the standard deviation of the Gaussian, in pixels.
4212%
4213% o x_offset: the shadow x-offset.
4214%
4215% o y_offset: the shadow y-offset.
4216%
4217% o exception: return any errors or warnings in this structure.
4218%
4219*/
4220MagickExport Image *ShadowImage(const Image *image,const double opacity,
cristybb503372010-05-27 20:51:26 +00004221 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004222 ExceptionInfo *exception)
4223{
4224#define ShadowImageTag "Shadow/Image"
4225
cristybd5a96c2011-08-21 00:04:26 +00004226 ChannelType
4227 channel_mask;
4228
cristy3ed852e2009-09-05 21:47:34 +00004229 Image
4230 *border_image,
4231 *clone_image,
4232 *shadow_image;
4233
cristy3ed852e2009-09-05 21:47:34 +00004234 RectangleInfo
4235 border_info;
4236
cristy3ed852e2009-09-05 21:47:34 +00004237 assert(image != (Image *) NULL);
4238 assert(image->signature == MagickSignature);
4239 if (image->debug != MagickFalse)
4240 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4241 assert(exception != (ExceptionInfo *) NULL);
4242 assert(exception->signature == MagickSignature);
4243 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4244 if (clone_image == (Image *) NULL)
4245 return((Image *) NULL);
4246 (void) SetImageVirtualPixelMethod(clone_image,EdgeVirtualPixelMethod);
4247 clone_image->compose=OverCompositeOp;
cristybb503372010-05-27 20:51:26 +00004248 border_info.width=(size_t) floor(2.0*sigma+0.5);
4249 border_info.height=(size_t) floor(2.0*sigma+0.5);
cristy3ed852e2009-09-05 21:47:34 +00004250 border_info.x=0;
4251 border_info.y=0;
cristy9950d572011-10-01 18:22:35 +00004252 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4253 exception);
cristy633f0c62011-09-15 13:27:36 +00004254 border_image=BorderImage(clone_image,&border_info,image->compose,exception);
cristy3ed852e2009-09-05 21:47:34 +00004255 clone_image=DestroyImage(clone_image);
4256 if (border_image == (Image *) NULL)
4257 return((Image *) NULL);
4258 if (border_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00004259 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004260 /*
4261 Shadow image.
4262 */
cristyea1a8aa2011-10-20 13:24:06 +00004263 (void) SetImageBackgroundColor(border_image,exception);
cristybd5a96c2011-08-21 00:04:26 +00004264 channel_mask=SetPixelChannelMask(border_image,AlphaChannel);
cristy05c0c9a2011-09-05 23:16:13 +00004265 shadow_image=BlurImage(border_image,0.0,sigma,image->bias,exception);
cristybd5a96c2011-08-21 00:04:26 +00004266 (void) SetPixelChannelMap(border_image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00004267 border_image=DestroyImage(border_image);
4268 if (shadow_image == (Image *) NULL)
4269 return((Image *) NULL);
4270 if (shadow_image->page.width == 0)
4271 shadow_image->page.width=shadow_image->columns;
4272 if (shadow_image->page.height == 0)
4273 shadow_image->page.height=shadow_image->rows;
cristybb503372010-05-27 20:51:26 +00004274 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4275 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4276 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4277 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
cristy3ed852e2009-09-05 21:47:34 +00004278 return(shadow_image);
4279}
4280
4281/*
4282%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4283% %
4284% %
4285% %
4286% S k e t c h I m a g e %
4287% %
4288% %
4289% %
4290%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4291%
4292% SketchImage() simulates a pencil sketch. We convolve the image with a
4293% Gaussian operator of the given radius and standard deviation (sigma). For
4294% reasonable results, radius should be larger than sigma. Use a radius of 0
4295% and SketchImage() selects a suitable radius for you. Angle gives the angle
4296% of the sketch.
4297%
4298% The format of the SketchImage method is:
4299%
4300% Image *SketchImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00004301% const double sigma,const double angle,const double bias,
4302% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004303%
4304% A description of each parameter follows:
4305%
4306% o image: the image.
4307%
cristy574cc262011-08-05 01:23:58 +00004308% o radius: the radius of the Gaussian, in pixels, not counting the
4309% center pixel.
cristy3ed852e2009-09-05 21:47:34 +00004310%
4311% o sigma: the standard deviation of the Gaussian, in pixels.
4312%
cristyf7ef0252011-09-09 14:50:06 +00004313% o angle: apply the effect along this angle.
4314%
4315% o bias: the bias.
cristy3ed852e2009-09-05 21:47:34 +00004316%
4317% o exception: return any errors or warnings in this structure.
4318%
4319*/
4320MagickExport Image *SketchImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00004321 const double sigma,const double angle,const double bias,
4322 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004323{
cristyfa112112010-01-04 17:48:07 +00004324 CacheView
4325 *random_view;
4326
cristy3ed852e2009-09-05 21:47:34 +00004327 Image
4328 *blend_image,
4329 *blur_image,
4330 *dodge_image,
4331 *random_image,
4332 *sketch_image;
4333
cristy3ed852e2009-09-05 21:47:34 +00004334 MagickBooleanType
4335 status;
4336
cristy3ed852e2009-09-05 21:47:34 +00004337 RandomInfo
cristyfa112112010-01-04 17:48:07 +00004338 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00004339
cristy9d314ff2011-03-09 01:30:28 +00004340 ssize_t
4341 y;
4342
cristy3ed852e2009-09-05 21:47:34 +00004343 /*
4344 Sketch image.
4345 */
4346 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4347 MagickTrue,exception);
4348 if (random_image == (Image *) NULL)
4349 return((Image *) NULL);
4350 status=MagickTrue;
cristy1b784432009-12-19 02:20:40 +00004351 random_info=AcquireRandomInfoThreadSet();
cristy3ed852e2009-09-05 21:47:34 +00004352 random_view=AcquireCacheView(random_image);
cristy1b784432009-12-19 02:20:40 +00004353#if defined(MAGICKCORE_OPENMP_SUPPORT)
4354 #pragma omp parallel for schedule(dynamic,4) shared(status)
4355#endif
cristybb503372010-05-27 20:51:26 +00004356 for (y=0; y < (ssize_t) random_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004357 {
cristy5c9e6f22010-09-17 17:31:01 +00004358 const int
4359 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00004360
cristybb503372010-05-27 20:51:26 +00004361 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004362 x;
4363
cristy4c08aed2011-07-01 19:47:50 +00004364 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004365 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004366
cristy1b784432009-12-19 02:20:40 +00004367 if (status == MagickFalse)
4368 continue;
cristy3ed852e2009-09-05 21:47:34 +00004369 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4370 exception);
cristyacd2ed22011-08-30 01:44:23 +00004371 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004372 {
4373 status=MagickFalse;
4374 continue;
4375 }
cristybb503372010-05-27 20:51:26 +00004376 for (x=0; x < (ssize_t) random_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004377 {
cristy76f512e2011-09-12 01:26:56 +00004378 MagickRealType
4379 value;
4380
4381 register ssize_t
4382 i;
4383
4384 value=GetPseudoRandomValue(random_info[id]);
4385 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4386 {
4387 PixelTrait
4388 traits;
4389
4390 traits=GetPixelChannelMapTraits(random_image,(PixelChannel) i);
4391 if (traits == UndefinedPixelTrait)
4392 continue;
4393 q[i]=ClampToQuantum(QuantumRange*value);
4394 }
cristyed231572011-07-14 02:18:59 +00004395 q+=GetPixelChannels(random_image);
cristy3ed852e2009-09-05 21:47:34 +00004396 }
4397 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4398 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004399 }
4400 random_view=DestroyCacheView(random_view);
cristy1b784432009-12-19 02:20:40 +00004401 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00004402 if (status == MagickFalse)
4403 {
4404 random_image=DestroyImage(random_image);
4405 return(random_image);
4406 }
cristyf7ef0252011-09-09 14:50:06 +00004407 blur_image=MotionBlurImage(random_image,radius,sigma,angle,bias,exception);
cristy3ed852e2009-09-05 21:47:34 +00004408 random_image=DestroyImage(random_image);
4409 if (blur_image == (Image *) NULL)
4410 return((Image *) NULL);
cristy8ae632d2011-09-05 17:29:53 +00004411 dodge_image=EdgeImage(blur_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00004412 blur_image=DestroyImage(blur_image);
4413 if (dodge_image == (Image *) NULL)
4414 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +00004415 (void) NormalizeImage(dodge_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +00004416 (void) NegateImage(dodge_image,MagickFalse,exception);
cristye941a752011-10-15 01:52:48 +00004417 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
cristy3ed852e2009-09-05 21:47:34 +00004418 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4419 if (sketch_image == (Image *) NULL)
4420 {
4421 dodge_image=DestroyImage(dodge_image);
4422 return((Image *) NULL);
4423 }
cristye941a752011-10-15 01:52:48 +00004424 (void) CompositeImage(sketch_image,ColorDodgeCompositeOp,dodge_image,0,0,
4425 exception);
cristy3ed852e2009-09-05 21:47:34 +00004426 dodge_image=DestroyImage(dodge_image);
4427 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4428 if (blend_image == (Image *) NULL)
4429 {
4430 sketch_image=DestroyImage(sketch_image);
4431 return((Image *) NULL);
4432 }
4433 (void) SetImageArtifact(blend_image,"compose:args","20x80");
cristye941a752011-10-15 01:52:48 +00004434 (void) CompositeImage(sketch_image,BlendCompositeOp,blend_image,0,0,
4435 exception);
cristy3ed852e2009-09-05 21:47:34 +00004436 blend_image=DestroyImage(blend_image);
4437 return(sketch_image);
4438}
4439
4440/*
4441%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4442% %
4443% %
4444% %
4445% S o l a r i z e I m a g e %
4446% %
4447% %
4448% %
4449%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4450%
4451% SolarizeImage() applies a special effect to the image, similar to the effect
4452% achieved in a photo darkroom by selectively exposing areas of photo
4453% sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4454% measure of the extent of the solarization.
4455%
4456% The format of the SolarizeImage method is:
4457%
cristy5cbc0162011-08-29 00:36:28 +00004458% MagickBooleanType SolarizeImage(Image *image,const double threshold,
4459% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004460%
4461% A description of each parameter follows:
4462%
4463% o image: the image.
4464%
4465% o threshold: Define the extent of the solarization.
4466%
cristy5cbc0162011-08-29 00:36:28 +00004467% o exception: return any errors or warnings in this structure.
4468%
cristy3ed852e2009-09-05 21:47:34 +00004469*/
4470MagickExport MagickBooleanType SolarizeImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00004471 const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004472{
4473#define SolarizeImageTag "Solarize/Image"
4474
cristyc4c8d132010-01-07 01:58:38 +00004475 CacheView
4476 *image_view;
4477
cristy3ed852e2009-09-05 21:47:34 +00004478 MagickBooleanType
4479 status;
4480
cristybb503372010-05-27 20:51:26 +00004481 MagickOffsetType
4482 progress;
4483
4484 ssize_t
4485 y;
4486
cristy3ed852e2009-09-05 21:47:34 +00004487 assert(image != (Image *) NULL);
4488 assert(image->signature == MagickSignature);
4489 if (image->debug != MagickFalse)
4490 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4491 if (image->storage_class == PseudoClass)
4492 {
cristybb503372010-05-27 20:51:26 +00004493 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004494 i;
4495
4496 /*
4497 Solarize colormap.
4498 */
cristybb503372010-05-27 20:51:26 +00004499 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00004500 {
4501 if ((MagickRealType) image->colormap[i].red > threshold)
4502 image->colormap[i].red=(Quantum) QuantumRange-image->colormap[i].red;
4503 if ((MagickRealType) image->colormap[i].green > threshold)
4504 image->colormap[i].green=(Quantum) QuantumRange-
4505 image->colormap[i].green;
4506 if ((MagickRealType) image->colormap[i].blue > threshold)
4507 image->colormap[i].blue=(Quantum) QuantumRange-
4508 image->colormap[i].blue;
4509 }
4510 }
4511 /*
4512 Solarize image.
4513 */
4514 status=MagickTrue;
4515 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00004516 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00004517#if defined(MAGICKCORE_OPENMP_SUPPORT)
4518 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004519#endif
cristybb503372010-05-27 20:51:26 +00004520 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004521 {
cristybb503372010-05-27 20:51:26 +00004522 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004523 x;
4524
cristy4c08aed2011-07-01 19:47:50 +00004525 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004526 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004527
4528 if (status == MagickFalse)
4529 continue;
cristy5cbc0162011-08-29 00:36:28 +00004530 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00004531 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004532 {
4533 status=MagickFalse;
4534 continue;
4535 }
cristybb503372010-05-27 20:51:26 +00004536 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004537 {
cristy76f512e2011-09-12 01:26:56 +00004538 register ssize_t
4539 i;
4540
4541 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4542 {
4543 PixelTrait
4544 traits;
4545
4546 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
4547 if ((traits == UndefinedPixelTrait) ||
4548 ((traits & CopyPixelTrait) != 0))
4549 continue;
4550 if ((MagickRealType) q[i] > threshold)
4551 q[i]=QuantumRange-q[i];
4552 }
cristyed231572011-07-14 02:18:59 +00004553 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004554 }
4555 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4556 status=MagickFalse;
4557 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4558 {
4559 MagickBooleanType
4560 proceed;
4561
cristyb5d5f722009-11-04 03:03:49 +00004562#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00004563 #pragma omp critical (MagickCore_SolarizeImage)
4564#endif
4565 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4566 if (proceed == MagickFalse)
4567 status=MagickFalse;
4568 }
4569 }
4570 image_view=DestroyCacheView(image_view);
4571 return(status);
4572}
4573
4574/*
4575%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4576% %
4577% %
4578% %
4579% S t e g a n o I m a g e %
4580% %
4581% %
4582% %
4583%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4584%
4585% SteganoImage() hides a digital watermark within the image. Recover
4586% the hidden watermark later to prove that the authenticity of an image.
4587% Offset defines the start position within the image to hide the watermark.
4588%
4589% The format of the SteganoImage method is:
4590%
4591% Image *SteganoImage(const Image *image,Image *watermark,
4592% ExceptionInfo *exception)
4593%
4594% A description of each parameter follows:
4595%
4596% o image: the image.
4597%
4598% o watermark: the watermark image.
4599%
4600% o exception: return any errors or warnings in this structure.
4601%
4602*/
4603MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4604 ExceptionInfo *exception)
4605{
cristye1bf8ad2010-09-19 17:07:03 +00004606#define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
cristy4c08aed2011-07-01 19:47:50 +00004607#define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
cristyeaedf062010-05-29 22:36:02 +00004608 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
cristy3ed852e2009-09-05 21:47:34 +00004609#define SteganoImageTag "Stegano/Image"
4610
cristyb0d3bb92010-09-22 14:37:58 +00004611 CacheView
4612 *stegano_view,
4613 *watermark_view;
4614
cristy3ed852e2009-09-05 21:47:34 +00004615 Image
4616 *stegano_image;
4617
4618 int
4619 c;
4620
cristy3ed852e2009-09-05 21:47:34 +00004621 MagickBooleanType
4622 status;
4623
cristy101ab702011-10-13 13:06:32 +00004624 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004625 pixel;
4626
cristy4c08aed2011-07-01 19:47:50 +00004627 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004628 *q;
4629
cristye1bf8ad2010-09-19 17:07:03 +00004630 register ssize_t
4631 x;
4632
cristybb503372010-05-27 20:51:26 +00004633 size_t
cristyeaedf062010-05-29 22:36:02 +00004634 depth,
4635 one;
cristy3ed852e2009-09-05 21:47:34 +00004636
cristye1bf8ad2010-09-19 17:07:03 +00004637 ssize_t
4638 i,
4639 j,
4640 k,
4641 y;
4642
cristy3ed852e2009-09-05 21:47:34 +00004643 /*
4644 Initialize steganographic image attributes.
4645 */
4646 assert(image != (const Image *) NULL);
4647 assert(image->signature == MagickSignature);
4648 if (image->debug != MagickFalse)
4649 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4650 assert(watermark != (const Image *) NULL);
4651 assert(watermark->signature == MagickSignature);
4652 assert(exception != (ExceptionInfo *) NULL);
4653 assert(exception->signature == MagickSignature);
cristyeaedf062010-05-29 22:36:02 +00004654 one=1UL;
cristy3ed852e2009-09-05 21:47:34 +00004655 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4656 if (stegano_image == (Image *) NULL)
4657 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004658 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004659 {
cristy3ed852e2009-09-05 21:47:34 +00004660 stegano_image=DestroyImage(stegano_image);
4661 return((Image *) NULL);
4662 }
4663 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
4664 /*
4665 Hide watermark in low-order bits of image.
4666 */
4667 c=0;
4668 i=0;
4669 j=0;
4670 depth=stegano_image->depth;
4671 k=image->offset;
cristyda16f162011-02-19 23:52:17 +00004672 status=MagickTrue;
cristyb0d3bb92010-09-22 14:37:58 +00004673 watermark_view=AcquireCacheView(watermark);
4674 stegano_view=AcquireCacheView(stegano_image);
cristybb503372010-05-27 20:51:26 +00004675 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
cristy3ed852e2009-09-05 21:47:34 +00004676 {
cristybb503372010-05-27 20:51:26 +00004677 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
cristy3ed852e2009-09-05 21:47:34 +00004678 {
cristybb503372010-05-27 20:51:26 +00004679 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
cristy3ed852e2009-09-05 21:47:34 +00004680 {
cristyda1f9c12011-10-02 21:39:49 +00004681 Quantum
cristy5f95f4f2011-10-23 01:01:01 +00004682 virtual_pixel[CompositePixelChannel];
cristyda1f9c12011-10-02 21:39:49 +00004683
4684 (void) GetOneCacheViewVirtualPixel(watermark_view,x,y,virtual_pixel,
4685 exception);
4686 pixel.red=(double) virtual_pixel[RedPixelChannel];
4687 pixel.green=(double) virtual_pixel[GreenPixelChannel];
4688 pixel.blue=(double) virtual_pixel[BluePixelChannel];
4689 pixel.alpha=(double) virtual_pixel[AlphaPixelChannel];
cristybb503372010-05-27 20:51:26 +00004690 if ((k/(ssize_t) stegano_image->columns) >= (ssize_t) stegano_image->rows)
cristy3ed852e2009-09-05 21:47:34 +00004691 break;
cristyb0d3bb92010-09-22 14:37:58 +00004692 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4693 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4694 exception);
cristyacd2ed22011-08-30 01:44:23 +00004695 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004696 break;
4697 switch (c)
4698 {
4699 case 0:
4700 {
cristy4c08aed2011-07-01 19:47:50 +00004701 SetPixelRed(image,SetBit(GetPixelRed(image,q),j,GetBit(
cristy101ab702011-10-13 13:06:32 +00004702 GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004703 break;
4704 }
4705 case 1:
4706 {
cristy4c08aed2011-07-01 19:47:50 +00004707 SetPixelGreen(image,SetBit(GetPixelGreen(image,q),j,GetBit(
cristy101ab702011-10-13 13:06:32 +00004708 GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004709 break;
4710 }
4711 case 2:
4712 {
cristy4c08aed2011-07-01 19:47:50 +00004713 SetPixelBlue(image,SetBit(GetPixelBlue(image,q),j,GetBit(
cristy101ab702011-10-13 13:06:32 +00004714 GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004715 break;
4716 }
4717 }
cristyb0d3bb92010-09-22 14:37:58 +00004718 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004719 break;
4720 c++;
4721 if (c == 3)
4722 c=0;
4723 k++;
cristybb503372010-05-27 20:51:26 +00004724 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
cristy3ed852e2009-09-05 21:47:34 +00004725 k=0;
4726 if (k == image->offset)
4727 j++;
4728 }
4729 }
cristy8b27a6d2010-02-14 03:31:15 +00004730 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004731 {
cristy8b27a6d2010-02-14 03:31:15 +00004732 MagickBooleanType
4733 proceed;
4734
4735 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4736 (depth-i),depth);
4737 if (proceed == MagickFalse)
4738 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004739 }
4740 }
cristyb0d3bb92010-09-22 14:37:58 +00004741 stegano_view=DestroyCacheView(stegano_view);
4742 watermark_view=DestroyCacheView(watermark_view);
cristy3ed852e2009-09-05 21:47:34 +00004743 if (stegano_image->storage_class == PseudoClass)
cristyea1a8aa2011-10-20 13:24:06 +00004744 (void) SyncImage(stegano_image,exception);
cristyda16f162011-02-19 23:52:17 +00004745 if (status == MagickFalse)
4746 {
4747 stegano_image=DestroyImage(stegano_image);
4748 return((Image *) NULL);
4749 }
cristy3ed852e2009-09-05 21:47:34 +00004750 return(stegano_image);
4751}
4752
4753/*
4754%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4755% %
4756% %
4757% %
4758% S t e r e o A n a g l y p h I m a g e %
4759% %
4760% %
4761% %
4762%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4763%
4764% StereoAnaglyphImage() combines two images and produces a single image that
4765% is the composite of a left and right image of a stereo pair. Special
4766% red-green stereo glasses are required to view this effect.
4767%
4768% The format of the StereoAnaglyphImage method is:
4769%
4770% Image *StereoImage(const Image *left_image,const Image *right_image,
4771% ExceptionInfo *exception)
4772% Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004773% const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004774% ExceptionInfo *exception)
4775%
4776% A description of each parameter follows:
4777%
4778% o left_image: the left image.
4779%
4780% o right_image: the right image.
4781%
4782% o exception: return any errors or warnings in this structure.
4783%
4784% o x_offset: amount, in pixels, by which the left image is offset to the
4785% right of the right image.
4786%
4787% o y_offset: amount, in pixels, by which the left image is offset to the
4788% bottom of the right image.
4789%
4790%
4791*/
4792MagickExport Image *StereoImage(const Image *left_image,
4793 const Image *right_image,ExceptionInfo *exception)
4794{
4795 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4796}
4797
4798MagickExport Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004799 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004800 ExceptionInfo *exception)
4801{
4802#define StereoImageTag "Stereo/Image"
4803
4804 const Image
4805 *image;
4806
4807 Image
4808 *stereo_image;
4809
cristy3ed852e2009-09-05 21:47:34 +00004810 MagickBooleanType
4811 status;
4812
cristy9d314ff2011-03-09 01:30:28 +00004813 ssize_t
4814 y;
4815
cristy3ed852e2009-09-05 21:47:34 +00004816 assert(left_image != (const Image *) NULL);
4817 assert(left_image->signature == MagickSignature);
4818 if (left_image->debug != MagickFalse)
4819 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4820 left_image->filename);
4821 assert(right_image != (const Image *) NULL);
4822 assert(right_image->signature == MagickSignature);
4823 assert(exception != (ExceptionInfo *) NULL);
4824 assert(exception->signature == MagickSignature);
4825 assert(right_image != (const Image *) NULL);
4826 image=left_image;
4827 if ((left_image->columns != right_image->columns) ||
4828 (left_image->rows != right_image->rows))
4829 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4830 /*
4831 Initialize stereo image attributes.
4832 */
4833 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4834 MagickTrue,exception);
4835 if (stereo_image == (Image *) NULL)
4836 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004837 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004838 {
cristy3ed852e2009-09-05 21:47:34 +00004839 stereo_image=DestroyImage(stereo_image);
4840 return((Image *) NULL);
4841 }
4842 /*
4843 Copy left image to red channel and right image to blue channel.
4844 */
cristyda16f162011-02-19 23:52:17 +00004845 status=MagickTrue;
cristybb503372010-05-27 20:51:26 +00004846 for (y=0; y < (ssize_t) stereo_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004847 {
cristy4c08aed2011-07-01 19:47:50 +00004848 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004849 *restrict p,
4850 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004851
cristybb503372010-05-27 20:51:26 +00004852 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004853 x;
4854
cristy4c08aed2011-07-01 19:47:50 +00004855 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004856 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +00004857
4858 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4859 exception);
4860 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4861 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
cristy76f512e2011-09-12 01:26:56 +00004862 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4863 (r == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004864 break;
cristybb503372010-05-27 20:51:26 +00004865 for (x=0; x < (ssize_t) stereo_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004866 {
cristy4c08aed2011-07-01 19:47:50 +00004867 SetPixelRed(image,GetPixelRed(left_image,p),r);
cristy76f512e2011-09-12 01:26:56 +00004868 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4869 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4870 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4871 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4872 GetPixelAlpha(right_image,q))/2,r);
cristyed231572011-07-14 02:18:59 +00004873 p+=GetPixelChannels(left_image);
cristy76f512e2011-09-12 01:26:56 +00004874 q+=GetPixelChannels(right_image);
4875 r+=GetPixelChannels(stereo_image);
cristy3ed852e2009-09-05 21:47:34 +00004876 }
4877 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4878 break;
cristy8b27a6d2010-02-14 03:31:15 +00004879 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004880 {
cristy8b27a6d2010-02-14 03:31:15 +00004881 MagickBooleanType
4882 proceed;
4883
cristybb503372010-05-27 20:51:26 +00004884 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4885 stereo_image->rows);
cristy8b27a6d2010-02-14 03:31:15 +00004886 if (proceed == MagickFalse)
4887 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004888 }
4889 }
cristyda16f162011-02-19 23:52:17 +00004890 if (status == MagickFalse)
4891 {
4892 stereo_image=DestroyImage(stereo_image);
4893 return((Image *) NULL);
4894 }
cristy3ed852e2009-09-05 21:47:34 +00004895 return(stereo_image);
4896}
4897
4898/*
4899%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4900% %
4901% %
4902% %
4903% S w i r l I m a g e %
4904% %
4905% %
4906% %
4907%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4908%
4909% SwirlImage() swirls the pixels about the center of the image, where
4910% degrees indicates the sweep of the arc through which each pixel is moved.
4911% You get a more dramatic effect as the degrees move from 1 to 360.
4912%
4913% The format of the SwirlImage method is:
4914%
4915% Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00004916% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004917%
4918% A description of each parameter follows:
4919%
4920% o image: the image.
4921%
4922% o degrees: Define the tightness of the swirling effect.
4923%
cristy76f512e2011-09-12 01:26:56 +00004924% o method: the pixel interpolation method.
4925%
cristy3ed852e2009-09-05 21:47:34 +00004926% o exception: return any errors or warnings in this structure.
4927%
4928*/
4929MagickExport Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00004930 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004931{
4932#define SwirlImageTag "Swirl/Image"
4933
cristyfa112112010-01-04 17:48:07 +00004934 CacheView
4935 *image_view,
4936 *swirl_view;
4937
cristy3ed852e2009-09-05 21:47:34 +00004938 Image
4939 *swirl_image;
4940
cristy3ed852e2009-09-05 21:47:34 +00004941 MagickBooleanType
4942 status;
4943
cristybb503372010-05-27 20:51:26 +00004944 MagickOffsetType
4945 progress;
4946
cristy3ed852e2009-09-05 21:47:34 +00004947 MagickRealType
4948 radius;
4949
4950 PointInfo
4951 center,
4952 scale;
4953
cristybb503372010-05-27 20:51:26 +00004954 ssize_t
4955 y;
4956
cristy3ed852e2009-09-05 21:47:34 +00004957 /*
4958 Initialize swirl image attributes.
4959 */
4960 assert(image != (const Image *) NULL);
4961 assert(image->signature == MagickSignature);
4962 if (image->debug != MagickFalse)
4963 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4964 assert(exception != (ExceptionInfo *) NULL);
4965 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00004966 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004967 if (swirl_image == (Image *) NULL)
4968 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004969 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004970 {
cristy3ed852e2009-09-05 21:47:34 +00004971 swirl_image=DestroyImage(swirl_image);
4972 return((Image *) NULL);
4973 }
cristy4c08aed2011-07-01 19:47:50 +00004974 if (swirl_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00004975 swirl_image->matte=MagickTrue;
4976 /*
4977 Compute scaling factor.
4978 */
4979 center.x=(double) image->columns/2.0;
4980 center.y=(double) image->rows/2.0;
4981 radius=MagickMax(center.x,center.y);
4982 scale.x=1.0;
4983 scale.y=1.0;
4984 if (image->columns > image->rows)
4985 scale.y=(double) image->columns/(double) image->rows;
4986 else
4987 if (image->columns < image->rows)
4988 scale.x=(double) image->rows/(double) image->columns;
4989 degrees=(double) DegreesToRadians(degrees);
4990 /*
4991 Swirl image.
4992 */
4993 status=MagickTrue;
4994 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00004995 image_view=AcquireCacheView(image);
4996 swirl_view=AcquireCacheView(swirl_image);
cristyb5d5f722009-11-04 03:03:49 +00004997#if defined(MAGICKCORE_OPENMP_SUPPORT)
4998 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004999#endif
cristybb503372010-05-27 20:51:26 +00005000 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005001 {
cristy3ed852e2009-09-05 21:47:34 +00005002 MagickRealType
5003 distance;
5004
5005 PointInfo
5006 delta;
5007
cristy6d188022011-09-12 13:23:33 +00005008 register const Quantum
5009 *restrict p;
5010
cristybb503372010-05-27 20:51:26 +00005011 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005012 x;
5013
cristy4c08aed2011-07-01 19:47:50 +00005014 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005015 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005016
5017 if (status == MagickFalse)
5018 continue;
cristy6d188022011-09-12 13:23:33 +00005019 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy3ed852e2009-09-05 21:47:34 +00005020 q=GetCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
5021 exception);
cristy6d188022011-09-12 13:23:33 +00005022 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005023 {
5024 status=MagickFalse;
5025 continue;
5026 }
cristy3ed852e2009-09-05 21:47:34 +00005027 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00005028 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005029 {
cristy6d188022011-09-12 13:23:33 +00005030 register ssize_t
5031 i;
5032
cristy3ed852e2009-09-05 21:47:34 +00005033 /*
5034 Determine if the pixel is within an ellipse.
5035 */
5036 delta.x=scale.x*(double) (x-center.x);
5037 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00005038 if (distance >= (radius*radius))
5039 {
5040 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5041 q[i]=p[i];
5042 }
5043 else
cristy3ed852e2009-09-05 21:47:34 +00005044 {
5045 MagickRealType
5046 cosine,
5047 factor,
5048 sine;
5049
5050 /*
5051 Swirl the pixel.
5052 */
5053 factor=1.0-sqrt((double) distance)/radius;
5054 sine=sin((double) (degrees*factor*factor));
5055 cosine=cos((double) (degrees*factor*factor));
cristy76f512e2011-09-12 01:26:56 +00005056 status=InterpolatePixelChannels(image,image_view,swirl_image,method,
5057 ((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5058 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00005059 }
cristy6d188022011-09-12 13:23:33 +00005060 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00005061 q+=GetPixelChannels(swirl_image);
cristy3ed852e2009-09-05 21:47:34 +00005062 }
5063 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5064 status=MagickFalse;
5065 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5066 {
5067 MagickBooleanType
5068 proceed;
5069
cristyb5d5f722009-11-04 03:03:49 +00005070#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005071 #pragma omp critical (MagickCore_SwirlImage)
5072#endif
5073 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5074 if (proceed == MagickFalse)
5075 status=MagickFalse;
5076 }
5077 }
5078 swirl_view=DestroyCacheView(swirl_view);
5079 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005080 if (status == MagickFalse)
5081 swirl_image=DestroyImage(swirl_image);
5082 return(swirl_image);
5083}
5084
5085/*
5086%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5087% %
5088% %
5089% %
5090% T i n t I m a g e %
5091% %
5092% %
5093% %
5094%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5095%
5096% TintImage() applies a color vector to each pixel in the image. The length
5097% of the vector is 0 for black and white and at its maximum for the midtones.
5098% The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5099%
5100% The format of the TintImage method is:
5101%
cristyb817c3f2011-10-03 14:00:35 +00005102% Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005103% const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005104%
5105% A description of each parameter follows:
5106%
5107% o image: the image.
5108%
cristyb817c3f2011-10-03 14:00:35 +00005109% o blend: A color value used for tinting.
cristy3ed852e2009-09-05 21:47:34 +00005110%
5111% o tint: A color value used for tinting.
5112%
5113% o exception: return any errors or warnings in this structure.
5114%
5115*/
cristyb817c3f2011-10-03 14:00:35 +00005116MagickExport Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005117 const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005118{
5119#define TintImageTag "Tint/Image"
5120
cristyc4c8d132010-01-07 01:58:38 +00005121 CacheView
5122 *image_view,
5123 *tint_view;
5124
cristy3ed852e2009-09-05 21:47:34 +00005125 GeometryInfo
5126 geometry_info;
5127
5128 Image
5129 *tint_image;
5130
cristy3ed852e2009-09-05 21:47:34 +00005131 MagickBooleanType
5132 status;
5133
cristybb503372010-05-27 20:51:26 +00005134 MagickOffsetType
5135 progress;
cristy3ed852e2009-09-05 21:47:34 +00005136
cristy28474bf2011-09-11 23:32:52 +00005137 MagickRealType
5138 intensity;
5139
cristy4c08aed2011-07-01 19:47:50 +00005140 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00005141 color_vector,
5142 pixel;
5143
cristybb503372010-05-27 20:51:26 +00005144 MagickStatusType
5145 flags;
5146
5147 ssize_t
5148 y;
5149
cristy3ed852e2009-09-05 21:47:34 +00005150 /*
5151 Allocate tint image.
5152 */
5153 assert(image != (const Image *) NULL);
5154 assert(image->signature == MagickSignature);
5155 if (image->debug != MagickFalse)
5156 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5157 assert(exception != (ExceptionInfo *) NULL);
5158 assert(exception->signature == MagickSignature);
5159 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5160 if (tint_image == (Image *) NULL)
5161 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005162 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005163 {
cristy3ed852e2009-09-05 21:47:34 +00005164 tint_image=DestroyImage(tint_image);
5165 return((Image *) NULL);
5166 }
cristyaed9c382011-10-03 17:54:21 +00005167 if (blend == (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005168 return(tint_image);
5169 /*
5170 Determine RGB values of the color.
5171 */
cristy28474bf2011-09-11 23:32:52 +00005172 GetPixelInfo(image,&pixel);
cristyb817c3f2011-10-03 14:00:35 +00005173 flags=ParseGeometry(blend,&geometry_info);
cristy3ed852e2009-09-05 21:47:34 +00005174 pixel.red=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005175 pixel.green=geometry_info.rho;
5176 pixel.blue=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005177 pixel.alpha=OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00005178 if ((flags & SigmaValue) != 0)
5179 pixel.green=geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +00005180 if ((flags & XiValue) != 0)
5181 pixel.blue=geometry_info.xi;
cristyb817c3f2011-10-03 14:00:35 +00005182 if ((flags & PsiValue) != 0)
5183 pixel.alpha=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005184 if (image->colorspace == CMYKColorspace)
5185 {
cristyb817c3f2011-10-03 14:00:35 +00005186 pixel.black=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005187 if ((flags & PsiValue) != 0)
5188 pixel.black=geometry_info.psi;
5189 if ((flags & ChiValue) != 0)
5190 pixel.alpha=geometry_info.chi;
5191 }
cristy28474bf2011-09-11 23:32:52 +00005192 intensity=(MagickRealType) GetPixelInfoIntensity(tint);
5193 color_vector.red=(MagickRealType) (pixel.red*tint->red/100.0-intensity);
5194 color_vector.green=(MagickRealType) (pixel.green*tint->green/100.0-intensity);
5195 color_vector.blue=(MagickRealType) (pixel.blue*tint->blue/100.0-intensity);
5196 color_vector.black=(MagickRealType) (pixel.black*tint->black/100.0-intensity);
5197 color_vector.alpha=(MagickRealType) (pixel.alpha*tint->alpha/100.0-intensity);
cristy3ed852e2009-09-05 21:47:34 +00005198 /*
5199 Tint image.
5200 */
5201 status=MagickTrue;
5202 progress=0;
5203 image_view=AcquireCacheView(image);
5204 tint_view=AcquireCacheView(tint_image);
cristyb5d5f722009-11-04 03:03:49 +00005205#if defined(MAGICKCORE_OPENMP_SUPPORT)
5206 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005207#endif
cristybb503372010-05-27 20:51:26 +00005208 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005209 {
cristy4c08aed2011-07-01 19:47:50 +00005210 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00005211 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00005212
cristy4c08aed2011-07-01 19:47:50 +00005213 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005214 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005215
cristy6b91acb2011-04-19 12:23:54 +00005216 register ssize_t
5217 x;
5218
cristy3ed852e2009-09-05 21:47:34 +00005219 if (status == MagickFalse)
5220 continue;
5221 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5222 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5223 exception);
cristy4c08aed2011-07-01 19:47:50 +00005224 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005225 {
5226 status=MagickFalse;
5227 continue;
5228 }
cristybb503372010-05-27 20:51:26 +00005229 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005230 {
cristy4c08aed2011-07-01 19:47:50 +00005231 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00005232 pixel;
5233
5234 MagickRealType
5235 weight;
5236
cristy76f512e2011-09-12 01:26:56 +00005237 if ((GetPixelRedTraits(tint_image) & UpdatePixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005238 {
5239 weight=QuantumScale*GetPixelRed(image,p)-0.5;
5240 pixel.red=(MagickRealType) GetPixelRed(image,p)+
5241 color_vector.red*(1.0-(4.0*(weight*weight)));
5242 SetPixelRed(tint_image,ClampToQuantum(pixel.red),q);
5243 }
cristy76f512e2011-09-12 01:26:56 +00005244 if ((GetPixelGreenTraits(tint_image) & UpdatePixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005245 {
5246 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
5247 pixel.green=(MagickRealType) GetPixelGreen(image,p)+
5248 color_vector.green*(1.0-(4.0*(weight*weight)));
5249 SetPixelGreen(tint_image,ClampToQuantum(pixel.green),q);
5250 }
cristy76f512e2011-09-12 01:26:56 +00005251 if ((GetPixelBlueTraits(tint_image) & UpdatePixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005252 {
5253 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
5254 pixel.blue=(MagickRealType) GetPixelBlue(image,p)+
5255 color_vector.blue*(1.0-(4.0*(weight*weight)));
5256 SetPixelBlue(tint_image,ClampToQuantum(pixel.blue),q);
5257 }
cristy76f512e2011-09-12 01:26:56 +00005258 if ((GetPixelBlackTraits(tint_image) & UpdatePixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005259 {
5260 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
5261 pixel.black=(MagickRealType) GetPixelBlack(image,p)+
5262 color_vector.black*(1.0-(4.0*(weight*weight)));
5263 SetPixelBlack(tint_image,ClampToQuantum(pixel.black),q);
5264 }
cristy76f512e2011-09-12 01:26:56 +00005265 if ((GetPixelAlphaTraits(tint_image) & CopyPixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005266 SetPixelAlpha(tint_image,GetPixelAlpha(image,p),q);
cristyed231572011-07-14 02:18:59 +00005267 p+=GetPixelChannels(image);
5268 q+=GetPixelChannels(tint_image);
cristy3ed852e2009-09-05 21:47:34 +00005269 }
5270 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5271 status=MagickFalse;
5272 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5273 {
5274 MagickBooleanType
5275 proceed;
5276
cristyb5d5f722009-11-04 03:03:49 +00005277#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005278 #pragma omp critical (MagickCore_TintImage)
5279#endif
5280 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5281 if (proceed == MagickFalse)
5282 status=MagickFalse;
5283 }
5284 }
5285 tint_view=DestroyCacheView(tint_view);
5286 image_view=DestroyCacheView(image_view);
5287 if (status == MagickFalse)
5288 tint_image=DestroyImage(tint_image);
5289 return(tint_image);
5290}
5291
5292/*
5293%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5294% %
5295% %
5296% %
5297% V i g n e t t e I m a g e %
5298% %
5299% %
5300% %
5301%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5302%
5303% VignetteImage() softens the edges of the image in vignette style.
5304%
5305% The format of the VignetteImage method is:
5306%
5307% Image *VignetteImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +00005308% const double sigma,const ssize_t x,const ssize_t y,
5309% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005310%
5311% A description of each parameter follows:
5312%
5313% o image: the image.
5314%
5315% o radius: the radius of the pixel neighborhood.
5316%
5317% o sigma: the standard deviation of the Gaussian, in pixels.
5318%
5319% o x, y: Define the x and y ellipse offset.
5320%
5321% o exception: return any errors or warnings in this structure.
5322%
5323*/
5324MagickExport Image *VignetteImage(const Image *image,const double radius,
cristybb503372010-05-27 20:51:26 +00005325 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005326{
5327 char
5328 ellipse[MaxTextExtent];
5329
5330 DrawInfo
5331 *draw_info;
5332
5333 Image
5334 *canvas_image,
5335 *blur_image,
5336 *oval_image,
5337 *vignette_image;
5338
5339 assert(image != (Image *) NULL);
5340 assert(image->signature == MagickSignature);
5341 if (image->debug != MagickFalse)
5342 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5343 assert(exception != (ExceptionInfo *) NULL);
5344 assert(exception->signature == MagickSignature);
5345 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5346 if (canvas_image == (Image *) NULL)
5347 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005348 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005349 {
cristy3ed852e2009-09-05 21:47:34 +00005350 canvas_image=DestroyImage(canvas_image);
5351 return((Image *) NULL);
5352 }
5353 canvas_image->matte=MagickTrue;
cristyb3a73b52011-07-26 01:34:43 +00005354 oval_image=CloneImage(canvas_image,canvas_image->columns,
5355 canvas_image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005356 if (oval_image == (Image *) NULL)
5357 {
5358 canvas_image=DestroyImage(canvas_image);
5359 return((Image *) NULL);
5360 }
cristy9950d572011-10-01 18:22:35 +00005361 (void) QueryColorCompliance("#000000",AllCompliance,
5362 &oval_image->background_color,exception);
cristyea1a8aa2011-10-20 13:24:06 +00005363 (void) SetImageBackgroundColor(oval_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00005364 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
cristy9950d572011-10-01 18:22:35 +00005365 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5366 exception);
5367 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5368 exception);
cristyb51dff52011-05-19 16:55:47 +00005369 (void) FormatLocaleString(ellipse,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00005370 "ellipse %g,%g,%g,%g,0.0,360.0",image->columns/2.0,
cristy8cd5b312010-01-07 01:10:24 +00005371 image->rows/2.0,image->columns/2.0-x,image->rows/2.0-y);
cristy3ed852e2009-09-05 21:47:34 +00005372 draw_info->primitive=AcquireString(ellipse);
cristy018f07f2011-09-04 21:15:19 +00005373 (void) DrawImage(oval_image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00005374 draw_info=DestroyDrawInfo(draw_info);
cristy05c0c9a2011-09-05 23:16:13 +00005375 blur_image=BlurImage(oval_image,radius,sigma,image->bias,exception);
cristy3ed852e2009-09-05 21:47:34 +00005376 oval_image=DestroyImage(oval_image);
5377 if (blur_image == (Image *) NULL)
5378 {
5379 canvas_image=DestroyImage(canvas_image);
5380 return((Image *) NULL);
5381 }
5382 blur_image->matte=MagickFalse;
cristye941a752011-10-15 01:52:48 +00005383 (void) CompositeImage(canvas_image,CopyOpacityCompositeOp,blur_image,0,0,
5384 exception);
cristy3ed852e2009-09-05 21:47:34 +00005385 blur_image=DestroyImage(blur_image);
5386 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5387 canvas_image=DestroyImage(canvas_image);
5388 return(vignette_image);
5389}
5390
5391/*
5392%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5393% %
5394% %
5395% %
5396% W a v e I m a g e %
5397% %
5398% %
5399% %
5400%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5401%
5402% WaveImage() creates a "ripple" effect in the image by shifting the pixels
cristycee97112010-05-28 00:44:52 +00005403% vertically along a sine wave whose amplitude and wavelength is specified
cristy3ed852e2009-09-05 21:47:34 +00005404% by the given parameters.
5405%
5406% The format of the WaveImage method is:
5407%
5408% Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005409% const double wave_length,const PixelInterpolateMethod method,
5410% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005411%
5412% A description of each parameter follows:
5413%
5414% o image: the image.
5415%
5416% o amplitude, wave_length: Define the amplitude and wave length of the
5417% sine wave.
5418%
cristy5c4e2582011-09-11 19:21:03 +00005419% o interpolate: the pixel interpolation method.
5420%
cristy3ed852e2009-09-05 21:47:34 +00005421% o exception: return any errors or warnings in this structure.
5422%
5423*/
5424MagickExport Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005425 const double wave_length,const PixelInterpolateMethod method,
5426 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005427{
5428#define WaveImageTag "Wave/Image"
5429
cristyfa112112010-01-04 17:48:07 +00005430 CacheView
cristyd76c51e2011-03-26 00:21:26 +00005431 *image_view,
cristyfa112112010-01-04 17:48:07 +00005432 *wave_view;
5433
cristy3ed852e2009-09-05 21:47:34 +00005434 Image
5435 *wave_image;
5436
cristy3ed852e2009-09-05 21:47:34 +00005437 MagickBooleanType
5438 status;
5439
cristybb503372010-05-27 20:51:26 +00005440 MagickOffsetType
5441 progress;
5442
cristy3ed852e2009-09-05 21:47:34 +00005443 MagickRealType
5444 *sine_map;
5445
cristybb503372010-05-27 20:51:26 +00005446 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005447 i;
5448
cristybb503372010-05-27 20:51:26 +00005449 ssize_t
5450 y;
5451
cristy3ed852e2009-09-05 21:47:34 +00005452 /*
5453 Initialize wave image attributes.
5454 */
5455 assert(image != (Image *) NULL);
5456 assert(image->signature == MagickSignature);
5457 if (image->debug != MagickFalse)
5458 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5459 assert(exception != (ExceptionInfo *) NULL);
5460 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00005461 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
cristy3ed852e2009-09-05 21:47:34 +00005462 fabs(amplitude)),MagickTrue,exception);
5463 if (wave_image == (Image *) NULL)
5464 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005465 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005466 {
cristy3ed852e2009-09-05 21:47:34 +00005467 wave_image=DestroyImage(wave_image);
5468 return((Image *) NULL);
5469 }
cristy4c08aed2011-07-01 19:47:50 +00005470 if (wave_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00005471 wave_image->matte=MagickTrue;
5472 /*
5473 Allocate sine map.
5474 */
5475 sine_map=(MagickRealType *) AcquireQuantumMemory((size_t) wave_image->columns,
5476 sizeof(*sine_map));
5477 if (sine_map == (MagickRealType *) NULL)
5478 {
5479 wave_image=DestroyImage(wave_image);
5480 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5481 }
cristybb503372010-05-27 20:51:26 +00005482 for (i=0; i < (ssize_t) wave_image->columns; i++)
cristy4205a3c2010-09-12 20:19:59 +00005483 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5484 wave_length));
cristy3ed852e2009-09-05 21:47:34 +00005485 /*
5486 Wave image.
5487 */
5488 status=MagickTrue;
5489 progress=0;
cristyd76c51e2011-03-26 00:21:26 +00005490 image_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00005491 wave_view=AcquireCacheView(wave_image);
cristyd76c51e2011-03-26 00:21:26 +00005492 (void) SetCacheViewVirtualPixelMethod(image_view,
5493 BackgroundVirtualPixelMethod);
cristyb5d5f722009-11-04 03:03:49 +00005494#if defined(MAGICKCORE_OPENMP_SUPPORT)
5495 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005496#endif
cristybb503372010-05-27 20:51:26 +00005497 for (y=0; y < (ssize_t) wave_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005498 {
cristy4c08aed2011-07-01 19:47:50 +00005499 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005500 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005501
cristye97bb922011-04-03 01:36:52 +00005502 register ssize_t
5503 x;
5504
cristy3ed852e2009-09-05 21:47:34 +00005505 if (status == MagickFalse)
5506 continue;
5507 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5508 exception);
cristyacd2ed22011-08-30 01:44:23 +00005509 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005510 {
5511 status=MagickFalse;
5512 continue;
5513 }
cristybb503372010-05-27 20:51:26 +00005514 for (x=0; x < (ssize_t) wave_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005515 {
cristy5c4e2582011-09-11 19:21:03 +00005516 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5517 (double) x,(double) (y-sine_map[x]),q,exception);
cristyed231572011-07-14 02:18:59 +00005518 q+=GetPixelChannels(wave_image);
cristy3ed852e2009-09-05 21:47:34 +00005519 }
5520 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5521 status=MagickFalse;
5522 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5523 {
5524 MagickBooleanType
5525 proceed;
5526
cristyb5d5f722009-11-04 03:03:49 +00005527#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005528 #pragma omp critical (MagickCore_WaveImage)
5529#endif
5530 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5531 if (proceed == MagickFalse)
5532 status=MagickFalse;
5533 }
5534 }
5535 wave_view=DestroyCacheView(wave_view);
cristyd76c51e2011-03-26 00:21:26 +00005536 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005537 sine_map=(MagickRealType *) RelinquishMagickMemory(sine_map);
5538 if (status == MagickFalse)
5539 wave_image=DestroyImage(wave_image);
5540 return(wave_image);
5541}