blob: 670af0d28a2a24a7c0ab4520f2527407e059ac86 [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% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 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"
cristyc53413d2011-11-17 13:04:26 +000053#include "MagickCore/distort.h"
cristy4c08aed2011-07-01 19:47:50 +000054#include "MagickCore/draw.h"
55#include "MagickCore/effect.h"
56#include "MagickCore/enhance.h"
57#include "MagickCore/exception.h"
58#include "MagickCore/exception-private.h"
59#include "MagickCore/fx.h"
60#include "MagickCore/fx-private.h"
61#include "MagickCore/gem.h"
cristy8ea81222011-09-04 10:33:32 +000062#include "MagickCore/gem-private.h"
cristy4c08aed2011-07-01 19:47:50 +000063#include "MagickCore/geometry.h"
64#include "MagickCore/layer.h"
65#include "MagickCore/list.h"
66#include "MagickCore/log.h"
67#include "MagickCore/image.h"
68#include "MagickCore/image-private.h"
69#include "MagickCore/magick.h"
70#include "MagickCore/memory_.h"
71#include "MagickCore/monitor.h"
72#include "MagickCore/monitor-private.h"
73#include "MagickCore/option.h"
74#include "MagickCore/pixel.h"
75#include "MagickCore/pixel-accessor.h"
76#include "MagickCore/property.h"
77#include "MagickCore/quantum.h"
78#include "MagickCore/quantum-private.h"
79#include "MagickCore/random_.h"
80#include "MagickCore/random-private.h"
81#include "MagickCore/resample.h"
82#include "MagickCore/resample-private.h"
83#include "MagickCore/resize.h"
cristy4c08aed2011-07-01 19:47:50 +000084#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)
cristye6178502011-12-23 17:02:29 +0000304 #pragma omp parallel for schedule(static,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
cristye2a912b2011-12-05 20:02:07 +0000347 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +0000348 traits=GetPixelChannelMapTraits(image,channel);
cristy850b3072011-10-08 01:38:05 +0000349 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)
cristye6178502011-12-23 17:02:29 +0000465 #pragma omp parallel for schedule(static,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)
cristye6178502011-12-23 17:02:29 +0000724 #pragma omp parallel for schedule(static,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
cristye2a912b2011-12-05 20:02:07 +0000764 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +0000765 traits=GetPixelChannelMapTraits(image,channel);
cristyc7e6ff62011-10-03 13:46:11 +0000766 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)
cristye6178502011-12-23 17:02:29 +0000970 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristye6365592010-04-02 17:31:23 +0000971#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)
cristydbdd0e32011-11-04 23:29:40 +00001178 return(QuantumScale*StringToDouble(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));
cristydbdd0e32011-11-04 23:29:40 +00001245 return(QuantumScale*StringToDouble(statistic,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001246}
1247
1248static MagickRealType
cristy0568ffc2011-07-25 16:54:14 +00001249 FxEvaluateSubexpression(FxInfo *,const PixelChannel,const ssize_t,
cristye85007d2010-06-06 22:51:36 +00001250 const ssize_t,const char *,MagickRealType *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +00001251
cristyb0aad4c2011-11-02 19:30:35 +00001252static MagickOffsetType FxGCD(MagickOffsetType alpha,MagickOffsetType beta)
1253{
1254 if (beta != 0)
1255 return(FxGCD(beta,alpha % beta));
1256 return(alpha);
1257}
1258
cristy3ed852e2009-09-05 21:47:34 +00001259static inline const char *FxSubexpression(const char *expression,
1260 ExceptionInfo *exception)
1261{
1262 const char
1263 *subexpression;
1264
cristybb503372010-05-27 20:51:26 +00001265 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001266 level;
1267
1268 level=0;
1269 subexpression=expression;
1270 while ((*subexpression != '\0') &&
1271 ((level != 1) || (strchr(")",(int) *subexpression) == (char *) NULL)))
1272 {
1273 if (strchr("(",(int) *subexpression) != (char *) NULL)
1274 level++;
1275 else
1276 if (strchr(")",(int) *subexpression) != (char *) NULL)
1277 level--;
1278 subexpression++;
1279 }
1280 if (*subexpression == '\0')
1281 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1282 "UnbalancedParenthesis","`%s'",expression);
1283 return(subexpression);
1284}
1285
cristy0568ffc2011-07-25 16:54:14 +00001286static MagickRealType FxGetSymbol(FxInfo *fx_info,const PixelChannel channel,
cristye85007d2010-06-06 22:51:36 +00001287 const ssize_t x,const ssize_t y,const char *expression,
1288 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001289{
1290 char
1291 *q,
1292 subexpression[MaxTextExtent],
1293 symbol[MaxTextExtent];
1294
1295 const char
1296 *p,
1297 *value;
1298
1299 Image
1300 *image;
1301
cristy4c08aed2011-07-01 19:47:50 +00001302 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001303 pixel;
1304
1305 MagickRealType
1306 alpha,
1307 beta;
1308
1309 PointInfo
1310 point;
1311
cristybb503372010-05-27 20:51:26 +00001312 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001313 i;
1314
1315 size_t
1316 length;
1317
cristybb503372010-05-27 20:51:26 +00001318 size_t
cristy3ed852e2009-09-05 21:47:34 +00001319 level;
1320
1321 p=expression;
1322 i=GetImageIndexInList(fx_info->images);
1323 level=0;
1324 point.x=(double) x;
1325 point.y=(double) y;
1326 if (isalpha((int) *(p+1)) == 0)
1327 {
1328 if (strchr("suv",(int) *p) != (char *) NULL)
1329 {
1330 switch (*p)
1331 {
1332 case 's':
1333 default:
1334 {
1335 i=GetImageIndexInList(fx_info->images);
1336 break;
1337 }
1338 case 'u': i=0; break;
1339 case 'v': i=1; break;
1340 }
1341 p++;
1342 if (*p == '[')
1343 {
1344 level++;
1345 q=subexpression;
1346 for (p++; *p != '\0'; )
1347 {
1348 if (*p == '[')
1349 level++;
1350 else
1351 if (*p == ']')
1352 {
1353 level--;
1354 if (level == 0)
1355 break;
1356 }
1357 *q++=(*p++);
1358 }
1359 *q='\0';
1360 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1361 &beta,exception);
cristybb503372010-05-27 20:51:26 +00001362 i=(ssize_t) (alpha+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001363 p++;
1364 }
1365 if (*p == '.')
1366 p++;
1367 }
1368 if ((isalpha((int) *(p+1)) == 0) && (*p == 'p'))
1369 {
1370 p++;
1371 if (*p == '{')
1372 {
1373 level++;
1374 q=subexpression;
1375 for (p++; *p != '\0'; )
1376 {
1377 if (*p == '{')
1378 level++;
1379 else
1380 if (*p == '}')
1381 {
1382 level--;
1383 if (level == 0)
1384 break;
1385 }
1386 *q++=(*p++);
1387 }
1388 *q='\0';
1389 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1390 &beta,exception);
1391 point.x=alpha;
1392 point.y=beta;
1393 p++;
1394 }
1395 else
1396 if (*p == '[')
1397 {
1398 level++;
1399 q=subexpression;
1400 for (p++; *p != '\0'; )
1401 {
1402 if (*p == '[')
1403 level++;
1404 else
1405 if (*p == ']')
1406 {
1407 level--;
1408 if (level == 0)
1409 break;
1410 }
1411 *q++=(*p++);
1412 }
1413 *q='\0';
1414 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1415 &beta,exception);
1416 point.x+=alpha;
1417 point.y+=beta;
1418 p++;
1419 }
1420 if (*p == '.')
1421 p++;
1422 }
1423 }
1424 length=GetImageListLength(fx_info->images);
1425 while (i < 0)
cristybb503372010-05-27 20:51:26 +00001426 i+=(ssize_t) length;
cristy3ed852e2009-09-05 21:47:34 +00001427 i%=length;
1428 image=GetImageFromList(fx_info->images,i);
1429 if (image == (Image *) NULL)
1430 {
1431 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1432 "NoSuchImage","`%s'",expression);
1433 return(0.0);
1434 }
cristy4c08aed2011-07-01 19:47:50 +00001435 GetPixelInfo(image,&pixel);
1436 (void) InterpolatePixelInfo(image,fx_info->view[i],image->interpolate,
cristy4f820712011-04-01 12:35:43 +00001437 point.x,point.y,&pixel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001438 if ((strlen(p) > 2) &&
1439 (LocaleCompare(p,"intensity") != 0) &&
1440 (LocaleCompare(p,"luminance") != 0) &&
1441 (LocaleCompare(p,"hue") != 0) &&
1442 (LocaleCompare(p,"saturation") != 0) &&
1443 (LocaleCompare(p,"lightness") != 0))
1444 {
1445 char
1446 name[MaxTextExtent];
1447
1448 (void) CopyMagickString(name,p,MaxTextExtent);
1449 for (q=name+(strlen(name)-1); q > name; q--)
1450 {
1451 if (*q == ')')
1452 break;
1453 if (*q == '.')
1454 {
1455 *q='\0';
1456 break;
1457 }
1458 }
1459 if ((strlen(name) > 2) &&
1460 (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
1461 {
cristy4c08aed2011-07-01 19:47:50 +00001462 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001463 *color;
1464
cristy4c08aed2011-07-01 19:47:50 +00001465 color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
1466 if (color != (PixelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001467 {
1468 pixel=(*color);
1469 p+=strlen(name);
1470 }
1471 else
cristy269c9412011-10-13 23:41:15 +00001472 if (QueryColorCompliance(name,AllCompliance,&pixel,fx_info->exception) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001473 {
1474 (void) AddValueToSplayTree(fx_info->colors,ConstantString(name),
cristy4c08aed2011-07-01 19:47:50 +00001475 ClonePixelInfo(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001476 p+=strlen(name);
1477 }
1478 }
1479 }
1480 (void) CopyMagickString(symbol,p,MaxTextExtent);
1481 StripString(symbol);
1482 if (*symbol == '\0')
1483 {
1484 switch (channel)
1485 {
cristy0568ffc2011-07-25 16:54:14 +00001486 case RedPixelChannel: return(QuantumScale*pixel.red);
1487 case GreenPixelChannel: return(QuantumScale*pixel.green);
1488 case BluePixelChannel: return(QuantumScale*pixel.blue);
1489 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001490 {
1491 if (image->colorspace != CMYKColorspace)
1492 {
1493 (void) ThrowMagickException(exception,GetMagickModule(),
cristy1a020e42011-12-06 18:13:23 +00001494 ImageError,"ColorSeparatedImageRequired","`%s'",
cristy3ed852e2009-09-05 21:47:34 +00001495 image->filename);
1496 return(0.0);
1497 }
cristy4c08aed2011-07-01 19:47:50 +00001498 return(QuantumScale*pixel.black);
1499 }
cristy0568ffc2011-07-25 16:54:14 +00001500 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001501 {
1502 MagickRealType
1503 alpha;
1504
1505 if (pixel.matte == MagickFalse)
1506 return(1.0);
1507 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
1508 return(alpha);
cristy3ed852e2009-09-05 21:47:34 +00001509 }
cristya382aca2011-12-06 18:22:48 +00001510 case IndexPixelChannel:
1511 return(0.0);
cristyb3a73b52011-07-26 01:34:43 +00001512 case IntensityPixelChannel:
cristyf364ed42010-12-15 01:54:43 +00001513 {
cristy4c08aed2011-07-01 19:47:50 +00001514 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristyf364ed42010-12-15 01:54:43 +00001515 }
cristy3ed852e2009-09-05 21:47:34 +00001516 default:
1517 break;
1518 }
1519 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1520 "UnableToParseExpression","`%s'",p);
1521 return(0.0);
1522 }
1523 switch (*symbol)
1524 {
1525 case 'A':
1526 case 'a':
1527 {
1528 if (LocaleCompare(symbol,"a") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001529 return((MagickRealType) (QuantumScale*pixel.alpha));
cristy3ed852e2009-09-05 21:47:34 +00001530 break;
1531 }
1532 case 'B':
1533 case 'b':
1534 {
1535 if (LocaleCompare(symbol,"b") == 0)
1536 return(QuantumScale*pixel.blue);
1537 break;
1538 }
1539 case 'C':
1540 case 'c':
1541 {
1542 if (LocaleNCompare(symbol,"channel",7) == 0)
1543 {
1544 GeometryInfo
1545 channel_info;
1546
1547 MagickStatusType
1548 flags;
1549
1550 flags=ParseGeometry(symbol+7,&channel_info);
1551 if (image->colorspace == CMYKColorspace)
1552 switch (channel)
1553 {
cristy0568ffc2011-07-25 16:54:14 +00001554 case CyanPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001555 {
1556 if ((flags & RhoValue) == 0)
1557 return(0.0);
1558 return(channel_info.rho);
1559 }
cristy0568ffc2011-07-25 16:54:14 +00001560 case MagentaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001561 {
1562 if ((flags & SigmaValue) == 0)
1563 return(0.0);
1564 return(channel_info.sigma);
1565 }
cristy0568ffc2011-07-25 16:54:14 +00001566 case YellowPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001567 {
1568 if ((flags & XiValue) == 0)
1569 return(0.0);
1570 return(channel_info.xi);
1571 }
cristy0568ffc2011-07-25 16:54:14 +00001572 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001573 {
1574 if ((flags & PsiValue) == 0)
1575 return(0.0);
1576 return(channel_info.psi);
1577 }
cristy0568ffc2011-07-25 16:54:14 +00001578 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001579 {
1580 if ((flags & ChiValue) == 0)
1581 return(0.0);
1582 return(channel_info.chi);
1583 }
1584 default:
1585 return(0.0);
1586 }
1587 switch (channel)
1588 {
cristy0568ffc2011-07-25 16:54:14 +00001589 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001590 {
1591 if ((flags & RhoValue) == 0)
1592 return(0.0);
1593 return(channel_info.rho);
1594 }
cristy0568ffc2011-07-25 16:54:14 +00001595 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001596 {
1597 if ((flags & SigmaValue) == 0)
1598 return(0.0);
1599 return(channel_info.sigma);
1600 }
cristy0568ffc2011-07-25 16:54:14 +00001601 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001602 {
1603 if ((flags & XiValue) == 0)
1604 return(0.0);
1605 return(channel_info.xi);
1606 }
cristy0568ffc2011-07-25 16:54:14 +00001607 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001608 {
1609 if ((flags & ChiValue) == 0)
1610 return(0.0);
1611 return(channel_info.chi);
1612 }
cristy0568ffc2011-07-25 16:54:14 +00001613 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001614 {
1615 if ((flags & PsiValue) == 0)
1616 return(0.0);
1617 return(channel_info.psi);
1618 }
cristy3ed852e2009-09-05 21:47:34 +00001619 default:
1620 return(0.0);
1621 }
1622 return(0.0);
1623 }
1624 if (LocaleCompare(symbol,"c") == 0)
1625 return(QuantumScale*pixel.red);
1626 break;
1627 }
1628 case 'D':
1629 case 'd':
1630 {
1631 if (LocaleNCompare(symbol,"depth",5) == 0)
1632 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1633 break;
1634 }
1635 case 'G':
1636 case 'g':
1637 {
1638 if (LocaleCompare(symbol,"g") == 0)
1639 return(QuantumScale*pixel.green);
1640 break;
1641 }
1642 case 'K':
1643 case 'k':
1644 {
1645 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1646 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1647 if (LocaleCompare(symbol,"k") == 0)
1648 {
1649 if (image->colorspace != CMYKColorspace)
1650 {
1651 (void) ThrowMagickException(exception,GetMagickModule(),
1652 OptionError,"ColorSeparatedImageRequired","`%s'",
1653 image->filename);
1654 return(0.0);
1655 }
cristy4c08aed2011-07-01 19:47:50 +00001656 return(QuantumScale*pixel.black);
cristy3ed852e2009-09-05 21:47:34 +00001657 }
1658 break;
1659 }
1660 case 'H':
1661 case 'h':
1662 {
1663 if (LocaleCompare(symbol,"h") == 0)
1664 return((MagickRealType) image->rows);
1665 if (LocaleCompare(symbol,"hue") == 0)
1666 {
1667 double
1668 hue,
1669 lightness,
1670 saturation;
1671
cristyda1f9c12011-10-02 21:39:49 +00001672 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1673 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001674 return(hue);
1675 }
1676 break;
1677 }
1678 case 'I':
1679 case 'i':
1680 {
1681 if ((LocaleCompare(symbol,"image.depth") == 0) ||
1682 (LocaleCompare(symbol,"image.minima") == 0) ||
1683 (LocaleCompare(symbol,"image.maxima") == 0) ||
1684 (LocaleCompare(symbol,"image.mean") == 0) ||
1685 (LocaleCompare(symbol,"image.kurtosis") == 0) ||
1686 (LocaleCompare(symbol,"image.skewness") == 0) ||
1687 (LocaleCompare(symbol,"image.standard_deviation") == 0))
1688 return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
1689 if (LocaleCompare(symbol,"image.resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001690 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001691 if (LocaleCompare(symbol,"image.resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001692 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001693 if (LocaleCompare(symbol,"intensity") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001694 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001695 if (LocaleCompare(symbol,"i") == 0)
1696 return((MagickRealType) x);
1697 break;
1698 }
1699 case 'J':
1700 case 'j':
1701 {
1702 if (LocaleCompare(symbol,"j") == 0)
1703 return((MagickRealType) y);
1704 break;
1705 }
1706 case 'L':
1707 case 'l':
1708 {
1709 if (LocaleCompare(symbol,"lightness") == 0)
1710 {
1711 double
1712 hue,
1713 lightness,
1714 saturation;
1715
cristyda1f9c12011-10-02 21:39:49 +00001716 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1717 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001718 return(lightness);
1719 }
1720 if (LocaleCompare(symbol,"luminance") == 0)
1721 {
1722 double
1723 luminence;
1724
1725 luminence=0.2126*pixel.red+0.7152*pixel.green+0.0722*pixel.blue;
1726 return(QuantumScale*luminence);
1727 }
1728 break;
1729 }
1730 case 'M':
1731 case 'm':
1732 {
1733 if (LocaleNCompare(symbol,"maxima",6) == 0)
1734 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1735 if (LocaleNCompare(symbol,"mean",4) == 0)
1736 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1737 if (LocaleNCompare(symbol,"minima",6) == 0)
1738 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1739 if (LocaleCompare(symbol,"m") == 0)
1740 return(QuantumScale*pixel.blue);
1741 break;
1742 }
1743 case 'N':
1744 case 'n':
1745 {
1746 if (LocaleCompare(symbol,"n") == 0)
anthony374f5dd2011-03-25 10:08:53 +00001747 return((MagickRealType) GetImageListLength(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001748 break;
1749 }
1750 case 'O':
1751 case 'o':
1752 {
1753 if (LocaleCompare(symbol,"o") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001754 return(QuantumScale*pixel.alpha);
cristy3ed852e2009-09-05 21:47:34 +00001755 break;
1756 }
1757 case 'P':
1758 case 'p':
1759 {
1760 if (LocaleCompare(symbol,"page.height") == 0)
1761 return((MagickRealType) image->page.height);
1762 if (LocaleCompare(symbol,"page.width") == 0)
1763 return((MagickRealType) image->page.width);
1764 if (LocaleCompare(symbol,"page.x") == 0)
1765 return((MagickRealType) image->page.x);
1766 if (LocaleCompare(symbol,"page.y") == 0)
1767 return((MagickRealType) image->page.y);
1768 break;
1769 }
1770 case 'R':
1771 case 'r':
1772 {
1773 if (LocaleCompare(symbol,"resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001774 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001775 if (LocaleCompare(symbol,"resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001776 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001777 if (LocaleCompare(symbol,"r") == 0)
1778 return(QuantumScale*pixel.red);
1779 break;
1780 }
1781 case 'S':
1782 case 's':
1783 {
1784 if (LocaleCompare(symbol,"saturation") == 0)
1785 {
1786 double
1787 hue,
1788 lightness,
1789 saturation;
1790
cristyda1f9c12011-10-02 21:39:49 +00001791 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1792 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001793 return(saturation);
1794 }
1795 if (LocaleNCompare(symbol,"skewness",8) == 0)
1796 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1797 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1798 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1799 break;
1800 }
1801 case 'T':
1802 case 't':
1803 {
1804 if (LocaleCompare(symbol,"t") == 0)
cristy5a15b932011-03-26 12:50:33 +00001805 return((MagickRealType) GetImageIndexInList(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001806 break;
1807 }
1808 case 'W':
1809 case 'w':
1810 {
1811 if (LocaleCompare(symbol,"w") == 0)
1812 return((MagickRealType) image->columns);
1813 break;
1814 }
1815 case 'Y':
1816 case 'y':
1817 {
1818 if (LocaleCompare(symbol,"y") == 0)
1819 return(QuantumScale*pixel.green);
1820 break;
1821 }
1822 case 'Z':
1823 case 'z':
1824 {
1825 if (LocaleCompare(symbol,"z") == 0)
1826 {
1827 MagickRealType
1828 depth;
1829
cristyfefab1b2011-07-05 00:33:22 +00001830 depth=(MagickRealType) GetImageDepth(image,fx_info->exception);
cristy3ed852e2009-09-05 21:47:34 +00001831 return(depth);
1832 }
1833 break;
1834 }
1835 default:
1836 break;
1837 }
1838 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1839 if (value != (const char *) NULL)
cristydbdd0e32011-11-04 23:29:40 +00001840 return((MagickRealType) StringToDouble(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001841 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1842 "UnableToParseExpression","`%s'",symbol);
1843 return(0.0);
1844}
1845
1846static const char *FxOperatorPrecedence(const char *expression,
1847 ExceptionInfo *exception)
1848{
1849 typedef enum
1850 {
1851 UndefinedPrecedence,
1852 NullPrecedence,
1853 BitwiseComplementPrecedence,
1854 ExponentPrecedence,
cristy116af162010-08-13 01:25:47 +00001855 ExponentialNotationPrecedence,
cristy3ed852e2009-09-05 21:47:34 +00001856 MultiplyPrecedence,
1857 AdditionPrecedence,
1858 ShiftPrecedence,
1859 RelationalPrecedence,
1860 EquivalencyPrecedence,
1861 BitwiseAndPrecedence,
1862 BitwiseOrPrecedence,
1863 LogicalAndPrecedence,
1864 LogicalOrPrecedence,
1865 TernaryPrecedence,
1866 AssignmentPrecedence,
1867 CommaPrecedence,
1868 SeparatorPrecedence
1869 } FxPrecedence;
1870
1871 FxPrecedence
1872 precedence,
1873 target;
1874
1875 register const char
1876 *subexpression;
1877
1878 register int
1879 c;
1880
cristybb503372010-05-27 20:51:26 +00001881 size_t
cristy3ed852e2009-09-05 21:47:34 +00001882 level;
1883
1884 c=0;
1885 level=0;
1886 subexpression=(const char *) NULL;
1887 target=NullPrecedence;
1888 while (*expression != '\0')
1889 {
1890 precedence=UndefinedPrecedence;
1891 if ((isspace((int) ((char) *expression)) != 0) || (c == (int) '@'))
1892 {
1893 expression++;
1894 continue;
1895 }
cristy488fa882010-03-01 22:34:24 +00001896 switch (*expression)
1897 {
1898 case 'A':
1899 case 'a':
cristy3ed852e2009-09-05 21:47:34 +00001900 {
cristyb33454f2011-08-03 02:10:45 +00001901#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00001902 if (LocaleNCompare(expression,"acosh",5) == 0)
1903 {
1904 expression+=5;
1905 break;
1906 }
cristyb33454f2011-08-03 02:10:45 +00001907#endif
1908#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00001909 if (LocaleNCompare(expression,"asinh",5) == 0)
1910 {
1911 expression+=5;
1912 break;
1913 }
cristyb33454f2011-08-03 02:10:45 +00001914#endif
1915#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00001916 if (LocaleNCompare(expression,"atanh",5) == 0)
cristy488fa882010-03-01 22:34:24 +00001917 {
1918 expression+=5;
1919 break;
1920 }
cristyb33454f2011-08-03 02:10:45 +00001921#endif
cristy488fa882010-03-01 22:34:24 +00001922 break;
cristy3ed852e2009-09-05 21:47:34 +00001923 }
cristy62d455f2011-11-03 11:42:28 +00001924 case 'E':
1925 case 'e':
1926 {
1927 if ((LocaleNCompare(expression,"E+",2) == 0) ||
1928 (LocaleNCompare(expression,"E-",2) == 0))
1929 {
1930 expression+=2; /* scientific notation */
1931 break;
1932 }
1933 }
cristy488fa882010-03-01 22:34:24 +00001934 case 'J':
1935 case 'j':
1936 {
1937 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1938 (LocaleNCompare(expression,"j1",2) == 0))
1939 {
1940 expression+=2;
1941 break;
1942 }
1943 break;
1944 }
cristy2def9322010-06-18 23:59:37 +00001945 case '#':
1946 {
1947 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1948 expression++;
1949 break;
1950 }
cristy488fa882010-03-01 22:34:24 +00001951 default:
1952 break;
1953 }
cristy3ed852e2009-09-05 21:47:34 +00001954 if ((c == (int) '{') || (c == (int) '['))
1955 level++;
1956 else
1957 if ((c == (int) '}') || (c == (int) ']'))
1958 level--;
1959 if (level == 0)
1960 switch ((unsigned char) *expression)
1961 {
1962 case '~':
1963 case '!':
1964 {
1965 precedence=BitwiseComplementPrecedence;
1966 break;
1967 }
1968 case '^':
cristy6621e252010-08-13 00:42:57 +00001969 case '@':
cristy3ed852e2009-09-05 21:47:34 +00001970 {
1971 precedence=ExponentPrecedence;
1972 break;
1973 }
1974 default:
1975 {
1976 if (((c != 0) && ((isdigit((int) ((char) c)) != 0) ||
1977 (strchr(")",c) != (char *) NULL))) &&
1978 (((islower((int) ((char) *expression)) != 0) ||
1979 (strchr("(",(int) *expression) != (char *) NULL)) ||
1980 ((isdigit((int) ((char) c)) == 0) &&
1981 (isdigit((int) ((char) *expression)) != 0))) &&
1982 (strchr("xy",(int) *expression) == (char *) NULL))
1983 precedence=MultiplyPrecedence;
1984 break;
1985 }
1986 case '*':
1987 case '/':
1988 case '%':
1989 {
1990 precedence=MultiplyPrecedence;
1991 break;
1992 }
1993 case '+':
1994 case '-':
1995 {
1996 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
1997 (isalpha(c) != 0))
1998 precedence=AdditionPrecedence;
1999 break;
2000 }
2001 case LeftShiftOperator:
2002 case RightShiftOperator:
2003 {
2004 precedence=ShiftPrecedence;
2005 break;
2006 }
2007 case '<':
2008 case LessThanEqualOperator:
2009 case GreaterThanEqualOperator:
2010 case '>':
2011 {
2012 precedence=RelationalPrecedence;
2013 break;
2014 }
2015 case EqualOperator:
2016 case NotEqualOperator:
2017 {
2018 precedence=EquivalencyPrecedence;
2019 break;
2020 }
2021 case '&':
2022 {
2023 precedence=BitwiseAndPrecedence;
2024 break;
2025 }
2026 case '|':
2027 {
2028 precedence=BitwiseOrPrecedence;
2029 break;
2030 }
2031 case LogicalAndOperator:
2032 {
2033 precedence=LogicalAndPrecedence;
2034 break;
2035 }
2036 case LogicalOrOperator:
2037 {
2038 precedence=LogicalOrPrecedence;
2039 break;
2040 }
cristy116af162010-08-13 01:25:47 +00002041 case ExponentialNotation:
2042 {
2043 precedence=ExponentialNotationPrecedence;
2044 break;
2045 }
cristy3ed852e2009-09-05 21:47:34 +00002046 case ':':
2047 case '?':
2048 {
2049 precedence=TernaryPrecedence;
2050 break;
2051 }
2052 case '=':
2053 {
2054 precedence=AssignmentPrecedence;
2055 break;
2056 }
2057 case ',':
2058 {
2059 precedence=CommaPrecedence;
2060 break;
2061 }
2062 case ';':
2063 {
2064 precedence=SeparatorPrecedence;
2065 break;
2066 }
2067 }
2068 if ((precedence == BitwiseComplementPrecedence) ||
2069 (precedence == TernaryPrecedence) ||
2070 (precedence == AssignmentPrecedence))
2071 {
2072 if (precedence > target)
2073 {
2074 /*
2075 Right-to-left associativity.
2076 */
2077 target=precedence;
2078 subexpression=expression;
2079 }
2080 }
2081 else
2082 if (precedence >= target)
2083 {
2084 /*
2085 Left-to-right associativity.
2086 */
2087 target=precedence;
2088 subexpression=expression;
2089 }
2090 if (strchr("(",(int) *expression) != (char *) NULL)
2091 expression=FxSubexpression(expression,exception);
2092 c=(int) (*expression++);
2093 }
2094 return(subexpression);
2095}
2096
2097static MagickRealType FxEvaluateSubexpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002098 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002099 const char *expression,MagickRealType *beta,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002100{
2101 char
2102 *q,
2103 subexpression[MaxTextExtent];
2104
2105 MagickRealType
2106 alpha,
2107 gamma;
2108
2109 register const char
2110 *p;
2111
2112 *beta=0.0;
2113 if (exception->severity != UndefinedException)
2114 return(0.0);
2115 while (isspace((int) *expression) != 0)
2116 expression++;
2117 if (*expression == '\0')
2118 {
2119 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
2120 "MissingExpression","`%s'",expression);
2121 return(0.0);
2122 }
cristy66322f02010-05-17 11:40:48 +00002123 *subexpression='\0';
cristy3ed852e2009-09-05 21:47:34 +00002124 p=FxOperatorPrecedence(expression,exception);
2125 if (p != (const char *) NULL)
2126 {
2127 (void) CopyMagickString(subexpression,expression,(size_t)
2128 (p-expression+1));
2129 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2130 exception);
2131 switch ((unsigned char) *p)
2132 {
2133 case '~':
2134 {
2135 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002136 *beta=(MagickRealType) (~(size_t) *beta);
cristy3ed852e2009-09-05 21:47:34 +00002137 return(*beta);
2138 }
2139 case '!':
2140 {
2141 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2142 return(*beta == 0.0 ? 1.0 : 0.0);
2143 }
2144 case '^':
2145 {
2146 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2147 channel,x,y,++p,beta,exception));
2148 return(*beta);
2149 }
2150 case '*':
cristy116af162010-08-13 01:25:47 +00002151 case ExponentialNotation:
cristy3ed852e2009-09-05 21:47:34 +00002152 {
2153 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2154 return(alpha*(*beta));
2155 }
2156 case '/':
2157 {
2158 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2159 if (*beta == 0.0)
2160 {
2161 if (exception->severity == UndefinedException)
2162 (void) ThrowMagickException(exception,GetMagickModule(),
2163 OptionError,"DivideByZero","`%s'",expression);
2164 return(0.0);
2165 }
2166 return(alpha/(*beta));
2167 }
2168 case '%':
2169 {
2170 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2171 *beta=fabs(floor(((double) *beta)+0.5));
2172 if (*beta == 0.0)
2173 {
2174 (void) ThrowMagickException(exception,GetMagickModule(),
2175 OptionError,"DivideByZero","`%s'",expression);
2176 return(0.0);
2177 }
2178 return(fmod((double) alpha,(double) *beta));
2179 }
2180 case '+':
2181 {
2182 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2183 return(alpha+(*beta));
2184 }
2185 case '-':
2186 {
2187 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2188 return(alpha-(*beta));
2189 }
2190 case LeftShiftOperator:
2191 {
2192 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002193 *beta=(MagickRealType) ((size_t) (alpha+0.5) << (size_t)
cristy3ed852e2009-09-05 21:47:34 +00002194 (gamma+0.5));
2195 return(*beta);
2196 }
2197 case RightShiftOperator:
2198 {
2199 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002200 *beta=(MagickRealType) ((size_t) (alpha+0.5) >> (size_t)
cristy3ed852e2009-09-05 21:47:34 +00002201 (gamma+0.5));
2202 return(*beta);
2203 }
2204 case '<':
2205 {
2206 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2207 return(alpha < *beta ? 1.0 : 0.0);
2208 }
2209 case LessThanEqualOperator:
2210 {
2211 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2212 return(alpha <= *beta ? 1.0 : 0.0);
2213 }
2214 case '>':
2215 {
2216 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2217 return(alpha > *beta ? 1.0 : 0.0);
2218 }
2219 case GreaterThanEqualOperator:
2220 {
2221 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2222 return(alpha >= *beta ? 1.0 : 0.0);
2223 }
2224 case EqualOperator:
2225 {
2226 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2227 return(fabs(alpha-(*beta)) <= MagickEpsilon ? 1.0 : 0.0);
2228 }
2229 case NotEqualOperator:
2230 {
2231 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2232 return(fabs(alpha-(*beta)) > MagickEpsilon ? 1.0 : 0.0);
2233 }
2234 case '&':
2235 {
2236 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002237 *beta=(MagickRealType) ((size_t) (alpha+0.5) & (size_t)
cristy3ed852e2009-09-05 21:47:34 +00002238 (gamma+0.5));
2239 return(*beta);
2240 }
2241 case '|':
2242 {
2243 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002244 *beta=(MagickRealType) ((size_t) (alpha+0.5) | (size_t)
cristy3ed852e2009-09-05 21:47:34 +00002245 (gamma+0.5));
2246 return(*beta);
2247 }
2248 case LogicalAndOperator:
2249 {
2250 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2251 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2252 return(*beta);
2253 }
2254 case LogicalOrOperator:
2255 {
2256 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2257 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2258 return(*beta);
2259 }
2260 case '?':
2261 {
2262 MagickRealType
2263 gamma;
2264
2265 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2266 q=subexpression;
2267 p=StringToken(":",&q);
2268 if (q == (char *) NULL)
2269 {
2270 (void) ThrowMagickException(exception,GetMagickModule(),
2271 OptionError,"UnableToParseExpression","`%s'",subexpression);
2272 return(0.0);
2273 }
2274 if (fabs((double) alpha) > MagickEpsilon)
2275 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2276 else
2277 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2278 return(gamma);
2279 }
2280 case '=':
2281 {
2282 char
2283 numeric[MaxTextExtent];
2284
2285 q=subexpression;
2286 while (isalpha((int) ((unsigned char) *q)) != 0)
2287 q++;
2288 if (*q != '\0')
2289 {
2290 (void) ThrowMagickException(exception,GetMagickModule(),
2291 OptionError,"UnableToParseExpression","`%s'",subexpression);
2292 return(0.0);
2293 }
2294 ClearMagickException(exception);
2295 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristyb51dff52011-05-19 16:55:47 +00002296 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
cristy8cd5b312010-01-07 01:10:24 +00002297 *beta);
cristy3ed852e2009-09-05 21:47:34 +00002298 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2299 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2300 subexpression),ConstantString(numeric));
2301 return(*beta);
2302 }
2303 case ',':
2304 {
2305 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2306 return(alpha);
2307 }
2308 case ';':
2309 {
2310 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2311 return(*beta);
2312 }
2313 default:
2314 {
2315 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2316 exception);
2317 return(gamma);
2318 }
2319 }
2320 }
2321 if (strchr("(",(int) *expression) != (char *) NULL)
2322 {
2323 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2324 subexpression[strlen(subexpression)-1]='\0';
2325 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2326 exception);
2327 return(gamma);
2328 }
cristy8b8a3ae2010-10-23 18:49:46 +00002329 switch (*expression)
cristy3ed852e2009-09-05 21:47:34 +00002330 {
2331 case '+':
2332 {
2333 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2334 exception);
2335 return(1.0*gamma);
2336 }
2337 case '-':
2338 {
2339 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2340 exception);
2341 return(-1.0*gamma);
2342 }
2343 case '~':
2344 {
2345 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2346 exception);
cristybb503372010-05-27 20:51:26 +00002347 return((MagickRealType) (~(size_t) (gamma+0.5)));
cristy3ed852e2009-09-05 21:47:34 +00002348 }
2349 case 'A':
2350 case 'a':
2351 {
2352 if (LocaleNCompare(expression,"abs",3) == 0)
2353 {
2354 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2355 exception);
2356 return((MagickRealType) fabs((double) alpha));
2357 }
cristyb33454f2011-08-03 02:10:45 +00002358#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00002359 if (LocaleNCompare(expression,"acosh",5) == 0)
2360 {
2361 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2362 exception);
2363 return((MagickRealType) acosh((double) alpha));
2364 }
cristyb33454f2011-08-03 02:10:45 +00002365#endif
cristy3ed852e2009-09-05 21:47:34 +00002366 if (LocaleNCompare(expression,"acos",4) == 0)
2367 {
2368 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2369 exception);
2370 return((MagickRealType) acos((double) alpha));
2371 }
cristy43c22f42010-03-30 12:34:07 +00002372#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002373 if (LocaleNCompare(expression,"airy",4) == 0)
2374 {
2375 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2376 exception);
2377 if (alpha == 0.0)
cristy2dd03222010-03-30 22:12:11 +00002378 return(1.0);
2379 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
cristy43c22f42010-03-30 12:34:07 +00002380 return(gamma*gamma);
cristyee56cf12010-03-01 22:17:06 +00002381 }
cristy43c22f42010-03-30 12:34:07 +00002382#endif
cristyb33454f2011-08-03 02:10:45 +00002383#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00002384 if (LocaleNCompare(expression,"asinh",5) == 0)
2385 {
2386 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2387 exception);
2388 return((MagickRealType) asinh((double) alpha));
2389 }
cristyb33454f2011-08-03 02:10:45 +00002390#endif
cristy3ed852e2009-09-05 21:47:34 +00002391 if (LocaleNCompare(expression,"asin",4) == 0)
2392 {
2393 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2394 exception);
2395 return((MagickRealType) asin((double) alpha));
2396 }
2397 if (LocaleNCompare(expression,"alt",3) == 0)
2398 {
2399 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2400 exception);
cristybb503372010-05-27 20:51:26 +00002401 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
cristy3ed852e2009-09-05 21:47:34 +00002402 }
2403 if (LocaleNCompare(expression,"atan2",5) == 0)
2404 {
2405 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2406 exception);
2407 return((MagickRealType) atan2((double) alpha,(double) *beta));
2408 }
cristyb33454f2011-08-03 02:10:45 +00002409#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00002410 if (LocaleNCompare(expression,"atanh",5) == 0)
2411 {
2412 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2413 exception);
2414 return((MagickRealType) atanh((double) alpha));
2415 }
cristyb33454f2011-08-03 02:10:45 +00002416#endif
cristy3ed852e2009-09-05 21:47:34 +00002417 if (LocaleNCompare(expression,"atan",4) == 0)
2418 {
2419 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2420 exception);
2421 return((MagickRealType) atan((double) alpha));
2422 }
2423 if (LocaleCompare(expression,"a") == 0)
2424 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2425 break;
2426 }
2427 case 'B':
2428 case 'b':
2429 {
2430 if (LocaleCompare(expression,"b") == 0)
2431 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2432 break;
2433 }
2434 case 'C':
2435 case 'c':
2436 {
2437 if (LocaleNCompare(expression,"ceil",4) == 0)
2438 {
2439 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2440 exception);
2441 return((MagickRealType) ceil((double) alpha));
2442 }
2443 if (LocaleNCompare(expression,"cosh",4) == 0)
2444 {
2445 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2446 exception);
2447 return((MagickRealType) cosh((double) alpha));
2448 }
2449 if (LocaleNCompare(expression,"cos",3) == 0)
2450 {
2451 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2452 exception);
2453 return((MagickRealType) cos((double) alpha));
2454 }
2455 if (LocaleCompare(expression,"c") == 0)
2456 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2457 break;
2458 }
2459 case 'D':
2460 case 'd':
2461 {
2462 if (LocaleNCompare(expression,"debug",5) == 0)
2463 {
2464 const char
2465 *type;
2466
2467 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2468 exception);
2469 if (fx_info->images->colorspace == CMYKColorspace)
2470 switch (channel)
2471 {
cristy0568ffc2011-07-25 16:54:14 +00002472 case CyanPixelChannel: type="cyan"; break;
2473 case MagentaPixelChannel: type="magenta"; break;
2474 case YellowPixelChannel: type="yellow"; break;
2475 case AlphaPixelChannel: type="opacity"; break;
2476 case BlackPixelChannel: type="black"; break;
cristy3ed852e2009-09-05 21:47:34 +00002477 default: type="unknown"; break;
2478 }
2479 else
2480 switch (channel)
2481 {
cristy0568ffc2011-07-25 16:54:14 +00002482 case RedPixelChannel: type="red"; break;
2483 case GreenPixelChannel: type="green"; break;
2484 case BluePixelChannel: type="blue"; break;
2485 case AlphaPixelChannel: type="opacity"; break;
cristy3ed852e2009-09-05 21:47:34 +00002486 default: type="unknown"; break;
2487 }
2488 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2489 if (strlen(subexpression) > 1)
2490 subexpression[strlen(subexpression)-1]='\0';
2491 if (fx_info->file != (FILE *) NULL)
cristy1e604812011-05-19 18:07:50 +00002492 (void) FormatLocaleFile(fx_info->file,
2493 "%s[%.20g,%.20g].%s: %s=%.*g\n",fx_info->images->filename,
2494 (double) x,(double) y,type,subexpression,GetMagickPrecision(),
2495 (double) alpha);
cristy3ed852e2009-09-05 21:47:34 +00002496 return(0.0);
2497 }
cristy5597a8d2011-11-04 00:25:32 +00002498 if (LocaleNCompare(expression,"drc",3) == 0)
2499 {
2500 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2501 exception);
2502 return((MagickRealType) (alpha/(*beta*(alpha-1.0)+1.0)));
2503 }
cristy3ed852e2009-09-05 21:47:34 +00002504 break;
2505 }
2506 case 'E':
2507 case 'e':
2508 {
2509 if (LocaleCompare(expression,"epsilon") == 0)
2510 return((MagickRealType) MagickEpsilon);
2511 if (LocaleNCompare(expression,"exp",3) == 0)
2512 {
2513 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2514 exception);
2515 return((MagickRealType) exp((double) alpha));
2516 }
2517 if (LocaleCompare(expression,"e") == 0)
2518 return((MagickRealType) 2.7182818284590452354);
2519 break;
2520 }
2521 case 'F':
2522 case 'f':
2523 {
2524 if (LocaleNCompare(expression,"floor",5) == 0)
2525 {
2526 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2527 exception);
2528 return((MagickRealType) floor((double) alpha));
2529 }
2530 break;
2531 }
2532 case 'G':
2533 case 'g':
2534 {
cristy9eeedea2011-11-02 19:04:05 +00002535 if (LocaleNCompare(expression,"gauss",5) == 0)
2536 {
2537 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2538 exception);
2539 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
2540 return((MagickRealType) gamma);
2541 }
cristyb0aad4c2011-11-02 19:30:35 +00002542 if (LocaleNCompare(expression,"gcd",3) == 0)
2543 {
2544 MagickOffsetType
2545 gcd;
2546
2547 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2548 exception);
cristy76461162011-11-02 19:32:19 +00002549 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType)
2550 (*beta+0.5));
cristyb0aad4c2011-11-02 19:30:35 +00002551 return((MagickRealType) gcd);
2552 }
cristy3ed852e2009-09-05 21:47:34 +00002553 if (LocaleCompare(expression,"g") == 0)
2554 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2555 break;
2556 }
2557 case 'H':
2558 case 'h':
2559 {
2560 if (LocaleCompare(expression,"h") == 0)
2561 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2562 if (LocaleCompare(expression,"hue") == 0)
2563 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2564 if (LocaleNCompare(expression,"hypot",5) == 0)
2565 {
2566 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2567 exception);
2568 return((MagickRealType) hypot((double) alpha,(double) *beta));
2569 }
2570 break;
2571 }
2572 case 'K':
2573 case 'k':
2574 {
2575 if (LocaleCompare(expression,"k") == 0)
2576 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2577 break;
2578 }
2579 case 'I':
2580 case 'i':
2581 {
2582 if (LocaleCompare(expression,"intensity") == 0)
2583 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2584 if (LocaleNCompare(expression,"int",3) == 0)
2585 {
2586 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2587 exception);
cristy16788e42010-08-13 13:44:26 +00002588 return((MagickRealType) floor(alpha));
cristy3ed852e2009-09-05 21:47:34 +00002589 }
cristy82b20722011-11-05 21:52:36 +00002590#if defined(MAGICKCORE_HAVE_ISNAN)
cristy639399c2011-11-02 19:16:15 +00002591 if (LocaleNCompare(expression,"isnan",5) == 0)
2592 {
2593 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2594 exception);
cristy17a10202011-11-02 19:17:04 +00002595 return((MagickRealType) !!isnan((double) alpha));
cristy639399c2011-11-02 19:16:15 +00002596 }
cristy82b20722011-11-05 21:52:36 +00002597#endif
cristy3ed852e2009-09-05 21:47:34 +00002598 if (LocaleCompare(expression,"i") == 0)
2599 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2600 break;
2601 }
2602 case 'J':
2603 case 'j':
2604 {
2605 if (LocaleCompare(expression,"j") == 0)
2606 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
cristy161b9262010-03-20 19:34:32 +00002607#if defined(MAGICKCORE_HAVE_J0)
cristyee56cf12010-03-01 22:17:06 +00002608 if (LocaleNCompare(expression,"j0",2) == 0)
2609 {
2610 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2611 exception);
2612 return((MagickRealType) j0((double) alpha));
2613 }
cristy161b9262010-03-20 19:34:32 +00002614#endif
2615#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002616 if (LocaleNCompare(expression,"j1",2) == 0)
2617 {
2618 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2619 exception);
2620 return((MagickRealType) j1((double) alpha));
2621 }
cristy161b9262010-03-20 19:34:32 +00002622#endif
cristyaa018fa2010-04-08 23:03:54 +00002623#if defined(MAGICKCORE_HAVE_J1)
cristya6a09e72010-03-02 14:51:02 +00002624 if (LocaleNCompare(expression,"jinc",4) == 0)
2625 {
2626 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2627 exception);
cristy0946a822010-03-12 17:14:58 +00002628 if (alpha == 0.0)
2629 return(1.0);
cristy69928f92010-03-12 13:27:09 +00002630 gamma=(MagickRealType) (2.0*j1((double) (MagickPI*alpha))/
cristyfce2f7b2010-03-12 00:29:49 +00002631 (MagickPI*alpha));
2632 return(gamma);
cristya6a09e72010-03-02 14:51:02 +00002633 }
cristyaa018fa2010-04-08 23:03:54 +00002634#endif
cristy3ed852e2009-09-05 21:47:34 +00002635 break;
2636 }
2637 case 'L':
2638 case 'l':
2639 {
2640 if (LocaleNCompare(expression,"ln",2) == 0)
2641 {
2642 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2643 exception);
2644 return((MagickRealType) log((double) alpha));
2645 }
cristyc8ed5322010-08-31 12:07:59 +00002646 if (LocaleNCompare(expression,"logtwo",6) == 0)
cristy3ed852e2009-09-05 21:47:34 +00002647 {
cristyc8ed5322010-08-31 12:07:59 +00002648 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
cristy3ed852e2009-09-05 21:47:34 +00002649 exception);
2650 return((MagickRealType) log10((double) alpha))/log10(2.0);
2651 }
2652 if (LocaleNCompare(expression,"log",3) == 0)
2653 {
2654 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2655 exception);
2656 return((MagickRealType) log10((double) alpha));
2657 }
2658 if (LocaleCompare(expression,"lightness") == 0)
2659 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2660 break;
2661 }
2662 case 'M':
2663 case 'm':
2664 {
2665 if (LocaleCompare(expression,"MaxRGB") == 0)
2666 return((MagickRealType) QuantumRange);
2667 if (LocaleNCompare(expression,"maxima",6) == 0)
2668 break;
2669 if (LocaleNCompare(expression,"max",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002670 {
2671 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2672 exception);
2673 return(alpha > *beta ? alpha : *beta);
2674 }
cristy3ed852e2009-09-05 21:47:34 +00002675 if (LocaleNCompare(expression,"minima",6) == 0)
2676 break;
2677 if (LocaleNCompare(expression,"min",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002678 {
2679 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2680 exception);
2681 return(alpha < *beta ? alpha : *beta);
2682 }
cristy3ed852e2009-09-05 21:47:34 +00002683 if (LocaleNCompare(expression,"mod",3) == 0)
2684 {
2685 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2686 exception);
cristy984049c2011-11-03 18:34:58 +00002687 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2688 return(gamma);
cristy3ed852e2009-09-05 21:47:34 +00002689 }
2690 if (LocaleCompare(expression,"m") == 0)
2691 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2692 break;
2693 }
2694 case 'N':
2695 case 'n':
2696 {
cristyad3502e2011-11-02 19:10:45 +00002697 if (LocaleNCompare(expression,"not",3) == 0)
2698 {
2699 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2700 exception);
2701 return((MagickRealType) (alpha < MagickEpsilon));
2702 }
cristy3ed852e2009-09-05 21:47:34 +00002703 if (LocaleCompare(expression,"n") == 0)
2704 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2705 break;
2706 }
2707 case 'O':
2708 case 'o':
2709 {
2710 if (LocaleCompare(expression,"Opaque") == 0)
2711 return(1.0);
2712 if (LocaleCompare(expression,"o") == 0)
2713 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2714 break;
2715 }
2716 case 'P':
2717 case 'p':
2718 {
cristy670aa3c2011-11-03 00:54:00 +00002719 if (LocaleCompare(expression,"phi") == 0)
2720 return((MagickRealType) MagickPHI);
cristy3ed852e2009-09-05 21:47:34 +00002721 if (LocaleCompare(expression,"pi") == 0)
2722 return((MagickRealType) MagickPI);
2723 if (LocaleNCompare(expression,"pow",3) == 0)
2724 {
2725 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2726 exception);
2727 return((MagickRealType) pow((double) alpha,(double) *beta));
2728 }
2729 if (LocaleCompare(expression,"p") == 0)
2730 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2731 break;
2732 }
2733 case 'Q':
2734 case 'q':
2735 {
2736 if (LocaleCompare(expression,"QuantumRange") == 0)
2737 return((MagickRealType) QuantumRange);
2738 if (LocaleCompare(expression,"QuantumScale") == 0)
2739 return((MagickRealType) QuantumScale);
2740 break;
2741 }
2742 case 'R':
2743 case 'r':
2744 {
2745 if (LocaleNCompare(expression,"rand",4) == 0)
2746 return((MagickRealType) GetPseudoRandomValue(fx_info->random_info));
2747 if (LocaleNCompare(expression,"round",5) == 0)
2748 {
2749 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2750 exception);
cristy16788e42010-08-13 13:44:26 +00002751 return((MagickRealType) floor((double) alpha+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002752 }
2753 if (LocaleCompare(expression,"r") == 0)
2754 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2755 break;
2756 }
2757 case 'S':
2758 case 's':
2759 {
2760 if (LocaleCompare(expression,"saturation") == 0)
2761 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2762 if (LocaleNCompare(expression,"sign",4) == 0)
2763 {
2764 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2765 exception);
2766 return(alpha < 0.0 ? -1.0 : 1.0);
2767 }
cristya6a09e72010-03-02 14:51:02 +00002768 if (LocaleNCompare(expression,"sinc",4) == 0)
2769 {
2770 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2771 exception);
2772 if (alpha == 0)
2773 return(1.0);
2774 gamma=(MagickRealType) (sin((double) (MagickPI*alpha))/
2775 (MagickPI*alpha));
2776 return(gamma);
2777 }
cristy3ed852e2009-09-05 21:47:34 +00002778 if (LocaleNCompare(expression,"sinh",4) == 0)
2779 {
2780 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2781 exception);
2782 return((MagickRealType) sinh((double) alpha));
2783 }
2784 if (LocaleNCompare(expression,"sin",3) == 0)
2785 {
2786 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2787 exception);
2788 return((MagickRealType) sin((double) alpha));
2789 }
2790 if (LocaleNCompare(expression,"sqrt",4) == 0)
2791 {
2792 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2793 exception);
2794 return((MagickRealType) sqrt((double) alpha));
2795 }
cristy9eeedea2011-11-02 19:04:05 +00002796 if (LocaleNCompare(expression,"squish",6) == 0)
2797 {
2798 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2799 exception);
2800 return((MagickRealType) (1.0/(1.0+exp((double) (4.0*alpha)))));
2801 }
cristy3ed852e2009-09-05 21:47:34 +00002802 if (LocaleCompare(expression,"s") == 0)
2803 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2804 break;
2805 }
2806 case 'T':
2807 case 't':
2808 {
2809 if (LocaleNCompare(expression,"tanh",4) == 0)
2810 {
2811 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2812 exception);
2813 return((MagickRealType) tanh((double) alpha));
2814 }
2815 if (LocaleNCompare(expression,"tan",3) == 0)
2816 {
2817 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2818 exception);
2819 return((MagickRealType) tan((double) alpha));
2820 }
2821 if (LocaleCompare(expression,"Transparent") == 0)
2822 return(0.0);
cristy16788e42010-08-13 13:44:26 +00002823 if (LocaleNCompare(expression,"trunc",5) == 0)
2824 {
2825 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2826 exception);
2827 if (alpha >= 0.0)
2828 return((MagickRealType) floor((double) alpha));
2829 return((MagickRealType) ceil((double) alpha));
2830 }
cristy3ed852e2009-09-05 21:47:34 +00002831 if (LocaleCompare(expression,"t") == 0)
2832 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2833 break;
2834 }
2835 case 'U':
2836 case 'u':
2837 {
2838 if (LocaleCompare(expression,"u") == 0)
2839 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2840 break;
2841 }
2842 case 'V':
2843 case 'v':
2844 {
2845 if (LocaleCompare(expression,"v") == 0)
2846 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2847 break;
2848 }
2849 case 'W':
2850 case 'w':
2851 {
cristy9eeedea2011-11-02 19:04:05 +00002852 if (LocaleNCompare(expression,"while",5) == 0)
2853 {
2854 do
2855 {
2856 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2857 exception);
2858 } while (fabs((double) alpha) >= MagickEpsilon);
2859 return((MagickRealType) *beta);
2860 }
cristy3ed852e2009-09-05 21:47:34 +00002861 if (LocaleCompare(expression,"w") == 0)
2862 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2863 break;
2864 }
2865 case 'Y':
2866 case 'y':
2867 {
2868 if (LocaleCompare(expression,"y") == 0)
2869 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2870 break;
2871 }
2872 case 'Z':
2873 case 'z':
2874 {
2875 if (LocaleCompare(expression,"z") == 0)
2876 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2877 break;
2878 }
2879 default:
2880 break;
2881 }
2882 q=(char *) expression;
cristydbdd0e32011-11-04 23:29:40 +00002883 alpha=InterpretSiPrefixValue(expression,&q);
cristy3ed852e2009-09-05 21:47:34 +00002884 if (q == expression)
2885 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2886 return(alpha);
2887}
2888
cristy7832dc22011-09-05 01:21:53 +00002889MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
cristy3ed852e2009-09-05 21:47:34 +00002890 MagickRealType *alpha,ExceptionInfo *exception)
2891{
2892 MagickBooleanType
2893 status;
2894
cristy541ae572011-08-05 19:08:59 +00002895 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2896 exception);
cristy3ed852e2009-09-05 21:47:34 +00002897 return(status);
2898}
2899
2900MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
2901 MagickRealType *alpha,ExceptionInfo *exception)
2902{
2903 FILE
2904 *file;
2905
2906 MagickBooleanType
2907 status;
2908
2909 file=fx_info->file;
2910 fx_info->file=(FILE *) NULL;
cristy541ae572011-08-05 19:08:59 +00002911 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2912 exception);
cristy3ed852e2009-09-05 21:47:34 +00002913 fx_info->file=file;
2914 return(status);
2915}
2916
cristy7832dc22011-09-05 01:21:53 +00002917MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002918 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002919 MagickRealType *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002920{
2921 MagickRealType
2922 beta;
2923
2924 beta=0.0;
2925 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2926 exception);
2927 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2928}
2929
2930/*
2931%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2932% %
2933% %
2934% %
2935% F x I m a g e %
2936% %
2937% %
2938% %
2939%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2940%
2941% FxImage() applies a mathematical expression to the specified image.
2942%
2943% The format of the FxImage method is:
2944%
2945% Image *FxImage(const Image *image,const char *expression,
2946% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002947%
2948% A description of each parameter follows:
2949%
2950% o image: the image.
2951%
cristy3ed852e2009-09-05 21:47:34 +00002952% o expression: A mathematical expression.
2953%
2954% o exception: return any errors or warnings in this structure.
2955%
2956*/
2957
2958static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2959{
cristybb503372010-05-27 20:51:26 +00002960 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002961 i;
2962
2963 assert(fx_info != (FxInfo **) NULL);
cristybb503372010-05-27 20:51:26 +00002964 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
cristy3ed852e2009-09-05 21:47:34 +00002965 if (fx_info[i] != (FxInfo *) NULL)
2966 fx_info[i]=DestroyFxInfo(fx_info[i]);
cristyb41ee102010-10-04 16:46:15 +00002967 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
cristy3ed852e2009-09-05 21:47:34 +00002968 return(fx_info);
2969}
2970
2971static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2972 ExceptionInfo *exception)
2973{
2974 char
2975 *fx_expression;
2976
2977 FxInfo
2978 **fx_info;
2979
2980 MagickRealType
2981 alpha;
2982
cristybb503372010-05-27 20:51:26 +00002983 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002984 i;
2985
cristybb503372010-05-27 20:51:26 +00002986 size_t
cristy3ed852e2009-09-05 21:47:34 +00002987 number_threads;
2988
2989 number_threads=GetOpenMPMaximumThreads();
cristyb41ee102010-10-04 16:46:15 +00002990 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +00002991 if (fx_info == (FxInfo **) NULL)
2992 return((FxInfo **) NULL);
2993 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2994 if (*expression != '@')
2995 fx_expression=ConstantString(expression);
2996 else
2997 fx_expression=FileToString(expression+1,~0,exception);
cristybb503372010-05-27 20:51:26 +00002998 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +00002999 {
3000 fx_info[i]=AcquireFxInfo(image,fx_expression);
3001 if (fx_info[i] == (FxInfo *) NULL)
3002 return(DestroyFxThreadSet(fx_info));
3003 (void) FxPreprocessExpression(fx_info[i],&alpha,fx_info[i]->exception);
3004 }
3005 fx_expression=DestroyString(fx_expression);
3006 return(fx_info);
3007}
3008
3009MagickExport Image *FxImage(const Image *image,const char *expression,
3010 ExceptionInfo *exception)
3011{
cristy3ed852e2009-09-05 21:47:34 +00003012#define FxImageTag "Fx/Image"
3013
cristyfa112112010-01-04 17:48:07 +00003014 CacheView
cristy79cedc72011-07-25 00:41:15 +00003015 *fx_view,
3016 *image_view;
cristyfa112112010-01-04 17:48:07 +00003017
cristy3ed852e2009-09-05 21:47:34 +00003018 FxInfo
cristyfa112112010-01-04 17:48:07 +00003019 **restrict fx_info;
cristy3ed852e2009-09-05 21:47:34 +00003020
3021 Image
3022 *fx_image;
3023
cristy3ed852e2009-09-05 21:47:34 +00003024 MagickBooleanType
3025 status;
3026
cristybb503372010-05-27 20:51:26 +00003027 MagickOffsetType
3028 progress;
3029
cristy3ed852e2009-09-05 21:47:34 +00003030 MagickRealType
3031 alpha;
3032
cristybb503372010-05-27 20:51:26 +00003033 ssize_t
3034 y;
3035
cristy3ed852e2009-09-05 21:47:34 +00003036 assert(image != (Image *) NULL);
3037 assert(image->signature == MagickSignature);
3038 if (image->debug != MagickFalse)
3039 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy79cedc72011-07-25 00:41:15 +00003040 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00003041 if (fx_image == (Image *) NULL)
3042 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003043 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003044 {
cristy3ed852e2009-09-05 21:47:34 +00003045 fx_image=DestroyImage(fx_image);
3046 return((Image *) NULL);
3047 }
3048 fx_info=AcquireFxThreadSet(image,expression,exception);
3049 if (fx_info == (FxInfo **) NULL)
3050 {
3051 fx_image=DestroyImage(fx_image);
3052 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3053 }
3054 status=FxPreprocessExpression(fx_info[0],&alpha,exception);
3055 if (status == MagickFalse)
3056 {
3057 fx_image=DestroyImage(fx_image);
3058 fx_info=DestroyFxThreadSet(fx_info);
3059 return((Image *) NULL);
3060 }
3061 /*
3062 Fx image.
3063 */
3064 status=MagickTrue;
3065 progress=0;
cristy79cedc72011-07-25 00:41:15 +00003066 image_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00003067 fx_view=AcquireCacheView(fx_image);
cristyb5d5f722009-11-04 03:03:49 +00003068#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003069 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003070#endif
cristybb503372010-05-27 20:51:26 +00003071 for (y=0; y < (ssize_t) fx_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003072 {
cristy5c9e6f22010-09-17 17:31:01 +00003073 const int
3074 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003075
cristy79cedc72011-07-25 00:41:15 +00003076 register const Quantum
3077 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003078
cristy4c08aed2011-07-01 19:47:50 +00003079 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003080 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003081
cristy79cedc72011-07-25 00:41:15 +00003082 register ssize_t
3083 x;
3084
cristy3ed852e2009-09-05 21:47:34 +00003085 if (status == MagickFalse)
3086 continue;
cristy79cedc72011-07-25 00:41:15 +00003087 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy3ed852e2009-09-05 21:47:34 +00003088 q=GetCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
cristy79cedc72011-07-25 00:41:15 +00003089 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003090 {
3091 status=MagickFalse;
3092 continue;
3093 }
cristybb503372010-05-27 20:51:26 +00003094 for (x=0; x < (ssize_t) fx_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003095 {
cristy79cedc72011-07-25 00:41:15 +00003096 register ssize_t
3097 i;
3098
3099 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3100 {
3101 MagickRealType
3102 alpha;
3103
3104 PixelChannel
3105 channel;
3106
3107 PixelTrait
3108 fx_traits,
3109 traits;
3110
cristye2a912b2011-12-05 20:02:07 +00003111 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003112 traits=GetPixelChannelMapTraits(image,channel);
cristy79cedc72011-07-25 00:41:15 +00003113 fx_traits=GetPixelChannelMapTraits(fx_image,channel);
cristy010d7d12011-08-31 01:02:48 +00003114 if ((traits == UndefinedPixelTrait) ||
3115 (fx_traits == UndefinedPixelTrait))
cristy79cedc72011-07-25 00:41:15 +00003116 continue;
3117 if ((fx_traits & CopyPixelTrait) != 0)
3118 {
cristy0beccfa2011-09-25 20:47:53 +00003119 SetPixelChannel(fx_image,channel,p[i],q);
cristy79cedc72011-07-25 00:41:15 +00003120 continue;
3121 }
3122 alpha=0.0;
cristya382aca2011-12-06 18:22:48 +00003123 (void) FxEvaluateChannelExpression(fx_info[id],channel,x,y,&alpha,
3124 exception);
cristyb3a73b52011-07-26 01:34:43 +00003125 q[i]=ClampToQuantum((MagickRealType) QuantumRange*alpha);
cristy79cedc72011-07-25 00:41:15 +00003126 }
3127 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003128 q+=GetPixelChannels(fx_image);
cristy3ed852e2009-09-05 21:47:34 +00003129 }
3130 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3131 status=MagickFalse;
3132 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3133 {
3134 MagickBooleanType
3135 proceed;
3136
cristyb5d5f722009-11-04 03:03:49 +00003137#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy490408a2011-07-07 14:42:05 +00003138 #pragma omp critical (MagickCore_FxImage)
cristy3ed852e2009-09-05 21:47:34 +00003139#endif
3140 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3141 if (proceed == MagickFalse)
3142 status=MagickFalse;
3143 }
3144 }
cristy3ed852e2009-09-05 21:47:34 +00003145 fx_view=DestroyCacheView(fx_view);
cristy79cedc72011-07-25 00:41:15 +00003146 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003147 fx_info=DestroyFxThreadSet(fx_info);
3148 if (status == MagickFalse)
3149 fx_image=DestroyImage(fx_image);
3150 return(fx_image);
3151}
3152
3153/*
3154%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3155% %
3156% %
3157% %
3158% I m p l o d e I m a g e %
3159% %
3160% %
3161% %
3162%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3163%
3164% ImplodeImage() creates a new image that is a copy of an existing
3165% one with the image pixels "implode" by the specified percentage. It
3166% allocates the memory necessary for the new Image structure and returns a
3167% pointer to the new image.
3168%
3169% The format of the ImplodeImage method is:
3170%
3171% Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003172% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003173%
3174% A description of each parameter follows:
3175%
3176% o implode_image: Method ImplodeImage returns a pointer to the image
3177% after it is implode. A null image is returned if there is a memory
3178% shortage.
3179%
3180% o image: the image.
3181%
3182% o amount: Define the extent of the implosion.
3183%
cristy76f512e2011-09-12 01:26:56 +00003184% o method: the pixel interpolation method.
3185%
cristy3ed852e2009-09-05 21:47:34 +00003186% o exception: return any errors or warnings in this structure.
3187%
3188*/
3189MagickExport Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003190 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003191{
3192#define ImplodeImageTag "Implode/Image"
3193
cristyfa112112010-01-04 17:48:07 +00003194 CacheView
3195 *image_view,
3196 *implode_view;
3197
cristy3ed852e2009-09-05 21:47:34 +00003198 Image
3199 *implode_image;
3200
cristy3ed852e2009-09-05 21:47:34 +00003201 MagickBooleanType
3202 status;
3203
cristybb503372010-05-27 20:51:26 +00003204 MagickOffsetType
3205 progress;
3206
cristy3ed852e2009-09-05 21:47:34 +00003207 MagickRealType
3208 radius;
3209
3210 PointInfo
3211 center,
3212 scale;
3213
cristybb503372010-05-27 20:51:26 +00003214 ssize_t
3215 y;
3216
cristy3ed852e2009-09-05 21:47:34 +00003217 /*
3218 Initialize implode image attributes.
3219 */
3220 assert(image != (Image *) NULL);
3221 assert(image->signature == MagickSignature);
3222 if (image->debug != MagickFalse)
3223 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3224 assert(exception != (ExceptionInfo *) NULL);
3225 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00003226 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3227 exception);
cristy3ed852e2009-09-05 21:47:34 +00003228 if (implode_image == (Image *) NULL)
3229 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003230 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003231 {
cristy3ed852e2009-09-05 21:47:34 +00003232 implode_image=DestroyImage(implode_image);
3233 return((Image *) NULL);
3234 }
cristy4c08aed2011-07-01 19:47:50 +00003235 if (implode_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00003236 implode_image->matte=MagickTrue;
3237 /*
3238 Compute scaling factor.
3239 */
3240 scale.x=1.0;
3241 scale.y=1.0;
3242 center.x=0.5*image->columns;
3243 center.y=0.5*image->rows;
3244 radius=center.x;
3245 if (image->columns > image->rows)
3246 scale.y=(double) image->columns/(double) image->rows;
3247 else
3248 if (image->columns < image->rows)
3249 {
3250 scale.x=(double) image->rows/(double) image->columns;
3251 radius=center.y;
3252 }
3253 /*
3254 Implode image.
3255 */
3256 status=MagickTrue;
3257 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00003258 image_view=AcquireCacheView(image);
3259 implode_view=AcquireCacheView(implode_image);
cristyb5d5f722009-11-04 03:03:49 +00003260#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003261 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003262#endif
cristybb503372010-05-27 20:51:26 +00003263 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003264 {
cristy3ed852e2009-09-05 21:47:34 +00003265 MagickRealType
3266 distance;
3267
3268 PointInfo
3269 delta;
3270
cristy6d188022011-09-12 13:23:33 +00003271 register const Quantum
3272 *restrict p;
3273
cristybb503372010-05-27 20:51:26 +00003274 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003275 x;
3276
cristy4c08aed2011-07-01 19:47:50 +00003277 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003278 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003279
3280 if (status == MagickFalse)
3281 continue;
cristy6d188022011-09-12 13:23:33 +00003282 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy3ed852e2009-09-05 21:47:34 +00003283 q=GetCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
3284 exception);
cristy6d188022011-09-12 13:23:33 +00003285 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003286 {
3287 status=MagickFalse;
3288 continue;
3289 }
cristy3ed852e2009-09-05 21:47:34 +00003290 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00003291 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003292 {
cristy6d188022011-09-12 13:23:33 +00003293 register ssize_t
3294 i;
3295
cristy3ed852e2009-09-05 21:47:34 +00003296 /*
3297 Determine if the pixel is within an ellipse.
3298 */
3299 delta.x=scale.x*(double) (x-center.x);
3300 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00003301 if (distance >= (radius*radius))
3302 {
3303 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3304 q[i]=p[i];
3305 }
3306 else
cristy3ed852e2009-09-05 21:47:34 +00003307 {
3308 double
3309 factor;
3310
3311 /*
3312 Implode the pixel.
3313 */
3314 factor=1.0;
3315 if (distance > 0.0)
3316 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/
3317 radius/2)),-amount);
cristy76f512e2011-09-12 01:26:56 +00003318 status=InterpolatePixelChannels(image,image_view,implode_image,method,
3319 (double) (factor*delta.x/scale.x+center.x),(double) (factor*delta.y/
3320 scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00003321 }
cristy6d188022011-09-12 13:23:33 +00003322 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003323 q+=GetPixelChannels(implode_image);
cristy3ed852e2009-09-05 21:47:34 +00003324 }
3325 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3326 status=MagickFalse;
3327 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3328 {
3329 MagickBooleanType
3330 proceed;
3331
cristyb5d5f722009-11-04 03:03:49 +00003332#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003333 #pragma omp critical (MagickCore_ImplodeImage)
3334#endif
3335 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3336 if (proceed == MagickFalse)
3337 status=MagickFalse;
3338 }
3339 }
3340 implode_view=DestroyCacheView(implode_view);
3341 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003342 if (status == MagickFalse)
3343 implode_image=DestroyImage(implode_image);
3344 return(implode_image);
3345}
3346
3347/*
3348%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3349% %
3350% %
3351% %
3352% M o r p h I m a g e s %
3353% %
3354% %
3355% %
3356%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3357%
3358% The MorphImages() method requires a minimum of two images. The first
3359% image is transformed into the second by a number of intervening images
3360% as specified by frames.
3361%
3362% The format of the MorphImage method is:
3363%
cristybb503372010-05-27 20:51:26 +00003364% Image *MorphImages(const Image *image,const size_t number_frames,
cristy3ed852e2009-09-05 21:47:34 +00003365% ExceptionInfo *exception)
3366%
3367% A description of each parameter follows:
3368%
3369% o image: the image.
3370%
3371% o number_frames: Define the number of in-between image to generate.
3372% The more in-between frames, the smoother the morph.
3373%
3374% o exception: return any errors or warnings in this structure.
3375%
3376*/
3377MagickExport Image *MorphImages(const Image *image,
cristybb503372010-05-27 20:51:26 +00003378 const size_t number_frames,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003379{
3380#define MorphImageTag "Morph/Image"
3381
3382 Image
3383 *morph_image,
3384 *morph_images;
3385
cristy9d314ff2011-03-09 01:30:28 +00003386 MagickBooleanType
3387 status;
cristy3ed852e2009-09-05 21:47:34 +00003388
3389 MagickOffsetType
3390 scene;
3391
3392 MagickRealType
3393 alpha,
3394 beta;
3395
3396 register const Image
3397 *next;
3398
cristybb503372010-05-27 20:51:26 +00003399 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003400 i;
3401
cristy9d314ff2011-03-09 01:30:28 +00003402 ssize_t
3403 y;
cristy3ed852e2009-09-05 21:47:34 +00003404
3405 /*
3406 Clone first frame in sequence.
3407 */
3408 assert(image != (Image *) NULL);
3409 assert(image->signature == MagickSignature);
3410 if (image->debug != MagickFalse)
3411 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3412 assert(exception != (ExceptionInfo *) NULL);
3413 assert(exception->signature == MagickSignature);
3414 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3415 if (morph_images == (Image *) NULL)
3416 return((Image *) NULL);
3417 if (GetNextImageInList(image) == (Image *) NULL)
3418 {
3419 /*
3420 Morph single image.
3421 */
cristybb503372010-05-27 20:51:26 +00003422 for (i=1; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003423 {
3424 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3425 if (morph_image == (Image *) NULL)
3426 {
3427 morph_images=DestroyImageList(morph_images);
3428 return((Image *) NULL);
3429 }
3430 AppendImageToList(&morph_images,morph_image);
cristy8b27a6d2010-02-14 03:31:15 +00003431 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003432 {
cristy8b27a6d2010-02-14 03:31:15 +00003433 MagickBooleanType
3434 proceed;
3435
cristybb503372010-05-27 20:51:26 +00003436 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3437 number_frames);
cristy8b27a6d2010-02-14 03:31:15 +00003438 if (proceed == MagickFalse)
3439 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00003440 }
3441 }
3442 return(GetFirstImageInList(morph_images));
3443 }
3444 /*
3445 Morph image sequence.
3446 */
3447 status=MagickTrue;
3448 scene=0;
3449 next=image;
3450 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3451 {
cristybb503372010-05-27 20:51:26 +00003452 for (i=0; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003453 {
3454 CacheView
3455 *image_view,
3456 *morph_view;
3457
3458 beta=(MagickRealType) (i+1.0)/(MagickRealType) (number_frames+1.0);
3459 alpha=1.0-beta;
cristy15b98cd2010-09-12 19:42:50 +00003460 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
cristybb503372010-05-27 20:51:26 +00003461 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*
cristy15b98cd2010-09-12 19:42:50 +00003462 next->rows+beta*GetNextImageInList(next)->rows+0.5),
3463 next->filter,next->blur,exception);
cristy3ed852e2009-09-05 21:47:34 +00003464 if (morph_image == (Image *) NULL)
3465 {
3466 morph_images=DestroyImageList(morph_images);
3467 return((Image *) NULL);
3468 }
cristy574cc262011-08-05 01:23:58 +00003469 if (SetImageStorageClass(morph_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003470 {
cristy3ed852e2009-09-05 21:47:34 +00003471 morph_image=DestroyImage(morph_image);
3472 return((Image *) NULL);
3473 }
3474 AppendImageToList(&morph_images,morph_image);
3475 morph_images=GetLastImageInList(morph_images);
cristy15b98cd2010-09-12 19:42:50 +00003476 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
3477 morph_images->rows,GetNextImageInList(next)->filter,
3478 GetNextImageInList(next)->blur,exception);
cristy3ed852e2009-09-05 21:47:34 +00003479 if (morph_image == (Image *) NULL)
3480 {
3481 morph_images=DestroyImageList(morph_images);
3482 return((Image *) NULL);
3483 }
3484 image_view=AcquireCacheView(morph_image);
3485 morph_view=AcquireCacheView(morph_images);
cristyb5d5f722009-11-04 03:03:49 +00003486#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003487 #pragma omp parallel for schedule(static,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +00003488#endif
cristybb503372010-05-27 20:51:26 +00003489 for (y=0; y < (ssize_t) morph_images->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003490 {
3491 MagickBooleanType
3492 sync;
3493
cristy4c08aed2011-07-01 19:47:50 +00003494 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003495 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003496
cristybb503372010-05-27 20:51:26 +00003497 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003498 x;
3499
cristy4c08aed2011-07-01 19:47:50 +00003500 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003501 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003502
3503 if (status == MagickFalse)
3504 continue;
3505 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3506 exception);
3507 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3508 exception);
cristy4c08aed2011-07-01 19:47:50 +00003509 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003510 {
3511 status=MagickFalse;
3512 continue;
3513 }
cristybb503372010-05-27 20:51:26 +00003514 for (x=0; x < (ssize_t) morph_images->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003515 {
cristy4c08aed2011-07-01 19:47:50 +00003516 SetPixelRed(morph_images,ClampToQuantum(alpha*
3517 GetPixelRed(morph_images,q)+beta*GetPixelRed(morph_image,p)),q);
3518 SetPixelGreen(morph_images,ClampToQuantum(alpha*
3519 GetPixelGreen(morph_images,q)+beta*GetPixelGreen(morph_image,p)),q);
3520 SetPixelBlue(morph_images,ClampToQuantum(alpha*
3521 GetPixelBlue(morph_images,q)+beta*GetPixelBlue(morph_image,p)),q);
3522 SetPixelAlpha(morph_images,ClampToQuantum(alpha*
3523 GetPixelAlpha(morph_images,q)+beta*GetPixelAlpha(morph_image,p)),q);
3524 if ((morph_image->colorspace == CMYKColorspace) &&
3525 (morph_images->colorspace == CMYKColorspace))
3526 SetPixelBlack(morph_images,ClampToQuantum(alpha*
3527 GetPixelBlack(morph_images,q)+beta*GetPixelBlack(morph_image,p)),
3528 q);
cristyed231572011-07-14 02:18:59 +00003529 p+=GetPixelChannels(morph_image);
3530 q+=GetPixelChannels(morph_images);
cristy3ed852e2009-09-05 21:47:34 +00003531 }
3532 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3533 if (sync == MagickFalse)
3534 status=MagickFalse;
3535 }
3536 morph_view=DestroyCacheView(morph_view);
3537 image_view=DestroyCacheView(image_view);
3538 morph_image=DestroyImage(morph_image);
3539 }
cristybb503372010-05-27 20:51:26 +00003540 if (i < (ssize_t) number_frames)
cristy3ed852e2009-09-05 21:47:34 +00003541 break;
3542 /*
3543 Clone last frame in sequence.
3544 */
3545 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3546 if (morph_image == (Image *) NULL)
3547 {
3548 morph_images=DestroyImageList(morph_images);
3549 return((Image *) NULL);
3550 }
3551 AppendImageToList(&morph_images,morph_image);
3552 morph_images=GetLastImageInList(morph_images);
3553 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3554 {
3555 MagickBooleanType
3556 proceed;
3557
cristyb5d5f722009-11-04 03:03:49 +00003558#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003559 #pragma omp critical (MagickCore_MorphImages)
3560#endif
3561 proceed=SetImageProgress(image,MorphImageTag,scene,
3562 GetImageListLength(image));
3563 if (proceed == MagickFalse)
3564 status=MagickFalse;
3565 }
3566 scene++;
3567 }
3568 if (GetNextImageInList(next) != (Image *) NULL)
3569 {
3570 morph_images=DestroyImageList(morph_images);
3571 return((Image *) NULL);
3572 }
3573 return(GetFirstImageInList(morph_images));
3574}
3575
3576/*
3577%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3578% %
3579% %
3580% %
3581% P l a s m a I m a g e %
3582% %
3583% %
3584% %
3585%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3586%
3587% PlasmaImage() initializes an image with plasma fractal values. The image
3588% must be initialized with a base color and the random number generator
3589% seeded before this method is called.
3590%
3591% The format of the PlasmaImage method is:
3592%
3593% MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
cristy5cbc0162011-08-29 00:36:28 +00003594% size_t attenuate,size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003595%
3596% A description of each parameter follows:
3597%
3598% o image: the image.
3599%
3600% o segment: Define the region to apply plasma fractals values.
3601%
glennrp7dae1ca2010-09-16 12:17:35 +00003602% o attenuate: Define the plasma attenuation factor.
cristy3ed852e2009-09-05 21:47:34 +00003603%
3604% o depth: Limit the plasma recursion depth.
3605%
cristy5cbc0162011-08-29 00:36:28 +00003606% o exception: return any errors or warnings in this structure.
3607%
cristy3ed852e2009-09-05 21:47:34 +00003608*/
3609
3610static inline Quantum PlasmaPixel(RandomInfo *random_info,
3611 const MagickRealType pixel,const MagickRealType noise)
3612{
3613 Quantum
3614 plasma;
3615
cristyce70c172010-01-07 17:15:30 +00003616 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
cristy3ed852e2009-09-05 21:47:34 +00003617 noise/2.0);
3618 return(plasma);
3619}
3620
cristyda1f9c12011-10-02 21:39:49 +00003621static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3622 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3623 const SegmentInfo *segment,size_t attenuate,size_t depth,
3624 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003625{
cristy3ed852e2009-09-05 21:47:34 +00003626 MagickRealType
3627 plasma;
3628
cristyda1f9c12011-10-02 21:39:49 +00003629 PixelChannel
3630 channel;
3631
3632 PixelTrait
3633 traits;
3634
3635 register const Quantum
3636 *restrict u,
3637 *restrict v;
3638
3639 register Quantum
3640 *restrict q;
3641
3642 register ssize_t
3643 i;
cristy3ed852e2009-09-05 21:47:34 +00003644
cristy9d314ff2011-03-09 01:30:28 +00003645 ssize_t
3646 x,
3647 x_mid,
3648 y,
3649 y_mid;
3650
cristy3ed852e2009-09-05 21:47:34 +00003651 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3652 return(MagickTrue);
3653 if (depth != 0)
3654 {
3655 SegmentInfo
3656 local_info;
3657
3658 /*
3659 Divide the area into quadrants and recurse.
3660 */
3661 depth--;
3662 attenuate++;
cristybb503372010-05-27 20:51:26 +00003663 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3664 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003665 local_info=(*segment);
3666 local_info.x2=(double) x_mid;
3667 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003668 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3669 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003670 local_info=(*segment);
3671 local_info.y1=(double) y_mid;
3672 local_info.x2=(double) x_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.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003678 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3679 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003680 local_info=(*segment);
3681 local_info.x1=(double) x_mid;
3682 local_info.y1=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003683 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3684 &local_info,attenuate,depth,exception));
cristy3ed852e2009-09-05 21:47:34 +00003685 }
cristybb503372010-05-27 20:51:26 +00003686 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3687 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003688 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3689 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3690 return(MagickFalse);
3691 /*
3692 Average pixels and apply plasma.
3693 */
cristy3ed852e2009-09-05 21:47:34 +00003694 plasma=(MagickRealType) QuantumRange/(2.0*attenuate);
3695 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3696 {
cristy3ed852e2009-09-05 21:47:34 +00003697 /*
3698 Left pixel.
3699 */
cristybb503372010-05-27 20:51:26 +00003700 x=(ssize_t) ceil(segment->x1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003701 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3702 1,1,exception);
3703 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3704 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003705 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003706 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3707 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003708 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003709 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3710 {
cristye2a912b2011-12-05 20:02:07 +00003711 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003712 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003713 if (traits == UndefinedPixelTrait)
3714 continue;
3715 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3716 }
cristyc5c6f662010-09-22 14:23:02 +00003717 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003718 if (segment->x1 != segment->x2)
3719 {
3720 /*
3721 Right pixel.
3722 */
cristybb503372010-05-27 20:51:26 +00003723 x=(ssize_t) ceil(segment->x2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003724 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3725 1,1,exception);
3726 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3727 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003728 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003729 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3730 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003731 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003732 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3733 {
cristye2a912b2011-12-05 20:02:07 +00003734 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003735 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003736 if (traits == UndefinedPixelTrait)
3737 continue;
3738 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3739 }
cristyc5c6f662010-09-22 14:23:02 +00003740 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003741 }
3742 }
3743 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3744 {
3745 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3746 {
cristy3ed852e2009-09-05 21:47:34 +00003747 /*
3748 Bottom pixel.
3749 */
cristybb503372010-05-27 20:51:26 +00003750 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003751 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3752 1,1,exception);
3753 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3754 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003755 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003756 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3757 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003758 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003759 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3760 {
cristye2a912b2011-12-05 20:02:07 +00003761 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003762 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003763 if (traits == UndefinedPixelTrait)
3764 continue;
3765 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3766 }
cristyc5c6f662010-09-22 14:23:02 +00003767 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003768 }
3769 if (segment->y1 != segment->y2)
3770 {
cristy3ed852e2009-09-05 21:47:34 +00003771 /*
3772 Top pixel.
3773 */
cristybb503372010-05-27 20:51:26 +00003774 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003775 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3776 1,1,exception);
3777 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3778 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003779 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003780 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3781 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003782 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003783 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3784 {
cristye2a912b2011-12-05 20:02:07 +00003785 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003786 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003787 if (traits == UndefinedPixelTrait)
3788 continue;
3789 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3790 }
cristyc5c6f662010-09-22 14:23:02 +00003791 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003792 }
3793 }
3794 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3795 {
cristy3ed852e2009-09-05 21:47:34 +00003796 /*
3797 Middle pixel.
3798 */
cristybb503372010-05-27 20:51:26 +00003799 x=(ssize_t) ceil(segment->x1-0.5);
3800 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003801 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
cristybb503372010-05-27 20:51:26 +00003802 x=(ssize_t) ceil(segment->x2-0.5);
3803 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003804 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003805 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003806 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3807 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003808 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003809 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3810 {
cristye2a912b2011-12-05 20:02:07 +00003811 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003812 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003813 if (traits == UndefinedPixelTrait)
3814 continue;
3815 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3816 }
cristyc5c6f662010-09-22 14:23:02 +00003817 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003818 }
3819 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3820 return(MagickTrue);
3821 return(MagickFalse);
3822}
cristyda1f9c12011-10-02 21:39:49 +00003823
cristy3ed852e2009-09-05 21:47:34 +00003824MagickExport MagickBooleanType PlasmaImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00003825 const SegmentInfo *segment,size_t attenuate,size_t depth,
3826 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003827{
cristyc5c6f662010-09-22 14:23:02 +00003828 CacheView
cristyda1f9c12011-10-02 21:39:49 +00003829 *image_view,
3830 *u_view,
3831 *v_view;
cristyc5c6f662010-09-22 14:23:02 +00003832
cristy3ed852e2009-09-05 21:47:34 +00003833 MagickBooleanType
3834 status;
3835
3836 RandomInfo
3837 *random_info;
3838
3839 if (image->debug != MagickFalse)
3840 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3841 assert(image != (Image *) NULL);
3842 assert(image->signature == MagickSignature);
3843 if (image->debug != MagickFalse)
3844 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy5cbc0162011-08-29 00:36:28 +00003845 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristyc5c6f662010-09-22 14:23:02 +00003846 return(MagickFalse);
3847 image_view=AcquireCacheView(image);
cristyda1f9c12011-10-02 21:39:49 +00003848 u_view=AcquireCacheView(image);
3849 v_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00003850 random_info=AcquireRandomInfo();
cristyda1f9c12011-10-02 21:39:49 +00003851 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3852 attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003853 random_info=DestroyRandomInfo(random_info);
cristyda1f9c12011-10-02 21:39:49 +00003854 v_view=DestroyCacheView(v_view);
3855 u_view=DestroyCacheView(u_view);
cristyc5c6f662010-09-22 14:23:02 +00003856 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003857 return(status);
3858}
3859
3860/*
3861%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3862% %
3863% %
3864% %
3865% P o l a r o i d I m a g e %
3866% %
3867% %
3868% %
3869%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3870%
3871% PolaroidImage() simulates a Polaroid picture.
3872%
3873% The format of the AnnotateImage method is:
3874%
3875% Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003876% const char *caption,const double angle,
3877% const PixelInterpolateMethod method,ExceptionInfo exception)
cristy3ed852e2009-09-05 21:47:34 +00003878%
3879% A description of each parameter follows:
3880%
3881% o image: the image.
3882%
3883% o draw_info: the draw info.
3884%
cristye9e3d382011-12-14 01:50:13 +00003885% o caption: the Polaroid caption.
3886%
cristycee97112010-05-28 00:44:52 +00003887% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00003888%
cristy5c4e2582011-09-11 19:21:03 +00003889% o method: the pixel interpolation method.
3890%
cristy3ed852e2009-09-05 21:47:34 +00003891% o exception: return any errors or warnings in this structure.
3892%
3893*/
3894MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003895 const char *caption,const double angle,const PixelInterpolateMethod method,
cristy5c4e2582011-09-11 19:21:03 +00003896 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003897{
cristy3ed852e2009-09-05 21:47:34 +00003898 Image
3899 *bend_image,
3900 *caption_image,
3901 *flop_image,
3902 *picture_image,
3903 *polaroid_image,
3904 *rotate_image,
3905 *trim_image;
3906
cristybb503372010-05-27 20:51:26 +00003907 size_t
cristy3ed852e2009-09-05 21:47:34 +00003908 height;
3909
cristy9d314ff2011-03-09 01:30:28 +00003910 ssize_t
3911 quantum;
3912
cristy3ed852e2009-09-05 21:47:34 +00003913 /*
3914 Simulate a Polaroid picture.
3915 */
3916 assert(image != (Image *) NULL);
3917 assert(image->signature == MagickSignature);
3918 if (image->debug != MagickFalse)
3919 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3920 assert(exception != (ExceptionInfo *) NULL);
3921 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00003922 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
cristy3ed852e2009-09-05 21:47:34 +00003923 image->rows)/25.0,10.0);
3924 height=image->rows+2*quantum;
3925 caption_image=(Image *) NULL;
cristye9e3d382011-12-14 01:50:13 +00003926 if (caption != (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003927 {
3928 char
cristye9e3d382011-12-14 01:50:13 +00003929 geometry[MaxTextExtent],
3930 *text;
cristy3ed852e2009-09-05 21:47:34 +00003931
3932 DrawInfo
3933 *annotate_info;
3934
cristy3ed852e2009-09-05 21:47:34 +00003935 MagickBooleanType
3936 status;
3937
cristy9d314ff2011-03-09 01:30:28 +00003938 ssize_t
3939 count;
3940
cristy3ed852e2009-09-05 21:47:34 +00003941 TypeMetric
3942 metrics;
3943
3944 /*
3945 Generate caption image.
3946 */
3947 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3948 if (caption_image == (Image *) NULL)
3949 return((Image *) NULL);
3950 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
cristye9e3d382011-12-14 01:50:13 +00003951 text=InterpretImageProperties((ImageInfo *) NULL,(Image *) image,caption,
3952 exception);
3953 (void) CloneString(&annotate_info->text,text);
cristy6b1d05e2010-09-22 19:17:27 +00003954 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
cristye9e3d382011-12-14 01:50:13 +00003955 &text,exception);
3956 status=SetImageExtent(caption_image,image->columns,(size_t) ((count+1)*
3957 (metrics.ascent-metrics.descent)+0.5),exception);
cristy3ed852e2009-09-05 21:47:34 +00003958 if (status == MagickFalse)
3959 caption_image=DestroyImage(caption_image);
3960 else
3961 {
3962 caption_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003963 (void) SetImageBackgroundColor(caption_image,exception);
cristye9e3d382011-12-14 01:50:13 +00003964 (void) CloneString(&annotate_info->text,text);
cristyb51dff52011-05-19 16:55:47 +00003965 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
cristy3ed852e2009-09-05 21:47:34 +00003966 metrics.ascent);
3967 if (annotate_info->gravity == UndefinedGravity)
3968 (void) CloneString(&annotate_info->geometry,AcquireString(
3969 geometry));
cristy5cbc0162011-08-29 00:36:28 +00003970 (void) AnnotateImage(caption_image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003971 height+=caption_image->rows;
3972 }
3973 annotate_info=DestroyDrawInfo(annotate_info);
cristye9e3d382011-12-14 01:50:13 +00003974 text=DestroyString(text);
cristy3ed852e2009-09-05 21:47:34 +00003975 }
3976 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
3977 exception);
3978 if (picture_image == (Image *) NULL)
3979 {
3980 if (caption_image != (Image *) NULL)
3981 caption_image=DestroyImage(caption_image);
3982 return((Image *) NULL);
3983 }
3984 picture_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003985 (void) SetImageBackgroundColor(picture_image,exception);
cristye941a752011-10-15 01:52:48 +00003986 (void) CompositeImage(picture_image,OverCompositeOp,image,quantum,quantum,
3987 exception);
cristy3ed852e2009-09-05 21:47:34 +00003988 if (caption_image != (Image *) NULL)
3989 {
3990 (void) CompositeImage(picture_image,OverCompositeOp,caption_image,
cristye941a752011-10-15 01:52:48 +00003991 quantum,(ssize_t) (image->rows+3*quantum/2),exception);
cristy3ed852e2009-09-05 21:47:34 +00003992 caption_image=DestroyImage(caption_image);
3993 }
cristy9950d572011-10-01 18:22:35 +00003994 (void) QueryColorCompliance("none",AllCompliance,
3995 &picture_image->background_color,exception);
cristy63240882011-08-05 19:05:27 +00003996 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00003997 rotate_image=RotateImage(picture_image,90.0,exception);
3998 picture_image=DestroyImage(picture_image);
3999 if (rotate_image == (Image *) NULL)
4000 return((Image *) NULL);
4001 picture_image=rotate_image;
4002 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
cristy5c4e2582011-09-11 19:21:03 +00004003 picture_image->columns,method,exception);
cristy3ed852e2009-09-05 21:47:34 +00004004 picture_image=DestroyImage(picture_image);
4005 if (bend_image == (Image *) NULL)
4006 return((Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00004007 picture_image=bend_image;
4008 rotate_image=RotateImage(picture_image,-90.0,exception);
4009 picture_image=DestroyImage(picture_image);
4010 if (rotate_image == (Image *) NULL)
4011 return((Image *) NULL);
4012 picture_image=rotate_image;
4013 picture_image->background_color=image->background_color;
cristy78ec1a92011-12-09 09:25:34 +00004014 polaroid_image=ShadowImage(picture_image,80.0,2.0,0.0,quantum/3,quantum/3,
cristy3ed852e2009-09-05 21:47:34 +00004015 exception);
4016 if (polaroid_image == (Image *) NULL)
4017 {
4018 picture_image=DestroyImage(picture_image);
4019 return(picture_image);
4020 }
4021 flop_image=FlopImage(polaroid_image,exception);
4022 polaroid_image=DestroyImage(polaroid_image);
4023 if (flop_image == (Image *) NULL)
4024 {
4025 picture_image=DestroyImage(picture_image);
4026 return(picture_image);
4027 }
4028 polaroid_image=flop_image;
4029 (void) CompositeImage(polaroid_image,OverCompositeOp,picture_image,
cristye941a752011-10-15 01:52:48 +00004030 (ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
cristy3ed852e2009-09-05 21:47:34 +00004031 picture_image=DestroyImage(picture_image);
cristy9950d572011-10-01 18:22:35 +00004032 (void) QueryColorCompliance("none",AllCompliance,
4033 &polaroid_image->background_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00004034 rotate_image=RotateImage(polaroid_image,angle,exception);
4035 polaroid_image=DestroyImage(polaroid_image);
4036 if (rotate_image == (Image *) NULL)
4037 return((Image *) NULL);
4038 polaroid_image=rotate_image;
4039 trim_image=TrimImage(polaroid_image,exception);
4040 polaroid_image=DestroyImage(polaroid_image);
4041 if (trim_image == (Image *) NULL)
4042 return((Image *) NULL);
4043 polaroid_image=trim_image;
4044 return(polaroid_image);
4045}
4046
4047/*
4048%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4049% %
4050% %
4051% %
cristy3ed852e2009-09-05 21:47:34 +00004052% S e p i a T o n e I m a g e %
4053% %
4054% %
4055% %
4056%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4057%
4058% MagickSepiaToneImage() applies a special effect to the image, similar to the
4059% effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4060% 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4061% threshold of 80% is a good starting point for a reasonable tone.
4062%
4063% The format of the SepiaToneImage method is:
4064%
4065% Image *SepiaToneImage(const Image *image,const double threshold,
4066% ExceptionInfo *exception)
4067%
4068% A description of each parameter follows:
4069%
4070% o image: the image.
4071%
4072% o threshold: the tone threshold.
4073%
4074% o exception: return any errors or warnings in this structure.
4075%
4076*/
4077MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4078 ExceptionInfo *exception)
4079{
4080#define SepiaToneImageTag "SepiaTone/Image"
4081
cristyc4c8d132010-01-07 01:58:38 +00004082 CacheView
4083 *image_view,
4084 *sepia_view;
4085
cristy3ed852e2009-09-05 21:47:34 +00004086 Image
4087 *sepia_image;
4088
cristy3ed852e2009-09-05 21:47:34 +00004089 MagickBooleanType
4090 status;
4091
cristybb503372010-05-27 20:51:26 +00004092 MagickOffsetType
4093 progress;
4094
4095 ssize_t
4096 y;
4097
cristy3ed852e2009-09-05 21:47:34 +00004098 /*
4099 Initialize sepia-toned image attributes.
4100 */
4101 assert(image != (const Image *) NULL);
4102 assert(image->signature == MagickSignature);
4103 if (image->debug != MagickFalse)
4104 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4105 assert(exception != (ExceptionInfo *) NULL);
4106 assert(exception->signature == MagickSignature);
4107 sepia_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
4108 if (sepia_image == (Image *) NULL)
4109 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004110 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004111 {
cristy3ed852e2009-09-05 21:47:34 +00004112 sepia_image=DestroyImage(sepia_image);
4113 return((Image *) NULL);
4114 }
4115 /*
4116 Tone each row of the image.
4117 */
4118 status=MagickTrue;
4119 progress=0;
4120 image_view=AcquireCacheView(image);
4121 sepia_view=AcquireCacheView(sepia_image);
cristyb5d5f722009-11-04 03:03:49 +00004122#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00004123 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004124#endif
cristybb503372010-05-27 20:51:26 +00004125 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004126 {
cristy4c08aed2011-07-01 19:47:50 +00004127 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004128 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004129
cristybb503372010-05-27 20:51:26 +00004130 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004131 x;
4132
cristy4c08aed2011-07-01 19:47:50 +00004133 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004134 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004135
4136 if (status == MagickFalse)
4137 continue;
4138 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
4139 q=QueueCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
4140 exception);
cristy4c08aed2011-07-01 19:47:50 +00004141 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004142 {
4143 status=MagickFalse;
4144 continue;
4145 }
cristybb503372010-05-27 20:51:26 +00004146 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004147 {
4148 MagickRealType
4149 intensity,
4150 tone;
4151
cristy4c08aed2011-07-01 19:47:50 +00004152 intensity=(MagickRealType) GetPixelIntensity(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004153 tone=intensity > threshold ? (MagickRealType) QuantumRange : intensity+
4154 (MagickRealType) QuantumRange-threshold;
cristy4c08aed2011-07-01 19:47:50 +00004155 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004156 tone=intensity > (7.0*threshold/6.0) ? (MagickRealType) QuantumRange :
4157 intensity+(MagickRealType) QuantumRange-7.0*threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004158 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004159 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004160 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004161 tone=threshold/7.0;
cristy4c08aed2011-07-01 19:47:50 +00004162 if ((MagickRealType) GetPixelGreen(image,q) < tone)
4163 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4164 if ((MagickRealType) GetPixelBlue(image,q) < tone)
4165 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristyed231572011-07-14 02:18:59 +00004166 p+=GetPixelChannels(image);
4167 q+=GetPixelChannels(sepia_image);
cristy3ed852e2009-09-05 21:47:34 +00004168 }
4169 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4170 status=MagickFalse;
4171 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4172 {
4173 MagickBooleanType
4174 proceed;
4175
cristyb5d5f722009-11-04 03:03:49 +00004176#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00004177 #pragma omp critical (MagickCore_SepiaToneImage)
4178#endif
4179 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4180 image->rows);
4181 if (proceed == MagickFalse)
4182 status=MagickFalse;
4183 }
4184 }
4185 sepia_view=DestroyCacheView(sepia_view);
4186 image_view=DestroyCacheView(image_view);
cristye23ec9d2011-08-16 18:15:40 +00004187 (void) NormalizeImage(sepia_image,exception);
4188 (void) ContrastImage(sepia_image,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004189 if (status == MagickFalse)
4190 sepia_image=DestroyImage(sepia_image);
4191 return(sepia_image);
4192}
4193
4194/*
4195%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4196% %
4197% %
4198% %
4199% S h a d o w I m a g e %
4200% %
4201% %
4202% %
4203%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4204%
4205% ShadowImage() simulates a shadow from the specified image and returns it.
4206%
4207% The format of the ShadowImage method is:
4208%
cristy70cddf72011-12-10 22:42:42 +00004209% Image *ShadowImage(const Image *image,const double alpha,
cristyeb6e6582011-12-09 09:14:23 +00004210% const double sigma,const double bias,const ssize_t x_offset,
4211% const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004212%
4213% A description of each parameter follows:
4214%
4215% o image: the image.
4216%
cristy70cddf72011-12-10 22:42:42 +00004217% o alpha: percentage transparency.
cristy3ed852e2009-09-05 21:47:34 +00004218%
4219% o sigma: the standard deviation of the Gaussian, in pixels.
4220%
cristyeb6e6582011-12-09 09:14:23 +00004221% o bias: the bias.
4222%
cristy3ed852e2009-09-05 21:47:34 +00004223% o x_offset: the shadow x-offset.
4224%
4225% o y_offset: the shadow y-offset.
4226%
4227% o exception: return any errors or warnings in this structure.
4228%
4229*/
cristy70cddf72011-12-10 22:42:42 +00004230MagickExport Image *ShadowImage(const Image *image,const double alpha,
cristyeb6e6582011-12-09 09:14:23 +00004231 const double sigma,const double bias,const ssize_t x_offset,
4232 const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004233{
4234#define ShadowImageTag "Shadow/Image"
4235
cristy70cddf72011-12-10 22:42:42 +00004236 CacheView
4237 *image_view;
4238
cristybd5a96c2011-08-21 00:04:26 +00004239 ChannelType
4240 channel_mask;
4241
cristy3ed852e2009-09-05 21:47:34 +00004242 Image
4243 *border_image,
4244 *clone_image,
4245 *shadow_image;
4246
cristy70cddf72011-12-10 22:42:42 +00004247 MagickBooleanType
4248 status;
4249
cristy3ed852e2009-09-05 21:47:34 +00004250 RectangleInfo
4251 border_info;
4252
cristy70cddf72011-12-10 22:42:42 +00004253 ssize_t
4254 y;
4255
cristy3ed852e2009-09-05 21:47:34 +00004256 assert(image != (Image *) NULL);
4257 assert(image->signature == MagickSignature);
4258 if (image->debug != MagickFalse)
4259 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4260 assert(exception != (ExceptionInfo *) NULL);
4261 assert(exception->signature == MagickSignature);
4262 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4263 if (clone_image == (Image *) NULL)
4264 return((Image *) NULL);
4265 (void) SetImageVirtualPixelMethod(clone_image,EdgeVirtualPixelMethod);
cristybb503372010-05-27 20:51:26 +00004266 border_info.width=(size_t) floor(2.0*sigma+0.5);
4267 border_info.height=(size_t) floor(2.0*sigma+0.5);
cristy3ed852e2009-09-05 21:47:34 +00004268 border_info.x=0;
4269 border_info.y=0;
cristy9950d572011-10-01 18:22:35 +00004270 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4271 exception);
cristy70cddf72011-12-10 22:42:42 +00004272 clone_image->matte=MagickTrue;
4273 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
cristy3ed852e2009-09-05 21:47:34 +00004274 clone_image=DestroyImage(clone_image);
4275 if (border_image == (Image *) NULL)
4276 return((Image *) NULL);
4277 if (border_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00004278 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004279 /*
4280 Shadow image.
4281 */
cristy70cddf72011-12-10 22:42:42 +00004282 status=MagickTrue;
4283 image_view=AcquireCacheView(border_image);
4284 for (y=0; y < (ssize_t) border_image->rows; y++)
4285 {
4286 PixelInfo
4287 background_color;
4288
4289 register Quantum
4290 *restrict q;
4291
4292 register ssize_t
4293 x;
4294
4295 if (status == MagickFalse)
4296 continue;
4297 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
4298 exception);
4299 if (q == (Quantum *) NULL)
4300 {
4301 status=MagickFalse;
4302 continue;
4303 }
4304 background_color=border_image->background_color;
4305 background_color.matte=MagickTrue;
4306 for (x=0; x < (ssize_t) border_image->columns; x++)
4307 {
4308 if (border_image->matte != MagickFalse)
4309 background_color.alpha=GetPixelAlpha(border_image,q)*alpha/100.0;
4310 SetPixelInfoPixel(border_image,&background_color,q);
4311 q+=GetPixelChannels(border_image);
4312 }
4313 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4314 status=MagickFalse;
4315 }
4316 image_view=DestroyCacheView(image_view);
4317 if (status == MagickFalse)
4318 {
4319 border_image=DestroyImage(border_image);
4320 return((Image *) NULL);
4321 }
cristybd5a96c2011-08-21 00:04:26 +00004322 channel_mask=SetPixelChannelMask(border_image,AlphaChannel);
cristyb27bb772011-12-11 16:12:50 +00004323 shadow_image=BlurImage(border_image,0.0,sigma,bias,exception);
cristy3ed852e2009-09-05 21:47:34 +00004324 border_image=DestroyImage(border_image);
4325 if (shadow_image == (Image *) NULL)
4326 return((Image *) NULL);
cristyae1969f2011-12-10 03:07:36 +00004327 (void) SetPixelChannelMapMask(shadow_image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00004328 if (shadow_image->page.width == 0)
4329 shadow_image->page.width=shadow_image->columns;
4330 if (shadow_image->page.height == 0)
4331 shadow_image->page.height=shadow_image->rows;
cristybb503372010-05-27 20:51:26 +00004332 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4333 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4334 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4335 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
cristy3ed852e2009-09-05 21:47:34 +00004336 return(shadow_image);
4337}
4338
4339/*
4340%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4341% %
4342% %
4343% %
4344% S k e t c h I m a g e %
4345% %
4346% %
4347% %
4348%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4349%
4350% SketchImage() simulates a pencil sketch. We convolve the image with a
4351% Gaussian operator of the given radius and standard deviation (sigma). For
4352% reasonable results, radius should be larger than sigma. Use a radius of 0
4353% and SketchImage() selects a suitable radius for you. Angle gives the angle
4354% of the sketch.
4355%
4356% The format of the SketchImage method is:
4357%
4358% Image *SketchImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00004359% const double sigma,const double angle,const double bias,
4360% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004361%
4362% A description of each parameter follows:
4363%
4364% o image: the image.
4365%
cristy574cc262011-08-05 01:23:58 +00004366% o radius: the radius of the Gaussian, in pixels, not counting the
4367% center pixel.
cristy3ed852e2009-09-05 21:47:34 +00004368%
4369% o sigma: the standard deviation of the Gaussian, in pixels.
4370%
cristyf7ef0252011-09-09 14:50:06 +00004371% o angle: apply the effect along this angle.
4372%
4373% o bias: the bias.
cristy3ed852e2009-09-05 21:47:34 +00004374%
4375% o exception: return any errors or warnings in this structure.
4376%
4377*/
4378MagickExport Image *SketchImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00004379 const double sigma,const double angle,const double bias,
4380 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004381{
cristyfa112112010-01-04 17:48:07 +00004382 CacheView
4383 *random_view;
4384
cristy3ed852e2009-09-05 21:47:34 +00004385 Image
4386 *blend_image,
4387 *blur_image,
4388 *dodge_image,
4389 *random_image,
4390 *sketch_image;
4391
cristy3ed852e2009-09-05 21:47:34 +00004392 MagickBooleanType
4393 status;
4394
cristy3ed852e2009-09-05 21:47:34 +00004395 RandomInfo
cristyfa112112010-01-04 17:48:07 +00004396 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00004397
cristy9d314ff2011-03-09 01:30:28 +00004398 ssize_t
4399 y;
4400
cristy3ed852e2009-09-05 21:47:34 +00004401 /*
4402 Sketch image.
4403 */
4404 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4405 MagickTrue,exception);
4406 if (random_image == (Image *) NULL)
4407 return((Image *) NULL);
4408 status=MagickTrue;
cristy1b784432009-12-19 02:20:40 +00004409 random_info=AcquireRandomInfoThreadSet();
cristy3ed852e2009-09-05 21:47:34 +00004410 random_view=AcquireCacheView(random_image);
cristy1b784432009-12-19 02:20:40 +00004411#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00004412 #pragma omp parallel for schedule(static,4) shared(status)
cristy1b784432009-12-19 02:20:40 +00004413#endif
cristybb503372010-05-27 20:51:26 +00004414 for (y=0; y < (ssize_t) random_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004415 {
cristy5c9e6f22010-09-17 17:31:01 +00004416 const int
4417 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00004418
cristybb503372010-05-27 20:51:26 +00004419 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004420 x;
4421
cristy4c08aed2011-07-01 19:47:50 +00004422 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004423 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004424
cristy1b784432009-12-19 02:20:40 +00004425 if (status == MagickFalse)
4426 continue;
cristy3ed852e2009-09-05 21:47:34 +00004427 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4428 exception);
cristyacd2ed22011-08-30 01:44:23 +00004429 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004430 {
4431 status=MagickFalse;
4432 continue;
4433 }
cristybb503372010-05-27 20:51:26 +00004434 for (x=0; x < (ssize_t) random_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004435 {
cristy76f512e2011-09-12 01:26:56 +00004436 MagickRealType
4437 value;
4438
4439 register ssize_t
4440 i;
4441
4442 value=GetPseudoRandomValue(random_info[id]);
4443 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4444 {
cristyabace412011-12-11 15:56:53 +00004445 PixelChannel
4446 channel;
4447
cristy76f512e2011-09-12 01:26:56 +00004448 PixelTrait
4449 traits;
4450
cristyabace412011-12-11 15:56:53 +00004451 channel=GetPixelChannelMapChannel(image,i);
4452 traits=GetPixelChannelMapTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004453 if (traits == UndefinedPixelTrait)
4454 continue;
4455 q[i]=ClampToQuantum(QuantumRange*value);
4456 }
cristyed231572011-07-14 02:18:59 +00004457 q+=GetPixelChannels(random_image);
cristy3ed852e2009-09-05 21:47:34 +00004458 }
4459 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4460 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004461 }
4462 random_view=DestroyCacheView(random_view);
cristy1b784432009-12-19 02:20:40 +00004463 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00004464 if (status == MagickFalse)
4465 {
4466 random_image=DestroyImage(random_image);
4467 return(random_image);
4468 }
cristyf7ef0252011-09-09 14:50:06 +00004469 blur_image=MotionBlurImage(random_image,radius,sigma,angle,bias,exception);
cristy3ed852e2009-09-05 21:47:34 +00004470 random_image=DestroyImage(random_image);
4471 if (blur_image == (Image *) NULL)
4472 return((Image *) NULL);
cristy6bfd6902011-12-09 01:33:45 +00004473 dodge_image=EdgeImage(blur_image,radius,1.0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004474 blur_image=DestroyImage(blur_image);
4475 if (dodge_image == (Image *) NULL)
4476 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +00004477 (void) NormalizeImage(dodge_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +00004478 (void) NegateImage(dodge_image,MagickFalse,exception);
cristye941a752011-10-15 01:52:48 +00004479 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
cristy3ed852e2009-09-05 21:47:34 +00004480 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4481 if (sketch_image == (Image *) NULL)
4482 {
4483 dodge_image=DestroyImage(dodge_image);
4484 return((Image *) NULL);
4485 }
cristye941a752011-10-15 01:52:48 +00004486 (void) CompositeImage(sketch_image,ColorDodgeCompositeOp,dodge_image,0,0,
4487 exception);
cristy3ed852e2009-09-05 21:47:34 +00004488 dodge_image=DestroyImage(dodge_image);
4489 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4490 if (blend_image == (Image *) NULL)
4491 {
4492 sketch_image=DestroyImage(sketch_image);
4493 return((Image *) NULL);
4494 }
4495 (void) SetImageArtifact(blend_image,"compose:args","20x80");
cristye941a752011-10-15 01:52:48 +00004496 (void) CompositeImage(sketch_image,BlendCompositeOp,blend_image,0,0,
4497 exception);
cristy3ed852e2009-09-05 21:47:34 +00004498 blend_image=DestroyImage(blend_image);
4499 return(sketch_image);
4500}
4501
4502/*
4503%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4504% %
4505% %
4506% %
4507% S o l a r i z e I m a g e %
4508% %
4509% %
4510% %
4511%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4512%
4513% SolarizeImage() applies a special effect to the image, similar to the effect
4514% achieved in a photo darkroom by selectively exposing areas of photo
4515% sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4516% measure of the extent of the solarization.
4517%
4518% The format of the SolarizeImage method is:
4519%
cristy5cbc0162011-08-29 00:36:28 +00004520% MagickBooleanType SolarizeImage(Image *image,const double threshold,
4521% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004522%
4523% A description of each parameter follows:
4524%
4525% o image: the image.
4526%
4527% o threshold: Define the extent of the solarization.
4528%
cristy5cbc0162011-08-29 00:36:28 +00004529% o exception: return any errors or warnings in this structure.
4530%
cristy3ed852e2009-09-05 21:47:34 +00004531*/
4532MagickExport MagickBooleanType SolarizeImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00004533 const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004534{
4535#define SolarizeImageTag "Solarize/Image"
4536
cristyc4c8d132010-01-07 01:58:38 +00004537 CacheView
4538 *image_view;
4539
cristy3ed852e2009-09-05 21:47:34 +00004540 MagickBooleanType
4541 status;
4542
cristybb503372010-05-27 20:51:26 +00004543 MagickOffsetType
4544 progress;
4545
4546 ssize_t
4547 y;
4548
cristy3ed852e2009-09-05 21:47:34 +00004549 assert(image != (Image *) NULL);
4550 assert(image->signature == MagickSignature);
4551 if (image->debug != MagickFalse)
4552 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4553 if (image->storage_class == PseudoClass)
4554 {
cristybb503372010-05-27 20:51:26 +00004555 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004556 i;
4557
4558 /*
4559 Solarize colormap.
4560 */
cristybb503372010-05-27 20:51:26 +00004561 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00004562 {
4563 if ((MagickRealType) image->colormap[i].red > threshold)
4564 image->colormap[i].red=(Quantum) QuantumRange-image->colormap[i].red;
4565 if ((MagickRealType) image->colormap[i].green > threshold)
4566 image->colormap[i].green=(Quantum) QuantumRange-
4567 image->colormap[i].green;
4568 if ((MagickRealType) image->colormap[i].blue > threshold)
4569 image->colormap[i].blue=(Quantum) QuantumRange-
4570 image->colormap[i].blue;
4571 }
4572 }
4573 /*
4574 Solarize image.
4575 */
4576 status=MagickTrue;
4577 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00004578 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00004579#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00004580 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004581#endif
cristybb503372010-05-27 20:51:26 +00004582 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004583 {
cristybb503372010-05-27 20:51:26 +00004584 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004585 x;
4586
cristy4c08aed2011-07-01 19:47:50 +00004587 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004588 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004589
4590 if (status == MagickFalse)
4591 continue;
cristy5cbc0162011-08-29 00:36:28 +00004592 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00004593 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004594 {
4595 status=MagickFalse;
4596 continue;
4597 }
cristybb503372010-05-27 20:51:26 +00004598 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004599 {
cristy76f512e2011-09-12 01:26:56 +00004600 register ssize_t
4601 i;
4602
4603 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4604 {
cristyabace412011-12-11 15:56:53 +00004605 PixelChannel
4606 channel;
4607
cristy76f512e2011-09-12 01:26:56 +00004608 PixelTrait
4609 traits;
4610
cristyabace412011-12-11 15:56:53 +00004611 channel=GetPixelChannelMapChannel(image,i);
4612 traits=GetPixelChannelMapTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004613 if ((traits == UndefinedPixelTrait) ||
4614 ((traits & CopyPixelTrait) != 0))
4615 continue;
4616 if ((MagickRealType) q[i] > threshold)
4617 q[i]=QuantumRange-q[i];
4618 }
cristyed231572011-07-14 02:18:59 +00004619 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004620 }
4621 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4622 status=MagickFalse;
4623 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4624 {
4625 MagickBooleanType
4626 proceed;
4627
cristyb5d5f722009-11-04 03:03:49 +00004628#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00004629 #pragma omp critical (MagickCore_SolarizeImage)
4630#endif
4631 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4632 if (proceed == MagickFalse)
4633 status=MagickFalse;
4634 }
4635 }
4636 image_view=DestroyCacheView(image_view);
4637 return(status);
4638}
4639
4640/*
4641%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4642% %
4643% %
4644% %
4645% S t e g a n o I m a g e %
4646% %
4647% %
4648% %
4649%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4650%
4651% SteganoImage() hides a digital watermark within the image. Recover
4652% the hidden watermark later to prove that the authenticity of an image.
4653% Offset defines the start position within the image to hide the watermark.
4654%
4655% The format of the SteganoImage method is:
4656%
4657% Image *SteganoImage(const Image *image,Image *watermark,
4658% ExceptionInfo *exception)
4659%
4660% A description of each parameter follows:
4661%
4662% o image: the image.
4663%
4664% o watermark: the watermark image.
4665%
4666% o exception: return any errors or warnings in this structure.
4667%
4668*/
4669MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4670 ExceptionInfo *exception)
4671{
cristye1bf8ad2010-09-19 17:07:03 +00004672#define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
cristy4c08aed2011-07-01 19:47:50 +00004673#define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
cristyeaedf062010-05-29 22:36:02 +00004674 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
cristy3ed852e2009-09-05 21:47:34 +00004675#define SteganoImageTag "Stegano/Image"
4676
cristyb0d3bb92010-09-22 14:37:58 +00004677 CacheView
4678 *stegano_view,
4679 *watermark_view;
4680
cristy3ed852e2009-09-05 21:47:34 +00004681 Image
4682 *stegano_image;
4683
4684 int
4685 c;
4686
cristy3ed852e2009-09-05 21:47:34 +00004687 MagickBooleanType
4688 status;
4689
cristy101ab702011-10-13 13:06:32 +00004690 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004691 pixel;
4692
cristy4c08aed2011-07-01 19:47:50 +00004693 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004694 *q;
4695
cristye1bf8ad2010-09-19 17:07:03 +00004696 register ssize_t
4697 x;
4698
cristybb503372010-05-27 20:51:26 +00004699 size_t
cristyeaedf062010-05-29 22:36:02 +00004700 depth,
4701 one;
cristy3ed852e2009-09-05 21:47:34 +00004702
cristye1bf8ad2010-09-19 17:07:03 +00004703 ssize_t
4704 i,
4705 j,
4706 k,
4707 y;
4708
cristy3ed852e2009-09-05 21:47:34 +00004709 /*
4710 Initialize steganographic image attributes.
4711 */
4712 assert(image != (const Image *) NULL);
4713 assert(image->signature == MagickSignature);
4714 if (image->debug != MagickFalse)
4715 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4716 assert(watermark != (const Image *) NULL);
4717 assert(watermark->signature == MagickSignature);
4718 assert(exception != (ExceptionInfo *) NULL);
4719 assert(exception->signature == MagickSignature);
cristyeaedf062010-05-29 22:36:02 +00004720 one=1UL;
cristy3ed852e2009-09-05 21:47:34 +00004721 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4722 if (stegano_image == (Image *) NULL)
4723 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004724 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004725 {
cristy3ed852e2009-09-05 21:47:34 +00004726 stegano_image=DestroyImage(stegano_image);
4727 return((Image *) NULL);
4728 }
4729 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
4730 /*
4731 Hide watermark in low-order bits of image.
4732 */
4733 c=0;
4734 i=0;
4735 j=0;
4736 depth=stegano_image->depth;
4737 k=image->offset;
cristyda16f162011-02-19 23:52:17 +00004738 status=MagickTrue;
cristyb0d3bb92010-09-22 14:37:58 +00004739 watermark_view=AcquireCacheView(watermark);
4740 stegano_view=AcquireCacheView(stegano_image);
cristybb503372010-05-27 20:51:26 +00004741 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
cristy3ed852e2009-09-05 21:47:34 +00004742 {
cristybb503372010-05-27 20:51:26 +00004743 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
cristy3ed852e2009-09-05 21:47:34 +00004744 {
cristybb503372010-05-27 20:51:26 +00004745 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
cristy3ed852e2009-09-05 21:47:34 +00004746 {
cristyda1f9c12011-10-02 21:39:49 +00004747 Quantum
cristy5f95f4f2011-10-23 01:01:01 +00004748 virtual_pixel[CompositePixelChannel];
cristyda1f9c12011-10-02 21:39:49 +00004749
4750 (void) GetOneCacheViewVirtualPixel(watermark_view,x,y,virtual_pixel,
4751 exception);
4752 pixel.red=(double) virtual_pixel[RedPixelChannel];
4753 pixel.green=(double) virtual_pixel[GreenPixelChannel];
4754 pixel.blue=(double) virtual_pixel[BluePixelChannel];
4755 pixel.alpha=(double) virtual_pixel[AlphaPixelChannel];
cristybb503372010-05-27 20:51:26 +00004756 if ((k/(ssize_t) stegano_image->columns) >= (ssize_t) stegano_image->rows)
cristy3ed852e2009-09-05 21:47:34 +00004757 break;
cristyb0d3bb92010-09-22 14:37:58 +00004758 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4759 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4760 exception);
cristyacd2ed22011-08-30 01:44:23 +00004761 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004762 break;
4763 switch (c)
4764 {
4765 case 0:
4766 {
cristy4c08aed2011-07-01 19:47:50 +00004767 SetPixelRed(image,SetBit(GetPixelRed(image,q),j,GetBit(
cristy101ab702011-10-13 13:06:32 +00004768 GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004769 break;
4770 }
4771 case 1:
4772 {
cristy4c08aed2011-07-01 19:47:50 +00004773 SetPixelGreen(image,SetBit(GetPixelGreen(image,q),j,GetBit(
cristy101ab702011-10-13 13:06:32 +00004774 GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004775 break;
4776 }
4777 case 2:
4778 {
cristy4c08aed2011-07-01 19:47:50 +00004779 SetPixelBlue(image,SetBit(GetPixelBlue(image,q),j,GetBit(
cristy101ab702011-10-13 13:06:32 +00004780 GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004781 break;
4782 }
4783 }
cristyb0d3bb92010-09-22 14:37:58 +00004784 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004785 break;
4786 c++;
4787 if (c == 3)
4788 c=0;
4789 k++;
cristybb503372010-05-27 20:51:26 +00004790 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
cristy3ed852e2009-09-05 21:47:34 +00004791 k=0;
4792 if (k == image->offset)
4793 j++;
4794 }
4795 }
cristy8b27a6d2010-02-14 03:31:15 +00004796 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004797 {
cristy8b27a6d2010-02-14 03:31:15 +00004798 MagickBooleanType
4799 proceed;
4800
4801 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4802 (depth-i),depth);
4803 if (proceed == MagickFalse)
4804 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004805 }
4806 }
cristyb0d3bb92010-09-22 14:37:58 +00004807 stegano_view=DestroyCacheView(stegano_view);
4808 watermark_view=DestroyCacheView(watermark_view);
cristy3ed852e2009-09-05 21:47:34 +00004809 if (stegano_image->storage_class == PseudoClass)
cristyea1a8aa2011-10-20 13:24:06 +00004810 (void) SyncImage(stegano_image,exception);
cristyda16f162011-02-19 23:52:17 +00004811 if (status == MagickFalse)
4812 {
4813 stegano_image=DestroyImage(stegano_image);
4814 return((Image *) NULL);
4815 }
cristy3ed852e2009-09-05 21:47:34 +00004816 return(stegano_image);
4817}
4818
4819/*
4820%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4821% %
4822% %
4823% %
4824% S t e r e o A n a g l y p h I m a g e %
4825% %
4826% %
4827% %
4828%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4829%
4830% StereoAnaglyphImage() combines two images and produces a single image that
4831% is the composite of a left and right image of a stereo pair. Special
4832% red-green stereo glasses are required to view this effect.
4833%
4834% The format of the StereoAnaglyphImage method is:
4835%
4836% Image *StereoImage(const Image *left_image,const Image *right_image,
4837% ExceptionInfo *exception)
4838% Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004839% const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004840% ExceptionInfo *exception)
4841%
4842% A description of each parameter follows:
4843%
4844% o left_image: the left image.
4845%
4846% o right_image: the right image.
4847%
4848% o exception: return any errors or warnings in this structure.
4849%
4850% o x_offset: amount, in pixels, by which the left image is offset to the
4851% right of the right image.
4852%
4853% o y_offset: amount, in pixels, by which the left image is offset to the
4854% bottom of the right image.
4855%
4856%
4857*/
4858MagickExport Image *StereoImage(const Image *left_image,
4859 const Image *right_image,ExceptionInfo *exception)
4860{
4861 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4862}
4863
4864MagickExport Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004865 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004866 ExceptionInfo *exception)
4867{
4868#define StereoImageTag "Stereo/Image"
4869
4870 const Image
4871 *image;
4872
4873 Image
4874 *stereo_image;
4875
cristy3ed852e2009-09-05 21:47:34 +00004876 MagickBooleanType
4877 status;
4878
cristy9d314ff2011-03-09 01:30:28 +00004879 ssize_t
4880 y;
4881
cristy3ed852e2009-09-05 21:47:34 +00004882 assert(left_image != (const Image *) NULL);
4883 assert(left_image->signature == MagickSignature);
4884 if (left_image->debug != MagickFalse)
4885 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4886 left_image->filename);
4887 assert(right_image != (const Image *) NULL);
4888 assert(right_image->signature == MagickSignature);
4889 assert(exception != (ExceptionInfo *) NULL);
4890 assert(exception->signature == MagickSignature);
4891 assert(right_image != (const Image *) NULL);
4892 image=left_image;
4893 if ((left_image->columns != right_image->columns) ||
4894 (left_image->rows != right_image->rows))
4895 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4896 /*
4897 Initialize stereo image attributes.
4898 */
4899 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4900 MagickTrue,exception);
4901 if (stereo_image == (Image *) NULL)
4902 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004903 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004904 {
cristy3ed852e2009-09-05 21:47:34 +00004905 stereo_image=DestroyImage(stereo_image);
4906 return((Image *) NULL);
4907 }
4908 /*
4909 Copy left image to red channel and right image to blue channel.
4910 */
cristyda16f162011-02-19 23:52:17 +00004911 status=MagickTrue;
cristybb503372010-05-27 20:51:26 +00004912 for (y=0; y < (ssize_t) stereo_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004913 {
cristy4c08aed2011-07-01 19:47:50 +00004914 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004915 *restrict p,
4916 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004917
cristybb503372010-05-27 20:51:26 +00004918 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004919 x;
4920
cristy4c08aed2011-07-01 19:47:50 +00004921 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004922 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +00004923
4924 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4925 exception);
4926 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4927 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
cristy76f512e2011-09-12 01:26:56 +00004928 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4929 (r == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004930 break;
cristybb503372010-05-27 20:51:26 +00004931 for (x=0; x < (ssize_t) stereo_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004932 {
cristy4c08aed2011-07-01 19:47:50 +00004933 SetPixelRed(image,GetPixelRed(left_image,p),r);
cristy76f512e2011-09-12 01:26:56 +00004934 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4935 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4936 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4937 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4938 GetPixelAlpha(right_image,q))/2,r);
cristyed231572011-07-14 02:18:59 +00004939 p+=GetPixelChannels(left_image);
cristy76f512e2011-09-12 01:26:56 +00004940 q+=GetPixelChannels(right_image);
4941 r+=GetPixelChannels(stereo_image);
cristy3ed852e2009-09-05 21:47:34 +00004942 }
4943 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4944 break;
cristy8b27a6d2010-02-14 03:31:15 +00004945 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004946 {
cristy8b27a6d2010-02-14 03:31:15 +00004947 MagickBooleanType
4948 proceed;
4949
cristybb503372010-05-27 20:51:26 +00004950 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4951 stereo_image->rows);
cristy8b27a6d2010-02-14 03:31:15 +00004952 if (proceed == MagickFalse)
4953 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004954 }
4955 }
cristyda16f162011-02-19 23:52:17 +00004956 if (status == MagickFalse)
4957 {
4958 stereo_image=DestroyImage(stereo_image);
4959 return((Image *) NULL);
4960 }
cristy3ed852e2009-09-05 21:47:34 +00004961 return(stereo_image);
4962}
4963
4964/*
4965%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4966% %
4967% %
4968% %
4969% S w i r l I m a g e %
4970% %
4971% %
4972% %
4973%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4974%
4975% SwirlImage() swirls the pixels about the center of the image, where
4976% degrees indicates the sweep of the arc through which each pixel is moved.
4977% You get a more dramatic effect as the degrees move from 1 to 360.
4978%
4979% The format of the SwirlImage method is:
4980%
4981% Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00004982% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004983%
4984% A description of each parameter follows:
4985%
4986% o image: the image.
4987%
4988% o degrees: Define the tightness of the swirling effect.
4989%
cristy76f512e2011-09-12 01:26:56 +00004990% o method: the pixel interpolation method.
4991%
cristy3ed852e2009-09-05 21:47:34 +00004992% o exception: return any errors or warnings in this structure.
4993%
4994*/
4995MagickExport Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00004996 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004997{
4998#define SwirlImageTag "Swirl/Image"
4999
cristyfa112112010-01-04 17:48:07 +00005000 CacheView
5001 *image_view,
5002 *swirl_view;
5003
cristy3ed852e2009-09-05 21:47:34 +00005004 Image
5005 *swirl_image;
5006
cristy3ed852e2009-09-05 21:47:34 +00005007 MagickBooleanType
5008 status;
5009
cristybb503372010-05-27 20:51:26 +00005010 MagickOffsetType
5011 progress;
5012
cristy3ed852e2009-09-05 21:47:34 +00005013 MagickRealType
5014 radius;
5015
5016 PointInfo
5017 center,
5018 scale;
5019
cristybb503372010-05-27 20:51:26 +00005020 ssize_t
5021 y;
5022
cristy3ed852e2009-09-05 21:47:34 +00005023 /*
5024 Initialize swirl image attributes.
5025 */
5026 assert(image != (const Image *) NULL);
5027 assert(image->signature == MagickSignature);
5028 if (image->debug != MagickFalse)
5029 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5030 assert(exception != (ExceptionInfo *) NULL);
5031 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00005032 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005033 if (swirl_image == (Image *) NULL)
5034 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005035 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005036 {
cristy3ed852e2009-09-05 21:47:34 +00005037 swirl_image=DestroyImage(swirl_image);
5038 return((Image *) NULL);
5039 }
cristy4c08aed2011-07-01 19:47:50 +00005040 if (swirl_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00005041 swirl_image->matte=MagickTrue;
5042 /*
5043 Compute scaling factor.
5044 */
5045 center.x=(double) image->columns/2.0;
5046 center.y=(double) image->rows/2.0;
5047 radius=MagickMax(center.x,center.y);
5048 scale.x=1.0;
5049 scale.y=1.0;
5050 if (image->columns > image->rows)
5051 scale.y=(double) image->columns/(double) image->rows;
5052 else
5053 if (image->columns < image->rows)
5054 scale.x=(double) image->rows/(double) image->columns;
5055 degrees=(double) DegreesToRadians(degrees);
5056 /*
5057 Swirl image.
5058 */
5059 status=MagickTrue;
5060 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00005061 image_view=AcquireCacheView(image);
5062 swirl_view=AcquireCacheView(swirl_image);
cristyb5d5f722009-11-04 03:03:49 +00005063#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00005064 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005065#endif
cristybb503372010-05-27 20:51:26 +00005066 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005067 {
cristy3ed852e2009-09-05 21:47:34 +00005068 MagickRealType
5069 distance;
5070
5071 PointInfo
5072 delta;
5073
cristy6d188022011-09-12 13:23:33 +00005074 register const Quantum
5075 *restrict p;
5076
cristybb503372010-05-27 20:51:26 +00005077 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005078 x;
5079
cristy4c08aed2011-07-01 19:47:50 +00005080 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005081 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005082
5083 if (status == MagickFalse)
5084 continue;
cristy6d188022011-09-12 13:23:33 +00005085 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy3ed852e2009-09-05 21:47:34 +00005086 q=GetCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
5087 exception);
cristy6d188022011-09-12 13:23:33 +00005088 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005089 {
5090 status=MagickFalse;
5091 continue;
5092 }
cristy3ed852e2009-09-05 21:47:34 +00005093 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00005094 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005095 {
cristy6d188022011-09-12 13:23:33 +00005096 register ssize_t
5097 i;
5098
cristy3ed852e2009-09-05 21:47:34 +00005099 /*
5100 Determine if the pixel is within an ellipse.
5101 */
5102 delta.x=scale.x*(double) (x-center.x);
5103 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00005104 if (distance >= (radius*radius))
5105 {
5106 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5107 q[i]=p[i];
5108 }
5109 else
cristy3ed852e2009-09-05 21:47:34 +00005110 {
5111 MagickRealType
5112 cosine,
5113 factor,
5114 sine;
5115
5116 /*
5117 Swirl the pixel.
5118 */
5119 factor=1.0-sqrt((double) distance)/radius;
5120 sine=sin((double) (degrees*factor*factor));
5121 cosine=cos((double) (degrees*factor*factor));
cristy76f512e2011-09-12 01:26:56 +00005122 status=InterpolatePixelChannels(image,image_view,swirl_image,method,
5123 ((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5124 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00005125 }
cristy6d188022011-09-12 13:23:33 +00005126 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00005127 q+=GetPixelChannels(swirl_image);
cristy3ed852e2009-09-05 21:47:34 +00005128 }
5129 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5130 status=MagickFalse;
5131 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5132 {
5133 MagickBooleanType
5134 proceed;
5135
cristyb5d5f722009-11-04 03:03:49 +00005136#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005137 #pragma omp critical (MagickCore_SwirlImage)
5138#endif
5139 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5140 if (proceed == MagickFalse)
5141 status=MagickFalse;
5142 }
5143 }
5144 swirl_view=DestroyCacheView(swirl_view);
5145 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005146 if (status == MagickFalse)
5147 swirl_image=DestroyImage(swirl_image);
5148 return(swirl_image);
5149}
5150
5151/*
5152%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5153% %
5154% %
5155% %
5156% T i n t I m a g e %
5157% %
5158% %
5159% %
5160%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5161%
5162% TintImage() applies a color vector to each pixel in the image. The length
5163% of the vector is 0 for black and white and at its maximum for the midtones.
5164% The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5165%
5166% The format of the TintImage method is:
5167%
cristyb817c3f2011-10-03 14:00:35 +00005168% Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005169% const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005170%
5171% A description of each parameter follows:
5172%
5173% o image: the image.
5174%
cristyb817c3f2011-10-03 14:00:35 +00005175% o blend: A color value used for tinting.
cristy3ed852e2009-09-05 21:47:34 +00005176%
5177% o tint: A color value used for tinting.
5178%
5179% o exception: return any errors or warnings in this structure.
5180%
5181*/
cristyb817c3f2011-10-03 14:00:35 +00005182MagickExport Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005183 const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005184{
5185#define TintImageTag "Tint/Image"
5186
cristyc4c8d132010-01-07 01:58:38 +00005187 CacheView
5188 *image_view,
5189 *tint_view;
5190
cristy3ed852e2009-09-05 21:47:34 +00005191 GeometryInfo
5192 geometry_info;
5193
5194 Image
5195 *tint_image;
5196
cristy3ed852e2009-09-05 21:47:34 +00005197 MagickBooleanType
5198 status;
5199
cristybb503372010-05-27 20:51:26 +00005200 MagickOffsetType
5201 progress;
cristy3ed852e2009-09-05 21:47:34 +00005202
cristy28474bf2011-09-11 23:32:52 +00005203 MagickRealType
5204 intensity;
5205
cristy4c08aed2011-07-01 19:47:50 +00005206 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00005207 color_vector,
5208 pixel;
5209
cristybb503372010-05-27 20:51:26 +00005210 MagickStatusType
5211 flags;
5212
5213 ssize_t
5214 y;
5215
cristy3ed852e2009-09-05 21:47:34 +00005216 /*
5217 Allocate tint image.
5218 */
5219 assert(image != (const Image *) NULL);
5220 assert(image->signature == MagickSignature);
5221 if (image->debug != MagickFalse)
5222 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5223 assert(exception != (ExceptionInfo *) NULL);
5224 assert(exception->signature == MagickSignature);
5225 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5226 if (tint_image == (Image *) NULL)
5227 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005228 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005229 {
cristy3ed852e2009-09-05 21:47:34 +00005230 tint_image=DestroyImage(tint_image);
5231 return((Image *) NULL);
5232 }
cristyaed9c382011-10-03 17:54:21 +00005233 if (blend == (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005234 return(tint_image);
5235 /*
5236 Determine RGB values of the color.
5237 */
cristy28474bf2011-09-11 23:32:52 +00005238 GetPixelInfo(image,&pixel);
cristyb817c3f2011-10-03 14:00:35 +00005239 flags=ParseGeometry(blend,&geometry_info);
cristy3ed852e2009-09-05 21:47:34 +00005240 pixel.red=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005241 pixel.green=geometry_info.rho;
5242 pixel.blue=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005243 pixel.alpha=OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00005244 if ((flags & SigmaValue) != 0)
5245 pixel.green=geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +00005246 if ((flags & XiValue) != 0)
5247 pixel.blue=geometry_info.xi;
cristyb817c3f2011-10-03 14:00:35 +00005248 if ((flags & PsiValue) != 0)
5249 pixel.alpha=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005250 if (image->colorspace == CMYKColorspace)
5251 {
cristyb817c3f2011-10-03 14:00:35 +00005252 pixel.black=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005253 if ((flags & PsiValue) != 0)
5254 pixel.black=geometry_info.psi;
5255 if ((flags & ChiValue) != 0)
5256 pixel.alpha=geometry_info.chi;
5257 }
cristy28474bf2011-09-11 23:32:52 +00005258 intensity=(MagickRealType) GetPixelInfoIntensity(tint);
5259 color_vector.red=(MagickRealType) (pixel.red*tint->red/100.0-intensity);
5260 color_vector.green=(MagickRealType) (pixel.green*tint->green/100.0-intensity);
5261 color_vector.blue=(MagickRealType) (pixel.blue*tint->blue/100.0-intensity);
5262 color_vector.black=(MagickRealType) (pixel.black*tint->black/100.0-intensity);
5263 color_vector.alpha=(MagickRealType) (pixel.alpha*tint->alpha/100.0-intensity);
cristy3ed852e2009-09-05 21:47:34 +00005264 /*
5265 Tint image.
5266 */
5267 status=MagickTrue;
5268 progress=0;
5269 image_view=AcquireCacheView(image);
5270 tint_view=AcquireCacheView(tint_image);
cristyb5d5f722009-11-04 03:03:49 +00005271#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00005272 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005273#endif
cristybb503372010-05-27 20:51:26 +00005274 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005275 {
cristy4c08aed2011-07-01 19:47:50 +00005276 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00005277 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00005278
cristy4c08aed2011-07-01 19:47:50 +00005279 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005280 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005281
cristy6b91acb2011-04-19 12:23:54 +00005282 register ssize_t
5283 x;
5284
cristy3ed852e2009-09-05 21:47:34 +00005285 if (status == MagickFalse)
5286 continue;
5287 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5288 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5289 exception);
cristy4c08aed2011-07-01 19:47:50 +00005290 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005291 {
5292 status=MagickFalse;
5293 continue;
5294 }
cristybb503372010-05-27 20:51:26 +00005295 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005296 {
cristy4c08aed2011-07-01 19:47:50 +00005297 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00005298 pixel;
5299
5300 MagickRealType
5301 weight;
5302
cristy76f512e2011-09-12 01:26:56 +00005303 if ((GetPixelRedTraits(tint_image) & UpdatePixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005304 {
5305 weight=QuantumScale*GetPixelRed(image,p)-0.5;
5306 pixel.red=(MagickRealType) GetPixelRed(image,p)+
5307 color_vector.red*(1.0-(4.0*(weight*weight)));
5308 SetPixelRed(tint_image,ClampToQuantum(pixel.red),q);
5309 }
cristy76f512e2011-09-12 01:26:56 +00005310 if ((GetPixelGreenTraits(tint_image) & UpdatePixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005311 {
5312 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
5313 pixel.green=(MagickRealType) GetPixelGreen(image,p)+
5314 color_vector.green*(1.0-(4.0*(weight*weight)));
5315 SetPixelGreen(tint_image,ClampToQuantum(pixel.green),q);
5316 }
cristy76f512e2011-09-12 01:26:56 +00005317 if ((GetPixelBlueTraits(tint_image) & UpdatePixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005318 {
5319 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
5320 pixel.blue=(MagickRealType) GetPixelBlue(image,p)+
5321 color_vector.blue*(1.0-(4.0*(weight*weight)));
5322 SetPixelBlue(tint_image,ClampToQuantum(pixel.blue),q);
5323 }
cristy76f512e2011-09-12 01:26:56 +00005324 if ((GetPixelBlackTraits(tint_image) & UpdatePixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005325 {
5326 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
5327 pixel.black=(MagickRealType) GetPixelBlack(image,p)+
5328 color_vector.black*(1.0-(4.0*(weight*weight)));
5329 SetPixelBlack(tint_image,ClampToQuantum(pixel.black),q);
5330 }
cristy76f512e2011-09-12 01:26:56 +00005331 if ((GetPixelAlphaTraits(tint_image) & CopyPixelTrait) != 0)
cristy28474bf2011-09-11 23:32:52 +00005332 SetPixelAlpha(tint_image,GetPixelAlpha(image,p),q);
cristyed231572011-07-14 02:18:59 +00005333 p+=GetPixelChannels(image);
5334 q+=GetPixelChannels(tint_image);
cristy3ed852e2009-09-05 21:47:34 +00005335 }
5336 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5337 status=MagickFalse;
5338 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5339 {
5340 MagickBooleanType
5341 proceed;
5342
cristyb5d5f722009-11-04 03:03:49 +00005343#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005344 #pragma omp critical (MagickCore_TintImage)
5345#endif
5346 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5347 if (proceed == MagickFalse)
5348 status=MagickFalse;
5349 }
5350 }
5351 tint_view=DestroyCacheView(tint_view);
5352 image_view=DestroyCacheView(image_view);
5353 if (status == MagickFalse)
5354 tint_image=DestroyImage(tint_image);
5355 return(tint_image);
5356}
5357
5358/*
5359%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5360% %
5361% %
5362% %
5363% V i g n e t t e I m a g e %
5364% %
5365% %
5366% %
5367%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5368%
5369% VignetteImage() softens the edges of the image in vignette style.
5370%
5371% The format of the VignetteImage method is:
5372%
5373% Image *VignetteImage(const Image *image,const double radius,
cristyeb6e6582011-12-09 09:14:23 +00005374% const double sigma,const double bias,const ssize_t x,const ssize_t y,
cristy05c0c9a2011-09-05 23:16:13 +00005375% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005376%
5377% A description of each parameter follows:
5378%
5379% o image: the image.
5380%
5381% o radius: the radius of the pixel neighborhood.
5382%
5383% o sigma: the standard deviation of the Gaussian, in pixels.
5384%
cristyeb6e6582011-12-09 09:14:23 +00005385% o bias: the bias.
5386%
cristy3ed852e2009-09-05 21:47:34 +00005387% o x, y: Define the x and y ellipse offset.
5388%
5389% o exception: return any errors or warnings in this structure.
5390%
5391*/
5392MagickExport Image *VignetteImage(const Image *image,const double radius,
cristyeb6e6582011-12-09 09:14:23 +00005393 const double sigma,const double bias,const ssize_t x,const ssize_t y,
5394 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005395{
5396 char
5397 ellipse[MaxTextExtent];
5398
5399 DrawInfo
5400 *draw_info;
5401
5402 Image
5403 *canvas_image,
5404 *blur_image,
5405 *oval_image,
5406 *vignette_image;
5407
5408 assert(image != (Image *) NULL);
5409 assert(image->signature == MagickSignature);
5410 if (image->debug != MagickFalse)
5411 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5412 assert(exception != (ExceptionInfo *) NULL);
5413 assert(exception->signature == MagickSignature);
5414 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5415 if (canvas_image == (Image *) NULL)
5416 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005417 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005418 {
cristy3ed852e2009-09-05 21:47:34 +00005419 canvas_image=DestroyImage(canvas_image);
5420 return((Image *) NULL);
5421 }
5422 canvas_image->matte=MagickTrue;
cristy98621462011-12-31 22:31:11 +00005423 oval_image=CloneImage(canvas_image,canvas_image->columns,canvas_image->rows,
5424 MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005425 if (oval_image == (Image *) NULL)
5426 {
5427 canvas_image=DestroyImage(canvas_image);
5428 return((Image *) NULL);
5429 }
cristy9950d572011-10-01 18:22:35 +00005430 (void) QueryColorCompliance("#000000",AllCompliance,
5431 &oval_image->background_color,exception);
cristyea1a8aa2011-10-20 13:24:06 +00005432 (void) SetImageBackgroundColor(oval_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00005433 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
cristy9950d572011-10-01 18:22:35 +00005434 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5435 exception);
5436 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5437 exception);
cristyb51dff52011-05-19 16:55:47 +00005438 (void) FormatLocaleString(ellipse,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00005439 "ellipse %g,%g,%g,%g,0.0,360.0",image->columns/2.0,
cristy8cd5b312010-01-07 01:10:24 +00005440 image->rows/2.0,image->columns/2.0-x,image->rows/2.0-y);
cristy3ed852e2009-09-05 21:47:34 +00005441 draw_info->primitive=AcquireString(ellipse);
cristy018f07f2011-09-04 21:15:19 +00005442 (void) DrawImage(oval_image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00005443 draw_info=DestroyDrawInfo(draw_info);
cristyeb6e6582011-12-09 09:14:23 +00005444 blur_image=BlurImage(oval_image,radius,sigma,bias,exception);
cristy3ed852e2009-09-05 21:47:34 +00005445 oval_image=DestroyImage(oval_image);
5446 if (blur_image == (Image *) NULL)
5447 {
5448 canvas_image=DestroyImage(canvas_image);
5449 return((Image *) NULL);
5450 }
5451 blur_image->matte=MagickFalse;
cristy98621462011-12-31 22:31:11 +00005452 (void) CompositeImage(canvas_image,IntensityCompositeOp,blur_image,0,0,
cristye941a752011-10-15 01:52:48 +00005453 exception);
cristy3ed852e2009-09-05 21:47:34 +00005454 blur_image=DestroyImage(blur_image);
5455 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5456 canvas_image=DestroyImage(canvas_image);
5457 return(vignette_image);
5458}
5459
5460/*
5461%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5462% %
5463% %
5464% %
5465% W a v e I m a g e %
5466% %
5467% %
5468% %
5469%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5470%
5471% WaveImage() creates a "ripple" effect in the image by shifting the pixels
cristycee97112010-05-28 00:44:52 +00005472% vertically along a sine wave whose amplitude and wavelength is specified
cristy3ed852e2009-09-05 21:47:34 +00005473% by the given parameters.
5474%
5475% The format of the WaveImage method is:
5476%
5477% Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005478% const double wave_length,const PixelInterpolateMethod method,
5479% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005480%
5481% A description of each parameter follows:
5482%
5483% o image: the image.
5484%
5485% o amplitude, wave_length: Define the amplitude and wave length of the
5486% sine wave.
5487%
cristy5c4e2582011-09-11 19:21:03 +00005488% o interpolate: the pixel interpolation method.
5489%
cristy3ed852e2009-09-05 21:47:34 +00005490% o exception: return any errors or warnings in this structure.
5491%
5492*/
5493MagickExport Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005494 const double wave_length,const PixelInterpolateMethod method,
5495 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005496{
5497#define WaveImageTag "Wave/Image"
5498
cristyfa112112010-01-04 17:48:07 +00005499 CacheView
cristyd76c51e2011-03-26 00:21:26 +00005500 *image_view,
cristyfa112112010-01-04 17:48:07 +00005501 *wave_view;
5502
cristy3ed852e2009-09-05 21:47:34 +00005503 Image
5504 *wave_image;
5505
cristy3ed852e2009-09-05 21:47:34 +00005506 MagickBooleanType
5507 status;
5508
cristybb503372010-05-27 20:51:26 +00005509 MagickOffsetType
5510 progress;
5511
cristy3ed852e2009-09-05 21:47:34 +00005512 MagickRealType
5513 *sine_map;
5514
cristybb503372010-05-27 20:51:26 +00005515 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005516 i;
5517
cristybb503372010-05-27 20:51:26 +00005518 ssize_t
5519 y;
5520
cristy3ed852e2009-09-05 21:47:34 +00005521 /*
5522 Initialize wave image attributes.
5523 */
5524 assert(image != (Image *) NULL);
5525 assert(image->signature == MagickSignature);
5526 if (image->debug != MagickFalse)
5527 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5528 assert(exception != (ExceptionInfo *) NULL);
5529 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00005530 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
cristy3ed852e2009-09-05 21:47:34 +00005531 fabs(amplitude)),MagickTrue,exception);
5532 if (wave_image == (Image *) NULL)
5533 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005534 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005535 {
cristy3ed852e2009-09-05 21:47:34 +00005536 wave_image=DestroyImage(wave_image);
5537 return((Image *) NULL);
5538 }
cristy4c08aed2011-07-01 19:47:50 +00005539 if (wave_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00005540 wave_image->matte=MagickTrue;
5541 /*
5542 Allocate sine map.
5543 */
5544 sine_map=(MagickRealType *) AcquireQuantumMemory((size_t) wave_image->columns,
5545 sizeof(*sine_map));
5546 if (sine_map == (MagickRealType *) NULL)
5547 {
5548 wave_image=DestroyImage(wave_image);
5549 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5550 }
cristybb503372010-05-27 20:51:26 +00005551 for (i=0; i < (ssize_t) wave_image->columns; i++)
cristy4205a3c2010-09-12 20:19:59 +00005552 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5553 wave_length));
cristy3ed852e2009-09-05 21:47:34 +00005554 /*
5555 Wave image.
5556 */
5557 status=MagickTrue;
5558 progress=0;
cristyd76c51e2011-03-26 00:21:26 +00005559 image_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00005560 wave_view=AcquireCacheView(wave_image);
cristyd76c51e2011-03-26 00:21:26 +00005561 (void) SetCacheViewVirtualPixelMethod(image_view,
5562 BackgroundVirtualPixelMethod);
cristyb5d5f722009-11-04 03:03:49 +00005563#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00005564 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005565#endif
cristybb503372010-05-27 20:51:26 +00005566 for (y=0; y < (ssize_t) wave_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005567 {
cristy4c08aed2011-07-01 19:47:50 +00005568 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005569 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005570
cristye97bb922011-04-03 01:36:52 +00005571 register ssize_t
5572 x;
5573
cristy3ed852e2009-09-05 21:47:34 +00005574 if (status == MagickFalse)
5575 continue;
5576 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5577 exception);
cristyacd2ed22011-08-30 01:44:23 +00005578 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005579 {
5580 status=MagickFalse;
5581 continue;
5582 }
cristybb503372010-05-27 20:51:26 +00005583 for (x=0; x < (ssize_t) wave_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005584 {
cristy5c4e2582011-09-11 19:21:03 +00005585 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5586 (double) x,(double) (y-sine_map[x]),q,exception);
cristyed231572011-07-14 02:18:59 +00005587 q+=GetPixelChannels(wave_image);
cristy3ed852e2009-09-05 21:47:34 +00005588 }
5589 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5590 status=MagickFalse;
5591 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5592 {
5593 MagickBooleanType
5594 proceed;
5595
cristyb5d5f722009-11-04 03:03:49 +00005596#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005597 #pragma omp critical (MagickCore_WaveImage)
5598#endif
5599 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5600 if (proceed == MagickFalse)
5601 status=MagickFalse;
5602 }
5603 }
5604 wave_view=DestroyCacheView(wave_view);
cristyd76c51e2011-03-26 00:21:26 +00005605 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005606 sine_map=(MagickRealType *) RelinquishMagickMemory(sine_map);
5607 if (status == MagickFalse)
5608 wave_image=DestroyImage(wave_image);
5609 return(wave_image);
5610}