blob: 240305935e457a7efe343e0aada1303797c60c7a [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 }
cristy488fa882010-03-01 22:34:24 +00001946 case 'J':
1947 case 'j':
1948 {
1949 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1950 (LocaleNCompare(expression,"j1",2) == 0))
1951 {
1952 expression+=2;
1953 break;
1954 }
1955 break;
1956 }
cristy2def9322010-06-18 23:59:37 +00001957 case '#':
1958 {
1959 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1960 expression++;
1961 break;
1962 }
cristy488fa882010-03-01 22:34:24 +00001963 default:
1964 break;
1965 }
cristy3ed852e2009-09-05 21:47:34 +00001966 if ((c == (int) '{') || (c == (int) '['))
1967 level++;
1968 else
1969 if ((c == (int) '}') || (c == (int) ']'))
1970 level--;
1971 if (level == 0)
1972 switch ((unsigned char) *expression)
1973 {
1974 case '~':
1975 case '!':
1976 {
1977 precedence=BitwiseComplementPrecedence;
1978 break;
1979 }
1980 case '^':
cristy6621e252010-08-13 00:42:57 +00001981 case '@':
cristy3ed852e2009-09-05 21:47:34 +00001982 {
1983 precedence=ExponentPrecedence;
1984 break;
1985 }
1986 default:
1987 {
1988 if (((c != 0) && ((isdigit((int) ((char) c)) != 0) ||
1989 (strchr(")",c) != (char *) NULL))) &&
1990 (((islower((int) ((char) *expression)) != 0) ||
1991 (strchr("(",(int) *expression) != (char *) NULL)) ||
1992 ((isdigit((int) ((char) c)) == 0) &&
1993 (isdigit((int) ((char) *expression)) != 0))) &&
1994 (strchr("xy",(int) *expression) == (char *) NULL))
1995 precedence=MultiplyPrecedence;
1996 break;
1997 }
1998 case '*':
1999 case '/':
2000 case '%':
2001 {
2002 precedence=MultiplyPrecedence;
2003 break;
2004 }
2005 case '+':
2006 case '-':
2007 {
2008 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
2009 (isalpha(c) != 0))
2010 precedence=AdditionPrecedence;
2011 break;
2012 }
2013 case LeftShiftOperator:
2014 case RightShiftOperator:
2015 {
2016 precedence=ShiftPrecedence;
2017 break;
2018 }
2019 case '<':
2020 case LessThanEqualOperator:
2021 case GreaterThanEqualOperator:
2022 case '>':
2023 {
2024 precedence=RelationalPrecedence;
2025 break;
2026 }
2027 case EqualOperator:
2028 case NotEqualOperator:
2029 {
2030 precedence=EquivalencyPrecedence;
2031 break;
2032 }
2033 case '&':
2034 {
2035 precedence=BitwiseAndPrecedence;
2036 break;
2037 }
2038 case '|':
2039 {
2040 precedence=BitwiseOrPrecedence;
2041 break;
2042 }
2043 case LogicalAndOperator:
2044 {
2045 precedence=LogicalAndPrecedence;
2046 break;
2047 }
2048 case LogicalOrOperator:
2049 {
2050 precedence=LogicalOrPrecedence;
2051 break;
2052 }
cristy116af162010-08-13 01:25:47 +00002053 case ExponentialNotation:
2054 {
2055 precedence=ExponentialNotationPrecedence;
2056 break;
2057 }
cristy3ed852e2009-09-05 21:47:34 +00002058 case ':':
2059 case '?':
2060 {
2061 precedence=TernaryPrecedence;
2062 break;
2063 }
2064 case '=':
2065 {
2066 precedence=AssignmentPrecedence;
2067 break;
2068 }
2069 case ',':
2070 {
2071 precedence=CommaPrecedence;
2072 break;
2073 }
2074 case ';':
2075 {
2076 precedence=SeparatorPrecedence;
2077 break;
2078 }
2079 }
2080 if ((precedence == BitwiseComplementPrecedence) ||
2081 (precedence == TernaryPrecedence) ||
2082 (precedence == AssignmentPrecedence))
2083 {
2084 if (precedence > target)
2085 {
2086 /*
2087 Right-to-left associativity.
2088 */
2089 target=precedence;
2090 subexpression=expression;
2091 }
2092 }
2093 else
2094 if (precedence >= target)
2095 {
2096 /*
2097 Left-to-right associativity.
2098 */
2099 target=precedence;
2100 subexpression=expression;
2101 }
2102 if (strchr("(",(int) *expression) != (char *) NULL)
2103 expression=FxSubexpression(expression,exception);
2104 c=(int) (*expression++);
2105 }
2106 return(subexpression);
2107}
2108
2109static MagickRealType FxEvaluateSubexpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002110 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002111 const char *expression,MagickRealType *beta,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002112{
2113 char
2114 *q,
2115 subexpression[MaxTextExtent];
2116
2117 MagickRealType
2118 alpha,
2119 gamma;
2120
2121 register const char
2122 *p;
2123
2124 *beta=0.0;
2125 if (exception->severity != UndefinedException)
2126 return(0.0);
2127 while (isspace((int) *expression) != 0)
2128 expression++;
2129 if (*expression == '\0')
2130 {
2131 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
2132 "MissingExpression","`%s'",expression);
2133 return(0.0);
2134 }
cristy66322f02010-05-17 11:40:48 +00002135 *subexpression='\0';
cristy3ed852e2009-09-05 21:47:34 +00002136 p=FxOperatorPrecedence(expression,exception);
2137 if (p != (const char *) NULL)
2138 {
2139 (void) CopyMagickString(subexpression,expression,(size_t)
2140 (p-expression+1));
2141 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2142 exception);
2143 switch ((unsigned char) *p)
2144 {
2145 case '~':
2146 {
2147 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002148 *beta=(MagickRealType) (~(size_t) *beta);
cristy3ed852e2009-09-05 21:47:34 +00002149 return(*beta);
2150 }
2151 case '!':
2152 {
2153 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2154 return(*beta == 0.0 ? 1.0 : 0.0);
2155 }
2156 case '^':
2157 {
2158 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2159 channel,x,y,++p,beta,exception));
2160 return(*beta);
2161 }
2162 case '*':
cristy116af162010-08-13 01:25:47 +00002163 case ExponentialNotation:
cristy3ed852e2009-09-05 21:47:34 +00002164 {
2165 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2166 return(alpha*(*beta));
2167 }
2168 case '/':
2169 {
2170 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2171 if (*beta == 0.0)
2172 {
2173 if (exception->severity == UndefinedException)
2174 (void) ThrowMagickException(exception,GetMagickModule(),
2175 OptionError,"DivideByZero","`%s'",expression);
2176 return(0.0);
2177 }
2178 return(alpha/(*beta));
2179 }
2180 case '%':
2181 {
2182 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2183 *beta=fabs(floor(((double) *beta)+0.5));
2184 if (*beta == 0.0)
2185 {
2186 (void) ThrowMagickException(exception,GetMagickModule(),
2187 OptionError,"DivideByZero","`%s'",expression);
2188 return(0.0);
2189 }
2190 return(fmod((double) alpha,(double) *beta));
2191 }
2192 case '+':
2193 {
2194 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2195 return(alpha+(*beta));
2196 }
2197 case '-':
2198 {
2199 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2200 return(alpha-(*beta));
2201 }
2202 case LeftShiftOperator:
2203 {
2204 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002205 *beta=(MagickRealType) ((size_t) (alpha+0.5) << (size_t)
cristy3ed852e2009-09-05 21:47:34 +00002206 (gamma+0.5));
2207 return(*beta);
2208 }
2209 case RightShiftOperator:
2210 {
2211 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002212 *beta=(MagickRealType) ((size_t) (alpha+0.5) >> (size_t)
cristy3ed852e2009-09-05 21:47:34 +00002213 (gamma+0.5));
2214 return(*beta);
2215 }
2216 case '<':
2217 {
2218 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2219 return(alpha < *beta ? 1.0 : 0.0);
2220 }
2221 case LessThanEqualOperator:
2222 {
2223 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2224 return(alpha <= *beta ? 1.0 : 0.0);
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 GreaterThanEqualOperator:
2232 {
2233 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2234 return(alpha >= *beta ? 1.0 : 0.0);
2235 }
2236 case EqualOperator:
2237 {
2238 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2239 return(fabs(alpha-(*beta)) <= MagickEpsilon ? 1.0 : 0.0);
2240 }
2241 case NotEqualOperator:
2242 {
2243 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2244 return(fabs(alpha-(*beta)) > MagickEpsilon ? 1.0 : 0.0);
2245 }
2246 case '&':
2247 {
2248 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002249 *beta=(MagickRealType) ((size_t) (alpha+0.5) & (size_t)
cristy3ed852e2009-09-05 21:47:34 +00002250 (gamma+0.5));
2251 return(*beta);
2252 }
2253 case '|':
2254 {
2255 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002256 *beta=(MagickRealType) ((size_t) (alpha+0.5) | (size_t)
cristy3ed852e2009-09-05 21:47:34 +00002257 (gamma+0.5));
2258 return(*beta);
2259 }
2260 case LogicalAndOperator:
2261 {
2262 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2263 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2264 return(*beta);
2265 }
2266 case LogicalOrOperator:
2267 {
2268 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2269 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2270 return(*beta);
2271 }
2272 case '?':
2273 {
2274 MagickRealType
2275 gamma;
2276
2277 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2278 q=subexpression;
2279 p=StringToken(":",&q);
2280 if (q == (char *) NULL)
2281 {
2282 (void) ThrowMagickException(exception,GetMagickModule(),
2283 OptionError,"UnableToParseExpression","`%s'",subexpression);
2284 return(0.0);
2285 }
2286 if (fabs((double) alpha) > MagickEpsilon)
2287 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2288 else
2289 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2290 return(gamma);
2291 }
2292 case '=':
2293 {
2294 char
2295 numeric[MaxTextExtent];
2296
2297 q=subexpression;
2298 while (isalpha((int) ((unsigned char) *q)) != 0)
2299 q++;
2300 if (*q != '\0')
2301 {
2302 (void) ThrowMagickException(exception,GetMagickModule(),
2303 OptionError,"UnableToParseExpression","`%s'",subexpression);
2304 return(0.0);
2305 }
2306 ClearMagickException(exception);
2307 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristyb51dff52011-05-19 16:55:47 +00002308 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
cristy8cd5b312010-01-07 01:10:24 +00002309 *beta);
cristy3ed852e2009-09-05 21:47:34 +00002310 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2311 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2312 subexpression),ConstantString(numeric));
2313 return(*beta);
2314 }
2315 case ',':
2316 {
2317 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2318 return(alpha);
2319 }
2320 case ';':
2321 {
2322 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2323 return(*beta);
2324 }
2325 default:
2326 {
2327 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2328 exception);
2329 return(gamma);
2330 }
2331 }
2332 }
2333 if (strchr("(",(int) *expression) != (char *) NULL)
2334 {
2335 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2336 subexpression[strlen(subexpression)-1]='\0';
2337 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2338 exception);
2339 return(gamma);
2340 }
cristy8b8a3ae2010-10-23 18:49:46 +00002341 switch (*expression)
cristy3ed852e2009-09-05 21:47:34 +00002342 {
2343 case '+':
2344 {
2345 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2346 exception);
2347 return(1.0*gamma);
2348 }
2349 case '-':
2350 {
2351 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2352 exception);
2353 return(-1.0*gamma);
2354 }
2355 case '~':
2356 {
2357 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2358 exception);
cristybb503372010-05-27 20:51:26 +00002359 return((MagickRealType) (~(size_t) (gamma+0.5)));
cristy3ed852e2009-09-05 21:47:34 +00002360 }
2361 case 'A':
2362 case 'a':
2363 {
2364 if (LocaleNCompare(expression,"abs",3) == 0)
2365 {
2366 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2367 exception);
2368 return((MagickRealType) fabs((double) alpha));
2369 }
cristyb33454f2011-08-03 02:10:45 +00002370#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00002371 if (LocaleNCompare(expression,"acosh",5) == 0)
2372 {
2373 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2374 exception);
2375 return((MagickRealType) acosh((double) alpha));
2376 }
cristyb33454f2011-08-03 02:10:45 +00002377#endif
cristy3ed852e2009-09-05 21:47:34 +00002378 if (LocaleNCompare(expression,"acos",4) == 0)
2379 {
2380 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2381 exception);
2382 return((MagickRealType) acos((double) alpha));
2383 }
cristy43c22f42010-03-30 12:34:07 +00002384#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002385 if (LocaleNCompare(expression,"airy",4) == 0)
2386 {
2387 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2388 exception);
2389 if (alpha == 0.0)
cristy2dd03222010-03-30 22:12:11 +00002390 return(1.0);
2391 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
cristy43c22f42010-03-30 12:34:07 +00002392 return(gamma*gamma);
cristyee56cf12010-03-01 22:17:06 +00002393 }
cristy43c22f42010-03-30 12:34:07 +00002394#endif
cristyb33454f2011-08-03 02:10:45 +00002395#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00002396 if (LocaleNCompare(expression,"asinh",5) == 0)
2397 {
2398 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2399 exception);
2400 return((MagickRealType) asinh((double) alpha));
2401 }
cristyb33454f2011-08-03 02:10:45 +00002402#endif
cristy3ed852e2009-09-05 21:47:34 +00002403 if (LocaleNCompare(expression,"asin",4) == 0)
2404 {
2405 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2406 exception);
2407 return((MagickRealType) asin((double) alpha));
2408 }
2409 if (LocaleNCompare(expression,"alt",3) == 0)
2410 {
2411 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2412 exception);
cristybb503372010-05-27 20:51:26 +00002413 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
cristy3ed852e2009-09-05 21:47:34 +00002414 }
2415 if (LocaleNCompare(expression,"atan2",5) == 0)
2416 {
2417 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2418 exception);
2419 return((MagickRealType) atan2((double) alpha,(double) *beta));
2420 }
cristyb33454f2011-08-03 02:10:45 +00002421#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00002422 if (LocaleNCompare(expression,"atanh",5) == 0)
2423 {
2424 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2425 exception);
2426 return((MagickRealType) atanh((double) alpha));
2427 }
cristyb33454f2011-08-03 02:10:45 +00002428#endif
cristy3ed852e2009-09-05 21:47:34 +00002429 if (LocaleNCompare(expression,"atan",4) == 0)
2430 {
2431 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2432 exception);
2433 return((MagickRealType) atan((double) alpha));
2434 }
2435 if (LocaleCompare(expression,"a") == 0)
2436 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2437 break;
2438 }
2439 case 'B':
2440 case 'b':
2441 {
2442 if (LocaleCompare(expression,"b") == 0)
2443 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2444 break;
2445 }
2446 case 'C':
2447 case 'c':
2448 {
2449 if (LocaleNCompare(expression,"ceil",4) == 0)
2450 {
2451 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2452 exception);
2453 return((MagickRealType) ceil((double) alpha));
2454 }
2455 if (LocaleNCompare(expression,"cosh",4) == 0)
2456 {
2457 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2458 exception);
2459 return((MagickRealType) cosh((double) alpha));
2460 }
2461 if (LocaleNCompare(expression,"cos",3) == 0)
2462 {
2463 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2464 exception);
2465 return((MagickRealType) cos((double) alpha));
2466 }
2467 if (LocaleCompare(expression,"c") == 0)
2468 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2469 break;
2470 }
2471 case 'D':
2472 case 'd':
2473 {
2474 if (LocaleNCompare(expression,"debug",5) == 0)
2475 {
2476 const char
2477 *type;
2478
2479 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2480 exception);
2481 if (fx_info->images->colorspace == CMYKColorspace)
2482 switch (channel)
2483 {
cristy0568ffc2011-07-25 16:54:14 +00002484 case CyanPixelChannel: type="cyan"; break;
2485 case MagentaPixelChannel: type="magenta"; break;
2486 case YellowPixelChannel: type="yellow"; break;
2487 case AlphaPixelChannel: type="opacity"; break;
2488 case BlackPixelChannel: type="black"; break;
cristy3ed852e2009-09-05 21:47:34 +00002489 default: type="unknown"; break;
2490 }
2491 else
2492 switch (channel)
2493 {
cristy0568ffc2011-07-25 16:54:14 +00002494 case RedPixelChannel: type="red"; break;
2495 case GreenPixelChannel: type="green"; break;
2496 case BluePixelChannel: type="blue"; break;
2497 case AlphaPixelChannel: type="opacity"; break;
cristy3ed852e2009-09-05 21:47:34 +00002498 default: type="unknown"; break;
2499 }
2500 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2501 if (strlen(subexpression) > 1)
2502 subexpression[strlen(subexpression)-1]='\0';
2503 if (fx_info->file != (FILE *) NULL)
cristy1e604812011-05-19 18:07:50 +00002504 (void) FormatLocaleFile(fx_info->file,
2505 "%s[%.20g,%.20g].%s: %s=%.*g\n",fx_info->images->filename,
2506 (double) x,(double) y,type,subexpression,GetMagickPrecision(),
2507 (double) alpha);
cristy3ed852e2009-09-05 21:47:34 +00002508 return(0.0);
2509 }
2510 break;
2511 }
2512 case 'E':
2513 case 'e':
2514 {
2515 if (LocaleCompare(expression,"epsilon") == 0)
2516 return((MagickRealType) MagickEpsilon);
2517 if (LocaleNCompare(expression,"exp",3) == 0)
2518 {
2519 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2520 exception);
2521 return((MagickRealType) exp((double) alpha));
2522 }
2523 if (LocaleCompare(expression,"e") == 0)
2524 return((MagickRealType) 2.7182818284590452354);
2525 break;
2526 }
2527 case 'F':
2528 case 'f':
2529 {
2530 if (LocaleNCompare(expression,"floor",5) == 0)
2531 {
2532 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2533 exception);
2534 return((MagickRealType) floor((double) alpha));
2535 }
2536 break;
2537 }
2538 case 'G':
2539 case 'g':
2540 {
cristy9eeedea2011-11-02 19:04:05 +00002541 if (LocaleNCompare(expression,"gauss",5) == 0)
2542 {
2543 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2544 exception);
2545 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
2546 return((MagickRealType) gamma);
2547 }
cristyb0aad4c2011-11-02 19:30:35 +00002548 if (LocaleNCompare(expression,"gcd",3) == 0)
2549 {
2550 MagickOffsetType
2551 gcd;
2552
2553 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2554 exception);
cristy76461162011-11-02 19:32:19 +00002555 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType)
2556 (*beta+0.5));
cristyb0aad4c2011-11-02 19:30:35 +00002557 return((MagickRealType) gcd);
2558 }
cristy3ed852e2009-09-05 21:47:34 +00002559 if (LocaleCompare(expression,"g") == 0)
2560 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2561 break;
2562 }
2563 case 'H':
2564 case 'h':
2565 {
2566 if (LocaleCompare(expression,"h") == 0)
2567 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2568 if (LocaleCompare(expression,"hue") == 0)
2569 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2570 if (LocaleNCompare(expression,"hypot",5) == 0)
2571 {
2572 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2573 exception);
2574 return((MagickRealType) hypot((double) alpha,(double) *beta));
2575 }
2576 break;
2577 }
2578 case 'K':
2579 case 'k':
2580 {
2581 if (LocaleCompare(expression,"k") == 0)
2582 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2583 break;
2584 }
2585 case 'I':
2586 case 'i':
2587 {
2588 if (LocaleCompare(expression,"intensity") == 0)
2589 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2590 if (LocaleNCompare(expression,"int",3) == 0)
2591 {
2592 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2593 exception);
cristy16788e42010-08-13 13:44:26 +00002594 return((MagickRealType) floor(alpha));
cristy3ed852e2009-09-05 21:47:34 +00002595 }
cristy639399c2011-11-02 19:16:15 +00002596 if (LocaleNCompare(expression,"isnan",5) == 0)
2597 {
2598 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2599 exception);
cristy17a10202011-11-02 19:17:04 +00002600 return((MagickRealType) !!isnan((double) alpha));
cristy639399c2011-11-02 19:16:15 +00002601 }
cristy3ed852e2009-09-05 21:47:34 +00002602 if (LocaleCompare(expression,"i") == 0)
2603 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2604 break;
2605 }
2606 case 'J':
2607 case 'j':
2608 {
2609 if (LocaleCompare(expression,"j") == 0)
2610 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
cristy161b9262010-03-20 19:34:32 +00002611#if defined(MAGICKCORE_HAVE_J0)
cristyee56cf12010-03-01 22:17:06 +00002612 if (LocaleNCompare(expression,"j0",2) == 0)
2613 {
2614 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2615 exception);
2616 return((MagickRealType) j0((double) alpha));
2617 }
cristy161b9262010-03-20 19:34:32 +00002618#endif
2619#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002620 if (LocaleNCompare(expression,"j1",2) == 0)
2621 {
2622 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2623 exception);
2624 return((MagickRealType) j1((double) alpha));
2625 }
cristy161b9262010-03-20 19:34:32 +00002626#endif
cristyaa018fa2010-04-08 23:03:54 +00002627#if defined(MAGICKCORE_HAVE_J1)
cristya6a09e72010-03-02 14:51:02 +00002628 if (LocaleNCompare(expression,"jinc",4) == 0)
2629 {
2630 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2631 exception);
cristy0946a822010-03-12 17:14:58 +00002632 if (alpha == 0.0)
2633 return(1.0);
cristy69928f92010-03-12 13:27:09 +00002634 gamma=(MagickRealType) (2.0*j1((double) (MagickPI*alpha))/
cristyfce2f7b2010-03-12 00:29:49 +00002635 (MagickPI*alpha));
2636 return(gamma);
cristya6a09e72010-03-02 14:51:02 +00002637 }
cristyaa018fa2010-04-08 23:03:54 +00002638#endif
cristy3ed852e2009-09-05 21:47:34 +00002639 break;
2640 }
2641 case 'L':
2642 case 'l':
2643 {
2644 if (LocaleNCompare(expression,"ln",2) == 0)
2645 {
2646 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2647 exception);
2648 return((MagickRealType) log((double) alpha));
2649 }
cristyc8ed5322010-08-31 12:07:59 +00002650 if (LocaleNCompare(expression,"logtwo",6) == 0)
cristy3ed852e2009-09-05 21:47:34 +00002651 {
cristyc8ed5322010-08-31 12:07:59 +00002652 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
cristy3ed852e2009-09-05 21:47:34 +00002653 exception);
2654 return((MagickRealType) log10((double) alpha))/log10(2.0);
2655 }
2656 if (LocaleNCompare(expression,"log",3) == 0)
2657 {
2658 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2659 exception);
2660 return((MagickRealType) log10((double) alpha));
2661 }
2662 if (LocaleCompare(expression,"lightness") == 0)
2663 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2664 break;
2665 }
2666 case 'M':
2667 case 'm':
2668 {
2669 if (LocaleCompare(expression,"MaxRGB") == 0)
2670 return((MagickRealType) QuantumRange);
2671 if (LocaleNCompare(expression,"maxima",6) == 0)
2672 break;
2673 if (LocaleNCompare(expression,"max",3) == 0)
2674 return(FxMax(fx_info,channel,x,y,expression+3,exception));
2675 if (LocaleNCompare(expression,"minima",6) == 0)
2676 break;
2677 if (LocaleNCompare(expression,"min",3) == 0)
2678 return(FxMin(fx_info,channel,x,y,expression+3,exception));
2679 if (LocaleNCompare(expression,"mod",3) == 0)
2680 {
2681 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2682 exception);
2683 return((MagickRealType) fmod((double) alpha,(double) *beta));
2684 }
2685 if (LocaleCompare(expression,"m") == 0)
2686 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2687 break;
2688 }
2689 case 'N':
2690 case 'n':
2691 {
cristyad3502e2011-11-02 19:10:45 +00002692 if (LocaleNCompare(expression,"not",3) == 0)
2693 {
2694 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2695 exception);
2696 return((MagickRealType) (alpha < MagickEpsilon));
2697 }
cristy3ed852e2009-09-05 21:47:34 +00002698 if (LocaleCompare(expression,"n") == 0)
2699 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2700 break;
2701 }
2702 case 'O':
2703 case 'o':
2704 {
2705 if (LocaleCompare(expression,"Opaque") == 0)
2706 return(1.0);
2707 if (LocaleCompare(expression,"o") == 0)
2708 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2709 break;
2710 }
2711 case 'P':
2712 case 'p':
2713 {
cristy670aa3c2011-11-03 00:54:00 +00002714 if (LocaleCompare(expression,"phi") == 0)
2715 return((MagickRealType) MagickPHI);
cristy3ed852e2009-09-05 21:47:34 +00002716 if (LocaleCompare(expression,"pi") == 0)
2717 return((MagickRealType) MagickPI);
2718 if (LocaleNCompare(expression,"pow",3) == 0)
2719 {
2720 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2721 exception);
2722 return((MagickRealType) pow((double) alpha,(double) *beta));
2723 }
2724 if (LocaleCompare(expression,"p") == 0)
2725 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2726 break;
2727 }
2728 case 'Q':
2729 case 'q':
2730 {
2731 if (LocaleCompare(expression,"QuantumRange") == 0)
2732 return((MagickRealType) QuantumRange);
2733 if (LocaleCompare(expression,"QuantumScale") == 0)
2734 return((MagickRealType) QuantumScale);
2735 break;
2736 }
2737 case 'R':
2738 case 'r':
2739 {
2740 if (LocaleNCompare(expression,"rand",4) == 0)
2741 return((MagickRealType) GetPseudoRandomValue(fx_info->random_info));
2742 if (LocaleNCompare(expression,"round",5) == 0)
2743 {
2744 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2745 exception);
cristy16788e42010-08-13 13:44:26 +00002746 return((MagickRealType) floor((double) alpha+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002747 }
2748 if (LocaleCompare(expression,"r") == 0)
2749 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2750 break;
2751 }
2752 case 'S':
2753 case 's':
2754 {
2755 if (LocaleCompare(expression,"saturation") == 0)
2756 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2757 if (LocaleNCompare(expression,"sign",4) == 0)
2758 {
2759 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2760 exception);
2761 return(alpha < 0.0 ? -1.0 : 1.0);
2762 }
cristya6a09e72010-03-02 14:51:02 +00002763 if (LocaleNCompare(expression,"sinc",4) == 0)
2764 {
2765 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2766 exception);
2767 if (alpha == 0)
2768 return(1.0);
2769 gamma=(MagickRealType) (sin((double) (MagickPI*alpha))/
2770 (MagickPI*alpha));
2771 return(gamma);
2772 }
cristy3ed852e2009-09-05 21:47:34 +00002773 if (LocaleNCompare(expression,"sinh",4) == 0)
2774 {
2775 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2776 exception);
2777 return((MagickRealType) sinh((double) alpha));
2778 }
2779 if (LocaleNCompare(expression,"sin",3) == 0)
2780 {
2781 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2782 exception);
2783 return((MagickRealType) sin((double) alpha));
2784 }
2785 if (LocaleNCompare(expression,"sqrt",4) == 0)
2786 {
2787 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2788 exception);
2789 return((MagickRealType) sqrt((double) alpha));
2790 }
cristy9eeedea2011-11-02 19:04:05 +00002791 if (LocaleNCompare(expression,"squish",6) == 0)
2792 {
2793 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2794 exception);
2795 return((MagickRealType) (1.0/(1.0+exp((double) (4.0*alpha)))));
2796 }
cristy3ed852e2009-09-05 21:47:34 +00002797 if (LocaleCompare(expression,"s") == 0)
2798 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2799 break;
2800 }
2801 case 'T':
2802 case 't':
2803 {
2804 if (LocaleNCompare(expression,"tanh",4) == 0)
2805 {
2806 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2807 exception);
2808 return((MagickRealType) tanh((double) alpha));
2809 }
2810 if (LocaleNCompare(expression,"tan",3) == 0)
2811 {
2812 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2813 exception);
2814 return((MagickRealType) tan((double) alpha));
2815 }
2816 if (LocaleCompare(expression,"Transparent") == 0)
2817 return(0.0);
cristy16788e42010-08-13 13:44:26 +00002818 if (LocaleNCompare(expression,"trunc",5) == 0)
2819 {
2820 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2821 exception);
2822 if (alpha >= 0.0)
2823 return((MagickRealType) floor((double) alpha));
2824 return((MagickRealType) ceil((double) alpha));
2825 }
cristy3ed852e2009-09-05 21:47:34 +00002826 if (LocaleCompare(expression,"t") == 0)
2827 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2828 break;
2829 }
2830 case 'U':
2831 case 'u':
2832 {
2833 if (LocaleCompare(expression,"u") == 0)
2834 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2835 break;
2836 }
2837 case 'V':
2838 case 'v':
2839 {
2840 if (LocaleCompare(expression,"v") == 0)
2841 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2842 break;
2843 }
2844 case 'W':
2845 case 'w':
2846 {
cristy9eeedea2011-11-02 19:04:05 +00002847 if (LocaleNCompare(expression,"while",5) == 0)
2848 {
2849 do
2850 {
2851 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2852 exception);
2853 } while (fabs((double) alpha) >= MagickEpsilon);
2854 return((MagickRealType) *beta);
2855 }
cristy3ed852e2009-09-05 21:47:34 +00002856 if (LocaleCompare(expression,"w") == 0)
2857 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2858 break;
2859 }
2860 case 'Y':
2861 case 'y':
2862 {
2863 if (LocaleCompare(expression,"y") == 0)
2864 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2865 break;
2866 }
2867 case 'Z':
2868 case 'z':
2869 {
2870 if (LocaleCompare(expression,"z") == 0)
2871 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2872 break;
2873 }
2874 default:
2875 break;
2876 }
2877 q=(char *) expression;
cristyc1acd842011-05-19 23:05:47 +00002878 alpha=InterpretLocaleValue(expression,&q);
cristy3ed852e2009-09-05 21:47:34 +00002879 if (q == expression)
2880 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2881 return(alpha);
2882}
2883
cristy7832dc22011-09-05 01:21:53 +00002884MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
cristy3ed852e2009-09-05 21:47:34 +00002885 MagickRealType *alpha,ExceptionInfo *exception)
2886{
2887 MagickBooleanType
2888 status;
2889
cristy541ae572011-08-05 19:08:59 +00002890 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2891 exception);
cristy3ed852e2009-09-05 21:47:34 +00002892 return(status);
2893}
2894
2895MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
2896 MagickRealType *alpha,ExceptionInfo *exception)
2897{
2898 FILE
2899 *file;
2900
2901 MagickBooleanType
2902 status;
2903
2904 file=fx_info->file;
2905 fx_info->file=(FILE *) NULL;
cristy541ae572011-08-05 19:08:59 +00002906 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2907 exception);
cristy3ed852e2009-09-05 21:47:34 +00002908 fx_info->file=file;
2909 return(status);
2910}
2911
cristy7832dc22011-09-05 01:21:53 +00002912MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002913 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002914 MagickRealType *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002915{
2916 MagickRealType
2917 beta;
2918
2919 beta=0.0;
2920 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2921 exception);
2922 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2923}
2924
2925/*
2926%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2927% %
2928% %
2929% %
2930% F x I m a g e %
2931% %
2932% %
2933% %
2934%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2935%
2936% FxImage() applies a mathematical expression to the specified image.
2937%
2938% The format of the FxImage method is:
2939%
2940% Image *FxImage(const Image *image,const char *expression,
2941% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002942%
2943% A description of each parameter follows:
2944%
2945% o image: the image.
2946%
cristy3ed852e2009-09-05 21:47:34 +00002947% o expression: A mathematical expression.
2948%
2949% o exception: return any errors or warnings in this structure.
2950%
2951*/
2952
2953static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2954{
cristybb503372010-05-27 20:51:26 +00002955 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002956 i;
2957
2958 assert(fx_info != (FxInfo **) NULL);
cristybb503372010-05-27 20:51:26 +00002959 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
cristy3ed852e2009-09-05 21:47:34 +00002960 if (fx_info[i] != (FxInfo *) NULL)
2961 fx_info[i]=DestroyFxInfo(fx_info[i]);
cristyb41ee102010-10-04 16:46:15 +00002962 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
cristy3ed852e2009-09-05 21:47:34 +00002963 return(fx_info);
2964}
2965
2966static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2967 ExceptionInfo *exception)
2968{
2969 char
2970 *fx_expression;
2971
2972 FxInfo
2973 **fx_info;
2974
2975 MagickRealType
2976 alpha;
2977
cristybb503372010-05-27 20:51:26 +00002978 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002979 i;
2980
cristybb503372010-05-27 20:51:26 +00002981 size_t
cristy3ed852e2009-09-05 21:47:34 +00002982 number_threads;
2983
2984 number_threads=GetOpenMPMaximumThreads();
cristyb41ee102010-10-04 16:46:15 +00002985 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +00002986 if (fx_info == (FxInfo **) NULL)
2987 return((FxInfo **) NULL);
2988 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2989 if (*expression != '@')
2990 fx_expression=ConstantString(expression);
2991 else
2992 fx_expression=FileToString(expression+1,~0,exception);
cristybb503372010-05-27 20:51:26 +00002993 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +00002994 {
2995 fx_info[i]=AcquireFxInfo(image,fx_expression);
2996 if (fx_info[i] == (FxInfo *) NULL)
2997 return(DestroyFxThreadSet(fx_info));
2998 (void) FxPreprocessExpression(fx_info[i],&alpha,fx_info[i]->exception);
2999 }
3000 fx_expression=DestroyString(fx_expression);
3001 return(fx_info);
3002}
3003
3004MagickExport Image *FxImage(const Image *image,const char *expression,
3005 ExceptionInfo *exception)
3006{
cristy3ed852e2009-09-05 21:47:34 +00003007#define FxImageTag "Fx/Image"
3008
cristyfa112112010-01-04 17:48:07 +00003009 CacheView
cristy79cedc72011-07-25 00:41:15 +00003010 *fx_view,
3011 *image_view;
cristyfa112112010-01-04 17:48:07 +00003012
cristy3ed852e2009-09-05 21:47:34 +00003013 FxInfo
cristyfa112112010-01-04 17:48:07 +00003014 **restrict fx_info;
cristy3ed852e2009-09-05 21:47:34 +00003015
3016 Image
3017 *fx_image;
3018
cristy3ed852e2009-09-05 21:47:34 +00003019 MagickBooleanType
3020 status;
3021
cristybb503372010-05-27 20:51:26 +00003022 MagickOffsetType
3023 progress;
3024
cristy3ed852e2009-09-05 21:47:34 +00003025 MagickRealType
3026 alpha;
3027
cristybb503372010-05-27 20:51:26 +00003028 ssize_t
3029 y;
3030
cristy3ed852e2009-09-05 21:47:34 +00003031 assert(image != (Image *) NULL);
3032 assert(image->signature == MagickSignature);
3033 if (image->debug != MagickFalse)
3034 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy79cedc72011-07-25 00:41:15 +00003035 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00003036 if (fx_image == (Image *) NULL)
3037 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003038 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003039 {
cristy3ed852e2009-09-05 21:47:34 +00003040 fx_image=DestroyImage(fx_image);
3041 return((Image *) NULL);
3042 }
3043 fx_info=AcquireFxThreadSet(image,expression,exception);
3044 if (fx_info == (FxInfo **) NULL)
3045 {
3046 fx_image=DestroyImage(fx_image);
3047 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3048 }
3049 status=FxPreprocessExpression(fx_info[0],&alpha,exception);
3050 if (status == MagickFalse)
3051 {
3052 fx_image=DestroyImage(fx_image);
3053 fx_info=DestroyFxThreadSet(fx_info);
3054 return((Image *) NULL);
3055 }
3056 /*
3057 Fx image.
3058 */
3059 status=MagickTrue;
3060 progress=0;
cristy79cedc72011-07-25 00:41:15 +00003061 image_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00003062 fx_view=AcquireCacheView(fx_image);
cristyb5d5f722009-11-04 03:03:49 +00003063#if defined(MAGICKCORE_OPENMP_SUPPORT)
3064 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003065#endif
cristybb503372010-05-27 20:51:26 +00003066 for (y=0; y < (ssize_t) fx_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003067 {
cristy5c9e6f22010-09-17 17:31:01 +00003068 const int
3069 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003070
cristy79cedc72011-07-25 00:41:15 +00003071 register const Quantum
3072 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003073
cristy4c08aed2011-07-01 19:47:50 +00003074 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003075 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003076
cristy79cedc72011-07-25 00:41:15 +00003077 register ssize_t
3078 x;
3079
cristy3ed852e2009-09-05 21:47:34 +00003080 if (status == MagickFalse)
3081 continue;
cristy79cedc72011-07-25 00:41:15 +00003082 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy3ed852e2009-09-05 21:47:34 +00003083 q=GetCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
cristy79cedc72011-07-25 00:41:15 +00003084 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003085 {
3086 status=MagickFalse;
3087 continue;
3088 }
cristybb503372010-05-27 20:51:26 +00003089 for (x=0; x < (ssize_t) fx_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003090 {
cristy79cedc72011-07-25 00:41:15 +00003091 register ssize_t
3092 i;
3093
3094 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3095 {
3096 MagickRealType
3097 alpha;
3098
3099 PixelChannel
3100 channel;
3101
3102 PixelTrait
3103 fx_traits,
3104 traits;
3105
3106 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
cristy79cedc72011-07-25 00:41:15 +00003107 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3108 fx_traits=GetPixelChannelMapTraits(fx_image,channel);
cristy010d7d12011-08-31 01:02:48 +00003109 if ((traits == UndefinedPixelTrait) ||
3110 (fx_traits == UndefinedPixelTrait))
cristy79cedc72011-07-25 00:41:15 +00003111 continue;
3112 if ((fx_traits & CopyPixelTrait) != 0)
3113 {
cristy0beccfa2011-09-25 20:47:53 +00003114 SetPixelChannel(fx_image,channel,p[i],q);
cristy79cedc72011-07-25 00:41:15 +00003115 continue;
3116 }
3117 alpha=0.0;
cristy0568ffc2011-07-25 16:54:14 +00003118 (void) FxEvaluateChannelExpression(fx_info[id],(PixelChannel) i,x,y,
3119 &alpha,exception);
cristyb3a73b52011-07-26 01:34:43 +00003120 q[i]=ClampToQuantum((MagickRealType) QuantumRange*alpha);
cristy79cedc72011-07-25 00:41:15 +00003121 }
3122 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003123 q+=GetPixelChannels(fx_image);
cristy3ed852e2009-09-05 21:47:34 +00003124 }
3125 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3126 status=MagickFalse;
3127 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3128 {
3129 MagickBooleanType
3130 proceed;
3131
cristyb5d5f722009-11-04 03:03:49 +00003132#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy490408a2011-07-07 14:42:05 +00003133 #pragma omp critical (MagickCore_FxImage)
cristy3ed852e2009-09-05 21:47:34 +00003134#endif
3135 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3136 if (proceed == MagickFalse)
3137 status=MagickFalse;
3138 }
3139 }
cristy3ed852e2009-09-05 21:47:34 +00003140 fx_view=DestroyCacheView(fx_view);
cristy79cedc72011-07-25 00:41:15 +00003141 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003142 fx_info=DestroyFxThreadSet(fx_info);
3143 if (status == MagickFalse)
3144 fx_image=DestroyImage(fx_image);
3145 return(fx_image);
3146}
3147
3148/*
3149%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3150% %
3151% %
3152% %
3153% I m p l o d e I m a g e %
3154% %
3155% %
3156% %
3157%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3158%
3159% ImplodeImage() creates a new image that is a copy of an existing
3160% one with the image pixels "implode" by the specified percentage. It
3161% allocates the memory necessary for the new Image structure and returns a
3162% pointer to the new image.
3163%
3164% The format of the ImplodeImage method is:
3165%
3166% Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003167% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003168%
3169% A description of each parameter follows:
3170%
3171% o implode_image: Method ImplodeImage returns a pointer to the image
3172% after it is implode. A null image is returned if there is a memory
3173% shortage.
3174%
3175% o image: the image.
3176%
3177% o amount: Define the extent of the implosion.
3178%
cristy76f512e2011-09-12 01:26:56 +00003179% o method: the pixel interpolation method.
3180%
cristy3ed852e2009-09-05 21:47:34 +00003181% o exception: return any errors or warnings in this structure.
3182%
3183*/
3184MagickExport Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003185 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003186{
3187#define ImplodeImageTag "Implode/Image"
3188
cristyfa112112010-01-04 17:48:07 +00003189 CacheView
3190 *image_view,
3191 *implode_view;
3192
cristy3ed852e2009-09-05 21:47:34 +00003193 Image
3194 *implode_image;
3195
cristy3ed852e2009-09-05 21:47:34 +00003196 MagickBooleanType
3197 status;
3198
cristybb503372010-05-27 20:51:26 +00003199 MagickOffsetType
3200 progress;
3201
cristy3ed852e2009-09-05 21:47:34 +00003202 MagickRealType
3203 radius;
3204
3205 PointInfo
3206 center,
3207 scale;
3208
cristybb503372010-05-27 20:51:26 +00003209 ssize_t
3210 y;
3211
cristy3ed852e2009-09-05 21:47:34 +00003212 /*
3213 Initialize implode image attributes.
3214 */
3215 assert(image != (Image *) NULL);
3216 assert(image->signature == MagickSignature);
3217 if (image->debug != MagickFalse)
3218 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3219 assert(exception != (ExceptionInfo *) NULL);
3220 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00003221 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3222 exception);
cristy3ed852e2009-09-05 21:47:34 +00003223 if (implode_image == (Image *) NULL)
3224 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003225 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003226 {
cristy3ed852e2009-09-05 21:47:34 +00003227 implode_image=DestroyImage(implode_image);
3228 return((Image *) NULL);
3229 }
cristy4c08aed2011-07-01 19:47:50 +00003230 if (implode_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00003231 implode_image->matte=MagickTrue;
3232 /*
3233 Compute scaling factor.
3234 */
3235 scale.x=1.0;
3236 scale.y=1.0;
3237 center.x=0.5*image->columns;
3238 center.y=0.5*image->rows;
3239 radius=center.x;
3240 if (image->columns > image->rows)
3241 scale.y=(double) image->columns/(double) image->rows;
3242 else
3243 if (image->columns < image->rows)
3244 {
3245 scale.x=(double) image->rows/(double) image->columns;
3246 radius=center.y;
3247 }
3248 /*
3249 Implode image.
3250 */
3251 status=MagickTrue;
3252 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00003253 image_view=AcquireCacheView(image);
3254 implode_view=AcquireCacheView(implode_image);
cristyb5d5f722009-11-04 03:03:49 +00003255#if defined(MAGICKCORE_OPENMP_SUPPORT)
3256 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003257#endif
cristybb503372010-05-27 20:51:26 +00003258 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003259 {
cristy3ed852e2009-09-05 21:47:34 +00003260 MagickRealType
3261 distance;
3262
3263 PointInfo
3264 delta;
3265
cristy6d188022011-09-12 13:23:33 +00003266 register const Quantum
3267 *restrict p;
3268
cristybb503372010-05-27 20:51:26 +00003269 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003270 x;
3271
cristy4c08aed2011-07-01 19:47:50 +00003272 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003273 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003274
3275 if (status == MagickFalse)
3276 continue;
cristy6d188022011-09-12 13:23:33 +00003277 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy3ed852e2009-09-05 21:47:34 +00003278 q=GetCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
3279 exception);
cristy6d188022011-09-12 13:23:33 +00003280 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003281 {
3282 status=MagickFalse;
3283 continue;
3284 }
cristy3ed852e2009-09-05 21:47:34 +00003285 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00003286 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003287 {
cristy6d188022011-09-12 13:23:33 +00003288 register ssize_t
3289 i;
3290
cristy3ed852e2009-09-05 21:47:34 +00003291 /*
3292 Determine if the pixel is within an ellipse.
3293 */
3294 delta.x=scale.x*(double) (x-center.x);
3295 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00003296 if (distance >= (radius*radius))
3297 {
3298 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3299 q[i]=p[i];
3300 }
3301 else
cristy3ed852e2009-09-05 21:47:34 +00003302 {
3303 double
3304 factor;
3305
3306 /*
3307 Implode the pixel.
3308 */
3309 factor=1.0;
3310 if (distance > 0.0)
3311 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/
3312 radius/2)),-amount);
cristy76f512e2011-09-12 01:26:56 +00003313 status=InterpolatePixelChannels(image,image_view,implode_image,method,
3314 (double) (factor*delta.x/scale.x+center.x),(double) (factor*delta.y/
3315 scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00003316 }
cristy6d188022011-09-12 13:23:33 +00003317 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003318 q+=GetPixelChannels(implode_image);
cristy3ed852e2009-09-05 21:47:34 +00003319 }
3320 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3321 status=MagickFalse;
3322 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3323 {
3324 MagickBooleanType
3325 proceed;
3326
cristyb5d5f722009-11-04 03:03:49 +00003327#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003328 #pragma omp critical (MagickCore_ImplodeImage)
3329#endif
3330 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3331 if (proceed == MagickFalse)
3332 status=MagickFalse;
3333 }
3334 }
3335 implode_view=DestroyCacheView(implode_view);
3336 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003337 if (status == MagickFalse)
3338 implode_image=DestroyImage(implode_image);
3339 return(implode_image);
3340}
3341
3342/*
3343%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3344% %
3345% %
3346% %
3347% M o r p h I m a g e s %
3348% %
3349% %
3350% %
3351%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3352%
3353% The MorphImages() method requires a minimum of two images. The first
3354% image is transformed into the second by a number of intervening images
3355% as specified by frames.
3356%
3357% The format of the MorphImage method is:
3358%
cristybb503372010-05-27 20:51:26 +00003359% Image *MorphImages(const Image *image,const size_t number_frames,
cristy3ed852e2009-09-05 21:47:34 +00003360% ExceptionInfo *exception)
3361%
3362% A description of each parameter follows:
3363%
3364% o image: the image.
3365%
3366% o number_frames: Define the number of in-between image to generate.
3367% The more in-between frames, the smoother the morph.
3368%
3369% o exception: return any errors or warnings in this structure.
3370%
3371*/
3372MagickExport Image *MorphImages(const Image *image,
cristybb503372010-05-27 20:51:26 +00003373 const size_t number_frames,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003374{
3375#define MorphImageTag "Morph/Image"
3376
3377 Image
3378 *morph_image,
3379 *morph_images;
3380
cristy9d314ff2011-03-09 01:30:28 +00003381 MagickBooleanType
3382 status;
cristy3ed852e2009-09-05 21:47:34 +00003383
3384 MagickOffsetType
3385 scene;
3386
3387 MagickRealType
3388 alpha,
3389 beta;
3390
3391 register const Image
3392 *next;
3393
cristybb503372010-05-27 20:51:26 +00003394 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003395 i;
3396
cristy9d314ff2011-03-09 01:30:28 +00003397 ssize_t
3398 y;
cristy3ed852e2009-09-05 21:47:34 +00003399
3400 /*
3401 Clone first frame in sequence.
3402 */
3403 assert(image != (Image *) NULL);
3404 assert(image->signature == MagickSignature);
3405 if (image->debug != MagickFalse)
3406 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3407 assert(exception != (ExceptionInfo *) NULL);
3408 assert(exception->signature == MagickSignature);
3409 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3410 if (morph_images == (Image *) NULL)
3411 return((Image *) NULL);
3412 if (GetNextImageInList(image) == (Image *) NULL)
3413 {
3414 /*
3415 Morph single image.
3416 */
cristybb503372010-05-27 20:51:26 +00003417 for (i=1; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003418 {
3419 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3420 if (morph_image == (Image *) NULL)
3421 {
3422 morph_images=DestroyImageList(morph_images);
3423 return((Image *) NULL);
3424 }
3425 AppendImageToList(&morph_images,morph_image);
cristy8b27a6d2010-02-14 03:31:15 +00003426 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003427 {
cristy8b27a6d2010-02-14 03:31:15 +00003428 MagickBooleanType
3429 proceed;
3430
cristybb503372010-05-27 20:51:26 +00003431 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3432 number_frames);
cristy8b27a6d2010-02-14 03:31:15 +00003433 if (proceed == MagickFalse)
3434 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00003435 }
3436 }
3437 return(GetFirstImageInList(morph_images));
3438 }
3439 /*
3440 Morph image sequence.
3441 */
3442 status=MagickTrue;
3443 scene=0;
3444 next=image;
3445 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3446 {
cristybb503372010-05-27 20:51:26 +00003447 for (i=0; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003448 {
3449 CacheView
3450 *image_view,
3451 *morph_view;
3452
3453 beta=(MagickRealType) (i+1.0)/(MagickRealType) (number_frames+1.0);
3454 alpha=1.0-beta;
cristy15b98cd2010-09-12 19:42:50 +00003455 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
cristybb503372010-05-27 20:51:26 +00003456 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*
cristy15b98cd2010-09-12 19:42:50 +00003457 next->rows+beta*GetNextImageInList(next)->rows+0.5),
3458 next->filter,next->blur,exception);
cristy3ed852e2009-09-05 21:47:34 +00003459 if (morph_image == (Image *) NULL)
3460 {
3461 morph_images=DestroyImageList(morph_images);
3462 return((Image *) NULL);
3463 }
cristy574cc262011-08-05 01:23:58 +00003464 if (SetImageStorageClass(morph_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003465 {
cristy3ed852e2009-09-05 21:47:34 +00003466 morph_image=DestroyImage(morph_image);
3467 return((Image *) NULL);
3468 }
3469 AppendImageToList(&morph_images,morph_image);
3470 morph_images=GetLastImageInList(morph_images);
cristy15b98cd2010-09-12 19:42:50 +00003471 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
3472 morph_images->rows,GetNextImageInList(next)->filter,
3473 GetNextImageInList(next)->blur,exception);
cristy3ed852e2009-09-05 21:47:34 +00003474 if (morph_image == (Image *) NULL)
3475 {
3476 morph_images=DestroyImageList(morph_images);
3477 return((Image *) NULL);
3478 }
3479 image_view=AcquireCacheView(morph_image);
3480 morph_view=AcquireCacheView(morph_images);
cristyb5d5f722009-11-04 03:03:49 +00003481#if defined(MAGICKCORE_OPENMP_SUPPORT)
3482 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +00003483#endif
cristybb503372010-05-27 20:51:26 +00003484 for (y=0; y < (ssize_t) morph_images->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003485 {
3486 MagickBooleanType
3487 sync;
3488
cristy4c08aed2011-07-01 19:47:50 +00003489 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003490 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003491
cristybb503372010-05-27 20:51:26 +00003492 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003493 x;
3494
cristy4c08aed2011-07-01 19:47:50 +00003495 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003496 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003497
3498 if (status == MagickFalse)
3499 continue;
3500 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3501 exception);
3502 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3503 exception);
cristy4c08aed2011-07-01 19:47:50 +00003504 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003505 {
3506 status=MagickFalse;
3507 continue;
3508 }
cristybb503372010-05-27 20:51:26 +00003509 for (x=0; x < (ssize_t) morph_images->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003510 {
cristy4c08aed2011-07-01 19:47:50 +00003511 SetPixelRed(morph_images,ClampToQuantum(alpha*
3512 GetPixelRed(morph_images,q)+beta*GetPixelRed(morph_image,p)),q);
3513 SetPixelGreen(morph_images,ClampToQuantum(alpha*
3514 GetPixelGreen(morph_images,q)+beta*GetPixelGreen(morph_image,p)),q);
3515 SetPixelBlue(morph_images,ClampToQuantum(alpha*
3516 GetPixelBlue(morph_images,q)+beta*GetPixelBlue(morph_image,p)),q);
3517 SetPixelAlpha(morph_images,ClampToQuantum(alpha*
3518 GetPixelAlpha(morph_images,q)+beta*GetPixelAlpha(morph_image,p)),q);
3519 if ((morph_image->colorspace == CMYKColorspace) &&
3520 (morph_images->colorspace == CMYKColorspace))
3521 SetPixelBlack(morph_images,ClampToQuantum(alpha*
3522 GetPixelBlack(morph_images,q)+beta*GetPixelBlack(morph_image,p)),
3523 q);
cristyed231572011-07-14 02:18:59 +00003524 p+=GetPixelChannels(morph_image);
3525 q+=GetPixelChannels(morph_images);
cristy3ed852e2009-09-05 21:47:34 +00003526 }
3527 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3528 if (sync == MagickFalse)
3529 status=MagickFalse;
3530 }
3531 morph_view=DestroyCacheView(morph_view);
3532 image_view=DestroyCacheView(image_view);
3533 morph_image=DestroyImage(morph_image);
3534 }
cristybb503372010-05-27 20:51:26 +00003535 if (i < (ssize_t) number_frames)
cristy3ed852e2009-09-05 21:47:34 +00003536 break;
3537 /*
3538 Clone last frame in sequence.
3539 */
3540 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3541 if (morph_image == (Image *) NULL)
3542 {
3543 morph_images=DestroyImageList(morph_images);
3544 return((Image *) NULL);
3545 }
3546 AppendImageToList(&morph_images,morph_image);
3547 morph_images=GetLastImageInList(morph_images);
3548 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3549 {
3550 MagickBooleanType
3551 proceed;
3552
cristyb5d5f722009-11-04 03:03:49 +00003553#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003554 #pragma omp critical (MagickCore_MorphImages)
3555#endif
3556 proceed=SetImageProgress(image,MorphImageTag,scene,
3557 GetImageListLength(image));
3558 if (proceed == MagickFalse)
3559 status=MagickFalse;
3560 }
3561 scene++;
3562 }
3563 if (GetNextImageInList(next) != (Image *) NULL)
3564 {
3565 morph_images=DestroyImageList(morph_images);
3566 return((Image *) NULL);
3567 }
3568 return(GetFirstImageInList(morph_images));
3569}
3570
3571/*
3572%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3573% %
3574% %
3575% %
3576% P l a s m a I m a g e %
3577% %
3578% %
3579% %
3580%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3581%
3582% PlasmaImage() initializes an image with plasma fractal values. The image
3583% must be initialized with a base color and the random number generator
3584% seeded before this method is called.
3585%
3586% The format of the PlasmaImage method is:
3587%
3588% MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
cristy5cbc0162011-08-29 00:36:28 +00003589% size_t attenuate,size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003590%
3591% A description of each parameter follows:
3592%
3593% o image: the image.
3594%
3595% o segment: Define the region to apply plasma fractals values.
3596%
glennrp7dae1ca2010-09-16 12:17:35 +00003597% o attenuate: Define the plasma attenuation factor.
cristy3ed852e2009-09-05 21:47:34 +00003598%
3599% o depth: Limit the plasma recursion depth.
3600%
cristy5cbc0162011-08-29 00:36:28 +00003601% o exception: return any errors or warnings in this structure.
3602%
cristy3ed852e2009-09-05 21:47:34 +00003603*/
3604
3605static inline Quantum PlasmaPixel(RandomInfo *random_info,
3606 const MagickRealType pixel,const MagickRealType noise)
3607{
3608 Quantum
3609 plasma;
3610
cristyce70c172010-01-07 17:15:30 +00003611 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
cristy3ed852e2009-09-05 21:47:34 +00003612 noise/2.0);
3613 return(plasma);
3614}
3615
cristyda1f9c12011-10-02 21:39:49 +00003616static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3617 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3618 const SegmentInfo *segment,size_t attenuate,size_t depth,
3619 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003620{
cristy3ed852e2009-09-05 21:47:34 +00003621 MagickRealType
3622 plasma;
3623
cristyda1f9c12011-10-02 21:39:49 +00003624 PixelChannel
3625 channel;
3626
3627 PixelTrait
3628 traits;
3629
3630 register const Quantum
3631 *restrict u,
3632 *restrict v;
3633
3634 register Quantum
3635 *restrict q;
3636
3637 register ssize_t
3638 i;
cristy3ed852e2009-09-05 21:47:34 +00003639
cristy9d314ff2011-03-09 01:30:28 +00003640 ssize_t
3641 x,
3642 x_mid,
3643 y,
3644 y_mid;
3645
cristy3ed852e2009-09-05 21:47:34 +00003646 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3647 return(MagickTrue);
3648 if (depth != 0)
3649 {
3650 SegmentInfo
3651 local_info;
3652
3653 /*
3654 Divide the area into quadrants and recurse.
3655 */
3656 depth--;
3657 attenuate++;
cristybb503372010-05-27 20:51:26 +00003658 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3659 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003660 local_info=(*segment);
3661 local_info.x2=(double) x_mid;
3662 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003663 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3664 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003665 local_info=(*segment);
3666 local_info.y1=(double) y_mid;
3667 local_info.x2=(double) x_mid;
cristyda1f9c12011-10-02 21:39:49 +00003668 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3669 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003670 local_info=(*segment);
3671 local_info.x1=(double) x_mid;
3672 local_info.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.x1=(double) x_mid;
3677 local_info.y1=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003678 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3679 &local_info,attenuate,depth,exception));
cristy3ed852e2009-09-05 21:47:34 +00003680 }
cristybb503372010-05-27 20:51:26 +00003681 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3682 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003683 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3684 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3685 return(MagickFalse);
3686 /*
3687 Average pixels and apply plasma.
3688 */
cristy3ed852e2009-09-05 21:47:34 +00003689 plasma=(MagickRealType) QuantumRange/(2.0*attenuate);
3690 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3691 {
cristy3ed852e2009-09-05 21:47:34 +00003692 /*
3693 Left pixel.
3694 */
cristybb503372010-05-27 20:51:26 +00003695 x=(ssize_t) ceil(segment->x1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003696 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3697 1,1,exception);
3698 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3699 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003700 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003701 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3702 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003703 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003704 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3705 {
3706 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3707 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3708 if (traits == UndefinedPixelTrait)
3709 continue;
3710 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3711 }
cristyc5c6f662010-09-22 14:23:02 +00003712 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003713 if (segment->x1 != segment->x2)
3714 {
3715 /*
3716 Right pixel.
3717 */
cristybb503372010-05-27 20:51:26 +00003718 x=(ssize_t) ceil(segment->x2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003719 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3720 1,1,exception);
3721 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3722 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003723 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003724 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3725 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003726 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003727 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3728 {
3729 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3730 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3731 if (traits == UndefinedPixelTrait)
3732 continue;
3733 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3734 }
cristyc5c6f662010-09-22 14:23:02 +00003735 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003736 }
3737 }
3738 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3739 {
3740 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3741 {
cristy3ed852e2009-09-05 21:47:34 +00003742 /*
3743 Bottom pixel.
3744 */
cristybb503372010-05-27 20:51:26 +00003745 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003746 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3747 1,1,exception);
3748 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3749 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003750 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003751 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3752 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003753 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003754 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3755 {
3756 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3757 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3758 if (traits == UndefinedPixelTrait)
3759 continue;
3760 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3761 }
cristyc5c6f662010-09-22 14:23:02 +00003762 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003763 }
3764 if (segment->y1 != segment->y2)
3765 {
cristy3ed852e2009-09-05 21:47:34 +00003766 /*
3767 Top pixel.
3768 */
cristybb503372010-05-27 20:51:26 +00003769 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003770 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3771 1,1,exception);
3772 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3773 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003774 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003775 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3776 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003777 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003778 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3779 {
3780 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3781 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3782 if (traits == UndefinedPixelTrait)
3783 continue;
3784 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3785 }
cristyc5c6f662010-09-22 14:23:02 +00003786 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003787 }
3788 }
3789 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3790 {
cristy3ed852e2009-09-05 21:47:34 +00003791 /*
3792 Middle pixel.
3793 */
cristybb503372010-05-27 20:51:26 +00003794 x=(ssize_t) ceil(segment->x1-0.5);
3795 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003796 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
cristybb503372010-05-27 20:51:26 +00003797 x=(ssize_t) ceil(segment->x2-0.5);
3798 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003799 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003800 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003801 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3802 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003803 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003804 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3805 {
3806 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
3807 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
3808 if (traits == UndefinedPixelTrait)
3809 continue;
3810 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3811 }
cristyc5c6f662010-09-22 14:23:02 +00003812 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003813 }
3814 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3815 return(MagickTrue);
3816 return(MagickFalse);
3817}
cristyda1f9c12011-10-02 21:39:49 +00003818
cristy3ed852e2009-09-05 21:47:34 +00003819MagickExport MagickBooleanType PlasmaImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00003820 const SegmentInfo *segment,size_t attenuate,size_t depth,
3821 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003822{
cristyc5c6f662010-09-22 14:23:02 +00003823 CacheView
cristyda1f9c12011-10-02 21:39:49 +00003824 *image_view,
3825 *u_view,
3826 *v_view;
cristyc5c6f662010-09-22 14:23:02 +00003827
cristy3ed852e2009-09-05 21:47:34 +00003828 MagickBooleanType
3829 status;
3830
3831 RandomInfo
3832 *random_info;
3833
3834 if (image->debug != MagickFalse)
3835 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3836 assert(image != (Image *) NULL);
3837 assert(image->signature == MagickSignature);
3838 if (image->debug != MagickFalse)
3839 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy5cbc0162011-08-29 00:36:28 +00003840 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristyc5c6f662010-09-22 14:23:02 +00003841 return(MagickFalse);
3842 image_view=AcquireCacheView(image);
cristyda1f9c12011-10-02 21:39:49 +00003843 u_view=AcquireCacheView(image);
3844 v_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00003845 random_info=AcquireRandomInfo();
cristyda1f9c12011-10-02 21:39:49 +00003846 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3847 attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003848 random_info=DestroyRandomInfo(random_info);
cristyda1f9c12011-10-02 21:39:49 +00003849 v_view=DestroyCacheView(v_view);
3850 u_view=DestroyCacheView(u_view);
cristyc5c6f662010-09-22 14:23:02 +00003851 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003852 return(status);
3853}
3854
3855/*
3856%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3857% %
3858% %
3859% %
3860% P o l a r o i d I m a g e %
3861% %
3862% %
3863% %
3864%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3865%
3866% PolaroidImage() simulates a Polaroid picture.
3867%
3868% The format of the AnnotateImage method is:
3869%
3870% Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristy5c4e2582011-09-11 19:21:03 +00003871% const double angle,const PixelInterpolateMethod method,
3872% ExceptionInfo exception)
cristy3ed852e2009-09-05 21:47:34 +00003873%
3874% A description of each parameter follows:
3875%
3876% o image: the image.
3877%
3878% o draw_info: the draw info.
3879%
cristycee97112010-05-28 00:44:52 +00003880% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00003881%
cristy5c4e2582011-09-11 19:21:03 +00003882% o method: the pixel interpolation method.
3883%
cristy3ed852e2009-09-05 21:47:34 +00003884% o exception: return any errors or warnings in this structure.
3885%
3886*/
3887MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristy5c4e2582011-09-11 19:21:03 +00003888 const double angle,const PixelInterpolateMethod method,
3889 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003890{
3891 const char
3892 *value;
3893
cristy3ed852e2009-09-05 21:47:34 +00003894 Image
3895 *bend_image,
3896 *caption_image,
3897 *flop_image,
3898 *picture_image,
3899 *polaroid_image,
3900 *rotate_image,
3901 *trim_image;
3902
cristybb503372010-05-27 20:51:26 +00003903 size_t
cristy3ed852e2009-09-05 21:47:34 +00003904 height;
3905
cristy9d314ff2011-03-09 01:30:28 +00003906 ssize_t
3907 quantum;
3908
cristy3ed852e2009-09-05 21:47:34 +00003909 /*
3910 Simulate a Polaroid picture.
3911 */
3912 assert(image != (Image *) NULL);
3913 assert(image->signature == MagickSignature);
3914 if (image->debug != MagickFalse)
3915 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3916 assert(exception != (ExceptionInfo *) NULL);
3917 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00003918 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
cristy3ed852e2009-09-05 21:47:34 +00003919 image->rows)/25.0,10.0);
3920 height=image->rows+2*quantum;
3921 caption_image=(Image *) NULL;
cristyd15e6592011-10-15 00:13:06 +00003922 value=GetImageProperty(image,"caption",exception);
cristy3ed852e2009-09-05 21:47:34 +00003923 if (value != (const char *) NULL)
3924 {
3925 char
3926 *caption,
3927 geometry[MaxTextExtent];
3928
3929 DrawInfo
3930 *annotate_info;
3931
cristy3ed852e2009-09-05 21:47:34 +00003932 MagickBooleanType
3933 status;
3934
cristy9d314ff2011-03-09 01:30:28 +00003935 ssize_t
3936 count;
3937
cristy3ed852e2009-09-05 21:47:34 +00003938 TypeMetric
3939 metrics;
3940
3941 /*
3942 Generate caption image.
3943 */
3944 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3945 if (caption_image == (Image *) NULL)
3946 return((Image *) NULL);
3947 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
3948 caption=InterpretImageProperties((ImageInfo *) NULL,(Image *) image,
cristy018f07f2011-09-04 21:15:19 +00003949 value,exception);
cristy3ed852e2009-09-05 21:47:34 +00003950 (void) CloneString(&annotate_info->text,caption);
cristy6b1d05e2010-09-22 19:17:27 +00003951 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
cristy5cbc0162011-08-29 00:36:28 +00003952 &caption,exception);
cristybb503372010-05-27 20:51:26 +00003953 status=SetImageExtent(caption_image,image->columns,(size_t)
cristy63240882011-08-05 19:05:27 +00003954 ((count+1)*(metrics.ascent-metrics.descent)+0.5),exception);
cristy3ed852e2009-09-05 21:47:34 +00003955 if (status == MagickFalse)
3956 caption_image=DestroyImage(caption_image);
3957 else
3958 {
3959 caption_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003960 (void) SetImageBackgroundColor(caption_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003961 (void) CloneString(&annotate_info->text,caption);
cristyb51dff52011-05-19 16:55:47 +00003962 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
cristy3ed852e2009-09-05 21:47:34 +00003963 metrics.ascent);
3964 if (annotate_info->gravity == UndefinedGravity)
3965 (void) CloneString(&annotate_info->geometry,AcquireString(
3966 geometry));
cristy5cbc0162011-08-29 00:36:28 +00003967 (void) AnnotateImage(caption_image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003968 height+=caption_image->rows;
3969 }
3970 annotate_info=DestroyDrawInfo(annotate_info);
3971 caption=DestroyString(caption);
3972 }
3973 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
3974 exception);
3975 if (picture_image == (Image *) NULL)
3976 {
3977 if (caption_image != (Image *) NULL)
3978 caption_image=DestroyImage(caption_image);
3979 return((Image *) NULL);
3980 }
3981 picture_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003982 (void) SetImageBackgroundColor(picture_image,exception);
cristye941a752011-10-15 01:52:48 +00003983 (void) CompositeImage(picture_image,OverCompositeOp,image,quantum,quantum,
3984 exception);
cristy3ed852e2009-09-05 21:47:34 +00003985 if (caption_image != (Image *) NULL)
3986 {
3987 (void) CompositeImage(picture_image,OverCompositeOp,caption_image,
cristye941a752011-10-15 01:52:48 +00003988 quantum,(ssize_t) (image->rows+3*quantum/2),exception);
cristy3ed852e2009-09-05 21:47:34 +00003989 caption_image=DestroyImage(caption_image);
3990 }
cristy9950d572011-10-01 18:22:35 +00003991 (void) QueryColorCompliance("none",AllCompliance,
3992 &picture_image->background_color,exception);
cristy63240882011-08-05 19:05:27 +00003993 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00003994 rotate_image=RotateImage(picture_image,90.0,exception);
3995 picture_image=DestroyImage(picture_image);
3996 if (rotate_image == (Image *) NULL)
3997 return((Image *) NULL);
3998 picture_image=rotate_image;
3999 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
cristy5c4e2582011-09-11 19:21:03 +00004000 picture_image->columns,method,exception);
cristy3ed852e2009-09-05 21:47:34 +00004001 picture_image=DestroyImage(picture_image);
4002 if (bend_image == (Image *) NULL)
4003 return((Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00004004 picture_image=bend_image;
4005 rotate_image=RotateImage(picture_image,-90.0,exception);
4006 picture_image=DestroyImage(picture_image);
4007 if (rotate_image == (Image *) NULL)
4008 return((Image *) NULL);
4009 picture_image=rotate_image;
4010 picture_image->background_color=image->background_color;
4011 polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
4012 exception);
4013 if (polaroid_image == (Image *) NULL)
4014 {
4015 picture_image=DestroyImage(picture_image);
4016 return(picture_image);
4017 }
4018 flop_image=FlopImage(polaroid_image,exception);
4019 polaroid_image=DestroyImage(polaroid_image);
4020 if (flop_image == (Image *) NULL)
4021 {
4022 picture_image=DestroyImage(picture_image);
4023 return(picture_image);
4024 }
4025 polaroid_image=flop_image;
4026 (void) CompositeImage(polaroid_image,OverCompositeOp,picture_image,
cristye941a752011-10-15 01:52:48 +00004027 (ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
cristy3ed852e2009-09-05 21:47:34 +00004028 picture_image=DestroyImage(picture_image);
cristy9950d572011-10-01 18:22:35 +00004029 (void) QueryColorCompliance("none",AllCompliance,
4030 &polaroid_image->background_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00004031 rotate_image=RotateImage(polaroid_image,angle,exception);
4032 polaroid_image=DestroyImage(polaroid_image);
4033 if (rotate_image == (Image *) NULL)
4034 return((Image *) NULL);
4035 polaroid_image=rotate_image;
4036 trim_image=TrimImage(polaroid_image,exception);
4037 polaroid_image=DestroyImage(polaroid_image);
4038 if (trim_image == (Image *) NULL)
4039 return((Image *) NULL);
4040 polaroid_image=trim_image;
4041 return(polaroid_image);
4042}
4043
4044/*
4045%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4046% %
4047% %
4048% %
cristy3ed852e2009-09-05 21:47:34 +00004049% S e p i a T o n e I m a g e %
4050% %
4051% %
4052% %
4053%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4054%
4055% MagickSepiaToneImage() applies a special effect to the image, similar to the
4056% effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4057% 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4058% threshold of 80% is a good starting point for a reasonable tone.
4059%
4060% The format of the SepiaToneImage method is:
4061%
4062% Image *SepiaToneImage(const Image *image,const double threshold,
4063% ExceptionInfo *exception)
4064%
4065% A description of each parameter follows:
4066%
4067% o image: the image.
4068%
4069% o threshold: the tone threshold.
4070%
4071% o exception: return any errors or warnings in this structure.
4072%
4073*/
4074MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4075 ExceptionInfo *exception)
4076{
4077#define SepiaToneImageTag "SepiaTone/Image"
4078
cristyc4c8d132010-01-07 01:58:38 +00004079 CacheView
4080 *image_view,
4081 *sepia_view;
4082
cristy3ed852e2009-09-05 21:47:34 +00004083 Image
4084 *sepia_image;
4085
cristy3ed852e2009-09-05 21:47:34 +00004086 MagickBooleanType
4087 status;
4088
cristybb503372010-05-27 20:51:26 +00004089 MagickOffsetType
4090 progress;
4091
4092 ssize_t
4093 y;
4094
cristy3ed852e2009-09-05 21:47:34 +00004095 /*
4096 Initialize sepia-toned image attributes.
4097 */
4098 assert(image != (const Image *) NULL);
4099 assert(image->signature == MagickSignature);
4100 if (image->debug != MagickFalse)
4101 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4102 assert(exception != (ExceptionInfo *) NULL);
4103 assert(exception->signature == MagickSignature);
4104 sepia_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
4105 if (sepia_image == (Image *) NULL)
4106 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004107 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004108 {
cristy3ed852e2009-09-05 21:47:34 +00004109 sepia_image=DestroyImage(sepia_image);
4110 return((Image *) NULL);
4111 }
4112 /*
4113 Tone each row of the image.
4114 */
4115 status=MagickTrue;
4116 progress=0;
4117 image_view=AcquireCacheView(image);
4118 sepia_view=AcquireCacheView(sepia_image);
cristyb5d5f722009-11-04 03:03:49 +00004119#if defined(MAGICKCORE_OPENMP_SUPPORT)
4120 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004121#endif
cristybb503372010-05-27 20:51:26 +00004122 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004123 {
cristy4c08aed2011-07-01 19:47:50 +00004124 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004125 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004126
cristybb503372010-05-27 20:51:26 +00004127 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004128 x;
4129
cristy4c08aed2011-07-01 19:47:50 +00004130 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004131 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004132
4133 if (status == MagickFalse)
4134 continue;
4135 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
4136 q=QueueCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
4137 exception);
cristy4c08aed2011-07-01 19:47:50 +00004138 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004139 {
4140 status=MagickFalse;
4141 continue;
4142 }
cristybb503372010-05-27 20:51:26 +00004143 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004144 {
4145 MagickRealType
4146 intensity,
4147 tone;
4148
cristy4c08aed2011-07-01 19:47:50 +00004149 intensity=(MagickRealType) GetPixelIntensity(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004150 tone=intensity > threshold ? (MagickRealType) QuantumRange : intensity+
4151 (MagickRealType) QuantumRange-threshold;
cristy4c08aed2011-07-01 19:47:50 +00004152 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004153 tone=intensity > (7.0*threshold/6.0) ? (MagickRealType) QuantumRange :
4154 intensity+(MagickRealType) QuantumRange-7.0*threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004155 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004156 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004157 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004158 tone=threshold/7.0;
cristy4c08aed2011-07-01 19:47:50 +00004159 if ((MagickRealType) GetPixelGreen(image,q) < tone)
4160 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4161 if ((MagickRealType) GetPixelBlue(image,q) < tone)
4162 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristyed231572011-07-14 02:18:59 +00004163 p+=GetPixelChannels(image);
4164 q+=GetPixelChannels(sepia_image);
cristy3ed852e2009-09-05 21:47:34 +00004165 }
4166 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4167 status=MagickFalse;
4168 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4169 {
4170 MagickBooleanType
4171 proceed;
4172
cristyb5d5f722009-11-04 03:03:49 +00004173#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00004174 #pragma omp critical (MagickCore_SepiaToneImage)
4175#endif
4176 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4177 image->rows);
4178 if (proceed == MagickFalse)
4179 status=MagickFalse;
4180 }
4181 }
4182 sepia_view=DestroyCacheView(sepia_view);
4183 image_view=DestroyCacheView(image_view);
cristye23ec9d2011-08-16 18:15:40 +00004184 (void) NormalizeImage(sepia_image,exception);
4185 (void) ContrastImage(sepia_image,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004186 if (status == MagickFalse)
4187 sepia_image=DestroyImage(sepia_image);
4188 return(sepia_image);
4189}
4190
4191/*
4192%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4193% %
4194% %
4195% %
4196% S h a d o w I m a g e %
4197% %
4198% %
4199% %
4200%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4201%
4202% ShadowImage() simulates a shadow from the specified image and returns it.
4203%
4204% The format of the ShadowImage method is:
4205%
4206% Image *ShadowImage(const Image *image,const double opacity,
cristybb503372010-05-27 20:51:26 +00004207% const double sigma,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004208% ExceptionInfo *exception)
4209%
4210% A description of each parameter follows:
4211%
4212% o image: the image.
4213%
4214% o opacity: percentage transparency.
4215%
4216% o sigma: the standard deviation of the Gaussian, in pixels.
4217%
4218% o x_offset: the shadow x-offset.
4219%
4220% o y_offset: the shadow y-offset.
4221%
4222% o exception: return any errors or warnings in this structure.
4223%
4224*/
4225MagickExport Image *ShadowImage(const Image *image,const double opacity,
cristybb503372010-05-27 20:51:26 +00004226 const double sigma,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004227 ExceptionInfo *exception)
4228{
4229#define ShadowImageTag "Shadow/Image"
4230
cristybd5a96c2011-08-21 00:04:26 +00004231 ChannelType
4232 channel_mask;
4233
cristy3ed852e2009-09-05 21:47:34 +00004234 Image
4235 *border_image,
4236 *clone_image,
4237 *shadow_image;
4238
cristy3ed852e2009-09-05 21:47:34 +00004239 RectangleInfo
4240 border_info;
4241
cristy3ed852e2009-09-05 21:47:34 +00004242 assert(image != (Image *) NULL);
4243 assert(image->signature == MagickSignature);
4244 if (image->debug != MagickFalse)
4245 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4246 assert(exception != (ExceptionInfo *) NULL);
4247 assert(exception->signature == MagickSignature);
4248 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4249 if (clone_image == (Image *) NULL)
4250 return((Image *) NULL);
4251 (void) SetImageVirtualPixelMethod(clone_image,EdgeVirtualPixelMethod);
4252 clone_image->compose=OverCompositeOp;
cristybb503372010-05-27 20:51:26 +00004253 border_info.width=(size_t) floor(2.0*sigma+0.5);
4254 border_info.height=(size_t) floor(2.0*sigma+0.5);
cristy3ed852e2009-09-05 21:47:34 +00004255 border_info.x=0;
4256 border_info.y=0;
cristy9950d572011-10-01 18:22:35 +00004257 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4258 exception);
cristy633f0c62011-09-15 13:27:36 +00004259 border_image=BorderImage(clone_image,&border_info,image->compose,exception);
cristy3ed852e2009-09-05 21:47:34 +00004260 clone_image=DestroyImage(clone_image);
4261 if (border_image == (Image *) NULL)
4262 return((Image *) NULL);
4263 if (border_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00004264 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004265 /*
4266 Shadow image.
4267 */
cristyea1a8aa2011-10-20 13:24:06 +00004268 (void) SetImageBackgroundColor(border_image,exception);
cristybd5a96c2011-08-21 00:04:26 +00004269 channel_mask=SetPixelChannelMask(border_image,AlphaChannel);
cristy05c0c9a2011-09-05 23:16:13 +00004270 shadow_image=BlurImage(border_image,0.0,sigma,image->bias,exception);
cristybd5a96c2011-08-21 00:04:26 +00004271 (void) SetPixelChannelMap(border_image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00004272 border_image=DestroyImage(border_image);
4273 if (shadow_image == (Image *) NULL)
4274 return((Image *) NULL);
4275 if (shadow_image->page.width == 0)
4276 shadow_image->page.width=shadow_image->columns;
4277 if (shadow_image->page.height == 0)
4278 shadow_image->page.height=shadow_image->rows;
cristybb503372010-05-27 20:51:26 +00004279 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4280 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4281 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4282 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
cristy3ed852e2009-09-05 21:47:34 +00004283 return(shadow_image);
4284}
4285
4286/*
4287%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4288% %
4289% %
4290% %
4291% S k e t c h I m a g e %
4292% %
4293% %
4294% %
4295%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4296%
4297% SketchImage() simulates a pencil sketch. We convolve the image with a
4298% Gaussian operator of the given radius and standard deviation (sigma). For
4299% reasonable results, radius should be larger than sigma. Use a radius of 0
4300% and SketchImage() selects a suitable radius for you. Angle gives the angle
4301% of the sketch.
4302%
4303% The format of the SketchImage method is:
4304%
4305% Image *SketchImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00004306% const double sigma,const double angle,const double bias,
4307% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004308%
4309% A description of each parameter follows:
4310%
4311% o image: the image.
4312%
cristy574cc262011-08-05 01:23:58 +00004313% o radius: the radius of the Gaussian, in pixels, not counting the
4314% center pixel.
cristy3ed852e2009-09-05 21:47:34 +00004315%
4316% o sigma: the standard deviation of the Gaussian, in pixels.
4317%
cristyf7ef0252011-09-09 14:50:06 +00004318% o angle: apply the effect along this angle.
4319%
4320% o bias: the bias.
cristy3ed852e2009-09-05 21:47:34 +00004321%
4322% o exception: return any errors or warnings in this structure.
4323%
4324*/
4325MagickExport Image *SketchImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00004326 const double sigma,const double angle,const double bias,
4327 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004328{
cristyfa112112010-01-04 17:48:07 +00004329 CacheView
4330 *random_view;
4331
cristy3ed852e2009-09-05 21:47:34 +00004332 Image
4333 *blend_image,
4334 *blur_image,
4335 *dodge_image,
4336 *random_image,
4337 *sketch_image;
4338
cristy3ed852e2009-09-05 21:47:34 +00004339 MagickBooleanType
4340 status;
4341
cristy3ed852e2009-09-05 21:47:34 +00004342 RandomInfo
cristyfa112112010-01-04 17:48:07 +00004343 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00004344
cristy9d314ff2011-03-09 01:30:28 +00004345 ssize_t
4346 y;
4347
cristy3ed852e2009-09-05 21:47:34 +00004348 /*
4349 Sketch image.
4350 */
4351 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4352 MagickTrue,exception);
4353 if (random_image == (Image *) NULL)
4354 return((Image *) NULL);
4355 status=MagickTrue;
cristy1b784432009-12-19 02:20:40 +00004356 random_info=AcquireRandomInfoThreadSet();
cristy3ed852e2009-09-05 21:47:34 +00004357 random_view=AcquireCacheView(random_image);
cristy1b784432009-12-19 02:20:40 +00004358#if defined(MAGICKCORE_OPENMP_SUPPORT)
4359 #pragma omp parallel for schedule(dynamic,4) shared(status)
4360#endif
cristybb503372010-05-27 20:51:26 +00004361 for (y=0; y < (ssize_t) random_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004362 {
cristy5c9e6f22010-09-17 17:31:01 +00004363 const int
4364 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00004365
cristybb503372010-05-27 20:51:26 +00004366 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004367 x;
4368
cristy4c08aed2011-07-01 19:47:50 +00004369 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004370 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004371
cristy1b784432009-12-19 02:20:40 +00004372 if (status == MagickFalse)
4373 continue;
cristy3ed852e2009-09-05 21:47:34 +00004374 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4375 exception);
cristyacd2ed22011-08-30 01:44:23 +00004376 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004377 {
4378 status=MagickFalse;
4379 continue;
4380 }
cristybb503372010-05-27 20:51:26 +00004381 for (x=0; x < (ssize_t) random_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004382 {
cristy76f512e2011-09-12 01:26:56 +00004383 MagickRealType
4384 value;
4385
4386 register ssize_t
4387 i;
4388
4389 value=GetPseudoRandomValue(random_info[id]);
4390 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4391 {
4392 PixelTrait
4393 traits;
4394
4395 traits=GetPixelChannelMapTraits(random_image,(PixelChannel) i);
4396 if (traits == UndefinedPixelTrait)
4397 continue;
4398 q[i]=ClampToQuantum(QuantumRange*value);
4399 }
cristyed231572011-07-14 02:18:59 +00004400 q+=GetPixelChannels(random_image);
cristy3ed852e2009-09-05 21:47:34 +00004401 }
4402 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4403 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004404 }
4405 random_view=DestroyCacheView(random_view);
cristy1b784432009-12-19 02:20:40 +00004406 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00004407 if (status == MagickFalse)
4408 {
4409 random_image=DestroyImage(random_image);
4410 return(random_image);
4411 }
cristyf7ef0252011-09-09 14:50:06 +00004412 blur_image=MotionBlurImage(random_image,radius,sigma,angle,bias,exception);
cristy3ed852e2009-09-05 21:47:34 +00004413 random_image=DestroyImage(random_image);
4414 if (blur_image == (Image *) NULL)
4415 return((Image *) NULL);
cristy8ae632d2011-09-05 17:29:53 +00004416 dodge_image=EdgeImage(blur_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +00004417 blur_image=DestroyImage(blur_image);
4418 if (dodge_image == (Image *) NULL)
4419 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +00004420 (void) NormalizeImage(dodge_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +00004421 (void) NegateImage(dodge_image,MagickFalse,exception);
cristye941a752011-10-15 01:52:48 +00004422 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
cristy3ed852e2009-09-05 21:47:34 +00004423 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4424 if (sketch_image == (Image *) NULL)
4425 {
4426 dodge_image=DestroyImage(dodge_image);
4427 return((Image *) NULL);
4428 }
cristye941a752011-10-15 01:52:48 +00004429 (void) CompositeImage(sketch_image,ColorDodgeCompositeOp,dodge_image,0,0,
4430 exception);
cristy3ed852e2009-09-05 21:47:34 +00004431 dodge_image=DestroyImage(dodge_image);
4432 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4433 if (blend_image == (Image *) NULL)
4434 {
4435 sketch_image=DestroyImage(sketch_image);
4436 return((Image *) NULL);
4437 }
4438 (void) SetImageArtifact(blend_image,"compose:args","20x80");
cristye941a752011-10-15 01:52:48 +00004439 (void) CompositeImage(sketch_image,BlendCompositeOp,blend_image,0,0,
4440 exception);
cristy3ed852e2009-09-05 21:47:34 +00004441 blend_image=DestroyImage(blend_image);
4442 return(sketch_image);
4443}
4444
4445/*
4446%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4447% %
4448% %
4449% %
4450% S o l a r i z e I m a g e %
4451% %
4452% %
4453% %
4454%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4455%
4456% SolarizeImage() applies a special effect to the image, similar to the effect
4457% achieved in a photo darkroom by selectively exposing areas of photo
4458% sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4459% measure of the extent of the solarization.
4460%
4461% The format of the SolarizeImage method is:
4462%
cristy5cbc0162011-08-29 00:36:28 +00004463% MagickBooleanType SolarizeImage(Image *image,const double threshold,
4464% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004465%
4466% A description of each parameter follows:
4467%
4468% o image: the image.
4469%
4470% o threshold: Define the extent of the solarization.
4471%
cristy5cbc0162011-08-29 00:36:28 +00004472% o exception: return any errors or warnings in this structure.
4473%
cristy3ed852e2009-09-05 21:47:34 +00004474*/
4475MagickExport MagickBooleanType SolarizeImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00004476 const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004477{
4478#define SolarizeImageTag "Solarize/Image"
4479
cristyc4c8d132010-01-07 01:58:38 +00004480 CacheView
4481 *image_view;
4482
cristy3ed852e2009-09-05 21:47:34 +00004483 MagickBooleanType
4484 status;
4485
cristybb503372010-05-27 20:51:26 +00004486 MagickOffsetType
4487 progress;
4488
4489 ssize_t
4490 y;
4491
cristy3ed852e2009-09-05 21:47:34 +00004492 assert(image != (Image *) NULL);
4493 assert(image->signature == MagickSignature);
4494 if (image->debug != MagickFalse)
4495 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4496 if (image->storage_class == PseudoClass)
4497 {
cristybb503372010-05-27 20:51:26 +00004498 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004499 i;
4500
4501 /*
4502 Solarize colormap.
4503 */
cristybb503372010-05-27 20:51:26 +00004504 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00004505 {
4506 if ((MagickRealType) image->colormap[i].red > threshold)
4507 image->colormap[i].red=(Quantum) QuantumRange-image->colormap[i].red;
4508 if ((MagickRealType) image->colormap[i].green > threshold)
4509 image->colormap[i].green=(Quantum) QuantumRange-
4510 image->colormap[i].green;
4511 if ((MagickRealType) image->colormap[i].blue > threshold)
4512 image->colormap[i].blue=(Quantum) QuantumRange-
4513 image->colormap[i].blue;
4514 }
4515 }
4516 /*
4517 Solarize image.
4518 */
4519 status=MagickTrue;
4520 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00004521 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00004522#if defined(MAGICKCORE_OPENMP_SUPPORT)
4523 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004524#endif
cristybb503372010-05-27 20:51:26 +00004525 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004526 {
cristybb503372010-05-27 20:51:26 +00004527 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004528 x;
4529
cristy4c08aed2011-07-01 19:47:50 +00004530 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004531 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004532
4533 if (status == MagickFalse)
4534 continue;
cristy5cbc0162011-08-29 00:36:28 +00004535 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00004536 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004537 {
4538 status=MagickFalse;
4539 continue;
4540 }
cristybb503372010-05-27 20:51:26 +00004541 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004542 {
cristy76f512e2011-09-12 01:26:56 +00004543 register ssize_t
4544 i;
4545
4546 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4547 {
4548 PixelTrait
4549 traits;
4550
4551 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
4552 if ((traits == UndefinedPixelTrait) ||
4553 ((traits & CopyPixelTrait) != 0))
4554 continue;
4555 if ((MagickRealType) q[i] > threshold)
4556 q[i]=QuantumRange-q[i];
4557 }
cristyed231572011-07-14 02:18:59 +00004558 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004559 }
4560 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4561 status=MagickFalse;
4562 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4563 {
4564 MagickBooleanType
4565 proceed;
4566
cristyb5d5f722009-11-04 03:03:49 +00004567#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00004568 #pragma omp critical (MagickCore_SolarizeImage)
4569#endif
4570 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4571 if (proceed == MagickFalse)
4572 status=MagickFalse;
4573 }
4574 }
4575 image_view=DestroyCacheView(image_view);
4576 return(status);
4577}
4578
4579/*
4580%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4581% %
4582% %
4583% %
4584% S t e g a n o I m a g e %
4585% %
4586% %
4587% %
4588%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4589%
4590% SteganoImage() hides a digital watermark within the image. Recover
4591% the hidden watermark later to prove that the authenticity of an image.
4592% Offset defines the start position within the image to hide the watermark.
4593%
4594% The format of the SteganoImage method is:
4595%
4596% Image *SteganoImage(const Image *image,Image *watermark,
4597% ExceptionInfo *exception)
4598%
4599% A description of each parameter follows:
4600%
4601% o image: the image.
4602%
4603% o watermark: the watermark image.
4604%
4605% o exception: return any errors or warnings in this structure.
4606%
4607*/
4608MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4609 ExceptionInfo *exception)
4610{
cristye1bf8ad2010-09-19 17:07:03 +00004611#define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
cristy4c08aed2011-07-01 19:47:50 +00004612#define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
cristyeaedf062010-05-29 22:36:02 +00004613 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
cristy3ed852e2009-09-05 21:47:34 +00004614#define SteganoImageTag "Stegano/Image"
4615
cristyb0d3bb92010-09-22 14:37:58 +00004616 CacheView
4617 *stegano_view,
4618 *watermark_view;
4619
cristy3ed852e2009-09-05 21:47:34 +00004620 Image
4621 *stegano_image;
4622
4623 int
4624 c;
4625
cristy3ed852e2009-09-05 21:47:34 +00004626 MagickBooleanType
4627 status;
4628
cristy101ab702011-10-13 13:06:32 +00004629 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004630 pixel;
4631
cristy4c08aed2011-07-01 19:47:50 +00004632 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004633 *q;
4634
cristye1bf8ad2010-09-19 17:07:03 +00004635 register ssize_t
4636 x;
4637
cristybb503372010-05-27 20:51:26 +00004638 size_t
cristyeaedf062010-05-29 22:36:02 +00004639 depth,
4640 one;
cristy3ed852e2009-09-05 21:47:34 +00004641
cristye1bf8ad2010-09-19 17:07:03 +00004642 ssize_t
4643 i,
4644 j,
4645 k,
4646 y;
4647
cristy3ed852e2009-09-05 21:47:34 +00004648 /*
4649 Initialize steganographic image attributes.
4650 */
4651 assert(image != (const Image *) NULL);
4652 assert(image->signature == MagickSignature);
4653 if (image->debug != MagickFalse)
4654 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4655 assert(watermark != (const Image *) NULL);
4656 assert(watermark->signature == MagickSignature);
4657 assert(exception != (ExceptionInfo *) NULL);
4658 assert(exception->signature == MagickSignature);
cristyeaedf062010-05-29 22:36:02 +00004659 one=1UL;
cristy3ed852e2009-09-05 21:47:34 +00004660 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4661 if (stegano_image == (Image *) NULL)
4662 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004663 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004664 {
cristy3ed852e2009-09-05 21:47:34 +00004665 stegano_image=DestroyImage(stegano_image);
4666 return((Image *) NULL);
4667 }
4668 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
4669 /*
4670 Hide watermark in low-order bits of image.
4671 */
4672 c=0;
4673 i=0;
4674 j=0;
4675 depth=stegano_image->depth;
4676 k=image->offset;
cristyda16f162011-02-19 23:52:17 +00004677 status=MagickTrue;
cristyb0d3bb92010-09-22 14:37:58 +00004678 watermark_view=AcquireCacheView(watermark);
4679 stegano_view=AcquireCacheView(stegano_image);
cristybb503372010-05-27 20:51:26 +00004680 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
cristy3ed852e2009-09-05 21:47:34 +00004681 {
cristybb503372010-05-27 20:51:26 +00004682 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
cristy3ed852e2009-09-05 21:47:34 +00004683 {
cristybb503372010-05-27 20:51:26 +00004684 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
cristy3ed852e2009-09-05 21:47:34 +00004685 {
cristyda1f9c12011-10-02 21:39:49 +00004686 Quantum
cristy5f95f4f2011-10-23 01:01:01 +00004687 virtual_pixel[CompositePixelChannel];
cristyda1f9c12011-10-02 21:39:49 +00004688
4689 (void) GetOneCacheViewVirtualPixel(watermark_view,x,y,virtual_pixel,
4690 exception);
4691 pixel.red=(double) virtual_pixel[RedPixelChannel];
4692 pixel.green=(double) virtual_pixel[GreenPixelChannel];
4693 pixel.blue=(double) virtual_pixel[BluePixelChannel];
4694 pixel.alpha=(double) virtual_pixel[AlphaPixelChannel];
cristybb503372010-05-27 20:51:26 +00004695 if ((k/(ssize_t) stegano_image->columns) >= (ssize_t) stegano_image->rows)
cristy3ed852e2009-09-05 21:47:34 +00004696 break;
cristyb0d3bb92010-09-22 14:37:58 +00004697 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4698 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4699 exception);
cristyacd2ed22011-08-30 01:44:23 +00004700 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004701 break;
4702 switch (c)
4703 {
4704 case 0:
4705 {
cristy4c08aed2011-07-01 19:47:50 +00004706 SetPixelRed(image,SetBit(GetPixelRed(image,q),j,GetBit(
cristy101ab702011-10-13 13:06:32 +00004707 GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004708 break;
4709 }
4710 case 1:
4711 {
cristy4c08aed2011-07-01 19:47:50 +00004712 SetPixelGreen(image,SetBit(GetPixelGreen(image,q),j,GetBit(
cristy101ab702011-10-13 13:06:32 +00004713 GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004714 break;
4715 }
4716 case 2:
4717 {
cristy4c08aed2011-07-01 19:47:50 +00004718 SetPixelBlue(image,SetBit(GetPixelBlue(image,q),j,GetBit(
cristy101ab702011-10-13 13:06:32 +00004719 GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004720 break;
4721 }
4722 }
cristyb0d3bb92010-09-22 14:37:58 +00004723 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004724 break;
4725 c++;
4726 if (c == 3)
4727 c=0;
4728 k++;
cristybb503372010-05-27 20:51:26 +00004729 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
cristy3ed852e2009-09-05 21:47:34 +00004730 k=0;
4731 if (k == image->offset)
4732 j++;
4733 }
4734 }
cristy8b27a6d2010-02-14 03:31:15 +00004735 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004736 {
cristy8b27a6d2010-02-14 03:31:15 +00004737 MagickBooleanType
4738 proceed;
4739
4740 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4741 (depth-i),depth);
4742 if (proceed == MagickFalse)
4743 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004744 }
4745 }
cristyb0d3bb92010-09-22 14:37:58 +00004746 stegano_view=DestroyCacheView(stegano_view);
4747 watermark_view=DestroyCacheView(watermark_view);
cristy3ed852e2009-09-05 21:47:34 +00004748 if (stegano_image->storage_class == PseudoClass)
cristyea1a8aa2011-10-20 13:24:06 +00004749 (void) SyncImage(stegano_image,exception);
cristyda16f162011-02-19 23:52:17 +00004750 if (status == MagickFalse)
4751 {
4752 stegano_image=DestroyImage(stegano_image);
4753 return((Image *) NULL);
4754 }
cristy3ed852e2009-09-05 21:47:34 +00004755 return(stegano_image);
4756}
4757
4758/*
4759%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4760% %
4761% %
4762% %
4763% S t e r e o A n a g l y p h I m a g e %
4764% %
4765% %
4766% %
4767%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4768%
4769% StereoAnaglyphImage() combines two images and produces a single image that
4770% is the composite of a left and right image of a stereo pair. Special
4771% red-green stereo glasses are required to view this effect.
4772%
4773% The format of the StereoAnaglyphImage method is:
4774%
4775% Image *StereoImage(const Image *left_image,const Image *right_image,
4776% ExceptionInfo *exception)
4777% Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004778% const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004779% ExceptionInfo *exception)
4780%
4781% A description of each parameter follows:
4782%
4783% o left_image: the left image.
4784%
4785% o right_image: the right image.
4786%
4787% o exception: return any errors or warnings in this structure.
4788%
4789% o x_offset: amount, in pixels, by which the left image is offset to the
4790% right of the right image.
4791%
4792% o y_offset: amount, in pixels, by which the left image is offset to the
4793% bottom of the right image.
4794%
4795%
4796*/
4797MagickExport Image *StereoImage(const Image *left_image,
4798 const Image *right_image,ExceptionInfo *exception)
4799{
4800 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4801}
4802
4803MagickExport Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004804 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004805 ExceptionInfo *exception)
4806{
4807#define StereoImageTag "Stereo/Image"
4808
4809 const Image
4810 *image;
4811
4812 Image
4813 *stereo_image;
4814
cristy3ed852e2009-09-05 21:47:34 +00004815 MagickBooleanType
4816 status;
4817
cristy9d314ff2011-03-09 01:30:28 +00004818 ssize_t
4819 y;
4820
cristy3ed852e2009-09-05 21:47:34 +00004821 assert(left_image != (const Image *) NULL);
4822 assert(left_image->signature == MagickSignature);
4823 if (left_image->debug != MagickFalse)
4824 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4825 left_image->filename);
4826 assert(right_image != (const Image *) NULL);
4827 assert(right_image->signature == MagickSignature);
4828 assert(exception != (ExceptionInfo *) NULL);
4829 assert(exception->signature == MagickSignature);
4830 assert(right_image != (const Image *) NULL);
4831 image=left_image;
4832 if ((left_image->columns != right_image->columns) ||
4833 (left_image->rows != right_image->rows))
4834 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4835 /*
4836 Initialize stereo image attributes.
4837 */
4838 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4839 MagickTrue,exception);
4840 if (stereo_image == (Image *) NULL)
4841 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004842 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004843 {
cristy3ed852e2009-09-05 21:47:34 +00004844 stereo_image=DestroyImage(stereo_image);
4845 return((Image *) NULL);
4846 }
4847 /*
4848 Copy left image to red channel and right image to blue channel.
4849 */
cristyda16f162011-02-19 23:52:17 +00004850 status=MagickTrue;
cristybb503372010-05-27 20:51:26 +00004851 for (y=0; y < (ssize_t) stereo_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004852 {
cristy4c08aed2011-07-01 19:47:50 +00004853 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004854 *restrict p,
4855 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004856
cristybb503372010-05-27 20:51:26 +00004857 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004858 x;
4859
cristy4c08aed2011-07-01 19:47:50 +00004860 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004861 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +00004862
4863 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4864 exception);
4865 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4866 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
cristy76f512e2011-09-12 01:26:56 +00004867 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4868 (r == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004869 break;
cristybb503372010-05-27 20:51:26 +00004870 for (x=0; x < (ssize_t) stereo_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004871 {
cristy4c08aed2011-07-01 19:47:50 +00004872 SetPixelRed(image,GetPixelRed(left_image,p),r);
cristy76f512e2011-09-12 01:26:56 +00004873 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4874 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4875 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4876 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4877 GetPixelAlpha(right_image,q))/2,r);
cristyed231572011-07-14 02:18:59 +00004878 p+=GetPixelChannels(left_image);
cristy76f512e2011-09-12 01:26:56 +00004879 q+=GetPixelChannels(right_image);
4880 r+=GetPixelChannels(stereo_image);
cristy3ed852e2009-09-05 21:47:34 +00004881 }
4882 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4883 break;
cristy8b27a6d2010-02-14 03:31:15 +00004884 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004885 {
cristy8b27a6d2010-02-14 03:31:15 +00004886 MagickBooleanType
4887 proceed;
4888
cristybb503372010-05-27 20:51:26 +00004889 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4890 stereo_image->rows);
cristy8b27a6d2010-02-14 03:31:15 +00004891 if (proceed == MagickFalse)
4892 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004893 }
4894 }
cristyda16f162011-02-19 23:52:17 +00004895 if (status == MagickFalse)
4896 {
4897 stereo_image=DestroyImage(stereo_image);
4898 return((Image *) NULL);
4899 }
cristy3ed852e2009-09-05 21:47:34 +00004900 return(stereo_image);
4901}
4902
4903/*
4904%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4905% %
4906% %
4907% %
4908% S w i r l I m a g e %
4909% %
4910% %
4911% %
4912%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4913%
4914% SwirlImage() swirls the pixels about the center of the image, where
4915% degrees indicates the sweep of the arc through which each pixel is moved.
4916% You get a more dramatic effect as the degrees move from 1 to 360.
4917%
4918% The format of the SwirlImage method is:
4919%
4920% Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00004921% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004922%
4923% A description of each parameter follows:
4924%
4925% o image: the image.
4926%
4927% o degrees: Define the tightness of the swirling effect.
4928%
cristy76f512e2011-09-12 01:26:56 +00004929% o method: the pixel interpolation method.
4930%
cristy3ed852e2009-09-05 21:47:34 +00004931% o exception: return any errors or warnings in this structure.
4932%
4933*/
4934MagickExport Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00004935 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004936{
4937#define SwirlImageTag "Swirl/Image"
4938
cristyfa112112010-01-04 17:48:07 +00004939 CacheView
4940 *image_view,
4941 *swirl_view;
4942
cristy3ed852e2009-09-05 21:47:34 +00004943 Image
4944 *swirl_image;
4945
cristy3ed852e2009-09-05 21:47:34 +00004946 MagickBooleanType
4947 status;
4948
cristybb503372010-05-27 20:51:26 +00004949 MagickOffsetType
4950 progress;
4951
cristy3ed852e2009-09-05 21:47:34 +00004952 MagickRealType
4953 radius;
4954
4955 PointInfo
4956 center,
4957 scale;
4958
cristybb503372010-05-27 20:51:26 +00004959 ssize_t
4960 y;
4961
cristy3ed852e2009-09-05 21:47:34 +00004962 /*
4963 Initialize swirl image attributes.
4964 */
4965 assert(image != (const Image *) NULL);
4966 assert(image->signature == MagickSignature);
4967 if (image->debug != MagickFalse)
4968 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4969 assert(exception != (ExceptionInfo *) NULL);
4970 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00004971 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004972 if (swirl_image == (Image *) NULL)
4973 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004974 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004975 {
cristy3ed852e2009-09-05 21:47:34 +00004976 swirl_image=DestroyImage(swirl_image);
4977 return((Image *) NULL);
4978 }
cristy4c08aed2011-07-01 19:47:50 +00004979 if (swirl_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00004980 swirl_image->matte=MagickTrue;
4981 /*
4982 Compute scaling factor.
4983 */
4984 center.x=(double) image->columns/2.0;
4985 center.y=(double) image->rows/2.0;
4986 radius=MagickMax(center.x,center.y);
4987 scale.x=1.0;
4988 scale.y=1.0;
4989 if (image->columns > image->rows)
4990 scale.y=(double) image->columns/(double) image->rows;
4991 else
4992 if (image->columns < image->rows)
4993 scale.x=(double) image->rows/(double) image->columns;
4994 degrees=(double) DegreesToRadians(degrees);
4995 /*
4996 Swirl image.
4997 */
4998 status=MagickTrue;
4999 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00005000 image_view=AcquireCacheView(image);
5001 swirl_view=AcquireCacheView(swirl_image);
cristyb5d5f722009-11-04 03:03:49 +00005002#if defined(MAGICKCORE_OPENMP_SUPPORT)
5003 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005004#endif
cristybb503372010-05-27 20:51:26 +00005005 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005006 {
cristy3ed852e2009-09-05 21:47:34 +00005007 MagickRealType
5008 distance;
5009
5010 PointInfo
5011 delta;
5012
cristy6d188022011-09-12 13:23:33 +00005013 register const Quantum
5014 *restrict p;
5015
cristybb503372010-05-27 20:51:26 +00005016 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005017 x;
5018
cristy4c08aed2011-07-01 19:47:50 +00005019 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005020 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005021
5022 if (status == MagickFalse)
5023 continue;
cristy6d188022011-09-12 13:23:33 +00005024 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy3ed852e2009-09-05 21:47:34 +00005025 q=GetCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
5026 exception);
cristy6d188022011-09-12 13:23:33 +00005027 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005028 {
5029 status=MagickFalse;
5030 continue;
5031 }
cristy3ed852e2009-09-05 21:47:34 +00005032 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00005033 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005034 {
cristy6d188022011-09-12 13:23:33 +00005035 register ssize_t
5036 i;
5037
cristy3ed852e2009-09-05 21:47:34 +00005038 /*
5039 Determine if the pixel is within an ellipse.
5040 */
5041 delta.x=scale.x*(double) (x-center.x);
5042 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00005043 if (distance >= (radius*radius))
5044 {
5045 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5046 q[i]=p[i];
5047 }
5048 else
cristy3ed852e2009-09-05 21:47:34 +00005049 {
5050 MagickRealType
5051 cosine,
5052 factor,
5053 sine;
5054
5055 /*
5056 Swirl the pixel.
5057 */
5058 factor=1.0-sqrt((double) distance)/radius;
5059 sine=sin((double) (degrees*factor*factor));
5060 cosine=cos((double) (degrees*factor*factor));
cristy76f512e2011-09-12 01:26:56 +00005061 status=InterpolatePixelChannels(image,image_view,swirl_image,method,
5062 ((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5063 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00005064 }
cristy6d188022011-09-12 13:23:33 +00005065 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00005066 q+=GetPixelChannels(swirl_image);
cristy3ed852e2009-09-05 21:47:34 +00005067 }
5068 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5069 status=MagickFalse;
5070 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5071 {
5072 MagickBooleanType
5073 proceed;
5074
cristyb5d5f722009-11-04 03:03:49 +00005075#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005076 #pragma omp critical (MagickCore_SwirlImage)
5077#endif
5078 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5079 if (proceed == MagickFalse)
5080 status=MagickFalse;
5081 }
5082 }
5083 swirl_view=DestroyCacheView(swirl_view);
5084 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005085 if (status == MagickFalse)
5086 swirl_image=DestroyImage(swirl_image);
5087 return(swirl_image);
5088}
5089
5090/*
5091%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5092% %
5093% %
5094% %
5095% T i n t I m a g e %
5096% %
5097% %
5098% %
5099%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5100%
5101% TintImage() applies a color vector to each pixel in the image. The length
5102% of the vector is 0 for black and white and at its maximum for the midtones.
5103% The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5104%
5105% The format of the TintImage method is:
5106%
cristyb817c3f2011-10-03 14:00:35 +00005107% Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005108% const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005109%
5110% A description of each parameter follows:
5111%
5112% o image: the image.
5113%
cristyb817c3f2011-10-03 14:00:35 +00005114% o blend: A color value used for tinting.
cristy3ed852e2009-09-05 21:47:34 +00005115%
5116% o tint: A color value used for tinting.
5117%
5118% o exception: return any errors or warnings in this structure.
5119%
5120*/
cristyb817c3f2011-10-03 14:00:35 +00005121MagickExport Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005122 const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005123{
5124#define TintImageTag "Tint/Image"
5125
cristyc4c8d132010-01-07 01:58:38 +00005126 CacheView
5127 *image_view,
5128 *tint_view;
5129
cristy3ed852e2009-09-05 21:47:34 +00005130 GeometryInfo
5131 geometry_info;
5132
5133 Image
5134 *tint_image;
5135
cristy3ed852e2009-09-05 21:47:34 +00005136 MagickBooleanType
5137 status;
5138
cristybb503372010-05-27 20:51:26 +00005139 MagickOffsetType
5140 progress;
cristy3ed852e2009-09-05 21:47:34 +00005141
cristy28474bf2011-09-11 23:32:52 +00005142 MagickRealType
5143 intensity;
5144
cristy4c08aed2011-07-01 19:47:50 +00005145 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00005146 color_vector,
5147 pixel;
5148
cristybb503372010-05-27 20:51:26 +00005149 MagickStatusType
5150 flags;
5151
5152 ssize_t
5153 y;
5154
cristy3ed852e2009-09-05 21:47:34 +00005155 /*
5156 Allocate tint image.
5157 */
5158 assert(image != (const Image *) NULL);
5159 assert(image->signature == MagickSignature);
5160 if (image->debug != MagickFalse)
5161 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5162 assert(exception != (ExceptionInfo *) NULL);
5163 assert(exception->signature == MagickSignature);
5164 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5165 if (tint_image == (Image *) NULL)
5166 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005167 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005168 {
cristy3ed852e2009-09-05 21:47:34 +00005169 tint_image=DestroyImage(tint_image);
5170 return((Image *) NULL);
5171 }
cristyaed9c382011-10-03 17:54:21 +00005172 if (blend == (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005173 return(tint_image);
5174 /*
5175 Determine RGB values of the color.
5176 */
cristy28474bf2011-09-11 23:32:52 +00005177 GetPixelInfo(image,&pixel);
cristyb817c3f2011-10-03 14:00:35 +00005178 flags=ParseGeometry(blend,&geometry_info);
cristy3ed852e2009-09-05 21:47:34 +00005179 pixel.red=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005180 pixel.green=geometry_info.rho;
5181 pixel.blue=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005182 pixel.alpha=OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00005183 if ((flags & SigmaValue) != 0)
5184 pixel.green=geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +00005185 if ((flags & XiValue) != 0)
5186 pixel.blue=geometry_info.xi;
cristyb817c3f2011-10-03 14:00:35 +00005187 if ((flags & PsiValue) != 0)
5188 pixel.alpha=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005189 if (image->colorspace == CMYKColorspace)
5190 {
cristyb817c3f2011-10-03 14:00:35 +00005191 pixel.black=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005192 if ((flags & PsiValue) != 0)
5193 pixel.black=geometry_info.psi;
5194 if ((flags & ChiValue) != 0)
5195 pixel.alpha=geometry_info.chi;
5196 }
cristy28474bf2011-09-11 23:32:52 +00005197 intensity=(MagickRealType) GetPixelInfoIntensity(tint);
5198 color_vector.red=(MagickRealType) (pixel.red*tint->red/100.0-intensity);
5199 color_vector.green=(MagickRealType) (pixel.green*tint->green/100.0-intensity);
5200 color_vector.blue=(MagickRealType) (pixel.blue*tint->blue/100.0-intensity);
5201 color_vector.black=(MagickRealType) (pixel.black*tint->black/100.0-intensity);
5202 color_vector.alpha=(MagickRealType) (pixel.alpha*tint->alpha/100.0-intensity);
cristy3ed852e2009-09-05 21:47:34 +00005203 /*
5204 Tint image.
5205 */
5206 status=MagickTrue;
5207 progress=0;
5208 image_view=AcquireCacheView(image);
5209 tint_view=AcquireCacheView(tint_image);
cristyb5d5f722009-11-04 03:03:49 +00005210#if defined(MAGICKCORE_OPENMP_SUPPORT)
5211 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005212#endif
cristybb503372010-05-27 20:51:26 +00005213 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005214 {
cristy4c08aed2011-07-01 19:47:50 +00005215 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00005216 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00005217
cristy4c08aed2011-07-01 19:47:50 +00005218 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005219 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005220
cristy6b91acb2011-04-19 12:23:54 +00005221 register ssize_t
5222 x;
5223
cristy3ed852e2009-09-05 21:47:34 +00005224 if (status == MagickFalse)
5225 continue;
5226 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5227 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5228 exception);
cristy4c08aed2011-07-01 19:47:50 +00005229 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005230 {
5231 status=MagickFalse;
5232 continue;
5233 }
cristybb503372010-05-27 20:51:26 +00005234 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005235 {
cristy4c08aed2011-07-01 19:47:50 +00005236 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00005237 pixel;
5238
5239 MagickRealType
5240 weight;
5241
cristy76f512e2011-09-12 01:26:56 +00005242 if ((GetPixelRedTraits(tint_image) & UpdatePixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005243 {
5244 weight=QuantumScale*GetPixelRed(image,p)-0.5;
5245 pixel.red=(MagickRealType) GetPixelRed(image,p)+
5246 color_vector.red*(1.0-(4.0*(weight*weight)));
5247 SetPixelRed(tint_image,ClampToQuantum(pixel.red),q);
5248 }
cristy76f512e2011-09-12 01:26:56 +00005249 if ((GetPixelGreenTraits(tint_image) & UpdatePixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005250 {
5251 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
5252 pixel.green=(MagickRealType) GetPixelGreen(image,p)+
5253 color_vector.green*(1.0-(4.0*(weight*weight)));
5254 SetPixelGreen(tint_image,ClampToQuantum(pixel.green),q);
5255 }
cristy76f512e2011-09-12 01:26:56 +00005256 if ((GetPixelBlueTraits(tint_image) & UpdatePixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005257 {
5258 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
5259 pixel.blue=(MagickRealType) GetPixelBlue(image,p)+
5260 color_vector.blue*(1.0-(4.0*(weight*weight)));
5261 SetPixelBlue(tint_image,ClampToQuantum(pixel.blue),q);
5262 }
cristy76f512e2011-09-12 01:26:56 +00005263 if ((GetPixelBlackTraits(tint_image) & UpdatePixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005264 {
5265 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
5266 pixel.black=(MagickRealType) GetPixelBlack(image,p)+
5267 color_vector.black*(1.0-(4.0*(weight*weight)));
5268 SetPixelBlack(tint_image,ClampToQuantum(pixel.black),q);
5269 }
cristy76f512e2011-09-12 01:26:56 +00005270 if ((GetPixelAlphaTraits(tint_image) & CopyPixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005271 SetPixelAlpha(tint_image,GetPixelAlpha(image,p),q);
cristyed231572011-07-14 02:18:59 +00005272 p+=GetPixelChannels(image);
5273 q+=GetPixelChannels(tint_image);
cristy3ed852e2009-09-05 21:47:34 +00005274 }
5275 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5276 status=MagickFalse;
5277 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5278 {
5279 MagickBooleanType
5280 proceed;
5281
cristyb5d5f722009-11-04 03:03:49 +00005282#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005283 #pragma omp critical (MagickCore_TintImage)
5284#endif
5285 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5286 if (proceed == MagickFalse)
5287 status=MagickFalse;
5288 }
5289 }
5290 tint_view=DestroyCacheView(tint_view);
5291 image_view=DestroyCacheView(image_view);
5292 if (status == MagickFalse)
5293 tint_image=DestroyImage(tint_image);
5294 return(tint_image);
5295}
5296
5297/*
5298%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5299% %
5300% %
5301% %
5302% V i g n e t t e I m a g e %
5303% %
5304% %
5305% %
5306%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5307%
5308% VignetteImage() softens the edges of the image in vignette style.
5309%
5310% The format of the VignetteImage method is:
5311%
5312% Image *VignetteImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +00005313% const double sigma,const ssize_t x,const ssize_t y,
5314% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005315%
5316% A description of each parameter follows:
5317%
5318% o image: the image.
5319%
5320% o radius: the radius of the pixel neighborhood.
5321%
5322% o sigma: the standard deviation of the Gaussian, in pixels.
5323%
5324% o x, y: Define the x and y ellipse offset.
5325%
5326% o exception: return any errors or warnings in this structure.
5327%
5328*/
5329MagickExport Image *VignetteImage(const Image *image,const double radius,
cristybb503372010-05-27 20:51:26 +00005330 const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005331{
5332 char
5333 ellipse[MaxTextExtent];
5334
5335 DrawInfo
5336 *draw_info;
5337
5338 Image
5339 *canvas_image,
5340 *blur_image,
5341 *oval_image,
5342 *vignette_image;
5343
5344 assert(image != (Image *) NULL);
5345 assert(image->signature == MagickSignature);
5346 if (image->debug != MagickFalse)
5347 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5348 assert(exception != (ExceptionInfo *) NULL);
5349 assert(exception->signature == MagickSignature);
5350 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5351 if (canvas_image == (Image *) NULL)
5352 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005353 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005354 {
cristy3ed852e2009-09-05 21:47:34 +00005355 canvas_image=DestroyImage(canvas_image);
5356 return((Image *) NULL);
5357 }
5358 canvas_image->matte=MagickTrue;
cristyb3a73b52011-07-26 01:34:43 +00005359 oval_image=CloneImage(canvas_image,canvas_image->columns,
5360 canvas_image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005361 if (oval_image == (Image *) NULL)
5362 {
5363 canvas_image=DestroyImage(canvas_image);
5364 return((Image *) NULL);
5365 }
cristy9950d572011-10-01 18:22:35 +00005366 (void) QueryColorCompliance("#000000",AllCompliance,
5367 &oval_image->background_color,exception);
cristyea1a8aa2011-10-20 13:24:06 +00005368 (void) SetImageBackgroundColor(oval_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00005369 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
cristy9950d572011-10-01 18:22:35 +00005370 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5371 exception);
5372 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5373 exception);
cristyb51dff52011-05-19 16:55:47 +00005374 (void) FormatLocaleString(ellipse,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00005375 "ellipse %g,%g,%g,%g,0.0,360.0",image->columns/2.0,
cristy8cd5b312010-01-07 01:10:24 +00005376 image->rows/2.0,image->columns/2.0-x,image->rows/2.0-y);
cristy3ed852e2009-09-05 21:47:34 +00005377 draw_info->primitive=AcquireString(ellipse);
cristy018f07f2011-09-04 21:15:19 +00005378 (void) DrawImage(oval_image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00005379 draw_info=DestroyDrawInfo(draw_info);
cristy05c0c9a2011-09-05 23:16:13 +00005380 blur_image=BlurImage(oval_image,radius,sigma,image->bias,exception);
cristy3ed852e2009-09-05 21:47:34 +00005381 oval_image=DestroyImage(oval_image);
5382 if (blur_image == (Image *) NULL)
5383 {
5384 canvas_image=DestroyImage(canvas_image);
5385 return((Image *) NULL);
5386 }
5387 blur_image->matte=MagickFalse;
cristye941a752011-10-15 01:52:48 +00005388 (void) CompositeImage(canvas_image,CopyOpacityCompositeOp,blur_image,0,0,
5389 exception);
cristy3ed852e2009-09-05 21:47:34 +00005390 blur_image=DestroyImage(blur_image);
5391 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5392 canvas_image=DestroyImage(canvas_image);
5393 return(vignette_image);
5394}
5395
5396/*
5397%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5398% %
5399% %
5400% %
5401% W a v e I m a g e %
5402% %
5403% %
5404% %
5405%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5406%
5407% WaveImage() creates a "ripple" effect in the image by shifting the pixels
cristycee97112010-05-28 00:44:52 +00005408% vertically along a sine wave whose amplitude and wavelength is specified
cristy3ed852e2009-09-05 21:47:34 +00005409% by the given parameters.
5410%
5411% The format of the WaveImage method is:
5412%
5413% Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005414% const double wave_length,const PixelInterpolateMethod method,
5415% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005416%
5417% A description of each parameter follows:
5418%
5419% o image: the image.
5420%
5421% o amplitude, wave_length: Define the amplitude and wave length of the
5422% sine wave.
5423%
cristy5c4e2582011-09-11 19:21:03 +00005424% o interpolate: the pixel interpolation method.
5425%
cristy3ed852e2009-09-05 21:47:34 +00005426% o exception: return any errors or warnings in this structure.
5427%
5428*/
5429MagickExport Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005430 const double wave_length,const PixelInterpolateMethod method,
5431 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005432{
5433#define WaveImageTag "Wave/Image"
5434
cristyfa112112010-01-04 17:48:07 +00005435 CacheView
cristyd76c51e2011-03-26 00:21:26 +00005436 *image_view,
cristyfa112112010-01-04 17:48:07 +00005437 *wave_view;
5438
cristy3ed852e2009-09-05 21:47:34 +00005439 Image
5440 *wave_image;
5441
cristy3ed852e2009-09-05 21:47:34 +00005442 MagickBooleanType
5443 status;
5444
cristybb503372010-05-27 20:51:26 +00005445 MagickOffsetType
5446 progress;
5447
cristy3ed852e2009-09-05 21:47:34 +00005448 MagickRealType
5449 *sine_map;
5450
cristybb503372010-05-27 20:51:26 +00005451 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005452 i;
5453
cristybb503372010-05-27 20:51:26 +00005454 ssize_t
5455 y;
5456
cristy3ed852e2009-09-05 21:47:34 +00005457 /*
5458 Initialize wave image attributes.
5459 */
5460 assert(image != (Image *) NULL);
5461 assert(image->signature == MagickSignature);
5462 if (image->debug != MagickFalse)
5463 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5464 assert(exception != (ExceptionInfo *) NULL);
5465 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00005466 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
cristy3ed852e2009-09-05 21:47:34 +00005467 fabs(amplitude)),MagickTrue,exception);
5468 if (wave_image == (Image *) NULL)
5469 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005470 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005471 {
cristy3ed852e2009-09-05 21:47:34 +00005472 wave_image=DestroyImage(wave_image);
5473 return((Image *) NULL);
5474 }
cristy4c08aed2011-07-01 19:47:50 +00005475 if (wave_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00005476 wave_image->matte=MagickTrue;
5477 /*
5478 Allocate sine map.
5479 */
5480 sine_map=(MagickRealType *) AcquireQuantumMemory((size_t) wave_image->columns,
5481 sizeof(*sine_map));
5482 if (sine_map == (MagickRealType *) NULL)
5483 {
5484 wave_image=DestroyImage(wave_image);
5485 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5486 }
cristybb503372010-05-27 20:51:26 +00005487 for (i=0; i < (ssize_t) wave_image->columns; i++)
cristy4205a3c2010-09-12 20:19:59 +00005488 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5489 wave_length));
cristy3ed852e2009-09-05 21:47:34 +00005490 /*
5491 Wave image.
5492 */
5493 status=MagickTrue;
5494 progress=0;
cristyd76c51e2011-03-26 00:21:26 +00005495 image_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00005496 wave_view=AcquireCacheView(wave_image);
cristyd76c51e2011-03-26 00:21:26 +00005497 (void) SetCacheViewVirtualPixelMethod(image_view,
5498 BackgroundVirtualPixelMethod);
cristyb5d5f722009-11-04 03:03:49 +00005499#if defined(MAGICKCORE_OPENMP_SUPPORT)
5500 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005501#endif
cristybb503372010-05-27 20:51:26 +00005502 for (y=0; y < (ssize_t) wave_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005503 {
cristy4c08aed2011-07-01 19:47:50 +00005504 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005505 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005506
cristye97bb922011-04-03 01:36:52 +00005507 register ssize_t
5508 x;
5509
cristy3ed852e2009-09-05 21:47:34 +00005510 if (status == MagickFalse)
5511 continue;
5512 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5513 exception);
cristyacd2ed22011-08-30 01:44:23 +00005514 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005515 {
5516 status=MagickFalse;
5517 continue;
5518 }
cristybb503372010-05-27 20:51:26 +00005519 for (x=0; x < (ssize_t) wave_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005520 {
cristy5c4e2582011-09-11 19:21:03 +00005521 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5522 (double) x,(double) (y-sine_map[x]),q,exception);
cristyed231572011-07-14 02:18:59 +00005523 q+=GetPixelChannels(wave_image);
cristy3ed852e2009-09-05 21:47:34 +00005524 }
5525 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5526 status=MagickFalse;
5527 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5528 {
5529 MagickBooleanType
5530 proceed;
5531
cristyb5d5f722009-11-04 03:03:49 +00005532#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005533 #pragma omp critical (MagickCore_WaveImage)
5534#endif
5535 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5536 if (proceed == MagickFalse)
5537 status=MagickFalse;
5538 }
5539 }
5540 wave_view=DestroyCacheView(wave_view);
cristyd76c51e2011-03-26 00:21:26 +00005541 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005542 sine_map=(MagickRealType *) RelinquishMagickMemory(sine_map);
5543 if (status == MagickFalse)
5544 wave_image=DestroyImage(wave_image);
5545 return(wave_image);
5546}