blob: 981373fadea3161af263a509505a219d0cb8f58f [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 }
cristy5597a8d2011-11-04 00:25:32 +00002496 if (LocaleNCompare(expression,"drc",3) == 0)
2497 {
2498 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2499 exception);
2500 return((MagickRealType) (alpha/(*beta*(alpha-1.0)+1.0)));
2501 }
cristy3ed852e2009-09-05 21:47:34 +00002502 break;
2503 }
2504 case 'E':
2505 case 'e':
2506 {
2507 if (LocaleCompare(expression,"epsilon") == 0)
2508 return((MagickRealType) MagickEpsilon);
2509 if (LocaleNCompare(expression,"exp",3) == 0)
2510 {
2511 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2512 exception);
2513 return((MagickRealType) exp((double) alpha));
2514 }
2515 if (LocaleCompare(expression,"e") == 0)
2516 return((MagickRealType) 2.7182818284590452354);
2517 break;
2518 }
2519 case 'F':
2520 case 'f':
2521 {
2522 if (LocaleNCompare(expression,"floor",5) == 0)
2523 {
2524 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2525 exception);
2526 return((MagickRealType) floor((double) alpha));
2527 }
2528 break;
2529 }
2530 case 'G':
2531 case 'g':
2532 {
cristy9eeedea2011-11-02 19:04:05 +00002533 if (LocaleNCompare(expression,"gauss",5) == 0)
2534 {
2535 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2536 exception);
2537 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
2538 return((MagickRealType) gamma);
2539 }
cristyb0aad4c2011-11-02 19:30:35 +00002540 if (LocaleNCompare(expression,"gcd",3) == 0)
2541 {
2542 MagickOffsetType
2543 gcd;
2544
2545 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2546 exception);
cristy76461162011-11-02 19:32:19 +00002547 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType)
2548 (*beta+0.5));
cristyb0aad4c2011-11-02 19:30:35 +00002549 return((MagickRealType) gcd);
2550 }
cristy3ed852e2009-09-05 21:47:34 +00002551 if (LocaleCompare(expression,"g") == 0)
2552 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2553 break;
2554 }
2555 case 'H':
2556 case 'h':
2557 {
2558 if (LocaleCompare(expression,"h") == 0)
2559 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2560 if (LocaleCompare(expression,"hue") == 0)
2561 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2562 if (LocaleNCompare(expression,"hypot",5) == 0)
2563 {
2564 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2565 exception);
2566 return((MagickRealType) hypot((double) alpha,(double) *beta));
2567 }
2568 break;
2569 }
2570 case 'K':
2571 case 'k':
2572 {
2573 if (LocaleCompare(expression,"k") == 0)
2574 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2575 break;
2576 }
2577 case 'I':
2578 case 'i':
2579 {
2580 if (LocaleCompare(expression,"intensity") == 0)
2581 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2582 if (LocaleNCompare(expression,"int",3) == 0)
2583 {
2584 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2585 exception);
cristy16788e42010-08-13 13:44:26 +00002586 return((MagickRealType) floor(alpha));
cristy3ed852e2009-09-05 21:47:34 +00002587 }
cristy639399c2011-11-02 19:16:15 +00002588 if (LocaleNCompare(expression,"isnan",5) == 0)
2589 {
2590 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2591 exception);
cristy17a10202011-11-02 19:17:04 +00002592 return((MagickRealType) !!isnan((double) alpha));
cristy639399c2011-11-02 19:16:15 +00002593 }
cristy3ed852e2009-09-05 21:47:34 +00002594 if (LocaleCompare(expression,"i") == 0)
2595 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2596 break;
2597 }
2598 case 'J':
2599 case 'j':
2600 {
2601 if (LocaleCompare(expression,"j") == 0)
2602 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
cristy161b9262010-03-20 19:34:32 +00002603#if defined(MAGICKCORE_HAVE_J0)
cristyee56cf12010-03-01 22:17:06 +00002604 if (LocaleNCompare(expression,"j0",2) == 0)
2605 {
2606 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2607 exception);
2608 return((MagickRealType) j0((double) alpha));
2609 }
cristy161b9262010-03-20 19:34:32 +00002610#endif
2611#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002612 if (LocaleNCompare(expression,"j1",2) == 0)
2613 {
2614 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2615 exception);
2616 return((MagickRealType) j1((double) alpha));
2617 }
cristy161b9262010-03-20 19:34:32 +00002618#endif
cristyaa018fa2010-04-08 23:03:54 +00002619#if defined(MAGICKCORE_HAVE_J1)
cristya6a09e72010-03-02 14:51:02 +00002620 if (LocaleNCompare(expression,"jinc",4) == 0)
2621 {
2622 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2623 exception);
cristy0946a822010-03-12 17:14:58 +00002624 if (alpha == 0.0)
2625 return(1.0);
cristy69928f92010-03-12 13:27:09 +00002626 gamma=(MagickRealType) (2.0*j1((double) (MagickPI*alpha))/
cristyfce2f7b2010-03-12 00:29:49 +00002627 (MagickPI*alpha));
2628 return(gamma);
cristya6a09e72010-03-02 14:51:02 +00002629 }
cristyaa018fa2010-04-08 23:03:54 +00002630#endif
cristy3ed852e2009-09-05 21:47:34 +00002631 break;
2632 }
2633 case 'L':
2634 case 'l':
2635 {
2636 if (LocaleNCompare(expression,"ln",2) == 0)
2637 {
2638 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2639 exception);
2640 return((MagickRealType) log((double) alpha));
2641 }
cristyc8ed5322010-08-31 12:07:59 +00002642 if (LocaleNCompare(expression,"logtwo",6) == 0)
cristy3ed852e2009-09-05 21:47:34 +00002643 {
cristyc8ed5322010-08-31 12:07:59 +00002644 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
cristy3ed852e2009-09-05 21:47:34 +00002645 exception);
2646 return((MagickRealType) log10((double) alpha))/log10(2.0);
2647 }
2648 if (LocaleNCompare(expression,"log",3) == 0)
2649 {
2650 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2651 exception);
2652 return((MagickRealType) log10((double) alpha));
2653 }
2654 if (LocaleCompare(expression,"lightness") == 0)
2655 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2656 break;
2657 }
2658 case 'M':
2659 case 'm':
2660 {
2661 if (LocaleCompare(expression,"MaxRGB") == 0)
2662 return((MagickRealType) QuantumRange);
2663 if (LocaleNCompare(expression,"maxima",6) == 0)
2664 break;
2665 if (LocaleNCompare(expression,"max",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002666 {
2667 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2668 exception);
2669 return(alpha > *beta ? alpha : *beta);
2670 }
cristy3ed852e2009-09-05 21:47:34 +00002671 if (LocaleNCompare(expression,"minima",6) == 0)
2672 break;
2673 if (LocaleNCompare(expression,"min",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002674 {
2675 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2676 exception);
2677 return(alpha < *beta ? alpha : *beta);
2678 }
cristy3ed852e2009-09-05 21:47:34 +00002679 if (LocaleNCompare(expression,"mod",3) == 0)
2680 {
2681 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2682 exception);
cristy984049c2011-11-03 18:34:58 +00002683 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2684 return(gamma);
cristy3ed852e2009-09-05 21:47:34 +00002685 }
2686 if (LocaleCompare(expression,"m") == 0)
2687 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2688 break;
2689 }
2690 case 'N':
2691 case 'n':
2692 {
cristyad3502e2011-11-02 19:10:45 +00002693 if (LocaleNCompare(expression,"not",3) == 0)
2694 {
2695 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2696 exception);
2697 return((MagickRealType) (alpha < MagickEpsilon));
2698 }
cristy3ed852e2009-09-05 21:47:34 +00002699 if (LocaleCompare(expression,"n") == 0)
2700 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2701 break;
2702 }
2703 case 'O':
2704 case 'o':
2705 {
2706 if (LocaleCompare(expression,"Opaque") == 0)
2707 return(1.0);
2708 if (LocaleCompare(expression,"o") == 0)
2709 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2710 break;
2711 }
2712 case 'P':
2713 case 'p':
2714 {
cristy670aa3c2011-11-03 00:54:00 +00002715 if (LocaleCompare(expression,"phi") == 0)
2716 return((MagickRealType) MagickPHI);
cristy3ed852e2009-09-05 21:47:34 +00002717 if (LocaleCompare(expression,"pi") == 0)
2718 return((MagickRealType) MagickPI);
2719 if (LocaleNCompare(expression,"pow",3) == 0)
2720 {
2721 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2722 exception);
2723 return((MagickRealType) pow((double) alpha,(double) *beta));
2724 }
2725 if (LocaleCompare(expression,"p") == 0)
2726 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2727 break;
2728 }
2729 case 'Q':
2730 case 'q':
2731 {
2732 if (LocaleCompare(expression,"QuantumRange") == 0)
2733 return((MagickRealType) QuantumRange);
2734 if (LocaleCompare(expression,"QuantumScale") == 0)
2735 return((MagickRealType) QuantumScale);
2736 break;
2737 }
2738 case 'R':
2739 case 'r':
2740 {
2741 if (LocaleNCompare(expression,"rand",4) == 0)
2742 return((MagickRealType) GetPseudoRandomValue(fx_info->random_info));
2743 if (LocaleNCompare(expression,"round",5) == 0)
2744 {
2745 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2746 exception);
cristy16788e42010-08-13 13:44:26 +00002747 return((MagickRealType) floor((double) alpha+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002748 }
2749 if (LocaleCompare(expression,"r") == 0)
2750 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2751 break;
2752 }
2753 case 'S':
2754 case 's':
2755 {
2756 if (LocaleCompare(expression,"saturation") == 0)
2757 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2758 if (LocaleNCompare(expression,"sign",4) == 0)
2759 {
2760 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2761 exception);
2762 return(alpha < 0.0 ? -1.0 : 1.0);
2763 }
cristya6a09e72010-03-02 14:51:02 +00002764 if (LocaleNCompare(expression,"sinc",4) == 0)
2765 {
2766 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2767 exception);
2768 if (alpha == 0)
2769 return(1.0);
2770 gamma=(MagickRealType) (sin((double) (MagickPI*alpha))/
2771 (MagickPI*alpha));
2772 return(gamma);
2773 }
cristy3ed852e2009-09-05 21:47:34 +00002774 if (LocaleNCompare(expression,"sinh",4) == 0)
2775 {
2776 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2777 exception);
2778 return((MagickRealType) sinh((double) alpha));
2779 }
2780 if (LocaleNCompare(expression,"sin",3) == 0)
2781 {
2782 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2783 exception);
2784 return((MagickRealType) sin((double) alpha));
2785 }
2786 if (LocaleNCompare(expression,"sqrt",4) == 0)
2787 {
2788 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2789 exception);
2790 return((MagickRealType) sqrt((double) alpha));
2791 }
cristy9eeedea2011-11-02 19:04:05 +00002792 if (LocaleNCompare(expression,"squish",6) == 0)
2793 {
2794 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2795 exception);
2796 return((MagickRealType) (1.0/(1.0+exp((double) (4.0*alpha)))));
2797 }
cristy3ed852e2009-09-05 21:47:34 +00002798 if (LocaleCompare(expression,"s") == 0)
2799 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2800 break;
2801 }
2802 case 'T':
2803 case 't':
2804 {
2805 if (LocaleNCompare(expression,"tanh",4) == 0)
2806 {
2807 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2808 exception);
2809 return((MagickRealType) tanh((double) alpha));
2810 }
2811 if (LocaleNCompare(expression,"tan",3) == 0)
2812 {
2813 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2814 exception);
2815 return((MagickRealType) tan((double) alpha));
2816 }
2817 if (LocaleCompare(expression,"Transparent") == 0)
2818 return(0.0);
cristy16788e42010-08-13 13:44:26 +00002819 if (LocaleNCompare(expression,"trunc",5) == 0)
2820 {
2821 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2822 exception);
2823 if (alpha >= 0.0)
2824 return((MagickRealType) floor((double) alpha));
2825 return((MagickRealType) ceil((double) alpha));
2826 }
cristy3ed852e2009-09-05 21:47:34 +00002827 if (LocaleCompare(expression,"t") == 0)
2828 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2829 break;
2830 }
2831 case 'U':
2832 case 'u':
2833 {
2834 if (LocaleCompare(expression,"u") == 0)
2835 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2836 break;
2837 }
2838 case 'V':
2839 case 'v':
2840 {
2841 if (LocaleCompare(expression,"v") == 0)
2842 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2843 break;
2844 }
2845 case 'W':
2846 case 'w':
2847 {
cristy9eeedea2011-11-02 19:04:05 +00002848 if (LocaleNCompare(expression,"while",5) == 0)
2849 {
2850 do
2851 {
2852 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2853 exception);
2854 } while (fabs((double) alpha) >= MagickEpsilon);
2855 return((MagickRealType) *beta);
2856 }
cristy3ed852e2009-09-05 21:47:34 +00002857 if (LocaleCompare(expression,"w") == 0)
2858 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2859 break;
2860 }
2861 case 'Y':
2862 case 'y':
2863 {
2864 if (LocaleCompare(expression,"y") == 0)
2865 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2866 break;
2867 }
2868 case 'Z':
2869 case 'z':
2870 {
2871 if (LocaleCompare(expression,"z") == 0)
2872 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2873 break;
2874 }
2875 default:
2876 break;
2877 }
2878 q=(char *) expression;
cristyc1acd842011-05-19 23:05:47 +00002879 alpha=InterpretLocaleValue(expression,&q);
cristy3ed852e2009-09-05 21:47:34 +00002880 if (q == expression)
2881 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2882 return(alpha);
2883}
2884
cristy7832dc22011-09-05 01:21:53 +00002885MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
cristy3ed852e2009-09-05 21:47:34 +00002886 MagickRealType *alpha,ExceptionInfo *exception)
2887{
2888 MagickBooleanType
2889 status;
2890
cristy541ae572011-08-05 19:08:59 +00002891 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2892 exception);
cristy3ed852e2009-09-05 21:47:34 +00002893 return(status);
2894}
2895
2896MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
2897 MagickRealType *alpha,ExceptionInfo *exception)
2898{
2899 FILE
2900 *file;
2901
2902 MagickBooleanType
2903 status;
2904
2905 file=fx_info->file;
2906 fx_info->file=(FILE *) NULL;
cristy541ae572011-08-05 19:08:59 +00002907 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2908 exception);
cristy3ed852e2009-09-05 21:47:34 +00002909 fx_info->file=file;
2910 return(status);
2911}
2912
cristy7832dc22011-09-05 01:21:53 +00002913MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002914 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002915 MagickRealType *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002916{
2917 MagickRealType
2918 beta;
2919
2920 beta=0.0;
2921 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2922 exception);
2923 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2924}
2925
2926/*
2927%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2928% %
2929% %
2930% %
2931% F x I m a g e %
2932% %
2933% %
2934% %
2935%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2936%
2937% FxImage() applies a mathematical expression to the specified image.
2938%
2939% The format of the FxImage method is:
2940%
2941% Image *FxImage(const Image *image,const char *expression,
2942% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002943%
2944% A description of each parameter follows:
2945%
2946% o image: the image.
2947%
cristy3ed852e2009-09-05 21:47:34 +00002948% o expression: A mathematical expression.
2949%
2950% o exception: return any errors or warnings in this structure.
2951%
2952*/
2953
2954static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2955{
cristybb503372010-05-27 20:51:26 +00002956 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002957 i;
2958
2959 assert(fx_info != (FxInfo **) NULL);
cristybb503372010-05-27 20:51:26 +00002960 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
cristy3ed852e2009-09-05 21:47:34 +00002961 if (fx_info[i] != (FxInfo *) NULL)
2962 fx_info[i]=DestroyFxInfo(fx_info[i]);
cristyb41ee102010-10-04 16:46:15 +00002963 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
cristy3ed852e2009-09-05 21:47:34 +00002964 return(fx_info);
2965}
2966
2967static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2968 ExceptionInfo *exception)
2969{
2970 char
2971 *fx_expression;
2972
2973 FxInfo
2974 **fx_info;
2975
2976 MagickRealType
2977 alpha;
2978
cristybb503372010-05-27 20:51:26 +00002979 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002980 i;
2981
cristybb503372010-05-27 20:51:26 +00002982 size_t
cristy3ed852e2009-09-05 21:47:34 +00002983 number_threads;
2984
2985 number_threads=GetOpenMPMaximumThreads();
cristyb41ee102010-10-04 16:46:15 +00002986 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +00002987 if (fx_info == (FxInfo **) NULL)
2988 return((FxInfo **) NULL);
2989 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2990 if (*expression != '@')
2991 fx_expression=ConstantString(expression);
2992 else
2993 fx_expression=FileToString(expression+1,~0,exception);
cristybb503372010-05-27 20:51:26 +00002994 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +00002995 {
2996 fx_info[i]=AcquireFxInfo(image,fx_expression);
2997 if (fx_info[i] == (FxInfo *) NULL)
2998 return(DestroyFxThreadSet(fx_info));
2999 (void) FxPreprocessExpression(fx_info[i],&alpha,fx_info[i]->exception);
3000 }
3001 fx_expression=DestroyString(fx_expression);
3002 return(fx_info);
3003}
3004
3005MagickExport Image *FxImage(const Image *image,const char *expression,
3006 ExceptionInfo *exception)
3007{
cristy3ed852e2009-09-05 21:47:34 +00003008#define FxImageTag "Fx/Image"
3009
cristyfa112112010-01-04 17:48:07 +00003010 CacheView
cristy79cedc72011-07-25 00:41:15 +00003011 *fx_view,
3012 *image_view;
cristyfa112112010-01-04 17:48:07 +00003013
cristy3ed852e2009-09-05 21:47:34 +00003014 FxInfo
cristyfa112112010-01-04 17:48:07 +00003015 **restrict fx_info;
cristy3ed852e2009-09-05 21:47:34 +00003016
3017 Image
3018 *fx_image;
3019
cristy3ed852e2009-09-05 21:47:34 +00003020 MagickBooleanType
3021 status;
3022
cristybb503372010-05-27 20:51:26 +00003023 MagickOffsetType
3024 progress;
3025
cristy3ed852e2009-09-05 21:47:34 +00003026 MagickRealType
3027 alpha;
3028
cristybb503372010-05-27 20:51:26 +00003029 ssize_t
3030 y;
3031
cristy3ed852e2009-09-05 21:47:34 +00003032 assert(image != (Image *) NULL);
3033 assert(image->signature == MagickSignature);
3034 if (image->debug != MagickFalse)
3035 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy79cedc72011-07-25 00:41:15 +00003036 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00003037 if (fx_image == (Image *) NULL)
3038 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003039 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003040 {
cristy3ed852e2009-09-05 21:47:34 +00003041 fx_image=DestroyImage(fx_image);
3042 return((Image *) NULL);
3043 }
3044 fx_info=AcquireFxThreadSet(image,expression,exception);
3045 if (fx_info == (FxInfo **) NULL)
3046 {
3047 fx_image=DestroyImage(fx_image);
3048 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3049 }
3050 status=FxPreprocessExpression(fx_info[0],&alpha,exception);
3051 if (status == MagickFalse)
3052 {
3053 fx_image=DestroyImage(fx_image);
3054 fx_info=DestroyFxThreadSet(fx_info);
3055 return((Image *) NULL);
3056 }
3057 /*
3058 Fx image.
3059 */
3060 status=MagickTrue;
3061 progress=0;
cristy79cedc72011-07-25 00:41:15 +00003062 image_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00003063 fx_view=AcquireCacheView(fx_image);
cristyb5d5f722009-11-04 03:03:49 +00003064#if defined(MAGICKCORE_OPENMP_SUPPORT)
3065 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003066#endif
cristybb503372010-05-27 20:51:26 +00003067 for (y=0; y < (ssize_t) fx_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003068 {
cristy5c9e6f22010-09-17 17:31:01 +00003069 const int
3070 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003071
cristy79cedc72011-07-25 00:41:15 +00003072 register const Quantum
3073 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003074
cristy4c08aed2011-07-01 19:47:50 +00003075 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003076 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003077
cristy79cedc72011-07-25 00:41:15 +00003078 register ssize_t
3079 x;
3080
cristy3ed852e2009-09-05 21:47:34 +00003081 if (status == MagickFalse)
3082 continue;
cristy79cedc72011-07-25 00:41:15 +00003083 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy3ed852e2009-09-05 21:47:34 +00003084 q=GetCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
cristy79cedc72011-07-25 00:41:15 +00003085 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003086 {
3087 status=MagickFalse;
3088 continue;
3089 }
cristybb503372010-05-27 20:51:26 +00003090 for (x=0; x < (ssize_t) fx_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003091 {
cristy79cedc72011-07-25 00:41:15 +00003092 register ssize_t
3093 i;
3094
3095 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3096 {
3097 MagickRealType
3098 alpha;
3099
3100 PixelChannel
3101 channel;
3102
3103 PixelTrait
3104 fx_traits,
3105 traits;
3106
3107 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
cristy79cedc72011-07-25 00:41:15 +00003108 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3109 fx_traits=GetPixelChannelMapTraits(fx_image,channel);
cristy010d7d12011-08-31 01:02:48 +00003110 if ((traits == UndefinedPixelTrait) ||
3111 (fx_traits == UndefinedPixelTrait))
cristy79cedc72011-07-25 00:41:15 +00003112 continue;
3113 if ((fx_traits & CopyPixelTrait) != 0)
3114 {
cristy0beccfa2011-09-25 20:47:53 +00003115 SetPixelChannel(fx_image,channel,p[i],q);
cristy79cedc72011-07-25 00:41:15 +00003116 continue;
3117 }
3118 alpha=0.0;
cristy0568ffc2011-07-25 16:54:14 +00003119 (void) FxEvaluateChannelExpression(fx_info[id],(PixelChannel) i,x,y,
3120 &alpha,exception);
cristyb3a73b52011-07-26 01:34:43 +00003121 q[i]=ClampToQuantum((MagickRealType) QuantumRange*alpha);
cristy79cedc72011-07-25 00:41:15 +00003122 }
3123 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003124 q+=GetPixelChannels(fx_image);
cristy3ed852e2009-09-05 21:47:34 +00003125 }
3126 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3127 status=MagickFalse;
3128 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3129 {
3130 MagickBooleanType
3131 proceed;
3132
cristyb5d5f722009-11-04 03:03:49 +00003133#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy490408a2011-07-07 14:42:05 +00003134 #pragma omp critical (MagickCore_FxImage)
cristy3ed852e2009-09-05 21:47:34 +00003135#endif
3136 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3137 if (proceed == MagickFalse)
3138 status=MagickFalse;
3139 }
3140 }
cristy3ed852e2009-09-05 21:47:34 +00003141 fx_view=DestroyCacheView(fx_view);
cristy79cedc72011-07-25 00:41:15 +00003142 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003143 fx_info=DestroyFxThreadSet(fx_info);
3144 if (status == MagickFalse)
3145 fx_image=DestroyImage(fx_image);
3146 return(fx_image);
3147}
3148
3149/*
3150%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3151% %
3152% %
3153% %
3154% I m p l o d e I m a g e %
3155% %
3156% %
3157% %
3158%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3159%
3160% ImplodeImage() creates a new image that is a copy of an existing
3161% one with the image pixels "implode" by the specified percentage. It
3162% allocates the memory necessary for the new Image structure and returns a
3163% pointer to the new image.
3164%
3165% The format of the ImplodeImage method is:
3166%
3167% Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003168% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003169%
3170% A description of each parameter follows:
3171%
3172% o implode_image: Method ImplodeImage returns a pointer to the image
3173% after it is implode. A null image is returned if there is a memory
3174% shortage.
3175%
3176% o image: the image.
3177%
3178% o amount: Define the extent of the implosion.
3179%
cristy76f512e2011-09-12 01:26:56 +00003180% o method: the pixel interpolation method.
3181%
cristy3ed852e2009-09-05 21:47:34 +00003182% o exception: return any errors or warnings in this structure.
3183%
3184*/
3185MagickExport Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003186 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003187{
3188#define ImplodeImageTag "Implode/Image"
3189
cristyfa112112010-01-04 17:48:07 +00003190 CacheView
3191 *image_view,
3192 *implode_view;
3193
cristy3ed852e2009-09-05 21:47:34 +00003194 Image
3195 *implode_image;
3196
cristy3ed852e2009-09-05 21:47:34 +00003197 MagickBooleanType
3198 status;
3199
cristybb503372010-05-27 20:51:26 +00003200 MagickOffsetType
3201 progress;
3202
cristy3ed852e2009-09-05 21:47:34 +00003203 MagickRealType
3204 radius;
3205
3206 PointInfo
3207 center,
3208 scale;
3209
cristybb503372010-05-27 20:51:26 +00003210 ssize_t
3211 y;
3212
cristy3ed852e2009-09-05 21:47:34 +00003213 /*
3214 Initialize implode image attributes.
3215 */
3216 assert(image != (Image *) NULL);
3217 assert(image->signature == MagickSignature);
3218 if (image->debug != MagickFalse)
3219 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3220 assert(exception != (ExceptionInfo *) NULL);
3221 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00003222 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3223 exception);
cristy3ed852e2009-09-05 21:47:34 +00003224 if (implode_image == (Image *) NULL)
3225 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003226 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003227 {
cristy3ed852e2009-09-05 21:47:34 +00003228 implode_image=DestroyImage(implode_image);
3229 return((Image *) NULL);
3230 }
cristy4c08aed2011-07-01 19:47:50 +00003231 if (implode_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00003232 implode_image->matte=MagickTrue;
3233 /*
3234 Compute scaling factor.
3235 */
3236 scale.x=1.0;
3237 scale.y=1.0;
3238 center.x=0.5*image->columns;
3239 center.y=0.5*image->rows;
3240 radius=center.x;
3241 if (image->columns > image->rows)
3242 scale.y=(double) image->columns/(double) image->rows;
3243 else
3244 if (image->columns < image->rows)
3245 {
3246 scale.x=(double) image->rows/(double) image->columns;
3247 radius=center.y;
3248 }
3249 /*
3250 Implode image.
3251 */
3252 status=MagickTrue;
3253 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00003254 image_view=AcquireCacheView(image);
3255 implode_view=AcquireCacheView(implode_image);
cristyb5d5f722009-11-04 03:03:49 +00003256#if defined(MAGICKCORE_OPENMP_SUPPORT)
3257 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003258#endif
cristybb503372010-05-27 20:51:26 +00003259 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003260 {
cristy3ed852e2009-09-05 21:47:34 +00003261 MagickRealType
3262 distance;
3263
3264 PointInfo
3265 delta;
3266
cristy6d188022011-09-12 13:23:33 +00003267 register const Quantum
3268 *restrict p;
3269
cristybb503372010-05-27 20:51:26 +00003270 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003271 x;
3272
cristy4c08aed2011-07-01 19:47:50 +00003273 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003274 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003275
3276 if (status == MagickFalse)
3277 continue;
cristy6d188022011-09-12 13:23:33 +00003278 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy3ed852e2009-09-05 21:47:34 +00003279 q=GetCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
3280 exception);
cristy6d188022011-09-12 13:23:33 +00003281 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003282 {
3283 status=MagickFalse;
3284 continue;
3285 }
cristy3ed852e2009-09-05 21:47:34 +00003286 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00003287 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003288 {
cristy6d188022011-09-12 13:23:33 +00003289 register ssize_t
3290 i;
3291
cristy3ed852e2009-09-05 21:47:34 +00003292 /*
3293 Determine if the pixel is within an ellipse.
3294 */
3295 delta.x=scale.x*(double) (x-center.x);
3296 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00003297 if (distance >= (radius*radius))
3298 {
3299 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3300 q[i]=p[i];
3301 }
3302 else
cristy3ed852e2009-09-05 21:47:34 +00003303 {
3304 double
3305 factor;
3306
3307 /*
3308 Implode the pixel.
3309 */
3310 factor=1.0;
3311 if (distance > 0.0)
3312 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/
3313 radius/2)),-amount);
cristy76f512e2011-09-12 01:26:56 +00003314 status=InterpolatePixelChannels(image,image_view,implode_image,method,
3315 (double) (factor*delta.x/scale.x+center.x),(double) (factor*delta.y/
3316 scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00003317 }
cristy6d188022011-09-12 13:23:33 +00003318 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003319 q+=GetPixelChannels(implode_image);
cristy3ed852e2009-09-05 21:47:34 +00003320 }
3321 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3322 status=MagickFalse;
3323 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3324 {
3325 MagickBooleanType
3326 proceed;
3327
cristyb5d5f722009-11-04 03:03:49 +00003328#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003329 #pragma omp critical (MagickCore_ImplodeImage)
3330#endif
3331 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3332 if (proceed == MagickFalse)
3333 status=MagickFalse;
3334 }
3335 }
3336 implode_view=DestroyCacheView(implode_view);
3337 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003338 if (status == MagickFalse)
3339 implode_image=DestroyImage(implode_image);
3340 return(implode_image);
3341}
3342
3343/*
3344%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3345% %
3346% %
3347% %
3348% M o r p h I m a g e s %
3349% %
3350% %
3351% %
3352%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3353%
3354% The MorphImages() method requires a minimum of two images. The first
3355% image is transformed into the second by a number of intervening images
3356% as specified by frames.
3357%
3358% The format of the MorphImage method is:
3359%
cristybb503372010-05-27 20:51:26 +00003360% Image *MorphImages(const Image *image,const size_t number_frames,
cristy3ed852e2009-09-05 21:47:34 +00003361% ExceptionInfo *exception)
3362%
3363% A description of each parameter follows:
3364%
3365% o image: the image.
3366%
3367% o number_frames: Define the number of in-between image to generate.
3368% The more in-between frames, the smoother the morph.
3369%
3370% o exception: return any errors or warnings in this structure.
3371%
3372*/
3373MagickExport Image *MorphImages(const Image *image,
cristybb503372010-05-27 20:51:26 +00003374 const size_t number_frames,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003375{
3376#define MorphImageTag "Morph/Image"
3377
3378 Image
3379 *morph_image,
3380 *morph_images;
3381
cristy9d314ff2011-03-09 01:30:28 +00003382 MagickBooleanType
3383 status;
cristy3ed852e2009-09-05 21:47:34 +00003384
3385 MagickOffsetType
3386 scene;
3387
3388 MagickRealType
3389 alpha,
3390 beta;
3391
3392 register const Image
3393 *next;
3394
cristybb503372010-05-27 20:51:26 +00003395 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003396 i;
3397
cristy9d314ff2011-03-09 01:30:28 +00003398 ssize_t
3399 y;
cristy3ed852e2009-09-05 21:47:34 +00003400
3401 /*
3402 Clone first frame in sequence.
3403 */
3404 assert(image != (Image *) NULL);
3405 assert(image->signature == MagickSignature);
3406 if (image->debug != MagickFalse)
3407 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3408 assert(exception != (ExceptionInfo *) NULL);
3409 assert(exception->signature == MagickSignature);
3410 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3411 if (morph_images == (Image *) NULL)
3412 return((Image *) NULL);
3413 if (GetNextImageInList(image) == (Image *) NULL)
3414 {
3415 /*
3416 Morph single image.
3417 */
cristybb503372010-05-27 20:51:26 +00003418 for (i=1; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003419 {
3420 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3421 if (morph_image == (Image *) NULL)
3422 {
3423 morph_images=DestroyImageList(morph_images);
3424 return((Image *) NULL);
3425 }
3426 AppendImageToList(&morph_images,morph_image);
cristy8b27a6d2010-02-14 03:31:15 +00003427 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003428 {
cristy8b27a6d2010-02-14 03:31:15 +00003429 MagickBooleanType
3430 proceed;
3431
cristybb503372010-05-27 20:51:26 +00003432 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3433 number_frames);
cristy8b27a6d2010-02-14 03:31:15 +00003434 if (proceed == MagickFalse)
3435 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00003436 }
3437 }
3438 return(GetFirstImageInList(morph_images));
3439 }
3440 /*
3441 Morph image sequence.
3442 */
3443 status=MagickTrue;
3444 scene=0;
3445 next=image;
3446 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3447 {
cristybb503372010-05-27 20:51:26 +00003448 for (i=0; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003449 {
3450 CacheView
3451 *image_view,
3452 *morph_view;
3453
3454 beta=(MagickRealType) (i+1.0)/(MagickRealType) (number_frames+1.0);
3455 alpha=1.0-beta;
cristy15b98cd2010-09-12 19:42:50 +00003456 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
cristybb503372010-05-27 20:51:26 +00003457 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*
cristy15b98cd2010-09-12 19:42:50 +00003458 next->rows+beta*GetNextImageInList(next)->rows+0.5),
3459 next->filter,next->blur,exception);
cristy3ed852e2009-09-05 21:47:34 +00003460 if (morph_image == (Image *) NULL)
3461 {
3462 morph_images=DestroyImageList(morph_images);
3463 return((Image *) NULL);
3464 }
cristy574cc262011-08-05 01:23:58 +00003465 if (SetImageStorageClass(morph_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003466 {
cristy3ed852e2009-09-05 21:47:34 +00003467 morph_image=DestroyImage(morph_image);
3468 return((Image *) NULL);
3469 }
3470 AppendImageToList(&morph_images,morph_image);
3471 morph_images=GetLastImageInList(morph_images);
cristy15b98cd2010-09-12 19:42:50 +00003472 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
3473 morph_images->rows,GetNextImageInList(next)->filter,
3474 GetNextImageInList(next)->blur,exception);
cristy3ed852e2009-09-05 21:47:34 +00003475 if (morph_image == (Image *) NULL)
3476 {
3477 morph_images=DestroyImageList(morph_images);
3478 return((Image *) NULL);
3479 }
3480 image_view=AcquireCacheView(morph_image);
3481 morph_view=AcquireCacheView(morph_images);
cristyb5d5f722009-11-04 03:03:49 +00003482#if defined(MAGICKCORE_OPENMP_SUPPORT)
3483 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +00003484#endif
cristybb503372010-05-27 20:51:26 +00003485 for (y=0; y < (ssize_t) morph_images->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003486 {
3487 MagickBooleanType
3488 sync;
3489
cristy4c08aed2011-07-01 19:47:50 +00003490 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003491 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003492
cristybb503372010-05-27 20:51:26 +00003493 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003494 x;
3495
cristy4c08aed2011-07-01 19:47:50 +00003496 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003497 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003498
3499 if (status == MagickFalse)
3500 continue;
3501 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3502 exception);
3503 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3504 exception);
cristy4c08aed2011-07-01 19:47:50 +00003505 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003506 {
3507 status=MagickFalse;
3508 continue;
3509 }
cristybb503372010-05-27 20:51:26 +00003510 for (x=0; x < (ssize_t) morph_images->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003511 {
cristy4c08aed2011-07-01 19:47:50 +00003512 SetPixelRed(morph_images,ClampToQuantum(alpha*
3513 GetPixelRed(morph_images,q)+beta*GetPixelRed(morph_image,p)),q);
3514 SetPixelGreen(morph_images,ClampToQuantum(alpha*
3515 GetPixelGreen(morph_images,q)+beta*GetPixelGreen(morph_image,p)),q);
3516 SetPixelBlue(morph_images,ClampToQuantum(alpha*
3517 GetPixelBlue(morph_images,q)+beta*GetPixelBlue(morph_image,p)),q);
3518 SetPixelAlpha(morph_images,ClampToQuantum(alpha*
3519 GetPixelAlpha(morph_images,q)+beta*GetPixelAlpha(morph_image,p)),q);
3520 if ((morph_image->colorspace == CMYKColorspace) &&
3521 (morph_images->colorspace == CMYKColorspace))
3522 SetPixelBlack(morph_images,ClampToQuantum(alpha*
3523 GetPixelBlack(morph_images,q)+beta*GetPixelBlack(morph_image,p)),
3524 q);
cristyed231572011-07-14 02:18:59 +00003525 p+=GetPixelChannels(morph_image);
3526 q+=GetPixelChannels(morph_images);
cristy3ed852e2009-09-05 21:47:34 +00003527 }
3528 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3529 if (sync == MagickFalse)
3530 status=MagickFalse;
3531 }
3532 morph_view=DestroyCacheView(morph_view);
3533 image_view=DestroyCacheView(image_view);
3534 morph_image=DestroyImage(morph_image);
3535 }
cristybb503372010-05-27 20:51:26 +00003536 if (i < (ssize_t) number_frames)
cristy3ed852e2009-09-05 21:47:34 +00003537 break;
3538 /*
3539 Clone last frame in sequence.
3540 */
3541 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3542 if (morph_image == (Image *) NULL)
3543 {
3544 morph_images=DestroyImageList(morph_images);
3545 return((Image *) NULL);
3546 }
3547 AppendImageToList(&morph_images,morph_image);
3548 morph_images=GetLastImageInList(morph_images);
3549 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3550 {
3551 MagickBooleanType
3552 proceed;
3553
cristyb5d5f722009-11-04 03:03:49 +00003554#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003555 #pragma omp critical (MagickCore_MorphImages)
3556#endif
3557 proceed=SetImageProgress(image,MorphImageTag,scene,
3558 GetImageListLength(image));
3559 if (proceed == MagickFalse)
3560 status=MagickFalse;
3561 }
3562 scene++;
3563 }
3564 if (GetNextImageInList(next) != (Image *) NULL)
3565 {
3566 morph_images=DestroyImageList(morph_images);
3567 return((Image *) NULL);
3568 }
3569 return(GetFirstImageInList(morph_images));
3570}
3571
3572/*
3573%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3574% %
3575% %
3576% %
3577% P l a s m a I m a g e %
3578% %
3579% %
3580% %
3581%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3582%
3583% PlasmaImage() initializes an image with plasma fractal values. The image
3584% must be initialized with a base color and the random number generator
3585% seeded before this method is called.
3586%
3587% The format of the PlasmaImage method is:
3588%
3589% MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
cristy5cbc0162011-08-29 00:36:28 +00003590% size_t attenuate,size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003591%
3592% A description of each parameter follows:
3593%
3594% o image: the image.
3595%
3596% o segment: Define the region to apply plasma fractals values.
3597%
glennrp7dae1ca2010-09-16 12:17:35 +00003598% o attenuate: Define the plasma attenuation factor.
cristy3ed852e2009-09-05 21:47:34 +00003599%
3600% o depth: Limit the plasma recursion depth.
3601%
cristy5cbc0162011-08-29 00:36:28 +00003602% o exception: return any errors or warnings in this structure.
3603%
cristy3ed852e2009-09-05 21:47:34 +00003604*/
3605
3606static inline Quantum PlasmaPixel(RandomInfo *random_info,
3607 const MagickRealType pixel,const MagickRealType noise)
3608{
3609 Quantum
3610 plasma;
3611
cristyce70c172010-01-07 17:15:30 +00003612 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
cristy3ed852e2009-09-05 21:47:34 +00003613 noise/2.0);
3614 return(plasma);
3615}
3616
cristyda1f9c12011-10-02 21:39:49 +00003617static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3618 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3619 const SegmentInfo *segment,size_t attenuate,size_t depth,
3620 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003621{
cristy3ed852e2009-09-05 21:47:34 +00003622 MagickRealType
3623 plasma;
3624
cristyda1f9c12011-10-02 21:39:49 +00003625 PixelChannel
3626 channel;
3627
3628 PixelTrait
3629 traits;
3630
3631 register const Quantum
3632 *restrict u,
3633 *restrict v;
3634
3635 register Quantum
3636 *restrict q;
3637
3638 register ssize_t
3639 i;
cristy3ed852e2009-09-05 21:47:34 +00003640
cristy9d314ff2011-03-09 01:30:28 +00003641 ssize_t
3642 x,
3643 x_mid,
3644 y,
3645 y_mid;
3646
cristy3ed852e2009-09-05 21:47:34 +00003647 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3648 return(MagickTrue);
3649 if (depth != 0)
3650 {
3651 SegmentInfo
3652 local_info;
3653
3654 /*
3655 Divide the area into quadrants and recurse.
3656 */
3657 depth--;
3658 attenuate++;
cristybb503372010-05-27 20:51:26 +00003659 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3660 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003661 local_info=(*segment);
3662 local_info.x2=(double) x_mid;
3663 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003664 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3665 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003666 local_info=(*segment);
3667 local_info.y1=(double) y_mid;
3668 local_info.x2=(double) x_mid;
cristyda1f9c12011-10-02 21:39:49 +00003669 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3670 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003671 local_info=(*segment);
3672 local_info.x1=(double) x_mid;
3673 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003674 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3675 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003676 local_info=(*segment);
3677 local_info.x1=(double) x_mid;
3678 local_info.y1=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003679 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3680 &local_info,attenuate,depth,exception));
cristy3ed852e2009-09-05 21:47:34 +00003681 }
cristybb503372010-05-27 20:51:26 +00003682 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3683 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003684 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3685 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3686 return(MagickFalse);
3687 /*
3688 Average pixels and apply plasma.
3689 */
cristy3ed852e2009-09-05 21:47:34 +00003690 plasma=(MagickRealType) QuantumRange/(2.0*attenuate);
3691 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3692 {
cristy3ed852e2009-09-05 21:47:34 +00003693 /*
3694 Left pixel.
3695 */
cristybb503372010-05-27 20:51:26 +00003696 x=(ssize_t) ceil(segment->x1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003697 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3698 1,1,exception);
3699 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3700 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003701 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003702 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3703 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003704 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003705 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3706 {
3707 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3708 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3709 if (traits == UndefinedPixelTrait)
3710 continue;
3711 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3712 }
cristyc5c6f662010-09-22 14:23:02 +00003713 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003714 if (segment->x1 != segment->x2)
3715 {
3716 /*
3717 Right pixel.
3718 */
cristybb503372010-05-27 20:51:26 +00003719 x=(ssize_t) ceil(segment->x2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003720 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3721 1,1,exception);
3722 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3723 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003724 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003725 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3726 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003727 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003728 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3729 {
3730 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3731 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3732 if (traits == UndefinedPixelTrait)
3733 continue;
3734 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3735 }
cristyc5c6f662010-09-22 14:23:02 +00003736 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003737 }
3738 }
3739 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3740 {
3741 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3742 {
cristy3ed852e2009-09-05 21:47:34 +00003743 /*
3744 Bottom pixel.
3745 */
cristybb503372010-05-27 20:51:26 +00003746 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003747 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3748 1,1,exception);
3749 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3750 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003751 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003752 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3753 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003754 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003755 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3756 {
3757 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3758 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3759 if (traits == UndefinedPixelTrait)
3760 continue;
3761 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3762 }
cristyc5c6f662010-09-22 14:23:02 +00003763 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003764 }
3765 if (segment->y1 != segment->y2)
3766 {
cristy3ed852e2009-09-05 21:47:34 +00003767 /*
3768 Top pixel.
3769 */
cristybb503372010-05-27 20:51:26 +00003770 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003771 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3772 1,1,exception);
3773 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3774 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003775 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003776 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3777 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003778 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003779 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3780 {
3781 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3782 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3783 if (traits == UndefinedPixelTrait)
3784 continue;
3785 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3786 }
cristyc5c6f662010-09-22 14:23:02 +00003787 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003788 }
3789 }
3790 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3791 {
cristy3ed852e2009-09-05 21:47:34 +00003792 /*
3793 Middle pixel.
3794 */
cristybb503372010-05-27 20:51:26 +00003795 x=(ssize_t) ceil(segment->x1-0.5);
3796 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003797 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
cristybb503372010-05-27 20:51:26 +00003798 x=(ssize_t) ceil(segment->x2-0.5);
3799 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003800 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003801 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003802 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3803 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003804 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003805 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3806 {
3807 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3808 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3809 if (traits == UndefinedPixelTrait)
3810 continue;
3811 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3812 }
cristyc5c6f662010-09-22 14:23:02 +00003813 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003814 }
3815 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3816 return(MagickTrue);
3817 return(MagickFalse);
3818}
cristyda1f9c12011-10-02 21:39:49 +00003819
cristy3ed852e2009-09-05 21:47:34 +00003820MagickExport MagickBooleanType PlasmaImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00003821 const SegmentInfo *segment,size_t attenuate,size_t depth,
3822 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003823{
cristyc5c6f662010-09-22 14:23:02 +00003824 CacheView
cristyda1f9c12011-10-02 21:39:49 +00003825 *image_view,
3826 *u_view,
3827 *v_view;
cristyc5c6f662010-09-22 14:23:02 +00003828
cristy3ed852e2009-09-05 21:47:34 +00003829 MagickBooleanType
3830 status;
3831
3832 RandomInfo
3833 *random_info;
3834
3835 if (image->debug != MagickFalse)
3836 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3837 assert(image != (Image *) NULL);
3838 assert(image->signature == MagickSignature);
3839 if (image->debug != MagickFalse)
3840 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy5cbc0162011-08-29 00:36:28 +00003841 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristyc5c6f662010-09-22 14:23:02 +00003842 return(MagickFalse);
3843 image_view=AcquireCacheView(image);
cristyda1f9c12011-10-02 21:39:49 +00003844 u_view=AcquireCacheView(image);
3845 v_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00003846 random_info=AcquireRandomInfo();
cristyda1f9c12011-10-02 21:39:49 +00003847 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3848 attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003849 random_info=DestroyRandomInfo(random_info);
cristyda1f9c12011-10-02 21:39:49 +00003850 v_view=DestroyCacheView(v_view);
3851 u_view=DestroyCacheView(u_view);
cristyc5c6f662010-09-22 14:23:02 +00003852 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003853 return(status);
3854}
3855
3856/*
3857%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3858% %
3859% %
3860% %
3861% P o l a r o i d I m a g e %
3862% %
3863% %
3864% %
3865%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3866%
3867% PolaroidImage() simulates a Polaroid picture.
3868%
3869% The format of the AnnotateImage method is:
3870%
3871% Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristy5c4e2582011-09-11 19:21:03 +00003872% const double angle,const PixelInterpolateMethod method,
3873% ExceptionInfo exception)
cristy3ed852e2009-09-05 21:47:34 +00003874%
3875% A description of each parameter follows:
3876%
3877% o image: the image.
3878%
3879% o draw_info: the draw info.
3880%
cristycee97112010-05-28 00:44:52 +00003881% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00003882%
cristy5c4e2582011-09-11 19:21:03 +00003883% o method: the pixel interpolation method.
3884%
cristy3ed852e2009-09-05 21:47:34 +00003885% o exception: return any errors or warnings in this structure.
3886%
3887*/
3888MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristy5c4e2582011-09-11 19:21:03 +00003889 const double angle,const PixelInterpolateMethod method,
3890 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003891{
3892 const char
3893 *value;
3894
cristy3ed852e2009-09-05 21:47:34 +00003895 Image
3896 *bend_image,
3897 *caption_image,
3898 *flop_image,
3899 *picture_image,
3900 *polaroid_image,
3901 *rotate_image,
3902 *trim_image;
3903
cristybb503372010-05-27 20:51:26 +00003904 size_t
cristy3ed852e2009-09-05 21:47:34 +00003905 height;
3906
cristy9d314ff2011-03-09 01:30:28 +00003907 ssize_t
3908 quantum;
3909
cristy3ed852e2009-09-05 21:47:34 +00003910 /*
3911 Simulate a Polaroid picture.
3912 */
3913 assert(image != (Image *) NULL);
3914 assert(image->signature == MagickSignature);
3915 if (image->debug != MagickFalse)
3916 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3917 assert(exception != (ExceptionInfo *) NULL);
3918 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00003919 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
cristy3ed852e2009-09-05 21:47:34 +00003920 image->rows)/25.0,10.0);
3921 height=image->rows+2*quantum;
3922 caption_image=(Image *) NULL;
cristyd15e6592011-10-15 00:13:06 +00003923 value=GetImageProperty(image,"caption",exception);
cristy3ed852e2009-09-05 21:47:34 +00003924 if (value != (const char *) NULL)
3925 {
3926 char
3927 *caption,
3928 geometry[MaxTextExtent];
3929
3930 DrawInfo
3931 *annotate_info;
3932
cristy3ed852e2009-09-05 21:47:34 +00003933 MagickBooleanType
3934 status;
3935
cristy9d314ff2011-03-09 01:30:28 +00003936 ssize_t
3937 count;
3938
cristy3ed852e2009-09-05 21:47:34 +00003939 TypeMetric
3940 metrics;
3941
3942 /*
3943 Generate caption image.
3944 */
3945 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3946 if (caption_image == (Image *) NULL)
3947 return((Image *) NULL);
3948 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
3949 caption=InterpretImageProperties((ImageInfo *) NULL,(Image *) image,
cristy018f07f2011-09-04 21:15:19 +00003950 value,exception);
cristy3ed852e2009-09-05 21:47:34 +00003951 (void) CloneString(&annotate_info->text,caption);
cristy6b1d05e2010-09-22 19:17:27 +00003952 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
cristy5cbc0162011-08-29 00:36:28 +00003953 &caption,exception);
cristybb503372010-05-27 20:51:26 +00003954 status=SetImageExtent(caption_image,image->columns,(size_t)
cristy63240882011-08-05 19:05:27 +00003955 ((count+1)*(metrics.ascent-metrics.descent)+0.5),exception);
cristy3ed852e2009-09-05 21:47:34 +00003956 if (status == MagickFalse)
3957 caption_image=DestroyImage(caption_image);
3958 else
3959 {
3960 caption_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003961 (void) SetImageBackgroundColor(caption_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003962 (void) CloneString(&annotate_info->text,caption);
cristyb51dff52011-05-19 16:55:47 +00003963 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
cristy3ed852e2009-09-05 21:47:34 +00003964 metrics.ascent);
3965 if (annotate_info->gravity == UndefinedGravity)
3966 (void) CloneString(&annotate_info->geometry,AcquireString(
3967 geometry));
cristy5cbc0162011-08-29 00:36:28 +00003968 (void) AnnotateImage(caption_image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003969 height+=caption_image->rows;
3970 }
3971 annotate_info=DestroyDrawInfo(annotate_info);
3972 caption=DestroyString(caption);
3973 }
3974 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
3975 exception);
3976 if (picture_image == (Image *) NULL)
3977 {
3978 if (caption_image != (Image *) NULL)
3979 caption_image=DestroyImage(caption_image);
3980 return((Image *) NULL);
3981 }
3982 picture_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003983 (void) SetImageBackgroundColor(picture_image,exception);
cristye941a752011-10-15 01:52:48 +00003984 (void) CompositeImage(picture_image,OverCompositeOp,image,quantum,quantum,
3985 exception);
cristy3ed852e2009-09-05 21:47:34 +00003986 if (caption_image != (Image *) NULL)
3987 {
3988 (void) CompositeImage(picture_image,OverCompositeOp,caption_image,
cristye941a752011-10-15 01:52:48 +00003989 quantum,(ssize_t) (image->rows+3*quantum/2),exception);
cristy3ed852e2009-09-05 21:47:34 +00003990 caption_image=DestroyImage(caption_image);
3991 }
cristy9950d572011-10-01 18:22:35 +00003992 (void) QueryColorCompliance("none",AllCompliance,
3993 &picture_image->background_color,exception);
cristy63240882011-08-05 19:05:27 +00003994 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00003995 rotate_image=RotateImage(picture_image,90.0,exception);
3996 picture_image=DestroyImage(picture_image);
3997 if (rotate_image == (Image *) NULL)
3998 return((Image *) NULL);
3999 picture_image=rotate_image;
4000 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
cristy5c4e2582011-09-11 19:21:03 +00004001 picture_image->columns,method,exception);
cristy3ed852e2009-09-05 21:47:34 +00004002 picture_image=DestroyImage(picture_image);
4003 if (bend_image == (Image *) NULL)
4004 return((Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00004005 picture_image=bend_image;
4006 rotate_image=RotateImage(picture_image,-90.0,exception);
4007 picture_image=DestroyImage(picture_image);
4008 if (rotate_image == (Image *) NULL)
4009 return((Image *) NULL);
4010 picture_image=rotate_image;
4011 picture_image->background_color=image->background_color;
4012 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
4013 exception);
4014 if (polaroid_image == (Image *) NULL)
4015 {
4016 picture_image=DestroyImage(picture_image);
4017 return(picture_image);
4018 }
4019 flop_image=FlopImage(polaroid_image,exception);
4020 polaroid_image=DestroyImage(polaroid_image);
4021 if (flop_image == (Image *) NULL)
4022 {
4023 picture_image=DestroyImage(picture_image);
4024 return(picture_image);
4025 }
4026 polaroid_image=flop_image;
4027 (void) CompositeImage(polaroid_image,OverCompositeOp,picture_image,
cristye941a752011-10-15 01:52:48 +00004028 (ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
cristy3ed852e2009-09-05 21:47:34 +00004029 picture_image=DestroyImage(picture_image);
cristy9950d572011-10-01 18:22:35 +00004030 (void) QueryColorCompliance("none",AllCompliance,
4031 &polaroid_image->background_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00004032 rotate_image=RotateImage(polaroid_image,angle,exception);
4033 polaroid_image=DestroyImage(polaroid_image);
4034 if (rotate_image == (Image *) NULL)
4035 return((Image *) NULL);
4036 polaroid_image=rotate_image;
4037 trim_image=TrimImage(polaroid_image,exception);
4038 polaroid_image=DestroyImage(polaroid_image);
4039 if (trim_image == (Image *) NULL)
4040 return((Image *) NULL);
4041 polaroid_image=trim_image;
4042 return(polaroid_image);
4043}
4044
4045/*
4046%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4047% %
4048% %
4049% %
cristy3ed852e2009-09-05 21:47:34 +00004050% S e p i a T o n e I m a g e %
4051% %
4052% %
4053% %
4054%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4055%
4056% MagickSepiaToneImage() applies a special effect to the image, similar to the
4057% effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4058% 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4059% threshold of 80% is a good starting point for a reasonable tone.
4060%
4061% The format of the SepiaToneImage method is:
4062%
4063% Image *SepiaToneImage(const Image *image,const double threshold,
4064% ExceptionInfo *exception)
4065%
4066% A description of each parameter follows:
4067%
4068% o image: the image.
4069%
4070% o threshold: the tone threshold.
4071%
4072% o exception: return any errors or warnings in this structure.
4073%
4074*/
4075MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4076 ExceptionInfo *exception)
4077{
4078#define SepiaToneImageTag "SepiaTone/Image"
4079
cristyc4c8d132010-01-07 01:58:38 +00004080 CacheView
4081 *image_view,
4082 *sepia_view;
4083
cristy3ed852e2009-09-05 21:47:34 +00004084 Image
4085 *sepia_image;
4086
cristy3ed852e2009-09-05 21:47:34 +00004087 MagickBooleanType
4088 status;
4089
cristybb503372010-05-27 20:51:26 +00004090 MagickOffsetType
4091 progress;
4092
4093 ssize_t
4094 y;
4095
cristy3ed852e2009-09-05 21:47:34 +00004096 /*
4097 Initialize sepia-toned image attributes.
4098 */
4099 assert(image != (const Image *) NULL);
4100 assert(image->signature == MagickSignature);
4101 if (image->debug != MagickFalse)
4102 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4103 assert(exception != (ExceptionInfo *) NULL);
4104 assert(exception->signature == MagickSignature);
4105 sepia_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
4106 if (sepia_image == (Image *) NULL)
4107 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004108 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004109 {
cristy3ed852e2009-09-05 21:47:34 +00004110 sepia_image=DestroyImage(sepia_image);
4111 return((Image *) NULL);
4112 }
4113 /*
4114 Tone each row of the image.
4115 */
4116 status=MagickTrue;
4117 progress=0;
4118 image_view=AcquireCacheView(image);
4119 sepia_view=AcquireCacheView(sepia_image);
cristyb5d5f722009-11-04 03:03:49 +00004120#if defined(MAGICKCORE_OPENMP_SUPPORT)
4121 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004122#endif
cristybb503372010-05-27 20:51:26 +00004123 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004124 {
cristy4c08aed2011-07-01 19:47:50 +00004125 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004126 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004127
cristybb503372010-05-27 20:51:26 +00004128 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004129 x;
4130
cristy4c08aed2011-07-01 19:47:50 +00004131 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004132 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004133
4134 if (status == MagickFalse)
4135 continue;
4136 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
4137 q=QueueCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
4138 exception);
cristy4c08aed2011-07-01 19:47:50 +00004139 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004140 {
4141 status=MagickFalse;
4142 continue;
4143 }
cristybb503372010-05-27 20:51:26 +00004144 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004145 {
4146 MagickRealType
4147 intensity,
4148 tone;
4149
cristy4c08aed2011-07-01 19:47:50 +00004150 intensity=(MagickRealType) GetPixelIntensity(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004151 tone=intensity > threshold ? (MagickRealType) QuantumRange : intensity+
4152 (MagickRealType) QuantumRange-threshold;
cristy4c08aed2011-07-01 19:47:50 +00004153 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004154 tone=intensity > (7.0*threshold/6.0) ? (MagickRealType) QuantumRange :
4155 intensity+(MagickRealType) QuantumRange-7.0*threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004156 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004157 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004158 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004159 tone=threshold/7.0;
cristy4c08aed2011-07-01 19:47:50 +00004160 if ((MagickRealType) GetPixelGreen(image,q) < tone)
4161 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4162 if ((MagickRealType) GetPixelBlue(image,q) < tone)
4163 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristyed231572011-07-14 02:18:59 +00004164 p+=GetPixelChannels(image);
4165 q+=GetPixelChannels(sepia_image);
cristy3ed852e2009-09-05 21:47:34 +00004166 }
4167 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4168 status=MagickFalse;
4169 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4170 {
4171 MagickBooleanType
4172 proceed;
4173
cristyb5d5f722009-11-04 03:03:49 +00004174#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00004175 #pragma omp critical (MagickCore_SepiaToneImage)
4176#endif
4177 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4178 image->rows);
4179 if (proceed == MagickFalse)
4180 status=MagickFalse;
4181 }
4182 }
4183 sepia_view=DestroyCacheView(sepia_view);
4184 image_view=DestroyCacheView(image_view);
cristye23ec9d2011-08-16 18:15:40 +00004185 (void) NormalizeImage(sepia_image,exception);
4186 (void) ContrastImage(sepia_image,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004187 if (status == MagickFalse)
4188 sepia_image=DestroyImage(sepia_image);
4189 return(sepia_image);
4190}
4191
4192/*
4193%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4194% %
4195% %
4196% %
4197% S h a d o w I m a g e %
4198% %
4199% %
4200% %
4201%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4202%
4203% ShadowImage() simulates a shadow from the specified image and returns it.
4204%
4205% The format of the ShadowImage method is:
4206%
4207% Image *ShadowImage(const Image *image,const double opacity,
cristybb503372010-05-27 20:51:26 +00004208% const double sigma,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004209% ExceptionInfo *exception)
4210%
4211% A description of each parameter follows:
4212%
4213% o image: the image.
4214%
4215% o opacity: percentage transparency.
4216%
4217% o sigma: the standard deviation of the Gaussian, in pixels.
4218%
4219% o x_offset: the shadow x-offset.
4220%
4221% o y_offset: the shadow y-offset.
4222%
4223% o exception: return any errors or warnings in this structure.
4224%
4225*/
4226MagickExport Image *ShadowImage(const Image *image,const double opacity,
cristybb503372010-05-27 20:51:26 +00004227 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004228 ExceptionInfo *exception)
4229{
4230#define ShadowImageTag "Shadow/Image"
4231
cristybd5a96c2011-08-21 00:04:26 +00004232 ChannelType
4233 channel_mask;
4234
cristy3ed852e2009-09-05 21:47:34 +00004235 Image
4236 *border_image,
4237 *clone_image,
4238 *shadow_image;
4239
cristy3ed852e2009-09-05 21:47:34 +00004240 RectangleInfo
4241 border_info;
4242
cristy3ed852e2009-09-05 21:47:34 +00004243 assert(image != (Image *) NULL);
4244 assert(image->signature == MagickSignature);
4245 if (image->debug != MagickFalse)
4246 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4247 assert(exception != (ExceptionInfo *) NULL);
4248 assert(exception->signature == MagickSignature);
4249 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4250 if (clone_image == (Image *) NULL)
4251 return((Image *) NULL);
4252 (void) SetImageVirtualPixelMethod(clone_image,EdgeVirtualPixelMethod);
4253 clone_image->compose=OverCompositeOp;
cristybb503372010-05-27 20:51:26 +00004254 border_info.width=(size_t) floor(2.0*sigma+0.5);
4255 border_info.height=(size_t) floor(2.0*sigma+0.5);
cristy3ed852e2009-09-05 21:47:34 +00004256 border_info.x=0;
4257 border_info.y=0;
cristy9950d572011-10-01 18:22:35 +00004258 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4259 exception);
cristy633f0c62011-09-15 13:27:36 +00004260 border_image=BorderImage(clone_image,&border_info,image->compose,exception);
cristy3ed852e2009-09-05 21:47:34 +00004261 clone_image=DestroyImage(clone_image);
4262 if (border_image == (Image *) NULL)
4263 return((Image *) NULL);
4264 if (border_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00004265 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004266 /*
4267 Shadow image.
4268 */
cristyea1a8aa2011-10-20 13:24:06 +00004269 (void) SetImageBackgroundColor(border_image,exception);
cristybd5a96c2011-08-21 00:04:26 +00004270 channel_mask=SetPixelChannelMask(border_image,AlphaChannel);
cristy05c0c9a2011-09-05 23:16:13 +00004271 shadow_image=BlurImage(border_image,0.0,sigma,image->bias,exception);
cristybd5a96c2011-08-21 00:04:26 +00004272 (void) SetPixelChannelMap(border_image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00004273 border_image=DestroyImage(border_image);
4274 if (shadow_image == (Image *) NULL)
4275 return((Image *) NULL);
4276 if (shadow_image->page.width == 0)
4277 shadow_image->page.width=shadow_image->columns;
4278 if (shadow_image->page.height == 0)
4279 shadow_image->page.height=shadow_image->rows;
cristybb503372010-05-27 20:51:26 +00004280 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4281 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4282 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4283 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
cristy3ed852e2009-09-05 21:47:34 +00004284 return(shadow_image);
4285}
4286
4287/*
4288%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4289% %
4290% %
4291% %
4292% S k e t c h I m a g e %
4293% %
4294% %
4295% %
4296%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4297%
4298% SketchImage() simulates a pencil sketch. We convolve the image with a
4299% Gaussian operator of the given radius and standard deviation (sigma). For
4300% reasonable results, radius should be larger than sigma. Use a radius of 0
4301% and SketchImage() selects a suitable radius for you. Angle gives the angle
4302% of the sketch.
4303%
4304% The format of the SketchImage method is:
4305%
4306% Image *SketchImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00004307% const double sigma,const double angle,const double bias,
4308% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004309%
4310% A description of each parameter follows:
4311%
4312% o image: the image.
4313%
cristy574cc262011-08-05 01:23:58 +00004314% o radius: the radius of the Gaussian, in pixels, not counting the
4315% center pixel.
cristy3ed852e2009-09-05 21:47:34 +00004316%
4317% o sigma: the standard deviation of the Gaussian, in pixels.
4318%
cristyf7ef0252011-09-09 14:50:06 +00004319% o angle: apply the effect along this angle.
4320%
4321% o bias: the bias.
cristy3ed852e2009-09-05 21:47:34 +00004322%
4323% o exception: return any errors or warnings in this structure.
4324%
4325*/
4326MagickExport Image *SketchImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00004327 const double sigma,const double angle,const double bias,
4328 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004329{
cristyfa112112010-01-04 17:48:07 +00004330 CacheView
4331 *random_view;
4332
cristy3ed852e2009-09-05 21:47:34 +00004333 Image
4334 *blend_image,
4335 *blur_image,
4336 *dodge_image,
4337 *random_image,
4338 *sketch_image;
4339
cristy3ed852e2009-09-05 21:47:34 +00004340 MagickBooleanType
4341 status;
4342
cristy3ed852e2009-09-05 21:47:34 +00004343 RandomInfo
cristyfa112112010-01-04 17:48:07 +00004344 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00004345
cristy9d314ff2011-03-09 01:30:28 +00004346 ssize_t
4347 y;
4348
cristy3ed852e2009-09-05 21:47:34 +00004349 /*
4350 Sketch image.
4351 */
4352 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4353 MagickTrue,exception);
4354 if (random_image == (Image *) NULL)
4355 return((Image *) NULL);
4356 status=MagickTrue;
cristy1b784432009-12-19 02:20:40 +00004357 random_info=AcquireRandomInfoThreadSet();
cristy3ed852e2009-09-05 21:47:34 +00004358 random_view=AcquireCacheView(random_image);
cristy1b784432009-12-19 02:20:40 +00004359#if defined(MAGICKCORE_OPENMP_SUPPORT)
4360 #pragma omp parallel for schedule(dynamic,4) shared(status)
4361#endif
cristybb503372010-05-27 20:51:26 +00004362 for (y=0; y < (ssize_t) random_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004363 {
cristy5c9e6f22010-09-17 17:31:01 +00004364 const int
4365 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00004366
cristybb503372010-05-27 20:51:26 +00004367 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004368 x;
4369
cristy4c08aed2011-07-01 19:47:50 +00004370 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004371 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004372
cristy1b784432009-12-19 02:20:40 +00004373 if (status == MagickFalse)
4374 continue;
cristy3ed852e2009-09-05 21:47:34 +00004375 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4376 exception);
cristyacd2ed22011-08-30 01:44:23 +00004377 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004378 {
4379 status=MagickFalse;
4380 continue;
4381 }
cristybb503372010-05-27 20:51:26 +00004382 for (x=0; x < (ssize_t) random_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004383 {
cristy76f512e2011-09-12 01:26:56 +00004384 MagickRealType
4385 value;
4386
4387 register ssize_t
4388 i;
4389
4390 value=GetPseudoRandomValue(random_info[id]);
4391 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4392 {
4393 PixelTrait
4394 traits;
4395
4396 traits=GetPixelChannelMapTraits(random_image,(PixelChannel) i);
4397 if (traits == UndefinedPixelTrait)
4398 continue;
4399 q[i]=ClampToQuantum(QuantumRange*value);
4400 }
cristyed231572011-07-14 02:18:59 +00004401 q+=GetPixelChannels(random_image);
cristy3ed852e2009-09-05 21:47:34 +00004402 }
4403 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4404 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004405 }
4406 random_view=DestroyCacheView(random_view);
cristy1b784432009-12-19 02:20:40 +00004407 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00004408 if (status == MagickFalse)
4409 {
4410 random_image=DestroyImage(random_image);
4411 return(random_image);
4412 }
cristyf7ef0252011-09-09 14:50:06 +00004413 blur_image=MotionBlurImage(random_image,radius,sigma,angle,bias,exception);
cristy3ed852e2009-09-05 21:47:34 +00004414 random_image=DestroyImage(random_image);
4415 if (blur_image == (Image *) NULL)
4416 return((Image *) NULL);
cristy8ae632d2011-09-05 17:29:53 +00004417 dodge_image=EdgeImage(blur_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00004418 blur_image=DestroyImage(blur_image);
4419 if (dodge_image == (Image *) NULL)
4420 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +00004421 (void) NormalizeImage(dodge_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +00004422 (void) NegateImage(dodge_image,MagickFalse,exception);
cristye941a752011-10-15 01:52:48 +00004423 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
cristy3ed852e2009-09-05 21:47:34 +00004424 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4425 if (sketch_image == (Image *) NULL)
4426 {
4427 dodge_image=DestroyImage(dodge_image);
4428 return((Image *) NULL);
4429 }
cristye941a752011-10-15 01:52:48 +00004430 (void) CompositeImage(sketch_image,ColorDodgeCompositeOp,dodge_image,0,0,
4431 exception);
cristy3ed852e2009-09-05 21:47:34 +00004432 dodge_image=DestroyImage(dodge_image);
4433 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4434 if (blend_image == (Image *) NULL)
4435 {
4436 sketch_image=DestroyImage(sketch_image);
4437 return((Image *) NULL);
4438 }
4439 (void) SetImageArtifact(blend_image,"compose:args","20x80");
cristye941a752011-10-15 01:52:48 +00004440 (void) CompositeImage(sketch_image,BlendCompositeOp,blend_image,0,0,
4441 exception);
cristy3ed852e2009-09-05 21:47:34 +00004442 blend_image=DestroyImage(blend_image);
4443 return(sketch_image);
4444}
4445
4446/*
4447%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4448% %
4449% %
4450% %
4451% S o l a r i z e I m a g e %
4452% %
4453% %
4454% %
4455%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4456%
4457% SolarizeImage() applies a special effect to the image, similar to the effect
4458% achieved in a photo darkroom by selectively exposing areas of photo
4459% sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4460% measure of the extent of the solarization.
4461%
4462% The format of the SolarizeImage method is:
4463%
cristy5cbc0162011-08-29 00:36:28 +00004464% MagickBooleanType SolarizeImage(Image *image,const double threshold,
4465% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004466%
4467% A description of each parameter follows:
4468%
4469% o image: the image.
4470%
4471% o threshold: Define the extent of the solarization.
4472%
cristy5cbc0162011-08-29 00:36:28 +00004473% o exception: return any errors or warnings in this structure.
4474%
cristy3ed852e2009-09-05 21:47:34 +00004475*/
4476MagickExport MagickBooleanType SolarizeImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00004477 const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004478{
4479#define SolarizeImageTag "Solarize/Image"
4480
cristyc4c8d132010-01-07 01:58:38 +00004481 CacheView
4482 *image_view;
4483
cristy3ed852e2009-09-05 21:47:34 +00004484 MagickBooleanType
4485 status;
4486
cristybb503372010-05-27 20:51:26 +00004487 MagickOffsetType
4488 progress;
4489
4490 ssize_t
4491 y;
4492
cristy3ed852e2009-09-05 21:47:34 +00004493 assert(image != (Image *) NULL);
4494 assert(image->signature == MagickSignature);
4495 if (image->debug != MagickFalse)
4496 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4497 if (image->storage_class == PseudoClass)
4498 {
cristybb503372010-05-27 20:51:26 +00004499 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004500 i;
4501
4502 /*
4503 Solarize colormap.
4504 */
cristybb503372010-05-27 20:51:26 +00004505 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00004506 {
4507 if ((MagickRealType) image->colormap[i].red > threshold)
4508 image->colormap[i].red=(Quantum) QuantumRange-image->colormap[i].red;
4509 if ((MagickRealType) image->colormap[i].green > threshold)
4510 image->colormap[i].green=(Quantum) QuantumRange-
4511 image->colormap[i].green;
4512 if ((MagickRealType) image->colormap[i].blue > threshold)
4513 image->colormap[i].blue=(Quantum) QuantumRange-
4514 image->colormap[i].blue;
4515 }
4516 }
4517 /*
4518 Solarize image.
4519 */
4520 status=MagickTrue;
4521 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00004522 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00004523#if defined(MAGICKCORE_OPENMP_SUPPORT)
4524 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004525#endif
cristybb503372010-05-27 20:51:26 +00004526 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004527 {
cristybb503372010-05-27 20:51:26 +00004528 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004529 x;
4530
cristy4c08aed2011-07-01 19:47:50 +00004531 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004532 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004533
4534 if (status == MagickFalse)
4535 continue;
cristy5cbc0162011-08-29 00:36:28 +00004536 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00004537 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004538 {
4539 status=MagickFalse;
4540 continue;
4541 }
cristybb503372010-05-27 20:51:26 +00004542 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004543 {
cristy76f512e2011-09-12 01:26:56 +00004544 register ssize_t
4545 i;
4546
4547 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4548 {
4549 PixelTrait
4550 traits;
4551
4552 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
4553 if ((traits == UndefinedPixelTrait) ||
4554 ((traits & CopyPixelTrait) != 0))
4555 continue;
4556 if ((MagickRealType) q[i] > threshold)
4557 q[i]=QuantumRange-q[i];
4558 }
cristyed231572011-07-14 02:18:59 +00004559 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004560 }
4561 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4562 status=MagickFalse;
4563 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4564 {
4565 MagickBooleanType
4566 proceed;
4567
cristyb5d5f722009-11-04 03:03:49 +00004568#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00004569 #pragma omp critical (MagickCore_SolarizeImage)
4570#endif
4571 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4572 if (proceed == MagickFalse)
4573 status=MagickFalse;
4574 }
4575 }
4576 image_view=DestroyCacheView(image_view);
4577 return(status);
4578}
4579
4580/*
4581%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4582% %
4583% %
4584% %
4585% S t e g a n o I m a g e %
4586% %
4587% %
4588% %
4589%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4590%
4591% SteganoImage() hides a digital watermark within the image. Recover
4592% the hidden watermark later to prove that the authenticity of an image.
4593% Offset defines the start position within the image to hide the watermark.
4594%
4595% The format of the SteganoImage method is:
4596%
4597% Image *SteganoImage(const Image *image,Image *watermark,
4598% ExceptionInfo *exception)
4599%
4600% A description of each parameter follows:
4601%
4602% o image: the image.
4603%
4604% o watermark: the watermark image.
4605%
4606% o exception: return any errors or warnings in this structure.
4607%
4608*/
4609MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4610 ExceptionInfo *exception)
4611{
cristye1bf8ad2010-09-19 17:07:03 +00004612#define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
cristy4c08aed2011-07-01 19:47:50 +00004613#define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
cristyeaedf062010-05-29 22:36:02 +00004614 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
cristy3ed852e2009-09-05 21:47:34 +00004615#define SteganoImageTag "Stegano/Image"
4616
cristyb0d3bb92010-09-22 14:37:58 +00004617 CacheView
4618 *stegano_view,
4619 *watermark_view;
4620
cristy3ed852e2009-09-05 21:47:34 +00004621 Image
4622 *stegano_image;
4623
4624 int
4625 c;
4626
cristy3ed852e2009-09-05 21:47:34 +00004627 MagickBooleanType
4628 status;
4629
cristy101ab702011-10-13 13:06:32 +00004630 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004631 pixel;
4632
cristy4c08aed2011-07-01 19:47:50 +00004633 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004634 *q;
4635
cristye1bf8ad2010-09-19 17:07:03 +00004636 register ssize_t
4637 x;
4638
cristybb503372010-05-27 20:51:26 +00004639 size_t
cristyeaedf062010-05-29 22:36:02 +00004640 depth,
4641 one;
cristy3ed852e2009-09-05 21:47:34 +00004642
cristye1bf8ad2010-09-19 17:07:03 +00004643 ssize_t
4644 i,
4645 j,
4646 k,
4647 y;
4648
cristy3ed852e2009-09-05 21:47:34 +00004649 /*
4650 Initialize steganographic image attributes.
4651 */
4652 assert(image != (const Image *) NULL);
4653 assert(image->signature == MagickSignature);
4654 if (image->debug != MagickFalse)
4655 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4656 assert(watermark != (const Image *) NULL);
4657 assert(watermark->signature == MagickSignature);
4658 assert(exception != (ExceptionInfo *) NULL);
4659 assert(exception->signature == MagickSignature);
cristyeaedf062010-05-29 22:36:02 +00004660 one=1UL;
cristy3ed852e2009-09-05 21:47:34 +00004661 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4662 if (stegano_image == (Image *) NULL)
4663 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004664 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004665 {
cristy3ed852e2009-09-05 21:47:34 +00004666 stegano_image=DestroyImage(stegano_image);
4667 return((Image *) NULL);
4668 }
4669 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
4670 /*
4671 Hide watermark in low-order bits of image.
4672 */
4673 c=0;
4674 i=0;
4675 j=0;
4676 depth=stegano_image->depth;
4677 k=image->offset;
cristyda16f162011-02-19 23:52:17 +00004678 status=MagickTrue;
cristyb0d3bb92010-09-22 14:37:58 +00004679 watermark_view=AcquireCacheView(watermark);
4680 stegano_view=AcquireCacheView(stegano_image);
cristybb503372010-05-27 20:51:26 +00004681 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
cristy3ed852e2009-09-05 21:47:34 +00004682 {
cristybb503372010-05-27 20:51:26 +00004683 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
cristy3ed852e2009-09-05 21:47:34 +00004684 {
cristybb503372010-05-27 20:51:26 +00004685 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
cristy3ed852e2009-09-05 21:47:34 +00004686 {
cristyda1f9c12011-10-02 21:39:49 +00004687 Quantum
cristy5f95f4f2011-10-23 01:01:01 +00004688 virtual_pixel[CompositePixelChannel];
cristyda1f9c12011-10-02 21:39:49 +00004689
4690 (void) GetOneCacheViewVirtualPixel(watermark_view,x,y,virtual_pixel,
4691 exception);
4692 pixel.red=(double) virtual_pixel[RedPixelChannel];
4693 pixel.green=(double) virtual_pixel[GreenPixelChannel];
4694 pixel.blue=(double) virtual_pixel[BluePixelChannel];
4695 pixel.alpha=(double) virtual_pixel[AlphaPixelChannel];
cristybb503372010-05-27 20:51:26 +00004696 if ((k/(ssize_t) stegano_image->columns) >= (ssize_t) stegano_image->rows)
cristy3ed852e2009-09-05 21:47:34 +00004697 break;
cristyb0d3bb92010-09-22 14:37:58 +00004698 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4699 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4700 exception);
cristyacd2ed22011-08-30 01:44:23 +00004701 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004702 break;
4703 switch (c)
4704 {
4705 case 0:
4706 {
cristy4c08aed2011-07-01 19:47:50 +00004707 SetPixelRed(image,SetBit(GetPixelRed(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 1:
4712 {
cristy4c08aed2011-07-01 19:47:50 +00004713 SetPixelGreen(image,SetBit(GetPixelGreen(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 case 2:
4718 {
cristy4c08aed2011-07-01 19:47:50 +00004719 SetPixelBlue(image,SetBit(GetPixelBlue(image,q),j,GetBit(
cristy101ab702011-10-13 13:06:32 +00004720 GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004721 break;
4722 }
4723 }
cristyb0d3bb92010-09-22 14:37:58 +00004724 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004725 break;
4726 c++;
4727 if (c == 3)
4728 c=0;
4729 k++;
cristybb503372010-05-27 20:51:26 +00004730 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
cristy3ed852e2009-09-05 21:47:34 +00004731 k=0;
4732 if (k == image->offset)
4733 j++;
4734 }
4735 }
cristy8b27a6d2010-02-14 03:31:15 +00004736 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004737 {
cristy8b27a6d2010-02-14 03:31:15 +00004738 MagickBooleanType
4739 proceed;
4740
4741 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4742 (depth-i),depth);
4743 if (proceed == MagickFalse)
4744 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004745 }
4746 }
cristyb0d3bb92010-09-22 14:37:58 +00004747 stegano_view=DestroyCacheView(stegano_view);
4748 watermark_view=DestroyCacheView(watermark_view);
cristy3ed852e2009-09-05 21:47:34 +00004749 if (stegano_image->storage_class == PseudoClass)
cristyea1a8aa2011-10-20 13:24:06 +00004750 (void) SyncImage(stegano_image,exception);
cristyda16f162011-02-19 23:52:17 +00004751 if (status == MagickFalse)
4752 {
4753 stegano_image=DestroyImage(stegano_image);
4754 return((Image *) NULL);
4755 }
cristy3ed852e2009-09-05 21:47:34 +00004756 return(stegano_image);
4757}
4758
4759/*
4760%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4761% %
4762% %
4763% %
4764% S t e r e o A n a g l y p h I m a g e %
4765% %
4766% %
4767% %
4768%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4769%
4770% StereoAnaglyphImage() combines two images and produces a single image that
4771% is the composite of a left and right image of a stereo pair. Special
4772% red-green stereo glasses are required to view this effect.
4773%
4774% The format of the StereoAnaglyphImage method is:
4775%
4776% Image *StereoImage(const Image *left_image,const Image *right_image,
4777% ExceptionInfo *exception)
4778% Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004779% const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004780% ExceptionInfo *exception)
4781%
4782% A description of each parameter follows:
4783%
4784% o left_image: the left image.
4785%
4786% o right_image: the right image.
4787%
4788% o exception: return any errors or warnings in this structure.
4789%
4790% o x_offset: amount, in pixels, by which the left image is offset to the
4791% right of the right image.
4792%
4793% o y_offset: amount, in pixels, by which the left image is offset to the
4794% bottom of the right image.
4795%
4796%
4797*/
4798MagickExport Image *StereoImage(const Image *left_image,
4799 const Image *right_image,ExceptionInfo *exception)
4800{
4801 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4802}
4803
4804MagickExport Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004805 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004806 ExceptionInfo *exception)
4807{
4808#define StereoImageTag "Stereo/Image"
4809
4810 const Image
4811 *image;
4812
4813 Image
4814 *stereo_image;
4815
cristy3ed852e2009-09-05 21:47:34 +00004816 MagickBooleanType
4817 status;
4818
cristy9d314ff2011-03-09 01:30:28 +00004819 ssize_t
4820 y;
4821
cristy3ed852e2009-09-05 21:47:34 +00004822 assert(left_image != (const Image *) NULL);
4823 assert(left_image->signature == MagickSignature);
4824 if (left_image->debug != MagickFalse)
4825 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4826 left_image->filename);
4827 assert(right_image != (const Image *) NULL);
4828 assert(right_image->signature == MagickSignature);
4829 assert(exception != (ExceptionInfo *) NULL);
4830 assert(exception->signature == MagickSignature);
4831 assert(right_image != (const Image *) NULL);
4832 image=left_image;
4833 if ((left_image->columns != right_image->columns) ||
4834 (left_image->rows != right_image->rows))
4835 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4836 /*
4837 Initialize stereo image attributes.
4838 */
4839 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4840 MagickTrue,exception);
4841 if (stereo_image == (Image *) NULL)
4842 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004843 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004844 {
cristy3ed852e2009-09-05 21:47:34 +00004845 stereo_image=DestroyImage(stereo_image);
4846 return((Image *) NULL);
4847 }
4848 /*
4849 Copy left image to red channel and right image to blue channel.
4850 */
cristyda16f162011-02-19 23:52:17 +00004851 status=MagickTrue;
cristybb503372010-05-27 20:51:26 +00004852 for (y=0; y < (ssize_t) stereo_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004853 {
cristy4c08aed2011-07-01 19:47:50 +00004854 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004855 *restrict p,
4856 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004857
cristybb503372010-05-27 20:51:26 +00004858 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004859 x;
4860
cristy4c08aed2011-07-01 19:47:50 +00004861 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004862 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +00004863
4864 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4865 exception);
4866 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4867 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
cristy76f512e2011-09-12 01:26:56 +00004868 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4869 (r == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004870 break;
cristybb503372010-05-27 20:51:26 +00004871 for (x=0; x < (ssize_t) stereo_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004872 {
cristy4c08aed2011-07-01 19:47:50 +00004873 SetPixelRed(image,GetPixelRed(left_image,p),r);
cristy76f512e2011-09-12 01:26:56 +00004874 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4875 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4876 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4877 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4878 GetPixelAlpha(right_image,q))/2,r);
cristyed231572011-07-14 02:18:59 +00004879 p+=GetPixelChannels(left_image);
cristy76f512e2011-09-12 01:26:56 +00004880 q+=GetPixelChannels(right_image);
4881 r+=GetPixelChannels(stereo_image);
cristy3ed852e2009-09-05 21:47:34 +00004882 }
4883 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4884 break;
cristy8b27a6d2010-02-14 03:31:15 +00004885 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004886 {
cristy8b27a6d2010-02-14 03:31:15 +00004887 MagickBooleanType
4888 proceed;
4889
cristybb503372010-05-27 20:51:26 +00004890 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4891 stereo_image->rows);
cristy8b27a6d2010-02-14 03:31:15 +00004892 if (proceed == MagickFalse)
4893 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004894 }
4895 }
cristyda16f162011-02-19 23:52:17 +00004896 if (status == MagickFalse)
4897 {
4898 stereo_image=DestroyImage(stereo_image);
4899 return((Image *) NULL);
4900 }
cristy3ed852e2009-09-05 21:47:34 +00004901 return(stereo_image);
4902}
4903
4904/*
4905%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4906% %
4907% %
4908% %
4909% S w i r l I m a g e %
4910% %
4911% %
4912% %
4913%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4914%
4915% SwirlImage() swirls the pixels about the center of the image, where
4916% degrees indicates the sweep of the arc through which each pixel is moved.
4917% You get a more dramatic effect as the degrees move from 1 to 360.
4918%
4919% The format of the SwirlImage method is:
4920%
4921% Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00004922% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004923%
4924% A description of each parameter follows:
4925%
4926% o image: the image.
4927%
4928% o degrees: Define the tightness of the swirling effect.
4929%
cristy76f512e2011-09-12 01:26:56 +00004930% o method: the pixel interpolation method.
4931%
cristy3ed852e2009-09-05 21:47:34 +00004932% o exception: return any errors or warnings in this structure.
4933%
4934*/
4935MagickExport Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00004936 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004937{
4938#define SwirlImageTag "Swirl/Image"
4939
cristyfa112112010-01-04 17:48:07 +00004940 CacheView
4941 *image_view,
4942 *swirl_view;
4943
cristy3ed852e2009-09-05 21:47:34 +00004944 Image
4945 *swirl_image;
4946
cristy3ed852e2009-09-05 21:47:34 +00004947 MagickBooleanType
4948 status;
4949
cristybb503372010-05-27 20:51:26 +00004950 MagickOffsetType
4951 progress;
4952
cristy3ed852e2009-09-05 21:47:34 +00004953 MagickRealType
4954 radius;
4955
4956 PointInfo
4957 center,
4958 scale;
4959
cristybb503372010-05-27 20:51:26 +00004960 ssize_t
4961 y;
4962
cristy3ed852e2009-09-05 21:47:34 +00004963 /*
4964 Initialize swirl image attributes.
4965 */
4966 assert(image != (const Image *) NULL);
4967 assert(image->signature == MagickSignature);
4968 if (image->debug != MagickFalse)
4969 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4970 assert(exception != (ExceptionInfo *) NULL);
4971 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00004972 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004973 if (swirl_image == (Image *) NULL)
4974 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004975 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004976 {
cristy3ed852e2009-09-05 21:47:34 +00004977 swirl_image=DestroyImage(swirl_image);
4978 return((Image *) NULL);
4979 }
cristy4c08aed2011-07-01 19:47:50 +00004980 if (swirl_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00004981 swirl_image->matte=MagickTrue;
4982 /*
4983 Compute scaling factor.
4984 */
4985 center.x=(double) image->columns/2.0;
4986 center.y=(double) image->rows/2.0;
4987 radius=MagickMax(center.x,center.y);
4988 scale.x=1.0;
4989 scale.y=1.0;
4990 if (image->columns > image->rows)
4991 scale.y=(double) image->columns/(double) image->rows;
4992 else
4993 if (image->columns < image->rows)
4994 scale.x=(double) image->rows/(double) image->columns;
4995 degrees=(double) DegreesToRadians(degrees);
4996 /*
4997 Swirl image.
4998 */
4999 status=MagickTrue;
5000 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00005001 image_view=AcquireCacheView(image);
5002 swirl_view=AcquireCacheView(swirl_image);
cristyb5d5f722009-11-04 03:03:49 +00005003#if defined(MAGICKCORE_OPENMP_SUPPORT)
5004 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005005#endif
cristybb503372010-05-27 20:51:26 +00005006 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005007 {
cristy3ed852e2009-09-05 21:47:34 +00005008 MagickRealType
5009 distance;
5010
5011 PointInfo
5012 delta;
5013
cristy6d188022011-09-12 13:23:33 +00005014 register const Quantum
5015 *restrict p;
5016
cristybb503372010-05-27 20:51:26 +00005017 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005018 x;
5019
cristy4c08aed2011-07-01 19:47:50 +00005020 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005021 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005022
5023 if (status == MagickFalse)
5024 continue;
cristy6d188022011-09-12 13:23:33 +00005025 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy3ed852e2009-09-05 21:47:34 +00005026 q=GetCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
5027 exception);
cristy6d188022011-09-12 13:23:33 +00005028 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005029 {
5030 status=MagickFalse;
5031 continue;
5032 }
cristy3ed852e2009-09-05 21:47:34 +00005033 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00005034 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005035 {
cristy6d188022011-09-12 13:23:33 +00005036 register ssize_t
5037 i;
5038
cristy3ed852e2009-09-05 21:47:34 +00005039 /*
5040 Determine if the pixel is within an ellipse.
5041 */
5042 delta.x=scale.x*(double) (x-center.x);
5043 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00005044 if (distance >= (radius*radius))
5045 {
5046 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5047 q[i]=p[i];
5048 }
5049 else
cristy3ed852e2009-09-05 21:47:34 +00005050 {
5051 MagickRealType
5052 cosine,
5053 factor,
5054 sine;
5055
5056 /*
5057 Swirl the pixel.
5058 */
5059 factor=1.0-sqrt((double) distance)/radius;
5060 sine=sin((double) (degrees*factor*factor));
5061 cosine=cos((double) (degrees*factor*factor));
cristy76f512e2011-09-12 01:26:56 +00005062 status=InterpolatePixelChannels(image,image_view,swirl_image,method,
5063 ((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5064 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00005065 }
cristy6d188022011-09-12 13:23:33 +00005066 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00005067 q+=GetPixelChannels(swirl_image);
cristy3ed852e2009-09-05 21:47:34 +00005068 }
5069 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5070 status=MagickFalse;
5071 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5072 {
5073 MagickBooleanType
5074 proceed;
5075
cristyb5d5f722009-11-04 03:03:49 +00005076#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005077 #pragma omp critical (MagickCore_SwirlImage)
5078#endif
5079 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5080 if (proceed == MagickFalse)
5081 status=MagickFalse;
5082 }
5083 }
5084 swirl_view=DestroyCacheView(swirl_view);
5085 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005086 if (status == MagickFalse)
5087 swirl_image=DestroyImage(swirl_image);
5088 return(swirl_image);
5089}
5090
5091/*
5092%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5093% %
5094% %
5095% %
5096% T i n t I m a g e %
5097% %
5098% %
5099% %
5100%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5101%
5102% TintImage() applies a color vector to each pixel in the image. The length
5103% of the vector is 0 for black and white and at its maximum for the midtones.
5104% The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5105%
5106% The format of the TintImage method is:
5107%
cristyb817c3f2011-10-03 14:00:35 +00005108% Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005109% const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005110%
5111% A description of each parameter follows:
5112%
5113% o image: the image.
5114%
cristyb817c3f2011-10-03 14:00:35 +00005115% o blend: A color value used for tinting.
cristy3ed852e2009-09-05 21:47:34 +00005116%
5117% o tint: A color value used for tinting.
5118%
5119% o exception: return any errors or warnings in this structure.
5120%
5121*/
cristyb817c3f2011-10-03 14:00:35 +00005122MagickExport Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005123 const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005124{
5125#define TintImageTag "Tint/Image"
5126
cristyc4c8d132010-01-07 01:58:38 +00005127 CacheView
5128 *image_view,
5129 *tint_view;
5130
cristy3ed852e2009-09-05 21:47:34 +00005131 GeometryInfo
5132 geometry_info;
5133
5134 Image
5135 *tint_image;
5136
cristy3ed852e2009-09-05 21:47:34 +00005137 MagickBooleanType
5138 status;
5139
cristybb503372010-05-27 20:51:26 +00005140 MagickOffsetType
5141 progress;
cristy3ed852e2009-09-05 21:47:34 +00005142
cristy28474bf2011-09-11 23:32:52 +00005143 MagickRealType
5144 intensity;
5145
cristy4c08aed2011-07-01 19:47:50 +00005146 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00005147 color_vector,
5148 pixel;
5149
cristybb503372010-05-27 20:51:26 +00005150 MagickStatusType
5151 flags;
5152
5153 ssize_t
5154 y;
5155
cristy3ed852e2009-09-05 21:47:34 +00005156 /*
5157 Allocate tint image.
5158 */
5159 assert(image != (const Image *) NULL);
5160 assert(image->signature == MagickSignature);
5161 if (image->debug != MagickFalse)
5162 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5163 assert(exception != (ExceptionInfo *) NULL);
5164 assert(exception->signature == MagickSignature);
5165 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5166 if (tint_image == (Image *) NULL)
5167 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005168 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005169 {
cristy3ed852e2009-09-05 21:47:34 +00005170 tint_image=DestroyImage(tint_image);
5171 return((Image *) NULL);
5172 }
cristyaed9c382011-10-03 17:54:21 +00005173 if (blend == (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005174 return(tint_image);
5175 /*
5176 Determine RGB values of the color.
5177 */
cristy28474bf2011-09-11 23:32:52 +00005178 GetPixelInfo(image,&pixel);
cristyb817c3f2011-10-03 14:00:35 +00005179 flags=ParseGeometry(blend,&geometry_info);
cristy3ed852e2009-09-05 21:47:34 +00005180 pixel.red=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005181 pixel.green=geometry_info.rho;
5182 pixel.blue=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005183 pixel.alpha=OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00005184 if ((flags & SigmaValue) != 0)
5185 pixel.green=geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +00005186 if ((flags & XiValue) != 0)
5187 pixel.blue=geometry_info.xi;
cristyb817c3f2011-10-03 14:00:35 +00005188 if ((flags & PsiValue) != 0)
5189 pixel.alpha=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005190 if (image->colorspace == CMYKColorspace)
5191 {
cristyb817c3f2011-10-03 14:00:35 +00005192 pixel.black=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005193 if ((flags & PsiValue) != 0)
5194 pixel.black=geometry_info.psi;
5195 if ((flags & ChiValue) != 0)
5196 pixel.alpha=geometry_info.chi;
5197 }
cristy28474bf2011-09-11 23:32:52 +00005198 intensity=(MagickRealType) GetPixelInfoIntensity(tint);
5199 color_vector.red=(MagickRealType) (pixel.red*tint->red/100.0-intensity);
5200 color_vector.green=(MagickRealType) (pixel.green*tint->green/100.0-intensity);
5201 color_vector.blue=(MagickRealType) (pixel.blue*tint->blue/100.0-intensity);
5202 color_vector.black=(MagickRealType) (pixel.black*tint->black/100.0-intensity);
5203 color_vector.alpha=(MagickRealType) (pixel.alpha*tint->alpha/100.0-intensity);
cristy3ed852e2009-09-05 21:47:34 +00005204 /*
5205 Tint image.
5206 */
5207 status=MagickTrue;
5208 progress=0;
5209 image_view=AcquireCacheView(image);
5210 tint_view=AcquireCacheView(tint_image);
cristyb5d5f722009-11-04 03:03:49 +00005211#if defined(MAGICKCORE_OPENMP_SUPPORT)
5212 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005213#endif
cristybb503372010-05-27 20:51:26 +00005214 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005215 {
cristy4c08aed2011-07-01 19:47:50 +00005216 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00005217 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00005218
cristy4c08aed2011-07-01 19:47:50 +00005219 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005220 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005221
cristy6b91acb2011-04-19 12:23:54 +00005222 register ssize_t
5223 x;
5224
cristy3ed852e2009-09-05 21:47:34 +00005225 if (status == MagickFalse)
5226 continue;
5227 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5228 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5229 exception);
cristy4c08aed2011-07-01 19:47:50 +00005230 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005231 {
5232 status=MagickFalse;
5233 continue;
5234 }
cristybb503372010-05-27 20:51:26 +00005235 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005236 {
cristy4c08aed2011-07-01 19:47:50 +00005237 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00005238 pixel;
5239
5240 MagickRealType
5241 weight;
5242
cristy76f512e2011-09-12 01:26:56 +00005243 if ((GetPixelRedTraits(tint_image) & UpdatePixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005244 {
5245 weight=QuantumScale*GetPixelRed(image,p)-0.5;
5246 pixel.red=(MagickRealType) GetPixelRed(image,p)+
5247 color_vector.red*(1.0-(4.0*(weight*weight)));
5248 SetPixelRed(tint_image,ClampToQuantum(pixel.red),q);
5249 }
cristy76f512e2011-09-12 01:26:56 +00005250 if ((GetPixelGreenTraits(tint_image) & UpdatePixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005251 {
5252 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
5253 pixel.green=(MagickRealType) GetPixelGreen(image,p)+
5254 color_vector.green*(1.0-(4.0*(weight*weight)));
5255 SetPixelGreen(tint_image,ClampToQuantum(pixel.green),q);
5256 }
cristy76f512e2011-09-12 01:26:56 +00005257 if ((GetPixelBlueTraits(tint_image) & UpdatePixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005258 {
5259 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
5260 pixel.blue=(MagickRealType) GetPixelBlue(image,p)+
5261 color_vector.blue*(1.0-(4.0*(weight*weight)));
5262 SetPixelBlue(tint_image,ClampToQuantum(pixel.blue),q);
5263 }
cristy76f512e2011-09-12 01:26:56 +00005264 if ((GetPixelBlackTraits(tint_image) & UpdatePixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005265 {
5266 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
5267 pixel.black=(MagickRealType) GetPixelBlack(image,p)+
5268 color_vector.black*(1.0-(4.0*(weight*weight)));
5269 SetPixelBlack(tint_image,ClampToQuantum(pixel.black),q);
5270 }
cristy76f512e2011-09-12 01:26:56 +00005271 if ((GetPixelAlphaTraits(tint_image) & CopyPixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005272 SetPixelAlpha(tint_image,GetPixelAlpha(image,p),q);
cristyed231572011-07-14 02:18:59 +00005273 p+=GetPixelChannels(image);
5274 q+=GetPixelChannels(tint_image);
cristy3ed852e2009-09-05 21:47:34 +00005275 }
5276 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5277 status=MagickFalse;
5278 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5279 {
5280 MagickBooleanType
5281 proceed;
5282
cristyb5d5f722009-11-04 03:03:49 +00005283#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005284 #pragma omp critical (MagickCore_TintImage)
5285#endif
5286 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5287 if (proceed == MagickFalse)
5288 status=MagickFalse;
5289 }
5290 }
5291 tint_view=DestroyCacheView(tint_view);
5292 image_view=DestroyCacheView(image_view);
5293 if (status == MagickFalse)
5294 tint_image=DestroyImage(tint_image);
5295 return(tint_image);
5296}
5297
5298/*
5299%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5300% %
5301% %
5302% %
5303% V i g n e t t e I m a g e %
5304% %
5305% %
5306% %
5307%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5308%
5309% VignetteImage() softens the edges of the image in vignette style.
5310%
5311% The format of the VignetteImage method is:
5312%
5313% Image *VignetteImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +00005314% const double sigma,const ssize_t x,const ssize_t y,
5315% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005316%
5317% A description of each parameter follows:
5318%
5319% o image: the image.
5320%
5321% o radius: the radius of the pixel neighborhood.
5322%
5323% o sigma: the standard deviation of the Gaussian, in pixels.
5324%
5325% o x, y: Define the x and y ellipse offset.
5326%
5327% o exception: return any errors or warnings in this structure.
5328%
5329*/
5330MagickExport Image *VignetteImage(const Image *image,const double radius,
cristybb503372010-05-27 20:51:26 +00005331 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005332{
5333 char
5334 ellipse[MaxTextExtent];
5335
5336 DrawInfo
5337 *draw_info;
5338
5339 Image
5340 *canvas_image,
5341 *blur_image,
5342 *oval_image,
5343 *vignette_image;
5344
5345 assert(image != (Image *) NULL);
5346 assert(image->signature == MagickSignature);
5347 if (image->debug != MagickFalse)
5348 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5349 assert(exception != (ExceptionInfo *) NULL);
5350 assert(exception->signature == MagickSignature);
5351 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5352 if (canvas_image == (Image *) NULL)
5353 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005354 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005355 {
cristy3ed852e2009-09-05 21:47:34 +00005356 canvas_image=DestroyImage(canvas_image);
5357 return((Image *) NULL);
5358 }
5359 canvas_image->matte=MagickTrue;
cristyb3a73b52011-07-26 01:34:43 +00005360 oval_image=CloneImage(canvas_image,canvas_image->columns,
5361 canvas_image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005362 if (oval_image == (Image *) NULL)
5363 {
5364 canvas_image=DestroyImage(canvas_image);
5365 return((Image *) NULL);
5366 }
cristy9950d572011-10-01 18:22:35 +00005367 (void) QueryColorCompliance("#000000",AllCompliance,
5368 &oval_image->background_color,exception);
cristyea1a8aa2011-10-20 13:24:06 +00005369 (void) SetImageBackgroundColor(oval_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00005370 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
cristy9950d572011-10-01 18:22:35 +00005371 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5372 exception);
5373 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5374 exception);
cristyb51dff52011-05-19 16:55:47 +00005375 (void) FormatLocaleString(ellipse,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00005376 "ellipse %g,%g,%g,%g,0.0,360.0",image->columns/2.0,
cristy8cd5b312010-01-07 01:10:24 +00005377 image->rows/2.0,image->columns/2.0-x,image->rows/2.0-y);
cristy3ed852e2009-09-05 21:47:34 +00005378 draw_info->primitive=AcquireString(ellipse);
cristy018f07f2011-09-04 21:15:19 +00005379 (void) DrawImage(oval_image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00005380 draw_info=DestroyDrawInfo(draw_info);
cristy05c0c9a2011-09-05 23:16:13 +00005381 blur_image=BlurImage(oval_image,radius,sigma,image->bias,exception);
cristy3ed852e2009-09-05 21:47:34 +00005382 oval_image=DestroyImage(oval_image);
5383 if (blur_image == (Image *) NULL)
5384 {
5385 canvas_image=DestroyImage(canvas_image);
5386 return((Image *) NULL);
5387 }
5388 blur_image->matte=MagickFalse;
cristye941a752011-10-15 01:52:48 +00005389 (void) CompositeImage(canvas_image,CopyOpacityCompositeOp,blur_image,0,0,
5390 exception);
cristy3ed852e2009-09-05 21:47:34 +00005391 blur_image=DestroyImage(blur_image);
5392 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5393 canvas_image=DestroyImage(canvas_image);
5394 return(vignette_image);
5395}
5396
5397/*
5398%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5399% %
5400% %
5401% %
5402% W a v e I m a g e %
5403% %
5404% %
5405% %
5406%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5407%
5408% WaveImage() creates a "ripple" effect in the image by shifting the pixels
cristycee97112010-05-28 00:44:52 +00005409% vertically along a sine wave whose amplitude and wavelength is specified
cristy3ed852e2009-09-05 21:47:34 +00005410% by the given parameters.
5411%
5412% The format of the WaveImage method is:
5413%
5414% Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005415% const double wave_length,const PixelInterpolateMethod method,
5416% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005417%
5418% A description of each parameter follows:
5419%
5420% o image: the image.
5421%
5422% o amplitude, wave_length: Define the amplitude and wave length of the
5423% sine wave.
5424%
cristy5c4e2582011-09-11 19:21:03 +00005425% o interpolate: the pixel interpolation method.
5426%
cristy3ed852e2009-09-05 21:47:34 +00005427% o exception: return any errors or warnings in this structure.
5428%
5429*/
5430MagickExport Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005431 const double wave_length,const PixelInterpolateMethod method,
5432 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005433{
5434#define WaveImageTag "Wave/Image"
5435
cristyfa112112010-01-04 17:48:07 +00005436 CacheView
cristyd76c51e2011-03-26 00:21:26 +00005437 *image_view,
cristyfa112112010-01-04 17:48:07 +00005438 *wave_view;
5439
cristy3ed852e2009-09-05 21:47:34 +00005440 Image
5441 *wave_image;
5442
cristy3ed852e2009-09-05 21:47:34 +00005443 MagickBooleanType
5444 status;
5445
cristybb503372010-05-27 20:51:26 +00005446 MagickOffsetType
5447 progress;
5448
cristy3ed852e2009-09-05 21:47:34 +00005449 MagickRealType
5450 *sine_map;
5451
cristybb503372010-05-27 20:51:26 +00005452 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005453 i;
5454
cristybb503372010-05-27 20:51:26 +00005455 ssize_t
5456 y;
5457
cristy3ed852e2009-09-05 21:47:34 +00005458 /*
5459 Initialize wave image attributes.
5460 */
5461 assert(image != (Image *) NULL);
5462 assert(image->signature == MagickSignature);
5463 if (image->debug != MagickFalse)
5464 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5465 assert(exception != (ExceptionInfo *) NULL);
5466 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00005467 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
cristy3ed852e2009-09-05 21:47:34 +00005468 fabs(amplitude)),MagickTrue,exception);
5469 if (wave_image == (Image *) NULL)
5470 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005471 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005472 {
cristy3ed852e2009-09-05 21:47:34 +00005473 wave_image=DestroyImage(wave_image);
5474 return((Image *) NULL);
5475 }
cristy4c08aed2011-07-01 19:47:50 +00005476 if (wave_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00005477 wave_image->matte=MagickTrue;
5478 /*
5479 Allocate sine map.
5480 */
5481 sine_map=(MagickRealType *) AcquireQuantumMemory((size_t) wave_image->columns,
5482 sizeof(*sine_map));
5483 if (sine_map == (MagickRealType *) NULL)
5484 {
5485 wave_image=DestroyImage(wave_image);
5486 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5487 }
cristybb503372010-05-27 20:51:26 +00005488 for (i=0; i < (ssize_t) wave_image->columns; i++)
cristy4205a3c2010-09-12 20:19:59 +00005489 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5490 wave_length));
cristy3ed852e2009-09-05 21:47:34 +00005491 /*
5492 Wave image.
5493 */
5494 status=MagickTrue;
5495 progress=0;
cristyd76c51e2011-03-26 00:21:26 +00005496 image_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00005497 wave_view=AcquireCacheView(wave_image);
cristyd76c51e2011-03-26 00:21:26 +00005498 (void) SetCacheViewVirtualPixelMethod(image_view,
5499 BackgroundVirtualPixelMethod);
cristyb5d5f722009-11-04 03:03:49 +00005500#if defined(MAGICKCORE_OPENMP_SUPPORT)
5501 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005502#endif
cristybb503372010-05-27 20:51:26 +00005503 for (y=0; y < (ssize_t) wave_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005504 {
cristy4c08aed2011-07-01 19:47:50 +00005505 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005506 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005507
cristye97bb922011-04-03 01:36:52 +00005508 register ssize_t
5509 x;
5510
cristy3ed852e2009-09-05 21:47:34 +00005511 if (status == MagickFalse)
5512 continue;
5513 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5514 exception);
cristyacd2ed22011-08-30 01:44:23 +00005515 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005516 {
5517 status=MagickFalse;
5518 continue;
5519 }
cristybb503372010-05-27 20:51:26 +00005520 for (x=0; x < (ssize_t) wave_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005521 {
cristy5c4e2582011-09-11 19:21:03 +00005522 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5523 (double) x,(double) (y-sine_map[x]),q,exception);
cristyed231572011-07-14 02:18:59 +00005524 q+=GetPixelChannels(wave_image);
cristy3ed852e2009-09-05 21:47:34 +00005525 }
5526 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5527 status=MagickFalse;
5528 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5529 {
5530 MagickBooleanType
5531 proceed;
5532
cristyb5d5f722009-11-04 03:03:49 +00005533#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005534 #pragma omp critical (MagickCore_WaveImage)
5535#endif
5536 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5537 if (proceed == MagickFalse)
5538 status=MagickFalse;
5539 }
5540 }
5541 wave_view=DestroyCacheView(wave_view);
cristyd76c51e2011-03-26 00:21:26 +00005542 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005543 sine_map=(MagickRealType *) RelinquishMagickMemory(sine_map);
5544 if (status == MagickFalse)
5545 wave_image=DestroyImage(wave_image);
5546 return(wave_image);
5547}