blob: b55dd6f8632ec97601506727a33e4170a2e9cd7b [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);
cristya6d7a9b2012-01-18 20:04:48 +0000326 q=QueueCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +0000327 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);
cristya6d7a9b2012-01-18 20:04:48 +0000448 shift_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000449 if (shift_image == (Image *) NULL)
450 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000451 if (SetImageStorageClass(shift_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000452 {
cristy3ed852e2009-09-05 21:47:34 +0000453 shift_image=DestroyImage(shift_image);
454 return((Image *) NULL);
455 }
456 /*
457 Blue-shift DirectClass image.
458 */
459 status=MagickTrue;
460 progress=0;
461 image_view=AcquireCacheView(image);
462 shift_view=AcquireCacheView(shift_image);
cristy319a1e72010-02-21 15:13:11 +0000463#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000464 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000465#endif
cristybb503372010-05-27 20:51:26 +0000466 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000467 {
468 MagickBooleanType
469 sync;
470
cristy4c08aed2011-07-01 19:47:50 +0000471 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000472 pixel;
473
474 Quantum
475 quantum;
476
cristy4c08aed2011-07-01 19:47:50 +0000477 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000478 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000479
cristybb503372010-05-27 20:51:26 +0000480 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000481 x;
482
cristy4c08aed2011-07-01 19:47:50 +0000483 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000484 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000485
486 if (status == MagickFalse)
487 continue;
488 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
489 q=QueueCacheViewAuthenticPixels(shift_view,0,y,shift_image->columns,1,
490 exception);
cristy4c08aed2011-07-01 19:47:50 +0000491 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000492 {
493 status=MagickFalse;
494 continue;
495 }
cristybb503372010-05-27 20:51:26 +0000496 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000497 {
cristy4c08aed2011-07-01 19:47:50 +0000498 quantum=GetPixelRed(image,p);
499 if (GetPixelGreen(image,p) < quantum)
500 quantum=GetPixelGreen(image,p);
501 if (GetPixelBlue(image,p) < quantum)
502 quantum=GetPixelBlue(image,p);
503 pixel.red=0.5*(GetPixelRed(image,p)+factor*quantum);
504 pixel.green=0.5*(GetPixelGreen(image,p)+factor*quantum);
505 pixel.blue=0.5*(GetPixelBlue(image,p)+factor*quantum);
506 quantum=GetPixelRed(image,p);
507 if (GetPixelGreen(image,p) > quantum)
508 quantum=GetPixelGreen(image,p);
509 if (GetPixelBlue(image,p) > quantum)
510 quantum=GetPixelBlue(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000511 pixel.red=0.5*(pixel.red+factor*quantum);
512 pixel.green=0.5*(pixel.green+factor*quantum);
513 pixel.blue=0.5*(pixel.blue+factor*quantum);
cristy4c08aed2011-07-01 19:47:50 +0000514 SetPixelRed(shift_image,ClampToQuantum(pixel.red),q);
515 SetPixelGreen(shift_image,ClampToQuantum(pixel.green),q);
516 SetPixelBlue(shift_image,ClampToQuantum(pixel.blue),q);
cristyed231572011-07-14 02:18:59 +0000517 p+=GetPixelChannels(image);
518 q+=GetPixelChannels(shift_image);
cristy3ed852e2009-09-05 21:47:34 +0000519 }
520 sync=SyncCacheViewAuthenticPixels(shift_view,exception);
521 if (sync == MagickFalse)
522 status=MagickFalse;
523 if (image->progress_monitor != (MagickProgressMonitor) NULL)
524 {
525 MagickBooleanType
526 proceed;
527
cristy319a1e72010-02-21 15:13:11 +0000528#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000529 #pragma omp critical (MagickCore_BlueShiftImage)
530#endif
531 proceed=SetImageProgress(image,BlueShiftImageTag,progress++,
532 image->rows);
533 if (proceed == MagickFalse)
534 status=MagickFalse;
535 }
536 }
537 image_view=DestroyCacheView(image_view);
538 shift_view=DestroyCacheView(shift_view);
539 if (status == MagickFalse)
540 shift_image=DestroyImage(shift_image);
541 return(shift_image);
542}
543
544/*
545%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
546% %
547% %
548% %
549% C h a r c o a l I m a g e %
550% %
551% %
552% %
553%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
554%
555% CharcoalImage() creates a new image that is a copy of an existing one with
556% the edge highlighted. It allocates the memory necessary for the new Image
557% structure and returns a pointer to the new image.
558%
559% The format of the CharcoalImage method is:
560%
561% Image *CharcoalImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +0000562% const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000563%
564% A description of each parameter follows:
565%
566% o image: the image.
567%
568% o radius: the radius of the pixel neighborhood.
569%
570% o sigma: the standard deviation of the Gaussian, in pixels.
571%
cristy05c0c9a2011-09-05 23:16:13 +0000572% o bias: the bias.
573%
cristy3ed852e2009-09-05 21:47:34 +0000574% o exception: return any errors or warnings in this structure.
575%
576*/
577MagickExport Image *CharcoalImage(const Image *image,const double radius,
cristy05c0c9a2011-09-05 23:16:13 +0000578 const double sigma,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000579{
580 Image
581 *charcoal_image,
582 *clone_image,
583 *edge_image;
584
585 assert(image != (Image *) NULL);
586 assert(image->signature == MagickSignature);
587 if (image->debug != MagickFalse)
588 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
589 assert(exception != (ExceptionInfo *) NULL);
590 assert(exception->signature == MagickSignature);
591 clone_image=CloneImage(image,0,0,MagickTrue,exception);
592 if (clone_image == (Image *) NULL)
593 return((Image *) NULL);
cristy018f07f2011-09-04 21:15:19 +0000594 (void) SetImageType(clone_image,GrayscaleType,exception);
cristy8ae632d2011-09-05 17:29:53 +0000595 edge_image=EdgeImage(clone_image,radius,sigma,exception);
cristy3ed852e2009-09-05 21:47:34 +0000596 clone_image=DestroyImage(clone_image);
597 if (edge_image == (Image *) NULL)
598 return((Image *) NULL);
cristy05c0c9a2011-09-05 23:16:13 +0000599 charcoal_image=BlurImage(edge_image,radius,sigma,bias,exception);
cristy3ed852e2009-09-05 21:47:34 +0000600 edge_image=DestroyImage(edge_image);
601 if (charcoal_image == (Image *) NULL)
602 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +0000603 (void) NormalizeImage(charcoal_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +0000604 (void) NegateImage(charcoal_image,MagickFalse,exception);
cristy018f07f2011-09-04 21:15:19 +0000605 (void) SetImageType(charcoal_image,GrayscaleType,exception);
cristy3ed852e2009-09-05 21:47:34 +0000606 return(charcoal_image);
607}
608
609/*
610%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
611% %
612% %
613% %
614% C o l o r i z e I m a g e %
615% %
616% %
617% %
618%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
619%
620% ColorizeImage() blends the fill color with each pixel in the image.
621% A percentage blend is specified with opacity. Control the application
622% of different color components by specifying a different percentage for
623% each component (e.g. 90/100/10 is 90% red, 100% green, and 10% blue).
624%
625% The format of the ColorizeImage method is:
626%
cristyc7e6ff62011-10-03 13:46:11 +0000627% Image *ColorizeImage(const Image *image,const char *blend,
628% const PixelInfo *colorize,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000629%
630% A description of each parameter follows:
631%
632% o image: the image.
633%
cristyc7e6ff62011-10-03 13:46:11 +0000634% o blend: A character string indicating the level of blending as a
cristy3ed852e2009-09-05 21:47:34 +0000635% percentage.
636%
637% o colorize: A color value.
638%
639% o exception: return any errors or warnings in this structure.
640%
641*/
cristyc7e6ff62011-10-03 13:46:11 +0000642MagickExport Image *ColorizeImage(const Image *image,const char *blend,
643 const PixelInfo *colorize,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000644{
645#define ColorizeImageTag "Colorize/Image"
646
cristyc4c8d132010-01-07 01:58:38 +0000647 CacheView
648 *colorize_view,
649 *image_view;
650
cristy3ed852e2009-09-05 21:47:34 +0000651 GeometryInfo
652 geometry_info;
653
654 Image
655 *colorize_image;
656
cristy3ed852e2009-09-05 21:47:34 +0000657 MagickBooleanType
658 status;
659
cristybb503372010-05-27 20:51:26 +0000660 MagickOffsetType
661 progress;
662
cristy3ed852e2009-09-05 21:47:34 +0000663 MagickStatusType
664 flags;
665
cristyc7e6ff62011-10-03 13:46:11 +0000666 PixelInfo
667 pixel;
668
cristybb503372010-05-27 20:51:26 +0000669 ssize_t
670 y;
671
cristy3ed852e2009-09-05 21:47:34 +0000672 /*
673 Allocate colorized image.
674 */
675 assert(image != (const Image *) NULL);
676 assert(image->signature == MagickSignature);
677 if (image->debug != MagickFalse)
678 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
679 assert(exception != (ExceptionInfo *) NULL);
680 assert(exception->signature == MagickSignature);
681 colorize_image=CloneImage(image,image->columns,image->rows,MagickTrue,
682 exception);
683 if (colorize_image == (Image *) NULL)
684 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000685 if (SetImageStorageClass(colorize_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000686 {
cristy3ed852e2009-09-05 21:47:34 +0000687 colorize_image=DestroyImage(colorize_image);
688 return((Image *) NULL);
689 }
cristyc7e6ff62011-10-03 13:46:11 +0000690 if (blend == (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000691 return(colorize_image);
692 /*
693 Determine RGB values of the pen color.
694 */
cristyc7e6ff62011-10-03 13:46:11 +0000695 GetPixelInfo(image,&pixel);
cristyb817c3f2011-10-03 14:00:35 +0000696 flags=ParseGeometry(blend,&geometry_info);
cristy3ed852e2009-09-05 21:47:34 +0000697 pixel.red=geometry_info.rho;
698 pixel.green=geometry_info.rho;
699 pixel.blue=geometry_info.rho;
cristyc7e6ff62011-10-03 13:46:11 +0000700 pixel.alpha=100.0;
cristy3ed852e2009-09-05 21:47:34 +0000701 if ((flags & SigmaValue) != 0)
702 pixel.green=geometry_info.sigma;
703 if ((flags & XiValue) != 0)
704 pixel.blue=geometry_info.xi;
705 if ((flags & PsiValue) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000706 pixel.alpha=geometry_info.psi;
cristyc7e6ff62011-10-03 13:46:11 +0000707 if (pixel.colorspace == CMYKColorspace)
708 {
709 pixel.black=geometry_info.rho;
710 if ((flags & PsiValue) != 0)
711 pixel.black=geometry_info.psi;
712 if ((flags & ChiValue) != 0)
713 pixel.alpha=geometry_info.chi;
714 }
cristy3ed852e2009-09-05 21:47:34 +0000715 /*
716 Colorize DirectClass image.
717 */
718 status=MagickTrue;
719 progress=0;
720 image_view=AcquireCacheView(image);
721 colorize_view=AcquireCacheView(colorize_image);
cristy319a1e72010-02-21 15:13:11 +0000722#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000723 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000724#endif
cristybb503372010-05-27 20:51:26 +0000725 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000726 {
727 MagickBooleanType
728 sync;
729
cristy4c08aed2011-07-01 19:47:50 +0000730 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000731 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000732
cristybb503372010-05-27 20:51:26 +0000733 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000734 x;
735
cristy4c08aed2011-07-01 19:47:50 +0000736 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000737 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000738
739 if (status == MagickFalse)
740 continue;
741 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
742 q=QueueCacheViewAuthenticPixels(colorize_view,0,y,colorize_image->columns,1,
743 exception);
cristy4c08aed2011-07-01 19:47:50 +0000744 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000745 {
746 status=MagickFalse;
747 continue;
748 }
cristybb503372010-05-27 20:51:26 +0000749 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000750 {
cristyc7e6ff62011-10-03 13:46:11 +0000751 register ssize_t
752 i;
753
754 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
755 {
756 PixelChannel
757 channel;
758
759 PixelTrait
760 colorize_traits,
761 traits;
762
cristye2a912b2011-12-05 20:02:07 +0000763 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +0000764 traits=GetPixelChannelMapTraits(image,channel);
cristyc7e6ff62011-10-03 13:46:11 +0000765 colorize_traits=GetPixelChannelMapTraits(colorize_image,channel);
766 if ((traits == UndefinedPixelTrait) ||
767 (colorize_traits == UndefinedPixelTrait))
768 continue;
769 if ((colorize_traits & CopyPixelTrait) != 0)
770 {
771 SetPixelChannel(colorize_image,channel,p[i],q);
772 continue;
773 }
774 switch (channel)
775 {
776 case RedPixelChannel:
777 {
778 SetPixelChannel(colorize_image,channel,ClampToQuantum((p[i]*
779 (100.0-pixel.red)+colorize->red*pixel.red)/100.0),q);
780 break;
781 }
782 case GreenPixelChannel:
783 {
784 SetPixelChannel(colorize_image,channel,ClampToQuantum((p[i]*
785 (100.0-pixel.green)+colorize->green*pixel.green)/100.0),q);
786 break;
787 }
788 case BluePixelChannel:
789 {
790 SetPixelChannel(colorize_image,channel,ClampToQuantum((p[i]*
791 (100.0-pixel.blue)+colorize->blue*pixel.blue)/100.0),q);
792 break;
793 }
794 case BlackPixelChannel:
795 {
796 SetPixelChannel(colorize_image,channel,ClampToQuantum((p[i]*
797 (100.0-pixel.black)+colorize->black*pixel.black)/100.0),q);
798 break;
799 }
800 case AlphaPixelChannel:
801 {
802 SetPixelChannel(colorize_image,channel,ClampToQuantum((p[i]*
803 (100.0-pixel.alpha)+colorize->alpha*pixel.alpha)/100.0),q);
804 break;
805 }
806 default:
807 {
808 SetPixelChannel(colorize_image,channel,p[i],q);
809 break;
810 }
811 }
812 }
cristyed231572011-07-14 02:18:59 +0000813 p+=GetPixelChannels(image);
814 q+=GetPixelChannels(colorize_image);
cristy3ed852e2009-09-05 21:47:34 +0000815 }
816 sync=SyncCacheViewAuthenticPixels(colorize_view,exception);
817 if (sync == MagickFalse)
818 status=MagickFalse;
819 if (image->progress_monitor != (MagickProgressMonitor) NULL)
820 {
821 MagickBooleanType
822 proceed;
823
cristy319a1e72010-02-21 15:13:11 +0000824#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000825 #pragma omp critical (MagickCore_ColorizeImage)
826#endif
827 proceed=SetImageProgress(image,ColorizeImageTag,progress++,image->rows);
828 if (proceed == MagickFalse)
829 status=MagickFalse;
830 }
831 }
832 image_view=DestroyCacheView(image_view);
833 colorize_view=DestroyCacheView(colorize_view);
834 if (status == MagickFalse)
835 colorize_image=DestroyImage(colorize_image);
836 return(colorize_image);
837}
838
839/*
840%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
841% %
842% %
843% %
cristye6365592010-04-02 17:31:23 +0000844% C o l o r M a t r i x I m a g e %
845% %
846% %
847% %
848%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
849%
850% ColorMatrixImage() applies color transformation to an image. This method
851% permits saturation changes, hue rotation, luminance to alpha, and various
852% other effects. Although variable-sized transformation matrices can be used,
853% typically one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA
854% (or RGBA with offsets). The matrix is similar to those used by Adobe Flash
855% except offsets are in column 6 rather than 5 (in support of CMYKA images)
856% and offsets are normalized (divide Flash offset by 255).
857%
858% The format of the ColorMatrixImage method is:
859%
860% Image *ColorMatrixImage(const Image *image,
861% const KernelInfo *color_matrix,ExceptionInfo *exception)
862%
863% A description of each parameter follows:
864%
865% o image: the image.
866%
867% o color_matrix: the color matrix.
868%
869% o exception: return any errors or warnings in this structure.
870%
871*/
872MagickExport Image *ColorMatrixImage(const Image *image,
873 const KernelInfo *color_matrix,ExceptionInfo *exception)
874{
875#define ColorMatrixImageTag "ColorMatrix/Image"
876
877 CacheView
878 *color_view,
879 *image_view;
880
881 double
882 ColorMatrix[6][6] =
883 {
884 { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
885 { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
886 { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
887 { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
888 { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
889 { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }
890 };
891
892 Image
893 *color_image;
894
cristye6365592010-04-02 17:31:23 +0000895 MagickBooleanType
896 status;
897
cristybb503372010-05-27 20:51:26 +0000898 MagickOffsetType
899 progress;
900
901 register ssize_t
cristye6365592010-04-02 17:31:23 +0000902 i;
903
cristybb503372010-05-27 20:51:26 +0000904 ssize_t
905 u,
906 v,
907 y;
908
cristye6365592010-04-02 17:31:23 +0000909 /*
910 Create color matrix.
911 */
912 assert(image != (Image *) NULL);
913 assert(image->signature == MagickSignature);
914 if (image->debug != MagickFalse)
915 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
916 assert(exception != (ExceptionInfo *) NULL);
917 assert(exception->signature == MagickSignature);
918 i=0;
cristybb503372010-05-27 20:51:26 +0000919 for (v=0; v < (ssize_t) color_matrix->height; v++)
920 for (u=0; u < (ssize_t) color_matrix->width; u++)
cristye6365592010-04-02 17:31:23 +0000921 {
922 if ((v < 6) && (u < 6))
923 ColorMatrix[v][u]=color_matrix->values[i];
924 i++;
925 }
926 /*
927 Initialize color image.
928 */
cristy12550e62010-06-07 12:46:40 +0000929 color_image=CloneImage(image,0,0,MagickTrue,exception);
cristye6365592010-04-02 17:31:23 +0000930 if (color_image == (Image *) NULL)
931 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000932 if (SetImageStorageClass(color_image,DirectClass,exception) == MagickFalse)
cristye6365592010-04-02 17:31:23 +0000933 {
cristye6365592010-04-02 17:31:23 +0000934 color_image=DestroyImage(color_image);
935 return((Image *) NULL);
936 }
937 if (image->debug != MagickFalse)
938 {
939 char
940 format[MaxTextExtent],
941 *message;
942
943 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
944 " ColorMatrix image with color matrix:");
945 message=AcquireString("");
946 for (v=0; v < 6; v++)
947 {
948 *message='\0';
cristyb51dff52011-05-19 16:55:47 +0000949 (void) FormatLocaleString(format,MaxTextExtent,"%.20g: ",(double) v);
cristye6365592010-04-02 17:31:23 +0000950 (void) ConcatenateString(&message,format);
951 for (u=0; u < 6; u++)
952 {
cristyb51dff52011-05-19 16:55:47 +0000953 (void) FormatLocaleString(format,MaxTextExtent,"%+f ",
cristye6365592010-04-02 17:31:23 +0000954 ColorMatrix[v][u]);
955 (void) ConcatenateString(&message,format);
956 }
957 (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
958 }
959 message=DestroyString(message);
960 }
961 /*
962 ColorMatrix image.
963 */
964 status=MagickTrue;
965 progress=0;
966 image_view=AcquireCacheView(image);
967 color_view=AcquireCacheView(color_image);
968#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000969 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristye6365592010-04-02 17:31:23 +0000970#endif
cristybb503372010-05-27 20:51:26 +0000971 for (y=0; y < (ssize_t) image->rows; y++)
cristye6365592010-04-02 17:31:23 +0000972 {
973 MagickRealType
974 pixel;
975
cristy4c08aed2011-07-01 19:47:50 +0000976 register const Quantum
cristye6365592010-04-02 17:31:23 +0000977 *restrict p;
978
cristy4c08aed2011-07-01 19:47:50 +0000979 register Quantum
980 *restrict q;
981
cristybb503372010-05-27 20:51:26 +0000982 register ssize_t
cristye6365592010-04-02 17:31:23 +0000983 x;
984
cristye6365592010-04-02 17:31:23 +0000985 if (status == MagickFalse)
986 continue;
987 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
988 q=GetCacheViewAuthenticPixels(color_view,0,y,color_image->columns,1,
989 exception);
cristy4c08aed2011-07-01 19:47:50 +0000990 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristye6365592010-04-02 17:31:23 +0000991 {
992 status=MagickFalse;
993 continue;
994 }
cristybb503372010-05-27 20:51:26 +0000995 for (x=0; x < (ssize_t) image->columns; x++)
cristye6365592010-04-02 17:31:23 +0000996 {
cristybb503372010-05-27 20:51:26 +0000997 register ssize_t
cristye6365592010-04-02 17:31:23 +0000998 v;
999
cristybb503372010-05-27 20:51:26 +00001000 size_t
cristye6365592010-04-02 17:31:23 +00001001 height;
1002
1003 height=color_matrix->height > 6 ? 6UL : color_matrix->height;
cristybb503372010-05-27 20:51:26 +00001004 for (v=0; v < (ssize_t) height; v++)
cristye6365592010-04-02 17:31:23 +00001005 {
cristy4c08aed2011-07-01 19:47:50 +00001006 pixel=ColorMatrix[v][0]*GetPixelRed(image,p)+ColorMatrix[v][1]*
1007 GetPixelGreen(image,p)+ColorMatrix[v][2]*GetPixelBlue(image,p);
cristye6365592010-04-02 17:31:23 +00001008 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00001009 pixel+=ColorMatrix[v][3]*GetPixelBlack(image,p);
1010 if (image->matte != MagickFalse)
1011 pixel+=ColorMatrix[v][4]*GetPixelAlpha(image,p);
cristye6365592010-04-02 17:31:23 +00001012 pixel+=QuantumRange*ColorMatrix[v][5];
1013 switch (v)
1014 {
cristy4c08aed2011-07-01 19:47:50 +00001015 case 0: SetPixelRed(color_image,ClampToQuantum(pixel),q); break;
1016 case 1: SetPixelGreen(color_image,ClampToQuantum(pixel),q); break;
1017 case 2: SetPixelBlue(color_image,ClampToQuantum(pixel),q); break;
cristye6365592010-04-02 17:31:23 +00001018 case 3:
1019 {
cristy4c08aed2011-07-01 19:47:50 +00001020 if (image->colorspace == CMYKColorspace)
1021 SetPixelBlack(color_image,ClampToQuantum(pixel),q);
cristye6365592010-04-02 17:31:23 +00001022 break;
1023 }
1024 case 4:
1025 {
cristy4c08aed2011-07-01 19:47:50 +00001026 if (image->matte != MagickFalse)
1027 SetPixelAlpha(color_image,ClampToQuantum(pixel),q);
cristye6365592010-04-02 17:31:23 +00001028 break;
1029 }
1030 }
1031 }
cristyed231572011-07-14 02:18:59 +00001032 p+=GetPixelChannels(image);
1033 q+=GetPixelChannels(color_image);
cristye6365592010-04-02 17:31:23 +00001034 }
1035 if (SyncCacheViewAuthenticPixels(color_view,exception) == MagickFalse)
1036 status=MagickFalse;
1037 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1038 {
1039 MagickBooleanType
1040 proceed;
1041
1042#if defined(MAGICKCORE_OPENMP_SUPPORT)
1043 #pragma omp critical (MagickCore_ColorMatrixImage)
1044#endif
1045 proceed=SetImageProgress(image,ColorMatrixImageTag,progress++,
1046 image->rows);
1047 if (proceed == MagickFalse)
1048 status=MagickFalse;
1049 }
1050 }
1051 color_view=DestroyCacheView(color_view);
1052 image_view=DestroyCacheView(image_view);
1053 if (status == MagickFalse)
1054 color_image=DestroyImage(color_image);
1055 return(color_image);
1056}
1057
1058/*
1059%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1060% %
1061% %
1062% %
cristy3ed852e2009-09-05 21:47:34 +00001063+ D e s t r o y F x I n f o %
1064% %
1065% %
1066% %
1067%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1068%
1069% DestroyFxInfo() deallocates memory associated with an FxInfo structure.
1070%
1071% The format of the DestroyFxInfo method is:
1072%
1073% ImageInfo *DestroyFxInfo(ImageInfo *fx_info)
1074%
1075% A description of each parameter follows:
1076%
1077% o fx_info: the fx info.
1078%
1079*/
cristy7832dc22011-09-05 01:21:53 +00001080MagickPrivate FxInfo *DestroyFxInfo(FxInfo *fx_info)
cristy3ed852e2009-09-05 21:47:34 +00001081{
cristybb503372010-05-27 20:51:26 +00001082 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001083 i;
1084
1085 fx_info->exception=DestroyExceptionInfo(fx_info->exception);
1086 fx_info->expression=DestroyString(fx_info->expression);
1087 fx_info->symbols=DestroySplayTree(fx_info->symbols);
1088 fx_info->colors=DestroySplayTree(fx_info->colors);
cristy0ea377f2011-03-24 00:54:19 +00001089 for (i=(ssize_t) GetImageListLength(fx_info->images)-1; i >= 0; i--)
cristyd76c51e2011-03-26 00:21:26 +00001090 fx_info->view[i]=DestroyCacheView(fx_info->view[i]);
1091 fx_info->view=(CacheView **) RelinquishMagickMemory(fx_info->view);
cristy3ed852e2009-09-05 21:47:34 +00001092 fx_info->random_info=DestroyRandomInfo(fx_info->random_info);
1093 fx_info=(FxInfo *) RelinquishMagickMemory(fx_info);
1094 return(fx_info);
1095}
1096
1097/*
1098%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1099% %
1100% %
1101% %
cristy3ed852e2009-09-05 21:47:34 +00001102+ 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 %
1103% %
1104% %
1105% %
1106%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1107%
1108% FxEvaluateChannelExpression() evaluates an expression and returns the
1109% results.
1110%
1111% The format of the FxEvaluateExpression method is:
1112%
1113% MagickRealType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00001114% const PixelChannel channel,const ssize_t x,const ssize_t y,
cristy3ed852e2009-09-05 21:47:34 +00001115% MagickRealType *alpha,Exceptioninfo *exception)
1116% MagickRealType FxEvaluateExpression(FxInfo *fx_info,
1117% MagickRealType *alpha,Exceptioninfo *exception)
1118%
1119% A description of each parameter follows:
1120%
1121% o fx_info: the fx info.
1122%
1123% o channel: the channel.
1124%
1125% o x,y: the pixel position.
1126%
1127% o alpha: the result.
1128%
1129% o exception: return any errors or warnings in this structure.
1130%
1131*/
1132
cristy351842f2010-03-07 15:27:38 +00001133static inline double MagickMax(const double x,const double y)
1134{
1135 if (x > y)
1136 return(x);
1137 return(y);
1138}
1139
1140static inline double MagickMin(const double x,const double y)
1141{
1142 if (x < y)
1143 return(x);
1144 return(y);
1145}
1146
cristy3ed852e2009-09-05 21:47:34 +00001147static MagickRealType FxChannelStatistics(FxInfo *fx_info,const Image *image,
cristy0568ffc2011-07-25 16:54:14 +00001148 PixelChannel channel,const char *symbol,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001149{
1150 char
1151 key[MaxTextExtent],
1152 statistic[MaxTextExtent];
1153
1154 const char
1155 *value;
1156
1157 register const char
1158 *p;
1159
1160 for (p=symbol; (*p != '.') && (*p != '\0'); p++) ;
1161 if (*p == '.')
1162 switch (*++p) /* e.g. depth.r */
1163 {
cristy541ae572011-08-05 19:08:59 +00001164 case 'r': channel=RedPixelChannel; break;
1165 case 'g': channel=GreenPixelChannel; break;
1166 case 'b': channel=BluePixelChannel; break;
1167 case 'c': channel=CyanPixelChannel; break;
1168 case 'm': channel=MagentaPixelChannel; break;
1169 case 'y': channel=YellowPixelChannel; break;
1170 case 'k': channel=BlackPixelChannel; break;
cristy3ed852e2009-09-05 21:47:34 +00001171 default: break;
1172 }
cristyb51dff52011-05-19 16:55:47 +00001173 (void) FormatLocaleString(key,MaxTextExtent,"%p.%.20g.%s",(void *) image,
cristye8c25f92010-06-03 00:53:06 +00001174 (double) channel,symbol);
cristy3ed852e2009-09-05 21:47:34 +00001175 value=(const char *) GetValueFromSplayTree(fx_info->symbols,key);
1176 if (value != (const char *) NULL)
cristydbdd0e32011-11-04 23:29:40 +00001177 return(QuantumScale*StringToDouble(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001178 (void) DeleteNodeFromSplayTree(fx_info->symbols,key);
1179 if (LocaleNCompare(symbol,"depth",5) == 0)
1180 {
cristybb503372010-05-27 20:51:26 +00001181 size_t
cristy3ed852e2009-09-05 21:47:34 +00001182 depth;
1183
cristyfefab1b2011-07-05 00:33:22 +00001184 depth=GetImageDepth(image,exception);
1185 (void) FormatLocaleString(statistic,MaxTextExtent,"%.20g",(double) depth);
cristy3ed852e2009-09-05 21:47:34 +00001186 }
1187 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1188 {
1189 double
1190 kurtosis,
1191 skewness;
1192
cristyd42d9952011-07-08 14:21:50 +00001193 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001194 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",kurtosis);
cristy3ed852e2009-09-05 21:47:34 +00001195 }
1196 if (LocaleNCompare(symbol,"maxima",6) == 0)
1197 {
1198 double
1199 maxima,
1200 minima;
1201
cristyd42d9952011-07-08 14:21:50 +00001202 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001203 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",maxima);
cristy3ed852e2009-09-05 21:47:34 +00001204 }
1205 if (LocaleNCompare(symbol,"mean",4) == 0)
1206 {
1207 double
1208 mean,
1209 standard_deviation;
1210
cristyd42d9952011-07-08 14:21:50 +00001211 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001212 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",mean);
cristy3ed852e2009-09-05 21:47:34 +00001213 }
1214 if (LocaleNCompare(symbol,"minima",6) == 0)
1215 {
1216 double
1217 maxima,
1218 minima;
1219
cristyd42d9952011-07-08 14:21:50 +00001220 (void) GetImageRange(image,&minima,&maxima,exception);
cristyb51dff52011-05-19 16:55:47 +00001221 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",minima);
cristy3ed852e2009-09-05 21:47:34 +00001222 }
1223 if (LocaleNCompare(symbol,"skewness",8) == 0)
1224 {
1225 double
1226 kurtosis,
1227 skewness;
1228
cristyd42d9952011-07-08 14:21:50 +00001229 (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
cristyb51dff52011-05-19 16:55:47 +00001230 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",skewness);
cristy3ed852e2009-09-05 21:47:34 +00001231 }
1232 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1233 {
1234 double
1235 mean,
1236 standard_deviation;
1237
cristyd42d9952011-07-08 14:21:50 +00001238 (void) GetImageMean(image,&mean,&standard_deviation,exception);
cristyb51dff52011-05-19 16:55:47 +00001239 (void) FormatLocaleString(statistic,MaxTextExtent,"%g",
cristy3ed852e2009-09-05 21:47:34 +00001240 standard_deviation);
1241 }
1242 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(key),
1243 ConstantString(statistic));
cristydbdd0e32011-11-04 23:29:40 +00001244 return(QuantumScale*StringToDouble(statistic,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001245}
1246
1247static MagickRealType
cristy0568ffc2011-07-25 16:54:14 +00001248 FxEvaluateSubexpression(FxInfo *,const PixelChannel,const ssize_t,
cristye85007d2010-06-06 22:51:36 +00001249 const ssize_t,const char *,MagickRealType *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +00001250
cristyb0aad4c2011-11-02 19:30:35 +00001251static MagickOffsetType FxGCD(MagickOffsetType alpha,MagickOffsetType beta)
1252{
1253 if (beta != 0)
1254 return(FxGCD(beta,alpha % beta));
1255 return(alpha);
1256}
1257
cristy3ed852e2009-09-05 21:47:34 +00001258static inline const char *FxSubexpression(const char *expression,
1259 ExceptionInfo *exception)
1260{
1261 const char
1262 *subexpression;
1263
cristybb503372010-05-27 20:51:26 +00001264 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001265 level;
1266
1267 level=0;
1268 subexpression=expression;
1269 while ((*subexpression != '\0') &&
1270 ((level != 1) || (strchr(")",(int) *subexpression) == (char *) NULL)))
1271 {
1272 if (strchr("(",(int) *subexpression) != (char *) NULL)
1273 level++;
1274 else
1275 if (strchr(")",(int) *subexpression) != (char *) NULL)
1276 level--;
1277 subexpression++;
1278 }
1279 if (*subexpression == '\0')
1280 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1281 "UnbalancedParenthesis","`%s'",expression);
1282 return(subexpression);
1283}
1284
cristy0568ffc2011-07-25 16:54:14 +00001285static MagickRealType FxGetSymbol(FxInfo *fx_info,const PixelChannel channel,
cristye85007d2010-06-06 22:51:36 +00001286 const ssize_t x,const ssize_t y,const char *expression,
1287 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001288{
1289 char
1290 *q,
1291 subexpression[MaxTextExtent],
1292 symbol[MaxTextExtent];
1293
1294 const char
1295 *p,
1296 *value;
1297
1298 Image
1299 *image;
1300
cristy4c08aed2011-07-01 19:47:50 +00001301 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001302 pixel;
1303
1304 MagickRealType
1305 alpha,
1306 beta;
1307
1308 PointInfo
1309 point;
1310
cristybb503372010-05-27 20:51:26 +00001311 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001312 i;
1313
1314 size_t
cristy1707c6c2012-01-18 23:30:54 +00001315 length,
cristy3ed852e2009-09-05 21:47:34 +00001316 level;
1317
1318 p=expression;
1319 i=GetImageIndexInList(fx_info->images);
1320 level=0;
1321 point.x=(double) x;
1322 point.y=(double) y;
1323 if (isalpha((int) *(p+1)) == 0)
1324 {
1325 if (strchr("suv",(int) *p) != (char *) NULL)
1326 {
1327 switch (*p)
1328 {
1329 case 's':
1330 default:
1331 {
1332 i=GetImageIndexInList(fx_info->images);
1333 break;
1334 }
1335 case 'u': i=0; break;
1336 case 'v': i=1; break;
1337 }
1338 p++;
1339 if (*p == '[')
1340 {
1341 level++;
1342 q=subexpression;
1343 for (p++; *p != '\0'; )
1344 {
1345 if (*p == '[')
1346 level++;
1347 else
1348 if (*p == ']')
1349 {
1350 level--;
1351 if (level == 0)
1352 break;
1353 }
1354 *q++=(*p++);
1355 }
1356 *q='\0';
1357 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1358 &beta,exception);
cristybb503372010-05-27 20:51:26 +00001359 i=(ssize_t) (alpha+0.5);
cristy3ed852e2009-09-05 21:47:34 +00001360 p++;
1361 }
1362 if (*p == '.')
1363 p++;
1364 }
1365 if ((isalpha((int) *(p+1)) == 0) && (*p == 'p'))
1366 {
1367 p++;
1368 if (*p == '{')
1369 {
1370 level++;
1371 q=subexpression;
1372 for (p++; *p != '\0'; )
1373 {
1374 if (*p == '{')
1375 level++;
1376 else
1377 if (*p == '}')
1378 {
1379 level--;
1380 if (level == 0)
1381 break;
1382 }
1383 *q++=(*p++);
1384 }
1385 *q='\0';
1386 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1387 &beta,exception);
1388 point.x=alpha;
1389 point.y=beta;
1390 p++;
1391 }
1392 else
1393 if (*p == '[')
1394 {
1395 level++;
1396 q=subexpression;
1397 for (p++; *p != '\0'; )
1398 {
1399 if (*p == '[')
1400 level++;
1401 else
1402 if (*p == ']')
1403 {
1404 level--;
1405 if (level == 0)
1406 break;
1407 }
1408 *q++=(*p++);
1409 }
1410 *q='\0';
1411 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
1412 &beta,exception);
1413 point.x+=alpha;
1414 point.y+=beta;
1415 p++;
1416 }
1417 if (*p == '.')
1418 p++;
1419 }
1420 }
1421 length=GetImageListLength(fx_info->images);
1422 while (i < 0)
cristybb503372010-05-27 20:51:26 +00001423 i+=(ssize_t) length;
cristy3ed852e2009-09-05 21:47:34 +00001424 i%=length;
1425 image=GetImageFromList(fx_info->images,i);
1426 if (image == (Image *) NULL)
1427 {
1428 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1429 "NoSuchImage","`%s'",expression);
1430 return(0.0);
1431 }
cristy4c08aed2011-07-01 19:47:50 +00001432 GetPixelInfo(image,&pixel);
1433 (void) InterpolatePixelInfo(image,fx_info->view[i],image->interpolate,
cristy4f820712011-04-01 12:35:43 +00001434 point.x,point.y,&pixel,exception);
cristy1707c6c2012-01-18 23:30:54 +00001435 if ((strlen(p) > 2) && (LocaleCompare(p,"intensity") != 0) &&
1436 (LocaleCompare(p,"luminance") != 0) && (LocaleCompare(p,"hue") != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00001437 (LocaleCompare(p,"saturation") != 0) &&
1438 (LocaleCompare(p,"lightness") != 0))
1439 {
1440 char
1441 name[MaxTextExtent];
1442
1443 (void) CopyMagickString(name,p,MaxTextExtent);
1444 for (q=name+(strlen(name)-1); q > name; q--)
1445 {
1446 if (*q == ')')
1447 break;
1448 if (*q == '.')
1449 {
1450 *q='\0';
1451 break;
1452 }
1453 }
1454 if ((strlen(name) > 2) &&
1455 (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
1456 {
cristy4c08aed2011-07-01 19:47:50 +00001457 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001458 *color;
1459
cristy4c08aed2011-07-01 19:47:50 +00001460 color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
1461 if (color != (PixelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001462 {
1463 pixel=(*color);
1464 p+=strlen(name);
1465 }
1466 else
cristy1707c6c2012-01-18 23:30:54 +00001467 {
1468 MagickBooleanType
1469 status;
1470
1471 status=QueryColorCompliance(name,AllCompliance,&pixel,
1472 fx_info->exception);
1473 if (status != MagickFalse)
1474 {
1475 (void) AddValueToSplayTree(fx_info->colors,ConstantString(
1476 name),ClonePixelInfo(&pixel));
1477 p+=strlen(name);
1478 }
1479 }
cristy3ed852e2009-09-05 21:47:34 +00001480 }
1481 }
1482 (void) CopyMagickString(symbol,p,MaxTextExtent);
1483 StripString(symbol);
1484 if (*symbol == '\0')
1485 {
1486 switch (channel)
1487 {
cristy0568ffc2011-07-25 16:54:14 +00001488 case RedPixelChannel: return(QuantumScale*pixel.red);
1489 case GreenPixelChannel: return(QuantumScale*pixel.green);
1490 case BluePixelChannel: return(QuantumScale*pixel.blue);
1491 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001492 {
1493 if (image->colorspace != CMYKColorspace)
1494 {
1495 (void) ThrowMagickException(exception,GetMagickModule(),
cristy1a020e42011-12-06 18:13:23 +00001496 ImageError,"ColorSeparatedImageRequired","`%s'",
cristy3ed852e2009-09-05 21:47:34 +00001497 image->filename);
1498 return(0.0);
1499 }
cristy4c08aed2011-07-01 19:47:50 +00001500 return(QuantumScale*pixel.black);
1501 }
cristy0568ffc2011-07-25 16:54:14 +00001502 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001503 {
1504 MagickRealType
1505 alpha;
1506
1507 if (pixel.matte == MagickFalse)
1508 return(1.0);
1509 alpha=(MagickRealType) (QuantumScale*pixel.alpha);
1510 return(alpha);
cristy3ed852e2009-09-05 21:47:34 +00001511 }
cristya382aca2011-12-06 18:22:48 +00001512 case IndexPixelChannel:
1513 return(0.0);
cristyb3a73b52011-07-26 01:34:43 +00001514 case IntensityPixelChannel:
cristyf364ed42010-12-15 01:54:43 +00001515 {
cristy4c08aed2011-07-01 19:47:50 +00001516 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristyf364ed42010-12-15 01:54:43 +00001517 }
cristy3ed852e2009-09-05 21:47:34 +00001518 default:
1519 break;
1520 }
1521 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1522 "UnableToParseExpression","`%s'",p);
1523 return(0.0);
1524 }
1525 switch (*symbol)
1526 {
1527 case 'A':
1528 case 'a':
1529 {
1530 if (LocaleCompare(symbol,"a") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001531 return((MagickRealType) (QuantumScale*pixel.alpha));
cristy3ed852e2009-09-05 21:47:34 +00001532 break;
1533 }
1534 case 'B':
1535 case 'b':
1536 {
1537 if (LocaleCompare(symbol,"b") == 0)
1538 return(QuantumScale*pixel.blue);
1539 break;
1540 }
1541 case 'C':
1542 case 'c':
1543 {
1544 if (LocaleNCompare(symbol,"channel",7) == 0)
1545 {
1546 GeometryInfo
1547 channel_info;
1548
1549 MagickStatusType
1550 flags;
1551
1552 flags=ParseGeometry(symbol+7,&channel_info);
1553 if (image->colorspace == CMYKColorspace)
1554 switch (channel)
1555 {
cristy0568ffc2011-07-25 16:54:14 +00001556 case CyanPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001557 {
1558 if ((flags & RhoValue) == 0)
1559 return(0.0);
1560 return(channel_info.rho);
1561 }
cristy0568ffc2011-07-25 16:54:14 +00001562 case MagentaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001563 {
1564 if ((flags & SigmaValue) == 0)
1565 return(0.0);
1566 return(channel_info.sigma);
1567 }
cristy0568ffc2011-07-25 16:54:14 +00001568 case YellowPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001569 {
1570 if ((flags & XiValue) == 0)
1571 return(0.0);
1572 return(channel_info.xi);
1573 }
cristy0568ffc2011-07-25 16:54:14 +00001574 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001575 {
1576 if ((flags & PsiValue) == 0)
1577 return(0.0);
1578 return(channel_info.psi);
1579 }
cristy0568ffc2011-07-25 16:54:14 +00001580 case AlphaPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001581 {
1582 if ((flags & ChiValue) == 0)
1583 return(0.0);
1584 return(channel_info.chi);
1585 }
1586 default:
1587 return(0.0);
1588 }
1589 switch (channel)
1590 {
cristy0568ffc2011-07-25 16:54:14 +00001591 case RedPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001592 {
1593 if ((flags & RhoValue) == 0)
1594 return(0.0);
1595 return(channel_info.rho);
1596 }
cristy0568ffc2011-07-25 16:54:14 +00001597 case GreenPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001598 {
1599 if ((flags & SigmaValue) == 0)
1600 return(0.0);
1601 return(channel_info.sigma);
1602 }
cristy0568ffc2011-07-25 16:54:14 +00001603 case BluePixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001604 {
1605 if ((flags & XiValue) == 0)
1606 return(0.0);
1607 return(channel_info.xi);
1608 }
cristy0568ffc2011-07-25 16:54:14 +00001609 case BlackPixelChannel:
cristy3ed852e2009-09-05 21:47:34 +00001610 {
1611 if ((flags & ChiValue) == 0)
1612 return(0.0);
1613 return(channel_info.chi);
1614 }
cristy0568ffc2011-07-25 16:54:14 +00001615 case AlphaPixelChannel:
cristy4c08aed2011-07-01 19:47:50 +00001616 {
1617 if ((flags & PsiValue) == 0)
1618 return(0.0);
1619 return(channel_info.psi);
1620 }
cristy3ed852e2009-09-05 21:47:34 +00001621 default:
1622 return(0.0);
1623 }
1624 return(0.0);
1625 }
1626 if (LocaleCompare(symbol,"c") == 0)
1627 return(QuantumScale*pixel.red);
1628 break;
1629 }
1630 case 'D':
1631 case 'd':
1632 {
1633 if (LocaleNCompare(symbol,"depth",5) == 0)
1634 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1635 break;
1636 }
1637 case 'G':
1638 case 'g':
1639 {
1640 if (LocaleCompare(symbol,"g") == 0)
1641 return(QuantumScale*pixel.green);
1642 break;
1643 }
1644 case 'K':
1645 case 'k':
1646 {
1647 if (LocaleNCompare(symbol,"kurtosis",8) == 0)
1648 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1649 if (LocaleCompare(symbol,"k") == 0)
1650 {
1651 if (image->colorspace != CMYKColorspace)
1652 {
1653 (void) ThrowMagickException(exception,GetMagickModule(),
1654 OptionError,"ColorSeparatedImageRequired","`%s'",
1655 image->filename);
1656 return(0.0);
1657 }
cristy4c08aed2011-07-01 19:47:50 +00001658 return(QuantumScale*pixel.black);
cristy3ed852e2009-09-05 21:47:34 +00001659 }
1660 break;
1661 }
1662 case 'H':
1663 case 'h':
1664 {
1665 if (LocaleCompare(symbol,"h") == 0)
1666 return((MagickRealType) image->rows);
1667 if (LocaleCompare(symbol,"hue") == 0)
1668 {
1669 double
1670 hue,
1671 lightness,
1672 saturation;
1673
cristyda1f9c12011-10-02 21:39:49 +00001674 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1675 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001676 return(hue);
1677 }
1678 break;
1679 }
1680 case 'I':
1681 case 'i':
1682 {
1683 if ((LocaleCompare(symbol,"image.depth") == 0) ||
1684 (LocaleCompare(symbol,"image.minima") == 0) ||
1685 (LocaleCompare(symbol,"image.maxima") == 0) ||
1686 (LocaleCompare(symbol,"image.mean") == 0) ||
1687 (LocaleCompare(symbol,"image.kurtosis") == 0) ||
1688 (LocaleCompare(symbol,"image.skewness") == 0) ||
1689 (LocaleCompare(symbol,"image.standard_deviation") == 0))
1690 return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
1691 if (LocaleCompare(symbol,"image.resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001692 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001693 if (LocaleCompare(symbol,"image.resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001694 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001695 if (LocaleCompare(symbol,"intensity") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001696 return(QuantumScale*GetPixelInfoIntensity(&pixel));
cristy3ed852e2009-09-05 21:47:34 +00001697 if (LocaleCompare(symbol,"i") == 0)
1698 return((MagickRealType) x);
1699 break;
1700 }
1701 case 'J':
1702 case 'j':
1703 {
1704 if (LocaleCompare(symbol,"j") == 0)
1705 return((MagickRealType) y);
1706 break;
1707 }
1708 case 'L':
1709 case 'l':
1710 {
1711 if (LocaleCompare(symbol,"lightness") == 0)
1712 {
1713 double
1714 hue,
1715 lightness,
1716 saturation;
1717
cristyda1f9c12011-10-02 21:39:49 +00001718 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1719 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001720 return(lightness);
1721 }
1722 if (LocaleCompare(symbol,"luminance") == 0)
1723 {
1724 double
1725 luminence;
1726
1727 luminence=0.2126*pixel.red+0.7152*pixel.green+0.0722*pixel.blue;
1728 return(QuantumScale*luminence);
1729 }
1730 break;
1731 }
1732 case 'M':
1733 case 'm':
1734 {
1735 if (LocaleNCompare(symbol,"maxima",6) == 0)
1736 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1737 if (LocaleNCompare(symbol,"mean",4) == 0)
1738 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1739 if (LocaleNCompare(symbol,"minima",6) == 0)
1740 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1741 if (LocaleCompare(symbol,"m") == 0)
1742 return(QuantumScale*pixel.blue);
1743 break;
1744 }
1745 case 'N':
1746 case 'n':
1747 {
1748 if (LocaleCompare(symbol,"n") == 0)
anthony374f5dd2011-03-25 10:08:53 +00001749 return((MagickRealType) GetImageListLength(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001750 break;
1751 }
1752 case 'O':
1753 case 'o':
1754 {
1755 if (LocaleCompare(symbol,"o") == 0)
cristy4c08aed2011-07-01 19:47:50 +00001756 return(QuantumScale*pixel.alpha);
cristy3ed852e2009-09-05 21:47:34 +00001757 break;
1758 }
1759 case 'P':
1760 case 'p':
1761 {
1762 if (LocaleCompare(symbol,"page.height") == 0)
1763 return((MagickRealType) image->page.height);
1764 if (LocaleCompare(symbol,"page.width") == 0)
1765 return((MagickRealType) image->page.width);
1766 if (LocaleCompare(symbol,"page.x") == 0)
1767 return((MagickRealType) image->page.x);
1768 if (LocaleCompare(symbol,"page.y") == 0)
1769 return((MagickRealType) image->page.y);
1770 break;
1771 }
1772 case 'R':
1773 case 'r':
1774 {
1775 if (LocaleCompare(symbol,"resolution.x") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001776 return(image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00001777 if (LocaleCompare(symbol,"resolution.y") == 0)
cristy2a11bef2011-10-28 18:33:11 +00001778 return(image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001779 if (LocaleCompare(symbol,"r") == 0)
1780 return(QuantumScale*pixel.red);
1781 break;
1782 }
1783 case 'S':
1784 case 's':
1785 {
1786 if (LocaleCompare(symbol,"saturation") == 0)
1787 {
1788 double
1789 hue,
1790 lightness,
1791 saturation;
1792
cristyda1f9c12011-10-02 21:39:49 +00001793 ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
1794 &lightness);
cristy3ed852e2009-09-05 21:47:34 +00001795 return(saturation);
1796 }
1797 if (LocaleNCompare(symbol,"skewness",8) == 0)
1798 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1799 if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
1800 return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
1801 break;
1802 }
1803 case 'T':
1804 case 't':
1805 {
1806 if (LocaleCompare(symbol,"t") == 0)
cristy5a15b932011-03-26 12:50:33 +00001807 return((MagickRealType) GetImageIndexInList(fx_info->images));
cristy3ed852e2009-09-05 21:47:34 +00001808 break;
1809 }
1810 case 'W':
1811 case 'w':
1812 {
1813 if (LocaleCompare(symbol,"w") == 0)
1814 return((MagickRealType) image->columns);
1815 break;
1816 }
1817 case 'Y':
1818 case 'y':
1819 {
1820 if (LocaleCompare(symbol,"y") == 0)
1821 return(QuantumScale*pixel.green);
1822 break;
1823 }
1824 case 'Z':
1825 case 'z':
1826 {
1827 if (LocaleCompare(symbol,"z") == 0)
1828 {
1829 MagickRealType
1830 depth;
1831
cristyfefab1b2011-07-05 00:33:22 +00001832 depth=(MagickRealType) GetImageDepth(image,fx_info->exception);
cristy3ed852e2009-09-05 21:47:34 +00001833 return(depth);
1834 }
1835 break;
1836 }
1837 default:
1838 break;
1839 }
1840 value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
1841 if (value != (const char *) NULL)
cristydbdd0e32011-11-04 23:29:40 +00001842 return((MagickRealType) StringToDouble(value,(char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00001843 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1844 "UnableToParseExpression","`%s'",symbol);
1845 return(0.0);
1846}
1847
1848static const char *FxOperatorPrecedence(const char *expression,
1849 ExceptionInfo *exception)
1850{
1851 typedef enum
1852 {
1853 UndefinedPrecedence,
1854 NullPrecedence,
1855 BitwiseComplementPrecedence,
1856 ExponentPrecedence,
cristy116af162010-08-13 01:25:47 +00001857 ExponentialNotationPrecedence,
cristy3ed852e2009-09-05 21:47:34 +00001858 MultiplyPrecedence,
1859 AdditionPrecedence,
1860 ShiftPrecedence,
1861 RelationalPrecedence,
1862 EquivalencyPrecedence,
1863 BitwiseAndPrecedence,
1864 BitwiseOrPrecedence,
1865 LogicalAndPrecedence,
1866 LogicalOrPrecedence,
1867 TernaryPrecedence,
1868 AssignmentPrecedence,
1869 CommaPrecedence,
1870 SeparatorPrecedence
1871 } FxPrecedence;
1872
1873 FxPrecedence
1874 precedence,
1875 target;
1876
1877 register const char
1878 *subexpression;
1879
1880 register int
1881 c;
1882
cristybb503372010-05-27 20:51:26 +00001883 size_t
cristy3ed852e2009-09-05 21:47:34 +00001884 level;
1885
1886 c=0;
1887 level=0;
1888 subexpression=(const char *) NULL;
1889 target=NullPrecedence;
1890 while (*expression != '\0')
1891 {
1892 precedence=UndefinedPrecedence;
1893 if ((isspace((int) ((char) *expression)) != 0) || (c == (int) '@'))
1894 {
1895 expression++;
1896 continue;
1897 }
cristy488fa882010-03-01 22:34:24 +00001898 switch (*expression)
1899 {
1900 case 'A':
1901 case 'a':
cristy3ed852e2009-09-05 21:47:34 +00001902 {
cristyb33454f2011-08-03 02:10:45 +00001903#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00001904 if (LocaleNCompare(expression,"acosh",5) == 0)
1905 {
1906 expression+=5;
1907 break;
1908 }
cristyb33454f2011-08-03 02:10:45 +00001909#endif
1910#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00001911 if (LocaleNCompare(expression,"asinh",5) == 0)
1912 {
1913 expression+=5;
1914 break;
1915 }
cristyb33454f2011-08-03 02:10:45 +00001916#endif
1917#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00001918 if (LocaleNCompare(expression,"atanh",5) == 0)
cristy488fa882010-03-01 22:34:24 +00001919 {
1920 expression+=5;
1921 break;
1922 }
cristyb33454f2011-08-03 02:10:45 +00001923#endif
cristy488fa882010-03-01 22:34:24 +00001924 break;
cristy3ed852e2009-09-05 21:47:34 +00001925 }
cristy62d455f2011-11-03 11:42:28 +00001926 case 'E':
1927 case 'e':
1928 {
1929 if ((LocaleNCompare(expression,"E+",2) == 0) ||
1930 (LocaleNCompare(expression,"E-",2) == 0))
1931 {
1932 expression+=2; /* scientific notation */
1933 break;
1934 }
1935 }
cristy488fa882010-03-01 22:34:24 +00001936 case 'J':
1937 case 'j':
1938 {
1939 if ((LocaleNCompare(expression,"j0",2) == 0) ||
1940 (LocaleNCompare(expression,"j1",2) == 0))
1941 {
1942 expression+=2;
1943 break;
1944 }
1945 break;
1946 }
cristy2def9322010-06-18 23:59:37 +00001947 case '#':
1948 {
1949 while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
1950 expression++;
1951 break;
1952 }
cristy488fa882010-03-01 22:34:24 +00001953 default:
1954 break;
1955 }
cristy3ed852e2009-09-05 21:47:34 +00001956 if ((c == (int) '{') || (c == (int) '['))
1957 level++;
1958 else
1959 if ((c == (int) '}') || (c == (int) ']'))
1960 level--;
1961 if (level == 0)
1962 switch ((unsigned char) *expression)
1963 {
1964 case '~':
1965 case '!':
1966 {
1967 precedence=BitwiseComplementPrecedence;
1968 break;
1969 }
1970 case '^':
cristy6621e252010-08-13 00:42:57 +00001971 case '@':
cristy3ed852e2009-09-05 21:47:34 +00001972 {
1973 precedence=ExponentPrecedence;
1974 break;
1975 }
1976 default:
1977 {
1978 if (((c != 0) && ((isdigit((int) ((char) c)) != 0) ||
1979 (strchr(")",c) != (char *) NULL))) &&
1980 (((islower((int) ((char) *expression)) != 0) ||
1981 (strchr("(",(int) *expression) != (char *) NULL)) ||
1982 ((isdigit((int) ((char) c)) == 0) &&
1983 (isdigit((int) ((char) *expression)) != 0))) &&
1984 (strchr("xy",(int) *expression) == (char *) NULL))
1985 precedence=MultiplyPrecedence;
1986 break;
1987 }
1988 case '*':
1989 case '/':
1990 case '%':
1991 {
1992 precedence=MultiplyPrecedence;
1993 break;
1994 }
1995 case '+':
1996 case '-':
1997 {
1998 if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
1999 (isalpha(c) != 0))
2000 precedence=AdditionPrecedence;
2001 break;
2002 }
2003 case LeftShiftOperator:
2004 case RightShiftOperator:
2005 {
2006 precedence=ShiftPrecedence;
2007 break;
2008 }
2009 case '<':
2010 case LessThanEqualOperator:
2011 case GreaterThanEqualOperator:
2012 case '>':
2013 {
2014 precedence=RelationalPrecedence;
2015 break;
2016 }
2017 case EqualOperator:
2018 case NotEqualOperator:
2019 {
2020 precedence=EquivalencyPrecedence;
2021 break;
2022 }
2023 case '&':
2024 {
2025 precedence=BitwiseAndPrecedence;
2026 break;
2027 }
2028 case '|':
2029 {
2030 precedence=BitwiseOrPrecedence;
2031 break;
2032 }
2033 case LogicalAndOperator:
2034 {
2035 precedence=LogicalAndPrecedence;
2036 break;
2037 }
2038 case LogicalOrOperator:
2039 {
2040 precedence=LogicalOrPrecedence;
2041 break;
2042 }
cristy116af162010-08-13 01:25:47 +00002043 case ExponentialNotation:
2044 {
2045 precedence=ExponentialNotationPrecedence;
2046 break;
2047 }
cristy3ed852e2009-09-05 21:47:34 +00002048 case ':':
2049 case '?':
2050 {
2051 precedence=TernaryPrecedence;
2052 break;
2053 }
2054 case '=':
2055 {
2056 precedence=AssignmentPrecedence;
2057 break;
2058 }
2059 case ',':
2060 {
2061 precedence=CommaPrecedence;
2062 break;
2063 }
2064 case ';':
2065 {
2066 precedence=SeparatorPrecedence;
2067 break;
2068 }
2069 }
2070 if ((precedence == BitwiseComplementPrecedence) ||
2071 (precedence == TernaryPrecedence) ||
2072 (precedence == AssignmentPrecedence))
2073 {
2074 if (precedence > target)
2075 {
2076 /*
2077 Right-to-left associativity.
2078 */
2079 target=precedence;
2080 subexpression=expression;
2081 }
2082 }
2083 else
2084 if (precedence >= target)
2085 {
2086 /*
2087 Left-to-right associativity.
2088 */
2089 target=precedence;
2090 subexpression=expression;
2091 }
2092 if (strchr("(",(int) *expression) != (char *) NULL)
2093 expression=FxSubexpression(expression,exception);
2094 c=(int) (*expression++);
2095 }
2096 return(subexpression);
2097}
2098
2099static MagickRealType FxEvaluateSubexpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002100 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002101 const char *expression,MagickRealType *beta,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002102{
2103 char
2104 *q,
2105 subexpression[MaxTextExtent];
2106
2107 MagickRealType
2108 alpha,
2109 gamma;
2110
2111 register const char
2112 *p;
2113
2114 *beta=0.0;
2115 if (exception->severity != UndefinedException)
2116 return(0.0);
2117 while (isspace((int) *expression) != 0)
2118 expression++;
2119 if (*expression == '\0')
2120 {
2121 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
2122 "MissingExpression","`%s'",expression);
2123 return(0.0);
2124 }
cristy66322f02010-05-17 11:40:48 +00002125 *subexpression='\0';
cristy3ed852e2009-09-05 21:47:34 +00002126 p=FxOperatorPrecedence(expression,exception);
2127 if (p != (const char *) NULL)
2128 {
2129 (void) CopyMagickString(subexpression,expression,(size_t)
2130 (p-expression+1));
2131 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2132 exception);
2133 switch ((unsigned char) *p)
2134 {
2135 case '~':
2136 {
2137 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristybb503372010-05-27 20:51:26 +00002138 *beta=(MagickRealType) (~(size_t) *beta);
cristy3ed852e2009-09-05 21:47:34 +00002139 return(*beta);
2140 }
2141 case '!':
2142 {
2143 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2144 return(*beta == 0.0 ? 1.0 : 0.0);
2145 }
2146 case '^':
2147 {
2148 *beta=pow((double) alpha,(double) FxEvaluateSubexpression(fx_info,
2149 channel,x,y,++p,beta,exception));
2150 return(*beta);
2151 }
2152 case '*':
cristy116af162010-08-13 01:25:47 +00002153 case ExponentialNotation:
cristy3ed852e2009-09-05 21:47:34 +00002154 {
2155 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2156 return(alpha*(*beta));
2157 }
2158 case '/':
2159 {
2160 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2161 if (*beta == 0.0)
2162 {
2163 if (exception->severity == UndefinedException)
2164 (void) ThrowMagickException(exception,GetMagickModule(),
2165 OptionError,"DivideByZero","`%s'",expression);
2166 return(0.0);
2167 }
2168 return(alpha/(*beta));
2169 }
2170 case '%':
2171 {
2172 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2173 *beta=fabs(floor(((double) *beta)+0.5));
2174 if (*beta == 0.0)
2175 {
2176 (void) ThrowMagickException(exception,GetMagickModule(),
2177 OptionError,"DivideByZero","`%s'",expression);
2178 return(0.0);
2179 }
2180 return(fmod((double) alpha,(double) *beta));
2181 }
2182 case '+':
2183 {
2184 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2185 return(alpha+(*beta));
2186 }
2187 case '-':
2188 {
2189 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2190 return(alpha-(*beta));
2191 }
2192 case LeftShiftOperator:
2193 {
2194 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002195 *beta=(MagickRealType) ((size_t) (alpha+0.5) << (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002196 return(*beta);
2197 }
2198 case RightShiftOperator:
2199 {
2200 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002201 *beta=(MagickRealType) ((size_t) (alpha+0.5) >> (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002202 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);
cristy1707c6c2012-01-18 23:30:54 +00002237 *beta=(MagickRealType) ((size_t) (alpha+0.5) & (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002238 return(*beta);
2239 }
2240 case '|':
2241 {
2242 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristy1707c6c2012-01-18 23:30:54 +00002243 *beta=(MagickRealType) ((size_t) (alpha+0.5) | (size_t) (gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002244 return(*beta);
2245 }
2246 case LogicalAndOperator:
2247 {
2248 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2249 *beta=(alpha > 0.0) && (gamma > 0.0) ? 1.0 : 0.0;
2250 return(*beta);
2251 }
2252 case LogicalOrOperator:
2253 {
2254 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2255 *beta=(alpha > 0.0) || (gamma > 0.0) ? 1.0 : 0.0;
2256 return(*beta);
2257 }
2258 case '?':
2259 {
2260 MagickRealType
2261 gamma;
2262
2263 (void) CopyMagickString(subexpression,++p,MaxTextExtent);
2264 q=subexpression;
2265 p=StringToken(":",&q);
2266 if (q == (char *) NULL)
2267 {
2268 (void) ThrowMagickException(exception,GetMagickModule(),
2269 OptionError,"UnableToParseExpression","`%s'",subexpression);
2270 return(0.0);
2271 }
2272 if (fabs((double) alpha) > MagickEpsilon)
2273 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,exception);
2274 else
2275 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,beta,exception);
2276 return(gamma);
2277 }
2278 case '=':
2279 {
2280 char
2281 numeric[MaxTextExtent];
2282
2283 q=subexpression;
2284 while (isalpha((int) ((unsigned char) *q)) != 0)
2285 q++;
2286 if (*q != '\0')
2287 {
2288 (void) ThrowMagickException(exception,GetMagickModule(),
2289 OptionError,"UnableToParseExpression","`%s'",subexpression);
2290 return(0.0);
2291 }
2292 ClearMagickException(exception);
2293 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
cristyb51dff52011-05-19 16:55:47 +00002294 (void) FormatLocaleString(numeric,MaxTextExtent,"%g",(double)
cristy8cd5b312010-01-07 01:10:24 +00002295 *beta);
cristy3ed852e2009-09-05 21:47:34 +00002296 (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
2297 (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
2298 subexpression),ConstantString(numeric));
2299 return(*beta);
2300 }
2301 case ',':
2302 {
2303 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2304 return(alpha);
2305 }
2306 case ';':
2307 {
2308 *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,beta,exception);
2309 return(*beta);
2310 }
2311 default:
2312 {
2313 gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,p,beta,
2314 exception);
2315 return(gamma);
2316 }
2317 }
2318 }
2319 if (strchr("(",(int) *expression) != (char *) NULL)
2320 {
2321 (void) CopyMagickString(subexpression,expression+1,MaxTextExtent);
2322 subexpression[strlen(subexpression)-1]='\0';
2323 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,beta,
2324 exception);
2325 return(gamma);
2326 }
cristy8b8a3ae2010-10-23 18:49:46 +00002327 switch (*expression)
cristy3ed852e2009-09-05 21:47:34 +00002328 {
2329 case '+':
2330 {
2331 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2332 exception);
2333 return(1.0*gamma);
2334 }
2335 case '-':
2336 {
2337 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2338 exception);
2339 return(-1.0*gamma);
2340 }
2341 case '~':
2342 {
2343 gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,beta,
2344 exception);
cristybb503372010-05-27 20:51:26 +00002345 return((MagickRealType) (~(size_t) (gamma+0.5)));
cristy3ed852e2009-09-05 21:47:34 +00002346 }
2347 case 'A':
2348 case 'a':
2349 {
2350 if (LocaleNCompare(expression,"abs",3) == 0)
2351 {
2352 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2353 exception);
2354 return((MagickRealType) fabs((double) alpha));
2355 }
cristyb33454f2011-08-03 02:10:45 +00002356#if defined(MAGICKCORE_HAVE_ACOSH)
cristy363772a2011-07-28 23:25:33 +00002357 if (LocaleNCompare(expression,"acosh",5) == 0)
2358 {
2359 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2360 exception);
2361 return((MagickRealType) acosh((double) alpha));
2362 }
cristyb33454f2011-08-03 02:10:45 +00002363#endif
cristy3ed852e2009-09-05 21:47:34 +00002364 if (LocaleNCompare(expression,"acos",4) == 0)
2365 {
2366 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2367 exception);
2368 return((MagickRealType) acos((double) alpha));
2369 }
cristy43c22f42010-03-30 12:34:07 +00002370#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002371 if (LocaleNCompare(expression,"airy",4) == 0)
2372 {
2373 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2374 exception);
2375 if (alpha == 0.0)
cristy2dd03222010-03-30 22:12:11 +00002376 return(1.0);
2377 gamma=2.0*j1((double) (MagickPI*alpha))/(MagickPI*alpha);
cristy43c22f42010-03-30 12:34:07 +00002378 return(gamma*gamma);
cristyee56cf12010-03-01 22:17:06 +00002379 }
cristy43c22f42010-03-30 12:34:07 +00002380#endif
cristyb33454f2011-08-03 02:10:45 +00002381#if defined(MAGICKCORE_HAVE_ASINH)
cristy363772a2011-07-28 23:25:33 +00002382 if (LocaleNCompare(expression,"asinh",5) == 0)
2383 {
2384 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2385 exception);
2386 return((MagickRealType) asinh((double) alpha));
2387 }
cristyb33454f2011-08-03 02:10:45 +00002388#endif
cristy3ed852e2009-09-05 21:47:34 +00002389 if (LocaleNCompare(expression,"asin",4) == 0)
2390 {
2391 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2392 exception);
2393 return((MagickRealType) asin((double) alpha));
2394 }
2395 if (LocaleNCompare(expression,"alt",3) == 0)
2396 {
2397 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2398 exception);
cristybb503372010-05-27 20:51:26 +00002399 return(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
cristy3ed852e2009-09-05 21:47:34 +00002400 }
2401 if (LocaleNCompare(expression,"atan2",5) == 0)
2402 {
2403 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2404 exception);
2405 return((MagickRealType) atan2((double) alpha,(double) *beta));
2406 }
cristyb33454f2011-08-03 02:10:45 +00002407#if defined(MAGICKCORE_HAVE_ATANH)
cristy363772a2011-07-28 23:25:33 +00002408 if (LocaleNCompare(expression,"atanh",5) == 0)
2409 {
2410 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2411 exception);
2412 return((MagickRealType) atanh((double) alpha));
2413 }
cristyb33454f2011-08-03 02:10:45 +00002414#endif
cristy3ed852e2009-09-05 21:47:34 +00002415 if (LocaleNCompare(expression,"atan",4) == 0)
2416 {
2417 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2418 exception);
2419 return((MagickRealType) atan((double) alpha));
2420 }
2421 if (LocaleCompare(expression,"a") == 0)
2422 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2423 break;
2424 }
2425 case 'B':
2426 case 'b':
2427 {
2428 if (LocaleCompare(expression,"b") == 0)
2429 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2430 break;
2431 }
2432 case 'C':
2433 case 'c':
2434 {
2435 if (LocaleNCompare(expression,"ceil",4) == 0)
2436 {
2437 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2438 exception);
2439 return((MagickRealType) ceil((double) alpha));
2440 }
2441 if (LocaleNCompare(expression,"cosh",4) == 0)
2442 {
2443 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2444 exception);
2445 return((MagickRealType) cosh((double) alpha));
2446 }
2447 if (LocaleNCompare(expression,"cos",3) == 0)
2448 {
2449 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2450 exception);
2451 return((MagickRealType) cos((double) alpha));
2452 }
2453 if (LocaleCompare(expression,"c") == 0)
2454 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2455 break;
2456 }
2457 case 'D':
2458 case 'd':
2459 {
2460 if (LocaleNCompare(expression,"debug",5) == 0)
2461 {
2462 const char
2463 *type;
2464
2465 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2466 exception);
2467 if (fx_info->images->colorspace == CMYKColorspace)
2468 switch (channel)
2469 {
cristy0568ffc2011-07-25 16:54:14 +00002470 case CyanPixelChannel: type="cyan"; break;
2471 case MagentaPixelChannel: type="magenta"; break;
2472 case YellowPixelChannel: type="yellow"; break;
2473 case AlphaPixelChannel: type="opacity"; break;
2474 case BlackPixelChannel: type="black"; break;
cristy3ed852e2009-09-05 21:47:34 +00002475 default: type="unknown"; break;
2476 }
2477 else
2478 switch (channel)
2479 {
cristy0568ffc2011-07-25 16:54:14 +00002480 case RedPixelChannel: type="red"; break;
2481 case GreenPixelChannel: type="green"; break;
2482 case BluePixelChannel: type="blue"; break;
2483 case AlphaPixelChannel: type="opacity"; break;
cristy3ed852e2009-09-05 21:47:34 +00002484 default: type="unknown"; break;
2485 }
2486 (void) CopyMagickString(subexpression,expression+6,MaxTextExtent);
2487 if (strlen(subexpression) > 1)
2488 subexpression[strlen(subexpression)-1]='\0';
2489 if (fx_info->file != (FILE *) NULL)
cristy1707c6c2012-01-18 23:30:54 +00002490 (void) FormatLocaleFile(fx_info->file,"%s[%.20g,%.20g].%s: "
2491 "%s=%.*g\n",fx_info->images->filename,(double) x,(double) y,type,
2492 subexpression,GetMagickPrecision(),(double) alpha);
cristy3ed852e2009-09-05 21:47:34 +00002493 return(0.0);
2494 }
cristy5597a8d2011-11-04 00:25:32 +00002495 if (LocaleNCompare(expression,"drc",3) == 0)
2496 {
2497 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2498 exception);
2499 return((MagickRealType) (alpha/(*beta*(alpha-1.0)+1.0)));
2500 }
cristy3ed852e2009-09-05 21:47:34 +00002501 break;
2502 }
2503 case 'E':
2504 case 'e':
2505 {
2506 if (LocaleCompare(expression,"epsilon") == 0)
2507 return((MagickRealType) MagickEpsilon);
2508 if (LocaleNCompare(expression,"exp",3) == 0)
2509 {
2510 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2511 exception);
2512 return((MagickRealType) exp((double) alpha));
2513 }
2514 if (LocaleCompare(expression,"e") == 0)
2515 return((MagickRealType) 2.7182818284590452354);
2516 break;
2517 }
2518 case 'F':
2519 case 'f':
2520 {
2521 if (LocaleNCompare(expression,"floor",5) == 0)
2522 {
2523 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2524 exception);
2525 return((MagickRealType) floor((double) alpha));
2526 }
2527 break;
2528 }
2529 case 'G':
2530 case 'g':
2531 {
cristy9eeedea2011-11-02 19:04:05 +00002532 if (LocaleNCompare(expression,"gauss",5) == 0)
2533 {
2534 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2535 exception);
2536 gamma=exp((double) (-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
2537 return((MagickRealType) gamma);
2538 }
cristyb0aad4c2011-11-02 19:30:35 +00002539 if (LocaleNCompare(expression,"gcd",3) == 0)
2540 {
2541 MagickOffsetType
2542 gcd;
2543
2544 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2545 exception);
cristy1707c6c2012-01-18 23:30:54 +00002546 gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType) (*beta+
2547 0.5));
cristyb0aad4c2011-11-02 19:30:35 +00002548 return((MagickRealType) gcd);
2549 }
cristy3ed852e2009-09-05 21:47:34 +00002550 if (LocaleCompare(expression,"g") == 0)
2551 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2552 break;
2553 }
2554 case 'H':
2555 case 'h':
2556 {
2557 if (LocaleCompare(expression,"h") == 0)
2558 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2559 if (LocaleCompare(expression,"hue") == 0)
2560 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2561 if (LocaleNCompare(expression,"hypot",5) == 0)
2562 {
2563 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2564 exception);
2565 return((MagickRealType) hypot((double) alpha,(double) *beta));
2566 }
2567 break;
2568 }
2569 case 'K':
2570 case 'k':
2571 {
2572 if (LocaleCompare(expression,"k") == 0)
2573 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2574 break;
2575 }
2576 case 'I':
2577 case 'i':
2578 {
2579 if (LocaleCompare(expression,"intensity") == 0)
2580 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2581 if (LocaleNCompare(expression,"int",3) == 0)
2582 {
2583 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2584 exception);
cristy16788e42010-08-13 13:44:26 +00002585 return((MagickRealType) floor(alpha));
cristy3ed852e2009-09-05 21:47:34 +00002586 }
cristy82b20722011-11-05 21:52:36 +00002587#if defined(MAGICKCORE_HAVE_ISNAN)
cristy639399c2011-11-02 19:16:15 +00002588 if (LocaleNCompare(expression,"isnan",5) == 0)
2589 {
2590 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2591 exception);
cristy17a10202011-11-02 19:17:04 +00002592 return((MagickRealType) !!isnan((double) alpha));
cristy639399c2011-11-02 19:16:15 +00002593 }
cristy82b20722011-11-05 21:52:36 +00002594#endif
cristy3ed852e2009-09-05 21:47:34 +00002595 if (LocaleCompare(expression,"i") == 0)
2596 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2597 break;
2598 }
2599 case 'J':
2600 case 'j':
2601 {
2602 if (LocaleCompare(expression,"j") == 0)
2603 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
cristy161b9262010-03-20 19:34:32 +00002604#if defined(MAGICKCORE_HAVE_J0)
cristyee56cf12010-03-01 22:17:06 +00002605 if (LocaleNCompare(expression,"j0",2) == 0)
2606 {
2607 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2608 exception);
2609 return((MagickRealType) j0((double) alpha));
2610 }
cristy161b9262010-03-20 19:34:32 +00002611#endif
2612#if defined(MAGICKCORE_HAVE_J1)
cristyee56cf12010-03-01 22:17:06 +00002613 if (LocaleNCompare(expression,"j1",2) == 0)
2614 {
2615 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2616 exception);
2617 return((MagickRealType) j1((double) alpha));
2618 }
cristy161b9262010-03-20 19:34:32 +00002619#endif
cristyaa018fa2010-04-08 23:03:54 +00002620#if defined(MAGICKCORE_HAVE_J1)
cristya6a09e72010-03-02 14:51:02 +00002621 if (LocaleNCompare(expression,"jinc",4) == 0)
2622 {
2623 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2624 exception);
cristy0946a822010-03-12 17:14:58 +00002625 if (alpha == 0.0)
2626 return(1.0);
cristy1707c6c2012-01-18 23:30:54 +00002627 gamma=(MagickRealType) (2.0*j1((double) (MagickPI*alpha))/(MagickPI*
2628 alpha));
cristyfce2f7b2010-03-12 00:29:49 +00002629 return(gamma);
cristya6a09e72010-03-02 14:51:02 +00002630 }
cristyaa018fa2010-04-08 23:03:54 +00002631#endif
cristy3ed852e2009-09-05 21:47:34 +00002632 break;
2633 }
2634 case 'L':
2635 case 'l':
2636 {
2637 if (LocaleNCompare(expression,"ln",2) == 0)
2638 {
2639 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,beta,
2640 exception);
2641 return((MagickRealType) log((double) alpha));
2642 }
cristyc8ed5322010-08-31 12:07:59 +00002643 if (LocaleNCompare(expression,"logtwo",6) == 0)
cristy3ed852e2009-09-05 21:47:34 +00002644 {
cristyc8ed5322010-08-31 12:07:59 +00002645 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
cristy3ed852e2009-09-05 21:47:34 +00002646 exception);
2647 return((MagickRealType) log10((double) alpha))/log10(2.0);
2648 }
2649 if (LocaleNCompare(expression,"log",3) == 0)
2650 {
2651 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2652 exception);
2653 return((MagickRealType) log10((double) alpha));
2654 }
2655 if (LocaleCompare(expression,"lightness") == 0)
2656 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2657 break;
2658 }
2659 case 'M':
2660 case 'm':
2661 {
2662 if (LocaleCompare(expression,"MaxRGB") == 0)
2663 return((MagickRealType) QuantumRange);
2664 if (LocaleNCompare(expression,"maxima",6) == 0)
2665 break;
2666 if (LocaleNCompare(expression,"max",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002667 {
2668 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2669 exception);
2670 return(alpha > *beta ? alpha : *beta);
2671 }
cristy3ed852e2009-09-05 21:47:34 +00002672 if (LocaleNCompare(expression,"minima",6) == 0)
2673 break;
2674 if (LocaleNCompare(expression,"min",3) == 0)
cristy984049c2011-11-03 18:34:58 +00002675 {
2676 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2677 exception);
2678 return(alpha < *beta ? alpha : *beta);
2679 }
cristy3ed852e2009-09-05 21:47:34 +00002680 if (LocaleNCompare(expression,"mod",3) == 0)
2681 {
2682 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2683 exception);
cristy984049c2011-11-03 18:34:58 +00002684 gamma=alpha-floor((double) (alpha/(*beta)))*(*beta);
2685 return(gamma);
cristy3ed852e2009-09-05 21:47:34 +00002686 }
2687 if (LocaleCompare(expression,"m") == 0)
2688 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2689 break;
2690 }
2691 case 'N':
2692 case 'n':
2693 {
cristyad3502e2011-11-02 19:10:45 +00002694 if (LocaleNCompare(expression,"not",3) == 0)
2695 {
2696 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2697 exception);
2698 return((MagickRealType) (alpha < MagickEpsilon));
2699 }
cristy3ed852e2009-09-05 21:47:34 +00002700 if (LocaleCompare(expression,"n") == 0)
2701 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2702 break;
2703 }
2704 case 'O':
2705 case 'o':
2706 {
2707 if (LocaleCompare(expression,"Opaque") == 0)
2708 return(1.0);
2709 if (LocaleCompare(expression,"o") == 0)
2710 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2711 break;
2712 }
2713 case 'P':
2714 case 'p':
2715 {
cristy670aa3c2011-11-03 00:54:00 +00002716 if (LocaleCompare(expression,"phi") == 0)
2717 return((MagickRealType) MagickPHI);
cristy3ed852e2009-09-05 21:47:34 +00002718 if (LocaleCompare(expression,"pi") == 0)
2719 return((MagickRealType) MagickPI);
2720 if (LocaleNCompare(expression,"pow",3) == 0)
2721 {
2722 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2723 exception);
2724 return((MagickRealType) pow((double) alpha,(double) *beta));
2725 }
2726 if (LocaleCompare(expression,"p") == 0)
2727 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2728 break;
2729 }
2730 case 'Q':
2731 case 'q':
2732 {
2733 if (LocaleCompare(expression,"QuantumRange") == 0)
2734 return((MagickRealType) QuantumRange);
2735 if (LocaleCompare(expression,"QuantumScale") == 0)
2736 return((MagickRealType) QuantumScale);
2737 break;
2738 }
2739 case 'R':
2740 case 'r':
2741 {
2742 if (LocaleNCompare(expression,"rand",4) == 0)
2743 return((MagickRealType) GetPseudoRandomValue(fx_info->random_info));
2744 if (LocaleNCompare(expression,"round",5) == 0)
2745 {
2746 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2747 exception);
cristy16788e42010-08-13 13:44:26 +00002748 return((MagickRealType) floor((double) alpha+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002749 }
2750 if (LocaleCompare(expression,"r") == 0)
2751 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2752 break;
2753 }
2754 case 'S':
2755 case 's':
2756 {
2757 if (LocaleCompare(expression,"saturation") == 0)
2758 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2759 if (LocaleNCompare(expression,"sign",4) == 0)
2760 {
2761 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2762 exception);
2763 return(alpha < 0.0 ? -1.0 : 1.0);
2764 }
cristya6a09e72010-03-02 14:51:02 +00002765 if (LocaleNCompare(expression,"sinc",4) == 0)
2766 {
2767 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2768 exception);
2769 if (alpha == 0)
2770 return(1.0);
2771 gamma=(MagickRealType) (sin((double) (MagickPI*alpha))/
2772 (MagickPI*alpha));
2773 return(gamma);
2774 }
cristy3ed852e2009-09-05 21:47:34 +00002775 if (LocaleNCompare(expression,"sinh",4) == 0)
2776 {
2777 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2778 exception);
2779 return((MagickRealType) sinh((double) alpha));
2780 }
2781 if (LocaleNCompare(expression,"sin",3) == 0)
2782 {
2783 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2784 exception);
2785 return((MagickRealType) sin((double) alpha));
2786 }
2787 if (LocaleNCompare(expression,"sqrt",4) == 0)
2788 {
2789 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2790 exception);
2791 return((MagickRealType) sqrt((double) alpha));
2792 }
cristy9eeedea2011-11-02 19:04:05 +00002793 if (LocaleNCompare(expression,"squish",6) == 0)
2794 {
2795 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,beta,
2796 exception);
2797 return((MagickRealType) (1.0/(1.0+exp((double) (4.0*alpha)))));
2798 }
cristy3ed852e2009-09-05 21:47:34 +00002799 if (LocaleCompare(expression,"s") == 0)
2800 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2801 break;
2802 }
2803 case 'T':
2804 case 't':
2805 {
2806 if (LocaleNCompare(expression,"tanh",4) == 0)
2807 {
2808 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,beta,
2809 exception);
2810 return((MagickRealType) tanh((double) alpha));
2811 }
2812 if (LocaleNCompare(expression,"tan",3) == 0)
2813 {
2814 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,beta,
2815 exception);
2816 return((MagickRealType) tan((double) alpha));
2817 }
2818 if (LocaleCompare(expression,"Transparent") == 0)
2819 return(0.0);
cristy16788e42010-08-13 13:44:26 +00002820 if (LocaleNCompare(expression,"trunc",5) == 0)
2821 {
2822 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2823 exception);
2824 if (alpha >= 0.0)
2825 return((MagickRealType) floor((double) alpha));
2826 return((MagickRealType) ceil((double) alpha));
2827 }
cristy3ed852e2009-09-05 21:47:34 +00002828 if (LocaleCompare(expression,"t") == 0)
2829 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2830 break;
2831 }
2832 case 'U':
2833 case 'u':
2834 {
2835 if (LocaleCompare(expression,"u") == 0)
2836 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2837 break;
2838 }
2839 case 'V':
2840 case 'v':
2841 {
2842 if (LocaleCompare(expression,"v") == 0)
2843 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2844 break;
2845 }
2846 case 'W':
2847 case 'w':
2848 {
cristy9eeedea2011-11-02 19:04:05 +00002849 if (LocaleNCompare(expression,"while",5) == 0)
2850 {
2851 do
2852 {
2853 alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,beta,
2854 exception);
2855 } while (fabs((double) alpha) >= MagickEpsilon);
2856 return((MagickRealType) *beta);
2857 }
cristy3ed852e2009-09-05 21:47:34 +00002858 if (LocaleCompare(expression,"w") == 0)
2859 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2860 break;
2861 }
2862 case 'Y':
2863 case 'y':
2864 {
2865 if (LocaleCompare(expression,"y") == 0)
2866 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2867 break;
2868 }
2869 case 'Z':
2870 case 'z':
2871 {
2872 if (LocaleCompare(expression,"z") == 0)
2873 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2874 break;
2875 }
2876 default:
2877 break;
2878 }
2879 q=(char *) expression;
cristydbdd0e32011-11-04 23:29:40 +00002880 alpha=InterpretSiPrefixValue(expression,&q);
cristy3ed852e2009-09-05 21:47:34 +00002881 if (q == expression)
2882 return(FxGetSymbol(fx_info,channel,x,y,expression,exception));
2883 return(alpha);
2884}
2885
cristy7832dc22011-09-05 01:21:53 +00002886MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
cristy3ed852e2009-09-05 21:47:34 +00002887 MagickRealType *alpha,ExceptionInfo *exception)
2888{
2889 MagickBooleanType
2890 status;
2891
cristy541ae572011-08-05 19:08:59 +00002892 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2893 exception);
cristy3ed852e2009-09-05 21:47:34 +00002894 return(status);
2895}
2896
2897MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
2898 MagickRealType *alpha,ExceptionInfo *exception)
2899{
2900 FILE
2901 *file;
2902
2903 MagickBooleanType
2904 status;
2905
2906 file=fx_info->file;
2907 fx_info->file=(FILE *) NULL;
cristy541ae572011-08-05 19:08:59 +00002908 status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
2909 exception);
cristy3ed852e2009-09-05 21:47:34 +00002910 fx_info->file=file;
2911 return(status);
2912}
2913
cristy7832dc22011-09-05 01:21:53 +00002914MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
cristy0568ffc2011-07-25 16:54:14 +00002915 const PixelChannel channel,const ssize_t x,const ssize_t y,
cristye85007d2010-06-06 22:51:36 +00002916 MagickRealType *alpha,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002917{
2918 MagickRealType
2919 beta;
2920
2921 beta=0.0;
2922 *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,&beta,
2923 exception);
2924 return(exception->severity == OptionError ? MagickFalse : MagickTrue);
2925}
2926
2927/*
2928%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2929% %
2930% %
2931% %
2932% F x I m a g e %
2933% %
2934% %
2935% %
2936%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2937%
2938% FxImage() applies a mathematical expression to the specified image.
2939%
2940% The format of the FxImage method is:
2941%
2942% Image *FxImage(const Image *image,const char *expression,
2943% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002944%
2945% A description of each parameter follows:
2946%
2947% o image: the image.
2948%
cristy3ed852e2009-09-05 21:47:34 +00002949% o expression: A mathematical expression.
2950%
2951% o exception: return any errors or warnings in this structure.
2952%
2953*/
2954
2955static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
2956{
cristybb503372010-05-27 20:51:26 +00002957 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002958 i;
2959
2960 assert(fx_info != (FxInfo **) NULL);
cristybb503372010-05-27 20:51:26 +00002961 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
cristy3ed852e2009-09-05 21:47:34 +00002962 if (fx_info[i] != (FxInfo *) NULL)
2963 fx_info[i]=DestroyFxInfo(fx_info[i]);
cristyb41ee102010-10-04 16:46:15 +00002964 fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
cristy3ed852e2009-09-05 21:47:34 +00002965 return(fx_info);
2966}
2967
2968static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
2969 ExceptionInfo *exception)
2970{
2971 char
2972 *fx_expression;
2973
2974 FxInfo
2975 **fx_info;
2976
2977 MagickRealType
2978 alpha;
2979
cristybb503372010-05-27 20:51:26 +00002980 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002981 i;
2982
cristybb503372010-05-27 20:51:26 +00002983 size_t
cristy3ed852e2009-09-05 21:47:34 +00002984 number_threads;
2985
2986 number_threads=GetOpenMPMaximumThreads();
cristyb41ee102010-10-04 16:46:15 +00002987 fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
cristy3ed852e2009-09-05 21:47:34 +00002988 if (fx_info == (FxInfo **) NULL)
2989 return((FxInfo **) NULL);
2990 (void) ResetMagickMemory(fx_info,0,number_threads*sizeof(*fx_info));
2991 if (*expression != '@')
2992 fx_expression=ConstantString(expression);
2993 else
2994 fx_expression=FileToString(expression+1,~0,exception);
cristybb503372010-05-27 20:51:26 +00002995 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +00002996 {
2997 fx_info[i]=AcquireFxInfo(image,fx_expression);
2998 if (fx_info[i] == (FxInfo *) NULL)
2999 return(DestroyFxThreadSet(fx_info));
3000 (void) FxPreprocessExpression(fx_info[i],&alpha,fx_info[i]->exception);
3001 }
3002 fx_expression=DestroyString(fx_expression);
3003 return(fx_info);
3004}
3005
3006MagickExport Image *FxImage(const Image *image,const char *expression,
3007 ExceptionInfo *exception)
3008{
cristy3ed852e2009-09-05 21:47:34 +00003009#define FxImageTag "Fx/Image"
3010
cristyfa112112010-01-04 17:48:07 +00003011 CacheView
cristy79cedc72011-07-25 00:41:15 +00003012 *fx_view,
3013 *image_view;
cristyfa112112010-01-04 17:48:07 +00003014
cristy3ed852e2009-09-05 21:47:34 +00003015 FxInfo
cristyfa112112010-01-04 17:48:07 +00003016 **restrict fx_info;
cristy3ed852e2009-09-05 21:47:34 +00003017
3018 Image
3019 *fx_image;
3020
cristy3ed852e2009-09-05 21:47:34 +00003021 MagickBooleanType
3022 status;
3023
cristybb503372010-05-27 20:51:26 +00003024 MagickOffsetType
3025 progress;
3026
cristy3ed852e2009-09-05 21:47:34 +00003027 MagickRealType
3028 alpha;
3029
cristybb503372010-05-27 20:51:26 +00003030 ssize_t
3031 y;
3032
cristy3ed852e2009-09-05 21:47:34 +00003033 assert(image != (Image *) NULL);
3034 assert(image->signature == MagickSignature);
3035 if (image->debug != MagickFalse)
3036 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy79cedc72011-07-25 00:41:15 +00003037 fx_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00003038 if (fx_image == (Image *) NULL)
3039 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003040 if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003041 {
cristy3ed852e2009-09-05 21:47:34 +00003042 fx_image=DestroyImage(fx_image);
3043 return((Image *) NULL);
3044 }
3045 fx_info=AcquireFxThreadSet(image,expression,exception);
3046 if (fx_info == (FxInfo **) NULL)
3047 {
3048 fx_image=DestroyImage(fx_image);
3049 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
3050 }
3051 status=FxPreprocessExpression(fx_info[0],&alpha,exception);
3052 if (status == MagickFalse)
3053 {
3054 fx_image=DestroyImage(fx_image);
3055 fx_info=DestroyFxThreadSet(fx_info);
3056 return((Image *) NULL);
3057 }
3058 /*
3059 Fx image.
3060 */
3061 status=MagickTrue;
3062 progress=0;
cristy79cedc72011-07-25 00:41:15 +00003063 image_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00003064 fx_view=AcquireCacheView(fx_image);
cristyb5d5f722009-11-04 03:03:49 +00003065#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003066 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003067#endif
cristybb503372010-05-27 20:51:26 +00003068 for (y=0; y < (ssize_t) fx_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003069 {
cristy5c9e6f22010-09-17 17:31:01 +00003070 const int
3071 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003072
cristy79cedc72011-07-25 00:41:15 +00003073 register const Quantum
3074 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003075
cristy4c08aed2011-07-01 19:47:50 +00003076 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003077 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003078
cristy79cedc72011-07-25 00:41:15 +00003079 register ssize_t
3080 x;
3081
cristy3ed852e2009-09-05 21:47:34 +00003082 if (status == MagickFalse)
3083 continue;
cristy79cedc72011-07-25 00:41:15 +00003084 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003085 q=QueueCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
cristy79cedc72011-07-25 00:41:15 +00003086 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003087 {
3088 status=MagickFalse;
3089 continue;
3090 }
cristybb503372010-05-27 20:51:26 +00003091 for (x=0; x < (ssize_t) fx_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003092 {
cristy79cedc72011-07-25 00:41:15 +00003093 register ssize_t
3094 i;
3095
3096 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3097 {
3098 MagickRealType
3099 alpha;
3100
3101 PixelChannel
3102 channel;
3103
3104 PixelTrait
3105 fx_traits,
3106 traits;
3107
cristye2a912b2011-12-05 20:02:07 +00003108 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003109 traits=GetPixelChannelMapTraits(image,channel);
cristy79cedc72011-07-25 00:41:15 +00003110 fx_traits=GetPixelChannelMapTraits(fx_image,channel);
cristy010d7d12011-08-31 01:02:48 +00003111 if ((traits == UndefinedPixelTrait) ||
3112 (fx_traits == UndefinedPixelTrait))
cristy79cedc72011-07-25 00:41:15 +00003113 continue;
3114 if ((fx_traits & CopyPixelTrait) != 0)
3115 {
cristy0beccfa2011-09-25 20:47:53 +00003116 SetPixelChannel(fx_image,channel,p[i],q);
cristy79cedc72011-07-25 00:41:15 +00003117 continue;
3118 }
3119 alpha=0.0;
cristya382aca2011-12-06 18:22:48 +00003120 (void) FxEvaluateChannelExpression(fx_info[id],channel,x,y,&alpha,
3121 exception);
cristyb3a73b52011-07-26 01:34:43 +00003122 q[i]=ClampToQuantum((MagickRealType) QuantumRange*alpha);
cristy79cedc72011-07-25 00:41:15 +00003123 }
3124 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003125 q+=GetPixelChannels(fx_image);
cristy3ed852e2009-09-05 21:47:34 +00003126 }
3127 if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
3128 status=MagickFalse;
3129 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3130 {
3131 MagickBooleanType
3132 proceed;
3133
cristyb5d5f722009-11-04 03:03:49 +00003134#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy490408a2011-07-07 14:42:05 +00003135 #pragma omp critical (MagickCore_FxImage)
cristy3ed852e2009-09-05 21:47:34 +00003136#endif
3137 proceed=SetImageProgress(image,FxImageTag,progress++,image->rows);
3138 if (proceed == MagickFalse)
3139 status=MagickFalse;
3140 }
3141 }
cristy3ed852e2009-09-05 21:47:34 +00003142 fx_view=DestroyCacheView(fx_view);
cristy79cedc72011-07-25 00:41:15 +00003143 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003144 fx_info=DestroyFxThreadSet(fx_info);
3145 if (status == MagickFalse)
3146 fx_image=DestroyImage(fx_image);
3147 return(fx_image);
3148}
3149
3150/*
3151%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3152% %
3153% %
3154% %
3155% I m p l o d e I m a g e %
3156% %
3157% %
3158% %
3159%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3160%
3161% ImplodeImage() creates a new image that is a copy of an existing
3162% one with the image pixels "implode" by the specified percentage. It
3163% allocates the memory necessary for the new Image structure and returns a
3164% pointer to the new image.
3165%
3166% The format of the ImplodeImage method is:
3167%
3168% Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003169% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003170%
3171% A description of each parameter follows:
3172%
3173% o implode_image: Method ImplodeImage returns a pointer to the image
3174% after it is implode. A null image is returned if there is a memory
3175% shortage.
3176%
3177% o image: the image.
3178%
3179% o amount: Define the extent of the implosion.
3180%
cristy76f512e2011-09-12 01:26:56 +00003181% o method: the pixel interpolation method.
3182%
cristy3ed852e2009-09-05 21:47:34 +00003183% o exception: return any errors or warnings in this structure.
3184%
3185*/
3186MagickExport Image *ImplodeImage(const Image *image,const double amount,
cristy76f512e2011-09-12 01:26:56 +00003187 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003188{
3189#define ImplodeImageTag "Implode/Image"
3190
cristyfa112112010-01-04 17:48:07 +00003191 CacheView
3192 *image_view,
3193 *implode_view;
3194
cristy3ed852e2009-09-05 21:47:34 +00003195 Image
3196 *implode_image;
3197
cristy3ed852e2009-09-05 21:47:34 +00003198 MagickBooleanType
3199 status;
3200
cristybb503372010-05-27 20:51:26 +00003201 MagickOffsetType
3202 progress;
3203
cristy3ed852e2009-09-05 21:47:34 +00003204 MagickRealType
3205 radius;
3206
3207 PointInfo
3208 center,
3209 scale;
3210
cristybb503372010-05-27 20:51:26 +00003211 ssize_t
3212 y;
3213
cristy3ed852e2009-09-05 21:47:34 +00003214 /*
3215 Initialize implode image attributes.
3216 */
3217 assert(image != (Image *) NULL);
3218 assert(image->signature == MagickSignature);
3219 if (image->debug != MagickFalse)
3220 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3221 assert(exception != (ExceptionInfo *) NULL);
3222 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00003223 implode_image=CloneImage(image,image->columns,image->rows,MagickTrue,
3224 exception);
cristy3ed852e2009-09-05 21:47:34 +00003225 if (implode_image == (Image *) NULL)
3226 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00003227 if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003228 {
cristy3ed852e2009-09-05 21:47:34 +00003229 implode_image=DestroyImage(implode_image);
3230 return((Image *) NULL);
3231 }
cristy4c08aed2011-07-01 19:47:50 +00003232 if (implode_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00003233 implode_image->matte=MagickTrue;
3234 /*
3235 Compute scaling factor.
3236 */
3237 scale.x=1.0;
3238 scale.y=1.0;
3239 center.x=0.5*image->columns;
3240 center.y=0.5*image->rows;
3241 radius=center.x;
3242 if (image->columns > image->rows)
3243 scale.y=(double) image->columns/(double) image->rows;
3244 else
3245 if (image->columns < image->rows)
3246 {
3247 scale.x=(double) image->rows/(double) image->columns;
3248 radius=center.y;
3249 }
3250 /*
3251 Implode image.
3252 */
3253 status=MagickTrue;
3254 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00003255 image_view=AcquireCacheView(image);
3256 implode_view=AcquireCacheView(implode_image);
cristyb5d5f722009-11-04 03:03:49 +00003257#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003258 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00003259#endif
cristybb503372010-05-27 20:51:26 +00003260 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003261 {
cristy3ed852e2009-09-05 21:47:34 +00003262 MagickRealType
3263 distance;
3264
3265 PointInfo
3266 delta;
3267
cristy6d188022011-09-12 13:23:33 +00003268 register const Quantum
3269 *restrict p;
3270
cristybb503372010-05-27 20:51:26 +00003271 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003272 x;
3273
cristy4c08aed2011-07-01 19:47:50 +00003274 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003275 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003276
3277 if (status == MagickFalse)
3278 continue;
cristy6d188022011-09-12 13:23:33 +00003279 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristya6d7a9b2012-01-18 20:04:48 +00003280 q=QueueCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00003281 exception);
cristy6d188022011-09-12 13:23:33 +00003282 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003283 {
3284 status=MagickFalse;
3285 continue;
3286 }
cristy3ed852e2009-09-05 21:47:34 +00003287 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00003288 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003289 {
cristy6d188022011-09-12 13:23:33 +00003290 register ssize_t
3291 i;
3292
cristy3ed852e2009-09-05 21:47:34 +00003293 /*
3294 Determine if the pixel is within an ellipse.
3295 */
3296 delta.x=scale.x*(double) (x-center.x);
3297 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00003298 if (distance >= (radius*radius))
cristya6d7a9b2012-01-18 20:04:48 +00003299 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy6d188022011-09-12 13:23:33 +00003300 {
cristya6d7a9b2012-01-18 20:04:48 +00003301 PixelChannel
3302 channel;
3303
cristy1707c6c2012-01-18 23:30:54 +00003304 PixelTrait
3305 implode_traits,
3306 traits;
3307
cristya6d7a9b2012-01-18 20:04:48 +00003308 channel=GetPixelChannelMapChannel(image,i);
cristy1707c6c2012-01-18 23:30:54 +00003309 traits=GetPixelChannelMapTraits(image,channel);
3310 implode_traits=GetPixelChannelMapTraits(implode_image,channel);
3311 if ((traits == UndefinedPixelTrait) ||
3312 (implode_traits == UndefinedPixelTrait))
3313 continue;
cristya6d7a9b2012-01-18 20:04:48 +00003314 SetPixelChannel(implode_image,channel,p[i],q);
cristy6d188022011-09-12 13:23:33 +00003315 }
3316 else
cristy3ed852e2009-09-05 21:47:34 +00003317 {
3318 double
3319 factor;
3320
3321 /*
3322 Implode the pixel.
3323 */
3324 factor=1.0;
3325 if (distance > 0.0)
cristy1707c6c2012-01-18 23:30:54 +00003326 factor=pow(sin((double) (MagickPI*sqrt((double) distance)/radius/
3327 2)),-amount);
cristy76f512e2011-09-12 01:26:56 +00003328 status=InterpolatePixelChannels(image,image_view,implode_image,method,
3329 (double) (factor*delta.x/scale.x+center.x),(double) (factor*delta.y/
3330 scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00003331 }
cristy6d188022011-09-12 13:23:33 +00003332 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00003333 q+=GetPixelChannels(implode_image);
cristy3ed852e2009-09-05 21:47:34 +00003334 }
3335 if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
3336 status=MagickFalse;
3337 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3338 {
3339 MagickBooleanType
3340 proceed;
3341
cristyb5d5f722009-11-04 03:03:49 +00003342#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003343 #pragma omp critical (MagickCore_ImplodeImage)
3344#endif
3345 proceed=SetImageProgress(image,ImplodeImageTag,progress++,image->rows);
3346 if (proceed == MagickFalse)
3347 status=MagickFalse;
3348 }
3349 }
3350 implode_view=DestroyCacheView(implode_view);
3351 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003352 if (status == MagickFalse)
3353 implode_image=DestroyImage(implode_image);
3354 return(implode_image);
3355}
3356
3357/*
3358%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3359% %
3360% %
3361% %
3362% M o r p h I m a g e s %
3363% %
3364% %
3365% %
3366%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3367%
3368% The MorphImages() method requires a minimum of two images. The first
3369% image is transformed into the second by a number of intervening images
3370% as specified by frames.
3371%
3372% The format of the MorphImage method is:
3373%
cristybb503372010-05-27 20:51:26 +00003374% Image *MorphImages(const Image *image,const size_t number_frames,
cristy3ed852e2009-09-05 21:47:34 +00003375% ExceptionInfo *exception)
3376%
3377% A description of each parameter follows:
3378%
3379% o image: the image.
3380%
3381% o number_frames: Define the number of in-between image to generate.
3382% The more in-between frames, the smoother the morph.
3383%
3384% o exception: return any errors or warnings in this structure.
3385%
3386*/
3387MagickExport Image *MorphImages(const Image *image,
cristybb503372010-05-27 20:51:26 +00003388 const size_t number_frames,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003389{
3390#define MorphImageTag "Morph/Image"
3391
3392 Image
3393 *morph_image,
3394 *morph_images;
3395
cristy9d314ff2011-03-09 01:30:28 +00003396 MagickBooleanType
3397 status;
cristy3ed852e2009-09-05 21:47:34 +00003398
3399 MagickOffsetType
3400 scene;
3401
3402 MagickRealType
3403 alpha,
3404 beta;
3405
3406 register const Image
3407 *next;
3408
cristybb503372010-05-27 20:51:26 +00003409 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003410 i;
3411
cristy9d314ff2011-03-09 01:30:28 +00003412 ssize_t
3413 y;
cristy3ed852e2009-09-05 21:47:34 +00003414
3415 /*
3416 Clone first frame in sequence.
3417 */
3418 assert(image != (Image *) NULL);
3419 assert(image->signature == MagickSignature);
3420 if (image->debug != MagickFalse)
3421 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3422 assert(exception != (ExceptionInfo *) NULL);
3423 assert(exception->signature == MagickSignature);
3424 morph_images=CloneImage(image,0,0,MagickTrue,exception);
3425 if (morph_images == (Image *) NULL)
3426 return((Image *) NULL);
3427 if (GetNextImageInList(image) == (Image *) NULL)
3428 {
3429 /*
3430 Morph single image.
3431 */
cristybb503372010-05-27 20:51:26 +00003432 for (i=1; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003433 {
3434 morph_image=CloneImage(image,0,0,MagickTrue,exception);
3435 if (morph_image == (Image *) NULL)
3436 {
3437 morph_images=DestroyImageList(morph_images);
3438 return((Image *) NULL);
3439 }
3440 AppendImageToList(&morph_images,morph_image);
cristy8b27a6d2010-02-14 03:31:15 +00003441 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003442 {
cristy8b27a6d2010-02-14 03:31:15 +00003443 MagickBooleanType
3444 proceed;
3445
cristybb503372010-05-27 20:51:26 +00003446 proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) i,
3447 number_frames);
cristy8b27a6d2010-02-14 03:31:15 +00003448 if (proceed == MagickFalse)
3449 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00003450 }
3451 }
3452 return(GetFirstImageInList(morph_images));
3453 }
3454 /*
3455 Morph image sequence.
3456 */
3457 status=MagickTrue;
3458 scene=0;
3459 next=image;
3460 for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
3461 {
cristybb503372010-05-27 20:51:26 +00003462 for (i=0; i < (ssize_t) number_frames; i++)
cristy3ed852e2009-09-05 21:47:34 +00003463 {
3464 CacheView
3465 *image_view,
3466 *morph_view;
3467
3468 beta=(MagickRealType) (i+1.0)/(MagickRealType) (number_frames+1.0);
3469 alpha=1.0-beta;
cristy15b98cd2010-09-12 19:42:50 +00003470 morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
cristybb503372010-05-27 20:51:26 +00003471 GetNextImageInList(next)->columns+0.5),(size_t) (alpha*
cristy15b98cd2010-09-12 19:42:50 +00003472 next->rows+beta*GetNextImageInList(next)->rows+0.5),
3473 next->filter,next->blur,exception);
cristy3ed852e2009-09-05 21:47:34 +00003474 if (morph_image == (Image *) NULL)
3475 {
3476 morph_images=DestroyImageList(morph_images);
3477 return((Image *) NULL);
3478 }
cristy1707c6c2012-01-18 23:30:54 +00003479 status=SetImageStorageClass(morph_image,DirectClass,exception);
3480 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003481 {
cristy3ed852e2009-09-05 21:47:34 +00003482 morph_image=DestroyImage(morph_image);
3483 return((Image *) NULL);
3484 }
3485 AppendImageToList(&morph_images,morph_image);
3486 morph_images=GetLastImageInList(morph_images);
cristy15b98cd2010-09-12 19:42:50 +00003487 morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
3488 morph_images->rows,GetNextImageInList(next)->filter,
3489 GetNextImageInList(next)->blur,exception);
cristy3ed852e2009-09-05 21:47:34 +00003490 if (morph_image == (Image *) NULL)
3491 {
3492 morph_images=DestroyImageList(morph_images);
3493 return((Image *) NULL);
3494 }
3495 image_view=AcquireCacheView(morph_image);
3496 morph_view=AcquireCacheView(morph_images);
cristyb5d5f722009-11-04 03:03:49 +00003497#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003498 #pragma omp parallel for schedule(static,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +00003499#endif
cristybb503372010-05-27 20:51:26 +00003500 for (y=0; y < (ssize_t) morph_images->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003501 {
3502 MagickBooleanType
3503 sync;
3504
cristy4c08aed2011-07-01 19:47:50 +00003505 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003506 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003507
cristybb503372010-05-27 20:51:26 +00003508 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003509 x;
3510
cristy4c08aed2011-07-01 19:47:50 +00003511 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003512 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003513
3514 if (status == MagickFalse)
3515 continue;
3516 p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
3517 exception);
3518 q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
3519 exception);
cristy4c08aed2011-07-01 19:47:50 +00003520 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003521 {
3522 status=MagickFalse;
3523 continue;
3524 }
cristybb503372010-05-27 20:51:26 +00003525 for (x=0; x < (ssize_t) morph_images->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003526 {
cristy1707c6c2012-01-18 23:30:54 +00003527 register ssize_t
3528 i;
3529
3530 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3531 {
3532 PixelChannel
3533 channel;
3534
3535 PixelTrait
3536 morph_traits,
3537 traits;
3538
3539 channel=GetPixelChannelMapChannel(image,i);
3540 traits=GetPixelChannelMapTraits(image,channel);
3541 morph_traits=GetPixelChannelMapTraits(morph_image,channel);
3542 if ((traits == UndefinedPixelTrait) ||
3543 (morph_traits == UndefinedPixelTrait))
3544 continue;
3545 if ((morph_traits & CopyPixelTrait) != 0)
3546 {
3547 SetPixelChannel(morph_image,channel,p[i],q);
3548 continue;
3549 }
3550 SetPixelChannel(morph_image,channel,ClampToQuantum(alpha*
3551 GetPixelChannel(morph_images,channel,q)+beta*p[i]),q);
3552 }
cristyed231572011-07-14 02:18:59 +00003553 p+=GetPixelChannels(morph_image);
3554 q+=GetPixelChannels(morph_images);
cristy3ed852e2009-09-05 21:47:34 +00003555 }
3556 sync=SyncCacheViewAuthenticPixels(morph_view,exception);
3557 if (sync == MagickFalse)
3558 status=MagickFalse;
3559 }
3560 morph_view=DestroyCacheView(morph_view);
3561 image_view=DestroyCacheView(image_view);
3562 morph_image=DestroyImage(morph_image);
3563 }
cristybb503372010-05-27 20:51:26 +00003564 if (i < (ssize_t) number_frames)
cristy3ed852e2009-09-05 21:47:34 +00003565 break;
3566 /*
3567 Clone last frame in sequence.
3568 */
3569 morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
3570 if (morph_image == (Image *) NULL)
3571 {
3572 morph_images=DestroyImageList(morph_images);
3573 return((Image *) NULL);
3574 }
3575 AppendImageToList(&morph_images,morph_image);
3576 morph_images=GetLastImageInList(morph_images);
3577 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3578 {
3579 MagickBooleanType
3580 proceed;
3581
cristyb5d5f722009-11-04 03:03:49 +00003582#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003583 #pragma omp critical (MagickCore_MorphImages)
3584#endif
3585 proceed=SetImageProgress(image,MorphImageTag,scene,
3586 GetImageListLength(image));
3587 if (proceed == MagickFalse)
3588 status=MagickFalse;
3589 }
3590 scene++;
3591 }
3592 if (GetNextImageInList(next) != (Image *) NULL)
3593 {
3594 morph_images=DestroyImageList(morph_images);
3595 return((Image *) NULL);
3596 }
3597 return(GetFirstImageInList(morph_images));
3598}
3599
3600/*
3601%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3602% %
3603% %
3604% %
3605% P l a s m a I m a g e %
3606% %
3607% %
3608% %
3609%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3610%
3611% PlasmaImage() initializes an image with plasma fractal values. The image
3612% must be initialized with a base color and the random number generator
3613% seeded before this method is called.
3614%
3615% The format of the PlasmaImage method is:
3616%
3617% MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
cristy5cbc0162011-08-29 00:36:28 +00003618% size_t attenuate,size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003619%
3620% A description of each parameter follows:
3621%
3622% o image: the image.
3623%
3624% o segment: Define the region to apply plasma fractals values.
3625%
glennrp7dae1ca2010-09-16 12:17:35 +00003626% o attenuate: Define the plasma attenuation factor.
cristy3ed852e2009-09-05 21:47:34 +00003627%
3628% o depth: Limit the plasma recursion depth.
3629%
cristy5cbc0162011-08-29 00:36:28 +00003630% o exception: return any errors or warnings in this structure.
3631%
cristy3ed852e2009-09-05 21:47:34 +00003632*/
3633
3634static inline Quantum PlasmaPixel(RandomInfo *random_info,
3635 const MagickRealType pixel,const MagickRealType noise)
3636{
3637 Quantum
3638 plasma;
3639
cristyce70c172010-01-07 17:15:30 +00003640 plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
cristy3ed852e2009-09-05 21:47:34 +00003641 noise/2.0);
3642 return(plasma);
3643}
3644
cristyda1f9c12011-10-02 21:39:49 +00003645static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
3646 CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
3647 const SegmentInfo *segment,size_t attenuate,size_t depth,
3648 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003649{
cristy3ed852e2009-09-05 21:47:34 +00003650 MagickRealType
3651 plasma;
3652
cristyda1f9c12011-10-02 21:39:49 +00003653 PixelChannel
3654 channel;
3655
3656 PixelTrait
3657 traits;
3658
3659 register const Quantum
3660 *restrict u,
3661 *restrict v;
3662
3663 register Quantum
3664 *restrict q;
3665
3666 register ssize_t
3667 i;
cristy3ed852e2009-09-05 21:47:34 +00003668
cristy9d314ff2011-03-09 01:30:28 +00003669 ssize_t
3670 x,
3671 x_mid,
3672 y,
3673 y_mid;
3674
cristy3ed852e2009-09-05 21:47:34 +00003675 if (((segment->x2-segment->x1) == 0.0) && ((segment->y2-segment->y1) == 0.0))
3676 return(MagickTrue);
3677 if (depth != 0)
3678 {
3679 SegmentInfo
3680 local_info;
3681
3682 /*
3683 Divide the area into quadrants and recurse.
3684 */
3685 depth--;
3686 attenuate++;
cristybb503372010-05-27 20:51:26 +00003687 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3688 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003689 local_info=(*segment);
3690 local_info.x2=(double) x_mid;
3691 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003692 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3693 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003694 local_info=(*segment);
3695 local_info.y1=(double) y_mid;
3696 local_info.x2=(double) x_mid;
cristyda1f9c12011-10-02 21:39:49 +00003697 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3698 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003699 local_info=(*segment);
3700 local_info.x1=(double) x_mid;
3701 local_info.y2=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003702 (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3703 &local_info,attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003704 local_info=(*segment);
3705 local_info.x1=(double) x_mid;
3706 local_info.y1=(double) y_mid;
cristyda1f9c12011-10-02 21:39:49 +00003707 return(PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
3708 &local_info,attenuate,depth,exception));
cristy3ed852e2009-09-05 21:47:34 +00003709 }
cristybb503372010-05-27 20:51:26 +00003710 x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
3711 y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
cristy3ed852e2009-09-05 21:47:34 +00003712 if ((segment->x1 == (double) x_mid) && (segment->x2 == (double) x_mid) &&
3713 (segment->y1 == (double) y_mid) && (segment->y2 == (double) y_mid))
3714 return(MagickFalse);
3715 /*
3716 Average pixels and apply plasma.
3717 */
cristy3ed852e2009-09-05 21:47:34 +00003718 plasma=(MagickRealType) QuantumRange/(2.0*attenuate);
3719 if ((segment->x1 != (double) x_mid) || (segment->x2 != (double) x_mid))
3720 {
cristy3ed852e2009-09-05 21:47:34 +00003721 /*
3722 Left pixel.
3723 */
cristybb503372010-05-27 20:51:26 +00003724 x=(ssize_t) ceil(segment->x1-0.5);
cristy1707c6c2012-01-18 23:30:54 +00003725 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),1,1,
3726 exception);
3727 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),1,1,
3728 exception);
cristyc5c6f662010-09-22 14:23:02 +00003729 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003730 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3731 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003732 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003733 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3734 {
cristye2a912b2011-12-05 20:02:07 +00003735 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003736 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003737 if (traits == UndefinedPixelTrait)
3738 continue;
3739 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3740 }
cristyc5c6f662010-09-22 14:23:02 +00003741 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003742 if (segment->x1 != segment->x2)
3743 {
3744 /*
3745 Right pixel.
3746 */
cristybb503372010-05-27 20:51:26 +00003747 x=(ssize_t) ceil(segment->x2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003748 u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
3749 1,1,exception);
3750 v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
3751 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003752 q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003753 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3754 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003755 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003756 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3757 {
cristye2a912b2011-12-05 20:02:07 +00003758 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003759 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003760 if (traits == UndefinedPixelTrait)
3761 continue;
3762 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3763 }
cristyc5c6f662010-09-22 14:23:02 +00003764 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003765 }
3766 }
3767 if ((segment->y1 != (double) y_mid) || (segment->y2 != (double) y_mid))
3768 {
3769 if ((segment->x1 != (double) x_mid) || (segment->y2 != (double) y_mid))
3770 {
cristy3ed852e2009-09-05 21:47:34 +00003771 /*
3772 Bottom pixel.
3773 */
cristybb503372010-05-27 20:51:26 +00003774 y=(ssize_t) ceil(segment->y2-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 if (segment->y1 != segment->y2)
3794 {
cristy3ed852e2009-09-05 21:47:34 +00003795 /*
3796 Top pixel.
3797 */
cristybb503372010-05-27 20:51:26 +00003798 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003799 u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
3800 1,1,exception);
3801 v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
3802 1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003803 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003804 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3805 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003806 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003807 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3808 {
cristye2a912b2011-12-05 20:02:07 +00003809 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003810 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003811 if (traits == UndefinedPixelTrait)
3812 continue;
3813 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3814 }
cristyc5c6f662010-09-22 14:23:02 +00003815 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003816 }
3817 }
3818 if ((segment->x1 != segment->x2) || (segment->y1 != segment->y2))
3819 {
cristy3ed852e2009-09-05 21:47:34 +00003820 /*
3821 Middle pixel.
3822 */
cristybb503372010-05-27 20:51:26 +00003823 x=(ssize_t) ceil(segment->x1-0.5);
3824 y=(ssize_t) ceil(segment->y1-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003825 u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
cristybb503372010-05-27 20:51:26 +00003826 x=(ssize_t) ceil(segment->x2-0.5);
3827 y=(ssize_t) ceil(segment->y2-0.5);
cristyda1f9c12011-10-02 21:39:49 +00003828 v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
cristyc5c6f662010-09-22 14:23:02 +00003829 q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
cristyda1f9c12011-10-02 21:39:49 +00003830 if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
3831 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00003832 return(MagickTrue);
cristyda1f9c12011-10-02 21:39:49 +00003833 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3834 {
cristye2a912b2011-12-05 20:02:07 +00003835 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00003836 traits=GetPixelChannelMapTraits(image,channel);
cristyda1f9c12011-10-02 21:39:49 +00003837 if (traits == UndefinedPixelTrait)
3838 continue;
3839 q[i]=PlasmaPixel(random_info,(u[channel]+v[channel])/2.0,plasma);
3840 }
cristyc5c6f662010-09-22 14:23:02 +00003841 (void) SyncCacheViewAuthenticPixels(image_view,exception);
cristy3ed852e2009-09-05 21:47:34 +00003842 }
3843 if (((segment->x2-segment->x1) < 3.0) && ((segment->y2-segment->y1) < 3.0))
3844 return(MagickTrue);
3845 return(MagickFalse);
3846}
cristyda1f9c12011-10-02 21:39:49 +00003847
cristy3ed852e2009-09-05 21:47:34 +00003848MagickExport MagickBooleanType PlasmaImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00003849 const SegmentInfo *segment,size_t attenuate,size_t depth,
3850 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003851{
cristyc5c6f662010-09-22 14:23:02 +00003852 CacheView
cristyda1f9c12011-10-02 21:39:49 +00003853 *image_view,
3854 *u_view,
3855 *v_view;
cristyc5c6f662010-09-22 14:23:02 +00003856
cristy3ed852e2009-09-05 21:47:34 +00003857 MagickBooleanType
3858 status;
3859
3860 RandomInfo
3861 *random_info;
3862
3863 if (image->debug != MagickFalse)
3864 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
3865 assert(image != (Image *) NULL);
3866 assert(image->signature == MagickSignature);
3867 if (image->debug != MagickFalse)
3868 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
cristy5cbc0162011-08-29 00:36:28 +00003869 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristyc5c6f662010-09-22 14:23:02 +00003870 return(MagickFalse);
3871 image_view=AcquireCacheView(image);
cristyda1f9c12011-10-02 21:39:49 +00003872 u_view=AcquireCacheView(image);
3873 v_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00003874 random_info=AcquireRandomInfo();
cristyda1f9c12011-10-02 21:39:49 +00003875 status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
3876 attenuate,depth,exception);
cristy3ed852e2009-09-05 21:47:34 +00003877 random_info=DestroyRandomInfo(random_info);
cristyda1f9c12011-10-02 21:39:49 +00003878 v_view=DestroyCacheView(v_view);
3879 u_view=DestroyCacheView(u_view);
cristyc5c6f662010-09-22 14:23:02 +00003880 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00003881 return(status);
3882}
3883
3884/*
3885%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3886% %
3887% %
3888% %
3889% P o l a r o i d I m a g e %
3890% %
3891% %
3892% %
3893%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3894%
3895% PolaroidImage() simulates a Polaroid picture.
3896%
3897% The format of the AnnotateImage method is:
3898%
3899% Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003900% const char *caption,const double angle,
3901% const PixelInterpolateMethod method,ExceptionInfo exception)
cristy3ed852e2009-09-05 21:47:34 +00003902%
3903% A description of each parameter follows:
3904%
3905% o image: the image.
3906%
3907% o draw_info: the draw info.
3908%
cristye9e3d382011-12-14 01:50:13 +00003909% o caption: the Polaroid caption.
3910%
cristycee97112010-05-28 00:44:52 +00003911% o angle: Apply the effect along this angle.
cristy3ed852e2009-09-05 21:47:34 +00003912%
cristy5c4e2582011-09-11 19:21:03 +00003913% o method: the pixel interpolation method.
3914%
cristy3ed852e2009-09-05 21:47:34 +00003915% o exception: return any errors or warnings in this structure.
3916%
3917*/
3918MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
cristye9e3d382011-12-14 01:50:13 +00003919 const char *caption,const double angle,const PixelInterpolateMethod method,
cristy5c4e2582011-09-11 19:21:03 +00003920 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003921{
cristy3ed852e2009-09-05 21:47:34 +00003922 Image
3923 *bend_image,
3924 *caption_image,
3925 *flop_image,
3926 *picture_image,
3927 *polaroid_image,
3928 *rotate_image,
3929 *trim_image;
3930
cristybb503372010-05-27 20:51:26 +00003931 size_t
cristy3ed852e2009-09-05 21:47:34 +00003932 height;
3933
cristy9d314ff2011-03-09 01:30:28 +00003934 ssize_t
3935 quantum;
3936
cristy3ed852e2009-09-05 21:47:34 +00003937 /*
3938 Simulate a Polaroid picture.
3939 */
3940 assert(image != (Image *) NULL);
3941 assert(image->signature == MagickSignature);
3942 if (image->debug != MagickFalse)
3943 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3944 assert(exception != (ExceptionInfo *) NULL);
3945 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00003946 quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
cristy3ed852e2009-09-05 21:47:34 +00003947 image->rows)/25.0,10.0);
3948 height=image->rows+2*quantum;
3949 caption_image=(Image *) NULL;
cristye9e3d382011-12-14 01:50:13 +00003950 if (caption != (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003951 {
3952 char
cristye9e3d382011-12-14 01:50:13 +00003953 geometry[MaxTextExtent],
3954 *text;
cristy3ed852e2009-09-05 21:47:34 +00003955
3956 DrawInfo
3957 *annotate_info;
3958
cristy3ed852e2009-09-05 21:47:34 +00003959 MagickBooleanType
3960 status;
3961
cristy9d314ff2011-03-09 01:30:28 +00003962 ssize_t
3963 count;
3964
cristy3ed852e2009-09-05 21:47:34 +00003965 TypeMetric
3966 metrics;
3967
3968 /*
3969 Generate caption image.
3970 */
3971 caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
3972 if (caption_image == (Image *) NULL)
3973 return((Image *) NULL);
3974 annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
cristye9e3d382011-12-14 01:50:13 +00003975 text=InterpretImageProperties((ImageInfo *) NULL,(Image *) image,caption,
3976 exception);
3977 (void) CloneString(&annotate_info->text,text);
cristy6b1d05e2010-09-22 19:17:27 +00003978 count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,&metrics,
cristye9e3d382011-12-14 01:50:13 +00003979 &text,exception);
3980 status=SetImageExtent(caption_image,image->columns,(size_t) ((count+1)*
3981 (metrics.ascent-metrics.descent)+0.5),exception);
cristy3ed852e2009-09-05 21:47:34 +00003982 if (status == MagickFalse)
3983 caption_image=DestroyImage(caption_image);
3984 else
3985 {
3986 caption_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00003987 (void) SetImageBackgroundColor(caption_image,exception);
cristye9e3d382011-12-14 01:50:13 +00003988 (void) CloneString(&annotate_info->text,text);
cristyb51dff52011-05-19 16:55:47 +00003989 (void) FormatLocaleString(geometry,MaxTextExtent,"+0+%g",
cristy3ed852e2009-09-05 21:47:34 +00003990 metrics.ascent);
3991 if (annotate_info->gravity == UndefinedGravity)
3992 (void) CloneString(&annotate_info->geometry,AcquireString(
3993 geometry));
cristy5cbc0162011-08-29 00:36:28 +00003994 (void) AnnotateImage(caption_image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003995 height+=caption_image->rows;
3996 }
3997 annotate_info=DestroyDrawInfo(annotate_info);
cristye9e3d382011-12-14 01:50:13 +00003998 text=DestroyString(text);
cristy3ed852e2009-09-05 21:47:34 +00003999 }
4000 picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
4001 exception);
4002 if (picture_image == (Image *) NULL)
4003 {
4004 if (caption_image != (Image *) NULL)
4005 caption_image=DestroyImage(caption_image);
4006 return((Image *) NULL);
4007 }
4008 picture_image->background_color=image->border_color;
cristyea1a8aa2011-10-20 13:24:06 +00004009 (void) SetImageBackgroundColor(picture_image,exception);
cristye941a752011-10-15 01:52:48 +00004010 (void) CompositeImage(picture_image,OverCompositeOp,image,quantum,quantum,
4011 exception);
cristy3ed852e2009-09-05 21:47:34 +00004012 if (caption_image != (Image *) NULL)
4013 {
cristy1707c6c2012-01-18 23:30:54 +00004014 (void) CompositeImage(picture_image,OverCompositeOp,caption_image,quantum,
4015 (ssize_t) (image->rows+3*quantum/2),exception);
cristy3ed852e2009-09-05 21:47:34 +00004016 caption_image=DestroyImage(caption_image);
4017 }
cristy9950d572011-10-01 18:22:35 +00004018 (void) QueryColorCompliance("none",AllCompliance,
4019 &picture_image->background_color,exception);
cristy63240882011-08-05 19:05:27 +00004020 (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004021 rotate_image=RotateImage(picture_image,90.0,exception);
4022 picture_image=DestroyImage(picture_image);
4023 if (rotate_image == (Image *) NULL)
4024 return((Image *) NULL);
4025 picture_image=rotate_image;
4026 bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
cristy5c4e2582011-09-11 19:21:03 +00004027 picture_image->columns,method,exception);
cristy3ed852e2009-09-05 21:47:34 +00004028 picture_image=DestroyImage(picture_image);
4029 if (bend_image == (Image *) NULL)
4030 return((Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00004031 picture_image=bend_image;
4032 rotate_image=RotateImage(picture_image,-90.0,exception);
4033 picture_image=DestroyImage(picture_image);
4034 if (rotate_image == (Image *) NULL)
4035 return((Image *) NULL);
4036 picture_image=rotate_image;
4037 picture_image->background_color=image->background_color;
cristy78ec1a92011-12-09 09:25:34 +00004038 polaroid_image=ShadowImage(picture_image,80.0,2.0,0.0,quantum/3,quantum/3,
cristy3ed852e2009-09-05 21:47:34 +00004039 exception);
4040 if (polaroid_image == (Image *) NULL)
4041 {
4042 picture_image=DestroyImage(picture_image);
4043 return(picture_image);
4044 }
4045 flop_image=FlopImage(polaroid_image,exception);
4046 polaroid_image=DestroyImage(polaroid_image);
4047 if (flop_image == (Image *) NULL)
4048 {
4049 picture_image=DestroyImage(picture_image);
4050 return(picture_image);
4051 }
4052 polaroid_image=flop_image;
cristy1707c6c2012-01-18 23:30:54 +00004053 (void) CompositeImage(polaroid_image,OverCompositeOp,picture_image,(ssize_t)
4054 (-0.01*picture_image->columns/2.0),0L,exception);
cristy3ed852e2009-09-05 21:47:34 +00004055 picture_image=DestroyImage(picture_image);
cristy9950d572011-10-01 18:22:35 +00004056 (void) QueryColorCompliance("none",AllCompliance,
4057 &polaroid_image->background_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00004058 rotate_image=RotateImage(polaroid_image,angle,exception);
4059 polaroid_image=DestroyImage(polaroid_image);
4060 if (rotate_image == (Image *) NULL)
4061 return((Image *) NULL);
4062 polaroid_image=rotate_image;
4063 trim_image=TrimImage(polaroid_image,exception);
4064 polaroid_image=DestroyImage(polaroid_image);
4065 if (trim_image == (Image *) NULL)
4066 return((Image *) NULL);
4067 polaroid_image=trim_image;
4068 return(polaroid_image);
4069}
4070
4071/*
4072%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4073% %
4074% %
4075% %
cristy3ed852e2009-09-05 21:47:34 +00004076% S e p i a T o n e I m a g e %
4077% %
4078% %
4079% %
4080%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4081%
4082% MagickSepiaToneImage() applies a special effect to the image, similar to the
4083% effect achieved in a photo darkroom by sepia toning. Threshold ranges from
4084% 0 to QuantumRange and is a measure of the extent of the sepia toning. A
4085% threshold of 80% is a good starting point for a reasonable tone.
4086%
4087% The format of the SepiaToneImage method is:
4088%
4089% Image *SepiaToneImage(const Image *image,const double threshold,
4090% ExceptionInfo *exception)
4091%
4092% A description of each parameter follows:
4093%
4094% o image: the image.
4095%
4096% o threshold: the tone threshold.
4097%
4098% o exception: return any errors or warnings in this structure.
4099%
4100*/
4101MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
4102 ExceptionInfo *exception)
4103{
4104#define SepiaToneImageTag "SepiaTone/Image"
4105
cristyc4c8d132010-01-07 01:58:38 +00004106 CacheView
4107 *image_view,
4108 *sepia_view;
4109
cristy3ed852e2009-09-05 21:47:34 +00004110 Image
4111 *sepia_image;
4112
cristy3ed852e2009-09-05 21:47:34 +00004113 MagickBooleanType
4114 status;
4115
cristybb503372010-05-27 20:51:26 +00004116 MagickOffsetType
4117 progress;
4118
4119 ssize_t
4120 y;
4121
cristy3ed852e2009-09-05 21:47:34 +00004122 /*
4123 Initialize sepia-toned image attributes.
4124 */
4125 assert(image != (const Image *) NULL);
4126 assert(image->signature == MagickSignature);
4127 if (image->debug != MagickFalse)
4128 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4129 assert(exception != (ExceptionInfo *) NULL);
4130 assert(exception->signature == MagickSignature);
cristy1707c6c2012-01-18 23:30:54 +00004131 sepia_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004132 if (sepia_image == (Image *) NULL)
4133 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004134 if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004135 {
cristy3ed852e2009-09-05 21:47:34 +00004136 sepia_image=DestroyImage(sepia_image);
4137 return((Image *) NULL);
4138 }
4139 /*
4140 Tone each row of the image.
4141 */
4142 status=MagickTrue;
4143 progress=0;
4144 image_view=AcquireCacheView(image);
4145 sepia_view=AcquireCacheView(sepia_image);
cristyb5d5f722009-11-04 03:03:49 +00004146#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00004147 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004148#endif
cristybb503372010-05-27 20:51:26 +00004149 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004150 {
cristy4c08aed2011-07-01 19:47:50 +00004151 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004152 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00004153
cristybb503372010-05-27 20:51:26 +00004154 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004155 x;
4156
cristy4c08aed2011-07-01 19:47:50 +00004157 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004158 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004159
4160 if (status == MagickFalse)
4161 continue;
4162 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00004163 q=GetCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00004164 exception);
cristy4c08aed2011-07-01 19:47:50 +00004165 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004166 {
4167 status=MagickFalse;
4168 continue;
4169 }
cristybb503372010-05-27 20:51:26 +00004170 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004171 {
4172 MagickRealType
4173 intensity,
4174 tone;
4175
cristy4c08aed2011-07-01 19:47:50 +00004176 intensity=(MagickRealType) GetPixelIntensity(image,p);
cristy3ed852e2009-09-05 21:47:34 +00004177 tone=intensity > threshold ? (MagickRealType) QuantumRange : intensity+
4178 (MagickRealType) QuantumRange-threshold;
cristy4c08aed2011-07-01 19:47:50 +00004179 SetPixelRed(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004180 tone=intensity > (7.0*threshold/6.0) ? (MagickRealType) QuantumRange :
4181 intensity+(MagickRealType) QuantumRange-7.0*threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004182 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004183 tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
cristy4c08aed2011-07-01 19:47:50 +00004184 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristy3ed852e2009-09-05 21:47:34 +00004185 tone=threshold/7.0;
cristy4c08aed2011-07-01 19:47:50 +00004186 if ((MagickRealType) GetPixelGreen(image,q) < tone)
4187 SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
4188 if ((MagickRealType) GetPixelBlue(image,q) < tone)
4189 SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
cristyed231572011-07-14 02:18:59 +00004190 p+=GetPixelChannels(image);
4191 q+=GetPixelChannels(sepia_image);
cristy3ed852e2009-09-05 21:47:34 +00004192 }
4193 if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
4194 status=MagickFalse;
4195 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4196 {
4197 MagickBooleanType
4198 proceed;
4199
cristyb5d5f722009-11-04 03:03:49 +00004200#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00004201 #pragma omp critical (MagickCore_SepiaToneImage)
4202#endif
4203 proceed=SetImageProgress(image,SepiaToneImageTag,progress++,
4204 image->rows);
4205 if (proceed == MagickFalse)
4206 status=MagickFalse;
4207 }
4208 }
4209 sepia_view=DestroyCacheView(sepia_view);
4210 image_view=DestroyCacheView(image_view);
cristye23ec9d2011-08-16 18:15:40 +00004211 (void) NormalizeImage(sepia_image,exception);
4212 (void) ContrastImage(sepia_image,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004213 if (status == MagickFalse)
4214 sepia_image=DestroyImage(sepia_image);
4215 return(sepia_image);
4216}
4217
4218/*
4219%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4220% %
4221% %
4222% %
4223% S h a d o w I m a g e %
4224% %
4225% %
4226% %
4227%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4228%
4229% ShadowImage() simulates a shadow from the specified image and returns it.
4230%
4231% The format of the ShadowImage method is:
4232%
cristy70cddf72011-12-10 22:42:42 +00004233% Image *ShadowImage(const Image *image,const double alpha,
cristyeb6e6582011-12-09 09:14:23 +00004234% const double sigma,const double bias,const ssize_t x_offset,
4235% const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004236%
4237% A description of each parameter follows:
4238%
4239% o image: the image.
4240%
cristy70cddf72011-12-10 22:42:42 +00004241% o alpha: percentage transparency.
cristy3ed852e2009-09-05 21:47:34 +00004242%
4243% o sigma: the standard deviation of the Gaussian, in pixels.
4244%
cristyeb6e6582011-12-09 09:14:23 +00004245% o bias: the bias.
4246%
cristy3ed852e2009-09-05 21:47:34 +00004247% o x_offset: the shadow x-offset.
4248%
4249% o y_offset: the shadow y-offset.
4250%
4251% o exception: return any errors or warnings in this structure.
4252%
4253*/
cristy70cddf72011-12-10 22:42:42 +00004254MagickExport Image *ShadowImage(const Image *image,const double alpha,
cristyeb6e6582011-12-09 09:14:23 +00004255 const double sigma,const double bias,const ssize_t x_offset,
4256 const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004257{
4258#define ShadowImageTag "Shadow/Image"
4259
cristy70cddf72011-12-10 22:42:42 +00004260 CacheView
4261 *image_view;
4262
cristybd5a96c2011-08-21 00:04:26 +00004263 ChannelType
4264 channel_mask;
4265
cristy3ed852e2009-09-05 21:47:34 +00004266 Image
4267 *border_image,
4268 *clone_image,
4269 *shadow_image;
4270
cristy70cddf72011-12-10 22:42:42 +00004271 MagickBooleanType
4272 status;
4273
cristy3ed852e2009-09-05 21:47:34 +00004274 RectangleInfo
4275 border_info;
4276
cristy70cddf72011-12-10 22:42:42 +00004277 ssize_t
4278 y;
4279
cristy3ed852e2009-09-05 21:47:34 +00004280 assert(image != (Image *) NULL);
4281 assert(image->signature == MagickSignature);
4282 if (image->debug != MagickFalse)
4283 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4284 assert(exception != (ExceptionInfo *) NULL);
4285 assert(exception->signature == MagickSignature);
4286 clone_image=CloneImage(image,0,0,MagickTrue,exception);
4287 if (clone_image == (Image *) NULL)
4288 return((Image *) NULL);
4289 (void) SetImageVirtualPixelMethod(clone_image,EdgeVirtualPixelMethod);
cristybb503372010-05-27 20:51:26 +00004290 border_info.width=(size_t) floor(2.0*sigma+0.5);
4291 border_info.height=(size_t) floor(2.0*sigma+0.5);
cristy3ed852e2009-09-05 21:47:34 +00004292 border_info.x=0;
4293 border_info.y=0;
cristy9950d572011-10-01 18:22:35 +00004294 (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
4295 exception);
cristy70cddf72011-12-10 22:42:42 +00004296 clone_image->matte=MagickTrue;
4297 border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
cristy3ed852e2009-09-05 21:47:34 +00004298 clone_image=DestroyImage(clone_image);
4299 if (border_image == (Image *) NULL)
4300 return((Image *) NULL);
4301 if (border_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00004302 (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004303 /*
4304 Shadow image.
4305 */
cristy70cddf72011-12-10 22:42:42 +00004306 status=MagickTrue;
4307 image_view=AcquireCacheView(border_image);
4308 for (y=0; y < (ssize_t) border_image->rows; y++)
4309 {
4310 PixelInfo
4311 background_color;
4312
4313 register Quantum
4314 *restrict q;
4315
4316 register ssize_t
4317 x;
4318
4319 if (status == MagickFalse)
4320 continue;
4321 q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
4322 exception);
4323 if (q == (Quantum *) NULL)
4324 {
4325 status=MagickFalse;
4326 continue;
4327 }
4328 background_color=border_image->background_color;
4329 background_color.matte=MagickTrue;
4330 for (x=0; x < (ssize_t) border_image->columns; x++)
4331 {
4332 if (border_image->matte != MagickFalse)
4333 background_color.alpha=GetPixelAlpha(border_image,q)*alpha/100.0;
4334 SetPixelInfoPixel(border_image,&background_color,q);
4335 q+=GetPixelChannels(border_image);
4336 }
4337 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4338 status=MagickFalse;
4339 }
4340 image_view=DestroyCacheView(image_view);
4341 if (status == MagickFalse)
4342 {
4343 border_image=DestroyImage(border_image);
4344 return((Image *) NULL);
4345 }
cristybd5a96c2011-08-21 00:04:26 +00004346 channel_mask=SetPixelChannelMask(border_image,AlphaChannel);
cristyb27bb772011-12-11 16:12:50 +00004347 shadow_image=BlurImage(border_image,0.0,sigma,bias,exception);
cristy3ed852e2009-09-05 21:47:34 +00004348 border_image=DestroyImage(border_image);
4349 if (shadow_image == (Image *) NULL)
4350 return((Image *) NULL);
cristyae1969f2011-12-10 03:07:36 +00004351 (void) SetPixelChannelMapMask(shadow_image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00004352 if (shadow_image->page.width == 0)
4353 shadow_image->page.width=shadow_image->columns;
4354 if (shadow_image->page.height == 0)
4355 shadow_image->page.height=shadow_image->rows;
cristybb503372010-05-27 20:51:26 +00004356 shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
4357 shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
4358 shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
4359 shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
cristy3ed852e2009-09-05 21:47:34 +00004360 return(shadow_image);
4361}
4362
4363/*
4364%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4365% %
4366% %
4367% %
4368% S k e t c h I m a g e %
4369% %
4370% %
4371% %
4372%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4373%
4374% SketchImage() simulates a pencil sketch. We convolve the image with a
4375% Gaussian operator of the given radius and standard deviation (sigma). For
4376% reasonable results, radius should be larger than sigma. Use a radius of 0
4377% and SketchImage() selects a suitable radius for you. Angle gives the angle
4378% of the sketch.
4379%
4380% The format of the SketchImage method is:
4381%
4382% Image *SketchImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00004383% const double sigma,const double angle,const double bias,
4384% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004385%
4386% A description of each parameter follows:
4387%
4388% o image: the image.
4389%
cristy574cc262011-08-05 01:23:58 +00004390% o radius: the radius of the Gaussian, in pixels, not counting the
4391% center pixel.
cristy3ed852e2009-09-05 21:47:34 +00004392%
4393% o sigma: the standard deviation of the Gaussian, in pixels.
4394%
cristyf7ef0252011-09-09 14:50:06 +00004395% o angle: apply the effect along this angle.
4396%
4397% o bias: the bias.
cristy3ed852e2009-09-05 21:47:34 +00004398%
4399% o exception: return any errors or warnings in this structure.
4400%
4401*/
4402MagickExport Image *SketchImage(const Image *image,const double radius,
cristyf7ef0252011-09-09 14:50:06 +00004403 const double sigma,const double angle,const double bias,
4404 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004405{
cristyfa112112010-01-04 17:48:07 +00004406 CacheView
4407 *random_view;
4408
cristy3ed852e2009-09-05 21:47:34 +00004409 Image
4410 *blend_image,
4411 *blur_image,
4412 *dodge_image,
4413 *random_image,
4414 *sketch_image;
4415
cristy3ed852e2009-09-05 21:47:34 +00004416 MagickBooleanType
4417 status;
4418
cristy3ed852e2009-09-05 21:47:34 +00004419 RandomInfo
cristyfa112112010-01-04 17:48:07 +00004420 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00004421
cristy9d314ff2011-03-09 01:30:28 +00004422 ssize_t
4423 y;
4424
cristy3ed852e2009-09-05 21:47:34 +00004425 /*
4426 Sketch image.
4427 */
4428 random_image=CloneImage(image,image->columns << 1,image->rows << 1,
4429 MagickTrue,exception);
4430 if (random_image == (Image *) NULL)
4431 return((Image *) NULL);
4432 status=MagickTrue;
cristy1b784432009-12-19 02:20:40 +00004433 random_info=AcquireRandomInfoThreadSet();
cristy3ed852e2009-09-05 21:47:34 +00004434 random_view=AcquireCacheView(random_image);
cristy1b784432009-12-19 02:20:40 +00004435#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00004436 #pragma omp parallel for schedule(static,4) shared(status)
cristy1b784432009-12-19 02:20:40 +00004437#endif
cristybb503372010-05-27 20:51:26 +00004438 for (y=0; y < (ssize_t) random_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004439 {
cristy5c9e6f22010-09-17 17:31:01 +00004440 const int
4441 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00004442
cristybb503372010-05-27 20:51:26 +00004443 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004444 x;
4445
cristy4c08aed2011-07-01 19:47:50 +00004446 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004447 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004448
cristy1b784432009-12-19 02:20:40 +00004449 if (status == MagickFalse)
4450 continue;
cristy3ed852e2009-09-05 21:47:34 +00004451 q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
4452 exception);
cristyacd2ed22011-08-30 01:44:23 +00004453 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004454 {
4455 status=MagickFalse;
4456 continue;
4457 }
cristybb503372010-05-27 20:51:26 +00004458 for (x=0; x < (ssize_t) random_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004459 {
cristy76f512e2011-09-12 01:26:56 +00004460 MagickRealType
4461 value;
4462
4463 register ssize_t
4464 i;
4465
4466 value=GetPseudoRandomValue(random_info[id]);
4467 for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
4468 {
cristyabace412011-12-11 15:56:53 +00004469 PixelChannel
4470 channel;
4471
cristy76f512e2011-09-12 01:26:56 +00004472 PixelTrait
4473 traits;
4474
cristyabace412011-12-11 15:56:53 +00004475 channel=GetPixelChannelMapChannel(image,i);
4476 traits=GetPixelChannelMapTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004477 if (traits == UndefinedPixelTrait)
4478 continue;
4479 q[i]=ClampToQuantum(QuantumRange*value);
4480 }
cristyed231572011-07-14 02:18:59 +00004481 q+=GetPixelChannels(random_image);
cristy3ed852e2009-09-05 21:47:34 +00004482 }
4483 if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
4484 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004485 }
4486 random_view=DestroyCacheView(random_view);
cristy1b784432009-12-19 02:20:40 +00004487 random_info=DestroyRandomInfoThreadSet(random_info);
cristy3ed852e2009-09-05 21:47:34 +00004488 if (status == MagickFalse)
4489 {
4490 random_image=DestroyImage(random_image);
4491 return(random_image);
4492 }
cristyf7ef0252011-09-09 14:50:06 +00004493 blur_image=MotionBlurImage(random_image,radius,sigma,angle,bias,exception);
cristy3ed852e2009-09-05 21:47:34 +00004494 random_image=DestroyImage(random_image);
4495 if (blur_image == (Image *) NULL)
4496 return((Image *) NULL);
cristy6bfd6902011-12-09 01:33:45 +00004497 dodge_image=EdgeImage(blur_image,radius,1.0,exception);
cristy3ed852e2009-09-05 21:47:34 +00004498 blur_image=DestroyImage(blur_image);
4499 if (dodge_image == (Image *) NULL)
4500 return((Image *) NULL);
cristye23ec9d2011-08-16 18:15:40 +00004501 (void) NormalizeImage(dodge_image,exception);
cristyb3e7c6c2011-07-24 01:43:55 +00004502 (void) NegateImage(dodge_image,MagickFalse,exception);
cristye941a752011-10-15 01:52:48 +00004503 (void) TransformImage(&dodge_image,(char *) NULL,"50%",exception);
cristy3ed852e2009-09-05 21:47:34 +00004504 sketch_image=CloneImage(image,0,0,MagickTrue,exception);
4505 if (sketch_image == (Image *) NULL)
4506 {
4507 dodge_image=DestroyImage(dodge_image);
4508 return((Image *) NULL);
4509 }
cristye941a752011-10-15 01:52:48 +00004510 (void) CompositeImage(sketch_image,ColorDodgeCompositeOp,dodge_image,0,0,
4511 exception);
cristy3ed852e2009-09-05 21:47:34 +00004512 dodge_image=DestroyImage(dodge_image);
4513 blend_image=CloneImage(image,0,0,MagickTrue,exception);
4514 if (blend_image == (Image *) NULL)
4515 {
4516 sketch_image=DestroyImage(sketch_image);
4517 return((Image *) NULL);
4518 }
4519 (void) SetImageArtifact(blend_image,"compose:args","20x80");
cristye941a752011-10-15 01:52:48 +00004520 (void) CompositeImage(sketch_image,BlendCompositeOp,blend_image,0,0,
4521 exception);
cristy3ed852e2009-09-05 21:47:34 +00004522 blend_image=DestroyImage(blend_image);
4523 return(sketch_image);
4524}
4525
4526/*
4527%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4528% %
4529% %
4530% %
4531% S o l a r i z e I m a g e %
4532% %
4533% %
4534% %
4535%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4536%
4537% SolarizeImage() applies a special effect to the image, similar to the effect
4538% achieved in a photo darkroom by selectively exposing areas of photo
4539% sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
4540% measure of the extent of the solarization.
4541%
4542% The format of the SolarizeImage method is:
4543%
cristy5cbc0162011-08-29 00:36:28 +00004544% MagickBooleanType SolarizeImage(Image *image,const double threshold,
4545% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004546%
4547% A description of each parameter follows:
4548%
4549% o image: the image.
4550%
4551% o threshold: Define the extent of the solarization.
4552%
cristy5cbc0162011-08-29 00:36:28 +00004553% o exception: return any errors or warnings in this structure.
4554%
cristy3ed852e2009-09-05 21:47:34 +00004555*/
4556MagickExport MagickBooleanType SolarizeImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00004557 const double threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004558{
4559#define SolarizeImageTag "Solarize/Image"
4560
cristyc4c8d132010-01-07 01:58:38 +00004561 CacheView
4562 *image_view;
4563
cristy3ed852e2009-09-05 21:47:34 +00004564 MagickBooleanType
4565 status;
4566
cristybb503372010-05-27 20:51:26 +00004567 MagickOffsetType
4568 progress;
4569
4570 ssize_t
4571 y;
4572
cristy3ed852e2009-09-05 21:47:34 +00004573 assert(image != (Image *) NULL);
4574 assert(image->signature == MagickSignature);
4575 if (image->debug != MagickFalse)
4576 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4577 if (image->storage_class == PseudoClass)
4578 {
cristybb503372010-05-27 20:51:26 +00004579 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004580 i;
4581
4582 /*
4583 Solarize colormap.
4584 */
cristybb503372010-05-27 20:51:26 +00004585 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00004586 {
4587 if ((MagickRealType) image->colormap[i].red > threshold)
4588 image->colormap[i].red=(Quantum) QuantumRange-image->colormap[i].red;
4589 if ((MagickRealType) image->colormap[i].green > threshold)
4590 image->colormap[i].green=(Quantum) QuantumRange-
4591 image->colormap[i].green;
4592 if ((MagickRealType) image->colormap[i].blue > threshold)
4593 image->colormap[i].blue=(Quantum) QuantumRange-
4594 image->colormap[i].blue;
4595 }
4596 }
4597 /*
4598 Solarize image.
4599 */
4600 status=MagickTrue;
4601 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00004602 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00004603#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00004604 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00004605#endif
cristybb503372010-05-27 20:51:26 +00004606 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004607 {
cristybb503372010-05-27 20:51:26 +00004608 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004609 x;
4610
cristy4c08aed2011-07-01 19:47:50 +00004611 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004612 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004613
4614 if (status == MagickFalse)
4615 continue;
cristy5cbc0162011-08-29 00:36:28 +00004616 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00004617 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004618 {
4619 status=MagickFalse;
4620 continue;
4621 }
cristybb503372010-05-27 20:51:26 +00004622 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004623 {
cristy76f512e2011-09-12 01:26:56 +00004624 register ssize_t
4625 i;
4626
4627 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4628 {
cristyabace412011-12-11 15:56:53 +00004629 PixelChannel
4630 channel;
4631
cristy76f512e2011-09-12 01:26:56 +00004632 PixelTrait
4633 traits;
4634
cristyabace412011-12-11 15:56:53 +00004635 channel=GetPixelChannelMapChannel(image,i);
4636 traits=GetPixelChannelMapTraits(image,channel);
cristy76f512e2011-09-12 01:26:56 +00004637 if ((traits == UndefinedPixelTrait) ||
4638 ((traits & CopyPixelTrait) != 0))
4639 continue;
4640 if ((MagickRealType) q[i] > threshold)
4641 q[i]=QuantumRange-q[i];
4642 }
cristyed231572011-07-14 02:18:59 +00004643 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004644 }
4645 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4646 status=MagickFalse;
4647 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4648 {
4649 MagickBooleanType
4650 proceed;
4651
cristyb5d5f722009-11-04 03:03:49 +00004652#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00004653 #pragma omp critical (MagickCore_SolarizeImage)
4654#endif
4655 proceed=SetImageProgress(image,SolarizeImageTag,progress++,image->rows);
4656 if (proceed == MagickFalse)
4657 status=MagickFalse;
4658 }
4659 }
4660 image_view=DestroyCacheView(image_view);
4661 return(status);
4662}
4663
4664/*
4665%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4666% %
4667% %
4668% %
4669% S t e g a n o I m a g e %
4670% %
4671% %
4672% %
4673%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4674%
4675% SteganoImage() hides a digital watermark within the image. Recover
4676% the hidden watermark later to prove that the authenticity of an image.
4677% Offset defines the start position within the image to hide the watermark.
4678%
4679% The format of the SteganoImage method is:
4680%
4681% Image *SteganoImage(const Image *image,Image *watermark,
4682% ExceptionInfo *exception)
4683%
4684% A description of each parameter follows:
4685%
4686% o image: the image.
4687%
4688% o watermark: the watermark image.
4689%
4690% o exception: return any errors or warnings in this structure.
4691%
4692*/
4693MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
4694 ExceptionInfo *exception)
4695{
cristye1bf8ad2010-09-19 17:07:03 +00004696#define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
cristy4c08aed2011-07-01 19:47:50 +00004697#define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
cristyeaedf062010-05-29 22:36:02 +00004698 | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
cristy3ed852e2009-09-05 21:47:34 +00004699#define SteganoImageTag "Stegano/Image"
4700
cristyb0d3bb92010-09-22 14:37:58 +00004701 CacheView
4702 *stegano_view,
4703 *watermark_view;
4704
cristy3ed852e2009-09-05 21:47:34 +00004705 Image
4706 *stegano_image;
4707
4708 int
4709 c;
4710
cristy3ed852e2009-09-05 21:47:34 +00004711 MagickBooleanType
4712 status;
4713
cristy101ab702011-10-13 13:06:32 +00004714 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004715 pixel;
4716
cristy4c08aed2011-07-01 19:47:50 +00004717 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004718 *q;
4719
cristye1bf8ad2010-09-19 17:07:03 +00004720 register ssize_t
4721 x;
4722
cristybb503372010-05-27 20:51:26 +00004723 size_t
cristyeaedf062010-05-29 22:36:02 +00004724 depth,
4725 one;
cristy3ed852e2009-09-05 21:47:34 +00004726
cristye1bf8ad2010-09-19 17:07:03 +00004727 ssize_t
4728 i,
4729 j,
4730 k,
4731 y;
4732
cristy3ed852e2009-09-05 21:47:34 +00004733 /*
4734 Initialize steganographic image attributes.
4735 */
4736 assert(image != (const Image *) NULL);
4737 assert(image->signature == MagickSignature);
4738 if (image->debug != MagickFalse)
4739 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4740 assert(watermark != (const Image *) NULL);
4741 assert(watermark->signature == MagickSignature);
4742 assert(exception != (ExceptionInfo *) NULL);
4743 assert(exception->signature == MagickSignature);
cristyeaedf062010-05-29 22:36:02 +00004744 one=1UL;
cristy3ed852e2009-09-05 21:47:34 +00004745 stegano_image=CloneImage(image,0,0,MagickTrue,exception);
4746 if (stegano_image == (Image *) NULL)
4747 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004748 if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004749 {
cristy3ed852e2009-09-05 21:47:34 +00004750 stegano_image=DestroyImage(stegano_image);
4751 return((Image *) NULL);
4752 }
4753 stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
4754 /*
4755 Hide watermark in low-order bits of image.
4756 */
4757 c=0;
4758 i=0;
4759 j=0;
4760 depth=stegano_image->depth;
4761 k=image->offset;
cristyda16f162011-02-19 23:52:17 +00004762 status=MagickTrue;
cristyb0d3bb92010-09-22 14:37:58 +00004763 watermark_view=AcquireCacheView(watermark);
4764 stegano_view=AcquireCacheView(stegano_image);
cristybb503372010-05-27 20:51:26 +00004765 for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
cristy3ed852e2009-09-05 21:47:34 +00004766 {
cristybb503372010-05-27 20:51:26 +00004767 for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
cristy3ed852e2009-09-05 21:47:34 +00004768 {
cristybb503372010-05-27 20:51:26 +00004769 for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
cristy3ed852e2009-09-05 21:47:34 +00004770 {
cristyda1f9c12011-10-02 21:39:49 +00004771 Quantum
cristy5f95f4f2011-10-23 01:01:01 +00004772 virtual_pixel[CompositePixelChannel];
cristyda1f9c12011-10-02 21:39:49 +00004773
cristy1707c6c2012-01-18 23:30:54 +00004774 ssize_t
4775 offset;
4776
cristyda1f9c12011-10-02 21:39:49 +00004777 (void) GetOneCacheViewVirtualPixel(watermark_view,x,y,virtual_pixel,
4778 exception);
4779 pixel.red=(double) virtual_pixel[RedPixelChannel];
4780 pixel.green=(double) virtual_pixel[GreenPixelChannel];
4781 pixel.blue=(double) virtual_pixel[BluePixelChannel];
4782 pixel.alpha=(double) virtual_pixel[AlphaPixelChannel];
cristy1707c6c2012-01-18 23:30:54 +00004783 offset=k/(ssize_t) stegano_image->columns;
4784 if (offset >= (ssize_t) stegano_image->rows)
cristy3ed852e2009-09-05 21:47:34 +00004785 break;
cristyb0d3bb92010-09-22 14:37:58 +00004786 q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
4787 stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
4788 exception);
cristyacd2ed22011-08-30 01:44:23 +00004789 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004790 break;
4791 switch (c)
4792 {
4793 case 0:
4794 {
cristy4c08aed2011-07-01 19:47:50 +00004795 SetPixelRed(image,SetBit(GetPixelRed(image,q),j,GetBit(
cristy101ab702011-10-13 13:06:32 +00004796 GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004797 break;
4798 }
4799 case 1:
4800 {
cristy4c08aed2011-07-01 19:47:50 +00004801 SetPixelGreen(image,SetBit(GetPixelGreen(image,q),j,GetBit(
cristy101ab702011-10-13 13:06:32 +00004802 GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004803 break;
4804 }
4805 case 2:
4806 {
cristy4c08aed2011-07-01 19:47:50 +00004807 SetPixelBlue(image,SetBit(GetPixelBlue(image,q),j,GetBit(
cristy101ab702011-10-13 13:06:32 +00004808 GetPixelInfoIntensity(&pixel),i)),q);
cristy3ed852e2009-09-05 21:47:34 +00004809 break;
4810 }
4811 }
cristyb0d3bb92010-09-22 14:37:58 +00004812 if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004813 break;
4814 c++;
4815 if (c == 3)
4816 c=0;
4817 k++;
cristybb503372010-05-27 20:51:26 +00004818 if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
cristy3ed852e2009-09-05 21:47:34 +00004819 k=0;
4820 if (k == image->offset)
4821 j++;
4822 }
4823 }
cristy8b27a6d2010-02-14 03:31:15 +00004824 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004825 {
cristy8b27a6d2010-02-14 03:31:15 +00004826 MagickBooleanType
4827 proceed;
4828
4829 proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
4830 (depth-i),depth);
4831 if (proceed == MagickFalse)
4832 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004833 }
4834 }
cristyb0d3bb92010-09-22 14:37:58 +00004835 stegano_view=DestroyCacheView(stegano_view);
4836 watermark_view=DestroyCacheView(watermark_view);
cristy3ed852e2009-09-05 21:47:34 +00004837 if (stegano_image->storage_class == PseudoClass)
cristyea1a8aa2011-10-20 13:24:06 +00004838 (void) SyncImage(stegano_image,exception);
cristyda16f162011-02-19 23:52:17 +00004839 if (status == MagickFalse)
4840 {
4841 stegano_image=DestroyImage(stegano_image);
4842 return((Image *) NULL);
4843 }
cristy3ed852e2009-09-05 21:47:34 +00004844 return(stegano_image);
4845}
4846
4847/*
4848%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4849% %
4850% %
4851% %
4852% S t e r e o A n a g l y p h I m a g e %
4853% %
4854% %
4855% %
4856%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4857%
4858% StereoAnaglyphImage() combines two images and produces a single image that
4859% is the composite of a left and right image of a stereo pair. Special
4860% red-green stereo glasses are required to view this effect.
4861%
4862% The format of the StereoAnaglyphImage method is:
4863%
4864% Image *StereoImage(const Image *left_image,const Image *right_image,
4865% ExceptionInfo *exception)
4866% Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004867% const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004868% ExceptionInfo *exception)
4869%
4870% A description of each parameter follows:
4871%
4872% o left_image: the left image.
4873%
4874% o right_image: the right image.
4875%
4876% o exception: return any errors or warnings in this structure.
4877%
4878% o x_offset: amount, in pixels, by which the left image is offset to the
4879% right of the right image.
4880%
4881% o y_offset: amount, in pixels, by which the left image is offset to the
4882% bottom of the right image.
4883%
4884%
4885*/
4886MagickExport Image *StereoImage(const Image *left_image,
4887 const Image *right_image,ExceptionInfo *exception)
4888{
4889 return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
4890}
4891
4892MagickExport Image *StereoAnaglyphImage(const Image *left_image,
cristybb503372010-05-27 20:51:26 +00004893 const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
cristy3ed852e2009-09-05 21:47:34 +00004894 ExceptionInfo *exception)
4895{
4896#define StereoImageTag "Stereo/Image"
4897
4898 const Image
4899 *image;
4900
4901 Image
4902 *stereo_image;
4903
cristy3ed852e2009-09-05 21:47:34 +00004904 MagickBooleanType
4905 status;
4906
cristy9d314ff2011-03-09 01:30:28 +00004907 ssize_t
4908 y;
4909
cristy3ed852e2009-09-05 21:47:34 +00004910 assert(left_image != (const Image *) NULL);
4911 assert(left_image->signature == MagickSignature);
4912 if (left_image->debug != MagickFalse)
4913 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
4914 left_image->filename);
4915 assert(right_image != (const Image *) NULL);
4916 assert(right_image->signature == MagickSignature);
4917 assert(exception != (ExceptionInfo *) NULL);
4918 assert(exception->signature == MagickSignature);
4919 assert(right_image != (const Image *) NULL);
4920 image=left_image;
4921 if ((left_image->columns != right_image->columns) ||
4922 (left_image->rows != right_image->rows))
4923 ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
4924 /*
4925 Initialize stereo image attributes.
4926 */
4927 stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
4928 MagickTrue,exception);
4929 if (stereo_image == (Image *) NULL)
4930 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00004931 if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004932 {
cristy3ed852e2009-09-05 21:47:34 +00004933 stereo_image=DestroyImage(stereo_image);
4934 return((Image *) NULL);
4935 }
4936 /*
4937 Copy left image to red channel and right image to blue channel.
4938 */
cristyda16f162011-02-19 23:52:17 +00004939 status=MagickTrue;
cristybb503372010-05-27 20:51:26 +00004940 for (y=0; y < (ssize_t) stereo_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004941 {
cristy4c08aed2011-07-01 19:47:50 +00004942 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00004943 *restrict p,
4944 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004945
cristybb503372010-05-27 20:51:26 +00004946 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004947 x;
4948
cristy4c08aed2011-07-01 19:47:50 +00004949 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004950 *restrict r;
cristy3ed852e2009-09-05 21:47:34 +00004951
4952 p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
4953 exception);
4954 q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
4955 r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
cristy76f512e2011-09-12 01:26:56 +00004956 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
4957 (r == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00004958 break;
cristybb503372010-05-27 20:51:26 +00004959 for (x=0; x < (ssize_t) stereo_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004960 {
cristy4c08aed2011-07-01 19:47:50 +00004961 SetPixelRed(image,GetPixelRed(left_image,p),r);
cristy76f512e2011-09-12 01:26:56 +00004962 SetPixelGreen(image,GetPixelGreen(right_image,q),r);
4963 SetPixelBlue(image,GetPixelBlue(right_image,q),r);
4964 if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
4965 SetPixelAlpha(image,(GetPixelAlpha(left_image,p)+
4966 GetPixelAlpha(right_image,q))/2,r);
cristyed231572011-07-14 02:18:59 +00004967 p+=GetPixelChannels(left_image);
cristy76f512e2011-09-12 01:26:56 +00004968 q+=GetPixelChannels(right_image);
4969 r+=GetPixelChannels(stereo_image);
cristy3ed852e2009-09-05 21:47:34 +00004970 }
4971 if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
4972 break;
cristy8b27a6d2010-02-14 03:31:15 +00004973 if (image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004974 {
cristy8b27a6d2010-02-14 03:31:15 +00004975 MagickBooleanType
4976 proceed;
4977
cristybb503372010-05-27 20:51:26 +00004978 proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
4979 stereo_image->rows);
cristy8b27a6d2010-02-14 03:31:15 +00004980 if (proceed == MagickFalse)
4981 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00004982 }
4983 }
cristyda16f162011-02-19 23:52:17 +00004984 if (status == MagickFalse)
4985 {
4986 stereo_image=DestroyImage(stereo_image);
4987 return((Image *) NULL);
4988 }
cristy3ed852e2009-09-05 21:47:34 +00004989 return(stereo_image);
4990}
4991
4992/*
4993%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4994% %
4995% %
4996% %
4997% S w i r l I m a g e %
4998% %
4999% %
5000% %
5001%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5002%
5003% SwirlImage() swirls the pixels about the center of the image, where
5004% degrees indicates the sweep of the arc through which each pixel is moved.
5005% You get a more dramatic effect as the degrees move from 1 to 360.
5006%
5007% The format of the SwirlImage method is:
5008%
5009% Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00005010% const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005011%
5012% A description of each parameter follows:
5013%
5014% o image: the image.
5015%
5016% o degrees: Define the tightness of the swirling effect.
5017%
cristy76f512e2011-09-12 01:26:56 +00005018% o method: the pixel interpolation method.
5019%
cristy3ed852e2009-09-05 21:47:34 +00005020% o exception: return any errors or warnings in this structure.
5021%
5022*/
5023MagickExport Image *SwirlImage(const Image *image,double degrees,
cristy76f512e2011-09-12 01:26:56 +00005024 const PixelInterpolateMethod method,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005025{
5026#define SwirlImageTag "Swirl/Image"
5027
cristyfa112112010-01-04 17:48:07 +00005028 CacheView
5029 *image_view,
5030 *swirl_view;
5031
cristy3ed852e2009-09-05 21:47:34 +00005032 Image
5033 *swirl_image;
5034
cristy3ed852e2009-09-05 21:47:34 +00005035 MagickBooleanType
5036 status;
5037
cristybb503372010-05-27 20:51:26 +00005038 MagickOffsetType
5039 progress;
5040
cristy3ed852e2009-09-05 21:47:34 +00005041 MagickRealType
5042 radius;
5043
5044 PointInfo
5045 center,
5046 scale;
5047
cristybb503372010-05-27 20:51:26 +00005048 ssize_t
5049 y;
5050
cristy3ed852e2009-09-05 21:47:34 +00005051 /*
5052 Initialize swirl image attributes.
5053 */
5054 assert(image != (const Image *) NULL);
5055 assert(image->signature == MagickSignature);
5056 if (image->debug != MagickFalse)
5057 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5058 assert(exception != (ExceptionInfo *) NULL);
5059 assert(exception->signature == MagickSignature);
cristy76f512e2011-09-12 01:26:56 +00005060 swirl_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005061 if (swirl_image == (Image *) NULL)
5062 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005063 if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005064 {
cristy3ed852e2009-09-05 21:47:34 +00005065 swirl_image=DestroyImage(swirl_image);
5066 return((Image *) NULL);
5067 }
cristy4c08aed2011-07-01 19:47:50 +00005068 if (swirl_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00005069 swirl_image->matte=MagickTrue;
5070 /*
5071 Compute scaling factor.
5072 */
5073 center.x=(double) image->columns/2.0;
5074 center.y=(double) image->rows/2.0;
5075 radius=MagickMax(center.x,center.y);
5076 scale.x=1.0;
5077 scale.y=1.0;
5078 if (image->columns > image->rows)
5079 scale.y=(double) image->columns/(double) image->rows;
5080 else
5081 if (image->columns < image->rows)
5082 scale.x=(double) image->rows/(double) image->columns;
5083 degrees=(double) DegreesToRadians(degrees);
5084 /*
5085 Swirl image.
5086 */
5087 status=MagickTrue;
5088 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00005089 image_view=AcquireCacheView(image);
5090 swirl_view=AcquireCacheView(swirl_image);
cristyb5d5f722009-11-04 03:03:49 +00005091#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy78778cb2012-01-17 14:48:47 +00005092 #pragma omp parallel for schedule(static,8) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005093#endif
cristybb503372010-05-27 20:51:26 +00005094 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005095 {
cristy3ed852e2009-09-05 21:47:34 +00005096 MagickRealType
5097 distance;
5098
5099 PointInfo
5100 delta;
5101
cristy6d188022011-09-12 13:23:33 +00005102 register const Quantum
5103 *restrict p;
5104
cristybb503372010-05-27 20:51:26 +00005105 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005106 x;
5107
cristy4c08aed2011-07-01 19:47:50 +00005108 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005109 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005110
5111 if (status == MagickFalse)
5112 continue;
cristy6d188022011-09-12 13:23:33 +00005113 p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy1707c6c2012-01-18 23:30:54 +00005114 q=QueueCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
cristy3ed852e2009-09-05 21:47:34 +00005115 exception);
cristy6d188022011-09-12 13:23:33 +00005116 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005117 {
5118 status=MagickFalse;
5119 continue;
5120 }
cristy3ed852e2009-09-05 21:47:34 +00005121 delta.y=scale.y*(double) (y-center.y);
cristybb503372010-05-27 20:51:26 +00005122 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005123 {
5124 /*
5125 Determine if the pixel is within an ellipse.
5126 */
5127 delta.x=scale.x*(double) (x-center.x);
5128 distance=delta.x*delta.x+delta.y*delta.y;
cristy6d188022011-09-12 13:23:33 +00005129 if (distance >= (radius*radius))
5130 {
cristy1707c6c2012-01-18 23:30:54 +00005131 register ssize_t
5132 i;
5133
cristy6d188022011-09-12 13:23:33 +00005134 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy1707c6c2012-01-18 23:30:54 +00005135 {
5136 PixelChannel
5137 channel;
5138
5139 PixelTrait
5140 swirl_traits,
5141 traits;
5142
5143 channel=GetPixelChannelMapChannel(image,i);
5144 traits=GetPixelChannelMapTraits(image,channel);
5145 swirl_traits=GetPixelChannelMapTraits(swirl_image,channel);
5146 if ((traits == UndefinedPixelTrait) ||
5147 (swirl_traits == UndefinedPixelTrait))
5148 continue;
5149 SetPixelChannel(swirl_image,channel,p[i],q);
5150 }
cristy6d188022011-09-12 13:23:33 +00005151 }
5152 else
cristy3ed852e2009-09-05 21:47:34 +00005153 {
5154 MagickRealType
5155 cosine,
5156 factor,
5157 sine;
5158
5159 /*
5160 Swirl the pixel.
5161 */
5162 factor=1.0-sqrt((double) distance)/radius;
5163 sine=sin((double) (degrees*factor*factor));
5164 cosine=cos((double) (degrees*factor*factor));
cristy76f512e2011-09-12 01:26:56 +00005165 status=InterpolatePixelChannels(image,image_view,swirl_image,method,
5166 ((cosine*delta.x-sine*delta.y)/scale.x+center.x),(double)
5167 ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,exception);
cristy3ed852e2009-09-05 21:47:34 +00005168 }
cristy6d188022011-09-12 13:23:33 +00005169 p+=GetPixelChannels(image);
cristyed231572011-07-14 02:18:59 +00005170 q+=GetPixelChannels(swirl_image);
cristy3ed852e2009-09-05 21:47:34 +00005171 }
5172 if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
5173 status=MagickFalse;
5174 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5175 {
5176 MagickBooleanType
5177 proceed;
5178
cristyb5d5f722009-11-04 03:03:49 +00005179#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005180 #pragma omp critical (MagickCore_SwirlImage)
5181#endif
5182 proceed=SetImageProgress(image,SwirlImageTag,progress++,image->rows);
5183 if (proceed == MagickFalse)
5184 status=MagickFalse;
5185 }
5186 }
5187 swirl_view=DestroyCacheView(swirl_view);
5188 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005189 if (status == MagickFalse)
5190 swirl_image=DestroyImage(swirl_image);
5191 return(swirl_image);
5192}
5193
5194/*
5195%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5196% %
5197% %
5198% %
5199% T i n t I m a g e %
5200% %
5201% %
5202% %
5203%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5204%
5205% TintImage() applies a color vector to each pixel in the image. The length
5206% of the vector is 0 for black and white and at its maximum for the midtones.
5207% The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
5208%
5209% The format of the TintImage method is:
5210%
cristyb817c3f2011-10-03 14:00:35 +00005211% Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005212% const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005213%
5214% A description of each parameter follows:
5215%
5216% o image: the image.
5217%
cristyb817c3f2011-10-03 14:00:35 +00005218% o blend: A color value used for tinting.
cristy3ed852e2009-09-05 21:47:34 +00005219%
5220% o tint: A color value used for tinting.
5221%
5222% o exception: return any errors or warnings in this structure.
5223%
5224*/
cristyb817c3f2011-10-03 14:00:35 +00005225MagickExport Image *TintImage(const Image *image,const char *blend,
cristy28474bf2011-09-11 23:32:52 +00005226 const PixelInfo *tint,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005227{
5228#define TintImageTag "Tint/Image"
5229
cristyc4c8d132010-01-07 01:58:38 +00005230 CacheView
5231 *image_view,
5232 *tint_view;
5233
cristy3ed852e2009-09-05 21:47:34 +00005234 GeometryInfo
5235 geometry_info;
5236
5237 Image
5238 *tint_image;
5239
cristy3ed852e2009-09-05 21:47:34 +00005240 MagickBooleanType
5241 status;
5242
cristybb503372010-05-27 20:51:26 +00005243 MagickOffsetType
5244 progress;
cristy3ed852e2009-09-05 21:47:34 +00005245
cristy28474bf2011-09-11 23:32:52 +00005246 MagickRealType
5247 intensity;
5248
cristy4c08aed2011-07-01 19:47:50 +00005249 PixelInfo
cristy1707c6c2012-01-18 23:30:54 +00005250 color_vector;
cristy3ed852e2009-09-05 21:47:34 +00005251
cristybb503372010-05-27 20:51:26 +00005252 MagickStatusType
5253 flags;
5254
5255 ssize_t
5256 y;
5257
cristy3ed852e2009-09-05 21:47:34 +00005258 /*
5259 Allocate tint image.
5260 */
5261 assert(image != (const Image *) NULL);
5262 assert(image->signature == MagickSignature);
5263 if (image->debug != MagickFalse)
5264 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5265 assert(exception != (ExceptionInfo *) NULL);
5266 assert(exception->signature == MagickSignature);
5267 tint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
5268 if (tint_image == (Image *) NULL)
5269 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005270 if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005271 {
cristy3ed852e2009-09-05 21:47:34 +00005272 tint_image=DestroyImage(tint_image);
5273 return((Image *) NULL);
5274 }
cristyaed9c382011-10-03 17:54:21 +00005275 if (blend == (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005276 return(tint_image);
5277 /*
5278 Determine RGB values of the color.
5279 */
cristy1707c6c2012-01-18 23:30:54 +00005280 GetPixelInfo(image,&color_vector);
cristyb817c3f2011-10-03 14:00:35 +00005281 flags=ParseGeometry(blend,&geometry_info);
cristy1707c6c2012-01-18 23:30:54 +00005282 color_vector.red=geometry_info.rho;
5283 color_vector.green=geometry_info.rho;
5284 color_vector.blue=geometry_info.rho;
5285 color_vector.alpha=OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00005286 if ((flags & SigmaValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005287 color_vector.green=geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +00005288 if ((flags & XiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005289 color_vector.blue=geometry_info.xi;
cristyb817c3f2011-10-03 14:00:35 +00005290 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005291 color_vector.alpha=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005292 if (image->colorspace == CMYKColorspace)
5293 {
cristy1707c6c2012-01-18 23:30:54 +00005294 color_vector.black=geometry_info.rho;
cristy76f512e2011-09-12 01:26:56 +00005295 if ((flags & PsiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005296 color_vector.black=geometry_info.psi;
cristy76f512e2011-09-12 01:26:56 +00005297 if ((flags & ChiValue) != 0)
cristy1707c6c2012-01-18 23:30:54 +00005298 color_vector.alpha=geometry_info.chi;
cristy76f512e2011-09-12 01:26:56 +00005299 }
cristy28474bf2011-09-11 23:32:52 +00005300 intensity=(MagickRealType) GetPixelInfoIntensity(tint);
cristy1707c6c2012-01-18 23:30:54 +00005301 color_vector.red=(MagickRealType) (color_vector.red*tint->red/100.0-
5302 intensity);
5303 color_vector.green=(MagickRealType) (color_vector.green*tint->green/100.0-
5304 intensity);
5305 color_vector.blue=(MagickRealType) (color_vector.blue*tint->blue/100.0-
5306 intensity);
5307 color_vector.black=(MagickRealType) (color_vector.black*tint->black/100.0-
5308 intensity);
5309 color_vector.alpha=(MagickRealType) (color_vector.alpha*tint->alpha/100.0-
5310 intensity);
cristy3ed852e2009-09-05 21:47:34 +00005311 /*
5312 Tint image.
5313 */
5314 status=MagickTrue;
5315 progress=0;
5316 image_view=AcquireCacheView(image);
5317 tint_view=AcquireCacheView(tint_image);
cristyb5d5f722009-11-04 03:03:49 +00005318#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00005319 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005320#endif
cristybb503372010-05-27 20:51:26 +00005321 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005322 {
cristy4c08aed2011-07-01 19:47:50 +00005323 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00005324 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00005325
cristy4c08aed2011-07-01 19:47:50 +00005326 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005327 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005328
cristy6b91acb2011-04-19 12:23:54 +00005329 register ssize_t
5330 x;
5331
cristy3ed852e2009-09-05 21:47:34 +00005332 if (status == MagickFalse)
5333 continue;
5334 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
5335 q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
5336 exception);
cristy4c08aed2011-07-01 19:47:50 +00005337 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00005338 {
5339 status=MagickFalse;
5340 continue;
5341 }
cristybb503372010-05-27 20:51:26 +00005342 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005343 {
cristy4c08aed2011-07-01 19:47:50 +00005344 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00005345 pixel;
5346
5347 MagickRealType
5348 weight;
5349
cristy1707c6c2012-01-18 23:30:54 +00005350 register ssize_t
5351 i;
5352
5353 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
5354 {
5355 PixelChannel
5356 channel;
5357
5358 PixelTrait
5359 tint_traits,
5360 traits;
5361
5362 channel=GetPixelChannelMapChannel(image,i);
5363 traits=GetPixelChannelMapTraits(image,channel);
5364 tint_traits=GetPixelChannelMapTraits(tint_image,channel);
5365 if ((traits == UndefinedPixelTrait) ||
5366 (tint_traits == UndefinedPixelTrait))
5367 continue;
5368 if ((tint_traits & CopyPixelTrait) != 0)
5369 {
5370 SetPixelChannel(tint_image,channel,p[i],q);
5371 continue;
5372 }
5373 }
5374 GetPixelInfo(image,&pixel);
5375 weight=QuantumScale*GetPixelRed(image,p)-0.5;
5376 pixel.red=(MagickRealType) GetPixelRed(image,p)+color_vector.red*
5377 (1.0-(4.0*(weight*weight)));
5378 weight=QuantumScale*GetPixelGreen(image,p)-0.5;
5379 pixel.green=(MagickRealType) GetPixelGreen(image,p)+color_vector.green*
5380 (1.0-(4.0*(weight*weight)));
5381 weight=QuantumScale*GetPixelBlue(image,p)-0.5;
5382 pixel.blue=(MagickRealType) GetPixelBlue(image,p)+color_vector.blue*
5383 (1.0-(4.0*(weight*weight)));
5384 weight=QuantumScale*GetPixelBlack(image,p)-0.5;
5385 pixel.black=(MagickRealType) GetPixelBlack(image,p)+color_vector.black*
5386 (1.0-(4.0*(weight*weight)));
5387 SetPixelInfoPixel(tint_image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00005388 p+=GetPixelChannels(image);
5389 q+=GetPixelChannels(tint_image);
cristy3ed852e2009-09-05 21:47:34 +00005390 }
5391 if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
5392 status=MagickFalse;
5393 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5394 {
5395 MagickBooleanType
5396 proceed;
5397
cristyb5d5f722009-11-04 03:03:49 +00005398#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005399 #pragma omp critical (MagickCore_TintImage)
5400#endif
5401 proceed=SetImageProgress(image,TintImageTag,progress++,image->rows);
5402 if (proceed == MagickFalse)
5403 status=MagickFalse;
5404 }
5405 }
5406 tint_view=DestroyCacheView(tint_view);
5407 image_view=DestroyCacheView(image_view);
5408 if (status == MagickFalse)
5409 tint_image=DestroyImage(tint_image);
5410 return(tint_image);
5411}
5412
5413/*
5414%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5415% %
5416% %
5417% %
5418% V i g n e t t e I m a g e %
5419% %
5420% %
5421% %
5422%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5423%
5424% VignetteImage() softens the edges of the image in vignette style.
5425%
5426% The format of the VignetteImage method is:
5427%
5428% Image *VignetteImage(const Image *image,const double radius,
cristyeb6e6582011-12-09 09:14:23 +00005429% const double sigma,const double bias,const ssize_t x,const ssize_t y,
cristy05c0c9a2011-09-05 23:16:13 +00005430% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005431%
5432% A description of each parameter follows:
5433%
5434% o image: the image.
5435%
5436% o radius: the radius of the pixel neighborhood.
5437%
5438% o sigma: the standard deviation of the Gaussian, in pixels.
5439%
cristyeb6e6582011-12-09 09:14:23 +00005440% o bias: the bias.
5441%
cristy3ed852e2009-09-05 21:47:34 +00005442% o x, y: Define the x and y ellipse offset.
5443%
5444% o exception: return any errors or warnings in this structure.
5445%
5446*/
5447MagickExport Image *VignetteImage(const Image *image,const double radius,
cristyeb6e6582011-12-09 09:14:23 +00005448 const double sigma,const double bias,const ssize_t x,const ssize_t y,
5449 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005450{
5451 char
5452 ellipse[MaxTextExtent];
5453
5454 DrawInfo
5455 *draw_info;
5456
5457 Image
5458 *canvas_image,
5459 *blur_image,
5460 *oval_image,
5461 *vignette_image;
5462
5463 assert(image != (Image *) NULL);
5464 assert(image->signature == MagickSignature);
5465 if (image->debug != MagickFalse)
5466 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5467 assert(exception != (ExceptionInfo *) NULL);
5468 assert(exception->signature == MagickSignature);
5469 canvas_image=CloneImage(image,0,0,MagickTrue,exception);
5470 if (canvas_image == (Image *) NULL)
5471 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005472 if (SetImageStorageClass(canvas_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005473 {
cristy3ed852e2009-09-05 21:47:34 +00005474 canvas_image=DestroyImage(canvas_image);
5475 return((Image *) NULL);
5476 }
5477 canvas_image->matte=MagickTrue;
cristy98621462011-12-31 22:31:11 +00005478 oval_image=CloneImage(canvas_image,canvas_image->columns,canvas_image->rows,
5479 MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00005480 if (oval_image == (Image *) NULL)
5481 {
5482 canvas_image=DestroyImage(canvas_image);
5483 return((Image *) NULL);
5484 }
cristy9950d572011-10-01 18:22:35 +00005485 (void) QueryColorCompliance("#000000",AllCompliance,
5486 &oval_image->background_color,exception);
cristyea1a8aa2011-10-20 13:24:06 +00005487 (void) SetImageBackgroundColor(oval_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00005488 draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
cristy9950d572011-10-01 18:22:35 +00005489 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
5490 exception);
5491 (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
5492 exception);
cristy1707c6c2012-01-18 23:30:54 +00005493 (void) FormatLocaleString(ellipse,MaxTextExtent,"ellipse %g,%g,%g,%g,"
5494 "0.0,360.0",image->columns/2.0,image->rows/2.0,image->columns/2.0-x,
5495 image->rows/2.0-y);
cristy3ed852e2009-09-05 21:47:34 +00005496 draw_info->primitive=AcquireString(ellipse);
cristy018f07f2011-09-04 21:15:19 +00005497 (void) DrawImage(oval_image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00005498 draw_info=DestroyDrawInfo(draw_info);
cristyeb6e6582011-12-09 09:14:23 +00005499 blur_image=BlurImage(oval_image,radius,sigma,bias,exception);
cristy3ed852e2009-09-05 21:47:34 +00005500 oval_image=DestroyImage(oval_image);
5501 if (blur_image == (Image *) NULL)
5502 {
5503 canvas_image=DestroyImage(canvas_image);
5504 return((Image *) NULL);
5505 }
5506 blur_image->matte=MagickFalse;
cristy98621462011-12-31 22:31:11 +00005507 (void) CompositeImage(canvas_image,IntensityCompositeOp,blur_image,0,0,
cristye941a752011-10-15 01:52:48 +00005508 exception);
cristy3ed852e2009-09-05 21:47:34 +00005509 blur_image=DestroyImage(blur_image);
5510 vignette_image=MergeImageLayers(canvas_image,FlattenLayer,exception);
5511 canvas_image=DestroyImage(canvas_image);
5512 return(vignette_image);
5513}
5514
5515/*
5516%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5517% %
5518% %
5519% %
5520% W a v e I m a g e %
5521% %
5522% %
5523% %
5524%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5525%
5526% WaveImage() creates a "ripple" effect in the image by shifting the pixels
cristycee97112010-05-28 00:44:52 +00005527% vertically along a sine wave whose amplitude and wavelength is specified
cristy3ed852e2009-09-05 21:47:34 +00005528% by the given parameters.
5529%
5530% The format of the WaveImage method is:
5531%
5532% Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005533% const double wave_length,const PixelInterpolateMethod method,
5534% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005535%
5536% A description of each parameter follows:
5537%
5538% o image: the image.
5539%
5540% o amplitude, wave_length: Define the amplitude and wave length of the
5541% sine wave.
5542%
cristy5c4e2582011-09-11 19:21:03 +00005543% o interpolate: the pixel interpolation method.
5544%
cristy3ed852e2009-09-05 21:47:34 +00005545% o exception: return any errors or warnings in this structure.
5546%
5547*/
5548MagickExport Image *WaveImage(const Image *image,const double amplitude,
cristy5c4e2582011-09-11 19:21:03 +00005549 const double wave_length,const PixelInterpolateMethod method,
5550 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00005551{
5552#define WaveImageTag "Wave/Image"
5553
cristyfa112112010-01-04 17:48:07 +00005554 CacheView
cristyd76c51e2011-03-26 00:21:26 +00005555 *image_view,
cristyfa112112010-01-04 17:48:07 +00005556 *wave_view;
5557
cristy3ed852e2009-09-05 21:47:34 +00005558 Image
5559 *wave_image;
5560
cristy3ed852e2009-09-05 21:47:34 +00005561 MagickBooleanType
5562 status;
5563
cristybb503372010-05-27 20:51:26 +00005564 MagickOffsetType
5565 progress;
5566
cristy3ed852e2009-09-05 21:47:34 +00005567 MagickRealType
5568 *sine_map;
5569
cristybb503372010-05-27 20:51:26 +00005570 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005571 i;
5572
cristybb503372010-05-27 20:51:26 +00005573 ssize_t
5574 y;
5575
cristy3ed852e2009-09-05 21:47:34 +00005576 /*
5577 Initialize wave image attributes.
5578 */
5579 assert(image != (Image *) NULL);
5580 assert(image->signature == MagickSignature);
5581 if (image->debug != MagickFalse)
5582 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5583 assert(exception != (ExceptionInfo *) NULL);
5584 assert(exception->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +00005585 wave_image=CloneImage(image,image->columns,(size_t) (image->rows+2.0*
cristy3ed852e2009-09-05 21:47:34 +00005586 fabs(amplitude)),MagickTrue,exception);
5587 if (wave_image == (Image *) NULL)
5588 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +00005589 if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00005590 {
cristy3ed852e2009-09-05 21:47:34 +00005591 wave_image=DestroyImage(wave_image);
5592 return((Image *) NULL);
5593 }
cristy4c08aed2011-07-01 19:47:50 +00005594 if (wave_image->background_color.alpha != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00005595 wave_image->matte=MagickTrue;
5596 /*
5597 Allocate sine map.
5598 */
5599 sine_map=(MagickRealType *) AcquireQuantumMemory((size_t) wave_image->columns,
5600 sizeof(*sine_map));
5601 if (sine_map == (MagickRealType *) NULL)
5602 {
5603 wave_image=DestroyImage(wave_image);
5604 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
5605 }
cristybb503372010-05-27 20:51:26 +00005606 for (i=0; i < (ssize_t) wave_image->columns; i++)
cristy4205a3c2010-09-12 20:19:59 +00005607 sine_map[i]=fabs(amplitude)+amplitude*sin((double) ((2.0*MagickPI*i)/
5608 wave_length));
cristy3ed852e2009-09-05 21:47:34 +00005609 /*
5610 Wave image.
5611 */
5612 status=MagickTrue;
5613 progress=0;
cristyd76c51e2011-03-26 00:21:26 +00005614 image_view=AcquireCacheView(image);
cristy3ed852e2009-09-05 21:47:34 +00005615 wave_view=AcquireCacheView(wave_image);
cristyd76c51e2011-03-26 00:21:26 +00005616 (void) SetCacheViewVirtualPixelMethod(image_view,
5617 BackgroundVirtualPixelMethod);
cristyb5d5f722009-11-04 03:03:49 +00005618#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00005619 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00005620#endif
cristybb503372010-05-27 20:51:26 +00005621 for (y=0; y < (ssize_t) wave_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00005622 {
cristy4c08aed2011-07-01 19:47:50 +00005623 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00005624 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00005625
cristye97bb922011-04-03 01:36:52 +00005626 register ssize_t
5627 x;
5628
cristy3ed852e2009-09-05 21:47:34 +00005629 if (status == MagickFalse)
5630 continue;
5631 q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
5632 exception);
cristyacd2ed22011-08-30 01:44:23 +00005633 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005634 {
5635 status=MagickFalse;
5636 continue;
5637 }
cristybb503372010-05-27 20:51:26 +00005638 for (x=0; x < (ssize_t) wave_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00005639 {
cristy5c4e2582011-09-11 19:21:03 +00005640 status=InterpolatePixelChannels(image,image_view,wave_image,method,
5641 (double) x,(double) (y-sine_map[x]),q,exception);
cristyed231572011-07-14 02:18:59 +00005642 q+=GetPixelChannels(wave_image);
cristy3ed852e2009-09-05 21:47:34 +00005643 }
5644 if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
5645 status=MagickFalse;
5646 if (image->progress_monitor != (MagickProgressMonitor) NULL)
5647 {
5648 MagickBooleanType
5649 proceed;
5650
cristyb5d5f722009-11-04 03:03:49 +00005651#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00005652 #pragma omp critical (MagickCore_WaveImage)
5653#endif
5654 proceed=SetImageProgress(image,WaveImageTag,progress++,image->rows);
5655 if (proceed == MagickFalse)
5656 status=MagickFalse;
5657 }
5658 }
5659 wave_view=DestroyCacheView(wave_view);
cristyd76c51e2011-03-26 00:21:26 +00005660 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00005661 sine_map=(MagickRealType *) RelinquishMagickMemory(sine_map);
5662 if (status == MagickFalse)
5663 wave_image=DestroyImage(wave_image);
5664 return(wave_image);
5665}