blob: fcc585ae7c8979c4a4bffc5a5bc782127cf8400e [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
cristy0568ffc2011-07-25 16:54:14 +00001259static inline MagickRealType FxMax(FxInfo *fx_info,const PixelChannel channel,
cristye85007d2010-06-06 22:51:36 +00001260 const ssize_t x,const ssize_t y,const char *expression,
1261 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001262{
1263 MagickRealType
1264 alpha,
1265 beta;
1266
1267 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression,&beta,exception);
1268 return((MagickRealType) MagickMax((double) alpha,(double) beta));
1269}
1270
cristy0568ffc2011-07-25 16:54:14 +00001271static inline MagickRealType FxMin(FxInfo *fx_info,PixelChannel channel,
cristye85007d2010-06-06 22:51:36 +00001272 const ssize_t x,const ssize_t y,const char *expression,
1273 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001274{
1275 MagickRealType
1276 alpha,
1277 beta;
1278
1279 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression,&beta,exception);
1280 return((MagickRealType) MagickMin((double) alpha,(double) beta));
1281}
1282
1283static inline const char *FxSubexpression(const char *expression,
1284 ExceptionInfo *exception)
1285{
1286 const char
1287 *subexpression;
1288
cristybb503372010-05-27 20:51:26 +00001289 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001290 level;
1291
1292 level=0;
1293 subexpression=expression;
1294 while ((*subexpression != '\0') &&
1295 ((level != 1) || (strchr(")",(int) *subexpression) == (char *) NULL)))
1296 {
1297 if (strchr("(",(int) *subexpression) != (char *) NULL)
1298 level++;
1299 else
1300 if (strchr(")",(int) *subexpression) != (char *) NULL)
1301 level--;
1302 subexpression++;
1303 }
1304 if (*subexpression == '\0')
1305 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1306 "UnbalancedParenthesis","`%s'",expression);
1307 return(subexpression);
1308}
1309
cristy0568ffc2011-07-25 16:54:14 +00001310static MagickRealType FxGetSymbol(FxInfo *fx_info,const PixelChannel channel,
cristye85007d2010-06-06 22:51:36 +00001311 const ssize_t x,const ssize_t y,const char *expression,
1312 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001313{
1314 char
1315 *q,
1316 subexpression[MaxTextExtent],
1317 symbol[MaxTextExtent];
1318
1319 const char
1320 *p,
1321 *value;
1322
1323 Image
1324 *image;
1325
cristy4c08aed2011-07-01 19:47:50 +00001326 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001327 pixel;
1328
1329 MagickRealType
1330 alpha,
1331 beta;
1332
1333 PointInfo
1334 point;
1335
cristybb503372010-05-27 20:51:26 +00001336 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001337 i;
1338
1339 size_t
1340 length;
1341
cristybb503372010-05-27 20:51:26 +00001342 size_t
cristy3ed852e2009-09-05 21:47:34 +00001343 level;
1344
1345 p=expression;
1346 i=GetImageIndexInList(fx_info->images);
1347 level=0;
1348 point.x=(double) x;
1349 point.y=(double) y;
1350 if (isalpha((int) *(p+1)) == 0)
1351 {
1352 if (strchr("suv",(int) *p) != (char *) NULL)
1353 {
1354 switch (*p)
1355 {
1356 case 's':
1357 default:
1358 {
1359 i=GetImageIndexInList(fx_info->images);
1360 break;
1361 }
1362 case 'u': i=0; break;
1363 case 'v': i=1; break;
1364 }
1365 p++;
1366 if (*p == '[')
1367 {
1368 level++;
1369 q=subexpression;
1370 for (p++; *p != '\0'; )
1371 {
1372 if (*p == '[')
1373 level++;
1374 else
1375 if (*p == ']')
1376 {
1377 level--;
1378 if (level == 0)
1379 break;
1380 }
1381 *q++=(*p++);
1382 }
1383 *q='\0';
1384 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1385 &beta,exception);
cristybb503372010-05-27 20:51:26 +00001386 i=(ssize_t) (alpha+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001387 p++;
1388 }
1389 if (*p == '.')
1390 p++;
1391 }
1392 if ((isalpha((int) *(p+1)) == 0) && (*p == 'p'))
1393 {
1394 p++;
1395 if (*p == '{')
1396 {
1397 level++;
1398 q=subexpression;
1399 for (p++; *p != '\0'; )
1400 {
1401 if (*p == '{')
1402 level++;
1403 else
1404 if (*p == '}')
1405 {
1406 level--;
1407 if (level == 0)
1408 break;
1409 }
1410 *q++=(*p++);
1411 }
1412 *q='\0';
1413 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1414 &beta,exception);
1415 point.x=alpha;
1416 point.y=beta;
1417 p++;
1418 }
1419 else
1420 if (*p == '[')
1421 {
1422 level++;
1423 q=subexpression;
1424 for (p++; *p != '\0'; )
1425 {
1426 if (*p == '[')
1427 level++;
1428 else
1429 if (*p == ']')
1430 {
1431 level--;
1432 if (level == 0)
1433 break;
1434 }
1435 *q++=(*p++);
1436 }
1437 *q='\0';
1438 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1439 &beta,exception);
1440 point.x+=alpha;
1441 point.y+=beta;
1442 p++;
1443 }
1444 if (*p == '.')
1445 p++;
1446 }
1447 }
1448 length=GetImageListLength(fx_info->images);
1449 while (i < 0)
cristybb503372010-05-27 20:51:26 +00001450 i+=(ssize_t) length;
cristy3ed852e2009-09-05 21:47:34 +00001451 i%=length;
1452 image=GetImageFromList(fx_info->images,i);
1453 if (image == (Image *) NULL)
1454 {
1455 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1456 "NoSuchImage","`%s'",expression);
1457 return(0.0);
1458 }
cristy4c08aed2011-07-01 19:47:50 +00001459 GetPixelInfo(image,&pixel);
1460 (void) InterpolatePixelInfo(image,fx_info->view[i],image->interpolate,
cristy4f820712011-04-01 12:35:43 +00001461 point.x,point.y,&pixel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001462 if ((strlen(p) > 2) &&
1463 (LocaleCompare(p,"intensity") != 0) &&
1464 (LocaleCompare(p,"luminance") != 0) &&
1465 (LocaleCompare(p,"hue") != 0) &&
1466 (LocaleCompare(p,"saturation") != 0) &&
1467 (LocaleCompare(p,"lightness") != 0))
1468 {
1469 char
1470 name[MaxTextExtent];
1471
1472 (void) CopyMagickString(name,p,MaxTextExtent);
1473 for (q=name+(strlen(name)-1); q > name; q--)
1474 {
1475 if (*q == ')')
1476 break;
1477 if (*q == '.')
1478 {
1479 *q='\0';
1480 break;
1481 }
1482 }
1483 if ((strlen(name) > 2) &&
1484 (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
1485 {
cristy4c08aed2011-07-01 19:47:50 +00001486 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001487 *color;
1488
cristy4c08aed2011-07-01 19:47:50 +00001489 color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
1490 if (color != (PixelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001491 {
1492 pixel=(*color);
1493 p+=strlen(name);
1494 }
1495 else
cristy269c9412011-10-13 23:41:15 +00001496 if (QueryColorCompliance(name,AllCompliance,&pixel,fx_info->exception) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001497 {
1498 (void) AddValueToSplayTree(fx_info->colors,ConstantString(name),
cristy4c08aed2011-07-01 19:47:50 +00001499 ClonePixelInfo(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001500 p+=strlen(name);
1501 }
1502 }
1503 }
1504 (void) CopyMagickString(symbol,p,MaxTextExtent);
1505 StripString(symbol);
1506 if (*symbol == '\0')
1507 {
1508 switch (channel)
1509 {
cristy0568ffc2011-07-25 16:54:14 +00001510 case RedPixelChannel: return(QuantumScale*pixel.red);
1511 case GreenPixelChannel: return(QuantumScale*pixel.green);
1512 case BluePixelChannel: return(QuantumScale*pixel.blue);
1513 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001514 {
1515 if (image->colorspace != CMYKColorspace)
1516 {
1517 (void) ThrowMagickException(exception,GetMagickModule(),
1518 OptionError,"ColorSeparatedImageRequired","`%s'",
1519 image->filename);
1520 return(0.0);
1521 }
cristy4c08aed2011-07-01 19:47:50 +00001522 return(QuantumScale*pixel.black);
1523 }
cristy0568ffc2011-07-25 16:54:14 +00001524 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001525 {
1526 MagickRealType
1527 alpha;
1528
1529 if (pixel.matte == MagickFalse)
1530 return(1.0);
1531 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
1532 return(alpha);
cristy3ed852e2009-09-05 21:47:34 +00001533 }
cristyb3a73b52011-07-26 01:34:43 +00001534 case IntensityPixelChannel:
cristyf364ed42010-12-15 01:54:43 +00001535 {
cristy4c08aed2011-07-01 19:47:50 +00001536 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristyf364ed42010-12-15 01:54:43 +00001537 }
cristy3ed852e2009-09-05 21:47:34 +00001538 default:
1539 break;
1540 }
1541 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1542 "UnableToParseExpression","`%s'",p);
1543 return(0.0);
1544 }
1545 switch (*symbol)
1546 {
1547 case 'A':
1548 case 'a':
1549 {
1550 if (LocaleCompare(symbol,"a") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001551 return((MagickRealType) (QuantumScale*pixel.alpha));
cristy3ed852e2009-09-05 21:47:34 +00001552 break;
1553 }
1554 case 'B':
1555 case 'b':
1556 {
1557 if (LocaleCompare(symbol,"b") == 0)
1558 return(QuantumScale*pixel.blue);
1559 break;
1560 }
1561 case 'C':
1562 case 'c':
1563 {
1564 if (LocaleNCompare(symbol,"channel",7) == 0)
1565 {
1566 GeometryInfo
1567 channel_info;
1568
1569 MagickStatusType
1570 flags;
1571
1572 flags=ParseGeometry(symbol+7,&channel_info);
1573 if (image->colorspace == CMYKColorspace)
1574 switch (channel)
1575 {
cristy0568ffc2011-07-25 16:54:14 +00001576 case CyanPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001577 {
1578 if ((flags & RhoValue) == 0)
1579 return(0.0);
1580 return(channel_info.rho);
1581 }
cristy0568ffc2011-07-25 16:54:14 +00001582 case MagentaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001583 {
1584 if ((flags & SigmaValue) == 0)
1585 return(0.0);
1586 return(channel_info.sigma);
1587 }
cristy0568ffc2011-07-25 16:54:14 +00001588 case YellowPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001589 {
1590 if ((flags & XiValue) == 0)
1591 return(0.0);
1592 return(channel_info.xi);
1593 }
cristy0568ffc2011-07-25 16:54:14 +00001594 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001595 {
1596 if ((flags & PsiValue) == 0)
1597 return(0.0);
1598 return(channel_info.psi);
1599 }
cristy0568ffc2011-07-25 16:54:14 +00001600 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001601 {
1602 if ((flags & ChiValue) == 0)
1603 return(0.0);
1604 return(channel_info.chi);
1605 }
1606 default:
1607 return(0.0);
1608 }
1609 switch (channel)
1610 {
cristy0568ffc2011-07-25 16:54:14 +00001611 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001612 {
1613 if ((flags & RhoValue) == 0)
1614 return(0.0);
1615 return(channel_info.rho);
1616 }
cristy0568ffc2011-07-25 16:54:14 +00001617 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001618 {
1619 if ((flags & SigmaValue) == 0)
1620 return(0.0);
1621 return(channel_info.sigma);
1622 }
cristy0568ffc2011-07-25 16:54:14 +00001623 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001624 {
1625 if ((flags & XiValue) == 0)
1626 return(0.0);
1627 return(channel_info.xi);
1628 }
cristy0568ffc2011-07-25 16:54:14 +00001629 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001630 {
1631 if ((flags & ChiValue) == 0)
1632 return(0.0);
1633 return(channel_info.chi);
1634 }
cristy0568ffc2011-07-25 16:54:14 +00001635 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001636 {
1637 if ((flags & PsiValue) == 0)
1638 return(0.0);
1639 return(channel_info.psi);
1640 }
cristy3ed852e2009-09-05 21:47:34 +00001641 default:
1642 return(0.0);
1643 }
1644 return(0.0);
1645 }
1646 if (LocaleCompare(symbol,"c") == 0)
1647 return(QuantumScale*pixel.red);
1648 break;
1649 }
1650 case 'D':
1651 case 'd':
1652 {
1653 if (LocaleNCompare(symbol,"depth",5) == 0)
1654 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1655 break;
1656 }
1657 case 'G':
1658 case 'g':
1659 {
1660 if (LocaleCompare(symbol,"g") == 0)
1661 return(QuantumScale*pixel.green);
1662 break;
1663 }
1664 case 'K':
1665 case 'k':
1666 {
1667 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1668 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1669 if (LocaleCompare(symbol,"k") == 0)
1670 {
1671 if (image->colorspace != CMYKColorspace)
1672 {
1673 (void) ThrowMagickException(exception,GetMagickModule(),
1674 OptionError,"ColorSeparatedImageRequired","`%s'",
1675 image->filename);
1676 return(0.0);
1677 }
cristy4c08aed2011-07-01 19:47:50 +00001678 return(QuantumScale*pixel.black);
cristy3ed852e2009-09-05 21:47:34 +00001679 }
1680 break;
1681 }
1682 case 'H':
1683 case 'h':
1684 {
1685 if (LocaleCompare(symbol,"h") == 0)
1686 return((MagickRealType) image->rows);
1687 if (LocaleCompare(symbol,"hue") == 0)
1688 {
1689 double
1690 hue,
1691 lightness,
1692 saturation;
1693
cristyda1f9c12011-10-02 21:39:49 +00001694 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1695 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001696 return(hue);
1697 }
1698 break;
1699 }
1700 case 'I':
1701 case 'i':
1702 {
1703 if ((LocaleCompare(symbol,"image.depth") == 0) ||
1704 (LocaleCompare(symbol,"image.minima") == 0) ||
1705 (LocaleCompare(symbol,"image.maxima") == 0) ||
1706 (LocaleCompare(symbol,"image.mean") == 0) ||
1707 (LocaleCompare(symbol,"image.kurtosis") == 0) ||
1708 (LocaleCompare(symbol,"image.skewness") == 0) ||
1709 (LocaleCompare(symbol,"image.standard_deviation") == 0))
1710 return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
1711 if (LocaleCompare(symbol,"image.resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001712 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001713 if (LocaleCompare(symbol,"image.resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001714 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001715 if (LocaleCompare(symbol,"intensity") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001716 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001717 if (LocaleCompare(symbol,"i") == 0)
1718 return((MagickRealType) x);
1719 break;
1720 }
1721 case 'J':
1722 case 'j':
1723 {
1724 if (LocaleCompare(symbol,"j") == 0)
1725 return((MagickRealType) y);
1726 break;
1727 }
1728 case 'L':
1729 case 'l':
1730 {
1731 if (LocaleCompare(symbol,"lightness") == 0)
1732 {
1733 double
1734 hue,
1735 lightness,
1736 saturation;
1737
cristyda1f9c12011-10-02 21:39:49 +00001738 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1739 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001740 return(lightness);
1741 }
1742 if (LocaleCompare(symbol,"luminance") == 0)
1743 {
1744 double
1745 luminence;
1746
1747 luminence=0.2126*pixel.red+0.7152*pixel.green+0.0722*pixel.blue;
1748 return(QuantumScale*luminence);
1749 }
1750 break;
1751 }
1752 case 'M':
1753 case 'm':
1754 {
1755 if (LocaleNCompare(symbol,"maxima",6) == 0)
1756 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1757 if (LocaleNCompare(symbol,"mean",4) == 0)
1758 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1759 if (LocaleNCompare(symbol,"minima",6) == 0)
1760 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1761 if (LocaleCompare(symbol,"m") == 0)
1762 return(QuantumScale*pixel.blue);
1763 break;
1764 }
1765 case 'N':
1766 case 'n':
1767 {
1768 if (LocaleCompare(symbol,"n") == 0)
anthony374f5dd2011-03-25 10:08:53 +00001769 return((MagickRealType) GetImageListLength(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001770 break;
1771 }
1772 case 'O':
1773 case 'o':
1774 {
1775 if (LocaleCompare(symbol,"o") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001776 return(QuantumScale*pixel.alpha);
cristy3ed852e2009-09-05 21:47:34 +00001777 break;
1778 }
1779 case 'P':
1780 case 'p':
1781 {
1782 if (LocaleCompare(symbol,"page.height") == 0)
1783 return((MagickRealType) image->page.height);
1784 if (LocaleCompare(symbol,"page.width") == 0)
1785 return((MagickRealType) image->page.width);
1786 if (LocaleCompare(symbol,"page.x") == 0)
1787 return((MagickRealType) image->page.x);
1788 if (LocaleCompare(symbol,"page.y") == 0)
1789 return((MagickRealType) image->page.y);
1790 break;
1791 }
1792 case 'R':
1793 case 'r':
1794 {
1795 if (LocaleCompare(symbol,"resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001796 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001797 if (LocaleCompare(symbol,"resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001798 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001799 if (LocaleCompare(symbol,"r") == 0)
1800 return(QuantumScale*pixel.red);
1801 break;
1802 }
1803 case 'S':
1804 case 's':
1805 {
1806 if (LocaleCompare(symbol,"saturation") == 0)
1807 {
1808 double
1809 hue,
1810 lightness,
1811 saturation;
1812
cristyda1f9c12011-10-02 21:39:49 +00001813 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1814 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001815 return(saturation);
1816 }
1817 if (LocaleNCompare(symbol,"skewness",8) == 0)
1818 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1819 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1820 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1821 break;
1822 }
1823 case 'T':
1824 case 't':
1825 {
1826 if (LocaleCompare(symbol,"t") == 0)
cristy5a15b932011-03-26 12:50:33 +00001827 return((MagickRealType) GetImageIndexInList(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001828 break;
1829 }
1830 case 'W':
1831 case 'w':
1832 {
1833 if (LocaleCompare(symbol,"w") == 0)
1834 return((MagickRealType) image->columns);
1835 break;
1836 }
1837 case 'Y':
1838 case 'y':
1839 {
1840 if (LocaleCompare(symbol,"y") == 0)
1841 return(QuantumScale*pixel.green);
1842 break;
1843 }
1844 case 'Z':
1845 case 'z':
1846 {
1847 if (LocaleCompare(symbol,"z") == 0)
1848 {
1849 MagickRealType
1850 depth;
1851
cristyfefab1b2011-07-05 00:33:22 +00001852 depth=(MagickRealType) GetImageDepth(image,fx_info->exception);
cristy3ed852e2009-09-05 21:47:34 +00001853 return(depth);
1854 }
1855 break;
1856 }
1857 default:
1858 break;
1859 }
1860 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1861 if (value != (const char *) NULL)
cristyc1acd842011-05-19 23:05:47 +00001862 return((MagickRealType) InterpretLocaleValue(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001863 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1864 "UnableToParseExpression","`%s'",symbol);
1865 return(0.0);
1866}
1867
1868static const char *FxOperatorPrecedence(const char *expression,
1869 ExceptionInfo *exception)
1870{
1871 typedef enum
1872 {
1873 UndefinedPrecedence,
1874 NullPrecedence,
1875 BitwiseComplementPrecedence,
1876 ExponentPrecedence,
cristy116af162010-08-13 01:25:47 +00001877 ExponentialNotationPrecedence,
cristy3ed852e2009-09-05 21:47:34 +00001878 MultiplyPrecedence,
1879 AdditionPrecedence,
1880 ShiftPrecedence,
1881 RelationalPrecedence,
1882 EquivalencyPrecedence,
1883 BitwiseAndPrecedence,
1884 BitwiseOrPrecedence,
1885 LogicalAndPrecedence,
1886 LogicalOrPrecedence,
1887 TernaryPrecedence,
1888 AssignmentPrecedence,
1889 CommaPrecedence,
1890 SeparatorPrecedence
1891 } FxPrecedence;
1892
1893 FxPrecedence
1894 precedence,
1895 target;
1896
1897 register const char
1898 *subexpression;
1899
1900 register int
1901 c;
1902
cristybb503372010-05-27 20:51:26 +00001903 size_t
cristy3ed852e2009-09-05 21:47:34 +00001904 level;
1905
1906 c=0;
1907 level=0;
1908 subexpression=(const char *) NULL;
1909 target=NullPrecedence;
1910 while (*expression != '\0')
1911 {
1912 precedence=UndefinedPrecedence;
1913 if ((isspace((int) ((char) *expression)) != 0) || (c == (int) '@'))
1914 {
1915 expression++;
1916 continue;
1917 }
cristy488fa882010-03-01 22:34:24 +00001918 switch (*expression)
1919 {
1920 case 'A':
1921 case 'a':
cristy3ed852e2009-09-05 21:47:34 +00001922 {
cristyb33454f2011-08-03 02:10:45 +00001923#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00001924 if (LocaleNCompare(expression,"acosh",5) == 0)
1925 {
1926 expression+=5;
1927 break;
1928 }
cristyb33454f2011-08-03 02:10:45 +00001929#endif
1930#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00001931 if (LocaleNCompare(expression,"asinh",5) == 0)
1932 {
1933 expression+=5;
1934 break;
1935 }
cristyb33454f2011-08-03 02:10:45 +00001936#endif
1937#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00001938 if (LocaleNCompare(expression,"atanh",5) == 0)
cristy488fa882010-03-01 22:34:24 +00001939 {
1940 expression+=5;
1941 break;
1942 }
cristyb33454f2011-08-03 02:10:45 +00001943#endif
cristy488fa882010-03-01 22:34:24 +00001944 break;
cristy3ed852e2009-09-05 21:47:34 +00001945 }
cristy62d455f2011-11-03 11:42:28 +00001946 case 'E':
1947 case 'e':
1948 {
1949 if ((LocaleNCompare(expression,"E+",2) == 0) ||
1950 (LocaleNCompare(expression,"E-",2) == 0))
1951 {
1952 expression+=2; /* scientific notation */
1953 break;
1954 }
1955 }
cristy488fa882010-03-01 22:34:24 +00001956 case 'J':
1957 case 'j':
1958 {
1959 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1960 (LocaleNCompare(expression,"j1",2) == 0))
1961 {
1962 expression+=2;
1963 break;
1964 }
1965 break;
1966 }
cristy2def9322010-06-18 23:59:37 +00001967 case '#':
1968 {
1969 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1970 expression++;
1971 break;
1972 }
cristy488fa882010-03-01 22:34:24 +00001973 default:
1974 break;
1975 }
cristy3ed852e2009-09-05 21:47:34 +00001976 if ((c == (int) '{') || (c == (int) '['))
1977 level++;
1978 else
1979 if ((c == (int) '}') || (c == (int) ']'))
1980 level--;
1981 if (level == 0)
1982 switch ((unsigned char) *expression)
1983 {
1984 case '~':
1985 case '!':
1986 {
1987 precedence=BitwiseComplementPrecedence;
1988 break;
1989 }
1990 case '^':
cristy6621e252010-08-13 00:42:57 +00001991 case '@':
cristy3ed852e2009-09-05 21:47:34 +00001992 {
1993 precedence=ExponentPrecedence;
1994 break;
1995 }
1996 default:
1997 {
1998 if (((c != 0) && ((isdigit((int) ((char) c)) != 0) ||
1999 (strchr(")",c) != (char *) NULL))) &&
2000 (((islower((int) ((char) *expression)) != 0) ||
2001 (strchr("(",(int) *expression) != (char *) NULL)) ||
2002 ((isdigit((int) ((char) c)) == 0) &&
2003 (isdigit((int) ((char) *expression)) != 0))) &&
2004 (strchr("xy",(int) *expression) == (char *) NULL))
2005 precedence=MultiplyPrecedence;
2006 break;
2007 }
2008 case '*':
2009 case '/':
2010 case '%':
2011 {
2012 precedence=MultiplyPrecedence;
2013 break;
2014 }
2015 case '+':
2016 case '-':
2017 {
2018 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
2019 (isalpha(c) != 0))
2020 precedence=AdditionPrecedence;
2021 break;
2022 }
2023 case LeftShiftOperator:
2024 case RightShiftOperator:
2025 {
2026 precedence=ShiftPrecedence;
2027 break;
2028 }
2029 case '<':
2030 case LessThanEqualOperator:
2031 case GreaterThanEqualOperator:
2032 case '>':
2033 {
2034 precedence=RelationalPrecedence;
2035 break;
2036 }
2037 case EqualOperator:
2038 case NotEqualOperator:
2039 {
2040 precedence=EquivalencyPrecedence;
2041 break;
2042 }
2043 case '&':
2044 {
2045 precedence=BitwiseAndPrecedence;
2046 break;
2047 }
2048 case '|':
2049 {
2050 precedence=BitwiseOrPrecedence;
2051 break;
2052 }
2053 case LogicalAndOperator:
2054 {
2055 precedence=LogicalAndPrecedence;
2056 break;
2057 }
2058 case LogicalOrOperator:
2059 {
2060 precedence=LogicalOrPrecedence;
2061 break;
2062 }
cristy116af162010-08-13 01:25:47 +00002063 case ExponentialNotation:
2064 {
2065 precedence=ExponentialNotationPrecedence;
2066 break;
2067 }
cristy3ed852e2009-09-05 21:47:34 +00002068 case ':':
2069 case '?':
2070 {
2071 precedence=TernaryPrecedence;
2072 break;
2073 }
2074 case '=':
2075 {
2076 precedence=AssignmentPrecedence;
2077 break;
2078 }
2079 case ',':
2080 {
2081 precedence=CommaPrecedence;
2082 break;
2083 }
2084 case ';':
2085 {
2086 precedence=SeparatorPrecedence;
2087 break;
2088 }
2089 }
2090 if ((precedence == BitwiseComplementPrecedence) ||
2091 (precedence == TernaryPrecedence) ||
2092 (precedence == AssignmentPrecedence))
2093 {
2094 if (precedence > target)
2095 {
2096 /*
2097 Right-to-left associativity.
2098 */
2099 target=precedence;
2100 subexpression=expression;
2101 }
2102 }
2103 else
2104 if (precedence >= target)
2105 {
2106 /*
2107 Left-to-right associativity.
2108 */
2109 target=precedence;
2110 subexpression=expression;
2111 }
2112 if (strchr("(",(int) *expression) != (char *) NULL)
2113 expression=FxSubexpression(expression,exception);
2114 c=(int) (*expression++);
2115 }
2116 return(subexpression);
2117}
2118
2119static MagickRealType FxEvaluateSubexpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002120 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002121 const char *expression,MagickRealType *beta,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002122{
2123 char
2124 *q,
2125 subexpression[MaxTextExtent];
2126
2127 MagickRealType
2128 alpha,
2129 gamma;
2130
2131 register const char
2132 *p;
2133
2134 *beta=0.0;
2135 if (exception->severity != UndefinedException)
2136 return(0.0);
2137 while (isspace((int) *expression) != 0)
2138 expression++;
2139 if (*expression == '\0')
2140 {
2141 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
2142 "MissingExpression","`%s'",expression);
2143 return(0.0);
2144 }
cristy66322f02010-05-17 11:40:48 +00002145 *subexpression='\0';
cristy3ed852e2009-09-05 21:47:34 +00002146 p=FxOperatorPrecedence(expression,exception);
2147 if (p != (const char *) NULL)
2148 {
2149 (void) CopyMagickString(subexpression,expression,(size_t)
2150 (p-expression+1));
2151 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2152 exception);
2153 switch ((unsigned char) *p)
2154 {
2155 case '~':
2156 {
2157 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002158 *beta=(MagickRealType) (~(size_t) *beta);
cristy3ed852e2009-09-05 21:47:34 +00002159 return(*beta);
2160 }
2161 case '!':
2162 {
2163 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2164 return(*beta == 0.0 ? 1.0 : 0.0);
2165 }
2166 case '^':
2167 {
2168 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2169 channel,x,y,++p,beta,exception));
2170 return(*beta);
2171 }
2172 case '*':
cristy116af162010-08-13 01:25:47 +00002173 case ExponentialNotation:
cristy3ed852e2009-09-05 21:47:34 +00002174 {
2175 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2176 return(alpha*(*beta));
2177 }
2178 case '/':
2179 {
2180 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2181 if (*beta == 0.0)
2182 {
2183 if (exception->severity == UndefinedException)
2184 (void) ThrowMagickException(exception,GetMagickModule(),
2185 OptionError,"DivideByZero","`%s'",expression);
2186 return(0.0);
2187 }
2188 return(alpha/(*beta));
2189 }
2190 case '%':
2191 {
2192 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2193 *beta=fabs(floor(((double) *beta)+0.5));
2194 if (*beta == 0.0)
2195 {
2196 (void) ThrowMagickException(exception,GetMagickModule(),
2197 OptionError,"DivideByZero","`%s'",expression);
2198 return(0.0);
2199 }
2200 return(fmod((double) alpha,(double) *beta));
2201 }
2202 case '+':
2203 {
2204 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2205 return(alpha+(*beta));
2206 }
2207 case '-':
2208 {
2209 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2210 return(alpha-(*beta));
2211 }
2212 case LeftShiftOperator:
2213 {
2214 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002215 *beta=(MagickRealType) ((size_t) (alpha+0.5) << (size_t)
cristy3ed852e2009-09-05 21:47:34 +00002216 (gamma+0.5));
2217 return(*beta);
2218 }
2219 case RightShiftOperator:
2220 {
2221 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002222 *beta=(MagickRealType) ((size_t) (alpha+0.5) >> (size_t)
cristy3ed852e2009-09-05 21:47:34 +00002223 (gamma+0.5));
2224 return(*beta);
2225 }
2226 case '<':
2227 {
2228 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2229 return(alpha < *beta ? 1.0 : 0.0);
2230 }
2231 case LessThanEqualOperator:
2232 {
2233 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2234 return(alpha <= *beta ? 1.0 : 0.0);
2235 }
2236 case '>':
2237 {
2238 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2239 return(alpha > *beta ? 1.0 : 0.0);
2240 }
2241 case GreaterThanEqualOperator:
2242 {
2243 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2244 return(alpha >= *beta ? 1.0 : 0.0);
2245 }
2246 case EqualOperator:
2247 {
2248 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2249 return(fabs(alpha-(*beta)) <= MagickEpsilon ? 1.0 : 0.0);
2250 }
2251 case NotEqualOperator:
2252 {
2253 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2254 return(fabs(alpha-(*beta)) > MagickEpsilon ? 1.0 : 0.0);
2255 }
2256 case '&':
2257 {
2258 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002259 *beta=(MagickRealType) ((size_t) (alpha+0.5) & (size_t)
cristy3ed852e2009-09-05 21:47:34 +00002260 (gamma+0.5));
2261 return(*beta);
2262 }
2263 case '|':
2264 {
2265 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002266 *beta=(MagickRealType) ((size_t) (alpha+0.5) | (size_t)
cristy3ed852e2009-09-05 21:47:34 +00002267 (gamma+0.5));
2268 return(*beta);
2269 }
2270 case LogicalAndOperator:
2271 {
2272 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2273 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2274 return(*beta);
2275 }
2276 case LogicalOrOperator:
2277 {
2278 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2279 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2280 return(*beta);
2281 }
2282 case '?':
2283 {
2284 MagickRealType
2285 gamma;
2286
2287 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2288 q=subexpression;
2289 p=StringToken(":",&q);
2290 if (q == (char *) NULL)
2291 {
2292 (void) ThrowMagickException(exception,GetMagickModule(),
2293 OptionError,"UnableToParseExpression","`%s'",subexpression);
2294 return(0.0);
2295 }
2296 if (fabs((double) alpha) > MagickEpsilon)
2297 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2298 else
2299 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2300 return(gamma);
2301 }
2302 case '=':
2303 {
2304 char
2305 numeric[MaxTextExtent];
2306
2307 q=subexpression;
2308 while (isalpha((int) ((unsigned char) *q)) != 0)
2309 q++;
2310 if (*q != '\0')
2311 {
2312 (void) ThrowMagickException(exception,GetMagickModule(),
2313 OptionError,"UnableToParseExpression","`%s'",subexpression);
2314 return(0.0);
2315 }
2316 ClearMagickException(exception);
2317 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristyb51dff52011-05-19 16:55:47 +00002318 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
cristy8cd5b312010-01-07 01:10:24 +00002319 *beta);
cristy3ed852e2009-09-05 21:47:34 +00002320 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2321 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2322 subexpression),ConstantString(numeric));
2323 return(*beta);
2324 }
2325 case ',':
2326 {
2327 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2328 return(alpha);
2329 }
2330 case ';':
2331 {
2332 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2333 return(*beta);
2334 }
2335 default:
2336 {
2337 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2338 exception);
2339 return(gamma);
2340 }
2341 }
2342 }
2343 if (strchr("(",(int) *expression) != (char *) NULL)
2344 {
2345 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2346 subexpression[strlen(subexpression)-1]='\0';
2347 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2348 exception);
2349 return(gamma);
2350 }
cristy8b8a3ae2010-10-23 18:49:46 +00002351 switch (*expression)
cristy3ed852e2009-09-05 21:47:34 +00002352 {
2353 case '+':
2354 {
2355 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2356 exception);
2357 return(1.0*gamma);
2358 }
2359 case '-':
2360 {
2361 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2362 exception);
2363 return(-1.0*gamma);
2364 }
2365 case '~':
2366 {
2367 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2368 exception);
cristybb503372010-05-27 20:51:26 +00002369 return((MagickRealType) (~(size_t) (gamma+0.5)));
cristy3ed852e2009-09-05 21:47:34 +00002370 }
2371 case 'A':
2372 case 'a':
2373 {
2374 if (LocaleNCompare(expression,"abs",3) == 0)
2375 {
2376 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2377 exception);
2378 return((MagickRealType) fabs((double) alpha));
2379 }
cristyb33454f2011-08-03 02:10:45 +00002380#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00002381 if (LocaleNCompare(expression,"acosh",5) == 0)
2382 {
2383 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2384 exception);
2385 return((MagickRealType) acosh((double) alpha));
2386 }
cristyb33454f2011-08-03 02:10:45 +00002387#endif
cristy3ed852e2009-09-05 21:47:34 +00002388 if (LocaleNCompare(expression,"acos",4) == 0)
2389 {
2390 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2391 exception);
2392 return((MagickRealType) acos((double) alpha));
2393 }
cristy43c22f42010-03-30 12:34:07 +00002394#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002395 if (LocaleNCompare(expression,"airy",4) == 0)
2396 {
2397 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2398 exception);
2399 if (alpha == 0.0)
cristy2dd03222010-03-30 22:12:11 +00002400 return(1.0);
2401 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
cristy43c22f42010-03-30 12:34:07 +00002402 return(gamma*gamma);
cristyee56cf12010-03-01 22:17:06 +00002403 }
cristy43c22f42010-03-30 12:34:07 +00002404#endif
cristyb33454f2011-08-03 02:10:45 +00002405#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00002406 if (LocaleNCompare(expression,"asinh",5) == 0)
2407 {
2408 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2409 exception);
2410 return((MagickRealType) asinh((double) alpha));
2411 }
cristyb33454f2011-08-03 02:10:45 +00002412#endif
cristy3ed852e2009-09-05 21:47:34 +00002413 if (LocaleNCompare(expression,"asin",4) == 0)
2414 {
2415 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2416 exception);
2417 return((MagickRealType) asin((double) alpha));
2418 }
2419 if (LocaleNCompare(expression,"alt",3) == 0)
2420 {
2421 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2422 exception);
cristybb503372010-05-27 20:51:26 +00002423 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
cristy3ed852e2009-09-05 21:47:34 +00002424 }
2425 if (LocaleNCompare(expression,"atan2",5) == 0)
2426 {
2427 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2428 exception);
2429 return((MagickRealType) atan2((double) alpha,(double) *beta));
2430 }
cristyb33454f2011-08-03 02:10:45 +00002431#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00002432 if (LocaleNCompare(expression,"atanh",5) == 0)
2433 {
2434 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2435 exception);
2436 return((MagickRealType) atanh((double) alpha));
2437 }
cristyb33454f2011-08-03 02:10:45 +00002438#endif
cristy3ed852e2009-09-05 21:47:34 +00002439 if (LocaleNCompare(expression,"atan",4) == 0)
2440 {
2441 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2442 exception);
2443 return((MagickRealType) atan((double) alpha));
2444 }
2445 if (LocaleCompare(expression,"a") == 0)
2446 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2447 break;
2448 }
2449 case 'B':
2450 case 'b':
2451 {
2452 if (LocaleCompare(expression,"b") == 0)
2453 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2454 break;
2455 }
2456 case 'C':
2457 case 'c':
2458 {
2459 if (LocaleNCompare(expression,"ceil",4) == 0)
2460 {
2461 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2462 exception);
2463 return((MagickRealType) ceil((double) alpha));
2464 }
2465 if (LocaleNCompare(expression,"cosh",4) == 0)
2466 {
2467 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2468 exception);
2469 return((MagickRealType) cosh((double) alpha));
2470 }
2471 if (LocaleNCompare(expression,"cos",3) == 0)
2472 {
2473 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2474 exception);
2475 return((MagickRealType) cos((double) alpha));
2476 }
2477 if (LocaleCompare(expression,"c") == 0)
2478 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2479 break;
2480 }
2481 case 'D':
2482 case 'd':
2483 {
2484 if (LocaleNCompare(expression,"debug",5) == 0)
2485 {
2486 const char
2487 *type;
2488
2489 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2490 exception);
2491 if (fx_info->images->colorspace == CMYKColorspace)
2492 switch (channel)
2493 {
cristy0568ffc2011-07-25 16:54:14 +00002494 case CyanPixelChannel: type="cyan"; break;
2495 case MagentaPixelChannel: type="magenta"; break;
2496 case YellowPixelChannel: type="yellow"; break;
2497 case AlphaPixelChannel: type="opacity"; break;
2498 case BlackPixelChannel: type="black"; break;
cristy3ed852e2009-09-05 21:47:34 +00002499 default: type="unknown"; break;
2500 }
2501 else
2502 switch (channel)
2503 {
cristy0568ffc2011-07-25 16:54:14 +00002504 case RedPixelChannel: type="red"; break;
2505 case GreenPixelChannel: type="green"; break;
2506 case BluePixelChannel: type="blue"; break;
2507 case AlphaPixelChannel: type="opacity"; break;
cristy3ed852e2009-09-05 21:47:34 +00002508 default: type="unknown"; break;
2509 }
2510 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2511 if (strlen(subexpression) > 1)
2512 subexpression[strlen(subexpression)-1]='\0';
2513 if (fx_info->file != (FILE *) NULL)
cristy1e604812011-05-19 18:07:50 +00002514 (void) FormatLocaleFile(fx_info->file,
2515 "%s[%.20g,%.20g].%s: %s=%.*g\n",fx_info->images->filename,
2516 (double) x,(double) y,type,subexpression,GetMagickPrecision(),
2517 (double) alpha);
cristy3ed852e2009-09-05 21:47:34 +00002518 return(0.0);
2519 }
2520 break;
2521 }
2522 case 'E':
2523 case 'e':
2524 {
2525 if (LocaleCompare(expression,"epsilon") == 0)
2526 return((MagickRealType) MagickEpsilon);
2527 if (LocaleNCompare(expression,"exp",3) == 0)
2528 {
2529 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2530 exception);
2531 return((MagickRealType) exp((double) alpha));
2532 }
2533 if (LocaleCompare(expression,"e") == 0)
2534 return((MagickRealType) 2.7182818284590452354);
2535 break;
2536 }
2537 case 'F':
2538 case 'f':
2539 {
2540 if (LocaleNCompare(expression,"floor",5) == 0)
2541 {
2542 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2543 exception);
2544 return((MagickRealType) floor((double) alpha));
2545 }
2546 break;
2547 }
2548 case 'G':
2549 case 'g':
2550 {
cristy9eeedea2011-11-02 19:04:05 +00002551 if (LocaleNCompare(expression,"gauss",5) == 0)
2552 {
2553 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2554 exception);
2555 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
2556 return((MagickRealType) gamma);
2557 }
cristyb0aad4c2011-11-02 19:30:35 +00002558 if (LocaleNCompare(expression,"gcd",3) == 0)
2559 {
2560 MagickOffsetType
2561 gcd;
2562
2563 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2564 exception);
cristy76461162011-11-02 19:32:19 +00002565 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType)
2566 (*beta+0.5));
cristyb0aad4c2011-11-02 19:30:35 +00002567 return((MagickRealType) gcd);
2568 }
cristy3ed852e2009-09-05 21:47:34 +00002569 if (LocaleCompare(expression,"g") == 0)
2570 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2571 break;
2572 }
2573 case 'H':
2574 case 'h':
2575 {
2576 if (LocaleCompare(expression,"h") == 0)
2577 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2578 if (LocaleCompare(expression,"hue") == 0)
2579 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2580 if (LocaleNCompare(expression,"hypot",5) == 0)
2581 {
2582 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2583 exception);
2584 return((MagickRealType) hypot((double) alpha,(double) *beta));
2585 }
2586 break;
2587 }
2588 case 'K':
2589 case 'k':
2590 {
2591 if (LocaleCompare(expression,"k") == 0)
2592 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2593 break;
2594 }
2595 case 'I':
2596 case 'i':
2597 {
2598 if (LocaleCompare(expression,"intensity") == 0)
2599 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2600 if (LocaleNCompare(expression,"int",3) == 0)
2601 {
2602 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2603 exception);
cristy16788e42010-08-13 13:44:26 +00002604 return((MagickRealType) floor(alpha));
cristy3ed852e2009-09-05 21:47:34 +00002605 }
cristy639399c2011-11-02 19:16:15 +00002606 if (LocaleNCompare(expression,"isnan",5) == 0)
2607 {
2608 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2609 exception);
cristy17a10202011-11-02 19:17:04 +00002610 return((MagickRealType) !!isnan((double) alpha));
cristy639399c2011-11-02 19:16:15 +00002611 }
cristy3ed852e2009-09-05 21:47:34 +00002612 if (LocaleCompare(expression,"i") == 0)
2613 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2614 break;
2615 }
2616 case 'J':
2617 case 'j':
2618 {
2619 if (LocaleCompare(expression,"j") == 0)
2620 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
cristy161b9262010-03-20 19:34:32 +00002621#if defined(MAGICKCORE_HAVE_J0)
cristyee56cf12010-03-01 22:17:06 +00002622 if (LocaleNCompare(expression,"j0",2) == 0)
2623 {
2624 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2625 exception);
2626 return((MagickRealType) j0((double) alpha));
2627 }
cristy161b9262010-03-20 19:34:32 +00002628#endif
2629#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002630 if (LocaleNCompare(expression,"j1",2) == 0)
2631 {
2632 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2633 exception);
2634 return((MagickRealType) j1((double) alpha));
2635 }
cristy161b9262010-03-20 19:34:32 +00002636#endif
cristyaa018fa2010-04-08 23:03:54 +00002637#if defined(MAGICKCORE_HAVE_J1)
cristya6a09e72010-03-02 14:51:02 +00002638 if (LocaleNCompare(expression,"jinc",4) == 0)
2639 {
2640 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2641 exception);
cristy0946a822010-03-12 17:14:58 +00002642 if (alpha == 0.0)
2643 return(1.0);
cristy69928f92010-03-12 13:27:09 +00002644 gamma=(MagickRealType) (2.0*j1((double) (MagickPI*alpha))/
cristyfce2f7b2010-03-12 00:29:49 +00002645 (MagickPI*alpha));
2646 return(gamma);
cristya6a09e72010-03-02 14:51:02 +00002647 }
cristyaa018fa2010-04-08 23:03:54 +00002648#endif
cristy3ed852e2009-09-05 21:47:34 +00002649 break;
2650 }
2651 case 'L':
2652 case 'l':
2653 {
2654 if (LocaleNCompare(expression,"ln",2) == 0)
2655 {
2656 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2657 exception);
2658 return((MagickRealType) log((double) alpha));
2659 }
cristyc8ed5322010-08-31 12:07:59 +00002660 if (LocaleNCompare(expression,"logtwo",6) == 0)
cristy3ed852e2009-09-05 21:47:34 +00002661 {
cristyc8ed5322010-08-31 12:07:59 +00002662 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
cristy3ed852e2009-09-05 21:47:34 +00002663 exception);
2664 return((MagickRealType) log10((double) alpha))/log10(2.0);
2665 }
2666 if (LocaleNCompare(expression,"log",3) == 0)
2667 {
2668 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2669 exception);
2670 return((MagickRealType) log10((double) alpha));
2671 }
2672 if (LocaleCompare(expression,"lightness") == 0)
2673 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2674 break;
2675 }
2676 case 'M':
2677 case 'm':
2678 {
2679 if (LocaleCompare(expression,"MaxRGB") == 0)
2680 return((MagickRealType) QuantumRange);
2681 if (LocaleNCompare(expression,"maxima",6) == 0)
2682 break;
2683 if (LocaleNCompare(expression,"max",3) == 0)
2684 return(FxMax(fx_info,channel,x,y,expression+3,exception));
2685 if (LocaleNCompare(expression,"minima",6) == 0)
2686 break;
2687 if (LocaleNCompare(expression,"min",3) == 0)
2688 return(FxMin(fx_info,channel,x,y,expression+3,exception));
2689 if (LocaleNCompare(expression,"mod",3) == 0)
2690 {
2691 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2692 exception);
2693 return((MagickRealType) fmod((double) alpha,(double) *beta));
2694 }
2695 if (LocaleCompare(expression,"m") == 0)
2696 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2697 break;
2698 }
2699 case 'N':
2700 case 'n':
2701 {
cristyad3502e2011-11-02 19:10:45 +00002702 if (LocaleNCompare(expression,"not",3) == 0)
2703 {
2704 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2705 exception);
2706 return((MagickRealType) (alpha < MagickEpsilon));
2707 }
cristy3ed852e2009-09-05 21:47:34 +00002708 if (LocaleCompare(expression,"n") == 0)
2709 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2710 break;
2711 }
2712 case 'O':
2713 case 'o':
2714 {
2715 if (LocaleCompare(expression,"Opaque") == 0)
2716 return(1.0);
2717 if (LocaleCompare(expression,"o") == 0)
2718 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2719 break;
2720 }
2721 case 'P':
2722 case 'p':
2723 {
cristy670aa3c2011-11-03 00:54:00 +00002724 if (LocaleCompare(expression,"phi") == 0)
2725 return((MagickRealType) MagickPHI);
cristy3ed852e2009-09-05 21:47:34 +00002726 if (LocaleCompare(expression,"pi") == 0)
2727 return((MagickRealType) MagickPI);
2728 if (LocaleNCompare(expression,"pow",3) == 0)
2729 {
2730 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2731 exception);
2732 return((MagickRealType) pow((double) alpha,(double) *beta));
2733 }
2734 if (LocaleCompare(expression,"p") == 0)
2735 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2736 break;
2737 }
2738 case 'Q':
2739 case 'q':
2740 {
2741 if (LocaleCompare(expression,"QuantumRange") == 0)
2742 return((MagickRealType) QuantumRange);
2743 if (LocaleCompare(expression,"QuantumScale") == 0)
2744 return((MagickRealType) QuantumScale);
2745 break;
2746 }
2747 case 'R':
2748 case 'r':
2749 {
2750 if (LocaleNCompare(expression,"rand",4) == 0)
2751 return((MagickRealType) GetPseudoRandomValue(fx_info->random_info));
2752 if (LocaleNCompare(expression,"round",5) == 0)
2753 {
2754 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2755 exception);
cristy16788e42010-08-13 13:44:26 +00002756 return((MagickRealType) floor((double) alpha+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002757 }
2758 if (LocaleCompare(expression,"r") == 0)
2759 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2760 break;
2761 }
2762 case 'S':
2763 case 's':
2764 {
2765 if (LocaleCompare(expression,"saturation") == 0)
2766 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2767 if (LocaleNCompare(expression,"sign",4) == 0)
2768 {
2769 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2770 exception);
2771 return(alpha < 0.0 ? -1.0 : 1.0);
2772 }
cristya6a09e72010-03-02 14:51:02 +00002773 if (LocaleNCompare(expression,"sinc",4) == 0)
2774 {
2775 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2776 exception);
2777 if (alpha == 0)
2778 return(1.0);
2779 gamma=(MagickRealType) (sin((double) (MagickPI*alpha))/
2780 (MagickPI*alpha));
2781 return(gamma);
2782 }
cristy3ed852e2009-09-05 21:47:34 +00002783 if (LocaleNCompare(expression,"sinh",4) == 0)
2784 {
2785 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2786 exception);
2787 return((MagickRealType) sinh((double) alpha));
2788 }
2789 if (LocaleNCompare(expression,"sin",3) == 0)
2790 {
2791 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2792 exception);
2793 return((MagickRealType) sin((double) alpha));
2794 }
2795 if (LocaleNCompare(expression,"sqrt",4) == 0)
2796 {
2797 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2798 exception);
2799 return((MagickRealType) sqrt((double) alpha));
2800 }
cristy9eeedea2011-11-02 19:04:05 +00002801 if (LocaleNCompare(expression,"squish",6) == 0)
2802 {
2803 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2804 exception);
2805 return((MagickRealType) (1.0/(1.0+exp((double) (4.0*alpha)))));
2806 }
cristy3ed852e2009-09-05 21:47:34 +00002807 if (LocaleCompare(expression,"s") == 0)
2808 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2809 break;
2810 }
2811 case 'T':
2812 case 't':
2813 {
2814 if (LocaleNCompare(expression,"tanh",4) == 0)
2815 {
2816 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2817 exception);
2818 return((MagickRealType) tanh((double) alpha));
2819 }
2820 if (LocaleNCompare(expression,"tan",3) == 0)
2821 {
2822 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2823 exception);
2824 return((MagickRealType) tan((double) alpha));
2825 }
2826 if (LocaleCompare(expression,"Transparent") == 0)
2827 return(0.0);
cristy16788e42010-08-13 13:44:26 +00002828 if (LocaleNCompare(expression,"trunc",5) == 0)
2829 {
2830 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2831 exception);
2832 if (alpha >= 0.0)
2833 return((MagickRealType) floor((double) alpha));
2834 return((MagickRealType) ceil((double) alpha));
2835 }
cristy3ed852e2009-09-05 21:47:34 +00002836 if (LocaleCompare(expression,"t") == 0)
2837 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2838 break;
2839 }
2840 case 'U':
2841 case 'u':
2842 {
2843 if (LocaleCompare(expression,"u") == 0)
2844 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2845 break;
2846 }
2847 case 'V':
2848 case 'v':
2849 {
2850 if (LocaleCompare(expression,"v") == 0)
2851 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2852 break;
2853 }
2854 case 'W':
2855 case 'w':
2856 {
cristy9eeedea2011-11-02 19:04:05 +00002857 if (LocaleNCompare(expression,"while",5) == 0)
2858 {
2859 do
2860 {
2861 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2862 exception);
2863 } while (fabs((double) alpha) >= MagickEpsilon);
2864 return((MagickRealType) *beta);
2865 }
cristy3ed852e2009-09-05 21:47:34 +00002866 if (LocaleCompare(expression,"w") == 0)
2867 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2868 break;
2869 }
2870 case 'Y':
2871 case 'y':
2872 {
2873 if (LocaleCompare(expression,"y") == 0)
2874 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2875 break;
2876 }
2877 case 'Z':
2878 case 'z':
2879 {
2880 if (LocaleCompare(expression,"z") == 0)
2881 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2882 break;
2883 }
2884 default:
2885 break;
2886 }
2887 q=(char *) expression;
cristyc1acd842011-05-19 23:05:47 +00002888 alpha=InterpretLocaleValue(expression,&q);
cristy3ed852e2009-09-05 21:47:34 +00002889 if (q == expression)
2890 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2891 return(alpha);
2892}
2893
cristy7832dc22011-09-05 01:21:53 +00002894MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
cristy3ed852e2009-09-05 21:47:34 +00002895 MagickRealType *alpha,ExceptionInfo *exception)
2896{
2897 MagickBooleanType
2898 status;
2899
cristy541ae572011-08-05 19:08:59 +00002900 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2901 exception);
cristy3ed852e2009-09-05 21:47:34 +00002902 return(status);
2903}
2904
2905MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
2906 MagickRealType *alpha,ExceptionInfo *exception)
2907{
2908 FILE
2909 *file;
2910
2911 MagickBooleanType
2912 status;
2913
2914 file=fx_info->file;
2915 fx_info->file=(FILE *) NULL;
cristy541ae572011-08-05 19:08:59 +00002916 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2917 exception);
cristy3ed852e2009-09-05 21:47:34 +00002918 fx_info->file=file;
2919 return(status);
2920}
2921
cristy7832dc22011-09-05 01:21:53 +00002922MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002923 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002924 MagickRealType *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002925{
2926 MagickRealType
2927 beta;
2928
2929 beta=0.0;
2930 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2931 exception);
2932 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2933}
2934
2935/*
2936%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2937% %
2938% %
2939% %
2940% F x I m a g e %
2941% %
2942% %
2943% %
2944%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2945%
2946% FxImage() applies a mathematical expression to the specified image.
2947%
2948% The format of the FxImage method is:
2949%
2950% Image *FxImage(const Image *image,const char *expression,
2951% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002952%
2953% A description of each parameter follows:
2954%
2955% o image: the image.
2956%
cristy3ed852e2009-09-05 21:47:34 +00002957% o expression: A mathematical expression.
2958%
2959% o exception: return any errors or warnings in this structure.
2960%
2961*/
2962
2963static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2964{
cristybb503372010-05-27 20:51:26 +00002965 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002966 i;
2967
2968 assert(fx_info != (FxInfo **) NULL);
cristybb503372010-05-27 20:51:26 +00002969 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
cristy3ed852e2009-09-05 21:47:34 +00002970 if (fx_info[i] != (FxInfo *) NULL)
2971 fx_info[i]=DestroyFxInfo(fx_info[i]);
cristyb41ee102010-10-04 16:46:15 +00002972 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
cristy3ed852e2009-09-05 21:47:34 +00002973 return(fx_info);
2974}
2975
2976static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2977 ExceptionInfo *exception)
2978{
2979 char
2980 *fx_expression;
2981
2982 FxInfo
2983 **fx_info;
2984
2985 MagickRealType
2986 alpha;
2987
cristybb503372010-05-27 20:51:26 +00002988 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002989 i;
2990
cristybb503372010-05-27 20:51:26 +00002991 size_t
cristy3ed852e2009-09-05 21:47:34 +00002992 number_threads;
2993
2994 number_threads=GetOpenMPMaximumThreads();
cristyb41ee102010-10-04 16:46:15 +00002995 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +00002996 if (fx_info == (FxInfo **) NULL)
2997 return((FxInfo **) NULL);
2998 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2999 if (*expression != '@')
3000 fx_expression=ConstantString(expression);
3001 else
3002 fx_expression=FileToString(expression+1,~0,exception);
cristybb503372010-05-27 20:51:26 +00003003 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +00003004 {
3005 fx_info[i]=AcquireFxInfo(image,fx_expression);
3006 if (fx_info[i] == (FxInfo *) NULL)
3007 return(DestroyFxThreadSet(fx_info));
3008 (void) FxPreprocessExpression(fx_info[i],&alpha,fx_info[i]->exception);
3009 }
3010 fx_expression=DestroyString(fx_expression);
3011 return(fx_info);
3012}
3013
3014MagickExport Image *FxImage(const Image *image,const char *expression,
3015 ExceptionInfo *exception)
3016{
cristy3ed852e2009-09-05 21:47:34 +00003017#define FxImageTag "Fx/Image"
3018
cristyfa112112010-01-04 17:48:07 +00003019 CacheView
cristy79cedc72011-07-25 00:41:15 +00003020 *fx_view,
3021 *image_view;
cristyfa112112010-01-04 17:48:07 +00003022
cristy3ed852e2009-09-05 21:47:34 +00003023 FxInfo
cristyfa112112010-01-04 17:48:07 +00003024 **restrict fx_info;
cristy3ed852e2009-09-05 21:47:34 +00003025
3026 Image
3027 *fx_image;
3028
cristy3ed852e2009-09-05 21:47:34 +00003029 MagickBooleanType
3030 status;
3031
cristybb503372010-05-27 20:51:26 +00003032 MagickOffsetType
3033 progress;
3034
cristy3ed852e2009-09-05 21:47:34 +00003035 MagickRealType
3036 alpha;
3037
cristybb503372010-05-27 20:51:26 +00003038 ssize_t
3039 y;
3040
cristy3ed852e2009-09-05 21:47:34 +00003041 assert(image != (Image *) NULL);
3042 assert(image->signature == MagickSignature);
3043 if (image->debug != MagickFalse)
3044 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy79cedc72011-07-25 00:41:15 +00003045 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00003046 if (fx_image == (Image *) NULL)
3047 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003048 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003049 {
cristy3ed852e2009-09-05 21:47:34 +00003050 fx_image=DestroyImage(fx_image);
3051 return((Image *) NULL);
3052 }
3053 fx_info=AcquireFxThreadSet(image,expression,exception);
3054 if (fx_info == (FxInfo **) NULL)
3055 {
3056 fx_image=DestroyImage(fx_image);
3057 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3058 }
3059 status=FxPreprocessExpression(fx_info[0],&alpha,exception);
3060 if (status == MagickFalse)
3061 {
3062 fx_image=DestroyImage(fx_image);
3063 fx_info=DestroyFxThreadSet(fx_info);
3064 return((Image *) NULL);
3065 }
3066 /*
3067 Fx image.
3068 */
3069 status=MagickTrue;
3070 progress=0;
cristy79cedc72011-07-25 00:41:15 +00003071 image_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00003072 fx_view=AcquireCacheView(fx_image);
cristyb5d5f722009-11-04 03:03:49 +00003073#if defined(MAGICKCORE_OPENMP_SUPPORT)
3074 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003075#endif
cristybb503372010-05-27 20:51:26 +00003076 for (y=0; y < (ssize_t) fx_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003077 {
cristy5c9e6f22010-09-17 17:31:01 +00003078 const int
3079 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003080
cristy79cedc72011-07-25 00:41:15 +00003081 register const Quantum
3082 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003083
cristy4c08aed2011-07-01 19:47:50 +00003084 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003085 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003086
cristy79cedc72011-07-25 00:41:15 +00003087 register ssize_t
3088 x;
3089
cristy3ed852e2009-09-05 21:47:34 +00003090 if (status == MagickFalse)
3091 continue;
cristy79cedc72011-07-25 00:41:15 +00003092 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy3ed852e2009-09-05 21:47:34 +00003093 q=GetCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
cristy79cedc72011-07-25 00:41:15 +00003094 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003095 {
3096 status=MagickFalse;
3097 continue;
3098 }
cristybb503372010-05-27 20:51:26 +00003099 for (x=0; x < (ssize_t) fx_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003100 {
cristy79cedc72011-07-25 00:41:15 +00003101 register ssize_t
3102 i;
3103
3104 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3105 {
3106 MagickRealType
3107 alpha;
3108
3109 PixelChannel
3110 channel;
3111
3112 PixelTrait
3113 fx_traits,
3114 traits;
3115
3116 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
cristy79cedc72011-07-25 00:41:15 +00003117 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3118 fx_traits=GetPixelChannelMapTraits(fx_image,channel);
cristy010d7d12011-08-31 01:02:48 +00003119 if ((traits == UndefinedPixelTrait) ||
3120 (fx_traits == UndefinedPixelTrait))
cristy79cedc72011-07-25 00:41:15 +00003121 continue;
3122 if ((fx_traits & CopyPixelTrait) != 0)
3123 {
cristy0beccfa2011-09-25 20:47:53 +00003124 SetPixelChannel(fx_image,channel,p[i],q);
cristy79cedc72011-07-25 00:41:15 +00003125 continue;
3126 }
3127 alpha=0.0;
cristy0568ffc2011-07-25 16:54:14 +00003128 (void) FxEvaluateChannelExpression(fx_info[id],(PixelChannel) i,x,y,
3129 &alpha,exception);
cristyb3a73b52011-07-26 01:34:43 +00003130 q[i]=ClampToQuantum((MagickRealType) QuantumRange*alpha);
cristy79cedc72011-07-25 00:41:15 +00003131 }
3132 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003133 q+=GetPixelChannels(fx_image);
cristy3ed852e2009-09-05 21:47:34 +00003134 }
3135 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3136 status=MagickFalse;
3137 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3138 {
3139 MagickBooleanType
3140 proceed;
3141
cristyb5d5f722009-11-04 03:03:49 +00003142#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy490408a2011-07-07 14:42:05 +00003143 #pragma omp critical (MagickCore_FxImage)
cristy3ed852e2009-09-05 21:47:34 +00003144#endif
3145 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3146 if (proceed == MagickFalse)
3147 status=MagickFalse;
3148 }
3149 }
cristy3ed852e2009-09-05 21:47:34 +00003150 fx_view=DestroyCacheView(fx_view);
cristy79cedc72011-07-25 00:41:15 +00003151 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003152 fx_info=DestroyFxThreadSet(fx_info);
3153 if (status == MagickFalse)
3154 fx_image=DestroyImage(fx_image);
3155 return(fx_image);
3156}
3157
3158/*
3159%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3160% %
3161% %
3162% %
3163% I m p l o d e I m a g e %
3164% %
3165% %
3166% %
3167%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3168%
3169% ImplodeImage() creates a new image that is a copy of an existing
3170% one with the image pixels "implode" by the specified percentage. It
3171% allocates the memory necessary for the new Image structure and returns a
3172% pointer to the new image.
3173%
3174% The format of the ImplodeImage method is:
3175%
3176% Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003177% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003178%
3179% A description of each parameter follows:
3180%
3181% o implode_image: Method ImplodeImage returns a pointer to the image
3182% after it is implode. A null image is returned if there is a memory
3183% shortage.
3184%
3185% o image: the image.
3186%
3187% o amount: Define the extent of the implosion.
3188%
cristy76f512e2011-09-12 01:26:56 +00003189% o method: the pixel interpolation method.
3190%
cristy3ed852e2009-09-05 21:47:34 +00003191% o exception: return any errors or warnings in this structure.
3192%
3193*/
3194MagickExport Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003195 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003196{
3197#define ImplodeImageTag "Implode/Image"
3198
cristyfa112112010-01-04 17:48:07 +00003199 CacheView
3200 *image_view,
3201 *implode_view;
3202
cristy3ed852e2009-09-05 21:47:34 +00003203 Image
3204 *implode_image;
3205
cristy3ed852e2009-09-05 21:47:34 +00003206 MagickBooleanType
3207 status;
3208
cristybb503372010-05-27 20:51:26 +00003209 MagickOffsetType
3210 progress;
3211
cristy3ed852e2009-09-05 21:47:34 +00003212 MagickRealType
3213 radius;
3214
3215 PointInfo
3216 center,
3217 scale;
3218
cristybb503372010-05-27 20:51:26 +00003219 ssize_t
3220 y;
3221
cristy3ed852e2009-09-05 21:47:34 +00003222 /*
3223 Initialize implode image attributes.
3224 */
3225 assert(image != (Image *) NULL);
3226 assert(image->signature == MagickSignature);
3227 if (image->debug != MagickFalse)
3228 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3229 assert(exception != (ExceptionInfo *) NULL);
3230 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00003231 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3232 exception);
cristy3ed852e2009-09-05 21:47:34 +00003233 if (implode_image == (Image *) NULL)
3234 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003235 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003236 {
cristy3ed852e2009-09-05 21:47:34 +00003237 implode_image=DestroyImage(implode_image);
3238 return((Image *) NULL);
3239 }
cristy4c08aed2011-07-01 19:47:50 +00003240 if (implode_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00003241 implode_image->matte=MagickTrue;
3242 /*
3243 Compute scaling factor.
3244 */
3245 scale.x=1.0;
3246 scale.y=1.0;
3247 center.x=0.5*image->columns;
3248 center.y=0.5*image->rows;
3249 radius=center.x;
3250 if (image->columns > image->rows)
3251 scale.y=(double) image->columns/(double) image->rows;
3252 else
3253 if (image->columns < image->rows)
3254 {
3255 scale.x=(double) image->rows/(double) image->columns;
3256 radius=center.y;
3257 }
3258 /*
3259 Implode image.
3260 */
3261 status=MagickTrue;
3262 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00003263 image_view=AcquireCacheView(image);
3264 implode_view=AcquireCacheView(implode_image);
cristyb5d5f722009-11-04 03:03:49 +00003265#if defined(MAGICKCORE_OPENMP_SUPPORT)
3266 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003267#endif
cristybb503372010-05-27 20:51:26 +00003268 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003269 {
cristy3ed852e2009-09-05 21:47:34 +00003270 MagickRealType
3271 distance;
3272
3273 PointInfo
3274 delta;
3275
cristy6d188022011-09-12 13:23:33 +00003276 register const Quantum
3277 *restrict p;
3278
cristybb503372010-05-27 20:51:26 +00003279 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003280 x;
3281
cristy4c08aed2011-07-01 19:47:50 +00003282 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003283 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003284
3285 if (status == MagickFalse)
3286 continue;
cristy6d188022011-09-12 13:23:33 +00003287 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy3ed852e2009-09-05 21:47:34 +00003288 q=GetCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
3289 exception);
cristy6d188022011-09-12 13:23:33 +00003290 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003291 {
3292 status=MagickFalse;
3293 continue;
3294 }
cristy3ed852e2009-09-05 21:47:34 +00003295 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00003296 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003297 {
cristy6d188022011-09-12 13:23:33 +00003298 register ssize_t
3299 i;
3300
cristy3ed852e2009-09-05 21:47:34 +00003301 /*
3302 Determine if the pixel is within an ellipse.
3303 */
3304 delta.x=scale.x*(double) (x-center.x);
3305 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00003306 if (distance >= (radius*radius))
3307 {
3308 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3309 q[i]=p[i];
3310 }
3311 else
cristy3ed852e2009-09-05 21:47:34 +00003312 {
3313 double
3314 factor;
3315
3316 /*
3317 Implode the pixel.
3318 */
3319 factor=1.0;
3320 if (distance > 0.0)
3321 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/
3322 radius/2)),-amount);
cristy76f512e2011-09-12 01:26:56 +00003323 status=InterpolatePixelChannels(image,image_view,implode_image,method,
3324 (double) (factor*delta.x/scale.x+center.x),(double) (factor*delta.y/
3325 scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00003326 }
cristy6d188022011-09-12 13:23:33 +00003327 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003328 q+=GetPixelChannels(implode_image);
cristy3ed852e2009-09-05 21:47:34 +00003329 }
3330 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3331 status=MagickFalse;
3332 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3333 {
3334 MagickBooleanType
3335 proceed;
3336
cristyb5d5f722009-11-04 03:03:49 +00003337#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003338 #pragma omp critical (MagickCore_ImplodeImage)
3339#endif
3340 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3341 if (proceed == MagickFalse)
3342 status=MagickFalse;
3343 }
3344 }
3345 implode_view=DestroyCacheView(implode_view);
3346 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003347 if (status == MagickFalse)
3348 implode_image=DestroyImage(implode_image);
3349 return(implode_image);
3350}
3351
3352/*
3353%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3354% %
3355% %
3356% %
3357% M o r p h I m a g e s %
3358% %
3359% %
3360% %
3361%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3362%
3363% The MorphImages() method requires a minimum of two images. The first
3364% image is transformed into the second by a number of intervening images
3365% as specified by frames.
3366%
3367% The format of the MorphImage method is:
3368%
cristybb503372010-05-27 20:51:26 +00003369% Image *MorphImages(const Image *image,const size_t number_frames,
cristy3ed852e2009-09-05 21:47:34 +00003370% ExceptionInfo *exception)
3371%
3372% A description of each parameter follows:
3373%
3374% o image: the image.
3375%
3376% o number_frames: Define the number of in-between image to generate.
3377% The more in-between frames, the smoother the morph.
3378%
3379% o exception: return any errors or warnings in this structure.
3380%
3381*/
3382MagickExport Image *MorphImages(const Image *image,
cristybb503372010-05-27 20:51:26 +00003383 const size_t number_frames,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003384{
3385#define MorphImageTag "Morph/Image"
3386
3387 Image
3388 *morph_image,
3389 *morph_images;
3390
cristy9d314ff2011-03-09 01:30:28 +00003391 MagickBooleanType
3392 status;
cristy3ed852e2009-09-05 21:47:34 +00003393
3394 MagickOffsetType
3395 scene;
3396
3397 MagickRealType
3398 alpha,
3399 beta;
3400
3401 register const Image
3402 *next;
3403
cristybb503372010-05-27 20:51:26 +00003404 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003405 i;
3406
cristy9d314ff2011-03-09 01:30:28 +00003407 ssize_t
3408 y;
cristy3ed852e2009-09-05 21:47:34 +00003409
3410 /*
3411 Clone first frame in sequence.
3412 */
3413 assert(image != (Image *) NULL);
3414 assert(image->signature == MagickSignature);
3415 if (image->debug != MagickFalse)
3416 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3417 assert(exception != (ExceptionInfo *) NULL);
3418 assert(exception->signature == MagickSignature);
3419 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3420 if (morph_images == (Image *) NULL)
3421 return((Image *) NULL);
3422 if (GetNextImageInList(image) == (Image *) NULL)
3423 {
3424 /*
3425 Morph single image.
3426 */
cristybb503372010-05-27 20:51:26 +00003427 for (i=1; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003428 {
3429 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3430 if (morph_image == (Image *) NULL)
3431 {
3432 morph_images=DestroyImageList(morph_images);
3433 return((Image *) NULL);
3434 }
3435 AppendImageToList(&morph_images,morph_image);
cristy8b27a6d2010-02-14 03:31:15 +00003436 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003437 {
cristy8b27a6d2010-02-14 03:31:15 +00003438 MagickBooleanType
3439 proceed;
3440
cristybb503372010-05-27 20:51:26 +00003441 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3442 number_frames);
cristy8b27a6d2010-02-14 03:31:15 +00003443 if (proceed == MagickFalse)
3444 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00003445 }
3446 }
3447 return(GetFirstImageInList(morph_images));
3448 }
3449 /*
3450 Morph image sequence.
3451 */
3452 status=MagickTrue;
3453 scene=0;
3454 next=image;
3455 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3456 {
cristybb503372010-05-27 20:51:26 +00003457 for (i=0; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003458 {
3459 CacheView
3460 *image_view,
3461 *morph_view;
3462
3463 beta=(MagickRealType) (i+1.0)/(MagickRealType) (number_frames+1.0);
3464 alpha=1.0-beta;
cristy15b98cd2010-09-12 19:42:50 +00003465 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
cristybb503372010-05-27 20:51:26 +00003466 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*
cristy15b98cd2010-09-12 19:42:50 +00003467 next->rows+beta*GetNextImageInList(next)->rows+0.5),
3468 next->filter,next->blur,exception);
cristy3ed852e2009-09-05 21:47:34 +00003469 if (morph_image == (Image *) NULL)
3470 {
3471 morph_images=DestroyImageList(morph_images);
3472 return((Image *) NULL);
3473 }
cristy574cc262011-08-05 01:23:58 +00003474 if (SetImageStorageClass(morph_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003475 {
cristy3ed852e2009-09-05 21:47:34 +00003476 morph_image=DestroyImage(morph_image);
3477 return((Image *) NULL);
3478 }
3479 AppendImageToList(&morph_images,morph_image);
3480 morph_images=GetLastImageInList(morph_images);
cristy15b98cd2010-09-12 19:42:50 +00003481 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
3482 morph_images->rows,GetNextImageInList(next)->filter,
3483 GetNextImageInList(next)->blur,exception);
cristy3ed852e2009-09-05 21:47:34 +00003484 if (morph_image == (Image *) NULL)
3485 {
3486 morph_images=DestroyImageList(morph_images);
3487 return((Image *) NULL);
3488 }
3489 image_view=AcquireCacheView(morph_image);
3490 morph_view=AcquireCacheView(morph_images);
cristyb5d5f722009-11-04 03:03:49 +00003491#if defined(MAGICKCORE_OPENMP_SUPPORT)
3492 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +00003493#endif
cristybb503372010-05-27 20:51:26 +00003494 for (y=0; y < (ssize_t) morph_images->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003495 {
3496 MagickBooleanType
3497 sync;
3498
cristy4c08aed2011-07-01 19:47:50 +00003499 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003500 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003501
cristybb503372010-05-27 20:51:26 +00003502 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003503 x;
3504
cristy4c08aed2011-07-01 19:47:50 +00003505 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003506 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003507
3508 if (status == MagickFalse)
3509 continue;
3510 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3511 exception);
3512 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3513 exception);
cristy4c08aed2011-07-01 19:47:50 +00003514 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003515 {
3516 status=MagickFalse;
3517 continue;
3518 }
cristybb503372010-05-27 20:51:26 +00003519 for (x=0; x < (ssize_t) morph_images->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003520 {
cristy4c08aed2011-07-01 19:47:50 +00003521 SetPixelRed(morph_images,ClampToQuantum(alpha*
3522 GetPixelRed(morph_images,q)+beta*GetPixelRed(morph_image,p)),q);
3523 SetPixelGreen(morph_images,ClampToQuantum(alpha*
3524 GetPixelGreen(morph_images,q)+beta*GetPixelGreen(morph_image,p)),q);
3525 SetPixelBlue(morph_images,ClampToQuantum(alpha*
3526 GetPixelBlue(morph_images,q)+beta*GetPixelBlue(morph_image,p)),q);
3527 SetPixelAlpha(morph_images,ClampToQuantum(alpha*
3528 GetPixelAlpha(morph_images,q)+beta*GetPixelAlpha(morph_image,p)),q);
3529 if ((morph_image->colorspace == CMYKColorspace) &&
3530 (morph_images->colorspace == CMYKColorspace))
3531 SetPixelBlack(morph_images,ClampToQuantum(alpha*
3532 GetPixelBlack(morph_images,q)+beta*GetPixelBlack(morph_image,p)),
3533 q);
cristyed231572011-07-14 02:18:59 +00003534 p+=GetPixelChannels(morph_image);
3535 q+=GetPixelChannels(morph_images);
cristy3ed852e2009-09-05 21:47:34 +00003536 }
3537 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3538 if (sync == MagickFalse)
3539 status=MagickFalse;
3540 }
3541 morph_view=DestroyCacheView(morph_view);
3542 image_view=DestroyCacheView(image_view);
3543 morph_image=DestroyImage(morph_image);
3544 }
cristybb503372010-05-27 20:51:26 +00003545 if (i < (ssize_t) number_frames)
cristy3ed852e2009-09-05 21:47:34 +00003546 break;
3547 /*
3548 Clone last frame in sequence.
3549 */
3550 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3551 if (morph_image == (Image *) NULL)
3552 {
3553 morph_images=DestroyImageList(morph_images);
3554 return((Image *) NULL);
3555 }
3556 AppendImageToList(&morph_images,morph_image);
3557 morph_images=GetLastImageInList(morph_images);
3558 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3559 {
3560 MagickBooleanType
3561 proceed;
3562
cristyb5d5f722009-11-04 03:03:49 +00003563#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003564 #pragma omp critical (MagickCore_MorphImages)
3565#endif
3566 proceed=SetImageProgress(image,MorphImageTag,scene,
3567 GetImageListLength(image));
3568 if (proceed == MagickFalse)
3569 status=MagickFalse;
3570 }
3571 scene++;
3572 }
3573 if (GetNextImageInList(next) != (Image *) NULL)
3574 {
3575 morph_images=DestroyImageList(morph_images);
3576 return((Image *) NULL);
3577 }
3578 return(GetFirstImageInList(morph_images));
3579}
3580
3581/*
3582%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3583% %
3584% %
3585% %
3586% P l a s m a I m a g e %
3587% %
3588% %
3589% %
3590%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3591%
3592% PlasmaImage() initializes an image with plasma fractal values. The image
3593% must be initialized with a base color and the random number generator
3594% seeded before this method is called.
3595%
3596% The format of the PlasmaImage method is:
3597%
3598% MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
cristy5cbc0162011-08-29 00:36:28 +00003599% size_t attenuate,size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003600%
3601% A description of each parameter follows:
3602%
3603% o image: the image.
3604%
3605% o segment: Define the region to apply plasma fractals values.
3606%
glennrp7dae1ca2010-09-16 12:17:35 +00003607% o attenuate: Define the plasma attenuation factor.
cristy3ed852e2009-09-05 21:47:34 +00003608%
3609% o depth: Limit the plasma recursion depth.
3610%
cristy5cbc0162011-08-29 00:36:28 +00003611% o exception: return any errors or warnings in this structure.
3612%
cristy3ed852e2009-09-05 21:47:34 +00003613*/
3614
3615static inline Quantum PlasmaPixel(RandomInfo *random_info,
3616 const MagickRealType pixel,const MagickRealType noise)
3617{
3618 Quantum
3619 plasma;
3620
cristyce70c172010-01-07 17:15:30 +00003621 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
cristy3ed852e2009-09-05 21:47:34 +00003622 noise/2.0);
3623 return(plasma);
3624}
3625
cristyda1f9c12011-10-02 21:39:49 +00003626static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3627 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3628 const SegmentInfo *segment,size_t attenuate,size_t depth,
3629 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003630{
cristy3ed852e2009-09-05 21:47:34 +00003631 MagickRealType
3632 plasma;
3633
cristyda1f9c12011-10-02 21:39:49 +00003634 PixelChannel
3635 channel;
3636
3637 PixelTrait
3638 traits;
3639
3640 register const Quantum
3641 *restrict u,
3642 *restrict v;
3643
3644 register Quantum
3645 *restrict q;
3646
3647 register ssize_t
3648 i;
cristy3ed852e2009-09-05 21:47:34 +00003649
cristy9d314ff2011-03-09 01:30:28 +00003650 ssize_t
3651 x,
3652 x_mid,
3653 y,
3654 y_mid;
3655
cristy3ed852e2009-09-05 21:47:34 +00003656 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3657 return(MagickTrue);
3658 if (depth != 0)
3659 {
3660 SegmentInfo
3661 local_info;
3662
3663 /*
3664 Divide the area into quadrants and recurse.
3665 */
3666 depth--;
3667 attenuate++;
cristybb503372010-05-27 20:51:26 +00003668 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3669 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003670 local_info=(*segment);
3671 local_info.x2=(double) x_mid;
3672 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003673 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3674 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003675 local_info=(*segment);
3676 local_info.y1=(double) y_mid;
3677 local_info.x2=(double) x_mid;
cristyda1f9c12011-10-02 21:39:49 +00003678 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3679 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003680 local_info=(*segment);
3681 local_info.x1=(double) x_mid;
3682 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003683 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3684 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003685 local_info=(*segment);
3686 local_info.x1=(double) x_mid;
3687 local_info.y1=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003688 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3689 &local_info,attenuate,depth,exception));
cristy3ed852e2009-09-05 21:47:34 +00003690 }
cristybb503372010-05-27 20:51:26 +00003691 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3692 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003693 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3694 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3695 return(MagickFalse);
3696 /*
3697 Average pixels and apply plasma.
3698 */
cristy3ed852e2009-09-05 21:47:34 +00003699 plasma=(MagickRealType) QuantumRange/(2.0*attenuate);
3700 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3701 {
cristy3ed852e2009-09-05 21:47:34 +00003702 /*
3703 Left pixel.
3704 */
cristybb503372010-05-27 20:51:26 +00003705 x=(ssize_t) ceil(segment->x1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003706 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3707 1,1,exception);
3708 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3709 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003710 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003711 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3712 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003713 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003714 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3715 {
3716 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3717 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3718 if (traits == UndefinedPixelTrait)
3719 continue;
3720 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3721 }
cristyc5c6f662010-09-22 14:23:02 +00003722 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003723 if (segment->x1 != segment->x2)
3724 {
3725 /*
3726 Right pixel.
3727 */
cristybb503372010-05-27 20:51:26 +00003728 x=(ssize_t) ceil(segment->x2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003729 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3730 1,1,exception);
3731 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3732 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003733 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003734 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3735 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003736 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003737 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3738 {
3739 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3740 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3741 if (traits == UndefinedPixelTrait)
3742 continue;
3743 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3744 }
cristyc5c6f662010-09-22 14:23:02 +00003745 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003746 }
3747 }
3748 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3749 {
3750 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3751 {
cristy3ed852e2009-09-05 21:47:34 +00003752 /*
3753 Bottom pixel.
3754 */
cristybb503372010-05-27 20:51:26 +00003755 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003756 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3757 1,1,exception);
3758 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3759 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003760 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003761 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3762 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003763 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003764 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3765 {
3766 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3767 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3768 if (traits == UndefinedPixelTrait)
3769 continue;
3770 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3771 }
cristyc5c6f662010-09-22 14:23:02 +00003772 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003773 }
3774 if (segment->y1 != segment->y2)
3775 {
cristy3ed852e2009-09-05 21:47:34 +00003776 /*
3777 Top pixel.
3778 */
cristybb503372010-05-27 20:51:26 +00003779 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003780 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3781 1,1,exception);
3782 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3783 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003784 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003785 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3786 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003787 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003788 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3789 {
3790 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3791 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3792 if (traits == UndefinedPixelTrait)
3793 continue;
3794 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3795 }
cristyc5c6f662010-09-22 14:23:02 +00003796 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003797 }
3798 }
3799 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3800 {
cristy3ed852e2009-09-05 21:47:34 +00003801 /*
3802 Middle pixel.
3803 */
cristybb503372010-05-27 20:51:26 +00003804 x=(ssize_t) ceil(segment->x1-0.5);
3805 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003806 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
cristybb503372010-05-27 20:51:26 +00003807 x=(ssize_t) ceil(segment->x2-0.5);
3808 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003809 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003810 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003811 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3812 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003813 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003814 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3815 {
3816 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3817 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3818 if (traits == UndefinedPixelTrait)
3819 continue;
3820 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3821 }
cristyc5c6f662010-09-22 14:23:02 +00003822 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003823 }
3824 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3825 return(MagickTrue);
3826 return(MagickFalse);
3827}
cristyda1f9c12011-10-02 21:39:49 +00003828
cristy3ed852e2009-09-05 21:47:34 +00003829MagickExport MagickBooleanType PlasmaImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00003830 const SegmentInfo *segment,size_t attenuate,size_t depth,
3831 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003832{
cristyc5c6f662010-09-22 14:23:02 +00003833 CacheView
cristyda1f9c12011-10-02 21:39:49 +00003834 *image_view,
3835 *u_view,
3836 *v_view;
cristyc5c6f662010-09-22 14:23:02 +00003837
cristy3ed852e2009-09-05 21:47:34 +00003838 MagickBooleanType
3839 status;
3840
3841 RandomInfo
3842 *random_info;
3843
3844 if (image->debug != MagickFalse)
3845 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3846 assert(image != (Image *) NULL);
3847 assert(image->signature == MagickSignature);
3848 if (image->debug != MagickFalse)
3849 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy5cbc0162011-08-29 00:36:28 +00003850 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristyc5c6f662010-09-22 14:23:02 +00003851 return(MagickFalse);
3852 image_view=AcquireCacheView(image);
cristyda1f9c12011-10-02 21:39:49 +00003853 u_view=AcquireCacheView(image);
3854 v_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00003855 random_info=AcquireRandomInfo();
cristyda1f9c12011-10-02 21:39:49 +00003856 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3857 attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003858 random_info=DestroyRandomInfo(random_info);
cristyda1f9c12011-10-02 21:39:49 +00003859 v_view=DestroyCacheView(v_view);
3860 u_view=DestroyCacheView(u_view);
cristyc5c6f662010-09-22 14:23:02 +00003861 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003862 return(status);
3863}
3864
3865/*
3866%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3867% %
3868% %
3869% %
3870% P o l a r o i d I m a g e %
3871% %
3872% %
3873% %
3874%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3875%
3876% PolaroidImage() simulates a Polaroid picture.
3877%
3878% The format of the AnnotateImage method is:
3879%
3880% Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristy5c4e2582011-09-11 19:21:03 +00003881% const double angle,const PixelInterpolateMethod method,
3882% ExceptionInfo exception)
cristy3ed852e2009-09-05 21:47:34 +00003883%
3884% A description of each parameter follows:
3885%
3886% o image: the image.
3887%
3888% o draw_info: the draw info.
3889%
cristycee97112010-05-28 00:44:52 +00003890% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00003891%
cristy5c4e2582011-09-11 19:21:03 +00003892% o method: the pixel interpolation method.
3893%
cristy3ed852e2009-09-05 21:47:34 +00003894% o exception: return any errors or warnings in this structure.
3895%
3896*/
3897MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristy5c4e2582011-09-11 19:21:03 +00003898 const double angle,const PixelInterpolateMethod method,
3899 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003900{
3901 const char
3902 *value;
3903
cristy3ed852e2009-09-05 21:47:34 +00003904 Image
3905 *bend_image,
3906 *caption_image,
3907 *flop_image,
3908 *picture_image,
3909 *polaroid_image,
3910 *rotate_image,
3911 *trim_image;
3912
cristybb503372010-05-27 20:51:26 +00003913 size_t
cristy3ed852e2009-09-05 21:47:34 +00003914 height;
3915
cristy9d314ff2011-03-09 01:30:28 +00003916 ssize_t
3917 quantum;
3918
cristy3ed852e2009-09-05 21:47:34 +00003919 /*
3920 Simulate a Polaroid picture.
3921 */
3922 assert(image != (Image *) NULL);
3923 assert(image->signature == MagickSignature);
3924 if (image->debug != MagickFalse)
3925 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3926 assert(exception != (ExceptionInfo *) NULL);
3927 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00003928 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
cristy3ed852e2009-09-05 21:47:34 +00003929 image->rows)/25.0,10.0);
3930 height=image->rows+2*quantum;
3931 caption_image=(Image *) NULL;
cristyd15e6592011-10-15 00:13:06 +00003932 value=GetImageProperty(image,"caption",exception);
cristy3ed852e2009-09-05 21:47:34 +00003933 if (value != (const char *) NULL)
3934 {
3935 char
3936 *caption,
3937 geometry[MaxTextExtent];
3938
3939 DrawInfo
3940 *annotate_info;
3941
cristy3ed852e2009-09-05 21:47:34 +00003942 MagickBooleanType
3943 status;
3944
cristy9d314ff2011-03-09 01:30:28 +00003945 ssize_t
3946 count;
3947
cristy3ed852e2009-09-05 21:47:34 +00003948 TypeMetric
3949 metrics;
3950
3951 /*
3952 Generate caption image.
3953 */
3954 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3955 if (caption_image == (Image *) NULL)
3956 return((Image *) NULL);
3957 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
3958 caption=InterpretImageProperties((ImageInfo *) NULL,(Image *) image,
cristy018f07f2011-09-04 21:15:19 +00003959 value,exception);
cristy3ed852e2009-09-05 21:47:34 +00003960 (void) CloneString(&annotate_info->text,caption);
cristy6b1d05e2010-09-22 19:17:27 +00003961 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
cristy5cbc0162011-08-29 00:36:28 +00003962 &caption,exception);
cristybb503372010-05-27 20:51:26 +00003963 status=SetImageExtent(caption_image,image->columns,(size_t)
cristy63240882011-08-05 19:05:27 +00003964 ((count+1)*(metrics.ascent-metrics.descent)+0.5),exception);
cristy3ed852e2009-09-05 21:47:34 +00003965 if (status == MagickFalse)
3966 caption_image=DestroyImage(caption_image);
3967 else
3968 {
3969 caption_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003970 (void) SetImageBackgroundColor(caption_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003971 (void) CloneString(&annotate_info->text,caption);
cristyb51dff52011-05-19 16:55:47 +00003972 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
cristy3ed852e2009-09-05 21:47:34 +00003973 metrics.ascent);
3974 if (annotate_info->gravity == UndefinedGravity)
3975 (void) CloneString(&annotate_info->geometry,AcquireString(
3976 geometry));
cristy5cbc0162011-08-29 00:36:28 +00003977 (void) AnnotateImage(caption_image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003978 height+=caption_image->rows;
3979 }
3980 annotate_info=DestroyDrawInfo(annotate_info);
3981 caption=DestroyString(caption);
3982 }
3983 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
3984 exception);
3985 if (picture_image == (Image *) NULL)
3986 {
3987 if (caption_image != (Image *) NULL)
3988 caption_image=DestroyImage(caption_image);
3989 return((Image *) NULL);
3990 }
3991 picture_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003992 (void) SetImageBackgroundColor(picture_image,exception);
cristye941a752011-10-15 01:52:48 +00003993 (void) CompositeImage(picture_image,OverCompositeOp,image,quantum,quantum,
3994 exception);
cristy3ed852e2009-09-05 21:47:34 +00003995 if (caption_image != (Image *) NULL)
3996 {
3997 (void) CompositeImage(picture_image,OverCompositeOp,caption_image,
cristye941a752011-10-15 01:52:48 +00003998 quantum,(ssize_t) (image->rows+3*quantum/2),exception);
cristy3ed852e2009-09-05 21:47:34 +00003999 caption_image=DestroyImage(caption_image);
4000 }
cristy9950d572011-10-01 18:22:35 +00004001 (void) QueryColorCompliance("none",AllCompliance,
4002 &picture_image->background_color,exception);
cristy63240882011-08-05 19:05:27 +00004003 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004004 rotate_image=RotateImage(picture_image,90.0,exception);
4005 picture_image=DestroyImage(picture_image);
4006 if (rotate_image == (Image *) NULL)
4007 return((Image *) NULL);
4008 picture_image=rotate_image;
4009 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
cristy5c4e2582011-09-11 19:21:03 +00004010 picture_image->columns,method,exception);
cristy3ed852e2009-09-05 21:47:34 +00004011 picture_image=DestroyImage(picture_image);
4012 if (bend_image == (Image *) NULL)
4013 return((Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00004014 picture_image=bend_image;
4015 rotate_image=RotateImage(picture_image,-90.0,exception);
4016 picture_image=DestroyImage(picture_image);
4017 if (rotate_image == (Image *) NULL)
4018 return((Image *) NULL);
4019 picture_image=rotate_image;
4020 picture_image->background_color=image->background_color;
4021 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
4022 exception);
4023 if (polaroid_image == (Image *) NULL)
4024 {
4025 picture_image=DestroyImage(picture_image);
4026 return(picture_image);
4027 }
4028 flop_image=FlopImage(polaroid_image,exception);
4029 polaroid_image=DestroyImage(polaroid_image);
4030 if (flop_image == (Image *) NULL)
4031 {
4032 picture_image=DestroyImage(picture_image);
4033 return(picture_image);
4034 }
4035 polaroid_image=flop_image;
4036 (void) CompositeImage(polaroid_image,OverCompositeOp,picture_image,
cristye941a752011-10-15 01:52:48 +00004037 (ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
cristy3ed852e2009-09-05 21:47:34 +00004038 picture_image=DestroyImage(picture_image);
cristy9950d572011-10-01 18:22:35 +00004039 (void) QueryColorCompliance("none",AllCompliance,
4040 &polaroid_image->background_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00004041 rotate_image=RotateImage(polaroid_image,angle,exception);
4042 polaroid_image=DestroyImage(polaroid_image);
4043 if (rotate_image == (Image *) NULL)
4044 return((Image *) NULL);
4045 polaroid_image=rotate_image;
4046 trim_image=TrimImage(polaroid_image,exception);
4047 polaroid_image=DestroyImage(polaroid_image);
4048 if (trim_image == (Image *) NULL)
4049 return((Image *) NULL);
4050 polaroid_image=trim_image;
4051 return(polaroid_image);
4052}
4053
4054/*
4055%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4056% %
4057% %
4058% %
cristy3ed852e2009-09-05 21:47:34 +00004059% S e p i a T o n e I m a g e %
4060% %
4061% %
4062% %
4063%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4064%
4065% MagickSepiaToneImage() applies a special effect to the image, similar to the
4066% effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4067% 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4068% threshold of 80% is a good starting point for a reasonable tone.
4069%
4070% The format of the SepiaToneImage method is:
4071%
4072% Image *SepiaToneImage(const Image *image,const double threshold,
4073% ExceptionInfo *exception)
4074%
4075% A description of each parameter follows:
4076%
4077% o image: the image.
4078%
4079% o threshold: the tone threshold.
4080%
4081% o exception: return any errors or warnings in this structure.
4082%
4083*/
4084MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4085 ExceptionInfo *exception)
4086{
4087#define SepiaToneImageTag "SepiaTone/Image"
4088
cristyc4c8d132010-01-07 01:58:38 +00004089 CacheView
4090 *image_view,
4091 *sepia_view;
4092
cristy3ed852e2009-09-05 21:47:34 +00004093 Image
4094 *sepia_image;
4095
cristy3ed852e2009-09-05 21:47:34 +00004096 MagickBooleanType
4097 status;
4098
cristybb503372010-05-27 20:51:26 +00004099 MagickOffsetType
4100 progress;
4101
4102 ssize_t
4103 y;
4104
cristy3ed852e2009-09-05 21:47:34 +00004105 /*
4106 Initialize sepia-toned image attributes.
4107 */
4108 assert(image != (const Image *) NULL);
4109 assert(image->signature == MagickSignature);
4110 if (image->debug != MagickFalse)
4111 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4112 assert(exception != (ExceptionInfo *) NULL);
4113 assert(exception->signature == MagickSignature);
4114 sepia_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
4115 if (sepia_image == (Image *) NULL)
4116 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004117 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004118 {
cristy3ed852e2009-09-05 21:47:34 +00004119 sepia_image=DestroyImage(sepia_image);
4120 return((Image *) NULL);
4121 }
4122 /*
4123 Tone each row of the image.
4124 */
4125 status=MagickTrue;
4126 progress=0;
4127 image_view=AcquireCacheView(image);
4128 sepia_view=AcquireCacheView(sepia_image);
cristyb5d5f722009-11-04 03:03:49 +00004129#if defined(MAGICKCORE_OPENMP_SUPPORT)
4130 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004131#endif
cristybb503372010-05-27 20:51:26 +00004132 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004133 {
cristy4c08aed2011-07-01 19:47:50 +00004134 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004135 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004136
cristybb503372010-05-27 20:51:26 +00004137 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004138 x;
4139
cristy4c08aed2011-07-01 19:47:50 +00004140 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004141 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004142
4143 if (status == MagickFalse)
4144 continue;
4145 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
4146 q=QueueCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
4147 exception);
cristy4c08aed2011-07-01 19:47:50 +00004148 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004149 {
4150 status=MagickFalse;
4151 continue;
4152 }
cristybb503372010-05-27 20:51:26 +00004153 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004154 {
4155 MagickRealType
4156 intensity,
4157 tone;
4158
cristy4c08aed2011-07-01 19:47:50 +00004159 intensity=(MagickRealType) GetPixelIntensity(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004160 tone=intensity > threshold ? (MagickRealType) QuantumRange : intensity+
4161 (MagickRealType) QuantumRange-threshold;
cristy4c08aed2011-07-01 19:47:50 +00004162 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004163 tone=intensity > (7.0*threshold/6.0) ? (MagickRealType) QuantumRange :
4164 intensity+(MagickRealType) QuantumRange-7.0*threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004165 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004166 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004167 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004168 tone=threshold/7.0;
cristy4c08aed2011-07-01 19:47:50 +00004169 if ((MagickRealType) GetPixelGreen(image,q) < tone)
4170 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4171 if ((MagickRealType) GetPixelBlue(image,q) < tone)
4172 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristyed231572011-07-14 02:18:59 +00004173 p+=GetPixelChannels(image);
4174 q+=GetPixelChannels(sepia_image);
cristy3ed852e2009-09-05 21:47:34 +00004175 }
4176 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4177 status=MagickFalse;
4178 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4179 {
4180 MagickBooleanType
4181 proceed;
4182
cristyb5d5f722009-11-04 03:03:49 +00004183#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00004184 #pragma omp critical (MagickCore_SepiaToneImage)
4185#endif
4186 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4187 image->rows);
4188 if (proceed == MagickFalse)
4189 status=MagickFalse;
4190 }
4191 }
4192 sepia_view=DestroyCacheView(sepia_view);
4193 image_view=DestroyCacheView(image_view);
cristye23ec9d2011-08-16 18:15:40 +00004194 (void) NormalizeImage(sepia_image,exception);
4195 (void) ContrastImage(sepia_image,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004196 if (status == MagickFalse)
4197 sepia_image=DestroyImage(sepia_image);
4198 return(sepia_image);
4199}
4200
4201/*
4202%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4203% %
4204% %
4205% %
4206% S h a d o w I m a g e %
4207% %
4208% %
4209% %
4210%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4211%
4212% ShadowImage() simulates a shadow from the specified image and returns it.
4213%
4214% The format of the ShadowImage method is:
4215%
4216% Image *ShadowImage(const Image *image,const double opacity,
cristybb503372010-05-27 20:51:26 +00004217% const double sigma,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004218% ExceptionInfo *exception)
4219%
4220% A description of each parameter follows:
4221%
4222% o image: the image.
4223%
4224% o opacity: percentage transparency.
4225%
4226% o sigma: the standard deviation of the Gaussian, in pixels.
4227%
4228% o x_offset: the shadow x-offset.
4229%
4230% o y_offset: the shadow y-offset.
4231%
4232% o exception: return any errors or warnings in this structure.
4233%
4234*/
4235MagickExport Image *ShadowImage(const Image *image,const double opacity,
cristybb503372010-05-27 20:51:26 +00004236 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004237 ExceptionInfo *exception)
4238{
4239#define ShadowImageTag "Shadow/Image"
4240
cristybd5a96c2011-08-21 00:04:26 +00004241 ChannelType
4242 channel_mask;
4243
cristy3ed852e2009-09-05 21:47:34 +00004244 Image
4245 *border_image,
4246 *clone_image,
4247 *shadow_image;
4248
cristy3ed852e2009-09-05 21:47:34 +00004249 RectangleInfo
4250 border_info;
4251
cristy3ed852e2009-09-05 21:47:34 +00004252 assert(image != (Image *) NULL);
4253 assert(image->signature == MagickSignature);
4254 if (image->debug != MagickFalse)
4255 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4256 assert(exception != (ExceptionInfo *) NULL);
4257 assert(exception->signature == MagickSignature);
4258 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4259 if (clone_image == (Image *) NULL)
4260 return((Image *) NULL);
4261 (void) SetImageVirtualPixelMethod(clone_image,EdgeVirtualPixelMethod);
4262 clone_image->compose=OverCompositeOp;
cristybb503372010-05-27 20:51:26 +00004263 border_info.width=(size_t) floor(2.0*sigma+0.5);
4264 border_info.height=(size_t) floor(2.0*sigma+0.5);
cristy3ed852e2009-09-05 21:47:34 +00004265 border_info.x=0;
4266 border_info.y=0;
cristy9950d572011-10-01 18:22:35 +00004267 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4268 exception);
cristy633f0c62011-09-15 13:27:36 +00004269 border_image=BorderImage(clone_image,&border_info,image->compose,exception);
cristy3ed852e2009-09-05 21:47:34 +00004270 clone_image=DestroyImage(clone_image);
4271 if (border_image == (Image *) NULL)
4272 return((Image *) NULL);
4273 if (border_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00004274 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004275 /*
4276 Shadow image.
4277 */
cristyea1a8aa2011-10-20 13:24:06 +00004278 (void) SetImageBackgroundColor(border_image,exception);
cristybd5a96c2011-08-21 00:04:26 +00004279 channel_mask=SetPixelChannelMask(border_image,AlphaChannel);
cristy05c0c9a2011-09-05 23:16:13 +00004280 shadow_image=BlurImage(border_image,0.0,sigma,image->bias,exception);
cristybd5a96c2011-08-21 00:04:26 +00004281 (void) SetPixelChannelMap(border_image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00004282 border_image=DestroyImage(border_image);
4283 if (shadow_image == (Image *) NULL)
4284 return((Image *) NULL);
4285 if (shadow_image->page.width == 0)
4286 shadow_image->page.width=shadow_image->columns;
4287 if (shadow_image->page.height == 0)
4288 shadow_image->page.height=shadow_image->rows;
cristybb503372010-05-27 20:51:26 +00004289 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4290 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4291 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4292 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
cristy3ed852e2009-09-05 21:47:34 +00004293 return(shadow_image);
4294}
4295
4296/*
4297%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4298% %
4299% %
4300% %
4301% S k e t c h I m a g e %
4302% %
4303% %
4304% %
4305%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4306%
4307% SketchImage() simulates a pencil sketch. We convolve the image with a
4308% Gaussian operator of the given radius and standard deviation (sigma). For
4309% reasonable results, radius should be larger than sigma. Use a radius of 0
4310% and SketchImage() selects a suitable radius for you. Angle gives the angle
4311% of the sketch.
4312%
4313% The format of the SketchImage method is:
4314%
4315% Image *SketchImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00004316% const double sigma,const double angle,const double bias,
4317% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004318%
4319% A description of each parameter follows:
4320%
4321% o image: the image.
4322%
cristy574cc262011-08-05 01:23:58 +00004323% o radius: the radius of the Gaussian, in pixels, not counting the
4324% center pixel.
cristy3ed852e2009-09-05 21:47:34 +00004325%
4326% o sigma: the standard deviation of the Gaussian, in pixels.
4327%
cristyf7ef0252011-09-09 14:50:06 +00004328% o angle: apply the effect along this angle.
4329%
4330% o bias: the bias.
cristy3ed852e2009-09-05 21:47:34 +00004331%
4332% o exception: return any errors or warnings in this structure.
4333%
4334*/
4335MagickExport Image *SketchImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00004336 const double sigma,const double angle,const double bias,
4337 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004338{
cristyfa112112010-01-04 17:48:07 +00004339 CacheView
4340 *random_view;
4341
cristy3ed852e2009-09-05 21:47:34 +00004342 Image
4343 *blend_image,
4344 *blur_image,
4345 *dodge_image,
4346 *random_image,
4347 *sketch_image;
4348
cristy3ed852e2009-09-05 21:47:34 +00004349 MagickBooleanType
4350 status;
4351
cristy3ed852e2009-09-05 21:47:34 +00004352 RandomInfo
cristyfa112112010-01-04 17:48:07 +00004353 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00004354
cristy9d314ff2011-03-09 01:30:28 +00004355 ssize_t
4356 y;
4357
cristy3ed852e2009-09-05 21:47:34 +00004358 /*
4359 Sketch image.
4360 */
4361 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4362 MagickTrue,exception);
4363 if (random_image == (Image *) NULL)
4364 return((Image *) NULL);
4365 status=MagickTrue;
cristy1b784432009-12-19 02:20:40 +00004366 random_info=AcquireRandomInfoThreadSet();
cristy3ed852e2009-09-05 21:47:34 +00004367 random_view=AcquireCacheView(random_image);
cristy1b784432009-12-19 02:20:40 +00004368#if defined(MAGICKCORE_OPENMP_SUPPORT)
4369 #pragma omp parallel for schedule(dynamic,4) shared(status)
4370#endif
cristybb503372010-05-27 20:51:26 +00004371 for (y=0; y < (ssize_t) random_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004372 {
cristy5c9e6f22010-09-17 17:31:01 +00004373 const int
4374 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00004375
cristybb503372010-05-27 20:51:26 +00004376 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004377 x;
4378
cristy4c08aed2011-07-01 19:47:50 +00004379 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004380 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004381
cristy1b784432009-12-19 02:20:40 +00004382 if (status == MagickFalse)
4383 continue;
cristy3ed852e2009-09-05 21:47:34 +00004384 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4385 exception);
cristyacd2ed22011-08-30 01:44:23 +00004386 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004387 {
4388 status=MagickFalse;
4389 continue;
4390 }
cristybb503372010-05-27 20:51:26 +00004391 for (x=0; x < (ssize_t) random_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004392 {
cristy76f512e2011-09-12 01:26:56 +00004393 MagickRealType
4394 value;
4395
4396 register ssize_t
4397 i;
4398
4399 value=GetPseudoRandomValue(random_info[id]);
4400 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4401 {
4402 PixelTrait
4403 traits;
4404
4405 traits=GetPixelChannelMapTraits(random_image,(PixelChannel) i);
4406 if (traits == UndefinedPixelTrait)
4407 continue;
4408 q[i]=ClampToQuantum(QuantumRange*value);
4409 }
cristyed231572011-07-14 02:18:59 +00004410 q+=GetPixelChannels(random_image);
cristy3ed852e2009-09-05 21:47:34 +00004411 }
4412 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4413 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004414 }
4415 random_view=DestroyCacheView(random_view);
cristy1b784432009-12-19 02:20:40 +00004416 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00004417 if (status == MagickFalse)
4418 {
4419 random_image=DestroyImage(random_image);
4420 return(random_image);
4421 }
cristyf7ef0252011-09-09 14:50:06 +00004422 blur_image=MotionBlurImage(random_image,radius,sigma,angle,bias,exception);
cristy3ed852e2009-09-05 21:47:34 +00004423 random_image=DestroyImage(random_image);
4424 if (blur_image == (Image *) NULL)
4425 return((Image *) NULL);
cristy8ae632d2011-09-05 17:29:53 +00004426 dodge_image=EdgeImage(blur_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00004427 blur_image=DestroyImage(blur_image);
4428 if (dodge_image == (Image *) NULL)
4429 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +00004430 (void) NormalizeImage(dodge_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +00004431 (void) NegateImage(dodge_image,MagickFalse,exception);
cristye941a752011-10-15 01:52:48 +00004432 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
cristy3ed852e2009-09-05 21:47:34 +00004433 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4434 if (sketch_image == (Image *) NULL)
4435 {
4436 dodge_image=DestroyImage(dodge_image);
4437 return((Image *) NULL);
4438 }
cristye941a752011-10-15 01:52:48 +00004439 (void) CompositeImage(sketch_image,ColorDodgeCompositeOp,dodge_image,0,0,
4440 exception);
cristy3ed852e2009-09-05 21:47:34 +00004441 dodge_image=DestroyImage(dodge_image);
4442 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4443 if (blend_image == (Image *) NULL)
4444 {
4445 sketch_image=DestroyImage(sketch_image);
4446 return((Image *) NULL);
4447 }
4448 (void) SetImageArtifact(blend_image,"compose:args","20x80");
cristye941a752011-10-15 01:52:48 +00004449 (void) CompositeImage(sketch_image,BlendCompositeOp,blend_image,0,0,
4450 exception);
cristy3ed852e2009-09-05 21:47:34 +00004451 blend_image=DestroyImage(blend_image);
4452 return(sketch_image);
4453}
4454
4455/*
4456%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4457% %
4458% %
4459% %
4460% S o l a r i z e I m a g e %
4461% %
4462% %
4463% %
4464%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4465%
4466% SolarizeImage() applies a special effect to the image, similar to the effect
4467% achieved in a photo darkroom by selectively exposing areas of photo
4468% sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4469% measure of the extent of the solarization.
4470%
4471% The format of the SolarizeImage method is:
4472%
cristy5cbc0162011-08-29 00:36:28 +00004473% MagickBooleanType SolarizeImage(Image *image,const double threshold,
4474% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004475%
4476% A description of each parameter follows:
4477%
4478% o image: the image.
4479%
4480% o threshold: Define the extent of the solarization.
4481%
cristy5cbc0162011-08-29 00:36:28 +00004482% o exception: return any errors or warnings in this structure.
4483%
cristy3ed852e2009-09-05 21:47:34 +00004484*/
4485MagickExport MagickBooleanType SolarizeImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00004486 const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004487{
4488#define SolarizeImageTag "Solarize/Image"
4489
cristyc4c8d132010-01-07 01:58:38 +00004490 CacheView
4491 *image_view;
4492
cristy3ed852e2009-09-05 21:47:34 +00004493 MagickBooleanType
4494 status;
4495
cristybb503372010-05-27 20:51:26 +00004496 MagickOffsetType
4497 progress;
4498
4499 ssize_t
4500 y;
4501
cristy3ed852e2009-09-05 21:47:34 +00004502 assert(image != (Image *) NULL);
4503 assert(image->signature == MagickSignature);
4504 if (image->debug != MagickFalse)
4505 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4506 if (image->storage_class == PseudoClass)
4507 {
cristybb503372010-05-27 20:51:26 +00004508 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004509 i;
4510
4511 /*
4512 Solarize colormap.
4513 */
cristybb503372010-05-27 20:51:26 +00004514 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00004515 {
4516 if ((MagickRealType) image->colormap[i].red > threshold)
4517 image->colormap[i].red=(Quantum) QuantumRange-image->colormap[i].red;
4518 if ((MagickRealType) image->colormap[i].green > threshold)
4519 image->colormap[i].green=(Quantum) QuantumRange-
4520 image->colormap[i].green;
4521 if ((MagickRealType) image->colormap[i].blue > threshold)
4522 image->colormap[i].blue=(Quantum) QuantumRange-
4523 image->colormap[i].blue;
4524 }
4525 }
4526 /*
4527 Solarize image.
4528 */
4529 status=MagickTrue;
4530 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00004531 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00004532#if defined(MAGICKCORE_OPENMP_SUPPORT)
4533 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004534#endif
cristybb503372010-05-27 20:51:26 +00004535 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004536 {
cristybb503372010-05-27 20:51:26 +00004537 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004538 x;
4539
cristy4c08aed2011-07-01 19:47:50 +00004540 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004541 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004542
4543 if (status == MagickFalse)
4544 continue;
cristy5cbc0162011-08-29 00:36:28 +00004545 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00004546 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004547 {
4548 status=MagickFalse;
4549 continue;
4550 }
cristybb503372010-05-27 20:51:26 +00004551 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004552 {
cristy76f512e2011-09-12 01:26:56 +00004553 register ssize_t
4554 i;
4555
4556 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4557 {
4558 PixelTrait
4559 traits;
4560
4561 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
4562 if ((traits == UndefinedPixelTrait) ||
4563 ((traits & CopyPixelTrait) != 0))
4564 continue;
4565 if ((MagickRealType) q[i] > threshold)
4566 q[i]=QuantumRange-q[i];
4567 }
cristyed231572011-07-14 02:18:59 +00004568 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004569 }
4570 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4571 status=MagickFalse;
4572 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4573 {
4574 MagickBooleanType
4575 proceed;
4576
cristyb5d5f722009-11-04 03:03:49 +00004577#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00004578 #pragma omp critical (MagickCore_SolarizeImage)
4579#endif
4580 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4581 if (proceed == MagickFalse)
4582 status=MagickFalse;
4583 }
4584 }
4585 image_view=DestroyCacheView(image_view);
4586 return(status);
4587}
4588
4589/*
4590%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4591% %
4592% %
4593% %
4594% S t e g a n o I m a g e %
4595% %
4596% %
4597% %
4598%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4599%
4600% SteganoImage() hides a digital watermark within the image. Recover
4601% the hidden watermark later to prove that the authenticity of an image.
4602% Offset defines the start position within the image to hide the watermark.
4603%
4604% The format of the SteganoImage method is:
4605%
4606% Image *SteganoImage(const Image *image,Image *watermark,
4607% ExceptionInfo *exception)
4608%
4609% A description of each parameter follows:
4610%
4611% o image: the image.
4612%
4613% o watermark: the watermark image.
4614%
4615% o exception: return any errors or warnings in this structure.
4616%
4617*/
4618MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4619 ExceptionInfo *exception)
4620{
cristye1bf8ad2010-09-19 17:07:03 +00004621#define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
cristy4c08aed2011-07-01 19:47:50 +00004622#define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
cristyeaedf062010-05-29 22:36:02 +00004623 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
cristy3ed852e2009-09-05 21:47:34 +00004624#define SteganoImageTag "Stegano/Image"
4625
cristyb0d3bb92010-09-22 14:37:58 +00004626 CacheView
4627 *stegano_view,
4628 *watermark_view;
4629
cristy3ed852e2009-09-05 21:47:34 +00004630 Image
4631 *stegano_image;
4632
4633 int
4634 c;
4635
cristy3ed852e2009-09-05 21:47:34 +00004636 MagickBooleanType
4637 status;
4638
cristy101ab702011-10-13 13:06:32 +00004639 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004640 pixel;
4641
cristy4c08aed2011-07-01 19:47:50 +00004642 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004643 *q;
4644
cristye1bf8ad2010-09-19 17:07:03 +00004645 register ssize_t
4646 x;
4647
cristybb503372010-05-27 20:51:26 +00004648 size_t
cristyeaedf062010-05-29 22:36:02 +00004649 depth,
4650 one;
cristy3ed852e2009-09-05 21:47:34 +00004651
cristye1bf8ad2010-09-19 17:07:03 +00004652 ssize_t
4653 i,
4654 j,
4655 k,
4656 y;
4657
cristy3ed852e2009-09-05 21:47:34 +00004658 /*
4659 Initialize steganographic image attributes.
4660 */
4661 assert(image != (const Image *) NULL);
4662 assert(image->signature == MagickSignature);
4663 if (image->debug != MagickFalse)
4664 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4665 assert(watermark != (const Image *) NULL);
4666 assert(watermark->signature == MagickSignature);
4667 assert(exception != (ExceptionInfo *) NULL);
4668 assert(exception->signature == MagickSignature);
cristyeaedf062010-05-29 22:36:02 +00004669 one=1UL;
cristy3ed852e2009-09-05 21:47:34 +00004670 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4671 if (stegano_image == (Image *) NULL)
4672 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004673 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004674 {
cristy3ed852e2009-09-05 21:47:34 +00004675 stegano_image=DestroyImage(stegano_image);
4676 return((Image *) NULL);
4677 }
4678 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
4679 /*
4680 Hide watermark in low-order bits of image.
4681 */
4682 c=0;
4683 i=0;
4684 j=0;
4685 depth=stegano_image->depth;
4686 k=image->offset;
cristyda16f162011-02-19 23:52:17 +00004687 status=MagickTrue;
cristyb0d3bb92010-09-22 14:37:58 +00004688 watermark_view=AcquireCacheView(watermark);
4689 stegano_view=AcquireCacheView(stegano_image);
cristybb503372010-05-27 20:51:26 +00004690 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
cristy3ed852e2009-09-05 21:47:34 +00004691 {
cristybb503372010-05-27 20:51:26 +00004692 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
cristy3ed852e2009-09-05 21:47:34 +00004693 {
cristybb503372010-05-27 20:51:26 +00004694 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
cristy3ed852e2009-09-05 21:47:34 +00004695 {
cristyda1f9c12011-10-02 21:39:49 +00004696 Quantum
cristy5f95f4f2011-10-23 01:01:01 +00004697 virtual_pixel[CompositePixelChannel];
cristyda1f9c12011-10-02 21:39:49 +00004698
4699 (void) GetOneCacheViewVirtualPixel(watermark_view,x,y,virtual_pixel,
4700 exception);
4701 pixel.red=(double) virtual_pixel[RedPixelChannel];
4702 pixel.green=(double) virtual_pixel[GreenPixelChannel];
4703 pixel.blue=(double) virtual_pixel[BluePixelChannel];
4704 pixel.alpha=(double) virtual_pixel[AlphaPixelChannel];
cristybb503372010-05-27 20:51:26 +00004705 if ((k/(ssize_t) stegano_image->columns) >= (ssize_t) stegano_image->rows)
cristy3ed852e2009-09-05 21:47:34 +00004706 break;
cristyb0d3bb92010-09-22 14:37:58 +00004707 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4708 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4709 exception);
cristyacd2ed22011-08-30 01:44:23 +00004710 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004711 break;
4712 switch (c)
4713 {
4714 case 0:
4715 {
cristy4c08aed2011-07-01 19:47:50 +00004716 SetPixelRed(image,SetBit(GetPixelRed(image,q),j,GetBit(
cristy101ab702011-10-13 13:06:32 +00004717 GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004718 break;
4719 }
4720 case 1:
4721 {
cristy4c08aed2011-07-01 19:47:50 +00004722 SetPixelGreen(image,SetBit(GetPixelGreen(image,q),j,GetBit(
cristy101ab702011-10-13 13:06:32 +00004723 GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004724 break;
4725 }
4726 case 2:
4727 {
cristy4c08aed2011-07-01 19:47:50 +00004728 SetPixelBlue(image,SetBit(GetPixelBlue(image,q),j,GetBit(
cristy101ab702011-10-13 13:06:32 +00004729 GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004730 break;
4731 }
4732 }
cristyb0d3bb92010-09-22 14:37:58 +00004733 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004734 break;
4735 c++;
4736 if (c == 3)
4737 c=0;
4738 k++;
cristybb503372010-05-27 20:51:26 +00004739 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
cristy3ed852e2009-09-05 21:47:34 +00004740 k=0;
4741 if (k == image->offset)
4742 j++;
4743 }
4744 }
cristy8b27a6d2010-02-14 03:31:15 +00004745 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004746 {
cristy8b27a6d2010-02-14 03:31:15 +00004747 MagickBooleanType
4748 proceed;
4749
4750 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4751 (depth-i),depth);
4752 if (proceed == MagickFalse)
4753 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004754 }
4755 }
cristyb0d3bb92010-09-22 14:37:58 +00004756 stegano_view=DestroyCacheView(stegano_view);
4757 watermark_view=DestroyCacheView(watermark_view);
cristy3ed852e2009-09-05 21:47:34 +00004758 if (stegano_image->storage_class == PseudoClass)
cristyea1a8aa2011-10-20 13:24:06 +00004759 (void) SyncImage(stegano_image,exception);
cristyda16f162011-02-19 23:52:17 +00004760 if (status == MagickFalse)
4761 {
4762 stegano_image=DestroyImage(stegano_image);
4763 return((Image *) NULL);
4764 }
cristy3ed852e2009-09-05 21:47:34 +00004765 return(stegano_image);
4766}
4767
4768/*
4769%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4770% %
4771% %
4772% %
4773% S t e r e o A n a g l y p h I m a g e %
4774% %
4775% %
4776% %
4777%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4778%
4779% StereoAnaglyphImage() combines two images and produces a single image that
4780% is the composite of a left and right image of a stereo pair. Special
4781% red-green stereo glasses are required to view this effect.
4782%
4783% The format of the StereoAnaglyphImage method is:
4784%
4785% Image *StereoImage(const Image *left_image,const Image *right_image,
4786% ExceptionInfo *exception)
4787% Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004788% const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004789% ExceptionInfo *exception)
4790%
4791% A description of each parameter follows:
4792%
4793% o left_image: the left image.
4794%
4795% o right_image: the right image.
4796%
4797% o exception: return any errors or warnings in this structure.
4798%
4799% o x_offset: amount, in pixels, by which the left image is offset to the
4800% right of the right image.
4801%
4802% o y_offset: amount, in pixels, by which the left image is offset to the
4803% bottom of the right image.
4804%
4805%
4806*/
4807MagickExport Image *StereoImage(const Image *left_image,
4808 const Image *right_image,ExceptionInfo *exception)
4809{
4810 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4811}
4812
4813MagickExport Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004814 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004815 ExceptionInfo *exception)
4816{
4817#define StereoImageTag "Stereo/Image"
4818
4819 const Image
4820 *image;
4821
4822 Image
4823 *stereo_image;
4824
cristy3ed852e2009-09-05 21:47:34 +00004825 MagickBooleanType
4826 status;
4827
cristy9d314ff2011-03-09 01:30:28 +00004828 ssize_t
4829 y;
4830
cristy3ed852e2009-09-05 21:47:34 +00004831 assert(left_image != (const Image *) NULL);
4832 assert(left_image->signature == MagickSignature);
4833 if (left_image->debug != MagickFalse)
4834 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4835 left_image->filename);
4836 assert(right_image != (const Image *) NULL);
4837 assert(right_image->signature == MagickSignature);
4838 assert(exception != (ExceptionInfo *) NULL);
4839 assert(exception->signature == MagickSignature);
4840 assert(right_image != (const Image *) NULL);
4841 image=left_image;
4842 if ((left_image->columns != right_image->columns) ||
4843 (left_image->rows != right_image->rows))
4844 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4845 /*
4846 Initialize stereo image attributes.
4847 */
4848 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4849 MagickTrue,exception);
4850 if (stereo_image == (Image *) NULL)
4851 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004852 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004853 {
cristy3ed852e2009-09-05 21:47:34 +00004854 stereo_image=DestroyImage(stereo_image);
4855 return((Image *) NULL);
4856 }
4857 /*
4858 Copy left image to red channel and right image to blue channel.
4859 */
cristyda16f162011-02-19 23:52:17 +00004860 status=MagickTrue;
cristybb503372010-05-27 20:51:26 +00004861 for (y=0; y < (ssize_t) stereo_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004862 {
cristy4c08aed2011-07-01 19:47:50 +00004863 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004864 *restrict p,
4865 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004866
cristybb503372010-05-27 20:51:26 +00004867 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004868 x;
4869
cristy4c08aed2011-07-01 19:47:50 +00004870 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004871 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +00004872
4873 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4874 exception);
4875 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4876 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
cristy76f512e2011-09-12 01:26:56 +00004877 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4878 (r == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004879 break;
cristybb503372010-05-27 20:51:26 +00004880 for (x=0; x < (ssize_t) stereo_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004881 {
cristy4c08aed2011-07-01 19:47:50 +00004882 SetPixelRed(image,GetPixelRed(left_image,p),r);
cristy76f512e2011-09-12 01:26:56 +00004883 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4884 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4885 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4886 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4887 GetPixelAlpha(right_image,q))/2,r);
cristyed231572011-07-14 02:18:59 +00004888 p+=GetPixelChannels(left_image);
cristy76f512e2011-09-12 01:26:56 +00004889 q+=GetPixelChannels(right_image);
4890 r+=GetPixelChannels(stereo_image);
cristy3ed852e2009-09-05 21:47:34 +00004891 }
4892 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4893 break;
cristy8b27a6d2010-02-14 03:31:15 +00004894 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004895 {
cristy8b27a6d2010-02-14 03:31:15 +00004896 MagickBooleanType
4897 proceed;
4898
cristybb503372010-05-27 20:51:26 +00004899 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4900 stereo_image->rows);
cristy8b27a6d2010-02-14 03:31:15 +00004901 if (proceed == MagickFalse)
4902 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004903 }
4904 }
cristyda16f162011-02-19 23:52:17 +00004905 if (status == MagickFalse)
4906 {
4907 stereo_image=DestroyImage(stereo_image);
4908 return((Image *) NULL);
4909 }
cristy3ed852e2009-09-05 21:47:34 +00004910 return(stereo_image);
4911}
4912
4913/*
4914%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4915% %
4916% %
4917% %
4918% S w i r l I m a g e %
4919% %
4920% %
4921% %
4922%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4923%
4924% SwirlImage() swirls the pixels about the center of the image, where
4925% degrees indicates the sweep of the arc through which each pixel is moved.
4926% You get a more dramatic effect as the degrees move from 1 to 360.
4927%
4928% The format of the SwirlImage method is:
4929%
4930% Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00004931% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004932%
4933% A description of each parameter follows:
4934%
4935% o image: the image.
4936%
4937% o degrees: Define the tightness of the swirling effect.
4938%
cristy76f512e2011-09-12 01:26:56 +00004939% o method: the pixel interpolation method.
4940%
cristy3ed852e2009-09-05 21:47:34 +00004941% o exception: return any errors or warnings in this structure.
4942%
4943*/
4944MagickExport Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00004945 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004946{
4947#define SwirlImageTag "Swirl/Image"
4948
cristyfa112112010-01-04 17:48:07 +00004949 CacheView
4950 *image_view,
4951 *swirl_view;
4952
cristy3ed852e2009-09-05 21:47:34 +00004953 Image
4954 *swirl_image;
4955
cristy3ed852e2009-09-05 21:47:34 +00004956 MagickBooleanType
4957 status;
4958
cristybb503372010-05-27 20:51:26 +00004959 MagickOffsetType
4960 progress;
4961
cristy3ed852e2009-09-05 21:47:34 +00004962 MagickRealType
4963 radius;
4964
4965 PointInfo
4966 center,
4967 scale;
4968
cristybb503372010-05-27 20:51:26 +00004969 ssize_t
4970 y;
4971
cristy3ed852e2009-09-05 21:47:34 +00004972 /*
4973 Initialize swirl image attributes.
4974 */
4975 assert(image != (const Image *) NULL);
4976 assert(image->signature == MagickSignature);
4977 if (image->debug != MagickFalse)
4978 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4979 assert(exception != (ExceptionInfo *) NULL);
4980 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00004981 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004982 if (swirl_image == (Image *) NULL)
4983 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004984 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004985 {
cristy3ed852e2009-09-05 21:47:34 +00004986 swirl_image=DestroyImage(swirl_image);
4987 return((Image *) NULL);
4988 }
cristy4c08aed2011-07-01 19:47:50 +00004989 if (swirl_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00004990 swirl_image->matte=MagickTrue;
4991 /*
4992 Compute scaling factor.
4993 */
4994 center.x=(double) image->columns/2.0;
4995 center.y=(double) image->rows/2.0;
4996 radius=MagickMax(center.x,center.y);
4997 scale.x=1.0;
4998 scale.y=1.0;
4999 if (image->columns > image->rows)
5000 scale.y=(double) image->columns/(double) image->rows;
5001 else
5002 if (image->columns < image->rows)
5003 scale.x=(double) image->rows/(double) image->columns;
5004 degrees=(double) DegreesToRadians(degrees);
5005 /*
5006 Swirl image.
5007 */
5008 status=MagickTrue;
5009 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00005010 image_view=AcquireCacheView(image);
5011 swirl_view=AcquireCacheView(swirl_image);
cristyb5d5f722009-11-04 03:03:49 +00005012#if defined(MAGICKCORE_OPENMP_SUPPORT)
5013 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005014#endif
cristybb503372010-05-27 20:51:26 +00005015 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005016 {
cristy3ed852e2009-09-05 21:47:34 +00005017 MagickRealType
5018 distance;
5019
5020 PointInfo
5021 delta;
5022
cristy6d188022011-09-12 13:23:33 +00005023 register const Quantum
5024 *restrict p;
5025
cristybb503372010-05-27 20:51:26 +00005026 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005027 x;
5028
cristy4c08aed2011-07-01 19:47:50 +00005029 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005030 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005031
5032 if (status == MagickFalse)
5033 continue;
cristy6d188022011-09-12 13:23:33 +00005034 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy3ed852e2009-09-05 21:47:34 +00005035 q=GetCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
5036 exception);
cristy6d188022011-09-12 13:23:33 +00005037 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005038 {
5039 status=MagickFalse;
5040 continue;
5041 }
cristy3ed852e2009-09-05 21:47:34 +00005042 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00005043 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005044 {
cristy6d188022011-09-12 13:23:33 +00005045 register ssize_t
5046 i;
5047
cristy3ed852e2009-09-05 21:47:34 +00005048 /*
5049 Determine if the pixel is within an ellipse.
5050 */
5051 delta.x=scale.x*(double) (x-center.x);
5052 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00005053 if (distance >= (radius*radius))
5054 {
5055 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5056 q[i]=p[i];
5057 }
5058 else
cristy3ed852e2009-09-05 21:47:34 +00005059 {
5060 MagickRealType
5061 cosine,
5062 factor,
5063 sine;
5064
5065 /*
5066 Swirl the pixel.
5067 */
5068 factor=1.0-sqrt((double) distance)/radius;
5069 sine=sin((double) (degrees*factor*factor));
5070 cosine=cos((double) (degrees*factor*factor));
cristy76f512e2011-09-12 01:26:56 +00005071 status=InterpolatePixelChannels(image,image_view,swirl_image,method,
5072 ((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5073 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00005074 }
cristy6d188022011-09-12 13:23:33 +00005075 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00005076 q+=GetPixelChannels(swirl_image);
cristy3ed852e2009-09-05 21:47:34 +00005077 }
5078 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5079 status=MagickFalse;
5080 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5081 {
5082 MagickBooleanType
5083 proceed;
5084
cristyb5d5f722009-11-04 03:03:49 +00005085#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005086 #pragma omp critical (MagickCore_SwirlImage)
5087#endif
5088 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5089 if (proceed == MagickFalse)
5090 status=MagickFalse;
5091 }
5092 }
5093 swirl_view=DestroyCacheView(swirl_view);
5094 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005095 if (status == MagickFalse)
5096 swirl_image=DestroyImage(swirl_image);
5097 return(swirl_image);
5098}
5099
5100/*
5101%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5102% %
5103% %
5104% %
5105% T i n t I m a g e %
5106% %
5107% %
5108% %
5109%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5110%
5111% TintImage() applies a color vector to each pixel in the image. The length
5112% of the vector is 0 for black and white and at its maximum for the midtones.
5113% The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5114%
5115% The format of the TintImage method is:
5116%
cristyb817c3f2011-10-03 14:00:35 +00005117% Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005118% const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005119%
5120% A description of each parameter follows:
5121%
5122% o image: the image.
5123%
cristyb817c3f2011-10-03 14:00:35 +00005124% o blend: A color value used for tinting.
cristy3ed852e2009-09-05 21:47:34 +00005125%
5126% o tint: A color value used for tinting.
5127%
5128% o exception: return any errors or warnings in this structure.
5129%
5130*/
cristyb817c3f2011-10-03 14:00:35 +00005131MagickExport Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005132 const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005133{
5134#define TintImageTag "Tint/Image"
5135
cristyc4c8d132010-01-07 01:58:38 +00005136 CacheView
5137 *image_view,
5138 *tint_view;
5139
cristy3ed852e2009-09-05 21:47:34 +00005140 GeometryInfo
5141 geometry_info;
5142
5143 Image
5144 *tint_image;
5145
cristy3ed852e2009-09-05 21:47:34 +00005146 MagickBooleanType
5147 status;
5148
cristybb503372010-05-27 20:51:26 +00005149 MagickOffsetType
5150 progress;
cristy3ed852e2009-09-05 21:47:34 +00005151
cristy28474bf2011-09-11 23:32:52 +00005152 MagickRealType
5153 intensity;
5154
cristy4c08aed2011-07-01 19:47:50 +00005155 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00005156 color_vector,
5157 pixel;
5158
cristybb503372010-05-27 20:51:26 +00005159 MagickStatusType
5160 flags;
5161
5162 ssize_t
5163 y;
5164
cristy3ed852e2009-09-05 21:47:34 +00005165 /*
5166 Allocate tint image.
5167 */
5168 assert(image != (const Image *) NULL);
5169 assert(image->signature == MagickSignature);
5170 if (image->debug != MagickFalse)
5171 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5172 assert(exception != (ExceptionInfo *) NULL);
5173 assert(exception->signature == MagickSignature);
5174 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5175 if (tint_image == (Image *) NULL)
5176 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005177 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005178 {
cristy3ed852e2009-09-05 21:47:34 +00005179 tint_image=DestroyImage(tint_image);
5180 return((Image *) NULL);
5181 }
cristyaed9c382011-10-03 17:54:21 +00005182 if (blend == (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005183 return(tint_image);
5184 /*
5185 Determine RGB values of the color.
5186 */
cristy28474bf2011-09-11 23:32:52 +00005187 GetPixelInfo(image,&pixel);
cristyb817c3f2011-10-03 14:00:35 +00005188 flags=ParseGeometry(blend,&geometry_info);
cristy3ed852e2009-09-05 21:47:34 +00005189 pixel.red=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005190 pixel.green=geometry_info.rho;
5191 pixel.blue=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005192 pixel.alpha=OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00005193 if ((flags & SigmaValue) != 0)
5194 pixel.green=geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +00005195 if ((flags & XiValue) != 0)
5196 pixel.blue=geometry_info.xi;
cristyb817c3f2011-10-03 14:00:35 +00005197 if ((flags & PsiValue) != 0)
5198 pixel.alpha=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005199 if (image->colorspace == CMYKColorspace)
5200 {
cristyb817c3f2011-10-03 14:00:35 +00005201 pixel.black=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005202 if ((flags & PsiValue) != 0)
5203 pixel.black=geometry_info.psi;
5204 if ((flags & ChiValue) != 0)
5205 pixel.alpha=geometry_info.chi;
5206 }
cristy28474bf2011-09-11 23:32:52 +00005207 intensity=(MagickRealType) GetPixelInfoIntensity(tint);
5208 color_vector.red=(MagickRealType) (pixel.red*tint->red/100.0-intensity);
5209 color_vector.green=(MagickRealType) (pixel.green*tint->green/100.0-intensity);
5210 color_vector.blue=(MagickRealType) (pixel.blue*tint->blue/100.0-intensity);
5211 color_vector.black=(MagickRealType) (pixel.black*tint->black/100.0-intensity);
5212 color_vector.alpha=(MagickRealType) (pixel.alpha*tint->alpha/100.0-intensity);
cristy3ed852e2009-09-05 21:47:34 +00005213 /*
5214 Tint image.
5215 */
5216 status=MagickTrue;
5217 progress=0;
5218 image_view=AcquireCacheView(image);
5219 tint_view=AcquireCacheView(tint_image);
cristyb5d5f722009-11-04 03:03:49 +00005220#if defined(MAGICKCORE_OPENMP_SUPPORT)
5221 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005222#endif
cristybb503372010-05-27 20:51:26 +00005223 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005224 {
cristy4c08aed2011-07-01 19:47:50 +00005225 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00005226 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00005227
cristy4c08aed2011-07-01 19:47:50 +00005228 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005229 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005230
cristy6b91acb2011-04-19 12:23:54 +00005231 register ssize_t
5232 x;
5233
cristy3ed852e2009-09-05 21:47:34 +00005234 if (status == MagickFalse)
5235 continue;
5236 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5237 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5238 exception);
cristy4c08aed2011-07-01 19:47:50 +00005239 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005240 {
5241 status=MagickFalse;
5242 continue;
5243 }
cristybb503372010-05-27 20:51:26 +00005244 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005245 {
cristy4c08aed2011-07-01 19:47:50 +00005246 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00005247 pixel;
5248
5249 MagickRealType
5250 weight;
5251
cristy76f512e2011-09-12 01:26:56 +00005252 if ((GetPixelRedTraits(tint_image) & UpdatePixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005253 {
5254 weight=QuantumScale*GetPixelRed(image,p)-0.5;
5255 pixel.red=(MagickRealType) GetPixelRed(image,p)+
5256 color_vector.red*(1.0-(4.0*(weight*weight)));
5257 SetPixelRed(tint_image,ClampToQuantum(pixel.red),q);
5258 }
cristy76f512e2011-09-12 01:26:56 +00005259 if ((GetPixelGreenTraits(tint_image) & UpdatePixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005260 {
5261 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
5262 pixel.green=(MagickRealType) GetPixelGreen(image,p)+
5263 color_vector.green*(1.0-(4.0*(weight*weight)));
5264 SetPixelGreen(tint_image,ClampToQuantum(pixel.green),q);
5265 }
cristy76f512e2011-09-12 01:26:56 +00005266 if ((GetPixelBlueTraits(tint_image) & UpdatePixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005267 {
5268 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
5269 pixel.blue=(MagickRealType) GetPixelBlue(image,p)+
5270 color_vector.blue*(1.0-(4.0*(weight*weight)));
5271 SetPixelBlue(tint_image,ClampToQuantum(pixel.blue),q);
5272 }
cristy76f512e2011-09-12 01:26:56 +00005273 if ((GetPixelBlackTraits(tint_image) & UpdatePixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005274 {
5275 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
5276 pixel.black=(MagickRealType) GetPixelBlack(image,p)+
5277 color_vector.black*(1.0-(4.0*(weight*weight)));
5278 SetPixelBlack(tint_image,ClampToQuantum(pixel.black),q);
5279 }
cristy76f512e2011-09-12 01:26:56 +00005280 if ((GetPixelAlphaTraits(tint_image) & CopyPixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005281 SetPixelAlpha(tint_image,GetPixelAlpha(image,p),q);
cristyed231572011-07-14 02:18:59 +00005282 p+=GetPixelChannels(image);
5283 q+=GetPixelChannels(tint_image);
cristy3ed852e2009-09-05 21:47:34 +00005284 }
5285 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5286 status=MagickFalse;
5287 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5288 {
5289 MagickBooleanType
5290 proceed;
5291
cristyb5d5f722009-11-04 03:03:49 +00005292#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005293 #pragma omp critical (MagickCore_TintImage)
5294#endif
5295 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5296 if (proceed == MagickFalse)
5297 status=MagickFalse;
5298 }
5299 }
5300 tint_view=DestroyCacheView(tint_view);
5301 image_view=DestroyCacheView(image_view);
5302 if (status == MagickFalse)
5303 tint_image=DestroyImage(tint_image);
5304 return(tint_image);
5305}
5306
5307/*
5308%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5309% %
5310% %
5311% %
5312% V i g n e t t e I m a g e %
5313% %
5314% %
5315% %
5316%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5317%
5318% VignetteImage() softens the edges of the image in vignette style.
5319%
5320% The format of the VignetteImage method is:
5321%
5322% Image *VignetteImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +00005323% const double sigma,const ssize_t x,const ssize_t y,
5324% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005325%
5326% A description of each parameter follows:
5327%
5328% o image: the image.
5329%
5330% o radius: the radius of the pixel neighborhood.
5331%
5332% o sigma: the standard deviation of the Gaussian, in pixels.
5333%
5334% o x, y: Define the x and y ellipse offset.
5335%
5336% o exception: return any errors or warnings in this structure.
5337%
5338*/
5339MagickExport Image *VignetteImage(const Image *image,const double radius,
cristybb503372010-05-27 20:51:26 +00005340 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005341{
5342 char
5343 ellipse[MaxTextExtent];
5344
5345 DrawInfo
5346 *draw_info;
5347
5348 Image
5349 *canvas_image,
5350 *blur_image,
5351 *oval_image,
5352 *vignette_image;
5353
5354 assert(image != (Image *) NULL);
5355 assert(image->signature == MagickSignature);
5356 if (image->debug != MagickFalse)
5357 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5358 assert(exception != (ExceptionInfo *) NULL);
5359 assert(exception->signature == MagickSignature);
5360 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5361 if (canvas_image == (Image *) NULL)
5362 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005363 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005364 {
cristy3ed852e2009-09-05 21:47:34 +00005365 canvas_image=DestroyImage(canvas_image);
5366 return((Image *) NULL);
5367 }
5368 canvas_image->matte=MagickTrue;
cristyb3a73b52011-07-26 01:34:43 +00005369 oval_image=CloneImage(canvas_image,canvas_image->columns,
5370 canvas_image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005371 if (oval_image == (Image *) NULL)
5372 {
5373 canvas_image=DestroyImage(canvas_image);
5374 return((Image *) NULL);
5375 }
cristy9950d572011-10-01 18:22:35 +00005376 (void) QueryColorCompliance("#000000",AllCompliance,
5377 &oval_image->background_color,exception);
cristyea1a8aa2011-10-20 13:24:06 +00005378 (void) SetImageBackgroundColor(oval_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00005379 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
cristy9950d572011-10-01 18:22:35 +00005380 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5381 exception);
5382 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5383 exception);
cristyb51dff52011-05-19 16:55:47 +00005384 (void) FormatLocaleString(ellipse,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00005385 "ellipse %g,%g,%g,%g,0.0,360.0",image->columns/2.0,
cristy8cd5b312010-01-07 01:10:24 +00005386 image->rows/2.0,image->columns/2.0-x,image->rows/2.0-y);
cristy3ed852e2009-09-05 21:47:34 +00005387 draw_info->primitive=AcquireString(ellipse);
cristy018f07f2011-09-04 21:15:19 +00005388 (void) DrawImage(oval_image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00005389 draw_info=DestroyDrawInfo(draw_info);
cristy05c0c9a2011-09-05 23:16:13 +00005390 blur_image=BlurImage(oval_image,radius,sigma,image->bias,exception);
cristy3ed852e2009-09-05 21:47:34 +00005391 oval_image=DestroyImage(oval_image);
5392 if (blur_image == (Image *) NULL)
5393 {
5394 canvas_image=DestroyImage(canvas_image);
5395 return((Image *) NULL);
5396 }
5397 blur_image->matte=MagickFalse;
cristye941a752011-10-15 01:52:48 +00005398 (void) CompositeImage(canvas_image,CopyOpacityCompositeOp,blur_image,0,0,
5399 exception);
cristy3ed852e2009-09-05 21:47:34 +00005400 blur_image=DestroyImage(blur_image);
5401 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5402 canvas_image=DestroyImage(canvas_image);
5403 return(vignette_image);
5404}
5405
5406/*
5407%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5408% %
5409% %
5410% %
5411% W a v e I m a g e %
5412% %
5413% %
5414% %
5415%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5416%
5417% WaveImage() creates a "ripple" effect in the image by shifting the pixels
cristycee97112010-05-28 00:44:52 +00005418% vertically along a sine wave whose amplitude and wavelength is specified
cristy3ed852e2009-09-05 21:47:34 +00005419% by the given parameters.
5420%
5421% The format of the WaveImage method is:
5422%
5423% Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005424% const double wave_length,const PixelInterpolateMethod method,
5425% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005426%
5427% A description of each parameter follows:
5428%
5429% o image: the image.
5430%
5431% o amplitude, wave_length: Define the amplitude and wave length of the
5432% sine wave.
5433%
cristy5c4e2582011-09-11 19:21:03 +00005434% o interpolate: the pixel interpolation method.
5435%
cristy3ed852e2009-09-05 21:47:34 +00005436% o exception: return any errors or warnings in this structure.
5437%
5438*/
5439MagickExport Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005440 const double wave_length,const PixelInterpolateMethod method,
5441 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005442{
5443#define WaveImageTag "Wave/Image"
5444
cristyfa112112010-01-04 17:48:07 +00005445 CacheView
cristyd76c51e2011-03-26 00:21:26 +00005446 *image_view,
cristyfa112112010-01-04 17:48:07 +00005447 *wave_view;
5448
cristy3ed852e2009-09-05 21:47:34 +00005449 Image
5450 *wave_image;
5451
cristy3ed852e2009-09-05 21:47:34 +00005452 MagickBooleanType
5453 status;
5454
cristybb503372010-05-27 20:51:26 +00005455 MagickOffsetType
5456 progress;
5457
cristy3ed852e2009-09-05 21:47:34 +00005458 MagickRealType
5459 *sine_map;
5460
cristybb503372010-05-27 20:51:26 +00005461 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005462 i;
5463
cristybb503372010-05-27 20:51:26 +00005464 ssize_t
5465 y;
5466
cristy3ed852e2009-09-05 21:47:34 +00005467 /*
5468 Initialize wave image attributes.
5469 */
5470 assert(image != (Image *) NULL);
5471 assert(image->signature == MagickSignature);
5472 if (image->debug != MagickFalse)
5473 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5474 assert(exception != (ExceptionInfo *) NULL);
5475 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00005476 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
cristy3ed852e2009-09-05 21:47:34 +00005477 fabs(amplitude)),MagickTrue,exception);
5478 if (wave_image == (Image *) NULL)
5479 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005480 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005481 {
cristy3ed852e2009-09-05 21:47:34 +00005482 wave_image=DestroyImage(wave_image);
5483 return((Image *) NULL);
5484 }
cristy4c08aed2011-07-01 19:47:50 +00005485 if (wave_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00005486 wave_image->matte=MagickTrue;
5487 /*
5488 Allocate sine map.
5489 */
5490 sine_map=(MagickRealType *) AcquireQuantumMemory((size_t) wave_image->columns,
5491 sizeof(*sine_map));
5492 if (sine_map == (MagickRealType *) NULL)
5493 {
5494 wave_image=DestroyImage(wave_image);
5495 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5496 }
cristybb503372010-05-27 20:51:26 +00005497 for (i=0; i < (ssize_t) wave_image->columns; i++)
cristy4205a3c2010-09-12 20:19:59 +00005498 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5499 wave_length));
cristy3ed852e2009-09-05 21:47:34 +00005500 /*
5501 Wave image.
5502 */
5503 status=MagickTrue;
5504 progress=0;
cristyd76c51e2011-03-26 00:21:26 +00005505 image_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00005506 wave_view=AcquireCacheView(wave_image);
cristyd76c51e2011-03-26 00:21:26 +00005507 (void) SetCacheViewVirtualPixelMethod(image_view,
5508 BackgroundVirtualPixelMethod);
cristyb5d5f722009-11-04 03:03:49 +00005509#if defined(MAGICKCORE_OPENMP_SUPPORT)
5510 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005511#endif
cristybb503372010-05-27 20:51:26 +00005512 for (y=0; y < (ssize_t) wave_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005513 {
cristy4c08aed2011-07-01 19:47:50 +00005514 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005515 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005516
cristye97bb922011-04-03 01:36:52 +00005517 register ssize_t
5518 x;
5519
cristy3ed852e2009-09-05 21:47:34 +00005520 if (status == MagickFalse)
5521 continue;
5522 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5523 exception);
cristyacd2ed22011-08-30 01:44:23 +00005524 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005525 {
5526 status=MagickFalse;
5527 continue;
5528 }
cristybb503372010-05-27 20:51:26 +00005529 for (x=0; x < (ssize_t) wave_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005530 {
cristy5c4e2582011-09-11 19:21:03 +00005531 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5532 (double) x,(double) (y-sine_map[x]),q,exception);
cristyed231572011-07-14 02:18:59 +00005533 q+=GetPixelChannels(wave_image);
cristy3ed852e2009-09-05 21:47:34 +00005534 }
5535 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5536 status=MagickFalse;
5537 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5538 {
5539 MagickBooleanType
5540 proceed;
5541
cristyb5d5f722009-11-04 03:03:49 +00005542#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005543 #pragma omp critical (MagickCore_WaveImage)
5544#endif
5545 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5546 if (proceed == MagickFalse)
5547 status=MagickFalse;
5548 }
5549 }
5550 wave_view=DestroyCacheView(wave_view);
cristyd76c51e2011-03-26 00:21:26 +00005551 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005552 sine_map=(MagickRealType *) RelinquishMagickMemory(sine_map);
5553 if (status == MagickFalse)
5554 wave_image=DestroyImage(wave_image);
5555 return(wave_image);
5556}